protocol_lws_status.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #if !defined (LWS_PLUGIN_STATIC)
  21. #define LWS_DLL
  22. #define LWS_INTERNAL
  23. #include <libwebsockets.h>
  24. #endif
  25. #include <time.h>
  26. #include <string.h>
  27. #ifdef WIN32
  28. #include <io.h>
  29. #include <gettimeofday.h>
  30. #endif
  31. typedef enum {
  32. WALK_NONE,
  33. WALK_INITIAL,
  34. WALK_LIST,
  35. WALK_FINAL
  36. } e_walk;
  37. struct per_session_data__lws_status {
  38. struct per_session_data__lws_status *next;
  39. struct lws *wsi;
  40. time_t time_est;
  41. char user_agent[256];
  42. e_walk walk;
  43. struct per_session_data__lws_status *walk_next;
  44. unsigned char subsequent:1;
  45. unsigned char changed_partway:1;
  46. unsigned char wss_over_h2:1;
  47. };
  48. struct per_vhost_data__lws_status {
  49. struct per_session_data__lws_status *live_pss_list;
  50. struct lws_context *context;
  51. struct lws_vhost *vhost;
  52. const struct lws_protocols *protocol;
  53. int count_live_pss;
  54. };
  55. static void
  56. trigger_resend(struct per_vhost_data__lws_status *vhd)
  57. {
  58. lws_start_foreach_ll(struct per_session_data__lws_status *, pss,
  59. vhd->live_pss_list) {
  60. if (pss->walk == WALK_NONE) {
  61. pss->subsequent = 0;
  62. pss->walk_next = vhd->live_pss_list;
  63. pss->walk = WALK_INITIAL;
  64. } else
  65. pss->changed_partway = 1;
  66. } lws_end_foreach_ll(pss, next);
  67. lws_callback_on_writable_all_protocol(vhd->context, vhd->protocol);
  68. }
  69. /* lws-status protocol */
  70. int
  71. callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason,
  72. void *user, void *in, size_t len)
  73. {
  74. struct per_session_data__lws_status *pss =
  75. (struct per_session_data__lws_status *)user;
  76. struct per_vhost_data__lws_status *vhd =
  77. (struct per_vhost_data__lws_status *)
  78. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  79. lws_get_protocol(wsi));
  80. char buf[LWS_PRE + 384], ip[24], *start = buf + LWS_PRE - 1, *p = start,
  81. *end = buf + sizeof(buf) - 1;
  82. int n, m;
  83. switch (reason) {
  84. case LWS_CALLBACK_PROTOCOL_INIT:
  85. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  86. lws_get_protocol(wsi),
  87. sizeof(struct per_vhost_data__lws_status));
  88. vhd->context = lws_get_context(wsi);
  89. vhd->protocol = lws_get_protocol(wsi);
  90. vhd->vhost = lws_get_vhost(wsi);
  91. break;
  92. case LWS_CALLBACK_ESTABLISHED:
  93. /*
  94. * This shows how to stage sending a single ws message in
  95. * multiple fragments. In this case, it lets us trade off
  96. * memory needed to make the data vs time to send it.
  97. */
  98. vhd->count_live_pss++;
  99. pss->next = vhd->live_pss_list;
  100. vhd->live_pss_list = pss;
  101. pss->wss_over_h2 = !!len;
  102. time(&pss->time_est);
  103. pss->wsi = wsi;
  104. #if defined(LWS_WITH_HTTP_UNCOMMON_HEADERS)
  105. if (lws_hdr_copy(wsi, pss->user_agent, sizeof(pss->user_agent),
  106. WSI_TOKEN_HTTP_USER_AGENT) < 0) /* too big */
  107. #endif
  108. strcpy(pss->user_agent, "unknown");
  109. trigger_resend(vhd);
  110. break;
  111. case LWS_CALLBACK_SERVER_WRITEABLE:
  112. switch (pss->walk) {
  113. case WALK_INITIAL:
  114. n = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;
  115. p += lws_snprintf(p, end - p,
  116. "{ \"version\":\"%s\","
  117. " \"wss_over_h2\":\"%d\","
  118. " \"hostname\":\"%s\","
  119. " \"wsi\":\"%d\", \"conns\":[",
  120. lws_get_library_version(),
  121. pss->wss_over_h2,
  122. lws_canonical_hostname(vhd->context),
  123. vhd->count_live_pss);
  124. pss->walk = WALK_LIST;
  125. pss->walk_next = vhd->live_pss_list;
  126. break;
  127. case WALK_LIST:
  128. n = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN;
  129. if (!pss->walk_next)
  130. goto walk_final;
  131. if (pss->subsequent)
  132. *p++ = ',';
  133. pss->subsequent = 1;
  134. m = 0;
  135. lws_start_foreach_ll(struct per_session_data__lws_status *,
  136. pss2, vhd->live_pss_list) {
  137. if (pss2 == pss->walk_next) {
  138. m = 1;
  139. break;
  140. }
  141. } lws_end_foreach_ll(pss2, next);
  142. if (!m) {
  143. /* our next guy went away */
  144. pss->walk = WALK_FINAL;
  145. pss->changed_partway = 1;
  146. break;
  147. }
  148. strcpy(ip, "unknown");
  149. lws_get_peer_simple(pss->walk_next->wsi, ip, sizeof(ip));
  150. p += lws_snprintf(p, end - p,
  151. "{\"peer\":\"%s\",\"time\":\"%ld\","
  152. "\"ua\":\"%s\"}",
  153. ip, (unsigned long)pss->walk_next->time_est,
  154. pss->walk_next->user_agent);
  155. pss->walk_next = pss->walk_next->next;
  156. if (!pss->walk_next)
  157. pss->walk = WALK_FINAL;
  158. break;
  159. case WALK_FINAL:
  160. walk_final:
  161. n = LWS_WRITE_CONTINUATION;
  162. p += lws_snprintf(p, 4, "]}");
  163. if (pss->changed_partway) {
  164. pss->changed_partway = 0;
  165. pss->subsequent = 0;
  166. pss->walk_next = vhd->live_pss_list;
  167. pss->walk = WALK_INITIAL;
  168. } else
  169. pss->walk = WALK_NONE;
  170. break;
  171. default:
  172. return 0;
  173. }
  174. m = lws_write(wsi, (unsigned char *)start, p - start, n);
  175. if (m < 0) {
  176. lwsl_err("ERROR %d writing to di socket\n", m);
  177. return -1;
  178. }
  179. if (pss->walk != WALK_NONE)
  180. lws_callback_on_writable(wsi);
  181. break;
  182. case LWS_CALLBACK_RECEIVE:
  183. lwsl_notice("pmd test: RX len %d\n", (int)len);
  184. break;
  185. case LWS_CALLBACK_CLOSED:
  186. // lwsl_debug("****** LWS_CALLBACK_CLOSED\n");
  187. lws_start_foreach_llp(struct per_session_data__lws_status **,
  188. ppss, vhd->live_pss_list) {
  189. if (*ppss == pss) {
  190. *ppss = pss->next;
  191. break;
  192. }
  193. } lws_end_foreach_llp(ppss, next);
  194. trigger_resend(vhd);
  195. break;
  196. default:
  197. break;
  198. }
  199. return 0;
  200. }
  201. #define LWS_PLUGIN_PROTOCOL_LWS_STATUS \
  202. { \
  203. "lws-status", \
  204. callback_lws_status, \
  205. sizeof(struct per_session_data__lws_status), \
  206. 512, /* rx buf size must be >= permessage-deflate rx size */ \
  207. 0, NULL, 0 \
  208. }
  209. #if !defined (LWS_PLUGIN_STATIC)
  210. static const struct lws_protocols protocols[] = {
  211. LWS_PLUGIN_PROTOCOL_LWS_STATUS
  212. };
  213. LWS_VISIBLE const lws_plugin_protocol_t lws_status = {
  214. .hdr = {
  215. "lws status",
  216. "lws_protocol_plugin",
  217. LWS_PLUGIN_API_MAGIC
  218. },
  219. .protocols = protocols,
  220. .count_protocols = LWS_ARRAY_SIZE(protocols),
  221. .extensions = NULL,
  222. .count_extensions = 0,
  223. };
  224. #endif