[svn-commits] rizzo: branch 1.4 r44776 - /branches/1.4/channels/chan_sip.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Tue Oct 10 01:18:42 MST 2006


Author: rizzo
Date: Tue Oct 10 03:18:42 2006
New Revision: 44776

URL: http://svn.digium.com/view/asterisk?rev=44776&view=rev
Log:
merge a few code simplifications that have gone
into trunk during last week, to reduce differences
between the two branches and make porting fixes easier.


Modified:
    branches/1.4/channels/chan_sip.c

Modified: branches/1.4/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/channels/chan_sip.c?rev=44776&r1=44775&r2=44776&view=diff
==============================================================================
--- branches/1.4/channels/chan_sip.c (original)
+++ branches/1.4/channels/chan_sip.c Tue Oct 10 03:18:42 2006
@@ -449,13 +449,20 @@
 /*! \brief SIP Extensions we support */
 #define SUPPORTED_EXTENSIONS "replaces" 
 
+/*! \brief Standard SIP port from RFC 3261. DO NOT CHANGE THIS */
+#define STANDARD_SIP_PORT	5060
+/* Note: in many SIP headers, absence of a port number implies port 5060,
+ * and this is why we cannot change the above constant.
+ * There is a limited number of places in asterisk where we could,
+ * in principle, use a different "default" port number, but
+ * we do not support this feature at the moment.
+ */
 
 /* Default values, set and reset in reload_config before reading configuration */
 /* These are default values in the source. There are other recommended values in the
    sip.conf.sample for new installations. These may differ to keep backwards compatibility,
    yet encouraging new behaviour on new installations 
  */
-#define DEFAULT_SIP_PORT	5060	/*!< From RFC 3261 (former 2543) */
 #define DEFAULT_CONTEXT		"default"
 #define DEFAULT_MOHINTERPRET    "default"
 #define DEFAULT_MOHSUGGEST      ""
@@ -527,6 +534,7 @@
 static int allow_external_domains;	/*!< Accept calls to external SIP domains? */
 static int global_callevents;		/*!< Whether we send manager events or not */
 static int global_t1min;		/*!< T1 roundtrip time minimum */
+static int global_autoframing;          /*!< ?????????? */
 static enum transfermodes global_allowtransfer;	/*!< SIP Refer restriction scheme */
 
 /*! \brief Codecs that we support by default: */
@@ -543,7 +551,6 @@
 
 static struct ast_flags global_flags[2] = {{0}};        /*!< global SIP_ flags */
 
-static int global_autoframing = 0;
 
 /*! \brief Protect the SIP dialog list (of sip_pvt's) */
 AST_MUTEX_DEFINE_STATIC(iflock);
@@ -2101,7 +2108,7 @@
 	if (sip_debug_test_pvt(p)) {
 		const struct sockaddr_in *dst = sip_real_dst(p);
 
-		ast_verbose("%sTransmitting (%s) to %s:%d:\n%s\n---\n",
+		ast_verbose("\n<--- %sTransmitting (%s) to %s:%d --->\n%s\n<------------>\n",
 			reliable ? "Reliably " : "", sip_nat_mode(p),
 			ast_inet_ntoa(dst->sin_addr),
 			ntohs(dst->sin_port), req->data);
@@ -2506,13 +2513,33 @@
 	return u;
 }
 
+/*! \brief Set nat mode on the various data sockets */
+static void do_setnat(struct sip_pvt *p, int natflags)
+{
+	const char *mode = natflags ? "On" : "Off";
+
+	if (p->rtp) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", mode);
+		ast_rtp_setnat(p->rtp, natflags);
+	}
+	if (p->vrtp) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", mode);
+		ast_rtp_setnat(p->vrtp, natflags);
+	}
+	if (p->udptl) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", mode);
+		ast_udptl_setnat(p->udptl, natflags);
+	}
+}
+
 /*! \brief Create address structure from peer reference.
  *  return -1 on error, 0 on success.
  */
 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
 {
-	int natflags;
-
 	if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
 	    (!peer->maxms || ((peer->lastms >= 0)  && (peer->lastms <= peer->maxms)))) {
 		dialog->sa = (peer->addr.sin_addr.s_addr) ? peer->addr : peer->defaddr;
@@ -2546,26 +2573,17 @@
 		ast_udptl_destroy(dialog->udptl);
 		dialog->udptl = NULL;
 	}
-	natflags = ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE;
+	do_setnat(dialog, ast_test_flag(&dialog->flags[0], SIP_NAT) & SIP_NAT_ROUTE );
+
 	if (dialog->rtp) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", natflags ? "On" : "Off");
-		ast_rtp_setnat(dialog->rtp, natflags);
 		ast_rtp_setdtmf(dialog->rtp, ast_test_flag(&dialog->flags[0], SIP_DTMF) != SIP_DTMF_INFO);
 		ast_rtp_setdtmfcompensate(dialog->rtp, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
 	}
 	if (dialog->vrtp) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", natflags ? "On" : "Off");
-		ast_rtp_setnat(dialog->vrtp, natflags);
 		ast_rtp_setdtmf(dialog->vrtp, 0);
 		ast_rtp_setdtmfcompensate(dialog->vrtp, 0);
 	}
-	if (dialog->udptl) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", natflags ? "On" : "Off");
-		ast_udptl_setnat(dialog->udptl, natflags);
-	}
+
 	/* Set Frame packetization */
 	if (dialog->rtp) {
 		ast_rtp_codec_setpref(dialog->rtp, &dialog->prefs);
@@ -2645,7 +2663,7 @@
 		return res;
 	}
 	hostn = peer;
-	portno = port ? atoi(port) : DEFAULT_SIP_PORT;
+	portno = port ? atoi(port) : STANDARD_SIP_PORT;
 	if (srvlookup) {
 		char service[MAXHOSTNAMELEN];
 		int tportno;
@@ -2907,7 +2925,7 @@
 static int update_call_counter(struct sip_pvt *fup, int event)
 {
 	char name[256];
-	int *inuse, *call_limit, *inringing = NULL;
+	int *inuse, *call_limit, *inringing;
 	int outgoing = ast_test_flag(&fup->flags[0], SIP_OUTGOING);
 	struct sip_user *u = NULL;
 	struct sip_peer *p = NULL;
@@ -2921,19 +2939,12 @@
 
 	ast_copy_string(name, fup->username, sizeof(name));
 
-	/* Check the list of users */
-	if (!outgoing)	/* Only check users for incoming calls */
-		u = find_user(name, 1);
-
-	if (u) {
+	/* Check the list of users only for incoming calls */
+	if (!outgoing && (u = find_user(name, 1)) ) {
 		inuse = &u->inUse;
 		call_limit = &u->call_limit;
-		p = NULL;
-	} else {
-		/* Try to find peer */
-		if (!p)
-			p = find_peer(fup->peername, NULL, 1);
-		if (p) {
+		inringing = NULL;
+	} else if ( (p = find_peer(fup->peername, NULL, 1) ) ) { /* Try to find peer */
 			inuse = &p->inUse;
 			call_limit = &p->call_limit;
 			inringing = &p->inRinging;
@@ -2943,7 +2954,7 @@
 				ast_log(LOG_DEBUG, "%s is not a local device, no call limit\n", name);
 			return 0;
 		}
-	}
+
 	switch(event) {
 		/* incoming and outgoing affects the inUse counter */
 		case DEC_CALL_LIMIT:
@@ -2966,6 +2977,7 @@
 				ast_log(LOG_DEBUG, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *call_limit);
 			}
 			break;
+
 		case INC_CALL_RINGING:
 		case INC_CALL_LIMIT:
 			if (*call_limit > 0 ) {
@@ -2991,6 +3003,7 @@
 				ast_log(LOG_DEBUG, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", u ? "user":"peer", name, *inuse, *call_limit);
 			}
 			break;
+
 		case DEC_CALL_RINGING:
 			if (inringing) {
 				if (ast_test_flag(&fup->flags[1], SIP_PAGE2_INC_RINGING)) {
@@ -3002,15 +3015,15 @@
 				}
 			}
 			break;
+
 		default:
 			ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
 	}
-	if (p)
+	if (p) {
 		ast_device_state_changed("SIP/%s", p->name);
-	if (u)
+		ASTOBJ_UNREF(p, sip_destroy_peer);
+	} else /* u must be set */
 		ASTOBJ_UNREF(u, sip_destroy_user);
-	else
-		ASTOBJ_UNREF(p, sip_destroy_peer);
 	return 0;
 }
 
@@ -3713,12 +3726,16 @@
 	}
 
 
+	{
+		const char *my_name;	/* pick a good name */
 	if (title)
-		 ast_string_field_build(tmp, name, "SIP/%s-%08x", title, (int)(long) i);
-	else if (strchr(i->fromdomain,':'))
-		ast_string_field_build(tmp, name, "SIP/%s-%08x", strchr(i->fromdomain,':') + 1, (int)(long) i);
+			my_name = title;
+		else if ( (my_name = strchr(i->fromdomain,':')) )
+			my_name++;	/* skip ':' */
 	else
-		ast_string_field_build(tmp, name, "SIP/%s-%08x", i->fromdomain, (int)(long) i);
+			my_name = i->fromdomain;
+		ast_string_field_build(tmp, name, "SIP/%s-%08x", my_name, (int)(long) i);
+	}
 
 	if (ast_test_flag(&i->flags[0], SIP_DTMF) ==  SIP_DTMF_INBAND) {
 		i->vad = ast_dsp_new();
@@ -4141,17 +4158,10 @@
 	}
 
 	if (useglobal_nat && sin) {
-		int natflags;
 		/* Setup NAT structure according to global settings if we have an address */
 		ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT);
 		p->recv = *sin;
-		natflags = ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE;
-		if (p->rtp)
-			ast_rtp_setnat(p->rtp, natflags);
-		if (p->vrtp)
-			ast_rtp_setnat(p->vrtp, natflags);
-		if (p->udptl)
-			ast_udptl_setnat(p->udptl, natflags);
+		do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT) & SIP_NAT_ROUTE);
 	}
 
 	if (p->method != SIP_REGISTER)
@@ -5317,14 +5327,14 @@
 	/* XXX bug here if string has been trimmed to sizeof(hostname) */
 	h += hn - 1;
 
-	/* Is "port" present? if not default to DEFAULT_SIP_PORT */
+	/* Is "port" present? if not default to STANDARD_SIP_PORT */
 	if (*h == ':') {
 		/* Parse port */
 		++h;
 		port = strtol(h, &h, 10);
 	}
 	else
-		port = DEFAULT_SIP_PORT;
+		port = STANDARD_SIP_PORT;
 
 	/* Got the hostname:port - but maybe there's a "maddr=" to override address? */
 	maddr = strstr(h, "maddr=");
@@ -6293,7 +6303,7 @@
 static void build_contact(struct sip_pvt *p)
 {
 	/* Construct Contact: header */
-	if (ourport != DEFAULT_SIP_PORT)	/* Needs to be 5060, according to the RFC */
+	if (ourport != STANDARD_SIP_PORT)
 		ast_string_field_build(p, our_contact, "<sip:%s%s%s:%d>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip), ourport);
 	else
 		ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten, ast_strlen_zero(p->exten) ? "" : "@", ast_inet_ntoa(p->ourip));
@@ -6389,31 +6399,25 @@
 	char tmp[BUFSIZ/2];
 	char tmp2[BUFSIZ/2];
 	const char *l = NULL, *n = NULL;
-	int x;
-	char urioptions[256]="";
+	const char *urioptions = "";
 
 	if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
-	 	char onlydigits = TRUE;
-		x=0;
+	 	const char *s = p->username;	/* being a string field, cannot be NULL */
 
 		/* Test p->username against allowed characters in AST_DIGIT_ANY
 			If it matches the allowed characters list, then sipuser = ";user=phone"
 			If not, then sipuser = ""
 		*/
 		/* + is allowed in first position in a tel: uri */
-        	if (p->username && p->username[0] == '+')
-			x=1;
-
-		for (; x < strlen(p->username); x++) {
-			if (!strchr(AST_DIGIT_ANYNUM, p->username[x])) {
-                		onlydigits = FALSE;
+		if (*s == '+')
+			s++;
+		for (; *s; s++) {
+			if (!strchr(AST_DIGIT_ANYNUM, *s) )
 				break;
 			}
-		}
-
 		/* If we have only digits, add ;user=phone to the uri */
-		if (onlydigits)
-			strcpy(urioptions, ";user=phone");
+		if (*s)
+			urioptions = ";user=phone";
 	}
 
 
@@ -6452,7 +6456,7 @@
 		l = tmp2;
 	}
 
-	if (ourport != DEFAULT_SIP_PORT && ast_strlen_zero(p->fromdomain))	/* Needs to be 5060 */
+	if (ourport != STANDARD_SIP_PORT && ast_strlen_zero(p->fromdomain))
 		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), ourport, p->tag);
 	else
 		snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, S_OR(p->fromdomain, ast_inet_ntoa(p->ourip)), p->tag);
@@ -6473,7 +6477,7 @@
 			ast_build_string(&invite, &invite_max, "%s@", n);
 		}
 		ast_build_string(&invite, &invite_max, "%s", p->tohost);
-		if (ntohs(p->sa.sin_port) != DEFAULT_SIP_PORT)		/* Needs to be 5060 */
+		if (ntohs(p->sa.sin_port) != STANDARD_SIP_PORT)
 			ast_build_string(&invite, &invite_max, ":%d", ntohs(p->sa.sin_port));
 		ast_build_string(&invite, &invite_max, "%s", urioptions);
 	}
@@ -6555,29 +6559,23 @@
 		add_header(&req, "Require", "replaces");
 	}
 
-	if (p->options && !ast_strlen_zero(p->options->distinctive_ring))
-		add_header(&req, "Alert-Info", p->options->distinctive_ring);
 	add_header(&req, "Allow", ALLOWED_METHODS);
 	add_header(&req, "Supported", SUPPORTED_EXTENSIONS);
-	if (p->options && p->options->addsipheaders ) {
-		struct ast_channel *ast;
-		struct varshead *headp = NULL;
-		const struct ast_var_t *current;
-
-		ast = p->owner;	/* The owner channel */
-		if (ast) {
-			char *headdup;
-	 		headp = &ast->varshead;
+	if (p->options && p->options->addsipheaders && p->owner) {
+		struct ast_channel *ast = p->owner; /* The owner channel */
+		struct varshead *headp = &ast->varshead;
+
 			if (!headp)
 				ast_log(LOG_WARNING,"No Headp for the channel...ooops!\n");
 			else {
+				const struct ast_var_t *current;
 				AST_LIST_TRAVERSE(headp, current, entries) {  
 					/* SIPADDHEADER: Add SIP header to outgoing call */
 					if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
 						char *content, *end;
 						const char *header = ast_var_value(current);
-
-						headdup = ast_strdupa(header);
+						char *headdup = ast_strdupa(header);
+
 						/* Strip of the starting " (if it's there) */
 						if (*headdup == '"')
 					 		headdup++;
@@ -6597,7 +6595,6 @@
 				}
 			}
 		}
-	}
 	if (sdp) {
 		if (p->udptl && p->t38.state == T38_LOCAL_DIRECT) {
 			ast_udptl_offered_from_local(p->udptl, 1);
@@ -7458,7 +7455,7 @@
 		*pt++ = '\0';
 		port = atoi(pt);
 	} else
-		port = DEFAULT_SIP_PORT;
+		port = 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 */
@@ -7565,7 +7562,7 @@
 		*pt++ = '\0';
 		port = atoi(pt);
 	} else
-		port = DEFAULT_SIP_PORT;
+		port = STANDARD_SIP_PORT;
 	oldsin = peer->addr;
 	if (!ast_test_flag(&peer->flags[0], SIP_NAT_ROUTE)) {
 		/* XXX This could block for a long time XXX */
@@ -8595,7 +8592,7 @@
 		memset(&p->sa, 0, sizeof(p->sa));
 		p->sa.sin_family = AF_INET;
 		memcpy(&p->sa.sin_addr, hp->h_addr, sizeof(p->sa.sin_addr));
-		p->sa.sin_port = htons(pt ? atoi(pt) : DEFAULT_SIP_PORT);
+		p->sa.sin_port = htons(pt ? atoi(pt) : STANDARD_SIP_PORT);
 
 		if (sip_debug_test_pvt(p)) {
 			const struct sockaddr_in *dst = sip_real_dst(p);
@@ -8699,7 +8696,6 @@
 	char calleridname[50];
 	int debug=sip_debug_test_addr(sin);
 	struct ast_variable *tmpvar = NULL, *v = NULL;
-	int usenatroute;
 	char *uri2 = ast_strdupa(uri);
 
 	/* Terminate URI */
@@ -8784,23 +8780,8 @@
 			ast_string_field_set(p, cid_num, tmp);
 		}
 		
-		usenatroute = ast_test_flag(&p->flags[0], SIP_NAT_ROUTE);
-
-		if (p->rtp) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", usenatroute ? "On" : "Off");
-			ast_rtp_setnat(p->rtp, usenatroute);
-		}
-		if (p->vrtp) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", usenatroute ? "On" : "Off");
-			ast_rtp_setnat(p->vrtp, usenatroute);
-		}
-		if (p->udptl) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", usenatroute ? "On" : "Off");
-			ast_udptl_setnat(p->udptl, usenatroute);
-		}
+		do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE) );
+
 		if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri2, reliable, ast_test_flag(req, SIP_PKT_IGNORE)))) {
 			sip_cancel_destroy(p);
 			ast_copy_flags(&p->flags[0], &user->flags[0], SIP_FLAGS_TO_COPY);
@@ -8906,22 +8887,8 @@
 					ast_shrink_phone_number(tmp);
 				ast_string_field_set(p, cid_num, tmp);
 			}
-			usenatroute = ast_test_flag(&p->flags[0], SIP_NAT_ROUTE);
-			if (p->rtp) {
-				if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on RTP to %s\n", usenatroute ? "On" : "Off");
-				ast_rtp_setnat(p->rtp, usenatroute);
-			}
-			if (p->vrtp) {
-				if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on VRTP to %s\n", usenatroute ? "On" : "Off");
-				ast_rtp_setnat(p->vrtp, usenatroute);
-			}
-			if (p->udptl) {
-				if (option_debug)
-				ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", usenatroute ? "On" : "Off");
-				ast_udptl_setnat(p->udptl, usenatroute);
-			}
+			do_setnat(p, ast_test_flag(&p->flags[0], SIP_NAT_ROUTE));
+
 			ast_string_field_set(p, peersecret, peer->secret);
 			ast_string_field_set(p, peermd5secret, peer->md5secret);
 			ast_string_field_set(p, subscribecontext, peer->subscribecontext);
@@ -10000,7 +9967,7 @@
 	ast_cli(fd, FORMAT2, "Host", "Username", "Refresh", "State", "Reg.Time");
 	ASTOBJ_CONTAINER_TRAVERSE(&regl, 1, do {
 		ASTOBJ_RDLOCK(iterator);
-		snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : DEFAULT_SIP_PORT);
+		snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
 		if (iterator->regtime) {
 			ast_localtime(&iterator->regtime, &tm, NULL);
 			strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
@@ -14330,12 +14297,11 @@
 	struct sockaddr_in sin = { 0, };
 	struct sip_pvt *p;
 	int res;
-	socklen_t len;
+	socklen_t len = sizeof(sin);
 	int nounlock;
 	int recount = 0;
-	unsigned int lockretry = 100;
-
-	len = sizeof(sin);
+	int lockretry;
+
 	memset(&req, 0, sizeof(req));
 	res = recvfrom(sipsock, req.data, sizeof(req.data) - 1, 0, (struct sockaddr *)&sin, &len);
 	if (res < 0) {
@@ -14359,22 +14325,19 @@
 	if (pedanticsipchecking)
 		req.len = lws2sws(req.data, req.len);	/* Fix multiline headers */
 	if (ast_test_flag(&req, SIP_PKT_DEBUG))
-		ast_verbose("\n<-- SIP read from %s:%d: \n%s\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
+		ast_verbose("\n<--- SIP read from %s:%d --->\n%s\n<------------->\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), req.data);
 
 	parse_request(&req);
 	req.method = find_sip_method(req.rlPart1);
-	if (ast_test_flag(&req, SIP_PKT_DEBUG)) {
+
+	if (ast_test_flag(&req, SIP_PKT_DEBUG))
 		ast_verbose("--- (%d headers %d lines)%s ---\n", req.headers, req.lines, (req.headers + req.lines == 0) ? " Nat keepalive" : "");
-	}
-
-	if (req.headers < 2) {
-		/* Must have at least two headers */
+
+	if (req.headers < 2)	/* Must have at least two headers */
 		return 1;
-	}
-
-
-	/* Process request, with netlock held */
-retrylock:
+
+	/* Process request, with netlock held, and with usual deadlock avoidance */
+	for (lockretry = 100; lockretry > 0; lockretry--) {
 	ast_mutex_lock(&netlock);
 
 	/* Find the active SIP dialog or create a new one */
@@ -14382,18 +14345,19 @@
 	if (p == NULL) {
 		if (option_debug)
 			ast_log(LOG_DEBUG, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
-	} else {
+			ast_mutex_unlock(&netlock);
+			return 1;
+		}
 		/* Go ahead and lock the owner if it has one -- we may need it */
 		/* becaues this is deadlock-prone, we need to try and unlock if failed */
-		if (p->owner && ast_channel_trylock(p->owner)) {
+		if (!p->owner || !ast_channel_trylock(p->owner))
+			break;	/* locking succeeded */
 			if (option_debug)
 				ast_log(LOG_DEBUG, "Failed to grab owner channel lock, trying again. (SIP call %s)\n", p->callid);
 			ast_mutex_unlock(&p->lock);
 			ast_mutex_unlock(&netlock);
 			/* Sleep for a very short amount of time */
 			usleep(1);
-			if (--lockretry)
-				goto retrylock;
 		}
 		p->recv = sin;
 
@@ -14401,7 +14365,7 @@
 			append_history(p, "Rx", "%s / %s / %s", req.data, get_header(&req, "CSeq"), req.rlPart2);
 
 		if (!lockretry) {
-			ast_log(LOG_ERROR, "We could NOT get the channel lock for %s! \n", p->owner->name ? p->owner->name : "- no channel name ??? - ");
+		ast_log(LOG_ERROR, "We could NOT get the channel lock for %s! \n", S_OR(p->owner->name, "- no channel name ??? - "));
 			ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
 			transmit_response(p, "503 Server error", &req);	/* We must respond according to RFC 3261 sec 12.2 */
 					/* XXX We could add retry-after to make sure they come back */
@@ -14418,7 +14382,6 @@
 		if (p->owner && !nounlock)
 			ast_channel_unlock(p->owner);
 		ast_mutex_unlock(&p->lock);
-	}
 	ast_mutex_unlock(&netlock);
 	if (recount)
 		ast_update_use_count();
@@ -15335,7 +15298,7 @@
 		*/
 		peer->expire = -1;
 		peer->pokeexpire = -1;
-		peer->addr.sin_port = htons(DEFAULT_SIP_PORT);
+		peer->addr.sin_port = htons(STANDARD_SIP_PORT);
 	}
 	ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
 	ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
@@ -15513,7 +15476,7 @@
 				else {
 					ast_copy_string(peer->tohost, v->value, sizeof(peer->tohost));
 					if (!peer->addr.sin_port)
-						peer->addr.sin_port = htons(DEFAULT_SIP_PORT);
+						peer->addr.sin_port = htons(STANDARD_SIP_PORT);
 				}
 			}
 		} else if (!strcasecmp(v->name, "defaultip")) {
@@ -15689,9 +15652,9 @@
 	memset(&localaddr, 0, sizeof(localaddr));
 	memset(&externip, 0, sizeof(externip));
 	memset(&default_prefs, 0 , sizeof(default_prefs));
-	outboundproxyip.sin_port = htons(DEFAULT_SIP_PORT);
+	outboundproxyip.sin_port = htons(STANDARD_SIP_PORT);
 	outboundproxyip.sin_family = AF_INET;	/* Type of address: IPv4 */
-	ourport = DEFAULT_SIP_PORT;
+	ourport = STANDARD_SIP_PORT;
 	srvlookup = DEFAULT_SRVLOOKUP;
 	global_tos_sip = DEFAULT_TOS_SIP;
 	global_tos_audio = DEFAULT_TOS_AUDIO;
@@ -15721,6 +15684,7 @@
 	pedanticsipchecking = DEFAULT_PEDANTIC;
 	global_mwitime = DEFAULT_MWITIME;
 	autocreatepeer = DEFAULT_AUTOCREATEPEER;
+	global_autoframing = 0;
 	global_allowguest = DEFAULT_ALLOWGUEST;
 	global_rtptimeout = 0;
 	global_rtpholdtimeout = 0;
@@ -16114,7 +16078,7 @@
 		return 0;
 	}
 	if (!ntohs(bindaddr.sin_port))
-		bindaddr.sin_port = ntohs(DEFAULT_SIP_PORT);
+		bindaddr.sin_port = ntohs(STANDARD_SIP_PORT);
 	bindaddr.sin_family = AF_INET;
 	ast_mutex_lock(&netlock);
 	if ((sipsock > -1) && (memcmp(&old_bindaddr, &bindaddr, sizeof(struct sockaddr_in)))) {
@@ -16690,9 +16654,9 @@
 {
 
 	ast_mutex_lock(&sip_reload_lock);
-	if (sip_reloading) {
+	if (sip_reloading) 
 		ast_verbose("Previous SIP reload not yet done\n");
-	} else {
+	else {
 		sip_reloading = TRUE;
 		if (fd)
 			sip_reloadreason = CHANNEL_CLI_RELOAD;
@@ -16929,6 +16893,7 @@
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
+/*! \brief PBX unload module API */
 static int unload_module(void)
 {
 	struct sip_pvt *p, *pl;
@@ -16936,20 +16901,26 @@
 	/* First, take us out of the channel type list */
 	ast_channel_unregister(&sip_tech);
 
+	/* Unregister dial plan functions */
 	ast_custom_function_unregister(&sipchaninfo_function);
 	ast_custom_function_unregister(&sippeer_function);
 	ast_custom_function_unregister(&sip_header_function);
 	ast_custom_function_unregister(&checksipdomain_function);
 
+	/* Unregister dial plan applications */
 	ast_unregister_application(app_dtmfmode);
 	ast_unregister_application(app_sipaddheader);
 
+	/* Unregister CLI commands */
 	ast_cli_unregister_multiple(cli_sip, sizeof(cli_sip) / sizeof(struct ast_cli_entry));
 
+	/* Disconnect from the RTP subsystem */
 	ast_rtp_proto_unregister(&sip_rtp);
 
+	/* Disconnect from UDPTL */
 	ast_udptl_proto_unregister(&sip_udptl);
 
+	/* Unregister AMI actions */
 	ast_manager_unregister("SIPpeers");
 	ast_manager_unregister("SIPshowpeer");
 



More information about the svn-commits mailing list