Debian Codegraph @

res/res_aeap.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Sangoma Technologies Corporation
5 *
6 * Ben Ford <[email protected]>
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
19 /*** MODULEINFO
20 <depend>res_http_websocket</depend>
21 <support_level>core</support_level>
22 ***/
23
24 #include "asterisk.h"
25
26 #include "asterisk/astobj2.h"
27 #include "asterisk/module.h"
28 #include "asterisk/sorcery.h"
29 #include "asterisk/cli.h"
30 #include "asterisk/format.h"
31 #include "asterisk/format_cap.h"
32 #include "asterisk/res_aeap.h"
33
34 #include "res_aeap/general.h"
35
36 /*** DOCUMENTATION
37 <configInfo name="res_aeap" language="en_US">
38 <synopsis>Asterisk External Application Protocol (AEAP) module for Asterisk</synopsis>
39 <configFile name="aeap.conf">
40 <configObject name="client">
41 <since>
42 <version>18.12.0</version>
43 <version>19.4.0</version>
44 </since>
45 <synopsis>AEAP client options</synopsis>
46 <configOption name="type">
47 <since>
48 <version>18.12.0</version>
49 <version>19.4.0</version>
50 </since>
51 <synopsis>Must be of type 'client'.</synopsis>
52 </configOption>
53 <configOption name="url">
54 <since>
55 <version>18.12.0</version>
56 <version>19.4.0</version>
57 </since>
58 <synopsis>The URL of the server to connect to.</synopsis>
59 </configOption>
60 <configOption name="protocol">
61 <since>
62 <version>18.12.0</version>
63 <version>19.4.0</version>
64 </since>
65 <synopsis>The application protocol.</synopsis>
66 </configOption>
67 <configOption name="codecs">
68 <since>
69 <version>18.12.0</version>
70 <version>19.4.0</version>
71 </since>
72 <synopsis>Optional media codec(s)</synopsis>
73 <description><para>
74 If this is specified, Asterisk will use this for codec related negotiations
75 with the external application. Otherwise, Asterisk will default to using the
76 codecs configured on the endpoint.
77 </para></description>
78 </configOption>
79 </configObject>
80 </configFile>
81 </configInfo>
82 ***/
83
84 /* Asterisk External Application Protocol sorcery object */
85 static struct ast_sorcery *aeap_sorcery;
86
87 struct ast_sorcery *ast_aeap_sorcery(void) {
88 return aeap_sorcery;
89 }
90
91 struct ast_aeap_client_config
92 {
93 SORCERY_OBJECT(details);
94 AST_DECLARE_STRING_FIELDS(
95 /*! The URL of the server to connect to */
96 AST_STRING_FIELD(url);
97 /*! The application protocol */
98 AST_STRING_FIELD(protocol);
99 );
100 /*! An optional list of codecs that will be used if provided */
101 struct ast_format_cap *codecs;
102 };
103
104 static void client_config_destructor(void *obj)
105 {
106 struct ast_aeap_client_config *cfg = obj;
107
108 ast_string_field_free_memory(cfg);
109 ao2_cleanup(cfg->codecs);
110 }
111
112 static void *client_config_alloc(const char *name)
113 {
114 struct ast_aeap_client_config *cfg;
115
116 cfg = ast_sorcery_generic_alloc(sizeof(*cfg), client_config_destructor);
117 if (!cfg) {
118 return NULL;
119 }
120
121 if (ast_string_field_init(cfg, 512)) {
122 ao2_ref(cfg, -1);
123 return NULL;
124 }
125
126 if (!(cfg->codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
127 ao2_ref(cfg, -1);
128 return NULL;
129 }
130
131 return cfg;
132 }
133
134 static int client_config_apply(const struct ast_sorcery *sorcery, void *obj)
135 {
136 struct ast_aeap_client_config *cfg = obj;
137
138 if (ast_strlen_zero(cfg->url)) {
139 ast_log(LOG_ERROR, "AEAP - URL must be present for '%s'\n", ast_sorcery_object_get_id(cfg));
140 return -1;
141 }
142
143 if (!ast_begins_with(cfg->url, "ws")) {
144 ast_log(LOG_ERROR, "AEAP - URL must be ws or wss for '%s'\n", ast_sorcery_object_get_id(cfg));
145 return -1;
146 }
147
148 return 0;
149 }
150
151 const struct ast_format_cap *ast_aeap_client_config_codecs(const struct ast_aeap_client_config *cfg)
152 {
153 return cfg->codecs;
154 }
155
156 int ast_aeap_client_config_has_protocol(const struct ast_aeap_client_config *cfg,
157 const char *protocol)
158 {
159 return !strcmp(protocol, cfg->protocol);
160 }
161
162 struct ao2_container *ast_aeap_client_configs_get(const char *protocol)
163 {
164 struct ao2_container *container;
165 struct ast_variable *var;
166
167 var = protocol ? ast_variable_new("protocol ==", protocol, "") : NULL;
168
169 container = ast_sorcery_retrieve_by_fields(aeap_sorcery,
170 AEAP_CONFIG_CLIENT, AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, var);
171
172 ast_variables_destroy(var);
173
174 return container;
175 }
176
177 static struct ast_aeap_client_config *client_config_get(const char *id)
178 {
179 return ast_sorcery_retrieve_by_id(aeap_sorcery, AEAP_CONFIG_CLIENT, id);
180 }
181
182 static char *aeap_tab_complete_name(const char *word, struct ao2_container *container)
183 {
184 void *obj;
185 struct ao2_iterator it;
186 int wordlen = strlen(word);
187 int ret;
188
189 it = ao2_iterator_init(container, 0);
190 while ((obj = ao2_iterator_next(&it))) {
191 if (!strncasecmp(word, ast_sorcery_object_get_id(obj), wordlen)) {
192 ret = ast_cli_completion_add(ast_strdup(ast_sorcery_object_get_id(obj)));
193 if (ret) {
194 ao2_ref(obj, -1);
195 break;
196 }
197 }
198 ao2_ref(obj, -1);
199 }
200 ao2_iterator_destroy(&it);
201
202 ao2_ref(container, -1);
203
204 return NULL;
205 }
206
207 static int aeap_cli_show(void *obj, void *arg, int flags)
208 {
209 struct ast_cli_args *a = arg;
210 struct ast_variable *options;
211 struct ast_variable *i;
212
213 if (!obj) {
214 ast_cli(a->fd, "No AEAP configuration found\n");
215 return 0;
216 }
217
218 options = ast_variable_list_sort(ast_sorcery_objectset_create(aeap_sorcery, obj));
219 if (!options) {
220 return 0;
221 }
222
223 ast_cli(a->fd, "%s: %s\n", ast_sorcery_object_get_type(obj),
224 ast_sorcery_object_get_id(obj));
225
226 for (i = options; i; i = i->next) {
227 ast_cli(a->fd, "\t%s: %s\n", i->name, i->value);
228 }
229
230 ast_cli(a->fd, "\n");
231
232 ast_variables_destroy(options);
233
234 return 0;
235 }
236
237 static char *client_config_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
238 {
239 struct ast_aeap_client_config *cfg;
240
241 switch(cmd) {
242 case CLI_INIT:
243 e->command = "aeap show client";
244 e->usage =
245 "Usage: aeap show client <id>\n"
246 " Show the AEAP settings for a given client\n";
247 return NULL;
248 case CLI_GENERATE:
249 if (a->pos == 3) {
250 return aeap_tab_complete_name(a->word, ast_aeap_client_configs_get(NULL));
251 } else {
252 return NULL;
253 }
254 }
255
256 if (a->argc != 4) {
257 return CLI_SHOWUSAGE;
258 }
259
260 cfg = client_config_get(a->argv[3]);
261 aeap_cli_show(cfg, a, 0);
262 ao2_cleanup(cfg);
263
264 return CLI_SUCCESS;
265 }
266
267 static char *client_config_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
268 {
269 struct ao2_container *container;
270
271 switch(cmd) {
272 case CLI_INIT:
273 e->command = "aeap show clients";
274 e->usage =
275 "Usage: aeap show clients\n"
276 " Show all configured AEAP clients\n";
277 return NULL;
278 case CLI_GENERATE:
279 return NULL;
280 }
281
282 if (a->argc != 3) {
283 return CLI_SHOWUSAGE;
284 }
285
286 container = ast_aeap_client_configs_get(NULL);
287 if (!container || ao2_container_count(container) == 0) {
288 ast_cli(a->fd, "No AEAP clients found\n");
289 ao2_cleanup(container);
290 return CLI_SUCCESS;
291 }
292
293 ao2_callback(container, OBJ_NODATA, aeap_cli_show, a);
294 ao2_ref(container, -1);
295
296 return CLI_SUCCESS;
297 }
298
299 static struct ast_cli_entry aeap_cli[] = {
300 AST_CLI_DEFINE(client_config_show, "Show AEAP client configuration by id"),
301 AST_CLI_DEFINE(client_config_show_all, "Show all AEAP client configurations"),
302 };
303
304 static struct ast_aeap *aeap_create(const char *id, const struct ast_aeap_params *params,
305 int connect, int timeout)
306 {
307 struct ast_aeap_client_config *cfg;
308 struct ast_aeap *aeap;
309 const char *url = NULL;
310 const char *protocol = NULL;
311
312 cfg = client_config_get(id);
313 if (cfg) {
314 url = cfg->url;
315 protocol = cfg->protocol;
316 }
317
318 #ifdef TEST_FRAMEWORK
319 else if (ast_begins_with(id, "_aeap_test_")) {
320 url = "ws://127.0.0.1:8088/ws";
321 protocol = id;
322 }
323 #endif
324
325 if (!url && !protocol) {
326 ast_log(LOG_ERROR, "AEAP: unable to get configuration for '%s'\n", id);
327 return NULL;
328 }
329
330 aeap = connect ? ast_aeap_create_and_connect(url, params, url, protocol, timeout) :
331 ast_aeap_create(url, params);
332
333 ao2_cleanup(cfg);
334 return aeap;
335 }
336
337 struct ast_aeap *ast_aeap_create_by_id(const char *id, const struct ast_aeap_params *params)
338 {
339 return aeap_create(id, params, 0, 0);
340 }
341
342 struct ast_aeap *ast_aeap_create_and_connect_by_id(const char *id,
343 const struct ast_aeap_params *params, int timeout)
344 {
345 return aeap_create(id, params, 1, timeout);
346 }
347
348 struct ast_variable *ast_aeap_custom_fields_get(const char *id)
349 {
350 struct ast_aeap_client_config *cfg;
351 struct ast_variable *vars;
352
353 cfg = client_config_get(id);
354 if (!cfg) {
355 ast_log(LOG_WARNING, "AEAP: no client configuration '%s' to get fields\n", id);
356 return NULL;
357 }
358
359 vars = ast_sorcery_objectset_create(aeap_sorcery, cfg);
360
361 ao2_ref(cfg, -1);
362 return vars;
363 }
364
365 static int reload_module(void)
366 {
367 ast_sorcery_reload(aeap_sorcery);
368
369 return 0;
370 }
371
372 static int unload_module(void)
373 {
374 ast_sorcery_unref(aeap_sorcery);
375 aeap_sorcery = NULL;
376
377 ast_cli_unregister_multiple(aeap_cli, ARRAY_LEN(aeap_cli));
378
379 aeap_general_finalize();
380
381 return 0;
382 }
383
384 static int load_module(void)
385 {
386 if (aeap_general_initialize()) {
387 return AST_MODULE_LOAD_DECLINE;
388 }
389
390 if (!(aeap_sorcery = ast_sorcery_open()))
391 {
392 ast_log(LOG_ERROR, "AEAP - failed to open sorcery\n");
393 return AST_MODULE_LOAD_DECLINE;
394 }
395
396 ast_sorcery_apply_default(aeap_sorcery, AEAP_CONFIG_CLIENT, "config", "aeap.conf,criteria=type=client");
397
398 if (ast_sorcery_object_register(aeap_sorcery, "client", client_config_alloc,
399 NULL, client_config_apply)) {
400 ast_log(LOG_ERROR, "AEAP - failed to register client sorcery object\n");
401 return AST_MODULE_LOAD_DECLINE;
402 }
403
404 ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "type", "", OPT_NOOP_T, 0, 0);
405 ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "url", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_aeap_client_config, url));
406 ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "protocol", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_aeap_client_config, protocol));
407 ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "codecs", "", OPT_CODEC_T, 1, FLDSET(struct ast_aeap_client_config, codecs));
408
409 ast_sorcery_load(aeap_sorcery);
410
411 ast_cli_register_multiple(aeap_cli, ARRAY_LEN(aeap_cli));
412
413 return 0;
414 }
415
416 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
417 "Asterisk External Application Protocol Module for Asterisk",
418 .support_level = AST_MODULE_SUPPORT_CORE,
419 .load = load_module,
420 .unload = unload_module,
421 .reload = reload_module,
422 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
423 .requires = "res_http_websocket",
424 );