[asterisk-commits] wedhorn: branch wedhorn/skinny-session r383949 - /team/wedhorn/skinny-session...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Mar 27 02:59:07 CDT 2013


Author: wedhorn
Date: Wed Mar 27 02:59:03 2013
New Revision: 383949

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=383949
Log:
skinny: move guts of get_input and skinny_req_parse into skinny_session

Modified:
    team/wedhorn/skinny-session/channels/chan_skinny.c

Modified: team/wedhorn/skinny-session/channels/chan_skinny.c
URL: http://svnview.digium.com/svn/asterisk/team/wedhorn/skinny-session/channels/chan_skinny.c?view=diff&rev=383949&r1=383948&r2=383949
==============================================================================
--- team/wedhorn/skinny-session/channels/chan_skinny.c (original)
+++ team/wedhorn/skinny-session/channels/chan_skinny.c Wed Mar 27 02:59:03 2013
@@ -7431,144 +7431,140 @@
 	AST_LIST_UNLOCK(&sessions);
 }
 
-static int get_input(struct skinnysession *s)
+static void *skinny_session(void *data)
 {
 	int res;
+	struct skinny_req *req;
+	struct skinnysession *s = data;
+	
 	int dlen = 0;
-	int timeout = keep_alive * 1100;
+	int timeout;
 	time_t now;
 	int *bufaddr;
 	struct pollfd fds[1];
 
-	if (!s->device) {
-		if(time(&now) == -1) {
-			ast_log(LOG_ERROR, "error executing time(): %s\n", strerror(errno));
-			return -1;
-		}
-
-		timeout = (auth_timeout - (now - s->start)) * 1000;
-		if (timeout < 0) {
-			/* we have timed out */
-			ast_log(LOG_WARNING, "Skinny Client failed to authenticate in %d seconds\n", auth_timeout);
-			return -1;
-		}
-	}
-
-	fds[0].fd = s->fd;
-	fds[0].events = POLLIN;
-	fds[0].revents = 0;
-	res = ast_poll(fds, 1, timeout); /* If nothing has happen, client is dead */
-						 /* we add 10% to the keep_alive to deal */
-						 /* with network delays, etc */
-	if (res < 0) {
-		if (errno != EINTR) {
-			ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
-			return res;
-		}
-	} else if (res == 0) {
-		if (s->device) {
-			ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
+	ast_verb(3, "Starting Skinny session from %s\n", ast_inet_ntoa(s->sin.sin_addr));
+
+	for (;;) {
+
+		if (!s->device) {
+			if(time(&now) == -1) {
+				ast_log(LOG_ERROR, "error executing time(): %s\n", strerror(errno));
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
+			timeout = (auth_timeout - (now - s->start)) * 1000;
+			if (timeout < 0) {
+				/* we have timed out */
+				ast_log(LOG_WARNING, "Skinny Client failed to authenticate in %d seconds\n", auth_timeout);
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
 		} else {
-			ast_log(LOG_WARNING, "Skinny Client failed to authenticate in %d seconds\n", auth_timeout);
-		}
-		skinny_unregister(NULL, s);
-		return -1;
-	}
-
-	if (fds[0].revents) {
-		ast_mutex_lock(&s->lock);
-		memset(s->inbuf, 0, sizeof(s->inbuf));
-		res = read(s->fd, s->inbuf, 4);
+			timeout = keep_alive * 1100;
+		}
+
+		fds[0].fd = s->fd;
+		fds[0].events = POLLIN;
+		fds[0].revents = 0;
+		res = ast_poll(fds, 1, timeout); /* If nothing has happen, client is dead */
+								/* we add 10% to the keep_alive to deal */
+								/* with network delays, etc */
 		if (res < 0) {
-			ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
-
-			ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
-
+			if (errno != EINTR) {
+				ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
+		} else if (res == 0) {
+			if (s->device) {
+				ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
+			} else {
+				ast_log(LOG_WARNING, "Skinny Client failed to authenticate in %d seconds\n", auth_timeout);
+			}
 			skinny_unregister(NULL, s);
-			ast_mutex_unlock(&s->lock);
-			return res;
-		} else if (res != 4) {
-			ast_log(LOG_WARNING, "Skinny Client sent less data than expected.  Expected 4 but got %d.\n", res);
-			ast_mutex_unlock(&s->lock);
-
-			if (res == 0) {
-				ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
-				skinny_unregister(NULL, s);
-			}
-
-			return -1;
-		}
-
-		bufaddr = (int *)s->inbuf;
-		dlen = letohl(*bufaddr);
-		if (dlen < 4) {
-			ast_log(LOG_WARNING, "Skinny Client sent invalid data.\n");
-			ast_mutex_unlock(&s->lock);
-			return -1;
-		}
-		if (dlen+8 > sizeof(s->inbuf)) {
-			ast_log(LOG_WARNING, "Skinny packet too large (%d bytes), max length(%d bytes)\n", dlen+8, SKINNY_MAX_PACKET);
-			dlen = sizeof(s->inbuf) - 8;
-		}
-		*bufaddr = htolel(dlen);
-
-		res = read(s->fd, s->inbuf+4, dlen+4);
-		ast_mutex_unlock(&s->lock);
-		if (res < 0) {
-			ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
-			return res;
-		} else if (res != (dlen+4)) {
-			ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
-			return -1;
-		}
-		return res;
-	}
-	return 0;
-}
-
-static struct skinny_req *skinny_req_parse(struct skinnysession *s)
-{
-	struct skinny_req *req;
-	int *bufaddr;
-
-	if (!(req = ast_calloc(1, SKINNY_MAX_PACKET)))
-		return NULL;
-
-	ast_mutex_lock(&s->lock);
-	memcpy(req, s->inbuf, skinny_header_size);
-	bufaddr = (int *)(s->inbuf);
-	memcpy(&req->data, s->inbuf+skinny_header_size, letohl(*bufaddr)-4);
-
-	ast_mutex_unlock(&s->lock);
-
-	if (letohl(req->e) < 0) {
-		ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
-		ast_free(req);
-		return NULL;
-	}
-
-	return req;
-}
-
-static void *skinny_session(void *data)
-{
-	int res;
-	struct skinny_req *req;
-	struct skinnysession *s = data;
-
-	ast_verb(3, "Starting Skinny session from %s\n", ast_inet_ntoa(s->sin.sin_addr));
-
-	for (;;) {
-		res = get_input(s);
-		if (res < 0) {
 			ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
 			destroy_session(s);
 			return NULL;
 		}
 
-		if (res > 0)
-		{
-			if (!(req = skinny_req_parse(s))) {
+		if (fds[0].revents) {
+			ast_mutex_lock(&s->lock);
+			memset(s->inbuf, 0, sizeof(s->inbuf));
+			res = read(s->fd, s->inbuf, 4);
+			if (res < 0) {
+				ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
+
+				ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
+
+				skinny_unregister(NULL, s);
+				ast_mutex_unlock(&s->lock);
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			} else if (res != 4) {
+				ast_log(LOG_WARNING, "Skinny Client sent less data than expected.  Expected 4 but got %d.\n", res);
+				ast_mutex_unlock(&s->lock);
+
+				if (res == 0) {
+					ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
+					skinny_unregister(NULL, s);
+				}
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
+
+			bufaddr = (int *)s->inbuf;
+			dlen = letohl(*bufaddr);
+			if (dlen < 4) {
+				ast_log(LOG_WARNING, "Skinny Client sent invalid data.\n");
+				ast_mutex_unlock(&s->lock);
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
+			if (dlen+8 > sizeof(s->inbuf)) {
+				ast_log(LOG_WARNING, "Skinny packet too large (%d bytes), max length(%d bytes)\n", dlen+8, SKINNY_MAX_PACKET);
+				dlen = sizeof(s->inbuf) - 8;
+			}
+			*bufaddr = htolel(dlen);
+
+			res = read(s->fd, s->inbuf+4, dlen+4);
+			ast_mutex_unlock(&s->lock);
+			if (res < 0) {
+				ast_log(LOG_WARNING, "read() returned error: %s\n", strerror(errno));
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			} else if (res != (dlen+4)) {
+				ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
+				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
+		}
+
+		if (res > 0) {
+			if (!(req = ast_calloc(1, SKINNY_MAX_PACKET))) {
+				ast_verb(3, "Ending Skinny session from %s (failed parse)\n", ast_inet_ntoa(s->sin.sin_addr));
+				destroy_session(s);
+				return NULL;
+			}
+
+			ast_mutex_lock(&s->lock);
+			memcpy(req, s->inbuf, skinny_header_size);
+			bufaddr = (int *)(s->inbuf);
+			memcpy(&req->data, s->inbuf+skinny_header_size, letohl(*bufaddr)-4);
+
+			ast_mutex_unlock(&s->lock);
+
+			if (letohl(req->e) < 0) {
+				ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
+				ast_free(req);
 				ast_verb(3, "Ending Skinny session from %s (failed parse)\n", ast_inet_ntoa(s->sin.sin_addr));
 				destroy_session(s);
 				return NULL;




More information about the asterisk-commits mailing list