Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions | Variables
res_http_websocket.c File Reference

WebSocket support for the Asterisk internal HTTP server. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/http.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/file.h"
#include "asterisk/unaligned.h"
#include "asterisk/uri.h"
#include "asterisk/uuid.h"
#include "asterisk/http_websocket.h"

Go to the source code of this file.

Data Structures

struct  ast_websocket
 Structure definition for session. More...
 
struct  ast_websocket_server
 Structure for a WebSocket server. More...
 
struct  websocket_client
 

Macros

#define AST_API_MODULE
 
#define CLIENT_KEY_SIZE   16
 Length of a websocket's client key.
 
#define DEFAULT_RECONSTRUCTION_CEILING   MAXIMUM_FRAME_SIZE
 Default reconstruction size for multi-frame payload reconstruction. If exceeded the next frame will start a payload.
 
#define MAX_PROTOCOL_BUCKETS   7
 Number of buckets for registered protocols.
 
#define MAX_WS_HDR_SZ   14
 Maximum size of a websocket frame header 1 byte flags and opcode 1 byte mask flag + payload len 8 bytes max extended length 4 bytes optional masking key ... payload follows ...
 
#define MAXIMUM_FRAME_SIZE   65535
 Size of the pre-determined buffer for WebSocket frames.
 
#define MAXIMUM_RECONSTRUCTION_CEILING   MAXIMUM_FRAME_SIZE
 Maximum reconstruction size for multi-frame payload reconstruction.
 
#define MIN_WS_HDR_SZ   2
 
#define WEBSOCKET_GUID   "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 GUID used to compute the accept key, defined in the specifications.
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
int AST_OPTIONAL_API_NAME() ast_websocket_add_protocol (const char *name, ast_websocket_callback callback)
 
int AST_OPTIONAL_API_NAME() ast_websocket_add_protocol2 (struct ast_websocket_protocol *protocol)
 
const char *AST_OPTIONAL_API_NAME() ast_websocket_client_accept_protocol (struct ast_websocket *ws)
 
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)
 
struct ast_websocket *AST_OPTIONAL_API_NAME() ast_websocket_client_create_with_options (struct ast_websocket_client_options *options, enum ast_websocket_result *result)
 
int AST_OPTIONAL_API_NAME() ast_websocket_close (struct ast_websocket *session, uint16_t reason)
 Close function for websocket session.
 
int AST_OPTIONAL_API_NAME() ast_websocket_fd (struct ast_websocket *session)
 
int AST_OPTIONAL_API_NAME() ast_websocket_is_secure (struct ast_websocket *session)
 
struct ast_sockaddr *AST_OPTIONAL_API_NAME() ast_websocket_local_address (struct ast_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)
 
int AST_OPTIONAL_API_NAME() ast_websocket_read_string (struct ast_websocket *ws, char **buf)
 
void AST_OPTIONAL_API_NAME() ast_websocket_reconstruct_disable (struct ast_websocket *session)
 
void AST_OPTIONAL_API_NAME() ast_websocket_reconstruct_enable (struct ast_websocket *session, size_t bytes)
 
void AST_OPTIONAL_API_NAME() ast_websocket_ref (struct ast_websocket *session)
 
struct ast_sockaddr *AST_OPTIONAL_API_NAME() ast_websocket_remote_address (struct ast_websocket *session)
 
int AST_OPTIONAL_API_NAME() ast_websocket_remove_protocol (const char *name, ast_websocket_callback callback)
 
int AST_OPTIONAL_API_NAME() ast_websocket_server_add_protocol (struct ast_websocket_server *server, const char *name, ast_websocket_callback callback)
 
int AST_OPTIONAL_API_NAME() ast_websocket_server_add_protocol2 (struct ast_websocket_server *server, struct ast_websocket_protocol *protocol)
 
struct ast_websocket_server *AST_OPTIONAL_API_NAME() ast_websocket_server_create (void)
 
int AST_OPTIONAL_API_NAME() ast_websocket_server_remove_protocol (struct ast_websocket_server *server, const char *name, ast_websocket_callback callback)
 
const char *AST_OPTIONAL_API_NAME() ast_websocket_session_id (struct ast_websocket *session)
 
int AST_OPTIONAL_API_NAME() ast_websocket_set_nonblock (struct ast_websocket *session)
 
int AST_OPTIONAL_API_NAME() ast_websocket_set_timeout (struct ast_websocket *session, int timeout)
 
struct ast_websocket_protocol *AST_OPTIONAL_API_NAME() ast_websocket_sub_protocol_alloc (const char *name)
 
void AST_OPTIONAL_API_NAME() ast_websocket_unref (struct ast_websocket *session)
 
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)
 
int AST_OPTIONAL_API_NAME() ast_websocket_wait_for_input (struct ast_websocket *session, int timeout)
 
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.
 
int AST_OPTIONAL_API_NAME() ast_websocket_write_string (struct ast_websocket *ws, const char *buf)
 
static int load_module (void)
 
static struct ast_websocket_protocolone_protocol (struct ast_websocket_server *server)
 If the server has exactly one configured protocol, return it.
 
static int protocol_cmp_fn (void *obj, void *arg, int flags)
 Comparison function for protocols.
 
static void protocol_destroy_fn (void *obj)
 Destructor function for protocols.
 
static int protocol_hash_fn (const void *obj, const int flags)
 Hashing function for protocols.
 
static void session_destroy_fn (void *obj)
 Destructor function for sessions.
 
static int unload_module (void)
 
static int websocket_add_protocol_internal (const char *name, ast_websocket_callback callback)
 
static void websocket_bad_request (struct ast_tcptls_session_instance *ser)
 
static struct ast_tcptls_session_argswebsocket_client_args_create (const char *host, struct ast_tls_config *tls_cfg, enum ast_websocket_result *result)
 
static void websocket_client_args_destroy (void *obj)
 
static enum ast_websocket_result websocket_client_connect (struct ast_websocket *ws, int timeout)
 
static struct ast_websocketwebsocket_client_create (struct ast_websocket_client_options *options, enum ast_websocket_result *result)
 
static char * websocket_client_create_key (void)
 
static void websocket_client_destroy (void *obj)
 
static enum ast_websocket_result websocket_client_handle_response_code (struct websocket_client *client, int response_code)
 
static enum ast_websocket_result websocket_client_handshake (struct websocket_client *client)
 
static enum ast_websocket_result websocket_client_handshake_get_response (struct websocket_client *client)
 
static int websocket_client_parse_uri (const char *uri, char **host, struct ast_str **path)
 Parse the given uri into a path and remote address. More...
 
static char * websocket_combine_key (const char *key, char *res, int res_size)
 
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 void websocket_mask_payload (struct ast_websocket *session, char *frame, char *payload, uint64_t payload_size)
 Perform payload masking for client sessions.
 
static const char * websocket_opcode2str (enum ast_websocket_opcode opcode)
 
static int websocket_remove_protocol_internal (const char *name, ast_websocket_callback callback)
 
static struct ast_websocket_serverwebsocket_server_create_impl (void)
 
static void websocket_server_dtor (void *obj)
 
static struct ast_websocket_serverwebsocket_server_internal_create (void)
 
static int ws_safe_read (struct ast_websocket *session, char *buf, size_t len, enum ast_websocket_opcode *opcode)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "HTTP WebSocket Support" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, .requires = "http", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const char * opcode_map []
 
static struct ast_http_uri websocketuri
 

Detailed Description

WebSocket support for the Asterisk internal HTTP server.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file res_http_websocket.c.

Function Documentation

static int websocket_client_parse_uri ( const char *  uri,
char **  host,
struct ast_str **  path 
)
static

Parse the given uri into a path and remote address.

Expected uri form:

[ws[s]]://<host>[:port][/<path>] 

The returned host will contain the address and optional port while path will contain everything after the address/port if included.

Definition at line 1094 of file res_http_websocket.c.

References ao2_ref, ast_str_append(), ast_str_create, and ast_str_set().

1095 {
1096  struct ast_uri *parsed_uri = ast_uri_parse_websocket(uri);
1097 
1098  if (!parsed_uri) {
1099  return -1;
1100  }
1101 
1102  *host = ast_uri_make_host_with_port(parsed_uri);
1103 
1104  if (ast_uri_path(parsed_uri) || ast_uri_query(parsed_uri)) {
1105  *path = ast_str_create(64);
1106  if (!*path) {
1107  ao2_ref(parsed_uri, -1);
1108  return -1;
1109  }
1110 
1111  if (ast_uri_path(parsed_uri)) {
1112  ast_str_set(path, 0, "%s", ast_uri_path(parsed_uri));
1113  }
1114 
1115  if (ast_uri_query(parsed_uri)) {
1116  ast_str_append(path, 0, "?%s", ast_uri_query(parsed_uri));
1117  }
1118  }
1119 
1120  ao2_ref(parsed_uri, -1);
1121  return 0;
1122 }
Stores parsed uri information.
Definition: uri.c:30
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
char uri[0]
Definition: uri.c:44
char * host
Definition: uri.c:36
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659