ws_mod.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2012-2013 Crocodile RCS Ltd
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Exception: permission to copy, modify, propagate, and distribute a work
  21. * formed by combining OpenSSL toolkit software and the code in this file,
  22. * such as linking with software components and libraries released under
  23. * OpenSSL project license.
  24. *
  25. */
  26. #include "../../dprint.h"
  27. #include "../../events.h"
  28. #include "../../ip_addr.h"
  29. #include "../../locking.h"
  30. #include "../../sr_module.h"
  31. #include "../../tcp_conn.h"
  32. #include "../../timer_proc.h"
  33. #include "../../cfg/cfg.h"
  34. #include "../../lib/kcore/kstats_wrapper.h"
  35. #include "../../lib/kmi/mi.h"
  36. #include "../../mem/mem.h"
  37. #include "../../mod_fix.h"
  38. #include "../../parser/msg_parser.h"
  39. #include "ws_conn.h"
  40. #include "ws_handshake.h"
  41. #include "ws_frame.h"
  42. #include "ws_mod.h"
  43. #include "config.h"
  44. MODULE_VERSION
  45. /* Maximum number of connections to display when using the ws.dump MI command */
  46. #define MAX_WS_CONNS_DUMP 50
  47. static int mod_init(void);
  48. static int child_init(int rank);
  49. static void destroy(void);
  50. static int ws_close_fixup(void** param, int param_no);
  51. sl_api_t ws_slb;
  52. #define DEFAULT_KEEPALIVE_INTERVAL 1
  53. static int ws_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
  54. static int ws_keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  55. #define DEFAULT_KEEPALIVE_PROCESSES 1
  56. static int ws_keepalive_processes = DEFAULT_KEEPALIVE_PROCESSES;
  57. static cmd_export_t cmds[]=
  58. {
  59. /* ws_frame.c */
  60. { "ws_close", (cmd_function) ws_close,
  61. 0, 0, 0,
  62. ANY_ROUTE },
  63. { "ws_close", (cmd_function) ws_close2,
  64. 2, ws_close_fixup, 0,
  65. ANY_ROUTE },
  66. { "ws_close", (cmd_function) ws_close3,
  67. 3, ws_close_fixup, 0,
  68. ANY_ROUTE },
  69. /* ws_handshake.c */
  70. { "ws_handle_handshake", (cmd_function) ws_handle_handshake,
  71. 0, 0, 0,
  72. ANY_ROUTE },
  73. { 0, 0, 0, 0, 0, 0 }
  74. };
  75. static param_export_t params[]=
  76. {
  77. /* ws_frame.c */
  78. { "keepalive_mechanism", INT_PARAM, &ws_keepalive_mechanism },
  79. { "keepalive_timeout", INT_PARAM, &ws_keepalive_timeout },
  80. { "ping_application_data", PARAM_STR, &ws_ping_application_data },
  81. /* ws_handshake.c */
  82. { "sub_protocols", INT_PARAM, &ws_sub_protocols },
  83. { "cors_mode", INT_PARAM, &ws_cors_mode },
  84. /* ws_mod.c */
  85. { "keepalive_interval", INT_PARAM, &ws_keepalive_interval },
  86. { "keepalive_processes", INT_PARAM, &ws_keepalive_processes },
  87. { 0, 0, 0 }
  88. };
  89. static stat_export_t stats[] =
  90. {
  91. /* ws_conn.c */
  92. { "ws_current_connections", 0, &ws_current_connections },
  93. { "ws_max_concurrent_connections", 0, &ws_max_concurrent_connections },
  94. { "ws_sip_current_connections", 0, &ws_sip_current_connections },
  95. { "ws_sip_max_concurrent_connectons", 0, &ws_sip_max_concurrent_connections },
  96. { "ws_msrp_current_connections", 0, &ws_msrp_current_connections },
  97. { "ws_msrp_max_concurrent_connectons", 0, &ws_msrp_max_concurrent_connections },
  98. /* ws_frame.c */
  99. { "ws_failed_connections", 0, &ws_failed_connections },
  100. { "ws_local_closed_connections", 0, &ws_local_closed_connections },
  101. { "ws_received_frames", 0, &ws_received_frames },
  102. { "ws_remote_closed_connections", 0, &ws_remote_closed_connections },
  103. { "ws_transmitted_frames", 0, &ws_transmitted_frames },
  104. { "ws_sip_failed_connections", 0, &ws_sip_failed_connections },
  105. { "ws_sip_local_closed_connections", 0, &ws_sip_local_closed_connections },
  106. { "ws_sip_received_frames", 0, &ws_sip_received_frames },
  107. { "ws_sip_remote_closed_connections", 0, &ws_sip_remote_closed_connections },
  108. { "ws_sip_transmitted_frames", 0, &ws_sip_transmitted_frames },
  109. { "ws_msrp_failed_connections", 0, &ws_msrp_failed_connections },
  110. { "ws_msrp_local_closed_connections", 0, &ws_msrp_local_closed_connections },
  111. { "ws_msrp_received_frames", 0, &ws_msrp_received_frames },
  112. { "ws_msrp_remote_closed_connections", 0, &ws_msrp_remote_closed_connections },
  113. { "ws_msrp_transmitted_frames", 0, &ws_msrp_transmitted_frames },
  114. /* ws_handshake.c */
  115. { "ws_failed_handshakes", 0, &ws_failed_handshakes },
  116. { "ws_successful_handshakes", 0, &ws_successful_handshakes },
  117. { "ws_sip_successful_handshakes", 0, &ws_sip_successful_handshakes },
  118. { "ws_msrp_successful_handshakes", 0, &ws_msrp_successful_handshakes },
  119. { 0, 0, 0 }
  120. };
  121. static mi_export_t mi_cmds[] =
  122. {
  123. /* ws_conn.c */
  124. { "ws.dump", ws_mi_dump, 0, 0, 0 },
  125. /* ws_frame.c */
  126. { "ws.close", ws_mi_close, 0, 0, 0 },
  127. { "ws.ping", ws_mi_ping, 0, 0, 0 },
  128. { "ws.pong", ws_mi_pong, 0, 0, 0 },
  129. /* ws_handshake.c */
  130. { "ws.disable", ws_mi_disable, 0, 0, 0 },
  131. { "ws.enable", ws_mi_enable, 0, 0, 0 },
  132. { 0, 0, 0, 0, 0 }
  133. };
  134. struct module_exports exports=
  135. {
  136. "websocket",
  137. DEFAULT_DLFLAGS, /* dlopen flags */
  138. cmds, /* Exported functions */
  139. params, /* Exported parameters */
  140. stats, /* exported statistics */
  141. mi_cmds, /* exported MI functions */
  142. 0, /* exported pseudo-variables */
  143. 0, /* extra processes */
  144. mod_init, /* module initialization function */
  145. 0, /* response function */
  146. destroy, /* destroy function */
  147. child_init /* per-child initialization function */
  148. };
  149. static int mod_init(void)
  150. {
  151. if (sl_load_api(&ws_slb) != 0)
  152. {
  153. LM_ERR("binding to SL\n");
  154. goto error;
  155. }
  156. if (sr_event_register_cb(SREV_TCP_WS_FRAME_IN, ws_frame_receive) != 0)
  157. {
  158. LM_ERR("registering WebSocket receive call-back\n");
  159. goto error;
  160. }
  161. if (sr_event_register_cb(SREV_TCP_WS_FRAME_OUT, ws_frame_transmit) != 0)
  162. {
  163. LM_ERR("registering WebSocket transmit call-back\n");
  164. goto error;
  165. }
  166. if (register_module_stats(exports.name, stats) != 0)
  167. {
  168. LM_ERR("registering core statistics\n");
  169. goto error;
  170. }
  171. if (register_mi_mod(exports.name, mi_cmds) != 0)
  172. {
  173. LM_ERR("registering MI commands\n");
  174. goto error;
  175. }
  176. if (wsconn_init() < 0)
  177. {
  178. LM_ERR("initialising WebSocket connections table\n");
  179. goto error;
  180. }
  181. if (ws_ping_application_data.len < 1
  182. || ws_ping_application_data.len > 125)
  183. {
  184. ws_ping_application_data.s = DEFAULT_PING_APPLICATION_DATA + 8;
  185. ws_ping_application_data.len =
  186. DEFAULT_PING_APPLICATION_DATA_LEN - 8;
  187. }
  188. if (ws_keepalive_mechanism != KEEPALIVE_MECHANISM_NONE)
  189. {
  190. if (ws_keepalive_timeout < 1 || ws_keepalive_timeout > 3600)
  191. ws_keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  192. switch(ws_keepalive_mechanism)
  193. {
  194. case KEEPALIVE_MECHANISM_PING:
  195. case KEEPALIVE_MECHANISM_PONG:
  196. break;
  197. default:
  198. ws_keepalive_mechanism = DEFAULT_KEEPALIVE_MECHANISM;
  199. break;
  200. }
  201. if (ws_keepalive_interval < 1 || ws_keepalive_interval > 60)
  202. ws_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
  203. if (ws_keepalive_processes < 1 || ws_keepalive_processes > 16)
  204. ws_keepalive_processes = DEFAULT_KEEPALIVE_PROCESSES;
  205. /* Add extra process/timer for the keepalive process */
  206. register_sync_timers(ws_keepalive_processes);
  207. }
  208. if (ws_sub_protocols & SUB_PROTOCOL_MSRP
  209. && !sr_event_enabled(SREV_TCP_MSRP_FRAME))
  210. ws_sub_protocols &= ~SUB_PROTOCOL_MSRP;
  211. if ((ws_sub_protocols & SUB_PROTOCOL_ALL) == 0)
  212. {
  213. LM_ERR("no sub-protocols enabled\n");
  214. goto error;
  215. }
  216. if ((ws_sub_protocols | SUB_PROTOCOL_ALL) != SUB_PROTOCOL_ALL)
  217. {
  218. LM_ERR("unrecognised sub-protocols enabled\n");
  219. goto error;
  220. }
  221. if (ws_cors_mode < 0 || ws_cors_mode > 2)
  222. {
  223. LM_ERR("bad value for cors_mode\n");
  224. goto error;
  225. }
  226. if (cfg_declare("websocket", ws_cfg_def, &default_ws_cfg,
  227. cfg_sizeof(websocket), &ws_cfg))
  228. {
  229. LM_ERR("declaring configuration\n");
  230. return -1;
  231. }
  232. cfg_get(websocket, ws_cfg, keepalive_timeout) = ws_keepalive_timeout;
  233. if (!module_loaded("xhttp"))
  234. {
  235. LM_ERR("\"xhttp\" must be loaded to use WebSocket.\n");
  236. return -1;
  237. }
  238. if (((ws_sub_protocols & SUB_PROTOCOL_SIP) == SUB_PROTOCOL_SIP)
  239. && !module_loaded("nathelper")
  240. && !module_loaded("outbound"))
  241. {
  242. LM_WARN("neither \"nathelper\" nor \"outbound\" modules are"
  243. " loaded. At least one of these is required for correct"
  244. " routing of SIP over WebSocket.\n");
  245. }
  246. return 0;
  247. error:
  248. wsconn_destroy();
  249. return -1;
  250. }
  251. static int child_init(int rank)
  252. {
  253. int i;
  254. if (rank == PROC_INIT || rank == PROC_TCP_MAIN)
  255. return 0;
  256. if (rank == PROC_MAIN
  257. && ws_keepalive_mechanism != KEEPALIVE_MECHANISM_NONE)
  258. {
  259. for (i = 0; i < ws_keepalive_processes; i++)
  260. {
  261. if (fork_sync_timer(PROC_TIMER, "WEBSOCKET KEEPALIVE",
  262. 1, ws_keepalive, NULL,
  263. ws_keepalive_interval) < 0)
  264. {
  265. LM_ERR("starting keepalive process\n");
  266. return -1;
  267. }
  268. }
  269. }
  270. return 0;
  271. }
  272. static void destroy(void)
  273. {
  274. wsconn_destroy();
  275. }
  276. static int ws_close_fixup(void** param, int param_no)
  277. {
  278. switch(param_no) {
  279. case 1:
  280. case 3:
  281. return fixup_var_int_1(param, 1);
  282. case 2:
  283. return fixup_spve_null(param, 1);
  284. default:
  285. return 0;
  286. }
  287. }