[asterisk-commits] file: branch group/pimp_my_sip r380408 - /team/group/pimp_my_sip/res/res_sip/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jan 29 14:26:41 CST 2013


Author: file
Date: Tue Jan 29 14:26:38 2013
New Revision: 380408

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380408
Log:
Tweak inbound OPTIONS handling. A few useful things to note:

1. pjsip has the response text for response codes already, you only need to specify it if you want something different
2. pjsip adds headers such as Allow, Supported, etc already in places where it knows to
3. Remember that by adding headers you are increasing the size of the SIP message, if it gets big... it'll go to TCP
4. It's easy to get the endpoint based on a received message :D
5. If you are only handling a single method a pjsip_method_cmp check as your first step is quick and easy

Modified:
    team/group/pimp_my_sip/res/res_sip/sip_options.c

Modified: team/group/pimp_my_sip/res/res_sip/sip_options.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip/sip_options.c?view=diff&rev=380408&r1=380407&r2=380408
==============================================================================
--- team/group/pimp_my_sip/res/res_sip/sip_options.c (original)
+++ team/group/pimp_my_sip/res/res_sip/sip_options.c Tue Jan 29 14:26:38 2013
@@ -38,8 +38,6 @@
 
 static pj_bool_t options_module_on_rx_request(pjsip_rx_data *rdata);
 static pj_bool_t options_module_on_rx_response(pjsip_rx_data *rdata);
-static pj_bool_t options_module_on_tx_request(pjsip_tx_data *tdata);
-static pj_bool_t options_module_on_tx_response(pjsip_tx_data *tdata);
 
 static pjsip_module options_module = {
 	.name = {"Options Module", 14},
@@ -47,40 +45,43 @@
 	.priority = PJSIP_MOD_PRIORITY_APPLICATION,
 	.on_rx_request = options_module_on_rx_request,
 	.on_rx_response = options_module_on_rx_response,
-	.on_tx_request = options_module_on_tx_request,
-	.on_tx_response = options_module_on_tx_response,
 };
 
 static pj_status_t send_options_response(pjsip_rx_data *rdata, int code)
 {
-	pj_str_t msg;
 	pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
 	pjsip_transaction *pj_trans = pjsip_rdata_get_tsx(rdata);
 	pjsip_dialog *pj_dlg = pjsip_rdata_get_dlg(rdata);
 	pjsip_tx_data *tdata;
+	const pjsip_hdr *hdr;
 	pjsip_response_addr res_addr;
 	pj_status_t status;
 
-	switch (code) {
-	case 200:
-		pj_cstr(&msg, "OK");
-		break;
-	case 404:
-		pj_cstr(&msg, "Not Found");
-		break;
-	case 503:
-		pj_cstr(&msg, "Unavailable");
-		break;
-	default:
-		pj_cstr(&msg, "");
-		break;
-	}
-
 	/* Make the response object */
-	status = pjsip_endpt_create_response(endpt, rdata, code, &msg, &tdata);
+	status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
 	if (status != PJ_SUCCESS) {
 		return status;
 	}
+
+	/* Add appropriate headers */
+	if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ACCEPT, NULL))) {
+		pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
+	}
+	if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ALLOW, NULL))) {
+		pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
+	}
+	if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, NULL))) {
+		pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
+	}
+
+	/*
+	 * XXX TODO: pjsip doesn't care a lot about either of these headers -
+	 * while it provides specific methods to create them, they are defined
+	 * to be the standard string header creation. We never did add them
+	 * in chan_sip, although RFC 3261 says they SHOULD. Hard coded here.
+	 */
+	ast_sip_add_header(tdata, "Accept-Encoding", DEFAULT_ENCODING);
+	ast_sip_add_header(tdata, "Accept-Language", DEFAULT_LANGUAGE);
 
 	if (pj_dlg && pj_trans) {
 		status = pjsip_dlg_send_response(pj_dlg, pj_trans, tdata);
@@ -99,116 +100,46 @@
 
 static pj_bool_t options_module_on_rx_request(pjsip_rx_data *rdata)
 {
-	pj_bool_t res = PJ_FALSE;
+	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
 	pjsip_uri *ruri;
 	pjsip_sip_uri *sip_ruri;
 	char exten[AST_MAX_EXTENSION];
 
-	switch (rdata->msg_info.msg->line.req.method.id) {
-	case PJSIP_OPTIONS_METHOD:
-		ruri = rdata->msg_info.msg->line.req.uri;
-		if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
-			break;
-		}
-		sip_ruri = pjsip_uri_get_uri(ruri);
-		ast_copy_pj_str(exten, &sip_ruri->user, sizeof(exten));
-
-		/* XXX TODO: eventually, this needs to be replaced with
-		 * some more advanced stuff (like inbound context, etc.)
-		 * We also need to authentication on the user.
-		 */
-		res = PJ_TRUE;
-
-		if (ast_shutting_down()) {
-			send_options_response(rdata, 503);
-			break;
-		}
-
-		/* XXX TODO: we should probably take the destination and see if it
-		 * matches a known endpoint. If so, and they happen to be in a dialog,
-		 * we can expose some additional information.
-		 */
-		if (!ast_exists_extension(NULL, "default", exten, 1, NULL)) {
-			send_options_response(rdata, 404);
-			break;
-		}
-
+	if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_options_method)) {
+		return PJ_FALSE;
+	}
+
+	/* If no endpoint is available for the request treat them as completely untrusted */
+	if (!(endpoint = ast_sip_identify_endpoint(rdata))) {
+		send_options_response(rdata, 404);
+		return PJ_TRUE;
+	}
+
+	ruri = rdata->msg_info.msg->line.req.uri;
+	if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
+		send_options_response(rdata, 416);
+		return PJ_TRUE;
+	}
+	
+	sip_ruri = pjsip_uri_get_uri(ruri);
+	ast_copy_pj_str(exten, &sip_ruri->user, sizeof(exten));
+
+	/* XXX TODO: authenticate the user */
+
+	if (ast_shutting_down()) {
+		send_options_response(rdata, 503);
+	} else if (!ast_exists_extension(NULL, endpoint->context, exten, 1, NULL)) {
+		send_options_response(rdata, 404);
+	} else {
 		send_options_response(rdata, 200);
-		break;
-	default:
-		break;
-	}
-
-	return res;
+	}
+
+	return PJ_TRUE;
 }
 
 static pj_bool_t options_module_on_rx_response(pjsip_rx_data *rdata)
 {
 
-	return PJ_SUCCESS;
-}
-
-static void add_outbound_headers(pjsip_tx_data *tdata)
-{
-	pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
-	const pjsip_hdr *c_hdr;
-	pjsip_hdr *hdr;
-
-	ast_assert(endpt != NULL);
-
-	/* XXX TODO: add a contact header? */
-	if (pjsip_msg_find_hdr(tdata->msg, PJSIP_H_ACCEPT, NULL) == NULL) {
-		c_hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ACCEPT, NULL);
-		if (c_hdr) {
-			hdr = (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, c_hdr);
-			pjsip_msg_add_hdr(tdata->msg, hdr);
-		}
-	}
-	if (pjsip_msg_find_hdr(tdata->msg, PJSIP_H_ALLOW, NULL) == NULL) {
-		c_hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ALLOW, NULL);
-		if (c_hdr) {
-			hdr = (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, c_hdr);
-			pjsip_msg_add_hdr(tdata->msg, hdr);
-		}
-	}
-	if (pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL) == NULL) {
-		c_hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, NULL);
-		if (c_hdr) {
-			hdr = (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, c_hdr);
-			pjsip_msg_add_hdr(tdata->msg, hdr);
-		}
-	}
-
-	/*
-	 * XXX TODO: pjsip doesn't care a lot about either of these headers -
-	 * while it provides specific methods to create them, they are defined
-	 * to be the standard string header creation. We never did add them
-	 * in chan_sip, although RFC 3261 says they SHOULD. Hard coded here.
-	 */
-	ast_sip_add_header(tdata, "Accept-Encoding", DEFAULT_ENCODING);
-	ast_sip_add_header(tdata, "Accept-Language", DEFAULT_LANGUAGE);
-}
-
-static pj_bool_t options_module_on_tx_request(pjsip_tx_data *tdata)
-{
-	add_outbound_headers(tdata);
-
-	return PJ_SUCCESS;
-}
-
-static pj_bool_t options_module_on_tx_response(pjsip_tx_data *tdata)
-{
-	struct pjsip_status_line status = tdata->msg->line.status;
-
-	if (status.code < 200 || status.code >= 300) {
-		return PJ_SUCCESS;
-	}
-
-	add_outbound_headers(tdata);
-
-	/* XXX TODO: If we can map the options requester against an endpoint,
-	 * we should include what media we think they have here in an SDP.
-	 */
 	return PJ_SUCCESS;
 }
 




More information about the asterisk-commits mailing list