| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#include "asterisk.h" |
| 141 |
|
| 142 |
#include <pjsip.h> |
| 143 |
|
| 144 |
#include "asterisk/res_pjsip.h" |
| 145 |
#include "asterisk/res_pjsip_session.h" |
| 146 |
#include "asterisk/manager.h" |
| 147 |
#include "asterisk/module.h" |
| 148 |
#include "asterisk/logger.h" |
| 149 |
#include "asterisk/cli.h" |
| 150 |
#include "asterisk/sorcery.h" |
| 151 |
#include "asterisk/astobj2.h" |
| 152 |
#include "asterisk/strings.h" |
| 153 |
|
| 154 |
enum { |
| 155 |
MAINT_HASH_BUCKETS = 53, |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static struct ao2_container *maintenance_set; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static int maint_set_add(const char *endpoint_name) |
| 172 |
{ |
| 173 |
char *entry; |
| 174 |
|
| 175 |
entry = ao2_find(maintenance_set, endpoint_name, OBJ_SEARCH_KEY); |
| 176 |
if (entry) { |
| 177 |
ao2_ref(entry, -1); |
| 178 |
return 0; |
| 179 |
} |
| 180 |
return ast_str_container_add(maintenance_set, endpoint_name) ? -1 : 1; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static int maint_set_remove(const char *endpoint_name) |
| 190 |
{ |
| 191 |
char *entry; |
| 192 |
|
| 193 |
entry = ao2_find(maintenance_set, endpoint_name, OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 194 |
if (!entry) { |
| 195 |
return 0; |
| 196 |
} |
| 197 |
ao2_ref(entry, -1); |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static int apply_maintenance_state(const char *endpoint_name, int enable) |
| 213 |
{ |
| 214 |
return enable ? maint_set_add(endpoint_name) : maint_set_remove(endpoint_name); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static int maint_session_create(struct ast_sip_endpoint *endpoint, |
| 227 |
struct ast_sip_contact *contact, const char *location, |
| 228 |
const char *request_user, struct ast_stream_topology *req_topology) |
| 229 |
{ |
| 230 |
const char *endpoint_name = ast_sorcery_object_get_id(endpoint); |
| 231 |
char *entry = ao2_find(maintenance_set, endpoint_name, OBJ_SEARCH_KEY); |
| 232 |
|
| 233 |
if (entry) { |
| 234 |
ao2_ref(entry, -1); |
| 235 |
ast_log(LOG_NOTICE, "PJSIP: Refusing outbound call to endpoint '%s': maintenance mode active\n", |
| 236 |
endpoint_name); |
| 237 |
return 1; |
| 238 |
} |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
static struct ast_sip_session_supplement maintenance_session_supplement = { |
| 243 |
.session_create = maint_session_create, |
| 244 |
.priority = AST_SIP_SUPPLEMENT_PRIORITY_FIRST, |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static pj_bool_t maintenance_on_rx_request(pjsip_rx_data *rdata) |
| 255 |
{ |
| 256 |
pjsip_msg *msg = rdata->msg_info.msg; |
| 257 |
const pjsip_method *method = &msg->line.req.method; |
| 258 |
pjsip_to_hdr *to; |
| 259 |
pjsip_expires_hdr *expires_hdr; |
| 260 |
struct ast_sip_endpoint *endpoint; |
| 261 |
char *entry; |
| 262 |
pjsip_hdr hdr_list; |
| 263 |
pjsip_generic_int_hdr *retry_after; |
| 264 |
static const pj_str_t str_retry_after = { "Retry-After", 11 }; |
| 265 |
int is_subscribe; |
| 266 |
int is_register; |
| 267 |
|
| 268 |
is_subscribe = pjsip_method_cmp(method, pjsip_get_subscribe_method()) == 0; |
| 269 |
is_register = pjsip_method_cmp(method, pjsip_get_register_method()) == 0; |
| 270 |
|
| 271 |
|
| 272 |
to = rdata->msg_info.to; |
| 273 |
if (to->tag.slen > 0) { |
| 274 |
return PJ_FALSE; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
if (is_subscribe || is_register) { |
| 279 |
expires_hdr = pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
| 280 |
if (expires_hdr && expires_hdr->ivalue == 0) { |
| 281 |
return PJ_FALSE; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
endpoint = ast_pjsip_rdata_get_endpoint(rdata); |
| 286 |
if (!endpoint) { |
| 287 |
return PJ_FALSE; |
| 288 |
} |
| 289 |
|
| 290 |
entry = ao2_find(maintenance_set, ast_sorcery_object_get_id(endpoint), OBJ_SEARCH_KEY); |
| 291 |
if (!entry) { |
| 292 |
ao2_ref(endpoint, -1); |
| 293 |
return PJ_FALSE; |
| 294 |
} |
| 295 |
ao2_ref(entry, -1); |
| 296 |
|
| 297 |
ast_log(LOG_NOTICE, "PJSIP: Endpoint '%s' is in maintenance mode; rejecting new %.*s from %s\n", |
| 298 |
ast_sorcery_object_get_id(endpoint), |
| 299 |
(int)method->name.slen, method->name.ptr, |
| 300 |
rdata->pkt_info.src_name); |
| 301 |
|
| 302 |
ao2_ref(endpoint, -1); |
| 303 |
|
| 304 |
pj_list_init(&hdr_list); |
| 305 |
retry_after = pjsip_generic_int_hdr_create(rdata->tp_info.pool, |
| 306 |
&str_retry_after, 300); |
| 307 |
if (retry_after) { |
| 308 |
pj_list_push_back(&hdr_list, retry_after); |
| 309 |
} |
| 310 |
|
| 311 |
pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 503, NULL, |
| 312 |
retry_after ? &hdr_list : NULL, NULL); |
| 313 |
|
| 314 |
return PJ_TRUE; |
| 315 |
} |
| 316 |
|
| 317 |
static struct pjsip_module maintenance_pjsip_mod = { |
| 318 |
.name = { "Maintenance Module", 18 }, |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
.priority = PJSIP_MOD_PRIORITY_APPLICATION - 3, |
| 328 |
.on_rx_request = maintenance_on_rx_request, |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static void maint_endpoint_deleted(const void *obj) |
| 334 |
{ |
| 335 |
maint_set_remove(ast_sorcery_object_get_id(obj)); |
| 336 |
} |
| 337 |
|
| 338 |
static const struct ast_sorcery_observer endpoint_observer = { |
| 339 |
.deleted = maint_endpoint_deleted, |
| 340 |
}; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static char *cli_complete_endpoint(const char *word) |
| 349 |
{ |
| 350 |
int wordlen = strlen(word); |
| 351 |
struct ao2_container *endpoints; |
| 352 |
struct ast_sip_endpoint *endpoint; |
| 353 |
struct ao2_iterator i; |
| 354 |
|
| 355 |
endpoints = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), |
| 356 |
"endpoint", word, wordlen); |
| 357 |
if (!endpoints) { |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
|
| 361 |
i = ao2_iterator_init(endpoints, 0); |
| 362 |
while ((endpoint = ao2_iterator_next(&i))) { |
| 363 |
ast_cli_completion_add(ast_strdup(ast_sorcery_object_get_id(endpoint))); |
| 364 |
ao2_cleanup(endpoint); |
| 365 |
} |
| 366 |
ao2_iterator_destroy(&i); |
| 367 |
ao2_ref(endpoints, -1); |
| 368 |
|
| 369 |
return NULL; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
static char *handle_cli_pjsip_set_maintenance(struct ast_cli_entry *e, int cmd, |
| 375 |
struct ast_cli_args *a) |
| 376 |
{ |
| 377 |
struct ast_sip_endpoint *endpoint; |
| 378 |
struct ao2_container *all_endpoints; |
| 379 |
struct ao2_iterator it; |
| 380 |
const char *endpoint_name; |
| 381 |
int enable; |
| 382 |
int rc; |
| 383 |
int count; |
| 384 |
int failed; |
| 385 |
|
| 386 |
switch (cmd) { |
| 387 |
case CLI_INIT: |
| 388 |
e->command = "pjsip set maintenance"; |
| 389 |
e->usage = |
| 390 |
"Usage: pjsip set maintenance <on|off> <endpoint|all>\n" |
| 391 |
" Place a PJSIP endpoint into or out of maintenance mode.\n" |
| 392 |
" Use 'all' to toggle maintenance mode for every endpoint.\n" |
| 393 |
" While in maintenance mode new inbound out-of-dialog requests\n" |
| 394 |
" to that endpoint are rejected with 503 (except SUBSCRIBE/\n" |
| 395 |
" REGISTER with Expires: 0), and outbound originations are\n" |
| 396 |
" refused.\n"; |
| 397 |
return NULL; |
| 398 |
case CLI_GENERATE: |
| 399 |
if (a->pos == 3) { |
| 400 |
static const char * const opts[] = { "on", "off", NULL }; |
| 401 |
return ast_cli_complete(a->word, opts, a->n); |
| 402 |
} |
| 403 |
if (a->pos == 4) { |
| 404 |
if (!strncasecmp("all", a->word, strlen(a->word))) { |
| 405 |
ast_cli_completion_add(ast_strdup("all")); |
| 406 |
} |
| 407 |
return cli_complete_endpoint(a->word); |
| 408 |
} |
| 409 |
return NULL; |
| 410 |
} |
| 411 |
|
| 412 |
if (a->argc != 5) { |
| 413 |
return CLI_SHOWUSAGE; |
| 414 |
} |
| 415 |
|
| 416 |
if (!strcasecmp(a->argv[3], "on")) { |
| 417 |
enable = 1; |
| 418 |
} else if (!strcasecmp(a->argv[3], "off")) { |
| 419 |
enable = 0; |
| 420 |
} else { |
| 421 |
return CLI_SHOWUSAGE; |
| 422 |
} |
| 423 |
|
| 424 |
endpoint_name = a->argv[4]; |
| 425 |
|
| 426 |
if (!strcasecmp(endpoint_name, "all")) { |
| 427 |
all_endpoints = ast_sip_get_endpoints(); |
| 428 |
if (!all_endpoints) { |
| 429 |
ast_cli(a->fd, "Failed to retrieve endpoint list\n"); |
| 430 |
return CLI_SUCCESS; |
| 431 |
} |
| 432 |
count = 0; |
| 433 |
failed = 0; |
| 434 |
it = ao2_iterator_init(all_endpoints, 0); |
| 435 |
while ((endpoint = ao2_iterator_next(&it))) { |
| 436 |
rc = apply_maintenance_state(ast_sorcery_object_get_id(endpoint), enable); |
| 437 |
if (rc > 0) { |
| 438 |
count++; |
| 439 |
} else if (rc < 0) { |
| 440 |
failed++; |
| 441 |
} |
| 442 |
ao2_ref(endpoint, -1); |
| 443 |
} |
| 444 |
ao2_iterator_destroy(&it); |
| 445 |
ao2_ref(all_endpoints, -1); |
| 446 |
if (count > 0) { |
| 447 |
manager_event(EVENT_FLAG_SYSTEM, "PJSIPMaintenanceStatus", |
| 448 |
"Endpoint: all\r\n" |
| 449 |
"Status: %s\r\n", |
| 450 |
enable ? "enabled" : "disabled"); |
| 451 |
ast_log(LOG_NOTICE, "PJSIP: Maintenance mode %s for all endpoints " |
| 452 |
"(%d endpoint%s affected)\n", |
| 453 |
enable ? "enabled" : "disabled", |
| 454 |
count, count == 1 ? "" : "s"); |
| 455 |
} |
| 456 |
ast_cli(a->fd, "Maintenance mode %s for %d endpoint%s%s\n", |
| 457 |
enable ? "ENABLED" : "DISABLED", |
| 458 |
count, count == 1 ? "" : "s", |
| 459 |
failed ? " (some failed)" : ""); |
| 460 |
return CLI_SUCCESS; |
| 461 |
} |
| 462 |
|
| 463 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name); |
| 464 |
if (!endpoint) { |
| 465 |
ast_cli(a->fd, "Endpoint '%s' not found\n", endpoint_name); |
| 466 |
return CLI_SUCCESS; |
| 467 |
} |
| 468 |
ao2_ref(endpoint, -1); |
| 469 |
|
| 470 |
rc = apply_maintenance_state(endpoint_name, enable); |
| 471 |
if (rc > 0) { |
| 472 |
manager_event(EVENT_FLAG_SYSTEM, "PJSIPMaintenanceStatus", |
| 473 |
"Endpoint: %s\r\n" |
| 474 |
"Status: %s\r\n", |
| 475 |
endpoint_name, enable ? "enabled" : "disabled"); |
| 476 |
ast_log(LOG_NOTICE, "PJSIP: Maintenance mode %s for endpoint '%s'\n", |
| 477 |
enable ? "enabled" : "disabled", endpoint_name); |
| 478 |
ast_cli(a->fd, "Maintenance mode %s for endpoint '%s'\n", |
| 479 |
enable ? "ENABLED" : "DISABLED", endpoint_name); |
| 480 |
} else if (rc == 0 && enable) { |
| 481 |
ast_cli(a->fd, "Endpoint '%s' is already in maintenance mode\n", endpoint_name); |
| 482 |
} else if (rc == 0) { |
| 483 |
ast_cli(a->fd, "Endpoint '%s' was not in maintenance mode\n", endpoint_name); |
| 484 |
} else { |
| 485 |
ast_cli(a->fd, "Failed to %s maintenance mode for endpoint '%s'\n", |
| 486 |
enable ? "enable" : "disable", endpoint_name); |
| 487 |
} |
| 488 |
|
| 489 |
return CLI_SUCCESS; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
static int cli_maint_entry_cb(void *obj, void *arg, int flags) |
| 496 |
{ |
| 497 |
const char *name = obj; |
| 498 |
int fd = *(int *)arg; |
| 499 |
ast_cli(fd, " %-40s ON\n", name); |
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
static char *handle_cli_pjsip_show_maintenance(struct ast_cli_entry *e, int cmd, |
| 504 |
struct ast_cli_args *a) |
| 505 |
{ |
| 506 |
const char *endpoint_name; |
| 507 |
char *entry; |
| 508 |
int fd; |
| 509 |
int count; |
| 510 |
|
| 511 |
switch (cmd) { |
| 512 |
case CLI_INIT: |
| 513 |
e->command = "pjsip show maintenance"; |
| 514 |
e->usage = |
| 515 |
"Usage: pjsip show maintenance [endpoint]\n" |
| 516 |
" Display endpoints currently in maintenance mode.\n" |
| 517 |
" If [endpoint] is given, show the status for that endpoint only.\n"; |
| 518 |
return NULL; |
| 519 |
case CLI_GENERATE: |
| 520 |
if (a->pos == 3) { |
| 521 |
return cli_complete_endpoint(a->word); |
| 522 |
} |
| 523 |
return NULL; |
| 524 |
} |
| 525 |
|
| 526 |
if (a->argc == 4) { |
| 527 |
endpoint_name = a->argv[3]; |
| 528 |
entry = ao2_find(maintenance_set, endpoint_name, OBJ_SEARCH_KEY); |
| 529 |
if (entry) { |
| 530 |
ast_cli(a->fd, "Endpoint '%s' is in maintenance mode\n", endpoint_name); |
| 531 |
ao2_ref(entry, -1); |
| 532 |
} else { |
| 533 |
ast_cli(a->fd, "Endpoint '%s' is NOT in maintenance mode\n", endpoint_name); |
| 534 |
} |
| 535 |
return CLI_SUCCESS; |
| 536 |
} |
| 537 |
|
| 538 |
if (a->argc != 3) { |
| 539 |
return CLI_SHOWUSAGE; |
| 540 |
} |
| 541 |
|
| 542 |
ast_cli(a->fd, "\n"); |
| 543 |
ast_cli(a->fd, " %-40s %s\n", "Endpoint", "State"); |
| 544 |
ast_cli(a->fd, " %-40s -----\n", "----------------------------------------"); |
| 545 |
fd = a->fd; |
| 546 |
ao2_callback(maintenance_set, OBJ_NODATA, cli_maint_entry_cb, &fd); |
| 547 |
|
| 548 |
count = ao2_container_count(maintenance_set); |
| 549 |
ast_cli(a->fd, "\n %d endpoint%s in maintenance mode\n\n", |
| 550 |
count, count == 1 ? "" : "s"); |
| 551 |
|
| 552 |
return CLI_SUCCESS; |
| 553 |
} |
| 554 |
|
| 555 |
static struct ast_cli_entry cli_maintenance[] = { |
| 556 |
AST_CLI_DEFINE(handle_cli_pjsip_set_maintenance, "Set PJSIP endpoint maintenance mode"), |
| 557 |
AST_CLI_DEFINE(handle_cli_pjsip_show_maintenance, "Show PJSIP endpoint maintenance status"), |
| 558 |
}; |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
static int ami_set_maintenance(struct mansession *s, const struct message *m) |
| 563 |
{ |
| 564 |
const char *endpoint_name; |
| 565 |
const char *state_str; |
| 566 |
struct ast_sip_endpoint *endpoint; |
| 567 |
struct ao2_container *all_endpoints; |
| 568 |
struct ao2_iterator it; |
| 569 |
int enable; |
| 570 |
int rc; |
| 571 |
|
| 572 |
endpoint_name = astman_get_header(m, "Endpoint"); |
| 573 |
state_str = astman_get_header(m, "State"); |
| 574 |
|
| 575 |
if (ast_strlen_zero(endpoint_name)) { |
| 576 |
astman_send_error(s, m, "Endpoint parameter missing"); |
| 577 |
return 0; |
| 578 |
} |
| 579 |
if (ast_strlen_zero(state_str)) { |
| 580 |
astman_send_error(s, m, "State parameter missing"); |
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
if (!strcasecmp(state_str, "on")) { |
| 585 |
enable = 1; |
| 586 |
} else if (!strcasecmp(state_str, "off")) { |
| 587 |
enable = 0; |
| 588 |
} else { |
| 589 |
astman_send_error(s, m, "State must be 'on' or 'off'"); |
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
if (!strcasecmp(endpoint_name, "all")) { |
| 594 |
int count = 0; |
| 595 |
|
| 596 |
all_endpoints = ast_sip_get_endpoints(); |
| 597 |
if (!all_endpoints) { |
| 598 |
astman_send_error(s, m, "Failed to retrieve endpoint list"); |
| 599 |
return 0; |
| 600 |
} |
| 601 |
it = ao2_iterator_init(all_endpoints, 0); |
| 602 |
while ((endpoint = ao2_iterator_next(&it))) { |
| 603 |
if (apply_maintenance_state(ast_sorcery_object_get_id(endpoint), enable) > 0) { |
| 604 |
count++; |
| 605 |
} |
| 606 |
ao2_ref(endpoint, -1); |
| 607 |
} |
| 608 |
ao2_iterator_destroy(&it); |
| 609 |
ao2_ref(all_endpoints, -1); |
| 610 |
if (count > 0) { |
| 611 |
manager_event(EVENT_FLAG_SYSTEM, "PJSIPMaintenanceStatus", |
| 612 |
"Endpoint: all\r\n" |
| 613 |
"Status: %s\r\n", |
| 614 |
enable ? "enabled" : "disabled"); |
| 615 |
ast_log(LOG_NOTICE, "PJSIP: Maintenance mode %s for all endpoints " |
| 616 |
"(%d endpoint%s affected)\n", |
| 617 |
enable ? "enabled" : "disabled", |
| 618 |
count, count == 1 ? "" : "s"); |
| 619 |
} |
| 620 |
astman_send_ack(s, m, |
| 621 |
enable ? "Maintenance mode enabled for all endpoints" |
| 622 |
: "Maintenance mode disabled for all endpoints"); |
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name); |
| 627 |
if (!endpoint) { |
| 628 |
astman_send_error_va(s, m, "Endpoint '%s' not found", endpoint_name); |
| 629 |
return 0; |
| 630 |
} |
| 631 |
ao2_ref(endpoint, -1); |
| 632 |
|
| 633 |
rc = apply_maintenance_state(endpoint_name, enable); |
| 634 |
if (rc < 0) { |
| 635 |
astman_send_error_va(s, m, "Failed to %s maintenance mode for endpoint '%s'", |
| 636 |
enable ? "enable" : "disable", endpoint_name); |
| 637 |
} else { |
| 638 |
if (rc > 0) { |
| 639 |
manager_event(EVENT_FLAG_SYSTEM, "PJSIPMaintenanceStatus", |
| 640 |
"Endpoint: %s\r\n" |
| 641 |
"Status: %s\r\n", |
| 642 |
endpoint_name, enable ? "enabled" : "disabled"); |
| 643 |
ast_log(LOG_NOTICE, "PJSIP: Maintenance mode %s for endpoint '%s'\n", |
| 644 |
enable ? "enabled" : "disabled", endpoint_name); |
| 645 |
} |
| 646 |
astman_send_ack(s, m, |
| 647 |
enable ? "Maintenance mode enabled" : "Maintenance mode disabled"); |
| 648 |
} |
| 649 |
|
| 650 |
return 0; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
static int ami_maint_entry_cb(void *obj, void *arg, int flags) |
| 655 |
{ |
| 656 |
const char *name = obj; |
| 657 |
struct ast_sip_ami *ami = arg; |
| 658 |
struct ast_str *buf; |
| 659 |
|
| 660 |
buf = ast_sip_create_ami_event("PJSIPMaintenanceStatus", ami); |
| 661 |
if (!buf) { |
| 662 |
return 0; |
| 663 |
} |
| 664 |
ast_str_append(&buf, 0, "Endpoint: %s\r\nStatus: enabled\r\n", name); |
| 665 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 666 |
ast_free(buf); |
| 667 |
++ami->count; |
| 668 |
|
| 669 |
return 0; |
| 670 |
} |
| 671 |
|
| 672 |
static int ami_show_maintenance(struct mansession *s, const struct message *m) |
| 673 |
{ |
| 674 |
const char *endpoint_name; |
| 675 |
struct ast_sip_ami ami; |
| 676 |
char *entry; |
| 677 |
struct ast_str *buf; |
| 678 |
|
| 679 |
endpoint_name = astman_get_header(m, "Endpoint"); |
| 680 |
|
| 681 |
ami.s = s; |
| 682 |
ami.m = m; |
| 683 |
ami.action_id = astman_get_header(m, "ActionID"); |
| 684 |
ami.arg = NULL; |
| 685 |
ami.count = 0; |
| 686 |
|
| 687 |
astman_send_listack(s, m, "Maintenance status events follow", "start"); |
| 688 |
|
| 689 |
if (!ast_strlen_zero(endpoint_name)) { |
| 690 |
buf = ast_sip_create_ami_event("PJSIPMaintenanceStatus", &ami); |
| 691 |
if (buf) { |
| 692 |
entry = ao2_find(maintenance_set, endpoint_name, OBJ_SEARCH_KEY); |
| 693 |
ast_str_append(&buf, 0, "Endpoint: %s\r\nStatus: %s\r\n", |
| 694 |
endpoint_name, entry ? "enabled" : "disabled"); |
| 695 |
if (entry) { |
| 696 |
ao2_ref(entry, -1); |
| 697 |
} |
| 698 |
astman_append(s, "%s\r\n", ast_str_buffer(buf)); |
| 699 |
ast_free(buf); |
| 700 |
} |
| 701 |
ami.count = 1; |
| 702 |
} else { |
| 703 |
ao2_callback(maintenance_set, OBJ_NODATA, ami_maint_entry_cb, &ami); |
| 704 |
} |
| 705 |
|
| 706 |
astman_send_list_complete_start(s, m, "PJSIPMaintenanceStatusComplete", ami.count); |
| 707 |
astman_send_list_complete_end(s); |
| 708 |
|
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
static int load_module(void) |
| 715 |
{ |
| 716 |
maintenance_set = ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, |
| 717 |
MAINT_HASH_BUCKETS); |
| 718 |
if (!maintenance_set) { |
| 719 |
ast_log(LOG_ERROR, "res_pjsip_maintenance: failed to allocate maintenance set\n"); |
| 720 |
return AST_MODULE_LOAD_DECLINE; |
| 721 |
} |
| 722 |
|
| 723 |
ast_sorcery_observer_add(ast_sip_get_sorcery(), "endpoint", &endpoint_observer); |
| 724 |
ast_sip_register_service(&maintenance_pjsip_mod); |
| 725 |
ast_sip_session_register_supplement(&maintenance_session_supplement); |
| 726 |
ast_manager_register_xml("PJSIPSetMaintenance", |
| 727 |
EVENT_FLAG_SYSTEM, ami_set_maintenance); |
| 728 |
ast_manager_register_xml("PJSIPShowMaintenance", |
| 729 |
EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_show_maintenance); |
| 730 |
ast_cli_register_multiple(cli_maintenance, ARRAY_LEN(cli_maintenance)); |
| 731 |
|
| 732 |
return AST_MODULE_LOAD_SUCCESS; |
| 733 |
} |
| 734 |
|
| 735 |
static int unload_module(void) |
| 736 |
{ |
| 737 |
ast_cli_unregister_multiple(cli_maintenance, ARRAY_LEN(cli_maintenance)); |
| 738 |
ast_manager_unregister("PJSIPShowMaintenance"); |
| 739 |
ast_manager_unregister("PJSIPSetMaintenance"); |
| 740 |
ast_sip_session_unregister_supplement(&maintenance_session_supplement); |
| 741 |
ast_sip_unregister_service(&maintenance_pjsip_mod); |
| 742 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "endpoint", &endpoint_observer); |
| 743 |
ao2_cleanup(maintenance_set); |
| 744 |
maintenance_set = NULL; |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Endpoint Maintenance Mode", |
| 749 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 750 |
.load = load_module, |
| 751 |
.unload = unload_module, |
| 752 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 753 |
.requires = "res_pjsip,res_pjsip_session", |
| 754 |
); |