[sr-dev] git:master:4acca8ad: core: add new function buf_print_ip(..) to print an IP address to an buffer

Henning Westerholt hw at skalatan.de
Mon Mar 23 12:07:52 CET 2020


Module: kamailio
Branch: master
Commit: 4acca8ad2f31f465b08f15d98cc4780236e2beb0
URL: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc4780236e2beb0

Author: Henning Westerholt <hw at skalatan.de>
Committer: Henning Westerholt <hw at 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/4acca8ad2f31f465b08f15d98cc4780236e2beb0.diff
Patch: https://github.com/kamailio/kamailio/commit/4acca8ad2f31f465b08f15d98cc4780236e2beb0.patch

---

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);




More information about the sr-dev mailing list