[asterisk-commits] bbryant: branch bbryant/ssl-tcp-tls r72305 - /team/bbryant/ssl-tcp-tls/channels/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jun 27 17:02:08 CDT 2007


Author: bbryant
Date: Wed Jun 27 17:02:08 2007
New Revision: 72305

URL: http://svn.digium.com/view/asterisk?view=rev&rev=72305
Log:
Cleanups of my sip code.

Modified:
    team/bbryant/ssl-tcp-tls/channels/chan_sip.c

Modified: team/bbryant/ssl-tcp-tls/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/bbryant/ssl-tcp-tls/channels/chan_sip.c?view=diff&rev=72305&r1=72304&r2=72305
==============================================================================
--- team/bbryant/ssl-tcp-tls/channels/chan_sip.c (original)
+++ team/bbryant/ssl-tcp-tls/channels/chan_sip.c Wed Jun 27 17:02:08 2007
@@ -1295,7 +1295,7 @@
 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
 
 #define get_transport(p) ( ( p->transport & SIP_TRANSPORT_UDP ) ? "UDP" : ( p->transport & SIP_TRANSPORT_TCP ) ? "TCP" : "TLS" )
-static int handle_request_do(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *nounlock);
+static int handle_request_do(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int transport, int fd);
 
 /*--- Transmitting responses and requests */
 static int sipsock_read(int *id, int fd, short events, void *ignore);
@@ -1716,7 +1716,7 @@
 static void *sip_tcp_helper_thread(void *data) {
 	struct sip_pvt *p;
 	struct server_instance *ser = data;
-	int lockretry, nounlock, cl;
+	int cl;
 	char buf[1024];
 	struct sip_request req = { 0, }, reqcpy = { 0, };
 
@@ -1748,67 +1748,7 @@
 			}
 		}
 
-		parse_request(&req);
-		req.method = find_sip_method(req.rlPart1);
-
-		if(sip_debug_test_addr(&ser->requestor))	/* Set the debug flag early on packet level */
-			ast_set_flag(&req, SIP_PKT_DEBUG);
-		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://%s:%d --->\n%s\n<------------->\n", 
-						(ser->parent->tls_cfg ? "TLS" : "TCP"), ast_inet_ntoa(ser->requestor.sin_addr), ntohs(ser->requestor.sin_port), req.data);
-
-		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 */
-			return NULL;
-
-		for (lockretry = 100; lockretry > 0; lockretry--) {
-			ast_mutex_lock(&netlock);
-
-			/* Find the active SIP dialog or create a new one */
-			p = find_call(&req, &ser->requestor, req.method);	/* returns p locked */
-
-			if (p == NULL) {
-				ast_debug(1, "Invalid SIP message - rejected , no callid, len %d\n", req.len);	
-				ast_mutex_unlock(&netlock);
-				return NULL;
-			}
-
-			p->transport = (ser->parent->tls_cfg) ? SIP_TRANSPORT_TLS : SIP_TRANSPORT_TCP;
-			p->ser = data;
-			/* 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))
-				break;	/* locking succeeded */
-			ast_debug(1, "Failed to grab owner channel lock, trying again. (SIP call %s)\n", p->callid);
-			sip_pvt_unlock(p);
-			ast_mutex_unlock(&netlock);
-			/* Sleep for a very short amount of time */
-			usleep(1);
-		}
-
-		p->recv = ser->requestor;
-
-		if (!lockretry) {
-			if (p->owner)
-				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);
-			if (req.method != SIP_ACK)
-				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 */
-			append_history(p, "LockFail", "Owner lock failed, transaction failed.");
-			return NULL;
-		}
-
-		handle_request_do(p, &req, &ser->requestor, &nounlock);
-
-		if (p->owner && !nounlock)
-			ast_channel_unlock(p->owner);
-		sip_pvt_unlock(p);
-		ast_mutex_unlock(&netlock);
+		handle_request_do(p, &req, &ser->requestor, ((ser->parent->tls_cfg) ? SIP_TRANSPORT_TLS : SIP_TRANSPORT_TCP), ser->fd);
 	}
 
 	return NULL;
@@ -2066,12 +2006,12 @@
 static int __sip_xmit(struct sip_pvt *p, char *data, int len)
 {
 	int res;
-	int fd;
+//	int fd;
 	const struct sockaddr_in *dst = sip_real_dst(p);
 
-	fd = (p->transport & SIP_TRANSPORT_UDP) ? sipsock : (p->ser) ? p->ser->fd : p->fd;
-
-	res = sendto(fd, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
+//	fd = (p->transport & SIP_TRANSPORT_UDP) ? sipsock : (p->ser) ? p->ser->fd : p->fd;
+
+	res = sendto(p->fd, data, len, 0, (const struct sockaddr *)dst, sizeof(struct sockaddr_in));
 
 	if (res == -1) {
 		switch (errno) {
@@ -3396,8 +3336,9 @@
 			char name[256];
 			struct server_args ca;
 
-			snprintf(ca.name, sizeof(ca.name), "SIP Call to %s", p->peername);
-
+			snprintf(name, sizeof(name), "SIP Call to %s", p->peername);
+
+			ca.name = name;
 			ca.accept_fd = -1;
 			ca.tls_cfg = (p->ser) ? p->ser->parent->tls_cfg : NULL;
 			ca.sin = p->sa;
@@ -3406,12 +3347,12 @@
 			if (!p->ser)
 				return -1;
 
-			p->fd = ca->accept_fd;
-
-			if (ast_pthread_create_background(&ca->master, NULL, sip_tcp_helper_thread, p->ser)) {
+			p->fd = ca.accept_fd;
+
+			if (ast_pthread_create_background(&ca.master, NULL, sip_tcp_helper_thread, p->ser)) {
 				ast_debug(1, "Unable to launch '%s'.", ca.name);
 				close(ca.accept_fd);
-				p->fd = ca->accept_fd = -1;
+				p->fd = ca.accept_fd = -1;
 				return -1;
 			}
 		}
@@ -15870,7 +15811,7 @@
 	struct sip_request req;
 	struct sockaddr_in sin = { 0, };
 	struct sip_pvt *p;
-	int res, lockretry, nounlock;
+	int res;
 	socklen_t len = sizeof(sin);
 
 	memset(&req, 0, sizeof(req));
@@ -15891,20 +15832,32 @@
 	} else
 		req.data[res] = '\0';
 	req.len = res;
-	if(sip_debug_test_addr(&sin))	/* Set the debug flag early on packet level */
-		ast_set_flag(&req, SIP_PKT_DEBUG);
+
+	handle_request_do(p, &req, &sin, SIP_TRANSPORT_UDP, sipsock);
+
+	return 1;
+}
+
+static int handle_request_do(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int transport, int fd) {
+	int recount = 0;
+	int nounlock = 0;
+	int lockretry;
+
+	if(sip_debug_test_addr(sin))	/* Set the debug flag early on packet level */
+		ast_set_flag(req, SIP_PKT_DEBUG);
 	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 udp://%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))
-		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 */
+		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://%s:%d --->\n%s\n<------------->\n", 
+				   (transport & SIP_TRANSPORT_UDP) ? "udp" : (transport & SIP_TRANSPORT_TCP) ? "tcp" : "tls", 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))
+		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 */
 		return 1;
 
 	/* Process request, with netlock held, and with usual deadlock avoidance */
@@ -15912,12 +15865,16 @@
 		ast_mutex_lock(&netlock);
 
 		/* Find the active SIP dialog or create a new one */
-		p = find_call(&req, &sin, req.method);	/* returns p locked */
+		p = find_call(req, sin, req->method);	/* returns p locked */
 		if (p == NULL) {
-			ast_debug(1, "Invalid SIP message - rejected , no callid, len %d\n", req.len);
+			ast_debug(1, "Invalid SIP message - rejected , no callid, len %d\n", req->len);
 			ast_mutex_unlock(&netlock);
 			return 1;
 		}
+
+		p->transport = transport;
+		p->fd = fd;
+
 		/* 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))
@@ -15928,43 +15885,34 @@
 		/* Sleep for a very short amount of time */
 		usleep(1);
 	}
-	p->recv = sin;
+	p->recv = *sin;
 
 	if (!lockretry) {
 		if (p->owner)
 			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);
-		if (req.method != SIP_ACK)
-			transmit_response(p, "503 Server error", &req);	/* We must respond according to RFC 3261 sec 12.2 */
+		if (req->method != SIP_ACK)
+			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 */
 		append_history(p, "LockFail", "Owner lock failed, transaction failed.");
 		return 1;
 	}
 
-	handle_request_do(p, &req, &sin, &nounlock);
+	if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) /* This is a request or response, note what it was for */
+		append_history(p, "Rx", "%s / %s / %s", req->data, get_header(req, "CSeq"), req->rlPart2);
+
+	if (handle_request(p, req, sin, &recount, &nounlock) == -1) {
+		/* Request failed */
+		ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
+	}
+		
+	if (recount)
+		ast_update_use_count();
 
 	if (p->owner && !nounlock)
 		ast_channel_unlock(p->owner);
 	sip_pvt_unlock(p);
 	ast_mutex_unlock(&netlock);
-
-	return 1;
-}
-
-static int handle_request_do(struct sip_pvt *p, struct sip_request *req, struct sockaddr_in *sin, int *nounlock) {
-	int recount = 0;
-
-	if (!ast_test_flag(&p->flags[0], SIP_NO_HISTORY)) /* This is a request or response, note what it was for */
-		append_history(p, "Rx", "%s / %s / %s", req->data, get_header(req, "CSeq"), req->rlPart2);
-
-	nounlock = 0;
-	if (handle_request(p, req, sin, &recount, nounlock) == -1) {
-		/* Request failed */
-		ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
-	}
-		
-	if (recount)
-		ast_update_use_count();
 
 	return 1;
 }




More information about the asterisk-commits mailing list