net: utils: Add net_addr_ntop() helper function

net_addr_ntop() will convert IPv4|6 address to string form.
Renamed existing net_sprint_ip_addr_buf() to net_addr_ntop()
and adjusted parameters as per API.

Jira: ZEP-1638

Change-Id: Ia497be6bf876ca63b120529acbadcfd9162a96e3
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
This commit is contained in:
Ravi kumar Veeramally 2017-01-30 12:30:13 +02:00 committed by Jukka Rissanen
commit b13f44bcdd
4 changed files with 46 additions and 47 deletions

View file

@ -795,6 +795,20 @@ struct sockaddr_in_ptr *net_sin_ptr(const struct sockaddr_ptr *addr)
*/
int net_addr_pton(sa_family_t family, const char *src, void *dst);
/**
* @brief Convert IP address to string form.
*
* @param family IP address family (AF_INET or AF_INET6)
* @param src Pointer to struct in_addr if family is AF_INET or
* pointer to struct in6_addr if family is AF_INET6
* @param dst IP address in a non-null terminated string
* @param size Number of bytes available in the buffer
*
* @return dst pointer if ok, NULL if error
*/
char *net_addr_ntop(sa_family_t family, const void *src,
char *dst, size_t size);
#ifdef __cplusplus
}
#endif

View file

@ -64,10 +64,6 @@ char *domains[] = {"not_a_real_domain_name",
"www.google.com",
NULL};
/* from subsys/net/ip/utils.c */
char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
char *buf, int buflen);
void run_dns(void)
{
struct dns_context ctx;
@ -141,11 +137,11 @@ void run_dns(void)
for (i = 0; i < ctx.items; i++) {
#ifdef CONFIG_NET_IPV6
net_sprint_ip_addr_buf(addresses[i].in6_u.u6_addr8,
16, str, sizeof(str));
net_addr_ntop(AF_INET6, &addresses[i],
str, sizeof(str));
#else
net_sprint_ip_addr_buf(addresses[i].in4_u.u4_addr8,
4, str, sizeof(str));
net_addr_ntop(AF_INET, &addresses[i],
str, sizeof(str));
#endif
printk("[%s:%d] %s\n", __func__, __LINE__, str);
}

View file

@ -23,8 +23,6 @@ extern void net_ipv6_init(void);
extern char *net_byte_to_hex(uint8_t *ptr, uint8_t byte, char base, bool pad);
extern char *net_sprint_ll_addr_buf(const uint8_t *ll, uint8_t ll_len,
char *buf, int buflen);
extern char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
char *buf, int buflen);
extern uint16_t net_calc_chksum(struct net_buf *buf, uint8_t proto);
#if defined(CONFIG_NET_IPV4)
@ -59,17 +57,12 @@ static inline char *net_sprint_ll_addr(const uint8_t *ll, uint8_t ll_len)
return net_sprint_ll_addr_buf(ll, ll_len, (char *)buf, sizeof(buf));
}
static inline char *net_sprint_ip_addr_ptr(const uint8_t *ptr, uint8_t len)
{
static char buf[NET_IPV6_ADDR_LEN];
return net_sprint_ip_addr_buf(ptr, len, (char *)buf, sizeof(buf));
}
static inline char *net_sprint_ipv6_addr(const struct in6_addr *addr)
{
#if defined(CONFIG_NET_IPV6)
return net_sprint_ip_addr_ptr(addr->s6_addr, 16);
static char buf[NET_IPV6_ADDR_LEN];
return net_addr_ntop(AF_INET6, addr, (char *)buf, sizeof(buf));
#else
return NULL;
#endif
@ -78,7 +71,9 @@ static inline char *net_sprint_ipv6_addr(const struct in6_addr *addr)
static inline char *net_sprint_ipv4_addr(const struct in_addr *addr)
{
#if defined(CONFIG_NET_IPV4)
return net_sprint_ip_addr_ptr(addr->s4_addr, 4);
static char buf[NET_IPV4_ADDR_LEN];
return net_addr_ntop(AF_INET, addr, (char *)buf, sizeof(buf));
#else
return NULL;
#endif
@ -159,14 +154,6 @@ static inline char *net_sprint_ll_addr(const uint8_t *ll, uint8_t ll_len)
return NULL;
}
static inline char *net_sprint_ip_addr_ptr(const uint8_t *ptr, uint8_t len)
{
ARG_UNUSED(ptr);
ARG_UNUSED(len);
return NULL;
}
static inline char *net_sprint_ipv6_addr(const struct in6_addr *addr)
{
ARG_UNUSED(addr);

View file

@ -99,21 +99,24 @@ static int net_value_to_udec(char *buf, uint32_t value, int precision)
return buf - start;
}
char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
char *buf, int buflen)
char *net_addr_ntop(sa_family_t family, const void *src,
char *dst, size_t size)
{
uint16_t *w = (uint16_t *)ip;
struct in_addr *addr;
struct in6_addr *addr6;
uint16_t *w;
uint8_t i, bl, bh, longest = 1;
int8_t pos = -1;
char delim = ':';
unsigned char zeros[8] = { 0 };
char *ptr = buf;
char *ptr = dst;
int len = -1;
uint16_t value;
bool needcolon = false;
switch (ip_len) {
case 16:
if (family == AF_INET6) {
addr6 = (struct in6_addr *)src;
w = (uint16_t *)addr6->s6_addr16;
len = 8;
for (i = 0; i < 8; i++) {
@ -123,6 +126,7 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
if (w[j] != 0) {
break;
}
zeros[i]++;
}
}
@ -137,28 +141,21 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
if (longest == 1) {
pos = -1;
}
break;
case 4:
} else if (family == AF_INET) {
addr = (struct in_addr *)src;
len = 4;
delim = '.';
break;
default:
break;
}
/* Invalid len, bail out */
if (len < 0) {
} else {
return NULL;
}
for (i = 0; i < len; i++) {
/* IPv4 address a.b.c.d */
if (len == 4) {
uint8_t l;
value = (uint32_t)ip[i];
value = (uint32_t)addr->s4_addr[i];
/* net_byte_to_udec() eats 0 */
if (value == 0) {
@ -171,6 +168,7 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
ptr += l;
*ptr++ = delim;
continue;
}
@ -179,9 +177,11 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
if (needcolon || i == 0) {
*ptr++ = ':';
}
*ptr++ = ':';
needcolon = false;
i += longest - 1;
continue;
}
@ -204,6 +204,7 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
*ptr++ = (char) (bh - 10 + 'a');
}
}
ptr = net_byte_to_hex(ptr, bl, 'a', true);
} else if (bl > 0x0f) {
ptr = net_byte_to_hex(ptr, bl, 'a', false);
@ -214,20 +215,21 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
*ptr++ = (char) (bl - 10 + 'a');
}
}
needcolon = true;
}
if (!(ptr - buf)) {
if (!(ptr - dst)) {
return NULL;
}
if (ip_len == 4) {
if (family == AF_INET) {
*(ptr - 1) = '\0';
} else {
*ptr = '\0';
}
return buf;
return dst;
}
int net_addr_pton(sa_family_t family, const char *src,