Module: kamailio Branch: master Commit: 0537f584d5032146a627b6173114a7cdd213e384 URL: https://github.com/kamailio/kamailio/commit/0537f584d5032146a627b6173114a7cd...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: 2018-04-10T13:28:34+02:00
core: added helper function to retrieve source address in socket format
---
Modified: src/core/parser/msg_parser.c Modified: src/core/parser/msg_parser.h
---
Diff: https://github.com/kamailio/kamailio/commit/0537f584d5032146a627b6173114a7cd... Patch: https://github.com/kamailio/kamailio/commit/0537f584d5032146a627b6173114a7cd...
---
diff --git a/src/core/parser/msg_parser.c b/src/core/parser/msg_parser.c index efad8f9f63..2a3d3b1de5 100644 --- a/src/core/parser/msg_parser.c +++ b/src/core/parser/msg_parser.c @@ -1102,6 +1102,65 @@ int get_src_uri(sip_msg_t *m, int tmode, str *uri) return 0; }
+/** + * get source proto:ip:port (socket address format) + */ +int get_src_address_socket(sip_msg_t *m, str *ssock) +{ + static char buf[MAX_URI_SIZE]; + char* p; + str ip, port; + int len; + str proto; + + if (!ssock || !m) { + ERR("invalid parameter value\n"); + return -1; + } + + if(get_valid_proto_string(m->rcv.proto, 1, 0, &proto)<0) { + ERR("unknown transport protocol\n"); + return -1; + } + + ip.s = ip_addr2a(&m->rcv.src_ip); + ip.len = strlen(ip.s); + + port.s = int2str(m->rcv.src_port, &port.len); + + len = proto.len + 1 + ip.len + 2*(m->rcv.src_ip.af==AF_INET6)+ 1 + port.len; + + if (len+1 >= MAX_URI_SIZE) { + ERR("buffer too small\n"); + return -1; + } + + p = buf; + + memcpy(p, proto.s, proto.len); + p += proto.len; + + *p++ = ':'; + + if (m->rcv.src_ip.af==AF_INET6) + *p++ = '['; + memcpy(p, ip.s, ip.len); + p += ip.len; + if (m->rcv.src_ip.af==AF_INET6) + *p++ = ']'; + + *p++ = ':'; + + memcpy(p, port.s, port.len); + p += port.len; + *p = '\0'; + + ssock->s = buf; + ssock->len = len; + + return 0; +} + /** * get received-on-socket ip, port and protocol in SIP URI format * - tmode - 0: short format (transport=udp is not added, being default) diff --git a/src/core/parser/msg_parser.h b/src/core/parser/msg_parser.h index 9270e0bba9..3d69e8d462 100644 --- a/src/core/parser/msg_parser.h +++ b/src/core/parser/msg_parser.h @@ -499,6 +499,11 @@ void msg_ldata_reset(sip_msg_t*); */ int get_src_uri(sip_msg_t *m, int tmode, str *uri);
+/** + * get source proto:ip:port (socket address format) + */ +int get_src_address_socket(sip_msg_t *m, str *ssock); + /** * get received-on-socket ip, port and protocol in SIP URI format */