diff --git a/src/core/ip_addr.c b/src/core/ip_addr.c index 9989063fff0..82f746e0d6b 100644 --- a/src/core/ip_addr.c +++ b/src/core/ip_addr.c @@ -274,6 +274,35 @@ void stdout_print_ip(struct ip_addr* ip) } +void buf_print_ip(char *buf, struct ip_addr* ip, unsigned int len) +{ + if (len < INET6_ADDRSTRLEN) { + LM_ERR("insufficent buffer length\n"); + return; + } + switch(ip->af){ + case AF_INET: + snprintf(buf, len, "%d.%d.%d.%d", ip->u.addr[0], + ip->u.addr[1], + ip->u.addr[2], + ip->u.addr[3]); + break; + case AF_INET6: + snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]), + htons(ip->u.addr16[1]), + htons(ip->u.addr16[2]), + htons(ip->u.addr16[3]), + htons(ip->u.addr16[4]), + htons(ip->u.addr16[5]), + htons(ip->u.addr16[6]), + htons(ip->u.addr16[7]) + ); + break; + default: + snprintf(buf, len, "warning unknown address family %d\n", ip->af); + } +} + void print_net(struct net* net) { diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h index 9a7b67ac5da..896d4b757eb 100644 --- a/src/core/ip_addr.h +++ b/src/core/ip_addr.h @@ -265,6 +265,8 @@ int mk_net_str(struct net* dst, str* s); void print_ip(char* prefix, struct ip_addr* ip, char* suffix); void stdout_print_ip(struct ip_addr* ip); +void buf_print_ip(char *buf, struct ip_addr* ip, unsigned int len); + void print_net(struct net* net); char* get_proto_name(unsigned int proto);