ws_conn.c 16 KB

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