res_pjsip_endpoint_identifier_ip: Add 'show identify(ies)' cli commands
authorGeorge Joseph <george.joseph@fairview5.com>
Tue, 2 Dec 2014 00:31:49 +0000 (00:31 +0000)
committerGeorge Joseph <george.joseph@fairview5.com>
Tue, 2 Dec 2014 00:31:49 +0000 (00:31 +0000)
While troubleshooting other things I realized there were no pjsip cli
commands for identify.  This patch adds them.  It also also fixes a
reference leak when a 'show endpoint' displayed identifies and properly
sets the return code if load_module can't allocate a cli formatter structure.

Tested-by: George Joseph

Review: https://reviewboard.asterisk.org/r/4212/
........

Merged revisions 428725 from http://svn.asterisk.org/svn/asterisk/branches/12
........

Merged revisions 428731 from http://svn.asterisk.org/svn/asterisk/branches/13

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@428732 65c4cc65-6c06-0410-ace0-fbb531ad65f3

res/res_pjsip/pjsip_cli.c
res/res_pjsip_endpoint_identifier_ip.c

index 0f0a676..e15e75d 100644 (file)
@@ -144,6 +144,11 @@ char *ast_sip_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_
        if (!ast_ends_with(cmd2, "s")) {
                ast_copy_string(formatter_type, cmd2, sizeof(formatter_type));
                is_container = 0;
+       } else if (ast_ends_with(cmd2, "ies")) {
+               /* Take the plural "ies" off of the object name and re[place with "y". */
+               int l = strlen(cmd2);
+               snprintf(formatter_type, 64, "%*.*sy", l - 3, l - 3, cmd2);
+               is_container = 1;
        } else {
                /* Take the plural "s" off of the object name. */
                ast_copy_string(formatter_type, cmd2, strlen(cmd2));
index aab2c76..4bd4f12 100644 (file)
@@ -323,6 +323,7 @@ static int cli_iterator(void *container, ao2_callback_fn callback, void *args)
        }
 
        ao2_callback(identifies, OBJ_NODATA, callback, args);
+       ao2_cleanup(identifies);
 
        return 0;
 }
@@ -379,14 +380,26 @@ 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 - 14;
+       int filler = CLI_MAX_WIDTH - indent - 22;
 
        ast_assert(context->output_buffer != NULL);
 
        ast_str_append(&context->output_buffer, 0,
-               "%*s:  <MatchList%*.*s>\n",
+               "%*s:  <Identify/Endpoint%*.*s>\n",
                indent, "Identify", filler, filler, CLI_HEADER_FILLER);
 
+       if (context->recurse) {
+               context->indent_level++;
+               indent = CLI_INDENT_TO_SPACES(context->indent_level);
+               filler = CLI_LAST_TABSTOP - indent - 24;
+
+               ast_str_append(&context->output_buffer, 0,
+                       "%*s:  <ip/cidr%*.*s>\n",
+                       indent, "Match", filler, filler, CLI_HEADER_FILLER);
+
+               context->indent_level--;
+       }
+
        return 0;
 }
 
@@ -395,17 +408,71 @@ static int cli_print_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;
+       struct ast_ha *match;
+       int indent;
 
        ast_assert(context->output_buffer != NULL);
 
-       ast_str_append(&context->output_buffer, 0, "%*s:  ",
-               CLI_INDENT_TO_SPACES(context->indent_level), "Identify");
-       ast_ha_join_cidr(ident->matches, &str);
-       ast_str_append(&context->output_buffer, 0, "%s\n", ast_str_buffer(str));
+       ast_str_append(&context->output_buffer, 0, "%*s:  %s/%s\n",
+               CLI_INDENT_TO_SPACES(context->indent_level), "Identify",
+               ast_sorcery_object_get_id(ident), ident->endpoint_name);
+
+       if (context->recurse) {
+               context->indent_level++;
+               indent = CLI_INDENT_TO_SPACES(context->indent_level);
+
+               for (match = ident->matches; match; match = match->next) {
+                       const char *addr = ast_sockaddr_stringify_addr(&match->addr);
+
+                       ast_str_append(&context->output_buffer, 0, "%*s: %s%s/%d\n",
+                               indent,
+                               "Match",
+                               match->sense == AST_SENSE_ALLOW ? "!" : "",
+                               addr, ast_sockaddr_cidr_bits(&match->netmask));
+               }
+
+               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)) {
+               ast_str_append(&context->output_buffer, 0, "\n");
+               ast_sip_cli_print_sorcery_objectset(ident, context, 0);
+       }
 
        return 0;
 }
 
+/*
+ * 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_identify[] = {
+AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Identifies",
+       .command = "pjsip list identifies",
+       .usage = "Usage: pjsip list identifies\n"
+       "       List the configured PJSIP Identifies\n"),
+AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identifies",
+       .command = "pjsip show identifies",
+       .usage = "Usage: pjsip show identifies\n"
+       "       Show the configured PJSIP Identifies\n"),
+AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identify",
+       .command = "pjsip show identify",
+       .usage = "Usage: pjsip show identify <id>\n"
+       "       Show the configured PJSIP Identify\n"),
+};
+
 static struct ast_sip_cli_formatter_entry *cli_formatter;
 
 static int load_module(void)
@@ -430,7 +497,7 @@ static int load_module(void)
        cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
        if (!cli_formatter) {
                ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
-               return -1;
+               return AST_MODULE_LOAD_DECLINE;
        }
        cli_formatter->name = "identify";
        cli_formatter->print_header = cli_print_header;
@@ -441,6 +508,7 @@ static int load_module(void)
        cli_formatter->retrieve_by_id = cli_retrieve_by_id;
 
        ast_sip_register_cli_formatter(cli_formatter);
+       ast_cli_register_multiple(cli_identify, ARRAY_LEN(cli_identify));
 
        return AST_MODULE_LOAD_SUCCESS;
 }
@@ -454,6 +522,7 @@ static int reload_module(void)
 
 static int unload_module(void)
 {
+       ast_cli_unregister_multiple(cli_identify, ARRAY_LEN(cli_identify));
        ast_sip_unregister_cli_formatter(cli_formatter);
        ast_sip_unregister_endpoint_formatter(&endpoint_identify_formatter);
        ast_sip_unregister_endpoint_identifier(&ip_identifier);