pjsip_cli.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Fairview 5 Engineering, LLC
  5. *
  6. * George Joseph <george.joseph@fairview5.com>
  7. *
  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.
  13. *
  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.
  17. */
  18. #include "asterisk.h"
  19. #include <pjsip.h>
  20. #include <pjsip_ua.h>
  21. #include "asterisk/res_pjsip.h"
  22. #include "include/res_pjsip_private.h"
  23. #include "asterisk/res_pjsip_cli.h"
  24. #include "asterisk/acl.h"
  25. #include "asterisk/cli.h"
  26. #include "asterisk/astobj2.h"
  27. #include "asterisk/hashtab.h"
  28. #include "asterisk/utils.h"
  29. #include "asterisk/sorcery.h"
  30. static struct ao2_container *formatter_registry;
  31. int ast_sip_cli_print_sorcery_objectset(void *obj, void *arg, int flags)
  32. {
  33. struct ast_sip_cli_context *context = arg;
  34. struct ast_variable *i;
  35. int max_name_width = 13;
  36. int max_value_width = 14;
  37. int width;
  38. char *separator;
  39. struct ast_variable *objset;
  40. if (!context->output_buffer) {
  41. return -1;
  42. }
  43. objset = ast_sorcery_objectset_create(ast_sip_get_sorcery(),obj);
  44. if (!objset) {
  45. return -1;
  46. }
  47. for (i = objset; i; i = i->next) {
  48. if (i->name) {
  49. width = strlen(i->name);
  50. max_name_width = width > max_name_width ? width : max_name_width;
  51. }
  52. if (i->value) {
  53. width = strlen(i->value);
  54. max_value_width = width > max_value_width ? width : max_value_width;
  55. }
  56. }
  57. separator = ast_alloca(max_name_width + max_value_width + 8);
  58. memset(separator, '=', max_name_width + max_value_width + 3);
  59. separator[max_name_width + max_value_width + 3] = 0;
  60. ast_str_append(&context->output_buffer, 0, " %-*s : %s\n", max_name_width, "ParameterName", "ParameterValue");
  61. ast_str_append(&context->output_buffer, 0, " %s\n", separator);
  62. objset = ast_variable_list_sort(objset);
  63. for (i = objset; i; i = i->next) {
  64. ast_str_append(&context->output_buffer, 0, " %-*s : %s\n", max_name_width, i->name, i->value);
  65. }
  66. ast_variables_destroy(objset);
  67. return 0;
  68. }
  69. static char *complete_show_sorcery_object(struct ao2_container *container,
  70. struct ast_sip_cli_formatter_entry *formatter_entry,
  71. const char *word, int state)
  72. {
  73. char *result = NULL;
  74. int wordlen = strlen(word);
  75. int which = 0;
  76. struct ao2_iterator i = ao2_iterator_init(container, 0);
  77. void *object;
  78. while ((object = ao2_t_iterator_next(&i, "iterate thru endpoints table"))) {
  79. const char *id = formatter_entry->get_id(object);
  80. if (!strncasecmp(word, id, wordlen)
  81. && ++which > state) {
  82. result = ast_strdup(id);
  83. }
  84. ao2_t_ref(object, -1, "toss iterator endpoint ptr before break");
  85. if (result) {
  86. break;
  87. }
  88. }
  89. ao2_iterator_destroy(&i);
  90. return result;
  91. }
  92. static void dump_str_and_free(int fd, struct ast_str *buf)
  93. {
  94. ast_cli(fd, "%s", ast_str_buffer(buf));
  95. ast_free(buf);
  96. }
  97. char *ast_sip_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  98. {
  99. RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
  100. RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
  101. RAII_VAR(void *, object, NULL, ao2_cleanup);
  102. int is_container = 0;
  103. const char *cmd1;
  104. const char *cmd2;
  105. const char *object_id;
  106. char formatter_type[64];
  107. struct ast_sip_cli_context context = {
  108. .indent_level = 0,
  109. .show_details = 0,
  110. .show_details_only_level_0 = 0,
  111. .recurse = 0,
  112. };
  113. if (cmd == CLI_INIT) {
  114. return NULL;
  115. }
  116. cmd1 = e->cmda[1];
  117. cmd2 = e->cmda[2];
  118. object_id = a->argv[3];
  119. if (!ast_ends_with(cmd2, "s")) {
  120. ast_copy_string(formatter_type, cmd2, sizeof(formatter_type));
  121. is_container = 0;
  122. } else {
  123. /* Take the plural "s" off of the object name. */
  124. ast_copy_string(formatter_type, cmd2, strlen(cmd2));
  125. is_container = 1;
  126. }
  127. if (!strcmp(cmd1, "show")) {
  128. context.show_details_only_level_0 = !is_container;
  129. context.recurse = 1;
  130. } else {
  131. is_container = 1;
  132. }
  133. if (cmd == CLI_GENERATE
  134. && (is_container
  135. || a->argc > 4
  136. || (a->argc == 4 && ast_strlen_zero(a->word)))) {
  137. return CLI_SUCCESS;
  138. }
  139. context.output_buffer = ast_str_create(256);
  140. if (!context.output_buffer) {
  141. return CLI_FAILURE;
  142. }
  143. formatter_entry = ast_sip_lookup_cli_formatter(formatter_type);
  144. if (!formatter_entry) {
  145. ast_log(LOG_ERROR, "No formatter registered for object type %s.\n",
  146. formatter_type);
  147. ast_free(context.output_buffer);
  148. return CLI_FAILURE;
  149. }
  150. ast_str_append(&context.output_buffer, 0, "\n");
  151. formatter_entry->print_header(NULL, &context, 0);
  152. ast_str_append(&context.output_buffer, 0,
  153. " =========================================================================================\n\n");
  154. if (is_container || cmd == CLI_GENERATE) {
  155. container = formatter_entry->get_container();
  156. if (!container) {
  157. ast_cli(a->fd, "No container returned for object type %s.\n",
  158. formatter_type);
  159. ast_free(context.output_buffer);
  160. return CLI_FAILURE;
  161. }
  162. }
  163. if (cmd == CLI_GENERATE) {
  164. ast_free(context.output_buffer);
  165. return complete_show_sorcery_object(container, formatter_entry, a->word, a->n);
  166. }
  167. if (is_container) {
  168. if (!ao2_container_count(container)) {
  169. ast_free(context.output_buffer);
  170. ast_cli(a->fd, "No objects found.\n\n");
  171. return CLI_SUCCESS;
  172. }
  173. ao2_callback(container, OBJ_NODATA, formatter_entry->print_body, &context);
  174. } else {
  175. if (ast_strlen_zero(object_id)) {
  176. ast_free(context.output_buffer);
  177. ast_cli(a->fd, "No object specified.\n");
  178. return CLI_FAILURE;
  179. }
  180. object = formatter_entry->retrieve_by_id(object_id);
  181. if (!object) {
  182. ast_free(context.output_buffer);
  183. ast_cli(a->fd, "Unable to find object %s.\n\n", object_id);
  184. return CLI_SUCCESS;
  185. }
  186. formatter_entry->print_body(object, &context, 0);
  187. }
  188. ast_str_append(&context.output_buffer, 0, "\n");
  189. dump_str_and_free(a->fd, context.output_buffer);
  190. return CLI_SUCCESS;
  191. }
  192. static int formatter_sort(const void *obj, const void *arg, int flags)
  193. {
  194. const struct ast_sip_cli_formatter_entry *left_obj = obj;
  195. const struct ast_sip_cli_formatter_entry *right_obj = arg;
  196. const char *right_key = arg;
  197. int cmp = 0;
  198. switch (flags & OBJ_SEARCH_MASK) {
  199. case OBJ_SEARCH_OBJECT:
  200. right_key = right_obj->name;
  201. /* Fall through */
  202. case OBJ_SEARCH_KEY:
  203. cmp = strcmp(left_obj->name, right_key);
  204. break;
  205. case OBJ_SEARCH_PARTIAL_KEY:
  206. cmp = strncmp(left_obj->name, right_key, strlen(right_key));
  207. break;
  208. default:
  209. cmp = 0;
  210. break;
  211. }
  212. return cmp;
  213. }
  214. static int formatter_compare(void *obj, void *arg, int flags)
  215. {
  216. const struct ast_sip_cli_formatter_entry *left_obj = obj;
  217. const struct ast_sip_cli_formatter_entry *right_obj = arg;
  218. const char *right_key = arg;
  219. int cmp = 0;
  220. switch (flags & OBJ_SEARCH_MASK) {
  221. case OBJ_SEARCH_OBJECT:
  222. right_key = right_obj->name;
  223. /* Fall through */
  224. case OBJ_SEARCH_KEY:
  225. if (strcmp(left_obj->name, right_key) == 0) {;
  226. cmp = CMP_MATCH | CMP_STOP;
  227. }
  228. break;
  229. case OBJ_SEARCH_PARTIAL_KEY:
  230. if (strncmp(left_obj->name, right_key, strlen(right_key)) == 0) {
  231. cmp = CMP_MATCH;
  232. }
  233. break;
  234. default:
  235. cmp = 0;
  236. break;
  237. }
  238. return cmp;
  239. }
  240. static int formatter_hash(const void *obj, int flags)
  241. {
  242. const struct ast_sip_cli_formatter_entry *left_obj = obj;
  243. if (flags & OBJ_SEARCH_OBJECT) {
  244. return ast_str_hash(left_obj->name);
  245. } else if (flags & OBJ_SEARCH_KEY) {
  246. return ast_str_hash(obj);
  247. }
  248. return -1;
  249. }
  250. struct ast_sip_cli_formatter_entry *ast_sip_lookup_cli_formatter(const char *name)
  251. {
  252. return ao2_find(formatter_registry, name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  253. }
  254. int ast_sip_register_cli_formatter(struct ast_sip_cli_formatter_entry *formatter)
  255. {
  256. ast_assert(formatter != NULL);
  257. ast_assert(formatter->name != NULL);
  258. ast_assert(formatter->print_body != NULL);
  259. ast_assert(formatter->print_header != NULL);
  260. ast_assert(formatter->get_container != NULL);
  261. ast_assert(formatter->iterate != NULL);
  262. ast_assert(formatter->get_id != NULL);
  263. ast_assert(formatter->retrieve_by_id != NULL);
  264. ao2_link(formatter_registry, formatter);
  265. return 0;
  266. }
  267. int ast_sip_unregister_cli_formatter(struct ast_sip_cli_formatter_entry *formatter)
  268. {
  269. if (formatter) {
  270. ao2_wrlock(formatter_registry);
  271. if (ao2_ref(formatter, -1) == 2) {
  272. ao2_unlink_flags(formatter_registry, formatter, OBJ_NOLOCK);
  273. }
  274. ao2_unlock(formatter_registry);
  275. }
  276. return 0;
  277. }
  278. int ast_sip_initialize_cli(void)
  279. {
  280. formatter_registry = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 17,
  281. formatter_hash, formatter_sort, formatter_compare);
  282. if (!formatter_registry) {
  283. ast_log(LOG_ERROR, "Unable to create formatter_registry.\n");
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. void ast_sip_destroy_cli(void)
  289. {
  290. ao2_ref(formatter_registry, -1);
  291. }