[svn-commits] dvossel: trunk r215681 - /trunk/channels/chan_sip.c
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Wed Sep 2 16:39:35 CDT 2009
Author: dvossel
Date: Wed Sep 2 16:39:31 2009
New Revision: 215681
URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=215681
Log:
port string to int conversion using sscanf
There are several instances where a port is parsed
from a uri or some other source and converted to
an int value using atoi(), if for some reason the
port string is empty, then a standard port is used.
This logic is used over and over, so I created a function
to handle it in a safer way using sscanf().
Modified:
trunk/channels/chan_sip.c
Modified: trunk/channels/chan_sip.c
URL: http://svn.asterisk.org/svn-view/asterisk/trunk/channels/chan_sip.c?view=diff&rev=215681&r1=215680&r2=215681
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Wed Sep 2 16:39:31 2009
@@ -3170,6 +3170,20 @@
return TRUE;
}
+/*! \brief converts ascii port to int representation. If no
+ * pt buffer is provided or the pt has errors when being converted
+ * to an int value, the port provided as the standard is used.
+ */
+static int port_str2int(const char *pt, unsigned int standard)
+{
+ int port = standard;
+ if (ast_strlen_zero(pt) || (sscanf(pt, "%30d", &port) != 1) || (port < 0)) {
+ port = standard;
+ }
+
+ return port;
+}
+
/*! \brief Allocate and initialize sip proxy */
static struct sip_proxy *proxy_allocate(char *name, char *port, int force)
{
@@ -3184,7 +3198,7 @@
return NULL;
proxy->force = force;
ast_copy_string(proxy->name, name, sizeof(proxy->name));
- proxy->ip.sin_port = htons((!ast_strlen_zero(port) ? atoi(port) : STANDARD_SIP_PORT));
+ proxy->ip.sin_port = htons(port_str2int(port, STANDARD_SIP_PORT));
proxy_update(proxy);
return proxy;
}
@@ -5229,10 +5243,7 @@
/* This address should be updated using dnsmgr */
memcpy(&dialog->sa.sin_addr, &sin->sin_addr, sizeof(dialog->sa.sin_addr));
if (!sin->sin_port) {
- if (ast_strlen_zero(port) || sscanf(port, "%30u", &portno) != 1) {
- portno = (dialog->socket.type & SIP_TRANSPORT_TLS) ?
- STANDARD_TLS_PORT : STANDARD_SIP_PORT;
- }
+ portno = port_str2int(port, (dialog->socket.type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT);
} else {
portno = ntohs(sin->sin_port);
}
@@ -5259,7 +5270,7 @@
}
}
if (!portno)
- portno = port ? atoi(port) : (dialog->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT;
+ portno = port_str2int(port, (dialog->socket.type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT);
hp = ast_gethostbyname(hostn, &ahp);
if (!hp) {
ast_log(LOG_WARNING, "No such host: %s\n", peername);
@@ -12204,7 +12215,6 @@
char *host, *pt, *transport;
char contact_buf[256];
char *contact;
- int use_tls = FALSE;
/* Work on a copy */
ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
@@ -12215,19 +12225,19 @@
*
* Note: The outbound proxy could be using UDP between the proxy and Asterisk.
* We still need to be able to send to the remote agent through the proxy.
- */
-
- if (!parse_uri(contact, "sip,sips", &contact, NULL, &host, &pt, NULL, &transport)) {
- if (((get_transport_str2enum(transport) == SIP_TRANSPORT_TLS)) ||
- !(strncasecmp(fullcontact, "sips", 4))) {
- use_tls = TRUE;
- port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_TLS_PORT;
- } else {
- port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_SIP_PORT;
- }
+ */
+
+ if (parse_uri(contact, "sip,sips", &contact, NULL, &host, &pt, NULL, &transport)) {
+ ast_log(LOG_WARNING, "Invalid contact uri %s (missing sip: or sips:), attempting to use anyway\n", fullcontact);
+ }
+
+ /* set port */
+ if (((get_transport_str2enum(transport) == SIP_TRANSPORT_TLS)) || !(strncasecmp(fullcontact, "sips", 4))) {
+ port = port_str2int(pt, STANDARD_TLS_PORT);
} else {
- ast_log(LOG_WARNING, "Invalid contact uri %s (missing sip: or sips:), attempting to use anyway\n", fullcontact);
- }
+ port = port_str2int(pt, STANDARD_SIP_PORT);
+ }
+
/* XXX This could block for a long time XXX */
/* We should only do this if it's a name, not an IP */
@@ -12347,13 +12357,10 @@
/* if the port is not specified but the transport is, make sure to set the
* default port to match the specified transport. This may or may not be the
* same transport used by the pvt struct for the Register dialog. */
- if (ast_strlen_zero(pt)) {
- port = (transport_type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT;
- } else {
- port = atoi(pt);
- }
+
+ port = port_str2int(pt, (transport_type == SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT);
} else {
- port = !ast_strlen_zero(pt) ? atoi(pt) : STANDARD_SIP_PORT;
+ port = port_str2int(pt, STANDARD_SIP_PORT);
transport_type = pvt->socket.type;
}
@@ -14024,7 +14031,7 @@
if (pt)
*pt++ = '\0'; /* remember port pointer */
p->sa = p->recv;
- p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
+ p->sa.sin_port = htons(port_str2int(pt, STANDARD_SIP_PORT));
if (sip_debug_test_pvt(p)) {
const struct sockaddr_in *dst = sip_real_dst(p);
More information about the svn-commits
mailing list