blob: d25e31f4b709e5e9e297c462bb4a341d1c37b732 [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 Greenca0a1292013-03-16 11:24:23 +0800125 fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
Andy Green4084af12011-05-24 22:06:17 +0100126 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
Andy Greenfc7c5e42013-02-23 10:50:10 +0800153 n = 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 Greenfc7c5e42013-02-23 10:50:10 +0800156 if (n < 0)
157 return -1;
158 if (n < l) {
159 lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
160 return -1;
161 }
162
Andy Green8e0c9842013-02-09 14:10:04 +0800163 mirror_lifetime--;
164 if (!mirror_lifetime) {
165 fprintf(stderr, "closing mirror session\n");
Andy Green310655b2013-02-11 14:08:50 +0800166 return -1;
Andy Green8e0c9842013-02-09 14:10:04 +0800167 } else
168 /* get notified as soon as we can write again */
169 libwebsocket_callback_on_writable(context, wsi);
Andy Green4739e5c2011-01-22 12:51:57 +0000170 break;
171
172 default:
173 break;
174 }
175
176 return 0;
177}
178
179
180/* list of supported protocols and callbacks */
181
182static struct libwebsocket_protocols protocols[] = {
Peter Hinz56885f32011-03-02 22:03:47 +0000183 {
184 "dumb-increment-protocol",
185 callback_dumb_increment,
186 0,
Andy Green54495112013-02-06 21:10:16 +0900187 20,
Andy Green4739e5c2011-01-22 12:51:57 +0000188 },
Peter Hinz56885f32011-03-02 22:03:47 +0000189 {
190 "lws-mirror-protocol",
191 callback_lws_mirror,
192 0,
Andy Greene7c97e82013-02-09 14:07:32 +0800193 128,
Andy Green4739e5c2011-01-22 12:51:57 +0000194 },
Andy Green54495112013-02-06 21:10:16 +0900195 { NULL, NULL, 0, 0 } /* end */
Andy Green4739e5c2011-01-22 12:51:57 +0000196};
197
Andy Green3886ec72013-02-11 14:32:02 +0800198void sighandler(int sig)
199{
200 force_exit = 1;
201}
202
Andy Green4739e5c2011-01-22 12:51:57 +0000203static struct option options[] = {
Andy Green90c7cbc2011-01-27 06:26:52 +0000204 { "help", no_argument, NULL, 'h' },
Andy Green46ef0cf2013-01-10 22:28:59 +0800205 { "debug", required_argument, NULL, 'd' },
Andy Green90c7cbc2011-01-27 06:26:52 +0000206 { "port", required_argument, NULL, 'p' },
207 { "ssl", no_argument, NULL, 's' },
Andy Greenbfb051f2011-02-09 08:49:14 +0000208 { "version", required_argument, NULL, 'v' },
Andy Green775884e2011-03-06 13:32:53 +0000209 { "undeflated", no_argument, NULL, 'u' },
Andy Green7448c7e2011-05-24 22:06:17 +0100210 { "nomux", no_argument, NULL, 'n' },
Andy Green43063dd2013-01-13 11:58:18 +0800211 { "longlived", no_argument, NULL, 'l' },
Andy Green4739e5c2011-01-22 12:51:57 +0000212 { NULL, 0, 0, 0 }
213};
214
215
216int main(int argc, char **argv)
217{
218 int n = 0;
Andy Green93f98d72013-02-11 14:05:02 +0800219 int ret = 0;
Andy Green4739e5c2011-01-22 12:51:57 +0000220 int port = 7681;
221 int use_ssl = 0;
222 struct libwebsocket_context *context;
Andy Green1efb63c2011-02-06 17:42:06 +0000223 const char *address;
Andy Green4739e5c2011-01-22 12:51:57 +0000224 struct libwebsocket *wsi_dumb;
Andy Greenbfb051f2011-02-09 08:49:14 +0000225 int ietf_version = -1; /* latest */
Andy Green43063dd2013-01-13 11:58:18 +0800226 int longlived = 0;
Andy Green1b265272013-02-09 14:01:09 +0800227 struct lws_context_creation_info info;
228
229 memset(&info, 0, sizeof info);
Andy Green90c7cbc2011-01-27 06:26:52 +0000230
Andy Green4739e5c2011-01-22 12:51:57 +0000231 fprintf(stderr, "libwebsockets test client\n"
Andy Green46ef0cf2013-01-10 22:28:59 +0800232 "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> "
Andy Green4739e5c2011-01-22 12:51:57 +0000233 "licensed under LGPL2.1\n");
234
235 if (argc < 2)
236 goto usage;
237
Andy Green4739e5c2011-01-22 12:51:57 +0000238 while (n >= 0) {
Andy Green5738c0e2013-01-21 09:53:35 +0800239 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
Andy Green4739e5c2011-01-22 12:51:57 +0000240 if (n < 0)
241 continue;
242 switch (n) {
Andy Green46ef0cf2013-01-10 22:28:59 +0800243 case 'd':
Andy Greende8f27a2013-01-12 09:17:42 +0800244 lws_set_log_level(atoi(optarg), NULL);
Andy Green46ef0cf2013-01-10 22:28:59 +0800245 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000246 case 's':
Andy Green90c7cbc2011-01-27 06:26:52 +0000247 use_ssl = 2; /* 2 = allow selfsigned */
Andy Green4739e5c2011-01-22 12:51:57 +0000248 break;
249 case 'p':
250 port = atoi(optarg);
251 break;
Andy Green43063dd2013-01-13 11:58:18 +0800252 case 'l':
253 longlived = 1;
254 break;
Andy Greenbfb051f2011-02-09 08:49:14 +0000255 case 'v':
256 ietf_version = atoi(optarg);
257 break;
Andy Green775884e2011-03-06 13:32:53 +0000258 case 'u':
259 deny_deflate = 1;
260 break;
Andy Green7448c7e2011-05-24 22:06:17 +0100261 case 'n':
262 deny_mux = 1;
263 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000264 case 'h':
265 goto usage;
266 }
267 }
268
Andy Green1efb63c2011-02-06 17:42:06 +0000269 if (optind >= argc)
270 goto usage;
271
Andy Green3886ec72013-02-11 14:32:02 +0800272 signal(SIGINT, sighandler);
273
Andy Green1efb63c2011-02-06 17:42:06 +0000274 address = argv[optind];
275
Andy Green4739e5c2011-01-22 12:51:57 +0000276 /*
277 * create the websockets context. This tracks open connections and
278 * knows how to route any traffic and which protocol version to use,
279 * and if each connection is client or server side.
280 *
281 * For this client-only demo, we tell it to not listen on any port.
282 */
283
Andy Green1b265272013-02-09 14:01:09 +0800284 info.port = CONTEXT_PORT_NO_LISTEN;
285 info.protocols = protocols;
Andy Green3182ece2013-01-20 17:08:31 +0800286#ifndef LWS_NO_EXTENSIONS
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800287 info.extensions = libwebsocket_get_internal_extensions();
Andy Green3182ece2013-01-20 17:08:31 +0800288#endif
Andy Green1b265272013-02-09 14:01:09 +0800289 info.gid = -1;
290 info.uid = -1;
291
292 context = libwebsocket_create_context(&info);
Andy Green4739e5c2011-01-22 12:51:57 +0000293 if (context == NULL) {
294 fprintf(stderr, "Creating libwebsocket context failed\n");
295 return 1;
296 }
297
Andy Green4739e5c2011-01-22 12:51:57 +0000298 /* create a client websocket using dumb increment protocol */
299
Andy Green90c7cbc2011-01-27 06:26:52 +0000300 wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
Andy Green1efb63c2011-02-06 17:42:06 +0000301 "/", argv[optind], argv[optind],
Andy Greenbfb051f2011-02-09 08:49:14 +0000302 protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
Andy Green4739e5c2011-01-22 12:51:57 +0000303
304 if (wsi_dumb == NULL) {
305 fprintf(stderr, "libwebsocket dumb connect failed\n");
Andy Green93f98d72013-02-11 14:05:02 +0800306 ret = 1;
307 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +0000308 }
309
Andy Green4739e5c2011-01-22 12:51:57 +0000310 fprintf(stderr, "Websocket connections opened\n");
311
312 /*
313 * sit there servicing the websocket context to handle incoming
314 * packets, and drawing random circles on the mirror protocol websocket
315 */
316
317 n = 0;
Andy Green3886ec72013-02-11 14:32:02 +0800318 while (n >= 0 && !was_closed && !force_exit) {
Andy Green8e0c9842013-02-09 14:10:04 +0800319 n = libwebsocket_service(context, 10);
Andy Green4739e5c2011-01-22 12:51:57 +0000320
Andy Green3928f612012-07-20 12:58:38 +0800321 if (n < 0)
322 continue;
323
Andy Green8e0c9842013-02-09 14:10:04 +0800324 if (wsi_mirror)
325 continue;
326
327 /* create a client websocket using mirror protocol */
328
329 wsi_mirror = libwebsocket_client_connect(context,
330 address, port, use_ssl, "/",
331 argv[optind], argv[optind],
332 protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
333
Andy Green4084af12011-05-24 22:06:17 +0100334 if (wsi_mirror == NULL) {
Andy Green8e0c9842013-02-09 14:10:04 +0800335 fprintf(stderr, "libwebsocket "
336 "dumb connect failed\n");
Andy Green93f98d72013-02-11 14:05:02 +0800337 ret = 1;
338 goto bail;
Andy Green4084af12011-05-24 22:06:17 +0100339 }
Andy Green8e0c9842013-02-09 14:10:04 +0800340
341 mirror_lifetime = 10 + (random() & 1023);
342 /* useful to test single connection stability */
343 if (longlived)
344 mirror_lifetime += 50000;
345
346 fprintf(stderr, "opened mirror connection with "
347 "%d lifetime\n", mirror_lifetime);
348
349 /*
350 * mirror_lifetime is decremented each send, when it reaches
351 * zero the connection is closed in the send callback.
352 * When the close callback comes, wsi_mirror is set to NULL
353 * so a new connection will be opened
354 */
Andy Green4084af12011-05-24 22:06:17 +0100355 }
356
Andy Green93f98d72013-02-11 14:05:02 +0800357bail:
Andy Greenb3ae0a32011-02-14 20:25:43 +0000358 fprintf(stderr, "Exiting\n");
359
Andy Green6964bb52011-01-23 16:50:33 +0000360 libwebsocket_context_destroy(context);
361
Andy Green93f98d72013-02-11 14:05:02 +0800362 return ret;
Andy Green4739e5c2011-01-22 12:51:57 +0000363
364usage:
365 fprintf(stderr, "Usage: libwebsockets-test-client "
Andy Green46ef0cf2013-01-10 22:28:59 +0800366 "<server address> [--port=<p>] "
367 "[--ssl] [-k] [-v <ver>] "
Andy Green43063dd2013-01-13 11:58:18 +0800368 "[-d <log bitfield>] [-l]\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000369 return 1;
370}