[sr-dev] git:master:cdafc5a6: core: helper function to print ip address in a buffer with square brackets

Daniel-Constantin Mierla miconda at gmail.com
Fri Oct 13 12:33:48 CEST 2017


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

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2017-10-13T12:33:30+02:00

core: helper function to print ip address in a buffer with square brackets

---

Modified: src/core/ip_addr.h

---

Diff:  https://github.com/kamailio/kamailio/commit/cdafc5a6a0a3487101820d608ffa5973d7464d00.diff
Patch: https://github.com/kamailio/kamailio/commit/cdafc5a6a0a3487101820d608ffa5973d7464d00.patch

---

diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h
index 7edb44e679..df23e64c6d 100644
--- a/src/core/ip_addr.h
+++ b/src/core/ip_addr.h
@@ -660,15 +660,39 @@ static inline int ip_addr2sbuf(struct ip_addr* ip, char* buff, int len)
 	}
 }
 
+/* same as ip_addr2sbuf, but with [  ] around IPv6 addresses */
+static inline int ip_addr2sbufz(struct ip_addr* ip, char* buff, int len)
+{
+	char *p;
+	int sz;
+
+	p = buff;
+	switch(ip->af){
+		case AF_INET6:
+			*p++ = '[';
+			sz = ip6tosbuf(ip->u.addr, p, len-2);
+			p += sz;
+			*p++ = ']';
+			*p=0;
+			return sz + 2;
+			break;
+		case AF_INET:
+			return ip4tosbuf(ip->u.addr, buff, len);
+			break;
+		default:
+			LM_CRIT("unknown address family %d\n", ip->af);
+			return 0;
+	}
+}
 
 /* maximum size of a str returned by ip_addr2a (including \0) */
 #define IP_ADDR_MAX_STR_SIZE (IP6_MAX_STR_SIZE+1) /* ip62ascii +  \0*/
+#define IP_ADDR_MAX_STRZ_SIZE (IP6_MAX_STR_SIZE+3) /* ip62ascii + [ + ] + \0*/
 /* fast ip_addr -> string converter;
  * it uses an internal buffer
  */
 static inline char* ip_addr2a(struct ip_addr* ip)
 {
-
 	static char buff[IP_ADDR_MAX_STR_SIZE];
 	int len;
 
@@ -683,7 +707,7 @@ static inline char* ip_addr2a(struct ip_addr* ip)
 static inline char* ip_addr2strz(struct ip_addr* ip)
 {
 
-	static char buff[IP_ADDR_MAX_STR_SIZE+2];
+	static char buff[IP_ADDR_MAX_STRZ_SIZE];
 	char *p;
 	int len;
 




More information about the sr-dev mailing list