[asterisk-commits] jpdionne: branch group/v6-new r260340 - in /team/group/v6-new: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 30 08:51:09 CDT 2010


Author: jpdionne
Date: Fri Apr 30 08:51:06 2010
New Revision: 260340

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=260340
Log:
Create "IP version-independent" netsock functions and other utility functions

- Created main/netsock2.c and include/asterisk/netsock2.h. These contain the new
  version-independ core networking API.

- Converted main/tcptls.c and include/asterisk/tcptls.h to use the new API.

- Created a new configuration option type in main/config.c for parsing IPv4 or
  IPv6 addresses.

- Converted main/http.c to use the new tcptls.


Modified:
    team/group/v6-new/include/asterisk/config.h
    team/group/v6-new/include/asterisk/tcptls.h
    team/group/v6-new/main/config.c
    team/group/v6-new/main/http.c
    team/group/v6-new/main/tcptls.c

Modified: team/group/v6-new/include/asterisk/config.h
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/include/asterisk/config.h?view=diff&rev=260340&r1=260339&r2=260340
==============================================================================
--- team/group/v6-new/include/asterisk/config.h (original)
+++ team/group/v6-new/include/asterisk/config.h Fri Apr 30 08:51:06 2010
@@ -482,6 +482,14 @@
 	PARSE_INT16	= 	0x0004,
 	PARSE_UINT16	= 	0x0005,
 #endif
+
+	/* Returns a struct ast_sockaddr, with optional default value
+	 * (passed by reference) and port handling (accept, ignore,
+	 * require, forbid). The format is 'host.name[:port]'. IPv6 address
+	 * literals need square brackets around them if a port is specified.
+	 */
+	PARSE_ADDR	=	0x000e,
+
 	/* Returns a struct sockaddr_in, with optional default value
 	 * (passed by reference) and port handling (accept, ignore,
 	 * require, forbid). The format is 'host.name[:port]'

Modified: team/group/v6-new/include/asterisk/tcptls.h
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/include/asterisk/tcptls.h?view=diff&rev=260340&r1=260339&r2=260340
==============================================================================
--- team/group/v6-new/include/asterisk/tcptls.h (original)
+++ team/group/v6-new/include/asterisk/tcptls.h Fri Apr 30 08:51:06 2010
@@ -48,6 +48,7 @@
 #ifndef _ASTERISK_TCPTLS_H
 #define _ASTERISK_TCPTLS_H
 
+#include "asterisk/netsock2.h"
 #include "asterisk/utils.h"
 
 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
@@ -120,9 +121,9 @@
  * arguments for the accepting thread
  */
 struct ast_tcptls_session_args {
-	struct sockaddr_in local_address;
-	struct sockaddr_in old_address; /*!< copy of the local or remote address depending on if its a client or server session */
-	struct sockaddr_in remote_address;
+	struct ast_sockaddr local_address;
+	struct ast_sockaddr old_address; /*!< copy of the local or remote address depending on if its a client or server session */
+	struct ast_sockaddr remote_address;
 	char hostname[MAXHOSTNAMELEN]; /*!< only necessary for SSL clients so we can compare to common name */
 	struct ast_tls_config *tls_cfg; /*!< points to the SSL configuration if any */
 	int accept_fd;
@@ -143,7 +144,7 @@
 	SSL *ssl;   /* ssl state */
 /*	iint (*ssl_setup)(SSL *); */
 	int client;
-	struct sockaddr_in remote_address;
+	struct ast_sockaddr remote_address;
 	struct ast_tcptls_session_args *parent;
 	ast_mutex_t lock;
 };

Modified: team/group/v6-new/main/config.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/config.c?view=diff&rev=260340&r1=260339&r2=260340
==============================================================================
--- team/group/v6-new/main/config.c (original)
+++ team/group/v6-new/main/config.c Fri Apr 30 08:51:06 2010
@@ -47,6 +47,7 @@
 #include "asterisk/app.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/strings.h"	/* for the ast_str_*() API */
+#include "asterisk/netsock2.h"
 
 #define MAX_NESTED_COMMENTS 128
 #define COMMENT_START ";--"
@@ -2389,7 +2390,19 @@
 			result ? *result : x, error);
 		break;
 	    }
-	case PARSE_INADDR:
+	case PARSE_ADDR:
+	    {
+		struct ast_sockaddr addr;
+
+		if (!ast_sockaddr_parse(&addr, arg, flags & PARSE_PORT_MASK))
+			error = 1;
+
+		ast_debug(3, "extract addr from %s gives %s(%d)\n",
+			  arg, ast_sockaddr_stringify(&addr), error);
+
+		break;
+	    }
+	case PARSE_INADDR:	/* TODO Remove this (use PARSE_ADDR instead). */
 	    {
 		char *port, *buf;
 		struct sockaddr_in _sa_buf;	/* buffer for the result */

Modified: team/group/v6-new/main/http.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/http.c?view=diff&rev=260340&r1=260339&r2=260340
==============================================================================
--- team/group/v6-new/main/http.c (original)
+++ team/group/v6-new/main/http.c Fri Apr 30 08:51:06 2010
@@ -316,12 +316,10 @@
 
 	ast_str_append(&out, 0, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
 	ast_str_append(&out, 0, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
-		       ast_inet_ntoa(http_desc.old_address.sin_addr));
-	ast_str_append(&out, 0, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
-		       ntohs(http_desc.old_address.sin_port));
+		       ast_sockaddr_stringify(&http_desc.old_address));
 	if (http_tls_cfg.enabled) {
-		ast_str_append(&out, 0, "<tr><td><i>SSL Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
-			       ntohs(https_desc.old_address.sin_port));
+		ast_str_append(&out, 0, "<tr><td><i>SSL Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
+			       ast_sockaddr_stringify(&https_desc.old_address));
 	}
 	ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
 	for (v = get_vars; v; v = v->next) {
@@ -983,8 +981,6 @@
 	struct ast_variable *v;
 	int enabled=0;
 	int newenablestatic=0;
-	struct hostent *hp;
-	struct ast_hostent ahp;
 	char newprefix[MAX_PREFIX] = "";
 	struct http_uri_redirect *redirect;
 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
@@ -995,11 +991,8 @@
 	}
 
 	/* default values */
-	memset(&http_desc.local_address, 0, sizeof(http_desc.local_address));
-	http_desc.local_address.sin_port = htons(8088);
-
-	memset(&https_desc.local_address, 0, sizeof(https_desc.local_address));
-	https_desc.local_address.sin_port = htons(8089);
+	ast_sockaddr_parse(&http_desc.local_address, "[::]:8088", 0);
+	ast_sockaddr_parse(&https_desc.local_address, "[::]:8089", 0);
 
 	http_tls_cfg.enabled = 0;
 	if (http_tls_cfg.certfile) {
@@ -1036,14 +1029,10 @@
 				enabled = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "enablestatic")) {
 				newenablestatic = ast_true(v->value);
-			} else if (!strcasecmp(v->name, "bindport")) {
-				http_desc.local_address.sin_port = htons(atoi(v->value));
-			} else if (!strcasecmp(v->name, "bindaddr")) {
-				if ((hp = ast_gethostbyname(v->value, &ahp))) {
-					memcpy(&http_desc.local_address.sin_addr, hp->h_addr, sizeof(http_desc.local_address.sin_addr));
-				} else {
+			} else if (!strcasecmp(v->name, "bind")) {
+				if (!ast_sockaddr_parse(&http_desc.local_address,
+							v->value, 0))
 					ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
-				}
 			} else if (!strcasecmp(v->name, "prefix")) {
 				if (!ast_strlen_zero(v->value)) {
 					newprefix[0] = '/';
@@ -1060,12 +1049,9 @@
 
 		ast_config_destroy(cfg);
 	}
-	/* if the https addres has not been set, default is the same as non secure http */
-	if (!https_desc.local_address.sin_addr.s_addr) {
-		https_desc.local_address.sin_addr = http_desc.local_address.sin_addr;
-	}
-	if (enabled) {
-		http_desc.local_address.sin_family = https_desc.local_address.sin_family = AF_INET;
+	if (!enabled) {
+		ast_sockaddr_setnull(&http_desc.local_address);
+		ast_sockaddr_setnull(&https_desc.local_address);
 	}
 	if (strcmp(prefix, newprefix)) {
 		ast_copy_string(prefix, newprefix, sizeof(prefix));
@@ -1100,16 +1086,14 @@
 	}
 	ast_cli(a->fd, "HTTP Server Status:\n");
 	ast_cli(a->fd, "Prefix: %s\n", prefix);
-	if (!http_desc.old_address.sin_family) {
+	if (ast_sockaddr_isnull(&http_desc.old_address)) {
 		ast_cli(a->fd, "Server Disabled\n\n");
 	} else {
-		ast_cli(a->fd, "Server Enabled and Bound to %s:%d\n\n",
-			ast_inet_ntoa(http_desc.old_address.sin_addr),
-			ntohs(http_desc.old_address.sin_port));
+		ast_cli(a->fd, "Server Enabled and Bound to %s\n\n",
+			ast_sockaddr_stringify(&http_desc.old_address));
 		if (http_tls_cfg.enabled) {
-			ast_cli(a->fd, "HTTPS Server Enabled and Bound to %s:%d\n\n",
-				ast_inet_ntoa(https_desc.old_address.sin_addr),
-				ntohs(https_desc.old_address.sin_port));
+			ast_cli(a->fd, "HTTPS Server Enabled and Bound to %s\n\n",
+				ast_sockaddr_stringify(&https_desc.old_address));
 		}
 	}
 

Modified: team/group/v6-new/main/tcptls.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/tcptls.c?view=diff&rev=260340&r1=260339&r2=260340
==============================================================================
--- team/group/v6-new/main/tcptls.c (original)
+++ team/group/v6-new/main/tcptls.c Fri Apr 30 08:51:06 2010
@@ -235,8 +235,7 @@
 {
 	struct ast_tcptls_session_args *desc = data;
 	int fd;
-	struct sockaddr_in sin;
-	socklen_t sinlen;
+	struct ast_sockaddr addr;
 	struct ast_tcptls_session_instance *tcptls_session;
 	pthread_t launched;
 
@@ -248,8 +247,7 @@
 		i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
 		if (i <= 0)
 			continue;
-		sinlen = sizeof(sin);
-		fd = accept(desc->accept_fd, (struct sockaddr *) &sin, &sinlen);
+		fd = ast_accept(desc->accept_fd, &addr);
 		if (fd < 0) {
 			if ((errno != EAGAIN) && (errno != EINTR))
 				ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
@@ -268,7 +266,7 @@
 		fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
 		tcptls_session->fd = fd;
 		tcptls_session->parent = desc;
-		memcpy(&tcptls_session->remote_address, &sin, sizeof(tcptls_session->remote_address));
+		ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
 
 		tcptls_session->client = 0;
 
@@ -373,10 +371,10 @@
 		goto client_start_error;
 	}
 
-	if (connect(desc->accept_fd, (const struct sockaddr *) &desc->remote_address, sizeof(desc->remote_address))) {
-		ast_log(LOG_ERROR, "Unable to connect %s to %s:%d: %s\n",
+	if (ast_connect(desc->accept_fd, &desc->remote_address)) {
+		ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",
 			desc->name,
-			ast_inet_ntoa(desc->remote_address.sin_addr), ntohs(desc->remote_address.sin_port),
+			ast_sockaddr_stringify(&desc->remote_address),
 			strerror(errno));
 		goto client_start_error;
 	}
@@ -407,12 +405,12 @@
 	struct ast_tcptls_session_instance *tcptls_session = NULL;
 
 	/* Do nothing if nothing has changed */
-	if (!memcmp(&desc->old_address, &desc->remote_address, sizeof(desc->old_address))) {
+	if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
 		ast_debug(1, "Nothing changed in %s\n", desc->name);
 		return NULL;
 	}
 
-	desc->old_address = desc->remote_address;
+	ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
 
 	if (desc->accept_fd != -1)
 		close(desc->accept_fd);
@@ -426,12 +424,12 @@
 
 	/* if a local address was specified, bind to it so the connection will
 	   originate from the desired address */
-	if (desc->local_address.sin_family != 0) {
+	if (!ast_sockaddr_isnull(&desc->local_address)) {
 		setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
-		if (bind(desc->accept_fd, (struct sockaddr *) &desc->local_address, sizeof(desc->local_address))) {
-			ast_log(LOG_ERROR, "Unable to bind %s to %s:%d: %s\n",
-			desc->name,
-				ast_inet_ntoa(desc->local_address.sin_addr), ntohs(desc->local_address.sin_port),
+		if (ast_bind(desc->accept_fd, &desc->local_address)) {
+			ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
+				desc->name,
+				ast_sockaddr_stringify(&desc->local_address),
 				strerror(errno));
 			goto error;
 		}
@@ -445,7 +443,8 @@
 	tcptls_session->fd = desc->accept_fd;
 	tcptls_session->parent = desc;
 	tcptls_session->parent->worker_fn = NULL;
-	memcpy(&tcptls_session->remote_address, &desc->remote_address, sizeof(tcptls_session->remote_address));
+	ast_sockaddr_copy(&tcptls_session->remote_address,
+			  &desc->remote_address);
 
 	return tcptls_session;
 
@@ -463,12 +462,12 @@
 	int x = 1;
 
 	/* Do nothing if nothing has changed */
-	if (!memcmp(&desc->old_address, &desc->local_address, sizeof(desc->old_address))) {
+	if (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
 		ast_debug(1, "Nothing changed in %s\n", desc->name);
 		return;
 	}
 
-	desc->old_address = desc->local_address;
+	ast_sockaddr_copy(&desc->old_address, &desc->local_address);
 
 	/* Shutdown a running server if there is one */
 	if (desc->master != AST_PTHREADT_NULL) {
@@ -481,7 +480,7 @@
 		close(desc->accept_fd);
 
 	/* If there's no new server, stop here */
-	if (desc->local_address.sin_family == 0) {
+	if (ast_sockaddr_isnull(&desc->local_address)) {
 		ast_debug(2, "Server disabled:  %s\n", desc->name);
 		return;
 	}
@@ -493,10 +492,10 @@
 	}
 
 	setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
-	if (bind(desc->accept_fd, (struct sockaddr *) &desc->local_address, sizeof(desc->local_address))) {
-		ast_log(LOG_ERROR, "Unable to bind %s to %s:%d: %s\n",
+	if (ast_bind(desc->accept_fd, &desc->local_address)) {
+		ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
 			desc->name,
-			ast_inet_ntoa(desc->local_address.sin_addr), ntohs(desc->local_address.sin_port),
+			ast_sockaddr_stringify(&desc->local_address),
 			strerror(errno));
 		goto error;
 	}
@@ -507,9 +506,9 @@
 	flags = fcntl(desc->accept_fd, F_GETFL);
 	fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
 	if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
-		ast_log(LOG_ERROR, "Unable to launch thread for %s on %s:%d: %s\n",
+		ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
 			desc->name,
-			ast_inet_ntoa(desc->local_address.sin_addr), ntohs(desc->local_address.sin_port),
+			ast_sockaddr_stringify(&desc->local_address),
 			strerror(errno));
 		goto error;
 	}
@@ -537,7 +536,6 @@
 {
 	if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
 		tls_cfg->enabled = ast_true(value) ? 1 : 0;
-		tls_desc->local_address.sin_family = AF_INET;
 	} else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
 		ast_free(tls_cfg->certfile);
 		tls_cfg->certfile = ast_strdup(value);
@@ -557,11 +555,9 @@
 		ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
 	} else if (!strcasecmp(varname, "tlsdontverifyserver")) {
 		ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
-	} else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
-		if (ast_parse_arg(value, PARSE_INADDR, &tls_desc->local_address))
+	} else if (!strcasecmp(varname, "tlsbind") || !strcasecmp(varname, "sslbind")) {
+		if (ast_parse_arg(value, PARSE_ADDR, &tls_desc->local_address))
 			ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
-	} else if (!strcasecmp(varname, "tlsbindport") || !strcasecmp(varname, "sslbindport")) {
-		tls_desc->local_address.sin_port = htons(atoi(value));
 	} else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
 		if (!strcasecmp(value, "tlsv1")) {
 			ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);




More information about the asterisk-commits mailing list