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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 18 17:29:22 CDT 2013


Author: kharwell
Date: Thu Apr 18 17:29:17 2013
New Revision: 386041

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=386041
Log:
Fixed some review items

Modified:
    team/kharwell/pimp_my_sip/channels/chan_gulp.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=386041&r1=386040&r2=386041
==============================================================================
--- team/kharwell/pimp_my_sip/channels/chan_gulp.c (original)
+++ team/kharwell/pimp_my_sip/channels/chan_gulp.c Thu Apr 18 17:29:17 2013
@@ -1086,25 +1086,27 @@
 
 struct sendtext_data {
 	struct ast_sip_session *session;
-	const char *text;
+	char text[0];
 };
 
 static void sendtext_data_destroy(void *obj)
 {
 	struct sendtext_data *data = obj;
-	ast_free((char *)data->text);
 	ao2_ref(data->session, -1);
 }
 
 static struct sendtext_data* sendtext_data_create(struct ast_sip_session *session, const char *text)
 {
-	struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
-
-	if (data) {
-		data->session = session;
-		ao2_ref(data->session, +1);
-		data->text = ast_strdup(text);
-	}
+	int size = strlen(text) + 1;
+	struct sendtext_data *data = ao2_alloc(sizeof(*data)+size, sendtext_data_destroy);
+
+	if (!data) {
+		return NULL;
+	}
+
+	data->session = session;
+	ao2_ref(data->session, +1);
+	ast_copy_string(data->text, text, size);
 	return data;
 }
 
@@ -1138,7 +1140,11 @@
 	struct gulp_pvt *pvt = ast_channel_tech_pvt(ast);
 	struct sendtext_data *data = sendtext_data_create(pvt->session, text);
 
-	return ast_sip_push_task(pvt->session->serializer, sendtext, data);
+	if (!data || ast_sip_push_task(pvt->session->serializer, sendtext, data)) {
+		ao2_ref(data, -1);
+		return -1;
+	}
+	return 0;
 }
 
 /*! \brief Convert SIP hangup causes to Asterisk hangup causes */

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=386041&r1=386040&r2=386041
==============================================================================
--- team/kharwell/pimp_my_sip/res/res_sip_messaging.c (original)
+++ team/kharwell/pimp_my_sip/res/res_sip_messaging.c Thu Apr 18 17:29:17 2013
@@ -86,101 +86,118 @@
 }
 
 /*!
- * \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;
+ * \brief Puts pointer past 'sip[s]:' string that should be at the 
+ * front of the given 'fromto' parameter
+ *
+ * \param fromto 'From' or 'To' field containing 'sip:'
+ */
+static const char* skip_sip(const char *fromto)
+{
+	const char *p;
 
 	/* 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 (!(p = strstr(fromto, "sip"))) {
+		return fromto;
+	}
+
+	p += 3;
+	if (*p == 's') {
+		++p;
+	}
+	return ++p;
+}
+
+/*!
+ * \brief Retrieves an endpoint if specified in the given 'fromto'
+ *
+ * Expects the given 'fromto' to be in one of the following formats:
+ *     	sip[s]:endpoint[/aor]
+ *      sip[s]:endpoint[/uri]
+ *
+ * If an optional aor is given it will try to find an associated uri
+ * to return.  If an optional uri is given then that will be returned,
+ * otherwise uri will be NULL.
+ *
+ * \param fromto 'From' or 'To' field with possible endpoint
+ * \param uri Optional uri to return
+ */
+static struct ast_sip_endpoint* get_endpoint(const char *fromto, char **uri)
+{
+	const char *name = skip_sip(fromto);	
+	struct ast_sip_endpoint* endpoint;
+	struct ast_sip_aor *aor;
+
+	if ((*uri = strchr(name, '/'))) {
+		*(*uri)++ = '\0';
+	}
+
+	/* endpoint is required */
 	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.
+	
+	if (!(endpoint = ast_sorcery_retrieve_by_id(
+		      ast_sip_get_sorcery(), "endpoint", name))) {
+		return NULL;
+	}
+
+	if (*uri && (aor = ast_sip_location_retrieve_aor(*uri))) {
+		*uri = (char*)ast_sip_location_retrieve_first_aor_contact(aor)->uri;
+	}
+
+	return endpoint;
+}
+
+/*!
+ * \brief Updates fields in an 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;
+static void update_from(pjsip_tx_data *tdata, const char *from)
+{
+	/* static const pj_str_t hname = { "From", 4 }; */
 	pjsip_name_addr *from_name_addr;
 	pjsip_sip_uri *from_uri;
-	RAII_VAR(struct ast_sip_endpoint *, ep, NULL, ao2_cleanup);
-
-	if (!from) {
+	pjsip_uri *parsed;
+	char *uri;
+
+	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
+
+	if (ast_strlen_zero(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;
+	if (!(endpoint = get_endpoint(from, &uri))) {
+		return;
+	}
+
+	if (ast_strlen_zero(uri)) {
+		/* if no aor/uri was specified get one from the endpoint */
+		uri = (char*)ast_sip_location_retrieve_contact_from_aor_list(endpoint->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))) {
-		/* 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);
+	/* check to see if uri is in 'name <sip:user at domain>' format */
+	if ((parsed = pjsip_parse_uri(tdata->pool, uri, strlen(uri), PJSIP_PARSE_URI_AS_NAMEADDR))) {
+		pjsip_name_addr *name_addr = (pjsip_name_addr *)parsed;
+		pjsip_sip_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->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);
+		/* assume it is 'user[@domain]' format */
+		char *domain = strchr(uri, '@');
+		if (domain) {
+			*domain++ = '\0';
+			pj_strdup2(tdata->pool, &from_uri->host, domain);
+		}
+		pj_strdup2(tdata->pool, &from_uri->user, uri);
 	}
 }
 
@@ -404,18 +421,21 @@
 	char *tag;
 	struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
 
-	if (mdata) {
-		/* typecast to suppress const warning */
-		mdata->msg = ast_msg_ref((struct ast_msg*)msg);
-
-		mdata->to = ast_strdup(to);
-		mdata->from = ast_strdup(from);
-
-		/* sometimes from can still contain the tag at this point, so remove it */
-		if ((tag = strchr(mdata->from, ';'))) {
-			*tag = '\0';
-		}
-	}
+	if (!mdata) {
+		return NULL;
+	}
+
+	/* typecast to suppress const warning */
+	mdata->msg = ast_msg_ref((struct ast_msg*)msg);
+
+	mdata->to = ast_strdup(to);
+	mdata->from = ast_strdup(from);
+
+	/* sometimes from can still contain the tag at this point, so remove it */
+	if ((tag = strchr(mdata->from, ';'))) {
+		*tag = '\0';
+	}
+
 	return mdata;
 }
 
@@ -429,33 +449,31 @@
 		.body_text = ast_msg_get_body(mdata->msg)
 	};
 
-	char user[MAX_USER_SIZE] = {0};
 	pjsip_tx_data *tdata;
-
-	RAII_VAR(struct ast_sip_endpoint *, ep, endpoint_by_name(
-			 mdata->to, user, 1), ao2_cleanup);
-	if (!ep) {
+	char *uri;
+
+	RAII_VAR(struct ast_sip_endpoint *, endpoint, get_endpoint(
+			 mdata->to, &uri), ao2_cleanup);
+	if (!endpoint) {
+		ast_log(LOG_ERROR, "SIP MESSAGE - Endpoint not found in %s\n", mdata->to);
 		return -1;
 	}
 
-	if (ast_sip_create_request("MESSAGE", NULL, ep, NULL, &tdata)) {
+	if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, &tdata)) {
 		ast_log(LOG_ERROR, "SIP MESSAGE - Could not create request\n");
 		return -1;
 	}
 
 	if (ast_sip_add_body(tdata, &body)) {
+		pjsip_tx_data_dec_ref(tdata);
 		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);
+	update_from(tdata, mdata->from);
 	vars_to_headers(mdata->msg, tdata);
-	if (ast_sip_send_request(tdata, NULL, ep)) {
+	if (ast_sip_send_request(tdata, NULL, endpoint)) {
+		pjsip_tx_data_dec_ref(tdata);
 		ast_log(LOG_ERROR, "SIP MESSAGE - Could not send request\n");
 		return -1;
 	}
@@ -472,9 +490,12 @@
 		return -1;
 	}
 
-	mdata = msg_data_create(msg, to, from);
-
-	return mdata ? ast_sip_push_task(NULL, msg_send, mdata) : -1;
+	if (!(mdata = msg_data_create(msg, to, from)) ||
+	    ast_sip_push_task(NULL, msg_send, mdata)) {
+		ao2_ref(mdata, -1);
+		return -1;
+	}
+	return 0;
 }
 
 static const struct ast_msg_tech msg_tech = {




More information about the asterisk-commits mailing list