[svn-commits] kharwell: branch kharwell/pimp_my_sip r382763 - in /team/kharwell/pimp_my_sip...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Mar 8 18:03:40 CST 2013


Author: kharwell
Date: Fri Mar  8 18:03:36 2013
New Revision: 382763

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382763
Log:
fixed a few bugs found during testing.  msg_send now defaults the to/from addresses if they are left empty

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

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=382763&r1=382762&r2=382763
==============================================================================
--- team/kharwell/pimp_my_sip/main/message.c (original)
+++ team/kharwell/pimp_my_sip/main/message.c Fri Mar  8 18:03:36 2013
@@ -1114,8 +1114,8 @@
 	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, ""),
-							S_OR(args.from, ""));
+		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)));
 	}
 	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=382763&r1=382762&r2=382763
==============================================================================
--- team/kharwell/pimp_my_sip/res/res_sip_messaging.c (original)
+++ team/kharwell/pimp_my_sip/res/res_sip_messaging.c Fri Mar  8 18:03:36 2013
@@ -38,13 +38,6 @@
 static const int MAX_BODY_SIZE = 1024;
 static const int MAX_EXTEN_SIZE = 256;
 
-/*! \brief Check to see if the given header type was already handled. */
-static int header_handled(pjsip_hdr_e type)
-{
-	return type == PJSIP_H_TO || type == PJSIP_H_FROM || 
-		type ==	PJSIP_H_CONTACT;
-}
-
 /*!
  * \brief Determine where in the dialplan a call should go
  *
@@ -91,6 +84,80 @@
 }
 
 /*!
+ * \internal
+ * \brief Checks if the given msg var name should be blocked.
+ *
+ * \details Some headers are not allowed to be overriden by the user.
+ *  Determine if the given var header name from the user is blocked for
+ *  an outgoing MESSAGE.
+ *
+ * \param name name of header to see if it is blocked.
+ *
+ * \retval TRUE if the given header is blocked.
+ */
+static int is_msg_var_blocked(const char *name)
+{
+	int i;
+
+	/*
+	 * Don't block Content-Type or Max-Forwards headers because the
+	 * user can override them.
+	 */
+	static const char *hdr[] = {
+		"To",
+		"From",
+		"Via",
+		"Route",
+		"Contact",
+		"Call-ID",
+		"CSeq",
+		"Allow",
+		"Content-Length",
+		"Request-URI",
+	};
+
+	for (i = 0; i < ARRAY_LEN(hdr); ++i) {
+		if (!strcasecmp(name, hdr[i])) {
+			/* Block addition of this header. */
+			return 1;
+		}
+	}
+	return 0;
+}
+
+/*!
+ * \brief Copies any other msg vars over to the request headers.
+ *
+ * \param msg The msg structure to copy headers from
+ * \param tdata The SIP transmission data
+ */
+static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
+{
+	const char *name;
+	const char *value;
+	int max_forwards;
+
+	struct ast_msg_var_iterator *i = ast_msg_var_iterator_init(msg);
+	while (ast_msg_var_iterator_next(msg, i, &name, &value)) {
+		if (!strcasecmp(name, "Max-Forwards")) {
+			/* Decrement Max-Forwards for SIP loop prevention. */
+			if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
+				ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero.  MESSAGE not sent.\n");
+				return -1;
+			}
+			sprintf((char*)value, "%d", max_forwards);
+			ast_sip_add_header(tdata, name, value);
+		}
+		else if (!is_msg_var_blocked(name)) {
+			ast_sip_add_header(tdata, name, value);
+		}
+		ast_msg_var_unref_current(i);
+	}
+	ast_msg_var_iterator_destroy(i);
+	return PJSIP_SC_OK;
+}
+
+/*!
  * \brief Copies any other request header data over to ast_msg structure.
  *
  * \param rdata The SIP request
@@ -98,21 +165,26 @@
  */
 static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
 {
+	char *c;
 	char buf[MAX_HDR_SIZE];
 	int res = 0;
 	pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
 	pjsip_hdr *end= &rdata->msg_info.msg->hdr;
 
 	while (h != end) {
-		if (!header_handled(h->type)) {
-			pjsip_hdr_print_on(h, buf, sizeof(buf)-1);
+		if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
+			buf[res] = '\0';
+			if ((c = strchr(buf, ':'))) {
+				ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf)-(c-buf));
+			}
+
 			if ((res = ast_msg_set_var(msg, pj_strbuf(&h->name), buf)) != 0) {
 				break;
 			}
 		}
 		h = h->next;
 	}
-	return res;
+	return 0;
 }
 
 /*!
@@ -126,11 +198,12 @@
  */
 static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
 {
-	int res;
+	int res, size;
 	char buf[MAX_BODY_SIZE];
 	pjsip_uri *uri;
 	const char *field;
 	pjsip_status_code code;
+	pj_str_t *display;
 	struct ast_sip_endpoint *endpt = ast_pjsip_rdata_get_endpoint(rdata);
 
 	/* make sure there is an appropriate context and extension*/
@@ -149,24 +222,28 @@
 	/* from header */
 	uri = pjsip_uri_get_uri(rdata->msg_info.from->uri);
 	pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, uri, buf, sizeof(buf)-1);
-	field = pj_strbuf(&((pjsip_name_addr*)rdata->msg_info.from->uri)->display);
-	if (field) {
-		res |= ast_msg_set_from(msg, "\"%s\" <%s>", field, buf);
+	display = &((pjsip_name_addr*)rdata->msg_info.from->uri)->display;
+	if (pj_strlen(display)) {
+		res |= ast_msg_set_from(msg, "\"%.*s\" <%s>", pj_strlen(display), pj_strbuf(display), buf);
 	} else {
 		res |= ast_msg_set_from(msg, "<%s>", buf);
 	}
 
 	/* contact header */
-	field = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
-	res |= ast_msg_set_var(msg, "SIP_FULLCONTACT", field);
+	if ((size = pjsip_hdr_print_on(pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL), buf, sizeof(buf)-1)) > 0) {
+		buf[size] = '\0';
+		res |= ast_msg_set_var(msg, "SIP_FULLCONTACT", buf);
+	}
 
 	/* receive address */
 	field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf)-1, 1);
 	res |= ast_msg_set_var(msg, "SIP_RECVADDR", field);
 
 	/* body */
-	rdata->msg_info.msg->body->print_body(rdata->msg_info.msg->body, buf, sizeof(buf)-1);
-	res |= ast_msg_set_body(msg, "%s", buf);
+	if ((size = rdata->msg_info.msg->body->print_body(rdata->msg_info.msg->body, buf, sizeof(buf)-1)) > 0) {
+		buf[size] = '\0';
+		res |= ast_msg_set_body(msg, "%s", buf);
+	}
 
 	/* endpoint name */
 	if (endpt->id.name.valid) {
@@ -181,75 +258,6 @@
 	}
 	return PJSIP_SC_OK;
 }
-
-/*!
- * \internal
- * \brief Check if the given header name is blocked.
- *
- * \details Determine if the given header name from the user is
- * blocked for outgoing MESSAGE packets.
- *
- * \param name Name of header to see if it is blocked.
- *
- * \retval TRUE if the given header is blocked.
- */
-/* static int is_msg_header_blocked(const char *name) */
-/* { */
-/* 	int i; */
-
-/* 	/\* */
-/* 	 * Don't block Content-Type or Max-Forwards headers because the */
-/* 	 * user can override them. */
-/* 	 *\/ */
-/* 	static const char *hdr[] = { */
-/* 		"To", */
-/* 		"From", */
-/* 		"Via", */
-/* 		"Route", */
-/* 		"Contact", */
-/* 		"Call-ID", */
-/* 		"CSeq", */
-/* 		"Allow", */
-/* 		"Content-Length", */
-/* 		"Request-URI", */
-/* 	}; */
-
-/* 	for (i = 0; i < ARRAY_LEN(hdr); ++i) { */
-/* 		if (!strcasecmp(name, hdr[i])) { */
-/* 			/\* Block addition of this header. *\/ */
-/* 			return 1; */
-/* 		} */
-/* 	} */
-/* 	return 0; */
-/* } */
-
-/* static void copy_headers_ast(pjsip_tx_data *tdata, const struct ast_msg *msg) */
-/* { */
-/* 	const char *name; */
-/* 	const char *value; */
-
-/* 	struct ast_msg_var_iterator *i = ast_msg_var_iterator_init(msg); */
-/* 	while (ast_msg_var_iterator_next(msg, i, &name, &value)) { */
-/* 		ast_msg_var_unref_current(i); */
-/* 		if (!strcasecmp(name, "Max-Forwards")) { */
-/* 			/\* Decrement Max-Forwards for SIP loop prevention. *\/ */
-/* 			/\* if (sscanf(value, "%30d", &pvt->maxforwards) != 1 || pvt->maxforwards < 1) { *\/ */
-/* 			/\* 	sip_pvt_unlock(pvt); *\/ */
-/* 			/\* 	dialog_unlink_all(pvt); *\/ */
-/* 			/\* 	dialog_unref(pvt, "MESSAGE(Max-Forwards) reached zero."); *\/ */
-/* 			/\* 	ast_log(LOG_NOTICE, *\/ */
-/* 			/\* 		"MESSAGE(Max-Forwards) reached zero.  MESSAGE not sent.\n"); *\/ */
-/* 			/\* 	return -1; *\/ */
-/* 			/\* } *\/ */
-/* 			/\* --pvt->maxforwards; *\/ */
-/* 		} */
-/* 		else if (!is_msg_header_blocked(name)) { */
-/* 			ast_sip_add_header(tdata, name, value); */
-/* 		} */
-
-/* 	} */
-/* 	ast_msg_var_iterator_destroy(i); */
-/* } */
 
 /* static void copy_headers_pjsip(pjsip_tx_data *tdata, const pjsip_msg *msg) */
 /* { */
@@ -297,15 +305,20 @@
 
 static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
 {
+	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);
 
-		/* /todo check if ast_strlen_zero(to) if so use from 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;
 }
@@ -329,6 +342,8 @@
 		ast_log(LOG_ERROR, "Unable to create request - %d\n", status);
 		return status;
 	}
+
+	vars_to_headers(mdata->msg, tdata);
 
 	status = pjsip_endpt_send_request(ep, tdata, -1, NULL, NULL);
 
@@ -369,7 +384,7 @@
 
 	status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
 	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Unable to create response (%d)", status);
+		ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
 		return status;
 	}
 
@@ -379,14 +394,14 @@
 		/* Get where to send request. */
 		status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
 		if (status != PJ_SUCCESS) {
-			ast_log(LOG_ERROR, "Unable to get response address (%d)", status);
+			ast_log(LOG_ERROR, "Unable to get response address (%d)\n", status);
 			return status;
 		}
 		status = pjsip_endpt_send_response(endpt, &res_addr, tdata, NULL, NULL);
 	}
 
 	if (status != PJ_SUCCESS) {
-		ast_log(LOG_ERROR, "Unable to send response (%d)", status);
+		ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
 	}
 
 	return status;




More information about the svn-commits mailing list