summaryrefslogtreecommitdiff
path: root/res/res_pjsip_endpoint_identifier_ip.c
diff options
context:
space:
mode:
authorRichard Mudgett <rmudgett@digium.com>2014-02-06 17:55:45 +0000
committerRichard Mudgett <rmudgett@digium.com>2014-02-06 17:55:45 +0000
commitb5ca213e34055724a5cd669938e356569847edcb (patch)
treeae81ec8dc3add7c1ed3cb94c361a205e94a3bcd7 /res/res_pjsip_endpoint_identifier_ip.c
parent8ff02ac951c979b1e5e340b3b1cc05736780be20 (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/res_pjsip_endpoint_identifier_ip.c')
-rw-r--r--res/res_pjsip_endpoint_identifier_ip.c137
1 files changed, 129 insertions, 8 deletions
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;