From b73dcfb8cfa18c448adbcb47307b8748422208b9 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Sun, 21 Feb 2010 12:15:01 +0000 Subject: [PATCH] task #10140: Remove DNS_USES_STATIC_BUF (keep the implementation of DNS_USES_STATIC_BUF==1) --- CHANGELOG | 4 ++++ src/core/dns.c | 42 +++++++++++------------------------------- src/include/lwip/mem.h | 15 +++++++++++++++ src/include/lwip/opt.h | 7 ------- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 467fe4d9..ab2b536b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -129,6 +129,10 @@ HISTORY ++ Bugfixes: + 2010-02-21: Simon Goldschmidt + * opt.h, mem.h, dns.c: task #10140: Remove DNS_USES_STATIC_BUF (keep + the implementation of DNS_USES_STATIC_BUF==1) + 2010-02-20: Simon Goldschmidt * tcp.h, tcp.c, tcp_in.c, tcp_out.c: Task #10088: Correctly implement close() vs. shutdown(). Now the application does not get any more diff --git a/src/core/dns.c b/src/core/dns.c index 44e0e41e..772188ef 100644 --- a/src/core/dns.c +++ b/src/core/dns.c @@ -227,10 +227,9 @@ static struct udp_pcb *dns_pcb; static u8_t dns_seqno; static struct dns_table_entry dns_table[DNS_TABLE_SIZE]; static ip_addr_t dns_servers[DNS_MAX_SERVERS]; - -#if (DNS_USES_STATIC_BUF == 1) -static u8_t dns_payload[DNS_MSG_SIZE]; -#endif /* (DNS_USES_STATIC_BUF == 1) */ +/** 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 @@ -240,6 +239,8 @@ void dns_init() { ip_addr_t dnsserver; + + dns_payload = LWIP_MEM_ALIGN(dns_payload_buffer); /* initialize default DNS server address */ DNS_SERVER_ADDRESS(&dnsserver); @@ -733,12 +734,6 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t struct dns_answer ans; struct dns_table_entry *pEntry; u16_t nquestions, nanswers; -#if (DNS_USES_STATIC_BUF == 0) - u8_t dns_payload[DNS_MSG_SIZE]; -#endif /* (DNS_USES_STATIC_BUF == 0) */ -#if (DNS_USES_STATIC_BUF == 2) - u8_t* dns_payload; -#endif /* (DNS_USES_STATIC_BUF == 2) */ LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(pcb); @@ -749,25 +744,16 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t if (p->tot_len > DNS_MSG_SIZE) { LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too big\n")); /* free pbuf and return */ - goto memerr1; + 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 memerr1; + goto memerr; } -#if (DNS_USES_STATIC_BUF == 2) - dns_payload = mem_malloc(p->tot_len); - if (dns_payload == NULL) { - LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: mem_malloc error\n")); - /* free pbuf and return */ - goto memerr1; - } -#endif /* (DNS_USES_STATIC_BUF == 2) */ - /* 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. */ @@ -804,7 +790,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t /* 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) { + while (nanswers > 0) { /* skip answer resource record's host name */ pHostname = (char *) dns_parse_name((unsigned char *)pHostname); @@ -827,7 +813,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg); } /* deallocate memory and return */ - goto memerr2; + goto memerr; } else { pHostname = pHostname + SIZEOF_DNS_ANSWER + htons(ans.len); } @@ -841,7 +827,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t } /* deallocate memory and return */ - goto memerr2; + goto memerr; responseerr: /* ERROR: call specified callback function with NULL as name to indicate an error */ @@ -852,13 +838,7 @@ responseerr: pEntry->state = DNS_STATE_UNUSED; pEntry->found = NULL; -memerr2: -#if (DNS_USES_STATIC_BUF == 2) - /* free dns buffer */ - mem_free(dns_payload); -#endif /* (DNS_USES_STATIC_BUF == 2) */ - -memerr1: +memerr: /* free pbuf */ pbuf_free(p); return; diff --git a/src/include/lwip/mem.h b/src/include/lwip/mem.h index 6e6c1d92..7a9a0ecb 100644 --- a/src/include/lwip/mem.h +++ b/src/include/lwip/mem.h @@ -90,10 +90,25 @@ void *mem_calloc(mem_size_t count, mem_size_t size); void mem_free(void *mem); #endif /* MEM_LIBC_MALLOC */ +/** Calculate memory size for an aligned buffer - returns the next highest + * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and + * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4). + */ #ifndef LWIP_MEM_ALIGN_SIZE #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1) & ~(MEM_ALIGNMENT-1)) #endif +/** Calculate safe memory size for an aligned buffer when using an unaligned + * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the + * start (e.g. if buffer is u8_t[] and actual data will be u32_t*) + */ +#ifndef LWIP_MEM_ALIGN_BUFFER +#define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1)) +#endif + +/** Align a memory pointer to the alignment defined by MEM_ALIGNMENT + * so that ADDR % MEM_ALIGNMENT == 0 + */ #ifndef LWIP_MEM_ALIGN #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1))) #endif diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index ab5be05a..444fe0ab 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -757,13 +757,6 @@ #define DNS_DOES_NAME_CHECK 1 #endif -/** DNS use a local buffer if DNS_USES_STATIC_BUF=0, a static one if - DNS_USES_STATIC_BUF=1, or a dynamic one if DNS_USES_STATIC_BUF=2. - The buffer will be of size DNS_MSG_SIZE */ -#ifndef DNS_USES_STATIC_BUF -#define DNS_USES_STATIC_BUF 1 -#endif - /** DNS message max. size. Default value is RFC compliant. */ #ifndef DNS_MSG_SIZE #define DNS_MSG_SIZE 512