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

Author: Daniel-Constantin Mierla <mico...@gmail.com>
Committer: Daniel-Constantin Mierla <mico...@gmail.com>
Date: 2024-11-25T10:16:07+01:00

core: helper functions to convert ip to string using a pool of buffers

---

Modified: src/core/ip_addr.c
Modified: src/core/ip_addr.h

---

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

---

diff --git a/src/core/ip_addr.c b/src/core/ip_addr.c
index 02c26f969d5..8e2ae6cf42d 100644
--- a/src/core/ip_addr.c
+++ b/src/core/ip_addr.c
@@ -298,6 +298,52 @@ char *ip_addr2strz(struct ip_addr *ip)
        return buff;
 }
 
+#define IP_ADDR_BUF_NR 8
+static char _ksr_addr2x_buff[IP_ADDR_BUF_NR][IP_ADDR_MAX_STR_SIZE];
+static int _ksr_addr2x_idx = 0;
+
+/* fast ip_addr -> string converter;
+ * it uses an internal buffer
+ */
+char *ip_addr2xa(struct ip_addr *ip)
+{
+       int len;
+       char *buff;
+
+       buff = _ksr_addr2x_buff[_ksr_addr2x_idx];
+       _ksr_addr2x_idx = (_ksr_addr2x_idx + 1) % IP_ADDR_BUF_NR;
+
+       len = ip_addr2sbuf(ip, buff, sizeof(buff) - 1);
+       buff[len] = 0;
+
+       return buff;
+}
+
+
+/* full address in text representation, including [] for ipv6 */
+char *ip_addr2xstrz(struct ip_addr *ip)
+{
+       char *p;
+       int len;
+       char *buff;
+
+       buff = _ksr_addr2x_buff[_ksr_addr2x_idx];
+       _ksr_addr2x_idx = (_ksr_addr2x_idx + 1) % IP_ADDR_BUF_NR;
+
+       p = buff;
+       if(ip->af == AF_INET6) {
+               *p++ = '[';
+       }
+       len = ip_addr2sbuf(ip, p, sizeof(buff) - 3);
+       p += len;
+       if(ip->af == AF_INET6) {
+               *p++ = ']';
+       }
+       *p = 0;
+
+       return buff;
+}
+
 
 /* returns an asciiz string containing the ip and the port
  *  (<ip_addr>:port or [<ipv6_addr>]:port)
diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h
index 4a266391190..b55c78bd8bc 100644
--- a/src/core/ip_addr.h
+++ b/src/core/ip_addr.h
@@ -614,14 +614,18 @@ int ip_addr2sbufz(struct ip_addr *ip, char *buff, int 
len);
 #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
- */
+/* ip addr to string converter; it uses an internal static buffer */
 char *ip_addr2a(struct ip_addr *ip);
+/* ip addr to string converter; it uses a pool of internal static buffers */
+char *ip_addr2xa(struct ip_addr *ip);
 
 
-/* full address in text representation, including [] for ipv6 */
+/* full address in text representation, including [] for ipv6
+ * - it uses an internal static buffer */
 char *ip_addr2strz(struct ip_addr *ip);
+/* full address in text representation, including [] for ipv6
+ * - it uses a pool of internal static buffers */
+char *ip_addr2xstrz(struct ip_addr *ip);
 
 
 #define SU2A_MAX_STR_SIZE                                            \

_______________________________________________
Kamailio - Development Mailing List -- sr-dev@lists.kamailio.org
To unsubscribe send an email to sr-dev-le...@lists.kamailio.org
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to