blob: debd43a7c3e7cd292c274f7e6a10c63980cf6c9f [file] [log] [blame]
Andy Green4739e5c2011-01-22 12:51:57 +00001/*
2 * libwebsockets-test-client - libwebsockets test implementation
3 *
4 * Copyright (C) 2011 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <getopt.h>
26#include <string.h>
Andy Green3886ec72013-02-11 14:32:02 +080027#include <signal.h>
Andy Green4739e5c2011-01-22 12:51:57 +000028
Joakim Soderberg7eadd582013-02-22 09:28:04 +080029#ifdef CMAKE_BUILD
30#include "lws_config.h"
31#endif
32
Andy Green4739e5c2011-01-22 12:51:57 +000033#include "../lib/libwebsockets.h"
Andy Green4739e5c2011-01-22 12:51:57 +000034
Andy Green990d5062011-01-30 21:04:24 +000035static unsigned int opts;
Andy Greenb3ae0a32011-02-14 20:25:43 +000036static int was_closed;
Andy Green775884e2011-03-06 13:32:53 +000037static int deny_deflate;
Andy Green7448c7e2011-05-24 22:06:17 +010038static int deny_mux;
Andy Green4084af12011-05-24 22:06:17 +010039static struct libwebsocket *wsi_mirror;
Andy Green8e0c9842013-02-09 14:10:04 +080040static int mirror_lifetime = 0;
Andy Green3886ec72013-02-11 14:32:02 +080041static int force_exit = 0;
Andy Green990d5062011-01-30 21:04:24 +000042
Andy Green4739e5c2011-01-22 12:51:57 +000043/*
44 * This demo shows how to connect multiple websockets simultaneously to a
45 * websocket server (there is no restriction on their having to be the same
46 * server just it simplifies the demo).
47 *
48 * dumb-increment-protocol: we connect to the server and print the number
Andy Green90c7cbc2011-01-27 06:26:52 +000049 * we are given
Andy Green4739e5c2011-01-22 12:51:57 +000050 *
51 * lws-mirror-protocol: draws random circles, which are mirrored on to every
Andy Green90c7cbc2011-01-27 06:26:52 +000052 * client (see them being drawn in every browser
53 * session also using the test server)
Andy Green4739e5c2011-01-22 12:51:57 +000054 */
55
56enum demo_protocols {
57
58 PROTOCOL_DUMB_INCREMENT,
59 PROTOCOL_LWS_MIRROR,
60
61 /* always last */
62 DEMO_PROTOCOL_COUNT
63};
64
65
66/* dumb_increment protocol */
67
68static int
Andy Green6ee372f2012-04-09 15:09:01 +080069callback_dumb_increment(struct libwebsocket_context *this,
Andy Green62c54d22011-02-14 09:14:25 +000070 struct libwebsocket *wsi,
Andy Green4739e5c2011-01-22 12:51:57 +000071 enum libwebsocket_callback_reasons reason,
72 void *user, void *in, size_t len)
73{
74 switch (reason) {
75
Andy Green4084af12011-05-24 22:06:17 +010076 case LWS_CALLBACK_CLOSED:
77 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
78 was_closed = 1;
79 break;
80
Andy Green4739e5c2011-01-22 12:51:57 +000081 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +000082 ((char *)in)[len] = '\0';
83 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
Andy Green4739e5c2011-01-22 12:51:57 +000084 break;
85
Andy Green775884e2011-03-06 13:32:53 +000086 /* because we are protocols[0] ... */
87
88 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
Andy Green7448c7e2011-05-24 22:06:17 +010089 if ((strcmp(in, "deflate-stream") == 0) && deny_deflate) {
90 fprintf(stderr, "denied deflate-stream extension\n");
91 return 1;
92 }
93 if ((strcmp(in, "x-google-mux") == 0) && deny_mux) {
94 fprintf(stderr, "denied x-google-mux extension\n");
95 return 1;
96 }
97
Andy Green775884e2011-03-06 13:32:53 +000098 break;
99
Andy Green4739e5c2011-01-22 12:51:57 +0000100 default:
101 break;
102 }
103
104 return 0;
105}
106
107
108/* lws-mirror_protocol */
109
Andy Green4739e5c2011-01-22 12:51:57 +0000110
111static int
Andy Green8e0c9842013-02-09 14:10:04 +0800112callback_lws_mirror(struct libwebsocket_context *context,
Andy Green62c54d22011-02-14 09:14:25 +0000113 struct libwebsocket *wsi,
Andy Green4739e5c2011-01-22 12:51:57 +0000114 enum libwebsocket_callback_reasons reason,
115 void *user, void *in, size_t len)
116{
Andy Green90c7cbc2011-01-27 06:26:52 +0000117 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
118 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800119 int l = 0;
120 int n;
Andy Green90c7cbc2011-01-27 06:26:52 +0000121
Andy Green4739e5c2011-01-22 12:51:57 +0000122 switch (reason) {
123
Andy Greenb3ae0a32011-02-14 20:25:43 +0000124 case LWS_CALLBACK_CLOSED:
Andy Green4084af12011-05-24 22:06:17 +0100125 fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n");
126 wsi_mirror = NULL;
Andy Greenb3ae0a32011-02-14 20:25:43 +0000127 break;
128
Andy Green90c7cbc2011-01-27 06:26:52 +0000129 case LWS_CALLBACK_CLIENT_ESTABLISHED:
130
131 /*
132 * start the ball rolling,
Andy Greenb6e6ebe2011-01-27 06:36:39 +0000133 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
Andy Green90c7cbc2011-01-27 06:26:52 +0000134 */
135
Andy Green8e0c9842013-02-09 14:10:04 +0800136 libwebsocket_callback_on_writable(context, wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +0000137 break;
138
Andy Green4739e5c2011-01-22 12:51:57 +0000139 case LWS_CALLBACK_CLIENT_RECEIVE:
Andy Green90c7cbc2011-01-27 06:26:52 +0000140/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
141 break;
142
143 case LWS_CALLBACK_CLIENT_WRITEABLE:
144
Andy Green3182ece2013-01-20 17:08:31 +0800145 for (n = 0; n < 1; n++)
146 l += sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING + l],
Andy Green90c7cbc2011-01-27 06:26:52 +0000147 "c #%06X %d %d %d;",
148 (int)random() & 0xffffff,
149 (int)random() % 500,
150 (int)random() % 250,
151 (int)random() % 24);
152
153 libwebsocket_write(wsi,
Andy Green990d5062011-01-30 21:04:24 +0000154 &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
Andy Green90c7cbc2011-01-27 06:26:52 +0000155
Andy Green8e0c9842013-02-09 14:10:04 +0800156 mirror_lifetime--;
157 if (!mirror_lifetime) {
158 fprintf(stderr, "closing mirror session\n");
Andy Green310655b2013-02-11 14:08:50 +0800159 return -1;
Andy Green8e0c9842013-02-09 14:10:04 +0800160 } else
161 /* get notified as soon as we can write again */
162 libwebsocket_callback_on_writable(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000163 break;
164
165 default:
166 break;
167 }
168
169 return 0;
170}
171
172
173/* list of supported protocols and callbacks */
174
175static struct libwebsocket_protocols protocols[] = {
Peter Hinz56885f32011-03-02 22:03:47 +0000176 {
177 "dumb-increment-protocol",
178 callback_dumb_increment,
179 0,
Andy Green54495112013-02-06 21:10:16 +0900180 20,
Andy Green4739e5c2011-01-22 12:51:57 +0000181 },
Peter Hinz56885f32011-03-02 22:03:47 +0000182 {
183 "lws-mirror-protocol",
184 callback_lws_mirror,
185 0,
Andy Greene7c97e82013-02-09 14:07:32 +0800186 128,
Andy Green4739e5c2011-01-22 12:51:57 +0000187 },
Andy Green54495112013-02-06 21:10:16 +0900188 { NULL, NULL, 0, 0 } /* end */
Andy Green4739e5c2011-01-22 12:51:57 +0000189};
190
Andy Green3886ec72013-02-11 14:32:02 +0800191void sighandler(int sig)
192{
193 force_exit = 1;
194}
195
Andy Green4739e5c2011-01-22 12:51:57 +0000196static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000197 { "help", no_argument, NULL, 'h' },
Andy Green46ef0cf2013-01-10 22:28:59 +0800198 { "debug", required_argument, NULL, 'd' },
Andy Green90c7cbc2011-01-27 06:26:52 +0000199 { "port", required_argument, NULL, 'p' },
200 { "ssl", no_argument, NULL, 's' },
Andy Greenbfb051f2011-02-09 08:49:14 +0000201 { "version", required_argument, NULL, 'v' },
Andy Green775884e2011-03-06 13:32:53 +0000202 { "undeflated", no_argument, NULL, 'u' },
Andy Green7448c7e2011-05-24 22:06:17 +0100203 { "nomux", no_argument, NULL, 'n' },
Andy Green43063dd2013-01-13 11:58:18 +0800204 { "longlived", no_argument, NULL, 'l' },
Andy Green4739e5c2011-01-22 12:51:57 +0000205 { NULL, 0, 0, 0 }
206};
207
208
209int main(int argc, char **argv)
210{
211 int n = 0;
Andy Green93f98d72013-02-11 14:05:02 +0800212 int ret = 0;
Andy Green4739e5c2011-01-22 12:51:57 +0000213 int port = 7681;
214 int use_ssl = 0;
215 struct libwebsocket_context *context;
Andy Green1efb63c2011-02-06 17:42:06 +0000216 const char *address;
Andy Green4739e5c2011-01-22 12:51:57 +0000217 struct libwebsocket *wsi_dumb;
Andy Greenbfb051f2011-02-09 08:49:14 +0000218 int ietf_version = -1; /* latest */
Andy Green43063dd2013-01-13 11:58:18 +0800219 int longlived = 0;
Andy Green1b265272013-02-09 14:01:09 +0800220 struct lws_context_creation_info info;
221
222 memset(&info, 0, sizeof info);
Andy Green90c7cbc2011-01-27 06:26:52 +0000223
Andy Green4739e5c2011-01-22 12:51:57 +0000224 fprintf(stderr, "libwebsockets test client\n"
Andy Green46ef0cf2013-01-10 22:28:59 +0800225 "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> "
Andy Green4739e5c2011-01-22 12:51:57 +0000226 "licensed under LGPL2.1\n");
227
228 if (argc < 2)
229 goto usage;
230
Andy Green4739e5c2011-01-22 12:51:57 +0000231 while (n >= 0) {
Andy Green5738c0e2013-01-21 09:53:35 +0800232 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
Andy Green4739e5c2011-01-22 12:51:57 +0000233 if (n < 0)
234 continue;
235 switch (n) {
Andy Green46ef0cf2013-01-10 22:28:59 +0800236 case 'd':
Andy Greende8f27a2013-01-12 09:17:42 +0800237 lws_set_log_level(atoi(optarg), NULL);
Andy Green46ef0cf2013-01-10 22:28:59 +0800238 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000239 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000240 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000241 break;
242 case 'p':
243 port = atoi(optarg);
244 break;
Andy Green43063dd2013-01-13 11:58:18 +0800245 case 'l':
246 longlived = 1;
247 break;
Andy Greenbfb051f2011-02-09 08:49:14 +0000248 case 'v':
249 ietf_version = atoi(optarg);
250 break;
Andy Green775884e2011-03-06 13:32:53 +0000251 case 'u':
252 deny_deflate = 1;
253 break;
Andy Green7448c7e2011-05-24 22:06:17 +0100254 case 'n':
255 deny_mux = 1;
256 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000257 case 'h':
258 goto usage;
259 }
260 }
261
Andy Green1efb63c2011-02-06 17:42:06 +0000262 if (optind >= argc)
263 goto usage;
264
Andy Green3886ec72013-02-11 14:32:02 +0800265 signal(SIGINT, sighandler);
266
Andy Green1efb63c2011-02-06 17:42:06 +0000267 address = argv[optind];
268
Andy Green4739e5c2011-01-22 12:51:57 +0000269 /*
270 * create the websockets context. This tracks open connections and
271 * knows how to route any traffic and which protocol version to use,
272 * and if each connection is client or server side.
273 *
274 * For this client-only demo, we tell it to not listen on any port.
275 */
276
Andy Green1b265272013-02-09 14:01:09 +0800277 info.port = CONTEXT_PORT_NO_LISTEN;
278 info.protocols = protocols;
Andy Green3182ece2013-01-20 17:08:31 +0800279#ifndef LWS_NO_EXTENSIONS
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800280 info.extensions = libwebsocket_get_internal_extensions();
Andy Green3182ece2013-01-20 17:08:31 +0800281#endif
Andy Green1b265272013-02-09 14:01:09 +0800282 info.gid = -1;
283 info.uid = -1;
284
285 context = libwebsocket_create_context(&info);
Andy Green4739e5c2011-01-22 12:51:57 +0000286 if (context == NULL) {
287 fprintf(stderr, "Creating libwebsocket context failed\n");
288 return 1;
289 }
290
Andy Green4739e5c2011-01-22 12:51:57 +0000291 /* create a client websocket using dumb increment protocol */
292
Andy Green90c7cbc2011-01-27 06:26:52 +0000293 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
Andy Green1efb63c2011-02-06 17:42:06 +0000294 "/", argv[optind], argv[optind],
Andy Greenbfb051f2011-02-09 08:49:14 +0000295 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
Andy Green4739e5c2011-01-22 12:51:57 +0000296
297 if (wsi_dumb == NULL) {
298 fprintf(stderr, "libwebsocket dumb connect failed\n");
Andy Green93f98d72013-02-11 14:05:02 +0800299 ret = 1;
300 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000301 }
302
Andy Green4739e5c2011-01-22 12:51:57 +0000303 fprintf(stderr, "Websocket connections opened\n");
304
305 /*
306 * sit there servicing the websocket context to handle incoming
307 * packets, and drawing random circles on the mirror protocol websocket
308 */
309
310 n = 0;
Andy Green3886ec72013-02-11 14:32:02 +0800311 while (n >= 0 && !was_closed && !force_exit) {
Andy Green8e0c9842013-02-09 14:10:04 +0800312 n = libwebsocket_service(context, 10);
Andy Green4739e5c2011-01-22 12:51:57 +0000313
Andy Green3928f612012-07-20 12:58:38 +0800314 if (n < 0)
315 continue;
316
Andy Green8e0c9842013-02-09 14:10:04 +0800317 if (wsi_mirror)
318 continue;
319
320 /* create a client websocket using mirror protocol */
321
322 wsi_mirror = libwebsocket_client_connect(context,
323 address, port, use_ssl, "/",
324 argv[optind], argv[optind],
325 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
326
Andy Green4084af12011-05-24 22:06:17 +0100327 if (wsi_mirror == NULL) {
Andy Green8e0c9842013-02-09 14:10:04 +0800328 fprintf(stderr, "libwebsocket "
329 "dumb connect failed\n");
Andy Green93f98d72013-02-11 14:05:02 +0800330 ret = 1;
331 goto bail;
Andy Green4084af12011-05-24 22:06:17 +0100332 }
Andy Green8e0c9842013-02-09 14:10:04 +0800333
334 mirror_lifetime = 10 + (random() & 1023);
335 /* useful to test single connection stability */
336 if (longlived)
337 mirror_lifetime += 50000;
338
339 fprintf(stderr, "opened mirror connection with "
340 "%d lifetime\n", mirror_lifetime);
341
342 /*
343 * mirror_lifetime is decremented each send, when it reaches
344 * zero the connection is closed in the send callback.
345 * When the close callback comes, wsi_mirror is set to NULL
346 * so a new connection will be opened
347 */
Andy Green4084af12011-05-24 22:06:17 +0100348 }
349
Andy Green93f98d72013-02-11 14:05:02 +0800350bail:
Andy Greenb3ae0a32011-02-14 20:25:43 +0000351 fprintf(stderr, "Exiting\n");
352
Andy Green6964bb52011-01-23 16:50:33 +0000353 libwebsocket_context_destroy(context);
354
Andy Green93f98d72013-02-11 14:05:02 +0800355 return ret;
Andy Green4739e5c2011-01-22 12:51:57 +0000356
357usage:
358 fprintf(stderr, "Usage: libwebsockets-test-client "
Andy Green46ef0cf2013-01-10 22:28:59 +0800359 "<server address> [--port=<p>] "
360 "[--ssl] [-k] [-v <ver>] "
Andy Green43063dd2013-01-13 11:58:18 +0800361 "[-d <log bitfield>] [-l]\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000362 return 1;
363}