[asterisk-commits] kharwell: branch kharwell/pimp_my_sip r382216 - /team/kharwell/pimp_my_sip/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Feb 27 16:01:01 CST 2013


Author: kharwell
Date: Wed Feb 27 16:00:57 2013
New Revision: 382216

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382216
Log:
the very basics of MESSAGE send/receive should be working.  Still need to propagate received text.

Added:
    team/kharwell/pimp_my_sip/res/res_sip_messaging.c   (with props)
Modified:
    team/kharwell/pimp_my_sip/res/res_sip.c

Modified: team/kharwell/pimp_my_sip/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_my_sip/res/res_sip.c?view=diff&rev=382216&r1=382215&r2=382216
==============================================================================
--- team/kharwell/pimp_my_sip/res/res_sip.c (original)
+++ team/kharwell/pimp_my_sip/res/res_sip.c Wed Feb 27 16:00:57 2013
@@ -194,6 +194,7 @@
 
 /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */
 const pjsip_method pjsip_info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} };
+const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, { "MESSAGE", 7 } };
 
 static struct {
 	const char *method;
@@ -209,6 +210,7 @@
 	{ "NOTIFY", &pjsip_notify_method },
 	{ "PUBLISH", &pjsip_publish_method },
 	{ "INFO", &pjsip_info_method },
+	{ "MESSAGE", &pjsip_message_method },
 };
 
 static const pjsip_method *get_pjsip_method(const char *method)

Added: 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=auto&rev=382216
==============================================================================
--- team/kharwell/pimp_my_sip/res/res_sip_messaging.c (added)
+++ team/kharwell/pimp_my_sip/res/res_sip_messaging.c Wed Feb 27 16:00:57 2013
@@ -1,0 +1,435 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*** MODULEINFO
+	<depend>res_sip</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#undef bzero
+#define bzero bzero
+
+#include "pjsua-lib/pjsua.h"
+
+#include "asterisk/res_sip.h"
+#include "asterisk/message.h"
+#include "asterisk/module.h"
+
+/* static int validate_uri() */
+/* { */
+/* 	to_uri = ast_strdupa(to); */
+/* 	to_uri = get_in_brackets(to_uri); */
+/* 	parse_uri(to_uri, "sip:,sips:", &to_user, NULL, &to_host, NULL); */
+
+/* 	if (ast_strlen_zero(to_host)) { */
+/* 		ast_log(LOG_WARNING, "MESSAGE(to) is invalid for SIP - '%s'\n", to); */
+/* 		dialog_unlink_all(pvt); */
+/* 		dialog_unref(pvt, "MESSAGE(to) is invalid for SIP"); */
+/* 		return -1; */
+/* 	} */
+/* } */
+
+/*!
+ * \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) */
+/* { */
+/* 	const pjsip_hdr *h = msg->hdr.next, *end=&msg->hdr; */
+/* 	while (h != end) { */
+/* 		pjsip_hdr *new_hdr; */
+
+/* 		new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); */
+/* 		pjsip_msg_add_hdr(tdata->msg, new_hdr); */
+
+/* 		h = h->next; */
+/* 	} */
+
+/* 	/\* const pjsip_hdr *h = msg->hdr_list.next; *\/ */
+/* 	/\* while (h && h != &msg->hdr_list) { *\/ */
+/* 	/\* 	pjsip_hdr *new_hdr; *\/ */
+
+/* 	/\* 	new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); *\/ */
+/* 	/\* 	pjsip_msg_add_hdr(tdata->msg, new_hdr); *\/ */
+
+/* 	/\* 	h = h->next; *\/ */
+/* 	/\* } *\/ */
+/* } */
+
+struct msg_data {
+	struct ast_msg *msg;
+        char *to;
+	char *from;
+};
+
+static void msg_data_destroy(void *obj)
+{
+	struct msg_data *mdata = obj;
+
+	if (mdata->from) {
+		free(mdata->from);
+	}
+
+	if (mdata->to) {
+		free(mdata->to);
+	}
+
+	ast_msg_destroy(mdata->msg);
+}
+
+static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
+{
+	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);
+	}
+	return mdata;
+}
+
+static int sip_send_request(struct msg_data *mdata)
+{
+	pj_status_t status;
+	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, /* target uri */
+					    &from, /* URL to put in From header */
+					    &to, /* URL to put in To header */
+					    NULL, /* Contact header value*/
+					    NULL, /* use random callid */
+					    -1, /* use random CSeq */
+					    &body, /*pj_str(ast_msg_get_body(msg)), */
+					    &tdata); /* pointer to receive transmit data */
+
+	if (status != PJ_SUCCESS) {
+		ast_log(LOG_ERROR, "Unable to create request - %d\n", status);
+		return status;
+	}
+
+	/* add an accept header */
+	/* pjsip_msg_add_hdr(data->msg, */
+	/* 		  (pjsip_hdr*)pjsua_im_create_accept(data->pool)); */
+
+	/* tdata->msg->body = pjsip_msg_body_create(tdata->pool, "text", "plain", */
+	/* 					   pj_str(ast_msg_get_body(msg))); */
+
+	/* if (!tdata->msg->body) { */
+	/* 	ast_log(LOG_ERROR, "Unable to create message body\n") */
+	/* 	return PJ_ENOMEM; */
+	/* } */
+
+	/* copy_headers_ast(tdata, mdata->msg); */
+
+	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;
+	}
+
+	return PJ_SUCCESS;
+}
+
+static int msg_send(void *data)
+{
+	RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
+
+	/* struct ast_sip_body body = { */
+	/* 	.type = "text", */
+	/* 	.subtype = "plain", */
+	/* 	.body_text = ast_msg_get_body(mdata->msg) */
+	/* }; */
+
+	/* /\* /todo - get pjsip_dialog *dlg or ast_sip_endpoint *\/ */
+	/* status = ast_sip_send_request("MESSAGE", &body, NULL, NULL); */
+	/* if (status != PJ_SUCCESS) { */
+	/* 	ast_log(LOG_ERROR, "Could not send message request"); */
+	/* 	return status; */
+	/* } */
+	
+	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);
+
+	return mdata ? ast_sip_push_task(NULL, msg_send, mdata) : -1;
+}
+
+static const struct ast_msg_tech msg_tech = {
+	.name = "sip",
+	.msg_send = sip_msg_send,
+};
+
+/* static pj_bool_t module_start(void) */
+/* { */
+/* 	return PJ_SUCCESS; */
+/* } */
+
+/* static pj_bool_t module_stop(void) */
+/* { */
+/* 	return PJ_SUCCESS; */
+/* } */
+
+static pj_status_t send_response(pjsip_rx_data *rdata, int code)
+{
+	pjsip_tx_data *tdata;
+	pj_status_t status;
+	pjsip_dialog *dlg;
+	pjsip_transaction *trans;
+	pjsip_response_addr res_addr;
+
+	pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
+
+	status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
+	if (status != PJ_SUCCESS) {
+		ast_log(LOG_ERROR, "Unable to create response (%d)", status);
+		return status;
+	}
+
+	dlg = pjsip_rdata_get_dlg(rdata);
+	trans = pjsip_rdata_get_tsx(rdata);
+
+	if (dlg && trans) {
+		status = pjsip_dlg_send_response(dlg, trans, tdata);
+	} else {
+		/* 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);
+			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);
+		return status;
+	}
+
+	return PJ_TRUE;
+}
+
+static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
+{
+	if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
+		return PJ_FALSE;
+	}
+
+	return send_response(rdata, 202);
+}
+
+/* static pj_bool_t module_on_rx_response(pjsip_rx_data *rdata) */
+/* { */
+/* 	return PJ_FALSE; */
+/* } */
+
+static pj_bool_t module_on_tx_request(pjsip_tx_data *tdata)
+{
+	return PJ_SUCCESS;
+}
+
+/* static pj_bool_t module_on_tx_response(pjsip_tx_data *tdata) */
+/* { */
+/* 	return PJ_SUCCESS; */
+/* } */
+
+static pjsip_module messaging_module = {
+	.name = {"Messaging Module", 16},
+	.id = -1,
+	.priority = PJSIP_MOD_PRIORITY_APPLICATION,
+	/* .start = module_start, */
+	/* .stop = module_stop, */
+	.on_rx_request = module_on_rx_request,
+	/* .on_rx_response = module_on_rx_response, */
+	.on_tx_request = module_on_tx_request,
+	/* .on_tx_response = module_on_tx_response, */
+};
+
+static int load_module(void)
+{
+	/* const pj_str_t TAG = { "MESSAGE", 7 }; */
+
+	if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
+				       NULL, PJSIP_H_ALLOW, NULL, 1,
+				       &pjsip_message_method.name) != PJ_SUCCESS) {
+
+		pjsip_endpt_unregister_module(ast_sip_get_pjsip_endpoint(), &messaging_module);
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	if (ast_msg_tech_register(&msg_tech)) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	ast_msg_tech_unregister(&msg_tech);
+
+	ast_sip_unregister_service(&messaging_module);
+
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP Messaging Support",
+		.load = load_module,
+		.unload = unload_module,
+		.load_pri = AST_MODPRI_APP_DEPEND,
+	       );
+
+	/* pjsip_endpoint *ep = ast_sip_get_pjsip_endpoint(); */
+	/* pj_status_t status; */
+	/* pjsip_tx_data tx_data; */
+	/* pjsip_im_data im_data; */
+
+	/* pjsip_method msg_method = { PJSIP_OTHER_METHOD, { "MESSAGE", 7 } }; */
+
+	/* status = pjsip_endpt_create_request(endpt, &msg_method, */
+	/* 				    pj_str(to), /\* target uri *\/ */
+	/* 				    pj_str(from), /\* URL to put in From header *\/ */
+	/* 				    pj_str(to), /\* URL to put in To header *\/ */
+	/* 				    NULL, /\* Contact header value*\/  */
+	/* 				    NULL, /\* use random callid *\/ */
+	/* 				    -1, /\* use random CSeq *\/, */
+	/* 				    pj_str(ast_msg_get_body(msg)), */
+	/* 				    &tx_data); /\* pointer to receive transmit data *\/ */
+
+	/* if (status != PJ_SUCCESS) { */
+	/* 	ast_log(LOG_ERROR, "Unable to create request - %d %.*s\n", status.code,  */
+	/* 		(int)pj_strlen(&status.reason),	pj_strbuf(&status.reason)); */
+	/* 	return status; */
+	/* } */
+
+	/* /\* add an accept header *\/ */
+	/* /\* pjsip_msg_add_hdr(data->msg, *\/ */
+	/* /\* 		  (pjsip_hdr*)pjsua_im_create_accept(data->pool)); *\/ */
+
+	/* im_data = PJ_POOL_ZALLOC_T(tx_data->pool, pjsua_im_data); */
+	/* im_data->call_id = PJSUA_INVALID_ID; */
+
+	/* pj_strdup_with_null(tx_data->pool, &im_data->to, pj_str(to)); */
+	/* pj_strdup_with_null(tx_data->pool, &im_data->body, */
+	/* 		    pj_str(ast_msg_get_body(msg))); */
+
+	/* im_data->user_data = NULL; */
+
+	/* tx_data->msg->body = pjsip_msg_body_create(tdata->pool, "text", "plain", */
+	/* 					   im_data->body); */
+
+	/* if (!tx_data->msg->body) { */
+	/* 	ast_log(LOG_ERROR, "Unable to create message body\n") */
+	/* 	return PJ_ENOMEM; */
+	/* } */
+
+	/* add_msg_headers(tx_data, msg); */
+
+	/* status = pjsip_endpt_send_request(endpt, tx_data, -1, NULL, NULL); */
+
+	/* if (status != PJ_SUCCESS) { */
+	/* 	ast_log(LOG_ERROR, "Unable to send request - %d %.*s\n", status.code,  */
+	/* 		(int)pj_strlen(&status.reason),	pj_strbuf(&status.reason)); */
+	/* 	return status; */
+	/* } */

Propchange: team/kharwell/pimp_my_sip/res/res_sip_messaging.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/kharwell/pimp_my_sip/res/res_sip_messaging.c
------------------------------------------------------------------------------
    svn:keywords = Author Data Id Revision

Propchange: team/kharwell/pimp_my_sip/res/res_sip_messaging.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list