summaryrefslogtreecommitdiff
path: root/res/res_pjsip/pjsip_cli.c
blob: 5f8157d2d8dbcc183215ac44c9bb066b8f2db1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Fairview 5 Engineering, LLC
 *
 * George Joseph <george.joseph@fairview5.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

#include "asterisk.h"

#include <pjsip.h>
#include <pjsip_ua.h>
#include "asterisk/res_pjsip.h"
#include "include/res_pjsip_private.h"
#include "asterisk/res_pjsip_cli.h"
#include "asterisk/acl.h"
#include "asterisk/cli.h"
#include "asterisk/astobj2.h"
#include "asterisk/hashtab.h"
#include "asterisk/utils.h"
#include "asterisk/sorcery.h"

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 = {
		.name = name,
	};
	return ast_hashtab_lookup(formatter_registry, &fake_entry);
}

int ast_sip_cli_print_sorcery_objectset(void *obj, void *arg, int flags)
{
	struct ast_sip_cli_context *context = arg;
	struct ast_variable *i;
	int max_name_width = 13;
	int max_value_width = 14;
	int width;
	char *separator;
	struct ast_variable *objset;

	if (!context->output_buffer) {
		return -1;
	}

	objset = ast_sorcery_objectset_create(ast_sip_get_sorcery(),obj);
	if (!objset) {
		return -1;
	}

	for (i = objset; i; i = i->next) {
		if (i->name) {
			width = strlen(i->name);
			max_name_width = width > max_name_width ? width : max_name_width;
		}
		if (i->value) {
			width = strlen(i->value);
			max_value_width = width > max_value_width ? width : max_value_width;
		}
	}

	if (!(separator = alloca(max_name_width + max_value_width + 8))) {
		return -1;
	}
	memset(separator, '=', max_name_width + max_value_width + 3);
	separator[max_name_width + max_value_width + 3] = 0;

	ast_str_append(&context->output_buffer, 0, " %-*s : %s\n", max_name_width, "ParameterName", "ParameterValue");
	ast_str_append(&context->output_buffer, 0, " %s\n", separator);

	objset = ast_variable_list_sort(objset);

	for (i = objset; i; i = i->next) {
		ast_str_append(&context->output_buffer, 0, " %-*s : %s\n", max_name_width, i->name, i->value);
	}

	return 0;
}

static char *complete_show_sorcery_object(struct ao2_container *container,
	const char *word, int state)
{
	char *result = NULL;
	int wordlen = strlen(word);
	int which = 0;

	struct ao2_iterator i = ao2_iterator_init(container, 0);
	void *object;

	while ((object = ao2_t_iterator_next(&i, "iterate thru endpoints table"))) {
		if (!strncasecmp(word, ast_sorcery_object_get_id(object), wordlen)
			&& ++which > state) {
			result = ast_strdup(ast_sorcery_object_get_id(object));
		}
		ao2_t_ref(object, -1, "toss iterator endpoint ptr before break");
		if (result) {
			break;
		}
	}
	ao2_iterator_destroy(&i);
	return result;
}

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)
{
	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;
	char formatter_type[64];
	struct ast_sip_cli_formatter_entry *formatter_entry;
	int is_plural = 0;

	struct ast_sip_cli_context context = {
		.peers_mon_online = 0,
		.peers_mon_offline = 0,
		.peers_unmon_online = 0,
		.peers_unmon_offline = 0,
		.a = a,
		.indent_level = 0,
	};

	if (cmd == CLI_INIT) {
		return NULL;
	}

	cmd1 = e->cmda[1];
	cmd2 = e->cmda[2];
	object_id = a->argv[3];

	if (!ast_ends_with(cmd2, "s")) {
		ast_copy_string(formatter_type, cmd2, strlen(cmd2)+1);
		is_plural = 0;
	} else {
		ast_copy_string(formatter_type, cmd2, strlen(cmd2));
		is_plural = 1;
	}

	if (!strcmp(cmd1, "show")) {
		if (is_plural) {
			context.recurse = 1;
			context.show_details = 0;
			context.show_details_only_level_0 = 0;
			is_container = 1;
		} else {
			context.recurse = 1;
			context.show_details = 0;
			context.show_details_only_level_0 = 1;
			is_container = 0;
		}
	} else {
		context.recurse = 0;
		context.show_details = 0;
		context.show_details_only_level_0 = 0;
		is_container = 1;
	}

	context.output_buffer = ast_str_create(256);
	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);
		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");

	if (is_container || cmd == CLI_GENERATE) {
		container = formatter_entry->get_container(sip_sorcery);
		if (!container) {
			ast_cli(a->fd, "CLI TRAVERSE failure.  No container found for object type %s\n", formatter_type);
			return CLI_FAILURE ;
		}
	}

	if (!is_container && cmd == CLI_GENERATE) {
		return complete_show_sorcery_object(container, a->word, a->n);
	}

	if (is_container) {
		if (!ao2_container_count(container)) {
			dump_str_and_free(a->fd, context.output_buffer);
			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);
	} else {
		if (!(object = ast_sorcery_retrieve_by_id(
			ast_sip_get_sorcery(), formatter_type, object_id))) {
			dump_str_and_free(a->fd, context.output_buffer);
			ast_cli(a->fd, "Unable to retrieve object %s\n", object_id);
			return CLI_FAILURE ;
		}
		formatter_entry->print_body(object, &context, 0);
	}

	ast_str_append(&context.output_buffer, 0, "\n");
	dump_str_and_free(a->fd, context.output_buffer);
	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) {
	const struct ast_sip_cli_formatter_entry *afe = a;
	const struct ast_sip_cli_formatter_entry *bfe = b;
	if (!afe || !bfe) {
		ast_log(LOG_ERROR, "One of the arguments to compare_formatters was NULL\n");
		return -1;
	}
	return strcmp(afe->name, bfe->name);
}

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) {
	ast_hashtab_insert_safe(formatter_registry, formatter);
	return 0;
}

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;
	}
	ast_hashtab_remove_this_object(formatter_registry, entry);
	return 0;
}

int ast_sip_initialize_cli(struct ast_sorcery *sorcery)
{
	formatter_registry = ast_hashtab_create(17, compare_formatters,
		ast_hashtab_resize_java, ast_hashtab_newsize_java, hash_formatters, 0);
	if (!formatter_registry) {
		ast_log(LOG_ERROR, "Unable to create formatter_registry.\n");
		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);
		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);
	}
}