[asterisk-commits] kharwell: branch kharwell/pimp_my_sip r385160 - in /team/kharwell/pimp_my_sip...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 9 16:54:34 CDT 2013


Author: kharwell
Date: Tue Apr  9 16:54:31 2013
New Revision: 385160

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385160
Log:
review updates

Modified:
    team/kharwell/pimp_my_sip/channels/chan_gulp.c
    team/kharwell/pimp_my_sip/main/message.c
    team/kharwell/pimp_my_sip/res/res_sip_messaging.c

Modified: team/kharwell/pimp_my_sip/channels/chan_gulp.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_my_sip/channels/chan_gulp.c?view=diff&rev=385160&r1=385159&r2=385160
==============================================================================
--- team/kharwell/pimp_my_sip/channels/chan_gulp.c (original)
+++ team/kharwell/pimp_my_sip/channels/chan_gulp.c Tue Apr  9 16:54:31 2013
@@ -1113,6 +1113,7 @@
 static int sendtext(void *obj)
 {
 	RAII_VAR(struct sendtext_data *, data, obj, ao2_cleanup);
+	pjsip_tx_data *tdata;
 
 	const struct ast_sip_body body = {
 		.type = "text",
@@ -1126,9 +1127,9 @@
 		return 0;
 	}
 
-	if (ast_sip_send_request("MESSAGE", &body, data->session->inv_session->dlg, NULL) != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Could not send text MESSAGE request\n");
-	}
+	ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, &tdata);
+	ast_sip_add_body(tdata, &body);
+	ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint);
 
 	return 0;
 }

Modified: team/kharwell/pimp_my_sip/main/message.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_my_sip/main/message.c?view=diff&rev=385160&r1=385159&r2=385160
==============================================================================
--- team/kharwell/pimp_my_sip/main/message.c (original)
+++ team/kharwell/pimp_my_sip/main/message.c Tue Apr  9 16:54:31 2013
@@ -1114,8 +1114,7 @@
 	ao2_lock(msg);
 	ast_rwlock_rdlock(&tech_holder->tech_lock);
 	if (tech_holder->tech) {
-		res = tech_holder->tech->msg_send(msg, S_OR(args.to, ast_str_buffer(msg->to)),
-						  S_OR(args.from, ast_str_buffer(msg->from)));
+		res = tech_holder->tech->msg_send(msg, S_OR(args.to, ""), S_OR(args.from, ""));
 	}
 	ast_rwlock_unlock(&tech_holder->tech_lock);
 	ao2_unlock(msg);

Modified: team/kharwell/pimp_my_sip/res/res_sip_messaging.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_my_sip/res/res_sip_messaging.c?view=diff&rev=385160&r1=385159&r2=385160
==============================================================================
--- team/kharwell/pimp_my_sip/res/res_sip_messaging.c (original)
+++ team/kharwell/pimp_my_sip/res/res_sip_messaging.c Tue Apr  9 16:54:31 2013
@@ -35,9 +35,10 @@
 
 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
 
-static const int MAX_HDR_SIZE = 512;
-static const int MAX_BODY_SIZE = 1024;
-static const int MAX_EXTEN_SIZE = 256;
+#define MAX_HDR_SIZE 512
+#define MAX_BODY_SIZE 1024
+#define MAX_EXTEN_SIZE 256
+#define MAX_USER_SIZE 128
 
 /*!
  * \brief Determine where in the dialplan a call should go
@@ -82,6 +83,107 @@
 		return PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
 	}
 	return PJSIP_SC_OK;
+}
+
+/*!
+ * \brief Finds an endpoint by name
+ *
+ * \details Searches the given 'value' for a potential endpoint name.  If it finds
+ * an endpoint it is returned.  If 'value' contains a user then it is copied into
+ * the given 'user' parameter.
+ *
+ * \param value A string possibly containing an endpoint name somewhere in itn
+ * \param user A possible user found in the 'value' field
+ * \param log If true logs statements if a endpoint is not specified or found.
+ */
+static struct ast_sip_endpoint* endpoint_by_name(const char *value, char *user, int log)
+{
+	struct ast_sip_endpoint* res;
+	const char *name;
+
+	/* need to be one past 'sip:' or 'sips:' */
+	if ((name = strstr(value, "sip"))) {
+		name += 3;
+		if (*name == 's') {
+			++name;
+		}
+		value = name + 1;
+	}
+
+	if ((name = strchr(value, '@'))) {
+		strncpy(user, value, name - value < MAX_USER_SIZE ?
+			name - value : MAX_USER_SIZE);
+		name++;
+	} else {
+		name = value;
+	}
+
+	if (ast_strlen_zero(name)) {
+		if (log) {
+			ast_log(LOG_ERROR, "SIP MESSAGE send - no endpoint specified\n");
+		}
+		return NULL;
+	}
+
+	if (!(res = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", name)) && log) {
+		ast_log(LOG_ERROR, "SIP MESSAGE send - endpoint '%s' was not found\n", name);		
+	}
+	return res;
+}
+
+/*!
+ * \brief Attempts to set various fields in an outgoing 'From' header.
+ *
+ * \details Tries to find an associated endpoint for the given 'from' parameter.
+ * If no endpoint is found it attemps to parse the 'from' parameter as a SIP
+ * URI.  Either way it will use the info in the given 'from' parameter to set
+ * values in the outgoing 'From' header.
+ *
+ * \param tdata The outgoing message data structure
+ * \param from Info to potentially copy into the 'From' header
+ */
+static void set_from(pjsip_tx_data *tdata, const char *from)
+{
+	const pj_str_t hname = { "From", 4 };
+	char user[MAX_USER_SIZE] = {0};
+	pjsip_name_addr *name_addr;
+	pjsip_from_hdr *hdr;
+	pjsip_sip_uri *sip_uri;
+	pjsip_name_addr *from_name_addr;
+	pjsip_sip_uri *from_uri;
+	RAII_VAR(struct ast_sip_endpoint *, ep, NULL, ao2_cleanup);
+
+	if (!from) {
+		return;
+	}
+
+	/* try to look up by endpoint first, e.g. 'user at endpoint' */
+	if ((ep = endpoint_by_name(from, user, 0))) {
+		from = ast_sip_location_retrieve_contact_from_aor_list(ep->aors)->uri;
+	}
+
+	/* get current 'from' hdr & uri - going to overwrite some fields */
+	from_name_addr = (pjsip_name_addr *)PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
+	from_uri = pjsip_uri_get_uri(from_name_addr);
+
+	if ((hdr = pjsip_parse_hdr(tdata->pool, &hname, (char*)from, strlen(from), NULL))) {
+
+	/* if ((uri = pjsip_parse_uri(tdata->pool, (char*)from, strlen(from), 0))) { */
+		/* see if its in a 'name <sip:user at domain>' format */
+		name_addr = (pjsip_name_addr *)hdr->uri;
+		sip_uri = pjsip_uri_get_uri(name_addr->uri);
+		pj_strdup(tdata->pool, &from_name_addr->display, &name_addr->display);
+		if (user[0]) {
+			pj_strdup2(tdata->pool, &from_uri->user, user);
+		} else {
+			pj_strdup(tdata->pool, &from_uri->user, &sip_uri->user);
+		}
+		pj_strdup(tdata->pool, &from_uri->host, &sip_uri->host);
+		from_uri->port = sip_uri->port;
+	} else {
+		/* assume we just have the name only */
+		pj_strdup2(tdata->pool, &from_uri->user, from);
+	}
 }
 
 /*!
@@ -319,47 +421,60 @@
 	return mdata;
 }
 
-static int sip_send_request(struct msg_data *mdata)
-{
-	pj_status_t status;
+static int msg_send(void *data)
+{
+	RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
+
+	const struct ast_sip_body body = {
+		.type = "text",
+		.subtype = "plain",
+		.body_text = ast_msg_get_body(mdata->msg)
+	};
+
+	char user[MAX_USER_SIZE] = {0};
 	pjsip_tx_data *tdata;
 
-	pjsip_endpoint *ep = ast_sip_get_pjsip_endpoint();
-
-	pj_str_t to = pj_str(mdata->to);
-	pj_str_t from = pj_str(mdata->from);
-	pj_str_t body = pj_str((char*)ast_msg_get_body(mdata->msg));
-
-	status = pjsip_endpt_create_request(ep, &pjsip_message_method, &to,
-					    &from, &to, NULL, NULL, -1, &body,
-					    &tdata);
-
-	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Unable to create request - %d\n", status);
-		return status;
-	}
-
+	RAII_VAR(struct ast_sip_endpoint *, ep, endpoint_by_name(
+			 mdata->to, user, 1), ao2_cleanup);
+	if (!ep) {
+		return -1;
+	}
+
+	if (ast_sip_create_request("MESSAGE", NULL, ep, NULL, &tdata)) {
+		ast_log(LOG_ERROR, "SIP MESSAGE - Could not create request\n");		
+		return -1;
+	}
+
+	if (ast_sip_add_body(tdata, &body)) {
+		ast_log(LOG_ERROR, "SIP MESSAGE - Could not add body to request\n");
+		return -1;
+	}
+
+	if (user[0]) {
+		pjsip_sip_uri *to_uri = pjsip_uri_get_uri(PJSIP_MSG_TO_HDR(tdata->msg)->uri);
+		pj_strdup2(tdata->pool, &to_uri->user, user);
+	}
+
+	set_from(tdata, mdata->from);
 	vars_to_headers(mdata->msg, tdata);
-
-	status = pjsip_endpt_send_request(ep, tdata, -1, NULL, NULL);
-
-	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Unable to send request - %d\n", status);
-		return status;
-	}
+	if (ast_sip_send_request(tdata, NULL, ep)) {
+		ast_log(LOG_ERROR, "SIP MESSAGE - Could not send request\n");		
+		return -1;
+	}
+
 	return PJ_SUCCESS;
 }
 
-static int msg_send(void *data)
-{
-	RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
-
-	return sip_send_request(mdata);
-}
-
 static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
 {
-	struct msg_data *mdata = msg_data_create(msg, to, from);
+	struct msg_data *mdata;
+
+	if (ast_strlen_zero(to)) {
+		ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI  must be specified\n");		
+		return -1;
+	}
+
+	mdata = msg_data_create(msg, to, from);
 
 	return mdata ? ast_sip_push_task(NULL, msg_send, mdata) : -1;
 }




More information about the asterisk-commits mailing list