38 static void websocket_session_dtor(
void *obj)
42 ast_websocket_unref(session->ws_session);
43 session->ws_session = NULL;
60 if (ws_session == NULL) {
64 if (config == NULL || config->general == NULL) {
68 if (validator == NULL) {
72 if (ast_websocket_set_nonblock(ws_session) != 0) {
74 "ARI web socket failed to set nonblock; closing: %s\n",
79 if (ast_websocket_set_timeout(ws_session, config->general->write_timeout)) {
80 ast_log(LOG_WARNING,
"Failed to set write timeout %d on ARI web socket\n",
81 config->general->write_timeout);
84 session = ao2_alloc(
sizeof(*session), websocket_session_dtor);
90 session->ws_session = ws_session;
91 session->validator = validator;
102 if (ast_websocket_fd(session->ws_session) < 0) {
109 uint64_t payload_len;
113 res = ast_wait_for_input(
114 ast_websocket_fd(session->ws_session), -1);
117 ast_log(LOG_WARNING,
"WebSocket poll error: %s\n",
122 res = ast_websocket_read(session->ws_session, &payload,
123 &payload_len, &opcode, &fragmented);
126 ast_log(LOG_WARNING,
"WebSocket read error: %s\n",
139 "WebSocket input failed to parse\n");
152 #define VALIDATION_FAILED \
154 " \"error\": \"InvalidMessage\"," \
155 " \"message\": \"Message validation failed\"" \
164 if (!session->validator(message)) {
165 ast_log(LOG_ERROR,
"Outgoing message failed validation\n");
166 return ast_websocket_write_string(session->ws_session, VALIDATION_FAILED);
173 ast_log(LOG_ERROR,
"Failed to encode JSON object\n");
177 if (ast_websocket_write_string(session->ws_session, str)) {
178 ast_log(LOG_NOTICE,
"Problem occurred during websocket write to %s, websocket closed\n",
188 return ast_websocket_remote_address(session->ws_session);
199 ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
206 return ast_websocket_session_id(session->ws_session);
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Asterisk main include file. File version handling, generic pbx functions.
enum ast_json_encoding_format ast_ari_json_format(void)
Configured encoding format for JSON output.
struct ast_json * ast_ari_websocket_session_read(struct ast_ari_websocket_session *session)
Read a message from an ARI WebSocket.
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
struct ast_ari_conf * ast_ari_config_get(void)
Get the current ARI configuration.
void ari_handle_websocket(struct ast_websocket_server *ws_server, struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
Wrapper for invoking the websocket code for an incoming connection.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
const char * ast_ari_websocket_session_id(const struct ast_ari_websocket_session *session)
Get the Session ID for an ARI WebSocket.
Structure for a WebSocket server.
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
All configuration options for ARI.
int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session, struct ast_json *message)
Send a message to an ARI WebSocket.
Structure for variables, used for configurations and for channel variables.
struct ast_sockaddr * ast_ari_websocket_session_get_remote_addr(struct ast_ari_websocket_session *session)
Get the remote address from an ARI WebSocket.
Asterisk RESTful API hooks.
Socket address structure.
static int null_validator(struct ast_json *json)
Validator that always succeeds.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ast_debug(level,...)
Log a DEBUG message.
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
char * ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format)
Encode a JSON value to a string.
describes a server instance
Internal API's for res_ari.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Structure definition for session.
struct ast_ari_websocket_session * ast_ari_websocket_session_create(struct ast_websocket *ws_session, int(*validator)(struct ast_json *))
Create an ARI WebSocket session.
Definition of a URI handler.
Abstract JSON element (object, array, string, int, ...).
Stasis Application API. See Stasis Application API for detailed documentation.
ast_http_method
HTTP Request methods known by Asterisk.
ast_websocket_opcode
WebSocket operation codes.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.