test-echo.c 12 KB

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