res_http_websocket.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. /*! \file
  19. *
  20. * \brief WebSocket support for the Asterisk internal HTTP server
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/module.h"
  30. #include "asterisk/http.h"
  31. #include "asterisk/astobj2.h"
  32. #include "asterisk/strings.h"
  33. #include "asterisk/file.h"
  34. #include "asterisk/unaligned.h"
  35. #define AST_API_MODULE
  36. #include "asterisk/http_websocket.h"
  37. /*! \brief GUID used to compute the accept key, defined in the specifications */
  38. #define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  39. /*! \brief Number of buckets for registered protocols */
  40. #define MAX_PROTOCOL_BUCKETS 7
  41. /*! \brief Size of the pre-determined buffer for WebSocket frames */
  42. #define MAXIMUM_FRAME_SIZE 8192
  43. /*! \brief Default reconstruction size for multi-frame payload reconstruction. If exceeded the next frame will start a
  44. * payload.
  45. */
  46. #define DEFAULT_RECONSTRUCTION_CEILING 16384
  47. /*! \brief Maximum reconstruction size for multi-frame payload reconstruction. */
  48. #define MAXIMUM_RECONSTRUCTION_CEILING 16384
  49. /*! \brief Maximum size of a websocket frame header
  50. * 1 byte flags and opcode
  51. * 1 byte mask flag + payload len
  52. * 8 bytes max extended length
  53. * 4 bytes optional masking key
  54. * ... payload follows ...
  55. * */
  56. #define MAX_WS_HDR_SZ 14
  57. #define MIN_WS_HDR_SZ 2
  58. /*! \brief Structure definition for session */
  59. struct ast_websocket {
  60. FILE *f; /*!< Pointer to the file instance used for writing and reading */
  61. int fd; /*!< File descriptor for the session, only used for polling */
  62. struct ast_sockaddr address; /*!< Address of the remote client */
  63. enum ast_websocket_opcode opcode; /*!< Cached opcode for multi-frame messages */
  64. size_t payload_len; /*!< Length of the payload */
  65. char *payload; /*!< Pointer to the payload */
  66. size_t reconstruct; /*!< Number of bytes before a reconstructed payload will be returned and a new one started */
  67. int timeout; /*!< The timeout for operations on the socket */
  68. unsigned int secure:1; /*!< Bit to indicate that the transport is secure */
  69. unsigned int closing:1; /*!< Bit to indicate that the session is in the process of being closed */
  70. unsigned int close_sent:1; /*!< Bit to indicate that the session close opcode has been sent and no further data will be sent */
  71. };
  72. /*! \brief Structure definition for protocols */
  73. struct websocket_protocol {
  74. char *name; /*!< Name of the protocol */
  75. ast_websocket_callback callback; /*!< Callback called when a new session is established */
  76. };
  77. /*! \brief Hashing function for protocols */
  78. static int protocol_hash_fn(const void *obj, const int flags)
  79. {
  80. const struct websocket_protocol *protocol = obj;
  81. const char *name = obj;
  82. return ast_str_case_hash(flags & OBJ_KEY ? name : protocol->name);
  83. }
  84. /*! \brief Comparison function for protocols */
  85. static int protocol_cmp_fn(void *obj, void *arg, int flags)
  86. {
  87. const struct websocket_protocol *protocol1 = obj, *protocol2 = arg;
  88. const char *protocol = arg;
  89. return !strcasecmp(protocol1->name, flags & OBJ_KEY ? protocol : protocol2->name) ? CMP_MATCH | CMP_STOP : 0;
  90. }
  91. /*! \brief Destructor function for protocols */
  92. static void protocol_destroy_fn(void *obj)
  93. {
  94. struct websocket_protocol *protocol = obj;
  95. ast_free(protocol->name);
  96. }
  97. /*! \brief Structure for a WebSocket server */
  98. struct ast_websocket_server {
  99. struct ao2_container *protocols; /*!< Container for registered protocols */
  100. };
  101. static void websocket_server_internal_dtor(void *obj)
  102. {
  103. struct ast_websocket_server *server = obj;
  104. ao2_cleanup(server->protocols);
  105. server->protocols = NULL;
  106. }
  107. static void websocket_server_dtor(void *obj)
  108. {
  109. websocket_server_internal_dtor(obj);
  110. ast_module_unref(ast_module_info->self);
  111. }
  112. static struct ast_websocket_server *websocket_server_create_impl(void (*dtor)(void *))
  113. {
  114. RAII_VAR(struct ast_websocket_server *, server, NULL, ao2_cleanup);
  115. server = ao2_alloc(sizeof(*server), dtor);
  116. if (!server) {
  117. return NULL;
  118. }
  119. server->protocols = ao2_container_alloc(MAX_PROTOCOL_BUCKETS, protocol_hash_fn, protocol_cmp_fn);
  120. if (!server->protocols) {
  121. return NULL;
  122. }
  123. ao2_ref(server, +1);
  124. return server;
  125. }
  126. static struct ast_websocket_server *websocket_server_internal_create(void)
  127. {
  128. return websocket_server_create_impl(websocket_server_internal_dtor);
  129. }
  130. struct ast_websocket_server *AST_OPTIONAL_API_NAME(ast_websocket_server_create)(void)
  131. {
  132. ast_module_ref(ast_module_info->self);
  133. return websocket_server_create_impl(websocket_server_dtor);
  134. }
  135. /*! \brief Destructor function for sessions */
  136. static void session_destroy_fn(void *obj)
  137. {
  138. struct ast_websocket *session = obj;
  139. ast_websocket_close(session, 0);
  140. if (session->f) {
  141. fclose(session->f);
  142. ast_verb(2, "WebSocket connection from '%s' closed\n", ast_sockaddr_stringify(&session->address));
  143. }
  144. ast_free(session->payload);
  145. }
  146. int AST_OPTIONAL_API_NAME(ast_websocket_server_add_protocol)(struct ast_websocket_server *server, const char *name, ast_websocket_callback callback)
  147. {
  148. struct websocket_protocol *protocol;
  149. if (!server->protocols) {
  150. return -1;
  151. }
  152. ao2_lock(server->protocols);
  153. /* Ensure a second protocol handler is not registered for the same protocol */
  154. if ((protocol = ao2_find(server->protocols, name, OBJ_KEY | OBJ_NOLOCK))) {
  155. ao2_ref(protocol, -1);
  156. ao2_unlock(server->protocols);
  157. return -1;
  158. }
  159. if (!(protocol = ao2_alloc(sizeof(*protocol), protocol_destroy_fn))) {
  160. ao2_unlock(server->protocols);
  161. return -1;
  162. }
  163. if (!(protocol->name = ast_strdup(name))) {
  164. ao2_ref(protocol, -1);
  165. ao2_unlock(server->protocols);
  166. return -1;
  167. }
  168. protocol->callback = callback;
  169. ao2_link_flags(server->protocols, protocol, OBJ_NOLOCK);
  170. ao2_unlock(server->protocols);
  171. ao2_ref(protocol, -1);
  172. ast_verb(2, "WebSocket registered sub-protocol '%s'\n", name);
  173. return 0;
  174. }
  175. int AST_OPTIONAL_API_NAME(ast_websocket_server_remove_protocol)(struct ast_websocket_server *server, const char *name, ast_websocket_callback callback)
  176. {
  177. struct websocket_protocol *protocol;
  178. if (!(protocol = ao2_find(server->protocols, name, OBJ_KEY))) {
  179. return -1;
  180. }
  181. if (protocol->callback != callback) {
  182. ao2_ref(protocol, -1);
  183. return -1;
  184. }
  185. ao2_unlink(server->protocols, protocol);
  186. ao2_ref(protocol, -1);
  187. ast_verb(2, "WebSocket unregistered sub-protocol '%s'\n", name);
  188. return 0;
  189. }
  190. /*! \brief Close function for websocket session */
  191. int AST_OPTIONAL_API_NAME(ast_websocket_close)(struct ast_websocket *session, uint16_t reason)
  192. {
  193. char frame[4] = { 0, }; /* The header is 2 bytes and the reason code takes up another 2 bytes */
  194. int res;
  195. if (session->close_sent) {
  196. return 0;
  197. }
  198. frame[0] = AST_WEBSOCKET_OPCODE_CLOSE | 0x80;
  199. frame[1] = 2; /* The reason code is always 2 bytes */
  200. /* If no reason has been specified assume 1000 which is normal closure */
  201. put_unaligned_uint16(&frame[2], htons(reason ? reason : 1000));
  202. session->closing = 1;
  203. session->close_sent = 1;
  204. ao2_lock(session);
  205. res = ast_careful_fwrite(session->f, session->fd, frame, 4, session->timeout);
  206. ao2_unlock(session);
  207. return res;
  208. }
  209. /*! \brief Write function for websocket traffic */
  210. int AST_OPTIONAL_API_NAME(ast_websocket_write)(struct ast_websocket *session, enum ast_websocket_opcode opcode, char *payload, uint64_t actual_length)
  211. {
  212. size_t header_size = 2; /* The minimum size of a websocket frame is 2 bytes */
  213. char *frame;
  214. uint64_t length = 0;
  215. if (actual_length < 126) {
  216. length = actual_length;
  217. } else if (actual_length < (1 << 16)) {
  218. length = 126;
  219. /* We need an additional 2 bytes to store the extended length */
  220. header_size += 2;
  221. } else {
  222. length = 127;
  223. /* We need an additional 8 bytes to store the really really extended length */
  224. header_size += 8;
  225. }
  226. frame = ast_alloca(header_size);
  227. memset(frame, 0, sizeof(*frame));
  228. frame[0] = opcode | 0x80;
  229. frame[1] = length;
  230. /* Use the additional available bytes to store the length */
  231. if (length == 126) {
  232. put_unaligned_uint16(&frame[2], htons(actual_length));
  233. } else if (length == 127) {
  234. put_unaligned_uint64(&frame[2], htonl(actual_length));
  235. }
  236. ao2_lock(session);
  237. if (session->closing) {
  238. ao2_unlock(session);
  239. return -1;
  240. }
  241. if (ast_careful_fwrite(session->f, session->fd, frame, header_size, session->timeout)) {
  242. ao2_unlock(session);
  243. return -1;
  244. }
  245. if (ast_careful_fwrite(session->f, session->fd, payload, actual_length, session->timeout)) {
  246. ao2_unlock(session);
  247. return -1;
  248. }
  249. fflush(session->f);
  250. ao2_unlock(session);
  251. return 0;
  252. }
  253. void AST_OPTIONAL_API_NAME(ast_websocket_reconstruct_enable)(struct ast_websocket *session, size_t bytes)
  254. {
  255. session->reconstruct = MIN(bytes, MAXIMUM_RECONSTRUCTION_CEILING);
  256. }
  257. void AST_OPTIONAL_API_NAME(ast_websocket_reconstruct_disable)(struct ast_websocket *session)
  258. {
  259. session->reconstruct = 0;
  260. }
  261. void AST_OPTIONAL_API_NAME(ast_websocket_ref)(struct ast_websocket *session)
  262. {
  263. ao2_ref(session, +1);
  264. }
  265. void AST_OPTIONAL_API_NAME(ast_websocket_unref)(struct ast_websocket *session)
  266. {
  267. ao2_cleanup(session);
  268. }
  269. int AST_OPTIONAL_API_NAME(ast_websocket_fd)(struct ast_websocket *session)
  270. {
  271. return session->closing ? -1 : session->fd;
  272. }
  273. struct ast_sockaddr * AST_OPTIONAL_API_NAME(ast_websocket_remote_address)(struct ast_websocket *session)
  274. {
  275. return &session->address;
  276. }
  277. int AST_OPTIONAL_API_NAME(ast_websocket_is_secure)(struct ast_websocket *session)
  278. {
  279. return session->secure;
  280. }
  281. int AST_OPTIONAL_API_NAME(ast_websocket_set_nonblock)(struct ast_websocket *session)
  282. {
  283. int flags;
  284. if ((flags = fcntl(session->fd, F_GETFL)) == -1) {
  285. return -1;
  286. }
  287. flags |= O_NONBLOCK;
  288. if ((flags = fcntl(session->fd, F_SETFL, flags)) == -1) {
  289. return -1;
  290. }
  291. return 0;
  292. }
  293. int AST_OPTIONAL_API_NAME(ast_websocket_set_timeout)(struct ast_websocket *session, int timeout)
  294. {
  295. session->timeout = timeout;
  296. return 0;
  297. }
  298. /* MAINTENANCE WARNING on ast_websocket_read()!
  299. *
  300. * We have to keep in mind during this function that the fact that session->fd seems ready
  301. * (via poll) does not necessarily mean we have application data ready, because in the case
  302. * of an SSL socket, there is some encryption data overhead that needs to be read from the
  303. * TCP socket, so poll() may say there are bytes to be read, but whether it is just 1 byte
  304. * or N bytes we do not know that, and we do not know how many of those bytes (if any) are
  305. * for application data (for us) and not just for the SSL protocol consumption
  306. *
  307. * There used to be a couple of nasty bugs here that were fixed in last refactoring but I
  308. * want to document them so the constraints are clear and we do not re-introduce them:
  309. *
  310. * - This function would incorrectly assume that fread() would necessarily return more than
  311. * 1 byte of data, just because a websocket frame is always >= 2 bytes, but the thing
  312. * is we're dealing with a TCP bitstream here, we could read just one byte and that's normal.
  313. * The problem before was that if just one byte was read, the function bailed out and returned
  314. * an error, effectively dropping the first byte of a websocket frame header!
  315. *
  316. * - Another subtle bug was that it would just read up to MAX_WS_HDR_SZ (14 bytes) via fread()
  317. * then assume that executing poll() would tell you if there is more to read, but since
  318. * we're dealing with a buffered stream (session->f is a FILE*), poll would say there is
  319. * nothing else to read (in the real tcp socket session->fd) and we would get stuck here
  320. * without processing the rest of the data in session->f internal buffers until another packet
  321. * came on the network to unblock us!
  322. *
  323. * Note during the header parsing stage we try to read in small chunks just what we need, this
  324. * is buffered data anyways, no expensive syscall required most of the time ...
  325. */
  326. static inline int ws_safe_read(struct ast_websocket *session, char *buf, int len, enum ast_websocket_opcode *opcode)
  327. {
  328. int sanity;
  329. size_t rlen;
  330. int xlen = len;
  331. char *rbuf = buf;
  332. for (sanity = 10; sanity; sanity--) {
  333. clearerr(session->f);
  334. rlen = fread(rbuf, 1, xlen, session->f);
  335. if (!rlen && ferror(session->f) && errno != EAGAIN) {
  336. ast_log(LOG_ERROR, "Error reading from web socket: %s\n", strerror(errno));
  337. *opcode = AST_WEBSOCKET_OPCODE_CLOSE;
  338. session->closing = 1;
  339. return -1;
  340. }
  341. xlen = xlen - rlen;
  342. rbuf = rbuf + rlen;
  343. if (!xlen) {
  344. break;
  345. }
  346. if (ast_wait_for_input(session->fd, 1000) < 0) {
  347. ast_log(LOG_ERROR, "ast_wait_for_input returned err: %s\n", strerror(errno));
  348. *opcode = AST_WEBSOCKET_OPCODE_CLOSE;
  349. session->closing = 1;
  350. return -1;
  351. }
  352. }
  353. if (!sanity) {
  354. ast_log(LOG_WARNING, "Websocket seems unresponsive, disconnecting ...\n");
  355. *opcode = AST_WEBSOCKET_OPCODE_CLOSE;
  356. session->closing = 1;
  357. return -1;
  358. }
  359. return 0;
  360. }
  361. int AST_OPTIONAL_API_NAME(ast_websocket_read)(struct ast_websocket *session, char **payload, uint64_t *payload_len, enum ast_websocket_opcode *opcode, int *fragmented)
  362. {
  363. char buf[MAXIMUM_FRAME_SIZE] = "";
  364. int fin = 0;
  365. int mask_present = 0;
  366. char *mask = NULL, *new_payload = NULL;
  367. size_t options_len = 0, frame_size = 0;
  368. *payload = NULL;
  369. *payload_len = 0;
  370. *fragmented = 0;
  371. if (ws_safe_read(session, &buf[0], MIN_WS_HDR_SZ, opcode)) {
  372. return 0;
  373. }
  374. frame_size += MIN_WS_HDR_SZ;
  375. /* ok, now we have the first 2 bytes, so we know some flags, opcode and payload length (or whether payload length extension will be required) */
  376. *opcode = buf[0] & 0xf;
  377. *payload_len = buf[1] & 0x7f;
  378. if (*opcode == AST_WEBSOCKET_OPCODE_TEXT || *opcode == AST_WEBSOCKET_OPCODE_BINARY || *opcode == AST_WEBSOCKET_OPCODE_CONTINUATION ||
  379. *opcode == AST_WEBSOCKET_OPCODE_PING || *opcode == AST_WEBSOCKET_OPCODE_PONG) {
  380. fin = (buf[0] >> 7) & 1;
  381. mask_present = (buf[1] >> 7) & 1;
  382. /* Based on the mask flag and payload length, determine how much more we need to read before start parsing the rest of the header */
  383. options_len += mask_present ? 4 : 0;
  384. options_len += (*payload_len == 126) ? 2 : (*payload_len == 127) ? 8 : 0;
  385. if (options_len) {
  386. /* read the rest of the header options */
  387. if (ws_safe_read(session, &buf[frame_size], options_len, opcode)) {
  388. return 0;
  389. }
  390. frame_size += options_len;
  391. }
  392. if (*payload_len == 126) {
  393. /* Grab the 2-byte payload length */
  394. *payload_len = ntohs(get_unaligned_uint16(&buf[2]));
  395. mask = &buf[4];
  396. } else if (*payload_len == 127) {
  397. /* Grab the 8-byte payload length */
  398. *payload_len = ntohl(get_unaligned_uint64(&buf[2]));
  399. mask = &buf[10];
  400. } else {
  401. /* Just set the mask after the small 2-byte header */
  402. mask = &buf[2];
  403. }
  404. /* Now read the rest of the payload */
  405. *payload = &buf[frame_size]; /* payload will start here, at the end of the options, if any */
  406. frame_size = frame_size + (*payload_len); /* final frame size is header + optional headers + payload data */
  407. if (frame_size > MAXIMUM_FRAME_SIZE) {
  408. ast_log(LOG_WARNING, "Cannot fit huge websocket frame of %zu bytes\n", frame_size);
  409. /* The frame won't fit :-( */
  410. ast_websocket_close(session, 1009);
  411. return -1;
  412. }
  413. if (ws_safe_read(session, *payload, *payload_len, opcode)) {
  414. return 0;
  415. }
  416. /* If a mask is present unmask the payload */
  417. if (mask_present) {
  418. unsigned int pos;
  419. for (pos = 0; pos < *payload_len; pos++) {
  420. (*payload)[pos] ^= mask[pos % 4];
  421. }
  422. }
  423. /* Per the RFC for PING we need to send back an opcode with the application data as received */
  424. if ((*opcode == AST_WEBSOCKET_OPCODE_PING) && (ast_websocket_write(session, AST_WEBSOCKET_OPCODE_PONG, *payload, *payload_len))) {
  425. *payload_len = 0;
  426. ast_websocket_close(session, 1009);
  427. return 0;
  428. }
  429. if (*payload_len) {
  430. if (!(new_payload = ast_realloc(session->payload, (session->payload_len + *payload_len)))) {
  431. ast_log(LOG_WARNING, "Failed allocation: %p, %zu, %"PRIu64"\n",
  432. session->payload, session->payload_len, *payload_len);
  433. *payload_len = 0;
  434. ast_websocket_close(session, 1009);
  435. return 0;
  436. }
  437. session->payload = new_payload;
  438. memcpy((session->payload + session->payload_len), (*payload), (*payload_len));
  439. session->payload_len += *payload_len;
  440. } else if (!session->payload_len && session->payload) {
  441. ast_free(session->payload);
  442. session->payload = NULL;
  443. }
  444. if (!fin && session->reconstruct && (session->payload_len < session->reconstruct)) {
  445. /* If this is not a final message we need to defer returning it until later */
  446. if (*opcode != AST_WEBSOCKET_OPCODE_CONTINUATION) {
  447. session->opcode = *opcode;
  448. }
  449. *opcode = AST_WEBSOCKET_OPCODE_CONTINUATION;
  450. *payload_len = 0;
  451. *payload = NULL;
  452. } else {
  453. if (*opcode == AST_WEBSOCKET_OPCODE_CONTINUATION) {
  454. if (!fin) {
  455. /* If this was not actually the final message tell the user it is fragmented so they can deal with it accordingly */
  456. *fragmented = 1;
  457. } else {
  458. /* Final frame in multi-frame so push up the actual opcode */
  459. *opcode = session->opcode;
  460. }
  461. }
  462. *payload_len = session->payload_len;
  463. *payload = session->payload;
  464. session->payload_len = 0;
  465. }
  466. } else if (*opcode == AST_WEBSOCKET_OPCODE_CLOSE) {
  467. /* Make the payload available so the user can look at the reason code if they so desire */
  468. if ((*payload_len) && (new_payload = ast_realloc(session->payload, *payload_len))) {
  469. if (ws_safe_read(session, &buf[frame_size], (*payload_len), opcode)) {
  470. return 0;
  471. }
  472. session->payload = new_payload;
  473. memcpy(session->payload, &buf[frame_size], *payload_len);
  474. *payload = session->payload;
  475. frame_size += (*payload_len);
  476. }
  477. session->closing = 1;
  478. } else {
  479. ast_log(LOG_WARNING, "WebSocket unknown opcode %u\n", *opcode);
  480. /* We received an opcode that we don't understand, the RFC states that 1003 is for a type of data that can't be accepted... opcodes
  481. * fit that, I think. */
  482. ast_websocket_close(session, 1003);
  483. }
  484. return 0;
  485. }
  486. /*!
  487. * \brief If the server has exactly one configured protocol, return it.
  488. */
  489. static struct websocket_protocol *one_protocol(
  490. struct ast_websocket_server *server)
  491. {
  492. SCOPED_AO2LOCK(lock, server->protocols);
  493. if (ao2_container_count(server->protocols) != 1) {
  494. return NULL;
  495. }
  496. return ao2_callback(server->protocols, OBJ_NOLOCK, NULL, NULL);
  497. }
  498. static void websocket_bad_request(struct ast_tcptls_session_instance *ser)
  499. {
  500. struct ast_str *http_header = ast_str_create(64);
  501. if (!http_header) {
  502. ast_http_request_close_on_completion(ser);
  503. ast_http_error(ser, 500, "Server Error", "Out of memory");
  504. return;
  505. }
  506. ast_str_set(&http_header, 0, "Sec-WebSocket-Version: 7, 8, 13\r\n");
  507. ast_http_send(ser, AST_HTTP_UNKNOWN, 400, "Bad Request", http_header, NULL, 0, 0);
  508. }
  509. int AST_OPTIONAL_API_NAME(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)
  510. {
  511. struct ast_variable *v;
  512. char *upgrade = NULL, *key = NULL, *key1 = NULL, *key2 = NULL, *protos = NULL, *requested_protocols = NULL, *protocol = NULL;
  513. int version = 0, flags = 1;
  514. struct websocket_protocol *protocol_handler = NULL;
  515. struct ast_websocket *session;
  516. struct ast_websocket_server *server;
  517. SCOPED_MODULE_USE(ast_module_info->self);
  518. /* Upgrade requests are only permitted on GET methods */
  519. if (method != AST_HTTP_GET) {
  520. ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
  521. return 0;
  522. }
  523. server = urih->data;
  524. /* Get the minimum headers required to satisfy our needs */
  525. for (v = headers; v; v = v->next) {
  526. if (!strcasecmp(v->name, "Upgrade")) {
  527. upgrade = ast_strip(ast_strdupa(v->value));
  528. } else if (!strcasecmp(v->name, "Sec-WebSocket-Key")) {
  529. key = ast_strip(ast_strdupa(v->value));
  530. } else if (!strcasecmp(v->name, "Sec-WebSocket-Key1")) {
  531. key1 = ast_strip(ast_strdupa(v->value));
  532. } else if (!strcasecmp(v->name, "Sec-WebSocket-Key2")) {
  533. key2 = ast_strip(ast_strdupa(v->value));
  534. } else if (!strcasecmp(v->name, "Sec-WebSocket-Protocol")) {
  535. requested_protocols = ast_strip(ast_strdupa(v->value));
  536. protos = ast_strdupa(requested_protocols);
  537. } else if (!strcasecmp(v->name, "Sec-WebSocket-Version")) {
  538. if (sscanf(v->value, "%30d", &version) != 1) {
  539. version = 0;
  540. }
  541. }
  542. }
  543. /* If this is not a websocket upgrade abort */
  544. if (!upgrade || strcasecmp(upgrade, "websocket")) {
  545. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - did not request WebSocket\n",
  546. ast_sockaddr_stringify(&ser->remote_address));
  547. ast_http_error(ser, 426, "Upgrade Required", NULL);
  548. return 0;
  549. } else if (ast_strlen_zero(requested_protocols)) {
  550. /* If there's only a single protocol registered, and the
  551. * client doesn't specify what protocol it's using, go ahead
  552. * and accept the connection */
  553. protocol_handler = one_protocol(server);
  554. if (!protocol_handler) {
  555. /* Multiple registered subprotocols; client must specify */
  556. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols requested\n",
  557. ast_sockaddr_stringify(&ser->remote_address));
  558. websocket_bad_request(ser);
  559. return 0;
  560. }
  561. } else if (key1 && key2) {
  562. /* Specification defined in http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 and
  563. * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00 -- not currently supported*/
  564. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - unsupported version '00/76' chosen\n",
  565. ast_sockaddr_stringify(&ser->remote_address));
  566. websocket_bad_request(ser);
  567. return 0;
  568. }
  569. /* Iterate through the requested protocols trying to find one that we have a handler for */
  570. while (!protocol_handler && (protocol = strsep(&requested_protocols, ","))) {
  571. protocol_handler = ao2_find(server->protocols, ast_strip(protocol), OBJ_KEY);
  572. }
  573. /* If no protocol handler exists bump this back to the requester */
  574. if (!protocol_handler) {
  575. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols out of '%s' supported\n",
  576. ast_sockaddr_stringify(&ser->remote_address), protos);
  577. websocket_bad_request(ser);
  578. return 0;
  579. }
  580. /* Determine how to respond depending on the version */
  581. if (version == 7 || version == 8 || version == 13) {
  582. /* Version 7 defined in specification http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07 */
  583. /* Version 8 defined in specification http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10 */
  584. /* Version 13 defined in specification http://tools.ietf.org/html/rfc6455 */
  585. char *combined, base64[64];
  586. unsigned combined_length;
  587. uint8_t sha[20];
  588. combined_length = (key ? strlen(key) : 0) + strlen(WEBSOCKET_GUID) + 1;
  589. if (!key || combined_length > 8192) { /* no stack overflows please */
  590. websocket_bad_request(ser);
  591. ao2_ref(protocol_handler, -1);
  592. return 0;
  593. }
  594. if (ast_http_body_discard(ser)) {
  595. websocket_bad_request(ser);
  596. ao2_ref(protocol_handler, -1);
  597. return 0;
  598. }
  599. if (!(session = ao2_alloc(sizeof(*session), session_destroy_fn))) {
  600. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted\n",
  601. ast_sockaddr_stringify(&ser->remote_address));
  602. websocket_bad_request(ser);
  603. ao2_ref(protocol_handler, -1);
  604. return 0;
  605. }
  606. session->timeout = AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT;
  607. combined = ast_alloca(combined_length);
  608. snprintf(combined, combined_length, "%s%s", key, WEBSOCKET_GUID);
  609. ast_sha1_hash_uint(sha, combined);
  610. ast_base64encode(base64, (const unsigned char*)sha, 20, sizeof(base64));
  611. fprintf(ser->f, "HTTP/1.1 101 Switching Protocols\r\n"
  612. "Upgrade: %s\r\n"
  613. "Connection: Upgrade\r\n"
  614. "Sec-WebSocket-Accept: %s\r\n",
  615. upgrade,
  616. base64);
  617. /* RFC 6455, Section 4.1:
  618. *
  619. * 6. If the response includes a |Sec-WebSocket-Protocol| header
  620. * field and this header field indicates the use of a
  621. * subprotocol that was not present in the client's handshake
  622. * (the server has indicated a subprotocol not requested by
  623. * the client), the client MUST _Fail the WebSocket
  624. * Connection_.
  625. */
  626. if (protocol) {
  627. fprintf(ser->f, "Sec-WebSocket-Protocol: %s\r\n",
  628. protocol);
  629. }
  630. fprintf(ser->f, "\r\n");
  631. fflush(ser->f);
  632. } else {
  633. /* Specification defined in http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75 or completely unknown */
  634. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - unsupported version '%d' chosen\n",
  635. ast_sockaddr_stringify(&ser->remote_address), version ? version : 75);
  636. websocket_bad_request(ser);
  637. ao2_ref(protocol_handler, -1);
  638. return 0;
  639. }
  640. /* Enable keepalive on all sessions so the underlying user does not have to */
  641. if (setsockopt(ser->fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags))) {
  642. ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - failed to enable keepalive\n",
  643. ast_sockaddr_stringify(&ser->remote_address));
  644. websocket_bad_request(ser);
  645. ao2_ref(session, -1);
  646. ao2_ref(protocol_handler, -1);
  647. return 0;
  648. }
  649. ast_verb(2, "WebSocket connection from '%s' for protocol '%s' accepted using version '%d'\n", ast_sockaddr_stringify(&ser->remote_address), protocol ? : "", version);
  650. /* Populate the session with all the needed details */
  651. session->f = ser->f;
  652. session->fd = ser->fd;
  653. ast_sockaddr_copy(&session->address, &ser->remote_address);
  654. session->opcode = -1;
  655. session->reconstruct = DEFAULT_RECONSTRUCTION_CEILING;
  656. session->secure = ser->ssl ? 1 : 0;
  657. /* Give up ownership of the socket and pass it to the protocol handler */
  658. ast_tcptls_stream_set_exclusive_input(ser->stream_cookie, 0);
  659. protocol_handler->callback(session, get_vars, headers);
  660. ao2_ref(protocol_handler, -1);
  661. /*
  662. * By dropping the FILE* and fd from the session the connection
  663. * won't get closed when the HTTP server cleans up because we
  664. * passed the connection to the protocol handler.
  665. */
  666. ser->f = NULL;
  667. ser->fd = -1;
  668. return 0;
  669. }
  670. static struct ast_http_uri websocketuri = {
  671. .callback = AST_OPTIONAL_API_NAME(ast_websocket_uri_cb),
  672. .description = "Asterisk HTTP WebSocket",
  673. .uri = "ws",
  674. .has_subtree = 0,
  675. .data = NULL,
  676. .key = __FILE__,
  677. };
  678. /*! \brief Simple echo implementation which echoes received text and binary frames */
  679. static void websocket_echo_callback(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers)
  680. {
  681. int flags, res;
  682. ast_debug(1, "Entering WebSocket echo loop\n");
  683. if ((flags = fcntl(ast_websocket_fd(session), F_GETFL)) == -1) {
  684. goto end;
  685. }
  686. flags |= O_NONBLOCK;
  687. if (fcntl(ast_websocket_fd(session), F_SETFL, flags) == -1) {
  688. goto end;
  689. }
  690. while ((res = ast_wait_for_input(ast_websocket_fd(session), -1)) > 0) {
  691. char *payload;
  692. uint64_t payload_len;
  693. enum ast_websocket_opcode opcode;
  694. int fragmented;
  695. if (ast_websocket_read(session, &payload, &payload_len, &opcode, &fragmented)) {
  696. /* We err on the side of caution and terminate the session if any error occurs */
  697. ast_log(LOG_WARNING, "Read failure during WebSocket echo loop\n");
  698. break;
  699. }
  700. if (opcode == AST_WEBSOCKET_OPCODE_TEXT || opcode == AST_WEBSOCKET_OPCODE_BINARY) {
  701. ast_websocket_write(session, opcode, payload, payload_len);
  702. } else if (opcode == AST_WEBSOCKET_OPCODE_CLOSE) {
  703. break;
  704. } else {
  705. ast_debug(1, "Ignored WebSocket opcode %u\n", opcode);
  706. }
  707. }
  708. end:
  709. ast_debug(1, "Exiting WebSocket echo loop\n");
  710. ast_websocket_unref(session);
  711. }
  712. static int websocket_add_protocol_internal(const char *name, ast_websocket_callback callback)
  713. {
  714. struct ast_websocket_server *ws_server = websocketuri.data;
  715. if (!ws_server) {
  716. return -1;
  717. }
  718. return ast_websocket_server_add_protocol(ws_server, name, callback);
  719. }
  720. int AST_OPTIONAL_API_NAME(ast_websocket_add_protocol)(const char *name, ast_websocket_callback callback)
  721. {
  722. int res = websocket_add_protocol_internal(name, callback);
  723. if (res == 0) {
  724. ast_module_ref(ast_module_info->self);
  725. }
  726. return res;
  727. }
  728. static int websocket_remove_protocol_internal(const char *name, ast_websocket_callback callback)
  729. {
  730. struct ast_websocket_server *ws_server = websocketuri.data;
  731. if (!ws_server) {
  732. return -1;
  733. }
  734. return ast_websocket_server_remove_protocol(ws_server, name, callback);
  735. }
  736. int AST_OPTIONAL_API_NAME(ast_websocket_remove_protocol)(const char *name, ast_websocket_callback callback)
  737. {
  738. int res = websocket_remove_protocol_internal(name, callback);
  739. if (res == 0) {
  740. ast_module_unref(ast_module_info->self);
  741. }
  742. return res;
  743. }
  744. static int load_module(void)
  745. {
  746. websocketuri.data = websocket_server_internal_create();
  747. if (!websocketuri.data) {
  748. return AST_MODULE_LOAD_FAILURE;
  749. }
  750. ast_http_uri_link(&websocketuri);
  751. websocket_add_protocol_internal("echo", websocket_echo_callback);
  752. return 0;
  753. }
  754. static int unload_module(void)
  755. {
  756. websocket_remove_protocol_internal("echo", websocket_echo_callback);
  757. ast_http_uri_unlink(&websocketuri);
  758. ao2_ref(websocketuri.data, -1);
  759. websocketuri.data = NULL;
  760. return 0;
  761. }
  762. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HTTP WebSocket Support",
  763. .load = load_module,
  764. .unload = unload_module,
  765. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  766. );