Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
ari_websockets.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <dlee@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19#include "asterisk.h"
20
21#include "resource_events.h"
22#include "ari_websockets.h"
23#include "internal.h"
24#if defined(AST_DEVMODE)
26#endif
27#include "asterisk/app.h"
28#include "asterisk/ari.h"
29#include "asterisk/astobj2.h"
31#include "asterisk/module.h"
32#include "asterisk/stasis_app.h"
33
34
35/*! \file
36 *
37 * \brief WebSocket support for RESTful API's.
38 * \author David M. Lee, II <dlee@digium.com>
39 */
40
41/*! Number of buckets for the event session registry. Remember to keep it a prime number! */
42#define ARI_WS_SESSION_NUM_BUCKETS 23
43
44/*! Number of buckets for a websocket apps container. Remember to keep it a prime number! */
45#define APPS_NUM_BUCKETS 7
46
47/*! Initial size of a message queue. */
48#define MESSAGES_INIT_SIZE 23
49
50
51/*! \brief Local registry for created \ref event_session objects. */
53
55
56#define MAX_VALS 128
57
58/*!
59 * \brief Validator that always succeeds.
60 */
61static int null_validator(struct ast_json *json)
62{
63 return 1;
64}
65
66#define VALIDATION_FAILED \
67 "{" \
68 " \"error\": \"InvalidMessage\"," \
69 " \"message\": \"Message validation failed\"" \
70 "}"
71
74 struct ast_json *message)
75{
76 RAII_VAR(char *, str, NULL, ast_json_free);
77
78#ifdef AST_DEVMODE
80 ast_log(LOG_ERROR, "Outgoing message failed validation\n");
82 }
83#endif
84
86
87 if (str == NULL) {
88 ast_log(LOG_ERROR, "Failed to encode JSON object\n");
89 return -1;
90 }
91
93 ast_log(LOG_NOTICE, "Problem occurred during websocket write to %s, websocket closed\n",
95 return -1;
96 }
97 return 0;
98}
99
100/*!
101 * \internal
102 * \brief Updates the websocket session.
103 *
104 * \details If the value of the \c ws_session is not \c NULL and there are messages in the
105 * event session's \c message_queue, the messages are dispatched and removed from
106 * the queue.
107 *
108 * \param ari_ws_session The ARI websocket session
109 * \param ast_ws_session The Asterisk websocket session
110 */
113 struct ast_websocket *ast_ws_session)
114{
116 int i;
117
118 if (ast_ws_session == NULL) {
119 return -1;
120 }
121
122 if (config == NULL || config->general == NULL) {
123 return -1;
124 }
125
126 if (ast_websocket_set_nonblock(ast_ws_session) != 0) {
128 "ARI web socket failed to set nonblock; closing: %s\n",
129 strerror(errno));
130 return -1;
131 }
132
133 if (ast_websocket_set_timeout(ast_ws_session, config->general->write_timeout)) {
134 ast_log(LOG_WARNING, "Failed to set write timeout %d on ARI web socket\n",
135 config->general->write_timeout);
136 }
137
138 ao2_ref(ast_ws_session, +1);
139 ari_ws_session->ast_ws_session = ast_ws_session;
141 for (i = 0; i < AST_VECTOR_SIZE(&ari_ws_session->message_queue); i++) {
144 ast_json_unref(msg);
145 }
146
149
150 return 0;
151}
152
155{
157
159 return NULL;
160 }
161
162 while (!message) {
163 int res;
164 char *payload;
165 uint64_t payload_len;
166 enum ast_websocket_opcode opcode;
167 int fragmented;
168
169 res = ast_wait_for_input(
171
172 if (res <= 0) {
173 ast_log(LOG_WARNING, "WebSocket poll error: %s\n",
174 strerror(errno));
175 return NULL;
176 }
177
179 &payload_len, &opcode, &fragmented);
180
181 if (res != 0) {
182 ast_log(LOG_WARNING, "WebSocket read error: %s\n",
183 strerror(errno));
184 return NULL;
185 }
186
187 switch (opcode) {
189 ast_debug(1, "WebSocket closed\n");
190 return NULL;
192 message = ast_json_load_buf(payload, payload_len, NULL);
193 if (message == NULL) {
194 struct ast_json *error = ast_json_pack(
195 "{s:s, s:s, s:s, s:i, s:s, s:s }",
196 "type", "RESTResponse",
197 "transaction_id", "",
198 "request_id", "",
199 "status_code", 400,
200 "reason_phrase", "Failed to parse request message JSON",
201 "uri", ""
202 );
204 error, 0);
207 "WebSocket input failed to parse\n");
208
209 }
210
211 break;
212 default:
213 /* Ignore all other message types */
214 break;
215 }
216 }
217
218 return ast_json_ref(message);
219}
220
222 struct ast_tcptls_session_instance *ser, const char *uri,
223 enum ast_http_method method, struct ast_variable *get_params,
224 struct ast_variable *headers)
225{
226 struct ast_http_uri fake_urih = {
228 };
229
230 ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
231 headers);
232}
233
234/*!
235 * \brief Callback handler for Stasis application messages.
236 *
237 * \internal
238 *
239 * \param data Void pointer to the event session (\ref event_session).
240 * \param app_name Name of the Stasis application that dispatched the message.
241 * \param message The dispatched message.
242 * \param debug_app Debug flag for the application.
243 */
245 const char *app_name, struct ast_json *message, int debug_app)
246{
247 char *remote_addr = ast_sockaddr_stringify(
249 const char *msg_type, *msg_application, *msg_timestamp, *msg_ast_id;
250 SCOPE_ENTER(4, "%s: Dispatching message from Stasis app '%s'\n", remote_addr, app_name);
251
253
255
256 msg_type = S_OR(ast_json_string_get(ast_json_object_get(message, "type")), "");
257 msg_application = S_OR(
258 ast_json_string_get(ast_json_object_get(message, "application")), "");
259
260 /* If we've been replaced, remove the application from our local
261 websocket_apps container */
262 if (strcmp(msg_type, "ApplicationReplaced") == 0 &&
263 strcmp(msg_application, app_name) == 0) {
264 ao2_find(ari_ws_session->websocket_apps, msg_application,
266 }
267
274 "%s: Failed to dispatch '%s' message from Stasis app '%s'; could not update message\n",
275 remote_addr, msg_type, msg_application);
276 }
277 }
278
279 msg_ast_id = S_OR(
280 ast_json_string_get(ast_json_object_get(message, "asterisk_id")), "");
281 if (ast_strlen_zero(msg_ast_id)) {
282 char eid[20];
283
284 if (ast_json_object_set(message, "asterisk_id",
288 "%s: Failed to dispatch '%s' message from Stasis app '%s'; could not update message\n",
289 remote_addr, msg_type, msg_application);
290 }
291 }
292
293 /* Now, we need to determine our state to see how we will handle the message */
297 "%s: Failed to dispatch '%s' message from Stasis app '%s'; could not update message\n",
298 remote_addr, msg_type, msg_application);
299 }
300
301 if (!ari_ws_session) {
302 /* If the websocket is NULL, the message goes to the queue */
305 }
307 "%s: Queued '%s' message for Stasis app '%s'; websocket is not ready\n",
308 remote_addr,
309 msg_type,
310 msg_application);
312
313 if (TRACE_ATLEAST(4) || debug_app) {
315
316 ast_verbose("<--- Sending ARI event to %s --->\n%s\n",
317 remote_addr,
318 str);
320 }
321
323 }
324
326 SCOPE_EXIT("%s: Dispatched '%s' message from Stasis app '%s'\n",
327 remote_addr, msg_type, app_name);
328}
329
330static void stasis_app_message_handler(void *data, const char *app_name,
331 struct ast_json *message)
332{
333 int debug_app = stasis_app_get_debug_by_name(app_name);
334 struct ari_ws_session *ari_ws_session = data;
337}
338
339static int parse_app_args(struct ast_variable *get_params,
340 struct ast_ari_response * response,
342{
343 struct ast_variable *i;
344 RAII_VAR(char *, app_parse, NULL, ast_free);
345
346 for (i = get_params; i; i = i->next) {
347 if (strcmp(i->name, "app") == 0) {
348 /* Parse comma separated list */
349 char *vals[MAX_VALS];
350 size_t j;
351
352 app_parse = ast_strdup(i->value);
353 if (!app_parse) {
355 return -1;
356 }
357
358 if (strlen(app_parse) == 0) {
359 /* ast_app_separate_args can't handle "" */
360 args->app_count = 1;
361 vals[0] = app_parse;
362 } else {
363 args->app_count = ast_app_separate_args(
364 app_parse, ',', vals,
365 ARRAY_LEN(vals));
366 }
367
368 if (args->app_count == 0) {
370 return -1;
371 }
372
373 if (args->app_count >= MAX_VALS) {
374 ast_ari_response_error(response, 400,
375 "Bad Request",
376 "Too many values for app");
377 return -1;
378 }
379
380 args->app = ast_malloc(sizeof(*args->app) * args->app_count);
381 if (!args->app) {
383 return -1;
384 }
385
386 for (j = 0; j < args->app_count; ++j) {
387 args->app[j] = (vals[j]);
388 }
389 } else if (strcmp(i->name, "subscribeAll") == 0) {
390 args->subscribe_all = ast_true(i->value);
391 }
392 }
393
394 args->app_parse = app_parse;
395 app_parse = NULL;
396
397 return 0;
398}
399
400/*
401 * Websocket session cleanup is a bit complicated because it can be
402 * in different states, it may or may not be in the registry container,
403 * and stasis may be sending asynchronous events to it and some
404 * stages of cleanup need to lock it.
405 *
406 * That's why there are 3 different cleanup functions.
407 */
408
409/*!
410 * \internal
411 * \brief Reset the ari_ws_session without destroying it.
412 * It can't be reused and will be cleaned up by the caller.
413 */
415{
416 struct ao2_iterator i;
417 char *app;
418 int j;
420
421 /* Clean up the websocket_apps container */
424 while ((app = ao2_iterator_next(&i))) {
427 }
431 }
432
433 /* Clean up the message_queue container */
434 for (j = 0; j < AST_VECTOR_SIZE(&ari_ws_session->message_queue); j++) {
436 ast_json_unref(msg);
437 }
439}
440
441/*!
442 * \internal
443 * \brief RAII_VAR and container ari_ws_session cleanup function.
444 * This unlinks the ari_ws_session from the registry and cleans up the
445 * decrements the reference count.
446 */
448{
449 if (!ari_ws_session) {
450 return;
451 }
452
456 }
458}
459
460/*!
461 * \internal
462 * \brief The ao2 destructor.
463 * This cleans up the reference to the parent ast_websocket.
464 */
465static void ari_ws_session_dtor(void *obj)
466{
467 struct ari_ws_session *ari_ws_session = obj;
468
471 return;
472 }
475}
476
478 int (*validator)(struct ast_json *),
479 struct ast_tcptls_session_instance *ser,
481 const char *session_id)
482{
484 int (* register_handler)(const char *, stasis_app_cb handler, void *data);
485 size_t size, i;
486
487 if (validator == NULL) {
489 }
490
491 size = sizeof(*ari_ws_session) + strlen(session_id) + 1;
492
494 if (!ari_ws_session) {
495 return -1;
496 }
497
498 ari_ws_session->app_name = ast_strdup(args->app_parse);
499 if (!ari_ws_session->app_name) {
500 ast_http_error(ser, 500, "Internal Server Error",
501 "Allocation failed");
502 return -1;
503 }
504
505 strcpy(ari_ws_session->session_id, session_id); /* Safe */
506
507 /* Instantiate the hash table for Stasis apps */
511 ast_http_error(ser, 500, "Internal Server Error",
512 "Allocation failed");
513 return -1;
514 }
515
516 /* Instantiate the message queue */
518 ast_http_error(ser, 500, "Internal Server Error",
519 "Allocation failed");
521 return -1;
522 }
523
524 /* Register the apps with Stasis */
525 if (args->subscribe_all) {
526 register_handler = &stasis_app_register_all;
527 } else {
528 register_handler = &stasis_app_register;
529 }
530
531 for (i = 0; i < args->app_count; ++i) {
532 const char *app = args->app[i];
533
534 if (ast_strlen_zero(app)) {
535 ast_http_error(ser, 400, "Bad Request",
536 "Invalid application provided in param [app].");
538 return -1;
539 }
540
542 ast_http_error(ser, 500, "Internal Server Error",
543 "Allocation failed");
545 return -1;
546 }
547
548 if (register_handler(app, stasis_app_message_handler, ari_ws_session)) {
549 ast_log(LOG_WARNING, "Stasis registration failed for application: '%s'\n", app);
550 ast_http_error(ser, 500, "Internal Server Error",
551 "Stasis registration failed");
553 return -1;
554 }
555 }
556
558
559 /*
560 * Add the event session to the session registry.
561 * When this functions returns, the registry will have
562 * the only reference to the session.
563 */
565 ast_http_error(ser, 500, "Internal Server Error",
566 "Allocation failed");
568 return -1;
569 }
570
571 return 0;
572}
573
574/*!
575 * \internal
576 * \brief This function gets called before the upgrade process is completed.
577 * HTTP is still in effect.
578 */
580 struct ast_variable *get_params, struct ast_variable *headers,
581 const char *session_id)
582{
584 int res = 0;
585 RAII_VAR(struct ast_ari_response *, response, NULL, ast_free);
586 char *remote_addr = ast_sockaddr_stringify(&ser->remote_address);
587
588 response = ast_calloc(1, sizeof(*response));
589 if (!response) {
590 ast_log(LOG_ERROR, "Failed to create response.\n");
591 ast_http_error(ser, 500, "Server Error", "Memory allocation error");
592 return -1;
593 }
594
595 res = parse_app_args(get_params, response, &args);
596 if (res != 0) {
597 /* Param parsing failure */
598 RAII_VAR(char *, msg, NULL, ast_json_free);
599 if (response->message) {
600 msg = ast_json_dump_string(response->message);
601 } else {
602 ast_log(LOG_ERROR, "Missing response message\n");
603 }
604
605 if (msg) {
606 ast_http_error(ser, response->response_code, response->response_text, msg);
607 return -1;
608 }
609 }
610
611 if (args.app_count == 0) {
612 ast_http_error(ser, 400, "Bad Request",
613 "HTTP request is missing param: [app]");
614 return -1;
615 }
616
617#if defined(AST_DEVMODE)
619 ser, &args, session_id);
620#else
621 res = ari_ws_session_create(NULL, ser, &args, session_id);
622#endif
623 if (res != 0) {
625 "%s: Failed to create ARI ari_session\n", remote_addr);
626 }
627
628 ast_free(args.app_parse);
629 ast_free(args.app);
630 return res;
631}
632
633/*!
634 * \internal
635 * \brief This function gets called after the upgrade process is completed.
636 * The websocket is now in effect.
637 */
638static void websocket_established_cb(struct ast_websocket *ast_ws_session,
639 struct ast_variable *get_params, struct ast_variable *upgrade_headers)
640{
641 RAII_VAR(struct ast_ari_response *, response, NULL, ast_free);
642 /*
643 * ast_ws_session is passed in with it's refcount bumped so
644 * we need to unref it when we're done. The refcount will
645 * be bumped again when we add it to the ari_ws_session.
646 */
647 RAII_VAR(struct ast_websocket *, s, ast_ws_session, ast_websocket_unref);
649 struct ast_json *msg;
650 struct ast_variable *v;
651 char *remote_addr = ast_sockaddr_stringify(
652 ast_websocket_remote_address(ast_ws_session));
653 const char *session_id = ast_websocket_session_id(ast_ws_session);
654
655 SCOPE_ENTER(2, "%s: WebSocket established\n", remote_addr);
656
657 if (TRACE_ATLEAST(2)) {
658 ast_trace(2, "%s: Websocket Upgrade Headers:\n", remote_addr);
659 for (v = upgrade_headers; v; v = v->next) {
660 ast_trace(3, "--> %s: %s\n", v->name, v->value);
661 }
662 }
663
664 response = ast_calloc(1, sizeof(*response));
665 if (!response) {
667 "%s: Failed to create response\n", remote_addr);
668 }
669
670 /* Find the event_session and update its websocket */
672 if (ari_ws_session) {
674 ari_ws_session_update(ari_ws_session, ast_ws_session);
675 } else {
677 "%s: Failed to locate an event session for the websocket session\n",
678 remote_addr);
679 }
680
681 ast_trace(-1, "%s: Waiting for messages\n", remote_addr);
682 while ((msg = ari_ws_session_read(ari_ws_session))) {
684 upgrade_headers, ari_ws_session->app_name, msg);
685 ast_json_unref(msg);
686 }
687
688 SCOPE_EXIT("%s: Websocket closed\n", remote_addr);
689}
690
691static int ari_ws_session_shutdown_cb(void *ari_ws_session, void *arg, int flags)
692{
694
695 return 0;
696}
697
699{
701 return;
702 }
703
706
709}
710
712{
716 return 0;
717}
718
721
723{
724 int res = 0;
725 struct ast_websocket_protocol *protocol;
726
728 ARI_WS_SESSION_NUM_BUCKETS, ari_ws_session_hash_fn,
729 NULL, ari_ws_session_cmp_fn);
732 "Failed to allocate the local registry for websocket applications\n");
734 }
735
737 if (!ast_ws_server) {
740 }
741
742 protocol = ast_websocket_sub_protocol_alloc("ari");
743 if (!protocol) {
748 }
752
754}
755
static const char app[]
Definition: app_adsiprog.c:56
const char * str
Definition: app_jack.c:150
ast_mutex_t lock
Definition: app_sla.c:337
Asterisk RESTful API hooks.
enum ast_json_encoding_format ast_ari_json_format(void)
Configured encoding format for JSON output.
Definition: res_ari.c:1015
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:319
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:358
ari_validator ast_ari_validate_message_fn(void)
Function pointer to ast_ari_validate_message().
Generated file - Build validators for ARI model objects.
int ari_websocket_process_request(struct ari_ws_session *ari_ws_session, const char *remote_addr, struct ast_variable *upgrade_headers, const char *app_name, struct ast_json *request_msg)
AO2_STRING_FIELD_CMP_FN(ari_ws_session, session_id)
static int null_validator(struct ast_json *json)
Validator that always succeeds.
static int ari_ws_session_create(int(*validator)(struct ast_json *), struct ast_tcptls_session_instance *ser, struct ast_ari_events_event_websocket_args *args, const char *session_id)
#define MAX_VALS
static int ari_ws_session_update(struct ari_ws_session *ari_ws_session, struct ast_websocket *ast_ws_session)
static void ari_ws_session_reset(struct ari_ws_session *ari_ws_session)
static void stasis_app_message_handler(void *data, const char *app_name, struct ast_json *message)
static int websocket_attempted_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *headers, const char *session_id)
#define MESSAGES_INIT_SIZE
static int parse_app_args(struct ast_variable *get_params, struct ast_ari_response *response, struct ast_ari_events_event_websocket_args *args)
#define APPS_NUM_BUCKETS
static struct ao2_container * ari_ws_session_registry
Local registry for created event_session objects.
static void ari_ws_session_dtor(void *obj)
void ari_websocket_send_event(struct ari_ws_session *ari_ws_session, const char *app_name, struct ast_json *message, int debug_app)
Callback handler for Stasis application messages.
AO2_STRING_FIELD_HASH_FN(ari_ws_session, session_id)
static void websocket_established_cb(struct ast_websocket *ast_ws_session, struct ast_variable *get_params, struct ast_variable *upgrade_headers)
struct ast_websocket_server * ast_ws_server
static void ari_ws_session_registry_dtor(void)
int ari_websocket_unload_module(void)
void ari_handle_websocket(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.
static int ari_ws_session_shutdown_cb(void *ari_ws_session, void *arg, int flags)
static int ari_ws_session_write(struct ari_ws_session *ari_ws_session, struct ast_json *message)
#define VALIDATION_FAILED
#define ARI_WS_SESSION_NUM_BUCKETS
int ari_websocket_load_module(void)
static void ari_ws_session_cleanup(struct ari_ws_session *ari_ws_session)
static struct ast_json * ari_ws_session_read(struct ari_ws_session *ari_ws_session)
Internal API's for websockets.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#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_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_MULTIPLE
Definition: astobj2.h:1049
@ OBJ_UNLINK
Definition: astobj2.h:1039
@ OBJ_SEARCH_KEY
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
#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.
Definition: astobj2.h:1303
static const char config[]
Definition: chan_ooh323.c:111
void ast_verbose(const char *fmt,...)
Definition: extconf.c:2206
#define TRACE_ATLEAST(level)
#define SCOPE_ENTER(level,...)
#define SCOPE_EXIT(...)
#define SCOPE_EXIT_LOG_RTN(__log_level,...)
#define ast_trace(level,...)
ast_http_method
HTTP Request methods known by Asterisk.
Definition: http.h:58
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.
Definition: http.c:664
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
struct ast_websocket_protocol * ast_websocket_sub_protocol_alloc(const char *name)
Allocate a websocket sub-protocol instance.
int ast_websocket_set_timeout(struct ast_websocket *session, int timeout)
Set the timeout on a non-blocking WebSocket session.
int ast_websocket_set_nonblock(struct ast_websocket *session)
Set the socket of a WebSocket session to be non-blocking.
const char * ast_websocket_session_id(struct ast_websocket *session)
Get the session ID for a WebSocket session.
int 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.
struct ast_sockaddr * ast_websocket_remote_address(struct ast_websocket *session)
Get the remote address for a WebSocket connected session.
ast_websocket_opcode
WebSocket operation codes.
@ AST_WEBSOCKET_OPCODE_CLOSE
@ AST_WEBSOCKET_OPCODE_TEXT
void ast_websocket_unref(struct ast_websocket *session)
Decrease the reference count for a WebSocket session.
int 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_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_websocket_write_string(struct ast_websocket *ws, const char *buf)
Construct and transmit a WebSocket frame containing string data.
int ast_websocket_fd(struct ast_websocket *session)
Get the file descriptor for a WebSocket session.
struct ast_websocket_server * ast_websocket_server_create(void)
Creates a ast_websocket_server.
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define ast_app_separate_args(a, b, c, d)
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
Internal API's for res_ari.
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:810
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:670
@ AST_JSON_PRETTY
Definition: json.h:795
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
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
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
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
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Definition: lock.h:608
int errno
Asterisk module definitions.
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
struct ast_ari_conf * ast_ari_config_get(void)
Get the current ARI configuration.
const char * method
Definition: res_pjsip.c:1279
static struct timeval msg_timestamp(void *msg, enum smdi_message_type type)
Definition: res_smdi.c:366
#define NULL
Definition: resample.c:96
Generated file - declares stubs to be implemented in res/ari/resource_events.c.
Stasis Application API. See Stasis Application API for detailed documentation.
int stasis_app_event_allowed(const char *app_name, struct ast_json *event)
Check if the given event should be filtered.
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
Register a new Stasis application.
Definition: res_stasis.c:1838
void(* stasis_app_cb)(void *data, const char *app_name, struct ast_json *message)
Callback for Stasis application handler.
Definition: stasis_app.h:67
void stasis_app_unregister(const char *app_name)
Unregister a Stasis application and unsubscribe from all event sources.
Definition: res_stasis.c:1848
int stasis_app_register_all(const char *app_name, stasis_app_cb handler, void *data)
Register a new Stasis application that receives all Asterisk events.
Definition: res_stasis.c:1843
int stasis_app_get_debug_by_name(const char *app_name)
Get debug status of an application.
#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
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_container_alloc(buckets)
Allocates a hash container for bare strings.
Definition: strings.h:1365
int ast_str_container_add(struct ao2_container *str_container, const char *add)
Adds a string to a string container allocated by ast_str_container_alloc.
Definition: strings.c:205
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
struct ari_ws_session::@424 message_queue
struct ao2_container * websocket_apps
struct ast_websocket * ast_ws_session
int(* validator)(struct ast_json *)
All configuration options for ARI.
Definition: internal.h:54
Definition of a URI handler.
Definition: http.h:102
const char * uri
Definition: http.h:105
void * data
Definition: http.h:116
Abstract JSON element (object, array, string, int, ...).
describes a server instance
Definition: tcptls.h:151
struct ast_sockaddr remote_address
Definition: tcptls.h:153
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
A websocket protocol implementation.
ast_websocket_callback session_established
Callback called when a new session is established. Mandatory.
ast_websocket_pre_callback session_attempted
Callback called when a new session is attempted. Optional.
Structure for a WebSocket server.
Structure definition for session.
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59
const char * args
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int error(const char *format,...)
Definition: utils/frame.c:999
#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
#define ast_assert(a)
Definition: utils.h:739
int ast_wait_for_input(int fd, int ms)
Definition: utils.c:1698
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: utils.c:2839
#define ARRAY_LEN(a)
Definition: utils.h:666
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#define AST_VECTOR_RESET(vec, cleanup)
Reset vector.
Definition: vector.h:625
#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
Vector element cleanup that does nothing.
Definition: vector.h:571
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680