diff options
author | Richard Mudgett <rmudgett@digium.com> | 2014-02-06 17:55:45 +0000 |
---|---|---|
committer | Richard Mudgett <rmudgett@digium.com> | 2014-02-06 17:55:45 +0000 |
commit | b5ca213e34055724a5cd669938e356569847edcb (patch) | |
tree | ae81ec8dc3add7c1ed3cb94c361a205e94a3bcd7 /res | |
parent | 8ff02ac951c979b1e5e340b3b1cc05736780be20 (diff) |
res_pjsip: Updates and adds more PJSIP CLI commands.
* Adds identify, transport, and registration support to the PJSIP CLI.
* Creates three additional callbacks, one for an iterator, one for a
comparator, and one for a container. This eliminates the link dependency
from higher level modules to lower level ones.
* Eliminates duplicate sorting in PJSIP CLI commands.
* Cleans up PJSIP CLI output formatting.
* Pushes CLI command registration down to the implementing source file.
* Adds several ast_sip_destroy_sorcery functions to complement existing
ast_sip_sorcery_initialize functions. The destroy functions unregister
PJSIP CLI commands and PJSIP CLI formatters.
Reported by: George Joseph
Review: https://reviewboard.asterisk.org/r/3104/
........
Merged revisions 407568 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@407573 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res')
-rw-r--r-- | res/res_pjsip/config_auth.c | 77 | ||||
-rw-r--r-- | res/res_pjsip/config_domain_aliases.c | 4 | ||||
-rw-r--r-- | res/res_pjsip/config_global.c | 4 | ||||
-rw-r--r-- | res/res_pjsip/config_transport.c | 122 | ||||
-rw-r--r-- | res/res_pjsip/include/res_pjsip_private.h | 2 | ||||
-rw-r--r-- | res/res_pjsip/location.c | 190 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_cli.c | 107 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_configuration.c | 231 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_options.c | 5 | ||||
-rw-r--r-- | res/res_pjsip_endpoint_identifier_ip.c | 137 | ||||
-rw-r--r-- | res/res_pjsip_logger.c | 2 | ||||
-rw-r--r-- | res/res_pjsip_outbound_registration.c | 115 |
12 files changed, 755 insertions, 241 deletions
diff --git a/res/res_pjsip/config_auth.c b/res/res_pjsip/config_auth.c index d7e759f5d..056449b60 100644 --- a/res/res_pjsip/config_auth.c +++ b/res/res_pjsip/config_auth.c @@ -199,13 +199,39 @@ static struct ast_sip_endpoint_formatter endpoint_auth_formatter = { .format_ami = format_ami_endpoint_auth }; -static struct ao2_container *cli_get_auth_container(struct ast_sorcery *sip_sorcery) +static struct ao2_container *cli_get_auth_container(void) { - return ast_sorcery_retrieve_by_fields(sip_sorcery, "auth", - AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup); + + container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "auth", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!container) { + return NULL; + } + + s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!s_container) { + return NULL; + } + + if (ao2_container_dup(s_container, container, 0)) { + return NULL; + } + ao2_ref(s_container, +1); + return s_container; +} + +static int cli_iterator(const void *container, ao2_callback_fn callback, void *args) +{ + const struct ast_sip_auth_vector *vector = container; + + return ast_sip_for_each_auth(vector, callback, args); } -static int cli_print_auth_header(void *obj, void *arg, int flags) { +static int cli_print_auth_header(void *obj, void *arg, int flags) +{ struct ast_sip_cli_context *context = arg; int indent = CLI_INDENT_TO_SPACES(context->indent_level); int filler = CLI_MAX_WIDTH - indent - 20; @@ -215,12 +241,14 @@ static int cli_print_auth_header(void *obj, void *arg, int flags) { } ast_str_append(&context->output_buffer, 0, - "%*s: <AuthId/UserName%*.*s>\n", indent, "I/OAuth", filler, filler, CLI_HEADER_FILLER); + "%*s: <AuthId/UserName%*.*s>\n", indent, "I/OAuth", filler, filler, + CLI_HEADER_FILLER); return 0; } -static int cli_print_auth_body(void *obj, void *arg, int flags) { +static int cli_print_auth_body(void *obj, void *arg, int flags) +{ struct ast_sip_auth *auth = obj; struct ast_sip_cli_context *context = arg; char title[32]; @@ -231,13 +259,15 @@ static int cli_print_auth_body(void *obj, void *arg, int flags) { return -1; } - snprintf(title, 32, "%sAuth",context->auth_direction ? context->auth_direction : ""); + snprintf(title, sizeof(title), "%sAuth", + context->auth_direction ? context->auth_direction : ""); ast_str_append(&context->output_buffer, 0, "%*s: %s/%s\n", CLI_INDENT_TO_SPACES(context->indent_level), title, ast_sorcery_object_get_id(auth), auth->auth_user); - if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) { + if (context->show_details + || (context->show_details_only_level_0 && context->indent_level == 0)) { ast_str_append(&context->output_buffer, 0, "\n"); ast_sip_cli_print_sorcery_objectset(auth, context, 0); } @@ -245,16 +275,35 @@ static int cli_print_auth_body(void *obj, void *arg, int flags) { return 0; } -static struct ast_sip_cli_formatter_entry cli_auth_formatter = { +static struct ast_sip_cli_formatter_entry cli_auth_formatter = { .name = SIP_SORCERY_AUTH_TYPE, .print_header = cli_print_auth_header, .print_body = cli_print_auth_body, .get_container = cli_get_auth_container, + .iterator = cli_iterator, + .comparator = ast_sorcery_object_id_compare, +}; + +static struct ast_cli_entry cli_commands[] = { + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Auths", + .command = "pjsip list auths", + .usage = "Usage: pjsip list auths\n" + " List the configured PJSIP Auths\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Auths", + .command = "pjsip show auths", + .usage = "Usage: pjsip show auths\n" + " Show the configured PJSIP Auths\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Auth", + .command = "pjsip show auth", + .usage = "Usage: pjsip show auth <id>\n" + " Show the configured PJSIP Auth\n"), }; /*! \brief Initialize sorcery with auth support */ -int ast_sip_initialize_sorcery_auth(struct ast_sorcery *sorcery) +int ast_sip_initialize_sorcery_auth(void) { + struct ast_sorcery *sorcery = ast_sip_get_sorcery(); + ast_sorcery_apply_default(sorcery, SIP_SORCERY_AUTH_TYPE, "config", "pjsip.conf,criteria=type=auth"); if (ast_sorcery_object_register(sorcery, SIP_SORCERY_AUTH_TYPE, auth_alloc, NULL, auth_apply)) { @@ -278,6 +327,14 @@ int ast_sip_initialize_sorcery_auth(struct ast_sorcery *sorcery) ast_sip_register_endpoint_formatter(&endpoint_auth_formatter); ast_sip_register_cli_formatter(&cli_auth_formatter); + ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); return 0; } + +int ast_sip_destroy_sorcery_auth(void) +{ + ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); + ast_sip_unregister_cli_formatter(&cli_auth_formatter); + return 0; +} diff --git a/res/res_pjsip/config_domain_aliases.c b/res/res_pjsip/config_domain_aliases.c index 6ca3736a0..232adacd8 100644 --- a/res/res_pjsip/config_domain_aliases.c +++ b/res/res_pjsip/config_domain_aliases.c @@ -48,8 +48,10 @@ static void *domain_alias_alloc(const char *name) } /*! \brief Initialize sorcery with domain alias support */ -int ast_sip_initialize_sorcery_domain_alias(struct ast_sorcery *sorcery) +int ast_sip_initialize_sorcery_domain_alias(void) { + struct ast_sorcery *sorcery = ast_sip_get_sorcery(); + ast_sorcery_apply_default(sorcery, SIP_SORCERY_DOMAIN_ALIAS_TYPE, "config", "pjsip.conf,criteria=type=domain_alias"); if (ast_sorcery_object_register(sorcery, SIP_SORCERY_DOMAIN_ALIAS_TYPE, domain_alias_alloc, NULL, NULL)) { diff --git a/res/res_pjsip/config_global.c b/res/res_pjsip/config_global.c index 739a2339f..8e2cb2a41 100644 --- a/res/res_pjsip/config_global.c +++ b/res/res_pjsip/config_global.c @@ -114,8 +114,10 @@ char *ast_sip_get_debug(void) return res; } -int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery) +int ast_sip_initialize_sorcery_global(void) { + struct ast_sorcery *sorcery = ast_sip_get_sorcery(); + snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version()); ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global"); diff --git a/res/res_pjsip/config_transport.c b/res/res_pjsip/config_transport.c index 370e49cb0..3c11dcc70 100644 --- a/res/res_pjsip/config_transport.c +++ b/res/res_pjsip/config_transport.c @@ -22,6 +22,7 @@ #include <pjlib.h> #include "asterisk/res_pjsip.h" +#include "asterisk/res_pjsip_cli.h" #include "asterisk/logger.h" #include "asterisk/astobj2.h" #include "asterisk/sorcery.h" @@ -276,8 +277,9 @@ static int transport_bind_handler(const struct aco_option *opt, struct ast_varia { struct ast_sip_transport *transport = obj; pj_str_t buf; + int rc = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &transport->host); - return (pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &transport->host) != PJ_SUCCESS) ? -1 : 0; + return rc != PJ_SUCCESS ? -1 : 0; } static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf) @@ -446,9 +448,115 @@ static int localnet_to_str(const void *obj, const intptr_t *args, char **buf) return 0; } +static struct ao2_container *cli_get_container(void) +{ + RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup); + + container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!container) { + return NULL; + } + + s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!s_container) { + return NULL; + } + + if (ao2_container_dup(s_container, container, 0)) { + return NULL; + } + ao2_ref(s_container, +1); + return s_container; +} + +static int cli_iterator(const void *container, ao2_callback_fn callback, void *args) +{ + const struct ast_sip_endpoint *endpoint = container; + struct ast_sip_transport *transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), + "transport", endpoint->transport); + + if (!transport) { + return -1; + } + return callback(transport, args, 0); +} + +static int cli_print_header(void *obj, void *arg, int flags) +{ + struct ast_sip_cli_context *context = arg; + int indent = CLI_INDENT_TO_SPACES(context->indent_level); + int filler = CLI_MAX_WIDTH - indent - 61; + + if (!context->output_buffer) { + return -1; + } + + ast_str_append(&context->output_buffer, 0, + "%*s: <TransportId........> <Type> <cos> <tos> <BindAddress%*.*s>\n", + indent, "Transport", filler, filler, CLI_HEADER_FILLER); + + return 0; +} + +static int cli_print_body(void *obj, void *arg, int flags) +{ + struct ast_sip_transport *transport = obj; + struct ast_sip_cli_context *context = arg; + char hoststr[PJ_INET6_ADDRSTRLEN]; + + if (!context->output_buffer) { + return -1; + } + + pj_sockaddr_print(&transport->host, hoststr, sizeof(hoststr), 3); + + ast_str_append(&context->output_buffer, 0, "%*s: %-21s %6s %5x %5x %s\n", + CLI_INDENT_TO_SPACES(context->indent_level), "Transport", + ast_sorcery_object_get_id(transport), + ARRAY_IN_BOUNDS(transport->type, transport_types) ? transport_types[transport->type] : "Unknown", + transport->cos, transport->tos, hoststr); + + if (context->show_details + || (context->show_details_only_level_0 && context->indent_level == 0)) { + ast_str_append(&context->output_buffer, 0, "\n"); + ast_sip_cli_print_sorcery_objectset(transport, context, 0); + } + + return 0; +} + +static struct ast_sip_cli_formatter_entry cli_formatter = { + .name = "transport", + .print_header = cli_print_header, + .print_body = cli_print_body, + .get_container = cli_get_container, + .iterator = cli_iterator, + .comparator = ast_sorcery_object_id_compare, +}; + +static struct ast_cli_entry cli_commands[] = { + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Transports", + .command = "pjsip list transports", + .usage = "Usage: pjsip list transports\n" + " List the configured PJSIP Transports\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transports", + .command = "pjsip show transports", + .usage = "Usage: pjsip show transports\n" + " Show the configured PJSIP Transport\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transport", + .command = "pjsip show transport", + .usage = "Usage: pjsip show transport <id>\n" + " Show the configured PJSIP Transport\n"), +}; + /*! \brief Initialize sorcery with transport support */ -int ast_sip_initialize_sorcery_transport(struct ast_sorcery *sorcery) +int ast_sip_initialize_sorcery_transport(void) { + struct ast_sorcery *sorcery = ast_sip_get_sorcery(); + ast_sorcery_apply_default(sorcery, "transport", "config", "pjsip.conf,criteria=type=transport"); if (ast_sorcery_object_register_no_reload(sorcery, "transport", transport_alloc, NULL, transport_apply)) { @@ -477,5 +585,15 @@ int ast_sip_initialize_sorcery_transport(struct ast_sorcery *sorcery) ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos)); ast_sip_register_endpoint_formatter(&endpoint_transport_formatter); + ast_sip_register_cli_formatter(&cli_formatter); + ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); + + return 0; +} + +int ast_sip_destroy_sorcery_transport(void) +{ + ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); + ast_sip_unregister_cli_formatter(&cli_formatter); return 0; } diff --git a/res/res_pjsip/include/res_pjsip_private.h b/res/res_pjsip/include/res_pjsip_private.h index aee6e3e3f..87ff7f3ac 100644 --- a/res/res_pjsip/include/res_pjsip_private.h +++ b/res/res_pjsip/include/res_pjsip_private.h @@ -99,7 +99,7 @@ char *ast_sip_global_default_outbound_endpoint(void); /*! * \brief Functions for initializing and destroying the CLI. */ -int ast_sip_initialize_cli(struct ast_sorcery *sip_sorcery); +int ast_sip_initialize_cli(void); void ast_sip_destroy_cli(void); #endif /* RES_PJSIP_PRIVATE_H_ */ diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c index 499ee9a6c..dd9037f60 100644 --- a/res/res_pjsip/location.c +++ b/res/res_pjsip/location.c @@ -97,7 +97,7 @@ static int contact_link_static(void *obj, void *arg, int flags) { struct ao2_container *dest = arg; - ao2_link_flags(dest, obj, OBJ_NOLOCK); + ao2_link(dest, obj); return 0; } @@ -117,7 +117,7 @@ struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct return NULL; } - contact = ao2_callback(contacts, OBJ_NOLOCK, contact_find_first, NULL); + contact = ao2_callback(contacts, 0, contact_find_first, NULL); return contact; } @@ -134,11 +134,11 @@ struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_si } /* Prune any expired contacts and delete them, we do this first because static contacts can never expire */ - ao2_callback(contacts, OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL); + ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL); /* Add any permanent contacts from the AOR */ if (aor->permanent_contacts) { - ao2_callback(aor->permanent_contacts, OBJ_NOLOCK | OBJ_NODATA, contact_link_static, contacts); + ao2_callback(aor->permanent_contacts, OBJ_NODATA, contact_link_static, contacts); } return contacts; @@ -269,7 +269,7 @@ static int permanent_uri_handler(const struct aco_option *opt, struct ast_variab } ast_string_field_set(contact, uri, var->value); - ao2_link_flags(aor->permanent_contacts, contact, OBJ_NOLOCK); + ao2_link(aor->permanent_contacts, contact); return 0; } @@ -299,33 +299,7 @@ int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg) return 0; } -static void destroy_contact_pair(void *obj) -{ - struct ast_sip_aor_contact_pair *pair = obj; - ao2_cleanup(pair->aor); - ao2_cleanup(pair->contact); -} - -static struct ast_sip_aor_contact_pair *create_contact_pair( - struct ast_sip_aor *aor, struct ast_sip_contact *contact) -{ - struct ast_sip_aor_contact_pair *pair = ao2_alloc( - sizeof(*pair), destroy_contact_pair); - - if (!pair) { - return NULL; - } - - pair->aor = aor; - pair->contact = contact; - - ao2_ref(pair->aor, +1); - ao2_ref(pair->contact, +1); - - return pair; -} - -int ast_sip_for_each_contact(struct ast_sip_aor *aor, +int ast_sip_for_each_contact(const struct ast_sip_aor *aor, ao2_callback_fn on_contact, void *arg) { RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); @@ -340,10 +314,9 @@ int ast_sip_for_each_contact(struct ast_sip_aor *aor, i = ao2_iterator_init(contacts, 0); while ((contact = ao2_iterator_next(&i))) { int res; - RAII_VAR(struct ast_sip_aor_contact_pair *, - acp, create_contact_pair(aor, contact), ao2_cleanup); - if (!acp || (res = on_contact(acp, arg, 0))) { + ast_sorcery_object_set_extended(contact, "@aor_id", ast_sorcery_object_get_id(aor)); + if ((res = on_contact(contact, arg, 0))) { ao2_iterator_destroy(&i); return -1; } @@ -354,11 +327,11 @@ int ast_sip_for_each_contact(struct ast_sip_aor *aor, int ast_sip_contact_to_str(void *object, void *arg, int flags) { - struct ast_sip_aor_contact_pair *acp = object; + struct ast_sip_contact *contact = object; struct ast_str **buf = arg; ast_str_append(buf, 0, "%s/%s,", - ast_sorcery_object_get_id(acp->aor), acp->contact->uri); + ast_sorcery_object_get_extended(contact, "aor_id"), contact->uri); return 0; } @@ -413,15 +386,40 @@ struct ast_sip_endpoint_formatter endpoint_aor_formatter = { .format_ami = format_ami_endpoint_aor }; +static struct ao2_container *cli_get_aor_container(void) +{ + RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup); + + container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "aor", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!container) { + return NULL; + } + + s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!s_container) { + return NULL; + } + + if (ao2_container_dup(s_container, container, 0)) { + return NULL; + } + ao2_ref(s_container, +1); + return s_container; +} + static int populate_contact_container(void *obj, void *arg, int flags) { - struct ast_sip_aor_contact_pair *acp = obj; + struct ast_sip_contact *contact = obj; struct ao2_container *container = arg; - ao2_link_flags(container, acp, OBJ_NOLOCK); + + ao2_link(container, contact); return 0; } -static int gather_aor_channels(void *obj, void *arg, int flags) +static int gather_aor_contacts(void *obj, void *arg, int flags) { struct ast_sip_aor *aor = obj; struct ao2_container *container = arg; @@ -429,35 +427,51 @@ static int gather_aor_channels(void *obj, void *arg, int flags) return 0; } -static struct ao2_container *cli_get_contact_container(struct ast_sorcery *sip_sorcery) +static int cli_contact_compare(const void *left, const void *right, int flags) +{ + const struct ast_sip_contact *left_contact = left; + const struct ast_sip_contact *right_contact = right; + int rc; + + if (!left_contact || !right_contact) { + return 0; + } + rc = strcmp(ast_sorcery_object_get_extended(left_contact, "aor_id"), + ast_sorcery_object_get_extended(right_contact, "aor_id")); + if (rc) { + return rc; + } + return strcmp(left_contact->uri, right_contact->uri); +} + +static struct ao2_container *cli_get_contact_container(void) { RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup); - RAII_VAR(struct ao2_container *, s_parent_container, NULL, ao2_cleanup); struct ao2_container *child_container; - parent_container = ast_sorcery_retrieve_by_fields(sip_sorcery, "aor", - AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + parent_container = cli_get_aor_container(); if (!parent_container) { return NULL; } - s_parent_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, &ast_sorcery_object_id_compare, NULL); - if (!s_parent_container) { - return NULL; - } - - ao2_container_dup(s_parent_container, parent_container, OBJ_ORDER_ASCENDING); - - child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL); + child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + cli_contact_compare, NULL); if (!child_container) { return NULL; } - ao2_callback(s_parent_container, OBJ_NODATA, gather_aor_channels, child_container); + ao2_ref(child_container, +1); + ao2_callback(parent_container, OBJ_NODATA, gather_aor_contacts, child_container); return child_container; } +static int cli_contact_iterator(const void *container, ao2_callback_fn callback, void *args) +{ + const struct ast_sip_aor *array = container; + + return ast_sip_for_each_contact(array, callback, args); +} static int cli_print_contact_header(void *obj, void *arg, int flags) { @@ -477,28 +491,25 @@ static int cli_print_contact_header(void *obj, void *arg, int flags) static int cli_print_contact_body(void *obj, void *arg, int flags) { - struct ast_sip_aor_contact_pair *acp = obj; + struct ast_sip_contact *contact = obj; struct ast_sip_cli_context *context = arg; char *print_name = NULL; int print_name_len; int indent; int flexwidth; + const char *aor_id = ast_sorcery_object_get_extended(contact, "aor_id"); RAII_VAR(struct ast_sip_contact_status *, status, - ast_sorcery_retrieve_by_id( ast_sip_get_sorcery(), CONTACT_STATUS, ast_sorcery_object_get_id(acp->contact)), + ast_sorcery_retrieve_by_id( ast_sip_get_sorcery(), CONTACT_STATUS, ast_sorcery_object_get_id(contact)), ao2_cleanup); if (!context->output_buffer) { return -1; } - print_name_len = strlen(ast_sorcery_object_get_id(acp->aor)) - + strlen(acp->contact->uri) + 2; - if (!(print_name = alloca(print_name_len))) { - return -1; - } - snprintf(print_name, print_name_len, "%s/%s", - ast_sorcery_object_get_id(acp->aor), acp->contact->uri); + print_name_len = strlen(aor_id) + strlen(contact->uri) + 2; + print_name = ast_alloca(print_name_len); + snprintf(print_name, print_name_len, "%s/%s", aor_id, contact->uri); indent = CLI_INDENT_TO_SPACES(context->indent_level); flexwidth = CLI_LAST_TABSTOP - indent - 2; @@ -514,10 +525,11 @@ static int cli_print_contact_body(void *obj, void *arg, int flags) return 0; } -static struct ao2_container *cli_get_aor_container(struct ast_sorcery *sip_sorcery) +static int cli_aor_iterator(const void *container, ao2_callback_fn callback, void *args) { - return ast_sorcery_retrieve_by_fields(sip_sorcery, "aor", - AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + const char *aor_list = container; + + return ast_sip_for_each_aor(aor_list, callback, args); } static int cli_print_aor_header(void *obj, void *arg, int flags) @@ -538,7 +550,7 @@ static int cli_print_aor_header(void *obj, void *arg, int flags) if (context->recurse) { context->indent_level++; formatter_entry = ast_sip_lookup_cli_formatter("contact"); - if (formatter_entry) { + if (formatter_entry && formatter_entry->print_header) { formatter_entry->print_header(NULL, context, 0); } context->indent_level--; @@ -571,11 +583,17 @@ static int cli_print_aor_body(void *obj, void *arg, int flags) if (context->recurse) { context->indent_level++; + formatter_entry = ast_sip_lookup_cli_formatter("contact"); - if (formatter_entry) { - ast_sip_for_each_contact(aor, formatter_entry->print_body, context); + if (formatter_entry && formatter_entry->print_body && formatter_entry->iterator) { + formatter_entry->iterator(aor, formatter_entry->print_body, context); } + context->indent_level--; + + if (context->indent_level == 0) { + ast_str_append(&context->output_buffer, 0, "\n"); + } } if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) { @@ -591,6 +609,8 @@ static struct ast_sip_cli_formatter_entry cli_contact_formatter = { .print_header = cli_print_contact_header, .print_body = cli_print_contact_body, .get_container = cli_get_contact_container, + .iterator = cli_contact_iterator, + .comparator = cli_contact_compare, }; static struct ast_sip_cli_formatter_entry cli_aor_formatter = { @@ -598,11 +618,34 @@ static struct ast_sip_cli_formatter_entry cli_aor_formatter = { .print_header = cli_print_aor_header, .print_body = cli_print_aor_body, .get_container = cli_get_aor_container, + .iterator = cli_aor_iterator, + .comparator = ast_sorcery_object_id_compare, +}; + +static struct ast_cli_entry cli_commands[] = { + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Aors", + .command = "pjsip list aors", + .usage = "Usage: pjsip list aors\n" + " List the configured PJSIP Aors\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aors", + .command = "pjsip show aors", + .usage = "Usage: pjsip show aors\n" + " Show the configured PJSIP Aors\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aor", + .command = "pjsip show aor", + .usage = "Usage: pjsip show aor <id>\n" + " Show the configured PJSIP Aor\n"), + + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Contacts", + .command = "pjsip list contacts", + .usage = "Usage: pjsip list contacts\n" + " List the configured PJSIP contacts\n"), }; /*! \brief Initialize sorcery with location support */ -int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery) +int ast_sip_initialize_sorcery_location(void) { + struct ast_sorcery *sorcery = ast_sip_get_sorcery(); ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar"); ast_sorcery_apply_default(sorcery, "aor", "config", "pjsip.conf,criteria=type=aor"); @@ -635,6 +678,15 @@ int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery) ast_sip_register_endpoint_formatter(&endpoint_aor_formatter); ast_sip_register_cli_formatter(&cli_contact_formatter); ast_sip_register_cli_formatter(&cli_aor_formatter); + ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); + return 0; +} + +int ast_sip_destroy_sorcery_location(void) +{ + ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); + ast_sip_unregister_cli_formatter(&cli_contact_formatter); + ast_sip_unregister_cli_formatter(&cli_aor_formatter); return 0; } diff --git a/res/res_pjsip/pjsip_cli.c b/res/res_pjsip/pjsip_cli.c index 01d6494bf..6abd62983 100644 --- a/res/res_pjsip/pjsip_cli.c +++ b/res/res_pjsip/pjsip_cli.c @@ -33,8 +33,6 @@ static struct ast_hashtab *formatter_registry; -static struct ast_sorcery *sip_sorcery; - struct ast_sip_cli_formatter_entry *ast_sip_lookup_cli_formatter(const char *name) { struct ast_sip_cli_formatter_entry fake_entry = { @@ -115,21 +113,20 @@ static char *complete_show_sorcery_object(struct ao2_container *container, return result; } -static void dump_str_and_free(int fd, struct ast_str *buf) { +static void dump_str_and_free(int fd, struct ast_str *buf) +{ ast_cli(fd, "%s", ast_str_buffer(buf)); ast_free(buf); } -static char *cli_traverse_objects(struct ast_cli_entry *e, int cmd, - struct ast_cli_args *a) +char *ast_sip_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); - RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup); RAII_VAR(void *, object, NULL, ao2_cleanup); int is_container = 0; - const char *cmd1 = NULL; - const char *cmd2 = NULL; - const char *object_id = NULL; + const char *cmd1; + const char *cmd2; + const char *object_id; char formatter_type[64]; struct ast_sip_cli_formatter_entry *formatter_entry; @@ -154,9 +151,10 @@ static char *cli_traverse_objects(struct ast_cli_entry *e, int cmd, object_id = a->argv[3]; if (!ast_ends_with(cmd2, "s")) { - ast_copy_string(formatter_type, cmd2, strlen(cmd2)+1); + ast_copy_string(formatter_type, cmd2, sizeof(formatter_type)); is_container = 0; } else { + /* Take the plural "s" off of the object name. */ ast_copy_string(formatter_type, cmd2, strlen(cmd2)); is_container = 1; } @@ -182,18 +180,21 @@ static char *cli_traverse_objects(struct ast_cli_entry *e, int cmd, formatter_entry = ast_sip_lookup_cli_formatter(formatter_type); if (!formatter_entry) { - ast_log(LOG_ERROR, "CLI TRAVERSE failure. No container found for object type %s\n", formatter_type); + ast_log(LOG_ERROR, "No formatter registered for object type %s.\n", + formatter_type); ast_free(context.output_buffer); return CLI_FAILURE; } ast_str_append(&context.output_buffer, 0, "\n"); formatter_entry->print_header(NULL, &context, 0); - ast_str_append(&context.output_buffer, 0, " =========================================================================================\n\n"); + ast_str_append(&context.output_buffer, 0, + " =========================================================================================\n\n"); if (is_container || cmd == CLI_GENERATE) { - container = formatter_entry->get_container(sip_sorcery); + container = formatter_entry->get_container(); if (!container) { - ast_cli(a->fd, "CLI TRAVERSE failure. No container found for object type %s\n", formatter_type); + ast_cli(a->fd, "No container returned for object type %s.\n", + formatter_type); ast_free(context.output_buffer); return CLI_FAILURE; } @@ -210,23 +211,20 @@ static char *cli_traverse_objects(struct ast_cli_entry *e, int cmd, ast_cli(a->fd, "No objects found.\n\n"); return CLI_SUCCESS; } - - if (!strcmp(formatter_type, "channel") || !strcmp(formatter_type, "contact")) { - s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL); - } else { - s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, &ast_sorcery_object_id_compare, NULL); - } - - ao2_container_dup(s_container, container, OBJ_ORDER_ASCENDING); - - ao2_callback(s_container, OBJ_NODATA, formatter_entry->print_body, &context); + ao2_callback(container, OBJ_NODATA, formatter_entry->print_body, &context); } else { - if (!(object = ast_sorcery_retrieve_by_id( - ast_sip_get_sorcery(), formatter_type, object_id))) { + if (ast_strlen_zero(object_id)) { dump_str_and_free(a->fd, context.output_buffer); - ast_cli(a->fd, "Unable to retrieve object %s\n", object_id); + ast_cli(a->fd, "No object specified.\n"); return CLI_FAILURE; } + object = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), formatter_type, + object_id); + if (!object) { + dump_str_and_free(a->fd, context.output_buffer); + ast_cli(a->fd, "Unable to find object %s.\n\n", object_id); + return CLI_SUCCESS; + } formatter_entry->print_body(object, &context, 0); } @@ -235,40 +233,8 @@ static char *cli_traverse_objects(struct ast_cli_entry *e, int cmd, return CLI_SUCCESS; } -static struct ast_cli_entry cli_commands[] = { - AST_CLI_DEFINE(cli_traverse_objects, "List PJSIP Channels", .command = "pjsip list channels", - .usage = "Usage: pjsip list channels\n List the active PJSIP channels\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Channels", .command = "pjsip show channels", - .usage = "Usage: pjsip show channels\n List(detailed) the active PJSIP channels\n"), - - AST_CLI_DEFINE(cli_traverse_objects, "List PJSIP Aors", .command = "pjsip list aors", - .usage = "Usage: pjsip list aors\n List the configured PJSIP Aors\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Aors", .command = "pjsip show aors", - .usage = "Usage: pjsip show aors\n Show the configured PJSIP Aors\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Aor", .command = "pjsip show aor", - .usage = "Usage: pjsip show aor\n Show the configured PJSIP Aor\n"), - - AST_CLI_DEFINE(cli_traverse_objects, "List PJSIP Contacts", .command = "pjsip list contacts", - .usage = "Usage: pjsip list contacts\n List the configured PJSIP contacts\n"), - - AST_CLI_DEFINE(cli_traverse_objects, "List PJSIP Endpoints", .command = "pjsip list endpoints", - .usage = "Usage: pjsip list endpoints\n List the configured PJSIP endpoints\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Endpoints", .command = "pjsip show endpoints", - .usage = "Usage: pjsip show endpoints\n List(detailed) the configured PJSIP endpoints\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Endpoint", .command = "pjsip show endpoint", - .usage = "Usage: pjsip show endpoint <id>\n Show the configured PJSIP endpoint\n"), - - AST_CLI_DEFINE(cli_traverse_objects, "List PJSIP Auths", .command = "pjsip list auths", - .usage = "Usage: pjsip list auths\n List the configured PJSIP Auths\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Auths", .command = "pjsip show auths", - .usage = "Usage: pjsip show auths\n Show the configured PJSIP Auths\n"), - AST_CLI_DEFINE(cli_traverse_objects, "Show PJSIP Auth", .command = "pjsip show auth", - .usage = "Usage: pjsip show auth\n Show the configured PJSIP Auth\n"), - -}; - - -static int compare_formatters(const void *a, const void *b) { +static int compare_formatters(const void *a, const void *b) +{ const struct ast_sip_cli_formatter_entry *afe = a; const struct ast_sip_cli_formatter_entry *bfe = b; if (!afe || !bfe) { @@ -278,18 +244,22 @@ static int compare_formatters(const void *a, const void *b) { return strcmp(afe->name, bfe->name); } -static unsigned int hash_formatters(const void *a) { +static unsigned int hash_formatters(const void *a) +{ const struct ast_sip_cli_formatter_entry *afe = a; return ast_hashtab_hash_string(afe->name); } -int ast_sip_register_cli_formatter(struct ast_sip_cli_formatter_entry *formatter) { +int ast_sip_register_cli_formatter(struct ast_sip_cli_formatter_entry *formatter) +{ ast_hashtab_insert_safe(formatter_registry, formatter); return 0; } -int ast_sip_unregister_cli_formatter(struct ast_sip_cli_formatter_entry *formatter) { +int ast_sip_unregister_cli_formatter(struct ast_sip_cli_formatter_entry *formatter) +{ struct ast_sip_cli_formatter_entry *entry = ast_hashtab_lookup(formatter_registry, formatter); + if (!entry) { return -1; } @@ -297,7 +267,7 @@ int ast_sip_unregister_cli_formatter(struct ast_sip_cli_formatter_entry *formatt return 0; } -int ast_sip_initialize_cli(struct ast_sorcery *sorcery) +int ast_sip_initialize_cli(void) { formatter_registry = ast_hashtab_create(17, compare_formatters, ast_hashtab_resize_java, ast_hashtab_newsize_java, hash_formatters, 0); @@ -306,18 +276,11 @@ int ast_sip_initialize_cli(struct ast_sorcery *sorcery) return -1; } - if (ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands))) { - ast_log(LOG_ERROR, "Failed to register pjsip cli commands.\n"); - ast_hashtab_destroy(formatter_registry, ast_free_ptr); - return -1; - } - sip_sorcery = sorcery; return 0; } void ast_sip_destroy_cli(void) { - ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); if (formatter_registry) { ast_hashtab_destroy(formatter_registry, ast_free_ptr); } diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c index c21582e1b..deb374e6e 100644 --- a/res/res_pjsip/pjsip_configuration.c +++ b/res/res_pjsip/pjsip_configuration.c @@ -1168,48 +1168,83 @@ static int ami_show_endpoints(struct mansession *s, const struct message *m) return 0; } -static int populate_channel_container(void *obj, void *arg, int flags) { +static struct ao2_container *cli_get_endpoint_container(void) +{ + RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup); + + container = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!container) { + return NULL; + } + + s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!s_container) { + return NULL; + } + + if (ao2_container_dup(s_container, container, 0)) { + return NULL; + } + ao2_ref(s_container, +1); + return s_container; +} + +static int populate_channel_container(void *obj, void *arg, int flags) +{ struct ast_channel_snapshot *snapshot = obj; struct ao2_container *container = arg; - ao2_link_flags(container, snapshot, OBJ_NOLOCK); + + ao2_link(container, snapshot); return 0; } -static int gather_endpoint_channels(void *obj, void *arg, int flags) { +static int cli_channel_iterator(const void *container, ao2_callback_fn callback, void *args) +{ + const struct ast_sip_endpoint *array = container; + + return ast_sip_for_each_channel(array, callback, args); +} + +static int gather_endpoint_channels(void *obj, void *arg, int flags) +{ struct ast_sip_endpoint *endpoint = obj; struct ao2_container *channels = arg; + ast_sip_for_each_channel(endpoint, populate_channel_container, channels); return 0; } -static struct ao2_container *cli_get_channel_container(struct ast_sorcery *sip_sorcery) +static int cli_channel_compare(const void *left, const void *right, int flags) { - RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup); - RAII_VAR(struct ao2_container *, s_parent_container, NULL, ao2_cleanup); - struct ao2_container *child_container; + const struct ast_channel_snapshot *left_snap = left; + const struct ast_channel_snapshot *right_snap = right; - parent_container = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", - AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); - if (!parent_container) { - return NULL; + if (!left_snap || !right_snap) { + return 0; } + return strcmp(left_snap->name, right_snap->name); +} - s_parent_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, &ast_sorcery_object_id_compare, NULL); - if (!s_parent_container) { - return NULL; - } +static struct ao2_container *cli_get_channel_container(void) +{ + RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, child_container, NULL, ao2_cleanup); - if (ao2_container_dup(s_parent_container, parent_container, OBJ_ORDER_ASCENDING)) { + parent_container = cli_get_endpoint_container(); + if (!parent_container) { return NULL; } - - child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL); + child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + cli_channel_compare, NULL); if (!child_container) { return NULL; } - ao2_callback(s_parent_container, OBJ_NODATA, gather_endpoint_channels, child_container); - + ao2_callback(parent_container, OBJ_NODATA, gather_endpoint_channels, child_container); + ao2_ref(child_container, +1); return child_container; } @@ -1236,7 +1271,8 @@ static int cli_print_channel_header(void *obj, void *arg, int flags) return 0; } -static int cli_print_channel_body(void *obj, void *arg, int flags) { +static int cli_print_channel_body(void *obj, void *arg, int flags) +{ struct ast_channel_snapshot *snapshot = obj; struct ast_sip_cli_context *context = arg; struct timeval current_time; @@ -1272,7 +1308,8 @@ static int cli_print_channel_body(void *obj, void *arg, int flags) { indent = CLI_INDENT_TO_SPACES(context->indent_level); flexwidth = CLI_LAST_TABSTOP - indent - 25; - ast_str_append(&context->output_buffer, 0, "%*s: %-7s Exten: %-*.*s CLCID: \"%s\" <%s>\n", + ast_str_append(&context->output_buffer, 0, + "%*s: %-7s Exten: %-*.*s CLCID: \"%s\" <%s>\n", indent, "Codec", snapshot->nativeformats, flexwidth, flexwidth, @@ -1281,58 +1318,76 @@ static int cli_print_channel_body(void *obj, void *arg, int flags) { snapshot->connected_number ); context->indent_level--; + if (context->indent_level == 0) { + ast_str_append(&context->output_buffer, 0, "\n"); + } + return 0; } -static struct ao2_container *cli_get_endpoint_container(struct ast_sorcery *sip_sorcery) +static int cli_endpoint_iterator(const void *container, ao2_callback_fn callback, + void *args) { - return ast_sip_get_endpoints(); + struct ao2_container *ao2container = (struct ao2_container *) container; + + ao2_callback(ao2container, OBJ_NODATA, callback, args); + return 0; +} + +static void print_child_header(char *type, struct ast_sip_cli_context *context) +{ + struct ast_sip_cli_formatter_entry *formatter_entry; + + formatter_entry = ast_sip_lookup_cli_formatter(type); + if (formatter_entry && formatter_entry->print_header) { + formatter_entry->print_header(NULL, context, 0); + } } static int cli_print_endpoint_header(void *obj, void *arg, int flags) { struct ast_sip_cli_context *context = arg; - struct ast_sip_cli_formatter_entry *formatter_entry; if (!context->output_buffer) { return -1; } ast_str_append(&context->output_buffer, 0, - " <Endpoint/CID................................................> <State.....> <Channels.>\n"); + " Endpoint: <Endpoint/CID.....................................> <State.....> <Channels.>\n"); if (context->recurse) { context->indent_level++; - formatter_entry = ast_sip_lookup_cli_formatter("auth"); - if (formatter_entry) { - formatter_entry->print_header(NULL, context, 0); - } - formatter_entry = ast_sip_lookup_cli_formatter("aor"); - if (formatter_entry) { - formatter_entry->print_header(NULL, context, 0); - } - formatter_entry = ast_sip_lookup_cli_formatter("identify"); - if (formatter_entry) { - formatter_entry->print_header(NULL, context, 0); - } - formatter_entry = ast_sip_lookup_cli_formatter("channel"); - if (formatter_entry) { - formatter_entry->print_header(NULL, context, 0); - } + print_child_header("auth", context); + print_child_header("aor", context); + print_child_header("transport", context); + print_child_header("identify", context); + print_child_header("channel", context); context->indent_level--; } return 0; } -static int cli_print_endpoint_body(void *obj, void *arg, int flags) { +static void print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context) +{ + struct ast_sip_cli_formatter_entry *formatter_entry; + + formatter_entry = ast_sip_lookup_cli_formatter(type); + if (formatter_entry && formatter_entry->print_body && formatter_entry->iterator) { + formatter_entry->iterator(obj, formatter_entry->print_body, context); + } +} + +static int cli_print_endpoint_body(void *obj, void *arg, int flags) +{ struct ast_sip_endpoint *endpoint = obj; RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup); struct ast_sip_cli_context *context = arg; const char *id = ast_sorcery_object_get_id(endpoint); - struct ast_sip_cli_formatter_entry *formatter_entry; char *print_name = NULL; int print_name_len; char *number = S_COR(endpoint->id.self.number.valid, endpoint->id.self.number.str, NULL); + int indent; + int flexwidth; if (!context->output_buffer) { return -1; @@ -1348,8 +1403,12 @@ static int cli_print_endpoint_body(void *obj, void *arg, int flags) { snprintf(print_name, print_name_len, "%s/%s", id, number); } - ast_str_append(&context->output_buffer, 0, " %-62s %-12.12s %d of %.0f\n", - print_name ? print_name : id, + indent = CLI_INDENT_TO_SPACES(context->indent_level); + flexwidth = CLI_LAST_TABSTOP - indent - 2; + + ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %d of %.0f\n", + indent, "Endpoint", + flexwidth, flexwidth, print_name ? print_name : id, ast_sip_get_device_state(endpoint), endpoint_snapshot->num_channels, (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at : @@ -1359,23 +1418,21 @@ static int cli_print_endpoint_body(void *obj, void *arg, int flags) { if (context->recurse) { context->indent_level++; - formatter_entry = ast_sip_lookup_cli_formatter("auth"); - if (formatter_entry) { - context->auth_direction = "Out"; - ast_sip_for_each_auth(&endpoint->outbound_auths, formatter_entry->print_body, context); - context->auth_direction = "In"; - ast_sip_for_each_auth(&endpoint->inbound_auths, formatter_entry->print_body, context); - } - formatter_entry = ast_sip_lookup_cli_formatter("aor"); - if (formatter_entry) { - ast_sip_for_each_aor(endpoint->aors, formatter_entry->print_body, context); - } - formatter_entry = ast_sip_lookup_cli_formatter("channel"); - if (formatter_entry) { - ast_sip_for_each_channel(endpoint, formatter_entry->print_body, context); - } + context->auth_direction = "Out"; + print_child_body("auth", &endpoint->outbound_auths, context); + context->auth_direction = "In"; + print_child_body("auth", &endpoint->inbound_auths, context); + + print_child_body("aor", endpoint->aors, context); + print_child_body("transport", endpoint, context); + print_child_body("identify", endpoint, context); + print_child_body("channel", endpoint, context); context->indent_level--; + + if (context->indent_level == 0) { + ast_str_append(&context->output_buffer, 0, "\n"); + } } if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) { @@ -1391,6 +1448,8 @@ static struct ast_sip_cli_formatter_entry cli_channel_formatter = { .print_header = cli_print_channel_header, .print_body = cli_print_channel_body, .get_container = cli_get_channel_container, + .iterator = cli_channel_iterator, + .comparator = cli_channel_compare, }; static struct ast_sip_cli_formatter_entry cli_endpoint_formatter = { @@ -1398,8 +1457,33 @@ static struct ast_sip_cli_formatter_entry cli_endpoint_formatter = { .print_header = cli_print_endpoint_header, .print_body = cli_print_endpoint_body, .get_container = cli_get_endpoint_container, + .iterator = cli_endpoint_iterator, + .comparator = ast_sorcery_object_id_compare, }; +static struct ast_cli_entry cli_commands[] = { + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Channels", + .command = "pjsip list channels", + .usage = "Usage: pjsip list channels\n" + " List the active PJSIP channels\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Channels", + .command = "pjsip show channels", + .usage = "Usage: pjsip show channels\n" + " List(detailed) the active PJSIP channels\n"), + + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints", + .command = "pjsip list endpoints", + .usage = "Usage: pjsip list endpoints\n" + " List the configured PJSIP endpoints\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints", + .command = "pjsip show endpoints", + .usage = "Usage: pjsip show endpoints\n" + " List(detailed) the configured PJSIP endpoints\n"), + AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint", + .command = "pjsip show endpoint", + .usage = "Usage: pjsip show endpoint <id>\n" + " Show the configured PJSIP endpoint\n"), +}; int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_module_info) { @@ -1420,12 +1504,9 @@ int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_mod ast_sorcery_apply_config(sip_sorcery, "res_pjsip"); - ast_sip_initialize_cli(sip_sorcery); - ast_sip_register_cli_formatter(&cli_channel_formatter); - ast_sip_register_cli_formatter(&cli_endpoint_formatter); + ast_sip_initialize_cli(); - - if (ast_sip_initialize_sorcery_auth(sip_sorcery)) { + if (ast_sip_initialize_sorcery_auth()) { ast_log(LOG_ERROR, "Failed to register SIP authentication support\n"); ast_sorcery_unref(sip_sorcery); sip_sorcery = NULL; @@ -1527,21 +1608,21 @@ int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_mod ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, 0, 0); ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, 0, 0); - if (ast_sip_initialize_sorcery_transport(sip_sorcery)) { + if (ast_sip_initialize_sorcery_transport()) { ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n"); ast_sorcery_unref(sip_sorcery); sip_sorcery = NULL; return -1; } - if (ast_sip_initialize_sorcery_location(sip_sorcery)) { + if (ast_sip_initialize_sorcery_location()) { ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n"); ast_sorcery_unref(sip_sorcery); sip_sorcery = NULL; return -1; } - if (ast_sip_initialize_sorcery_qualify(sip_sorcery)) { + if (ast_sip_initialize_sorcery_qualify()) { ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n"); ast_sorcery_unref(sip_sorcery); sip_sorcery = NULL; @@ -1550,20 +1631,24 @@ int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_mod ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer); - if (ast_sip_initialize_sorcery_domain_alias(sip_sorcery)) { + if (ast_sip_initialize_sorcery_domain_alias()) { ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n"); ast_sorcery_unref(sip_sorcery); sip_sorcery = NULL; return -1; } - if (ast_sip_initialize_sorcery_global(sip_sorcery)) { + if (ast_sip_initialize_sorcery_global()) { ast_log(LOG_ERROR, "Failed to register SIP Global support\n"); ast_sorcery_unref(sip_sorcery); sip_sorcery = NULL; return -1; } + ast_sip_register_cli_formatter(&cli_channel_formatter); + ast_sip_register_cli_formatter(&cli_endpoint_formatter); + ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); + ast_sorcery_load(sip_sorcery); return 0; @@ -1571,6 +1656,10 @@ int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_mod void ast_res_pjsip_destroy_configuration(void) { + ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); + ast_sip_destroy_sorcery_location(); + ast_sip_destroy_sorcery_auth(); + ast_sip_destroy_sorcery_transport(); ast_manager_unregister(AMI_SHOW_ENDPOINT); ast_manager_unregister(AMI_SHOW_ENDPOINTS); ast_sorcery_unref(sip_sorcery); diff --git a/res/res_pjsip/pjsip_options.c b/res/res_pjsip/pjsip_options.c index 3e745d55f..2fe104aa8 100644 --- a/res/res_pjsip/pjsip_options.c +++ b/res/res_pjsip/pjsip_options.c @@ -576,6 +576,7 @@ static int cli_on_contact(void *obj, void *arg, void *data, int flags) struct ast_sip_contact *contact = obj; struct ast_sip_endpoint *endpoint = data; int *cli_fd = arg; + ast_cli(*cli_fd, " contact %s\n", contact->uri); qualify_contact(endpoint, contact); return 0; @@ -765,8 +766,10 @@ static int sched_qualifies_cmp_fn(void *obj, void *arg, int flags) ast_sorcery_object_get_id(arg)); } -int ast_sip_initialize_sorcery_qualify(struct ast_sorcery *sorcery) +int ast_sip_initialize_sorcery_qualify(void) { + struct ast_sorcery *sorcery = ast_sip_get_sorcery(); + /* initialize sorcery ast_sip_contact_status resource */ ast_sorcery_apply_default(sorcery, CONTACT_STATUS, "memory", NULL); diff --git a/res/res_pjsip_endpoint_identifier_ip.c b/res/res_pjsip_endpoint_identifier_ip.c index d71345b19..d78901e66 100644 --- a/res/res_pjsip_endpoint_identifier_ip.c +++ b/res/res_pjsip_endpoint_identifier_ip.c @@ -27,6 +27,7 @@ #include <pjsip.h> #include "asterisk/res_pjsip.h" +#include "asterisk/res_pjsip_cli.h" #include "asterisk/module.h" #include "asterisk/acl.h" #include "asterisk/manager.h" @@ -204,7 +205,7 @@ static int find_identify_by_endpoint(void *obj, void *arg, int flags) struct ip_identify_match *identify = obj; const char *endpoint_name = arg; - return strcmp(identify->endpoint_name, endpoint_name) ? 0 : CMP_MATCH | CMP_STOP; + return strcmp(identify->endpoint_name, endpoint_name) ? 0 : CMP_MATCH; } static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint, @@ -214,15 +215,15 @@ static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint, RAII_VAR(struct ip_identify_match *, identify, NULL, ao2_cleanup); RAII_VAR(struct ast_str *, buf, NULL, ast_free); - if (!(identifies = ast_sorcery_retrieve_by_fields( - ast_sip_get_sorcery(), "identify", AST_RETRIEVE_FLAG_MULTIPLE | - AST_RETRIEVE_FLAG_ALL, NULL))) { + identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!identifies) { return -1; } - if (!(identify = ao2_callback(identifies, OBJ_NOLOCK, - find_identify_by_endpoint, - (void*)ast_sorcery_object_get_id(endpoint)))) { + identify = ao2_callback(identifies, 0, find_identify_by_endpoint, + (void *) ast_sorcery_object_get_id(endpoint)); + if (!identify) { return 1; } @@ -235,7 +236,7 @@ static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint, } ast_str_append(&buf, 0, "EndpointName: %s\r\n", - ast_sorcery_object_get_id(endpoint)); + ast_sorcery_object_get_id(endpoint)); astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); return 0; @@ -245,6 +246,124 @@ struct ast_sip_endpoint_formatter endpoint_identify_formatter = { .format_ami = format_ami_endpoint_identify }; +static int populate_identify_container(void *obj, void *arg, int flags) +{ + struct ast_sip_ip_identify_match *ident = obj; + struct ao2_container *container = arg; + + ao2_link(container, ident); + return 0; +} + +static int cli_iterator(const void *container, ao2_callback_fn callback, void *args) +{ + const struct ast_sip_endpoint *endpoint = container; + struct ao2_container *identifies; + + struct ast_variable fields = { + .name = "endpoint", + .value = ast_sorcery_object_get_id(endpoint), + .next = NULL, + }; + + identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", + AST_RETRIEVE_FLAG_MULTIPLE, &fields); + if (!identifies) { + return -1; + } + + ao2_callback(identifies, OBJ_NODATA, callback, args); + + return 0; +} + +static int gather_endpoint_identifies(void *obj, void *arg, int flags) +{ + struct ast_sip_endpoint *endpoint = obj; + struct ao2_container *container = arg; + + cli_iterator(endpoint, populate_identify_container, container); + return 0; +} + +static struct ao2_container *cli_get_identify_container(void) +{ + RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, s_parent_container, NULL, ao2_cleanup); + struct ao2_container *child_container; + + parent_container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!parent_container) { + return NULL; + } + + s_parent_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!s_parent_container) { + return NULL; + } + + if (ao2_container_dup(s_parent_container, parent_container, 0)) { + return NULL; + } + + child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!child_container) { + return NULL; + } + + ao2_callback(s_parent_container, OBJ_NODATA, gather_endpoint_identifies, child_container); + ao2_ref(child_container, +1); + return child_container; +} + + +static int cli_print_identify_header(void *obj, void *arg, int flags) +{ + struct ast_sip_cli_context *context = arg; + int indent = CLI_INDENT_TO_SPACES(context->indent_level); + int filler = CLI_MAX_WIDTH - indent - 14; + + if (!context->output_buffer) { + return -1; + } + + ast_str_append(&context->output_buffer, 0, + "%*s: <MatchList%*.*s>\n", + indent, "Identify", filler, filler, CLI_HEADER_FILLER); + + return 0; +} + +static int cli_print_identify_body(void *obj, void *arg, int flags) +{ + RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); + struct ip_identify_match *ident = obj; + struct ast_sip_cli_context *context = arg; + + if (!context->output_buffer || !str) { + return -1; + } + + ast_str_append(&context->output_buffer, 0, "%*s: ", + CLI_INDENT_TO_SPACES(context->indent_level), "Identify"); + ast_ha_join(ident->matches, &str); + ast_str_append(&context->output_buffer, 0, "%s\n", ast_str_buffer(str)); + + return 0; +} + +static struct ast_sip_cli_formatter_entry cli_identify_formatter = { + .name = "identify", + .print_header = cli_print_identify_header, + .print_body = cli_print_identify_body, + .get_container = cli_get_identify_container, + .comparator = ast_sorcery_object_id_compare, + .iterator = cli_iterator, +}; + static int load_module(void) { ast_sorcery_apply_default(ast_sip_get_sorcery(), "identify", "config", "pjsip.conf,criteria=type=identify"); @@ -260,6 +379,7 @@ static int load_module(void) ast_sip_register_endpoint_identifier(&ip_identifier); ast_sip_register_endpoint_formatter(&endpoint_identify_formatter); + ast_sip_register_cli_formatter(&cli_identify_formatter); return AST_MODULE_LOAD_SUCCESS; } @@ -272,6 +392,7 @@ static int reload_module(void) static int unload_module(void) { + ast_sip_unregister_cli_formatter(&cli_identify_formatter); ast_sip_unregister_endpoint_formatter(&endpoint_identify_formatter); ast_sip_unregister_endpoint_identifier(&ip_identifier); return 0; diff --git a/res/res_pjsip_logger.c b/res/res_pjsip_logger.c index c1785b5db..a69ca429c 100644 --- a/res/res_pjsip_logger.c +++ b/res/res_pjsip_logger.c @@ -159,7 +159,7 @@ static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_a if (cmd == CLI_INIT) { e->command = "pjsip set logger {on|off|host}"; e->usage = - "Usage: pjsip set logger {on|off}\n" + "Usage: pjsip set logger {on|off|host <name>}\n" " Enables or disabling logging of SIP packets\n" " read on ports bound to PJSIP transports either\n" " globally or enables logging for an individual\n" diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c index c51904253..c97fb750c 100644 --- a/res/res_pjsip_outbound_registration.c +++ b/res/res_pjsip_outbound_registration.c @@ -28,6 +28,7 @@ #include <pjsip_ua.h> #include "asterisk/res_pjsip.h" +#include "asterisk/res_pjsip_cli.h" #include "asterisk/module.h" #include "asterisk/taskprocessor.h" #include "asterisk/cli.h" @@ -1030,10 +1031,6 @@ static int ami_unregister(struct mansession *s, const struct message *m) return 0; } -static struct ast_cli_entry cli_outbound_registration[] = { - AST_CLI_DEFINE(cli_unregister, "Send a REGISTER request to an outbound registration target with a expiration of 0") -}; - struct sip_ami_outbound { struct ast_sip_ami *ami; int registered; @@ -1113,6 +1110,113 @@ static int ami_show_outbound_registrations(struct mansession *s, return 0; } +static struct ao2_container *cli_get_container(void) +{ + RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup); + + container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!container) { + return NULL; + } + + s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, + ast_sorcery_object_id_compare, NULL); + if (!s_container) { + return NULL; + } + + if (ao2_container_dup(s_container, container, 0)) { + return NULL; + } + ao2_ref(s_container, +1); + return s_container; +} + +static int cli_iterator(const void *container, ao2_callback_fn callback, void *args) +{ + struct ao2_container *ao2container = (struct ao2_container *) container; + + ao2_callback(ao2container, OBJ_NODATA, callback, args); + return 0; +} + +static int cli_print_header(void *obj, void *arg, int flags) +{ + struct ast_sip_cli_context *context = arg; + + if (!context->output_buffer) { + return -1; + } + + ast_str_append(&context->output_buffer, 0, + " <Registration/ServerURI..............................> <Auth..........> <Status.......>\n"); + + return 0; +} + +static int cli_print_body(void *obj, void *arg, int flags) +{ + struct sip_outbound_registration *registration = obj; + struct ast_sip_cli_context *context = arg; + const char *id = ast_sorcery_object_get_id(registration); +#define REGISTRATION_URI_FIELD_LEN 53 + + if (!context->output_buffer) { + return -1; + } + + ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s %-16s %-16s\n", + id, + (int) (REGISTRATION_URI_FIELD_LEN - strlen(id)), + (int) (REGISTRATION_URI_FIELD_LEN - strlen(id)), + registration->server_uri, + AST_VECTOR_SIZE(®istration->outbound_auths) + ? AST_VECTOR_GET(®istration->outbound_auths, 0) + : "n/a", + sip_outbound_registration_status_str[registration->state->client_state->status]); + + if (context->show_details + || (context->show_details_only_level_0 && context->indent_level == 0)) { + ast_str_append(&context->output_buffer, 0, "\n"); + ast_sip_cli_print_sorcery_objectset(registration, context, 0); + } + + return 0; +} + +static struct ast_sip_cli_formatter_entry cli_formatter = { + .name = "registration", + .print_header = cli_print_header, + .print_body = cli_print_body, + .get_container = cli_get_container, + .iterator = cli_iterator, + .comparator = ast_sorcery_object_id_compare, +}; + +/* + * A function pointer to callback needs to be within the + * module in order to avoid problems with an undefined + * symbol when the module is loaded. + */ +static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + return ast_sip_cli_traverse_objects(e, cmd, a); +} + +static struct ast_cli_entry cli_outbound_registration[] = { + AST_CLI_DEFINE(cli_unregister, "Send a REGISTER request to an outbound registration target with a expiration of 0"), + AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Registrations", + .command = "pjsip list registrations", + .usage = "Usage: pjsip list registrations\n" + " List the configured PJSIP Registrations\n"), + AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registration", + .command = "pjsip show registration", + .usage = "Usage: pjsip show registration <id>\n" + " Show the configured PJSIP Registration\n"), +}; + static int load_module(void) { ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration"); @@ -1140,6 +1244,8 @@ static int load_module(void) ast_cli_register_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration)); ast_manager_register_xml("PJSIPUnregister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_unregister); ast_manager_register_xml("PJSIPShowRegistrationsOutbound", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING,ami_show_outbound_registrations); + ast_sip_register_cli_formatter(&cli_formatter); + return AST_MODULE_LOAD_SUCCESS; } @@ -1152,6 +1258,7 @@ static int reload_module(void) static int unload_module(void) { + ast_sip_unregister_cli_formatter(&cli_formatter); ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration)); ast_manager_unregister("PJSIPShowRegistrationsOutbound"); ast_manager_unregister("PJSIPUnregister"); |