ping test app: avoid FPE when no packets received
[platform/upstream/libwebsockets.git] / test-server / test-ping.c
index 297dbb5..3917781 100644 (file)
@@ -1,22 +1,21 @@
 /*
- * libwebsockets-test-ping - libwebsockets floodping
+ * libwebsockets-test-ping - libwebsockets test floodping
  *
  * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
  *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Lesser General Public
- *  License as published by the Free Software Foundation:
- *  version 2.1 of the License.
+ * This file is made available under the Creative Commons CC0 1.0
+ * Universal Public Domain Dedication.
  *
- *  This library is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *  Lesser General Public License for more details.
+ * The person who associated a work with this deed has dedicated
+ * the work to the public domain by waiving all of his or her rights
+ * to the work worldwide under copyright law, including all related
+ * and neighboring rights, to the extent allowed by law. You can copy,
+ * modify, distribute and perform the work, even for commercial purposes,
+ * all without asking permission.
  *
- *  You should have received a copy of the GNU Lesser General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- *  MA  02110-1301  USA
+ * The test apps are intended to be adapted for use in your code, which
+ * may be proprietary. So unlike the library itself, they are licensed
+ * Public Domain.
  */
 
 #include <stdio.h>
 #include <termiosh>
 #endif
 
+#ifdef __sun
+#include <sys/termios.h>
+#endif
+
 /*
  * this is specified in the 04 standard, control frames can only have small
  * payload length styles
@@ -79,7 +82,7 @@ struct ping {
 };
 
 struct per_session_data__ping {
-       uint64_t ping_index;
+       unsigned long long ping_index;
 
        struct ping ringbuffer[PING_RINGBUFFER_SIZE];
        int ringbuffer_head;
@@ -111,7 +114,7 @@ callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
        unsigned char *p;
        unsigned long iv;
        int match = 0;
-       uint64_t l;
+       unsigned long long l;
        int shift;
        int n;
 
@@ -159,7 +162,7 @@ callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
                l = 0;
 
                while (shift >= 0) {
-                       l |= ((uint64_t)*p++) << shift;
+                       l |= ((lws_intptr_t)*p++) << shift;
                        shift -= 8;
                }
 
@@ -298,6 +301,20 @@ static struct lws_protocols protocols[] = {
        }
 };
 
+static const struct lws_extension exts[] = {
+       {
+               "permessage-deflate",
+               lws_extension_callback_pm_deflate,
+               "permessage-deflate; client_no_context_takeover; client_max_window_bits"
+       },
+       {
+               "deflate-frame",
+               lws_extension_callback_pm_deflate,
+               "deflate_frame"
+       },
+       { NULL, NULL, NULL /* terminator */ }
+};
+
 static struct option options[] = {
        { "help",       no_argument,            NULL, 'h' },
        { "debug",      required_argument,      NULL, 'd' },
@@ -331,7 +348,7 @@ int main(int argc, char **argv)
        int port = 7681;
        int use_ssl = 0;
        struct lws_context *context;
-       char protocol_name[256];
+       char protocol_name[256], ads_port[300];
        char ip[30];
 #ifndef _WIN32
        struct sigaction sa;
@@ -342,15 +359,13 @@ int main(int argc, char **argv)
        unsigned long l;
        int ietf_version = -1;
        struct lws_context_creation_info info;
+       struct lws_client_connect_info i;
 
        memset(&info, 0, sizeof info);
 
        if (argc < 2)
                goto usage;
 
-       address = argv[1];
-       optind++;
-
        while (n >= 0) {
                n = getopt_long(argc, argv, "v:kr:hmfts:n:i:p:d:", options, NULL);
                if (n < 0)
@@ -385,7 +400,7 @@ int main(int argc, char **argv)
                case 'r':
                        clients = atoi(optarg);
                        if (clients > MAX_PING_CLIENTS || clients < 1) {
-                               fprintf(stderr, "Max clients supportd = %d\n",
+                               fprintf(stderr, "Max clients supported = %d\n",
                                                              MAX_PING_CLIENTS);
                                return 1;
                        }
@@ -425,12 +440,14 @@ int main(int argc, char **argv)
 
        info.port = CONTEXT_PORT_NO_LISTEN;
        info.protocols = protocols;
-#ifndef LWS_NO_EXTENSIONS
-       info.extensions = lws_get_internal_extensions();
-#endif
+       info.extensions = exts;
+
        info.gid = -1;
        info.uid = -1;
 
+       if (use_ssl)
+               info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
+
        context = lws_create_context(&info);
        if (context == NULL) {
                fprintf(stderr, "Creating libwebsocket context failed\n");
@@ -439,14 +456,25 @@ int main(int argc, char **argv)
 
        /* create client websockets using dumb increment protocol */
 
+       address = argv[optind];
+       lws_snprintf(ads_port, sizeof(ads_port), "%s:%u",
+                address, port & 65535);
+       lwsl_notice("Connecting to %s...\n", ads_port);
+       memset(&i, 0, sizeof(i));
+       i.context = context;
+       i.address = address;
+       i.port = port;
+       i.ssl_connection = use_ssl;
+       i.path = "/";
+       i.host = ads_port;
+       i.origin = ads_port;
+       i.protocol = protocols[PROTOCOL_LWS_MIRROR].name;
+       i.ietf_version_or_minus_one = ietf_version;
+
        for (n = 0; n < clients; n++) {
-               ping_wsi[n] = lws_client_connect(context, address,
-                                                  port, use_ssl, "/", address,
-                                "origin", protocols[PROTOCOL_LWS_MIRROR].name,
-                                                                 ietf_version);
+               ping_wsi[n] = lws_client_connect_via_info(&i);
                if (ping_wsi[n] == NULL) {
-                       fprintf(stderr, "client connection %d failed to "
-                                                               "connect\n", n);
+                       lwsl_err("client %d failed to connect\n", n);
                        return 1;
                }
        }
@@ -508,7 +536,8 @@ int main(int argc, char **argv)
 
        /* stats */
 
-       fprintf(stderr, "\n--- %s websocket ping statistics "
+       if (global_rx_count && global_tx_count)
+               fprintf(stderr, "\n--- %s websocket ping statistics "
                "using %d connections ---\n"
                "%lu packets transmitted, %lu received, "
                "%lu%% packet loss, time %ldms\n"