1 /*
2  * lws-minimal-ws-client-spam
3  *
4  * Written in 2010-2021 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 client that makes continuous mass ws connections
10  * asynchronously
11  */
12 
13 #include <libwebsockets.h>
14 #include <string.h>
15 #include <signal.h>
16 #if defined(WIN32)
17 #define HAVE_STRUCT_TIMESPEC
18 #if defined(pid_t)
19 #undef pid_t
20 #endif
21 #endif
22 #include <pthread.h>
23 
24 enum {
25 	CLIENT_IDLE,
26 	CLIENT_CONNECTING,
27 	CLIENT_AWAITING_SEND,
28 };
29 
30 struct client {
31 	struct lws *wsi;
32 	int index;
33 	int state;
34 };
35 
36 static struct lws_context *context;
37 static struct client clients[200];
38 static int interrupted, port = 443, ssl_connection = LCCSCF_USE_SSL;
39 static const char *server_address = "libwebsockets.org",
40 		  *pro = "lws-mirror-protocol";
41 static int concurrent = 3, conn, tries, est, errors, closed, sent, limit = 15;
42 
43 struct pss {
44 	int conn;
45 };
46 
47 static int
connect_client(int idx)48 connect_client(int idx)
49 {
50 	struct lws_client_connect_info i;
51 
52 	if (tries == limit) {
53 		lwsl_user("Reached limit... finishing\n");
54 		return 0;
55 	}
56 
57 	memset(&i, 0, sizeof(i));
58 
59 	i.context = context;
60 	i.port = port;
61 	i.address = server_address;
62 	i.path = "/";
63 	i.host = i.address;
64 	i.origin = i.address;
65 	i.ssl_connection = ssl_connection;
66 	i.protocol = pro;
67 	i.local_protocol_name = pro;
68 	i.pwsi = &clients[idx].wsi;
69 
70 	clients[idx].state = CLIENT_CONNECTING;
71 	tries++;
72 
73 	lwsl_notice("%s: connection %s:%d\n", __func__, i.address, i.port);
74 	if (!lws_client_connect_via_info(&i)) {
75 		clients[idx].wsi = NULL;
76 		clients[idx].state = CLIENT_IDLE;
77 
78 		return 1;
79 	}
80 
81 	return 0;
82 }
83 
84 static int
callback_minimal_spam(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)85 callback_minimal_spam(struct lws *wsi, enum lws_callback_reasons reason,
86 			void *user, void *in, size_t len)
87 {
88 	struct pss *pss = (struct pss *)user;
89 	uint8_t ping[LWS_PRE + 125];
90 	int n, m;
91 
92 	switch (reason) {
93 
94 	case LWS_CALLBACK_PROTOCOL_INIT:
95 		for (n = 0; n < concurrent; n++) {
96 			clients[n].index = n;
97 			connect_client(n);
98 		}
99 		break;
100 
101 	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
102 		errors++;
103 		lwsl_err("CLIENT_CONNECTION_ERROR: %s (try %d, est %d, closed %d, err %d)\n",
104 			 in ? (char *)in : "(null)", tries, est, closed, errors);
105 		for (n = 0; n < concurrent; n++) {
106 			if (clients[n].wsi == wsi) {
107 				clients[n].wsi = NULL;
108 				clients[n].state = CLIENT_IDLE;
109 				connect_client(n);
110 				break;
111 			}
112 		}
113 		if (tries == closed + errors) {
114 			interrupted = 1;
115 			lws_cancel_service(lws_get_context(wsi));
116 		}
117 		break;
118 
119 	/* --- client callbacks --- */
120 
121 	case LWS_CALLBACK_CLIENT_ESTABLISHED:
122 		lwsl_user("%s: established (try %d, est %d, closed %d, err %d)\n",
123 				__func__, tries, est, closed, errors);
124 		est++;
125 		pss->conn = conn++;
126 		lws_callback_on_writable(wsi);
127 		break;
128 
129 	case LWS_CALLBACK_CLIENT_CLOSED:
130 		closed++;
131 		if (tries == closed + errors) {
132 			interrupted = 1;
133 			lws_cancel_service(lws_get_context(wsi));
134 		}
135 		if (tries == limit) {
136 			lwsl_user("%s: leaving CLOSED (try %d, est %d, sent %d, closed %d, err %d)\n",
137 					__func__, tries, est, sent, closed, errors);
138 			break;
139 		}
140 
141 		for (n = 0; n < concurrent; n++) {
142 			if (clients[n].wsi == wsi) {
143 				connect_client(n);
144 				lwsl_user("%s: reopening (try %d, est %d, closed %d, err %d)\n",
145 						__func__, tries, est, closed, errors);
146 				break;
147 			}
148 		}
149 		if (n == concurrent)
150 			lwsl_user("CLOSED: can't find client wsi\n");
151 		break;
152 
153 	case LWS_CALLBACK_CLIENT_WRITEABLE:
154 		n = lws_snprintf((char *)ping + LWS_PRE, sizeof(ping) - LWS_PRE,
155 					  "hello %d", pss->conn);
156 
157 		m = lws_write(wsi, ping + LWS_PRE, (unsigned int)n, LWS_WRITE_TEXT);
158 		if (m < n) {
159 			lwsl_err("sending ping failed: %d\n", m);
160 
161 			return -1;
162 		}
163 		lws_set_timeout(wsi, PENDING_TIMEOUT_USER_OK, LWS_TO_KILL_ASYNC);
164 		break;
165 
166 	default:
167 		break;
168 	}
169 
170 	return lws_callback_http_dummy(wsi, reason, user, in, len);
171 }
172 
173 static const struct lws_protocols protocols[] = {
174 	{
175 		"lws-spam-test",
176 		callback_minimal_spam,
177 		sizeof(struct pss),
178 		0,
179 	},
180 	{ NULL, NULL, 0, 0 }
181 };
182 
183 static struct lws_protocol_vhost_options pvo = {
184         NULL,                  /* "next" pvo linked-list */
185         NULL,                 /* "child" pvo linked-list */
186         "lws-spam-test",        /* protocol name we belong to on this vhost */
187         "OK"                     /* ignored */
188 };
189 
190 static void
sigint_handler(int sig)191 sigint_handler(int sig)
192 {
193 	interrupted = 1;
194 }
195 
main(int argc,const char ** argv)196 int main(int argc, const char **argv)
197 {
198 	struct lws_context_creation_info info;
199 	const char *p;
200 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
201 			/* for LLL_ verbosity above NOTICE to be built into lws,
202 			 * lws must have been configured and built with
203 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
204 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
205 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
206 			/* | LLL_DEBUG */;
207 
208 	signal(SIGINT, sigint_handler);
209 
210 	if ((p = lws_cmdline_option(argc, argv, "-d")))
211 		logs = atoi(p);
212 
213 	lws_set_log_level(logs, NULL);
214 	lwsl_user("LWS minimal ws client SPAM\n");
215 
216 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
217 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
218 	info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
219 	info.protocols = protocols;
220 	info.pvo = &pvo;
221 #if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL)
222 	/*
223 	 * OpenSSL uses the system trust store.  mbedTLS has to be told which
224 	 * CA to trust explicitly.
225 	 */
226 	info.client_ssl_ca_filepath = "./libwebsockets.org.cer";
227 #endif
228 
229 	if ((p = lws_cmdline_option(argc, argv, "--server"))) {
230 		server_address = p;
231 		ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
232 	}
233 
234 	if ((p = lws_cmdline_option(argc, argv, "--port")))
235 		port = atoi(p);
236 
237 	if ((p = lws_cmdline_option(argc, argv, "-l")))
238 		limit = atoi(p);
239 
240 	if ((p = lws_cmdline_option(argc, argv, "-c")))
241 		concurrent = atoi(p);
242 
243 	if (lws_cmdline_option(argc, argv, "-n")) {
244 		ssl_connection = 0;
245 		info.options = 0;
246 	}
247 
248 	if (concurrent < 0 ||
249 	    concurrent > (int)LWS_ARRAY_SIZE(clients)) {
250 		lwsl_err("%s: -c %d larger than max concurrency %d\n", __func__,
251 				concurrent, (int)LWS_ARRAY_SIZE(clients));
252 
253 		return 1;
254 	}
255 
256 	/*
257 	 * since we know this lws context is only ever going to be used with
258 	 * one client wsis / fds / sockets at a time, let lws know it doesn't
259 	 * have to use the default allocations for fd tables up to ulimit -n.
260 	 * It will just allocate for 1 internal and n (+ 1 http2 nwsi) that we
261 	 * will use.
262 	 */
263 	info.fd_limit_per_thread = (unsigned int)(1 + concurrent + 1);
264 
265 	context = lws_create_context(&info);
266 	if (!context) {
267 		lwsl_err("lws init failed\n");
268 		return 1;
269 	}
270 
271 	while (n >= 0 && !interrupted)
272 		n = lws_service(context, 0);
273 
274 	lwsl_notice("%s: exiting service loop\n", __func__);
275 
276 	lws_context_destroy(context);
277 
278 	if (tries == limit && closed == tries) {
279 		lwsl_user("Completed\n");
280 		return 0;
281 	}
282 
283 	lwsl_err("Failed\n");
284 
285 	return 1;
286 }
287