outbound tls connections were not defaulting to port 5061
authorDavid Vossel <dvossel@digium.com>
Thu, 1 Oct 2009 19:33:33 +0000 (19:33 +0000)
committerDavid Vossel <dvossel@digium.com>
Thu, 1 Oct 2009 19:33:33 +0000 (19:33 +0000)
(closes issue #15854)
Reported by: dvossel
Patches:
      sip_port_config_trunk.diff uploaded by dvossel (license 671)
Tested by: dvossel

Review: https://reviewboard.asterisk.org/r/357/

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@221697 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_sip.c

index a7f9880..034745e 100644 (file)
@@ -7606,29 +7606,32 @@ static int sip_register(const char *value, int lineno)
        AST_NONSTANDARD_RAW_ARGS(user2, user1.userpart, '@');
 
        if (host3.port) {
-               if (sscanf(host3.port, "%5u", &portnum) != 1 || portnum > 65535) {
+               if (!(portnum = port_str2int(host3.port, 0))) {
                        ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", host3.port, lineno);
-                       portnum = -1;
                }
        }
 
+       /* set transport type */
        if (!pre2.transport) {
                transport = SIP_TRANSPORT_UDP;
        } else if (!strncasecmp(pre2.transport, "tcp", 3)) {
                transport = SIP_TRANSPORT_TCP;
        } else if (!strncasecmp(pre2.transport, "tls", 3)) {
                transport = SIP_TRANSPORT_TLS;
-               if (portnum < 0) {
-                       portnum = STANDARD_TLS_PORT;
-               }
        } else if (!strncasecmp(pre2.transport, "udp", 3)) {
                transport = SIP_TRANSPORT_UDP;
        } else {
+               transport = SIP_TRANSPORT_UDP;
                ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", pre2.transport, lineno);
        }
 
-       if (portnum < 0) {
-               portnum = STANDARD_SIP_PORT;
+       /* if no portnum specified, set default for transport */
+       if (!portnum) {
+               if (transport == SIP_TRANSPORT_TLS) {
+                       portnum = STANDARD_TLS_PORT;
+               } else {
+                       portnum = STANDARD_SIP_PORT;
+               }
        }
 
        if (!(reg = ast_calloc(1, sizeof(*reg)))) {
@@ -24166,6 +24169,7 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
        struct ast_ha *oldha = NULL;
        int found = 0;
        int firstpass = 1;
+       uint16_t port = 0;
        int format = 0;         /* Ama flags */
        time_t regseconds = 0;
        struct ast_flags peerflags[2] = {{(0)}};
@@ -24341,17 +24345,15 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                                                /* Initialize stuff if this is a new peer, or if it used to
                                                 * not be dynamic before the reload. */
                                                memset(&peer->addr.sin_addr, 0, 4);
-                                               if (peer->addr.sin_port) {
-                                                       /* If we've already got a port, make it the default rather than absolute */
-                                                       peer->defaddr.sin_port = peer->addr.sin_port;
-                                                       peer->addr.sin_port = 0;
-                                               }
+                                               peer->addr.sin_port = 0;
                                        }
                                        peer->host_dynamic = TRUE;
                                } else {
                                        /* Non-dynamic.  Make sure we become that way if we're not */
                                        AST_SCHED_DEL_UNREF(sched, peer->expire,
                                                        unref_peer(peer, "removing register expire ref"));
+                                       /* the port will either be set to a default value or a config specified value once all option parsing is complete */
+                                       peer->addr.sin_port = 0;
                                        peer->host_dynamic = FALSE;
                                        srvlookup = v->value;
                                        if (global_dynamic_exclude_static) {
@@ -24381,10 +24383,8 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                                }
                        } else if (!strcasecmp(v->name, "port")) {
                                peer->portinuri = 1;
-                               if (!realtime && peer->host_dynamic) {
-                                       peer->defaddr.sin_port = htons(atoi(v->value));
-                               } else {
-                                       peer->addr.sin_port = htons(atoi(v->value));
+                               if (!(port = port_str2int(v->value, 0))) {
+                                       ast_log(LOG_WARNING, "Invalid peer port configuration at line %d : %s\n", v->lineno, v->value);
                                }
                        } else if (!strcasecmp(v->name, "callingpres")) {
                                peer->callingpres = ast_parse_caller_presentation(v->value);
@@ -24608,6 +24608,12 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                        set_socket_transport(&peer->socket, peer->default_outbound_transport);
                }
 
+               if (port && !realtime && peer->host_dynamic) {
+                       peer->defaddr.sin_port = htons(port);
+               } else if (port) {
+                       peer->addr.sin_port = htons(port);
+               }
+
                if (ast_str_strlen(fullcontact)) {
                        ast_string_field_set(peer, fullcontact, ast_str_buffer(fullcontact));
                        peer->rt_fromcontact = TRUE;
@@ -24648,7 +24654,9 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                if (!peer->addr.sin_port) {
                        peer->addr.sin_port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
                }
-
+               if (!peer->defaddr.sin_port) {
+                       peer->defaddr.sin_port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
+               }
                if (!peer->socket.port) {
                        peer->socket.port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
                }