[asterisk-commits] qwell: branch group/pimp_my_sip r389004 - in /team/group/pimp_my_sip: include...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 17 14:38:21 CDT 2013


Author: qwell
Date: Fri May 17 14:38:17 2013
New Revision: 389004

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=389004
Log:
Add support for SIP over WebSocket in res_sip.

This uses a pjsip custom transport type.

(closes issue ASTERISK-20952)
Review: https://reviewboard.asterisk.org/r/2493/

Added:
    team/group/pimp_my_sip/res/res_sip_transport_websocket.c
      - copied unchanged from r389002, team/qwell/fun_with_transports/res/res_sip_transport_websocket.c
Modified:
    team/group/pimp_my_sip/include/asterisk/res_sip.h
    team/group/pimp_my_sip/res/res_sip.c
    team/group/pimp_my_sip/res/res_sip.exports.in
    team/group/pimp_my_sip/res/res_sip/config_transport.c
    team/group/pimp_my_sip/res/res_sip/include/res_sip_private.h
    team/group/pimp_my_sip/res/res_sip/location.c
    team/group/pimp_my_sip/res/res_sip_outbound_registration.c

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=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/include/asterisk/res_sip.h (original)
+++ team/group/pimp_my_sip/include/asterisk/res_sip.h Fri May 17 14:38:17 2013
@@ -75,7 +75,8 @@
 	AST_SIP_TRANSPORT_UDP,
 	AST_SIP_TRANSPORT_TCP,
 	AST_SIP_TRANSPORT_TLS,
-	/* XXX Websocket ? */
+	AST_SIP_TRANSPORT_WS,
+	AST_SIP_TRANSPORT_WSS,
 };
 
 /*! \brief Maximum number of ciphers supported for a TLS transport */
@@ -147,6 +148,17 @@
 	);
 	/*! Absolute time that this contact is no longer valid after */
 	struct timeval expiration_time;
+};
+
+/*!
+ * \brief A transport to be used for messages to a contact
+ */
+struct ast_sip_contact_transport {
+	AST_DECLARE_STRING_FIELDS(
+		/*! Full URI of the contact */
+		AST_STRING_FIELD(uri);
+	);
+	pjsip_transport *transport;
 };
 
 /*!
@@ -634,6 +646,37 @@
 struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name);
 
 /*!
+ * \brief Add a transport for a contact to use
+ */
+
+void ast_sip_location_add_contact_transport(struct ast_sip_contact_transport *ct);
+
+/*!
+ * \brief Delete a transport for a contact that went away
+ */
+void ast_sip_location_delete_contact_transport(struct ast_sip_contact_transport *ct);
+
+/*!
+ * \brief Retrieve a contact_transport, by URI
+ *
+ * \param contact_uri URI of the contact
+ *
+ * \retval NULL if not found
+ * \retval non-NULL if found
+ */
+struct ast_sip_contact_transport *ast_sip_location_retrieve_contact_transport_by_uri(const char *contact_uri);
+
+/*!
+ * \brief Retrieve a contact_transport, by transport
+ *
+ * \param transport transport the contact uses
+ *
+ * \retval NULL if not found
+ * \retval non-NULL if found
+ */
+struct ast_sip_contact_transport *ast_sip_location_retrieve_contact_transport_by_transport(pjsip_transport *transport);
+
+/*!
  * \brief Add a new contact to an AOR
  *
  * \param aor Pointer to the AOR

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=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/res/res_sip.c (original)
+++ team/group/pimp_my_sip/res/res_sip.c Fri May 17 14:38:17 2013
@@ -269,7 +269,7 @@
 	}
 
 	/* If the host is IPv6 turn the transport into an IPv6 version */
-	if (pj_strchr(&sip_uri->host, ':')) {
+	if (pj_strchr(&sip_uri->host, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
 		type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
 	}
 
@@ -279,8 +279,8 @@
 		return -1;
 	}
 
-	/* If IPv6 was not specified in the host but is in the transport, set the proper type */
-	if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) {
+	/* If IPv6 was specified in the transport, set the proper type */
+	if (pj_strchr(&local_addr, ':') && type < PJSIP_TRANSPORT_START_OTHER) {
 		type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
 	}
 
@@ -315,15 +315,31 @@
 		return -1;
 	}
 
-	if (transport->type == AST_SIP_TRANSPORT_UDP) {
+	if (transport->state->transport) {
 		selector->type = PJSIP_TPSELECTOR_TRANSPORT;
 		selector->u.transport = transport->state->transport;
-	} else if (transport->type == AST_SIP_TRANSPORT_TCP || transport->type == AST_SIP_TRANSPORT_TLS) {
+	} else if (transport->state->factory) {
 		selector->type = PJSIP_TPSELECTOR_LISTENER;
 		selector->u.listener = transport->state->factory;
 	} else {
 		return -1;
 	}
+
+	return 0;
+}
+
+static int sip_get_tpselector_from_uri(const char *uri, pjsip_tpselector *selector)
+{
+	RAII_VAR(struct ast_sip_contact_transport *, contact_transport, NULL, ao2_cleanup);
+
+	contact_transport = ast_sip_location_retrieve_contact_transport_by_uri(uri);
+
+	if (!contact_transport) {
+		return -1;
+	}
+
+	selector->type = PJSIP_TPSELECTOR_TRANSPORT;
+	selector->u.transport = contact_transport->transport;
 
 	return 0;
 }
@@ -342,7 +358,7 @@
 		return NULL;
 	}
 
-	if (sip_get_tpselector_from_endpoint(endpoint, &selector)) {
+	if (sip_get_tpselector_from_uri(uri, &selector) && sip_get_tpselector_from_endpoint(endpoint, &selector)) {
 		pjsip_dlg_terminate(dlg);
 		return NULL;
 	}
@@ -854,6 +870,8 @@
 
 	ast_res_sip_init_options_handling(0);
 
+	ast_res_sip_init_contact_transports();
+
 return AST_MODULE_LOAD_SUCCESS;
 
 error:

Modified: team/group/pimp_my_sip/res/res_sip.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip.exports.in?view=diff&rev=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/res/res_sip.exports.in (original)
+++ team/group/pimp_my_sip/res/res_sip.exports.in Fri May 17 14:38:17 2013
@@ -37,6 +37,10 @@
 		LINKER_SYMBOL_PREFIXast_sip_location_retrieve_contact_from_aor_list;
 		LINKER_SYMBOL_PREFIXast_sip_location_retrieve_aor_contacts;
 		LINKER_SYMBOL_PREFIXast_sip_location_retrieve_contact;
+		LINKER_SYMBOL_PREFIXast_sip_location_add_contact_transport;
+		LINKER_SYMBOL_PREFIXast_sip_location_delete_contact_transport;
+		LINKER_SYMBOL_PREFIXast_sip_location_retrieve_contact_transport_by_uri;
+		LINKER_SYMBOL_PREFIXast_sip_location_retrieve_contact_transport_by_transport;
 		LINKER_SYMBOL_PREFIXast_sip_location_add_contact;
 		LINKER_SYMBOL_PREFIXast_sip_location_update_contact;
 		LINKER_SYMBOL_PREFIXast_sip_location_delete_contact;

Modified: 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=diff&rev=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/res/res_sip/config_transport.c (original)
+++ team/group/pimp_my_sip/res/res_sip/config_transport.c Fri May 17 14:38:17 2013
@@ -145,6 +145,8 @@
 		transport->tls.password = pj_str((char*)transport->password);
 
 		res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &transport->tls, &transport->host, NULL, transport->async_operations, &transport->state->factory);
+	} else if (transport->type == AST_SIP_TRANSPORT_WS || transport->type == AST_SIP_TRANSPORT_WSS) {
+		res = PJ_SUCCESS;
 	}
 
 	if (res != PJ_SUCCESS) {
@@ -168,8 +170,11 @@
 		transport->type = AST_SIP_TRANSPORT_TCP;
 	} else if (!strcasecmp(var->value, "tls")) {
 		transport->type = AST_SIP_TRANSPORT_TLS;
-	} else {
-		/* TODO: Implement websockets */
+	} else if (!strcasecmp(var->value, "ws")) {
+		transport->type = AST_SIP_TRANSPORT_WS;
+	} else if (!strcasecmp(var->value, "wss")) {
+		transport->type = AST_SIP_TRANSPORT_WSS;
+	} else {
 		return -1;
 	}
 

Modified: team/group/pimp_my_sip/res/res_sip/include/res_sip_private.h
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip/include/res_sip_private.h?view=diff&rev=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/res/res_sip/include/res_sip_private.h (original)
+++ team/group/pimp_my_sip/res/res_sip/include/res_sip_private.h Fri May 17 14:38:17 2013
@@ -40,6 +40,14 @@
 int ast_res_sip_init_options_handling(int reload);
 
 /*!
+ * \brief Initialize transport storage for contacts.
+ *
+ * \retval 0 on success
+ * \retval other on failure
+ */
+int ast_res_sip_init_contact_transports(void);
+
+/*!
  * \brief Initialize outbound authentication support
  *
  * \retval 0 Success

Modified: team/group/pimp_my_sip/res/res_sip/location.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip/location.c?view=diff&rev=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/res/res_sip/location.c (original)
+++ team/group/pimp_my_sip/res/res_sip/location.c Fri May 17 14:38:17 2013
@@ -24,6 +24,10 @@
 #include "asterisk/logger.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/sorcery.h"
+#include "include/res_sip_private.h"
+
+#define CONTACT_TRANSPORTS_BUCKETS 7
+static struct ao2_container *contact_transports;
 
 /*! \brief Destructor for AOR */
 static void aor_destroy(void *obj)
@@ -68,6 +72,48 @@
 	}
 
 	return contact;
+}
+
+/*! \brief Callback function for finding a contact_transport by URI */
+static int contact_transport_find_by_uri(void *obj, void *arg, int flags)
+{
+	struct ast_sip_contact_transport *ct = obj;
+	const char *contact_uri = arg;
+
+	return (!strcmp(ct->uri, contact_uri)) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+/*! \brief Callback function for finding a contact_transport by transport */
+static int contact_transport_find_by_transport(void *obj, void *arg, int flags)
+{
+	struct ast_sip_contact_transport *ct = obj;
+	pjsip_transport *transport = arg;
+
+	return (ct->transport == transport) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+void ast_sip_location_add_contact_transport(struct ast_sip_contact_transport *ct)
+{
+	ao2_link(contact_transports, ct);
+
+	return;
+}
+
+void ast_sip_location_delete_contact_transport(struct ast_sip_contact_transport *ct)
+{
+	ao2_unlink(contact_transports, ct);
+
+	return;
+}
+
+struct ast_sip_contact_transport *ast_sip_location_retrieve_contact_transport_by_uri(const char *contact_uri)
+{
+	return ao2_callback(contact_transports, 0, contact_transport_find_by_uri, (void *)contact_uri);
+}
+
+struct ast_sip_contact_transport *ast_sip_location_retrieve_contact_transport_by_transport(pjsip_transport *transport)
+{
+	return ao2_callback(contact_transports, 0, contact_transport_find_by_transport, transport);
 }
 
 struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
@@ -260,3 +306,17 @@
 
 	return 0;
 }
+
+int ast_res_sip_init_contact_transports(void)
+{
+	if (contact_transports) {
+		ao2_t_ref(contact_transports, -1, "Remove old contact transports");
+	}
+
+	contact_transports = ao2_t_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CONTACT_TRANSPORTS_BUCKETS, NULL, NULL, "Create container for contact transports");
+	if (!contact_transports) {
+		return -1;
+	}
+
+	return 0;
+}

Modified: team/group/pimp_my_sip/res/res_sip_outbound_registration.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_outbound_registration.c?view=diff&rev=389004&r1=389003&r2=389004
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_outbound_registration.c (original)
+++ team/group/pimp_my_sip/res/res_sip_outbound_registration.c Fri May 17 14:38:17 2013
@@ -539,10 +539,10 @@
 			return -1;
 		}
 
-		if (transport->type == AST_SIP_TRANSPORT_UDP) {
+		if (transport->state->transport) {
 			selector.type = PJSIP_TPSELECTOR_TRANSPORT;
 			selector.u.transport = transport->state->transport;
-		} else if (transport->type == AST_SIP_TRANSPORT_TCP || transport->type == AST_SIP_TRANSPORT_TLS) {
+		} else if (transport->state->factory) {
 			selector.type = PJSIP_TPSELECTOR_LISTENER;
 			selector.u.listener = transport->state->factory;
 		} else {




More information about the asterisk-commits mailing list