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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 23 09:59:27 CST 2013


Author: file
Date: Wed Jan 23 09:59:23 2013
New Revision: 380002

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380002
Log:
Automatically produce a From and Contact header with the correct transport IP address that will be used.

Modified:
    team/group/pimp_my_sip/res/res_sip_session.c

Modified: team/group/pimp_my_sip/res/res_sip_session.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_session.c?view=diff&rev=380002&r1=380001&r2=380002
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_session.c (original)
+++ team/group/pimp_my_sip/res/res_sip_session.c Wed Jan 23 09:59:23 2013
@@ -464,11 +464,66 @@
 	return session;
 }
 
+static int sip_session_create_from(pj_pool_t *pool, pj_str_t *from, const char *user, const pj_str_t *target)
+{
+	pj_str_t tmp, local_addr;
+	pjsip_uri *uri;
+	pjsip_sip_uri *sip_uri;
+	pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
+	int local_port;
+
+	/* Parse the provided target URI so we can determine what transport it will end up using */
+	pj_strdup_with_null(pool, &tmp, target);
+
+	if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
+	    (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
+		return -1;
+	}
+
+	sip_uri = pjsip_uri_get_uri(uri);
+
+	/* Determine the transport type to use */
+	if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
+		type = PJSIP_TRANSPORT_TLS;
+	} else if (!sip_uri->transport_param.slen) {
+		type = PJSIP_TRANSPORT_UDP;
+	} else {
+		type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
+	}
+
+	if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
+		return -1;
+	}
+
+	/* If the host is IPv6 turn the transport into an IPv6 version */
+	if (pj_strchr(&sip_uri->host, ':')) {
+		type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
+	}
+
+	/* Get the local bound address for the transport that will be used when communicating with the provided URI */
+	if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, NULL,
+							      &local_addr, &local_port) != PJ_SUCCESS) {
+		return -1;
+	}
+
+	/* TODO: This needs to add the transport if needed */
+	from->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
+	from->slen = pj_ansi_snprintf(from->ptr, PJSIP_MAX_URL_SIZE,
+				      "<%s:%s@%.*s:%d>",
+				      (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
+				      user,
+				      (int)local_addr.slen,
+				      local_addr.ptr,
+				      local_port);
+
+	return 0;
+}
+
 struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, const char *uri)
 {
-	/* TODO: The local URI needs to be dynamically constructed based on different things */
-	pj_str_t local_uri = pj_str("sip:test at local"), remote_uri = pj_str((char*)uri);
+	pj_str_t local_uri = pj_str("sip:temp at localhost"), remote_uri = pj_str((char*)uri);
 	pjsip_dialog *dlg = NULL;
+	const pj_str_t HCONTACT = { "Contact", 7 };
 	pjsip_inv_session *inv_session = NULL;
 	struct ast_sip_session *session = NULL;
 	pjmedia_sdp_session *offer = NULL;
@@ -476,6 +531,16 @@
 	if (pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, NULL, &remote_uri, NULL, &dlg) != PJ_SUCCESS) {
 		return NULL;
 	}
+
+	if (sip_session_create_from(dlg->pool, &local_uri, "test", &remote_uri)) {
+		pjsip_dlg_terminate(dlg);
+		return NULL;
+	}
+
+	/* Update the dialog with the new local URI, we do it afterwards so we can use the dialog pool for construction */
+	pj_strdup_with_null(dlg->pool, &dlg->local.info_str, &local_uri);
+	dlg->local.info->uri = pjsip_parse_uri(dlg->pool, dlg->local.info_str.ptr, dlg->local.info_str.slen, 0);
+	dlg->local.contact = pjsip_parse_hdr(dlg->pool, &HCONTACT, local_uri.ptr, local_uri.slen, NULL);
 
 	if (pjsip_inv_create_uac(dlg, NULL, 0, &inv_session) != PJ_SUCCESS) {
 		pjsip_dlg_terminate(dlg);




More information about the asterisk-commits mailing list