LWIP之DNS域名解析分析 (一)

来源:互联网 发布:mac os x 10.11.6 u盘 编辑:程序博客网 时间:2024/05/17 20:34

原创声明: 本文分析属于 tiandimz 所有,转载请注明出处!谢谢!

本文源码版本为 LWIP 1.4.0

源码下载地址

下面的代码是 dns.c 的全部代码!主要完成ipv4的客户端域名解析部分,不涉及复杂的服务器端的处理过程。

下面进行简要分析,由于LWIP是轻量级的IP协议栈,所以对于DNS的要求也很简单,主要是针对客户端测的实现,能够提供API接口,通过调用接口可以实现DNS查询请求,并能够解析DNS响应,并建立本地缓存!

主要涉及的文件有   dns.c   dns.h    netdb.c   netdb.h   ip_addr.h    ip_addr.c

对于复杂的域名解析服务器的程序,可以参考  bind 9.

dns报文格式请见:

DNS报文格式



#include "lwip/opt.h"    //LWIP的配置选项文件#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */#include "lwip/udp.h"#include "lwip/mem.h"#include "lwip/memp.h"#include "lwip/dns.h"  //dns的头文件#include <string.h>/** DNS server IP address     opendns  是一个开放dns服务,我们的请求也是发往这个服务器地址!*/#ifndef DNS_SERVER_ADDRESS#define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */#endif/** DNS server port address */#ifndef DNS_SERVER_PORT#define DNS_SERVER_PORT           53    //  DNS通过 udp承载,主要利用  53端口#endif/** DNS maximum number of retries when asking for a name, before "timeout". */#ifndef DNS_MAX_RETRIES#define DNS_MAX_RETRIES           4    //dns请求超时的重试最大次数   4次#endif



/** DNS resource record max. TTL (one week as default) */#ifndef DNS_MAX_TTL#define DNS_MAX_TTL               604800    //源记录的最大生存时间#endif/* DNS protocol flags         DNS的标记位  */#define DNS_FLAG1_RESPONSE        0x80#define DNS_FLAG1_OPCODE_STATUS   0x10#define DNS_FLAG1_OPCODE_INVERSE  0x08#define DNS_FLAG1_OPCODE_STANDARD 0x00#define DNS_FLAG1_AUTHORATIVE     0x04#define DNS_FLAG1_TRUNC           0x02#define DNS_FLAG1_RD              0x01#define DNS_FLAG2_RA              0x80#define DNS_FLAG2_ERR_MASK        0x0f#define DNS_FLAG2_ERR_NONE        0x00#define DNS_FLAG2_ERR_NAME        0x03/* DNS protocol states */#define DNS_STATE_UNUSED          0#define DNS_STATE_NEW             1#define DNS_STATE_ASKING          2#define DNS_STATE_DONE            3#ifdef PACK_STRUCT_USE_INCLUDES#  include "arch/bpstruct.h"#endifPACK_STRUCT_BEGIN/** DNS message header      DNS的消息头部  根据DNS的RFC协议 1035 描述进行设计的,采用结构体和位域来实现*/struct dns_hdr {  PACK_STRUCT_FIELD(u16_t id);  PACK_STRUCT_FIELD(u8_t flags1);  PACK_STRUCT_FIELD(u8_t flags2);  PACK_STRUCT_FIELD(u16_t numquestions);  PACK_STRUCT_FIELD(u16_t numanswers);  PACK_STRUCT_FIELD(u16_t numauthrr);  PACK_STRUCT_FIELD(u16_t numextrarr);} PACK_STRUCT_STRUCT;PACK_STRUCT_END#ifdef PACK_STRUCT_USE_INCLUDES#  include "arch/epstruct.h"#endif#define SIZEOF_DNS_HDR 12/** DNS query message structure.    No packing needed: only used locally on the stack.   dns消息查询结构体          */struct dns_query {  /* DNS query record starts with either a domain name or a pointer     to a name already present somewhere in the packet. */  u16_t type;  u16_t cls;};#define SIZEOF_DNS_QUERY 4




/** DNS answer message structure.    No packing needed: only used locally on the stack. DNS应答消息报文格式 */struct dns_answer {  /* DNS answer record starts with either a domain name or a pointer     to a name already present somewhere in the packet. */  u16_t type;  u16_t cls;  u32_t ttl;  u16_t len;};#define SIZEOF_DNS_ANSWER 10/** DNS table entry   DNS的表格 */struct dns_table_entry {  u8_t  state;  u8_t  numdns;  u8_t  tmr;  u8_t  retries;  u8_t  seqno;  u8_t  err;  u32_t ttl;  char name[DNS_MAX_NAME_LENGTH];//DNS_MAX_NAME_LENGTH  256,域名字符串最大长度 256  ip_addr_t ipaddr;  /* pointer to callback on DNS query done     callback  函数指针*/  dns_found_callback found;  void *arg;};#if DNS_LOCAL_HOSTLIST#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC/** Local host-list. For hostnames in this list, no *  external name resolution is performed */static struct local_hostlist_entry *local_hostlist_dynamic;   //本地的主机列表#else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC *//** Defining this allows the local_hostlist_static to be placed in a different * linker section (e.g. FLASH) */#ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE#define DNS_LOCAL_HOSTLIST_STORAGE_PRE static#endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE *//** Defining this allows the local_hostlist_static to be placed in a different * linker section (e.g. FLASH) */#ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST#define DNS_LOCAL_HOSTLIST_STORAGE_POST#endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[]  DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */



static void dns_init_local();   //dns初始化本地列表#endif /* DNS_LOCAL_HOSTLIST *//* forward declarations   函数声明包括:dns接受响应报文处理函数和dns检查本地列表函数*/static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port); static void dns_check_entries(void);/*----------------------------------------------------------------------------- * Globales   一些全局变量 *----------------------------------------------------------------------------*//* DNS variables */static struct udp_pcb        *dns_pcb;  //因为用UDP承载,所有需要在UDP 的pcb中记录static u8_t                   dns_seqno; //static struct dns_table_entry dns_table[DNS_TABLE_SIZE];  //DNS_TABLE_SIZE 为 4 ,只存4个历史域名记录static ip_addr_t              dns_servers[DNS_MAX_SERVERS];  //DNS_MAX_SERVERS为 2 ,主DNS和辅DNS/** Contiguous buffer for processing responses   处理响应的缓存  */static u8_t                   dns_payload_buffer[LWIP_MEM_ALIGN_BUFFER(DNS_MSG_SIZE)];static u8_t*                  dns_payload;/** * Initialize the resolver: set up the UDP pcb and configure the default server * (DNS_SERVER_ADDRESS).初始化解析,建立UDP pcb 配置默认的服务器地址 */voiddns_init(){  ip_addr_t dnsserver;  /*  声明dns服务器地址     */  dns_payload = (u8_t *)LWIP_MEM_ALIGN(dns_payload_buffer);   /* 分配内存并对齐    */    /* initialize default DNS server address */  DNS_SERVER_ADDRESS(&dnsserver);   /* 设置DNS服务器地址   */  LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n"));  /* LWIP的打印LOG语句   */  /* if dns client not yet initialized... */  if (dns_pcb == NULL) {    dns_pcb = udp_new();  /* 创建PCB,由  udp承载 */    if (dns_pcb != NULL) {      /* initialize DNS table not needed (initialized to zero since it is a       * global variable) */      LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",        DNS_STATE_UNUSED == 0);      /* initialize DNS client */      udp_bind(dns_pcb, IP_ADDR_ANY, 0);    /* 绑定udp   */      udp_recv(dns_pcb, dns_recv, NULL);    /* 完成udp_recv与 dns_recv的映射*/      /* initialize default DNS primary server */      dns_setserver(0, &dnsserver);   /* 设置首选 DNS服务器  */    }  }#if DNS_LOCAL_HOSTLIST  dns_init_local();  /* 如果有本地缓存的话,就初始化本地列表  */#endif}/** * Initialize one of the DNS servers. * * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS   索引:DNS服务器数组的索引号,不能大于上限MAX * @param dnsserver IP address of the DNS server to set */voiddns_setserver(u8_t numdns, ip_addr_t *dnsserver){  if ((numdns < DNS_MAX_SERVERS) && (dns_pcb != NULL) &&      (dnsserver != NULL) && !ip_addr_isany(dnsserver)) {    dns_servers[numdns] = (*dnsserver);   /*    根据numdns,设置DNS服务器   */    }}/** * Obtain one of the currently configured DNS server.   读dns配置信息 * * @param numdns the index of the DNS server * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS *         server has not been configured. */ip_addr_tdns_getserver(u8_t numdns)  /* 入参是索引号,如果有该服务器信息,就返回,否则,就返回IP_ADDR_ANY,*/{  if (numdns < DNS_MAX_SERVERS) {    return dns_servers[numdns];  } else {    return *IP_ADDR_ANY;  }}/** * The DNS resolver client timer - handle retries and timeouts and should * be called every DNS_TMR_INTERVAL milliseconds (every second by default). 定时管理与重传管理 */voiddns_tmr(void){  if (dns_pcb != NULL) {    LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n"));    dns_check_entries();  /*  定时管理与重传管理  */  }}



#if DNS_LOCAL_HOSTLIST    /* 如果配置了本地缓存列表,就要有本地的初始化   */static voiddns_init_local(){#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT)   /*  如果是动态列表   */  int i;  struct local_hostlist_entry *entry;  /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */  struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;  size_t namelen;  for (i = 0; i < sizeof(local_hostlist_init) / sizeof(struct local_hostlist_entry); i++) {    struct local_hostlist_entry *init_entry = &local_hostlist_init[i];  /* 把本地列表初始化的表项遍历读出来,存到缓存里   */    LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL);    namelen = strlen(init_entry->name);  /* 域名长度    */    LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);    entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); /* 分配内存,返回地址指针  */    LWIP_ASSERT("mem-error in dns_init_local", entry != NULL);    if (entry != NULL) {      entry->name = (char*)entry + sizeof(struct local_hostlist_entry);      MEMCPY((char*)entry->name, init_entry->name, namelen);   /*  字符串拷贝*/      ((char*)entry->name)[namelen] = 0;      entry->addr = init_entry->addr;   /* 链表移动,指向下一个   */      entry->next = local_hostlist_dynamic;      local_hostlist_dynamic = entry;    }  }#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */}/** * Scans the local host-list for a hostname. * * @param hostname Hostname to look for in the local host-list * @return The first IP address for the hostname in the local host-list or *         IPADDR_NONE if not found. */static u32_tdns_lookup_local(const char *hostname)   /*  正向解析,解析出IP地址,可能有多个IP地址,返回一个;如果没有,就返回IPADDR_NONE  */{#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC  struct local_hostlist_entry *entry = local_hostlist_dynamic;  while(entry != NULL) {    if(strcmp(entry->name, hostname) == 0) {  /* 字符串匹配,遍历整个缓存链表,如果找到,就返回ip地址   */      return ip4_addr_get_u32(&entry->addr);   /* 在此处可以做个判断,如果是v6地址,要返回v6地址,可以增加条件编译语句  */    }    entry = entry->next;  }#else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */  int i;  for (i = 0; i < sizeof(local_hostlist_static) / sizeof(struct local_hostlist_entry); i++) {    if(strcmp(local_hostlist_static[i].name, hostname) == 0) {      return ip4_addr_get_u32(&local_hostlist_static[i].addr);    }  }#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */  return IPADDR_NONE;   /*  否则,返回无  */}#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC/** Remove all entries from the local host-list for a specific hostname * and/or IP addess * * @param hostname hostname for which entries shall be removed from the local *                 host-list * @param addr address for which entries shall be removed from the local host-list * @return the number of removed entries */intdns_local_removehost(const char *hostname, const ip_addr_t *addr) /* 删除DNS本地的记录,类似删除链表节点操作,先指向下一个节点,然后释放*/{  int removed = 0;  struct local_hostlist_entry *entry = local_hostlist_dynamic;  struct local_hostlist_entry *last_entry = NULL;  while (entry != NULL) {    if (((hostname == NULL) || !strcmp(entry->name, hostname)) &&        ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) {      struct local_hostlist_entry *free_entry;      if (last_entry != NULL) {        last_entry->next = entry->next;      } else {        local_hostlist_dynamic = entry->next;      }      free_entry = entry;      entry = entry->next;      memp_free(MEMP_LOCALHOSTLIST, free_entry);      removed++;    } else {      last_entry = entry;      entry = entry->next;    }  }  return removed;   /* 返回删除个数   */}/** * Add a hostname/IP address pair to the local host-list. * Duplicates are not checked. * * @param hostname hostname of the new entry * @param addr IP address of the new entry * @return ERR_OK if succeeded or ERR_MEM on memory error */err_tdns_local_addhost(const char *hostname, const ip_addr_t *addr)   /* 向本地增加新的记录    */{  struct local_hostlist_entry *entry;  size_t namelen;  LWIP_ASSERT("invalid host name (NULL)", hostname != NULL);  namelen = strlen(hostname);  LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);  entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);  if (entry == NULL) {    return ERR_MEM;  }  entry->name = (char*)entry + sizeof(struct local_hostlist_entry);  /* 一段域名+长度 */  MEMCPY((char*)entry->name, hostname, namelen);  ((char*)entry->name)[namelen] = 0; /*  以0为结束   */  ip_addr_copy(entry->addr, *addr);  entry->next = local_hostlist_dynamic;  local_hostlist_dynamic = entry;  return ERR_OK;}#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/#endif /* DNS_LOCAL_HOSTLIST */



/** * Look up a hostname in the array of known hostnames.   查询域名 * * @note This function only looks in the internal array of known * hostnames, it does not send out a query for the hostname if none * was found. The function dns_enqueue() can be used to send a query * for a hostname. * * @param name the hostname to look up * @return the hostname's IP address, as u32_t (instead of ip_addr_t to *         better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname *         was not found in the cached dns_table. */static u32_tdns_lookup(const char *name){  u8_t i;#if DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN)  u32_t addr;#endif /* DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) */#if DNS_LOCAL_HOSTLIST  if ((addr = dns_lookup_local(name)) != IPADDR_NONE) {    return addr; /*  如果查询本地域名,找到了,就返回    */  }#endif /* DNS_LOCAL_HOSTLIST */#ifdef DNS_LOOKUP_LOCAL_EXTERN  if((addr = DNS_LOOKUP_LOCAL_EXTERN(name)) != IPADDR_NONE) {    return addr;  }#endif /* DNS_LOOKUP_LOCAL_EXTERN */  /* Walk through name list, return entry if found. If not, return NULL. */  for (i = 0; i < DNS_TABLE_SIZE; ++i) {    if ((dns_table[i].state == DNS_STATE_DONE) &&        (strcmp(name, dns_table[i].name) == 0)) {  /* 遍历列表,字符串匹配查找  */      LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name));      ip_addr_debug_print(DNS_DEBUG, &(dns_table[i].ipaddr));      LWIP_DEBUGF(DNS_DEBUG, ("\n"));      return ip4_addr_get_u32(&dns_table[i].ipaddr);    }  }  return IPADDR_NONE;}#if DNS_DOES_NAME_CHECK/** * Compare the "dotted" name "query" with the encoded name "response" * to make sure an answer from the DNS server matches the current dns_table * entry (otherwise, answers might arrive late for hostname not on the list * any more). * * @param query hostname (not encoded) from the dns_table * @param response encoded hostname in the DNS response * @return 0: names equal; 1: names differ   比较,查询请求的域名和解析响应的域名是否相等,相同为0,不同为1  */static u8_tdns_compare_name(unsigned char *query, unsigned char *response){  unsigned char n;  do {    n = *response++;    /** @see RFC 1035 - 4.1.4. Message compression */    if ((n & 0xc0) == 0xc0) {      /* Compressed name */      break;    } else {      /* Not compressed name */      while (n > 0) {        if ((*query) != (*response)) {          return 1;        }        ++response;        ++query;        --n;      };      ++query;    }  } while (*response != 0);  return 0;}#endif /* DNS_DOES_NAME_CHECK *//** * Walk through a compact encoded DNS name and return the end of the name.   解析查询字符串的名字 * * @param query encoded DNS name in the DNS server response * @return end of the name */static unsigned char *dns_parse_name(unsigned char *query){  unsigned char n;  do {    n = *query++;    /** @see RFC 1035 - 4.1.4. Message compression */    if ((n & 0xc0) == 0xc0) {      /* Compressed name */      break;    } else {      /* Not compressed name */      while (n > 0) {        ++query;        --n;      };    }  } while (*query != 0);  return query + 1;}

/** * Send a DNS query packet. * * @param numdns index of the DNS server in the dns_servers table * @param name hostname to query * @param id index of the hostname in dns_table, used as transaction ID in the *        DNS query packet * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise */static err_tdns_send(u8_t numdns, const char* name, u8_t id)   /* 发送请求  */{  err_t err;  struct dns_hdr *hdr;  struct dns_query qry;  struct pbuf *p;  char *query, *nptr;  const char *pHostname;  u8_t n;  LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",              (u16_t)(numdns), name));  LWIP_ASSERT("dns server out of array", numdns < DNS_MAX_SERVERS);  LWIP_ASSERT("dns server has no IP address set", !ip_addr_isany(&dns_servers[numdns]));  /* if here, we have either a new query or a retry on a previous query to process */  p = pbuf_alloc(PBUF_TRANSPORT, SIZEOF_DNS_HDR + DNS_MAX_NAME_LENGTH +                 SIZEOF_DNS_QUERY, PBUF_RAM);  if (p != NULL) {    LWIP_ASSERT("pbuf must be in one piece", p->next == NULL);    /* fill dns header */    hdr = (struct dns_hdr*)p->payload;    memset(hdr, 0, SIZEOF_DNS_HDR);    hdr->id = htons(id);    hdr->flags1 = DNS_FLAG1_RD;    hdr->numquestions = PP_HTONS(1);    query = (char*)hdr + SIZEOF_DNS_HDR;    pHostname = name;    --pHostname;    /* convert hostname into suitable query format. */    do {      ++pHostname;      nptr = query;      ++query;      for(n = 0; *pHostname != '.' && *pHostname != 0; ++pHostname) {        *query = *pHostname;        ++query;        ++n;      }      *nptr = n;    } while(*pHostname != 0);    *query++='\0';    /* fill dns query */    qry.type = PP_HTONS(DNS_RRTYPE_A);    qry.cls = PP_HTONS(DNS_RRCLASS_IN);    SMEMCPY(query, &qry, SIZEOF_DNS_QUERY);    /* resize pbuf to the exact dns query */    pbuf_realloc(p, (u16_t)((query + SIZEOF_DNS_QUERY) - ((char*)(p->payload))));    /* connect to the server for faster receiving */    udp_connect(dns_pcb, &dns_servers[numdns], DNS_SERVER_PORT);    /* send dns packet */    err = udp_sendto(dns_pcb, p, &dns_servers[numdns], DNS_SERVER_PORT);    /* free pbuf */    pbuf_free(p);  } else {    err = ERR_MEM;  }  return err;}/** * dns_check_entry() - see if pEntry has not yet been queried and, if so, sends out a query.  检查一个dns,根据state 来处理。 * Check an entry in the dns_table: * - send out query for new entries    * - retry old pending entries on timeout (also with different servers) * - remove completed entries from the table if their TTL has expired * * @param i index of the dns_table entry to check */static voiddns_check_entry(u8_t i){  err_t err;  struct dns_table_entry *pEntry = &dns_table[i];  LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE);  switch(pEntry->state) {    case DNS_STATE_NEW: {  /* 一个新的dns  */      /* initialize new entry */      pEntry->state   = DNS_STATE_ASKING;      pEntry->numdns  = 0;      pEntry->tmr     = 1;      pEntry->retries = 0;            /* send DNS packet for this entry */      err = dns_send(pEntry->numdns, pEntry->name, i);      if (err != ERR_OK) {        LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,                    ("dns_send returned error: %s\n", lwip_strerr(err)));      }      break;    }    case DNS_STATE_ASKING: {  /* 正在询问   */      if (--pEntry->tmr == 0) {        if (++pEntry->retries == DNS_MAX_RETRIES) {          if ((pEntry->numdns+1<DNS_MAX_SERVERS) && !ip_addr_isany(&dns_servers[pEntry->numdns+1])) {            /* change of server */            pEntry->numdns++;            pEntry->tmr     = 1;            pEntry->retries = 0;            break;          } else {            LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", pEntry->name));            /* call specified callback function if provided */            if (pEntry->found)              (*pEntry->found)(pEntry->name, NULL, pEntry->arg);  /*  callback 函数  */            /* flush this entry */            pEntry->state   = DNS_STATE_UNUSED;            pEntry->found   = NULL;            break;          }        }        /* wait longer for the next retry */        pEntry->tmr = pEntry->retries;        /* send DNS packet for this entry */        err = dns_send(pEntry->numdns, pEntry->name, i);        if (err != ERR_OK) {          LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,                      ("dns_send returned error: %s\n", lwip_strerr(err)));        }      }      break;    }    case DNS_STATE_DONE: {      /* if the time to live is nul */      if (--pEntry->ttl == 0) {        LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name));        /* flush this entry */        pEntry->state = DNS_STATE_UNUSED;  /*  状态改变  */        pEntry->found = NULL;      }      break;    }    case DNS_STATE_UNUSED:      /* nothing to do */      break;    default:      LWIP_ASSERT("unknown dns_table entry state:", 0);      break;  }}



/** * Call dns_check_entry for each entry in dns_table - check all entries.  检查所有的dns */static voiddns_check_entries(void){  u8_t i;  for (i = 0; i < DNS_TABLE_SIZE; ++i) {    dns_check_entry(i); /*循环调用*/  }}/** * Receive input function for DNS response packets arriving for the dns UDP pcb.  接受响应处理,根据udp报文 * * @params see udp.h */static voiddns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)  {
  /* 初始化局部变量 */  u16_t i;  char *pHostname;  struct dns_hdr *hdr;  struct dns_answer ans;  struct dns_table_entry *pEntry;  u16_t nquestions, nanswers;  
  /* 对入参的检查 */  LWIP_UNUSED_ARG(arg);  LWIP_UNUSED_ARG(pcb);  LWIP_UNUSED_ARG(addr);  LWIP_UNUSED_ARG(port);  /* is the dns message too big ? */  if (p->tot_len > DNS_MSG_SIZE) {    LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too big\n"));    /* free pbuf and return */    goto memerr;  }  /* is the dns message big enough ? */  if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY + SIZEOF_DNS_ANSWER)) {    LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));    /* free pbuf and return */    goto memerr;  }  /*以上是一些异常情况处理 */
  /* copy dns payload inside static buffer for processing */   if (pbuf_copy_partial(p, dns_payload, p->tot_len, 0) == p->tot_len) {    /* The ID in the DNS header should be our entry into the name table. */    hdr = (struct dns_hdr*)dns_payload;    i = htons(hdr->id);
    if (i < DNS_TABLE_SIZE) {      pEntry = &dns_table[i];
      if(pEntry->state == DNS_STATE_ASKING) {        /* This entry is now completed. */        pEntry->state = DNS_STATE_DONE;        pEntry->err   = hdr->flags2 & DNS_FLAG2_ERR_MASK;        /* We only care about the question(s) and the answers. The authrr           and the extrarr are simply discarded. */        nquestions = htons(hdr->numquestions);  /* 主机字节序转换网络字节序 */        nanswers   = htons(hdr->numanswers);        /* Check for error. If so, call callback to inform. */        if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (pEntry->err != 0) || (nquestions != 1)) {          LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", pEntry->name));          /* call callback to indicate error, clean up memory and return */          goto responseerr;        }#if DNS_DOES_NAME_CHECK  /* 域名比较 */        /* Check if the name in the "question" part match with the name in the entry. */        if (dns_compare_name((unsigned char *)(pEntry->name), (unsigned char *)dns_payload + SIZEOF_DNS_HDR) != 0) {          LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", pEntry->name));          /* call callback to indicate error, clean up memory and return */          goto responseerr;        }#endif /* DNS_DOES_NAME_CHECK */        /* Skip the name in the "question" part */        pHostname = (char *) dns_parse_name((unsigned char *)dns_payload + SIZEOF_DNS_HDR) + SIZEOF_DNS_QUERY; /* 解析域名 */        while (nanswers > 0) { /* 循环  */          /* skip answer resource record's host name */          pHostname = (char *) dns_parse_name((unsigned char *)pHostname);          /* Check for IP address type and Internet class. Others are discarded. */          SMEMCPY(&ans, pHostname, SIZEOF_DNS_ANSWER);
  /* 字节序转换,读响应头里的类型,类,长度信息是否匹配*/          if((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) &&             (ans.len == PP_HTONS(sizeof(ip_addr_t))) ) {            /* read the answer resource record's TTL, and maximize it if needed */            pEntry->ttl = ntohl(ans.ttl); /* 字节序转换 */            if (pEntry->ttl > DNS_MAX_TTL) {              pEntry->ttl = DNS_MAX_TTL;            }            /* read the IP address after answer resource record's header */            SMEMCPY(&(pEntry->ipaddr), (pHostname+SIZEOF_DNS_ANSWER), sizeof(ip_addr_t));            LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", pEntry->name));            ip_addr_debug_print(DNS_DEBUG, (&(pEntry->ipaddr)));            LWIP_DEBUGF(DNS_DEBUG, ("\n"));            /* call specified callback function if provided */            if (pEntry->found) {              (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg);   /* callback函数 */            }            /* deallocate memory and return */            goto memerr;          } else {            pHostname = pHostname + SIZEOF_DNS_ANSWER + htons(ans.len);          }          --nanswers;        }        LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", pEntry->name));        /* call callback to indicate error, clean up memory and return */        goto responseerr;      }    }  }  /* deallocate memory and return */  goto memerr;responseerr:  /* ERROR: call specified callback function with NULL as name to indicate an error */  if (pEntry->found) {    (*pEntry->found)(pEntry->name, NULL, pEntry->arg);  }  /* flush this entry */  pEntry->state = DNS_STATE_UNUSED;  pEntry->found = NULL;memerr:  /* free pbuf   释放buffer  */  pbuf_free(p);  return;}



/** * Queues a new hostname to resolve and sends out a DNS query for that hostname * * @param name the hostname that is to be queried * @param found a callback founction to be called on success, failure or timeout * @param callback_arg argument to pass to the callback function * @return @return a err_t return code. */static err_tdns_enqueue(const char *name, dns_found_callback found, void *callback_arg) /* dns入队,加入一个新的域名*/{  u8_t i;  u8_t lseq, lseqi;  struct dns_table_entry *pEntry = NULL;  size_t namelen;  /* search an unused entry, or the oldest one */  lseq = lseqi = 0;  for (i = 0; i < DNS_TABLE_SIZE; ++i) {    pEntry = &dns_table[i];    /* is it an unused entry ? */    if (pEntry->state == DNS_STATE_UNUSED)      break;    /* check if this is the oldest completed entry */    if (pEntry->state == DNS_STATE_DONE) {      if ((dns_seqno - pEntry->seqno) > lseq) {        lseq = dns_seqno - pEntry->seqno;        lseqi = i;      }    }  }  /* if we don't have found an unused entry, use the oldest completed one */  if (i == DNS_TABLE_SIZE) {    if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {      /* no entry can't be used now, table is full */      LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name));      return ERR_MEM;    } else {      /* use the oldest completed one */      i = lseqi;      pEntry = &dns_table[i];    }  }  /* use this entry */  LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i)));  /* fill the entry */  pEntry->state = DNS_STATE_NEW;  pEntry->seqno = dns_seqno++;  pEntry->found = found;  pEntry->arg   = callback_arg;  namelen = LWIP_MIN(strlen(name), DNS_MAX_NAME_LENGTH-1);  MEMCPY(pEntry->name, name, namelen);  pEntry->name[namelen] = 0;  /* force to send query without waiting timer */  dns_check_entry(i);  /* dns query is enqueued */  return ERR_INPROGRESS;}/** * Resolve a hostname (string) into an IP address. 通过域名字符串解析出IP地址 * NON-BLOCKING callback version for use with raw API!!! * * Returns immediately with one of err_t return codes:  返回有三种结果,一种ok,一种正在处理,一种无效参数 * - ERR_OK if hostname is a valid IP address string or the host *   name is already in the local names table. * - ERR_INPROGRESS enqueue a request to be sent to the DNS server *   for resolution if no errors are present. * - ERR_ARG: dns client not initialized or invalid hostname * 四个入参,域名,地址,回调函数,回调函数入参 * @param hostname the hostname that is to be queried * @param addr pointer to a ip_addr_t where to store the address if it is already *             cached in the dns_table (only valid if ERR_OK is returned!) * @param found a callback function to be called on success, failure or timeout (only if *              ERR_INPROGRESS is returned!) * @param callback_arg argument to pass to the callback function * @return a err_t return code. */err_tdns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found,                  void *callback_arg){  u32_t ipaddr;  /* not initialized or no valid server yet, or invalid addr pointer 入参有误时的处理   * or invalid hostname or invalid hostname length */  if ((dns_pcb == NULL) || (addr == NULL) ||      (!hostname) || (!hostname[0]) ||      (strlen(hostname) >= DNS_MAX_NAME_LENGTH)) {    return ERR_ARG;  }#if LWIP_HAVE_LOOPIF  if (strcmp(hostname, "localhost")==0) { /* 查询是本机域名 */    ip_addr_set_loopback(addr);    return ERR_OK;  }#endif /* LWIP_HAVE_LOOPIF */  /* host name already in octet notation? set ip addr and return ERR_OK */  ipaddr = ipaddr_addr(hostname);  if (ipaddr == IPADDR_NONE) {    /* already have this address cached? */    ipaddr = dns_lookup(hostname);  }  if (ipaddr != IPADDR_NONE) {    ip4_addr_set_u32(addr, ipaddr);  /* 此处也可以返回v6地址   */
    return ERR_OK;  }  /* queue query with specified callback */  return dns_enqueue(hostname, found, callback_arg); /* 入队*/}#endif /* LWIP_DNS */






原创粉丝点击