2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Kevin Harwell <kharwell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_session</depend>
23 <support_level>core</support_level>
28 #include "pjsua-lib/pjsua.h"
30 #include "asterisk/message.h"
31 #include "asterisk/module.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/res_pjsip.h"
34 #include "asterisk/res_pjsip_session.h"
36 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
38 #define MAX_HDR_SIZE 512
39 #define MAX_BODY_SIZE 1024
40 #define MAX_EXTEN_SIZE 256
41 #define MAX_USER_SIZE 128
45 * \brief Determine where in the dialplan a call should go
47 * \details This uses the username in the request URI to try to match
48 * an extension in an endpoint's context in order to route the call.
50 * \param rdata The SIP request
51 * \param context The context to use
52 * \param exten The extension to use
54 static enum pjsip_status_code get_destination(const pjsip_rx_data *rdata, const char *context, char *exten)
56 pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
57 pjsip_sip_uri *sip_ruri;
59 if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
60 return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
63 sip_ruri = pjsip_uri_get_uri(ruri);
64 ast_copy_pj_str(exten, &sip_ruri->user, MAX_EXTEN_SIZE);
66 if (ast_exists_extension(NULL, context, exten, 1, NULL)) {
69 return PJSIP_SC_NOT_FOUND;
74 * \brief Checks to make sure the request has the correct content type.
76 * \details This module supports the following media types: "text/plain".
77 * Return unsupported otherwise.
79 * \param rdata The SIP request
81 static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
83 if (ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
88 return PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
94 * \brief Puts pointer past 'sip[s]:' string that should be at the
95 * front of the given 'fromto' parameter
97 * \param fromto 'From' or 'To' field containing 'sip:'
99 static const char* skip_sip(const char *fromto)
103 /* need to be one past 'sip:' or 'sips:' */
104 if (!(p = strstr(fromto, "sip"))) {
117 * \brief Retrieves an endpoint if specified in the given 'fromto'
119 * Expects the given 'fromto' to be in one of the following formats:
120 * sip[s]:endpoint[/aor]
121 * sip[s]:endpoint[/uri]
123 * If an optional aor is given it will try to find an associated uri
124 * to return. If an optional uri is given then that will be returned,
125 * otherwise uri will be NULL.
127 * \param fromto 'From' or 'To' field with possible endpoint
128 * \param uri Optional uri to return
130 static struct ast_sip_endpoint* get_endpoint(const char *fromto, char **uri)
132 const char *name = skip_sip(fromto);
133 struct ast_sip_endpoint* endpoint;
134 struct ast_sip_aor *aor;
136 if ((*uri = strchr(name, '/'))) {
140 /* endpoint is required */
141 if (ast_strlen_zero(name)) {
145 if (!(endpoint = ast_sorcery_retrieve_by_id(
146 ast_sip_get_sorcery(), "endpoint", name))) {
150 if (*uri && (aor = ast_sip_location_retrieve_aor(*uri))) {
151 *uri = (char*)ast_sip_location_retrieve_first_aor_contact(aor)->uri;
159 * \brief Updates fields in an outgoing 'From' header.
161 * \param tdata The outgoing message data structure
162 * \param from Info to potentially copy into the 'From' header
164 static void update_from(pjsip_tx_data *tdata, const char *from)
166 /* static const pj_str_t hname = { "From", 4 }; */
167 pjsip_name_addr *from_name_addr;
168 pjsip_sip_uri *from_uri;
172 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
174 if (ast_strlen_zero(from)) {
178 if (!(endpoint = get_endpoint(from, &uri))) {
182 if (ast_strlen_zero(uri)) {
183 /* if no aor/uri was specified get one from the endpoint */
184 uri = (char*)ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors)->uri;
187 /* get current 'from' hdr & uri - going to overwrite some fields */
188 from_name_addr = (pjsip_name_addr *)PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
189 from_uri = pjsip_uri_get_uri(from_name_addr);
191 /* check to see if uri is in 'name <sip:user@domain>' format */
192 if ((parsed = pjsip_parse_uri(tdata->pool, uri, strlen(uri), PJSIP_PARSE_URI_AS_NAMEADDR))) {
193 pjsip_name_addr *name_addr = (pjsip_name_addr *)parsed;
194 pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
196 pj_strdup(tdata->pool, &from_name_addr->display, &name_addr->display);
197 pj_strdup(tdata->pool, &from_uri->user, &sip_uri->user);
198 pj_strdup(tdata->pool, &from_uri->host, &sip_uri->host);
199 from_uri->port = sip_uri->port;
201 /* assume it is 'user[@domain]' format */
202 char *domain = strchr(uri, '@');
205 pj_strdup2(tdata->pool, &from_uri->host, domain);
207 pj_strdup2(tdata->pool, &from_uri->user, uri);
213 * \brief Checks if the given msg var name should be blocked.
215 * \details Some headers are not allowed to be overriden by the user.
216 * Determine if the given var header name from the user is blocked for
217 * an outgoing MESSAGE.
219 * \param name name of header to see if it is blocked.
221 * \retval TRUE if the given header is blocked.
223 static int is_msg_var_blocked(const char *name)
228 * Don't block Content-Type or Max-Forwards headers because the
229 * user can override them.
231 static const char *hdr[] = {
244 for (i = 0; i < ARRAY_LEN(hdr); ++i) {
245 if (!strcasecmp(name, hdr[i])) {
246 /* Block addition of this header. */
255 * \brief Copies any other msg vars over to the request headers.
257 * \param msg The msg structure to copy headers from
258 * \param tdata The SIP transmission data
260 static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
266 RAII_VAR(struct ast_msg_var_iterator *, i, ast_msg_var_iterator_init(msg), ast_msg_var_iterator_destroy);
267 while (ast_msg_var_iterator_next(msg, i, &name, &value)) {
268 if (!strcasecmp(name, "Max-Forwards")) {
269 /* Decrement Max-Forwards for SIP loop prevention. */
270 if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
271 ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero. MESSAGE not sent.\n");
274 sprintf((char*)value, "%d", max_forwards);
275 ast_sip_add_header(tdata, name, value);
277 else if (!is_msg_var_blocked(name)) {
278 ast_sip_add_header(tdata, name, value);
280 ast_msg_var_unref_current(i);
287 * \brief Copies any other request header data over to ast_msg structure.
289 * \param rdata The SIP request
290 * \param msg The msg structure to copy headers into
292 static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
295 char buf[MAX_HDR_SIZE];
297 pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
298 pjsip_hdr *end= &rdata->msg_info.msg->hdr;
301 if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
303 if ((c = strchr(buf, ':'))) {
304 ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf)-(c-buf));
307 if ((res = ast_msg_set_var(msg, pj_strbuf(&h->name), buf)) != 0) {
318 * \brief Prints the message body into the given char buffer.
320 * \details Copies body content from the received data into the given
321 * character buffer removing any extra carriage return/line feeds.
323 * \param rdata The SIP request
324 * \param buf Buffer to fill
325 * \param len The length of the buffer
327 static int print_body(pjsip_rx_data *rdata, char *buf, int len)
329 int res = rdata->msg_info.msg->body->print_body(
330 rdata->msg_info.msg->body, buf, len);
336 /* remove any trailing carriage return/line feeds */
337 while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n')));
346 * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
348 * \details Attempts to fill in as much information as possible into the given
349 * msg structure copied from the given request data.
351 * \param rdata The SIP request
352 * \param msg The asterisk message structure to fill in.
354 static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
357 #define CHECK_RES(z_) do { if (z_) { ast_msg_destroy(msg); \
358 return PJSIP_SC_INTERNAL_SERVER_ERROR; } } while (0)
361 char buf[MAX_BODY_SIZE];
362 pjsip_name_addr *name_addr;
364 pjsip_status_code code;
365 struct ast_sip_endpoint *endpt = ast_pjsip_rdata_get_endpoint(rdata);
367 /* make sure there is an appropriate context and extension*/
368 if ((code = get_destination(rdata, endpt->context, buf)) != PJSIP_SC_OK) {
372 CHECK_RES(ast_msg_set_context(msg, "%s", endpt->context));
373 CHECK_RES(ast_msg_set_exten(msg, "%s", buf));
376 name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
377 if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
379 CHECK_RES(ast_msg_set_to(msg, "%s", buf));
383 name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
384 if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
386 CHECK_RES(ast_msg_set_from(msg, "%s", buf));
390 if ((size = pjsip_hdr_print_on(pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL), buf, sizeof(buf)-1)) > 0) {
392 CHECK_RES(ast_msg_set_var(msg, "SIP_FULLCONTACT", buf));
395 /* receive address */
396 field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf)-1, 1);
397 CHECK_RES(ast_msg_set_var(msg, "SIP_RECVADDR", field));
400 if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
401 CHECK_RES(ast_msg_set_body(msg, "%s", buf));
405 if (endpt->id.self.name.valid) {
406 CHECK_RES(ast_msg_set_var(msg, "SIP_PEERNAME", endpt->id.self.name.str));
409 CHECK_RES(headers_to_vars(rdata, msg));
420 static void msg_data_destroy(void *obj)
422 struct msg_data *mdata = obj;
424 ast_free(mdata->from);
427 ast_msg_destroy(mdata->msg);
430 static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
433 struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
439 /* typecast to suppress const warning */
440 mdata->msg = ast_msg_ref((struct ast_msg*)msg);
442 mdata->to = ast_strdup(to);
443 mdata->from = ast_strdup(from);
445 /* sometimes from can still contain the tag at this point, so remove it */
446 if ((tag = strchr(mdata->from, ';'))) {
453 static int msg_send(void *data)
455 RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
457 const struct ast_sip_body body = {
460 .body_text = ast_msg_get_body(mdata->msg)
463 pjsip_tx_data *tdata;
466 RAII_VAR(struct ast_sip_endpoint *, endpoint, get_endpoint(
467 mdata->to, &uri), ao2_cleanup);
469 ast_log(LOG_ERROR, "SIP MESSAGE - Endpoint not found in %s\n", mdata->to);
473 if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, &tdata)) {
474 ast_log(LOG_ERROR, "SIP MESSAGE - Could not create request\n");
478 if (ast_sip_add_body(tdata, &body)) {
479 pjsip_tx_data_dec_ref(tdata);
480 ast_log(LOG_ERROR, "SIP MESSAGE - Could not add body to request\n");
484 update_from(tdata, mdata->from);
485 vars_to_headers(mdata->msg, tdata);
486 if (ast_sip_send_request(tdata, NULL, endpoint)) {
487 ast_log(LOG_ERROR, "SIP MESSAGE - Could not send request\n");
494 static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
496 struct msg_data *mdata;
498 if (ast_strlen_zero(to)) {
499 ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n");
503 if (!(mdata = msg_data_create(msg, to, from)) ||
504 ast_sip_push_task(NULL, msg_send, mdata)) {
511 static const struct ast_msg_tech msg_tech = {
513 .msg_send = sip_msg_send,
516 static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
517 pjsip_dialog *dlg, pjsip_transaction *tsx)
519 pjsip_tx_data *tdata;
521 pjsip_response_addr res_addr;
523 pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
525 status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
526 if (status != PJ_SUCCESS) {
527 ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
532 status = pjsip_dlg_send_response(dlg, tsx, tdata);
534 /* Get where to send request. */
535 status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
536 if (status != PJ_SUCCESS) {
537 ast_log(LOG_ERROR, "Unable to get response address (%d)\n", status);
540 status = pjsip_endpt_send_response(endpt, &res_addr, tdata, NULL, NULL);
543 if (status != PJ_SUCCESS) {
544 ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
550 static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
552 enum pjsip_status_code code;
555 /* if not a MESSAGE, don't handle */
556 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
560 msg = ast_msg_alloc();
562 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
566 if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
567 send_response(rdata, code, NULL, NULL);
571 if ((code = rx_data_to_ast_msg(rdata, msg)) == PJSIP_SC_OK) {
572 /* send it to the dialplan */
574 code = PJSIP_SC_ACCEPTED;
577 send_response(rdata, code, NULL, NULL);
581 static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
583 char buf[MAX_BODY_SIZE];
584 enum pjsip_status_code code;
587 pjsip_dialog *dlg = session->inv_session->dlg;
588 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
590 if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
591 send_response(rdata, code, dlg, tsx);
595 if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
596 /* invalid body size */
600 memset(&f, 0, sizeof(f));
601 f.frametype = AST_FRAME_TEXT;
602 f.subclass.integer = 0;
605 f.datalen = strlen(buf) + 1;
606 ast_queue_frame(session->channel, &f);
608 send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
612 static struct ast_sip_session_supplement messaging_supplement = {
614 .incoming_request = incoming_in_dialog_request
617 static pjsip_module messaging_module = {
618 .name = {"Messaging Module", 16},
620 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
621 .on_rx_request = module_on_rx_request,
624 static int load_module(void)
626 if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
627 return AST_MODULE_LOAD_DECLINE;
630 if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
631 NULL, PJSIP_H_ALLOW, NULL, 1,
632 &pjsip_message_method.name) != PJ_SUCCESS) {
634 ast_sip_unregister_service(&messaging_module);
635 return AST_MODULE_LOAD_DECLINE;
638 if (ast_msg_tech_register(&msg_tech)) {
639 ast_sip_unregister_service(&messaging_module);
640 return AST_MODULE_LOAD_DECLINE;
643 ast_sip_session_register_supplement(&messaging_supplement);
644 return AST_MODULE_LOAD_SUCCESS;
647 static int unload_module(void)
649 ast_sip_session_unregister_supplement(&messaging_supplement);
650 ast_msg_tech_unregister(&msg_tech);
651 ast_sip_unregister_service(&messaging_module);
655 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
657 .unload = unload_module,
658 .load_pri = AST_MODPRI_APP_DEPEND,