2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
23 #include "asterisk/res_pjsip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/astobj2.h"
26 #include "asterisk/sorcery.h"
27 #include "include/res_pjsip_private.h"
28 #include "asterisk/res_pjsip_cli.h"
30 /*! \brief Destructor for AOR */
31 static void aor_destroy(void *obj)
33 struct ast_sip_aor *aor = obj;
35 ao2_cleanup(aor->permanent_contacts);
36 ast_string_field_free_memory(aor);
39 /*! \brief Allocator for AOR */
40 static void *aor_alloc(const char *name)
42 struct ast_sip_aor *aor = ast_sorcery_generic_alloc(sizeof(struct ast_sip_aor), aor_destroy);
46 ast_string_field_init(aor, 128);
50 /*! \brief Destructor for contact */
51 static void contact_destroy(void *obj)
53 struct ast_sip_contact *contact = obj;
55 ast_string_field_free_memory(contact);
58 /*! \brief Allocator for contact */
59 static void *contact_alloc(const char *name)
61 struct ast_sip_contact *contact = ast_sorcery_generic_alloc(sizeof(*contact), contact_destroy);
67 if (ast_string_field_init(contact, 256)) {
75 struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
77 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name);
80 /*! \brief Internal callback function which deletes and unlinks any expired contacts */
81 static int contact_expire(void *obj, void *arg, int flags)
83 struct ast_sip_contact *contact = obj;
85 /* If the contact has not yet expired it is valid */
86 if (ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) > 0) {
90 ast_sip_location_delete_contact(contact);
95 /*! \brief Internal callback function which links static contacts into another container */
96 static int contact_link_static(void *obj, void *arg, int flags)
98 struct ao2_container *dest = arg;
104 /*! \brief Simple callback function which returns immediately, used to grab the first contact of an AOR */
105 static int contact_find_first(void *obj, void *arg, int flags)
107 return CMP_MATCH | CMP_STOP;
110 struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor)
112 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
113 struct ast_sip_contact *contact;
115 contacts = ast_sip_location_retrieve_aor_contacts(aor);
116 if (!contacts || (ao2_container_count(contacts) == 0)) {
120 contact = ao2_callback(contacts, 0, contact_find_first, NULL);
124 struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
126 /* Give enough space for ^ at the beginning and ;@ at the end, since that is our object naming scheme */
127 char regex[strlen(ast_sorcery_object_get_id(aor)) + 4];
128 struct ao2_container *contacts;
130 snprintf(regex, sizeof(regex), "^%s;@", ast_sorcery_object_get_id(aor));
132 if (!(contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex))) {
136 /* Prune any expired contacts and delete them, we do this first because static contacts can never expire */
137 ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL);
139 /* Add any permanent contacts from the AOR */
140 if (aor->permanent_contacts) {
141 ao2_callback(aor->permanent_contacts, OBJ_NODATA, contact_link_static, contacts);
147 void ast_sip_location_retrieve_contact_and_aor_from_list(const char *aor_list, struct ast_sip_aor **aor,
148 struct ast_sip_contact **contact)
153 /* If the location is still empty we have nowhere to go */
154 if (ast_strlen_zero(aor_list) || !(rest = ast_strdupa(aor_list))) {
155 ast_log(LOG_WARNING, "Unable to determine contacts from empty aor list\n");
162 while ((aor_name = strsep(&rest, ","))) {
163 *aor = ast_sip_location_retrieve_aor(aor_name);
168 *contact = ast_sip_location_retrieve_first_aor_contact(*aor);
169 /* If a valid contact is available use its URI for dialing */
179 struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
181 struct ast_sip_aor *aor;
182 struct ast_sip_contact *contact;
184 ast_sip_location_retrieve_contact_and_aor_from_list(aor_list, &aor, &contact);
191 static int permanent_uri_sort_fn(const void *obj_left, const void *obj_right, int flags);
192 static int cli_contact_populate_container(void *obj, void *arg, int flags);
194 static int gather_contacts_for_aor(void *obj, void *arg, int flags)
196 struct ao2_container *aor_contacts;
197 struct ast_sip_aor *aor = obj;
198 struct ao2_container *container = arg;
200 aor_contacts = ast_sip_location_retrieve_aor_contacts(aor);
204 ao2_callback(aor_contacts, OBJ_MULTIPLE | OBJ_NODATA, cli_contact_populate_container,
206 ao2_ref(aor_contacts, -1);
210 struct ao2_container *ast_sip_location_retrieve_contacts_from_aor_list(const char *aor_list)
212 struct ao2_container *contacts;
214 contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
215 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, permanent_uri_sort_fn, NULL);
220 ast_sip_for_each_aor(aor_list, gather_contacts_for_aor, contacts);
225 struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name)
227 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
230 int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
231 struct timeval expiration_time, const char *path_info, const char *user_agent)
233 char name[MAX_OBJECT_FIELD * 2 + 3];
234 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
236 snprintf(name, sizeof(name), "%s;@%s", ast_sorcery_object_get_id(aor), uri);
238 if (!(contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", name))) {
242 ast_string_field_set(contact, uri, uri);
243 contact->expiration_time = expiration_time;
244 contact->qualify_frequency = aor->qualify_frequency;
245 contact->qualify_timeout = aor->qualify_timeout;
246 contact->authenticate_qualify = aor->authenticate_qualify;
247 if (path_info && aor->support_path) {
248 ast_string_field_set(contact, path, path_info);
251 if (!ast_strlen_zero(aor->outbound_proxy)) {
252 ast_string_field_set(contact, outbound_proxy, aor->outbound_proxy);
255 if (!ast_strlen_zero(user_agent)) {
256 ast_string_field_set(contact, user_agent, user_agent);
259 return ast_sorcery_create(ast_sip_get_sorcery(), contact);
262 int ast_sip_location_update_contact(struct ast_sip_contact *contact)
264 return ast_sorcery_update(ast_sip_get_sorcery(), contact);
267 int ast_sip_location_delete_contact(struct ast_sip_contact *contact)
269 return ast_sorcery_delete(ast_sip_get_sorcery(), contact);
272 /*! \brief Custom handler for translating from a string timeval to actual structure */
273 static int expiration_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
275 struct ast_sip_contact *contact = obj;
276 return ast_get_timeval(var->value, &contact->expiration_time, ast_tv(0, 0), NULL);
279 /*! \brief Custom handler for translating from an actual structure timeval to string */
280 static int expiration_struct2str(const void *obj, const intptr_t *args, char **buf)
282 const struct ast_sip_contact *contact = obj;
283 return (ast_asprintf(buf, "%ld", contact->expiration_time.tv_sec) < 0) ? -1 : 0;
286 /*! \brief Helper function which validates a permanent contact */
287 static int permanent_contact_validate(void *data)
289 const char *value = data;
291 pj_str_t contact_uri;
292 static const pj_str_t HCONTACT = { "Contact", 7 };
293 pjsip_contact_hdr *contact_hdr;
296 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Permanent Contact Validation", 256, 256);
301 pj_strdup2_with_null(pool, &contact_uri, value);
302 if (!(contact_hdr = pjsip_parse_hdr(pool, &HCONTACT, contact_uri.ptr, contact_uri.slen, NULL))
303 || !(PJSIP_URI_SCHEME_IS_SIP(contact_hdr->uri)
304 || PJSIP_URI_SCHEME_IS_SIPS(contact_hdr->uri))) {
308 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
312 static int permanent_uri_sort_fn(const void *obj_left, const void *obj_right, int flags)
314 const struct ast_sip_contact *object_left = obj_left;
315 const struct ast_sip_contact *object_right = obj_right;
316 const char *right_key = obj_right;
319 switch (flags & OBJ_SEARCH_MASK) {
320 case OBJ_SEARCH_OBJECT:
321 right_key = ast_sorcery_object_get_id(object_right);
324 cmp = strcmp(ast_sorcery_object_get_id(object_left), right_key);
326 case OBJ_SEARCH_PARTIAL_KEY:
328 * We could also use a partial key struct containing a length
329 * so strlen() does not get called for every comparison instead.
331 cmp = strncmp(ast_sorcery_object_get_id(object_left), right_key, strlen(right_key));
334 /* Sort can only work on something with a full or partial key. */
342 /*! \brief Custom handler for permanent URIs */
343 static int permanent_uri_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
345 struct ast_sip_aor *aor = obj;
346 const char *aor_id = ast_sorcery_object_get_id(aor);
350 if (ast_strlen_zero(var->value)) {
354 contacts = ast_strdupa(var->value);
355 while ((contact_uri = strsep(&contacts, ","))) {
356 struct ast_sip_contact *contact;
357 struct ast_sip_contact_status *status;
358 char contact_id[strlen(aor_id) + strlen(contact_uri) + 2 + 1];
360 if (ast_sip_push_task_synchronous(NULL, permanent_contact_validate, contact_uri)) {
361 ast_log(LOG_ERROR, "Permanent URI on aor '%s' with contact '%s' failed to parse\n",
362 ast_sorcery_object_get_id(aor), contact_uri);
366 if (!aor->permanent_contacts) {
367 aor->permanent_contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
368 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, permanent_uri_sort_fn, NULL);
369 if (!aor->permanent_contacts) {
374 snprintf(contact_id, sizeof(contact_id), "%s@@%s", aor_id, contact_uri);
375 contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", contact_id);
380 status = ast_res_pjsip_find_or_create_contact_status(contact);
382 ao2_ref(contact, -1);
387 ast_string_field_set(contact, uri, contact_uri);
388 ao2_link(aor->permanent_contacts, contact);
389 ao2_ref(contact, -1);
395 static int contact_to_var_list(void *object, void *arg, int flags)
397 struct ast_sip_contact_wrapper *wrapper = object;
398 struct ast_variable **var = arg;
400 ast_variable_list_append(&*var, ast_variable_new("contact", wrapper->contact->uri, ""));
405 static int contacts_to_var_list(const void *obj, struct ast_variable **fields)
407 const struct ast_sip_aor *aor = obj;
409 ast_sip_for_each_contact(aor, contact_to_var_list, fields);
414 int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg)
418 if (!on_aor || ast_strlen_zero(aors)) {
422 copy = ast_strdupa(aors);
423 while ((name = strsep(©, ","))) {
424 RAII_VAR(struct ast_sip_aor *, aor,
425 ast_sip_location_retrieve_aor(name), ao2_cleanup);
431 if (on_aor(aor, arg, 0)) {
439 static void contact_wrapper_destroy(void *obj)
441 struct ast_sip_contact_wrapper *wrapper = obj;
442 ast_free(wrapper->aor_id);
443 ast_free(wrapper->contact_id);
444 ao2_ref(wrapper->contact, -1);
447 int ast_sip_for_each_contact(const struct ast_sip_aor *aor,
448 ao2_callback_fn on_contact, void *arg)
450 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
451 struct ao2_iterator i;
456 !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
460 i = ao2_iterator_init(contacts, 0);
461 while ((object = ao2_iterator_next(&i))) {
462 RAII_VAR(struct ast_sip_contact *, contact, object, ao2_cleanup);
463 RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);
464 const char *aor_id = ast_sorcery_object_get_id(aor);
466 wrapper = ao2_alloc(sizeof(struct ast_sip_contact_wrapper), contact_wrapper_destroy);
471 wrapper->contact_id = ast_malloc(strlen(aor_id) + strlen(contact->uri) + 2);
472 if (!wrapper->contact_id) {
476 sprintf(wrapper->contact_id, "%s/%s", aor_id, contact->uri);
477 wrapper->aor_id = ast_strdup(aor_id);
478 if (!wrapper->aor_id) {
482 wrapper->contact = contact;
483 ao2_bump(wrapper->contact);
485 if ((res = on_contact(wrapper, arg, 0))) {
489 ao2_iterator_destroy(&i);
493 int ast_sip_contact_to_str(void *object, void *arg, int flags)
495 struct ast_sip_contact_wrapper *wrapper = object;
496 struct ast_str **buf = arg;
498 ast_str_append(buf, 0, "%s,", wrapper->contact_id);
503 static int sip_aor_to_ami(const struct ast_sip_aor *aor, struct ast_str **buf)
505 RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
506 ast_sip_get_sorcery(), aor, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
507 struct ast_variable *i;
513 ast_str_append(buf, 0, "ObjectType: %s\r\n",
514 ast_sorcery_object_get_type(aor));
515 ast_str_append(buf, 0, "ObjectName: %s\r\n",
516 ast_sorcery_object_get_id(aor));
518 for (i = objset; i; i = i->next) {
519 char *camel = ast_to_camel_case(i->name);
520 if (strcmp(camel, "Contact") == 0) {
524 ast_str_append(buf, 0, "%s: %s\r\n", S_OR(camel, "Contacts"), i->value);
531 static int contacts_to_str(const void *obj, const intptr_t *args, char **buf)
533 const struct ast_sip_aor *aor = obj;
534 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
536 ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &str);
537 ast_str_truncate(str, -1);
539 *buf = ast_strdup(ast_str_buffer(str));
547 static int format_ami_aor_handler(void *obj, void *arg, int flags)
549 struct ast_sip_aor *aor = obj;
550 struct ast_sip_ami *ami = arg;
551 const struct ast_sip_endpoint *endpoint = ami->arg;
552 RAII_VAR(struct ast_str *, buf,
553 ast_sip_create_ami_event("AorDetail", ami), ast_free);
557 RAII_VAR(struct ao2_container *, contacts,
558 ast_sip_location_retrieve_aor_contacts(aor), ao2_cleanup);
564 sip_aor_to_ami(aor, &buf);
565 total_contacts = ao2_container_count(contacts);
566 num_permanent = aor->permanent_contacts ?
567 ao2_container_count(aor->permanent_contacts) : 0;
569 ast_str_append(&buf, 0, "TotalContacts: %d\r\n", total_contacts);
570 ast_str_append(&buf, 0, "ContactsRegistered: %d\r\n",
571 total_contacts - num_permanent);
572 ast_str_append(&buf, 0, "EndpointName: %s\r\n",
573 ast_sorcery_object_get_id(endpoint));
575 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
581 static int format_ami_endpoint_aor(const struct ast_sip_endpoint *endpoint,
582 struct ast_sip_ami *ami)
584 ami->arg = (void *)endpoint;
585 return ast_sip_for_each_aor(endpoint->aors,
586 format_ami_aor_handler, ami);
589 struct ast_sip_endpoint_formatter endpoint_aor_formatter = {
590 .format_ami = format_ami_endpoint_aor
593 static struct ao2_container *cli_aor_get_container(void)
595 RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
596 struct ao2_container *s_container;
598 container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "aor",
599 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
604 s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
605 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
610 if (ao2_container_dup(s_container, container, 0)) {
611 ao2_ref(s_container, -1);
618 static int cli_contact_populate_container(void *obj, void *arg, int flags)
625 static int cli_aor_gather_contacts(void *obj, void *arg, int flags)
627 struct ast_sip_aor *aor = obj;
629 return ast_sip_for_each_contact(aor, cli_contact_populate_container, arg);
632 static const char *cli_contact_get_id(const void *obj)
634 const struct ast_sip_contact_wrapper *wrapper = obj;
635 return wrapper->contact_id;
638 static int cli_contact_sort(const void *obj, const void *arg, int flags)
640 const struct ast_sip_contact_wrapper *left_wrapper = obj;
641 const struct ast_sip_contact_wrapper *right_wrapper = arg;
642 const char *right_key = arg;
645 switch (flags & OBJ_SEARCH_MASK) {
646 case OBJ_SEARCH_OBJECT:
647 right_key = right_wrapper->contact_id;
650 cmp = strcmp(left_wrapper->contact_id, right_key);
652 case OBJ_SEARCH_PARTIAL_KEY:
653 cmp = strncmp(left_wrapper->contact_id, right_key, strlen(right_key));
663 static int cli_contact_compare(void *obj, void *arg, int flags)
665 const struct ast_sip_contact_wrapper *left_wrapper = obj;
666 const struct ast_sip_contact_wrapper *right_wrapper = arg;
667 const char *right_key = arg;
670 switch (flags & OBJ_SEARCH_MASK) {
671 case OBJ_SEARCH_OBJECT:
672 right_key = right_wrapper->contact_id;
675 if (strcmp(left_wrapper->contact_id, right_key) == 0) {;
676 cmp = CMP_MATCH | CMP_STOP;
679 case OBJ_SEARCH_PARTIAL_KEY:
680 if (strncmp(left_wrapper->contact_id, right_key, strlen(right_key)) == 0) {
692 static int cli_contact_iterate(void *container, ao2_callback_fn callback, void *args)
694 return ast_sip_for_each_contact(container, callback, args);
697 static struct ao2_container *cli_contact_get_container(void)
699 RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);
700 struct ao2_container *child_container;
702 parent_container = cli_aor_get_container();
703 if (!parent_container) {
707 child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
708 cli_contact_sort, cli_contact_compare);
709 if (!child_container) {
713 ao2_callback(parent_container, OBJ_NODATA, cli_aor_gather_contacts, child_container);
715 return child_container;
718 static void *cli_contact_retrieve_by_id(const char *id)
720 return ao2_find(cli_contact_get_container(), id, OBJ_KEY | OBJ_NOLOCK);
723 static int cli_contact_print_header(void *obj, void *arg, int flags)
725 struct ast_sip_cli_context *context = arg;
726 int indent = CLI_INDENT_TO_SPACES(context->indent_level);
727 int filler = CLI_LAST_TABSTOP - indent - 18;
729 ast_assert(context->output_buffer != NULL);
731 ast_str_append(&context->output_buffer, 0,
732 "%*s: <Aor/ContactUri%*.*s> <Status....> <RTT(ms)..>\n",
733 indent, "Contact", filler, filler, CLI_HEADER_FILLER);
738 static int cli_contact_print_body(void *obj, void *arg, int flags)
740 struct ast_sip_contact_wrapper *wrapper = obj;
741 struct ast_sip_contact *contact = wrapper->contact;
742 struct ast_sip_cli_context *context = arg;
746 RAII_VAR(struct ast_sip_contact_status *, status,
747 ast_sorcery_retrieve_by_id( ast_sip_get_sorcery(), CONTACT_STATUS, ast_sorcery_object_get_id(contact)),
750 ast_assert(contact->uri != NULL);
751 ast_assert(context->output_buffer != NULL);
753 indent = CLI_INDENT_TO_SPACES(context->indent_level);
754 flexwidth = CLI_LAST_TABSTOP - indent - 2;
756 ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %11.3f\n",
759 flexwidth, flexwidth,
761 ast_sip_get_contact_short_status_label(status ? status->status : UNKNOWN),
762 (status && (status->status != UNKNOWN) ? ((long long) status->rtt) / 1000.0 : NAN));
767 static int cli_aor_iterate(void *container, ao2_callback_fn callback, void *args)
769 const char *aor_list = container;
771 return ast_sip_for_each_aor(aor_list, callback, args);
774 static void *cli_aor_retrieve_by_id(const char *id)
776 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", id);
779 static const char *cli_aor_get_id(const void *obj)
781 return ast_sorcery_object_get_id(obj);
784 static int cli_aor_print_header(void *obj, void *arg, int flags)
786 struct ast_sip_cli_context *context = arg;
787 RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
789 int indent = CLI_INDENT_TO_SPACES(context->indent_level);
790 int filler = CLI_LAST_TABSTOP - indent - 7;
792 ast_assert(context->output_buffer != NULL);
794 ast_str_append(&context->output_buffer, 0,
795 "%*s: <Aor%*.*s> <MaxContact>\n",
796 indent, "Aor", filler, filler, CLI_HEADER_FILLER);
798 if (context->recurse) {
799 context->indent_level++;
800 formatter_entry = ast_sip_lookup_cli_formatter("contact");
801 if (formatter_entry) {
802 formatter_entry->print_header(NULL, context, 0);
804 context->indent_level--;
810 static int cli_aor_print_body(void *obj, void *arg, int flags)
812 struct ast_sip_aor *aor = obj;
813 struct ast_sip_cli_context *context = arg;
814 RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
818 ast_assert(context->output_buffer != NULL);
820 // context->current_aor = aor;
822 indent = CLI_INDENT_TO_SPACES(context->indent_level);
823 flexwidth = CLI_LAST_TABSTOP - indent - 12;
825 ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %12u\n",
828 flexwidth, flexwidth,
829 ast_sorcery_object_get_id(aor), aor->max_contacts);
831 if (context->recurse) {
832 context->indent_level++;
834 formatter_entry = ast_sip_lookup_cli_formatter("contact");
835 if (formatter_entry) {
836 formatter_entry->iterate(aor, formatter_entry->print_body, context);
839 context->indent_level--;
841 if (context->indent_level == 0) {
842 ast_str_append(&context->output_buffer, 0, "\n");
846 if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
847 ast_str_append(&context->output_buffer, 0, "\n");
848 ast_sip_cli_print_sorcery_objectset(aor, context, 0);
854 static struct ast_cli_entry cli_commands[] = {
855 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Aors",
856 .command = "pjsip list aors",
857 .usage = "Usage: pjsip list aors\n"
858 " List the configured PJSIP Aors\n"),
859 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aors",
860 .command = "pjsip show aors",
861 .usage = "Usage: pjsip show aors\n"
862 " Show the configured PJSIP Aors\n"),
863 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aor",
864 .command = "pjsip show aor",
865 .usage = "Usage: pjsip show aor <id>\n"
866 " Show the configured PJSIP Aor\n"),
868 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Contacts",
869 .command = "pjsip list contacts",
870 .usage = "Usage: pjsip list contacts\n"
871 " List the configured PJSIP contacts\n"),
872 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Contacts",
873 .command = "pjsip show contacts",
874 .usage = "Usage: pjsip show contacts\n"
875 " Show the configured PJSIP contacts\n"),
876 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Contact",
877 .command = "pjsip show contact",
878 .usage = "Usage: pjsip show contact\n"
879 " Show the configured PJSIP contact\n"),
882 struct ast_sip_cli_formatter_entry *contact_formatter;
883 struct ast_sip_cli_formatter_entry *aor_formatter;
885 /*! \brief Always create a contact_status for each contact */
886 static int contact_apply_handler(const struct ast_sorcery *sorcery, void *object)
888 struct ast_sip_contact_status *status;
889 struct ast_sip_contact *contact = object;
891 status = ast_res_pjsip_find_or_create_contact_status(contact);
893 return status ? 0 : -1;
896 /*! \brief Initialize sorcery with location support */
897 int ast_sip_initialize_sorcery_location(void)
899 struct ast_sorcery *sorcery = ast_sip_get_sorcery();
900 ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar");
901 ast_sorcery_apply_default(sorcery, "aor", "config", "pjsip.conf,criteria=type=aor");
903 if (ast_sorcery_object_register(sorcery, "contact", contact_alloc, NULL, contact_apply_handler) ||
904 ast_sorcery_object_register(sorcery, "aor", aor_alloc, NULL, NULL)) {
908 ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
909 ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
910 ast_sorcery_object_field_register(sorcery, "contact", "path", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, path));
911 ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, NULL, 0, 0);
912 ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
913 PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
914 ast_sorcery_object_field_register(sorcery, "contact", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_contact, qualify_timeout));
915 ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy));
916 ast_sorcery_object_field_register(sorcery, "contact", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, user_agent));
918 ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0);
919 ast_sorcery_object_field_register(sorcery, "aor", "minimum_expiration", "60", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, minimum_expiration));
920 ast_sorcery_object_field_register(sorcery, "aor", "maximum_expiration", "7200", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, maximum_expiration));
921 ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration));
922 ast_sorcery_object_field_register(sorcery, "aor", "qualify_frequency", 0, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_aor, qualify_frequency), 0, 86400);
923 ast_sorcery_object_field_register(sorcery, "aor", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_aor, qualify_timeout));
924 ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));
925 ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
926 ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));
927 ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, contacts_to_str, contacts_to_var_list, 0, 0);
928 ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));
929 ast_sorcery_object_field_register(sorcery, "aor", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, outbound_proxy));
930 ast_sorcery_object_field_register(sorcery, "aor", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, support_path));
932 internal_sip_register_endpoint_formatter(&endpoint_aor_formatter);
934 contact_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
935 if (!contact_formatter) {
936 ast_log(LOG_ERROR, "Unable to allocate memory for contact_formatter\n");
939 contact_formatter->name = "contact";
940 contact_formatter->print_header = cli_contact_print_header;
941 contact_formatter->print_body = cli_contact_print_body;
942 contact_formatter->get_container = cli_contact_get_container;
943 contact_formatter->iterate = cli_contact_iterate;
944 contact_formatter->get_id = cli_contact_get_id;
945 contact_formatter->retrieve_by_id = cli_contact_retrieve_by_id;
947 aor_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
948 if (!aor_formatter) {
949 ast_log(LOG_ERROR, "Unable to allocate memory for aor_formatter\n");
952 aor_formatter->name = "aor";
953 aor_formatter->print_header = cli_aor_print_header;
954 aor_formatter->print_body = cli_aor_print_body;
955 aor_formatter->get_container = cli_aor_get_container;
956 aor_formatter->iterate = cli_aor_iterate;
957 aor_formatter->get_id = cli_aor_get_id;
958 aor_formatter->retrieve_by_id = cli_aor_retrieve_by_id;
960 ast_sip_register_cli_formatter(contact_formatter);
961 ast_sip_register_cli_formatter(aor_formatter);
962 ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
967 int ast_sip_destroy_sorcery_location(void)
969 ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
970 ast_sip_unregister_cli_formatter(contact_formatter);
971 ast_sip_unregister_cli_formatter(aor_formatter);
973 internal_sip_unregister_endpoint_formatter(&endpoint_aor_formatter);