res_pjsip_diversion.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Kevin Harwell <kharwell@digium.com>
  7. *
  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.
  13. *
  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.
  17. */
  18. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <depend>res_pjsip_session</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_ua.h>
  27. #include "asterisk/res_pjsip.h"
  28. #include "asterisk/res_pjsip_session.h"
  29. #include "asterisk/callerid.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/strings.h"
  33. static const pj_str_t diversion_name = { "Diversion", 9 };
  34. /*! \brief Diversion header reasons
  35. *
  36. * The core defines a bunch of constants used to define
  37. * redirecting reasons. This provides a translation table
  38. * between those and the strings which may be present in
  39. * a SIP Diversion header
  40. */
  41. static const struct reasons {
  42. enum AST_REDIRECTING_REASON code;
  43. char *const text;
  44. } reason_table[] = {
  45. { AST_REDIRECTING_REASON_UNKNOWN, "unknown" },
  46. { AST_REDIRECTING_REASON_USER_BUSY, "user-busy" },
  47. { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer" },
  48. { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable" },
  49. { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional" },
  50. { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day" },
  51. { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },
  52. { AST_REDIRECTING_REASON_DEFLECTION, "deflection" },
  53. { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me" },
  54. { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service" },
  55. { AST_REDIRECTING_REASON_AWAY, "away" },
  56. { AST_REDIRECTING_REASON_CALL_FWD_DTE, "unknown"},
  57. { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm"},
  58. };
  59. static const char *reason_code_to_str(const struct ast_party_redirecting_reason *reason)
  60. {
  61. int code = reason->code;
  62. /* use specific string if given */
  63. if (!ast_strlen_zero(reason->str)) {
  64. return reason->str;
  65. }
  66. if (code >= 0 && code < ARRAY_LEN(reason_table)) {
  67. return reason_table[code].text;
  68. }
  69. return "unknown";
  70. }
  71. static enum AST_REDIRECTING_REASON reason_str_to_code(const char *text)
  72. {
  73. enum AST_REDIRECTING_REASON code = AST_REDIRECTING_REASON_UNKNOWN;
  74. int i;
  75. for (i = 0; i < ARRAY_LEN(reason_table); ++i) {
  76. if (!strcasecmp(text, reason_table[i].text)) {
  77. code = reason_table[i].code;
  78. break;
  79. }
  80. }
  81. return code;
  82. }
  83. static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
  84. {
  85. static const pj_str_t from_name = { "From", 4 };
  86. pjsip_generic_string_hdr *hdr;
  87. pj_str_t value;
  88. int size;
  89. if (!(hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &diversion_name, NULL))) {
  90. return NULL;
  91. }
  92. pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
  93. /* parse as a fromto header */
  94. return pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr,
  95. pj_strlen(&value), &size);
  96. }
  97. static void set_redirecting_value(char **dst, const pj_str_t *src)
  98. {
  99. ast_free(*dst);
  100. *dst = ast_malloc(pj_strlen(src) + 1);
  101. ast_copy_pj_str(*dst, src, pj_strlen(src) + 1);
  102. }
  103. static void set_redirecting_id(pjsip_name_addr *name_addr, struct ast_party_id *data,
  104. struct ast_set_party_id *update)
  105. {
  106. pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr->uri);
  107. if (pj_strlen(&uri->user)) {
  108. update->number = 1;
  109. data->number.valid = 1;
  110. set_redirecting_value(&data->number.str, &uri->user);
  111. }
  112. if (pj_strlen(&name_addr->display)) {
  113. update->name = 1;
  114. data->name.valid = 1;
  115. set_redirecting_value(&data->name.str, &name_addr->display);
  116. }
  117. }
  118. static void copy_redirecting_id(struct ast_party_id *dst, const struct ast_party_id *src,
  119. struct ast_set_party_id *update)
  120. {
  121. ast_party_id_copy(dst, src);
  122. if (dst->number.valid) {
  123. update->number = 1;
  124. }
  125. if (dst->name.valid) {
  126. update->name = 1;
  127. }
  128. }
  129. static void set_redirecting_reason(pjsip_fromto_hdr *hdr,
  130. struct ast_party_redirecting_reason *data)
  131. {
  132. static const pj_str_t reason_name = { "reason", 6 };
  133. pjsip_param *reason = pjsip_param_find(&hdr->other_param, &reason_name);
  134. if (!reason) {
  135. return;
  136. }
  137. set_redirecting_value(&data->str, &reason->value);
  138. data->code = reason_str_to_code(data->str);
  139. }
  140. static void set_redirecting(struct ast_sip_session *session,
  141. pjsip_fromto_hdr *from_info,
  142. pjsip_name_addr *to_info)
  143. {
  144. struct ast_party_redirecting data;
  145. struct ast_set_party_redirecting update;
  146. if (!session->channel) {
  147. return;
  148. }
  149. ast_party_redirecting_init(&data);
  150. memset(&update, 0, sizeof(update));
  151. if (from_info) {
  152. set_redirecting_id((pjsip_name_addr*)from_info->uri,
  153. &data.from, &update.from);
  154. set_redirecting_reason(from_info, &data.reason);
  155. } else {
  156. copy_redirecting_id(&data.from, &session->id, &update.from);
  157. }
  158. set_redirecting_id(to_info, &data.to, &update.to);
  159. ast_set_party_id_all(&update.priv_orig);
  160. ast_set_party_id_all(&update.priv_from);
  161. ast_set_party_id_all(&update.priv_to);
  162. ++data.count;
  163. ast_channel_set_redirecting(session->channel, &data, &update);
  164. ast_party_redirecting_free(&data);
  165. }
  166. static int diversion_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  167. {
  168. pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
  169. if (hdr) {
  170. set_redirecting(session, hdr, (pjsip_name_addr*)
  171. PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri);
  172. }
  173. return 0;
  174. }
  175. static void diversion_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
  176. {
  177. static const pj_str_t contact_name = { "Contact", 7 };
  178. pjsip_status_line status = rdata->msg_info.msg->line.status;
  179. pjsip_fromto_hdr *div_hdr;
  180. pjsip_contact_hdr *contact_hdr;
  181. if ((status.code != 302) && (status.code != 181)) {
  182. return;
  183. }
  184. /* use the diversion header info if there is one. if not one then use the
  185. session caller id info. if that doesn't exist use info from the To hdr*/
  186. if (!(div_hdr = get_diversion_header(rdata)) && !session->id.number.valid) {
  187. div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);
  188. }
  189. contact_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &contact_name, NULL);
  190. set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :
  191. (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);
  192. }
  193. /*!
  194. * \internal
  195. * \brief Adds diversion header information to an outbound SIP message
  196. *
  197. * \param tdata The outbound message
  198. * \param data The redirecting data used to fill parts of the diversion header
  199. */
  200. static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)
  201. {
  202. pjsip_fromto_hdr *hdr;
  203. pjsip_name_addr *name_addr;
  204. pjsip_sip_uri *uri;
  205. pjsip_param *param;
  206. struct ast_party_id *id = &data->from;
  207. pjsip_uri *base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
  208. if (!id->number.valid || ast_strlen_zero(id->number.str)) {
  209. return;
  210. }
  211. hdr = pjsip_from_hdr_create(tdata->pool);
  212. hdr->type = PJSIP_H_OTHER;
  213. pj_strdup(tdata->pool, &hdr->name, &diversion_name);
  214. hdr->sname.slen = 0;
  215. name_addr = pjsip_uri_clone(tdata->pool, base);
  216. uri = pjsip_uri_get_uri(name_addr->uri);
  217. pj_strdup2(tdata->pool, &name_addr->display, id->name.str);
  218. pj_strdup2(tdata->pool, &uri->user, id->number.str);
  219. param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
  220. param->name = pj_str("reason");
  221. param->value = pj_str((char*)reason_code_to_str(&data->reason));
  222. pj_list_insert_before(&hdr->other_param, param);
  223. hdr->uri = (pjsip_uri *) name_addr;
  224. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
  225. }
  226. static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata)
  227. {
  228. struct ast_party_redirecting *data;
  229. if (session->channel && session->endpoint->id.send_diversion &&
  230. (data = ast_channel_redirecting(session->channel))->count) {
  231. add_diversion_header(tdata, data);
  232. }
  233. }
  234. /*!
  235. * \internal
  236. * \brief Adds a diversion header to an outgoing INVITE request if
  237. * redirecting information is available.
  238. *
  239. * \param session The session on which the INVITE request is to be sent
  240. * \param tdata The outbound INVITE request
  241. */
  242. static void diversion_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
  243. {
  244. get_redirecting_add_diversion(session, tdata);
  245. }
  246. /*!
  247. * \internal
  248. * \brief Adds a diversion header to an outgoing 3XX response
  249. *
  250. * \param session The session on which the INVITE response is to be sent
  251. * \param tdata The outbound INVITE response
  252. */
  253. static void diversion_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
  254. {
  255. struct pjsip_status_line status = tdata->msg->line.status;
  256. /* add to 302 and 181 */
  257. if (PJSIP_IS_STATUS_IN_CLASS(status.code, 300) || (status.code == 181)) {
  258. get_redirecting_add_diversion(session, tdata);
  259. }
  260. }
  261. static struct ast_sip_session_supplement diversion_supplement = {
  262. .method = "INVITE",
  263. /* this supplement needs to be called after caller id
  264. and after the channel has been created */
  265. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100,
  266. .incoming_request = diversion_incoming_request,
  267. .incoming_response = diversion_incoming_response,
  268. .outgoing_request = diversion_outgoing_request,
  269. .outgoing_response = diversion_outgoing_response,
  270. .response_priority = AST_SIP_SESSION_BEFORE_REDIRECTING,
  271. };
  272. static int load_module(void)
  273. {
  274. CHECK_PJSIP_SESSION_MODULE_LOADED();
  275. ast_sip_session_register_supplement(&diversion_supplement);
  276. return AST_MODULE_LOAD_SUCCESS;
  277. }
  278. static int unload_module(void)
  279. {
  280. ast_sip_session_unregister_supplement(&diversion_supplement);
  281. return 0;
  282. }
  283. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Add Diversion Header Support",
  284. .support_level = AST_MODULE_SUPPORT_CORE,
  285. .load = load_module,
  286. .unload = unload_module,
  287. .load_pri = AST_MODPRI_APP_DEPEND,
  288. );