ws_mod.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include "../../dprint.h"
  24. #include "../../events.h"
  25. #include "../../ip_addr.h"
  26. #include "../../locking.h"
  27. #include "../../sr_module.h"
  28. #include "../../tcp_conn.h"
  29. #include "../../timer_proc.h"
  30. #include "../../lib/kcore/kstats_wrapper.h"
  31. #include "../../lib/kmi/mi.h"
  32. #include "../../mem/mem.h"
  33. #include "../../parser/msg_parser.h"
  34. #include "ws_conn.h"
  35. #include "ws_handshake.h"
  36. #include "ws_frame.h"
  37. #include "ws_mod.h"
  38. MODULE_VERSION
  39. /* Maximum number of connections to display when using the ws.dump MI command */
  40. #define MAX_WS_CONNS_DUMP 50
  41. static int mod_init(void);
  42. static int child_init(int rank);
  43. static void destroy(void);
  44. sl_api_t ws_slb;
  45. int *ws_enabled;
  46. #define DEFAULT_KEEPALIVE_INTERVAL 1
  47. static int ws_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
  48. #define DEFAULT_KEEPALIVE_PROCESSES 1
  49. static int ws_keepalive_processes = DEFAULT_KEEPALIVE_PROCESSES;
  50. static cmd_export_t cmds[]=
  51. {
  52. /* ws_handshake.c */
  53. { "ws_handle_handshake", (cmd_function) ws_handle_handshake,
  54. 0, 0, 0,
  55. ANY_ROUTE },
  56. { 0, 0, 0, 0, 0, 0 }
  57. };
  58. static param_export_t params[]=
  59. {
  60. /* ws_frame.c */
  61. { "keepalive_mechanism", INT_PARAM, &ws_keepalive_mechanism },
  62. { "keepalive_timeout", INT_PARAM, &ws_keepalive_timeout },
  63. { "ping_application_data", STR_PARAM, &ws_ping_application_data.s},
  64. /* ws_handshake.c */
  65. { "sub_protocols", INT_PARAM, &ws_sub_protocols},
  66. /* ws_mod.c */
  67. { "keepalive_interval", INT_PARAM, &ws_keepalive_interval },
  68. { "keepalive_processes", INT_PARAM, &ws_keepalive_processes },
  69. { 0, 0, 0 }
  70. };
  71. static stat_export_t stats[] =
  72. {
  73. /* ws_conn.c */
  74. { "ws_current_connections", 0, &ws_current_connections },
  75. { "ws_max_concurrent_connections",0, &ws_max_concurrent_connections },
  76. /* ws_frame.c */
  77. { "ws_failed_connections", 0, &ws_failed_connections },
  78. { "ws_local_closed_connections", 0, &ws_local_closed_connections },
  79. { "ws_received_frames", 0, &ws_received_frames },
  80. { "ws_remote_closed_connections", 0, &ws_remote_closed_connections },
  81. { "ws_transmitted_frames", 0, &ws_transmitted_frames },
  82. /* ws_handshake.c */
  83. { "ws_failed_handshakes", 0, &ws_failed_handshakes },
  84. { "ws_successful_handshakes", 0, &ws_successful_handshakes },
  85. { 0, 0, 0 }
  86. };
  87. static mi_export_t mi_cmds[] =
  88. {
  89. /* ws_conn.c */
  90. { "ws.dump", ws_mi_dump, 0, 0, 0 },
  91. /* ws_frame.c */
  92. { "ws.close", ws_mi_close, 0, 0, 0 },
  93. { "ws.ping", ws_mi_ping, 0, 0, 0 },
  94. { "ws.pong", ws_mi_pong, 0, 0, 0 },
  95. /* ws_handshake.c */
  96. { "ws.disable", ws_mi_disable, 0, 0, 0 },
  97. { "ws.enable", ws_mi_enable, 0, 0, 0 },
  98. { 0, 0, 0, 0, 0 }
  99. };
  100. struct module_exports exports=
  101. {
  102. "websocket",
  103. DEFAULT_DLFLAGS, /* dlopen flags */
  104. cmds, /* Exported functions */
  105. params, /* Exported parameters */
  106. stats, /* exported statistics */
  107. mi_cmds, /* exported MI functions */
  108. 0, /* exported pseudo-variables */
  109. 0, /* extra processes */
  110. mod_init, /* module initialization function */
  111. 0, /* response function */
  112. destroy, /* destroy function */
  113. child_init /* per-child initialization function */
  114. };
  115. static int mod_init(void)
  116. {
  117. if (sl_load_api(&ws_slb) != 0)
  118. {
  119. LM_ERR("binding to SL\n");
  120. goto error;
  121. }
  122. if (sr_event_register_cb(SREV_TCP_WS_FRAME_IN, ws_frame_receive) != 0)
  123. {
  124. LM_ERR("registering WebSocket receive call-back\n");
  125. goto error;
  126. }
  127. if (sr_event_register_cb(SREV_TCP_WS_FRAME_OUT, ws_frame_transmit) != 0)
  128. {
  129. LM_ERR("registering WebSocket transmit call-back\n");
  130. goto error;
  131. }
  132. if (register_module_stats(exports.name, stats) != 0)
  133. {
  134. LM_ERR("registering core statistics\n");
  135. goto error;
  136. }
  137. if (register_mi_mod(exports.name, mi_cmds) != 0)
  138. {
  139. LM_ERR("registering MI commands\n");
  140. goto error;
  141. }
  142. if (wsconn_init() < 0)
  143. {
  144. LM_ERR("initialising WebSocket connections table\n");
  145. goto error;
  146. }
  147. if ((ws_enabled = (int *) shm_malloc(sizeof(int))) == NULL)
  148. {
  149. LM_ERR("allocating shared memory\n");
  150. goto error;
  151. }
  152. *ws_enabled = 1;
  153. if (ws_ping_application_data.s != 0)
  154. ws_ping_application_data.len =
  155. strlen(ws_ping_application_data.s);
  156. if (ws_ping_application_data.len < 1
  157. || ws_ping_application_data.len > 125)
  158. {
  159. ws_ping_application_data.s = DEFAULT_PING_APPLICATION_DATA + 8;
  160. ws_ping_application_data.len =
  161. DEFAULT_PING_APPLICATION_DATA_LEN - 8;
  162. }
  163. if (ws_keepalive_mechanism != KEEPALIVE_MECHANISM_NONE)
  164. {
  165. if (ws_keepalive_timeout < 1 || ws_keepalive_timeout > 3600)
  166. ws_keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  167. switch(ws_keepalive_mechanism)
  168. {
  169. case KEEPALIVE_MECHANISM_PING:
  170. case KEEPALIVE_MECHANISM_PONG:
  171. break;
  172. default:
  173. ws_keepalive_mechanism = DEFAULT_KEEPALIVE_MECHANISM;
  174. break;
  175. }
  176. if (ws_keepalive_interval < 1 || ws_keepalive_interval > 60)
  177. ws_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
  178. if (ws_keepalive_processes < 1 || ws_keepalive_processes > 16)
  179. ws_keepalive_processes = DEFAULT_KEEPALIVE_PROCESSES;
  180. /* Add extra process/timer for the keepalive process */
  181. register_sync_timers(ws_keepalive_processes);
  182. }
  183. if (ws_sub_protocols & SUB_PROTOCOL_MSRP
  184. && !sr_event_enabled(SREV_TCP_MSRP_FRAME))
  185. ws_sub_protocols &= ~SUB_PROTOCOL_MSRP;
  186. if ((ws_sub_protocols & SUB_PROTOCOL_ALL) == 0)
  187. {
  188. LM_ERR("no sub-protocols enabled\n");
  189. goto error;
  190. }
  191. if ((ws_sub_protocols | SUB_PROTOCOL_ALL) != SUB_PROTOCOL_ALL)
  192. {
  193. LM_ERR("unrecognised sub-protocols enabled\n");
  194. goto error;
  195. }
  196. return 0;
  197. error:
  198. wsconn_destroy();
  199. shm_free(ws_enabled);
  200. return -1;
  201. }
  202. static int child_init(int rank)
  203. {
  204. int i;
  205. if (rank == PROC_INIT || rank == PROC_TCP_MAIN)
  206. return 0;
  207. if (rank == PROC_MAIN
  208. && ws_keepalive_mechanism != KEEPALIVE_MECHANISM_NONE)
  209. {
  210. for (i = 0; i < ws_keepalive_processes; i++)
  211. {
  212. if (fork_sync_timer(PROC_TIMER, "WEBSOCKET KEEPALIVE",
  213. 1, ws_keepalive, NULL,
  214. ws_keepalive_interval) < 0)
  215. {
  216. LM_ERR("starting keepalive process\n");
  217. return -1;
  218. }
  219. }
  220. }
  221. return 0;
  222. }
  223. static void destroy(void)
  224. {
  225. wsconn_destroy();
  226. shm_free(ws_enabled);
  227. }