Asterisk - The Open Source Telephony Project GIT-master-6144b6b
Loading...
Searching...
No Matches
Macros | Functions | Variables
res_websocket_client.c File Reference
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/vector.h"
#include "asterisk/websocket_client.h"
Include dependency graph for res_websocket_client.c:

Go to the source code of this file.

Macros

#define DEFAULT_RECONNECT_ATTEMPTS   -2147483648
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
void ast_websocket_client_add_uri_params (struct ast_websocket_client *wc, const char *uri_params)
 Add additional parameters to the URI.
 
struct ast_websocketast_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.
 
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.
 
int ast_websocket_client_observer_add (const struct ast_sorcery_observer *callbacks)
 Add sorcery observers for websocket client events.
 
void ast_websocket_client_observer_remove (const struct ast_sorcery_observer *callbacks)
 Remove sorcery observers for websocket client events.
 
int ast_websocket_client_reload (void)
 Force res_websocket_client to reload its configuration.
 
struct ao2_containerast_websocket_client_retrieve_all (void)
 Retrieve a container of all websocket client objects.
 
struct ast_websocket_clientast_websocket_client_retrieve_by_id (const char *id)
 Retrieve a websocket client object by ID.
 
static int load_module (void)
 
static int reload_module (void)
 
static int unload_module (void)
 
static void * wc_alloc (const char *id)
 
static int wc_apply (const struct ast_sorcery *sorcery, void *obj)
 
static void wc_dtor (void *obj)
 
static int websocket_client_connection_type_from_str (const struct aco_option *opt, struct ast_variable *var, void *obj)
 
static int websocket_client_connection_type_to_str (const void *obj, const intptr_t *args, char **buf)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "WebSocket Client Support" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, .requires = "res_http_websocket", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_sorcerysorcery = NULL
 

Macro Definition Documentation

◆ DEFAULT_RECONNECT_ATTEMPTS

#define DEFAULT_RECONNECT_ATTEMPTS   -2147483648

Definition at line 573 of file res_websocket_client.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 843 of file res_websocket_client.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 843 of file res_websocket_client.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 843 of file res_websocket_client.c.

◆ ast_websocket_client_add_uri_params()

void ast_websocket_client_add_uri_params ( struct ast_websocket_client wc,
const char *  uri_params 
)

Add additional parameters to the URI.

Parameters
wcA pointer to the ast_websocket_structure
uri_paramsA string containing URLENCODED parameters to append to the URI.

Definition at line 362 of file res_websocket_client.c.

364{
365 ast_string_field_set(wc, uri_params, uri_params);
366}
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.

References ast_string_field_set.

Referenced by webchan_call().

◆ ast_websocket_client_connect()

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.

Parameters
wcA pointer to the ast_websocket_structure
lock_objA pointer to an ao2 object to lock while the connection is being attempted or NULL if no locking is needed.
display_nameAn id string to use for logging messages. If NULL or empty the connection's ID will be used.
resultA pointer to an enum ast_websocket_result to store the result of the connection attempt.
Returns
A pointer to the ast_websocket structure on success, or NULL on failure.
Warning
The returned websocket must be closed with ast_websocket_close and its reference count decremented with ast_websocket_unref when it's no longer needed.

Definition at line 368 of file res_websocket_client.c.

370{
371 int reconnect_counter = wc->reconnect_attempts;
372 char *uri = NULL;
373
374 if (ast_strlen_zero(display_name)) {
375 display_name = ast_sorcery_object_get_id(wc);
376 }
377
378 if (!ast_strlen_zero(wc->uri_params)) {
379 /*
380 * If the configured URI doesn't already contain parameters, we append the
381 * new ones to the URI path component with '?'. If it does, we append the
382 * new ones to the existing ones with a '&'.
383 */
384 char sep = '?';
385 uri = ast_alloca(strlen(wc->uri) + strlen(wc->uri_params) + 2);
386 if (strchr(wc->uri, '?')) {
387 sep = '&';
388 }
389 sprintf(uri, "%s%c%s", wc->uri, sep, wc->uri_params); /*Safe */
390 }
391
392 while (1) {
393 struct ast_websocket *astws = NULL;
395 .uri = S_OR(uri, wc->uri),
396 .protocols = wc->protocols,
397 .username = wc->username,
398 .password = wc->password,
399 .timeout = wc->connect_timeout,
400 .suppress_connection_msgs = 1,
401 .proxy_host = wc->proxy_host,
402 .proxy_username = wc->proxy_username,
403 .proxy_password = wc->proxy_password,
405 .tcp_keepalive_time = wc->tcp_keepalive_time,
406 .tcp_keepalive_interval = wc->tcp_keepalive_interval,
407 .tcp_keepalive_probes = wc->tcp_keepalive_probes,
408 .pingpongs = wc->pingpongs,
409 .pingpong_interval = wc->pingpong_interval,
410 .pingpong_probes = wc->pingpong_probes,
411 .tls_cfg = NULL,
412 };
413
414 if (lock_obj) {
415 ao2_lock(lock_obj);
416 }
417
418 if (wc->tls_enabled) {
419 /*
420 * tls_cfg and its contents are freed automatically
421 * by res_http_websocket when the connection ends.
422 * We create it even if tls is not enabled to we can
423 * suppress connection error messages and print our own.
424 */
425 options.tls_cfg = ast_calloc(1, sizeof(*options.tls_cfg));
426 if (!options.tls_cfg) {
427 if (lock_obj) {
428 ao2_unlock(lock_obj);
429 }
430 return NULL;
431 }
432 /* TLS options */
433 options.tls_cfg->enabled = wc->tls_enabled;
434 options.tls_cfg->cafile = ast_strdup(wc->ca_list_file);
435 options.tls_cfg->capath = ast_strdup(wc->ca_list_path);
436 options.tls_cfg->certfile = ast_strdup(wc->cert_file);
437 options.tls_cfg->pvtfile = ast_strdup(wc->priv_key_file);
440 }
441
443 if (astws && *result == WS_OK) {
444 if (lock_obj) {
445 ao2_unlock(lock_obj);
446 }
447 return astws;
448 }
449
450 reconnect_counter--;
451 if (reconnect_counter <= 0) {
454 "%s: Websocket connection to %s failed after %d tries: %s%s%s%s. Retrying in %d ms.\n",
455 display_name,
456 wc->uri,
459 errno ? " (" : "",
460 errno ? strerror(errno) : "",
461 errno ? ")" : "",
462 wc->reconnect_interval
463 );
464 } else {
466 "%s: Websocket connection to %s failed after %d tries: %s%s%s%s. Hanging up after exhausting retries.\n",
467 display_name,
468 wc->uri,
471 errno ? " (" : "",
472 errno ? strerror(errno) : "",
473 errno ? ")" : ""
474 );
475 }
476 break;
477 }
478
479 if (lock_obj) {
480 ao2_lock(lock_obj);
481 }
482 usleep(wc->reconnect_interval * 1000);
483 }
484
485 return NULL;
486}
#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_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
#define ast_log
Definition astobj2.c:42
#define ao2_unlock(a)
Definition astobj2.h:729
#define ao2_lock(a)
Definition astobj2.h:717
static PGresult * result
Definition cel_pgsql.c:84
@ 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.
@ 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.
#define LOG_WARNING
int errno
#define NULL
Definition resample.c:96
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition sorcery.c:2381
#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
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 ast_set2_flag(p, value, flag)
Definition utils.h:95

References ao2_lock, ao2_unlock, ast_alloca, ast_calloc, ast_log, ast_set2_flag, ast_sorcery_object_get_id(), AST_SSL_DONT_VERIFY_SERVER, AST_SSL_IGNORE_COMMON_NAME, ast_strdup, ast_strlen_zero(), ast_websocket_client_create_with_options(), ast_websocket_result_to_str(), AST_WS_TYPE_CLIENT_PERSISTENT, ast_websocket_client::ca_list_file, ast_websocket_client::ca_list_path, ast_websocket_client::cert_file, ast_websocket_client::connect_timeout, ast_websocket_client::connection_type, errno, LOG_WARNING, NULL, options, ast_websocket_client::password, ast_websocket_client::pingpong_interval, ast_websocket_client::pingpong_probes, ast_websocket_client::pingpongs, ast_websocket_client::priv_key_file, ast_websocket_client::protocols, ast_websocket_client::reconnect_attempts, ast_websocket_client::reconnect_interval, result, S_OR, ast_websocket_client::tcp_keepalive_interval, ast_websocket_client::tcp_keepalive_probes, ast_websocket_client::tcp_keepalive_time, ast_websocket_client::tcp_keepalives, ast_websocket_client::tls_enabled, ast_websocket_client_options::uri, ast_websocket_client::uri, ast_websocket_client::username, ast_websocket_client::verify_server_cert, ast_websocket_client::verify_server_hostname, and WS_OK.

Referenced by outbound_session_handler_thread(), and webchan_call().

◆ ast_websocket_client_get_field_diff()

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.

Parameters
old_owThe old websocket configuration.
new_owThe new websocket configuration.
Returns
A bitmask of changed fields.

Definition at line 647 of file res_websocket_client.c.

650{
652 const char *new_id = ast_sorcery_object_get_id(new_wc);
653 RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
654 struct ast_variable *v = NULL;
655 int res = 0;
656 int changes_found = 0;
657
658 ast_debug(2, "%s: Detecting changes\n", new_id);
659
660 res = ast_sorcery_diff(sorcery, old_wc, new_wc, &changes);
661 if (res != 0) {
662 ast_log(LOG_WARNING, "%s: Failed to create changeset\n", new_id);
664 }
665
666 for (v = changes; v; v = v->next) {
667 changes_found = 1;
668 ast_debug(2, "%s: %s changed to %s\n", new_id, v->name, v->value);
669 if (ast_strings_equal(v->name, "connection_type")) {
671 } else if (ast_strings_equal(v->name, "uri")) {
672 changed |= AST_WS_CLIENT_FIELD_URI;
673 } else if (ast_strings_equal(v->name, "protocols")) {
675 } else if (ast_strings_equal(v->name, "username")) {
677 } else if (ast_strings_equal(v->name, "password")) {
679 } else if (ast_strings_equal(v->name, "tls_enabled")) {
681 } else if (ast_strings_equal(v->name, "ca_list_file")) {
683 } else if (ast_strings_equal(v->name, "ca_list_path")) {
685 } else if (ast_strings_equal(v->name, "cert_file")) {
687 } else if (ast_strings_equal(v->name, "priv_key_file")) {
689 } else if (ast_strings_equal(v->name, "reconnect_interval")) {
691 } else if (ast_strings_equal(v->name, "reconnect_attempts")) {
693 } else if (ast_strings_equal(v->name, "connection_timeout")) {
695 } else if (ast_strings_equal(v->name, "verify_server_cert")) {
697 } else if (ast_strings_equal(v->name, "verify_server_hostname")) {
699 } else if (ast_strings_equal(v->name, "proxy_host")) {
701 } else if (ast_strings_equal(v->name, "proxy_username")) {
703 } else if (ast_strings_equal(v->name, "proxy_password")) {
705 } else if (ast_strings_equal(v->name, "enable_tcp_keepalives")) {
707 } else if (ast_strings_equal(v->name, "tcp_keepalive_time")) {
709 } else if (ast_strings_equal(v->name, "tcp_keepalive_interval")) {
711 } else if (ast_strings_equal(v->name, "tcp_keepalive_probes")) {
713 } else if (ast_strings_equal(v->name, "enable_pingpongs")) {
715 } else if (ast_strings_equal(v->name, "pingpong_interval")) {
717 } else if (ast_strings_equal(v->name, "pingpong_probes")) {
719 } else {
720 ast_debug(2, "%s: Unknown change %s\n", new_id, v->name);
721 }
722 }
723
724 if (!changes_found) {
725 ast_debug(2, "%s: No changes found %p %p\n", new_id,
726 old_wc,new_wc);
727 }
728 return changed;
729
730}
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
#define ast_debug(level,...)
Log a DEBUG message.
static struct ast_sorcery * sorcery
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
int ast_strings_equal(const char *str1, const char *str2)
Compare strings for equality checking for NULL.
Definition strings.c:238
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
#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
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_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

References ast_debug, ast_log, ast_sorcery_diff(), ast_sorcery_object_get_id(), ast_strings_equal(), ast_variables_destroy(), AST_WS_CLIENT_FIELD_CA_LIST_FILE, AST_WS_CLIENT_FIELD_CA_LIST_PATH, AST_WS_CLIENT_FIELD_CERT_FILE, AST_WS_CLIENT_FIELD_CONNECTION_TIMEOUT, AST_WS_CLIENT_FIELD_CONNECTION_TYPE, AST_WS_CLIENT_FIELD_NONE, AST_WS_CLIENT_FIELD_PASSWORD, AST_WS_CLIENT_FIELD_PINGPONG_INTERVAL, AST_WS_CLIENT_FIELD_PINGPONG_PROBES, AST_WS_CLIENT_FIELD_PINGPONGS, AST_WS_CLIENT_FIELD_PRIV_KEY_FILE, AST_WS_CLIENT_FIELD_PROTOCOLS, AST_WS_CLIENT_FIELD_PROXY_HOST, AST_WS_CLIENT_FIELD_PROXY_PASSWORD, AST_WS_CLIENT_FIELD_PROXY_USERNAME, AST_WS_CLIENT_FIELD_RECONNECT_ATTEMPTS, AST_WS_CLIENT_FIELD_RECONNECT_INTERVAL, AST_WS_CLIENT_FIELD_TCP_KEEPALIVE_INTERVAL, AST_WS_CLIENT_FIELD_TCP_KEEPALIVE_PROBES, AST_WS_CLIENT_FIELD_TCP_KEEPALIVE_TIME, AST_WS_CLIENT_FIELD_TCP_KEEPALIVES, AST_WS_CLIENT_FIELD_TLS_ENABLED, AST_WS_CLIENT_FIELD_URI, AST_WS_CLIENT_FIELD_USERNAME, AST_WS_CLIENT_FIELD_VERIFY_SERVER_CERT, AST_WS_CLIENT_FIELD_VERIFY_SERVER_HOSTNAME, LOG_WARNING, ast_variable::name, ast_variable::next, NULL, RAII_VAR, sorcery, and ast_variable::value.

Referenced by ari_conf_owc_detect_changes().

◆ ast_websocket_client_observer_add()

int ast_websocket_client_observer_add ( const struct ast_sorcery_observer callbacks)

Add sorcery observers for websocket client events.

Parameters
callbacksThe observer callbacks to add.
Returns
0 on success, -1 on failure.

Definition at line 732 of file res_websocket_client.c.

733{
734 if (!sorcery || !callbacks) {
735 return -1;
736 }
737
738 if (ast_sorcery_observer_add(sorcery, "websocket_client", callbacks)) {
739 ast_log(LOG_ERROR, "Failed to register websocket client observers\n");
740 return -1;
741 }
742
743 return 0;
744}
#define LOG_ERROR
struct @509 callbacks
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

References ast_log, ast_sorcery_observer_add(), callbacks, LOG_ERROR, and sorcery.

Referenced by ari_conf_init().

◆ ast_websocket_client_observer_remove()

void ast_websocket_client_observer_remove ( const struct ast_sorcery_observer callbacks)

Remove sorcery observers for websocket client events.

Parameters
callbacksThe observer callbacks to remove.

Definition at line 746 of file res_websocket_client.c.

747{
748 if (!sorcery || !callbacks) {
749 return;
750 }
751
752 ast_sorcery_observer_remove(sorcery, "websocket_client", callbacks);
753}
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

References ast_sorcery_observer_remove(), callbacks, and sorcery.

Referenced by ari_conf_destroy().

◆ ast_websocket_client_reload()

int ast_websocket_client_reload ( void  )

Force res_websocket_client to reload its configuration.

Returns
0 on success, -1 on failure.

Definition at line 816 of file res_websocket_client.c.

817{
818 ast_debug(2, "Reloading Websocket Client Configuration\n");
819 if (sorcery) {
821 }
822
823 return 0;
824}
void ast_sorcery_reload(const struct ast_sorcery *sorcery)
Inform any wizards to reload persistent objects.
Definition sorcery.c:1472

References ast_debug, ast_sorcery_reload(), and sorcery.

Referenced by ari_conf_load().

◆ ast_websocket_client_retrieve_all()

struct ao2_container * ast_websocket_client_retrieve_all ( void  )

Retrieve a container of all websocket client objects.

Returns
The container. It may be empty but must always be cleaned up by the caller.

Definition at line 628 of file res_websocket_client.c.

629{
630 if (!sorcery) {
631 return NULL;
632 }
633
634 return ast_sorcery_retrieve_by_fields(sorcery, "websocket_client",
636}
@ 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_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

References AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_retrieve_by_fields(), NULL, and sorcery.

◆ ast_websocket_client_retrieve_by_id()

struct ast_websocket_client * ast_websocket_client_retrieve_by_id ( const char *  id)

Retrieve a websocket client object by ID.

Parameters
idThe ID of the websocket client object.
Returns
The websocket client ao2 object or NULL if not found. The reference must be cleaned up by the caller.

Definition at line 638 of file res_websocket_client.c.

639{
640 if (!sorcery) {
641 return NULL;
642 }
643
644 return ast_sorcery_retrieve_by_id(sorcery, "websocket_client", id);
645}
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

References ast_sorcery_retrieve_by_id(), NULL, and sorcery.

Referenced by ast_ari_channels_external_media(), outbound_websocket_websocket_client_id_from_str(), and websocket_new().

◆ load_module()

static int load_module ( void  )
static

Definition at line 756 of file res_websocket_client.c.

757{
758 ast_debug(2, "Initializing Websocket Client Configuration\n");
760 if (!sorcery) {
761 ast_log(LOG_ERROR, "Failed to open sorcery\n");
762 return -1;
763 }
764
765 ast_sorcery_apply_default(sorcery, "websocket_client", "config",
766 "websocket_client.conf,criteria=type=websocket_client");
767
768 if (ast_sorcery_object_register(sorcery, "websocket_client", wc_alloc,
769 NULL, wc_apply)) {
770 ast_log(LOG_ERROR, "Failed to register websocket_client object with sorcery\n");
772 sorcery = NULL;
773 return -1;
774 }
775
776 ast_sorcery_object_field_register(sorcery, "websocket_client", "type", "", OPT_NOOP_T, 0, 0);
777 ast_sorcery_register_cust(websocket_client, connection_type, "");
782 ast_sorcery_register_sf(websocket_client, ast_websocket_client, ca_list_file, ca_list_file, "");
783 ast_sorcery_register_sf(websocket_client, ast_websocket_client, ca_list_path, ca_list_path, "");
785 ast_sorcery_register_sf(websocket_client, ast_websocket_client, priv_key_file, priv_key_file, "");
786 ast_sorcery_register_bool(websocket_client, ast_websocket_client, tls_enabled, tls_enabled, "no");
787 ast_sorcery_register_bool(websocket_client, ast_websocket_client, verify_server_cert, verify_server_cert, "yes");
788 ast_sorcery_register_bool(websocket_client, ast_websocket_client, verify_server_hostname, verify_server_hostname, "yes");
789 ast_sorcery_register_int(websocket_client, ast_websocket_client, connection_timeout, connect_timeout, 500);
790 ast_sorcery_register_int(websocket_client, ast_websocket_client, reconnect_attempts, reconnect_attempts, 4);
791 ast_sorcery_register_int(websocket_client, ast_websocket_client, reconnect_interval, reconnect_interval, 500);
793 ast_sorcery_register_sf(websocket_client, ast_websocket_client, proxy_username, proxy_username, "");
794 ast_sorcery_register_sf(websocket_client, ast_websocket_client, proxy_password, proxy_password, "");
795 ast_sorcery_register_bool(websocket_client, ast_websocket_client, enable_tcp_keepalives, tcp_keepalives, "no");
796 ast_sorcery_register_uint(websocket_client, ast_websocket_client, tcp_keepalive_time, tcp_keepalive_time, 20);
797 ast_sorcery_register_uint(websocket_client, ast_websocket_client, tcp_keepalive_interval, tcp_keepalive_interval, 20);
798 ast_sorcery_register_uint(websocket_client, ast_websocket_client, tcp_keepalive_probes, tcp_keepalive_probes, 3);
799 ast_sorcery_register_bool(websocket_client, ast_websocket_client, enable_pingpongs, pingpongs, "no");
800 ast_sorcery_register_uint(websocket_client, ast_websocket_client, pingpong_interval, pingpong_interval, 20);
801 ast_sorcery_register_uint(websocket_client, ast_websocket_client, pingpong_probes, pingpong_probes, 3);
802
804
805 return 0;
806}
@ OPT_NOOP_T
Type for a default handler that should do nothing.
static void * wc_alloc(const char *id)
static int wc_apply(const struct ast_sorcery *sorcery, void *obj)
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition sorcery.h:1500
#define ast_sorcery_register_cust(object, option, def_value)
Register a custom field within an object.
Definition sorcery.h:1767
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
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition sorcery.h:837
#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
#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

References ast_debug, ast_log, ast_sorcery_apply_default, ast_sorcery_load(), ast_sorcery_object_field_register, ast_sorcery_object_register, ast_sorcery_open, ast_sorcery_register_bool, ast_sorcery_register_cust, ast_sorcery_register_int, ast_sorcery_register_sf, ast_sorcery_register_uint, ast_sorcery_unref, LOG_ERROR, NULL, OPT_NOOP_T, sorcery, wc_alloc(), and wc_apply().

◆ reload_module()

static int reload_module ( void  )
static

Definition at line 808 of file res_websocket_client.c.

809{
810 ast_debug(2, "Reloading Websocket Client Configuration\n");
812
813 return 0;
814}

References ast_debug, ast_sorcery_reload(), and sorcery.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 826 of file res_websocket_client.c.

827{
828 ast_debug(2, "Unloading Websocket Client Configuration\n");
829 if (sorcery) {
831 sorcery = NULL;
832 }
833 return 0;
834}

References ast_debug, ast_sorcery_unref, NULL, and sorcery.

◆ wc_alloc()

static void * wc_alloc ( const char *  id)
static

Definition at line 499 of file res_websocket_client.c.

500{
501 struct ast_websocket_client *wc = NULL;
502
503 wc = ast_sorcery_generic_alloc(sizeof(*wc), wc_dtor);
504 if (!wc) {
505 return NULL;
506 }
507
508 if (ast_string_field_init(wc, 1024) != 0) {
509 ao2_cleanup(wc);
510 return NULL;
511 }
512
513 if (ast_string_field_init_extended(wc, uri_params) != 0) {
514 ao2_cleanup(wc);
515 return NULL;
516 }
517
518 if (ast_string_field_init_extended(wc, proxy_host) != 0) {
519 ao2_cleanup(wc);
520 return NULL;
521 }
522
523 if (ast_string_field_init_extended(wc, proxy_username) != 0) {
524 ao2_cleanup(wc);
525 return NULL;
526 }
527
528 if (ast_string_field_init_extended(wc, proxy_password) != 0) {
529 ao2_cleanup(wc);
530 return NULL;
531 }
532
533 ast_debug(2, "%s: Allocated websocket client config\n", id);
534 return wc;
535}
#define ao2_cleanup(obj)
Definition astobj2.h:1934
static void wc_dtor(void *obj)
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition sorcery.c:1792
#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.

References ao2_cleanup, ast_debug, ast_sorcery_generic_alloc(), ast_string_field_init, ast_string_field_init_extended, NULL, and wc_dtor().

Referenced by load_module().

◆ wc_apply()

static int wc_apply ( const struct ast_sorcery sorcery,
void *  obj 
)
static

Definition at line 575 of file res_websocket_client.c.

576{
577 struct ast_websocket_client *wc = obj;
578 const char *id = ast_sorcery_object_get_id(wc);
579 int res = 0;
580
581 ast_debug(3, "%s: Applying config\n", id);
582
583 if (ast_strlen_zero(wc->uri)) {
584 ast_log(LOG_WARNING, "%s: Websocket client missing uri\n", id);
585 res = -1;
586 }
587
588 if (!ast_strlen_zero(wc->proxy_host)) {
589 char *host = NULL;
590 char *port = NULL;
591 char *s = ast_strdupa(wc->proxy_host);
592 if (!ast_sockaddr_split_hostport(s, &host, &port, PARSE_PORT_REQUIRE)) {
593 ast_log(LOG_WARNING, "%s: proxy_host '%s' is missing a port\n", id, wc->proxy_host);
594 res = -1;
595 }
596 }
597
598 if (wc->tcp_keepalives) {
600 ast_log(LOG_WARNING, "%s: tcp_keepalive_time, tcp_keepalive_interval and tcp_keepalive_probes must all be non-zero\n", id);
601 res = -1;
602 }
603 }
604
605 if (wc->pingpongs) {
606 if (!wc->pingpong_interval || !wc->pingpong_probes) {
607 ast_log(LOG_WARNING, "%s: pingpong_interval and pingpong_probes must be non-zero\n", id);
608 res = -1;
609 }
610 }
611 if (res != 0) {
612 ast_log(LOG_WARNING, "%s: Websocket client configuration failed\n", id);
613 } else {
614 ast_debug(3, "%s: Websocket client configuration succeeded\n", id);
615
618 wc->reconnect_attempts = INT_MAX;
619 } else {
620 wc->reconnect_attempts = 4;
621 }
622 }
623 }
624
625 return res;
626}
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
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
#define DEFAULT_RECONNECT_ATTEMPTS

References ast_debug, ast_log, ast_sockaddr_split_hostport(), ast_sorcery_object_get_id(), ast_strdupa, ast_strlen_zero(), AST_WS_TYPE_CLIENT_PERSISTENT, ast_websocket_client::connection_type, DEFAULT_RECONNECT_ATTEMPTS, LOG_WARNING, NULL, PARSE_PORT_REQUIRE, ast_websocket_client::pingpong_interval, ast_websocket_client::pingpong_probes, ast_websocket_client::pingpongs, ast_websocket_client::reconnect_attempts, ast_websocket_client::tcp_keepalive_interval, ast_websocket_client::tcp_keepalive_probes, ast_websocket_client::tcp_keepalive_time, ast_websocket_client::tcp_keepalives, and ast_websocket_client::uri.

Referenced by load_module().

◆ wc_dtor()

static void wc_dtor ( void *  obj)
static

Definition at line 490 of file res_websocket_client.c.

491{
492 struct ast_websocket_client *wc = obj;
493
494 ast_debug(3, "%s: Disposing of websocket client config\n",
497}
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object

References ast_debug, ast_sorcery_object_get_id(), and ast_string_field_free_memory.

Referenced by wc_alloc().

◆ websocket_client_connection_type_from_str()

static int websocket_client_connection_type_from_str ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Definition at line 537 of file res_websocket_client.c.

539{
540 struct ast_websocket_client *ws = obj;
541
542 if (strcasecmp(var->value, "persistent") == 0) {
544 } else if (strcasecmp(var->value, "per_call_config") == 0) {
546 } else {
547 return -1;
548 }
549
550 return 0;
551}
#define var
Definition ast_expr2f.c:605
@ AST_WS_TYPE_CLIENT_PER_CALL_CONFIG

References AST_WS_TYPE_CLIENT_PER_CALL_CONFIG, AST_WS_TYPE_CLIENT_PERSISTENT, ast_websocket_client::connection_type, and var.

◆ websocket_client_connection_type_to_str()

static int websocket_client_connection_type_to_str ( const void *  obj,
const intptr_t *  args,
char **  buf 
)
static

Definition at line 553 of file res_websocket_client.c.

554{
555 const struct ast_websocket_client *wc = obj;
556
558 *buf = ast_strdup("persistent");
560 *buf = ast_strdup("per_call_config");
561 } else {
562 return -1;
563 }
564
565 return 0;
566}
char buf[BUFSIZE]
Definition eagi_proxy.c:66

References ast_strdup, AST_WS_TYPE_CLIENT_PER_CALL_CONFIG, AST_WS_TYPE_CLIENT_PERSISTENT, buf, and ast_websocket_client::connection_type.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "WebSocket Client Support" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, .requires = "res_http_websocket", }
static

Definition at line 843 of file res_websocket_client.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 843 of file res_websocket_client.c.

◆ sorcery

struct ast_sorcery* sorcery = NULL
static