diff --git a/CHANGELOG b/CHANGELOG index 180cb405..666ae700 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -20,7 +20,11 @@ HISTORY ++ New features: 2010-02-08: Simon Goldschmidt - * netif.h: Added + * inet.h, ip_addr.c/.h: Added reentrant versions of inet_ntoa/ipaddr_ntoa + inet_ntoa_r/ipaddr_ntoa_r + + 2010-02-08: Simon Goldschmidt + * netif.h: Added netif_s/get_igmp_mac_filter() macros 2010-02-05: Simon Goldschmidt * netif.h: Added function-like macros to get/set the hostname on a netif diff --git a/src/core/ipv4/ip_addr.c b/src/core/ipv4/ip_addr.c index e54c6f76..c4bcdf73 100644 --- a/src/core/ipv4/ip_addr.c +++ b/src/core/ipv4/ip_addr.c @@ -223,6 +223,20 @@ char * ipaddr_ntoa(ip_addr_t *addr) { static char str[16]; + return ipaddr_ntoa_r(addr, str, 16); +} + +/** + * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used. + * + * @param addr ip address in network order to convert + * @param buf target buffer where the string is stored + * @param buflen length of buf + * @return either pointer to buf which now holds the ASCII + * representation of addr or NULL if buf was too small + */ +char *ipaddr_ntoa_r(ip_addr_t *addr, char *buf, int buflen) +{ u32_t s_addr; char inv[3]; char *rp; @@ -230,10 +244,11 @@ ipaddr_ntoa(ip_addr_t *addr) u8_t rem; u8_t n; u8_t i; + int len = 0; s_addr = ip4_addr_get_u32(addr); - rp = str; + rp = buf; ap = (u8_t *)&s_addr; for(n = 0; n < 4; n++) { i = 0; @@ -242,11 +257,18 @@ ipaddr_ntoa(ip_addr_t *addr) *ap /= (u8_t)10; inv[i++] = '0' + rem; } while(*ap); - while(i--) + while(i--) { + if (len++ >= buflen) { + return NULL; + } *rp++ = inv[i]; + } + if (len++ >= buflen) { + return NULL; + } *rp++ = '.'; ap++; } *--rp = 0; - return str; -} \ No newline at end of file + return buf; +} diff --git a/src/include/ipv4/lwip/inet.h b/src/include/ipv4/lwip/inet.h index 640608fc..cb33486b 100644 --- a/src/include/ipv4/lwip/inet.h +++ b/src/include/ipv4/lwip/inet.h @@ -96,6 +96,7 @@ struct in_addr { #define inet_addr(cp) ipaddr_addr(cp) #define inet_aton(cp, addr) ipaddr_aton(cp, (ip_addr_t*)addr) #define inet_ntoa(addr) ipaddr_ntoa((ip_addr_t*)&(addr)) +#define inet_ntoa_r(addr, buf, buflen) ipaddr_ntoa_r((ip_addr_t*)&(addr), buf, buflen) #ifdef __cplusplus } diff --git a/src/include/ipv4/lwip/ip_addr.h b/src/include/ipv4/lwip/ip_addr.h index afe2a8ac..e84c3e78 100644 --- a/src/include/ipv4/lwip/ip_addr.h +++ b/src/include/ipv4/lwip/ip_addr.h @@ -204,6 +204,7 @@ u32_t ipaddr_addr(const char *cp); int ipaddr_aton(const char *cp, ip_addr_t *addr); /** returns ptr to static buffer; not reentrant! */ char *ipaddr_ntoa(ip_addr_t *addr); +char *ipaddr_ntoa_r(ip_addr_t *addr, char *buf, int buflen); #ifdef __cplusplus }