mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2024-09-13 10:15:43 +00:00
Changes on DNS client. Add multiples dns servers support.
This commit is contained in:
parent
9dd4ad6c1f
commit
8ff1194b6a
@ -522,6 +522,9 @@ static void dhcp_handle_ack(struct netif *netif)
|
|||||||
dns_setserver( n, (struct ip_addr *)(&(dhcp->offered_dns_addr[n].addr)));
|
dns_setserver( n, (struct ip_addr *)(&(dhcp->offered_dns_addr[n].addr)));
|
||||||
#endif /* LWIP_DNS */
|
#endif /* LWIP_DNS */
|
||||||
}
|
}
|
||||||
|
#if LWIP_DNS
|
||||||
|
dns_setserver( n, &ip_addr_any);
|
||||||
|
#endif /* LWIP_DNS */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,6 @@
|
|||||||
*----------------------------------------------------------------------------*/
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/** @todo: define good default values (rfc compliance) */
|
/** @todo: define good default values (rfc compliance) */
|
||||||
/** @todo: secondary server support */
|
|
||||||
/** @todo: pbuf chain not yet supported */
|
/** @todo: pbuf chain not yet supported */
|
||||||
/** @todo: improve answer parsing, more checkings... */
|
/** @todo: improve answer parsing, more checkings... */
|
||||||
|
|
||||||
@ -104,7 +103,7 @@
|
|||||||
|
|
||||||
/* The maximum number of retries when asking for a name, before "timeout". */
|
/* The maximum number of retries when asking for a name, before "timeout". */
|
||||||
#ifndef DNS_MAX_RETRIES
|
#ifndef DNS_MAX_RETRIES
|
||||||
#define DNS_MAX_RETRIES 8
|
#define DNS_MAX_RETRIES 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* DNS entry time to live (in DNS_TMR_INTERVAL ticks) */
|
/* DNS entry time to live (in DNS_TMR_INTERVAL ticks) */
|
||||||
@ -191,6 +190,7 @@ PACK_STRUCT_BEGIN
|
|||||||
/** DNS table entry */
|
/** DNS table entry */
|
||||||
struct dns_table_entry {
|
struct dns_table_entry {
|
||||||
u8_t state;
|
u8_t state;
|
||||||
|
u8_t numdns;
|
||||||
u8_t tmr;
|
u8_t tmr;
|
||||||
u8_t retries;
|
u8_t retries;
|
||||||
u8_t ttl;
|
u8_t ttl;
|
||||||
@ -218,6 +218,7 @@ static void dns_check_entries(void);
|
|||||||
static struct udp_pcb *dns_pcb;
|
static struct udp_pcb *dns_pcb;
|
||||||
static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
|
static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
|
||||||
static u8_t dns_seqno;
|
static u8_t dns_seqno;
|
||||||
|
static struct ip_addr dns_servers[DNS_MAX_SERVERS];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the resolver and configure which DNS server to use for queries.
|
* Initialize the resolver and configure which DNS server to use for queries.
|
||||||
@ -264,9 +265,8 @@ dns_init()
|
|||||||
void
|
void
|
||||||
dns_setserver(u8_t numdns, struct ip_addr *dnsserver)
|
dns_setserver(u8_t numdns, struct ip_addr *dnsserver)
|
||||||
{
|
{
|
||||||
LWIP_UNUSED_ARG(numdns);
|
if ((numdns<DNS_MAX_SERVERS) && (dns_pcb != NULL) && (dnsserver != NULL) && (dnsserver->addr !=0 )) {
|
||||||
if ((dns_pcb != NULL) && (dnsserver != NULL) && (dnsserver->addr !=0 )) {
|
dns_servers[numdns] = (*dnsserver);
|
||||||
udp_connect( dns_pcb, dnsserver, DNS_SERVER_PORT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,11 +276,10 @@ dns_setserver(u8_t numdns, struct ip_addr *dnsserver)
|
|||||||
* the currently configured DNS server or NULL if no DNS server has
|
* the currently configured DNS server or NULL if no DNS server has
|
||||||
* been configured.
|
* been configured.
|
||||||
*/
|
*/
|
||||||
u32_t
|
struct ip_addr
|
||||||
dns_getserver(u8_t numdns)
|
dns_getserver(u8_t numdns)
|
||||||
{
|
{
|
||||||
LWIP_UNUSED_ARG(numdns);
|
return (((numdns<DNS_MAX_SERVERS) && (dns_pcb != NULL))?dns_servers[numdns]:(*IP_ADDR_ANY));
|
||||||
return ((dns_pcb != NULL)?dns_pcb->remote_ip.addr:0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -356,7 +355,7 @@ dns_parse_name(unsigned char *query)
|
|||||||
* dns_send
|
* dns_send
|
||||||
*/
|
*/
|
||||||
static err_t
|
static err_t
|
||||||
dns_send(const char* name, u8_t id)
|
dns_send(u8_t numdns, const char* name, u8_t id)
|
||||||
{
|
{
|
||||||
struct dns_hdr *hdr;
|
struct dns_hdr *hdr;
|
||||||
struct dns_query *qry;
|
struct dns_query *qry;
|
||||||
@ -365,7 +364,7 @@ dns_send(const char* name, u8_t id)
|
|||||||
const char *pHostname;
|
const char *pHostname;
|
||||||
u8_t n;
|
u8_t n;
|
||||||
|
|
||||||
LWIP_DEBUGF(DNS_DEBUG, ("dns_send: \"%s\": request\n", name));
|
LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n", (u16_t)(numdns), name));
|
||||||
|
|
||||||
/* if here, we have either a new query or a retry on a previous query to process */
|
/* if here, we have either a new query or a retry on a previous query to process */
|
||||||
p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dns_hdr)+DNS_MAX_NAME_LENGTH+sizeof(struct dns_query), PBUF_RAM);
|
p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dns_hdr)+DNS_MAX_NAME_LENGTH+sizeof(struct dns_query), PBUF_RAM);
|
||||||
@ -403,7 +402,7 @@ dns_send(const char* name, u8_t id)
|
|||||||
pbuf_realloc(p, (query+sizeof(struct dns_query))-((char*)(p->payload)));
|
pbuf_realloc(p, (query+sizeof(struct dns_query))-((char*)(p->payload)));
|
||||||
|
|
||||||
/* send dns packet */
|
/* send dns packet */
|
||||||
udp_send(dns_pcb, p);
|
udp_sendto(dns_pcb, p, dns_servers+numdns, DNS_SERVER_PORT);
|
||||||
|
|
||||||
/* free pbuf */
|
/* free pbuf */
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
@ -433,6 +432,13 @@ dns_check_entries(void)
|
|||||||
if (pEntry->state == DNS_STATE_ASKING) {
|
if (pEntry->state == DNS_STATE_ASKING) {
|
||||||
if (--pEntry->tmr == 0) {
|
if (--pEntry->tmr == 0) {
|
||||||
if (++pEntry->retries == DNS_MAX_RETRIES) {
|
if (++pEntry->retries == DNS_MAX_RETRIES) {
|
||||||
|
if ((pEntry->numdns+1<DNS_MAX_SERVERS) && (dns_servers[pEntry->numdns+1].addr!=0)) {
|
||||||
|
/* change of server */
|
||||||
|
pEntry->numdns++;
|
||||||
|
pEntry->tmr = 1;
|
||||||
|
pEntry->retries = 0;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entries: \"%s\": timeout\n", pEntry->name));
|
LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entries: \"%s\": timeout\n", pEntry->name));
|
||||||
/* call specified callback function if provided */
|
/* call specified callback function if provided */
|
||||||
if (pEntry->found)
|
if (pEntry->found)
|
||||||
@ -442,6 +448,7 @@ dns_check_entries(void)
|
|||||||
pEntry->found = NULL;
|
pEntry->found = NULL;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/* wait longer for the next retry */
|
/* wait longer for the next retry */
|
||||||
pEntry->tmr = pEntry->retries;
|
pEntry->tmr = pEntry->retries;
|
||||||
} else {
|
} else {
|
||||||
@ -450,11 +457,12 @@ dns_check_entries(void)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pEntry->state = DNS_STATE_ASKING;
|
pEntry->state = DNS_STATE_ASKING;
|
||||||
|
pEntry->numdns = 0;
|
||||||
pEntry->tmr = 1;
|
pEntry->tmr = 1;
|
||||||
pEntry->retries = 0;
|
pEntry->retries = 0;
|
||||||
}
|
}
|
||||||
/* send DNS packet for this entry */
|
/* send DNS packet for this entry */
|
||||||
dns_send(pEntry->name, i);
|
dns_send(pEntry->numdns, pEntry->name, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -485,16 +493,28 @@ dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16
|
|||||||
struct dns_table_entry *pEntry;
|
struct dns_table_entry *pEntry;
|
||||||
u8_t nquestions, nanswers;
|
u8_t nquestions, nanswers;
|
||||||
|
|
||||||
LWIP_ASSERT("dns_recv: pbuf chain not yet supported", (p->next==NULL));
|
/* is the dns message in a pbuf chain ? */
|
||||||
|
if (p->next != NULL) {
|
||||||
|
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf chain not yet supported\n"));
|
||||||
|
pbuf_free(p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
hdr = (struct dns_hdr *)p->payload;
|
/* is the dns message big enought ? */
|
||||||
|
if (p->tot_len < (sizeof(struct dns_hdr)+sizeof(struct dns_query)+sizeof(struct dns_answer))) {
|
||||||
|
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));
|
||||||
|
pbuf_free(p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/** @todo: check RFC1035 - 7.3. Processing responses */
|
/** @todo: check RFC1035 - 7.3. Processing responses */
|
||||||
|
|
||||||
/* The ID in the DNS header should be our entry into the name table. */
|
/* The ID in the DNS header should be our entry into the name table. */
|
||||||
|
hdr = (struct dns_hdr *)p->payload;
|
||||||
i = htons(hdr->id);
|
i = htons(hdr->id);
|
||||||
|
if(i < DNS_TABLE_SIZE) {
|
||||||
pEntry = &dns_table[i];
|
pEntry = &dns_table[i];
|
||||||
if( (i < DNS_TABLE_SIZE) && (pEntry->state == DNS_STATE_ASKING) ) {
|
if(pEntry->state == DNS_STATE_ASKING) {
|
||||||
/* This entry is now completed. */
|
/* This entry is now completed. */
|
||||||
pEntry->state = DNS_STATE_DONE;
|
pEntry->state = DNS_STATE_DONE;
|
||||||
pEntry->ttl = DNS_TTL_ENTRY;
|
pEntry->ttl = DNS_TTL_ENTRY;
|
||||||
@ -514,6 +534,8 @@ dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16
|
|||||||
/* flush this entry */
|
/* flush this entry */
|
||||||
pEntry->state = DNS_STATE_UNUSED;
|
pEntry->state = DNS_STATE_UNUSED;
|
||||||
pEntry->found = NULL;
|
pEntry->found = NULL;
|
||||||
|
/* free pbuf */
|
||||||
|
pbuf_free(p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -536,6 +558,8 @@ dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16
|
|||||||
/* call specified callback function if provided */
|
/* call specified callback function if provided */
|
||||||
if (pEntry->found)
|
if (pEntry->found)
|
||||||
(*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg);
|
(*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg);
|
||||||
|
/* free pbuf */
|
||||||
|
pbuf_free(p);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
pHostname = pHostname + sizeof(struct dns_answer) + htons(ans->len);
|
pHostname = pHostname + sizeof(struct dns_answer) + htons(ans->len);
|
||||||
@ -544,6 +568,10 @@ dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16
|
|||||||
}
|
}
|
||||||
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", pEntry->name));
|
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", pEntry->name));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* free pbuf */
|
||||||
|
pbuf_free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -584,11 +612,11 @@ dns_query(const char *name, void (*found)(const char *name, struct ip_addr *addr
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* fill the entry */
|
/* fill the entry */
|
||||||
strcpy(pEntry->name, name);
|
|
||||||
pEntry->found = found;
|
|
||||||
pEntry->arg = arg;
|
|
||||||
pEntry->state = DNS_STATE_NEW;
|
pEntry->state = DNS_STATE_NEW;
|
||||||
pEntry->seqno = dns_seqno++;
|
pEntry->seqno = dns_seqno++;
|
||||||
|
pEntry->found = found;
|
||||||
|
pEntry->arg = arg;
|
||||||
|
strcpy( pEntry->name, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,6 +43,11 @@
|
|||||||
#define DNS_TMR_INTERVAL 1000
|
#define DNS_TMR_INTERVAL 1000
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/** The maximum of DNS servers */
|
||||||
|
#ifndef DNS_MAX_SERVERS
|
||||||
|
#define DNS_MAX_SERVERS 2
|
||||||
|
#endif
|
||||||
|
|
||||||
/** DNS message max. size */
|
/** DNS message max. size */
|
||||||
#define DNS_MSG_SIZE 512
|
#define DNS_MSG_SIZE 512
|
||||||
|
|
||||||
@ -89,10 +94,7 @@ void dns_tmr(void);
|
|||||||
void dns_setserver(u8_t numdns, struct ip_addr *dnsserver);
|
void dns_setserver(u8_t numdns, struct ip_addr *dnsserver);
|
||||||
|
|
||||||
/* returns configured DNS server IP address */
|
/* returns configured DNS server IP address */
|
||||||
u32_t dns_getserver(u8_t numdns);
|
struct ip_addr dns_getserver(u8_t numdns);
|
||||||
|
|
||||||
/* returns IP for host 'name' only if already in table */
|
|
||||||
u32_t dns_lookup(const char *name);
|
|
||||||
|
|
||||||
/* resolves a host 'name' in ip address */
|
/* resolves a host 'name' in ip address */
|
||||||
DNS_RESULT dns_gethostbyname(const char *hostName, struct ip_addr *addr,
|
DNS_RESULT dns_gethostbyname(const char *hostName, struct ip_addr *addr,
|
||||||
|
Loading…
Reference in New Issue
Block a user