peer-limits.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <libwebsockets.h>
  25. #include "private-lib-core.h"
  26. /* requires context->lock */
  27. static void
  28. __lws_peer_remove_from_peer_wait_list(struct lws_context *context,
  29. struct lws_peer *peer)
  30. {
  31. struct lws_peer *df;
  32. lws_start_foreach_llp(struct lws_peer **, p, context->peer_wait_list) {
  33. if (*p == peer) {
  34. df = *p;
  35. *p = df->peer_wait_list;
  36. df->peer_wait_list = NULL;
  37. return;
  38. }
  39. } lws_end_foreach_llp(p, peer_wait_list);
  40. }
  41. /* requires context->lock */
  42. static void
  43. __lws_peer_add_to_peer_wait_list(struct lws_context *context,
  44. struct lws_peer *peer)
  45. {
  46. __lws_peer_remove_from_peer_wait_list(context, peer);
  47. peer->peer_wait_list = context->peer_wait_list;
  48. context->peer_wait_list = peer;
  49. }
  50. struct lws_peer *
  51. lws_get_or_create_peer(struct lws_vhost *vhost, lws_sockfd_type sockfd)
  52. {
  53. struct lws_context *context = vhost->context;
  54. struct lws_peer *peer;
  55. lws_sockaddr46 sa46;
  56. socklen_t rlen = 0;
  57. uint32_t hash = 0;
  58. uint8_t *q8;
  59. void *q;
  60. int n;
  61. if (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
  62. return NULL;
  63. rlen = sizeof(sa46);
  64. if (getpeername(sockfd, (struct sockaddr*)&sa46, &rlen))
  65. /* eg, udp doesn't have to have a peer */
  66. return NULL;
  67. #ifdef LWS_WITH_IPV6
  68. if (sa46.sa4.sin_family == AF_INET6) {
  69. q = &sa46.sa6.sin6_addr;
  70. rlen = sizeof(sa46.sa6.sin6_addr);
  71. } else
  72. #endif
  73. {
  74. q = &sa46.sa4.sin_addr;
  75. rlen = sizeof(sa46.sa4.sin_addr);
  76. }
  77. q8 = q;
  78. for (n = 0; n < (int)rlen; n++)
  79. hash = (((hash << 4) | (hash >> 28)) * n) ^ q8[n];
  80. hash = hash % context->pl_hash_elements;
  81. lws_context_lock(context, "peer search"); /* <======================= */
  82. lws_start_foreach_ll(struct lws_peer *, peerx,
  83. context->pl_hash_table[hash]) {
  84. if (peerx->sa46.sa4.sin_family == sa46.sa4.sin_family) {
  85. #if defined(LWS_WITH_IPV6)
  86. if (sa46.sa4.sin_family == AF_INET6 &&
  87. !memcmp(q, &peerx->sa46.sa6.sin6_addr, rlen))
  88. goto hit;
  89. #endif
  90. if (sa46.sa4.sin_family == AF_INET &&
  91. !memcmp(q, &peerx->sa46.sa4.sin_addr, rlen)) {
  92. #if defined(LWS_WITH_IPV6)
  93. hit:
  94. #endif
  95. lws_context_unlock(context); /* === */
  96. return peerx;
  97. }
  98. }
  99. } lws_end_foreach_ll(peerx, next);
  100. lwsl_info("%s: creating new peer\n", __func__);
  101. peer = lws_zalloc(sizeof(*peer), "peer");
  102. if (!peer) {
  103. lws_context_unlock(context); /* === */
  104. lwsl_err("%s: OOM for new peer\n", __func__);
  105. return NULL;
  106. }
  107. context->count_peers++;
  108. peer->next = context->pl_hash_table[hash];
  109. peer->hash = hash;
  110. peer->sa46 = sa46;
  111. context->pl_hash_table[hash] = peer;
  112. time(&peer->time_created);
  113. /*
  114. * On creation, the peer has no wsi attached, so is created on the
  115. * wait list. When a wsi is added it is removed from the wait list.
  116. */
  117. time(&peer->time_closed_all);
  118. __lws_peer_add_to_peer_wait_list(context, peer);
  119. lws_context_unlock(context); /* ====================================> */
  120. return peer;
  121. }
  122. /* requires context->lock */
  123. static int
  124. __lws_peer_destroy(struct lws_context *context, struct lws_peer *peer)
  125. {
  126. lws_start_foreach_llp(struct lws_peer **, p,
  127. context->pl_hash_table[peer->hash]) {
  128. if (*p == peer) {
  129. struct lws_peer *df = *p;
  130. *p = df->next;
  131. lws_free(df);
  132. context->count_peers--;
  133. return 0;
  134. }
  135. } lws_end_foreach_llp(p, next);
  136. return 1;
  137. }
  138. void
  139. lws_peer_cull_peer_wait_list(struct lws_context *context)
  140. {
  141. struct lws_peer *df;
  142. time_t t;
  143. time(&t);
  144. if (context->next_cull && t < context->next_cull)
  145. return;
  146. lws_context_lock(context, "peer cull"); /* <========================= */
  147. context->next_cull = t + 5;
  148. lws_start_foreach_llp(struct lws_peer **, p, context->peer_wait_list) {
  149. if (t - (*p)->time_closed_all > 10) {
  150. df = *p;
  151. /* remove us from the peer wait list */
  152. *p = df->peer_wait_list;
  153. df->peer_wait_list = NULL;
  154. __lws_peer_destroy(context, df);
  155. continue; /* we already point to next, if any */
  156. }
  157. } lws_end_foreach_llp(p, peer_wait_list);
  158. lws_context_unlock(context); /* ====================================> */
  159. }
  160. void
  161. lws_peer_add_wsi(struct lws_context *context, struct lws_peer *peer,
  162. struct lws *wsi)
  163. {
  164. if (!peer)
  165. return;
  166. lws_context_lock(context, "peer add"); /* <========================== */
  167. peer->count_wsi++;
  168. wsi->peer = peer;
  169. __lws_peer_remove_from_peer_wait_list(context, peer);
  170. lws_context_unlock(context); /* ====================================> */
  171. }
  172. void
  173. lws_peer_dump_from_wsi(struct lws *wsi)
  174. {
  175. struct lws_peer *peer;
  176. if (!wsi || !wsi->peer)
  177. return;
  178. peer = wsi->peer;
  179. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  180. lwsl_notice("%s: wsi %p: created %llu: wsi: %d/%d, ah %d/%d\n",
  181. __func__,
  182. wsi, (unsigned long long)peer->time_created,
  183. peer->count_wsi, peer->total_wsi,
  184. peer->http.count_ah, peer->http.total_ah);
  185. #else
  186. lwsl_notice("%s: wsi %p: created %llu: wsi: %d/%d\n", __func__,
  187. wsi, (unsigned long long)peer->time_created,
  188. peer->count_wsi, peer->total_wsi);
  189. #endif
  190. }
  191. void
  192. lws_peer_track_wsi_close(struct lws_context *context, struct lws_peer *peer)
  193. {
  194. if (!peer)
  195. return;
  196. lws_context_lock(context, "peer wsi close"); /* <==================== */
  197. assert(peer->count_wsi);
  198. peer->count_wsi--;
  199. if (!peer->count_wsi
  200. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  201. && !peer->http.count_ah
  202. #endif
  203. ) {
  204. /*
  205. * in order that we can accumulate peer activity correctly
  206. * allowing for periods when the peer has no connections,
  207. * we don't synchronously destroy the peer when his last
  208. * wsi closes. Instead we mark the time his last wsi
  209. * closed and add him to a peer_wait_list to be reaped
  210. * later if no further activity is coming.
  211. */
  212. time(&peer->time_closed_all);
  213. __lws_peer_add_to_peer_wait_list(context, peer);
  214. }
  215. lws_context_unlock(context); /* ====================================> */
  216. }
  217. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  218. int
  219. lws_peer_confirm_ah_attach_ok(struct lws_context *context,
  220. struct lws_peer *peer)
  221. {
  222. if (!peer)
  223. return 0;
  224. if (context->ip_limit_ah &&
  225. peer->http.count_ah >= context->ip_limit_ah) {
  226. lwsl_info("peer reached ah limit %d, deferring\n",
  227. context->ip_limit_ah);
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. void
  233. lws_peer_track_ah_detach(struct lws_context *context, struct lws_peer *peer)
  234. {
  235. if (!peer)
  236. return;
  237. lws_context_lock(context, "peer ah detach"); /* <==================== */
  238. assert(peer->http.count_ah);
  239. peer->http.count_ah--;
  240. lws_context_unlock(context); /* ====================================> */
  241. }
  242. #endif