Asterisk - The Open Source Telephony Project GIT-master-545c459
Loading...
Searching...
No Matches
res_websocket_client.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2025, Sangoma Technologies Corporation
5 *
6 * George Joseph <gjoseph@sangoma.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
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]
53type = websocket_client
54uri = ws://localhost:8787
55protocols = media
56username = media_username
57password = media_password
58connection_type = per_call_config
59connection_timeout = 500
60reconnect_interval = 500
61reconnect_attempts = 5
62tls_enabled = no
63;
64; A TLS connection for use by ARI Outbound Websocket
65[ari_connection1]
66type = websocket_client
67uri = wss://localhost:8765
68protocols = ari
69username = some_username
70password = some_password
71connection_type = persistent
72connection_timeout = 500
73reconnect_interval = 500
74reconnect_attempts = 5
75tls_enabled = yes
76ca_list_file = /etc/pki/tls/cert.pem
77verify_server_cert = no
78verify_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 <description>
143 <para>
144 The maximum number of milliseconds to wait for a connection
145 to the WebSocket server to succeed. If the timer expires,
146 reconnection will be attempted based on the settings of the
147 <literal>reconnect_attempts</literal> and <literal>reconnect_interval</literal>
148 parameters.
149 </para>
150 </description>
151 <see-also>
152 <ref type="configOption">write_timeout</ref>
153 <ref type="configOption">reconnect_attempts</ref>
154 <ref type="configOption">reconnect_interval</ref>
155 </see-also>
156 </configOption>
157 <configOption name="reconnect_attempts">
158 <since>
159 <version>20.15.0</version>
160 <version>21.10.0</version>
161 <version>22.5.0</version>
162 </since>
163 <synopsis>On failure, how many times should reconnection be attempted?</synopsis>
164 <description>
165 <para>
166 For per_call connections, this is the number of
167 (re)connection attempts to make before returning an
168 and terminating the call. Persistent connections
169 always retry forever but this setting will control
170 how often failure messages are logged.
171 </para>
172 </description>
173 <see-also>
174 <ref type="configOption">connection_timeout</ref>
175 <ref type="configOption">reconnect_interval</ref>
176 </see-also>
177 </configOption>
178 <configOption name="reconnect_interval">
179 <since>
180 <version>20.15.0</version>
181 <version>21.10.0</version>
182 <version>22.5.0</version>
183 </since>
184 <synopsis>How often should reconnection be attempted (ms)?</synopsis>
185 <see-also>
186 <ref type="configOption">connection_timeout</ref>
187 <ref type="configOption">reconnect_attempts</ref>
188 </see-also>
189 </configOption>
190 <configOption name="write_timeout">
191 <since>
192 <version>20.22.0</version>
193 <version>22.12.0</version>
194 <version>23.6.0</version>
195 <version>24.0.0</version>
196 </since>
197 <synopsis>Write timeout (ms).</synopsis>
198 <description>
199 <para>
200 The maximum number of milliseconds to wait for a write
201 to the WebSocket to succeed. If this connection is used
202 by chan_websocket, the WebSocket will be closed and the
203 channel hung up when the timeout is reached. The default
204 value is set by the module creating the client.
205 </para>
206 <note>
207 <para>
208 Success means the write request was accepted by the
209 operating system and does not imply the payload was
210 actually transmitted or received by the server.
211 </para>
212 </note>
213 </description>
214 <see-also>
215 <ref type="configOption">connection_timeout</ref>
216 </see-also>
217 </configOption>
218 <configOption name="tls_enabled">
219 <since>
220 <version>20.15.0</version>
221 <version>21.10.0</version>
222 <version>22.5.0</version>
223 </since>
224 <synopsis>Enable TLS</synopsis>
225 </configOption>
226 <configOption name="ca_list_file">
227 <since>
228 <version>20.15.0</version>
229 <version>21.10.0</version>
230 <version>22.5.0</version>
231 </since>
232 <synopsis>File containing the server's CA certificate. (optional)</synopsis>
233 </configOption>
234 <configOption name="ca_list_path">
235 <since>
236 <version>20.15.0</version>
237 <version>21.10.0</version>
238 <version>22.5.0</version>
239 </since>
240 <synopsis>Path to a directory containing one or more hashed CA certificates. (optional)</synopsis>
241 </configOption>
242 <configOption name="cert_file">
243 <since>
244 <version>20.15.0</version>
245 <version>21.10.0</version>
246 <version>22.5.0</version>
247 </since>
248 <synopsis>File containing a client certificate. (optional)</synopsis>
249 </configOption>
250 <configOption name="priv_key_file">
251 <since>
252 <version>20.15.0</version>
253 <version>21.10.0</version>
254 <version>22.5.0</version>
255 </since>
256 <synopsis>File containing the client's private key. (optional)</synopsis>
257 </configOption>
258 <configOption name="verify_server_cert">
259 <since>
260 <version>20.15.0</version>
261 <version>21.10.0</version>
262 <version>22.5.0</version>
263 </since>
264 <synopsis>If set to true, verify the server's certificate. (optional)</synopsis>
265 </configOption>
266 <configOption name="verify_server_hostname">
267 <since>
268 <version>20.15.0</version>
269 <version>21.10.0</version>
270 <version>22.5.0</version>
271 </since>
272 <synopsis>If set to true, verify that the server's hostname matches the common name in it's certificate. (optional)</synopsis>
273 </configOption>
274 <configOption name="proxy_host">
275 <since>
276 <version>20.21.0</version>
277 <version>22.11.0</version>
278 <version>23.5.0</version>
279 </since>
280 <synopsis>Proxy host including port for outbound proxy if required. (optional)</synopsis>
281 <description>
282 <para>
283 If an outbound proxy is required to reach the websocket server,
284 specify a host in the form <literal>&lt;host&gt;:&lt;port7gt;</literal>.
285 Currently only http (non-TLS) proxies are supported although the tunnelled
286 connection to the websocket server can have TLS enabled.
287 </para>
288 </description>
289 </configOption>
290 <configOption name="proxy_username">
291 <since>
292 <version>20.21.0</version>
293 <version>22.11.0</version>
294 <version>23.5.0</version>
295 </since>
296 <synopsis>Proxy authentication username if required. (optional)</synopsis>
297 </configOption>
298 <configOption name="proxy_password">
299 <since>
300 <version>20.21.0</version>
301 <version>22.11.0</version>
302 <version>23.5.0</version>
303 </since>
304 <synopsis>Proxy authentication password if required. (optional)</synopsis>
305 </configOption>
306 <configOption name="enable_tcp_keepalives">
307 <since>
308 <version>20.21.0</version>
309 <version>22.11.0</version>
310 <version>23.5.0</version>
311 </since>
312 <synopsis>Enable TCP Keepalives. (optional)</synopsis>
313 </configOption>
314 <configOption name="tcp_keepalive_time">
315 <since>
316 <version>20.21.0</version>
317 <version>22.11.0</version>
318 <version>23.5.0</version>
319 </since>
320 <synopsis>Start sending keepalives when no data has been sent for this many seconds. (optional)</synopsis>
321 </configOption>
322 <configOption name="tcp_keepalive_interval">
323 <since>
324 <version>20.21.0</version>
325 <version>22.11.0</version>
326 <version>23.5.0</version>
327 </since>
328 <synopsis>Send keepalives at this interval in seconds. (optional)</synopsis>
329 <description>
330 <para>
331 If a reply isn't received by the time the next keepalive is due
332 to be sent, it's considered missed so this option also controls
333 how long it takes to detect a failure.
334 </para>
335 </description>
336 </configOption>
337 <configOption name="tcp_keepalive_probes">
338 <since>
339 <version>20.21.0</version>
340 <version>22.11.0</version>
341 <version>23.5.0</version>
342 </since>
343 <synopsis>Close the connection after this many missed replies. (optional)</synopsis>
344 <description>
345 <para>
346 If a reply isn't received by the time the next keepalive is due
347 to be sent, it's considered missed. The time to detect a failure
348 is therefore between (probes * interval) and
349 ((probes + 1) * interval) seconds. If the connection closes
350 and reconnect_interval reconnect_attempts are set, a new connection
351 will be attempted using those parameters.
352 </para>
353 </description>
354 </configOption>
355 <configOption name="enable_pingpongs">
356 <since>
357 <version>20.21.0</version>
358 <version>22.11.0</version>
359 <version>23.5.0</version>
360 </since>
361 <synopsis>Enable WebSocket PING/PONGs.. (optional)</synopsis>
362 </configOption>
363 <configOption name="pingpong_interval">
364 <since>
365 <version>20.21.0</version>
366 <version>22.11.0</version>
367 <version>23.5.0</version>
368 </since>
369 <synopsis>Send WebSocket PINGs at this interval in seconds. (optional)</synopsis>
370 <description>
371 <para>
372 If a reply isn't received by the time the next PING is due
373 to be sent, it's considered missed so this option also controls
374 how long it takes to detect a failure.
375 </para>
376 </description>
377 </configOption>
378 <configOption name="pingpong_probes">
379 <since>
380 <version>20.21.0</version>
381 <version>22.11.0</version>
382 <version>23.5.0</version>
383 </since>
384 <synopsis>Close the connection after this many missed PONG replies. (optional)</synopsis>
385 <description>
386 <para>
387 If a reply isn't received by the time the next PING is due
388 to be sent, it's considered missepingd. The time to detect a failure
389 is therefore between (probes * interval) and
390 ((probes + 1) * interval) seconds. If the connection closes
391 and reconnect_interval reconnect_attempts are set, a new connection
392 will be attempted using those parameters.
393 </para>
394 </description>
395 </configOption>
396 </configObject>
397 </configFile>
398 </configInfo>
399***/
400
401
402#include "asterisk.h"
403
404#include "asterisk/module.h"
405#include "asterisk/astobj2.h"
406#include "asterisk/strings.h"
407#include "asterisk/vector.h"
409
410static struct ast_sorcery *sorcery = NULL;
411
413 const char *uri_params)
414{
415 ast_string_field_set(wc, uri_params, uri_params);
416}
417
419 void *lock_obj, const char *display_name, enum ast_websocket_result *result)
420{
421 int reconnect_counter = wc->reconnect_attempts;
422 char *uri = NULL;
423
424 if (ast_strlen_zero(display_name)) {
425 display_name = ast_sorcery_object_get_id(wc);
426 }
427
428 if (!ast_strlen_zero(wc->uri_params)) {
429 /*
430 * If the configured URI doesn't already contain parameters, we append the
431 * new ones to the URI path component with '?'. If it does, we append the
432 * new ones to the existing ones with a '&'.
433 */
434 char sep = '?';
435 uri = ast_alloca(strlen(wc->uri) + strlen(wc->uri_params) + 2);
436 if (strchr(wc->uri, '?')) {
437 sep = '&';
438 }
439 sprintf(uri, "%s%c%s", wc->uri, sep, wc->uri_params); /*Safe */
440 }
441
442 while (1) {
443 struct ast_websocket *astws = NULL;
445 .uri = S_OR(uri, wc->uri),
446 .protocols = wc->protocols,
447 .username = wc->username,
448 .password = wc->password,
449 .timeout = wc->connect_timeout,
450 .write_timeout = wc->write_timeout != INT_MAX ? wc->write_timeout : AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT,
451 .suppress_connection_msgs = 1,
452 .proxy_host = wc->proxy_host,
453 .proxy_username = wc->proxy_username,
454 .proxy_password = wc->proxy_password,
456 .tcp_keepalive_time = wc->tcp_keepalive_time,
457 .tcp_keepalive_interval = wc->tcp_keepalive_interval,
458 .tcp_keepalive_probes = wc->tcp_keepalive_probes,
459 .pingpongs = wc->pingpongs,
460 .pingpong_interval = wc->pingpong_interval,
461 .pingpong_probes = wc->pingpong_probes,
462 .tls_cfg = NULL,
463 };
464
465 if (lock_obj) {
466 ao2_lock(lock_obj);
467 }
468
469 if (wc->tls_enabled) {
470 /*
471 * tls_cfg and its contents are freed automatically
472 * by res_http_websocket when the connection ends.
473 * We create it even if tls is not enabled to we can
474 * suppress connection error messages and print our own.
475 */
476 options.tls_cfg = ast_calloc(1, sizeof(*options.tls_cfg));
477 if (!options.tls_cfg) {
478 if (lock_obj) {
479 ao2_unlock(lock_obj);
480 }
481 return NULL;
482 }
483 /* TLS options */
484 options.tls_cfg->enabled = wc->tls_enabled;
485 options.tls_cfg->cafile = ast_strdup(wc->ca_list_file);
486 options.tls_cfg->capath = ast_strdup(wc->ca_list_path);
487 options.tls_cfg->certfile = ast_strdup(wc->cert_file);
488 options.tls_cfg->pvtfile = ast_strdup(wc->priv_key_file);
491 }
492
494 if (astws && *result == WS_OK) {
495 if (lock_obj) {
496 ao2_unlock(lock_obj);
497 }
498 return astws;
499 }
500
501 reconnect_counter--;
502 if (reconnect_counter <= 0) {
505 "%s: Websocket connection to %s failed after %d tries: %s%s%s%s. Retrying in %d ms.\n",
506 display_name,
507 wc->uri,
510 errno ? " (" : "",
511 errno ? strerror(errno) : "",
512 errno ? ")" : "",
514 );
515 } else {
517 "%s: Websocket connection to %s failed after %d tries: %s%s%s%s. Hanging up after exhausting retries.\n",
518 display_name,
519 wc->uri,
522 errno ? " (" : "",
523 errno ? strerror(errno) : "",
524 errno ? ")" : ""
525 );
526 }
527 break;
528 }
529
530 if (lock_obj) {
531 ao2_unlock(lock_obj);
532 }
533 usleep(wc->reconnect_interval * 1000);
534 }
535
536 if (lock_obj) {
537 ao2_unlock(lock_obj);
538 }
539
540 return NULL;
541}
542
543
544
545static void wc_dtor(void *obj)
546{
547 struct ast_websocket_client *wc = obj;
548
549 ast_debug(3, "%s: Disposing of websocket client config\n",
552}
553
554static void *wc_alloc(const char *id)
555{
556 struct ast_websocket_client *wc = NULL;
557
558 wc = ast_sorcery_generic_alloc(sizeof(*wc), wc_dtor);
559 if (!wc) {
560 return NULL;
561 }
562
563 if (ast_string_field_init(wc, 1024) != 0) {
564 ao2_cleanup(wc);
565 return NULL;
566 }
567
568 if (ast_string_field_init_extended(wc, uri_params) != 0) {
569 ao2_cleanup(wc);
570 return NULL;
571 }
572
573 if (ast_string_field_init_extended(wc, proxy_host) != 0) {
574 ao2_cleanup(wc);
575 return NULL;
576 }
577
578 if (ast_string_field_init_extended(wc, proxy_username) != 0) {
579 ao2_cleanup(wc);
580 return NULL;
581 }
582
583 if (ast_string_field_init_extended(wc, proxy_password) != 0) {
584 ao2_cleanup(wc);
585 return NULL;
586 }
587
588 ast_debug(2, "%s: Allocated websocket client config\n", id);
589 return wc;
590}
591
593 struct ast_variable *var, void *obj)
594{
595 struct ast_websocket_client *ws = obj;
596
597 if (strcasecmp(var->value, "persistent") == 0) {
599 } else if (strcasecmp(var->value, "per_call_config") == 0) {
601 } else {
602 return -1;
603 }
604
605 return 0;
606}
607
608static int websocket_client_connection_type_to_str(const void *obj, const intptr_t *args, char **buf)
609{
610 const struct ast_websocket_client *wc = obj;
611
613 *buf = ast_strdup("persistent");
615 *buf = ast_strdup("per_call_config");
616 } else {
617 return -1;
618 }
619
620 return 0;
621}
622
623/*
624 * Can't use INT_MIN because it's an expression
625 * and macro substitutions using stringify can't
626 * handle that.
627 */
628#define DEFAULT_RECONNECT_ATTEMPTS -2147483648
629
630static int wc_apply(const struct ast_sorcery *sorcery, void *obj)
631{
632 struct ast_websocket_client *wc = obj;
633 const char *id = ast_sorcery_object_get_id(wc);
634 int res = 0;
635
636 ast_debug(3, "%s: Applying config\n", id);
637
638 if (ast_strlen_zero(wc->uri)) {
639 ast_log(LOG_WARNING, "%s: Websocket client missing uri\n", id);
640 res = -1;
641 }
642
643 if (!ast_strlen_zero(wc->proxy_host)) {
644 char *host = NULL;
645 char *port = NULL;
646 char *s = ast_strdupa(wc->proxy_host);
647 if (!ast_sockaddr_split_hostport(s, &host, &port, PARSE_PORT_REQUIRE)) {
648 ast_log(LOG_WARNING, "%s: proxy_host '%s' is missing a port\n", id, wc->proxy_host);
649 res = -1;
650 }
651 }
652
653 if (wc->tcp_keepalives) {
655 ast_log(LOG_WARNING, "%s: tcp_keepalive_time, tcp_keepalive_interval and tcp_keepalive_probes must all be non-zero\n", id);
656 res = -1;
657 }
658 }
659
660 if (wc->pingpongs) {
661 if (!wc->pingpong_interval || !wc->pingpong_probes) {
662 ast_log(LOG_WARNING, "%s: pingpong_interval and pingpong_probes must be non-zero\n", id);
663 res = -1;
664 }
665 }
666
667 if (wc->write_timeout <= 0) {
668 ast_log(LOG_WARNING, "The write_timeout parameter must be > 0\n");
669 res = -1;
670 }
671
672 if (res != 0) {
673 ast_log(LOG_WARNING, "%s: Websocket client configuration failed\n", id);
674 } else {
675 ast_debug(3, "%s: Websocket client configuration succeeded\n", id);
676
679 wc->reconnect_attempts = INT_MAX;
680 } else {
681 wc->reconnect_attempts = 4;
682 }
683 }
684 }
685
686 return res;
687}
688
690{
691 if (!sorcery) {
692 return NULL;
693 }
694
695 return ast_sorcery_retrieve_by_fields(sorcery, "websocket_client",
697}
698
700{
701 if (!sorcery) {
702 return NULL;
703 }
704
705 return ast_sorcery_retrieve_by_id(sorcery, "websocket_client", id);
706}
707
709 struct ast_websocket_client *old_wc,
710 struct ast_websocket_client *new_wc)
711{
713 const char *new_id = ast_sorcery_object_get_id(new_wc);
714 RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
715 struct ast_variable *v = NULL;
716 int res = 0;
717 int changes_found = 0;
718
719 ast_debug(2, "%s: Detecting changes\n", new_id);
720
721 res = ast_sorcery_diff(sorcery, old_wc, new_wc, &changes);
722 if (res != 0) {
723 ast_log(LOG_WARNING, "%s: Failed to create changeset\n", new_id);
725 }
726
727 for (v = changes; v; v = v->next) {
728 changes_found = 1;
729 ast_debug(2, "%s: %s changed to %s\n", new_id, v->name, v->value);
730 if (ast_strings_equal(v->name, "connection_type")) {
732 } else if (ast_strings_equal(v->name, "uri")) {
733 changed |= AST_WS_CLIENT_FIELD_URI;
734 } else if (ast_strings_equal(v->name, "protocols")) {
736 } else if (ast_strings_equal(v->name, "username")) {
738 } else if (ast_strings_equal(v->name, "password")) {
740 } else if (ast_strings_equal(v->name, "tls_enabled")) {
742 } else if (ast_strings_equal(v->name, "ca_list_file")) {
744 } else if (ast_strings_equal(v->name, "ca_list_path")) {
746 } else if (ast_strings_equal(v->name, "cert_file")) {
748 } else if (ast_strings_equal(v->name, "priv_key_file")) {
750 } else if (ast_strings_equal(v->name, "reconnect_interval")) {
752 } else if (ast_strings_equal(v->name, "reconnect_attempts")) {
754 } else if (ast_strings_equal(v->name, "connection_timeout")) {
756 } else if (ast_strings_equal(v->name, "verify_server_cert")) {
758 } else if (ast_strings_equal(v->name, "verify_server_hostname")) {
760 } else if (ast_strings_equal(v->name, "proxy_host")) {
762 } else if (ast_strings_equal(v->name, "proxy_username")) {
764 } else if (ast_strings_equal(v->name, "proxy_password")) {
766 } else if (ast_strings_equal(v->name, "enable_tcp_keepalives")) {
768 } else if (ast_strings_equal(v->name, "tcp_keepalive_time")) {
770 } else if (ast_strings_equal(v->name, "tcp_keepalive_interval")) {
772 } else if (ast_strings_equal(v->name, "tcp_keepalive_probes")) {
774 } else if (ast_strings_equal(v->name, "enable_pingpongs")) {
776 } else if (ast_strings_equal(v->name, "pingpong_interval")) {
778 } else if (ast_strings_equal(v->name, "pingpong_probes")) {
780 } else if (ast_strings_equal(v->name, "write_timeout")) {
782 } else {
783 ast_debug(2, "%s: Unknown change %s\n", new_id, v->name);
784 }
785 }
786
787 if (!changes_found) {
788 ast_debug(2, "%s: No changes found %p %p\n", new_id,
789 old_wc,new_wc);
790 }
791 return changed;
792
793}
794
796{
797 if (!sorcery || !callbacks) {
798 return -1;
799 }
800
801 if (ast_sorcery_observer_add(sorcery, "websocket_client", callbacks)) {
802 ast_log(LOG_ERROR, "Failed to register websocket client observers\n");
803 return -1;
804 }
805
806 return 0;
807}
808
810{
811 if (!sorcery || !callbacks) {
812 return;
813 }
814
815 ast_sorcery_observer_remove(sorcery, "websocket_client", callbacks);
816}
817
818
819static int load_module(void)
820{
821 ast_debug(2, "Initializing Websocket Client Configuration\n");
823 if (!sorcery) {
824 ast_log(LOG_ERROR, "Failed to open sorcery\n");
825 return -1;
826 }
827
828 ast_sorcery_apply_default(sorcery, "websocket_client", "config",
829 "websocket_client.conf,criteria=type=websocket_client");
830
831 if (ast_sorcery_object_register(sorcery, "websocket_client", wc_alloc,
832 NULL, wc_apply)) {
833 ast_log(LOG_ERROR, "Failed to register websocket_client object with sorcery\n");
835 sorcery = NULL;
836 return -1;
837 }
838
839 ast_sorcery_object_field_register(sorcery, "websocket_client", "type", "", OPT_NOOP_T, 0, 0);
840 ast_sorcery_register_cust(websocket_client, connection_type, "");
845 ast_sorcery_register_sf(websocket_client, ast_websocket_client, ca_list_file, ca_list_file, "");
846 ast_sorcery_register_sf(websocket_client, ast_websocket_client, ca_list_path, ca_list_path, "");
848 ast_sorcery_register_sf(websocket_client, ast_websocket_client, priv_key_file, priv_key_file, "");
849 ast_sorcery_register_bool(websocket_client, ast_websocket_client, tls_enabled, tls_enabled, "no");
850 ast_sorcery_register_bool(websocket_client, ast_websocket_client, verify_server_cert, verify_server_cert, "yes");
851 ast_sorcery_register_bool(websocket_client, ast_websocket_client, verify_server_hostname, verify_server_hostname, "yes");
852 ast_sorcery_register_int(websocket_client, ast_websocket_client, connection_timeout, connect_timeout, 500);
853 ast_sorcery_register_int(websocket_client, ast_websocket_client, reconnect_attempts, reconnect_attempts, 4);
854 ast_sorcery_register_int(websocket_client, ast_websocket_client, reconnect_interval, reconnect_interval, 500);
856 ast_sorcery_register_sf(websocket_client, ast_websocket_client, proxy_username, proxy_username, "");
857 ast_sorcery_register_sf(websocket_client, ast_websocket_client, proxy_password, proxy_password, "");
858 ast_sorcery_register_bool(websocket_client, ast_websocket_client, enable_tcp_keepalives, tcp_keepalives, "no");
859 ast_sorcery_register_uint(websocket_client, ast_websocket_client, tcp_keepalive_time, tcp_keepalive_time, 20);
860 ast_sorcery_register_uint(websocket_client, ast_websocket_client, tcp_keepalive_interval, tcp_keepalive_interval, 20);
861 ast_sorcery_register_uint(websocket_client, ast_websocket_client, tcp_keepalive_probes, tcp_keepalive_probes, 3);
862 ast_sorcery_register_bool(websocket_client, ast_websocket_client, enable_pingpongs, pingpongs, "no");
863 ast_sorcery_register_uint(websocket_client, ast_websocket_client, pingpong_interval, pingpong_interval, 20);
864 ast_sorcery_register_uint(websocket_client, ast_websocket_client, pingpong_probes, pingpong_probes, 3);
865 /*
866 * The default for write_timeout needs to be INT_MAX so we can tell that it's not set.
867 * This allows the modules using this capability to apply their own default value.
868 */
869 ast_sorcery_register_int(websocket_client, ast_websocket_client, write_timeout, write_timeout, INT_MAX);
870
872
873 return 0;
874}
875
876static int reload_module(void)
877{
878 ast_debug(2, "Reloading Websocket Client Configuration\n");
880
881 return 0;
882}
883
885{
886 ast_debug(2, "Reloading Websocket Client Configuration\n");
887 if (sorcery) {
889 }
890
891 return 0;
892}
893
894static int unload_module(void)
895{
896 ast_debug(2, "Unloading Websocket Client Configuration\n");
897 if (sorcery) {
899 sorcery = NULL;
900 }
901 return 0;
902}
903
905 .support_level = AST_MODULE_SUPPORT_CORE,
906 .load = load_module,
907 .unload = unload_module,
909 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
910 .requires = "res_http_websocket",
#define var
Definition ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition astmm.h:288
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
#define ast_log
Definition astobj2.c:42
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_unlock(a)
Definition astobj2.h:729
#define ao2_lock(a)
Definition astobj2.h:717
static PGresult * result
Definition cel_pgsql.c:84
@ OPT_NOOP_T
Type for a default handler that should do nothing.
char buf[BUFSIZE]
Definition eagi_proxy.c:66
ast_websocket_result
Result code for a websocket client.
@ WS_OK
struct ast_websocket *AST_OPTIONAL_API_NAME() ast_websocket_client_create_with_options(struct ast_websocket_client_options *options, enum ast_websocket_result *result)
Create, and connect, a websocket client using given options.
#define AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT
Default websocket write timeout, in ms.
@ AST_WS_TYPE_CLIENT_PER_CALL_CONFIG
@ AST_WS_TYPE_CLIENT_PERSISTENT
const char *AST_OPTIONAL_API_NAME() ast_websocket_result_to_str(enum ast_websocket_result result)
Convert a websocket result code to a string.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
int errno
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition module.h:331
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition module.h:330
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition module.h:557
@ AST_MODPRI_CHANNEL_DEPEND
Definition module.h:340
@ AST_MODULE_SUPPORT_CORE
Definition module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
Definition netsock2.c:164
static int reload(void)
struct @510 callbacks
static struct @523 args
static int websocket_client_connection_type_to_str(const void *obj, const intptr_t *args, char **buf)
void ast_websocket_client_add_uri_params(struct ast_websocket_client *wc, const char *uri_params)
Add additional parameters to the URI.
struct ao2_container * ast_websocket_client_retrieve_all(void)
Retrieve a container of all websocket client objects.
int ast_websocket_client_reload(void)
Force res_websocket_client to reload its configuration.
static void * wc_alloc(const char *id)
enum ast_ws_client_fields ast_websocket_client_get_field_diff(struct ast_websocket_client *old_wc, struct ast_websocket_client *new_wc)
Detect changes between two websocket client configurations.
static int wc_apply(const struct ast_sorcery *sorcery, void *obj)
struct ast_websocket_client * ast_websocket_client_retrieve_by_id(const char *id)
Retrieve a websocket client object by ID.
static int reload_module(void)
static struct ast_sorcery * sorcery
static int websocket_client_connection_type_from_str(const struct aco_option *opt, struct ast_variable *var, void *obj)
static int load_module(void)
#define DEFAULT_RECONNECT_ATTEMPTS
void ast_websocket_client_observer_remove(const struct ast_sorcery_observer *callbacks)
Remove sorcery observers for websocket client events.
int ast_websocket_client_observer_add(const struct ast_sorcery_observer *callbacks)
Add sorcery observers for websocket client events.
static int unload_module(void)
static void wc_dtor(void *obj)
struct ast_websocket * ast_websocket_client_connect(struct ast_websocket_client *wc, void *lock_obj, const char *display_name, enum ast_websocket_result *result)
Connect to a websocket server using the configured authentication, retry and TLS options.
#define NULL
Definition resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition sorcery.h:1500
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition sorcery.c:2381
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
Definition sorcery.c:2487
#define ast_sorcery_register_cust(object, option, def_value)
Register a custom field within an object.
Definition sorcery.h:1767
@ AST_RETRIEVE_FLAG_MULTIPLE
Return all matching objects.
Definition sorcery.h:120
@ AST_RETRIEVE_FLAG_ALL
Perform no matching, return all objects.
Definition sorcery.h:123
void ast_sorcery_load(const struct ast_sorcery *sorcery)
Inform any wizards to load persistent objects.
Definition sorcery.c:1441
#define ast_sorcery_register_uint(object, structure, option, field, def_value)
Register an unsigned int field as type OPT_UINT_T within an object.
Definition sorcery.h:1728
int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
Create a changeset of two objects.
Definition sorcery.c:1869
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition sorcery.c:1917
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition sorcery.h:837
void ast_sorcery_reload(const struct ast_sorcery *sorcery)
Inform any wizards to reload persistent objects.
Definition sorcery.c:1472
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
Definition sorcery.c:2455
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition sorcery.c:1792
#define ast_sorcery_register_int(object, structure, option, field, def_value)
Register an int field as type OPT_INT_T within an object.
Definition sorcery.h:1710
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition sorcery.h:955
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition sorcery.h:476
#define ast_sorcery_register_bool(object, structure, option, field, def_value)
Register a boolean field as type OPT_YESNO_T within an object.
Definition sorcery.h:1684
#define ast_sorcery_open()
Open a new sorcery structure.
Definition sorcery.h:406
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition sorcery.c:1961
#define ast_sorcery_register_sf(object, structure, option, field, def_value)
Register a stringfield field as type OPT_STRINGFIELD_T within an object.
Definition sorcery.h:1746
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
#define ast_string_field_init_extended(x, field)
Initialize an extended string field.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
String manipulation functions.
int ast_strings_equal(const char *str1, const char *str2)
Compare strings for equality checking for NULL.
Definition strings.c:238
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
Generic container type.
Interface for a sorcery object type observer.
Definition sorcery.h:332
Full structure for sorcery.
Definition sorcery.c:231
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Options used for a websocket client.
unsigned int pingpong_interval
unsigned int reconnect_interval
const ast_string_field uri
enum ast_websocket_type connection_type
const ast_string_field ca_list_path
const ast_string_field cert_file
const ast_string_field password
const ast_string_field username
const ast_string_field ca_list_file
unsigned int tcp_keepalive_time
const ast_string_field protocols
unsigned int tcp_keepalive_interval
unsigned int tcp_keepalive_probes
const ast_string_field priv_key_file
unsigned int reconnect_attempts
Structure definition for session.
@ AST_SSL_DONT_VERIFY_SERVER
Definition tcptls.h:69
@ AST_SSL_IGNORE_COMMON_NAME
Definition tcptls.h:71
static struct test_options options
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981
#define ast_set2_flag(p, value, flag)
Definition utils.h:95
Vector container support.
ast_ws_client_fields
@ AST_WS_CLIENT_FIELD_RECONNECT_INTERVAL
@ AST_WS_CLIENT_FIELD_NONE
@ AST_WS_CLIENT_FIELD_URI
@ AST_WS_CLIENT_FIELD_PROTOCOLS
@ AST_WS_CLIENT_FIELD_TCP_KEEPALIVES
@ AST_WS_CLIENT_FIELD_CA_LIST_PATH
@ AST_WS_CLIENT_FIELD_TLS_ENABLED
@ AST_WS_CLIENT_FIELD_PROXY_PASSWORD
@ AST_WS_CLIENT_FIELD_TCP_KEEPALIVE_TIME
@ AST_WS_CLIENT_FIELD_CA_LIST_FILE
@ AST_WS_CLIENT_FIELD_PINGPONG_PROBES
@ AST_WS_CLIENT_FIELD_USERNAME
@ AST_WS_CLIENT_FIELD_VERIFY_SERVER_CERT
@ AST_WS_CLIENT_FIELD_PASSWORD
@ AST_WS_CLIENT_FIELD_PROXY_USERNAME
@ AST_WS_CLIENT_FIELD_CONNECTION_TIMEOUT
@ AST_WS_CLIENT_FIELD_PINGPONG_INTERVAL
@ AST_WS_CLIENT_FIELD_RECONNECT_ATTEMPTS
@ AST_WS_CLIENT_FIELD_WRITE_TIMEOUT
@ AST_WS_CLIENT_FIELD_CONNECTION_TYPE
@ AST_WS_CLIENT_FIELD_TCP_KEEPALIVE_PROBES
@ AST_WS_CLIENT_FIELD_VERIFY_SERVER_HOSTNAME
@ AST_WS_CLIENT_FIELD_PINGPONGS
@ AST_WS_CLIENT_FIELD_TCP_KEEPALIVE_INTERVAL
@ AST_WS_CLIENT_FIELD_CERT_FILE
@ AST_WS_CLIENT_FIELD_PRIV_KEY_FILE
@ AST_WS_CLIENT_FIELD_PROXY_HOST