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

WebSocket support for RESTful API's. More...

#include "asterisk.h"
#include "asterisk/ari.h"
#include "asterisk/astobj2.h"
#include "asterisk/http_websocket.h"
#include "asterisk/stasis_app.h"
#include "internal.h"

Go to the source code of this file.

Data Structures

struct  ast_ari_websocket_session
 

Macros

#define VALIDATION_FAILED
 

Functions

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. More...
 
struct ast_ari_websocket_sessionast_ari_websocket_session_create (struct ast_websocket *ws_session, int(*validator)(struct ast_json *))
 Create an ARI WebSocket session. More...
 
struct ast_sockaddrast_ari_websocket_session_get_remote_addr (struct ast_ari_websocket_session *session)
 Get the remote address from an ARI WebSocket. More...
 
const char * ast_ari_websocket_session_id (const struct ast_ari_websocket_session *session)
 Get the Session ID for an ARI WebSocket. More...
 
struct ast_jsonast_ari_websocket_session_read (struct ast_ari_websocket_session *session)
 Read a message from an ARI WebSocket. More...
 
int ast_ari_websocket_session_write (struct ast_ari_websocket_session *session, struct ast_json *message)
 Send a message to an ARI WebSocket. More...
 
static int null_validator (struct ast_json *json)
 Validator that always succeeds.
 
static void websocket_session_dtor (void *obj)
 

Detailed Description

WebSocket support for RESTful API's.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file ari_websockets.c.

Macro Definition Documentation

#define VALIDATION_FAILED
Value:
"{" \
" \"error\": \"InvalidMessage\"," \
" \"message\": \"Message validation failed\"" \
"}"

Definition at line 152 of file ari_websockets.c.

Function Documentation

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.

Parameters
ws_serverWebSocket server to invoke.
serHTTP session.
uriRequested URI.
methodRequested HTTP method.
get_paramsParsed query parameters.
headersParsed HTTP headers.

Definition at line 191 of file ari_websockets.c.

References ast_http_uri::data.

195 {
196  struct ast_http_uri fake_urih = {
197  .data = ws_server,
198  };
199  ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
200  headers);
201 }
Definition of a URI handler.
Definition: http.h:102
void * data
Definition: http.h:116
struct ast_ari_websocket_session* ast_ari_websocket_session_create ( struct ast_websocket ws_session,
int(*)(struct ast_json *)  validator 
)

Create an ARI WebSocket session.

If NULL is given for the validator function, no validation will be performed.

Parameters
ws_sessionUnderlying WebSocket session.
validatorFunction to validate outgoing messages.
Returns
New ARI WebSocket session.
Return values
NULLon error.

Definition at line 54 of file ari_websockets.c.

References ao2_ref, ast_ari_config_get(), null_validator(), and RAII_VAR.

56 {
57  RAII_VAR(struct ast_ari_websocket_session *, session, NULL, ao2_cleanup);
58  RAII_VAR(struct ast_ari_conf *, config, ast_ari_config_get(), ao2_cleanup);
59 
60  if (ws_session == NULL) {
61  return NULL;
62  }
63 
64  if (config == NULL || config->general == NULL) {
65  return NULL;
66  }
67 
68  if (validator == NULL) {
69  validator = null_validator;
70  }
71 
72  if (ast_websocket_set_nonblock(ws_session) != 0) {
73  ast_log(LOG_ERROR,
74  "ARI web socket failed to set nonblock; closing: %s\n",
75  strerror(errno));
76  return NULL;
77  }
78 
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);
82  }
83 
84  session = ao2_alloc(sizeof(*session), websocket_session_dtor);
85  if (!session) {
86  return NULL;
87  }
88 
89  ao2_ref(ws_session, +1);
90  session->ws_session = ws_session;
91  session->validator = validator;
92 
93  ao2_ref(session, +1);
94  return session;
95 }
struct ast_ari_conf * ast_ari_config_get(void)
Get the current ARI configuration.
All configuration options for ARI.
Definition: internal.h:54
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.
Definition: astobj2.h:459
#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:941
struct ast_sockaddr* ast_ari_websocket_session_get_remote_addr ( struct ast_ari_websocket_session session)

Get the remote address from an ARI WebSocket.

Parameters
sessionSession to write to.
Returns
ast_sockaddr (does not have to be freed)

Definition at line 185 of file ari_websockets.c.

Referenced by ast_ari_websocket_session_write(), and stasis_app_message_handler().

187 {
188  return ast_websocket_remote_address(session->ws_session);
189 }
const char* ast_ari_websocket_session_id ( const struct ast_ari_websocket_session session)

Get the Session ID for an ARI WebSocket.

Parameters
sessionSession to query.
Returns
Session ID.
Return values
NULLon error.

Definition at line 203 of file ari_websockets.c.

Referenced by ast_ari_websocket_events_event_websocket_established().

205 {
206  return ast_websocket_session_id(session->ws_session);
207 }
struct ast_json* ast_ari_websocket_session_read ( struct ast_ari_websocket_session session)

Read a message from an ARI WebSocket.

Parameters
sessionSession to read from.
Returns
Message received.
Return values
NULLif WebSocket could not be read.

Definition at line 97 of file ari_websockets.c.

References ast_debug, ast_json_load_buf(), ast_json_ref(), ast_json_unref(), AST_WEBSOCKET_OPCODE_CLOSE, AST_WEBSOCKET_OPCODE_TEXT, and RAII_VAR.

Referenced by ast_ari_websocket_events_event_websocket_established().

99 {
100  RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
101 
102  if (ast_websocket_fd(session->ws_session) < 0) {
103  return NULL;
104  }
105 
106  while (!message) {
107  int res;
108  char *payload;
109  uint64_t payload_len;
110  enum ast_websocket_opcode opcode;
111  int fragmented;
112 
113  res = ast_wait_for_input(
114  ast_websocket_fd(session->ws_session), -1);
115 
116  if (res <= 0) {
117  ast_log(LOG_WARNING, "WebSocket poll error: %s\n",
118  strerror(errno));
119  return NULL;
120  }
121 
122  res = ast_websocket_read(session->ws_session, &payload,
123  &payload_len, &opcode, &fragmented);
124 
125  if (res != 0) {
126  ast_log(LOG_WARNING, "WebSocket read error: %s\n",
127  strerror(errno));
128  return NULL;
129  }
130 
131  switch (opcode) {
133  ast_debug(1, "WebSocket closed\n");
134  return NULL;
136  message = ast_json_load_buf(payload, payload_len, NULL);
137  if (message == NULL) {
138  ast_log(LOG_WARNING,
139  "WebSocket input failed to parse\n");
140  }
141 
142  break;
143  default:
144  /* Ignore all other message types */
145  break;
146  }
147  }
148 
149  return ast_json_ref(message);
150 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
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.
Definition: json.c:585
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define ast_debug(level,...)
Log a DEBUG message.
Abstract JSON element (object, array, string, int, ...).
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.
Definition: utils.h:941
int ast_ari_websocket_session_write ( struct ast_ari_websocket_session session,
struct ast_json message 
)

Send a message to an ARI WebSocket.

Parameters
sessionSession to write to.
messageMessage to send.
Return values
0on success.
Non-zeroon error.

Definition at line 158 of file ari_websockets.c.

References ast_ari_json_format(), ast_ari_websocket_session_get_remote_addr(), ast_json_dump_string_format(), ast_json_free(), ast_sockaddr_stringify(), and RAII_VAR.

Referenced by event_session_update_websocket(), and stasis_app_message_handler().

160 {
161  RAII_VAR(char *, str, NULL, ast_json_free);
162 
163 #ifdef AST_DEVMODE
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);
167  }
168 #endif
169 
171 
172  if (str == NULL) {
173  ast_log(LOG_ERROR, "Failed to encode JSON object\n");
174  return -1;
175  }
176 
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",
180  return -1;
181  }
182  return 0;
183 }
enum ast_json_encoding_format ast_ari_json_format(void)
Configured encoding format for JSON output.
Definition: res_ari.c:806
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
struct ast_sockaddr * ast_ari_websocket_session_get_remote_addr(struct ast_ari_websocket_session *session)
Get the remote address from an ARI WebSocket.
char * ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format)
Encode a JSON value to a string.
Definition: json.c:484
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
#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:941