| 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_simple.h> |
| 30 |
|
| 31 |
#include "asterisk/res_pjproject.h" |
| 32 |
#include "asterisk/res_pjsip.h" |
| 33 |
#include "asterisk/res_pjsip_outbound_publish.h" |
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/taskprocessor.h" |
| 36 |
#include "asterisk/taskpool.h" |
| 37 |
#include "asterisk/serializer_shutdown_group.h" |
| 38 |
#include "asterisk/datastore.h" |
| 39 |
#include "res_pjsip/include/res_pjsip_private.h" |
| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static int pjsip_max_url_size = PJSIP_MAX_URL_SIZE; |
| 166 |
|
| 167 |
|
| 168 |
struct sip_outbound_publish_message { |
| 169 |
|
| 170 |
struct ast_sip_body body; |
| 171 |
|
| 172 |
AST_LIST_ENTRY(sip_outbound_publish_message) entry; |
| 173 |
|
| 174 |
char body_contents[0]; |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
struct ast_sip_outbound_publish { |
| 212 |
|
| 213 |
SORCERY_OBJECT(details); |
| 214 |
|
| 215 |
AST_DECLARE_STRING_FIELDS( |
| 216 |
|
| 217 |
AST_STRING_FIELD(server_uri); |
| 218 |
|
| 219 |
AST_STRING_FIELD(from_uri); |
| 220 |
|
| 221 |
AST_STRING_FIELD(to_uri); |
| 222 |
|
| 223 |
AST_STRING_FIELD(transport); |
| 224 |
|
| 225 |
AST_STRING_FIELD(outbound_proxy); |
| 226 |
|
| 227 |
AST_STRING_FIELD(event); |
| 228 |
); |
| 229 |
|
| 230 |
unsigned int expiration; |
| 231 |
|
| 232 |
unsigned int max_auth_attempts; |
| 233 |
|
| 234 |
struct ast_sip_auth_vector outbound_auths; |
| 235 |
|
| 236 |
unsigned int multi_user; |
| 237 |
}; |
| 238 |
|
| 239 |
struct sip_outbound_publisher { |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
struct ast_sip_outbound_publish_client *owner; |
| 246 |
|
| 247 |
pjsip_publishc *client; |
| 248 |
|
| 249 |
char *from_uri; |
| 250 |
|
| 251 |
char *to_uri; |
| 252 |
|
| 253 |
pj_timer_entry timer; |
| 254 |
|
| 255 |
unsigned int auth_attempts; |
| 256 |
|
| 257 |
AST_LIST_HEAD_NOLOCK(, sip_outbound_publish_message) queue; |
| 258 |
|
| 259 |
struct sip_outbound_publish_message *sending; |
| 260 |
|
| 261 |
unsigned int destroy; |
| 262 |
|
| 263 |
struct ast_taskprocessor *serializer; |
| 264 |
|
| 265 |
char user[0]; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
struct ast_sip_outbound_publish_client { |
| 270 |
|
| 271 |
struct ast_sip_outbound_publish *publish; |
| 272 |
|
| 273 |
struct ao2_container *datastores; |
| 274 |
|
| 275 |
struct ao2_container *publishers; |
| 276 |
|
| 277 |
unsigned int started; |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
struct ast_sip_outbound_publish_state { |
| 282 |
|
| 283 |
struct ast_sip_outbound_publish_client *client; |
| 284 |
|
| 285 |
char id[0]; |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
AST_RWLOCK_DEFINE_STATIC(load_lock); |
| 297 |
|
| 298 |
#define DEFAULT_PUBLISHER_BUCKETS 119 |
| 299 |
AO2_STRING_FIELD_HASH_FN(sip_outbound_publisher, user); |
| 300 |
AO2_STRING_FIELD_CMP_FN(sip_outbound_publisher, user); |
| 301 |
|
| 302 |
|
| 303 |
#define MAX_UNLOAD_TIMEOUT_TIME 35 |
| 304 |
|
| 305 |
|
| 306 |
static struct ast_serializer_shutdown_group *shutdown_group; |
| 307 |
|
| 308 |
|
| 309 |
#define DEFAULT_STATE_BUCKETS 31 |
| 310 |
static AO2_GLOBAL_OBJ_STATIC(current_states); |
| 311 |
|
| 312 |
static struct ao2_container *new_states; |
| 313 |
|
| 314 |
|
| 315 |
static int outbound_publish_state_hash(const void *obj, const int flags) |
| 316 |
{ |
| 317 |
const struct ast_sip_outbound_publish_state *object; |
| 318 |
const char *key; |
| 319 |
|
| 320 |
switch (flags & OBJ_SEARCH_MASK) { |
| 321 |
case OBJ_SEARCH_KEY: |
| 322 |
key = obj; |
| 323 |
break; |
| 324 |
case OBJ_SEARCH_OBJECT: |
| 325 |
object = obj; |
| 326 |
key = object->id; |
| 327 |
break; |
| 328 |
default: |
| 329 |
ast_assert(0); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
return ast_str_hash(key); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
static int outbound_publish_state_cmp(void *obj, void *arg, int flags) |
| 337 |
{ |
| 338 |
const struct ast_sip_outbound_publish_state *object_left = obj; |
| 339 |
const struct ast_sip_outbound_publish_state *object_right = arg; |
| 340 |
const char *right_key = arg; |
| 341 |
int cmp; |
| 342 |
|
| 343 |
switch (flags & OBJ_SEARCH_MASK) { |
| 344 |
case OBJ_SEARCH_OBJECT: |
| 345 |
right_key = object_right->id; |
| 346 |
|
| 347 |
case OBJ_SEARCH_KEY: |
| 348 |
cmp = strcmp(object_left->id, right_key); |
| 349 |
break; |
| 350 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 351 |
|
| 352 |
ast_assert(0); |
| 353 |
return 0; |
| 354 |
default: |
| 355 |
cmp = 0; |
| 356 |
break; |
| 357 |
} |
| 358 |
if (cmp) { |
| 359 |
return 0; |
| 360 |
} |
| 361 |
return CMP_MATCH; |
| 362 |
} |
| 363 |
|
| 364 |
static struct ao2_container *get_publishes_and_update_state(void) |
| 365 |
{ |
| 366 |
struct ao2_container *container; |
| 367 |
SCOPED_WRLOCK(lock, &load_lock); |
| 368 |
|
| 369 |
container = ast_sorcery_retrieve_by_fields( |
| 370 |
ast_sip_get_sorcery(), "outbound-publish", |
| 371 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 372 |
|
| 373 |
if (!new_states) { |
| 374 |
return container; |
| 375 |
} |
| 376 |
|
| 377 |
ao2_global_obj_replace_unref(current_states, new_states); |
| 378 |
ao2_cleanup(new_states); |
| 379 |
new_states = NULL; |
| 380 |
|
| 381 |
return container; |
| 382 |
} |
| 383 |
|
| 384 |
AST_RWLIST_HEAD_STATIC(publisher_handlers, ast_sip_event_publisher_handler); |
| 385 |
|
| 386 |
static void sub_add_handler(struct ast_sip_event_publisher_handler *handler) |
| 387 |
{ |
| 388 |
AST_RWLIST_INSERT_TAIL(&publisher_handlers, handler, next); |
| 389 |
} |
| 390 |
|
| 391 |
static struct ast_sip_event_publisher_handler *find_publisher_handler_for_event_name(const char *event_name) |
| 392 |
{ |
| 393 |
struct ast_sip_event_publisher_handler *iter; |
| 394 |
|
| 395 |
AST_RWLIST_TRAVERSE(&publisher_handlers, iter, next) { |
| 396 |
if (!strcmp(iter->event_name, event_name)) { |
| 397 |
break; |
| 398 |
} |
| 399 |
} |
| 400 |
return iter; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
static void cancel_publish_refresh(struct sip_outbound_publisher *publisher) |
| 405 |
{ |
| 406 |
if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), |
| 407 |
&publisher->timer, 0)) { |
| 408 |
|
| 409 |
ao2_ref(publisher, -1); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
static void schedule_publish_refresh(struct sip_outbound_publisher *publisher, int expiration) |
| 415 |
{ |
| 416 |
struct ast_sip_outbound_publish *publish = ao2_bump(publisher->owner->publish); |
| 417 |
pj_time_val delay = { .sec = 0, }; |
| 418 |
|
| 419 |
cancel_publish_refresh(publisher); |
| 420 |
|
| 421 |
if (expiration > 0) { |
| 422 |
delay.sec = expiration - PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH; |
| 423 |
} |
| 424 |
if (publish->expiration && ((delay.sec > publish->expiration) || !delay.sec)) { |
| 425 |
delay.sec = publish->expiration; |
| 426 |
} |
| 427 |
if (delay.sec < PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH) { |
| 428 |
delay.sec = PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH; |
| 429 |
} |
| 430 |
|
| 431 |
ao2_ref(publisher, +1); |
| 432 |
if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &publisher->timer, &delay) != PJ_SUCCESS) { |
| 433 |
ast_log(LOG_WARNING, "Failed to pass timed publish refresh to scheduler\n"); |
| 434 |
ao2_ref(publisher, -1); |
| 435 |
} |
| 436 |
ao2_ref(publish, -1); |
| 437 |
} |
| 438 |
|
| 439 |
static int publisher_client_send(void *obj, void *arg, void *data, int flags); |
| 440 |
|
| 441 |
|
| 442 |
static void sip_outbound_publish_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry) |
| 443 |
{ |
| 444 |
struct sip_outbound_publisher *publisher = entry->user_data; |
| 445 |
|
| 446 |
ao2_lock(publisher); |
| 447 |
if (AST_LIST_EMPTY(&publisher->queue)) { |
| 448 |
int res; |
| 449 |
|
| 450 |
publisher_client_send(publisher, NULL, &res, 0); |
| 451 |
} |
| 452 |
ao2_unlock(publisher); |
| 453 |
|
| 454 |
ao2_ref(publisher, -1); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
static int cancel_refresh_timer_task(void *data) |
| 459 |
{ |
| 460 |
struct sip_outbound_publisher *publisher = data; |
| 461 |
|
| 462 |
cancel_publish_refresh(publisher); |
| 463 |
ao2_ref(publisher, -1); |
| 464 |
|
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
static void set_transport(struct sip_outbound_publisher *publisher, pjsip_tx_data *tdata) |
| 469 |
{ |
| 470 |
if (!ast_strlen_zero(publisher->owner->publish->transport)) { |
| 471 |
pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; |
| 472 |
|
| 473 |
ast_sip_set_tpselector_from_transport_name( |
| 474 |
publisher->owner->publish->transport, &selector); |
| 475 |
pjsip_tx_data_set_transport(tdata, &selector); |
| 476 |
ast_sip_tpselector_unref(&selector); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
static int send_unpublish_task(void *data) |
| 482 |
{ |
| 483 |
struct sip_outbound_publisher *publisher = data; |
| 484 |
pjsip_tx_data *tdata; |
| 485 |
|
| 486 |
if (pjsip_publishc_unpublish(publisher->client, &tdata) == PJ_SUCCESS) { |
| 487 |
set_transport(publisher, tdata); |
| 488 |
pjsip_publishc_send(publisher->client, tdata); |
| 489 |
} |
| 490 |
|
| 491 |
ao2_ref(publisher, -1); |
| 492 |
|
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static void stop_publishing(struct ast_sip_outbound_publish_client *client, |
| 497 |
struct ast_sip_event_publisher_handler *handler) |
| 498 |
{ |
| 499 |
if (!handler) { |
| 500 |
handler = find_publisher_handler_for_event_name(client->publish->event); |
| 501 |
} |
| 502 |
|
| 503 |
if (handler) { |
| 504 |
handler->stop_publishing(client); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
static int cancel_and_unpublish(void *obj, void *arg, int flags); |
| 509 |
|
| 510 |
|
| 511 |
static void sip_outbound_publish_synchronize(struct ast_sip_event_publisher_handler *removed) |
| 512 |
{ |
| 513 |
RAII_VAR(struct ao2_container *, publishes, get_publishes_and_update_state(), ao2_cleanup); |
| 514 |
struct ao2_container *states; |
| 515 |
struct ao2_iterator i; |
| 516 |
struct ast_sip_outbound_publish_state *state; |
| 517 |
|
| 518 |
if (!publishes) { |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
states = ao2_global_obj_ref(current_states); |
| 523 |
if (!states) { |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
i = ao2_iterator_init(states, 0); |
| 528 |
while ((state = ao2_iterator_next(&i))) { |
| 529 |
struct ast_sip_outbound_publish *publish = ao2_bump(state->client->publish); |
| 530 |
struct ast_sip_event_publisher_handler *handler = find_publisher_handler_for_event_name(publish->event); |
| 531 |
|
| 532 |
if (!state->client->started) { |
| 533 |
|
| 534 |
if (!handler) { |
| 535 |
ast_debug(2, "Could not find handler for event '%s' for outbound publish client '%s'\n", |
| 536 |
publish->event, ast_sorcery_object_get_id(publish)); |
| 537 |
} else if (handler->start_publishing(publish, state->client)) { |
| 538 |
ast_log(LOG_ERROR, "Failed to start outbound publish with event '%s' for client '%s'\n", |
| 539 |
publish->event, ast_sorcery_object_get_id(publish)); |
| 540 |
} else { |
| 541 |
state->client->started = 1; |
| 542 |
} |
| 543 |
} else if (state->client->started && !handler && removed && !strcmp(publish->event, removed->event_name)) { |
| 544 |
stop_publishing(state->client, removed); |
| 545 |
ao2_callback(state->client->publishers, OBJ_NODATA, cancel_and_unpublish, NULL); |
| 546 |
state->client->started = 0; |
| 547 |
} |
| 548 |
ao2_ref(publish, -1); |
| 549 |
ao2_ref(state, -1); |
| 550 |
} |
| 551 |
ao2_iterator_destroy(&i); |
| 552 |
ao2_ref(states, -1); |
| 553 |
} |
| 554 |
|
| 555 |
static struct ast_sip_outbound_publish_state *sip_publish_state_get(const char *id) |
| 556 |
{ |
| 557 |
struct ao2_container *states = ao2_global_obj_ref(current_states); |
| 558 |
struct ast_sip_outbound_publish_state *res; |
| 559 |
|
| 560 |
if (!states) { |
| 561 |
return NULL; |
| 562 |
} |
| 563 |
|
| 564 |
res = ao2_find(states, id, OBJ_SEARCH_KEY); |
| 565 |
ao2_ref(states, -1); |
| 566 |
return res; |
| 567 |
} |
| 568 |
|
| 569 |
struct ast_sip_outbound_publish_client *ast_sip_publish_client_get(const char *name) |
| 570 |
{ |
| 571 |
struct ast_sip_outbound_publish_state *state = sip_publish_state_get(name); |
| 572 |
|
| 573 |
if (!state) { |
| 574 |
return NULL; |
| 575 |
} |
| 576 |
|
| 577 |
ao2_ref(state->client, +1); |
| 578 |
ao2_ref(state, -1); |
| 579 |
return state->client; |
| 580 |
} |
| 581 |
|
| 582 |
const char *ast_sip_publish_client_get_from_uri(struct ast_sip_outbound_publish_client *client) |
| 583 |
{ |
| 584 |
struct ast_sip_outbound_publish *publish = client->publish; |
| 585 |
|
| 586 |
return S_OR(publish->from_uri, S_OR(publish->server_uri, "")); |
| 587 |
} |
| 588 |
|
| 589 |
static struct sip_outbound_publisher *sip_outbound_publish_client_add_publisher( |
| 590 |
struct ast_sip_outbound_publish_client *client, const char *user); |
| 591 |
|
| 592 |
static struct sip_outbound_publisher *sip_outbound_publish_client_get_publisher( |
| 593 |
struct ast_sip_outbound_publish_client *client, const char *user) |
| 594 |
{ |
| 595 |
struct sip_outbound_publisher *publisher; |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
ast_rwlock_wrlock(&load_lock); |
| 604 |
publisher = ao2_find(client->publishers, user, OBJ_SEARCH_KEY); |
| 605 |
if (!publisher) { |
| 606 |
if (!(publisher = sip_outbound_publish_client_add_publisher(client, user))) { |
| 607 |
ast_rwlock_unlock(&load_lock); |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
} |
| 611 |
ast_rwlock_unlock(&load_lock); |
| 612 |
|
| 613 |
return publisher; |
| 614 |
} |
| 615 |
|
| 616 |
const char *ast_sip_publish_client_get_user_from_uri(struct ast_sip_outbound_publish_client *client, const char *user, |
| 617 |
char *uri, size_t size) |
| 618 |
{ |
| 619 |
struct sip_outbound_publisher *publisher; |
| 620 |
|
| 621 |
publisher = sip_outbound_publish_client_get_publisher(client, user); |
| 622 |
if (!publisher) { |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
|
| 626 |
ast_copy_string(uri, publisher->from_uri, size); |
| 627 |
ao2_ref(publisher, -1); |
| 628 |
|
| 629 |
return uri; |
| 630 |
} |
| 631 |
|
| 632 |
const char *ast_sip_publish_client_get_to_uri(struct ast_sip_outbound_publish_client *client) |
| 633 |
{ |
| 634 |
struct ast_sip_outbound_publish *publish = client->publish; |
| 635 |
|
| 636 |
return S_OR(publish->to_uri, S_OR(publish->server_uri, "")); |
| 637 |
} |
| 638 |
|
| 639 |
const char *ast_sip_publish_client_get_user_to_uri(struct ast_sip_outbound_publish_client *client, const char *user, |
| 640 |
char *uri, size_t size) |
| 641 |
{ |
| 642 |
struct sip_outbound_publisher *publisher; |
| 643 |
|
| 644 |
publisher = sip_outbound_publish_client_get_publisher(client, user); |
| 645 |
if (!publisher) { |
| 646 |
return NULL; |
| 647 |
} |
| 648 |
|
| 649 |
ast_copy_string(uri, publisher->to_uri, size); |
| 650 |
ao2_ref(publisher, -1); |
| 651 |
|
| 652 |
return uri; |
| 653 |
} |
| 654 |
|
| 655 |
int ast_sip_register_event_publisher_handler(struct ast_sip_event_publisher_handler *handler) |
| 656 |
{ |
| 657 |
struct ast_sip_event_publisher_handler *existing; |
| 658 |
SCOPED_LOCK(lock, &publisher_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK); |
| 659 |
|
| 660 |
if (!handler->start_publishing || !handler->stop_publishing) { |
| 661 |
ast_log(LOG_ERROR, "Handler does not implement required callbacks. Cannot register\n"); |
| 662 |
return -1; |
| 663 |
} else if (ast_strlen_zero(handler->event_name)) { |
| 664 |
ast_log(LOG_ERROR, "No event package specified for event publisher handler. Cannot register\n"); |
| 665 |
return -1; |
| 666 |
} |
| 667 |
|
| 668 |
existing = find_publisher_handler_for_event_name(handler->event_name); |
| 669 |
if (existing) { |
| 670 |
ast_log(LOG_ERROR, "Unable to register event publisher handler for event %s. " |
| 671 |
"A handler is already registered\n", handler->event_name); |
| 672 |
return -1; |
| 673 |
} |
| 674 |
|
| 675 |
sub_add_handler(handler); |
| 676 |
|
| 677 |
sip_outbound_publish_synchronize(NULL); |
| 678 |
|
| 679 |
return 0; |
| 680 |
} |
| 681 |
|
| 682 |
void ast_sip_unregister_event_publisher_handler(struct ast_sip_event_publisher_handler *handler) |
| 683 |
{ |
| 684 |
struct ast_sip_event_publisher_handler *iter; |
| 685 |
SCOPED_LOCK(lock, &publisher_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK); |
| 686 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&publisher_handlers, iter, next) { |
| 687 |
if (handler == iter) { |
| 688 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 689 |
break; |
| 690 |
} |
| 691 |
} |
| 692 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 693 |
|
| 694 |
sip_outbound_publish_synchronize(handler); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
static void sip_outbound_publish_destroy(void *obj) |
| 699 |
{ |
| 700 |
struct ast_sip_outbound_publish *publish = obj; |
| 701 |
|
| 702 |
ast_sip_auth_vector_destroy(&publish->outbound_auths); |
| 703 |
|
| 704 |
ast_string_field_free_memory(publish); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
static void *sip_outbound_publish_alloc(const char *name) |
| 709 |
{ |
| 710 |
struct ast_sip_outbound_publish *publish = ast_sorcery_generic_alloc(sizeof(*publish), |
| 711 |
sip_outbound_publish_destroy); |
| 712 |
|
| 713 |
if (!publish || ast_string_field_init(publish, 256)) { |
| 714 |
ao2_cleanup(publish); |
| 715 |
return NULL; |
| 716 |
} |
| 717 |
|
| 718 |
return publish; |
| 719 |
} |
| 720 |
|
| 721 |
static void sip_outbound_publish_datastore_destroy(void *obj) |
| 722 |
{ |
| 723 |
struct ast_datastore *datastore = obj; |
| 724 |
|
| 725 |
|
| 726 |
if (datastore->info->destroy != NULL && datastore->data != NULL) { |
| 727 |
datastore->info->destroy(datastore->data); |
| 728 |
datastore->data = NULL; |
| 729 |
} |
| 730 |
|
| 731 |
ast_free((void *) datastore->uid); |
| 732 |
datastore->uid = NULL; |
| 733 |
} |
| 734 |
|
| 735 |
struct ast_datastore *ast_sip_publish_client_alloc_datastore(const struct ast_datastore_info *info, const char *uid) |
| 736 |
{ |
| 737 |
RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup); |
| 738 |
const char *uid_ptr = uid; |
| 739 |
char uuid_buf[AST_UUID_STR_LEN]; |
| 740 |
|
| 741 |
if (!info) { |
| 742 |
return NULL; |
| 743 |
} |
| 744 |
|
| 745 |
datastore = ao2_alloc(sizeof(*datastore), sip_outbound_publish_datastore_destroy); |
| 746 |
if (!datastore) { |
| 747 |
return NULL; |
| 748 |
} |
| 749 |
|
| 750 |
datastore->info = info; |
| 751 |
if (ast_strlen_zero(uid)) { |
| 752 |
|
| 753 |
uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf)); |
| 754 |
} |
| 755 |
|
| 756 |
datastore->uid = ast_strdup(uid_ptr); |
| 757 |
if (!datastore->uid) { |
| 758 |
return NULL; |
| 759 |
} |
| 760 |
|
| 761 |
ao2_ref(datastore, +1); |
| 762 |
return datastore; |
| 763 |
} |
| 764 |
|
| 765 |
int ast_sip_publish_client_add_datastore(struct ast_sip_outbound_publish_client *client, |
| 766 |
struct ast_datastore *datastore) |
| 767 |
{ |
| 768 |
ast_assert(datastore != NULL); |
| 769 |
ast_assert(datastore->info != NULL); |
| 770 |
ast_assert(!ast_strlen_zero(datastore->uid)); |
| 771 |
|
| 772 |
if (!ao2_link(client->datastores, datastore)) { |
| 773 |
return -1; |
| 774 |
} |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
struct ast_datastore *ast_sip_publish_client_get_datastore(struct ast_sip_outbound_publish_client *client, |
| 779 |
const char *name) |
| 780 |
{ |
| 781 |
return ao2_find(client->datastores, name, OBJ_SEARCH_KEY); |
| 782 |
} |
| 783 |
|
| 784 |
void ast_sip_publish_client_remove_datastore(struct ast_sip_outbound_publish_client *client, |
| 785 |
const char *name) |
| 786 |
{ |
| 787 |
ao2_find(client->datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 788 |
} |
| 789 |
|
| 790 |
static int sip_publisher_service_queue(void *data) |
| 791 |
{ |
| 792 |
RAII_VAR(struct sip_outbound_publisher *, publisher, data, ao2_cleanup); |
| 793 |
SCOPED_AO2LOCK(lock, publisher); |
| 794 |
struct sip_outbound_publish_message *message; |
| 795 |
pjsip_tx_data *tdata; |
| 796 |
pj_status_t status; |
| 797 |
|
| 798 |
if (publisher->destroy || publisher->sending || !(message = AST_LIST_FIRST(&publisher->queue))) { |
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
if (pjsip_publishc_publish(publisher->client, PJ_FALSE, &tdata) != PJ_SUCCESS) { |
| 803 |
goto fatal; |
| 804 |
} |
| 805 |
|
| 806 |
if (!ast_strlen_zero(message->body.type) && !ast_strlen_zero(message->body.subtype) && |
| 807 |
ast_sip_add_body(tdata, &message->body)) { |
| 808 |
pjsip_tx_data_dec_ref(tdata); |
| 809 |
goto fatal; |
| 810 |
} |
| 811 |
|
| 812 |
set_transport(publisher, tdata); |
| 813 |
|
| 814 |
status = pjsip_publishc_send(publisher->client, tdata); |
| 815 |
if (status == PJ_EBUSY) { |
| 816 |
|
| 817 |
goto service; |
| 818 |
} else if (status != PJ_SUCCESS) { |
| 819 |
goto fatal; |
| 820 |
} |
| 821 |
|
| 822 |
publisher->sending = message; |
| 823 |
|
| 824 |
return 0; |
| 825 |
|
| 826 |
fatal: |
| 827 |
AST_LIST_REMOVE_HEAD(&publisher->queue, entry); |
| 828 |
ast_free(message); |
| 829 |
|
| 830 |
service: |
| 831 |
if (ast_sip_push_task(publisher->serializer, sip_publisher_service_queue, ao2_bump(publisher))) { |
| 832 |
ao2_ref(publisher, -1); |
| 833 |
} |
| 834 |
return -1; |
| 835 |
} |
| 836 |
|
| 837 |
static int publisher_client_send(void *obj, void *arg, void *data, int flags) |
| 838 |
{ |
| 839 |
struct sip_outbound_publisher *publisher = obj; |
| 840 |
const struct ast_sip_body *body = arg; |
| 841 |
struct sip_outbound_publish_message *message; |
| 842 |
size_t type_len = 0, subtype_len = 0, body_text_len = 0; |
| 843 |
int *res = data; |
| 844 |
SCOPED_AO2LOCK(lock, publisher); |
| 845 |
|
| 846 |
*res = -1; |
| 847 |
if (!publisher->client) { |
| 848 |
return -1; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
if (body) { |
| 853 |
type_len = strlen(body->type) + 1; |
| 854 |
subtype_len = strlen(body->subtype) + 1; |
| 855 |
body_text_len = strlen(body->body_text) + 1; |
| 856 |
} |
| 857 |
|
| 858 |
message = ast_calloc(1, sizeof(*message) + type_len + subtype_len + body_text_len); |
| 859 |
if (!message) { |
| 860 |
return -1; |
| 861 |
} |
| 862 |
|
| 863 |
if (body) { |
| 864 |
char *dst = message->body_contents; |
| 865 |
|
| 866 |
message->body.type = strcpy(dst, body->type); |
| 867 |
dst += type_len; |
| 868 |
message->body.subtype = strcpy(dst, body->subtype); |
| 869 |
dst += subtype_len; |
| 870 |
message->body.body_text = strcpy(dst, body->body_text); |
| 871 |
} |
| 872 |
|
| 873 |
AST_LIST_INSERT_TAIL(&publisher->queue, message, entry); |
| 874 |
|
| 875 |
*res = ast_sip_push_task(publisher->serializer, sip_publisher_service_queue, ao2_bump(publisher)); |
| 876 |
if (*res) { |
| 877 |
ao2_ref(publisher, -1); |
| 878 |
} |
| 879 |
|
| 880 |
return *res; |
| 881 |
} |
| 882 |
|
| 883 |
int ast_sip_publish_client_send(struct ast_sip_outbound_publish_client *client, |
| 884 |
const struct ast_sip_body *body) |
| 885 |
{ |
| 886 |
SCOPED_AO2LOCK(lock, client); |
| 887 |
int res = 0; |
| 888 |
|
| 889 |
ao2_callback_data(client->publishers, OBJ_NODATA, |
| 890 |
publisher_client_send, (void *)body, &res); |
| 891 |
return res; |
| 892 |
} |
| 893 |
|
| 894 |
static int sip_outbound_publisher_set_uri( |
| 895 |
pj_pool_t *pool, const char *uri, const char *user, pj_str_t *res_uri) |
| 896 |
{ |
| 897 |
pj_str_t tmp; |
| 898 |
pjsip_uri *parsed; |
| 899 |
pjsip_sip_uri *parsed_uri; |
| 900 |
int size; |
| 901 |
|
| 902 |
pj_strdup2_with_null(pool, &tmp, uri); |
| 903 |
if (!(parsed = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0))) { |
| 904 |
return -1; |
| 905 |
} |
| 906 |
|
| 907 |
if (!(parsed_uri = pjsip_uri_get_uri(parsed))) { |
| 908 |
return -1; |
| 909 |
} |
| 910 |
|
| 911 |
if (!ast_strlen_zero(user)) { |
| 912 |
pj_strdup2(pool, &parsed_uri->user, user); |
| 913 |
} |
| 914 |
|
| 915 |
res_uri->ptr = (char*) pj_pool_alloc(pool, pjsip_max_url_size); |
| 916 |
if (!res_uri->ptr) { |
| 917 |
return -1; |
| 918 |
} |
| 919 |
|
| 920 |
if ((size = pjsip_uri_print(PJSIP_URI_IN_OTHER, parsed_uri, res_uri->ptr, |
| 921 |
pjsip_max_url_size - 1)) <= 0) { |
| 922 |
return -1; |
| 923 |
} |
| 924 |
res_uri->ptr[size] = '\0'; |
| 925 |
res_uri->slen = size; |
| 926 |
|
| 927 |
return 0; |
| 928 |
} |
| 929 |
|
| 930 |
static int sip_outbound_publisher_set_uris( |
| 931 |
pj_pool_t *pool, struct sip_outbound_publisher *publisher, |
| 932 |
pj_str_t *server_uri, pj_str_t *to_uri, pj_str_t *from_uri) |
| 933 |
{ |
| 934 |
struct ast_sip_outbound_publish *publish = publisher->owner->publish; |
| 935 |
|
| 936 |
if (sip_outbound_publisher_set_uri(pool, publish->server_uri, publisher->user, server_uri)) { |
| 937 |
ast_log(LOG_ERROR, "Invalid server URI '%s' specified on outbound publish '%s'\n", |
| 938 |
publish->server_uri, ast_sorcery_object_get_id(publish)); |
| 939 |
return -1; |
| 940 |
} |
| 941 |
|
| 942 |
if (ast_strlen_zero(publish->to_uri)) { |
| 943 |
to_uri->ptr = server_uri->ptr; |
| 944 |
to_uri->slen = server_uri->slen; |
| 945 |
} else if (sip_outbound_publisher_set_uri(pool, publish->to_uri, publisher->user, to_uri)) { |
| 946 |
ast_log(LOG_ERROR, "Invalid to URI '%s' specified on outbound publish '%s'\n", |
| 947 |
publish->to_uri, ast_sorcery_object_get_id(publish)); |
| 948 |
return -1; |
| 949 |
} |
| 950 |
|
| 951 |
publisher->to_uri = ast_strdup(to_uri->ptr); |
| 952 |
if (!publisher->to_uri) { |
| 953 |
return -1; |
| 954 |
} |
| 955 |
|
| 956 |
if (ast_strlen_zero(publish->from_uri)) { |
| 957 |
from_uri->ptr = server_uri->ptr; |
| 958 |
from_uri->slen = server_uri->slen; |
| 959 |
} else if (sip_outbound_publisher_set_uri(pool, publish->from_uri, publisher->user, from_uri)) { |
| 960 |
ast_log(LOG_ERROR, "Invalid from URI '%s' specified on outbound publish '%s'\n", |
| 961 |
publish->from_uri, ast_sorcery_object_get_id(publish)); |
| 962 |
return -1; |
| 963 |
} |
| 964 |
|
| 965 |
publisher->from_uri = ast_strdup(from_uri->ptr); |
| 966 |
if (!publisher->from_uri) { |
| 967 |
return -1; |
| 968 |
} |
| 969 |
|
| 970 |
return 0; |
| 971 |
} |
| 972 |
|
| 973 |
static void sip_outbound_publish_callback(struct pjsip_publishc_cbparam *param); |
| 974 |
|
| 975 |
|
| 976 |
static int sip_outbound_publisher_init(void *data) |
| 977 |
{ |
| 978 |
struct sip_outbound_publisher *publisher = data; |
| 979 |
RAII_VAR(struct ast_sip_outbound_publish *, publish, NULL, ao2_cleanup); |
| 980 |
pjsip_publishc_opt opt = { |
| 981 |
.queue_request = PJ_FALSE, |
| 982 |
}; |
| 983 |
pj_pool_t *pool; |
| 984 |
pj_str_t event, server_uri, to_uri, from_uri; |
| 985 |
|
| 986 |
if (publisher->client) { |
| 987 |
return 0; |
| 988 |
} |
| 989 |
|
| 990 |
if (pjsip_publishc_create(ast_sip_get_pjsip_endpoint(), &opt, |
| 991 |
ao2_bump(publisher), sip_outbound_publish_callback, |
| 992 |
&publisher->client) != PJ_SUCCESS) { |
| 993 |
ao2_ref(publisher, -1); |
| 994 |
return -1; |
| 995 |
} |
| 996 |
|
| 997 |
publish = ao2_bump(publisher->owner->publish); |
| 998 |
|
| 999 |
if (!ast_strlen_zero(publish->outbound_proxy)) { |
| 1000 |
pjsip_route_hdr route_set, *route; |
| 1001 |
static const pj_str_t ROUTE_HNAME = { "Route", 5 }; |
| 1002 |
|
| 1003 |
pj_list_init(&route_set); |
| 1004 |
|
| 1005 |
if (!(route = pjsip_parse_hdr(pjsip_publishc_get_pool(publisher->client), &ROUTE_HNAME, |
| 1006 |
(char*)publish->outbound_proxy, strlen(publish->outbound_proxy), NULL))) { |
| 1007 |
pjsip_publishc_destroy(publisher->client); |
| 1008 |
return -1; |
| 1009 |
} |
| 1010 |
pj_list_insert_nodes_before(&route_set, route); |
| 1011 |
|
| 1012 |
pjsip_publishc_set_route_set(publisher->client, &route_set); |
| 1013 |
} |
| 1014 |
|
| 1015 |
pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "URI Validation", |
| 1016 |
pjsip_max_url_size, pjsip_max_url_size); |
| 1017 |
if (!pool) { |
| 1018 |
ast_log(LOG_ERROR, "Could not create pool for URI validation on outbound publish '%s'\n", |
| 1019 |
ast_sorcery_object_get_id(publish)); |
| 1020 |
pjsip_publishc_destroy(publisher->client); |
| 1021 |
return -1; |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (sip_outbound_publisher_set_uris(pool, publisher, &server_uri, &from_uri, &to_uri)) { |
| 1025 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 1026 |
pjsip_publishc_destroy(publisher->client); |
| 1027 |
return -1; |
| 1028 |
} |
| 1029 |
|
| 1030 |
pj_cstr(&event, publish->event); |
| 1031 |
if (pjsip_publishc_init(publisher->client, &event, &server_uri, &from_uri, &to_uri, |
| 1032 |
publish->expiration) != PJ_SUCCESS) { |
| 1033 |
ast_log(LOG_ERROR, "Failed to initialize publishing client on outbound publish '%s'\n", |
| 1034 |
ast_sorcery_object_get_id(publish)); |
| 1035 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 1036 |
pjsip_publishc_destroy(publisher->client); |
| 1037 |
return -1; |
| 1038 |
} |
| 1039 |
|
| 1040 |
pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); |
| 1041 |
return 0; |
| 1042 |
} |
| 1043 |
|
| 1044 |
static int sip_outbound_publisher_reinit(void *obj, void *arg, int flags) |
| 1045 |
{ |
| 1046 |
return sip_outbound_publisher_init(obj); |
| 1047 |
} |
| 1048 |
|
| 1049 |
static int sip_outbound_publisher_reinit_all(void *data) |
| 1050 |
{ |
| 1051 |
ao2_callback(data, OBJ_NODATA, sip_outbound_publisher_reinit, NULL); |
| 1052 |
return 0; |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
static void sip_outbound_publisher_destroy(void *obj) |
| 1057 |
{ |
| 1058 |
struct sip_outbound_publisher *publisher = obj; |
| 1059 |
struct sip_outbound_publish_message *message; |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
while ((message = AST_LIST_REMOVE_HEAD(&publisher->queue, entry))) { |
| 1064 |
ast_free(message); |
| 1065 |
} |
| 1066 |
|
| 1067 |
ao2_cleanup(publisher->owner); |
| 1068 |
ast_free(publisher->from_uri); |
| 1069 |
ast_free(publisher->to_uri); |
| 1070 |
|
| 1071 |
ast_taskprocessor_unreference(publisher->serializer); |
| 1072 |
} |
| 1073 |
|
| 1074 |
static struct sip_outbound_publisher *sip_outbound_publisher_alloc( |
| 1075 |
struct ast_sip_outbound_publish_client *client, const char *user) |
| 1076 |
{ |
| 1077 |
struct sip_outbound_publisher *publisher; |
| 1078 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 1079 |
|
| 1080 |
publisher = ao2_alloc(sizeof(*publisher) + (user ? strlen(user) : 0) + 1, |
| 1081 |
sip_outbound_publisher_destroy); |
| 1082 |
if (!publisher) { |
| 1083 |
return NULL; |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
publisher->owner = ao2_bump(client); |
| 1098 |
publisher->timer.user_data = publisher; |
| 1099 |
publisher->timer.cb = sip_outbound_publish_timer_cb; |
| 1100 |
if (user) { |
| 1101 |
strcpy(publisher->user, user); |
| 1102 |
} else { |
| 1103 |
*publisher->user = '\0'; |
| 1104 |
} |
| 1105 |
|
| 1106 |
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outpub/%s", |
| 1107 |
ast_sorcery_object_get_id(client->publish)); |
| 1108 |
|
| 1109 |
publisher->serializer = ast_sip_create_serializer_group(tps_name, |
| 1110 |
shutdown_group); |
| 1111 |
if (!publisher->serializer) { |
| 1112 |
ao2_ref(publisher, -1); |
| 1113 |
return NULL; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (ast_sip_push_task_wait_servant(NULL, sip_outbound_publisher_init, publisher)) { |
| 1117 |
ast_log(LOG_ERROR, "Unable to create publisher for outbound publish '%s'\n", |
| 1118 |
ast_sorcery_object_get_id(client->publish)); |
| 1119 |
ao2_ref(publisher, -1); |
| 1120 |
return NULL; |
| 1121 |
} |
| 1122 |
|
| 1123 |
return publisher; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static struct sip_outbound_publisher *sip_outbound_publish_client_add_publisher( |
| 1127 |
struct ast_sip_outbound_publish_client *client, const char *user) |
| 1128 |
{ |
| 1129 |
struct sip_outbound_publisher *publisher = |
| 1130 |
sip_outbound_publisher_alloc(client, user); |
| 1131 |
|
| 1132 |
if (!publisher) { |
| 1133 |
return NULL; |
| 1134 |
} |
| 1135 |
|
| 1136 |
if (!ao2_link(client->publishers, publisher)) { |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
if (ast_sip_push_task(publisher->serializer, cancel_refresh_timer_task, publisher)) { |
| 1142 |
ao2_ref(publisher, -1); |
| 1143 |
} |
| 1144 |
return NULL; |
| 1145 |
} |
| 1146 |
|
| 1147 |
return publisher; |
| 1148 |
} |
| 1149 |
|
| 1150 |
int ast_sip_publish_client_user_send(struct ast_sip_outbound_publish_client *client, |
| 1151 |
const char *user, const struct ast_sip_body *body) |
| 1152 |
{ |
| 1153 |
struct sip_outbound_publisher *publisher; |
| 1154 |
int res; |
| 1155 |
|
| 1156 |
publisher = sip_outbound_publish_client_get_publisher(client, user); |
| 1157 |
if (!publisher) { |
| 1158 |
return -1; |
| 1159 |
} |
| 1160 |
|
| 1161 |
publisher_client_send(publisher, (void *)body, &res, 0); |
| 1162 |
ao2_ref(publisher, -1); |
| 1163 |
return res; |
| 1164 |
} |
| 1165 |
|
| 1166 |
void ast_sip_publish_client_remove(struct ast_sip_outbound_publish_client *client, |
| 1167 |
const char *user) |
| 1168 |
{ |
| 1169 |
SCOPED_WRLOCK(lock, &load_lock); |
| 1170 |
ao2_find(client->publishers, user, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 1171 |
} |
| 1172 |
|
| 1173 |
static int explicit_publish_destroy(void *data) |
| 1174 |
{ |
| 1175 |
struct sip_outbound_publisher *publisher = data; |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
if (publisher->client) { |
| 1183 |
pjsip_publishc_destroy(publisher->client); |
| 1184 |
ao2_ref(publisher, -1); |
| 1185 |
} |
| 1186 |
|
| 1187 |
ao2_ref(publisher, -1); |
| 1188 |
|
| 1189 |
return 0; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
static int cancel_and_unpublish(void *obj, void *arg, int flags) |
| 1194 |
{ |
| 1195 |
struct sip_outbound_publisher *publisher = obj; |
| 1196 |
struct ast_sip_outbound_publish_client *client = publisher->owner; |
| 1197 |
|
| 1198 |
SCOPED_AO2LOCK(lock, publisher); |
| 1199 |
|
| 1200 |
if (!client->started) { |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
if (ast_sip_push_task(publisher->serializer, explicit_publish_destroy, ao2_bump(publisher))) { |
| 1205 |
ao2_ref(publisher, -1); |
| 1206 |
} |
| 1207 |
return 0; |
| 1208 |
} |
| 1209 |
|
| 1210 |
if (ast_sip_push_task(publisher->serializer, cancel_refresh_timer_task, ao2_bump(publisher))) { |
| 1211 |
ast_log(LOG_WARNING, "Could not stop refresh timer on outbound publish '%s'\n", |
| 1212 |
ast_sorcery_object_get_id(client->publish)); |
| 1213 |
ao2_ref(publisher, -1); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
if (!publisher->sending) { |
| 1218 |
if (ast_sip_push_task(publisher->serializer, send_unpublish_task, ao2_bump(publisher))) { |
| 1219 |
ast_log(LOG_WARNING, "Could not send unpublish message on outbound publish '%s'\n", |
| 1220 |
ast_sorcery_object_get_id(client->publish)); |
| 1221 |
ao2_ref(publisher, -1); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
publisher->destroy = 1; |
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
static void sip_outbound_publish_client_destroy(void *obj) |
| 1230 |
{ |
| 1231 |
struct ast_sip_outbound_publish_client *client = obj; |
| 1232 |
|
| 1233 |
ao2_cleanup(client->datastores); |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
ao2_cleanup(client->publish); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
static void sip_outbound_publish_state_destroy(void *obj) |
| 1246 |
{ |
| 1247 |
struct ast_sip_outbound_publish_state *state = obj; |
| 1248 |
|
| 1249 |
stop_publishing(state->client, NULL); |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
ao2_callback(state->client->publishers, OBJ_NODATA | OBJ_UNLINK, cancel_and_unpublish, NULL); |
| 1264 |
ao2_cleanup(state->client->publishers); |
| 1265 |
|
| 1266 |
state->client->started = 0; |
| 1267 |
ao2_cleanup(state->client); |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
static int can_reuse_publish(struct ast_sip_outbound_publish *existing, struct ast_sip_outbound_publish *applied) |
| 1281 |
{ |
| 1282 |
int i; |
| 1283 |
|
| 1284 |
if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->from_uri, applied->from_uri) || |
| 1285 |
strcmp(existing->to_uri, applied->to_uri) || strcmp(existing->outbound_proxy, applied->outbound_proxy) || |
| 1286 |
strcmp(existing->event, applied->event) || |
| 1287 |
AST_VECTOR_SIZE(&existing->outbound_auths) != AST_VECTOR_SIZE(&applied->outbound_auths)) { |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
|
| 1291 |
for (i = 0; i < AST_VECTOR_SIZE(&existing->outbound_auths); ++i) { |
| 1292 |
if (strcmp(AST_VECTOR_GET(&existing->outbound_auths, i), AST_VECTOR_GET(&applied->outbound_auths, i))) { |
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
return 1; |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
static void sip_outbound_publish_callback(struct pjsip_publishc_cbparam *param) |
| 1302 |
{ |
| 1303 |
#define DESTROY_CLIENT() do { \ |
| 1304 |
pjsip_publishc_destroy(publisher->client); \ |
| 1305 |
publisher->client = NULL; \ |
| 1306 |
ao2_ref(publisher, -1); } while (0) |
| 1307 |
|
| 1308 |
RAII_VAR(struct sip_outbound_publisher *, publisher, ao2_bump(param->token), ao2_cleanup); |
| 1309 |
RAII_VAR(struct ast_sip_outbound_publish *, publish, ao2_bump(publisher->owner->publish), ao2_cleanup); |
| 1310 |
SCOPED_AO2LOCK(lock, publisher); |
| 1311 |
pjsip_tx_data *tdata; |
| 1312 |
|
| 1313 |
if (publisher->destroy) { |
| 1314 |
if (publisher->sending) { |
| 1315 |
publisher->sending = NULL; |
| 1316 |
|
| 1317 |
if (!ast_sip_push_task(publisher->serializer, send_unpublish_task, ao2_bump(publisher))) { |
| 1318 |
return; |
| 1319 |
} |
| 1320 |
ast_log(LOG_WARNING, "Could not send unpublish message on outbound publish '%s'\n", |
| 1321 |
ast_sorcery_object_get_id(publish)); |
| 1322 |
ao2_ref(publisher, -1); |
| 1323 |
} |
| 1324 |
|
| 1325 |
DESTROY_CLIENT(); |
| 1326 |
return; |
| 1327 |
} |
| 1328 |
|
| 1329 |
if (param->code == 401 || param->code == 407) { |
| 1330 |
pjsip_transaction *tsx = pjsip_rdata_get_tsx(param->rdata); |
| 1331 |
|
| 1332 |
if (!ast_sip_create_request_with_auth(&publish->outbound_auths, |
| 1333 |
param->rdata, tsx->last_tx, &tdata)) { |
| 1334 |
set_transport(publisher, tdata); |
| 1335 |
pjsip_publishc_send(publisher->client, tdata); |
| 1336 |
} |
| 1337 |
publisher->auth_attempts++; |
| 1338 |
|
| 1339 |
if (publisher->auth_attempts == publish->max_auth_attempts) { |
| 1340 |
DESTROY_CLIENT(); |
| 1341 |
ast_log(LOG_ERROR, "Reached maximum number of PUBLISH authentication attempts on outbound publish '%s'\n", |
| 1342 |
ast_sorcery_object_get_id(publish)); |
| 1343 |
|
| 1344 |
goto end; |
| 1345 |
} |
| 1346 |
return; |
| 1347 |
} |
| 1348 |
|
| 1349 |
publisher->auth_attempts = 0; |
| 1350 |
|
| 1351 |
if (param->code == 412) { |
| 1352 |
DESTROY_CLIENT(); |
| 1353 |
if (sip_outbound_publisher_init(publisher)) { |
| 1354 |
ast_log(LOG_ERROR, "Failed to create a new outbound publish client for '%s' on 412 response\n", |
| 1355 |
ast_sorcery_object_get_id(publish)); |
| 1356 |
goto end; |
| 1357 |
} |
| 1358 |
|
| 1359 |
|
| 1360 |
publisher->sending = NULL; |
| 1361 |
} else if (param->code == 423) { |
| 1362 |
|
| 1363 |
pjsip_expires_hdr *expires; |
| 1364 |
|
| 1365 |
expires = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_MIN_EXPIRES, NULL); |
| 1366 |
if (!expires || !expires->ivalue) { |
| 1367 |
DESTROY_CLIENT(); |
| 1368 |
ast_log(LOG_ERROR, "Received 423 response on outbound publish '%s' without a Min-Expires header\n", |
| 1369 |
ast_sorcery_object_get_id(publish)); |
| 1370 |
goto end; |
| 1371 |
} |
| 1372 |
|
| 1373 |
pjsip_publishc_update_expires(publisher->client, expires->ivalue); |
| 1374 |
publisher->sending = NULL; |
| 1375 |
} else if (publisher->sending) { |
| 1376 |
|
| 1377 |
AST_LIST_REMOVE_HEAD(&publisher->queue, entry); |
| 1378 |
ast_free(publisher->sending); |
| 1379 |
publisher->sending = NULL; |
| 1380 |
if (!param->rdata) { |
| 1381 |
ast_log(LOG_NOTICE, "No response received for outbound publish '%s'\n", |
| 1382 |
ast_sorcery_object_get_id(publish)); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
if (AST_LIST_EMPTY(&publisher->queue)) { |
| 1387 |
schedule_publish_refresh(publisher, param->expiration); |
| 1388 |
} |
| 1389 |
|
| 1390 |
end: |
| 1391 |
if (!publisher->client) { |
| 1392 |
struct sip_outbound_publish_message *message; |
| 1393 |
|
| 1394 |
while ((message = AST_LIST_REMOVE_HEAD(&publisher->queue, entry))) { |
| 1395 |
ast_free(message); |
| 1396 |
} |
| 1397 |
} else { |
| 1398 |
if (ast_sip_push_task(publisher->serializer, sip_publisher_service_queue, ao2_bump(publisher))) { |
| 1399 |
ao2_ref(publisher, -1); |
| 1400 |
} |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
#define DATASTORE_BUCKETS 53 |
| 1405 |
|
| 1406 |
static int datastore_hash(const void *obj, int flags) |
| 1407 |
{ |
| 1408 |
const struct ast_datastore *datastore; |
| 1409 |
const char *uid; |
| 1410 |
|
| 1411 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1412 |
case OBJ_SEARCH_KEY: |
| 1413 |
uid = obj; |
| 1414 |
break; |
| 1415 |
case OBJ_SEARCH_OBJECT: |
| 1416 |
datastore = obj; |
| 1417 |
uid = datastore->uid; |
| 1418 |
break; |
| 1419 |
default: |
| 1420 |
|
| 1421 |
ast_assert(0); |
| 1422 |
return 0; |
| 1423 |
} |
| 1424 |
|
| 1425 |
return ast_str_hash(uid); |
| 1426 |
} |
| 1427 |
|
| 1428 |
static int datastore_cmp(void *obj, void *arg, int flags) |
| 1429 |
{ |
| 1430 |
const struct ast_datastore *object_left = obj; |
| 1431 |
const struct ast_datastore *object_right = arg; |
| 1432 |
const char *right_key = arg; |
| 1433 |
int cmp; |
| 1434 |
|
| 1435 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1436 |
case OBJ_SEARCH_OBJECT: |
| 1437 |
right_key = object_right->uid; |
| 1438 |
|
| 1439 |
case OBJ_SEARCH_KEY: |
| 1440 |
cmp = strcmp(object_left->uid, right_key); |
| 1441 |
break; |
| 1442 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 1443 |
cmp = strncmp(object_left->uid, right_key, strlen(right_key)); |
| 1444 |
break; |
| 1445 |
default: |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
cmp = 0; |
| 1451 |
break; |
| 1452 |
} |
| 1453 |
if (cmp) { |
| 1454 |
return 0; |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
return CMP_MATCH; |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
static struct ast_sip_outbound_publish_state *sip_outbound_publish_state_alloc( |
| 1465 |
struct ast_sip_outbound_publish *publish) |
| 1466 |
{ |
| 1467 |
const char *id = ast_sorcery_object_get_id(publish); |
| 1468 |
struct ast_sip_outbound_publish_state *state = |
| 1469 |
ao2_alloc(sizeof(*state) + strlen(id) + 1, sip_outbound_publish_state_destroy); |
| 1470 |
|
| 1471 |
if (!state) { |
| 1472 |
return NULL; |
| 1473 |
} |
| 1474 |
|
| 1475 |
state->client = ao2_alloc(sizeof(*state->client), sip_outbound_publish_client_destroy); |
| 1476 |
if (!state->client) { |
| 1477 |
ao2_ref(state, -1); |
| 1478 |
return NULL; |
| 1479 |
} |
| 1480 |
|
| 1481 |
state->client->datastores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 1482 |
DATASTORE_BUCKETS, datastore_hash, NULL, datastore_cmp); |
| 1483 |
if (!state->client->datastores) { |
| 1484 |
ao2_ref(state, -1); |
| 1485 |
return NULL; |
| 1486 |
} |
| 1487 |
|
| 1488 |
state->client->publishers = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 1489 |
DATASTORE_BUCKETS, |
| 1490 |
sip_outbound_publisher_hash_fn, NULL, sip_outbound_publisher_cmp_fn); |
| 1491 |
if (!state->client->publishers) { |
| 1492 |
ao2_ref(state, -1); |
| 1493 |
return NULL; |
| 1494 |
} |
| 1495 |
|
| 1496 |
state->client->publish = ao2_bump(publish); |
| 1497 |
|
| 1498 |
strcpy(state->id, id); |
| 1499 |
return state; |
| 1500 |
} |
| 1501 |
|
| 1502 |
static int validate_publish_config(struct ast_sip_outbound_publish *publish) |
| 1503 |
{ |
| 1504 |
if (ast_strlen_zero(publish->server_uri)) { |
| 1505 |
ast_log(LOG_ERROR, "No server URI specified on outbound publish '%s'\n", |
| 1506 |
ast_sorcery_object_get_id(publish)); |
| 1507 |
return -1; |
| 1508 |
} else if (ast_sip_validate_uri_length(publish->server_uri)) { |
| 1509 |
ast_log(LOG_ERROR, "Server URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s' on outbound publish '%s'\n", |
| 1510 |
publish->server_uri, |
| 1511 |
ast_sorcery_object_get_id(publish)); |
| 1512 |
return -1; |
| 1513 |
} else if (ast_strlen_zero(publish->event)) { |
| 1514 |
ast_log(LOG_ERROR, "No event type specified for outbound publish '%s'\n", |
| 1515 |
ast_sorcery_object_get_id(publish)); |
| 1516 |
return -1; |
| 1517 |
} else if (!ast_strlen_zero(publish->from_uri) |
| 1518 |
&& ast_sip_validate_uri_length(publish->from_uri)) { |
| 1519 |
ast_log(LOG_ERROR, "From URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s' on outbound publish '%s'\n", |
| 1520 |
publish->from_uri, |
| 1521 |
ast_sorcery_object_get_id(publish)); |
| 1522 |
return -1; |
| 1523 |
} else if (!ast_strlen_zero(publish->to_uri) |
| 1524 |
&& ast_sip_validate_uri_length(publish->to_uri)) { |
| 1525 |
ast_log(LOG_ERROR, "To URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s' on outbound publish '%s'\n", |
| 1526 |
publish->to_uri, |
| 1527 |
ast_sorcery_object_get_id(publish)); |
| 1528 |
return -1; |
| 1529 |
} |
| 1530 |
return 0; |
| 1531 |
} |
| 1532 |
|
| 1533 |
static int current_state_reusable(struct ast_sip_outbound_publish *publish, |
| 1534 |
struct ast_sip_outbound_publish_state *current_state) |
| 1535 |
{ |
| 1536 |
struct ast_sip_outbound_publish *old_publish; |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
if ((!publish->multi_user && current_state->client->publish->multi_user) || |
| 1542 |
(publish->multi_user && !current_state->client->publish->multi_user)) { |
| 1543 |
return 0; |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
if (!can_reuse_publish(current_state->client->publish, publish)) { |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
return 0; |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
old_publish = current_state->client->publish; |
| 1561 |
current_state->client->publish = publish; |
| 1562 |
if (ast_sip_push_task_wait_servant(NULL, sip_outbound_publisher_reinit_all, |
| 1563 |
current_state->client->publishers)) { |
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
current_state->client->publish = publish; |
| 1569 |
ast_log(LOG_ERROR, "Unable to reinitialize client(s) for outbound publish '%s'\n", |
| 1570 |
ast_sorcery_object_get_id(current_state->client->publish)); |
| 1571 |
return -1; |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
ao2_ref(current_state->client->publish, +1); |
| 1579 |
ao2_cleanup(old_publish); |
| 1580 |
|
| 1581 |
|
| 1582 |
return 1; |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
static int sip_outbound_publish_apply(const struct ast_sorcery *sorcery, void *obj) |
| 1587 |
{ |
| 1588 |
#define ADD_TO_NEW_STATES(__obj) \ |
| 1589 |
do { if (__obj) { \ |
| 1590 |
ao2_link(new_states, __obj); \ |
| 1591 |
ao2_ref(__obj, -1); } } while (0) |
| 1592 |
|
| 1593 |
struct ast_sip_outbound_publish *applied = obj; |
| 1594 |
struct ast_sip_outbound_publish_state *current_state, *new_state; |
| 1595 |
struct sip_outbound_publisher *publisher = NULL; |
| 1596 |
int res; |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
if (!new_states) { |
| 1603 |
new_states = ao2_container_alloc_hash( |
| 1604 |
AO2_ALLOC_OPT_LOCK_NOLOCK, 0, DEFAULT_STATE_BUCKETS, |
| 1605 |
outbound_publish_state_hash, NULL, outbound_publish_state_cmp); |
| 1606 |
|
| 1607 |
if (!new_states) { |
| 1608 |
ast_log(LOG_ERROR, "Unable to allocate new states container\n"); |
| 1609 |
return -1; |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
current_state = sip_publish_state_get(ast_sorcery_object_get_id(applied)); |
| 1615 |
|
| 1616 |
if ((res = validate_publish_config(applied))) { |
| 1617 |
ADD_TO_NEW_STATES(current_state); |
| 1618 |
return res; |
| 1619 |
} |
| 1620 |
|
| 1621 |
if (current_state && (res = current_state_reusable(applied, current_state))) { |
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
ADD_TO_NEW_STATES(current_state); |
| 1627 |
return res == 1 ? 0 : -1; |
| 1628 |
} |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
new_state = sip_outbound_publish_state_alloc(applied); |
| 1635 |
if (!new_state) { |
| 1636 |
ast_log(LOG_ERROR, "Unable to create state for outbound publish '%s'\n", |
| 1637 |
ast_sorcery_object_get_id(applied)); |
| 1638 |
ADD_TO_NEW_STATES(current_state); |
| 1639 |
return -1; |
| 1640 |
}; |
| 1641 |
|
| 1642 |
if (!applied->multi_user && |
| 1643 |
!(publisher = sip_outbound_publish_client_add_publisher(new_state->client, NULL))) { |
| 1644 |
ADD_TO_NEW_STATES(current_state); |
| 1645 |
ao2_ref(new_state, -1); |
| 1646 |
return -1; |
| 1647 |
} |
| 1648 |
ao2_cleanup(publisher); |
| 1649 |
|
| 1650 |
ADD_TO_NEW_STATES(new_state); |
| 1651 |
ao2_cleanup(current_state); |
| 1652 |
return res; |
| 1653 |
} |
| 1654 |
|
| 1655 |
static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 1656 |
{ |
| 1657 |
struct ast_sip_outbound_publish *publish = obj; |
| 1658 |
|
| 1659 |
return ast_sip_auth_vector_init(&publish->outbound_auths, var->value); |
| 1660 |
} |
| 1661 |
|
| 1662 |
|
| 1663 |
static int unload_module(void) |
| 1664 |
{ |
| 1665 |
int remaining; |
| 1666 |
|
| 1667 |
ast_sorcery_object_unregister(ast_sip_get_sorcery(), "outbound-publish"); |
| 1668 |
|
| 1669 |
ao2_global_obj_release(current_states); |
| 1670 |
|
| 1671 |
|
| 1672 |
ast_debug(2, "Waiting for publication to complete for unload.\n"); |
| 1673 |
remaining = ast_serializer_shutdown_group_join(shutdown_group, MAX_UNLOAD_TIMEOUT_TIME); |
| 1674 |
if (remaining) { |
| 1675 |
ast_log(LOG_WARNING, "Unload incomplete. Could not stop %d outbound publications. Try again later.\n", |
| 1676 |
remaining); |
| 1677 |
return -1; |
| 1678 |
} |
| 1679 |
|
| 1680 |
ast_debug(2, "Successful shutdown.\n"); |
| 1681 |
|
| 1682 |
ao2_cleanup(shutdown_group); |
| 1683 |
shutdown_group = NULL; |
| 1684 |
|
| 1685 |
return 0; |
| 1686 |
} |
| 1687 |
|
| 1688 |
static int load_module(void) |
| 1689 |
{ |
| 1690 |
|
| 1691 |
ast_pjproject_get_buildopt("PJSIP_MAX_URL_SIZE", "%d", &pjsip_max_url_size); |
| 1692 |
|
| 1693 |
shutdown_group = ast_serializer_shutdown_group_alloc(); |
| 1694 |
if (!shutdown_group) { |
| 1695 |
return AST_MODULE_LOAD_DECLINE; |
| 1696 |
} |
| 1697 |
|
| 1698 |
ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_outbound_publish"); |
| 1699 |
ast_sorcery_apply_default(ast_sip_get_sorcery(), "outbound-publish", "config", "pjsip.conf,criteria=type=outbound-publish"); |
| 1700 |
|
| 1701 |
if (ast_sorcery_object_register(ast_sip_get_sorcery(), "outbound-publish", sip_outbound_publish_alloc, NULL, |
| 1702 |
sip_outbound_publish_apply)) { |
| 1703 |
ast_log(LOG_ERROR, "Unable to register 'outbound-publish' type with sorcery\n"); |
| 1704 |
unload_module(); |
| 1705 |
return AST_MODULE_LOAD_DECLINE; |
| 1706 |
} |
| 1707 |
|
| 1708 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "type", "", OPT_NOOP_T, 0, 0); |
| 1709 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, server_uri)); |
| 1710 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "from_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, from_uri)); |
| 1711 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "event", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, event)); |
| 1712 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "to_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, to_uri)); |
| 1713 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, outbound_proxy)); |
| 1714 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_outbound_publish, expiration)); |
| 1715 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "max_auth_attempts", "5", OPT_UINT_T, 0, FLDSET(struct ast_sip_outbound_publish, max_auth_attempts)); |
| 1716 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, transport)); |
| 1717 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "outbound-publish", "outbound_auth", "", outbound_auth_handler, NULL, NULL, 0, 0); |
| 1718 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "multi_user", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_outbound_publish, multi_user)); |
| 1719 |
|
| 1720 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "outbound-publish"); |
| 1721 |
|
| 1722 |
AST_RWLIST_RDLOCK(&publisher_handlers); |
| 1723 |
sip_outbound_publish_synchronize(NULL); |
| 1724 |
AST_RWLIST_UNLOCK(&publisher_handlers); |
| 1725 |
|
| 1726 |
pjsip_publishc_init_module(ast_sip_get_pjsip_endpoint()); |
| 1727 |
|
| 1728 |
return AST_MODULE_LOAD_SUCCESS; |
| 1729 |
} |
| 1730 |
|
| 1731 |
static int reload_module(void) |
| 1732 |
{ |
| 1733 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "outbound-publish"); |
| 1734 |
|
| 1735 |
AST_RWLIST_RDLOCK(&publisher_handlers); |
| 1736 |
sip_outbound_publish_synchronize(NULL); |
| 1737 |
AST_RWLIST_UNLOCK(&publisher_handlers); |
| 1738 |
return 0; |
| 1739 |
} |
| 1740 |
|
| 1741 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Publish Support", |
| 1742 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1743 |
.load = load_module, |
| 1744 |
.reload = reload_module, |
| 1745 |
.unload = unload_module, |
| 1746 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 1747 |
.requires = "res_pjproject,res_pjsip", |
| 1748 |
); |