ws_conn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 "../../locking.h"
  24. #include "../../str.h"
  25. #include "../../tcp_conn.h"
  26. #include "../../lib/kcore/faked_msg.h"
  27. #include "../../lib/kcore/kstats_wrapper.h"
  28. #include "../../lib/kmi/tree.h"
  29. #include "../../mem/mem.h"
  30. #include "ws_conn.h"
  31. #include "ws_mod.h"
  32. /* Maximum number of connections to display when using the ws.dump MI command */
  33. #define MAX_WS_CONNS_DUMP 50
  34. ws_connection_t **wsconn_id_hash = NULL;
  35. #define wsconn_listadd tcpconn_listadd
  36. #define wsconn_listrm tcpconn_listrm
  37. gen_lock_t *wsconn_lock = NULL;
  38. #define WSCONN_LOCK lock_get(wsconn_lock)
  39. #define WSCONN_UNLOCK lock_release(wsconn_lock)
  40. gen_lock_t *wsstat_lock = NULL;
  41. ws_connection_used_list_t *wsconn_used_list = NULL;
  42. stat_var *ws_current_connections;
  43. stat_var *ws_max_concurrent_connections;
  44. char *wsconn_state_str[] =
  45. {
  46. "CONNECTING", /* WS_S_CONNECTING */
  47. "OPEN", /* WS_S_OPEN */
  48. "CLOSING", /* WS_S_CLOSING */
  49. "CLOSED" /* WS_S_CLOSED */
  50. };
  51. /* MI command status text */
  52. static str str_status_empty_param = str_init("Empty display order parameter");
  53. static str str_status_bad_param = str_init("Bad display order parameter");
  54. static str str_status_too_many_params = str_init("Too many parameters");
  55. int wsconn_init(void)
  56. {
  57. wsconn_lock = lock_alloc();
  58. if (wsconn_lock == NULL)
  59. {
  60. LM_ERR("allocating lock\n");
  61. goto error;
  62. }
  63. if (lock_init(wsconn_lock) == 0)
  64. {
  65. LM_ERR("initialising lock\n");
  66. goto error;
  67. }
  68. wsstat_lock = lock_alloc();
  69. if (wsstat_lock == NULL)
  70. {
  71. LM_ERR("allocating lock\n");
  72. goto error;
  73. }
  74. if (lock_init(wsstat_lock) == NULL)
  75. {
  76. LM_ERR("initialising lock\n");
  77. goto error;
  78. }
  79. wsconn_id_hash =
  80. (ws_connection_t **) shm_malloc(TCP_ID_HASH_SIZE *
  81. sizeof(ws_connection_t*));
  82. if (wsconn_id_hash == NULL)
  83. {
  84. LM_ERR("allocating WebSocket hash-table\n");
  85. goto error;
  86. }
  87. memset((void *) wsconn_id_hash, 0,
  88. TCP_ID_HASH_SIZE * sizeof(ws_connection_t *));
  89. wsconn_used_list = (ws_connection_used_list_t *) shm_malloc(
  90. sizeof(ws_connection_used_list_t));
  91. if (wsconn_used_list == NULL)
  92. {
  93. LM_ERR("allocating WebSocket used list\n");
  94. goto error;
  95. }
  96. memset((void *) wsconn_used_list, 0, sizeof(ws_connection_used_list_t));
  97. return 0;
  98. error:
  99. if (wsconn_lock) lock_dealloc((void *) wsconn_lock);
  100. if (wsstat_lock) lock_dealloc((void *) wsstat_lock);
  101. wsconn_lock = wsstat_lock = NULL;
  102. if (wsconn_id_hash) shm_free(wsconn_id_hash);
  103. if (wsconn_used_list) shm_free(wsconn_used_list);
  104. wsconn_id_hash = NULL;
  105. wsconn_used_list = NULL;
  106. return -1;
  107. }
  108. static inline void _wsconn_rm(ws_connection_t *wsc)
  109. {
  110. wsconn_listrm(wsconn_id_hash[wsc->id_hash], wsc, id_next, id_prev);
  111. shm_free(wsc);
  112. wsc = NULL;
  113. update_stat(ws_current_connections, -1);
  114. }
  115. void wsconn_destroy(void)
  116. {
  117. int h;
  118. if (wsconn_used_list)
  119. {
  120. shm_free(wsconn_used_list);
  121. wsconn_used_list = NULL;
  122. }
  123. if (wsconn_id_hash)
  124. {
  125. WSCONN_UNLOCK;
  126. WSCONN_LOCK;
  127. for (h = 0; h < TCP_ID_HASH_SIZE; h++)
  128. {
  129. ws_connection_t *wsc = wsconn_id_hash[h];
  130. while (wsc)
  131. {
  132. ws_connection_t *next = wsc->id_next;
  133. _wsconn_rm(wsc);
  134. wsc = next;
  135. }
  136. }
  137. WSCONN_UNLOCK;
  138. shm_free(wsconn_id_hash);
  139. wsconn_id_hash = NULL;
  140. }
  141. if (wsconn_lock)
  142. {
  143. lock_destroy(wsconn_lock);
  144. lock_dealloc((void *) wsconn_lock);
  145. wsconn_lock = NULL;
  146. }
  147. if (wsstat_lock)
  148. {
  149. lock_destroy(wsstat_lock);
  150. lock_dealloc((void *) wsstat_lock);
  151. wsstat_lock = NULL;
  152. }
  153. }
  154. int wsconn_add(struct receive_info rcv, unsigned int sub_protocol)
  155. {
  156. int cur_cons, max_cons;
  157. int id = rcv.proto_reserved1;
  158. int id_hash = tcp_id_hash(id);
  159. ws_connection_t *wsc;
  160. /* Allocate and fill in new WebSocket connection */
  161. wsc = shm_malloc(sizeof(ws_connection_t));
  162. if (wsc == NULL)
  163. {
  164. LM_ERR("allocating shared memory\n");
  165. return -1;
  166. }
  167. memset(wsc, 0, sizeof(ws_connection_t));
  168. wsc->id = id;
  169. wsc->id_hash = id_hash;
  170. wsc->state = WS_S_OPEN;
  171. wsc->rcv = rcv;
  172. wsc->sub_protocol = sub_protocol;
  173. WSCONN_LOCK;
  174. /* Add to WebSocket connection table */
  175. wsconn_listadd(wsconn_id_hash[wsc->id_hash], wsc, id_next, id_prev);
  176. /* Add to the end of the WebSocket used list */
  177. wsc->last_used = (int)time(NULL);
  178. if (wsconn_used_list->head == NULL)
  179. wsconn_used_list->head = wsconn_used_list->tail = wsc;
  180. else
  181. {
  182. wsc->used_prev = wsconn_used_list->tail;
  183. wsconn_used_list->tail->used_next = wsc;
  184. wsconn_used_list->tail = wsc;
  185. }
  186. WSCONN_UNLOCK;
  187. /* Update connection statistics */
  188. lock_get(wsstat_lock);
  189. update_stat(ws_current_connections, 1);
  190. cur_cons = get_stat_val(ws_current_connections);
  191. max_cons = get_stat_val(ws_max_concurrent_connections);
  192. if (max_cons < cur_cons)
  193. update_stat(ws_max_concurrent_connections, cur_cons - max_cons);
  194. lock_release(wsstat_lock);
  195. return 0;
  196. }
  197. static void wsconn_run_route(ws_connection_t *wsc)
  198. {
  199. int rt, backup_rt;
  200. struct run_act_ctx ctx;
  201. sip_msg_t *fmsg;
  202. LM_DBG("wsconn_run_route event_route[websocket:closed]\n");
  203. rt = route_get(&event_rt, "websocket:closed");
  204. if (rt < 0 || event_rt.rlist[rt] == NULL)
  205. {
  206. LM_DBG("route does not exist");
  207. return;
  208. }
  209. if (faked_msg_init() < 0)
  210. {
  211. LM_ERR("faked_msg_init() failed\n");
  212. return;
  213. }
  214. fmsg = faked_msg_next();
  215. fmsg->rcv = wsc->rcv;
  216. backup_rt = get_route_type();
  217. set_route_type(REQUEST_ROUTE);
  218. init_run_actions_ctx(&ctx);
  219. run_top_route(event_rt.rlist[rt], fmsg, 0);
  220. set_route_type(backup_rt);
  221. }
  222. int wsconn_rm(ws_connection_t *wsc, ws_conn_eventroute_t run_event_route)
  223. {
  224. if (!wsc)
  225. {
  226. LM_ERR("wsconn_rm: null pointer\n");
  227. return -1;
  228. }
  229. if (run_event_route == WSCONN_EVENTROUTE_YES)
  230. wsconn_run_route(wsc);
  231. WSCONN_LOCK;
  232. /* Remove from the WebSocket used list */
  233. if (wsconn_used_list->head == wsc)
  234. wsconn_used_list->head = wsc->used_next;
  235. if (wsconn_used_list->tail == wsc)
  236. wsconn_used_list->tail = wsc->used_prev;
  237. if (wsc->used_prev)
  238. wsc->used_prev->used_next = wsc->used_next;
  239. if (wsc->used_next)
  240. wsc->used_next->used_prev = wsc->used_prev;
  241. _wsconn_rm(wsc);
  242. WSCONN_UNLOCK;
  243. return 0;
  244. }
  245. int wsconn_update(ws_connection_t *wsc)
  246. {
  247. if (!wsc)
  248. {
  249. LM_ERR("wsconn_update: null pointer\n");
  250. return -1;
  251. }
  252. WSCONN_LOCK;
  253. wsc->last_used = (int) time(NULL);
  254. if (wsconn_used_list->tail == wsc)
  255. /* Already at the end of the list */
  256. goto end;
  257. if (wsconn_used_list->head == wsc)
  258. wsconn_used_list->head = wsc->used_next;
  259. if (wsc->used_prev)
  260. wsc->used_prev->used_next = wsc->used_next;
  261. if (wsc->used_next)
  262. wsc->used_next->used_prev = wsc->used_prev;
  263. wsc->used_prev = wsconn_used_list->tail;
  264. wsc->used_next = NULL;
  265. wsconn_used_list->tail->used_next = wsc;
  266. wsconn_used_list->tail = wsc;
  267. end:
  268. WSCONN_UNLOCK;
  269. return 0;
  270. }
  271. void wsconn_close_now(ws_connection_t *wsc)
  272. {
  273. struct tcp_connection *con = tcpconn_get(wsc->id, 0, 0, 0, 0);
  274. if (wsconn_rm(wsc, WSCONN_EVENTROUTE_YES) < 0)
  275. LM_ERR("removing WebSocket connection\n");
  276. if (con == NULL)
  277. {
  278. LM_ERR("getting TCP/TLS connection\n");
  279. return;
  280. }
  281. tcpconn_put(con);
  282. con->send_flags.f |= SND_F_CON_CLOSE;
  283. con->state = S_CONN_BAD;
  284. con->timeout = get_ticks_raw();
  285. }
  286. ws_connection_t *wsconn_get(int id)
  287. {
  288. int id_hash = tcp_id_hash(id);
  289. ws_connection_t *wsc;
  290. WSCONN_LOCK;
  291. for (wsc = wsconn_id_hash[id_hash]; wsc; wsc = wsc->id_next)
  292. {
  293. if (wsc->id == id)
  294. {
  295. WSCONN_UNLOCK;
  296. return wsc;
  297. }
  298. }
  299. WSCONN_UNLOCK;
  300. return NULL;
  301. }
  302. static int add_node(struct mi_root *tree, ws_connection_t *wsc)
  303. {
  304. int interval;
  305. char *src_proto, *dst_proto, *pong;
  306. char src_ip[IP6_MAX_STR_SIZE + 1], dst_ip[IP6_MAX_STR_SIZE + 1];
  307. struct tcp_connection *con = tcpconn_get(wsc->id, 0, 0, 0, 0);
  308. if (con)
  309. {
  310. src_proto = (con->rcv.proto== PROTO_WS) ? "ws" : "wss";
  311. memset(src_ip, 0, IP6_MAX_STR_SIZE + 1);
  312. ip_addr2sbuf(&con->rcv.src_ip, src_ip, IP6_MAX_STR_SIZE);
  313. dst_proto = (con->rcv.proto == PROTO_WS) ? "ws" : "wss";
  314. memset(dst_ip, 0, IP6_MAX_STR_SIZE + 1);
  315. ip_addr2sbuf(&con->rcv.dst_ip, dst_ip, IP6_MAX_STR_SIZE);
  316. pong = wsc->awaiting_pong ? "awaiting Pong, " : "";
  317. interval = (int)time(NULL) - wsc->last_used;
  318. if (addf_mi_node_child(&tree->node, 0, 0, 0,
  319. "%d: %s:%s:%hu -> %s:%s:%hu (state: %s"
  320. ", %slast used %ds ago)",
  321. wsc->id,
  322. src_proto,
  323. strlen(src_ip) ? src_ip : "*",
  324. con->rcv.src_port,
  325. dst_proto,
  326. strlen(dst_ip) ? dst_ip : "*",
  327. con->rcv.dst_port,
  328. wsconn_state_str[wsc->state],
  329. pong,
  330. interval) == 0)
  331. {
  332. tcpconn_put(con);
  333. return -1;
  334. }
  335. tcpconn_put(con);
  336. return 1;
  337. }
  338. else
  339. return 0;
  340. }
  341. struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
  342. {
  343. int h, connections = 0, truncated = 0, order = 0, found = 0;
  344. ws_connection_t *wsc;
  345. struct mi_node *node = NULL;
  346. struct mi_root *rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  347. if (!rpl_tree)
  348. return 0;
  349. node = cmd->node.kids;
  350. if (node != NULL)
  351. {
  352. if (node->value.s == NULL || node->value.len == 0)
  353. {
  354. LM_WARN("empty display order parameter\n");
  355. return init_mi_tree(400, str_status_empty_param.s,
  356. str_status_empty_param.len);
  357. }
  358. strlower(&node->value);
  359. if (strncmp(node->value.s, "id_hash", 7) == 0)
  360. order = 0;
  361. else if (strncmp(node->value.s, "used_desc", 9) == 0)
  362. order = 1;
  363. else if (strncmp(node->value.s, "used_asc", 8) == 0)
  364. order = 2;
  365. else
  366. {
  367. LM_WARN("bad display order parameter\n");
  368. return init_mi_tree(400, str_status_bad_param.s,
  369. str_status_bad_param.len);
  370. }
  371. if (node->next != NULL)
  372. {
  373. LM_WARN("too many parameters\n");
  374. return init_mi_tree(400, str_status_too_many_params.s,
  375. str_status_too_many_params.len);
  376. }
  377. }
  378. WSCONN_LOCK;
  379. if (order == 0)
  380. {
  381. for (h = 0; h < TCP_ID_HASH_SIZE; h++)
  382. {
  383. wsc = wsconn_id_hash[h];
  384. while(wsc)
  385. {
  386. if ((found = add_node(rpl_tree, wsc)) < 0)
  387. return 0;
  388. connections += found;
  389. if (connections >= MAX_WS_CONNS_DUMP)
  390. {
  391. truncated = 1;
  392. break;
  393. }
  394. wsc = wsc->id_next;
  395. }
  396. if (truncated == 1)
  397. break;
  398. }
  399. }
  400. else if (order == 1)
  401. {
  402. wsc = wsconn_used_list->head;
  403. while (wsc)
  404. {
  405. if ((found = add_node(rpl_tree, wsc)) < 0)
  406. return 0;
  407. connections += found;
  408. if (connections >= MAX_WS_CONNS_DUMP)
  409. {
  410. truncated = 1;
  411. break;
  412. }
  413. wsc = wsc->used_next;
  414. }
  415. }
  416. else
  417. {
  418. wsc = wsconn_used_list->tail;
  419. while (wsc)
  420. {
  421. if ((found = add_node(rpl_tree, wsc)) < 0)
  422. return 0;
  423. connections += found;
  424. if (connections >= MAX_WS_CONNS_DUMP)
  425. {
  426. truncated = 1;
  427. break;
  428. }
  429. wsc = wsc->used_prev;
  430. }
  431. }
  432. WSCONN_UNLOCK;
  433. if (addf_mi_node_child(&rpl_tree->node, 0, 0, 0,
  434. "%d WebSocket connection%s found%s",
  435. connections, connections == 1 ? "" : "s",
  436. truncated == 1 ? "(truncated)" : "") == 0)
  437. return 0;
  438. return rpl_tree;
  439. }