ws_frame.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012-2013 Crocodile RCS Ltd
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Exception: permission to copy, modify, propagate, and distribute a work
  23. * formed by combining OpenSSL toolkit software and the code in this file,
  24. * such as linking with software components and libraries released under
  25. * OpenSSL project license.
  26. *
  27. */
  28. #include <limits.h>
  29. #ifdef EMBEDDED_UTF8_DECODE
  30. #include "utf8_decode.h"
  31. #else
  32. #include <unistr.h>
  33. #endif
  34. #include "../../events.h"
  35. #include "../../receive.h"
  36. #include "../../stats.h"
  37. #include "../../str.h"
  38. #include "../../tcp_conn.h"
  39. #include "../../tcp_read.h"
  40. #include "../../tcp_server.h"
  41. #include "../../lib/kcore/kstats_wrapper.h"
  42. #include "../../lib/kmi/tree.h"
  43. #include "../../mem/mem.h"
  44. #include "ws_conn.h"
  45. #include "ws_frame.h"
  46. #include "ws_mod.h"
  47. #include "ws_handshake.h"
  48. #include "config.h"
  49. /* 0 1 2 3
  50. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  51. +-+-+-+-+-------+-+-------------+-------------------------------+
  52. |F|R|R|R| opcode|M| Payload len | Extended payload length |
  53. |I|S|S|S| (4) |A| (7) | (16/64) |
  54. |N|V|V|V| |S| | (if payload len==126/127) |
  55. | |1|2|3| |K| | |
  56. +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  57. | Extended payload length continued, if payload len == 127 |
  58. + - - - - - - - - - - - - - - - +-------------------------------+
  59. | |Masking-key, if MASK set to 1 |
  60. +-------------------------------+-------------------------------+
  61. | Masking-key (continued) | Payload Data |
  62. +-------------------------------- - - - - - - - - - - - - - - - +
  63. : Payload Data continued ... :
  64. + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  65. | Payload Data continued ... |
  66. +---------------------------------------------------------------+ */
  67. typedef struct {
  68. unsigned int fin;
  69. unsigned int rsv1;
  70. unsigned int rsv2;
  71. unsigned int rsv3;
  72. unsigned int opcode;
  73. unsigned int mask;
  74. unsigned int payload_len;
  75. unsigned char masking_key[4];
  76. char *payload_data;
  77. ws_connection_t *wsc;
  78. } ws_frame_t;
  79. typedef enum
  80. {
  81. CONN_CLOSE_DO = 0,
  82. CONN_CLOSE_DONT
  83. } conn_close_t;
  84. #define BYTE0_MASK_FIN (0x80)
  85. #define BYTE0_MASK_RSV1 (0x40)
  86. #define BYTE0_MASK_RSV2 (0x20)
  87. #define BYTE0_MASK_RSV3 (0x10)
  88. #define BYTE0_MASK_OPCODE (0x0F)
  89. #define BYTE1_MASK_MASK (0x80)
  90. #define BYTE1_MASK_PAYLOAD_LEN (0x7F)
  91. #define OPCODE_CONTINUATION (0x0)
  92. #define OPCODE_TEXT_FRAME (0x1)
  93. #define OPCODE_BINARY_FRAME (0x2)
  94. /* 0x3 - 0x7 are reserved for further non-control frames */
  95. #define OPCODE_CLOSE (0x8)
  96. #define OPCODE_PING (0x9)
  97. #define OPCODE_PONG (0xa)
  98. /* 0xb - 0xf are reserved for further control frames */
  99. int ws_keepalive_mechanism = DEFAULT_KEEPALIVE_MECHANISM;
  100. str ws_ping_application_data = STR_NULL;
  101. stat_var *ws_failed_connections;
  102. stat_var *ws_local_closed_connections;
  103. stat_var *ws_received_frames;
  104. stat_var *ws_remote_closed_connections;
  105. stat_var *ws_transmitted_frames;
  106. stat_var *ws_sip_failed_connections;
  107. stat_var *ws_sip_local_closed_connections;
  108. stat_var *ws_sip_received_frames;
  109. stat_var *ws_sip_remote_closed_connections;
  110. stat_var *ws_sip_transmitted_frames;
  111. stat_var *ws_msrp_failed_connections;
  112. stat_var *ws_msrp_local_closed_connections;
  113. stat_var *ws_msrp_received_frames;
  114. stat_var *ws_msrp_remote_closed_connections;
  115. stat_var *ws_msrp_transmitted_frames;
  116. /* WebSocket status text */
  117. static str str_status_normal_closure = str_init("Normal closure");
  118. static str str_status_protocol_error = str_init("Protocol error");
  119. static str str_status_unsupported_opcode = str_init("Unsupported opcode");
  120. static str str_status_message_too_big = str_init("Message too big");
  121. /* MI command status text */
  122. static str str_status_empty_param = str_init("Empty connection ID parameter");
  123. static str str_status_too_many_params = str_init("Too many parameters");
  124. static str str_status_bad_param = str_init("Bad connection ID parameter");
  125. static str str_status_error_closing = str_init("Error closing connection");
  126. static str str_status_error_sending = str_init("Error sending frame");
  127. static str str_status_string_error = str_init("Error converting string to int");
  128. static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
  129. {
  130. int pos = 0, extended_length;
  131. unsigned int frame_length;
  132. char *send_buf;
  133. struct tcp_connection *con;
  134. struct dest_info dst;
  135. union sockaddr_union *from = NULL;
  136. union sockaddr_union local_addr;
  137. int sub_proto;
  138. LM_DBG("encoding WebSocket frame\n");
  139. if (frame->wsc->state != WS_S_OPEN)
  140. {
  141. LM_WARN("sending on closing connection\n");
  142. return -1;
  143. }
  144. wsconn_update(frame->wsc);
  145. /* Validate the first byte */
  146. if (!frame->fin)
  147. {
  148. LM_ERR("WebSocket fragmentation not supported in the sip "
  149. "sub-protocol\n");
  150. return -1;
  151. }
  152. if (frame->rsv1 || frame->rsv2 || frame->rsv3)
  153. {
  154. LM_ERR("WebSocket reserved fields with non-zero values\n");
  155. return -1;
  156. }
  157. sub_proto = frame->wsc->sub_protocol;
  158. switch(frame->opcode)
  159. {
  160. case OPCODE_TEXT_FRAME:
  161. case OPCODE_BINARY_FRAME:
  162. LM_DBG("supported non-control frame: 0x%x\n",
  163. (unsigned char) frame->opcode);
  164. break;
  165. case OPCODE_CLOSE:
  166. case OPCODE_PING:
  167. case OPCODE_PONG:
  168. LM_DBG("supported control frame: 0x%x\n",
  169. (unsigned char) frame->opcode);
  170. break;
  171. default:
  172. LM_ERR("unsupported opcode: 0x%x\n",
  173. (unsigned char) frame->opcode);
  174. return -1;
  175. }
  176. /* validate the second byte */
  177. if (frame->mask)
  178. {
  179. LM_ERR("this is a server - all messages sent will be "
  180. "unmasked\n");
  181. return -1;
  182. }
  183. if (frame->payload_len < 126) extended_length = 0;
  184. else if (frame->payload_len <= USHRT_MAX ) extended_length = 2;
  185. else if (frame->payload_len <= UINT_MAX) extended_length = 4;
  186. else
  187. {
  188. LM_ERR("Kamailio only supports WebSocket frames with payload "
  189. "<= %u\n", UINT_MAX);
  190. return -1;
  191. }
  192. /* Allocate send buffer and build frame */
  193. frame_length = frame->payload_len + extended_length + 2;
  194. if ((send_buf = pkg_malloc(sizeof(unsigned char) * frame_length))
  195. == NULL)
  196. {
  197. LM_ERR("allocating send buffer from pkg memory\n");
  198. return -1;
  199. }
  200. memset(send_buf, 0, sizeof(unsigned char) * frame_length);
  201. send_buf[pos++] = 0x80 | (frame->opcode & 0xff);
  202. if (extended_length == 0)
  203. send_buf[pos++] = (frame->payload_len & 0xff);
  204. else if (extended_length == 2)
  205. {
  206. send_buf[pos++] = 126;
  207. send_buf[pos++] = (frame->payload_len & 0xff00) >> 8;
  208. send_buf[pos++] = (frame->payload_len & 0x00ff) >> 0;
  209. }
  210. else
  211. {
  212. send_buf[pos++] = 127;
  213. send_buf[pos++] = (frame->payload_len & 0xff000000) >> 24;
  214. send_buf[pos++] = (frame->payload_len & 0x00ff0000) >> 16;
  215. send_buf[pos++] = (frame->payload_len & 0x0000ff00) >> 8;
  216. send_buf[pos++] = (frame->payload_len & 0x000000ff) >> 0;
  217. }
  218. memcpy(&send_buf[pos], frame->payload_data, frame->payload_len);
  219. if ((con = tcpconn_get(frame->wsc->id, 0, 0, 0, 0)) == NULL)
  220. {
  221. LM_WARN("TCP/TLS connection get failed\n");
  222. pkg_free(send_buf);
  223. if (wsconn_rm(frame->wsc, WSCONN_EVENTROUTE_YES) < 0)
  224. LM_ERR("removing WebSocket connection\n");
  225. return -1;
  226. }
  227. init_dst_from_rcv(&dst, &con->rcv);
  228. if (conn_close == CONN_CLOSE_DO)
  229. {
  230. dst.send_flags.f |= SND_F_CON_CLOSE;
  231. if (wsconn_rm(frame->wsc, WSCONN_EVENTROUTE_YES) < 0)
  232. {
  233. LM_ERR("removing WebSocket connection\n");
  234. tcpconn_put(con);
  235. pkg_free(send_buf);
  236. return -1;
  237. }
  238. }
  239. if (dst.proto == PROTO_WS)
  240. {
  241. if (unlikely(tcp_disable))
  242. {
  243. STATS_TX_DROPS;
  244. LM_WARN("TCP disabled\n");
  245. pkg_free(send_buf);
  246. tcpconn_put(con);
  247. return -1;
  248. }
  249. }
  250. #ifdef USE_TLS
  251. else if (dst.proto == PROTO_WSS)
  252. {
  253. if (unlikely(tls_disable))
  254. {
  255. STATS_TX_DROPS;
  256. LM_WARN("TLS disabled\n");
  257. pkg_free(send_buf);
  258. tcpconn_put(con);
  259. return -1;
  260. }
  261. }
  262. #endif /* USE_TLS */
  263. if (unlikely((dst.send_flags.f & SND_F_FORCE_SOCKET)
  264. && dst.send_sock))
  265. {
  266. local_addr = dst.send_sock->su;
  267. su_setport(&local_addr, 0);
  268. from = &local_addr;
  269. }
  270. /* Regardless of what has been set before _always_ use existing
  271. connections for WebSockets. This is required because a WebSocket
  272. server (which Kamailio is) CANNOT create connections. */
  273. dst.send_flags.f |= SND_F_FORCE_CON_REUSE;
  274. if (tcp_send(&dst, from, send_buf, frame_length) < 0)
  275. {
  276. STATS_TX_DROPS;
  277. LM_ERR("sending WebSocket frame\n");
  278. pkg_free(send_buf);
  279. update_stat(ws_failed_connections, 1);
  280. if (sub_proto == SUB_PROTOCOL_SIP)
  281. update_stat(ws_sip_failed_connections, 1);
  282. else if (sub_proto == SUB_PROTOCOL_MSRP)
  283. update_stat(ws_msrp_failed_connections, 1);
  284. if (wsconn_rm(frame->wsc, WSCONN_EVENTROUTE_YES) < 0)
  285. LM_ERR("removing WebSocket connection\n");
  286. tcpconn_put(con);
  287. return -1;
  288. }
  289. update_stat(ws_transmitted_frames, 1);
  290. switch (frame->opcode)
  291. {
  292. case OPCODE_TEXT_FRAME:
  293. case OPCODE_BINARY_FRAME:
  294. if (frame->wsc->sub_protocol == SUB_PROTOCOL_SIP)
  295. update_stat(ws_sip_transmitted_frames, 1);
  296. else if (frame->wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  297. update_stat(ws_msrp_transmitted_frames, 1);
  298. }
  299. pkg_free(send_buf);
  300. tcpconn_put(con);
  301. return 0;
  302. }
  303. static int close_connection(ws_connection_t **p_wsc, ws_close_type_t type,
  304. short int status, str reason)
  305. {
  306. char *data;
  307. ws_frame_t frame;
  308. ws_connection_t * wsc = NULL;
  309. int sub_proto = -1;
  310. if (!p_wsc || !(*p_wsc))
  311. {
  312. LM_ERR("Invalid parameters\n");
  313. return -1;
  314. }
  315. wsc = *p_wsc;
  316. if (wsc->state == WS_S_OPEN)
  317. {
  318. data = pkg_malloc(sizeof(char) * (reason.len + 2));
  319. if (data == NULL)
  320. {
  321. LM_ERR("allocating pkg memory\n");
  322. return -1;
  323. }
  324. data[0] = (status & 0xff00) >> 8;
  325. data[1] = (status & 0x00ff) >> 0;
  326. memcpy(&data[2], reason.s, reason.len);
  327. memset(&frame, 0, sizeof(frame));
  328. frame.fin = 1;
  329. frame.opcode = OPCODE_CLOSE;
  330. frame.payload_len = reason.len + 2;
  331. frame.payload_data = data;
  332. frame.wsc = wsc;
  333. sub_proto = wsc->sub_protocol;
  334. if (encode_and_send_ws_frame(&frame,
  335. type ==
  336. REMOTE_CLOSE ? CONN_CLOSE_DO : CONN_CLOSE_DONT) < 0)
  337. {
  338. LM_ERR("sending WebSocket close\n");
  339. pkg_free(data);
  340. return -1;
  341. }
  342. pkg_free(data);
  343. if (type == LOCAL_CLOSE)
  344. {
  345. frame.wsc->state = WS_S_CLOSING;
  346. update_stat(ws_local_closed_connections, 1);
  347. if (frame.wsc->sub_protocol == SUB_PROTOCOL_SIP)
  348. update_stat(ws_sip_local_closed_connections, 1);
  349. else if (frame.wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  350. update_stat(ws_msrp_local_closed_connections,
  351. 1);
  352. }
  353. else
  354. {
  355. update_stat(ws_remote_closed_connections, 1);
  356. if (sub_proto == SUB_PROTOCOL_SIP)
  357. update_stat(ws_sip_remote_closed_connections,
  358. 1);
  359. else if (sub_proto == SUB_PROTOCOL_MSRP)
  360. update_stat(ws_msrp_remote_closed_connections,
  361. 1);
  362. }
  363. }
  364. else /* if (frame->wsc->state == WS_S_CLOSING) */
  365. {
  366. wsconn_close_now(wsc);
  367. }
  368. return 0;
  369. }
  370. static int decode_and_validate_ws_frame(ws_frame_t *frame,
  371. tcp_event_info_t *tcpinfo,
  372. short *err_code, str *err_text)
  373. {
  374. unsigned int i, len = tcpinfo->len;
  375. int mask_start, j;
  376. char *buf = tcpinfo->buf;
  377. LM_DBG("decoding WebSocket frame\n");
  378. wsconn_update(frame->wsc);
  379. /* Decode and validate first 9 bits */
  380. if (len < 2)
  381. {
  382. LM_WARN("message is too short\n");
  383. *err_code = 1002;
  384. *err_text = str_status_protocol_error;
  385. return -1;
  386. }
  387. frame->fin = (buf[0] & 0xff) & BYTE0_MASK_FIN;
  388. frame->rsv1 = (buf[0] & 0xff) & BYTE0_MASK_RSV1;
  389. frame->rsv2 = (buf[0] & 0xff) & BYTE0_MASK_RSV2;
  390. frame->rsv3 = (buf[0] & 0xff) & BYTE0_MASK_RSV3;
  391. frame->opcode = (buf[0] & 0xff) & BYTE0_MASK_OPCODE;
  392. frame->mask = (buf[1] & 0xff) & BYTE1_MASK_MASK;
  393. if (!frame->fin)
  394. {
  395. LM_WARN("WebSocket fragmentation not supported in the sip "
  396. "sub-protocol\n");
  397. *err_code = 1002;
  398. *err_text = str_status_protocol_error;
  399. return -1;
  400. }
  401. if (frame->rsv1 || frame->rsv2 || frame->rsv3)
  402. {
  403. LM_WARN("WebSocket reserved fields with non-zero values\n");
  404. *err_code = 1002;
  405. *err_text = str_status_protocol_error;
  406. return -1;
  407. }
  408. switch(frame->opcode)
  409. {
  410. case OPCODE_TEXT_FRAME:
  411. case OPCODE_BINARY_FRAME:
  412. LM_DBG("supported non-control frame: 0x%x\n",
  413. (unsigned char) frame->opcode);
  414. break;
  415. case OPCODE_CLOSE:
  416. case OPCODE_PING:
  417. case OPCODE_PONG:
  418. LM_DBG("supported control frame: 0x%x\n",
  419. (unsigned char) frame->opcode);
  420. break;
  421. default:
  422. LM_WARN("unsupported opcode: 0x%x\n",
  423. (unsigned char) frame->opcode);
  424. *err_code = 1008;
  425. *err_text = str_status_unsupported_opcode;
  426. return -1;
  427. }
  428. if (!frame->mask)
  429. {
  430. LM_WARN("this is a server - all received messages must be "
  431. "masked\n");
  432. *err_code = 1002;
  433. *err_text = str_status_protocol_error;
  434. return -1;
  435. }
  436. /* Decode and validate length */
  437. frame->payload_len = (buf[1] & 0xff) & BYTE1_MASK_PAYLOAD_LEN;
  438. if (frame->payload_len == 126)
  439. {
  440. if (len < 4)
  441. {
  442. LM_WARN("message is too short\n");
  443. *err_code = 1002;
  444. *err_text = str_status_protocol_error;
  445. return -1;
  446. }
  447. mask_start = 4;
  448. frame->payload_len = ((buf[2] & 0xff) << 8)
  449. | ((buf[3] & 0xff) << 0);
  450. }
  451. else if (frame->payload_len == 127)
  452. {
  453. if (len < 10)
  454. {
  455. LM_WARN("message is too short\n");
  456. *err_code = 1002;
  457. *err_text = str_status_protocol_error;
  458. return -1;
  459. }
  460. mask_start = 10;
  461. if ((buf[2] & 0xff) != 0 || (buf[3] & 0xff) != 0
  462. || (buf[4] & 0xff) != 0 || (buf[5] & 0xff) != 0)
  463. {
  464. LM_WARN("message is too long\n");
  465. *err_code = 1009;
  466. *err_text = str_status_message_too_big;
  467. return -1;
  468. }
  469. /* Only decoding the last four bytes of the length...
  470. This limits the size of WebSocket messages that can be
  471. handled to 2^32 = which should be plenty for SIP! */
  472. frame->payload_len = ((buf[6] & 0xff) << 24)
  473. | ((buf[7] & 0xff) << 16)
  474. | ((buf[8] & 0xff) << 8)
  475. | ((buf[9] & 0xff) << 0);
  476. }
  477. else
  478. mask_start = 2;
  479. /* Decode mask */
  480. frame->masking_key[0] = (buf[mask_start + 0] & 0xff);
  481. frame->masking_key[1] = (buf[mask_start + 1] & 0xff);
  482. frame->masking_key[2] = (buf[mask_start + 2] & 0xff);
  483. frame->masking_key[3] = (buf[mask_start + 3] & 0xff);
  484. /* Decode and unmask payload */
  485. if ((unsigned long long)len != (unsigned long long)frame->payload_len
  486. + mask_start + 4)
  487. {
  488. LM_WARN("message not complete frame size %u but received %u\n",
  489. frame->payload_len + mask_start + 4, len);
  490. *err_code = 1002;
  491. *err_text = str_status_protocol_error;
  492. return -1;
  493. }
  494. frame->payload_data = &buf[mask_start + 4];
  495. for (i = 0; i < frame->payload_len; i++)
  496. {
  497. j = i % 4;
  498. frame->payload_data[i]
  499. = frame->payload_data[i] ^ frame->masking_key[j];
  500. }
  501. LM_DBG("Rx (decoded): %.*s\n",
  502. (int) frame->payload_len, frame->payload_data);
  503. return frame->opcode;
  504. }
  505. static int handle_close(ws_frame_t *frame)
  506. {
  507. unsigned short code = 0;
  508. str reason = {0, 0};
  509. if (frame->payload_len >= 2)
  510. code = ((frame->payload_data[0] & 0xff) << 8)
  511. | ((frame->payload_data[1] & 0xff) << 0);
  512. if (frame->payload_len > 2)
  513. {
  514. reason.s = &frame->payload_data[2];
  515. reason.len = frame->payload_len - 2;
  516. }
  517. LM_DBG("Rx Close: %hu %.*s\n", code, reason.len, reason.s);
  518. if (close_connection(&frame->wsc,
  519. frame->wsc->state == WS_S_OPEN ? REMOTE_CLOSE : LOCAL_CLOSE,
  520. 1000, str_status_normal_closure) < 0)
  521. {
  522. LM_ERR("closing connection\n");
  523. return -1;
  524. }
  525. return 0;
  526. }
  527. static int handle_ping(ws_frame_t *frame)
  528. {
  529. LM_DBG("Rx Ping: %.*s\n", frame->payload_len, frame->payload_data);
  530. frame->opcode = OPCODE_PONG;
  531. frame->mask = 0;
  532. if (encode_and_send_ws_frame(frame, CONN_CLOSE_DONT) < 0)
  533. {
  534. LM_ERR("sending Pong\n");
  535. return -1;
  536. }
  537. return 0;
  538. }
  539. static int handle_pong(ws_frame_t *frame)
  540. {
  541. LM_DBG("Rx Pong: %.*s\n", frame->payload_len, frame->payload_data);
  542. if (strncmp(frame->payload_data, ws_ping_application_data.s,
  543. ws_ping_application_data.len) == 0)
  544. frame->wsc->awaiting_pong = 0;
  545. return 0;
  546. }
  547. int ws_frame_receive(void *data)
  548. {
  549. ws_frame_t frame;
  550. tcp_event_info_t *tcpinfo = (tcp_event_info_t *) data;
  551. int opcode = -1;
  552. int ret = 0;
  553. short err_code = 0;
  554. str err_text = {NULL, 0};
  555. update_stat(ws_received_frames, 1);
  556. if (tcpinfo == NULL || tcpinfo->buf == NULL || tcpinfo->len <= 0)
  557. {
  558. LM_WARN("received bad frame\n");
  559. return -1;
  560. }
  561. /* wsc refcnt++ */
  562. frame.wsc = wsconn_get(tcpinfo->con->id);
  563. if (frame.wsc == NULL)
  564. {
  565. LM_ERR("WebSocket connection not found\n");
  566. return -1;
  567. }
  568. opcode = decode_and_validate_ws_frame(&frame, tcpinfo, &err_code, &err_text);
  569. if (opcode < 0)
  570. {
  571. if (close_connection(&frame.wsc, LOCAL_CLOSE, err_code, err_text) < 0)
  572. LM_ERR("closing connection\n");
  573. wsconn_put(frame.wsc);
  574. return -1;
  575. }
  576. switch(opcode)
  577. {
  578. case OPCODE_TEXT_FRAME:
  579. case OPCODE_BINARY_FRAME:
  580. if (likely(frame.wsc->sub_protocol == SUB_PROTOCOL_SIP))
  581. {
  582. LM_DBG("Rx SIP message:\n%.*s\n", frame.payload_len,
  583. frame.payload_data);
  584. update_stat(ws_sip_received_frames, 1);
  585. wsconn_put(frame.wsc);
  586. return receive_msg(frame.payload_data,
  587. frame.payload_len,
  588. tcpinfo->rcv);
  589. }
  590. else if (frame.wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  591. {
  592. LM_DBG("Rx MSRP frame:\n%.*s\n", frame.payload_len,
  593. frame.payload_data);
  594. update_stat(ws_msrp_received_frames, 1);
  595. if (likely(sr_event_enabled(SREV_TCP_MSRP_FRAME)))
  596. {
  597. tcp_event_info_t tev;
  598. memset(&tev, 0, sizeof(tcp_event_info_t));
  599. tev.type = SREV_TCP_MSRP_FRAME;
  600. tev.buf = frame.payload_data;
  601. tev.len = frame.payload_len;
  602. tev.rcv = tcpinfo->rcv;
  603. tev.con = tcpinfo->con;
  604. wsconn_put(frame.wsc);
  605. return sr_event_exec(SREV_TCP_MSRP_FRAME,
  606. (void *) &tev);
  607. }
  608. else
  609. {
  610. LM_ERR("no callback registered for MSRP\n");
  611. wsconn_put(frame.wsc);
  612. return -1;
  613. }
  614. }
  615. case OPCODE_CLOSE:
  616. ret = handle_close(&frame);
  617. if (frame.wsc) wsconn_put(frame.wsc);
  618. return ret;
  619. case OPCODE_PING:
  620. ret = handle_ping(&frame);
  621. if (frame.wsc) wsconn_put(frame.wsc);
  622. return ret;
  623. case OPCODE_PONG:
  624. ret = handle_pong(&frame);
  625. if (frame.wsc) wsconn_put(frame.wsc);
  626. return ret;
  627. default:
  628. LM_WARN("received bad frame\n");
  629. wsconn_put(frame.wsc);
  630. return -1;
  631. }
  632. /* how can we get here ? */
  633. wsconn_put(frame.wsc);
  634. return 0;
  635. }
  636. int ws_frame_transmit(void *data)
  637. {
  638. ws_event_info_t *wsev = (ws_event_info_t *) data;
  639. ws_frame_t frame;
  640. memset(&frame, 0, sizeof(frame));
  641. frame.fin = 1;
  642. /* Can't be sure whether this message is UTF-8 or not so check to see
  643. if it "might" be UTF-8 and send as binary if it definitely isn't */
  644. #ifdef EMBEDDED_UTF8_DECODE
  645. frame.opcode = IsUTF8((uint8_t *) wsev->buf, wsev->len) ?
  646. OPCODE_TEXT_FRAME : OPCODE_BINARY_FRAME;
  647. #else
  648. frame.opcode = (u8_check((uint8_t *) wsev->buf, wsev->len) == NULL) ?
  649. OPCODE_TEXT_FRAME : OPCODE_BINARY_FRAME;
  650. #endif
  651. frame.payload_len = wsev->len;
  652. frame.payload_data = wsev->buf;
  653. frame.wsc = wsconn_get(wsev->id);
  654. LM_DBG("Tx message:\n%.*s\n", frame.payload_len,
  655. frame.payload_data);
  656. if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0)
  657. {
  658. LM_ERR("sending message\n");
  659. wsconn_put(frame.wsc);
  660. return -1;
  661. }
  662. wsconn_put(frame.wsc);
  663. return 0;
  664. }
  665. static int ping_pong(ws_connection_t *wsc, int opcode)
  666. {
  667. ws_frame_t frame;
  668. memset(&frame, 0, sizeof(frame));
  669. frame.fin = 1;
  670. frame.opcode = opcode;
  671. frame.payload_len = ws_ping_application_data.len;
  672. frame.payload_data = ws_ping_application_data.s;
  673. frame.wsc = wsc;
  674. if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0)
  675. {
  676. LM_ERR("sending keepalive\n");
  677. return -1;
  678. }
  679. if (opcode == OPCODE_PING)
  680. wsc->awaiting_pong = 1;
  681. return 0;
  682. }
  683. struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
  684. {
  685. unsigned int id;
  686. struct mi_node *node = NULL;
  687. ws_connection_t *wsc;
  688. node = cmd->node.kids;
  689. if (node == NULL)
  690. {
  691. LM_WARN("no connection ID parameter\n");
  692. return init_mi_tree(400, str_status_empty_param.s,
  693. str_status_empty_param.len);
  694. }
  695. if (node->value.s == NULL || node->value.len == 0)
  696. {
  697. LM_WARN("empty connection ID parameter\n");
  698. return init_mi_tree(400, str_status_empty_param.s,
  699. str_status_empty_param.len);
  700. }
  701. if (str2int(&node->value, &id) < 0)
  702. {
  703. LM_ERR("converting string to int\n");
  704. return init_mi_tree(400, str_status_string_error.s,
  705. str_status_string_error.len);
  706. }
  707. if (node->next != NULL)
  708. {
  709. LM_WARN("too many parameters\n");
  710. return init_mi_tree(400, str_status_too_many_params.s,
  711. str_status_too_many_params.len);
  712. }
  713. if ((wsc = wsconn_get(id)) == NULL)
  714. {
  715. LM_WARN("bad connection ID parameter\n");
  716. return init_mi_tree(400, str_status_bad_param.s,
  717. str_status_bad_param.len);
  718. }
  719. int ret = close_connection(&wsc, LOCAL_CLOSE, 1000, str_status_normal_closure);
  720. wsconn_put(wsc);
  721. if (ret < 0)
  722. {
  723. LM_WARN("closing connection\n");
  724. return init_mi_tree(500, str_status_error_closing.s,
  725. str_status_error_closing.len);
  726. }
  727. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  728. }
  729. static struct mi_root *mi_ping_pong(struct mi_root *cmd, void *param,
  730. int opcode)
  731. {
  732. unsigned int id;
  733. struct mi_node *node = NULL;
  734. ws_connection_t *wsc;
  735. node = cmd->node.kids;
  736. if (node == NULL)
  737. {
  738. LM_WARN("no connection ID parameter\n");
  739. return init_mi_tree(400, str_status_empty_param.s,
  740. str_status_empty_param.len);
  741. }
  742. if (node->value.s == NULL || node->value.len == 0)
  743. {
  744. LM_WARN("empty connection ID parameter\n");
  745. return init_mi_tree(400, str_status_empty_param.s,
  746. str_status_empty_param.len);
  747. }
  748. if (str2int(&node->value, &id) < 0)
  749. {
  750. LM_ERR("converting string to int\n");
  751. return init_mi_tree(400, str_status_string_error.s,
  752. str_status_string_error.len);
  753. }
  754. if (node->next != NULL)
  755. {
  756. LM_WARN("too many parameters\n");
  757. return init_mi_tree(400, str_status_too_many_params.s,
  758. str_status_too_many_params.len);
  759. }
  760. if ((wsc = wsconn_get(id)) == NULL)
  761. {
  762. LM_WARN("bad connection ID parameter\n");
  763. return init_mi_tree(400, str_status_bad_param.s,
  764. str_status_bad_param.len);
  765. }
  766. int ret = ping_pong(wsc, opcode);
  767. wsconn_put(wsc);
  768. if (ret < 0)
  769. {
  770. LM_WARN("sending %s\n", OPCODE_PING ? "Ping" : "Pong");
  771. return init_mi_tree(500, str_status_error_sending.s,
  772. str_status_error_sending.len);
  773. }
  774. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  775. }
  776. struct mi_root *ws_mi_ping(struct mi_root *cmd, void *param)
  777. {
  778. return mi_ping_pong(cmd, param, OPCODE_PING);
  779. }
  780. struct mi_root *ws_mi_pong(struct mi_root *cmd, void *param)
  781. {
  782. return mi_ping_pong(cmd, param, OPCODE_PONG);
  783. }
  784. void ws_keepalive(unsigned int ticks, void *param)
  785. {
  786. int check_time = (int) time(NULL)
  787. - cfg_get(websocket, ws_cfg, keepalive_timeout);
  788. ws_connection_t **list = NULL,
  789. **list_head = NULL;
  790. ws_connection_t *wsc = NULL;
  791. /* get an array of pointer to all ws connection */
  792. list_head = wsconn_get_list();
  793. if (!list_head)
  794. return;
  795. list = list_head;
  796. wsc = *list_head;
  797. while (wsc && wsc->last_used < check_time)
  798. {
  799. if (wsc->state == WS_S_CLOSING || wsc->awaiting_pong)
  800. {
  801. LM_WARN("forcibly closing connection\n");
  802. wsconn_close_now(wsc);
  803. }
  804. else
  805. {
  806. int opcode = (ws_keepalive_mechanism == KEEPALIVE_MECHANISM_PING)
  807. ? OPCODE_PING
  808. : OPCODE_PONG;
  809. ping_pong(wsc, opcode);
  810. }
  811. wsc = *(++list);
  812. }
  813. wsconn_put_list(list_head);
  814. }
  815. int ws_close(sip_msg_t *msg)
  816. {
  817. ws_connection_t *wsc;
  818. int ret;
  819. if ((wsc = wsconn_get(msg->rcv.proto_reserved1)) == NULL) {
  820. LM_ERR("failed to retrieve WebSocket connection\n");
  821. return -1;
  822. }
  823. ret = (close_connection(&wsc, LOCAL_CLOSE, 1000,
  824. str_status_normal_closure) == 0) ? 1: 0;
  825. wsconn_put(wsc);
  826. return ret;
  827. }
  828. int ws_close2(sip_msg_t *msg, char *_status, char *_reason)
  829. {
  830. int status;
  831. str reason;
  832. ws_connection_t *wsc;
  833. int ret;
  834. if (get_int_fparam(&status, msg, (fparam_t *) _status) < 0) {
  835. LM_ERR("failed to get status code\n");
  836. return -1;
  837. }
  838. if (get_str_fparam(&reason, msg, (fparam_t *) _reason) < 0) {
  839. LM_ERR("failed to get reason string\n");
  840. return -1;
  841. }
  842. if ((wsc = wsconn_get(msg->rcv.proto_reserved1)) == NULL) {
  843. LM_ERR("failed to retrieve WebSocket connection\n");
  844. return -1;
  845. }
  846. ret = (close_connection(&wsc, LOCAL_CLOSE, status, reason) == 0) ? 1: 0;
  847. wsconn_put(wsc);
  848. return ret;
  849. }
  850. int ws_close3(sip_msg_t *msg, char *_status, char *_reason, char *_con)
  851. {
  852. int status;
  853. str reason;
  854. int con;
  855. ws_connection_t *wsc;
  856. int ret;
  857. if (get_int_fparam(&status, msg, (fparam_t *) _status) < 0) {
  858. LM_ERR("failed to get status code\n");
  859. return -1;
  860. }
  861. if (get_str_fparam(&reason, msg, (fparam_t *) _reason) < 0) {
  862. LM_ERR("failed to get reason string\n");
  863. return -1;
  864. }
  865. if (get_int_fparam(&con, msg, (fparam_t *) _con) < 0) {
  866. LM_ERR("failed to get connection ID\n");
  867. return -1;
  868. }
  869. if ((wsc = wsconn_get(con)) == NULL) {
  870. LM_ERR("failed to retrieve WebSocket connection\n");
  871. return -1;
  872. }
  873. ret = (close_connection(&wsc, LOCAL_CLOSE, status, reason) == 0) ? 1: 0;
  874. wsconn_put(wsc);
  875. return ret;
  876. }