| 1 | /* |
| 2 | * Asterisk -- An open source telephony toolkit. |
| 3 | * |
| 4 | * Copyright (C) 2013, Digium, Inc. |
| 5 | * |
| 6 | * David M. Lee, II <[email protected]> |
| 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 | #ifndef ARI_WEBSOCKETS_H_ |
| 20 | #define ARI_WEBSOCKETS_H_ |
| 21 | |
| 22 | /*! \file |
| 23 | * |
| 24 | * \brief Internal API's for websockets. |
| 25 | * \author David M. Lee, II <[email protected]> |
| 26 | */ |
| 27 | |
| 28 | #include "asterisk/http.h" |
| 29 | #include "asterisk/json.h" |
| 30 | #include "asterisk/vector.h" |
| 31 | #include "asterisk/websocket_client.h" |
| 32 | |
| 33 | struct ast_ari_events_event_websocket_args; |
| 34 | |
| 35 | /* Forward-declare websocket structs. This avoids including http_websocket.h, |
| 36 | * which causes optional_api stuff to happen, which makes optional_api more |
| 37 | * difficult to debug. */ |
| 38 | |
| 39 | struct ast_websocket; |
| 40 | |
| 41 | /* |
| 42 | * Since we create a "stasis-<appname>" dialplan context for each |
| 43 | * stasis app, we need to make sure that the total length will be |
| 44 | * <= AST_MAX_CONTEXT |
| 45 | */ |
| 46 | #define STASIS_CONTEXT_PREFIX "stasis-" |
| 47 | #define STASIS_CONTEXT_PREFIX_LEN (sizeof(STASIS_CONTEXT_PREFIX) - 1) |
| 48 | #define ARI_MAX_APP_NAME_LEN (AST_MAX_CONTEXT - STASIS_CONTEXT_PREFIX_LEN) |
| 49 | |
| 50 | struct ari_ws_session { |
| 51 | enum ast_websocket_type type; /*!< The type of websocket session. */ |
| 52 | struct ast_websocket *ast_ws_session; /*!< The parent websocket session. */ |
| 53 | int (*validator)(struct ast_json *); /*!< The message validator. */ |
| 54 | struct ast_vector_string websocket_apps; /*!< List of Stasis apps registered to |
| 55 | the websocket session. */ |
| 56 | int subscribe_all; /*!< Flag indicating if all events are subscribed to. */ |
| 57 | AST_VECTOR(, struct ast_json *) message_queue; /*!< Container for holding delayed messages. */ |
| 58 | char *app_name; /*!< The name of the Stasis application. */ |
| 59 | char *remote_addr; /*!< The remote address. */ |
| 60 | struct ari_conf_outbound_websocket *owc; /*!< The outbound websocket configuration. */ |
| 61 | pthread_t thread; /*!< The thread that handles the websocket. */ |
| 62 | char *channel_id; /*!< The channel id for per-call websocket. */ |
| 63 | char *channel_name; /*!< The channel name for per-call websocket. */ |
| 64 | int stasis_end_sent; /*!< Flag indicating if the StasisEnd message was sent. */ |
| 65 | int connected; /*!< Flag indicating if the websocket is connected. */ |
| 66 | int closing; /*!< Flag indicating if the session is closing. */ |
| 67 | char session_id[]; /*!< The id for the websocket session. */ |
| 68 | }; |
| 69 | |
| 70 | struct ao2_container* ari_websocket_get_sessions(void); |
| 71 | struct ari_ws_session *ari_websocket_get_session(const char *session_id); |
| 72 | struct ari_ws_session *ari_websocket_get_session_by_app(const char *app_name); |
| 73 | const char *ari_websocket_type_to_str(enum ast_websocket_type type); |
| 74 | void ari_websocket_shutdown(struct ari_ws_session *session); |
| 75 | void ari_websocket_shutdown_all(void); |
| 76 | int ari_outbound_websocket_start(struct ari_conf_outbound_websocket *owc); |
| 77 | |
| 78 | /*! |
| 79 | * \internal |
| 80 | * \brief Send a JSON event to a websocket. |
| 81 | * |
| 82 | * \param ari_ws_session ARI websocket session |
| 83 | * \param app_name Application name |
| 84 | * \param message JSON message |
| 85 | * \param debug_app Debug flag for application |
| 86 | */ |
| 87 | void ari_websocket_send_event(struct ari_ws_session *ari_ws_session, |
| 88 | const char *app_name, struct ast_json *message, int debug_app); |
| 89 | |
| 90 | /*! |
| 91 | * \internal |
| 92 | * \brief Process an ARI REST over Websocket request |
| 93 | * |
| 94 | * \param ari_ws_session ARI websocket session |
| 95 | * \param remote_addr Remote address for log messages |
| 96 | * \param upgrade_headers HTTP headers from the upgrade request |
| 97 | * \param app_name Application name |
| 98 | * \param msg JSON Request message |
| 99 | * \retval 0 on success, -1 on failure |
| 100 | */ |
| 101 | int ari_websocket_process_request(struct ari_ws_session *ast_ws_session, |
| 102 | const char *remote_addr, struct ast_variable *get_params, |
| 103 | struct ast_variable *upgrade_headers, |
| 104 | const char *app_name, struct ast_json *msg); |
| 105 | |
| 106 | /*! |
| 107 | * \brief Wrapper for invoking the websocket code for an incoming connection. |
| 108 | * |
| 109 | * \param ws_server WebSocket server to invoke. |
| 110 | * \param ser HTTP session. |
| 111 | * \param uri Requested URI. |
| 112 | * \param method Requested HTTP method. |
| 113 | * \param get_params Parsed query parameters. |
| 114 | * \param headers Parsed HTTP headers. |
| 115 | */ |
| 116 | void ari_handle_websocket(struct ast_tcptls_session_instance *ser, |
| 117 | const char *uri, enum ast_http_method method, |
| 118 | struct ast_variable *get_params, |
| 119 | struct ast_variable *headers); |
| 120 | |
| 121 | int ari_websocket_unload_module(void); |
| 122 | int ari_websocket_load_module(int is_enabled); |
| 123 | |
| 124 | #endif /* ARI_WEBSOCKETS_H_ */ |