test-echo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * libwebsockets-test-echo
  3. *
  4. * Copyright (C) 2010-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 <assert.h>
  25. #include <signal.h>
  26. #include "../lib/libwebsockets.h"
  27. #ifndef _WIN32
  28. #include <syslog.h>
  29. #include <sys/time.h>
  30. #include <unistd.h>
  31. #else
  32. #include "gettimeofday.h"
  33. #include <process.h>
  34. #endif
  35. static volatile int force_exit = 0;
  36. static int versa, state;
  37. static int times = -1;
  38. #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
  39. #define MAX_ECHO_PAYLOAD 1024
  40. struct per_session_data__echo {
  41. size_t rx, tx;
  42. unsigned char buf[LWS_PRE + MAX_ECHO_PAYLOAD];
  43. unsigned int len;
  44. unsigned int index;
  45. int final;
  46. int continuation;
  47. int binary;
  48. };
  49. static int
  50. callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user,
  51. void *in, size_t len)
  52. {
  53. struct per_session_data__echo *pss =
  54. (struct per_session_data__echo *)user;
  55. int n;
  56. switch (reason) {
  57. #ifndef LWS_NO_SERVER
  58. case LWS_CALLBACK_ESTABLISHED:
  59. pss->index = 0;
  60. pss->len = -1;
  61. break;
  62. case LWS_CALLBACK_SERVER_WRITEABLE:
  63. do_tx:
  64. n = LWS_WRITE_CONTINUATION;
  65. if (!pss->continuation) {
  66. if (pss->binary)
  67. n = LWS_WRITE_BINARY;
  68. else
  69. n = LWS_WRITE_TEXT;
  70. pss->continuation = 1;
  71. }
  72. if (!pss->final)
  73. n |= LWS_WRITE_NO_FIN;
  74. lwsl_info("+++ test-echo: writing %d, with final %d\n",
  75. pss->len, pss->final);
  76. pss->tx += pss->len;
  77. n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, n);
  78. if (n < 0) {
  79. lwsl_err("ERROR %d writing to socket, hanging up\n", n);
  80. return 1;
  81. }
  82. if (n < (int)pss->len) {
  83. lwsl_err("Partial write\n");
  84. return -1;
  85. }
  86. pss->len = -1;
  87. if (pss->final)
  88. pss->continuation = 0;
  89. lws_rx_flow_control(wsi, 1);
  90. break;
  91. case LWS_CALLBACK_RECEIVE:
  92. do_rx:
  93. pss->final = lws_is_final_fragment(wsi);
  94. pss->binary = lws_frame_is_binary(wsi);
  95. lwsl_info("+++ test-echo: RX len %ld final %ld, pss->len=%ld\n",
  96. (long)len, (long)pss->final, (long)pss->len);
  97. memcpy(&pss->buf[LWS_PRE], in, len);
  98. assert((int)pss->len == -1);
  99. pss->len = (unsigned int)len;
  100. pss->rx += len;
  101. lws_rx_flow_control(wsi, 0);
  102. lws_callback_on_writable(wsi);
  103. break;
  104. #endif
  105. #ifndef LWS_NO_CLIENT
  106. /* when the callback is used for client operations --> */
  107. case LWS_CALLBACK_CLOSED:
  108. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
  109. lwsl_debug("closed\n");
  110. state = 0;
  111. break;
  112. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  113. lwsl_debug("Client has connected\n");
  114. pss->index = 0;
  115. pss->len = -1;
  116. state = 2;
  117. break;
  118. case LWS_CALLBACK_CLIENT_RECEIVE:
  119. #ifndef LWS_NO_SERVER
  120. if (versa)
  121. goto do_rx;
  122. #endif
  123. lwsl_notice("Client RX: %s", (char *)in);
  124. break;
  125. case LWS_CALLBACK_CLIENT_WRITEABLE:
  126. #ifndef LWS_NO_SERVER
  127. if (versa) {
  128. if (pss->len != (unsigned int)-1)
  129. goto do_tx;
  130. break;
  131. }
  132. #endif
  133. /* we will send our packet... */
  134. pss->len = sprintf((char *)&pss->buf[LWS_PRE],
  135. "hello from libwebsockets-test-echo client pid %d index %d\n",
  136. getpid(), pss->index++);
  137. lwsl_notice("Client TX: %s", &pss->buf[LWS_PRE]);
  138. n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, LWS_WRITE_TEXT);
  139. if (n < 0) {
  140. lwsl_err("ERROR %d writing to socket, hanging up\n", n);
  141. return -1;
  142. }
  143. if (n < (int)pss->len) {
  144. lwsl_err("Partial write\n");
  145. return -1;
  146. }
  147. break;
  148. #endif
  149. case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
  150. /* reject everything else except permessage-deflate */
  151. if (strcmp(in, "permessage-deflate"))
  152. return 1;
  153. break;
  154. default:
  155. break;
  156. }
  157. return 0;
  158. }
  159. static struct lws_protocols protocols[] = {
  160. /* first protocol must always be HTTP handler */
  161. {
  162. "", /* name - can be overridden with -e */
  163. callback_echo,
  164. sizeof(struct per_session_data__echo), /* per_session_data_size */
  165. MAX_ECHO_PAYLOAD,
  166. },
  167. {
  168. NULL, NULL, 0 /* End of list */
  169. }
  170. };
  171. static const struct lws_extension exts[] = {
  172. {
  173. "permessage-deflate",
  174. lws_extension_callback_pm_deflate,
  175. "permessage-deflate; client_no_context_takeover; client_max_window_bits"
  176. },
  177. {
  178. "deflate-frame",
  179. lws_extension_callback_pm_deflate,
  180. "deflate_frame"
  181. },
  182. { NULL, NULL, NULL /* terminator */ }
  183. };
  184. void sighandler(int sig)
  185. {
  186. force_exit = 1;
  187. }
  188. static struct option options[] = {
  189. { "help", no_argument, NULL, 'h' },
  190. { "debug", required_argument, NULL, 'd' },
  191. { "port", required_argument, NULL, 'p' },
  192. { "ssl-cert", required_argument, NULL, 'C' },
  193. { "ssl-key", required_argument, NULL, 'k' },
  194. #ifndef LWS_NO_CLIENT
  195. { "client", required_argument, NULL, 'c' },
  196. { "ratems", required_argument, NULL, 'r' },
  197. #endif
  198. { "ssl", no_argument, NULL, 's' },
  199. { "versa", no_argument, NULL, 'v' },
  200. { "uri", required_argument, NULL, 'u' },
  201. { "passphrase", required_argument, NULL, 'P' },
  202. { "interface", required_argument, NULL, 'i' },
  203. { "times", required_argument, NULL, 'n' },
  204. { "echogen", no_argument, NULL, 'e' },
  205. #ifndef LWS_NO_DAEMONIZE
  206. { "daemonize", no_argument, NULL, 'D' },
  207. #endif
  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] = "";
  218. const char *_interface = NULL;
  219. char ssl_cert[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
  220. char ssl_key[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
  221. #ifndef _WIN32
  222. /* LOG_PERROR is not POSIX standard, and may not be portable */
  223. #ifdef __sun
  224. int syslog_options = LOG_PID;
  225. #else
  226. int syslog_options = LOG_PID | LOG_PERROR;
  227. #endif
  228. #endif
  229. int client = 0;
  230. int listen_port = 80;
  231. struct lws_context_creation_info info;
  232. char passphrase[256];
  233. char uri[256] = "/";
  234. #ifndef LWS_NO_CLIENT
  235. char address[256], ads_port[256 + 30];
  236. int rate_us = 250000;
  237. unsigned long long oldus;
  238. struct lws *wsi;
  239. int disallow_selfsigned = 0;
  240. struct timeval tv;
  241. const char *connect_protocol = NULL;
  242. struct lws_client_connect_info i;
  243. #endif
  244. int debug_level = 7;
  245. #ifndef LWS_NO_DAEMONIZE
  246. int daemonize = 0;
  247. #endif
  248. memset(&info, 0, sizeof info);
  249. #ifndef LWS_NO_CLIENT
  250. lwsl_notice("Built to support client operations\n");
  251. #endif
  252. #ifndef LWS_NO_SERVER
  253. lwsl_notice("Built to support server operations\n");
  254. #endif
  255. while (n >= 0) {
  256. n = getopt_long(argc, argv, "i:hsp:d:DC:k:P:vu:n:e"
  257. #ifndef LWS_NO_CLIENT
  258. "c:r:"
  259. #endif
  260. , options, NULL);
  261. if (n < 0)
  262. continue;
  263. switch (n) {
  264. case 'P':
  265. strncpy(passphrase, optarg, sizeof(passphrase));
  266. passphrase[sizeof(passphrase) - 1] = '\0';
  267. info.ssl_private_key_password = passphrase;
  268. break;
  269. case 'C':
  270. strncpy(ssl_cert, optarg, sizeof(ssl_cert));
  271. ssl_cert[sizeof(ssl_cert) - 1] = '\0';
  272. disallow_selfsigned = 1;
  273. break;
  274. case 'k':
  275. strncpy(ssl_key, optarg, sizeof(ssl_key));
  276. ssl_key[sizeof(ssl_key) - 1] = '\0';
  277. break;
  278. case 'u':
  279. strncpy(uri, optarg, sizeof(uri));
  280. uri[sizeof(uri) - 1] = '\0';
  281. break;
  282. #ifndef LWS_NO_DAEMONIZE
  283. case 'D':
  284. daemonize = 1;
  285. #if !defined(_WIN32) && !defined(__sun)
  286. syslog_options &= ~LOG_PERROR;
  287. #endif
  288. break;
  289. #endif
  290. #ifndef LWS_NO_CLIENT
  291. case 'c':
  292. client = 1;
  293. strncpy(address, optarg, sizeof(address) - 1);
  294. address[sizeof(address) - 1] = '\0';
  295. port = 80;
  296. break;
  297. case 'r':
  298. rate_us = atoi(optarg) * 1000;
  299. break;
  300. #endif
  301. case 'd':
  302. debug_level = atoi(optarg);
  303. break;
  304. case 's':
  305. use_ssl = 1; /* 1 = take care about cert verification, 2 = allow anything */
  306. break;
  307. case 'p':
  308. port = atoi(optarg);
  309. break;
  310. case 'v':
  311. versa = 1;
  312. break;
  313. case 'e':
  314. protocols[0].name = "lws-echogen";
  315. connect_protocol = protocols[0].name;
  316. lwsl_err("using lws-echogen\n");
  317. break;
  318. case 'i':
  319. strncpy(interface_name, optarg, sizeof interface_name);
  320. interface_name[(sizeof interface_name) - 1] = '\0';
  321. _interface = interface_name;
  322. break;
  323. case 'n':
  324. times = atoi(optarg);
  325. break;
  326. case '?':
  327. case 'h':
  328. fprintf(stderr, "Usage: libwebsockets-test-echo\n"
  329. " --debug / -d <debug bitfield>\n"
  330. " --port / -p <port>\n"
  331. " --ssl-cert / -C <cert path>\n"
  332. " --ssl-key / -k <key path>\n"
  333. #ifndef LWS_NO_CLIENT
  334. " --client / -c <server IP>\n"
  335. " --ratems / -r <rate in ms>\n"
  336. #endif
  337. " --ssl / -s\n"
  338. " --passphrase / -P <passphrase>\n"
  339. " --interface / -i <interface>\n"
  340. " --uri / -u <uri path>\n"
  341. " --times / -n <-1 unlimited or times to echo>\n"
  342. #ifndef LWS_NO_DAEMONIZE
  343. " --daemonize / -D\n"
  344. #endif
  345. );
  346. exit(1);
  347. }
  348. }
  349. #ifndef LWS_NO_DAEMONIZE
  350. /*
  351. * normally lock path would be /var/lock/lwsts or similar, to
  352. * simplify getting started without having to take care about
  353. * permissions or running as root, set to /tmp/.lwsts-lock
  354. */
  355. #if defined(WIN32) || defined(_WIN32)
  356. #else
  357. if (!client && daemonize && lws_daemonize("/tmp/.lwstecho-lock")) {
  358. fprintf(stderr, "Failed to daemonize\n");
  359. return 1;
  360. }
  361. #endif
  362. #endif
  363. #ifndef _WIN32
  364. /* we will only try to log things according to our debug_level */
  365. setlogmask(LOG_UPTO (LOG_DEBUG));
  366. openlog("lwsts", syslog_options, LOG_DAEMON);
  367. #endif
  368. /* tell the library what debug level to emit and to send it to syslog */
  369. lws_set_log_level(debug_level, lwsl_emit_syslog);
  370. lwsl_notice("libwebsockets test server echo - license LGPL2.1+SLE\n");
  371. lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
  372. #ifndef LWS_NO_CLIENT
  373. if (client) {
  374. lwsl_notice("Running in client mode\n");
  375. listen_port = CONTEXT_PORT_NO_LISTEN;
  376. if (use_ssl && !disallow_selfsigned) {
  377. lwsl_info("allowing selfsigned\n");
  378. use_ssl = 2;
  379. } else {
  380. lwsl_info("requiring server cert validation against %s\n",
  381. ssl_cert);
  382. info.ssl_ca_filepath = ssl_cert;
  383. }
  384. } else {
  385. #endif
  386. #ifndef LWS_NO_SERVER
  387. lwsl_notice("Running in server mode\n");
  388. listen_port = port;
  389. #endif
  390. #ifndef LWS_NO_CLIENT
  391. }
  392. #endif
  393. info.port = listen_port;
  394. info.iface = _interface;
  395. info.protocols = protocols;
  396. if (use_ssl && !client) {
  397. info.ssl_cert_filepath = ssl_cert;
  398. info.ssl_private_key_filepath = ssl_key;
  399. } else
  400. if (use_ssl && client) {
  401. info.ssl_cert_filepath = NULL;
  402. info.ssl_private_key_filepath = NULL;
  403. }
  404. info.gid = -1;
  405. info.uid = -1;
  406. info.extensions = exts;
  407. info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
  408. if (use_ssl)
  409. info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  410. #ifndef LWS_NO_EXTENSIONS
  411. info.extensions = exts;
  412. #endif
  413. context = lws_create_context(&info);
  414. if (context == NULL) {
  415. lwsl_err("libwebsocket init failed\n");
  416. return -1;
  417. }
  418. signal(SIGINT, sighandler);
  419. #ifndef LWS_NO_CLIENT
  420. gettimeofday(&tv, NULL);
  421. oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
  422. #endif
  423. n = 0;
  424. while (n >= 0 && !force_exit) {
  425. #ifndef LWS_NO_CLIENT
  426. if (client && !state && times) {
  427. state = 1;
  428. lwsl_notice("Client connecting to %s:%u....\n",
  429. address, port);
  430. /* we are in client mode */
  431. address[sizeof(address) - 1] = '\0';
  432. sprintf(ads_port, "%s:%u", address, port & 65535);
  433. if (times > 0)
  434. times--;
  435. memset(&i, 0, sizeof(i));
  436. i.context = context;
  437. i.address = address;
  438. i.port = port;
  439. i.ssl_connection = use_ssl;
  440. i.path = uri;
  441. i.host = ads_port;
  442. i.origin = ads_port;
  443. i.protocol = connect_protocol;
  444. wsi = lws_client_connect_via_info(&i);
  445. if (!wsi) {
  446. lwsl_err("Client failed to connect to %s:%u\n",
  447. address, port);
  448. goto bail;
  449. }
  450. }
  451. if (client && !versa && times) {
  452. gettimeofday(&tv, NULL);
  453. if (((((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec) - oldus) > rate_us) {
  454. lws_callback_on_writable_all_protocol(context,
  455. &protocols[0]);
  456. oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
  457. if (times > 0)
  458. times--;
  459. }
  460. }
  461. if (client && !state && !times)
  462. break;
  463. #endif
  464. n = lws_service(context, 10);
  465. }
  466. #ifndef LWS_NO_CLIENT
  467. bail:
  468. #endif
  469. lws_context_destroy(context);
  470. lwsl_notice("libwebsockets-test-echo exited cleanly\n");
  471. #ifndef _WIN32
  472. closelog();
  473. #endif
  474. return 0;
  475. }