Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Examples

resolv.c

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

Generated on Thu Jun 22 17:45:42 2006 for Contiki 2.x by  doxygen 1.4.4