nathelper: add_contact_alias() with parameters to enclose ipv6 in square brackets
authorDaniel-Constantin Mierla <miconda@gmail.com>
Wed, 4 Sep 2024 17:36:34 +0000 (19:36 +0200)
committerVictor Seva <vseva@debian.org>
Tue, 8 Oct 2024 06:56:35 +0000 (08:56 +0200)
- the add_contact_alias() without parameters was doing it already
- discovered while investigating GH #3968

(cherry picked from commit 744e5ac9678c0c9ef07bfb0de44c7c478a2debdc)

Gbp-Pq: Name nathelper-add_contact_alias-with-parameters-to-enclo.patch

src/modules/nathelper/nathelper.c

index 9ee73e62a21aed765f27fb679b316a0ebd339eb5..999045578477fde1d9c2d61d283673ff5c0859a9 100644 (file)
@@ -998,6 +998,8 @@ static int add_contact_alias_3(
        struct lump *anchor;
        struct sip_uri uri;
        char *bracket, *lt, *param, *at, *start;
+       int is_ipv6 = 0;
+       int i;
 
        /* Do nothing if Contact header does not exist */
        if(!msg->contact) {
@@ -1058,8 +1060,8 @@ static int add_contact_alias_3(
        }
 
        /* Create  ;alias param */
-       param_len = _ksr_contact_salias.len + IP6_MAX_STR_SIZE
-                               + 1 /* ~ */ + 5 /* port */
+       param_len = _ksr_contact_salias.len + 1 /* [ */ + IP6_MAX_STR_SIZE
+                               + 1 /* ] */ + 1 /* ~ */ + 5 /* port */
                                + 1 /* ~ */ + 1 /* proto */ + 1 /* closing > */;
        param = (char *)pkg_malloc(param_len);
        if(!param) {
@@ -1068,8 +1070,20 @@ static int add_contact_alias_3(
        }
        at = param;
        /* ip address */
+       for(i = 0; i < ip_str->len; i++) {
+               if(ip_str->s[i] == ':') {
+                       is_ipv6 = 1;
+                       break;
+               }
+       }
+       if(is_ipv6 && ip_str->s[0] != '[') {
+               append_chr(at, '[');
+       }
        append_str(at, _ksr_contact_salias.s, _ksr_contact_salias.len);
        append_str(at, ip_str->s, ip_str->len);
+       if(is_ipv6 && ip_str->s[0] != '[') {
+               append_chr(at, ']');
+       }
        /* port */
        append_chr(at, '~');
        append_str(at, port_str->s, port_str->len);