?? dns.c
字號:
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); /* 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. */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. * * @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); } /* 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 */ 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){ 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. * NON-BLOCKING callback version for use with raw API!!! * * Returns immediately with one of err_t return codes: * - 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); return ERR_OK; } /* queue query with specified callback */ return dns_enqueue(hostname, found, callback_arg);}#endif /* LWIP_DNS */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -