4 * Created on: Jan 25, 2013
13 #include "asterisk/res_pjsip.h"
14 #include "include/res_pjsip_private.h"
15 #include "asterisk/res_pjsip_cli.h"
16 #include "asterisk/acl.h"
17 #include "asterisk/manager.h"
18 #include "asterisk/astobj2.h"
19 #include "asterisk/utils.h"
20 #include "asterisk/sorcery.h"
21 #include "asterisk/callerid.h"
23 /*! \brief Number of buckets for persistent endpoint information */
24 #define PERSISTENT_BUCKETS 53
26 /*! \brief Persistent endpoint information */
27 struct sip_persistent_endpoint {
28 /*! \brief Asterisk endpoint itself */
29 struct ast_endpoint *endpoint;
30 /*! \brief AORs that we should react to */
34 /*! \brief Container for persistent endpoint information */
35 static struct ao2_container *persistent_endpoints;
37 static struct ast_sorcery *sip_sorcery;
39 /*! \brief Hashing function for persistent endpoint information */
40 static int persistent_endpoint_hash(const void *obj, const int flags)
42 const struct sip_persistent_endpoint *persistent = obj;
43 const char *id = (flags & OBJ_KEY ? obj : ast_endpoint_get_resource(persistent->endpoint));
45 return ast_str_hash(id);
48 /*! \brief Comparison function for persistent endpoint information */
49 static int persistent_endpoint_cmp(void *obj, void *arg, int flags)
51 const struct sip_persistent_endpoint *persistent1 = obj;
52 const struct sip_persistent_endpoint *persistent2 = arg;
53 const char *id = (flags & OBJ_KEY ? arg : ast_endpoint_get_resource(persistent2->endpoint));
55 return !strcmp(ast_endpoint_get_resource(persistent1->endpoint), id) ? CMP_MATCH | CMP_STOP : 0;
58 /*! \brief Callback function for changing the state of an endpoint */
59 static int persistent_endpoint_update_state(void *obj, void *arg, int flags)
61 struct sip_persistent_endpoint *persistent = obj;
63 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
64 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
66 if (!ast_strlen_zero(aor) && !strstr(persistent->aors, aor)) {
70 if ((contact = ast_sip_location_retrieve_contact_from_aor_list(persistent->aors))) {
71 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
72 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
74 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
75 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
78 ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
80 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
85 /*! \brief Function called when stuff relating to a contact happens (created/deleted) */
86 static void persistent_endpoint_contact_observer(const void *object)
88 char *id = ast_strdupa(ast_sorcery_object_get_id(object)), *aor = NULL;
90 aor = strsep(&id, ";@");
92 ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, aor);
95 /*! \brief Observer for contacts so state can be updated on respective endpoints */
96 static const struct ast_sorcery_observer state_contact_observer = {
97 .created = persistent_endpoint_contact_observer,
98 .deleted = persistent_endpoint_contact_observer,
102 static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
104 struct ast_sip_endpoint *endpoint = obj;
106 if (!strcasecmp(var->value, "rfc4733")) {
107 endpoint->dtmf = AST_SIP_DTMF_RFC_4733;
108 } else if (!strcasecmp(var->value, "inband")) {
109 endpoint->dtmf = AST_SIP_DTMF_INBAND;
110 } else if (!strcasecmp(var->value, "info")) {
111 endpoint->dtmf = AST_SIP_DTMF_INFO;
112 } else if (!strcasecmp(var->value, "none")) {
113 endpoint->dtmf = AST_SIP_DTMF_NONE;
121 static int dtmf_to_str(const void *obj, const intptr_t *args, char **buf)
123 const struct ast_sip_endpoint *endpoint = obj;
125 switch (endpoint->dtmf) {
126 case AST_SIP_DTMF_RFC_4733 :
127 *buf = "rfc4733"; break;
128 case AST_SIP_DTMF_INBAND :
129 *buf = "inband"; break;
130 case AST_SIP_DTMF_INFO :
131 *buf = "info"; break;
136 *buf = ast_strdup(*buf);
140 static int prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
142 struct ast_sip_endpoint *endpoint = obj;
144 if (ast_true(var->value)) {
145 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
146 } else if (ast_false(var->value)) {
147 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_100REL;
148 } else if (!strcasecmp(var->value, "required")) {
149 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
157 static int prack_to_str(const void *obj, const intptr_t *args, char **buf)
159 const struct ast_sip_endpoint *endpoint = obj;
161 if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_100REL) {
163 } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_100REL) {
169 *buf = ast_strdup(*buf);
173 static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
175 struct ast_sip_endpoint *endpoint = obj;
177 if (ast_true(var->value)) {
178 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
179 } else if (ast_false(var->value)) {
180 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_TIMER;
181 } else if (!strcasecmp(var->value, "required")) {
182 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
183 } else if (!strcasecmp(var->value, "always")) {
184 endpoint->extensions.flags |= PJSIP_INV_ALWAYS_USE_TIMER;
192 static int timers_to_str(const void *obj, const intptr_t *args, char **buf)
194 const struct ast_sip_endpoint *endpoint = obj;
196 if (endpoint->extensions.flags & PJSIP_INV_ALWAYS_USE_TIMER) {
198 } else if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_TIMER) {
200 } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_TIMER) {
206 *buf = ast_strdup(*buf);
210 void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths)
219 size = AST_VECTOR_SIZE(auths);
221 for (i = 0; i < size; ++i) {
222 const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0);
223 ast_free((char *) name);
225 AST_VECTOR_FREE(auths);
228 int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value)
230 char *auth_names = ast_strdupa(value);
233 ast_assert(auths != NULL);
235 if (AST_VECTOR_SIZE(auths)) {
236 ast_sip_auth_vector_destroy(auths);
238 if (AST_VECTOR_INIT(auths, 1)) {
242 while ((val = strsep(&auth_names, ","))) {
243 val = ast_strdup(val);
247 if (AST_VECTOR_APPEND(auths, val)) {
254 ast_sip_auth_vector_destroy(auths);
258 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
260 struct ast_sip_endpoint *endpoint = obj;
262 return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value);
265 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
267 struct ast_sip_endpoint *endpoint = obj;
269 return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value);
272 int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf)
274 if (!auths || !AST_VECTOR_SIZE(auths)) {
278 if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
282 /* I feel like accessing the vector's elem array directly is cheating...*/
283 ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ',');
287 static int inbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
289 const struct ast_sip_endpoint *endpoint = obj;
290 return ast_sip_auths_to_str(&endpoint->inbound_auths, buf);
293 static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
295 const struct ast_sip_endpoint *endpoint = obj;
296 return ast_sip_auths_to_str(&endpoint->outbound_auths, buf);
299 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
301 struct ast_sip_endpoint *endpoint = obj;
302 char *idents = ast_strdupa(var->value);
305 while ((val = strsep(&idents, ","))) {
306 if (!strcasecmp(val, "username")) {
307 endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
309 ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
310 val, ast_sorcery_object_get_id(endpoint));
317 static int ident_to_str(const void *obj, const intptr_t *args, char **buf)
319 const struct ast_sip_endpoint *endpoint = obj;
320 switch (endpoint->ident_method) {
321 case AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME :
322 *buf = "username"; break;
327 *buf = ast_strdup(*buf);
331 static int redirect_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
333 struct ast_sip_endpoint *endpoint = obj;
335 if (!strcasecmp(var->value, "user")) {
336 endpoint->redirect_method = AST_SIP_REDIRECT_USER;
337 } else if (!strcasecmp(var->value, "uri_core")) {
338 endpoint->redirect_method = AST_SIP_REDIRECT_URI_CORE;
339 } else if (!strcasecmp(var->value, "uri_pjsip")) {
340 endpoint->redirect_method = AST_SIP_REDIRECT_URI_PJSIP;
342 ast_log(LOG_ERROR, "Unrecognized redirect method %s specified for endpoint %s\n",
343 var->value, ast_sorcery_object_get_id(endpoint));
350 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
352 struct ast_sip_endpoint *endpoint = obj;
354 if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
355 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
356 } else if (!strcasecmp(var->value, "update")) {
357 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
359 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
360 var->value, var->name, ast_sorcery_object_get_id(endpoint));
366 static const char *id_configuration_refresh_methods[] = {
367 [AST_SIP_SESSION_REFRESH_METHOD_INVITE] = "invite",
368 [AST_SIP_SESSION_REFRESH_METHOD_UPDATE] = "update"
371 static int direct_media_method_to_str(const void *obj, const intptr_t *args, char **buf)
373 const struct ast_sip_endpoint *endpoint = obj;
374 if (ARRAY_IN_BOUNDS(endpoint->id.refresh_method, id_configuration_refresh_methods)) {
375 *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
380 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
382 struct ast_sip_endpoint *endpoint = obj;
384 if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
385 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
386 } else if (!strcasecmp(var->value, "update")) {
387 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
389 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
390 var->value, var->name, ast_sorcery_object_get_id(endpoint));
396 static int connected_line_method_to_str(const void *obj, const intptr_t *args, char **buf)
398 const struct ast_sip_endpoint *endpoint = obj;
399 *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
403 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
405 struct ast_sip_endpoint *endpoint = obj;
407 if (!strcasecmp(var->value, "none")) {
408 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
409 } else if (!strcasecmp(var->value, "outgoing")) {
410 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
411 } else if (!strcasecmp(var->value, "incoming")) {
412 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
414 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
415 var->value, var->name, ast_sorcery_object_get_id(endpoint));
422 static const char *direct_media_glare_mitigation_map[] = {
423 [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE] = "none",
424 [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING] = "outgoing",
425 [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING] = "incoming"
428 static int direct_media_glare_mitigation_to_str(const void *obj, const intptr_t *args, char **buf)
430 const struct ast_sip_endpoint *endpoint = obj;
431 if (ARRAY_IN_BOUNDS(endpoint->media.direct_media.glare_mitigation, direct_media_glare_mitigation_map)) {
432 *buf = ast_strdup(direct_media_glare_mitigation_map[endpoint->media.direct_media.glare_mitigation]);
438 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
440 struct ast_sip_endpoint *endpoint = obj;
441 char cid_name[80] = { '\0' };
442 char cid_num[80] = { '\0' };
444 ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
445 if (!ast_strlen_zero(cid_name)) {
446 endpoint->id.self.name.str = ast_strdup(cid_name);
447 if (!endpoint->id.self.name.str) {
450 endpoint->id.self.name.valid = 1;
452 if (!ast_strlen_zero(cid_num)) {
453 endpoint->id.self.number.str = ast_strdup(cid_num);
454 if (!endpoint->id.self.number.str) {
457 endpoint->id.self.number.valid = 1;
462 static int caller_id_to_str(const void *obj, const intptr_t *args, char **buf)
464 const struct ast_sip_endpoint *endpoint = obj;
465 const char *name = S_COR(endpoint->id.self.name.valid,
466 endpoint->id.self.name.str, NULL);
467 const char *number = S_COR(endpoint->id.self.number.valid,
468 endpoint->id.self.number.str, NULL);
470 /* make sure size is at least 10 - that should cover the "<unknown>"
471 case as well as any additional formatting characters added in
472 the name and/or number case. */
474 size += name ? strlen(name) : 0;
475 size += number ? strlen(number) : 0;
477 if (!(*buf = ast_calloc(size + 1, sizeof(char)))) {
481 ast_callerid_merge(*buf, size + 1, name, number, NULL);
485 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
487 struct ast_sip_endpoint *endpoint = obj;
488 int callingpres = ast_parse_caller_presentation(var->value);
489 if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
492 endpoint->id.self.number.presentation = callingpres;
493 endpoint->id.self.name.presentation = callingpres;
497 static int caller_id_privacy_to_str(const void *obj, const intptr_t *args, char **buf)
499 const struct ast_sip_endpoint *endpoint = obj;
500 const char *presentation = ast_named_caller_presentation(
501 endpoint->id.self.name.presentation);
503 *buf = ast_strdup(presentation);
507 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
509 struct ast_sip_endpoint *endpoint = obj;
510 endpoint->id.self.tag = ast_strdup(var->value);
511 return endpoint->id.self.tag ? 0 : -1;
514 static int caller_id_tag_to_str(const void *obj, const intptr_t *args, char **buf)
516 const struct ast_sip_endpoint *endpoint = obj;
517 *buf = ast_strdup(endpoint->id.self.tag);
521 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
523 struct ast_sip_endpoint *endpoint = obj;
525 if (!strcasecmp("no", var->value)) {
526 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
527 } else if (!strcasecmp("sdes", var->value)) {
528 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
529 } else if (!strcasecmp("dtls", var->value)) {
530 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
531 ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
539 static const char *media_encryption_map[] = {
540 [AST_SIP_MEDIA_TRANSPORT_INVALID] = "invalid",
541 [AST_SIP_MEDIA_ENCRYPT_NONE] = "none",
542 [AST_SIP_MEDIA_ENCRYPT_SDES] = "sdes",
543 [AST_SIP_MEDIA_ENCRYPT_DTLS] = "dtls",
546 static int media_encryption_to_str(const void *obj, const intptr_t *args, char **buf)
548 const struct ast_sip_endpoint *endpoint = obj;
549 if (ARRAY_IN_BOUNDS(endpoint->media.rtp.encryption, media_encryption_map)) {
550 *buf = ast_strdup(media_encryption_map[
551 endpoint->media.rtp.encryption]);
556 static int group_handler(const struct aco_option *opt,
557 struct ast_variable *var, void *obj)
559 struct ast_sip_endpoint *endpoint = obj;
561 if (!strncmp(var->name, "call_group", 10)) {
562 if (!(endpoint->pickup.callgroup = ast_get_group(var->value))) {
565 } else if (!strncmp(var->name, "pickup_group", 12)) {
566 if (!(endpoint->pickup.pickupgroup = ast_get_group(var->value))) {
576 static int callgroup_to_str(const void *obj, const intptr_t *args, char **buf)
578 const struct ast_sip_endpoint *endpoint = obj;
580 if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
584 ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.callgroup);
588 static int pickupgroup_to_str(const void *obj, const intptr_t *args, char **buf)
590 const struct ast_sip_endpoint *endpoint = obj;
592 if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
596 ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.pickupgroup);
600 static int named_groups_handler(const struct aco_option *opt,
601 struct ast_variable *var, void *obj)
603 struct ast_sip_endpoint *endpoint = obj;
605 if (!strncmp(var->name, "named_call_group", 16)) {
606 if (!(endpoint->pickup.named_callgroups =
607 ast_get_namedgroups(var->value))) {
610 } else if (!strncmp(var->name, "named_pickup_group", 18)) {
611 if (!(endpoint->pickup.named_pickupgroups =
612 ast_get_namedgroups(var->value))) {
622 static int named_callgroups_to_str(const void *obj, const intptr_t *args, char **buf)
624 const struct ast_sip_endpoint *endpoint = obj;
625 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
627 ast_print_namedgroups(&str, endpoint->pickup.named_callgroups);
628 *buf = ast_strdup(ast_str_buffer(str));
632 static int named_pickupgroups_to_str(const void *obj, const intptr_t *args, char **buf)
634 const struct ast_sip_endpoint *endpoint = obj;
635 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
637 ast_print_namedgroups(&str, endpoint->pickup.named_pickupgroups);
638 *buf = ast_strdup(ast_str_buffer(str));
642 static int dtls_handler(const struct aco_option *opt,
643 struct ast_variable *var, void *obj)
645 struct ast_sip_endpoint *endpoint = obj;
646 char *name = ast_strdupa(var->name);
647 char *front, *buf = name;
649 /* strip out underscores in the name */
650 front = strtok(buf, "_");
652 int size = strlen(front);
653 ast_copy_string(buf, front, size + 1);
655 front = strtok(NULL, "_");
658 return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, name, var->value);
661 static int dtlsverify_to_str(const void *obj, const intptr_t *args, char **buf)
663 const struct ast_sip_endpoint *endpoint = obj;
664 *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.verify));
668 static int dtlsrekey_to_str(const void *obj, const intptr_t *args, char **buf)
670 const struct ast_sip_endpoint *endpoint = obj;
673 buf, "%d", endpoint->media.rtp.dtls_cfg.rekey) >=0 ? 0 : -1;
676 static int dtlscertfile_to_str(const void *obj, const intptr_t *args, char **buf)
678 const struct ast_sip_endpoint *endpoint = obj;
679 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.certfile);
683 static int dtlsprivatekey_to_str(const void *obj, const intptr_t *args, char **buf)
685 const struct ast_sip_endpoint *endpoint = obj;
686 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.pvtfile);
690 static int dtlscipher_to_str(const void *obj, const intptr_t *args, char **buf)
692 const struct ast_sip_endpoint *endpoint = obj;
693 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cipher);
697 static int dtlscafile_to_str(const void *obj, const intptr_t *args, char **buf)
699 const struct ast_sip_endpoint *endpoint = obj;
700 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cafile);
704 static int dtlscapath_to_str(const void *obj, const intptr_t *args, char **buf)
706 const struct ast_sip_endpoint *endpoint = obj;
707 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.capath);
711 static const char *ast_rtp_dtls_setup_map[] = {
712 [AST_RTP_DTLS_SETUP_ACTIVE] = "active",
713 [AST_RTP_DTLS_SETUP_PASSIVE] = "passive",
714 [AST_RTP_DTLS_SETUP_ACTPASS] = "actpass",
715 [AST_RTP_DTLS_SETUP_HOLDCONN] = "holdconn",
718 static int dtlssetup_to_str(const void *obj, const intptr_t *args, char **buf)
720 const struct ast_sip_endpoint *endpoint = obj;
721 if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.default_setup, ast_rtp_dtls_setup_map)) {
722 *buf = ast_strdup(ast_rtp_dtls_setup_map[endpoint->media.rtp.dtls_cfg.default_setup]);
727 static int t38udptl_ec_handler(const struct aco_option *opt,
728 struct ast_variable *var, void *obj)
730 struct ast_sip_endpoint *endpoint = obj;
732 if (!strcmp(var->value, "none")) {
733 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
734 } else if (!strcmp(var->value, "fec")) {
735 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
736 } else if (!strcmp(var->value, "redundancy")) {
737 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
745 static const char *ast_t38_ec_modes_map[] = {
746 [UDPTL_ERROR_CORRECTION_NONE] = "none",
747 [UDPTL_ERROR_CORRECTION_FEC] = "fec",
748 [UDPTL_ERROR_CORRECTION_REDUNDANCY] = "redundancy"
751 static int t38udptl_ec_to_str(const void *obj, const intptr_t *args, char **buf)
753 const struct ast_sip_endpoint *endpoint = obj;
754 if (ARRAY_IN_BOUNDS(endpoint->media.t38.error_correction, ast_t38_ec_modes_map)) {
755 *buf = ast_strdup(ast_t38_ec_modes_map[
756 endpoint->media.t38.error_correction]);
761 static int tos_handler(const struct aco_option *opt,
762 struct ast_variable *var, void *obj)
764 struct ast_sip_endpoint *endpoint = obj;
767 if (ast_str2tos(var->value, &value)) {
768 ast_log(LOG_ERROR, "Error configuring endpoint '%s' - Could not "
769 "interpret '%s' value '%s'\n",
770 ast_sorcery_object_get_id(endpoint), var->name, var->value);
774 if (!strcmp(var->name, "tos_audio")) {
775 endpoint->media.tos_audio = value;
776 } else if (!strcmp(var->name, "tos_video")) {
777 endpoint->media.tos_video = value;
779 /* If we reach this point, someone called the tos_handler when they shouldn't have. */
786 static int tos_audio_to_str(const void *obj, const intptr_t *args, char **buf)
788 const struct ast_sip_endpoint *endpoint = obj;
790 if (ast_asprintf(buf, "%d", endpoint->media.tos_audio) == -1) {
796 static int tos_video_to_str(const void *obj, const intptr_t *args, char **buf)
798 const struct ast_sip_endpoint *endpoint = obj;
800 if (ast_asprintf(buf, "%d", endpoint->media.tos_video) == -1) {
806 static int set_var_handler(const struct aco_option *opt,
807 struct ast_variable *var, void *obj)
809 struct ast_sip_endpoint *endpoint = obj;
810 struct ast_variable *new_var;
811 char *name = ast_strdupa(var->value), *val = strchr(name, '=');
819 if (!(new_var = ast_variable_new(name, val, ""))) {
823 ast_variable_list_append(&endpoint->channel_vars, new_var);
828 static int set_var_to_str(const void *obj, const intptr_t *args, char **buf)
830 struct ast_str *str = ast_str_create(MAX_OBJECT_FIELD);
831 const struct ast_sip_endpoint *endpoint = obj;
832 struct ast_variable *var;
834 for (var = endpoint->channel_vars; var; var = var->next) {
835 ast_str_append(&str, 0, "%s=%s,", var->name, var->value);
838 *buf = ast_strdup(ast_str_truncate(str, -1));
843 static int set_var_to_vl(const void *obj, struct ast_variable **fields)
845 const struct ast_sip_endpoint *endpoint = obj;
846 if (endpoint->channel_vars) {
847 *fields = ast_variables_dup(endpoint->channel_vars);
853 static void *sip_nat_hook_alloc(const char *name)
855 return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
858 /*! \brief Destructor function for persistent endpoint information */
859 static void persistent_endpoint_destroy(void *obj)
861 struct sip_persistent_endpoint *persistent = obj;
863 ast_endpoint_shutdown(persistent->endpoint);
864 ast_free(persistent->aors);
867 /*! \brief Internal function which finds (or creates) persistent endpoint information */
868 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
870 RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
871 SCOPED_AO2LOCK(lock, persistent_endpoints);
873 if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
874 if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
878 if (!(persistent->endpoint = ast_endpoint_create("PJSIP", ast_sorcery_object_get_id(endpoint)))) {
882 persistent->aors = ast_strdup(endpoint->aors);
884 if (ast_strlen_zero(persistent->aors)) {
885 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
887 persistent_endpoint_update_state(persistent, NULL, 0);
890 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
893 ao2_ref(persistent->endpoint, +1);
894 return persistent->endpoint;
897 /*! \brief Helper function which validates an outbound proxy */
898 static int outbound_proxy_validate(void *data)
900 const char *proxy = data;
903 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
905 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound Proxy Validation", 256, 256);
910 pj_strdup2_with_null(pool, &tmp, proxy);
911 if (!pjsip_parse_hdr(pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL)) {
912 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
916 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
920 /*! \brief Callback function for when an object is finalized */
921 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
923 struct ast_sip_endpoint *endpoint = obj;
925 if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
929 if (!ast_strlen_zero(endpoint->outbound_proxy) &&
930 ast_sip_push_task_synchronous(NULL, outbound_proxy_validate, (char*)endpoint->outbound_proxy)) {
931 ast_log(LOG_ERROR, "Invalid outbound proxy '%s' specified on endpoint '%s'\n",
932 endpoint->outbound_proxy, ast_sorcery_object_get_id(endpoint));
939 const char *ast_sip_get_device_state(const struct ast_sip_endpoint *endpoint)
941 char device[MAX_OBJECT_FIELD];
943 snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
944 return ast_devstate2str(ast_device_state(device));
947 struct ast_endpoint_snapshot *ast_sip_get_endpoint_snapshot(
948 const struct ast_sip_endpoint *endpoint)
950 return ast_endpoint_latest_snapshot(
951 ast_endpoint_get_tech(endpoint->persistent),
952 ast_endpoint_get_resource(endpoint->persistent));
955 int ast_sip_for_each_channel_snapshot(
956 const struct ast_endpoint_snapshot *endpoint_snapshot,
957 ao2_callback_fn on_channel_snapshot, void *arg)
959 int num, num_channels = endpoint_snapshot->num_channels;
961 if (!on_channel_snapshot || !num_channels) {
965 for (num = 0; num < num_channels; ++num) {
966 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
969 snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
974 res = on_channel_snapshot(snapshot, arg, 0);
982 int ast_sip_for_each_channel(
983 const struct ast_sip_endpoint *endpoint,
984 ao2_callback_fn on_channel_snapshot, void *arg)
986 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
987 return ast_sip_for_each_channel_snapshot(endpoint_snapshot, on_channel_snapshot, arg);
990 static int active_channels_to_str_cb(void *object, void *arg, int flags)
992 const struct ast_channel_snapshot *snapshot = object;
993 struct ast_str **buf = arg;
994 ast_str_append(buf, 0, "%s,", snapshot->name);
998 static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
999 struct ast_str **str)
1002 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
1003 ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1005 if (endpoint_snapshot) {
1009 ast_sip_for_each_channel_snapshot(endpoint_snapshot,
1010 active_channels_to_str_cb, str);
1011 ast_str_truncate(*str, -1);
1014 #define AMI_DEFAULT_STR_SIZE 512
1016 struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
1018 struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
1021 astman_send_error_va(ami->s, ami->m, "Unable create event "
1026 ast_str_set(&buf, 0, "Event: %s\r\n", event);
1030 static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
1032 ast_str_append(buf, 0, "ObjectType: %s\r\n",
1033 ast_sorcery_object_get_type(obj));
1034 ast_str_append(buf, 0, "ObjectName: %s\r\n",
1035 ast_sorcery_object_get_id(obj));
1038 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
1040 RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
1041 ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
1042 struct ast_variable *i;
1048 sip_sorcery_object_ami_set_type_name(obj, buf);
1050 for (i = objset; i; i = i->next) {
1051 RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
1052 ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
1058 static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
1060 struct ast_sip_aor *aor = obj;
1061 struct ast_str **buf = arg;
1063 ast_str_append(buf, 0, "Contacts: ");
1064 ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
1065 ast_str_append(buf, 0, "\r\n");
1070 static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
1071 struct ast_str **buf)
1073 if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
1077 ast_str_append(buf, 0, "DeviceState: %s\r\n",
1078 ast_sip_get_device_state(endpoint));
1080 ast_str_append(buf, 0, "ActiveChannels: ");
1081 active_channels_to_str(endpoint, buf);
1082 ast_str_append(buf, 0, "\r\n");
1087 static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
1088 struct ast_sip_ami *ami)
1090 RAII_VAR(struct ast_str *, buf,
1091 ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
1097 sip_endpoint_to_ami(endpoint, &buf);
1098 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1102 #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
1103 #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
1105 static int ami_show_endpoint(struct mansession *s, const struct message *m)
1107 struct ast_sip_ami ami = { .s = s, .m = m };
1108 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1109 const char *endpoint_name = astman_get_header(m, "Endpoint");
1112 if (ast_strlen_zero(endpoint_name)) {
1113 astman_send_error_va(s, m, "%s requires an endpoint name\n",
1118 if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
1122 if (!(endpoint = ast_sorcery_retrieve_by_id(
1123 ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1124 astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
1129 astman_send_listack(s, m, "Following are Events for each object "
1130 "associated with the the Endpoint", "start");
1132 /* the endpoint detail needs to always come first so apply as such */
1133 if (format_ami_endpoint(endpoint, &ami) ||
1134 ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
1135 astman_send_error_va(s, m, "Unable to format endpoint %s\n",
1140 "Event: EndpointDetailComplete\r\n"
1141 "EventList: Complete\r\n"
1142 "ListItems: %d\r\n\r\n", count + 1);
1146 static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
1147 struct ast_str **buf)
1150 if (ast_sip_auths_to_str(auths, &str)) {
1153 ast_str_append(buf, 0, "%s", str ? str : "");
1158 static int format_ami_endpoints(void *obj, void *arg, int flags)
1161 struct ast_sip_endpoint *endpoint = obj;
1162 struct ast_sip_ami *ami = arg;
1163 RAII_VAR(struct ast_str *, buf,
1164 ast_sip_create_ami_event("EndpointList", ami), ast_free);
1170 sip_sorcery_object_ami_set_type_name(endpoint, &buf);
1171 ast_str_append(&buf, 0, "Transport: %s\r\n",
1172 endpoint->transport);
1173 ast_str_append(&buf, 0, "Aor: %s\r\n",
1176 ast_str_append(&buf, 0, "Auths: ");
1177 format_str_append_auth(&endpoint->inbound_auths, &buf);
1178 ast_str_append(&buf, 0, "\r\n");
1180 ast_str_append(&buf, 0, "OutboundAuths: ");
1181 format_str_append_auth(&endpoint->outbound_auths, &buf);
1182 ast_str_append(&buf, 0, "\r\n");
1184 ast_sip_for_each_aor(endpoint->aors,
1185 sip_endpoints_aors_ami, &buf);
1187 ast_str_append(&buf, 0, "DeviceState: %s\r\n",
1188 ast_sip_get_device_state(endpoint));
1190 ast_str_append(&buf, 0, "ActiveChannels: ");
1191 active_channels_to_str(endpoint, &buf);
1192 ast_str_append(&buf, 0, "\r\n");
1194 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1198 static int ami_show_endpoints(struct mansession *s, const struct message *m)
1200 struct ast_sip_ami ami = { .s = s, .m = m };
1201 RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
1204 endpoints = ast_sip_get_endpoints();
1209 if (!(num = ao2_container_count(endpoints))) {
1210 astman_send_error(s, m, "No endpoints found\n");
1214 astman_send_listack(s, m, "A listing of Endpoints follows, "
1215 "presented as EndpointList events", "start");
1217 ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
1220 "Event: EndpointListComplete\r\n"
1221 "EventList: Complete\r\n"
1222 "ListItems: %d\r\n\r\n", num);
1226 static struct ao2_container *cli_endpoint_get_container(void)
1228 RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
1229 struct ao2_container *s_container;
1231 container = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint",
1232 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1237 s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
1238 (void *)ast_sorcery_object_id_sort, (void *)ast_sorcery_object_id_compare);
1243 if (ao2_container_dup(s_container, container, 0)) {
1244 ao2_ref(s_container, -1);
1251 static int cli_channel_populate_container(void *obj, void *arg, int flags)
1253 struct ast_channel_snapshot *snapshot = obj;
1255 ao2_link(arg, snapshot);
1260 static int cli_channel_iterate(void *container, ao2_callback_fn callback, void *args)
1262 const struct ast_sip_endpoint *endpoint = container;
1264 ast_sip_for_each_channel(endpoint, callback, args);
1269 static int cli_channel_sort(const void *obj, const void *arg, int flags)
1271 const struct ast_channel_snapshot *left_obj = obj;
1272 const struct ast_channel_snapshot *right_obj = arg;
1273 const char *right_key = arg;
1276 switch (flags & OBJ_SEARCH_MASK) {
1277 case OBJ_SEARCH_OBJECT:
1278 right_key = right_obj->name;
1280 case OBJ_SEARCH_KEY:
1281 cmp = strcmp(left_obj->name, right_key);
1283 case OBJ_SEARCH_PARTIAL_KEY:
1284 cmp = strncmp(left_obj->name, right_key, strlen(right_key));
1294 static int cli_channel_compare(void *obj, void *arg, int flags)
1296 const struct ast_channel_snapshot *left_obj = obj;
1297 const struct ast_channel_snapshot *right_obj = arg;
1298 const char *right_key = arg;
1301 switch (flags & OBJ_SEARCH_MASK) {
1302 case OBJ_SEARCH_OBJECT:
1303 right_key = right_obj->name;
1305 case OBJ_SEARCH_KEY:
1306 if (strcmp(left_obj->name, right_key) == 0) {
1307 cmp = CMP_MATCH | CMP_STOP;
1310 case OBJ_SEARCH_PARTIAL_KEY:
1311 if (strncmp(left_obj->name, right_key, strlen(right_key)) == 0) {
1323 static int cli_channel_hash(const void *obj, int flags)
1325 const struct ast_channel_snapshot *snapshot = obj;
1327 if (flags & OBJ_SEARCH_OBJECT) {
1328 return ast_str_hash(snapshot->name);
1329 } else if (flags & OBJ_SEARCH_KEY) {
1330 return ast_str_hash(obj);
1336 static int cli_endpoint_gather_channels(void *obj, void *arg, int flags)
1338 struct ast_sip_endpoint *endpoint = obj;
1339 struct ao2_container *channels = arg;
1341 ast_sip_for_each_channel(endpoint, cli_channel_populate_container, channels);
1346 static struct ao2_container *cli_channel_get_container(void)
1348 RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);
1349 struct ao2_container *child_container;
1351 parent_container = cli_endpoint_get_container();
1352 if (!parent_container) {
1355 child_container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 17,
1356 cli_channel_hash, cli_channel_sort, cli_channel_compare);
1357 if (!child_container) {
1361 ao2_callback(parent_container, OBJ_NODATA, cli_endpoint_gather_channels, child_container);
1363 return child_container;
1366 static const char *cli_channel_get_id(const void *obj)
1368 const struct ast_channel_snapshot *snapshot = obj;
1370 return snapshot->name;
1373 static void *cli_channel_retrieve_by_id(const char *id)
1375 RAII_VAR(struct ao2_container *, container, cli_channel_get_container(), ao2_cleanup);
1377 return ao2_find(container, id, OBJ_KEY | OBJ_NOLOCK);
1380 static int cli_channel_print_header(void *obj, void *arg, int flags)
1382 struct ast_sip_cli_context *context = arg;
1383 int indent = CLI_INDENT_TO_SPACES(context->indent_level);
1384 int filler = CLI_LAST_TABSTOP - indent - 13;
1386 ast_assert(context->output_buffer != NULL);
1388 ast_str_append(&context->output_buffer, 0,
1389 "%*s: <ChannelId%*.*s> <State.....> <Time(sec)>\n",
1390 indent, "Channel", filler, filler, CLI_HEADER_FILLER);
1391 if (context->recurse) {
1392 context->indent_level++;
1393 indent = CLI_INDENT_TO_SPACES(context->indent_level);
1394 filler = CLI_LAST_TABSTOP - indent - 38;
1395 ast_str_append(&context->output_buffer, 0,
1396 "%*s: <Codec> Exten: <DialedExten%*.*s> CLCID: <ConnectedLineCID.......>\n",
1397 indent, "Codec", filler, filler, CLI_HEADER_FILLER);
1398 context->indent_level--;
1404 static int cli_channel_print_body(void *obj, void *arg, int flags)
1406 const struct ast_channel_snapshot *snapshot = obj;
1407 struct ast_sip_cli_context *context = arg;
1408 struct timeval current_time;
1409 char *print_name = NULL;
1414 ast_assert(context->output_buffer != NULL);
1416 gettimeofday(¤t_time, NULL);
1418 print_name_len = strlen(snapshot->name) + strlen(snapshot->appl) + 2;
1419 if (!(print_name = alloca(print_name_len))) {
1423 snprintf(print_name, print_name_len, "%s/%s", snapshot->name, snapshot->appl);
1425 indent = CLI_INDENT_TO_SPACES(context->indent_level);
1426 flexwidth = CLI_LAST_TABSTOP - indent;
1428 ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %11ld\n",
1429 CLI_INDENT_TO_SPACES(context->indent_level), "Channel",
1430 flexwidth, flexwidth,
1432 ast_state2str(snapshot->state),
1433 current_time.tv_sec - snapshot->creationtime.tv_sec);
1435 if (context->recurse) {
1436 context->indent_level++;
1437 indent = CLI_INDENT_TO_SPACES(context->indent_level);
1438 flexwidth = CLI_LAST_TABSTOP - indent - 25;
1440 ast_str_append(&context->output_buffer, 0,
1441 "%*s: %-7s Exten: %-*.*s CLCID: \"%s\" <%s>\n",
1443 snapshot->nativeformats,
1444 flexwidth, flexwidth,
1446 snapshot->connected_name,
1447 snapshot->connected_number
1449 context->indent_level--;
1450 if (context->indent_level == 0) {
1451 ast_str_append(&context->output_buffer, 0, "\n");
1458 static int cli_endpoint_iterate(void *obj, ao2_callback_fn callback, void *args)
1460 ao2_callback(obj, OBJ_NODATA, callback, args);
1465 static void *cli_endpoint_retrieve_by_id(const char *id)
1467 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
1470 static void cli_endpoint_print_child_header(char *type, struct ast_sip_cli_context *context)
1472 RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1474 formatter_entry = ast_sip_lookup_cli_formatter(type);
1475 if (formatter_entry) {
1476 formatter_entry->print_header(NULL, context, 0);
1480 static int cli_endpoint_print_header(void *obj, void *arg, int flags)
1482 struct ast_sip_cli_context *context = arg;
1484 ast_assert(context->output_buffer != NULL);
1486 ast_str_append(&context->output_buffer, 0,
1487 " Endpoint: <Endpoint/CID.....................................> <State.....> <Channels.>\n");
1489 if (context->recurse) {
1490 context->indent_level++;
1491 cli_endpoint_print_child_header("auth", context);
1492 cli_endpoint_print_child_header("aor", context);
1493 cli_endpoint_print_child_header("transport", context);
1494 cli_endpoint_print_child_header("identify", context);
1495 cli_endpoint_print_child_header("channel", context);
1496 context->indent_level--;
1502 static void cli_endpoint_print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context)
1504 RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1506 formatter_entry = ast_sip_lookup_cli_formatter(type);
1507 if (formatter_entry) {
1508 formatter_entry->iterate((void *)obj, formatter_entry->print_body, context);
1512 static int cli_endpoint_print_body(void *obj, void *arg, int flags)
1514 struct ast_sip_endpoint *endpoint = obj;
1515 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1516 struct ast_sip_cli_context *context = arg;
1517 const char *id = ast_sorcery_object_get_id(endpoint);
1518 char *print_name = NULL;
1520 char *number = S_COR(endpoint->id.self.number.valid,
1521 endpoint->id.self.number.str, NULL);
1525 ast_assert(context->output_buffer != NULL);
1528 print_name_len = strlen(id) + strlen(number) + 2;
1529 if (!(print_name = alloca(print_name_len))) {
1532 snprintf(print_name, print_name_len, "%s/%s", id, number);
1535 indent = CLI_INDENT_TO_SPACES(context->indent_level);
1536 flexwidth = CLI_LAST_TABSTOP - indent - 2;
1538 ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %d of %.0f\n",
1540 flexwidth, flexwidth, print_name ? print_name : id,
1541 ast_sip_get_device_state(endpoint),
1542 endpoint_snapshot->num_channels,
1543 (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at :
1547 if (context->recurse) {
1548 context->indent_level++;
1550 context->auth_direction = "Out";
1551 cli_endpoint_print_child_body("auth", &endpoint->outbound_auths, context);
1552 context->auth_direction = "In";
1553 cli_endpoint_print_child_body("auth", &endpoint->inbound_auths, context);
1555 cli_endpoint_print_child_body("aor", endpoint->aors, context);
1556 cli_endpoint_print_child_body("transport", endpoint, context);
1557 cli_endpoint_print_child_body("identify", endpoint, context);
1558 cli_endpoint_print_child_body("channel", endpoint, context);
1560 context->indent_level--;
1562 if (context->indent_level == 0) {
1563 ast_str_append(&context->output_buffer, 0, "\n");
1567 if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
1568 ast_str_append(&context->output_buffer, 0, "\n");
1569 ast_sip_cli_print_sorcery_objectset(endpoint, context, 0);
1575 static struct ast_cli_entry cli_commands[] = {
1576 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Channels",
1577 .command = "pjsip list channels",
1578 .usage = "Usage: pjsip list channels\n"
1579 " List the active PJSIP channels\n"),
1580 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Channels",
1581 .command = "pjsip show channels",
1582 .usage = "Usage: pjsip show channels\n"
1583 " List(detailed) the active PJSIP channels\n"),
1584 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Channel",
1585 .command = "pjsip show channel",
1586 .usage = "Usage: pjsip show channel\n"
1587 " List(detailed) the active PJSIP channel\n"),
1589 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints",
1590 .command = "pjsip list endpoints",
1591 .usage = "Usage: pjsip list endpoints\n"
1592 " List the configured PJSIP endpoints\n"),
1593 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints",
1594 .command = "pjsip show endpoints",
1595 .usage = "Usage: pjsip show endpoints\n"
1596 " List(detailed) the configured PJSIP endpoints\n"),
1597 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint",
1598 .command = "pjsip show endpoint",
1599 .usage = "Usage: pjsip show endpoint <id>\n"
1600 " Show the configured PJSIP endpoint\n"),
1603 struct ast_sip_cli_formatter_entry *channel_formatter;
1604 struct ast_sip_cli_formatter_entry *endpoint_formatter;
1606 int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_module_info)
1608 if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
1609 ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
1613 if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
1617 if (!(sip_sorcery = ast_sorcery_open())) {
1618 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
1622 ast_sorcery_apply_config(sip_sorcery, "res_pjsip");
1624 ast_sip_initialize_cli();
1626 if (ast_sip_initialize_sorcery_auth()) {
1627 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
1628 ast_sorcery_unref(sip_sorcery);
1633 ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
1634 ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
1636 if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
1637 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
1638 ast_sorcery_unref(sip_sorcery);
1643 ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL);
1645 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
1646 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
1647 ast_sorcery_object_field_register_alias(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
1648 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
1649 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, NULL, 0, 0);
1650 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
1651 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
1652 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
1653 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
1654 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
1655 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
1656 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
1657 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
1658 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
1659 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, NULL, 0, 0);
1660 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, NULL, 0, 0);
1661 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_min_se", "90", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, extensions.timer.min_se));
1662 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_sess_expires", "1800", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, extensions.timer.sess_expires));
1663 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, NULL, 0, 0);
1664 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, NULL, 0, 0);
1665 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
1666 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
1667 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username", ident_handler, ident_to_str, NULL, 0, 0);
1668 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
1669 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, direct_media_method_to_str, NULL, 0, 0);
1670 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, connected_line_method_to_str, NULL, 0, 0);
1671 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, direct_media_glare_mitigation_to_str, NULL, 0, 0);
1672 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disable_direct_media_on_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.disable_on_nat));
1673 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, NULL, 0, 0);
1674 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "", caller_id_privacy_handler, caller_id_privacy_to_str, NULL, 0, 0);
1675 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, NULL, 0, 0);
1676 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
1677 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
1678 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
1679 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
1680 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
1681 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
1682 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
1683 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, NULL, 0, 0);
1684 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
1685 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
1686 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
1687 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
1688 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, NULL, 0, 0);
1689 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, NULL, 0, 0);
1690 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, NULL, 0, 0);
1691 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "device_state_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at));
1692 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
1693 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, NULL, 0, 0);
1694 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
1695 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
1696 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
1697 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
1698 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
1699 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
1700 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
1701 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
1702 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
1703 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
1704 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
1705 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_audio", "0", tos_handler, tos_audio_to_str, NULL, 0, 0);
1706 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_video", "0", tos_handler, tos_video_to_str, NULL, 0, 0);
1707 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
1708 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
1709 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
1710 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
1711 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
1712 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
1713 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
1714 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
1715 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "", dtls_handler, dtlsverify_to_str, NULL, 0, 0);
1716 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "", dtls_handler, dtlsrekey_to_str, NULL, 0, 0);
1717 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, NULL, 0, 0);
1718 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, NULL, 0, 0);
1719 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, NULL, 0, 0);
1720 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, NULL, 0, 0);
1721 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, NULL, 0, 0);
1722 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, NULL, 0, 0);
1723 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "srtp_tag_32", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.srtp_tag_32));
1724 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, NULL, 0, 0);
1725 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, set_var_to_vl, 0, 0);
1727 if (ast_sip_initialize_sorcery_transport()) {
1728 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
1729 ast_sorcery_unref(sip_sorcery);
1734 if (ast_sip_initialize_sorcery_location()) {
1735 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
1736 ast_sorcery_unref(sip_sorcery);
1741 if (ast_sip_initialize_sorcery_qualify()) {
1742 ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
1743 ast_sorcery_unref(sip_sorcery);
1748 ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
1750 if (ast_sip_initialize_sorcery_domain_alias()) {
1751 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
1752 ast_sorcery_unref(sip_sorcery);
1757 if (ast_sip_initialize_sorcery_global()) {
1758 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
1759 ast_sorcery_unref(sip_sorcery);
1764 channel_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1765 if (!channel_formatter) {
1766 ast_log(LOG_ERROR, "Unable to allocate memory for channel_formatter\n");
1767 ast_sorcery_unref(sip_sorcery);
1771 channel_formatter->name = "channel";
1772 channel_formatter->print_header = cli_channel_print_header;
1773 channel_formatter->print_body = cli_channel_print_body;
1774 channel_formatter->get_container = cli_channel_get_container;
1775 channel_formatter->iterate = cli_channel_iterate;
1776 channel_formatter->retrieve_by_id = cli_channel_retrieve_by_id;
1777 channel_formatter->get_id = cli_channel_get_id;
1779 endpoint_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1780 if (!endpoint_formatter) {
1781 ast_log(LOG_ERROR, "Unable to allocate memory for endpoint_formatter\n");
1782 ast_sorcery_unref(sip_sorcery);
1786 endpoint_formatter->name = "endpoint";
1787 endpoint_formatter->print_header = cli_endpoint_print_header;
1788 endpoint_formatter->print_body = cli_endpoint_print_body;
1789 endpoint_formatter->get_container = cli_endpoint_get_container;
1790 endpoint_formatter->iterate = cli_endpoint_iterate;
1791 endpoint_formatter->retrieve_by_id = cli_endpoint_retrieve_by_id;
1792 endpoint_formatter->get_id = ast_sorcery_object_get_id;
1794 ast_sip_register_cli_formatter(channel_formatter);
1795 ast_sip_register_cli_formatter(endpoint_formatter);
1796 ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
1798 ast_sorcery_load(sip_sorcery);
1803 void ast_res_pjsip_destroy_configuration(void)
1805 ast_sip_destroy_sorcery_location();
1806 ast_sip_destroy_sorcery_auth();
1807 ast_sip_destroy_sorcery_transport();
1808 ast_manager_unregister(AMI_SHOW_ENDPOINT);
1809 ast_manager_unregister(AMI_SHOW_ENDPOINTS);
1810 ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1811 ast_sip_unregister_cli_formatter(endpoint_formatter);
1812 ast_sip_unregister_cli_formatter(channel_formatter);
1813 ast_sorcery_unref(sip_sorcery);
1818 * \brief Reload configuration within a PJSIP thread
1820 static int reload_configuration_task(void *obj)
1823 ast_sorcery_reload(sip_sorcery);
1828 int ast_res_pjsip_reload_configuration(void)
1830 if (ast_sip_push_task(NULL, reload_configuration_task, NULL)) {
1831 ast_log(LOG_WARNING, "Failed to reload PJSIP configuration\n");
1837 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
1839 ast_string_field_free_memory(&subscription->mwi);
1842 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
1844 ast_string_field_free_memory(&info->recording);
1847 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
1849 ast_string_field_free_memory(&media->rtp);
1850 ast_string_field_free_memory(media);
1853 static void endpoint_destructor(void* obj)
1855 struct ast_sip_endpoint *endpoint = obj;
1857 ast_string_field_free_memory(endpoint);
1859 if (endpoint->media.codecs) {
1860 ast_format_cap_destroy(endpoint->media.codecs);
1862 subscription_configuration_destroy(&endpoint->subscription);
1863 info_configuration_destroy(&endpoint->info);
1864 media_configuration_destroy(&endpoint->media);
1865 ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
1866 ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
1867 ast_party_id_free(&endpoint->id.self);
1868 endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
1869 endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
1870 ao2_cleanup(endpoint->persistent);
1871 ast_variables_destroy(endpoint->channel_vars);
1874 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
1876 return ast_string_field_init(&subscription->mwi, 64);
1879 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
1881 return ast_string_field_init(&info->recording, 32);
1884 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
1886 return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
1889 void *ast_sip_endpoint_alloc(const char *name)
1891 struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
1895 if (ast_string_field_init(endpoint, 64)) {
1896 ao2_cleanup(endpoint);
1899 if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
1900 ao2_cleanup(endpoint);
1903 if (init_subscription_configuration(&endpoint->subscription)) {
1904 ao2_cleanup(endpoint);
1907 if (init_info_configuration(&endpoint->info)) {
1908 ao2_cleanup(endpoint);
1911 if (init_media_configuration(&endpoint->media)) {
1912 ao2_cleanup(endpoint);
1915 ast_party_id_init(&endpoint->id.self);
1919 struct ao2_container *ast_sip_get_endpoints(void)
1921 struct ao2_container *endpoints;
1923 endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1928 struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
1930 RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
1931 return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
1932 sip_sorcery, "endpoint", name);
1935 int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
1939 for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
1940 /* Using AST_VECTOR_GET is safe since the vector is immutable */
1941 const char *name = AST_VECTOR_GET(auths, i);
1942 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
1944 ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
1952 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
1955 for (i = 0; i < num_auths; ++i) {
1956 ao2_cleanup(auths[i]);
1960 struct ast_sorcery *ast_sip_get_sorcery(void)