Added reentrant versions of inet_ntoa/ipaddr_ntoa inet_ntoa_r/ipaddr_ntoa_r

This commit is contained in:
goldsimon 2010-02-08 20:14:21 +00:00
parent ececc3ca45
commit 79e6b4c819
4 changed files with 33 additions and 5 deletions

View File

@ -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

View File

@ -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;
}
return buf;
}

View File

@ -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
}

View File

@ -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
}