dns.c: Fix bug #22108 "DNS problem" caused by unaligned structures.

This commit is contained in:
fbernon 2008-01-25 13:12:12 +00:00
parent f02c39a402
commit e937521014
2 changed files with 12 additions and 10 deletions

View File

@ -596,6 +596,8 @@ HISTORY
++ Bug fixes: ++ Bug fixes:
2008-01-25 Cui hengbin (integrated by Frédéric Bernon)
* dns.c: Fix bug #22108 "DNS problem" caused by unaligned structures.
2008-01-15 Kieran Mansley 2008-01-15 Kieran Mansley
* tcp_out.c: BUG20511. Modify persist timer to start when we are * tcp_out.c: BUG20511. Modify persist timer to start when we are

View File

@ -403,7 +403,7 @@ dns_send(u8_t numdns, const char* name, u8_t id)
{ {
err_t err; err_t err;
struct dns_hdr *hdr; struct dns_hdr *hdr;
struct dns_query *qry; struct dns_query qry;
struct pbuf *p; struct pbuf *p;
char *query, *nptr; char *query, *nptr;
const char *pHostname; const char *pHostname;
@ -444,9 +444,9 @@ dns_send(u8_t numdns, const char* name, u8_t id)
*query++='\0'; *query++='\0';
/* fill dns query */ /* fill dns query */
qry = (struct dns_query *)query; qry.type = htons(DNS_RRTYPE_A);
qry->type = htons(DNS_RRTYPE_A); qry.class = htons(DNS_RRCLASS_IN);
qry->class = htons(DNS_RRCLASS_IN); memcpy( query, &qry, sizeof(struct dns_query));
/* resize pbuf to the exact dns query */ /* resize pbuf to the exact dns query */
pbuf_realloc(p, (query + sizeof(struct dns_query)) - ((char*)(p->payload))); pbuf_realloc(p, (query + sizeof(struct dns_query)) - ((char*)(p->payload)));
@ -568,7 +568,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u
u8_t i; u8_t i;
char *pHostname; char *pHostname;
struct dns_hdr *hdr; struct dns_hdr *hdr;
struct dns_answer *ans; struct dns_answer ans;
struct dns_table_entry *pEntry; struct dns_table_entry *pEntry;
u8_t nquestions, nanswers; u8_t nquestions, nanswers;
#if (DNS_USES_STATIC_BUF == 0) #if (DNS_USES_STATIC_BUF == 0)
@ -647,15 +647,15 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u
pHostname = (char *) dns_parse_name((unsigned char *)pHostname); pHostname = (char *) dns_parse_name((unsigned char *)pHostname);
/* Check for IP address type and Internet class. Others are discarded. */ /* Check for IP address type and Internet class. Others are discarded. */
ans = (struct dns_answer *)pHostname; memcpy(&ans, pHostname, sizeof(struct dns_answer));
if((ntohs(ans->type) == DNS_RRTYPE_A) && (ntohs(ans->class) == DNS_RRCLASS_IN) && (ntohs(ans->len) == sizeof(struct ip_addr)) ) { if((ntohs(ans.type) == DNS_RRTYPE_A) && (ntohs(ans.class) == DNS_RRCLASS_IN) && (ntohs(ans.len) == sizeof(struct ip_addr)) ) {
/* read the answer resource record's TTL, and maximize it if needed */ /* read the answer resource record's TTL, and maximize it if needed */
pEntry->ttl = ntohl(ans->ttl); pEntry->ttl = ntohl(ans.ttl);
if (pEntry->ttl > DNS_MAX_TTL) { if (pEntry->ttl > DNS_MAX_TTL) {
pEntry->ttl = DNS_MAX_TTL; pEntry->ttl = DNS_MAX_TTL;
} }
/* read the IP address after answer resource record's header */ /* read the IP address after answer resource record's header */
pEntry->ipaddr = (*((struct ip_addr*)(ans+1))); memcpy( &(pEntry->ipaddr), (pHostname+sizeof(struct dns_answer)), sizeof(struct ip_addr));
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", pEntry->name)); LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", pEntry->name));
ip_addr_debug_print(DNS_DEBUG, (&(pEntry->ipaddr))); ip_addr_debug_print(DNS_DEBUG, (&(pEntry->ipaddr)));
LWIP_DEBUGF(DNS_DEBUG, ("\n")); LWIP_DEBUGF(DNS_DEBUG, ("\n"));
@ -666,7 +666,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u
/* deallocate memory and return */ /* deallocate memory and return */
goto memerr2; goto memerr2;
} else { } else {
pHostname = pHostname + sizeof(struct dns_answer) + htons(ans->len); pHostname = pHostname + sizeof(struct dns_answer) + htons(ans.len);
} }
--nanswers; --nanswers;
} }