| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#include "asterisk.h" |
| 121 |
|
| 122 |
#include <pjsip.h> |
| 123 |
#include <pjsip_ua.h> |
| 124 |
|
| 125 |
#include "asterisk/message.h" |
| 126 |
#include "asterisk/module.h" |
| 127 |
#include "asterisk/pbx.h" |
| 128 |
#include "asterisk/res_pjsip.h" |
| 129 |
#include "asterisk/res_pjsip_redirect.h" |
| 130 |
#include "asterisk/res_pjsip_session.h" |
| 131 |
#include "asterisk/taskprocessor.h" |
| 132 |
#include "asterisk/test.h" |
| 133 |
#include "asterisk/uri.h" |
| 134 |
|
| 135 |
const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} }; |
| 136 |
|
| 137 |
#define MAX_HDR_SIZE 512 |
| 138 |
#define MAX_BODY_SIZE 1024 |
| 139 |
#define MAX_USER_SIZE 128 |
| 140 |
|
| 141 |
static struct ast_taskprocessor *message_serializer; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata) |
| 153 |
{ |
| 154 |
int res; |
| 155 |
if (rdata->msg_info.msg->body && rdata->msg_info.msg->body->len) { |
| 156 |
res = ast_sip_is_content_type( |
| 157 |
&rdata->msg_info.msg->body->content_type, "text", "plain"); |
| 158 |
} else { |
| 159 |
res = rdata->msg_info.ctype && |
| 160 |
ast_sip_is_content_type( |
| 161 |
&rdata->msg_info.ctype->media, "text", "plain"); |
| 162 |
} |
| 163 |
|
| 164 |
return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static enum pjsip_status_code check_content_type_in_dialog(const pjsip_rx_data *rdata) |
| 177 |
{ |
| 178 |
int res = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE; |
| 179 |
static const pj_str_t text = { "text", 4}; |
| 180 |
static const pj_str_t application = { "application", 11}; |
| 181 |
|
| 182 |
if (!(rdata->msg_info.msg->body && rdata->msg_info.msg->body->len > 0)) { |
| 183 |
return res; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if (pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &text) == 0 |
| 188 |
|| pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &application) == 0) { |
| 189 |
res = PJSIP_SC_OK; |
| 190 |
} else if (rdata->msg_info.ctype |
| 191 |
&& (pj_stricmp(&rdata->msg_info.ctype->media.type, &text) == 0 |
| 192 |
|| pj_stricmp(&rdata->msg_info.ctype->media.type, &application) == 0)) { |
| 193 |
res = PJSIP_SC_OK; |
| 194 |
} |
| 195 |
|
| 196 |
return res; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static int update_to_display_name(pjsip_tx_data *tdata, char *to) |
| 209 |
{ |
| 210 |
pjsip_name_addr *parsed_name_addr; |
| 211 |
|
| 212 |
parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, to, strlen(to), |
| 213 |
PJSIP_PARSE_URI_AS_NAMEADDR); |
| 214 |
|
| 215 |
if (parsed_name_addr) { |
| 216 |
if (pj_strlen(&parsed_name_addr->display)) { |
| 217 |
pjsip_name_addr *name_addr = |
| 218 |
(pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri; |
| 219 |
|
| 220 |
pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display); |
| 221 |
|
| 222 |
} |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
static int is_msg_var_blocked(const char *name) |
| 242 |
{ |
| 243 |
int i; |
| 244 |
|
| 245 |
|
| 246 |
static const char *hdr[] = { |
| 247 |
"To", |
| 248 |
"From", |
| 249 |
"Via", |
| 250 |
"Route", |
| 251 |
"Contact", |
| 252 |
"Call-ID", |
| 253 |
"CSeq", |
| 254 |
"Allow", |
| 255 |
"Content-Length", |
| 256 |
"Content-Type", |
| 257 |
"Request-URI", |
| 258 |
}; |
| 259 |
|
| 260 |
for (i = 0; i < ARRAY_LEN(hdr); ++i) { |
| 261 |
if (!strcasecmp(name, hdr[i])) { |
| 262 |
|
| 263 |
return 1; |
| 264 |
} |
| 265 |
} |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata) |
| 277 |
{ |
| 278 |
const char *name; |
| 279 |
const char *value; |
| 280 |
int max_forwards; |
| 281 |
struct ast_msg_var_iterator *iter; |
| 282 |
|
| 283 |
for (iter = ast_msg_var_iterator_init(msg); |
| 284 |
ast_msg_var_iterator_next(msg, iter, &name, &value); |
| 285 |
ast_msg_var_unref_current(iter)) { |
| 286 |
if (!strcasecmp(name, "Max-Forwards")) { |
| 287 |
|
| 288 |
if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) { |
| 289 |
ast_msg_var_iterator_destroy(iter); |
| 290 |
ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero. MESSAGE not sent.\n"); |
| 291 |
return -1; |
| 292 |
} |
| 293 |
sprintf((char *) value, "%d", max_forwards); |
| 294 |
ast_sip_add_header(tdata, name, value); |
| 295 |
} else if (!is_msg_var_blocked(name)) { |
| 296 |
ast_sip_add_header(tdata, name, value); |
| 297 |
} |
| 298 |
} |
| 299 |
ast_msg_var_iterator_destroy(iter); |
| 300 |
|
| 301 |
return PJSIP_SC_OK; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg) |
| 312 |
{ |
| 313 |
char *c; |
| 314 |
char name[MAX_HDR_SIZE]; |
| 315 |
char buf[MAX_HDR_SIZE]; |
| 316 |
int res = 0; |
| 317 |
pjsip_hdr *h = rdata->msg_info.msg->hdr.next; |
| 318 |
pjsip_hdr *end= &rdata->msg_info.msg->hdr; |
| 319 |
|
| 320 |
while (h != end) { |
| 321 |
if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) { |
| 322 |
buf[res] = '\0'; |
| 323 |
if ((c = strchr(buf, ':'))) { |
| 324 |
ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf)); |
| 325 |
} |
| 326 |
|
| 327 |
ast_copy_pj_str(name, &h->name, sizeof(name)); |
| 328 |
if ((res = ast_msg_set_var(msg, name, buf)) != 0) { |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
h = h->next; |
| 333 |
} |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static int print_body(pjsip_rx_data *rdata, char *buf, int len) |
| 349 |
{ |
| 350 |
int res; |
| 351 |
|
| 352 |
if (!rdata->msg_info.msg->body || !rdata->msg_info.msg->body->len) { |
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
if ((res = rdata->msg_info.msg->body->print_body( |
| 357 |
rdata->msg_info.msg->body, buf, len)) < 0) { |
| 358 |
return res; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n'))); |
| 363 |
|
| 364 |
buf[++res] = '\0'; |
| 365 |
|
| 366 |
return res; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
static char *sip_to_pjsip(char *buf, int size, int capacity) |
| 379 |
{ |
| 380 |
int count; |
| 381 |
const char *scheme; |
| 382 |
char *res = buf; |
| 383 |
|
| 384 |
|
| 385 |
if (*buf == '<') { |
| 386 |
++buf; |
| 387 |
--size; |
| 388 |
} |
| 389 |
|
| 390 |
scheme = strncmp(buf, "sip", 3) ? "pjsip:" : "pj"; |
| 391 |
count = strlen(scheme); |
| 392 |
if (count + size >= capacity) { |
| 393 |
ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri " |
| 394 |
"too large for given buffer\n"); |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
|
| 398 |
memmove(res + count, buf, size); |
| 399 |
memcpy(res, scheme, count); |
| 400 |
|
| 401 |
buf += size - 1; |
| 402 |
if (*buf == '>') { |
| 403 |
*buf = '\0'; |
| 404 |
} |
| 405 |
|
| 406 |
return res; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg) |
| 420 |
{ |
| 421 |
RAII_VAR(struct ast_sip_endpoint *, endpt, NULL, ao2_cleanup); |
| 422 |
pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri; |
| 423 |
pjsip_name_addr *name_addr; |
| 424 |
pjsip_sip_uri *suri; |
| 425 |
char *display_name; |
| 426 |
char buf[MAX_BODY_SIZE]; |
| 427 |
const char *field; |
| 428 |
const char *context; |
| 429 |
char exten[AST_MAX_EXTENSION]; |
| 430 |
int res = 0; |
| 431 |
int size; |
| 432 |
|
| 433 |
if (!ast_sip_is_allowed_uri(ruri)) { |
| 434 |
return PJSIP_SC_UNSUPPORTED_URI_SCHEME; |
| 435 |
} |
| 436 |
|
| 437 |
ast_copy_pj_str(exten, ast_sip_pjsip_uri_get_username(ruri), AST_MAX_EXTENSION); |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten); |
| 444 |
|
| 445 |
endpt = ast_pjsip_rdata_get_endpoint(rdata); |
| 446 |
ast_assert(endpt != NULL); |
| 447 |
|
| 448 |
context = S_OR(endpt->message_context, endpt->context); |
| 449 |
res |= ast_msg_set_context(msg, "%s", context); |
| 450 |
res |= ast_msg_set_exten(msg, "%s", exten); |
| 451 |
|
| 452 |
|
| 453 |
name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri; |
| 454 |
size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1); |
| 455 |
if (size <= 0) { |
| 456 |
return PJSIP_SC_INTERNAL_SERVER_ERROR; |
| 457 |
} |
| 458 |
buf[size] = '\0'; |
| 459 |
res |= ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf) - 1)); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri; |
| 472 |
suri = pjsip_uri_get_uri((pjsip_uri *)name_addr); |
| 473 |
if (name_addr->display.slen > 0) { |
| 474 |
int i = 0; |
| 475 |
char *temp_name = ast_alloca(name_addr->display.slen + 1); |
| 476 |
for (i = 0; i < name_addr->display.slen; i++) { |
| 477 |
if (name_addr->display.ptr[i] < 32) { |
| 478 |
temp_name[i] = ' '; |
| 479 |
} else { |
| 480 |
temp_name[i] = name_addr->display.ptr[i]; |
| 481 |
} |
| 482 |
} |
| 483 |
temp_name[name_addr->display.slen] = '\0'; |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
display_name = ast_alloca(name_addr->display.slen + 5); |
| 489 |
size = sprintf(display_name, "\"%s\" ", temp_name); |
| 490 |
} else { |
| 491 |
display_name = ""; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
res |= ast_msg_set_from(msg, "%s<" PJSTR_PRINTF_SPEC ":" PJSTR_PRINTF_SPEC "@" PJSTR_PRINTF_SPEC ">", |
| 502 |
display_name, |
| 503 |
PJSTR_PRINTF_VAR(*pjsip_uri_get_scheme(suri)), |
| 504 |
PJSTR_PRINTF_VAR(*ast_sip_pjsip_uri_get_username((pjsip_uri *)name_addr)), |
| 505 |
PJSTR_PRINTF_VAR(*ast_sip_pjsip_uri_get_hostname((pjsip_uri *)name_addr))); |
| 506 |
|
| 507 |
field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 3); |
| 508 |
res |= ast_msg_set_var(msg, "PJSIP_RECVADDR", field); |
| 509 |
|
| 510 |
switch (rdata->tp_info.transport->key.type) { |
| 511 |
case PJSIP_TRANSPORT_UDP: |
| 512 |
case PJSIP_TRANSPORT_UDP6: |
| 513 |
field = "udp"; |
| 514 |
break; |
| 515 |
case PJSIP_TRANSPORT_TCP: |
| 516 |
case PJSIP_TRANSPORT_TCP6: |
| 517 |
field = "tcp"; |
| 518 |
break; |
| 519 |
case PJSIP_TRANSPORT_TLS: |
| 520 |
case PJSIP_TRANSPORT_TLS6: |
| 521 |
field = "tls"; |
| 522 |
break; |
| 523 |
default: |
| 524 |
field = rdata->tp_info.transport->type_name; |
| 525 |
} |
| 526 |
ast_msg_set_var(msg, "PJSIP_TRANSPORT", field); |
| 527 |
|
| 528 |
if (print_body(rdata, buf, sizeof(buf) - 1) > 0) { |
| 529 |
res |= ast_msg_set_body(msg, "%s", buf); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
res |= ast_msg_set_tech(msg, "%s", "PJSIP"); |
| 534 |
res |= ast_msg_set_endpoint(msg, "%s", ast_sorcery_object_get_id(endpt)); |
| 535 |
if (endpt->id.self.name.valid) { |
| 536 |
res |= ast_msg_set_var(msg, "PJSIP_ENDPOINT", endpt->id.self.name.str); |
| 537 |
} |
| 538 |
|
| 539 |
res |= headers_to_vars(rdata, msg); |
| 540 |
|
| 541 |
return !res ? PJSIP_SC_OK : PJSIP_SC_INTERNAL_SERVER_ERROR; |
| 542 |
} |
| 543 |
|
| 544 |
struct msg_data { |
| 545 |
struct ast_msg *msg; |
| 546 |
char *destination; |
| 547 |
char *from; |
| 548 |
}; |
| 549 |
|
| 550 |
static void msg_data_destroy(void *obj) |
| 551 |
{ |
| 552 |
struct msg_data *mdata = obj; |
| 553 |
|
| 554 |
ast_free(mdata->from); |
| 555 |
ast_free(mdata->destination); |
| 556 |
|
| 557 |
ast_msg_destroy(mdata->msg); |
| 558 |
} |
| 559 |
|
| 560 |
static struct msg_data *msg_data_create(const struct ast_msg *msg, const char *destination, const char *from) |
| 561 |
{ |
| 562 |
struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy); |
| 563 |
|
| 564 |
if (!mdata) { |
| 565 |
return NULL; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
mdata->msg = ast_msg_ref((struct ast_msg *) msg); |
| 570 |
|
| 571 |
|
| 572 |
if (!(destination = strchr(destination, ':'))) { |
| 573 |
ao2_ref(mdata, -1); |
| 574 |
return NULL; |
| 575 |
} |
| 576 |
++destination; |
| 577 |
|
| 578 |
mdata->destination = ast_strdup(destination); |
| 579 |
mdata->from = ast_strdup(from); |
| 580 |
|
| 581 |
return mdata; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
struct message_response_data { |
| 589 |
char *from; |
| 590 |
char *to; |
| 591 |
char *body; |
| 592 |
char *content_type; |
| 593 |
struct ast_sip_redirect_state *redirect_state; |
| 594 |
}; |
| 595 |
|
| 596 |
static void message_response_data_destroy(void *obj) |
| 597 |
{ |
| 598 |
struct message_response_data *resp_data = obj; |
| 599 |
|
| 600 |
ast_free(resp_data->from); |
| 601 |
ast_free(resp_data->to); |
| 602 |
ast_free(resp_data->body); |
| 603 |
ast_free(resp_data->content_type); |
| 604 |
|
| 605 |
if (resp_data->redirect_state) { |
| 606 |
ast_sip_redirect_state_destroy(resp_data->redirect_state); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
static struct message_response_data *message_response_data_create( |
| 611 |
struct ast_sip_endpoint *endpoint, |
| 612 |
const char *from, |
| 613 |
const char *to, |
| 614 |
const char *body, |
| 615 |
const char *content_type, |
| 616 |
const char *initial_uri) |
| 617 |
{ |
| 618 |
struct message_response_data *resp_data; |
| 619 |
|
| 620 |
resp_data = ao2_alloc_options(sizeof(*resp_data), message_response_data_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 621 |
if (!resp_data) { |
| 622 |
return NULL; |
| 623 |
} |
| 624 |
|
| 625 |
resp_data->from = ast_strdup(from); |
| 626 |
resp_data->to = ast_strdup(to); |
| 627 |
resp_data->body = ast_strdup(body); |
| 628 |
resp_data->content_type = ast_strdup(content_type); |
| 629 |
|
| 630 |
if (!resp_data->from || !resp_data->to || !resp_data->body || !resp_data->content_type) { |
| 631 |
ast_log(LOG_ERROR, "Failed to allocate memory for response data strings on endpoint '%s'.\n", |
| 632 |
ast_sorcery_object_get_id(endpoint)); |
| 633 |
ao2_ref(resp_data, -1); |
| 634 |
return NULL; |
| 635 |
} |
| 636 |
|
| 637 |
resp_data->redirect_state = ast_sip_redirect_state_create(endpoint, initial_uri); |
| 638 |
if (!resp_data->redirect_state) { |
| 639 |
ast_log(LOG_ERROR, "Failed to create redirect state for endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint)); |
| 640 |
ao2_ref(resp_data, -1); |
| 641 |
return NULL; |
| 642 |
} |
| 643 |
|
| 644 |
return resp_data; |
| 645 |
} |
| 646 |
|
| 647 |
static void update_content_type(pjsip_tx_data *tdata, struct ast_msg *msg, struct ast_sip_body *body) |
| 648 |
{ |
| 649 |
static const pj_str_t CONTENT_TYPE = { "Content-Type", sizeof("Content-Type") - 1 }; |
| 650 |
|
| 651 |
const char *content_type = ast_msg_get_var(msg, pj_strbuf(&CONTENT_TYPE)); |
| 652 |
if (content_type) { |
| 653 |
pj_str_t type, subtype; |
| 654 |
pjsip_ctype_hdr *parsed; |
| 655 |
|
| 656 |
|
| 657 |
parsed = pjsip_parse_hdr(tdata->pool, &CONTENT_TYPE, |
| 658 |
ast_strdupa(content_type), strlen(content_type), |
| 659 |
NULL); |
| 660 |
|
| 661 |
if (!parsed) { |
| 662 |
ast_log(LOG_WARNING, "Failed to parse '%s' as a content type. Using text/plain\n", |
| 663 |
content_type); |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
pj_strdup_with_null(tdata->pool, &type, &parsed->media.type); |
| 669 |
pj_strdup_with_null(tdata->pool, &subtype, &parsed->media.subtype); |
| 670 |
|
| 671 |
body->type = pj_strbuf(&type); |
| 672 |
body->subtype = pj_strbuf(&subtype); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
static void msg_response_callback(void *token, pjsip_event *e); |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
static int send_message_to_uri(struct message_response_data *resp_data, const char *target_uri) |
| 689 |
{ |
| 690 |
pjsip_tx_data *tdata; |
| 691 |
struct message_response_data *new_resp_data; |
| 692 |
struct ast_sip_endpoint *endpoint; |
| 693 |
struct ast_sip_body body = { |
| 694 |
.type = "text", |
| 695 |
.subtype = "plain", |
| 696 |
.body_text = resp_data->body |
| 697 |
}; |
| 698 |
|
| 699 |
if (!resp_data->redirect_state) { |
| 700 |
ast_log(LOG_ERROR, "No redirect state available for sending a redirect message.\n"); |
| 701 |
return -1; |
| 702 |
} |
| 703 |
|
| 704 |
endpoint = ast_sip_redirect_get_endpoint(resp_data->redirect_state); |
| 705 |
|
| 706 |
ast_debug(1, "Sending redirected MESSAGE to '%s' (hop %d) on endpoint '%s'\n", |
| 707 |
target_uri, ast_sip_redirect_get_hop_count(resp_data->redirect_state), ast_sorcery_object_get_id(endpoint)); |
| 708 |
|
| 709 |
if (ast_sip_create_request("MESSAGE", NULL, endpoint, target_uri, NULL, &tdata)) { |
| 710 |
ast_log(LOG_WARNING, "Could not create redirect request for endpoint '%s'.\n", |
| 711 |
ast_sorcery_object_get_id(endpoint)); |
| 712 |
return -1; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
if (!ast_strlen_zero(resp_data->to)) { |
| 717 |
ast_sip_update_to_uri(tdata, resp_data->to); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
if (!ast_strlen_zero(resp_data->from)) { |
| 722 |
ast_sip_update_from(tdata, resp_data->from); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
if (!ast_strlen_zero(resp_data->content_type)) { |
| 727 |
char *type_copy = ast_strdupa(resp_data->content_type); |
| 728 |
char *subtype = strchr(type_copy, '/'); |
| 729 |
if (subtype) { |
| 730 |
*subtype = '\0'; |
| 731 |
subtype++; |
| 732 |
body.type = type_copy; |
| 733 |
body.subtype = subtype; |
| 734 |
} |
| 735 |
} |
| 736 |
ast_sip_add_body(tdata, &body); |
| 737 |
if (!tdata->msg->body) { |
| 738 |
pjsip_tx_data_dec_ref(tdata); |
| 739 |
ast_log(LOG_ERROR, "Could not add body to redirect request on endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint)); |
| 740 |
return -1; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
new_resp_data = ao2_alloc(sizeof(*new_resp_data), message_response_data_destroy); |
| 745 |
if (!new_resp_data) { |
| 746 |
pjsip_tx_data_dec_ref(tdata); |
| 747 |
ast_log(LOG_ERROR, "Could not allocate redirect callback data for endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint)); |
| 748 |
return -1; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
new_resp_data->from = ast_strdup(resp_data->from); |
| 753 |
new_resp_data->to = ast_strdup(resp_data->to); |
| 754 |
new_resp_data->body = ast_strdup(resp_data->body); |
| 755 |
new_resp_data->content_type = ast_strdup(resp_data->content_type); |
| 756 |
|
| 757 |
|
| 758 |
if (!new_resp_data->from || !new_resp_data->to || !new_resp_data->body || !new_resp_data->content_type) { |
| 759 |
pjsip_tx_data_dec_ref(tdata); |
| 760 |
ao2_ref(new_resp_data, -1); |
| 761 |
ast_log(LOG_ERROR, "Failed to allocate memory for redirect callback strings for endpoint '%s'.\n", |
| 762 |
ast_sorcery_object_get_id(endpoint)); |
| 763 |
return -1; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
new_resp_data->redirect_state = resp_data->redirect_state; |
| 768 |
resp_data->redirect_state = NULL; |
| 769 |
|
| 770 |
|
| 771 |
if (ast_sip_send_request(tdata, NULL, endpoint, new_resp_data, msg_response_callback)) { |
| 772 |
ao2_ref(new_resp_data, -1); |
| 773 |
ast_log(LOG_ERROR, "Failed to send redirect request to '%s' on endpoint '%s'.\n", |
| 774 |
new_resp_data->to, ast_sorcery_object_get_id(endpoint)); |
| 775 |
return -1; |
| 776 |
} |
| 777 |
|
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
static void handle_message_redirect(struct message_response_data *resp_data, pjsip_rx_data *rdata) |
| 789 |
{ |
| 790 |
char *uri = NULL; |
| 791 |
int hop_count; |
| 792 |
|
| 793 |
if (!resp_data->redirect_state) { |
| 794 |
ast_log(LOG_ERROR, "MESSAGE redirect: no redirect state available\n"); |
| 795 |
return; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
if (ast_sip_redirect_parse_3xx(rdata, resp_data->redirect_state)) { |
| 800 |
ast_debug(1, "MESSAGE redirect on endpoint '%s': not following redirect (parse failed or conditions not met)\n", |
| 801 |
ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state))); |
| 802 |
return; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
if (ast_sip_redirect_get_next_uri(resp_data->redirect_state, &uri)) { |
| 807 |
ast_log(LOG_WARNING, "MESSAGE redirect on endpoint '%s': no valid URIs to try\n", |
| 808 |
ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state))); |
| 809 |
return; |
| 810 |
} |
| 811 |
|
| 812 |
hop_count = ast_sip_redirect_get_hop_count(resp_data->redirect_state); |
| 813 |
ast_log(LOG_NOTICE, "MESSAGE redirect on endpoint '%s': Following redirect to '%s' (hop %d/%d)\n", |
| 814 |
ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state)), uri, hop_count, AST_SIP_MAX_REDIRECT_HOPS); |
| 815 |
|
| 816 |
|
| 817 |
send_message_to_uri(resp_data, uri); |
| 818 |
ast_free(uri); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
static void msg_response_callback(void *token, pjsip_event *e) |
| 829 |
{ |
| 830 |
struct message_response_data *resp_data = token; |
| 831 |
struct ast_sip_redirect_state *state = resp_data->redirect_state; |
| 832 |
struct ast_sip_endpoint *endpoint = ast_sip_redirect_get_endpoint(state); |
| 833 |
pjsip_rx_data *rdata; |
| 834 |
int status_code; |
| 835 |
char *next_uri = NULL; |
| 836 |
|
| 837 |
|
| 838 |
if (e->body.tsx_state.type == PJSIP_EVENT_TIMER) { |
| 839 |
ast_debug(1, "MESSAGE request on endpoint '%s' timed out\n", ast_sorcery_object_get_id(endpoint)); |
| 840 |
|
| 841 |
if (state && !ast_sip_redirect_get_next_uri(state, &next_uri)) { |
| 842 |
ast_log(LOG_NOTICE, "MESSAGE timed out on endpoint '%s', trying next Contact: '%s'\n", |
| 843 |
ast_sorcery_object_get_id(endpoint), next_uri); |
| 844 |
send_message_to_uri(resp_data, next_uri); |
| 845 |
ast_free(next_uri); |
| 846 |
} |
| 847 |
ao2_ref(resp_data, -1); |
| 848 |
return; |
| 849 |
} |
| 850 |
|
| 851 |
if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) { |
| 852 |
ast_debug(3, "MESSAGE response event type %d (not RX_MSG) on endpoint '%s'.\n", |
| 853 |
e->body.tsx_state.type, ast_sorcery_object_get_id(endpoint)); |
| 854 |
ao2_ref(resp_data, -1); |
| 855 |
return; |
| 856 |
} |
| 857 |
|
| 858 |
rdata = e->body.tsx_state.src.rdata; |
| 859 |
status_code = e->body.tsx_state.tsx->status_code; |
| 860 |
|
| 861 |
ast_debug(3, "Received MESSAGE response %d on endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint)); |
| 862 |
|
| 863 |
|
| 864 |
if (PJSIP_IS_STATUS_IN_CLASS(status_code, 300)) { |
| 865 |
handle_message_redirect(resp_data, rdata); |
| 866 |
} |
| 867 |
|
| 868 |
else if (status_code >= 400 && state && !ast_sip_redirect_get_next_uri(state, &next_uri)) { |
| 869 |
ast_log(LOG_NOTICE, "MESSAGE to redirect target failed (%d) on endpoint '%s', trying next Contact: '%s'\n", |
| 870 |
status_code, ast_sorcery_object_get_id(endpoint), next_uri); |
| 871 |
send_message_to_uri(resp_data, next_uri); |
| 872 |
ast_free(next_uri); |
| 873 |
} |
| 874 |
|
| 875 |
else if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) { |
| 876 |
ast_debug(1, "MESSAGE successfully delivered (%d) for endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint)); |
| 877 |
} |
| 878 |
|
| 879 |
ao2_ref(resp_data, -1); |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
static int msg_send(void *data) |
| 902 |
{ |
| 903 |
struct msg_data *mdata = data; |
| 904 |
|
| 905 |
struct message_response_data *resp_data; |
| 906 |
const char *from_uri; |
| 907 |
const char *to_uri; |
| 908 |
|
| 909 |
struct ast_sip_body body = { |
| 910 |
.type = "text", |
| 911 |
.subtype = "plain", |
| 912 |
.body_text = ast_msg_get_body(mdata->msg) |
| 913 |
}; |
| 914 |
|
| 915 |
pjsip_hdr *contact; |
| 916 |
pjsip_tx_data *tdata; |
| 917 |
RAII_VAR(char *, uri, NULL, ast_free); |
| 918 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 919 |
RAII_VAR(struct ast_str *, content_type_buf , ast_str_create(128), ast_free); |
| 920 |
|
| 921 |
ast_debug(3, "mdata From: %s msg From: %s mdata Destination: %s msg To: %s\n", |
| 922 |
mdata->from, ast_msg_get_from(mdata->msg), mdata->destination, ast_msg_get_to(mdata->msg)); |
| 923 |
|
| 924 |
endpoint = ast_sip_get_endpoint(mdata->destination, 1, &uri); |
| 925 |
if (!endpoint) { |
| 926 |
ast_log(LOG_ERROR, |
| 927 |
"PJSIP MESSAGE - Could not find endpoint '%s' and no default outbound endpoint configured\n", |
| 928 |
mdata->destination); |
| 929 |
|
| 930 |
ast_test_suite_event_notify("MSG_ENDPOINT_URI_FAIL", |
| 931 |
"MdataFrom: %s\r\n" |
| 932 |
"MsgFrom: %s\r\n" |
| 933 |
"MdataDestination: %s\r\n" |
| 934 |
"MsgTo: %s\r\n", |
| 935 |
mdata->from, |
| 936 |
ast_msg_get_from(mdata->msg), |
| 937 |
mdata->destination, |
| 938 |
ast_msg_get_to(mdata->msg)); |
| 939 |
|
| 940 |
return -1; |
| 941 |
} |
| 942 |
|
| 943 |
ast_debug(3, "Request URI: %s\n", uri); |
| 944 |
|
| 945 |
if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) { |
| 946 |
ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n"); |
| 947 |
return -1; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) { |
| 952 |
char *msg_to = ast_strdupa(ast_msg_get_to(mdata->msg)); |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
if (ast_begins_with(msg_to, "pjsip:")) { |
| 960 |
msg_to += 6; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
if (ast_begins_with(msg_to, "sip:") || ast_begins_with(msg_to, "sips:")) { |
| 969 |
ast_sip_update_to_uri(tdata, msg_to); |
| 970 |
} |
| 971 |
} else { |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
update_to_display_name(tdata, uri); |
| 978 |
} |
| 979 |
|
| 980 |
if (!ast_strlen_zero(mdata->from)) { |
| 981 |
ast_sip_update_from(tdata, mdata->from); |
| 982 |
} else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) { |
| 983 |
ast_sip_update_from(tdata, (char *)ast_msg_get_from(mdata->msg)); |
| 984 |
} |
| 985 |
|
| 986 |
#ifdef TEST_FRAMEWORK |
| 987 |
{ |
| 988 |
pjsip_name_addr *tdata_name_addr; |
| 989 |
pjsip_sip_uri *tdata_sip_uri; |
| 990 |
char touri[128]; |
| 991 |
char fromuri[128]; |
| 992 |
|
| 993 |
tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri; |
| 994 |
tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri); |
| 995 |
pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, touri, sizeof(touri)); |
| 996 |
tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri; |
| 997 |
tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri); |
| 998 |
pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, fromuri, sizeof(fromuri)); |
| 999 |
|
| 1000 |
ast_test_suite_event_notify("MSG_FROMTO_URI", |
| 1001 |
"MdataFrom: %s\r\n" |
| 1002 |
"MsgFrom: %s\r\n" |
| 1003 |
"MdataDestination: %s\r\n" |
| 1004 |
"MsgTo: %s\r\n" |
| 1005 |
"Endpoint: %s\r\n" |
| 1006 |
"RequestURI: %s\r\n" |
| 1007 |
"ToURI: %s\r\n" |
| 1008 |
"FromURI: %s\r\n", |
| 1009 |
mdata->from, |
| 1010 |
ast_msg_get_from(mdata->msg), |
| 1011 |
mdata->destination, |
| 1012 |
ast_msg_get_to(mdata->msg), |
| 1013 |
ast_sorcery_object_get_id(endpoint), |
| 1014 |
uri, |
| 1015 |
touri, |
| 1016 |
fromuri |
| 1017 |
); |
| 1018 |
} |
| 1019 |
#endif |
| 1020 |
|
| 1021 |
update_content_type(tdata, mdata->msg, &body); |
| 1022 |
|
| 1023 |
ast_sip_add_body(tdata, &body); |
| 1024 |
if (!tdata->msg->body) { |
| 1025 |
pjsip_tx_data_dec_ref(tdata); |
| 1026 |
ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n"); |
| 1027 |
return -1; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
vars_to_headers(mdata->msg, tdata); |
| 1035 |
|
| 1036 |
contact = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL); |
| 1037 |
if (contact) { |
| 1038 |
pj_list_erase(contact); |
| 1039 |
} |
| 1040 |
ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n", |
| 1041 |
uri, ast_sorcery_object_get_id(endpoint), mdata->from); |
| 1042 |
|
| 1043 |
|
| 1044 |
if (!ast_strlen_zero(mdata->from)) { |
| 1045 |
from_uri = mdata->from; |
| 1046 |
} else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) { |
| 1047 |
from_uri = ast_msg_get_from(mdata->msg); |
| 1048 |
} else { |
| 1049 |
from_uri = ""; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) { |
| 1054 |
to_uri = ast_msg_get_to(mdata->msg); |
| 1055 |
} else { |
| 1056 |
to_uri = ""; |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
ast_str_set(&content_type_buf, 0, "%s/%s", body.type, body.subtype); |
| 1061 |
|
| 1062 |
|
| 1063 |
resp_data = message_response_data_create(endpoint, from_uri, to_uri, |
| 1064 |
body.body_text, ast_str_buffer(content_type_buf), uri); |
| 1065 |
|
| 1066 |
if (!resp_data) { |
| 1067 |
pjsip_tx_data_dec_ref(tdata); |
| 1068 |
ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not allocate callback data for endpoint '%s'\n", |
| 1069 |
ast_sorcery_object_get_id(endpoint)); |
| 1070 |
return -1; |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
if (ast_sip_send_request(tdata, NULL, endpoint, resp_data, msg_response_callback)) { |
| 1075 |
ao2_ref(resp_data, -1); |
| 1076 |
ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request on endpoint '%s'\n", ast_sorcery_object_get_id(endpoint)); |
| 1077 |
return -1; |
| 1078 |
} |
| 1079 |
|
| 1080 |
return 0; |
| 1081 |
} |
| 1082 |
|
| 1083 |
static int sip_msg_send(const struct ast_msg *msg, const char *destination, const char *from) |
| 1084 |
{ |
| 1085 |
struct msg_data *mdata; |
| 1086 |
int res; |
| 1087 |
|
| 1088 |
if (ast_strlen_zero(destination)) { |
| 1089 |
ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n"); |
| 1090 |
return -1; |
| 1091 |
} |
| 1092 |
|
| 1093 |
mdata = msg_data_create(msg, destination, from); |
| 1094 |
if (!mdata) { |
| 1095 |
return -1; |
| 1096 |
} |
| 1097 |
|
| 1098 |
res = ast_sip_push_task_wait_serializer(message_serializer, msg_send, mdata); |
| 1099 |
ao2_ref(mdata, -1); |
| 1100 |
|
| 1101 |
return res; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static const struct ast_msg_tech msg_tech = { |
| 1105 |
.name = "pjsip", |
| 1106 |
.msg_send = sip_msg_send, |
| 1107 |
}; |
| 1108 |
|
| 1109 |
static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code, |
| 1110 |
pjsip_dialog *dlg, pjsip_transaction *tsx) |
| 1111 |
{ |
| 1112 |
pjsip_tx_data *tdata; |
| 1113 |
pj_status_t status; |
| 1114 |
|
| 1115 |
status = ast_sip_create_response(rdata, code, NULL, &tdata); |
| 1116 |
if (status != PJ_SUCCESS) { |
| 1117 |
ast_log(LOG_ERROR, "Unable to create response (%d)\n", status); |
| 1118 |
return status; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (dlg && tsx) { |
| 1122 |
status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 1123 |
} else { |
| 1124 |
struct ast_sip_endpoint *endpoint; |
| 1125 |
|
| 1126 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 1127 |
status = ast_sip_send_stateful_response(rdata, tdata, endpoint); |
| 1128 |
ao2_cleanup(endpoint); |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (status != PJ_SUCCESS) { |
| 1132 |
ast_log(LOG_ERROR, "Unable to send response (%d)\n", status); |
| 1133 |
} |
| 1134 |
|
| 1135 |
return status; |
| 1136 |
} |
| 1137 |
|
| 1138 |
static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata) |
| 1139 |
{ |
| 1140 |
enum pjsip_status_code code; |
| 1141 |
struct ast_msg *msg; |
| 1142 |
|
| 1143 |
|
| 1144 |
if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) { |
| 1145 |
return PJ_FALSE; |
| 1146 |
} |
| 1147 |
|
| 1148 |
code = check_content_type(rdata); |
| 1149 |
if (code != PJSIP_SC_OK) { |
| 1150 |
send_response(rdata, code, NULL, NULL); |
| 1151 |
return PJ_TRUE; |
| 1152 |
} |
| 1153 |
|
| 1154 |
msg = ast_msg_alloc(); |
| 1155 |
if (!msg) { |
| 1156 |
send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL); |
| 1157 |
return PJ_TRUE; |
| 1158 |
} |
| 1159 |
|
| 1160 |
code = rx_data_to_ast_msg(rdata, msg); |
| 1161 |
if (code != PJSIP_SC_OK) { |
| 1162 |
send_response(rdata, code, NULL, NULL); |
| 1163 |
ast_msg_destroy(msg); |
| 1164 |
return PJ_TRUE; |
| 1165 |
} |
| 1166 |
|
| 1167 |
if (!ast_msg_has_destination(msg)) { |
| 1168 |
ast_debug(1, "MESSAGE request received, but no handler wanted it\n"); |
| 1169 |
send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL); |
| 1170 |
ast_msg_destroy(msg); |
| 1171 |
return PJ_TRUE; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) { |
| 1182 |
ast_msg_queue(msg); |
| 1183 |
} |
| 1184 |
|
| 1185 |
return PJ_TRUE; |
| 1186 |
} |
| 1187 |
|
| 1188 |
static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata) |
| 1189 |
{ |
| 1190 |
enum pjsip_status_code code; |
| 1191 |
int rc; |
| 1192 |
pjsip_dialog *dlg = session->inv_session->dlg; |
| 1193 |
pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata); |
| 1194 |
struct ast_msg_data *msg; |
| 1195 |
struct ast_party_caller *caller; |
| 1196 |
pjsip_name_addr *name_addr; |
| 1197 |
size_t from_len; |
| 1198 |
size_t to_len; |
| 1199 |
struct ast_msg_data_attribute attrs[4]; |
| 1200 |
int pos = 0; |
| 1201 |
int body_pos; |
| 1202 |
|
| 1203 |
if (!session->channel) { |
| 1204 |
send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx); |
| 1205 |
return 0; |
| 1206 |
} |
| 1207 |
|
| 1208 |
code = check_content_type_in_dialog(rdata); |
| 1209 |
if (code != PJSIP_SC_OK) { |
| 1210 |
send_response(rdata, code, dlg, tsx); |
| 1211 |
return 0; |
| 1212 |
} |
| 1213 |
|
| 1214 |
caller = ast_channel_caller(session->channel); |
| 1215 |
|
| 1216 |
name_addr = (pjsip_name_addr *) rdata->msg_info.from->uri; |
| 1217 |
from_len = pj_strlen(&name_addr->display); |
| 1218 |
if (from_len) { |
| 1219 |
attrs[pos].type = AST_MSG_DATA_ATTR_FROM; |
| 1220 |
from_len++; |
| 1221 |
attrs[pos].value = ast_alloca(from_len); |
| 1222 |
ast_copy_pj_str(attrs[pos].value, &name_addr->display, from_len); |
| 1223 |
pos++; |
| 1224 |
} else if (caller->id.name.valid && !ast_strlen_zero(caller->id.name.str)) { |
| 1225 |
attrs[pos].type = AST_MSG_DATA_ATTR_FROM; |
| 1226 |
attrs[pos].value = caller->id.name.str; |
| 1227 |
pos++; |
| 1228 |
} |
| 1229 |
|
| 1230 |
name_addr = (pjsip_name_addr *) rdata->msg_info.to->uri; |
| 1231 |
to_len = pj_strlen(&name_addr->display); |
| 1232 |
if (to_len) { |
| 1233 |
attrs[pos].type = AST_MSG_DATA_ATTR_TO; |
| 1234 |
to_len++; |
| 1235 |
attrs[pos].value = ast_alloca(to_len); |
| 1236 |
ast_copy_pj_str(attrs[pos].value, &name_addr->display, to_len); |
| 1237 |
pos++; |
| 1238 |
} |
| 1239 |
|
| 1240 |
attrs[pos].type = AST_MSG_DATA_ATTR_CONTENT_TYPE; |
| 1241 |
attrs[pos].value = ast_alloca(rdata->msg_info.msg->body->content_type.type.slen |
| 1242 |
+ rdata->msg_info.msg->body->content_type.subtype.slen + 2); |
| 1243 |
sprintf(attrs[pos].value, "%.*s/%.*s", |
| 1244 |
(int)rdata->msg_info.msg->body->content_type.type.slen, |
| 1245 |
rdata->msg_info.msg->body->content_type.type.ptr, |
| 1246 |
(int)rdata->msg_info.msg->body->content_type.subtype.slen, |
| 1247 |
rdata->msg_info.msg->body->content_type.subtype.ptr); |
| 1248 |
pos++; |
| 1249 |
|
| 1250 |
body_pos = pos; |
| 1251 |
attrs[pos].type = AST_MSG_DATA_ATTR_BODY; |
| 1252 |
attrs[pos].value = ast_malloc(rdata->msg_info.msg->body->len + 1); |
| 1253 |
if (!attrs[pos].value) { |
| 1254 |
send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx); |
| 1255 |
return 0; |
| 1256 |
} |
| 1257 |
ast_copy_string(attrs[pos].value, rdata->msg_info.msg->body->data, rdata->msg_info.msg->body->len + 1); |
| 1258 |
pos++; |
| 1259 |
|
| 1260 |
msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG, attrs, pos); |
| 1261 |
if (!msg) { |
| 1262 |
ast_free(attrs[body_pos].value); |
| 1263 |
send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx); |
| 1264 |
return 0; |
| 1265 |
} |
| 1266 |
|
| 1267 |
ast_debug(1, "Received in-dialog MESSAGE from '%s:%s': %s %s\n", |
| 1268 |
ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM), |
| 1269 |
ast_channel_name(session->channel), |
| 1270 |
ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO), |
| 1271 |
ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY)); |
| 1272 |
|
| 1273 |
rc = ast_msg_data_queue_frame(session->channel, msg); |
| 1274 |
ast_free(attrs[body_pos].value); |
| 1275 |
ast_free(msg); |
| 1276 |
if (rc != 0) { |
| 1277 |
send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx); |
| 1278 |
} else { |
| 1279 |
send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx); |
| 1280 |
} |
| 1281 |
|
| 1282 |
return 0; |
| 1283 |
} |
| 1284 |
|
| 1285 |
static struct ast_sip_session_supplement messaging_supplement = { |
| 1286 |
.method = "MESSAGE", |
| 1287 |
.incoming_request = incoming_in_dialog_request |
| 1288 |
}; |
| 1289 |
|
| 1290 |
static pjsip_module messaging_module = { |
| 1291 |
.name = {"Messaging Module", 16}, |
| 1292 |
.id = -1, |
| 1293 |
.priority = PJSIP_MOD_PRIORITY_APPLICATION, |
| 1294 |
.on_rx_request = module_on_rx_request, |
| 1295 |
}; |
| 1296 |
|
| 1297 |
static int load_module(void) |
| 1298 |
{ |
| 1299 |
if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) { |
| 1300 |
return AST_MODULE_LOAD_DECLINE; |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), |
| 1304 |
NULL, PJSIP_H_ALLOW, NULL, 1, |
| 1305 |
&pjsip_message_method.name) != PJ_SUCCESS) { |
| 1306 |
|
| 1307 |
ast_sip_unregister_service(&messaging_module); |
| 1308 |
return AST_MODULE_LOAD_DECLINE; |
| 1309 |
} |
| 1310 |
|
| 1311 |
if (ast_msg_tech_register(&msg_tech)) { |
| 1312 |
ast_sip_unregister_service(&messaging_module); |
| 1313 |
return AST_MODULE_LOAD_DECLINE; |
| 1314 |
} |
| 1315 |
|
| 1316 |
message_serializer = ast_sip_create_serializer("pjsip/messaging"); |
| 1317 |
if (!message_serializer) { |
| 1318 |
ast_sip_unregister_service(&messaging_module); |
| 1319 |
ast_msg_tech_unregister(&msg_tech); |
| 1320 |
return AST_MODULE_LOAD_DECLINE; |
| 1321 |
} |
| 1322 |
|
| 1323 |
ast_sip_session_register_supplement(&messaging_supplement); |
| 1324 |
return AST_MODULE_LOAD_SUCCESS; |
| 1325 |
} |
| 1326 |
|
| 1327 |
static int unload_module(void) |
| 1328 |
{ |
| 1329 |
ast_sip_session_unregister_supplement(&messaging_supplement); |
| 1330 |
ast_msg_tech_unregister(&msg_tech); |
| 1331 |
ast_sip_unregister_service(&messaging_module); |
| 1332 |
ast_taskprocessor_unreference(message_serializer); |
| 1333 |
return 0; |
| 1334 |
} |
| 1335 |
|
| 1336 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support", |
| 1337 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1338 |
.load = load_module, |
| 1339 |
.unload = unload_module, |
| 1340 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 1341 |
.requires = "res_pjsip,res_pjsip_session", |
| 1342 |
); |