[asterisk-commits] qwell: branch qwell/fun_with_transports r387967 - /team/qwell/fun_with_transp...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed May 8 11:08:38 CDT 2013


Author: qwell
Date: Wed May  8 11:08:37 2013
New Revision: 387967

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=387967
Log:
Address review feedback.

Modified:
    team/qwell/fun_with_transports/res/res_sip.c
    team/qwell/fun_with_transports/res/res_sip_transport_websocket.c

Modified: team/qwell/fun_with_transports/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/qwell/fun_with_transports/res/res_sip.c?view=diff&rev=387967&r1=387966&r2=387967
==============================================================================
--- team/qwell/fun_with_transports/res/res_sip.c (original)
+++ team/qwell/fun_with_transports/res/res_sip.c Wed May  8 11:08:37 2013
@@ -358,8 +358,7 @@
 		return NULL;
 	}
 
-	if (!sip_get_tpselector_from_uri(uri, &selector)) {
-	} else 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;
 	}

Modified: team/qwell/fun_with_transports/res/res_sip_transport_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/qwell/fun_with_transports/res/res_sip_transport_websocket.c?view=diff&rev=387967&r1=387966&r2=387967
==============================================================================
--- team/qwell/fun_with_transports/res/res_sip_transport_websocket.c (original)
+++ team/qwell/fun_with_transports/res/res_sip_transport_websocket.c Wed May  8 11:08:37 2013
@@ -14,6 +14,10 @@
  * 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.
+ */
+
+/*!
+ * \brief WebSocket transport module
  */
 
 /*** MODULEINFO
@@ -37,11 +41,19 @@
 static int transport_type_ws;
 static int transport_type_wss;
 
+/*!
+ * \brief Wrapper for pjsip_transport, for storing the WebSocket session
+ */
 struct ws_transport {
 	pjsip_transport transport;
 	struct ast_websocket *ws_session;
 };
 
+/*!
+ * \brief Send a message over the WebSocket connection.
+ *
+ * Called by pjsip transport manager.
+ */
 static pj_status_t ws_send_msg(pjsip_transport *transport,
                             pjsip_tx_data *tdata,
                             const pj_sockaddr_t *rem_addr,
@@ -51,7 +63,9 @@
 {
 	struct ws_transport *wstransport = (struct ws_transport *)transport;
 
-	ast_websocket_write(wstransport->ws_session, AST_WEBSOCKET_OPCODE_TEXT, tdata->buf.start, (int)(tdata->buf.cur - tdata->buf.start));
+	if (ast_websocket_write(wstransport->ws_session, AST_WEBSOCKET_OPCODE_TEXT, tdata->buf.start, (int)(tdata->buf.cur - tdata->buf.start))) {
+		return PJ_EUNKNOWN;
+	}
 
 	return PJ_SUCCESS;
 }
@@ -61,6 +75,11 @@
 	return PJ_SUCCESS;
 }
 
+/*!
+ * \brief Destroy the pjsip transport.
+ *
+ * Called by pjsip transport manager.
+ */
 static pj_status_t ws_destroy(pjsip_transport *transport)
 {
 	struct ws_transport *wstransport = (struct ws_transport *)transport;
@@ -96,6 +115,9 @@
 	struct ast_websocket *ws_session;
 };
 
+/*!
+ * \brief Create a pjsip transport.
+ */
 static int transport_create(void *data)
 {
 	struct transport_create_data *create_data = data;
@@ -104,11 +126,19 @@
 	pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
 	struct pjsip_tpmgr *tpmgr = pjsip_endpt_get_tpmgr(endpt);
 
-	pj_pool_t *pool = pjsip_endpt_create_pool(endpt, "ws", 512, 512);
+	pj_pool_t *pool;
 
 	pj_str_t buf;
 
-	newtransport = PJ_POOL_ZALLOC_T(pool, struct ws_transport);
+	if (!(pool = pjsip_endpt_create_pool(endpt, "ws", 512, 512))) {
+		ast_log(LOG_ERROR, "Failed to allocate WebSocket endpoint pool.\n");
+		return -1;
+	}
+
+	if (!(newtransport = PJ_POOL_ZALLOC_T(pool, struct ws_transport))) {
+		ast_log(LOG_ERROR, "Failed to allocate WebSocket transport.\n");
+		return -1;
+	}
 
 	newtransport->ws_session = create_data->ws_session;
 
@@ -129,9 +159,9 @@
 	newtransport->transport.local_name.host.slen = pj_ansi_strlen(newtransport->transport.local_name.host.ptr);
 	newtransport->transport.local_name.port = pj_sockaddr_get_port(&newtransport->transport.key.rem_addr);
 
-	newtransport->transport.type_name = (char*)pjsip_transport_get_type_name(newtransport->transport.key.type);
+	newtransport->transport.type_name = (char *)pjsip_transport_get_type_name(newtransport->transport.key.type);
 	newtransport->transport.flag = pjsip_transport_get_flag_from_type((pjsip_transport_type_e)newtransport->transport.key.type);
-	newtransport->transport.info = (char*) pj_pool_alloc(newtransport->transport.pool, 64);
+	newtransport->transport.info = (char *)pj_pool_alloc(newtransport->transport.pool, 64);
 
 	newtransport->transport.endpt = endpt;
 	newtransport->transport.tpmgr = tpmgr;
@@ -151,6 +181,9 @@
 	uint64_t payload_len;
 };
 
+/*!
+ * \brief Pass WebSocket data into pjsip transport manager.
+ */
 static int transport_read(void *data)
 {
 	struct transport_read_data *read_data = data;
@@ -161,7 +194,10 @@
 	int recvd;
 	pj_str_t buf;
 
-	rdata = PJ_POOL_ZALLOC_T(newtransport->transport.pool, pjsip_rx_data);
+	if (!(rdata = PJ_POOL_ZALLOC_T(newtransport->transport.pool, pjsip_rx_data))) {
+		ast_log(LOG_ERROR, "Failed to allocate WebSocket endpoint pool.\n");
+		return -1;
+	}
 
 	rdata->tp_info.pool = newtransport->transport.pool;
 	rdata->tp_info.transport = &newtransport->transport;
@@ -185,6 +221,9 @@
 	return (read_data->payload_len == recvd) ? 0 : -1;
 }
 
+/*!
+ \brief WebSocket connection handler.
+ */
 static void websocket_cb(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers)
 {
 	struct ast_taskprocessor *serializer = NULL;
@@ -195,7 +234,10 @@
 		return;
 	}
 
-	serializer = ast_sip_create_serializer();
+	if (!(serializer = ast_sip_create_serializer())) {
+		ast_websocket_unref(session);
+		return;
+	}
 
 	while (ast_wait_for_input(ast_websocket_fd(session), -1) > 0) {
 		struct transport_read_data read_data;
@@ -211,7 +253,10 @@
 				struct transport_create_data create_data;
 				create_data.ws_session = session;
 
-				ast_sip_push_task_synchronous(serializer, transport_create, &create_data);
+				if (ast_sip_push_task_synchronous(serializer, transport_create, &create_data)) {
+					ast_log(LOG_ERROR, "Could not create WebSocket transport.\n");
+					break;
+				}
 
 				transport = create_data.transport;
 			}
@@ -232,31 +277,39 @@
 	ast_websocket_unref(session);
 }
 
+/*!
+ * \brief Session supplement handler for avoiding DNS lookup on bogus address.
+ */
 static void websocket_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
 {
 	char contact_uri[PJSIP_MAX_URL_SIZE] = { 0, };
 	RAII_VAR(struct ast_sip_contact_transport *, ct, NULL, ao2_cleanup);
+	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_TRANSPORT, };
 
 	pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri, contact_uri, sizeof(contact_uri));
 
-	if ((ct = ast_sip_location_retrieve_contact_transport_by_uri(contact_uri))) {
-		pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_TRANSPORT, };
-
-		selector.u.transport = ct->transport;
-
-		pjsip_tx_data_set_transport(tdata, &selector);
-
-		tdata->dest_info.addr.count = 1;
-		tdata->dest_info.addr.entry[0].type = ct->transport->key.type;
-		tdata->dest_info.addr.entry[0].addr = ct->transport->key.rem_addr;
-		tdata->dest_info.addr.entry[0].addr_len = ct->transport->addr_len;
-	}
+	if (!(ct = ast_sip_location_retrieve_contact_transport_by_uri(contact_uri))) {
+		return;
+	}
+
+	selector.u.transport = ct->transport;
+
+	pjsip_tx_data_set_transport(tdata, &selector);
+
+	tdata->dest_info.addr.count = 1;
+	tdata->dest_info.addr.entry[0].type = ct->transport->key.type;
+	tdata->dest_info.addr.entry[0].addr = ct->transport->key.rem_addr;
+	tdata->dest_info.addr.entry[0].addr_len = ct->transport->addr_len;
 }
 
 static struct ast_sip_session_supplement websocket_supplement = {
 	.outgoing_request = websocket_outgoing_request,
 };
 
+
+/*!
+ * \brief Store the transport a message came in on, so it can be used for outbound messages to that contact.
+ */
 static pj_bool_t websocket_on_rx_msg(pjsip_rx_data *rdata)
 {
 	pjsip_contact_hdr *contact_hdr = NULL;




More information about the asterisk-commits mailing list