• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-ws-server-echo
3  *
4  * Written in 2010-2019 by 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  * This demonstrates a ws server that echoes back what it was sent, in a way
10  * compatible with autobahn -m fuzzingclient
11  */
12 
13 #include <libwebsockets.h>
14 #include <string.h>
15 #include <signal.h>
16 
17 #define LWS_PLUGIN_STATIC
18 #include "protocol_lws_minimal_server_echo.c"
19 
20 static struct lws_protocols protocols[] = {
21 	LWS_PLUGIN_PROTOCOL_MINIMAL_SERVER_ECHO,
22 	{ NULL, NULL, 0, 0 } /* terminator */
23 };
24 
25 static int interrupted, port = 7681, options;
26 
27 /* pass pointers to shared vars to the protocol */
28 
29 static const struct lws_protocol_vhost_options pvo_options = {
30 	NULL,
31 	NULL,
32 	"options",		/* pvo name */
33 	(void *)&options	/* pvo value */
34 };
35 
36 static const struct lws_protocol_vhost_options pvo_interrupted = {
37 	&pvo_options,
38 	NULL,
39 	"interrupted",		/* pvo name */
40 	(void *)&interrupted	/* pvo value */
41 };
42 
43 static const struct lws_protocol_vhost_options pvo = {
44 	NULL,				/* "next" pvo linked-list */
45 	&pvo_interrupted,		/* "child" pvo linked-list */
46 	"lws-minimal-server-echo",	/* protocol name we belong to on this vhost */
47 	""				/* ignored */
48 };
49 static const struct lws_extension extensions[] = {
50 	{
51 		"permessage-deflate",
52 		lws_extension_callback_pm_deflate,
53 		"permessage-deflate"
54 		 "; client_no_context_takeover"
55 		 "; client_max_window_bits"
56 	},
57 	{ NULL, NULL, NULL /* terminator */ }
58 };
59 
sigint_handler(int sig)60 void sigint_handler(int sig)
61 {
62 	interrupted = 1;
63 }
64 
main(int argc,const char ** argv)65 int main(int argc, const char **argv)
66 {
67 	struct lws_context_creation_info info;
68 	struct lws_context *context;
69 	const char *p;
70 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
71 			/* for LLL_ verbosity above NOTICE to be built into lws,
72 			 * lws must have been configured and built with
73 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
74 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
75 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
76 			/* | LLL_DEBUG */;
77 
78 	signal(SIGINT, sigint_handler);
79 
80 	if ((p = lws_cmdline_option(argc, argv, "-d")))
81 		logs = atoi(p);
82 
83 	lws_set_log_level(logs, NULL);
84 	lwsl_user("LWS minimal ws client echo + permessage-deflate + multifragment bulk message\n");
85 	lwsl_user("   lws-minimal-ws-client-echo [-n (no exts)] [-p port] [-o (once)]\n");
86 
87 
88 	if ((p = lws_cmdline_option(argc, argv, "-p")))
89 		port = atoi(p);
90 
91 	if (lws_cmdline_option(argc, argv, "-o"))
92 		options |= 1;
93 
94 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
95 	info.port = port;
96 	info.protocols = protocols;
97 	info.pvo = &pvo;
98 	if (!lws_cmdline_option(argc, argv, "-n"))
99 		info.extensions = extensions;
100 	info.pt_serv_buf_size = 32 * 1024;
101 	info.options = LWS_SERVER_OPTION_VALIDATE_UTF8 |
102 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
103 
104 	context = lws_create_context(&info);
105 	if (!context) {
106 		lwsl_err("lws init failed\n");
107 		return 1;
108 	}
109 
110 	while (n >= 0 && !interrupted)
111 		n = lws_service(context, 0);
112 
113 	lws_context_destroy(context);
114 
115 	lwsl_user("Completed %s\n", interrupted == 2 ? "OK" : "failed");
116 
117 	return interrupted != 2;
118 }
119