Module: kamailio Branch: master Commit: 52ab6f3dcf5ad8d967be8bdecaa64ef31cbfbe33 URL: https://github.com/kamailio/kamailio/commit/52ab6f3dcf5ad8d967be8bdecaa64ef3...
Author: Dragos Vingarzan vingarzan@gmail.com Committer: Dragos Vingarzan vingarzan@gmail.com Date: 2024-07-15T21:50:48+02:00
nathelper: fixed handle_ruri_alias() for alias being an IPv6
---
Modified: src/modules/nathelper/nathelper.c
---
Diff: https://github.com/kamailio/kamailio/commit/52ab6f3dcf5ad8d967be8bdecaa64ef3... Patch: https://github.com/kamailio/kamailio/commit/52ab6f3dcf5ad8d967be8bdecaa64ef3...
---
diff --git a/src/modules/nathelper/nathelper.c b/src/modules/nathelper/nathelper.c index 6e6d44ad7ed..9c0bebad3a7 100644 --- a/src/modules/nathelper/nathelper.c +++ b/src/modules/nathelper/nathelper.c @@ -1139,7 +1139,7 @@ static int ki_handle_ruri_alias_mode(struct sip_msg *msg, int mode) char buf[MAX_URI_SIZE], *val, *sep, *at, *next, *cur_uri, *rest, *port, *trans, *start; unsigned int len, rest_len, val_len, alias_len, proto_type, cur_uri_len, - ip_port_len; + ip_len, ip_port_len, port_len, i;
if(parse_sip_msg_uri(msg) < 0) { LM_ERR("while parsing Request-URI\n"); @@ -1185,18 +1185,46 @@ static int ki_handle_ruri_alias_mode(struct sip_msg *msg, int mode) LM_ERR("no '~' in alias param value\n"); return -1; } + // IPv6 needs some [] added when composing a SIP URI, which further + // complicates this code. + ip_len = port - val; + int is_ipv6 = 0; + for(i = 0; i < ip_len; i++) { + if(val[i] == ':') { + is_ipv6 = 1; + break; + } + } *(port++) = ':'; trans = memchr(port, 126 /* ~ */, val_len - (port - val)); if(trans == NULL) { LM_ERR("no second '~' in alias param value\n"); return -1; } + // Compose the URI in a buffer at = &(buf[0]); append_str(at, "sip:", 4); ip_port_len = trans - val; alias_len = _ksr_contact_salias.len + ip_port_len + 2 /* ~n */; - memcpy(at, val, ip_port_len); - at = at + ip_port_len; + if(is_ipv6) { + // IPv6 - add '[' ']' around IP + // then append ':' and copy the port + append_chr(at, '['); + memcpy(at, val, ip_len); + at = at + ip_len; + append_chr(at, ']'); + port_len = trans - port; + if(port_len > 0) { + append_chr(at, ':'); + memcpy(at, port, port_len); + at = at + port_len; + } + } else { + // IPv4 - copy directly as is + // separator '~' between IP and port was changed to ':' + memcpy(at, val, ip_port_len); + at = at + ip_port_len; + } trans = trans + 1; if((ip_port_len + 2 > val_len) || (*trans == ';') || (*trans == '?')) { LM_ERR("no proto in alias param\n");