| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include <pjsip.h> |
| 29 |
#include <pjsip_ua.h> |
| 30 |
|
| 31 |
#include "asterisk/res_pjsip.h" |
| 32 |
#include "asterisk/res_pjsip_session.h" |
| 33 |
#include "asterisk/callerid.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/strings.h" |
| 37 |
|
| 38 |
static const pj_str_t diversion_name = { "Diversion", 9 }; |
| 39 |
static const pj_str_t history_info_name = { "History-Info", 12 }; |
| 40 |
static pj_str_t HISTINFO_SUPPORTED_NAME = { "histinfo", 8 }; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
typedef enum { |
| 46 |
PJSIP_DIVERSION_NOSEND_UPDATE = 0, |
| 47 |
PJSIP_DIVERSION_SEND_UPDATE = 1, |
| 48 |
} pjsip_diversion_send_update; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static int sip_is_token(const char *str) |
| 62 |
{ |
| 63 |
int is_token; |
| 64 |
|
| 65 |
if (ast_strlen_zero(str)) { |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
is_token = 1; |
| 71 |
do { |
| 72 |
if (!isalnum(*str) |
| 73 |
&& !strchr("-.!%*_+`'~", *str)) { |
| 74 |
|
| 75 |
is_token = 0; |
| 76 |
break; |
| 77 |
} |
| 78 |
} while (*++str); |
| 79 |
|
| 80 |
return is_token; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static const struct reasons { |
| 91 |
enum AST_REDIRECTING_REASON code; |
| 92 |
const char *text; |
| 93 |
const unsigned int cause; |
| 94 |
} reason_table[] = { |
| 95 |
{ AST_REDIRECTING_REASON_UNKNOWN, "unknown", 404 }, |
| 96 |
{ AST_REDIRECTING_REASON_USER_BUSY, "user-busy", 486 }, |
| 97 |
{ AST_REDIRECTING_REASON_NO_ANSWER, "no-answer", 408 }, |
| 98 |
{ AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable", 503 }, |
| 99 |
{ AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional", 302 }, |
| 100 |
{ AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day", 404 }, |
| 101 |
{ AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb", 404 }, |
| 102 |
{ AST_REDIRECTING_REASON_DEFLECTION, "deflection", 480 }, |
| 103 |
{ AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me", 404 }, |
| 104 |
{ AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service", 404 }, |
| 105 |
{ AST_REDIRECTING_REASON_AWAY, "away", 404 }, |
| 106 |
{ AST_REDIRECTING_REASON_CALL_FWD_DTE, "cf_dte", 404 }, |
| 107 |
{ AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm", 404 }, |
| 108 |
}; |
| 109 |
|
| 110 |
static enum AST_REDIRECTING_REASON cause_to_reason(const unsigned long cause) { |
| 111 |
switch(cause) { |
| 112 |
case 302: |
| 113 |
return AST_REDIRECTING_REASON_UNCONDITIONAL; |
| 114 |
case 486: |
| 115 |
return AST_REDIRECTING_REASON_USER_BUSY; |
| 116 |
case 408: |
| 117 |
return AST_REDIRECTING_REASON_NO_ANSWER; |
| 118 |
case 480: |
| 119 |
case 487: |
| 120 |
return AST_REDIRECTING_REASON_DEFLECTION; |
| 121 |
case 503: |
| 122 |
return AST_REDIRECTING_REASON_UNAVAILABLE; |
| 123 |
default: |
| 124 |
return AST_REDIRECTING_REASON_UNKNOWN; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static int add_supported(pjsip_tx_data *tdata) |
| 129 |
{ |
| 130 |
pjsip_supported_hdr *hdr; |
| 131 |
unsigned int i; |
| 132 |
|
| 133 |
hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL); |
| 134 |
if (!hdr) { |
| 135 |
|
| 136 |
hdr = pjsip_supported_hdr_create(tdata->pool); |
| 137 |
if (!hdr) { |
| 138 |
return -1; |
| 139 |
} |
| 140 |
|
| 141 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
for (i = 0; i < hdr->count; ++i) { |
| 148 |
if (pj_stricmp(&hdr->values[i], &HISTINFO_SUPPORTED_NAME) == 0) { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (hdr->count >= PJSIP_GENERIC_ARRAY_MAX_COUNT) { |
| 154 |
return -1; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
pj_strassign(&hdr->values[hdr->count++], &HISTINFO_SUPPORTED_NAME); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static const char *reason_code_to_str(const struct ast_party_redirecting_reason *reason) |
| 164 |
{ |
| 165 |
int idx; |
| 166 |
int code; |
| 167 |
|
| 168 |
|
| 169 |
if (!ast_strlen_zero(reason->str)) { |
| 170 |
return reason->str; |
| 171 |
} |
| 172 |
|
| 173 |
code = reason->code; |
| 174 |
for (idx = 0; idx < ARRAY_LEN(reason_table); ++idx) { |
| 175 |
if (code == reason_table[idx].code) { |
| 176 |
return reason_table[idx].text; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return "unknown"; |
| 181 |
} |
| 182 |
|
| 183 |
static const unsigned int reason_code_to_cause(const struct ast_party_redirecting_reason *reason) |
| 184 |
{ |
| 185 |
int idx; |
| 186 |
int code; |
| 187 |
|
| 188 |
code = reason->code; |
| 189 |
for (idx = 0; idx < ARRAY_LEN(reason_table); ++idx) { |
| 190 |
if (code == reason_table[idx].code) { |
| 191 |
return reason_table[idx].cause; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return 404; |
| 196 |
} |
| 197 |
|
| 198 |
static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata) |
| 199 |
{ |
| 200 |
static const pj_str_t from_name = { "From", 4 }; |
| 201 |
|
| 202 |
pjsip_generic_string_hdr *hdr; |
| 203 |
pj_str_t value; |
| 204 |
int size; |
| 205 |
|
| 206 |
if (!(hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &diversion_name, NULL))) { |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue); |
| 211 |
|
| 212 |
|
| 213 |
return pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr, |
| 214 |
pj_strlen(&value), &size); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static pjsip_fromto_hdr *get_history_info_header(pjsip_rx_data *rdata, const unsigned int first) |
| 224 |
{ |
| 225 |
static const pj_str_t from_name = { "From", 4 }; |
| 226 |
pjsip_fromto_hdr * result_hdr = NULL; |
| 227 |
|
| 228 |
pjsip_generic_string_hdr *hdr = NULL; |
| 229 |
|
| 230 |
hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &history_info_name, NULL); |
| 231 |
|
| 232 |
if (!hdr) { |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
do { |
| 237 |
static const pj_str_t index_name = { "index", 5 }; |
| 238 |
pj_str_t value; |
| 239 |
int size; |
| 240 |
pjsip_fromto_hdr * fromto_hdr = NULL; |
| 241 |
pjsip_param * index = NULL; |
| 242 |
|
| 243 |
pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue); |
| 244 |
|
| 245 |
|
| 246 |
fromto_hdr = pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr, |
| 247 |
pj_strlen(&value), &size); |
| 248 |
|
| 249 |
if (fromto_hdr == NULL) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
|
| 253 |
index = pjsip_param_find(&fromto_hdr->other_param, &index_name); |
| 254 |
|
| 255 |
if (index) { |
| 256 |
if (!pj_strcmp2(&index->value, "1")) { |
| 257 |
if (!first) { |
| 258 |
continue; |
| 259 |
} else { |
| 260 |
return fromto_hdr; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
result_hdr = fromto_hdr; |
| 266 |
|
| 267 |
} while ((hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &history_info_name, hdr->next))); |
| 268 |
|
| 269 |
return result_hdr; |
| 270 |
} |
| 271 |
|
| 272 |
static void set_redirecting_value(char **dst, const pj_str_t *src) |
| 273 |
{ |
| 274 |
ast_free(*dst); |
| 275 |
*dst = ast_malloc(pj_strlen(src) + 1); |
| 276 |
if (*dst) { |
| 277 |
ast_copy_pj_str(*dst, src, pj_strlen(src) + 1); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
static void set_redirecting_id(pjsip_name_addr *name_addr, struct ast_party_id *data, |
| 282 |
struct ast_set_party_id *update) |
| 283 |
{ |
| 284 |
pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr->uri); |
| 285 |
char *semi; |
| 286 |
pj_str_t uri_user; |
| 287 |
|
| 288 |
uri_user = uri->user; |
| 289 |
|
| 290 |
|
| 291 |
semi = pj_strchr(&uri_user, ';'); |
| 292 |
if (semi) { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
pj_strset(&uri_user, (char *) pj_strbuf(&uri_user), semi - pj_strbuf(&uri_user)); |
| 305 |
} |
| 306 |
|
| 307 |
if (pj_strlen(&uri_user)) { |
| 308 |
update->number = 1; |
| 309 |
data->number.valid = 1; |
| 310 |
set_redirecting_value(&data->number.str, &uri_user); |
| 311 |
} |
| 312 |
|
| 313 |
if (pj_strlen(&name_addr->display)) { |
| 314 |
update->name = 1; |
| 315 |
data->name.valid = 1; |
| 316 |
set_redirecting_value(&data->name.str, &name_addr->display); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
static void copy_redirecting_id(struct ast_party_id *dst, const struct ast_party_id *src, |
| 321 |
struct ast_set_party_id *update) |
| 322 |
{ |
| 323 |
ast_party_id_copy(dst, src); |
| 324 |
|
| 325 |
if (dst->number.valid) { |
| 326 |
update->number = 1; |
| 327 |
} |
| 328 |
|
| 329 |
if (dst->name.valid) { |
| 330 |
update->name = 1; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
static void set_redirecting_reason_by_cause(pjsip_name_addr *name_addr, |
| 335 |
struct ast_party_redirecting_reason *data) |
| 336 |
{ |
| 337 |
static const pj_str_t cause_name = { "cause", 5 }; |
| 338 |
pjsip_uri *uri = name_addr->uri; |
| 339 |
pjsip_param *cause = NULL; |
| 340 |
unsigned long cause_value = 0; |
| 341 |
|
| 342 |
if (!ast_sip_is_allowed_uri(uri)) { |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
cause = ast_sip_pjsip_uri_get_other_param(uri, &cause_name); |
| 347 |
|
| 348 |
if (!cause) { |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
cause_value = pj_strtoul(&cause->value); |
| 353 |
|
| 354 |
data->code = cause_to_reason(cause_value); |
| 355 |
ast_free(data->str); |
| 356 |
data->str = ast_strdup(""); |
| 357 |
} |
| 358 |
|
| 359 |
static void set_redirecting_reason(pjsip_fromto_hdr *from_info, pjsip_name_addr *to_info, |
| 360 |
struct ast_party_redirecting_reason *data) |
| 361 |
{ |
| 362 |
static const pj_str_t reason_name = { "reason", 6 }; |
| 363 |
pjsip_param *reason = pjsip_param_find(&from_info->other_param, &reason_name); |
| 364 |
char *reason_str; |
| 365 |
|
| 366 |
if (!reason) { |
| 367 |
if (to_info) { |
| 368 |
set_redirecting_reason_by_cause(to_info, data); |
| 369 |
} |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
set_redirecting_value(&data->str, &reason->value); |
| 374 |
if (!data->str) { |
| 375 |
|
| 376 |
return; |
| 377 |
} |
| 378 |
reason_str = ast_strdupa(data->str); |
| 379 |
|
| 380 |
|
| 381 |
if (*reason_str == '"') { |
| 382 |
reason_str = ast_strip_quoted(reason_str, "\"", "\""); |
| 383 |
} |
| 384 |
|
| 385 |
data->code = ast_redirecting_reason_parse(reason_str); |
| 386 |
if (data->code < 0) { |
| 387 |
data->code = AST_REDIRECTING_REASON_UNKNOWN; |
| 388 |
} else { |
| 389 |
ast_free(data->str); |
| 390 |
data->str = ast_strdup(""); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static void set_redirecting(struct ast_sip_session *session, |
| 395 |
pjsip_fromto_hdr *from_info, |
| 396 |
pjsip_name_addr *to_info, |
| 397 |
pjsip_diversion_send_update send_update) |
| 398 |
{ |
| 399 |
struct ast_party_redirecting data; |
| 400 |
struct ast_set_party_redirecting update; |
| 401 |
|
| 402 |
if (!session->channel) { |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
ast_party_redirecting_init(&data); |
| 407 |
memset(&update, 0, sizeof(update)); |
| 408 |
|
| 409 |
data.reason.code = AST_REDIRECTING_REASON_UNKNOWN; |
| 410 |
if (from_info) { |
| 411 |
set_redirecting_id((pjsip_name_addr*)from_info->uri, |
| 412 |
&data.from, &update.from); |
| 413 |
set_redirecting_reason(from_info, to_info, &data.reason); |
| 414 |
ast_set_party_id_all(&update.priv_to); |
| 415 |
} else { |
| 416 |
copy_redirecting_id(&data.from, &session->id, &update.from); |
| 417 |
} |
| 418 |
|
| 419 |
if (to_info) { |
| 420 |
set_redirecting_id(to_info, &data.to, &update.to); |
| 421 |
} |
| 422 |
|
| 423 |
ast_set_party_id_all(&update.priv_orig); |
| 424 |
ast_set_party_id_all(&update.priv_from); |
| 425 |
ast_set_party_id_all(&update.priv_to); |
| 426 |
++data.count; |
| 427 |
|
| 428 |
ast_channel_set_redirecting(session->channel, &data, &update); |
| 429 |
|
| 430 |
if (session->inv_session->role == PJSIP_ROLE_UAC && send_update) { |
| 431 |
ast_channel_queue_redirecting_update(session->channel, &data, &update); |
| 432 |
} |
| 433 |
ast_party_redirecting_free(&data); |
| 434 |
} |
| 435 |
|
| 436 |
static int diversion_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata) |
| 437 |
{ |
| 438 |
pjsip_fromto_hdr *hdr = get_diversion_header(rdata); |
| 439 |
|
| 440 |
if (hdr) { |
| 441 |
set_redirecting(session, hdr, (pjsip_name_addr*) |
| 442 |
PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri, PJSIP_DIVERSION_SEND_UPDATE); |
| 443 |
} else { |
| 444 |
pjsip_fromto_hdr *history_info_to; |
| 445 |
pjsip_fromto_hdr *history_info_from; |
| 446 |
history_info_to = get_history_info_header(rdata, 0); |
| 447 |
|
| 448 |
if (history_info_to) { |
| 449 |
|
| 450 |
|
| 451 |
history_info_from = get_history_info_header(rdata, 1); |
| 452 |
set_redirecting(session, history_info_from, (pjsip_name_addr*)history_info_to->uri, PJSIP_DIVERSION_SEND_UPDATE); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static void diversion_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata, pjsip_diversion_send_update send_update) |
| 460 |
{ |
| 461 |
static const pj_str_t contact_name = { "Contact", 7 }; |
| 462 |
static const pj_str_t contact_name_s = { "m", 1 }; |
| 463 |
|
| 464 |
pjsip_status_line status = rdata->msg_info.msg->line.status; |
| 465 |
pjsip_fromto_hdr *div_hdr; |
| 466 |
pjsip_fromto_hdr *history_info_to; |
| 467 |
pjsip_fromto_hdr *history_info_from; |
| 468 |
pjsip_contact_hdr *contact_hdr; |
| 469 |
|
| 470 |
if ((status.code != 302) && (status.code != 181)) { |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
if (!(div_hdr = get_diversion_header(rdata))) { |
| 478 |
history_info_to = get_history_info_header(rdata, 0); |
| 479 |
|
| 480 |
if (history_info_to) { |
| 481 |
|
| 482 |
|
| 483 |
history_info_from = get_history_info_header(rdata, 1); |
| 484 |
set_redirecting(session, history_info_from, (pjsip_name_addr*)history_info_to->uri, send_update); |
| 485 |
return; |
| 486 |
} |
| 487 |
if (!div_hdr && !session->id.number.valid) { |
| 488 |
div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
if (status.code == 302) { |
| 494 |
|
| 495 |
contact_hdr = pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &contact_name, &contact_name_s, NULL); |
| 496 |
set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri : |
| 497 |
(pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri, send_update); |
| 498 |
} else { |
| 499 |
|
| 500 |
set_redirecting(session, PJSIP_MSG_TO_HDR(rdata->msg_info.msg), |
| 501 |
div_hdr ? (pjsip_name_addr*)div_hdr->uri : NULL, send_update); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
static void diversion_incoming_response_media(struct ast_sip_session *session, pjsip_rx_data *rdata) |
| 506 |
{ |
| 507 |
|
| 508 |
diversion_incoming_response(session, rdata, PJSIP_DIVERSION_SEND_UPDATE); |
| 509 |
return; |
| 510 |
} |
| 511 |
|
| 512 |
static void diversion_incoming_response_redirecting(struct ast_sip_session *session, pjsip_rx_data *rdata) |
| 513 |
{ |
| 514 |
|
| 515 |
|
| 516 |
diversion_incoming_response(session, rdata, PJSIP_DIVERSION_NOSEND_UPDATE); |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data) |
| 528 |
{ |
| 529 |
static const pj_str_t reason_name = { "reason", 6 }; |
| 530 |
|
| 531 |
pjsip_fromto_hdr *hdr; |
| 532 |
pjsip_name_addr *name_addr; |
| 533 |
pjsip_param *param; |
| 534 |
pjsip_fromto_hdr *old_hdr; |
| 535 |
const char *reason_str; |
| 536 |
const char *quote_str; |
| 537 |
char *reason_buf; |
| 538 |
pjsip_uri *base; |
| 539 |
|
| 540 |
struct ast_party_id *id = NULL; |
| 541 |
if (tdata->msg->type == PJSIP_REQUEST_MSG) { |
| 542 |
id = &data->from; |
| 543 |
} else { |
| 544 |
|
| 545 |
id = &data->to; |
| 546 |
} |
| 547 |
|
| 548 |
base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri; |
| 549 |
|
| 550 |
if (!id->number.valid || ast_strlen_zero(id->number.str)) { |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
hdr = pjsip_from_hdr_create(tdata->pool); |
| 555 |
hdr->type = PJSIP_H_OTHER; |
| 556 |
hdr->sname = hdr->name = diversion_name; |
| 557 |
|
| 558 |
name_addr = pjsip_uri_clone(tdata->pool, base); |
| 559 |
|
| 560 |
pj_strdup2(tdata->pool, &name_addr->display, id->name.str); |
| 561 |
pj_strdup2(tdata->pool, (pj_str_t *)ast_sip_pjsip_uri_get_username(name_addr->uri), id->number.str); |
| 562 |
|
| 563 |
param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param); |
| 564 |
param->name = reason_name; |
| 565 |
|
| 566 |
reason_str = reason_code_to_str(&data->reason); |
| 567 |
|
| 568 |
|
| 569 |
quote_str = *reason_str == '\"' || sip_is_token(reason_str) ? "" : "\""; |
| 570 |
|
| 571 |
reason_buf = pj_pool_alloc(tdata->pool, strlen(reason_str) + 3); |
| 572 |
sprintf(reason_buf, "%s%s%s", quote_str, reason_str, quote_str); |
| 573 |
|
| 574 |
param->value = pj_str(reason_buf); |
| 575 |
|
| 576 |
pj_list_insert_before(&hdr->other_param, param); |
| 577 |
|
| 578 |
hdr->uri = (pjsip_uri *) name_addr; |
| 579 |
old_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &diversion_name, NULL); |
| 580 |
if (old_hdr) { |
| 581 |
pj_list_erase(old_hdr); |
| 582 |
} |
| 583 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
static void add_history_info_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data) |
| 594 |
{ |
| 595 |
static const pj_str_t index_name = { "index", 5 }; |
| 596 |
static const pj_str_t cause_name = { "cause", 5 }; |
| 597 |
static const pj_str_t first_index = { "1", 1 }; |
| 598 |
static const pj_str_t last_index = { "1.1", 3 }; |
| 599 |
|
| 600 |
pjsip_fromto_hdr *hdr; |
| 601 |
pjsip_name_addr *name_addr; |
| 602 |
pjsip_sip_uri *uri; |
| 603 |
pjsip_param *param; |
| 604 |
pjsip_fromto_hdr *old_hdr; |
| 605 |
unsigned int cause; |
| 606 |
char *cause_buf; |
| 607 |
|
| 608 |
struct ast_party_id *to = &data->to; |
| 609 |
struct ast_party_id *from = &data->from; |
| 610 |
|
| 611 |
pjsip_uri *base = PJSIP_MSG_TO_HDR(tdata->msg)->uri; |
| 612 |
|
| 613 |
|
| 614 |
hdr = pjsip_from_hdr_create(tdata->pool); |
| 615 |
hdr->type = PJSIP_H_OTHER; |
| 616 |
hdr->sname = hdr->name = history_info_name; |
| 617 |
|
| 618 |
name_addr = pjsip_uri_clone(tdata->pool, base); |
| 619 |
uri = pjsip_uri_get_uri(name_addr->uri); |
| 620 |
|
| 621 |
|
| 622 |
if (from->number.valid && !ast_strlen_zero(from->number.str)) { |
| 623 |
pj_strdup2(tdata->pool, &name_addr->display, from->name.str); |
| 624 |
pj_strdup2(tdata->pool, &uri->user, from->number.str); |
| 625 |
} |
| 626 |
|
| 627 |
param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param); |
| 628 |
param->name = index_name; |
| 629 |
param->value = first_index; |
| 630 |
|
| 631 |
|
| 632 |
pj_list_insert_before(&hdr->other_param, param); |
| 633 |
hdr->uri = (pjsip_uri *) name_addr; |
| 634 |
|
| 635 |
while ((old_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &history_info_name, NULL)) != NULL) { |
| 636 |
pj_list_erase(old_hdr); |
| 637 |
} |
| 638 |
|
| 639 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr); |
| 640 |
|
| 641 |
if (!to->number.valid || ast_strlen_zero(to->number.str)) { |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
hdr = pjsip_from_hdr_create(tdata->pool); |
| 646 |
hdr->type = PJSIP_H_OTHER; |
| 647 |
hdr->sname = hdr->name = history_info_name; |
| 648 |
|
| 649 |
name_addr = pjsip_uri_clone(tdata->pool, base); |
| 650 |
uri = pjsip_uri_get_uri(name_addr->uri); |
| 651 |
|
| 652 |
pj_strdup2(tdata->pool, &name_addr->display, to->name.str); |
| 653 |
pj_strdup2(tdata->pool, &uri->user, to->number.str); |
| 654 |
|
| 655 |
param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param); |
| 656 |
param->name = index_name; |
| 657 |
param->value = last_index; |
| 658 |
pj_list_insert_before(&hdr->other_param, param); |
| 659 |
|
| 660 |
param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param); |
| 661 |
param->name = cause_name; |
| 662 |
cause = reason_code_to_cause(&data->reason); |
| 663 |
cause_buf = pj_pool_alloc(tdata->pool, 4); |
| 664 |
snprintf(cause_buf, 4, "%ud", cause); |
| 665 |
param->value = pj_str(cause_buf); |
| 666 |
pj_list_insert_before(&uri->other_param, param); |
| 667 |
hdr->uri = (pjsip_uri *) name_addr; |
| 668 |
|
| 669 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr); |
| 670 |
} |
| 671 |
|
| 672 |
static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata) |
| 673 |
{ |
| 674 |
struct ast_party_redirecting *data; |
| 675 |
|
| 676 |
add_supported(tdata); |
| 677 |
|
| 678 |
if (session->channel && session->endpoint->id.send_diversion && |
| 679 |
(data = ast_channel_redirecting(session->channel))->count) { |
| 680 |
add_diversion_header(tdata, data); |
| 681 |
} |
| 682 |
if (session->channel && session->endpoint->id.send_history_info) { |
| 683 |
data = ast_channel_redirecting(session->channel); |
| 684 |
add_history_info_header(tdata, data); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
static void diversion_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata) |
| 697 |
{ |
| 698 |
get_redirecting_add_diversion(session, tdata); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
static void diversion_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata) |
| 709 |
{ |
| 710 |
struct pjsip_status_line status = tdata->msg->line.status; |
| 711 |
|
| 712 |
|
| 713 |
if (PJSIP_IS_STATUS_IN_CLASS(status.code, 300) || (status.code == 181)) { |
| 714 |
get_redirecting_add_diversion(session, tdata); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
static struct ast_sip_session_supplement diversion_supplement_media = { |
| 719 |
.method = "INVITE", |
| 720 |
|
| 721 |
|
| 722 |
.priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100, |
| 723 |
.incoming_response = diversion_incoming_response_media, |
| 724 |
.response_priority = AST_SIP_SESSION_BEFORE_MEDIA, |
| 725 |
}; |
| 726 |
|
| 727 |
static struct ast_sip_session_supplement diversion_supplement_redirecting = { |
| 728 |
.method = "INVITE", |
| 729 |
|
| 730 |
|
| 731 |
.priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100, |
| 732 |
.incoming_request = diversion_incoming_request, |
| 733 |
.incoming_response = diversion_incoming_response_redirecting, |
| 734 |
.outgoing_request = diversion_outgoing_request, |
| 735 |
.outgoing_response = diversion_outgoing_response, |
| 736 |
.response_priority = AST_SIP_SESSION_BEFORE_REDIRECTING, |
| 737 |
}; |
| 738 |
|
| 739 |
static int load_module(void) |
| 740 |
{ |
| 741 |
|
| 742 |
|
| 743 |
ast_module_shutdown_ref(ast_module_info->self); |
| 744 |
ast_sip_session_register_supplement(&diversion_supplement_media); |
| 745 |
ast_sip_session_register_supplement(&diversion_supplement_redirecting); |
| 746 |
return AST_MODULE_LOAD_SUCCESS; |
| 747 |
} |
| 748 |
|
| 749 |
static int unload_module(void) |
| 750 |
{ |
| 751 |
ast_sip_session_unregister_supplement(&diversion_supplement_media); |
| 752 |
ast_sip_session_unregister_supplement(&diversion_supplement_redirecting); |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Add Diversion Header Support", |
| 757 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 758 |
.load = load_module, |
| 759 |
.unload = unload_module, |
| 760 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 761 |
.requires = "res_pjsip,res_pjsip_session", |
| 762 |
); |