46#define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
49#define CLIENT_KEY_SIZE 16
52#define MAX_PROTOCOL_BUCKETS 7
58#define DEFAULT_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE
61#define MAXIMUM_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE
66#define DEFAULT_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE
69#define MAXIMUM_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE
79#define MAX_WS_HDR_SZ 14
80#define MIN_WS_HDR_SZ 2
87#define WS_PING_PAYLOAD "WS_CLIENT_PING"
88#define WS_PING_PAYLOAD_LEN 14
150#define WS_SESSION_REMOTE(_session) (_session ? (_session->client ? _session->client->options->uri : ast_sockaddr_stringify(&_session->remote_address)) : "NULL")
151#define ARE_PINGPONGS_ENABLED(_session) (_session && _session->client && _session->client->options->pingpongs && _session->client->ping_sched_timer >= 0)
175 return "per_call_config";
193 const char *
name = obj;
202 const char *protocol = arg;
237 if (!server->protocols) {
260 SCOPE_ENTER(2,
"%s: Session %p destructor\n",
id, obj);
286 if (!protocol->
name) {
327 "expected version '%u', got version '%u'\n",
345 ast_debug(1,
"WebSocket registered sub-protocol '%s'\n", protocol->name);
367 ast_debug(1,
"WebSocket unregistered sub-protocol '%s'\n",
name);
378 uint8_t mask_key_idx;
380 uint8_t length = frame[1] & 0x7f;
383 mask_key_idx = length == 126 ? 4 : length == 127 ? 10 : 2;
385 for (i = 0; i < payload_size; i++) {
386 payload[i] ^= ((
char *)&mask_key)[i % 4];
426 char frame[8] = { 0, };
427 int header_size, fsize, res = 0;
457 header_size =
session->client ? 6 : 2;
458 fsize = header_size + 2;
461 frame[0] = opcode | 0x80;
469 reason =
session->close_status_code;
489 ast_trace(-1,
"%s: %s Closing socket. fd: %d\n",
513 if (
session->client->missed_pong_count
516 int mpc =
session->client->missed_pong_count;
518 session->client->missed_pong_count = 0;
519 SCOPE_EXIT_RTN_VALUE(1,
"%s: Received PONG from our own PING. Missed count was: %d. Cleared.\n",
528 if (payload_len >= 2) {
537 ast_trace(-1,
"%s: Received CLOSE response from remote with reason: %s (%d)\n",
564 char *
payload, uint64_t payload_size)
566 size_t header_size = 2;
570 SCOPE_ENTER(4,
"%s: Opcode: %s Length: %"PRIu64
"\n",
573 if (payload_size < 126) {
574 length = payload_size;
575 }
else if (payload_size < (1 << 16)) {
601 }
else if (length == 127) {
605 memcpy(&frame[header_size],
payload, payload_size);
619 ast_trace(-1,
"%s: Closing WS with 1011 because we can't fulfill a write request\n",
622 SCOPE_EXIT_RTN_VALUE(-1,
"%s: Closed WS with 1011 because we couldn't fulfill a write request\n",
647 SCOPE_ENTER(2,
"%s: Reffing. Refcount: %d\n",
id, refcount);
656 SCOPE_ENTER(2,
"%s: Unreffing. Refcount: %d\n",
id, refcount);
674 return &
session->remote_address;
679 return &
session->local_address;
752 errno = ECONNABORTED;
767 if (rlen < 0 &&
errno != EAGAIN) {
806 int mask_present = 0;
807 char *mask =
NULL, *new_payload =
NULL;
825 fin = (
session->buf[0] >> 7) & 1;
826 mask_present = (
session->buf[1] >> 7) & 1;
829 options_len += mask_present ? 4 : 0;
872 (*payload)[pos] ^= mask[pos % 4];
898 session->payload = new_payload;
957 unsigned combined_length = strlen(key) + strlen(
WEBSOCKET_GUID) + 1;
976 ast_str_set(&http_header, 0,
"Sec-WebSocket-Version: 7, 8, 13\r\n");
984 char *requested_protocols =
NULL, *protocol =
NULL;
994 ast_http_error(ser, 501,
"Not Implemented",
"Attempt to use unimplemented / unsupported method");
1001 for (v = headers; v; v = v->
next) {
1002 if (!strcasecmp(v->
name,
"Upgrade")) {
1004 }
else if (!strcasecmp(v->
name,
"Sec-WebSocket-Key")) {
1006 }
else if (!strcasecmp(v->
name,
"Sec-WebSocket-Key1")) {
1008 }
else if (!strcasecmp(v->
name,
"Sec-WebSocket-Key2")) {
1010 }
else if (!strcasecmp(v->
name,
"Sec-WebSocket-Protocol")) {
1012 }
else if (!strcasecmp(v->
name,
"Sec-WebSocket-Version")) {
1020 if (!upgrade || strcasecmp(upgrade,
"websocket")) {
1021 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - did not request WebSocket\n",
1030 if (!protocol_handler) {
1032 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - no protocols requested\n",
1037 }
else if (key1 && key2) {
1040 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - unsupported version '00/76' chosen\n",
1046 if (!protocol_handler && protos) {
1049 while (!protocol_handler && (protocol =
strsep(&requested_protocols,
","))) {
1055 if (!protocol_handler) {
1056 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - no protocols out of '%s' supported\n",
1068 ao2_ref(protocol_handler, -1);
1074 ao2_ref(protocol_handler, -1);
1082 ao2_ref(protocol_handler, -1);
1089 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - failed to generate a session id\n",
1091 ast_http_error(ser, 500,
"Internal Server Error",
"Allocation failed");
1092 ao2_ref(protocol_handler, -1);
1098 ast_debug(3,
"WebSocket connection from '%s' rejected by protocol handler '%s'\n",
1101 ao2_ref(protocol_handler, -1);
1116 "HTTP/1.1 101 Switching Protocols\r\n"
1118 "Connection: Upgrade\r\n"
1119 "Sec-WebSocket-Accept: %s\r\n"
1120 "Sec-WebSocket-Protocol: %s\r\n\r\n",
1126 "HTTP/1.1 101 Switching Protocols\r\n"
1128 "Connection: Upgrade\r\n"
1129 "Sec-WebSocket-Accept: %s\r\n\r\n",
1136 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - unsupported version '%d' chosen\n",
1139 ao2_ref(protocol_handler, -1);
1145 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - failed to enable keepalive\n",
1149 ao2_ref(protocol_handler, -1);
1155 ast_log(
LOG_WARNING,
"WebSocket connection from '%s' could not be accepted - failed to get local address\n",
1159 ao2_ref(protocol_handler, -1);
1175 ao2_ref(protocol_handler, -1);
1189 .description =
"Asterisk HTTP WebSocket",
1201 ast_debug(1,
"Entering WebSocket echo loop\n");
1209 uint64_t payload_len;
1224 ast_debug(1,
"Ignored WebSocket opcode %u\n", opcode);
1229 ast_debug(1,
"Exiting WebSocket echo loop\n");
1285 struct ast_str **path,
char **userinfo,
int proxy)
1319 if (
args->tls_cfg) {
1337 const char *resolve_host =
NULL;
1344 args->accept_fd = -1;
1346 args->name =
"websocket client";
1364 snprintf(
args->hostname,
sizeof(
args->hostname),
1386 memcpy(key + i, &num,
sizeof(
long));
1398 SCOPE_ENTER(2,
"%s: Client destructor %p\n",
id, obj);
1428#define SAFE_STRDUP_WITH_ERROR_RTN(_clone, _str) \
1430 char *_duped = NULL; \
1432 _duped = ast_strdup(_str); \
1434 ao2_cleanup(_clone); \
1572 if (response_code <= 0) {
1576 switch (response_code) {
1620 int has_upgrade = 0;
1621 int has_connection = 0;
1623 int has_protocol = 0;
1624 int status_code = 0;
1631 (
errno == EINTR ||
errno == EAGAIN) ?
"Timeout" :
"Error");
1654 (
errno == EINTR ||
errno == EAGAIN) ?
"Timeout" :
"Error");
1666 if (proxy || parsed > 0) {
1672 name,
"upgrade",
value,
"websocket")) < 0) {
1674 }
else if (!has_connection &&
1676 name,
"connection",
value,
"upgrade")) < 0) {
1678 }
else if (!has_accept &&
1684 }
else if (!has_protocol &&
1687 if (has_protocol < 0) {
1691 }
else if (!strcasecmp(
name,
"sec-websocket-extensions")) {
1693 "supported by client\n");
1698 if (status_code == 408) {
1704 res = has_upgrade && has_connection && has_accept ?
1741#define optional_header_spec "%s%s%s"
1742#define print_optional_header(test, name, value) \
1744 test ? value : "", \
1752 size_t bytes_written = 0;
1765 "CONNECT %s HTTP/1.1\r\n"
1768 "Proxy-Connection: Keep-Alive\r\n"
1776 if (bytes_written < 0) {
1791 size_t protocols_len = 0;
1808 "GET /%s HTTP/1.1\r\n"
1809 "Sec-WebSocket-Version: %d\r\n"
1810 "Upgrade: websocket\r\n"
1811 "Connection: Upgrade\r\n"
1815 "Sec-WebSocket-Key: %s\r\n"
1851 if (proxy && original_tls_enabled ) {
1852 ast_trace(-1,
"%s: Disabling TLS while handshaking with proxy\n",
options->uri);
1915 (
const char *uri,
const char *protocols,
struct ast_tls_config *tls_cfg,
1946 if (
session->client->missed_pong_count > 1) {
1948 session->client->missed_pong_count);
1951 if (
session->client->missed_pong_count >=
session->client->options->pingpong_probes) {
1955 session->client->ping_sched_timer = -1;
1961 session->client->missed_pong_count++;
1964 return session->client->options->pingpong_interval * 1000;
2029 setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &
enabled,
sizeof(
enabled));
2030 setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &
options->tcp_keepalive_time,
sizeof(
options->tcp_keepalive_time));
2031 setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &
options->tcp_keepalive_interval,
sizeof(&
options->tcp_keepalive_interval));
2032 setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, &
options->tcp_keepalive_probes,
sizeof(&
options->tcp_keepalive_probes));
2057 while (fragmented) {
2061 "error reading string data\n");
2081 "non string data received\n");
2096 uint64_t
len = strlen(
buf);
2098 ast_debug(3,
"Writing websocket string of length %" PRIu64
"\n",
len);
uint64_t ntohll(uint64_t net64)
char * strsep(char **str, const char *delims)
uint64_t htonll(uint64_t host64)
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_mansession session
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
#define ast_strndup(str, len)
A wrapper for strndup()
#define ast_realloc(p, len)
A wrapper for realloc()
#define ast_strdup(str)
A wrapper for strdup()
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
#define ast_calloc(num, len)
A wrapper for calloc()
#define ast_malloc(len)
A wrapper for malloc()
@ AO2_ALLOC_OPT_LOCK_MUTEX
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_unlink(container, obj)
Remove an object from a container.
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
#define ao2_find(container, arg, flags)
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
#define ao2_alloc(data_size, destructor_fn)
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
static char version[AST_MAX_EXTENSION]
static struct ast_channel * callback(struct ast_channelstorage_instance *driver, ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags, int rdlock)
Generic File Format Support. Should be included by clients of the file handling routines....
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define SCOPE_EXIT_RTN(...)
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
#define SCOPE_ENTER(level,...)
#define ast_trace(level,...)
Support for Private Asterisk HTTP Servers.
void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method, int status_code, const char *status_title, struct ast_str *http_header, struct ast_str *out, int fd, unsigned int static_content)
Generic function for sending HTTP/1.1 response.
ast_http_method
HTTP Request methods known by Asterisk.
int ast_http_header_parse(char *buf, char **name, char **value)
Parse a header into the given name/value strings.
int ast_http_header_match_in(const char *name, const char *expected_name, const char *value, const char *expected_value)
Check if the header name matches the expected header name. If so, then check to see if the value can ...
void ast_http_uri_unlink(struct ast_http_uri *urihandler)
Unregister a URI handler.
int ast_http_body_discard(struct ast_tcptls_session_instance *ser)
Read and discard any unread HTTP request body.
struct ast_variable * ast_http_create_basic_auth_header(const char *userid, const char *password)
Create an HTTP authorization header.
int ast_http_header_match(const char *name, const char *expected_name, const char *value, const char *expected_value)
Check if the header and value match (case insensitive) their associated expected values.
int ast_http_response_status_line(const char *buf, const char *version, int code)
Parse the http response status line.
void ast_http_request_close_on_completion(struct ast_tcptls_session_instance *ser)
Request the HTTP connection be closed after this HTTP request.
void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text)
Send HTTP error message and close socket.
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
ast_websocket_status_code
Websocket Status Codes from RFC-6455.
@ AST_WEBSOCKET_STATUS_RESERVED_1015
@ AST_WEBSOCKET_STATUS_BAD_GATEWAY
@ AST_WEBSOCKET_STATUS_UNSUPPORTED_DATA
@ AST_WEBSOCKET_STATUS_PROTOCOL_ERROR
@ AST_WEBSOCKET_STATUS_NORMAL
@ AST_WEBSOCKET_STATUS_MANDATORY_EXT
@ AST_WEBSOCKET_STATUS_TOO_BIG
@ AST_WEBSOCKET_STATUS_RESERVED_1006
@ AST_WEBSOCKET_STATUS_RESERVED_1004
@ AST_WEBSOCKET_STATUS_RESERVED_1013
@ AST_WEBSOCKET_STATUS_RESERVED_1005
@ AST_WEBSOCKET_STATUS_POLICY_VIOLATION
@ AST_WEBSOCKET_STATUS_GOING_AWAY
@ AST_WEBSOCKET_STATUS_INTERNAL_ERROR
@ AST_WEBSOCKET_STATUS_RESERVED_1012
@ AST_WEBSOCKET_STATUS_INVALID_FRAME
#define AST_WEBSOCKET_PROTOCOL_VERSION
Protocol version. This prevents dynamically loadable modules from registering if this struct is chang...
ast_websocket_result
Result code for a websocket client.
#define AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE
Size of the pre-determined buffer for WebSocket frames.
#define AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT
Default websocket write timeout, in ms.
ast_websocket_opcode
WebSocket operation codes.
@ AST_WEBSOCKET_OPCODE_PING
@ AST_WEBSOCKET_OPCODE_PONG
@ AST_WEBSOCKET_OPCODE_CONTINUATION
@ AST_WEBSOCKET_OPCODE_BINARY
@ AST_WEBSOCKET_OPCODE_CLOSE
@ AST_WEBSOCKET_OPCODE_TEXT
ast_websocket_type
WebSocket connection/configuration types.
@ AST_WS_TYPE_CLIENT_PER_CALL_CONFIG
@ AST_WS_TYPE_CLIENT_PERSISTENT
@ AST_WS_TYPE_CLIENT_PER_CALL
void(* ast_websocket_callback)(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers)
Callback for when a new connection for a sub-protocol is established.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
#define ast_debug(level,...)
Log a DEBUG message.
void ast_iostream_blocking(struct ast_iostream *stream)
Make an iostream blocking.
ssize_t ast_iostream_printf(struct ast_iostream *stream, const char *format,...)
Write a formatted string to an iostream.
ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size)
Read a LF-terminated string from an iostream.
void ast_iostream_set_timeout_inactivity(struct ast_iostream *stream, int timeout)
Set the iostream inactivity timeout timer.
SSL * ast_iostream_get_ssl(struct ast_iostream *stream)
Get a pointer to an iostream's OpenSSL SSL structure.
ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t count)
Write data to an iostream.
int ast_iostream_get_fd(struct ast_iostream *stream)
Get an iostream's file descriptor.
void ast_iostream_set_exclusive_input(struct ast_iostream *stream, int exclusive_input)
Set the iostream if it can exclusively depend upon the set timeouts.
ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count)
Read data from an iostream.
void ast_iostream_set_timeout_sequence(struct ast_iostream *stream, struct timeval start, int timeout)
Set the iostream I/O sequence timeout timer.
void ast_iostream_nonblock(struct ast_iostream *stream)
Make an iostream non-blocking.
int ast_iostream_close(struct ast_iostream *stream)
Close an iostream.
void ast_iostream_set_timeout_disable(struct ast_iostream *stream)
Disable the iostream timeout timer.
int ast_iostream_wait_for_input(struct ast_iostream *stream, int timeout)
Wait for input on the iostream's file descriptor.
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Asterisk module definitions.
#define SCOPED_MODULE_USE(module)
@ AST_MODFLAG_GLOBAL_SYMBOLS
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
@ AST_MODPRI_CHANNEL_DEPEND
@ AST_MODULE_SUPPORT_CORE
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
Wrapper around getsockname(2) that uses struct ast_sockaddr.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
#define AST_OPTIONAL_API_NAME(name)
Expands to the name of the implementation function.
void AST_OPTIONAL_API_NAME() ast_websocket_ref(struct ast_websocket *session)
Increase the reference count for a WebSocket session.
int AST_OPTIONAL_API_NAME() ast_websocket_add_protocol(const char *name, ast_websocket_callback callback)
Add a sub-protocol handler to the default /ws server.
static int websocket_close(struct ast_websocket *session, uint16_t reason, int force)
const char *AST_OPTIONAL_API_NAME() ast_websocket_session_id(struct ast_websocket *session)
Get the session ID for a WebSocket session.
static void websocket_client_destroy(void *obj)
static void websocket_echo_callback(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers)
Simple echo implementation which echoes received text and binary frames.
static enum ast_websocket_result websocket_client_handle_response_code(struct websocket_client *client, int response_code, int proxy)
int AST_OPTIONAL_API_NAME() ast_websocket_write(struct ast_websocket *session, enum ast_websocket_opcode opcode, char *payload, uint64_t payload_size)
Write function for websocket traffic.
static int ping_scheduler_callback(const void *obj)
static void websocket_bad_request(struct ast_tcptls_session_instance *ser)
static int websocket_client_parse_uri(const char *uri, char **host, struct ast_str **path, char **userinfo, int proxy)
Parse the given uri into a path and remote address.
static struct ast_http_uri websocketuri
int AST_OPTIONAL_API_NAME() ast_websocket_server_add_protocol2(struct ast_websocket_server *server, struct ast_websocket_protocol *protocol)
Add a sub-protocol handler to the given server.
int AST_OPTIONAL_API_NAME() ast_websocket_write_string(struct ast_websocket *ws, const char *buf)
Construct and transmit a WebSocket frame containing string data.
void AST_OPTIONAL_API_NAME() ast_websocket_reconstruct_enable(struct ast_websocket *session, size_t bytes)
Enable multi-frame reconstruction up to a certain number of bytes.
static struct ast_websocket_protocol * one_protocol(struct ast_websocket_server *server)
If the server has exactly one configured protocol, return it.
static void websocket_client_args_destroy(void *obj)
static int ws_safe_read(struct ast_websocket *session, char *buf, size_t len, enum ast_websocket_opcode *opcode)
struct ast_sockaddr *AST_OPTIONAL_API_NAME() ast_websocket_local_address(struct ast_websocket *session)
Get the local address for a WebSocket connection session.
static int websocket_remove_protocol_internal(const char *name, ast_websocket_callback callback)
static struct ast_websocket * websocket_client_create(struct ast_websocket_client_options *options, enum ast_websocket_result *result)
static struct ast_sched_context * ping_scheduler
#define SAFE_STRDUP_WITH_ERROR_RTN(_clone, _str)
#define optional_header_spec
#define MAX_PROTOCOL_BUCKETS
Number of buckets for registered protocols.
struct ast_websocket *AST_OPTIONAL_API_NAME() ast_websocket_client_create(const char *uri, const char *protocols, struct ast_tls_config *tls_cfg, enum ast_websocket_result *result)
Create, and connect, a websocket client.
struct ast_sockaddr *AST_OPTIONAL_API_NAME() ast_websocket_remote_address(struct ast_websocket *session)
Get the remote address for a WebSocket connected session.
static struct ast_websocket_server * websocket_server_internal_create(void)
int AST_OPTIONAL_API_NAME() ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
Callback suitable for use with a ast_http_uri.
int AST_OPTIONAL_API_NAME() ast_websocket_is_secure(struct ast_websocket *session)
Get whether the WebSocket session is using a secure transport or not.
static void websocket_server_dtor(void *obj)
static enum ast_websocket_result websocket_client_handshake_get_response(struct websocket_client *client, int proxy)
static enum ast_websocket_result websocket_client_handshake(struct websocket_client *client)
static void websocket_mask_payload(struct ast_websocket *session, char *frame, char *payload, uint64_t payload_size)
Perform payload masking for client sessions.
void AST_OPTIONAL_API_NAME() ast_websocket_reconstruct_disable(struct ast_websocket *session)
Disable multi-frame reconstruction.
static struct ast_tcptls_session_args * websocket_client_args_create(struct ast_websocket *ws, struct ast_websocket_client_options *options, enum ast_websocket_result *result)
static int protocol_cmp_fn(void *obj, void *arg, int flags)
Comparison function for protocols.
#define DEFAULT_RECONSTRUCTION_CEILING
Default reconstruction size for multi-frame payload reconstruction. If exceeded the next frame will s...
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.
static char * websocket_client_create_key(void)
int AST_OPTIONAL_API_NAME() ast_websocket_wait_for_input(struct ast_websocket *session, int timeout)
Wait for the WebSocket session to be ready to be read.
static const char * closed_by_str[]
const char * ast_websocket_type_to_str(enum ast_websocket_type type)
int AST_OPTIONAL_API_NAME() ast_websocket_fd(struct ast_websocket *session)
Get the file descriptor for a WebSocket session.
static void protocol_destroy_fn(void *obj)
Destructor function for protocols.
static char * websocket_combine_key(const char *key, char *res, int res_size)
#define print_optional_header(test, name, value)
static const char * closed_by_to_str(enum ws_closed_by closed_by)
#define WS_PING_PAYLOAD
WS_PING_PAYLOAD It's possible that a user of this API could be sending their own PINGs and expecting ...
static struct ast_websocket_server * websocket_server_create_impl(void)
static int websocket_handled_pong_or_close(struct ast_websocket *session, char *payload, uint64_t payload_len, enum ast_websocket_opcode opcode)
const char *AST_OPTIONAL_API_NAME() ast_websocket_client_accept_protocol(struct ast_websocket *ws)
Retrieve the server accepted sub-protocol on the client.
static struct ast_websocket_client_options * client_options_clone(struct ast_websocket_client_options *options)
static int protocol_hash_fn(const void *obj, const int flags)
Hashing function for protocols.
int AST_OPTIONAL_API_NAME() ast_websocket_set_nonblock(struct ast_websocket *session)
Set the socket of a WebSocket session to be non-blocking.
#define WS_SESSION_REMOTE(_session)
int AST_OPTIONAL_API_NAME() ast_websocket_server_add_protocol(struct ast_websocket_server *server, const char *name, ast_websocket_callback callback)
Add a sub-protocol handler to the given server.
static int websocket_add_protocol_internal(const char *name, ast_websocket_callback callback)
static enum ast_websocket_result websocket_proxy_handshake(struct websocket_client *client)
#define CLIENT_KEY_SIZE
Length of a websocket's client key.
static void ping_scheduler_cancel(struct ast_websocket *session)
const char *AST_OPTIONAL_API_NAME() ast_websocket_status_to_str(enum ast_websocket_status_code code)
Convert a websocket status code to a string.
static int load_module(void)
static void client_options_destroy(void *obj)
#define ARE_PINGPONGS_ENABLED(_session)
int AST_OPTIONAL_API_NAME() ast_websocket_server_remove_protocol(struct ast_websocket_server *server, const char *name, ast_websocket_callback callback)
Remove a sub-protocol handler from the given server.
static const struct status_map websocket_status_map[]
static int unload_module(void)
#define WEBSOCKET_GUID
GUID used to compute the accept key, defined in the specifications.
const char * websocket_result_string_map[]
#define WS_PING_PAYLOAD_LEN
struct ast_websocket_protocol *AST_OPTIONAL_API_NAME() ast_websocket_sub_protocol_alloc(const char *name)
Allocate a websocket sub-protocol instance.
int AST_OPTIONAL_API_NAME() ast_websocket_set_timeout(struct ast_websocket *session, int timeout)
Set the timeout on a non-blocking WebSocket session.
int AST_OPTIONAL_API_NAME() ast_websocket_read(struct ast_websocket *session, char **payload, uint64_t *payload_len, enum ast_websocket_opcode *opcode, int *fragmented)
Read a WebSocket frame and handle it.
static enum ast_websocket_result websocket_client_connect(struct ast_websocket *ws, struct ast_websocket_client_options *options)
int AST_OPTIONAL_API_NAME() ast_websocket_close(struct ast_websocket *session, uint16_t reason)
Close function for websocket session.
struct ast_websocket_server *AST_OPTIONAL_API_NAME() ast_websocket_server_create(void)
Creates a ast_websocket_server.
static void websocket_client_start_handshake_timer(struct websocket_client *client)
int AST_OPTIONAL_API_NAME() ast_websocket_remove_protocol(const char *name, ast_websocket_callback callback)
Remove a sub-protocol handler from the default /ws server.
static void session_destroy_fn(void *obj)
Destructor function for sessions.
void AST_OPTIONAL_API_NAME() ast_websocket_unref(struct ast_websocket *session)
Decrease the reference count for a WebSocket session.
static const char * websocket_opcode2str(enum ast_websocket_opcode opcode)
static const char * opcode_map[]
int AST_OPTIONAL_API_NAME() ast_websocket_read_string(struct ast_websocket *ws, char **buf)
Read a WebSocket frame containing string data.
int AST_OPTIONAL_API_NAME() ast_websocket_add_protocol2(struct ast_websocket_protocol *protocol)
Add a sub-protocol handler to the default /ws server.
static void websocket_client_stop_handshake_timer(struct websocket_client *client)
#define MAXIMUM_RECONSTRUCTION_CEILING
Maximum reconstruction size for multi-frame payload reconstruction.
const char *AST_OPTIONAL_API_NAME() ast_websocket_result_to_str(enum ast_websocket_result result)
Convert a websocket result code to a string.
Scheduler Routines (derived from cheops)
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
static force_inline int attribute_pure ast_str_case_hash(const char *str)
Compute a hash value on a case-insensitive string.
#define AST_YESNO(x)
return Yes or No depending on the argument.
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition of a URI handler.
ast_http_callback callback
Socket address structure.
Support for dynamic strings.
arguments for the accepting thread
struct ast_tls_config * tls_cfg
int suppress_connection_msgs
describes a server instance
struct ast_iostream * stream
struct ast_sockaddr remote_address
Stores parsed uri information.
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Options used for a websocket client.
unsigned int pingpong_interval
const char * proxy_password
struct ast_tls_config * tls_cfg
const char * proxy_username
A websocket protocol implementation.
ast_websocket_callback session_established
Callback called when a new session is established. Mandatory.
unsigned int version
Protocol version. Should be set to /ref AST_WEBSOCKET_PROTOCOL_VERSION.
char * name
Name of the protocol.
ast_websocket_pre_callback session_attempted
Callback called when a new session is attempted. Optional.
Structure for a WebSocket server.
struct ao2_container * protocols
Structure definition for session.
struct ast_iostream * stream
enum ws_closed_by closed_by
struct websocket_client * client
struct ast_sockaddr local_address
uint16_t close_status_code
struct ast_sockaddr remote_address
char session_id[AST_UUID_STR_LEN]
enum ast_websocket_opcode opcode
char buf[AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE]
unsigned int non_blocking
enum ast_websocket_status_code code
struct ast_websocket_client_options * options
struct ast_tcptls_session_args * args
struct ast_tcptls_session_instance * ser
struct ast_str * resource_name
int suppress_connection_msgs
int ast_ssl_setup_client(struct ast_tls_config *cfg)
Set up an SSL client.
struct ast_tcptls_session_instance * ast_tcptls_start_tls(struct ast_tcptls_session_instance *tcptls_session)
Start TLS negotiation on an existing unsecured connection.
void ast_ssl_teardown(struct ast_tls_config *cfg)
free resources used by an SSL server
struct ast_tcptls_session_instance * ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
Creates a client connection's ast_tcptls_session_instance.
struct ast_tcptls_session_instance * ast_tcptls_client_start_timeout(struct ast_tcptls_session_instance *tcptls_session, int timeout)
Attempt to connect and start a tcptls session within the given timeout.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Handle unaligned data access.
static void put_unaligned_uint16(void *p, unsigned short datum)
static unsigned short get_unaligned_uint16(const void *p)
static void put_unaligned_uint64(void *p, uint64_t datum)
static void put_unaligned_uint32(void *p, unsigned int datum)
static uint64_t get_unaligned_uint64(const void *p)
const char * ast_uri_path(const struct ast_uri *uri)
Retrieve the uri path.
struct ast_uri * ast_uri_parse_http(const char *uri)
Parse the given http uri into a structure.
struct ast_uri * ast_uri_parse_websocket(const char *uri)
Parse the given websocket uri into a structure.
const char * ast_uri_query(const struct ast_uri *uri)
Retrieve the uri query parameters.
const char * ast_uri_user_info(const struct ast_uri *uri)
Retrieve the uri user information.
char * ast_uri_make_host_with_port(const struct ast_uri *uri)
Retrieve a string of the host and port.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
long int ast_random(void)
#define ast_fd_set_flags(fd, flags)
Set flags on the given file descriptor.
#define ARRAY_IN_BOUNDS(v, a)
Checks to see if value is within the bounds of the given array.
void ast_sha1_hash_uint(uint8_t *digest, const char *input)
Produces SHA1 hash based on input string, stored in uint8_t array.
Universally unique identifier support.
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.