瀏覽代碼

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

Daniel-Constantin Mierla 8 年之前
父節點
當前提交
cdafc5a6a0
共有 1 個文件被更改,包括 26 次插入2 次删除
  1. 26 2
      src/core/ip_addr.h

+ 26 - 2
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;