Module: kamailio Branch: master Commit: 4acca8ad2f31f465b08f15d98cc4780236e2beb0 URL: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc47802...
Author: Henning Westerholt hw@skalatan.de Committer: Henning Westerholt hw@skalatan.de Date: 2020-03-23T12:04:54+01:00
core: add new function buf_print_ip(..) to print an IP address to an buffer
---
Modified: src/core/ip_addr.c Modified: src/core/ip_addr.h
---
Diff: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc47802... Patch: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc47802...
---
diff --git a/src/core/ip_addr.c b/src/core/ip_addr.c index 9989063fff..82f746e0d6 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 9a7b67ac5d..896d4b757e 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);
Hello,
if I haven't misunderstood what this function does, there is another similar function (and few variants) for the same purpose, see ip_addr2sbuf() inside src/core/ip_addr.h
If can be reused, then makes no sense to add this new function that uses snprintf().
Cheers, Daniel
On 23.03.20 12:07, Henning Westerholt wrote:
Module: kamailio Branch: master Commit: 4acca8ad2f31f465b08f15d98cc4780236e2beb0 URL: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc47802...
Author: Henning Westerholt hw@skalatan.de Committer: Henning Westerholt hw@skalatan.de Date: 2020-03-23T12:04:54+01:00
core: add new function buf_print_ip(..) to print an IP address to an buffer
Modified: src/core/ip_addr.c Modified: src/core/ip_addr.h
Diff: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc47802... Patch: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc47802...
diff --git a/src/core/ip_addr.c b/src/core/ip_addr.c index 9989063fff..82f746e0d6 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 9a7b67ac5d..896d4b757e 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);
Kamailio (SER) - Development Mailing List sr-dev@lists.kamailio.org https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-dev
Hi Daniel,
ok - I will check and if it does the same thing, use this one/remove the new one.
Cheers,
Henning