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