[asterisk-commits] file: branch group/pimp_my_sip r380191 - in /team/group/pimp_my_sip: configs/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jan 27 13:46:27 CST 2013


Author: file
Date: Sun Jan 27 13:46:23 2013
New Revision: 380191

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380191
Log:
Add support for configuring transports from res_sip.conf and add a small sample config file.

Added:
    team/group/pimp_my_sip/configs/res_sip.conf.sample   (with props)
    team/group/pimp_my_sip/res/res_sip/
    team/group/pimp_my_sip/res/res_sip/config_transport.c   (with props)
Modified:
    team/group/pimp_my_sip/include/asterisk/res_sip.h
    team/group/pimp_my_sip/res/res_sip.c

Added: team/group/pimp_my_sip/configs/res_sip.conf.sample
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/configs/res_sip.conf.sample?view=auto&rev=380191
==============================================================================
--- team/group/pimp_my_sip/configs/res_sip.conf.sample (added)
+++ team/group/pimp_my_sip/configs/res_sip.conf.sample Sun Jan 27 13:46:23 2013
@@ -1,0 +1,14 @@
+; This is an in-flux configuration file for the res_sip module, it will change as things progress
+
+;;; Transports
+[local]
+type=transport
+protocol=udp ; Supported protocols are udp, tcp, and tls
+bind=0.0.0.0 ; This supports both IPv4 and IPv6, port is optional
+
+;;; Endpoints
+[endpoint]
+type=endpoint
+context=default
+disallow=all
+allow=ulaw

Propchange: team/group/pimp_my_sip/configs/res_sip.conf.sample
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/group/pimp_my_sip/configs/res_sip.conf.sample
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/group/pimp_my_sip/configs/res_sip.conf.sample
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/group/pimp_my_sip/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/include/asterisk/res_sip.h?view=diff&rev=380191&r1=380190&r2=380191
==============================================================================
--- team/group/pimp_my_sip/include/asterisk/res_sip.h (original)
+++ team/group/pimp_my_sip/include/asterisk/res_sip.h Sun Jan 27 13:46:23 2013
@@ -28,17 +28,33 @@
 #include "asterisk/channel.h"
 /* Needed for ast_sorcery */
 #include "asterisk/sorcery.h"
+/* Needed for pj_sockaddr */
+#include "pjlib.h"
 
 /* Forward declarations of PJSIP stuff */
 struct pjsip_rx_data;
 struct pjsip_module;
 struct pjsip_tx_data;
 struct pjsip_dialog;
+struct pjsip_transport;
+struct pjsip_tpfactory;
+struct pjsip_tls_setting;
 
 /*!
  * \brief Opaque structure representing related SIP tasks
  */
 struct ast_sip_work;
+
+/*!
+ * \brief Structure for SIP transport information
+ */
+struct ast_sip_transport_state {
+	/*! \brief Transport itself */
+	struct pjsip_transport *transport;
+
+	/*! \brief Transport factory */
+	struct pjsip_tpfactory *factory;
+};
 
 /*!
  * Details about a SIP domain
@@ -62,18 +78,37 @@
 	/* XXX Websocket ? */
 };
 
+/*! \brief Maximum number of ciphers supported for a TLS transport */
+#define SIP_TLS_MAX_CIPHERS 64
+
 /*!
  * \brief Transport to bind to
  */
 struct ast_sip_transport {
+	/*! Sorcery object details */
+	SORCERY_OBJECT(details);
 	AST_DECLARE_STRING_FIELDS(
-		/*! Name for identifying the transport */
-		AST_STRING_FIELD(name);
-	);
+		/*! Certificate of authority list file */
+		AST_STRING_FIELD(ca_list_file);
+		/*! Public certificate file */
+		AST_STRING_FIELD(cert_file);
+		/*! Optional private key of the certificate file */
+		AST_STRING_FIELD(privkey_file);
+		/*! Password to open the private key */
+		AST_STRING_FIELD(password);
+		);
 	/*! Type of transport */
 	enum ast_sip_transport_type type;
 	/*! Address and port to bind to */
-	struct ast_sockaddr host;
+	pj_sockaddr host;
+	/*! Number of simultaneous asynchronous operations */
+	unsigned int async_operations;
+	/*! TLS settings */
+	pjsip_tls_setting tls;
+	/*! Configured TLS ciphers */
+	pj_ssl_cipher ciphers[SIP_TLS_MAX_CIPHERS];
+	/*! Transport state information */
+	struct ast_sip_transport_state *state;
 };
 
 /*!
@@ -395,6 +430,16 @@
 struct ast_sorcery *ast_sip_get_sorcery(void);
 
 /*!
+ * \brief Initialize transport support on a sorcery instance
+ *
+ * \param sorcery The sorcery instance
+ *
+ * \retval -1 failure
+ * \retval 0 success
+ */
+int ast_sip_initialize_sorcery_transport(struct ast_sorcery *sorcery);
+
+/*!
  * \brief Create a new SIP work structure
  *
  * A SIP work is a means of grouping together SIP tasks. For instance, one

Modified: team/group/pimp_my_sip/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip.c?view=diff&rev=380191&r1=380190&r2=380191
==============================================================================
--- team/group/pimp_my_sip/res/res_sip.c (original)
+++ team/group/pimp_my_sip/res/res_sip.c Sun Jan 27 13:46:23 2013
@@ -152,6 +152,13 @@
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, prefs, codecs));
 	ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "host", "", host_handler, NULL, 0, 0);
 
+	if (ast_sip_initialize_sorcery_transport(sip_sorcery)) {
+		ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
+		ast_sorcery_unref(sip_sorcery);
+		sip_sorcery = NULL;
+		return -1;
+	}
+
 	return 0;
 }
 
@@ -635,33 +642,6 @@
 	return NULL;
 }
 
-static int start_transports(void)
-{
-	/* XXX In reality, this should use sorcery to determine
-	 * configured transports and add them to ast_pjsip_endpoint.
-	 * However, our initial goal is to get just *something* working
-	 * so we're hardcoding our sole transport to UDP 127.0.0.1:5060
-	 */
-	pjsip_transport *hardcoded_transport = NULL;
-	pj_sockaddr addr;
-	pj_str_t home = { "0.0.0.0:5060", 12 };
-	pj_status_t status;
-	char errbuf[20];
-
-	if (pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &home, &addr) != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Failed to parse IP address\n");
-		return -1;
-	}
-
-	status = pjsip_udp_transport_start(ast_pjsip_endpoint, &addr.ipv4, NULL, 2, &hardcoded_transport);
-	if (status != PJ_SUCCESS) {
-		pj_strerror(status, errbuf, sizeof(errbuf));
-		ast_log(LOG_ERROR, "Failed to start PJSIP UDP transport, %s\n", errbuf);
-		return -1;
-	}
-	return 0;
-}
-
 static void stop_monitor_thread(void)
 {
 	monitor_continue = 0;
@@ -745,10 +725,6 @@
 
 	ast_sorcery_load(sip_sorcery);
 
-	if (start_transports()) {
-		ast_log(LOG_ERROR, "Failed to start SIP transports. Aborting load\n");
-		goto error;
-	}
 	return AST_MODULE_LOAD_SUCCESS;
 
 error:

Added: team/group/pimp_my_sip/res/res_sip/config_transport.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip/config_transport.c?view=auto&rev=380191
==============================================================================
--- team/group/pimp_my_sip/res/res_sip/config_transport.c (added)
+++ team/group/pimp_my_sip/res/res_sip/config_transport.c Sun Jan 27 13:46:23 2013
@@ -1,0 +1,248 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#include "asterisk.h"
+#undef bzero
+#define bzero bzero
+#include "pjsip.h"
+#include "pjlib.h"
+
+#include "asterisk/res_sip.h"
+#include "asterisk/logger.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/sorcery.h"
+
+/*! \brief Destructor for transport state information */
+static void transport_state_destroy(void *obj)
+{
+	struct ast_sip_transport_state *state = obj;
+
+	if (state->transport) {
+		pjsip_transport_shutdown(state->transport);
+	}
+}
+
+/*! \brief Destructor for transport */
+static void transport_destroy(void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+
+	ast_string_field_free_memory(transport);
+
+	ao2_cleanup(transport->state);
+}
+
+/*! \brief Allocator for transport */
+static void *transport_alloc(const char *name)
+{
+	struct ast_sip_transport *transport = ao2_alloc(sizeof(*transport), transport_destroy);
+
+	if (!transport) {
+		return NULL;
+	}
+
+	if (ast_string_field_init(transport, 256)) {
+		ao2_cleanup(transport);
+		return NULL;
+	}
+
+	pjsip_tls_setting_default(&transport->tls);
+	transport->tls.ciphers = transport->ciphers;
+
+	return transport;
+}
+
+/*! \brief Apply handler for transports */
+static void transport_apply(const struct ast_sorcery *sorcery, void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+	RAII_VAR(struct ast_sip_transport *, existing, ast_sorcery_retrieve_by_id(sorcery, "transport", ast_sorcery_object_get_id(obj)), ao2_cleanup);
+	pj_status_t res = -1;
+
+	if (!existing || !existing->state) {
+		if (!(transport->state = ao2_alloc(sizeof(*transport->state), transport_state_destroy))) {
+			ast_log(LOG_ERROR, "Transport state for '%s' could not be allocated\n", ast_sorcery_object_get_id(obj));
+			return;
+		}
+	} else {
+		transport->state = existing->state;
+		ao2_ref(transport->state, +1);
+	}
+
+	/* Once active a transport can not be reconfigured */
+	if (transport->state->transport || transport->state->factory) {
+		return;
+	}
+
+	/* Set default port if not present */
+	if (!pj_sockaddr_get_port(&transport->host)) {
+		pj_sockaddr_set_port(&transport->host, (transport->type == AST_SIP_TRANSPORT_TLS) ? 5061 : 5060);
+	}
+
+	/* TODO: Upgrade pjproject so we get IPv6 TCP and TLS */
+	if (transport->type == AST_SIP_TRANSPORT_UDP) {
+		if (transport->host.addr.sa_family == pj_AF_INET()) {
+			res = pjsip_udp_transport_start(ast_sip_get_pjsip_endpoint(), &transport->host.ipv4, NULL, transport->async_operations, &transport->state->transport);
+		} else if (transport->host.addr.sa_family == pj_AF_INET6()) {
+			res = pjsip_udp_transport_start6(ast_sip_get_pjsip_endpoint(), &transport->host.ipv6, NULL, transport->async_operations, &transport->state->transport);
+		}
+	} else if (transport->type == AST_SIP_TRANSPORT_TCP) {
+		if (transport->host.addr.sa_family == pj_AF_INET()) {
+			res = pjsip_tcp_transport_start(ast_sip_get_pjsip_endpoint(), &transport->host.ipv4, transport->async_operations, &transport->state->factory);
+		}
+	} else if (transport->type == AST_SIP_TRANSPORT_TLS) {
+		transport->tls.ca_list_file = pj_str((char*)transport->ca_list_file);
+		transport->tls.cert_file = pj_str((char*)transport->cert_file);
+		transport->tls.privkey_file = pj_str((char*)transport->privkey_file);
+		transport->tls.password = pj_str((char*)transport->password);
+
+		if (transport->host.addr.sa_family == pj_AF_INET()) {
+			res = pjsip_tls_transport_start(ast_sip_get_pjsip_endpoint(), &transport->tls, &transport->host.ipv4, NULL, transport->async_operations, &transport->state->factory);
+		}
+	}
+
+	if (res != PJ_SUCCESS) {
+		char msg[PJ_ERR_MSG_SIZE];
+
+		pjsip_strerror(res, msg, sizeof(msg));
+		ast_log(LOG_ERROR, "Transport '%s' could not be started: %s\n", ast_sorcery_object_get_id(obj), msg);
+	}
+}
+
+/*! \brief Custom handler for turning a string protocol into an enum */
+static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+
+	if (!strcasecmp(var->value, "udp")) {
+		transport->type = AST_SIP_TRANSPORT_UDP;
+	} else if (!strcasecmp(var->value, "tcp")) {
+		transport->type = AST_SIP_TRANSPORT_TCP;
+	} else if (!strcasecmp(var->value, "tls")) {
+		transport->type = AST_SIP_TRANSPORT_TLS;
+	} else {
+		/* TODO: Implement websockets */
+		return -1;
+	}
+
+	return 0;
+}
+
+/*! \brief Custom handler for turning a string bind into a pj_sockaddr */
+static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+	pj_str_t buf;
+
+	return (pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &transport->host) != PJ_SUCCESS) ? -1 : 0;
+}
+
+/*! \brief Custom handler for TLS boolean settings */
+static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+
+	if (!strcasecmp(var->name, "verify_server")) {
+		transport->tls.verify_server = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
+	} else if (!strcasecmp(var->name, "verify_client")) {
+		transport->tls.verify_client = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
+	} else if (!strcasecmp(var->name, "require_client_cert")) {
+		transport->tls.require_client_cert = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
+	} else {
+		return -1;
+	}
+
+	return 0;
+}
+
+/*! \brief Custom handler for TLS method setting */
+static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+
+	if (!strcasecmp(var->value, "default")) {
+		transport->tls.method = PJSIP_SSL_DEFAULT_METHOD;
+	} else if (!strcasecmp(var->value, "unspecified")) {
+		transport->tls.method = PJSIP_SSL_UNSPECIFIED_METHOD;
+	} else if (!strcasecmp(var->value, "tlsv1")) {
+		transport->tls.method = PJSIP_TLSV1_METHOD;
+	} else if (!strcasecmp(var->value, "sslv2")) {
+		transport->tls.method = PJSIP_SSLV2_METHOD;
+	} else if (!strcasecmp(var->value, "sslv3")) {
+		transport->tls.method = PJSIP_SSLV3_METHOD;
+	} else if (!strcasecmp(var->value, "sslv23")) {
+		transport->tls.method = PJSIP_SSLV23_METHOD;
+	} else {
+		return -1;
+	}
+
+	return 0;
+}
+
+/*! \brief Custom handler for TLS cipher setting */
+static int transport_tls_cipher_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct ast_sip_transport *transport = obj;
+	pj_ssl_cipher cipher;
+
+	if (transport->tls.ciphers_num == (SIP_TLS_MAX_CIPHERS - 1)) {
+		return -1;
+	}
+
+	/* TODO: Check this over/tweak - it's taken from pjsua for now */
+	if (!strnicmp(var->value, "0x", 2)) {
+		pj_str_t cipher_st = pj_str((char*)var->value + 2);
+		cipher = pj_strtoul2(&cipher_st, NULL, 16);
+	} else {
+		cipher = atoi(var->value);
+	}
+
+	if (pj_ssl_cipher_is_supported(cipher)) {
+		transport->ciphers[transport->tls.ciphers_num++] = cipher;
+		return 0;
+	} else {
+		ast_log(LOG_ERROR, "Cipher '%s' is unsupported\n", var->value);
+		return -1;
+	}
+}
+
+/*! \brief Initialize sorcery with transport support */
+int ast_sip_initialize_sorcery_transport(struct ast_sorcery *sorcery)
+{
+	ast_sorcery_apply_default(sorcery, "transport", "config", "res_sip.conf,criteria=type=transport");
+
+	if (ast_sorcery_object_register(sorcery, "transport", transport_alloc, NULL, transport_apply)) {
+		return -1;
+	}
+
+	ast_sorcery_object_field_register(sorcery, "transport", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
+	ast_sorcery_object_field_register(sorcery, "transport", "ca_list_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, ca_list_file));
+	ast_sorcery_object_field_register(sorcery, "transport", "cert_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, cert_file));
+	ast_sorcery_object_field_register(sorcery, "transport", "privkey_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, privkey_file));
+	ast_sorcery_object_field_register(sorcery, "transport", "password", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, password));
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, NULL, 0, 0);
+
+	return 0;
+}

Propchange: team/group/pimp_my_sip/res/res_sip/config_transport.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/group/pimp_my_sip/res/res_sip/config_transport.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/group/pimp_my_sip/res/res_sip/config_transport.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list