ari_websockets.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Digium, Inc.
00005  *
00006  * David M. Lee, II <dlee@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 #include "asterisk.h"
00020 
00021 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 431695 $")
00022 
00023 #include "asterisk/ari.h"
00024 #include "asterisk/astobj2.h"
00025 #include "asterisk/http_websocket.h"
00026 #include "internal.h"
00027 
00028 /*! \file
00029  *
00030  * \brief WebSocket support for RESTful API's.
00031  * \author David M. Lee, II <dlee@digium.com>
00032  */
00033 
00034 struct ast_ari_websocket_session {
00035    struct ast_websocket *ws_session;
00036    int (*validator)(struct ast_json *);
00037 };
00038 
00039 static void websocket_session_dtor(void *obj)
00040 {
00041    struct ast_ari_websocket_session *session = obj;
00042 
00043    ast_websocket_unref(session->ws_session);
00044    session->ws_session = NULL;
00045 }
00046 
00047 /*!
00048  * \brief Validator that always succeeds.
00049  */
00050 static int null_validator(struct ast_json *json)
00051 {
00052    return 1;
00053 }
00054 
00055 struct ast_ari_websocket_session *ast_ari_websocket_session_create(
00056    struct ast_websocket *ws_session, int (*validator)(struct ast_json *))
00057 {
00058    RAII_VAR(struct ast_ari_websocket_session *, session, NULL, ao2_cleanup);
00059    RAII_VAR(struct ast_ari_conf *, config, ast_ari_config_get(), ao2_cleanup);
00060 
00061    if (ws_session == NULL) {
00062       return NULL;
00063    }
00064 
00065    if (config == NULL || config->general == NULL) {
00066       return NULL;
00067    }
00068 
00069    if (validator == NULL) {
00070       validator = null_validator;
00071    }
00072 
00073    if (ast_websocket_set_nonblock(ws_session) != 0) {
00074       ast_log(LOG_ERROR,
00075          "ARI web socket failed to set nonblock; closing: %s\n",
00076          strerror(errno));
00077       return NULL;
00078    }
00079 
00080    if (ast_websocket_set_timeout(ws_session, config->general->write_timeout)) {
00081       ast_log(LOG_WARNING, "Failed to set write timeout %d on ARI web socket\n",
00082          config->general->write_timeout);
00083    }
00084 
00085    session = ao2_alloc(sizeof(*session), websocket_session_dtor);
00086    if (!session) {
00087       return NULL;
00088    }
00089 
00090    ao2_ref(ws_session, +1);
00091    session->ws_session = ws_session;
00092    session->validator = validator;
00093 
00094    ao2_ref(session, +1);
00095    return session;
00096 }
00097 
00098 struct ast_json *ast_ari_websocket_session_read(
00099    struct ast_ari_websocket_session *session)
00100 {
00101    RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
00102 
00103    if (ast_websocket_fd(session->ws_session) < 0) {
00104       return NULL;
00105    }
00106 
00107    while (!message) {
00108       int res;
00109       char *payload;
00110       uint64_t payload_len;
00111       enum ast_websocket_opcode opcode;
00112       int fragmented;
00113 
00114       res = ast_wait_for_input(
00115          ast_websocket_fd(session->ws_session), -1);
00116 
00117       if (res <= 0) {
00118          ast_log(LOG_WARNING, "WebSocket poll error: %s\n",
00119             strerror(errno));
00120          return NULL;
00121       }
00122 
00123       res = ast_websocket_read(session->ws_session, &payload,
00124          &payload_len, &opcode, &fragmented);
00125 
00126       if (res != 0) {
00127          ast_log(LOG_WARNING, "WebSocket read error: %s\n",
00128             strerror(errno));
00129          return NULL;
00130       }
00131 
00132       switch (opcode) {
00133       case AST_WEBSOCKET_OPCODE_CLOSE:
00134          ast_debug(1, "WebSocket closed\n");
00135          return NULL;
00136       case AST_WEBSOCKET_OPCODE_TEXT:
00137          message = ast_json_load_buf(payload, payload_len, NULL);
00138          if (message == NULL) {
00139             ast_log(LOG_WARNING,
00140                "WebSocket input failed to parse\n");
00141          }
00142          break;
00143       default:
00144          /* Ignore all other message types */
00145          break;
00146       }
00147    }
00148 
00149    return ast_json_ref(message);
00150 }
00151 
00152 #define VALIDATION_FAILED           \
00153    "{"                  \
00154    "  \"error\": \"InvalidMessage\","     \
00155    "  \"message\": \"Message validation failed\""  \
00156    "}" 
00157 
00158 int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session,
00159    struct ast_json *message)
00160 {
00161    RAII_VAR(char *, str, NULL, ast_json_free);
00162 
00163 #ifdef AST_DEVMODE
00164    if (!session->validator(message)) {
00165       ast_log(LOG_ERROR, "Outgoing message failed validation\n");
00166       return ast_websocket_write(session->ws_session,
00167          AST_WEBSOCKET_OPCODE_TEXT, VALIDATION_FAILED,
00168          strlen(VALIDATION_FAILED));
00169    }
00170 #endif
00171 
00172    str = ast_json_dump_string_format(message, ast_ari_json_format());
00173 
00174    if (str == NULL) {
00175       ast_log(LOG_ERROR, "Failed to encode JSON object\n");
00176       return -1;
00177    }
00178 
00179    ast_debug(3, "Examining ARI event: \n%s\n", str);
00180    if (ast_websocket_write(session->ws_session,
00181             AST_WEBSOCKET_OPCODE_TEXT, str,  strlen(str))) {
00182       ast_log(LOG_NOTICE, "Problem occurred during websocket write, websocket closed\n");
00183       return -1;
00184    }
00185    return 0;
00186 }
00187 
00188 void ari_handle_websocket(struct ast_websocket_server *ws_server,
00189    struct ast_tcptls_session_instance *ser, const char *uri,
00190    enum ast_http_method method, struct ast_variable *get_params,
00191    struct ast_variable *headers)
00192 {
00193    struct ast_http_uri fake_urih = {
00194       .data = ws_server,
00195    };
00196    ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
00197       headers);
00198 }

Generated on Thu Apr 16 06:27:14 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6