Debian Codegraph @

res/res_websocket_client.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2025, Sangoma Technologies Corporation
5 *
6 * George Joseph <[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 <support_level>core</support_level>
21 ***/
22
23 /*** DOCUMENTATION
24 <configInfo name="res_websocket_client" language="en_US">
25 <synopsis>Websocket Client Configuration</synopsis>
26 <configFile name="websocket_client.conf">
27 <configObject name="websocket_client">
28 <since>
29 <version>20.15.0</version>
30 <version>21.10.0</version>
31 <version>22.5.0</version>
32 </since>
33 <synopsis>Websocket Client Configuration</synopsis>
34 <see-also>
35 <ref type="link">/Configuration/Channel-Drivers/WebSocket/</ref>
36 <ref type="link">/Configuration/Interfaces/Asterisk-REST-Interface-ARI/ARI-Outbound-Websockets/</ref>
37 </see-also>
38 <description>
39 <para>
40 These config objects are currently shared by the following Asterisk capabilities:
41 </para>
42 <enumlist>
43 <enum name="chan_websocket"><para>The WebSocket channel driver.</para></enum>
44 <enum name="res_ari"><para>ARI Outbound WebSockets.</para></enum>
45 </enumlist>
46 <para>
47 They may have more specific information or restrictions on the parameters below.
48 </para>
49 <example title="websocket_client.conf">
50 ;
51 ; A connection for use by chan_websocket
52 [media_connection1]
53 type = websocket_client
54 uri = ws://localhost:8787
55 protocols = media
56 username = media_username
57 password = media_password
58 connection_type = per_call_config
59 connection_timeout = 500
60 reconnect_interval = 500
61 reconnect_attempts = 5
62 tls_enabled = no
63 ;
64 ; A TLS connection for use by ARI Outbound Websocket
65 [ari_connection1]
66 type = websocket_client
67 uri = wss://localhost:8765
68 protocols = ari
69 username = some_username
70 password = some_password
71 connection_type = persistent
72 connection_timeout = 500
73 reconnect_interval = 500
74 reconnect_attempts = 5
75 tls_enabled = yes
76 ca_list_file = /etc/pki/tls/cert.pem
77 verify_server_cert = no
78 verify_server_hostname = no
79 </example>
80 </description>
81 <configOption name="type">
82 <since>
83 <version>20.15.0</version>
84 <version>21.10.0</version>
85 <version>22.5.0</version>
86 </since>
87 <synopsis>Must be "websocket_client".</synopsis>
88 </configOption>
89 <configOption name="uri">
90 <since>
91 <version>20.15.0</version>
92 <version>21.10.0</version>
93 <version>22.5.0</version>
94 </since>
95 <synopsis>Full URI to remote server.</synopsis>
96 </configOption>
97 <configOption name="protocols">
98 <since>
99 <version>20.15.0</version>
100 <version>21.10.0</version>
101 <version>22.5.0</version>
102 </since>
103 <synopsis>Comma separated list of protocols acceptable to the server.</synopsis>
104 </configOption>
105 <configOption name="username">
106 <since>
107 <version>20.15.0</version>
108 <version>21.10.0</version>
109 <version>22.5.0</version>
110 </since>
111 <synopsis>Server authentication username if required.</synopsis>
112 </configOption>
113 <configOption name="password">
114 <since>
115 <version>20.15.0</version>
116 <version>21.10.0</version>
117 <version>22.5.0</version>
118 </since>
119 <synopsis>Server authentication password if required.</synopsis>
120 </configOption>
121 <configOption name="connection_type">
122 <since>
123 <version>20.15.0</version>
124 <version>21.10.0</version>
125 <version>22.5.0</version>
126 </since>
127 <synopsis>Single persistent connection or per-call configuration.</synopsis>
128 <description>
129 <enumlist>
130 <enum name="persistent"><para>Single persistent connection for all calls.</para></enum>
131 <enum name="per_call_config"><para>New connection for each call to the Stasis() dialplan app.</para></enum>
132 </enumlist>
133 </description>
134 </configOption>
135 <configOption name="connection_timeout">
136 <since>
137 <version>20.15.0</version>
138 <version>21.10.0</version>
139 <version>22.5.0</version>
140 </since>
141 <synopsis>Connection timeout (ms).</synopsis>
142 </configOption>
143 <configOption name="reconnect_attempts">
144 <since>
145 <version>20.15.0</version>
146 <version>21.10.0</version>
147 <version>22.5.0</version>
148 </since>
149 <synopsis>On failure, how many times should reconnection be attempted?</synopsis>
150 <description>
151 <para>
152 For per_call connections, this is the number of
153 (re)connection attempts to make before returning an
154 and terminating the call. Persistent connections
155 always retry forever but this setting will control
156 how often failure messages are logged.
157 </para>
158 </description>
159 </configOption>
160 <configOption name="reconnect_interval">
161 <since>
162 <version>20.15.0</version>
163 <version>21.10.0</version>
164 <version>22.5.0</version>
165 </since>
166 <synopsis>How often should reconnection be attempted (ms)?</synopsis>
167 </configOption>
168 <configOption name="tls_enabled">
169 <since>
170 <version>20.15.0</version>
171 <version>21.10.0</version>
172 <version>22.5.0</version>
173 </since>
174 <synopsis>Enable TLS</synopsis>
175 </configOption>
176 <configOption name="ca_list_file">
177 <since>
178 <version>20.15.0</version>
179 <version>21.10.0</version>
180 <version>22.5.0</version>
181 </since>
182 <synopsis>File containing the server's CA certificate. (optional)</synopsis>
183 </configOption>
184 <configOption name="ca_list_path">
185 <since>
186 <version>20.15.0</version>
187 <version>21.10.0</version>
188 <version>22.5.0</version>
189 </since>
190 <synopsis>Path to a directory containing one or more hashed CA certificates. (optional)</synopsis>
191 </configOption>
192 <configOption name="cert_file">
193 <since>
194 <version>20.15.0</version>
195 <version>21.10.0</version>
196 <version>22.5.0</version>
197 </since>
198 <synopsis>File containing a client certificate. (optional)</synopsis>
199 </configOption>
200 <configOption name="priv_key_file">
201 <since>
202 <version>20.15.0</version>
203 <version>21.10.0</version>
204 <version>22.5.0</version>
205 </since>
206 <synopsis>File containing the client's private key. (optional)</synopsis>
207 </configOption>
208 <configOption name="verify_server_cert">
209 <since>
210 <version>20.15.0</version>
211 <version>21.10.0</version>
212 <version>22.5.0</version>
213 </since>
214 <synopsis>If set to true, verify the server's certificate. (optional)</synopsis>
215 </configOption>
216 <configOption name="verify_server_hostname">
217 <since>
218 <version>20.15.0</version>
219 <version>21.10.0</version>
220 <version>22.5.0</version>
221 </since>
222 <synopsis>If set to true, verify that the server's hostname matches the common name in it's certificate. (optional)</synopsis>
223 </configOption>
224 </configObject>
225 </configFile>
226 </configInfo>
227 ***/
228
229
230 #include "asterisk.h"
231
232 #include "asterisk/module.h"
233 #include "asterisk/astobj2.h"
234 #include "asterisk/strings.h"
235 #include "asterisk/vector.h"
236 #include "asterisk/websocket_client.h"
237
238 static struct ast_sorcery *sorcery = NULL;
239
240 void ast_websocket_client_add_uri_params(struct ast_websocket_client *wc,
241 const char *uri_params)
242 {
243 ast_string_field_set(wc, uri_params, uri_params);
244 }
245
246 struct ast_websocket *ast_websocket_client_connect(struct ast_websocket_client *wc,
247 void *lock_obj, const char *display_name, enum ast_websocket_result *result)
248 {
249 int reconnect_counter = wc->reconnect_attempts;
250 char *uri = NULL;
251
252 if (ast_strlen_zero(display_name)) {
253 display_name = ast_sorcery_object_get_id(wc);
254 }
255
256 if (!ast_strlen_zero(wc->uri_params)) {
257 /*
258 * If the configured URI doesn't already contain parameters, we append the
259 * new ones to the URI path component with '?'. If it does, we append the
260 * new ones to the existing ones with a '&'.
261 */
262 char sep = '?';
263 uri = ast_alloca(strlen(wc->uri) + strlen(wc->uri_params) + 2);
264 if (strchr(wc->uri, '?')) {
265 sep = '&';
266 }
267 sprintf(uri, "%s%c%s", wc->uri, sep, wc->uri_params); /*Safe */
268 }
269
270 while (1) {
271 struct ast_websocket *astws = NULL;
272 struct ast_websocket_client_options options = {
273 .uri = S_OR(uri, wc->uri),
274 .protocols = wc->protocols,
275 .username = wc->username,
276 .password = wc->password,
277 .timeout = wc->connect_timeout,
278 .suppress_connection_msgs = 1,
279 .tls_cfg = NULL,
280 };
281
282 if (lock_obj) {
283 ao2_lock(lock_obj);
284 }
285
286 if (wc->tls_enabled) {
287 /*
288 * tls_cfg and its contents are freed automatically
289 * by res_http_websocket when the connection ends.
290 * We create it even if tls is not enabled to we can
291 * suppress connection error messages and print our own.
292 */
293 options.tls_cfg = ast_calloc(1, sizeof(*options.tls_cfg));
294 if (!options.tls_cfg) {
295 if (lock_obj) {
296 ao2_unlock(lock_obj);
297 }
298 return NULL;
299 }
300 /* TLS options */
301 options.tls_cfg->enabled = wc->tls_enabled;
302 options.tls_cfg->cafile = ast_strdup(wc->ca_list_file);
303 options.tls_cfg->capath = ast_strdup(wc->ca_list_path);
304 options.tls_cfg->certfile = ast_strdup(wc->cert_file);
305 options.tls_cfg->pvtfile = ast_strdup(wc->priv_key_file);
306 ast_set2_flag(&options.tls_cfg->flags, !wc->verify_server_cert, AST_SSL_DONT_VERIFY_SERVER);
307 ast_set2_flag(&options.tls_cfg->flags, !wc->verify_server_hostname, AST_SSL_IGNORE_COMMON_NAME);
308 }
309
310 astws = ast_websocket_client_create_with_options(&options, result);
311 if (astws && *result == WS_OK) {
312 if (lock_obj) {
313 ao2_unlock(lock_obj);
314 }
315 return astws;
316 }
317
318 reconnect_counter--;
319 if (reconnect_counter <= 0) {
320 if (wc->connection_type == AST_WS_TYPE_CLIENT_PERSISTENT) {
321 ast_log(LOG_WARNING,
322 "%s: Websocket connection to %s failed after %d tries: %s%s%s%s. Retrying in %d ms.\n",
323 display_name,
324 wc->uri,
325 wc->reconnect_attempts,
326 ast_websocket_result_to_str(*result),
327 errno ? " (" : "",
328 errno ? strerror(errno) : "",
329 errno ? ")" : "",
330 wc->reconnect_interval
331 );
332 } else {
333 ast_log(LOG_WARNING,
334 "%s: Websocket connection to %s failed after %d tries: %s%s%s%s. Hanging up after exhausting retries.\n",
335 display_name,
336 wc->uri,
337 wc->reconnect_attempts,
338 ast_websocket_result_to_str(*result),
339 errno ? " (" : "",
340 errno ? strerror(errno) : "",
341 errno ? ")" : ""
342 );
343 }
344 break;
345 }
346
347 if (lock_obj) {
348 ao2_lock(lock_obj);
349 }
350 usleep(wc->reconnect_interval * 1000);
351 }
352
353 return NULL;
354 }
355
356
357
358 static void wc_dtor(void *obj)
359 {
360 struct ast_websocket_client *wc = obj;
361
362 ast_debug(3, "%s: Disposing of websocket client config\n",
363 ast_sorcery_object_get_id(wc));
364 ast_string_field_free_memory(wc);
365 }
366
367 static void *wc_alloc(const char *id)
368 {
369 struct ast_websocket_client *wc = NULL;
370
371 wc = ast_sorcery_generic_alloc(sizeof(*wc), wc_dtor);
372 if (!wc) {
373 return NULL;
374 }
375
376 if (ast_string_field_init(wc, 1024) != 0) {
377 ao2_cleanup(wc);
378 return NULL;
379 }
380
381 if (ast_string_field_init_extended(wc, uri_params) != 0) {
382 ao2_cleanup(wc);
383 return NULL;
384 }
385
386 ast_debug(2, "%s: Allocated websocket client config\n", id);
387 return wc;
388 }
389
390 static int websocket_client_connection_type_from_str(const struct aco_option *opt,
391 struct ast_variable *var, void *obj)
392 {
393 struct ast_websocket_client *ws = obj;
394
395 if (strcasecmp(var->value, "persistent") == 0) {
396 ws->connection_type = AST_WS_TYPE_CLIENT_PERSISTENT;
397 } else if (strcasecmp(var->value, "per_call_config") == 0) {
398 ws->connection_type = AST_WS_TYPE_CLIENT_PER_CALL_CONFIG;
399 } else {
400 return -1;
401 }
402
403 return 0;
404 }
405
406 static int websocket_client_connection_type_to_str(const void *obj, const intptr_t *args, char **buf)
407 {
408 const struct ast_websocket_client *wc = obj;
409
410 if (wc->connection_type == AST_WS_TYPE_CLIENT_PERSISTENT) {
411 *buf = ast_strdup("persistent");
412 } else if (wc->connection_type == AST_WS_TYPE_CLIENT_PER_CALL_CONFIG) {
413 *buf = ast_strdup("per_call_config");
414 } else {
415 return -1;
416 }
417
418 return 0;
419 }
420
421 /*
422 * Can't use INT_MIN because it's an expression
423 * and macro substitutions using stringify can't
424 * handle that.
425 */
426 #define DEFAULT_RECONNECT_ATTEMPTS -2147483648
427
428 static int wc_apply(const struct ast_sorcery *sorcery, void *obj)
429 {
430 struct ast_websocket_client *wc = obj;
431 const char *id = ast_sorcery_object_get_id(wc);
432 int res = 0;
433
434 ast_debug(3, "%s: Applying config\n", id);
435
436 if (ast_strlen_zero(wc->uri)) {
437 ast_log(LOG_WARNING, "%s: Websocket client missing uri\n", id);
438 res = -1;
439 }
440
441 if (res != 0) {
442 ast_log(LOG_WARNING, "%s: Websocket client configuration failed\n", id);
443 } else {
444 ast_debug(3, "%s: Websocket client configuration succeeded\n", id);
445
446 if (wc->reconnect_attempts == DEFAULT_RECONNECT_ATTEMPTS) {
447 if (wc->connection_type == AST_WS_TYPE_CLIENT_PERSISTENT) {
448 wc->reconnect_attempts = INT_MAX;
449 } else {
450 wc->reconnect_attempts = 4;
451 }
452 }
453 }
454
455 return res;
456 }
457
458 struct ao2_container *ast_websocket_client_retrieve_all(void)
459 {
460 if (!sorcery) {
461 return NULL;
462 }
463
464 return ast_sorcery_retrieve_by_fields(sorcery, "websocket_client",
465 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
466 }
467
468 struct ast_websocket_client *ast_websocket_client_retrieve_by_id(const char *id)
469 {
470 if (!sorcery) {
471 return NULL;
472 }
473
474 return ast_sorcery_retrieve_by_id(sorcery, "websocket_client", id);
475 }
476
477 enum ast_ws_client_fields ast_websocket_client_get_field_diff(
478 struct ast_websocket_client *old_wc,
479 struct ast_websocket_client *new_wc)
480 {
481 enum ast_ws_client_fields changed = AST_WS_CLIENT_FIELD_NONE;
482 const char *new_id = ast_sorcery_object_get_id(new_wc);
483 RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
484 struct ast_variable *v = NULL;
485 int res = 0;
486 int changes_found = 0;
487
488 ast_debug(2, "%s: Detecting changes\n", new_id);
489
490 res = ast_sorcery_diff(sorcery, old_wc, new_wc, &changes);
491 if (res != 0) {
492 ast_log(LOG_WARNING, "%s: Failed to create changeset\n", new_id);
493 return AST_WS_CLIENT_FIELD_NONE;
494 }
495
496 for (v = changes; v; v = v->next) {
497 changes_found = 1;
498 ast_debug(2, "%s: %s changed to %s\n", new_id, v->name, v->value);
499 if (ast_strings_equal(v->name, "connection_type")) {
500 changed |= AST_WS_CLIENT_FIELD_CONNECTION_TYPE;
501 } else if (ast_strings_equal(v->name, "uri")) {
502 changed |= AST_WS_CLIENT_FIELD_URI;
503 } else if (ast_strings_equal(v->name, "protocols")) {
504 changed |= AST_WS_CLIENT_FIELD_PROTOCOLS;
505 } else if (ast_strings_equal(v->name, "username")) {
506 changed |= AST_WS_CLIENT_FIELD_USERNAME;
507 } else if (ast_strings_equal(v->name, "password")) {
508 changed |= AST_WS_CLIENT_FIELD_PASSWORD;
509 } else if (ast_strings_equal(v->name, "tls_enabled")) {
510 changed |= AST_WS_CLIENT_FIELD_TLS_ENABLED;
511 } else if (ast_strings_equal(v->name, "ca_list_file")) {
512 changed |= AST_WS_CLIENT_FIELD_CA_LIST_FILE;
513 } else if (ast_strings_equal(v->name, "ca_list_path")) {
514 changed |= AST_WS_CLIENT_FIELD_CA_LIST_PATH;
515 } else if (ast_strings_equal(v->name, "cert_file")) {
516 changed |= AST_WS_CLIENT_FIELD_CERT_FILE;
517 } else if (ast_strings_equal(v->name, "priv_key_file")) {
518 changed |= AST_WS_CLIENT_FIELD_PRIV_KEY_FILE;
519 } else if (ast_strings_equal(v->name, "reconnect_interval")) {
520 changed |= AST_WS_CLIENT_FIELD_RECONNECT_INTERVAL;
521 } else if (ast_strings_equal(v->name, "reconnect_attempts")) {
522 changed |= AST_WS_CLIENT_FIELD_RECONNECT_ATTEMPTS;
523 } else if (ast_strings_equal(v->name, "connection_timeout")) {
524 changed |= AST_WS_CLIENT_FIELD_CONNECTION_TIMEOUT;
525 } else if (ast_strings_equal(v->name, "verify_server_cert")) {
526 changed |= AST_WS_CLIENT_FIELD_VERIFY_SERVER_CERT;
527 } else if (ast_strings_equal(v->name, "verify_server_hostname")) {
528 changed |= AST_WS_CLIENT_FIELD_VERIFY_SERVER_HOSTNAME;
529 } else {
530 ast_debug(2, "%s: Unknown change %s\n", new_id, v->name);
531 }
532 }
533
534 if (!changes_found) {
535 ast_debug(2, "%s: No changes found %p %p\n", new_id,
536 old_wc,new_wc);
537 }
538 return changed;
539
540 }
541
542 int ast_websocket_client_observer_add(const struct ast_sorcery_observer *callbacks)
543 {
544 if (!sorcery || !callbacks) {
545 return -1;
546 }
547
548 if (ast_sorcery_observer_add(sorcery, "websocket_client", callbacks)) {
549 ast_log(LOG_ERROR, "Failed to register websocket client observers\n");
550 return -1;
551 }
552
553 return 0;
554 }
555
556 void ast_websocket_client_observer_remove(const struct ast_sorcery_observer *callbacks)
557 {
558 if (!sorcery || !callbacks) {
559 return;
560 }
561
562 ast_sorcery_observer_remove(sorcery, "websocket_client", callbacks);
563 }
564
565
566 static int load_module(void)
567 {
568 ast_debug(2, "Initializing Websocket Client Configuration\n");
569 sorcery = ast_sorcery_open();
570 if (!sorcery) {
571 ast_log(LOG_ERROR, "Failed to open sorcery\n");
572 return -1;
573 }
574
575 ast_sorcery_apply_default(sorcery, "websocket_client", "config",
576 "websocket_client.conf,criteria=type=websocket_client");
577
578 if (ast_sorcery_object_register(sorcery, "websocket_client", wc_alloc,
579 NULL, wc_apply)) {
580 ast_log(LOG_ERROR, "Failed to register websocket_client object with sorcery\n");
581 ast_sorcery_unref(sorcery);
582 sorcery = NULL;
583 return -1;
584 }
585
586 ast_sorcery_object_field_register(sorcery, "websocket_client", "type", "", OPT_NOOP_T, 0, 0);
587 ast_sorcery_register_cust(websocket_client, connection_type, "");
588 ast_sorcery_register_sf(websocket_client, ast_websocket_client, uri, uri, "");
589 ast_sorcery_register_sf(websocket_client, ast_websocket_client, protocols, protocols, "");
590 ast_sorcery_register_sf(websocket_client, ast_websocket_client, username, username, "");
591 ast_sorcery_register_sf(websocket_client, ast_websocket_client, password, password, "");
592 ast_sorcery_register_sf(websocket_client, ast_websocket_client, ca_list_file, ca_list_file, "");
593 ast_sorcery_register_sf(websocket_client, ast_websocket_client, ca_list_path, ca_list_path, "");
594 ast_sorcery_register_sf(websocket_client, ast_websocket_client, cert_file, cert_file, "");
595 ast_sorcery_register_sf(websocket_client, ast_websocket_client, priv_key_file, priv_key_file, "");
596 ast_sorcery_register_bool(websocket_client, ast_websocket_client, tls_enabled, tls_enabled, "no");
597 ast_sorcery_register_bool(websocket_client, ast_websocket_client, verify_server_cert, verify_server_cert, "yes");
598 ast_sorcery_register_bool(websocket_client, ast_websocket_client, verify_server_hostname, verify_server_hostname, "yes");
599 ast_sorcery_register_int(websocket_client, ast_websocket_client, connection_timeout, connect_timeout, 500);
600 ast_sorcery_register_int(websocket_client, ast_websocket_client, reconnect_attempts, reconnect_attempts, 4);
601 ast_sorcery_register_int(websocket_client, ast_websocket_client, reconnect_interval, reconnect_interval, 500);
602
603 ast_sorcery_load(sorcery);
604
605 return 0;
606 }
607
608 static int reload_module(void)
609 {
610 ast_debug(2, "Reloading Websocket Client Configuration\n");
611 ast_sorcery_reload(sorcery);
612
613 return 0;
614 }
615
616 int ast_websocket_client_reload(void)
617 {
618 ast_debug(2, "Reloading Websocket Client Configuration\n");
619 if (sorcery) {
620 ast_sorcery_reload(sorcery);
621 }
622
623 return 0;
624 }
625
626 static int unload_module(void)
627 {
628 ast_debug(2, "Unloading Websocket Client Configuration\n");
629 if (sorcery) {
630 ast_sorcery_unref(sorcery);
631 sorcery = NULL;
632 }
633 return 0;
634 }
635
636 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "WebSocket Client Support",
637 .support_level = AST_MODULE_SUPPORT_CORE,
638 .load = load_module,
639 .unload = unload_module,
640 .reload = reload_module,
641 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
642 .requires = "res_http_websocket",
643 );