test-fraggle.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * libwebsockets-test-fraggle - random fragmentation test
  3. *
  4. * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
  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. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <getopt.h>
  23. #include <string.h>
  24. #include "../lib/libwebsockets.h"
  25. #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
  26. static int client;
  27. static int terminate;
  28. enum demo_protocols {
  29. PROTOCOL_FRAGGLE,
  30. /* always last */
  31. DEMO_PROTOCOL_COUNT
  32. };
  33. /* fraggle protocol */
  34. enum fraggle_states {
  35. FRAGSTATE_START_MESSAGE,
  36. FRAGSTATE_RANDOM_PAYLOAD,
  37. FRAGSTATE_POST_PAYLOAD_SUM,
  38. };
  39. struct per_session_data__fraggle {
  40. int packets_left;
  41. int total_message;
  42. unsigned long sum;
  43. enum fraggle_states state;
  44. };
  45. static int
  46. callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason,
  47. void *user, void *in, size_t len)
  48. {
  49. int n;
  50. unsigned char buf[LWS_PRE + 8000];
  51. struct per_session_data__fraggle *psf = user;
  52. int chunk;
  53. int write_mode = LWS_WRITE_CONTINUATION;
  54. unsigned long sum;
  55. unsigned char *p = (unsigned char *)in;
  56. unsigned char *bp = &buf[LWS_PRE];
  57. int ran;
  58. switch (reason) {
  59. case LWS_CALLBACK_ESTABLISHED:
  60. fprintf(stderr, "server sees client connect\n");
  61. psf->state = FRAGSTATE_START_MESSAGE;
  62. /* start the ball rolling */
  63. lws_callback_on_writable(wsi);
  64. break;
  65. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  66. fprintf(stderr, "client connects to server\n");
  67. psf->state = FRAGSTATE_START_MESSAGE;
  68. break;
  69. case LWS_CALLBACK_CLIENT_RECEIVE:
  70. switch (psf->state) {
  71. case FRAGSTATE_START_MESSAGE:
  72. psf->state = FRAGSTATE_RANDOM_PAYLOAD;
  73. psf->sum = 0;
  74. psf->total_message = 0;
  75. psf->packets_left = 0;
  76. /* fallthru */
  77. case FRAGSTATE_RANDOM_PAYLOAD:
  78. for (n = 0; (unsigned int)n < len; n++)
  79. psf->sum += p[n];
  80. psf->total_message += len;
  81. psf->packets_left++;
  82. if (lws_is_final_fragment(wsi))
  83. psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
  84. break;
  85. case FRAGSTATE_POST_PAYLOAD_SUM:
  86. sum = ((unsigned int)p[0]) << 24;
  87. sum |= p[1] << 16;
  88. sum |= p[2] << 8;
  89. sum |= p[3];
  90. if (sum == psf->sum)
  91. fprintf(stderr, "EOM received %d correctly "
  92. "from %d fragments\n",
  93. psf->total_message, psf->packets_left);
  94. else
  95. fprintf(stderr, "**** ERROR at EOM: "
  96. "length %d, rx sum = 0x%lX, "
  97. "server says it sent 0x%lX\n",
  98. psf->total_message, psf->sum, sum);
  99. psf->state = FRAGSTATE_START_MESSAGE;
  100. break;
  101. }
  102. break;
  103. case LWS_CALLBACK_SERVER_WRITEABLE:
  104. switch (psf->state) {
  105. case FRAGSTATE_START_MESSAGE:
  106. lws_get_random(lws_get_context(wsi), &ran, sizeof(ran));
  107. psf->packets_left = (ran & 1023) + 1;
  108. fprintf(stderr, "Spamming %d random fragments\n",
  109. psf->packets_left);
  110. psf->sum = 0;
  111. psf->total_message = 0;
  112. write_mode = LWS_WRITE_BINARY;
  113. psf->state = FRAGSTATE_RANDOM_PAYLOAD;
  114. /* fallthru */
  115. case FRAGSTATE_RANDOM_PAYLOAD:
  116. /*
  117. * note how one chunk can be 8000, but we use the
  118. * default rx buffer size of 4096, so we exercise the
  119. * code for rx spill because the rx buffer is full
  120. */
  121. lws_get_random(lws_get_context(wsi), &ran, sizeof(ran));
  122. chunk = (ran & 511) + 1;
  123. psf->total_message += chunk;
  124. lws_get_random(lws_get_context(wsi), bp, chunk);
  125. for (n = 0; n < chunk; n++)
  126. psf->sum += bp[n];
  127. psf->packets_left--;
  128. if (psf->packets_left)
  129. write_mode |= LWS_WRITE_NO_FIN;
  130. else
  131. psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
  132. n = lws_write(wsi, bp, chunk, write_mode);
  133. if (n < 0)
  134. return -1;
  135. if (n < chunk) {
  136. lwsl_err("Partial write\n");
  137. return -1;
  138. }
  139. lws_callback_on_writable(wsi);
  140. break;
  141. case FRAGSTATE_POST_PAYLOAD_SUM:
  142. fprintf(stderr, "Spamming session over, "
  143. "len = %d. sum = 0x%lX\n",
  144. psf->total_message, psf->sum);
  145. bp[0] = psf->sum >> 24;
  146. bp[1] = (unsigned char)(psf->sum >> 16);
  147. bp[2] = (unsigned char)(psf->sum >> 8);
  148. bp[3] = (unsigned char)psf->sum;
  149. n = lws_write(wsi, (unsigned char *)bp,
  150. 4, LWS_WRITE_BINARY);
  151. if (n < 0)
  152. return -1;
  153. if (n < 4) {
  154. lwsl_err("Partial write\n");
  155. return -1;
  156. }
  157. psf->state = FRAGSTATE_START_MESSAGE;
  158. lws_callback_on_writable(wsi);
  159. break;
  160. }
  161. break;
  162. case LWS_CALLBACK_CLOSED:
  163. terminate = 1;
  164. break;
  165. /* because we are protocols[0] ... */
  166. case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
  167. if (strcmp(in, "deflate-stream") == 0) {
  168. fprintf(stderr, "denied deflate-stream extension\n");
  169. return 1;
  170. }
  171. break;
  172. default:
  173. break;
  174. }
  175. return 0;
  176. }
  177. /* list of supported protocols and callbacks */
  178. static struct lws_protocols protocols[] = {
  179. {
  180. "fraggle-protocol",
  181. callback_fraggle,
  182. sizeof(struct per_session_data__fraggle),
  183. },
  184. {
  185. NULL, NULL, 0 /* End of list */
  186. }
  187. };
  188. static const struct lws_extension exts[] = {
  189. {
  190. "permessage-deflate",
  191. lws_extension_callback_pm_deflate,
  192. "permessage-deflate; client_no_context_takeover; client_max_window_bits"
  193. },
  194. {
  195. "deflate-frame",
  196. lws_extension_callback_pm_deflate,
  197. "deflate_frame"
  198. },
  199. { NULL, NULL, NULL /* terminator */ }
  200. };
  201. static struct option options[] = {
  202. { "help", no_argument, NULL, 'h' },
  203. { "debug", required_argument, NULL, 'd' },
  204. { "port", required_argument, NULL, 'p' },
  205. { "ssl", no_argument, NULL, 's' },
  206. { "interface", required_argument, NULL, 'i' },
  207. { "client", no_argument, NULL, 'c' },
  208. { NULL, 0, 0, 0 }
  209. };
  210. int main(int argc, char **argv)
  211. {
  212. int n = 0;
  213. int port = 7681;
  214. int use_ssl = 0;
  215. struct lws_context *context;
  216. int opts = 0;
  217. char interface_name[128] = "", ads_port[300];
  218. const char *iface = NULL;
  219. struct lws *wsi;
  220. const char *address = NULL;
  221. int server_port = port;
  222. struct lws_context_creation_info info;
  223. memset(&info, 0, sizeof info);
  224. lwsl_notice("libwebsockets test server fraggle - license LGPL2.1+SLE\n");
  225. lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
  226. while (n >= 0) {
  227. n = getopt_long(argc, argv, "ci:hsp:d:", options, NULL);
  228. if (n < 0)
  229. continue;
  230. switch (n) {
  231. case 'd':
  232. lws_set_log_level(atoi(optarg), NULL);
  233. break;
  234. case 's':
  235. use_ssl = 1;
  236. break;
  237. case 'p':
  238. port = atoi(optarg);
  239. server_port = port;
  240. break;
  241. case 'i':
  242. strncpy(interface_name, optarg, sizeof interface_name);
  243. interface_name[(sizeof interface_name) - 1] = '\0';
  244. iface = interface_name;
  245. break;
  246. case 'c':
  247. client = 1;
  248. fprintf(stderr, " Client mode\n");
  249. break;
  250. case 'h':
  251. fprintf(stderr, "Usage: libwebsockets-test-fraggle "
  252. "[--port=<p>] [--ssl] "
  253. "[-d <log bitfield>] "
  254. "[--client]\n");
  255. exit(1);
  256. }
  257. }
  258. if (client) {
  259. server_port = CONTEXT_PORT_NO_LISTEN;
  260. if (optind >= argc) {
  261. fprintf(stderr, "Must give address of server\n");
  262. return 1;
  263. }
  264. }
  265. info.port = server_port;
  266. info.iface = iface;
  267. info.protocols = protocols;
  268. info.extensions = exts;
  269. if (use_ssl) {
  270. info.ssl_cert_filepath = LOCAL_RESOURCE_PATH
  271. "/libwebsockets-test-server.pem";
  272. info.ssl_private_key_filepath = LOCAL_RESOURCE_PATH
  273. "/libwebsockets-test-server.key.pem";
  274. }
  275. info.gid = -1;
  276. info.uid = -1;
  277. info.options = opts;
  278. info.extensions = exts;
  279. if (use_ssl)
  280. info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  281. context = lws_create_context(&info);
  282. if (context == NULL) {
  283. fprintf(stderr, "libwebsocket init failed\n");
  284. return -1;
  285. }
  286. if (client) {
  287. struct lws_client_connect_info i;
  288. address = argv[optind];
  289. lws_snprintf(ads_port, sizeof(ads_port), "%s:%u",
  290. address, port & 65535);
  291. memset(&i, 0, sizeof(i));
  292. i.context = context;
  293. i.address = address;
  294. i.port = port;
  295. i.ssl_connection = use_ssl;
  296. i.path = "/";
  297. i.host = ads_port;
  298. i.origin = ads_port;
  299. i.protocol = protocols[PROTOCOL_FRAGGLE].name;
  300. lwsl_notice("Connecting to %s:%u\n", address, port);
  301. wsi = lws_client_connect_via_info(&i);
  302. if (wsi == NULL) {
  303. fprintf(stderr, "Client connect to server failed\n");
  304. goto bail;
  305. }
  306. }
  307. n = 0;
  308. while (!n && !terminate)
  309. n = lws_service(context, 50);
  310. fprintf(stderr, "Terminating...\n");
  311. bail:
  312. lws_context_destroy(context);
  313. return 0;
  314. }