4 * Created on: Jan 25, 2013
13 #include "asterisk/res_pjsip.h"
14 #include "include/res_pjsip_private.h"
15 #include "asterisk/cli.h"
16 #include "asterisk/astobj2.h"
17 #include "asterisk/utils.h"
18 #include "asterisk/sorcery.h"
19 #include "asterisk/callerid.h"
20 #include "asterisk/stasis_endpoints.h"
22 /*! \brief Number of buckets for persistent endpoint information */
23 #define PERSISTENT_BUCKETS 53
25 /*! \brief Persistent endpoint information */
26 struct sip_persistent_endpoint {
27 /*! \brief Asterisk endpoint itself */
28 struct ast_endpoint *endpoint;
29 /*! \brief AORs that we should react to */
33 /*! \brief Container for persistent endpoint information */
34 static struct ao2_container *persistent_endpoints;
36 static struct ast_sorcery *sip_sorcery;
38 /*! \brief Hashing function for persistent endpoint information */
39 static int persistent_endpoint_hash(const void *obj, const int flags)
41 const struct sip_persistent_endpoint *persistent = obj;
42 const char *id = (flags & OBJ_KEY ? obj : ast_endpoint_get_resource(persistent->endpoint));
44 return ast_str_hash(id);
47 /*! \brief Comparison function for persistent endpoint information */
48 static int persistent_endpoint_cmp(void *obj, void *arg, int flags)
50 const struct sip_persistent_endpoint *persistent1 = obj;
51 const struct sip_persistent_endpoint *persistent2 = arg;
52 const char *id = (flags & OBJ_KEY ? arg : ast_endpoint_get_resource(persistent2->endpoint));
54 return !strcmp(ast_endpoint_get_resource(persistent1->endpoint), id) ? CMP_MATCH | CMP_STOP : 0;
57 /*! \brief Callback function for changing the state of an endpoint */
58 static int persistent_endpoint_update_state(void *obj, void *arg, int flags)
60 struct sip_persistent_endpoint *persistent = obj;
62 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
63 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
65 if (!ast_strlen_zero(aor) && !strstr(persistent->aors, aor)) {
69 if ((contact = ast_sip_location_retrieve_contact_from_aor_list(persistent->aors))) {
70 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
71 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
73 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
74 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
77 ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
79 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
84 /*! \brief Function called when stuff relating to a contact happens (created/deleted) */
85 static void persistent_endpoint_contact_observer(const void *object)
87 char *id = ast_strdupa(ast_sorcery_object_get_id(object)), *aor = NULL;
89 aor = strsep(&id, ";@");
91 ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, aor);
94 /*! \brief Observer for contacts so state can be updated on respective endpoints */
95 static struct ast_sorcery_observer state_contact_observer = {
96 .created = persistent_endpoint_contact_observer,
97 .deleted = persistent_endpoint_contact_observer,
100 static char *handle_cli_show_endpoints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
102 RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
103 struct ao2_iterator it_endpoints;
104 struct ast_sip_endpoint *endpoint;
108 e->command = "pjsip show endpoints";
110 "Usage: pjsip show endpoints\n"
111 " Show the registered PJSIP endpoints\n";
117 endpoints = ast_sip_get_endpoints();
122 if (!ao2_container_count(endpoints)) {
123 ast_cli(a->fd, "No endpoints found\n");
127 ast_cli(a->fd, "Endpoints:\n");
128 it_endpoints = ao2_iterator_init(endpoints, 0);
129 while ((endpoint = ao2_iterator_next(&it_endpoints))) {
130 ast_cli(a->fd, "%s\n", ast_sorcery_object_get_id(endpoint));
131 ao2_ref(endpoint, -1);
133 ao2_iterator_destroy(&it_endpoints);
137 static int show_contact(void *obj, void *arg, int flags)
139 struct ast_sip_contact *contact = obj;
140 struct ast_cli_args *a = arg;
141 RAII_VAR(struct ast_sip_contact_status *, status, ast_sorcery_retrieve_by_id(
142 ast_sip_get_sorcery(), CONTACT_STATUS,
143 ast_sorcery_object_get_id(contact)), ao2_cleanup);
145 ast_cli(a->fd, "\tContact %s:\n", contact->uri);
148 ast_cli(a->fd, "\tStatus not found!\n");
152 ast_cli(a->fd, "\t\tavailable = %s\n", status->status ? "yes" : "no");
154 if (status->status) {
155 ast_cli(a->fd, "\t\tRTT = %lld microseconds\n", (long long)status->rtt);
161 static void show_endpoint(struct ast_sip_endpoint *endpoint, struct ast_cli_args *a)
163 char *aor_name, *aors;
165 if (ast_strlen_zero(endpoint->aors)) {
169 aors = ast_strdupa(endpoint->aors);
171 while ((aor_name = strsep(&aors, ","))) {
172 RAII_VAR(struct ast_sip_aor *, aor,
173 ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
174 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
176 if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
180 ast_cli(a->fd, "AOR %s:\n", ast_sorcery_object_get_id(aor));
181 ao2_callback(contacts, OBJ_NODATA, show_contact, a);
187 static char *cli_show_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
189 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
190 const char *endpoint_name;
194 e->command = "pjsip show endpoint";
196 "Usage: pjsip show endpoint <endpoint>\n"
197 " Show the given PJSIP endpoint.\n";
204 return CLI_SHOWUSAGE;
207 endpoint_name = a->argv[3];
209 if (!(endpoint = ast_sorcery_retrieve_by_id(
210 ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
211 ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name);
215 ast_cli(a->fd, "Endpoint %s:\n", endpoint_name);
216 show_endpoint(endpoint, a);
221 static struct ast_cli_entry cli_commands[] = {
222 AST_CLI_DEFINE(handle_cli_show_endpoints, "Show PJSIP Endpoints"),
223 AST_CLI_DEFINE(cli_show_endpoint, "Show PJSIP Endpoint")
226 static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
228 struct ast_sip_endpoint *endpoint = obj;
230 if (!strcasecmp(var->value, "rfc4733")) {
231 endpoint->dtmf = AST_SIP_DTMF_RFC_4733;
232 } else if (!strcasecmp(var->value, "inband")) {
233 endpoint->dtmf = AST_SIP_DTMF_INBAND;
234 } else if (!strcasecmp(var->value, "info")) {
235 endpoint->dtmf = AST_SIP_DTMF_INFO;
236 } else if (!strcasecmp(var->value, "none")) {
237 endpoint->dtmf = AST_SIP_DTMF_NONE;
245 static int prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
247 struct ast_sip_endpoint *endpoint = obj;
249 if (ast_true(var->value)) {
250 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
251 } else if (ast_false(var->value)) {
252 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_100REL;
253 } else if (!strcasecmp(var->value, "required")) {
254 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
262 static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
264 struct ast_sip_endpoint *endpoint = obj;
266 if (ast_true(var->value)) {
267 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
268 } else if (ast_false(var->value)) {
269 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_TIMER;
270 } else if (!strcasecmp(var->value, "required")) {
271 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
272 } else if (!strcasecmp(var->value, "always")) {
273 endpoint->extensions.flags |= PJSIP_INV_ALWAYS_USE_TIMER;
281 void ast_sip_auth_array_destroy(struct ast_sip_auth_array *auths)
289 for (i = 0; i < auths->num; ++i) {
290 ast_free((char *) auths->names[i]);
292 ast_free(auths->names);
296 #define AUTH_INCREMENT 4
298 int ast_sip_auth_array_init(struct ast_sip_auth_array *auths, const char *value)
300 char *auth_names = ast_strdupa(value);
303 const char **alloced_auths = NULL;
305 while ((val = strsep(&auth_names, ","))) {
306 if (auths->num >= num_alloced) {
308 num_alloced += AUTH_INCREMENT;
309 size = num_alloced * sizeof(char *);
310 auths->names = ast_realloc(alloced_auths, size);
315 auths->names[auths->num] = ast_strdup(val);
316 if (!auths->names[auths->num]) {
324 ast_sip_auth_array_destroy(auths);
328 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
330 struct ast_sip_endpoint *endpoint = obj;
332 return ast_sip_auth_array_init(&endpoint->inbound_auths, var->value);
335 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
337 struct ast_sip_endpoint *endpoint = obj;
339 return ast_sip_auth_array_init(&endpoint->outbound_auths, var->value);
342 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
344 struct ast_sip_endpoint *endpoint = obj;
345 char *idents = ast_strdupa(var->value);
348 while ((val = strsep(&idents, ","))) {
349 if (!strcasecmp(val, "username")) {
350 endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
352 ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
353 val, ast_sorcery_object_get_id(endpoint));
360 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
362 struct ast_sip_endpoint *endpoint = obj;
364 if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
365 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
366 } else if (!strcasecmp(var->value, "update")) {
367 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
369 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
370 var->value, var->name, ast_sorcery_object_get_id(endpoint));
376 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
378 struct ast_sip_endpoint *endpoint = obj;
380 if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
381 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
382 } else if (!strcasecmp(var->value, "update")) {
383 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
385 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
386 var->value, var->name, ast_sorcery_object_get_id(endpoint));
392 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
394 struct ast_sip_endpoint *endpoint = obj;
396 if (!strcasecmp(var->value, "none")) {
397 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
398 } else if (!strcasecmp(var->value, "outgoing")) {
399 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
400 } else if (!strcasecmp(var->value, "incoming")) {
401 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
403 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
404 var->value, var->name, ast_sorcery_object_get_id(endpoint));
411 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
413 struct ast_sip_endpoint *endpoint = obj;
414 char cid_name[80] = { '\0' };
415 char cid_num[80] = { '\0' };
417 ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
418 if (!ast_strlen_zero(cid_name)) {
419 endpoint->id.self.name.str = ast_strdup(cid_name);
420 if (!endpoint->id.self.name.str) {
423 endpoint->id.self.name.valid = 1;
425 if (!ast_strlen_zero(cid_num)) {
426 endpoint->id.self.number.str = ast_strdup(cid_num);
427 if (!endpoint->id.self.number.str) {
430 endpoint->id.self.number.valid = 1;
435 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
437 struct ast_sip_endpoint *endpoint = obj;
438 int callingpres = ast_parse_caller_presentation(var->value);
439 if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
442 endpoint->id.self.number.presentation = callingpres;
443 endpoint->id.self.name.presentation = callingpres;
447 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
449 struct ast_sip_endpoint *endpoint = obj;
450 endpoint->id.self.tag = ast_strdup(var->value);
451 return endpoint->id.self.tag ? 0 : -1;
454 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
456 struct ast_sip_endpoint *endpoint = obj;
458 if (!strcasecmp("no", var->value)) {
459 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
460 } else if (!strcasecmp("sdes", var->value)) {
461 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
462 } else if (!strcasecmp("dtls", var->value)) {
463 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
464 ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
472 static int group_handler(const struct aco_option *opt,
473 struct ast_variable *var, void *obj)
475 struct ast_sip_endpoint *endpoint = obj;
477 if (!strncmp(var->name, "callgroup", 9)) {
478 if (!(endpoint->pickup.callgroup = ast_get_group(var->value))) {
481 } else if (!strncmp(var->name, "pickupgroup", 11)) {
482 if (!(endpoint->pickup.pickupgroup = ast_get_group(var->value))) {
492 static int named_groups_handler(const struct aco_option *opt,
493 struct ast_variable *var, void *obj)
495 struct ast_sip_endpoint *endpoint = obj;
497 if (!strncmp(var->name, "namedcallgroup", 14)) {
498 if (!(endpoint->pickup.named_callgroups =
499 ast_get_namedgroups(var->value))) {
502 } else if (!strncmp(var->name, "namedpickupgroup", 16)) {
503 if (!(endpoint->pickup.named_pickupgroups =
504 ast_get_namedgroups(var->value))) {
514 static int dtls_handler(const struct aco_option *opt,
515 struct ast_variable *var, void *obj)
517 struct ast_sip_endpoint *endpoint = obj;
519 return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, var->name, var->value);
522 static int t38udptl_ec_handler(const struct aco_option *opt,
523 struct ast_variable *var, void *obj)
525 struct ast_sip_endpoint *endpoint = obj;
527 if (!strcmp(var->value, "none")) {
528 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
529 } else if (!strcmp(var->value, "fec")) {
530 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
531 } else if (!strcmp(var->value, "redundancy")) {
532 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
540 static void *sip_nat_hook_alloc(const char *name)
542 return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
545 /*! \brief Destructor function for persistent endpoint information */
546 static void persistent_endpoint_destroy(void *obj)
548 struct sip_persistent_endpoint *persistent = obj;
550 ast_endpoint_shutdown(persistent->endpoint);
551 ast_free(persistent->aors);
554 /*! \brief Internal function which finds (or creates) persistent endpoint information */
555 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
557 RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
558 SCOPED_AO2LOCK(lock, persistent_endpoints);
560 if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
561 if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
565 if (!(persistent->endpoint = ast_endpoint_create("PJSIP", ast_sorcery_object_get_id(endpoint)))) {
569 persistent->aors = ast_strdup(endpoint->aors);
571 if (ast_strlen_zero(persistent->aors)) {
572 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
574 persistent_endpoint_update_state(persistent, NULL, 0);
577 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
580 ao2_ref(persistent->endpoint, +1);
581 return persistent->endpoint;
584 /*! \brief Callback function for when an object is finalized */
585 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
587 struct ast_sip_endpoint *endpoint = obj;
589 if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
596 int ast_res_pjsip_initialize_configuration(void)
598 if (ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands))) {
602 if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
606 if (!(sip_sorcery = ast_sorcery_open())) {
607 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
611 ast_sorcery_apply_config(sip_sorcery, "res_pjsip");
613 if (ast_sip_initialize_sorcery_auth(sip_sorcery)) {
614 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
615 ast_sorcery_unref(sip_sorcery);
620 ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
622 ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
624 if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
625 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
626 ast_sorcery_unref(sip_sorcery);
631 ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL);
633 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
634 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
635 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
636 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
637 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmfmode", "rfc4733", dtmf_handler, NULL, 0, 0);
638 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
639 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
640 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
641 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
642 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
643 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
644 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
645 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
646 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mohsuggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
647 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, NULL, 0, 0);
648 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, NULL, 0, 0);
649 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));
650 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));
651 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, NULL, 0, 0);
652 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, NULL, 0, 0);
653 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
654 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.external_address));
655 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username", ident_handler, NULL, 0, 0);
656 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
657 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, NULL, 0, 0);
658 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, NULL, 0, 0);
659 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, NULL, 0, 0);
660 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));
661 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, NULL, 0, 0);
662 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "", caller_id_privacy_handler, NULL, 0, 0);
663 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, NULL, 0, 0);
664 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
665 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
666 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
667 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
668 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
669 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
670 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
671 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, NULL, 0, 0);
672 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
673 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
674 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
675 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callgroup", "", group_handler, NULL, 0, 0);
676 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickupgroup", "", group_handler, NULL, 0, 0);
677 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedcallgroup", "", named_groups_handler, NULL, 0, 0);
678 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedpickupgroup", "", named_groups_handler, NULL, 0, 0);
679 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "devicestate_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at));
680 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
681 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38udptl_ec", "none", t38udptl_ec_handler, NULL, 0, 0);
682 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
683 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "faxdetect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
684 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
685 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
686 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tonezone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
687 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
688 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "recordonfeature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
689 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "recordofffeature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
690 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allowtransfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
691 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdpowner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
692 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdpsession", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
693 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.tos_audio));
694 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.tos_video));
695 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
696 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
697 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allowsubscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
698 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subminexpiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
699 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fromuser", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
700 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fromdomain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
701 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwifromuser", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
702 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlsverify", "", dtls_handler, NULL, 0, 0);
703 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlsrekey", "", dtls_handler, NULL, 0, 0);
704 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscertfile", "", dtls_handler, NULL, 0, 0);
705 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlsprivatekey", "", dtls_handler, NULL, 0, 0);
706 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscipher", "", dtls_handler, NULL, 0, 0);
707 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscafile", "", dtls_handler, NULL, 0, 0);
708 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscapath", "", dtls_handler, NULL, 0, 0);
709 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlssetup", "", dtls_handler, NULL, 0, 0);
710 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));
712 if (ast_sip_initialize_sorcery_transport(sip_sorcery)) {
713 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
714 ast_sorcery_unref(sip_sorcery);
719 if (ast_sip_initialize_sorcery_location(sip_sorcery)) {
720 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
721 ast_sorcery_unref(sip_sorcery);
726 if (ast_sip_initialize_sorcery_qualify(sip_sorcery)) {
727 ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
728 ast_sorcery_unref(sip_sorcery);
733 ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
735 if (ast_sip_initialize_sorcery_domain_alias(sip_sorcery)) {
736 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
737 ast_sorcery_unref(sip_sorcery);
742 if (ast_sip_initialize_sorcery_global(sip_sorcery)) {
743 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
744 ast_sorcery_unref(sip_sorcery);
749 ast_sorcery_load(sip_sorcery);
754 void ast_res_pjsip_destroy_configuration(void)
756 ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
757 ast_sorcery_unref(sip_sorcery);
760 int ast_res_pjsip_reload_configuration(void)
763 ast_sorcery_reload(sip_sorcery);
768 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
770 ast_string_field_free_memory(&subscription->mwi);
773 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
775 ast_string_field_free_memory(&info->recording);
778 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
780 ast_string_field_free_memory(&media->rtp);
781 ast_string_field_free_memory(media);
784 static void endpoint_destructor(void* obj)
786 struct ast_sip_endpoint *endpoint = obj;
788 ast_string_field_free_memory(endpoint);
790 if (endpoint->media.codecs) {
791 ast_format_cap_destroy(endpoint->media.codecs);
793 subscription_configuration_destroy(&endpoint->subscription);
794 info_configuration_destroy(&endpoint->info);
795 media_configuration_destroy(&endpoint->media);
796 ast_sip_auth_array_destroy(&endpoint->inbound_auths);
797 ast_sip_auth_array_destroy(&endpoint->outbound_auths);
798 ast_party_id_free(&endpoint->id.self);
799 endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
800 endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
801 ao2_cleanup(endpoint->persistent);
804 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
806 return ast_string_field_init(&subscription->mwi, 64);
809 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
811 return ast_string_field_init(&info->recording, 32);
814 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
816 return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
819 void *ast_sip_endpoint_alloc(const char *name)
821 struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
825 if (ast_string_field_init(endpoint, 64)) {
826 ao2_cleanup(endpoint);
829 if (!(endpoint->media.codecs = ast_format_cap_alloc_nolock())) {
830 ao2_cleanup(endpoint);
833 if (init_subscription_configuration(&endpoint->subscription)) {
834 ao2_cleanup(endpoint);
837 if (init_info_configuration(&endpoint->info)) {
838 ao2_cleanup(endpoint);
841 if (init_media_configuration(&endpoint->media)) {
842 ao2_cleanup(endpoint);
845 ast_party_id_init(&endpoint->id.self);
849 struct ao2_container *ast_sip_get_endpoints(void)
851 struct ao2_container *endpoints;
853 endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
858 int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_sip_auth **out)
862 for (i = 0; i < auths->num; ++i) {
863 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, auths->names[i]);
865 ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", auths->names[i]);
873 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
876 for (i = 0; i < num_auths; ++i) {
877 ao2_cleanup(auths[i]);
881 struct ast_sorcery *ast_sip_get_sorcery(void)