clean windows warnings
[platform/upstream/libwebsockets.git] / test-server / test-client.c
1 /*
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 <getopt.h>
25 #include <string.h>
26 #include <signal.h>
27
28 #ifdef _WIN32
29 #define random rand
30 #include "gettimeofday.h"
31 #else
32 #include <syslog.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #endif
36
37 #include "../lib/libwebsockets.h"
38
39 static int deny_deflate, deny_mux, longlived, mirror_lifetime;
40 static struct lws *wsi_dumb, *wsi_mirror;
41 static volatile int force_exit;
42 static unsigned int opts;
43
44 /*
45  * This demo shows how to connect multiple websockets simultaneously to a
46  * websocket server (there is no restriction on their having to be the same
47  * server just it simplifies the demo).
48  *
49  *  dumb-increment-protocol:  we connect to the server and print the number
50  *                              we are given
51  *
52  *  lws-mirror-protocol: draws random circles, which are mirrored on to every
53  *                              client (see them being drawn in every browser
54  *                              session also using the test server)
55  */
56
57 enum demo_protocols {
58
59         PROTOCOL_DUMB_INCREMENT,
60         PROTOCOL_LWS_MIRROR,
61
62         /* always last */
63         DEMO_PROTOCOL_COUNT
64 };
65
66
67 /*
68  * dumb_increment protocol
69  *
70  * since this also happens to be protocols[0], some callbacks that are not
71  * bound to a specific protocol also turn up here.
72  */
73
74 static int
75 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
76                         void *user, void *in, size_t len)
77 {
78         switch (reason) {
79
80         case LWS_CALLBACK_CLIENT_ESTABLISHED:
81                 lwsl_info("dumb: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
82                 break;
83
84         case LWS_CALLBACK_CLOSED:
85                 lwsl_notice("dumb: LWS_CALLBACK_CLOSED\n");
86                 wsi_dumb = NULL;
87                 break;
88
89         case LWS_CALLBACK_CLIENT_RECEIVE:
90                 ((char *)in)[len] = '\0';
91                 lwsl_info("rx %d '%s'\n", (int)len, (char *)in);
92                 break;
93
94         /* because we are protocols[0] ... */
95
96         case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
97                 if (wsi == wsi_dumb) {
98                         lwsl_err("dumb: LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
99                         wsi_dumb = NULL;
100                 }
101                 if (wsi == wsi_mirror) {
102                         lwsl_err("mirror: LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
103                         wsi_mirror = NULL;
104                 }
105                 break;
106
107         case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
108                 if ((strcmp(in, "deflate-stream") == 0) && deny_deflate) {
109                         lwsl_notice("denied deflate-stream extension\n");
110                         return 1;
111                 }
112                 if ((strcmp(in, "deflate-frame") == 0) && deny_deflate) {
113                         lwsl_notice("denied deflate-frame extension\n");
114                         return 1;
115                 }
116                 if ((strcmp(in, "x-google-mux") == 0) && deny_mux) {
117                         lwsl_notice("denied x-google-mux extension\n");
118                         return 1;
119                 }
120                 break;
121
122         default:
123                 break;
124         }
125
126         return 0;
127 }
128
129
130 /* lws-mirror_protocol */
131
132
133 static int
134 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
135                     void *user, void *in, size_t len)
136 {
137         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096];
138         unsigned int rands[4];
139         int l = 0;
140         int n;
141
142         switch (reason) {
143         case LWS_CALLBACK_CLIENT_ESTABLISHED:
144
145                 lwsl_notice("mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
146
147                 lws_get_random(lws_get_context(wsi), rands, sizeof(rands[0]));
148                 mirror_lifetime = 16384 + (rands[0] & 65535);
149                 /* useful to test single connection stability */
150                 if (longlived)
151                         mirror_lifetime += 500000;
152
153                 lwsl_info("opened mirror connection with "
154                           "%d lifetime\n", mirror_lifetime);
155
156                 /*
157                  * mirror_lifetime is decremented each send, when it reaches
158                  * zero the connection is closed in the send callback.
159                  * When the close callback comes, wsi_mirror is set to NULL
160                  * so a new connection will be opened
161                  *
162                  * start the ball rolling,
163                  * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
164                  */
165                 lws_callback_on_writable(wsi);
166                 break;
167
168         case LWS_CALLBACK_CLOSED:
169                 lwsl_notice("mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
170                 wsi_mirror = NULL;
171                 break;
172
173         case LWS_CALLBACK_CLIENT_WRITEABLE:
174                 for (n = 0; n < 1; n++) {
175                         lws_get_random(lws_get_context(wsi), rands, sizeof(rands));
176                         l += sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING + l],
177                                         "c #%06X %d %d %d;",
178                                         (int)rands[0] & 0xffffff,
179                                         (int)rands[1] % 500,
180                                         (int)rands[2] % 250,
181                                         (int)rands[3] % 24);
182                 }
183
184                 n = lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], l,
185                               opts | LWS_WRITE_TEXT);
186                 if (n < 0)
187                         return -1;
188                 if (n < l) {
189                         lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
190                         return -1;
191                 }
192
193                 mirror_lifetime--;
194                 if (!mirror_lifetime) {
195                         lwsl_info("closing mirror session\n");
196                         return -1;
197                 }
198                 /* get notified as soon as we can write again */
199                 lws_callback_on_writable(wsi);
200                 break;
201
202         default:
203                 break;
204         }
205
206         return 0;
207 }
208
209
210 /* list of supported protocols and callbacks */
211
212 static struct lws_protocols protocols[] = {
213         {
214                 "dumb-increment-protocol,fake-nonexistant-protocol",
215                 callback_dumb_increment,
216                 0,
217                 20,
218         },
219         {
220                 "fake-nonexistant-protocol,lws-mirror-protocol",
221                 callback_lws_mirror,
222                 0,
223                 128,
224         },
225         { NULL, NULL, 0, 0 } /* end */
226 };
227
228 void sighandler(int sig)
229 {
230         force_exit = 1;
231 }
232
233 static struct option options[] = {
234         { "help",       no_argument,            NULL, 'h' },
235         { "debug",      required_argument,      NULL, 'd' },
236         { "port",       required_argument,      NULL, 'p' },
237         { "ssl",        no_argument,            NULL, 's' },
238         { "version",    required_argument,      NULL, 'v' },
239         { "undeflated", no_argument,            NULL, 'u' },
240         { "nomux",      no_argument,            NULL, 'n' },
241         { "longlived",  no_argument,            NULL, 'l' },
242         { NULL, 0, 0, 0 }
243 };
244
245 static int ratelimit_connects(unsigned int *last, unsigned int secs)
246 {
247         struct timeval tv;
248
249         gettimeofday(&tv, NULL);
250         if (tv.tv_sec - (*last) < secs)
251                 return 0;
252
253         *last = tv.tv_sec;
254
255         return 1;
256 }
257
258 int main(int argc, char **argv)
259 {
260         int n = 0, ret = 0, port = 7681, use_ssl = 0;
261         unsigned int rl_dumb = 0, rl_mirror = 0;
262         struct lws_context_creation_info info;
263         struct lws_context *context;
264         int ietf_version = -1; /* latest */
265         const char *address;
266
267         memset(&info, 0, sizeof info);
268
269         fprintf(stderr, "libwebsockets test client\n"
270                         "(C) Copyright 2010-2015 Andy Green <andy@warmcat.com> "
271                         "licensed under LGPL2.1\n");
272
273         if (argc < 2)
274                 goto usage;
275
276         while (n >= 0) {
277                 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
278                 if (n < 0)
279                         continue;
280                 switch (n) {
281                 case 'd':
282                         lws_set_log_level(atoi(optarg), NULL);
283                         break;
284                 case 's':
285                         use_ssl = 2; /* 2 = allow selfsigned */
286                         break;
287                 case 'p':
288                         port = atoi(optarg);
289                         break;
290                 case 'l':
291                         longlived = 1;
292                         break;
293                 case 'v':
294                         ietf_version = atoi(optarg);
295                         break;
296                 case 'u':
297                         deny_deflate = 1;
298                         break;
299                 case 'n':
300                         deny_mux = 1;
301                         break;
302                 case 'h':
303                         goto usage;
304                 }
305         }
306
307         if (optind >= argc)
308                 goto usage;
309
310         signal(SIGINT, sighandler);
311
312         address = argv[optind];
313
314         /*
315          * create the websockets context.  This tracks open connections and
316          * knows how to route any traffic and which protocol version to use,
317          * and if each connection is client or server side.
318          *
319          * For this client-only demo, we tell it to not listen on any port.
320          */
321
322         info.port = CONTEXT_PORT_NO_LISTEN;
323         info.protocols = protocols;
324 #ifndef LWS_NO_EXTENSIONS
325         info.extensions = lws_get_internal_extensions();
326 #endif
327         info.gid = -1;
328         info.uid = -1;
329
330         context = lws_create_context(&info);
331         if (context == NULL) {
332                 fprintf(stderr, "Creating libwebsocket context failed\n");
333                 return 1;
334         }
335
336         /*
337          * sit there servicing the websocket context to handle incoming
338          * packets, and drawing random circles on the mirror protocol websocket
339          *
340          * nothing happens until the client websocket connection is
341          * asynchronously established... calling lws_client_connect() only
342          * instantiates the connection logically, lws_service() progresses it
343          * asynchronously.
344          */
345
346         while (!force_exit) {
347
348                 if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
349                         lwsl_notice("dumb: connecting\n");
350                         wsi_dumb = lws_client_connect(context, address, port,
351                                 use_ssl, "/", argv[optind], argv[optind],
352                                 protocols[PROTOCOL_DUMB_INCREMENT].name,
353                                 ietf_version);
354                 }
355
356                 if (!wsi_mirror && ratelimit_connects(&rl_mirror, 2u)) {
357                         lwsl_notice("mirror: connecting\n");
358                         wsi_mirror = lws_client_connect(context,
359                                 address, port, use_ssl,  "/",
360                                 argv[optind], argv[optind],
361                                 protocols[PROTOCOL_LWS_MIRROR].name,
362                                 ietf_version);
363                 }
364
365                 lws_service(context, 500);
366         }
367
368         lwsl_err("Exiting\n");
369         lws_context_destroy(context);
370
371         return ret;
372
373 usage:
374         fprintf(stderr, "Usage: libwebsockets-test-client "
375                                 "<server address> [--port=<p>] "
376                                 "[--ssl] [-k] [-v <ver>] "
377                                 "[-d <log bitfield>] [-l]\n");
378         return 1;
379 }