ws_conn.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 "../../locking.h"
  29. #include "../../str.h"
  30. #include "../../tcp_conn.h"
  31. #include "../../lib/kcore/faked_msg.h"
  32. #include "../../lib/kcore/kstats_wrapper.h"
  33. #include "../../lib/kmi/tree.h"
  34. #include "../../mem/mem.h"
  35. #include "ws_conn.h"
  36. #include "ws_mod.h"
  37. /* Maximum number of connections to display when using the ws.dump MI command */
  38. #define MAX_WS_CONNS_DUMP 50
  39. ws_connection_t **wsconn_id_hash = NULL;
  40. #define wsconn_listadd tcpconn_listadd
  41. #define wsconn_listrm tcpconn_listrm
  42. gen_lock_t *wsconn_lock = NULL;
  43. #define WSCONN_LOCK lock_get(wsconn_lock)
  44. #define WSCONN_UNLOCK lock_release(wsconn_lock)
  45. #define wsconn_ref(c) atomic_inc(&((c)->refcnt))
  46. #define wsconn_unref(c) atomic_dec_and_test(&((c)->refcnt))
  47. gen_lock_t *wsstat_lock = NULL;
  48. ws_connection_used_list_t *wsconn_used_list = NULL;
  49. stat_var *ws_current_connections;
  50. stat_var *ws_max_concurrent_connections;
  51. stat_var *ws_sip_current_connections;
  52. stat_var *ws_sip_max_concurrent_connections;
  53. stat_var *ws_msrp_current_connections;
  54. stat_var *ws_msrp_max_concurrent_connections;
  55. char *wsconn_state_str[] =
  56. {
  57. "CONNECTING", /* WS_S_CONNECTING */
  58. "OPEN", /* WS_S_OPEN */
  59. "CLOSING", /* WS_S_CLOSING */
  60. "CLOSED" /* WS_S_CLOSED */
  61. };
  62. /* MI command status text */
  63. static str str_status_empty_param = str_init("Empty display order parameter");
  64. static str str_status_bad_param = str_init("Bad display order parameter");
  65. static str str_status_too_many_params = str_init("Too many parameters");
  66. int wsconn_init(void)
  67. {
  68. wsconn_lock = lock_alloc();
  69. if (wsconn_lock == NULL)
  70. {
  71. LM_ERR("allocating lock\n");
  72. goto error;
  73. }
  74. if (lock_init(wsconn_lock) == 0)
  75. {
  76. LM_ERR("initialising lock\n");
  77. goto error;
  78. }
  79. wsstat_lock = lock_alloc();
  80. if (wsstat_lock == NULL)
  81. {
  82. LM_ERR("allocating lock\n");
  83. goto error;
  84. }
  85. if (lock_init(wsstat_lock) == NULL)
  86. {
  87. LM_ERR("initialising lock\n");
  88. goto error;
  89. }
  90. wsconn_id_hash =
  91. (ws_connection_t **) shm_malloc(TCP_ID_HASH_SIZE *
  92. sizeof(ws_connection_t*));
  93. if (wsconn_id_hash == NULL)
  94. {
  95. LM_ERR("allocating WebSocket hash-table\n");
  96. goto error;
  97. }
  98. memset((void *) wsconn_id_hash, 0,
  99. TCP_ID_HASH_SIZE * sizeof(ws_connection_t *));
  100. wsconn_used_list = (ws_connection_used_list_t *) shm_malloc(
  101. sizeof(ws_connection_used_list_t));
  102. if (wsconn_used_list == NULL)
  103. {
  104. LM_ERR("allocating WebSocket used list\n");
  105. goto error;
  106. }
  107. memset((void *) wsconn_used_list, 0, sizeof(ws_connection_used_list_t));
  108. return 0;
  109. error:
  110. if (wsconn_lock) lock_dealloc((void *) wsconn_lock);
  111. if (wsstat_lock) lock_dealloc((void *) wsstat_lock);
  112. wsconn_lock = wsstat_lock = NULL;
  113. if (wsconn_id_hash) shm_free(wsconn_id_hash);
  114. if (wsconn_used_list) shm_free(wsconn_used_list);
  115. wsconn_id_hash = NULL;
  116. wsconn_used_list = NULL;
  117. return -1;
  118. }
  119. static inline void _wsconn_rm(ws_connection_t *wsc)
  120. {
  121. wsconn_listrm(wsconn_id_hash[wsc->id_hash], wsc, id_next, id_prev);
  122. update_stat(ws_current_connections, -1);
  123. if (wsc->sub_protocol == SUB_PROTOCOL_SIP)
  124. update_stat(ws_sip_current_connections, -1);
  125. else if (wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  126. update_stat(ws_msrp_current_connections, -1);
  127. shm_free(wsc);
  128. }
  129. void wsconn_destroy(void)
  130. {
  131. int h;
  132. if (wsconn_used_list)
  133. {
  134. shm_free(wsconn_used_list);
  135. wsconn_used_list = NULL;
  136. }
  137. if (wsconn_id_hash)
  138. {
  139. WSCONN_UNLOCK;
  140. WSCONN_LOCK;
  141. for (h = 0; h < TCP_ID_HASH_SIZE; h++)
  142. {
  143. ws_connection_t *wsc = wsconn_id_hash[h];
  144. while (wsc)
  145. {
  146. ws_connection_t *next = wsc->id_next;
  147. _wsconn_rm(wsc);
  148. wsc = next;
  149. }
  150. }
  151. WSCONN_UNLOCK;
  152. shm_free(wsconn_id_hash);
  153. wsconn_id_hash = NULL;
  154. }
  155. if (wsconn_lock)
  156. {
  157. lock_destroy(wsconn_lock);
  158. lock_dealloc((void *) wsconn_lock);
  159. wsconn_lock = NULL;
  160. }
  161. if (wsstat_lock)
  162. {
  163. lock_destroy(wsstat_lock);
  164. lock_dealloc((void *) wsstat_lock);
  165. wsstat_lock = NULL;
  166. }
  167. }
  168. int wsconn_add(struct receive_info rcv, unsigned int sub_protocol)
  169. {
  170. int cur_cons, max_cons;
  171. int id = rcv.proto_reserved1;
  172. int id_hash = tcp_id_hash(id);
  173. ws_connection_t *wsc;
  174. LM_DBG("wsconn_add id [%d]\n", id);
  175. /* Allocate and fill in new WebSocket connection */
  176. wsc = shm_malloc(sizeof(ws_connection_t));
  177. if (wsc == NULL)
  178. {
  179. LM_ERR("allocating shared memory\n");
  180. return -1;
  181. }
  182. memset(wsc, 0, sizeof(ws_connection_t));
  183. wsc->id = id;
  184. wsc->id_hash = id_hash;
  185. wsc->state = WS_S_OPEN;
  186. wsc->rcv = rcv;
  187. wsc->sub_protocol = sub_protocol;
  188. wsc->run_event = 0;
  189. atomic_set(&wsc->refcnt, 0);
  190. LM_DBG("wsconn_add new wsc => [%p], ref => [%d]\n", wsc, atomic_get(&wsc->refcnt));
  191. WSCONN_LOCK;
  192. /* Add to WebSocket connection table */
  193. wsconn_listadd(wsconn_id_hash[wsc->id_hash], wsc, id_next, id_prev);
  194. /* Add to the end of the WebSocket used list */
  195. wsc->last_used = (int)time(NULL);
  196. if (wsconn_used_list->head == NULL)
  197. wsconn_used_list->head = wsconn_used_list->tail = wsc;
  198. else
  199. {
  200. wsc->used_prev = wsconn_used_list->tail;
  201. wsconn_used_list->tail->used_next = wsc;
  202. wsconn_used_list->tail = wsc;
  203. }
  204. wsconn_ref(wsc);
  205. WSCONN_UNLOCK;
  206. LM_DBG("wsconn_add added to conn_table wsc => [%p], ref => [%d]\n", wsc, atomic_get(&wsc->refcnt));
  207. /* Update connection statistics */
  208. lock_get(wsstat_lock);
  209. update_stat(ws_current_connections, 1);
  210. cur_cons = get_stat_val(ws_current_connections);
  211. max_cons = get_stat_val(ws_max_concurrent_connections);
  212. if (max_cons < cur_cons)
  213. update_stat(ws_max_concurrent_connections, cur_cons - max_cons);
  214. if (wsc->sub_protocol == SUB_PROTOCOL_SIP)
  215. {
  216. update_stat(ws_sip_current_connections, 1);
  217. cur_cons = get_stat_val(ws_sip_current_connections);
  218. max_cons = get_stat_val(ws_sip_max_concurrent_connections);
  219. if (max_cons < cur_cons)
  220. update_stat(ws_sip_max_concurrent_connections,
  221. cur_cons - max_cons);
  222. }
  223. else if (wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  224. {
  225. update_stat(ws_msrp_current_connections, 1);
  226. cur_cons = get_stat_val(ws_msrp_current_connections);
  227. max_cons = get_stat_val(ws_msrp_max_concurrent_connections);
  228. if (max_cons < cur_cons)
  229. update_stat(ws_msrp_max_concurrent_connections,
  230. cur_cons - max_cons);
  231. }
  232. lock_release(wsstat_lock);
  233. return 0;
  234. }
  235. static void wsconn_run_route(ws_connection_t *wsc)
  236. {
  237. int rt, backup_rt;
  238. struct run_act_ctx ctx;
  239. sip_msg_t *fmsg;
  240. LM_DBG("wsconn_run_route event_route[websocket:closed]\n");
  241. rt = route_get(&event_rt, "websocket:closed");
  242. if (rt < 0 || event_rt.rlist[rt] == NULL)
  243. {
  244. LM_DBG("route does not exist");
  245. return;
  246. }
  247. if (faked_msg_init() < 0)
  248. {
  249. LM_ERR("faked_msg_init() failed\n");
  250. return;
  251. }
  252. fmsg = faked_msg_next();
  253. fmsg->rcv = wsc->rcv;
  254. backup_rt = get_route_type();
  255. set_route_type(REQUEST_ROUTE);
  256. init_run_actions_ctx(&ctx);
  257. run_top_route(event_rt.rlist[rt], fmsg, 0);
  258. set_route_type(backup_rt);
  259. }
  260. static void wsconn_dtor(ws_connection_t *wsc)
  261. {
  262. if (!wsc)
  263. return;
  264. LM_DBG("wsconn_dtor for [%p] refcnt [%d]\n", wsc, atomic_get(&wsc->refcnt));
  265. if (wsc->run_event)
  266. wsconn_run_route(wsc);
  267. shm_free(wsc);
  268. LM_DBG("wsconn_dtor for [%p] destroyed\n", wsc);
  269. }
  270. int wsconn_rm(ws_connection_t *wsc, ws_conn_eventroute_t run_event_route)
  271. {
  272. LM_DBG("wsconn_rm for [%p] refcnt [%d]\n", wsc, atomic_get(&wsc->refcnt));
  273. if (run_event_route == WSCONN_EVENTROUTE_YES)
  274. wsc->run_event = 1;
  275. return wsconn_put(wsc);
  276. }
  277. int wsconn_update(ws_connection_t *wsc)
  278. {
  279. if (!wsc)
  280. {
  281. LM_ERR("wsconn_update: null pointer\n");
  282. return -1;
  283. }
  284. WSCONN_LOCK;
  285. wsc->last_used = (int) time(NULL);
  286. if (wsconn_used_list->tail == wsc)
  287. /* Already at the end of the list */
  288. goto end;
  289. if (wsconn_used_list->head == wsc)
  290. wsconn_used_list->head = wsc->used_next;
  291. if (wsc->used_prev)
  292. wsc->used_prev->used_next = wsc->used_next;
  293. if (wsc->used_next)
  294. wsc->used_next->used_prev = wsc->used_prev;
  295. wsc->used_prev = wsconn_used_list->tail;
  296. wsc->used_next = NULL;
  297. wsconn_used_list->tail->used_next = wsc;
  298. wsconn_used_list->tail = wsc;
  299. end:
  300. WSCONN_UNLOCK;
  301. return 0;
  302. }
  303. void wsconn_close_now(ws_connection_t *wsc)
  304. {
  305. struct tcp_connection *con = tcpconn_get(wsc->id, 0, 0, 0, 0);
  306. if (wsconn_rm(wsc, WSCONN_EVENTROUTE_YES) < 0)
  307. LM_ERR("removing WebSocket connection\n");
  308. if (con == NULL)
  309. {
  310. LM_ERR("getting TCP/TLS connection\n");
  311. return;
  312. }
  313. tcpconn_put(con);
  314. con->send_flags.f |= SND_F_CON_CLOSE;
  315. con->state = S_CONN_BAD;
  316. con->timeout = get_ticks_raw();
  317. }
  318. /* must be called with unlocked WSCONN_LOCK */
  319. int wsconn_put(ws_connection_t *wsc)
  320. {
  321. int destroy = 0;
  322. LM_DBG("wsconn_put start for [%p] refcnt [%d]\n", wsc, atomic_get(&wsc->refcnt));
  323. if (!wsc)
  324. return -1;
  325. WSCONN_LOCK;
  326. /* refcnt == 0*/
  327. if (wsconn_unref(wsc))
  328. {
  329. /* Remove from the WebSocket used list */
  330. if (wsconn_used_list->head == wsc)
  331. wsconn_used_list->head = wsc->used_next;
  332. if (wsconn_used_list->tail == wsc)
  333. wsconn_used_list->tail = wsc->used_prev;
  334. if (wsc->used_prev)
  335. wsc->used_prev->used_next = wsc->used_next;
  336. if (wsc->used_next)
  337. wsc->used_next->used_prev = wsc->used_prev;
  338. /* remove from wsconn_id_hash */
  339. wsconn_listrm(wsconn_id_hash[wsc->id_hash], wsc, id_next, id_prev);
  340. /* stat */
  341. update_stat(ws_current_connections, -1);
  342. if (wsc->sub_protocol == SUB_PROTOCOL_SIP)
  343. update_stat(ws_sip_current_connections, -1);
  344. else if (wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  345. update_stat(ws_msrp_current_connections, -1);
  346. destroy = 1;
  347. }
  348. WSCONN_UNLOCK;
  349. LM_DBG("wsconn_put end for [%p] refcnt [%d]\n", wsc, atomic_get(&wsc->refcnt));
  350. /* wsc is removed from all lists and can be destroyed safely */
  351. if (destroy)
  352. wsconn_dtor(wsc);
  353. return 0;
  354. }
  355. ws_connection_t *wsconn_get(int id)
  356. {
  357. int id_hash = tcp_id_hash(id);
  358. ws_connection_t *wsc;
  359. LM_DBG("wsconn_get for id [%d]\n", id);
  360. WSCONN_LOCK;
  361. for (wsc = wsconn_id_hash[id_hash]; wsc; wsc = wsc->id_next)
  362. {
  363. if (wsc->id == id)
  364. {
  365. wsconn_ref(wsc);
  366. LM_DBG("wsconn_get returns wsc [%p] refcnt [%d]\n", wsc, atomic_get(&wsc->refcnt));
  367. WSCONN_UNLOCK;
  368. return wsc;
  369. }
  370. }
  371. WSCONN_UNLOCK;
  372. return NULL;
  373. }
  374. ws_connection_t **wsconn_get_list(void)
  375. {
  376. ws_connection_t **list = NULL;
  377. ws_connection_t *wsc = NULL;
  378. size_t list_size = 0;
  379. size_t list_len = 0;
  380. size_t i = 0;
  381. LM_DBG("wsconn_get_list\n");
  382. WSCONN_LOCK;
  383. /* get the number of used connections */
  384. wsc = wsconn_used_list->head;
  385. while (wsc)
  386. {
  387. LM_DBG("counter wsc [%p] prev => [%p] next => [%p]\n", wsc, wsc->used_prev, wsc->used_next);
  388. list_len++;
  389. wsc = wsc->used_next;
  390. }
  391. if (!list_len)
  392. goto end;
  393. /* allocate a NULL terminated list of wsconn pointers */
  394. list_size = (list_len + 1) * sizeof(ws_connection_t *);
  395. list = pkg_malloc(list_size);
  396. if (!list)
  397. goto end;
  398. memset(list, 0, list_size);
  399. /* copy */
  400. wsc = wsconn_used_list->head;
  401. for(i = 0; i < list_len; i++)
  402. {
  403. if (!wsc) {
  404. LM_ERR("Wrong list length\n");
  405. break;
  406. }
  407. list[i] = wsc;
  408. wsconn_ref(wsc);
  409. LM_DBG("wsc [%p] id [%d] ref++\n", wsc, wsc->id);
  410. wsc = wsc->used_next;
  411. }
  412. list[i] = NULL; /* explicit NULL termination */
  413. end:
  414. WSCONN_UNLOCK;
  415. LM_DBG("wsconn_get_list returns list [%p] with [%d] members\n", list, (int)list_len);
  416. return list;
  417. }
  418. int wsconn_put_list(ws_connection_t **list_head)
  419. {
  420. ws_connection_t **list = NULL;
  421. ws_connection_t *wsc = NULL;
  422. LM_DBG("wsconn_put_list [%p]\n", list_head);
  423. if (!list_head)
  424. return -1;
  425. list = list_head;
  426. wsc = *list_head;
  427. while (wsc)
  428. {
  429. wsconn_put(wsc);
  430. wsc = *(++list);
  431. }
  432. pkg_free(list_head);
  433. return 0;
  434. }
  435. static int add_node(struct mi_root *tree, ws_connection_t *wsc)
  436. {
  437. int interval;
  438. char *src_proto, *dst_proto, *pong, *sub_protocol;
  439. char src_ip[IP6_MAX_STR_SIZE + 1], dst_ip[IP6_MAX_STR_SIZE + 1];
  440. struct tcp_connection *con = tcpconn_get(wsc->id, 0, 0, 0, 0);
  441. if (con)
  442. {
  443. src_proto = (con->rcv.proto== PROTO_WS) ? "ws" : "wss";
  444. memset(src_ip, 0, IP6_MAX_STR_SIZE + 1);
  445. ip_addr2sbuf(&con->rcv.src_ip, src_ip, IP6_MAX_STR_SIZE);
  446. dst_proto = (con->rcv.proto == PROTO_WS) ? "ws" : "wss";
  447. memset(dst_ip, 0, IP6_MAX_STR_SIZE + 1);
  448. ip_addr2sbuf(&con->rcv.dst_ip, dst_ip, IP6_MAX_STR_SIZE);
  449. pong = wsc->awaiting_pong ? "awaiting Pong, " : "";
  450. interval = (int)time(NULL) - wsc->last_used;
  451. if (wsc->sub_protocol == SUB_PROTOCOL_SIP)
  452. sub_protocol = "sip";
  453. else if (wsc->sub_protocol == SUB_PROTOCOL_MSRP)
  454. sub_protocol = "msrp";
  455. else
  456. sub_protocol = "**UNKNOWN**";
  457. if (addf_mi_node_child(&tree->node, 0, 0, 0,
  458. "%d: %s:%s:%hu -> %s:%s:%hu (state: %s"
  459. ", %s last used %ds ago"
  460. ", sub-protocol: %s)",
  461. wsc->id,
  462. src_proto,
  463. strlen(src_ip) ? src_ip : "*",
  464. con->rcv.src_port,
  465. dst_proto,
  466. strlen(dst_ip) ? dst_ip : "*",
  467. con->rcv.dst_port,
  468. wsconn_state_str[wsc->state],
  469. pong,
  470. interval,
  471. sub_protocol) == 0)
  472. {
  473. tcpconn_put(con);
  474. return -1;
  475. }
  476. tcpconn_put(con);
  477. return 1;
  478. }
  479. else
  480. return 0;
  481. }
  482. struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
  483. {
  484. int h, connections = 0, truncated = 0, order = 0, found = 0;
  485. ws_connection_t *wsc;
  486. struct mi_node *node = NULL;
  487. struct mi_root *rpl_tree;
  488. node = cmd->node.kids;
  489. if (node != NULL)
  490. {
  491. if (node->value.s == NULL || node->value.len == 0)
  492. {
  493. LM_WARN("empty display order parameter\n");
  494. return init_mi_tree(400, str_status_empty_param.s,
  495. str_status_empty_param.len);
  496. }
  497. strlower(&node->value);
  498. if (strncmp(node->value.s, "id_hash", 7) == 0)
  499. order = 0;
  500. else if (strncmp(node->value.s, "used_desc", 9) == 0)
  501. order = 1;
  502. else if (strncmp(node->value.s, "used_asc", 8) == 0)
  503. order = 2;
  504. else
  505. {
  506. LM_WARN("bad display order parameter\n");
  507. return init_mi_tree(400, str_status_bad_param.s,
  508. str_status_bad_param.len);
  509. }
  510. if (node->next != NULL)
  511. {
  512. LM_WARN("too many parameters\n");
  513. return init_mi_tree(400, str_status_too_many_params.s,
  514. str_status_too_many_params.len);
  515. }
  516. }
  517. rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  518. if (rpl_tree == NULL)
  519. return 0;
  520. WSCONN_LOCK;
  521. if (order == 0)
  522. {
  523. for (h = 0; h < TCP_ID_HASH_SIZE; h++)
  524. {
  525. wsc = wsconn_id_hash[h];
  526. while(wsc)
  527. {
  528. if ((found = add_node(rpl_tree, wsc)) < 0)
  529. {
  530. free_mi_tree(rpl_tree);
  531. return 0;
  532. }
  533. connections += found;
  534. if (connections >= MAX_WS_CONNS_DUMP)
  535. {
  536. truncated = 1;
  537. break;
  538. }
  539. wsc = wsc->id_next;
  540. }
  541. if (truncated == 1)
  542. break;
  543. }
  544. }
  545. else if (order == 1)
  546. {
  547. wsc = wsconn_used_list->head;
  548. while (wsc)
  549. {
  550. if ((found = add_node(rpl_tree, wsc)) < 0)
  551. {
  552. free_mi_tree(rpl_tree);
  553. return 0;
  554. }
  555. connections += found;
  556. if (connections >= MAX_WS_CONNS_DUMP)
  557. {
  558. truncated = 1;
  559. break;
  560. }
  561. wsc = wsc->used_next;
  562. }
  563. }
  564. else
  565. {
  566. wsc = wsconn_used_list->tail;
  567. while (wsc)
  568. {
  569. if ((found = add_node(rpl_tree, wsc)) < 0)
  570. {
  571. free_mi_tree(rpl_tree);
  572. return 0;
  573. }
  574. connections += found;
  575. if (connections >= MAX_WS_CONNS_DUMP)
  576. {
  577. truncated = 1;
  578. break;
  579. }
  580. wsc = wsc->used_prev;
  581. }
  582. }
  583. WSCONN_UNLOCK;
  584. if (addf_mi_node_child(&rpl_tree->node, 0, 0, 0,
  585. "%d WebSocket connection%s found%s",
  586. connections, connections == 1 ? "" : "s",
  587. truncated == 1 ? "(truncated)" : "") == 0)
  588. {
  589. free_mi_tree(rpl_tree);
  590. return 0;
  591. }
  592. return rpl_tree;
  593. }