- Main Page
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

# apps/resolv/resolv.c

Go to the documentation of this file.

    00001 /**
    00002  * \addtogroup apps
    00003  * @{
    00004  */
    00005
    00006 /**
    00007  * \defgroup resolv DNS resolver
    00008  * @{
    00009  *
    00010  * The uIP DNS resolver functions are used to lookup a hostname and
    00011  * map it to a numerical IP address. It maintains a list of resolved
    00012  * hostnames that can be queried with the resolv_lookup()
    00013  * function. New hostnames can be resolved using the resolv_query()
    00014  * function.
    00015  *
    00016  * When a hostname has been resolved (or found to be non-existant),
    00017  * the resolver code calls a callback function called resolv_found()
    00018  * that must be implemented by the module that uses the resolver.
    00019  */
    00020
    00021 /**
    00022  * \file
    00023  * DNS host name to IP address resolver.
    00024  * \author Adam Dunkels <adam@dunkels.com>
    00025  *
    00026  * This file implements a DNS host name to IP address resolver.
    00027  */
    00028
    00029 /*
    00030  * Copyright (c) 2002-2003, Adam Dunkels.
    00031  * All rights reserved.
    00032  *
    00033  * Redistribution and use in source and binary forms, with or without
    00034  * modification, are permitted provided that the following conditions
    00035  * are met:
    00036  * 1. Redistributions of source code must retain the above copyright
    00037  *    notice, this list of conditions and the following disclaimer.
    00038  * 2. Redistributions in binary form must reproduce the above copyright
    00039  *    notice, this list of conditions and the following disclaimer in the
    00040  *    documentation and/or other materials provided with the distribution.
    00041  * 3. The name of the author may not be used to endorse or promote
    00042  *    products derived from this software without specific prior
    00043  *    written permission.
    00044  *
    00045  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
    00046  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    00047  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00048  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
    00049  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00050  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    00051  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    00052  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    00053  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00054  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00055  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00056  *
    00057  * This file is part of the uIP TCP/IP stack.
    00058  *
    00059  * $Id: resolv.c,v 1.5 2006/06/11 21:46:37 adam Exp $
    00060  *
    00061  */
    00062
    00063 #include "resolv.h"
    00064 #include "uip.h"
    00065
    00066 #include <string.h>
    00067
    00068 #ifndef NULL
    00069 #define NULL (void *)0
    00070 #endif /* NULL */
    00071
    00072 /** \internal The maximum number of retries when asking for a name. */
    00073 #define MAX_RETRIES 8
    00074
    00075 /** \internal The DNS message header. */
    00076 struct dns_hdr {
    00077   u16_t id;
    00078   u8_t flags1, flags2;
    00079 #define DNS_FLAG1_RESPONSE        0x80
    00080 #define DNS_FLAG1_OPCODE_STATUS   0x10
    00081 #define DNS_FLAG1_OPCODE_INVERSE  0x08
    00082 #define DNS_FLAG1_OPCODE_STANDARD 0x00
    00083 #define DNS_FLAG1_AUTHORATIVE     0x04
    00084 #define DNS_FLAG1_TRUNC           0x02
    00085 #define DNS_FLAG1_RD              0x01
    00086 #define DNS_FLAG2_RA              0x80
    00087 #define DNS_FLAG2_ERR_MASK        0x0f
    00088 #define DNS_FLAG2_ERR_NONE        0x00
    00089 #define DNS_FLAG2_ERR_NAME        0x03
    00090   u16_t numquestions;
    00091   u16_t numanswers;
    00092   u16_t numauthrr;
    00093   u16_t numextrarr;
    00094 };
    00095
    00096 /** \internal The DNS answer message structure. */
    00097 struct dns_answer {
    00098   /* DNS answer record starts with either a domain name or a pointer
    00099      to a name already present somewhere in the packet. */
    00100   u16_t type;
    00101   u16_t class;
    00102   u16_t ttl[2];
    00103   u16_t len;
    00104   uip_ipaddr_t ipaddr;
    00105 };
    00106
    00107 struct namemap {
    00108 #define STATE_UNUSED 0
    00109 #define STATE_NEW    1
    00110 #define STATE_ASKING 2
    00111 #define STATE_DONE   3
    00112 #define STATE_ERROR  4
    00113   u8_t state;
    00114   u8_t tmr;
    00115   u8_t retries;
    00116   u8_t seqno;
    00117   u8_t err;
    00118   char name[32];
    00119   uip_ipaddr_t ipaddr;
    00120 };
    00121
    00122 #ifndef UIP_CONF_RESOLV_ENTRIES
    00123 #define RESOLV_ENTRIES 4
    00124 #else /* UIP_CONF_RESOLV_ENTRIES */
    00125 #define RESOLV_ENTRIES UIP_CONF_RESOLV_ENTRIES
    00126 #endif /* UIP_CONF_RESOLV_ENTRIES */
    00127
    00128
    00129 static struct namemap names[RESOLV_ENTRIES];
    00130
    00131 static u8_t seqno;
    00132
    00133 static struct uip_udp_conn *resolv_conn = NULL;
    00134
    00135
    00136 /*---------------------------------------------------------------------------*/
    00137 /** \internal
    00138  * Walk through a compact encoded DNS name and return the end of it.
    00139  *
    00140  * \return The end of the name.
    00141  */
    00142 /*---------------------------------------------------------------------------*/
    00143 static unsigned char *
    00144 parse_name(unsigned char *query)
    00145 {
    00146   unsigned char n;
    00147
    00148   do {
    00149     n = *query++;
    00150
    00151     while(n > 0) {
    00152       /*      printf("%c", *query);*/
    00153       ++query;
    00154       --n;
    00155     };
    00156     /*    printf(".");*/
    00157   } while(*query != 0);
    00158   /*  printf("\n");*/
    00159   return query + 1;
    00160 }
    00161 /*---------------------------------------------------------------------------*/
    00162 /** \internal
    00163  * Runs through the list of names to see if there are any that have
    00164  * not yet been queried and, if so, sends out a query.
    00165  */
    00166 /*---------------------------------------------------------------------------*/
    00167 static void
    00168 check_entries(void)
    00169 {
    00170   register struct dns_hdr *hdr;
    00171   char *query, *nptr, *nameptr;
    00172   static u8_t i;
    00173   static u8_t n;
    00174   register struct namemap *namemapptr;
    00175
    00176   for(i = 0; i < RESOLV_ENTRIES; ++i) {
    00177     namemapptr = &names[i];
    00178     if(namemapptr->state == STATE_NEW ||
    00179        namemapptr->state == STATE_ASKING) {
    00180       if(namemapptr->state == STATE_ASKING) {
    00181         if(--namemapptr->tmr == 0) {
    00182           if(++namemapptr->retries == MAX_RETRIES) {
    00183             namemapptr->state = STATE_ERROR;
    00184             resolv_found(namemapptr->name, NULL);
    00185             continue;
    00186           }
    00187           namemapptr->tmr = namemapptr->retries;
    00188         } else {
    00189           /*      printf("Timer %d\n", namemapptr->tmr);*/
    00190           /* Its timer has not run out, so we move on to next
    00191              entry. */
    00192           continue;
    00193         }
    00194       } else {
    00195         namemapptr->state = STATE_ASKING;
    00196         namemapptr->tmr = 1;
    00197         namemapptr->retries = 0;
    00198       }
    00199       hdr = (struct dns_hdr *)uip_appdata;
    00200       memset(hdr, 0, sizeof(struct dns_hdr));
    00201       hdr->id = htons(i);
    00202       hdr->flags1 = DNS_FLAG1_RD;
    00203       hdr->numquestions = HTONS(1);
    00204       query = (char *)uip_appdata + 12;
    00205       nameptr = namemapptr->name;
    00206       --nameptr;
    00207       /* Convert hostname into suitable query format. */
    00208       do {
    00209         ++nameptr;
    00210         nptr = query;
    00211         ++query;
    00212         for(n = 0; *nameptr != '.' && *nameptr != 0; ++nameptr) {
    00213           *query = *nameptr;
    00214           ++query;
    00215           ++n;
    00216         }
    00217         *nptr = n;
    00218       } while(*nameptr != 0);
    00219       {
    00220         static unsigned char endquery[] =
    00221           {0,0,1,0,1};
    00222         memcpy(query, endquery, 5);
    00223       }
    00224       uip_udp_send((unsigned char)(query + 5 - (char *)uip_appdata));
    00225       break;
    00226     }
    00227   }
    00228 }
    00229 /*---------------------------------------------------------------------------*/
    00230 /** \internal
    00231  * Called when new UDP data arrives.
    00232  */
    00233 /*---------------------------------------------------------------------------*/
    00234 static void
    00235 newdata(void)
    00236 {
    00237   char *nameptr;
    00238   struct dns_answer *ans;
    00239   struct dns_hdr *hdr;
    00240   static u8_t nquestions, nanswers;
    00241   static u8_t i;
    00242   register struct namemap *namemapptr;
    00243
    00244   hdr = (struct dns_hdr *)uip_appdata;
    00245   /*  printf("ID %d\n", htons(hdr->id));
    00246       printf("Query %d\n", hdr->flags1 & DNS_FLAG1_RESPONSE);
    00247       printf("Error %d\n", hdr->flags2 & DNS_FLAG2_ERR_MASK);
    00248       printf("Num questions %d, answers %d, authrr %d, extrarr %d\n",
    00249       htons(hdr->numquestions),
    00250       htons(hdr->numanswers),
    00251       htons(hdr->numauthrr),
    00252       htons(hdr->numextrarr));
    00253   */
    00254
    00255   /* The ID in the DNS header should be our entry into the name
    00256      table. */
    00257   i = htons(hdr->id);
    00258   namemapptr = &names[i];
    00259   if(i < RESOLV_ENTRIES &&
    00260      namemapptr->state == STATE_ASKING) {
    00261
    00262     /* This entry is now finished. */
    00263     namemapptr->state = STATE_DONE;
    00264     namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
    00265
    00266     /* Check for error. If so, call callback to inform. */
    00267     if(namemapptr->err != 0) {
    00268       namemapptr->state = STATE_ERROR;
    00269       resolv_found(namemapptr->name, NULL);
    00270       return;
    00271     }
    00272
    00273     /* We only care about the question(s) and the answers. The authrr
    00274        and the extrarr are simply discarded. */
    00275     nquestions = htons(hdr->numquestions);
    00276     nanswers = htons(hdr->numanswers);
    00277
    00278     /* Skip the name in the question. XXX: This should really be
    00279        checked agains the name in the question, to be sure that they
    00280        match. */
    00281     nameptr = parse_name((char *)uip_appdata + 12) + 4;
    00282
    00283     while(nanswers > 0) {
    00284       /* The first byte in the answer resource record determines if it
    00285          is a compressed record or a normal one. */
    00286       if(*nameptr & 0xc0) {
    00287         /* Compressed name. */
    00288         nameptr +=2;
    00289         /*      printf("Compressed anwser\n");*/
    00290       } else {
    00291         /* Not compressed name. */
    00292         nameptr = parse_name((char *)nameptr);
    00293       }
    00294
    00295       ans = (struct dns_answer *)nameptr;
    00296       /*      printf("Answer: type %x, class %x, ttl %x, length %x\n",
    00297              htons(ans->type), htons(ans->class), (htons(ans->ttl[0])
    00298              << 16) | htons(ans->ttl[1]), htons(ans->len));*/
    00299
    00300       /* Check for IP address type and Internet class. Others are
    00301          discarded. */
    00302       if(ans->type == HTONS(1) &&
    00303          ans->class == HTONS(1) &&
    00304          ans->len == HTONS(4)) {
    00305         /*      printf("IP address %d.%d.%d.%d\n",
    00306                htons(ans->ipaddr[0]) >> 8,
    00307                htons(ans->ipaddr[0]) & 0xff,
    00308                htons(ans->ipaddr[1]) >> 8,
    00309                htons(ans->ipaddr[1]) & 0xff);*/
    00310         /* XXX: we should really check that this IP address is the one
    00311            we want. */
    00312         namemapptr->ipaddr[0] = ans->ipaddr[0];
    00313         namemapptr->ipaddr[1] = ans->ipaddr[1];
    00314
    00315         resolv_found(namemapptr->name, namemapptr->ipaddr);
    00316         return;
    00317       } else {
    00318         nameptr = nameptr + 10 + htons(ans->len);
    00319       }
    00320       --nanswers;
    00321     }
    00322   }
    00323
    00324 }
    00325 /*---------------------------------------------------------------------------*/
    00326 /** \internal
    00327  * The main UDP function.
    00328  */
    00329 /*---------------------------------------------------------------------------*/
    00330 void
    00331 resolv_appcall(void)
    00332 {
    00333   if(uip_udp_conn->rport == HTONS(53)) {
    00334     if(uip_poll()) {
    00335       check_entries();
    00336     }
    00337     if(uip_newdata()) {
    00338       newdata();
    00339     }
    00340   }
    00341 }
    00342 /*---------------------------------------------------------------------------*/
    00343 /**
    00344  * Queues a name so that a question for the name will be sent out.
    00345  *
    00346  * \param name The hostname that is to be queried.
    00347  */
    00348 /*---------------------------------------------------------------------------*/
    00349 void
    00350 resolv_query(char *name)
    00351 {
    00352   static u8_t i;
    00353   static u8_t lseq, lseqi;
    00354   register struct namemap *nameptr;
    00355
    00356   lseq = lseqi = 0;
    00357
    00358   for(i = 0; i < RESOLV_ENTRIES; ++i) {
    00359     nameptr = &names[i];
    00360     if(nameptr->state == STATE_UNUSED) {
    00361       break;
    00362     }
    00363     if(seqno - nameptr->seqno > lseq) {
    00364       lseq = seqno - nameptr->seqno;
    00365       lseqi = i;
    00366     }
    00367   }
    00368
    00369   if(i == RESOLV_ENTRIES) {
    00370     i = lseqi;
    00371     nameptr = &names[i];
    00372   }
    00373
    00374   /*  printf("Using entry %d\n", i);*/
    00375
    00376   strcpy(nameptr->name, name);
    00377   nameptr->state = STATE_NEW;
    00378   nameptr->seqno = seqno;
    00379   ++seqno;
    00380 }
    00381 /*---------------------------------------------------------------------------*/
    00382 /**
    00383  * Look up a hostname in the array of known hostnames.
    00384  *
    00385  * \note This function only looks in the internal array of known
    00386  * hostnames, it does not send out a query for the hostname if none
    00387  * was found. The function resolv_query() can be used to send a query
    00388  * for a hostname.
    00389  *
    00390  * \return A pointer to a 4-byte representation of the hostname's IP
    00391  * address, or NULL if the hostname was not found in the array of
    00392  * hostnames.
    00393  */
    00394 /*---------------------------------------------------------------------------*/
    00395 u16_t *
    00396 resolv_lookup(char *name)
    00397 {
    00398   static u8_t i;
    00399   struct namemap *nameptr;
    00400
    00401   /* Walk through the list to see if the name is in there. If it is
    00402      not, we return NULL. */
    00403   for(i = 0; i < RESOLV_ENTRIES; ++i) {
    00404     nameptr = &names[i];
    00405     if(nameptr->state == STATE_DONE &&
    00406        strcmp(name, nameptr->name) == 0) {
    00407       return nameptr->ipaddr;
    00408     }
    00409   }
    00410   return NULL;
    00411 }
    00412 /*---------------------------------------------------------------------------*/
    00413 /**
    00414  * Obtain the currently configured DNS server.
    00415  *
    00416  * \return A pointer to a 4-byte representation of the IP address of
    00417  * the currently configured DNS server or NULL if no DNS server has
    00418  * been configured.
    00419  */
    00420 /*---------------------------------------------------------------------------*/
    00421 u16_t *
    00422 resolv_getserver(void)
    00423 {
    00424   if(resolv_conn == NULL) {
    00425     return NULL;
    00426   }
    00427   return resolv_conn->ripaddr;
    00428 }
    00429 /*---------------------------------------------------------------------------*/
    00430 /**
    00431  * Configure which DNS server to use for queries.
    00432  *
    00433  * \param dnsserver A pointer to a 4-byte representation of the IP
    00434  * address of the DNS server to be configured.
    00435  */
    00436 /*---------------------------------------------------------------------------*/
    00437 void
    00438 resolv_conf(u16_t *dnsserver)
    00439 {
    00440   if(resolv_conn != NULL) {
    00441     uip_udp_remove(resolv_conn);
    00442   }
    00443
    00444   resolv_conn = uip_udp_new(dnsserver, HTONS(53));
    00445 }
    00446 /*---------------------------------------------------------------------------*/
    00447 /**
    00448  * Initalize the resolver.
    00449  */
    00450 /*---------------------------------------------------------------------------*/
    00451 void
    00452 resolv_init(void)
    00453 {
    00454   static u8_t i;
    00455
    00456   for(i = 0; i < RESOLV_ENTRIES; ++i) {
    00457     names[i].state = STATE_DONE;
    00458   }
    00459
    00460 }
    00461 /*---------------------------------------------------------------------------*/
    00462
    00463 /** @} */
    00464 /** @} */