update test echo for iface info member namechange
[profile/ivi/libwebsockets.git] / test-server / test-ping.c
1 /*
2  * libwebsockets-test-ping - libwebsockets floodping
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>
27 #include <signal.h>
28 #include <unistd.h>
29
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #ifndef WIN32
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <poll.h>
36 #endif
37
38 #include <netdb.h>
39
40 #include "../lib/libwebsockets.h"
41
42 /*
43  * this is specified in the 04 standard, control frames can only have small
44  * payload length styles
45  */
46 #define MAX_PING_PAYLOAD 125
47 #define MAX_MIRROR_PAYLOAD 4096
48 #define MAX_PING_CLIENTS 256
49 #define PING_RINGBUFFER_SIZE 256
50
51 static struct libwebsocket *ping_wsi[MAX_PING_CLIENTS];
52 static unsigned int interval_us = 1000000;
53 static unsigned int size = 64;
54 static int flood;
55 static const char *address;
56 static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD +
57                                                   LWS_SEND_BUFFER_POST_PADDING];
58 static char peer_name[128];
59 static unsigned long started;
60 static int screen_width = 80;
61 static int use_mirror;
62 static unsigned int write_options;
63
64 static unsigned long rtt_min = 100000000;
65 static unsigned long rtt_max;
66 static unsigned long rtt_avg;
67 static unsigned long global_rx_count;
68 static unsigned long global_tx_count;
69 static int clients = 1;
70 static unsigned long interrupted_time;
71
72 struct ping {
73         unsigned long issue_timestamp;
74         unsigned long index;
75         unsigned int seen;
76 };
77
78 struct per_session_data__ping {
79         unsigned long ping_index;
80
81         struct ping ringbuffer[PING_RINGBUFFER_SIZE];
82         int ringbuffer_head;
83         int ringbuffer_tail;
84
85         unsigned long rx_count;
86 };
87
88 /*
89  * uses the ping pong protocol features to provide an equivalent for the
90  * ping utility for 04+ websockets
91  */
92
93 enum demo_protocols {
94
95         PROTOCOL_LWS_MIRROR,
96
97         /* always last */
98         DEMO_PROTOCOL_COUNT
99 };
100
101
102 static int
103 callback_lws_mirror(struct libwebsocket_context * this,
104                         struct libwebsocket *wsi,
105                         enum libwebsocket_callback_reasons reason,
106                                                void *user, void *in, size_t len)
107 {
108         struct timeval tv;
109         unsigned char *p;
110         int shift;
111         unsigned long l;
112         unsigned long iv;
113         int n;
114         int match = 0;
115         struct per_session_data__ping *psd = user;
116
117         switch (reason) {
118         case LWS_CALLBACK_CLOSED:
119
120                 fprintf(stderr, "LWS_CALLBACK_CLOSED on %p\n", (void *)wsi);
121
122                 /* remove closed guy */
123         
124                 for (n = 0; n < clients; n++)
125                         if (ping_wsi[n] == wsi) {                               
126                                 clients--;
127                                 while (n < clients) {
128                                         ping_wsi[n] = ping_wsi[n + 1];
129                                         n++;
130                                 }
131                         }
132
133                 break;
134
135         case LWS_CALLBACK_CLIENT_ESTABLISHED:
136
137                 psd->rx_count = 0;
138                 psd->ping_index = 1;
139                 psd->ringbuffer_head = 0;
140                 psd->ringbuffer_tail = 0;
141
142                 /*
143                  * start the ball rolling,
144                  * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
145                  */
146
147                 libwebsocket_callback_on_writable(this, wsi);
148                 break;
149
150         case LWS_CALLBACK_CLIENT_RECEIVE:
151         case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
152                 gettimeofday(&tv, NULL);
153                 iv = (tv.tv_sec * 1000000) + tv.tv_usec;
154
155                 psd->rx_count++;
156
157                 shift = 56;
158                 p = in;
159                 l = 0;
160
161                 while (shift >= 0) {
162                         l |= (*p++) << shift;
163                         shift -= 8;
164                 }
165
166                 /* find it in the ringbuffer, look backwards from head */
167                 n = psd->ringbuffer_head;
168                 while (!match) {
169
170                         if (psd->ringbuffer[n].index == l) {
171                                 psd->ringbuffer[n].seen++;
172                                 match = 1;
173                                 continue;
174                         }
175
176                         if (n == psd->ringbuffer_tail) {
177                                 match = -1;
178                                 continue;
179                         }
180
181                         if (n == 0)
182                                 n = PING_RINGBUFFER_SIZE - 1;
183                         else
184                                 n--;
185                 }
186
187                 if (match < 1) {
188
189                         if (!flood)
190                                 fprintf(stderr, "%d bytes from %s: req=%ld "
191                                       "time=(unknown)\n", (int)len, address, l);
192                         else
193                                 fprintf(stderr, "\b \b");
194
195                         break;
196                 }
197
198                 if (psd->ringbuffer[n].seen > 1)
199                         fprintf(stderr, "DUP! ");
200
201                 if ((iv - psd->ringbuffer[n].issue_timestamp) < rtt_min)
202                         rtt_min = iv - psd->ringbuffer[n].issue_timestamp;
203
204                 if ((iv - psd->ringbuffer[n].issue_timestamp) > rtt_max)
205                         rtt_max = iv - psd->ringbuffer[n].issue_timestamp;
206
207                 rtt_avg += iv - psd->ringbuffer[n].issue_timestamp;
208                 global_rx_count++;
209
210                 if (!flood)
211                         fprintf(stderr, "%d bytes from %s: req=%ld "
212                                 "time=%lu.%lums\n", (int)len, address, l,
213                                (iv - psd->ringbuffer[n].issue_timestamp) / 1000,
214                         ((iv - psd->ringbuffer[n].issue_timestamp) / 100) % 10);
215                 else
216                         fprintf(stderr, "\b \b");
217                 break;
218
219         case LWS_CALLBACK_CLIENT_WRITEABLE:
220
221                 shift = 56;
222                 p = &pingbuf[LWS_SEND_BUFFER_PRE_PADDING];
223
224                 /* 64-bit ping index in network byte order */
225
226                 while (shift >= 0) {
227                         *p++ = psd->ping_index >> shift;
228                         shift -= 8;
229                 }
230
231                 while (p - &pingbuf[LWS_SEND_BUFFER_PRE_PADDING] < size)
232                         *p++ = 0;
233
234                 gettimeofday(&tv, NULL);
235
236                 psd->ringbuffer[psd->ringbuffer_head].issue_timestamp =
237                                              (tv.tv_sec * 1000000) + tv.tv_usec;
238                 psd->ringbuffer[psd->ringbuffer_head].index = psd->ping_index++;
239                 psd->ringbuffer[psd->ringbuffer_head].seen = 0;
240
241                 if (psd->ringbuffer_head == PING_RINGBUFFER_SIZE - 1)
242                         psd->ringbuffer_head = 0;
243                 else
244                         psd->ringbuffer_head++;
245
246                 /* snip any re-used tail so we keep to the ring length */
247
248                 if (psd->ringbuffer_tail == psd->ringbuffer_head) {
249                         if (psd->ringbuffer_tail == PING_RINGBUFFER_SIZE - 1)
250                                 psd->ringbuffer_tail = 0;
251                         else
252                                 psd->ringbuffer_tail++;
253                 }
254
255                 global_tx_count++;
256
257                 if (use_mirror)
258                         libwebsocket_write(wsi,
259                                 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
260                                         size, write_options | LWS_WRITE_BINARY);
261                 else
262                         libwebsocket_write(wsi,
263                                 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
264                                         size, write_options | LWS_WRITE_PING);
265
266                 if (flood &&
267                          (psd->ping_index - psd->rx_count) < (screen_width - 1))
268                         fprintf(stderr, ".");
269                 break;
270
271         default:
272                 break;
273         }
274
275         return 0;
276 }
277
278
279 /* list of supported protocols and callbacks */
280
281 static struct libwebsocket_protocols protocols[] = {
282
283         {
284                 "lws-mirror-protocol",
285                 callback_lws_mirror,
286                 sizeof (struct per_session_data__ping),
287         },
288         { 
289                 NULL, NULL, 0/* end of list */          
290         }
291 };
292
293 static struct option options[] = {
294         { "help",       no_argument,            NULL, 'h' },
295         { "debug",      required_argument,      NULL, 'd' },
296         { "port",       required_argument,      NULL, 'p' },
297         { "ssl",        no_argument,            NULL, 't' },
298         { "interval",   required_argument,      NULL, 'i' },
299         { "size",       required_argument,      NULL, 's' },
300         { "protocol",   required_argument,      NULL, 'n' },
301         { "flood",      no_argument,            NULL, 'f' },
302         { "mirror",     no_argument,            NULL, 'm' },
303         { "replicate",  required_argument,      NULL, 'r' },
304         { "killmask",   no_argument,            NULL, 'k' },
305         { "version",    required_argument,      NULL, 'v' },
306         { NULL, 0, 0, 0 }
307 };
308
309 #ifndef WIN32
310 static void
311 signal_handler(int sig, siginfo_t *si, void *v)
312 {
313         struct timeval tv;
314
315         gettimeofday(&tv, NULL);
316         interrupted_time = (tv.tv_sec * 1000000) + tv.tv_usec;
317 }
318 #endif
319
320 int main(int argc, char **argv)
321 {
322         int n = 0;
323         int port = 7681;
324         int use_ssl = 0;
325         struct libwebsocket_context *context;
326         char protocol_name[256];
327         char ip[30];
328 #ifndef WIN32
329         struct sigaction sa;
330         struct winsize w;
331 #endif
332         struct timeval tv;
333         unsigned long oldus = 0;
334         unsigned long l;
335         int ietf_version = -1;
336         struct lws_context_creation_info info;
337
338         memset(&info, 0, sizeof info);
339
340         if (argc < 2)
341                 goto usage;
342
343         address = argv[1];
344         optind++;
345
346         while (n >= 0) {
347                 n = getopt_long(argc, argv, "v:kr:hmfts:n:i:p:d:", options, NULL);
348                 if (n < 0)
349                         continue;
350                 switch (n) {
351                 case 'd':
352                         lws_set_log_level(atoi(optarg), NULL);
353                         break;
354                 case 'm':
355                         use_mirror = 1;
356                         break;
357                 case 't':
358                         use_ssl = 2; /* 2 = allow selfsigned */
359                         break;
360                 case 'p':
361                         port = atoi(optarg);
362                         break;
363                 case 'n':
364                         strncpy(protocol_name, optarg, sizeof protocol_name);
365                         protocol_name[(sizeof protocol_name) - 1] = '\0';
366                         protocols[PROTOCOL_LWS_MIRROR].name = protocol_name;
367                         break;
368                 case 'i':
369                         interval_us = 1000000.0 * atof(optarg);
370                         break;
371                 case 's':
372                         size = atoi(optarg);
373                         break;
374                 case 'f':
375                         flood = 1;
376                         break;
377                 case 'r':
378                         clients = atoi(optarg);
379                         if (clients > MAX_PING_CLIENTS || clients < 1) {
380                                 fprintf(stderr, "Max clients supportd = %d\n",
381                                                               MAX_PING_CLIENTS);
382                                 return 1;
383                         }
384                         break;
385                 case 'k':
386                         write_options = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
387                         break;
388                 case 'v':
389                         ietf_version = atoi(optarg);
390                         break;
391
392                 case 'h':
393                         goto usage;
394                 }
395         }
396
397         if (!use_mirror) {
398                 if (size > MAX_PING_PAYLOAD) {
399                         fprintf(stderr, "Max ping opcode payload size %d\n",
400                                                               MAX_PING_PAYLOAD);
401                         return 1;
402                 }
403         } else {
404                 if (size > MAX_MIRROR_PAYLOAD) {
405                         fprintf(stderr, "Max mirror payload size %d\n",
406                                                             MAX_MIRROR_PAYLOAD);
407                         return 1;
408                 }
409         }
410
411 #ifndef WIN32
412         if (isatty(STDOUT_FILENO))
413                 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
414                         if (w.ws_col > 0)
415                                 screen_width = w.ws_col;
416 #endif
417
418         info.port = CONTEXT_PORT_NO_LISTEN;
419         info.protocols = protocols;
420 #ifndef LWS_NO_EXTENSIONS
421         info.extensions = libwebsocket_internal_extensions;
422 #endif
423         info.gid = -1;
424         info.uid = -1;
425
426         context = libwebsocket_create_context(&info);
427         if (context == NULL) {
428                 fprintf(stderr, "Creating libwebsocket context failed\n");
429                 return 1;
430         }
431
432         /* create client websockets using dumb increment protocol */
433
434         for (n = 0; n < clients; n++) {
435                 ping_wsi[n] = libwebsocket_client_connect(context, address,
436                                                    port, use_ssl, "/", address,
437                                  "origin", protocols[PROTOCOL_LWS_MIRROR].name,
438                                                                   ietf_version);
439                 if (ping_wsi[n] == NULL) {
440                         fprintf(stderr, "client connection %d failed to "
441                                                                 "connect\n", n);
442                         return 1;
443                 }
444         }
445
446         libwebsockets_get_peer_addresses(context, ping_wsi[0],
447                         libwebsocket_get_socket_fd(ping_wsi[0]),
448                                     peer_name, sizeof peer_name, ip, sizeof ip);
449
450         fprintf(stderr, "Websocket PING %s (%s) %d bytes of data.\n",
451                                                            peer_name, ip, size);
452
453 #ifndef WIN32
454         /* set the ^C handler */
455         sa.sa_sigaction = signal_handler;
456         sa.sa_flags = SA_SIGINFO;
457         sigemptyset(&sa.sa_mask);
458         sigaction(SIGINT, &sa, NULL);
459 #endif
460
461         gettimeofday(&tv, NULL);
462         started = (tv.tv_sec * 1000000) + tv.tv_usec;
463
464         /* service loop */
465
466         n = 0;
467         while (n >= 0) {
468
469                 gettimeofday(&tv, NULL);
470                 l = (tv.tv_sec * 1000000) + tv.tv_usec;
471
472                 /* servers can hang up on us */
473
474                 if (clients == 0) {
475                         n = -1;
476                         continue;
477                 }
478
479                 if (!interrupted_time) {
480                         if ((l - oldus) > interval_us) {
481                                 for (n = 0; n < clients; n++)
482                                         libwebsocket_callback_on_writable(
483                                                           context, ping_wsi[n]);
484                                 oldus = l;
485                         }
486                 } else
487
488                         /* allow time for in-flight pongs to come */
489                 
490                         if ((l - interrupted_time) > 250000) {
491                                 n = -1;
492                                 continue;
493                         }
494
495                 if (!interval_us)
496                         n = libwebsocket_service(context, 0);
497                 else
498                         n = libwebsocket_service(context, 1);
499         }
500
501         /* stats */
502
503         fprintf(stderr, "\n--- %s websocket ping statistics "
504                 "using %d connections ---\n"
505                 "%lu packets transmitted, %lu received, "
506                 "%lu%% packet loss, time %ldms\n"
507                 "rtt min/avg/max = %0.3f/%0.3f/%0.3f ms\n"
508                 "payload bandwidth average %0.3f KiBytes/sec\n",
509                 peer_name, clients, global_tx_count, global_rx_count,
510                 ((global_tx_count - global_rx_count) * 100) / global_tx_count,
511                 (l - started) / 1000,
512                 ((double)rtt_min) / 1000.0,
513                 ((double)rtt_avg / global_rx_count) / 1000.0,
514                 ((double)rtt_max) / 1000.0,
515                 ((double)global_rx_count * (double)size) /
516                                   ((double)(l - started) / 1000000.0) / 1024.0);
517
518         libwebsocket_context_destroy(context);
519
520         return 0;
521
522 usage:
523         fprintf(stderr, "Usage: libwebsockets-test-ping "
524                                              "<server address> [--port=<p>] "
525                                              "[--ssl] [--interval=<float sec>] "
526                                              "[--size=<bytes>] "
527                                              "[--protocol=<protocolname>] "
528                                              "[--mirror] "
529                                              "[--replicate=clients>] "
530                                              "[--version <version>] "
531                                              "[-d <log bitfield> ]"
532                                              "\n");
533         return 1;
534 }