res_pjsip: Log IPv6 addresses correctly
authorSean Bright <sean.bright@gmail.com>
Fri, 7 Sep 2018 14:40:05 +0000 (10:40 -0400)
committerSean Bright <sean.bright@gmail.com>
Fri, 14 Sep 2018 19:59:23 +0000 (14:59 -0500)
Both pjsip_tx_data.tp_info.dst_name and pjsip_rx_data.pkt_info.src_name
store IPv6 addresses without enclosing brackets. This causes some log
output to be confusing because it is difficult to separate the IPv6
address from a port specification.

* Use pj_sockaddr_print() along with pjsip_tx_data.tp_info.dst_addr and
  pjsip_rx_data.pkt_info.src_addr where possible for consistent IPv6
  output.

* When a pj_sockaddr is not available, explicitly wrap IPv6 addresses
  in brackets.

* When assigning pjsip_rx_data.pkt_info.src_name ourselves, make sure
  to also set pjsip_rx_data.pkt_info.src_addr.

Change-Id: I5cfe997ced7883862a12b9c7d8551d76ae02fcf8

include/asterisk/netsock2.h
res/res_pjsip.c
res/res_pjsip/pjsip_distributor.c
res/res_pjsip_logger.c
res/res_pjsip_outbound_authenticator_digest.c
res/res_pjsip_session.c
res/res_pjsip_transport_websocket.c

index fc3a0c0..3ba15a8 100644 (file)
@@ -31,6 +31,18 @@ extern "C" {
 
 #include <netinet/in.h>
 
+/*
+ * String buffer size that can accommodate a fully stringified representation of a
+ * supported IP address & port:
+ *
+ * - 45 bytes for an IPv6 address
+ * -  2 bytes for brackets around an IPv6 address
+ * -  1 byte for the port separator (a colon)
+ * -  5 bytes for the port
+ * -  1 byte for the zero-terminator
+ */
+#define AST_SOCKADDR_BUFLEN (45 + 2 + 1 + 5 + 1)
+
 /*!
  * Values for address families that we support. This is reproduced from socket.h
  * because we do not want users to include that file. Only netsock2.c should
index 49defab..c2e77cb 100644 (file)
@@ -3674,6 +3674,8 @@ int ast_sip_create_rdata_with_contact(pjsip_rx_data *rdata, char *packet, const
        ast_copy_string(rdata->pkt_info.packet, packet, sizeof(rdata->pkt_info.packet));
        ast_copy_string(rdata->pkt_info.src_name, src_name, sizeof(rdata->pkt_info.src_name));
        rdata->pkt_info.src_port = src_port;
+       pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&tmp, src_name), &rdata->pkt_info.src_addr);
+       pj_sockaddr_set_port(&rdata->pkt_info.src_addr, src_port);
 
        pjsip_parse_rdata(packet, strlen(packet), rdata);
        if (!rdata->msg_info.msg || !pj_list_empty(&rdata->msg_info.parse_err)) {
index 626f07c..4bd3b00 100644 (file)
@@ -648,16 +648,21 @@ static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int cou
        char from_buf[PJSIP_MAX_URL_SIZE];
        char callid_buf[PJSIP_MAX_URL_SIZE];
        char method_buf[PJSIP_MAX_URL_SIZE];
+       char src_addr_buf[AST_SOCKADDR_BUFLEN];
        pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, rdata->msg_info.from->uri, from_buf, PJSIP_MAX_URL_SIZE);
        ast_copy_pj_str(callid_buf, &rdata->msg_info.cid->id, PJSIP_MAX_URL_SIZE);
        ast_copy_pj_str(method_buf, &rdata->msg_info.msg->line.req.method.name, PJSIP_MAX_URL_SIZE);
        if (count) {
-               ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s:%d' (callid: %s) - %s"
+               ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s' (callid: %s) - %s"
                        " after %u tries in %.3f ms\n",
-                       method_buf, from_buf, rdata->pkt_info.src_name, rdata->pkt_info.src_port, callid_buf, msg, count, period / 1000.0);
+                       method_buf, from_buf,
+                       pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buf, sizeof(src_addr_buf), 3),
+                       callid_buf, msg, count, period / 1000.0);
        } else {
-               ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s:%d' (callid: %s) - %s\n",
-                       method_buf, from_buf, rdata->pkt_info.src_name, rdata->pkt_info.src_port, callid_buf, msg);
+               ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s' (callid: %s) - %s\n",
+                       method_buf, from_buf,
+                       pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buf, sizeof(src_addr_buf), 3),
+                       callid_buf, msg);
        }
 }
 
index fb58f2b..172deec 100644 (file)
@@ -74,22 +74,25 @@ static inline int pjsip_log_test_addr(const char *address, int port)
 
 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
 {
+       char buffer[AST_SOCKADDR_BUFLEN];
+
        if (!pjsip_log_test_addr(tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
                return PJ_SUCCESS;
        }
 
-       ast_verbose("<--- Transmitting SIP %s (%d bytes) to %s:%s:%d --->\n%.*s\n",
+       ast_verbose("<--- Transmitting SIP %s (%d bytes) to %s:%s --->\n%.*s\n",
                    tdata->msg->type == PJSIP_REQUEST_MSG ? "request" : "response",
                    (int) (tdata->buf.cur - tdata->buf.start),
                    tdata->tp_info.transport->type_name,
-                   tdata->tp_info.dst_name,
-                   tdata->tp_info.dst_port,
+                   pj_sockaddr_print(&tdata->tp_info.dst_addr, buffer, sizeof(buffer), 3),
                    (int) (tdata->buf.end - tdata->buf.start), tdata->buf.start);
        return PJ_SUCCESS;
 }
 
 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
 {
+       char buffer[AST_SOCKADDR_BUFLEN];
+
        if (!pjsip_log_test_addr(rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
                return PJ_FALSE;
        }
@@ -98,12 +101,11 @@ static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
                return PJ_FALSE;
        }
 
-       ast_verbose("<--- Received SIP %s (%d bytes) from %s:%s:%d --->\n%s\n",
+       ast_verbose("<--- Received SIP %s (%d bytes) from %s:%s --->\n%s\n",
                    rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ? "request" : "response",
                    rdata->msg_info.len,
                    rdata->tp_info.transport->type_name,
-                   rdata->pkt_info.src_name,
-                   rdata->pkt_info.src_port,
+                   pj_sockaddr_print(&rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
                    rdata->pkt_info.packet);
        return PJ_FALSE;
 }
index 063b4d3..5f6d994 100644 (file)
@@ -118,8 +118,8 @@ static int digest_create_request_with_auth(const struct ast_sip_auth_vector *aut
        }
        /* If there was no dialog, then this is probably a REGISTER so no endpoint */
        if (!id) {
-               id = ast_alloca(strlen(challenge->pkt_info.src_name) + 7 /* ':' + port + NULL */);
-               sprintf(id, "%s:%d", challenge->pkt_info.src_name, challenge->pkt_info.src_port);
+               id = ast_alloca(AST_SOCKADDR_BUFLEN);
+               pj_sockaddr_print(&challenge->pkt_info.src_addr, id, AST_SOCKADDR_BUFLEN, 3);
                id_type = "Host";
        }
 
index 244b8cc..2b78cfa 100644 (file)
@@ -2931,6 +2931,7 @@ static int new_invite(struct new_invite *invite)
        pjsip_timer_setting timer;
        pjsip_rdata_sdp_info *sdp_info;
        pjmedia_sdp_session *local = NULL;
+       char buffer[AST_SOCKADDR_BUFLEN];
 
        /* From this point on, any calls to pjsip_inv_terminate have the last argument as PJ_TRUE
         * so that we will be notified so we can destroy the session properly
@@ -2958,8 +2959,11 @@ static int new_invite(struct new_invite *invite)
                }
                goto end;
        case SIP_GET_DEST_EXTEN_PARTIAL:
-               ast_debug(1, "Call from '%s' (%s:%s:%d) to extension '%s' - partial match\n", ast_sorcery_object_get_id(invite->session->endpoint),
-                       invite->rdata->tp_info.transport->type_name, invite->rdata->pkt_info.src_name, invite->rdata->pkt_info.src_port, invite->session->exten);
+               ast_debug(1, "Call from '%s' (%s:%s) to extension '%s' - partial match\n",
+                       ast_sorcery_object_get_id(invite->session->endpoint),
+                       invite->rdata->tp_info.transport->type_name,
+                       pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
+                       invite->session->exten);
 
                if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 484, NULL, NULL, &tdata) == PJ_SUCCESS) {
                        ast_sip_session_send_response(invite->session, tdata);
@@ -2969,9 +2973,12 @@ static int new_invite(struct new_invite *invite)
                goto end;
        case SIP_GET_DEST_EXTEN_NOT_FOUND:
        default:
-               ast_log(LOG_NOTICE, "Call from '%s' (%s:%s:%d) to extension '%s' rejected because extension not found in context '%s'.\n",
-                       ast_sorcery_object_get_id(invite->session->endpoint), invite->rdata->tp_info.transport->type_name, invite->rdata->pkt_info.src_name,
-                       invite->rdata->pkt_info.src_port, invite->session->exten, invite->session->endpoint->context);
+               ast_log(LOG_NOTICE, "Call from '%s' (%s:%s) to extension '%s' rejected because extension not found in context '%s'.\n",
+                       ast_sorcery_object_get_id(invite->session->endpoint),
+                       invite->rdata->tp_info.transport->type_name,
+                       pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
+                       invite->session->exten,
+                       invite->session->endpoint->context);
 
                if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 404, NULL, NULL, &tdata) == PJ_SUCCESS) {
                        ast_sip_session_send_response(invite->session, tdata);
index 75869f5..2abbf15 100644 (file)
@@ -429,12 +429,23 @@ static pj_bool_t websocket_on_rx_msg(pjsip_rx_data *rdata)
                pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
                const pj_str_t *txp_str = &STR_WS;
 
-               ast_debug(4, "%s re-writing Contact URI from %.*s:%d%s%.*s to %s:%d;transport=%s\n",
-                       pjsip_rx_data_get_info(rdata),
-                       (int)pj_strlen(&uri->host), pj_strbuf(&uri->host), uri->port,
-                       pj_strlen(&uri->transport_param) ? ";transport=" : "",
-                       (int)pj_strlen(&uri->transport_param), pj_strbuf(&uri->transport_param),
-                       rdata->pkt_info.src_name ?: "", rdata->pkt_info.src_port, pj_strbuf(txp_str));
+               if (DEBUG_ATLEAST(4)) {
+                       char src_addr_buffer[AST_SOCKADDR_BUFLEN];
+                       const char *ipv6_s = "", *ipv6_e = "";
+
+                       if (pj_strchr(&uri->host, ':')) {
+                               ipv6_s = "[";
+                               ipv6_e = "]";
+                       }
+
+                       ast_log(LOG_DEBUG, "%s re-writing Contact URI from %s%.*s%s:%d%s%.*s to %s;transport=%s\n",
+                               pjsip_rx_data_get_info(rdata),
+                               ipv6_s, (int) pj_strlen(&uri->host), pj_strbuf(&uri->host), ipv6_e, uri->port,
+                               pj_strlen(&uri->transport_param) ? ";transport=" : "",
+                               (int) pj_strlen(&uri->transport_param), pj_strbuf(&uri->transport_param),
+                               pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buffer, sizeof(src_addr_buffer), 3),
+                               pj_strbuf(txp_str));
+               }
 
                pj_cstr(&uri->host, rdata->pkt_info.src_name);
                uri->port = rdata->pkt_info.src_port;