2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-client - libwebsockets test implementation
|
|
|
|
*
|
2019-05-01 07:57:56 +01:00
|
|
|
* Written in 2010-2019 by Andy Green <andy@warmcat.com>
|
2011-01-22 12:51:57 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +08:00
|
|
|
* This file is made available under the Creative Commons CC0 1.0
|
|
|
|
* Universal Public Domain Dedication.
|
2011-01-22 12:51:57 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +08:00
|
|
|
* 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.
|
2011-01-22 12:51:57 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +08:00
|
|
|
* 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.
|
2011-01-22 12:51:57 +00:00
|
|
|
*/
|
2016-05-03 07:26:10 +08:00
|
|
|
|
2016-04-26 22:51:20 +02:00
|
|
|
#include "lws_config.h"
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-01-11 16:48:53 +08:00
|
|
|
#if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
|
2011-01-22 12:51:57 +00:00
|
|
|
#include <getopt.h>
|
2019-01-11 16:48:53 +08:00
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
#include <string.h>
|
2013-02-11 14:32:02 +08:00
|
|
|
#include <signal.h>
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2014-03-29 08:05:07 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define random rand
|
2015-12-09 07:10:03 +08:00
|
|
|
#include "gettimeofday.h"
|
2014-03-29 08:05:07 +01:00
|
|
|
#else
|
2015-12-09 07:10:03 +08:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/time.h>
|
2014-03-29 07:43:38 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-23 09:46:01 +08:00
|
|
|
#include <libwebsockets.h>
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2017-11-05 06:47:56 +08:00
|
|
|
struct lws_poly_gen {
|
|
|
|
uint32_t cyc[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define block_size (3 * 4096)
|
|
|
|
|
2017-11-14 11:25:54 +08:00
|
|
|
static int deny_deflate, longlived, mirror_lifetime, test_post, once;
|
2015-12-09 07:10:03 +08:00
|
|
|
static struct lws *wsi_dumb, *wsi_mirror;
|
2016-08-07 08:33:08 +08:00
|
|
|
static struct lws *wsi_multi[3];
|
2015-12-09 08:07:38 +08:00
|
|
|
static volatile int force_exit;
|
2016-08-07 08:33:08 +08:00
|
|
|
static unsigned int opts, rl_multi[3];
|
2017-11-05 06:47:56 +08:00
|
|
|
static int flag_no_mirror_traffic, justmirror, flag_echo;
|
|
|
|
static uint32_t count_blocks = 1024, txb, rxb, rx_count, errs;
|
|
|
|
static struct lws_poly_gen tx = { { 0xabcde, 0x23456789 } },
|
|
|
|
rx = { { 0xabcde, 0x23456789 } }
|
|
|
|
;
|
2017-02-22 09:54:47 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param)
|
2016-04-17 11:28:43 +08:00
|
|
|
char crl_path[1024] = "";
|
|
|
|
#endif
|
2011-01-30 21:04:24 +00:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* This demo shows how to connect multiple websockets simultaneously to a
|
|
|
|
* websocket server (there is no restriction on their having to be the same
|
|
|
|
* server just it simplifies the demo).
|
|
|
|
*
|
|
|
|
* dumb-increment-protocol: we connect to the server and print the number
|
2011-01-27 06:26:52 +00:00
|
|
|
* we are given
|
2011-01-22 12:51:57 +00:00
|
|
|
*
|
|
|
|
* lws-mirror-protocol: draws random circles, which are mirrored on to every
|
2011-01-27 06:26:52 +00:00
|
|
|
* client (see them being drawn in every browser
|
|
|
|
* session also using the test server)
|
2011-01-22 12:51:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum demo_protocols {
|
|
|
|
|
|
|
|
PROTOCOL_DUMB_INCREMENT,
|
|
|
|
PROTOCOL_LWS_MIRROR,
|
|
|
|
|
|
|
|
/* always last */
|
|
|
|
DEMO_PROTOCOL_COUNT
|
|
|
|
};
|
|
|
|
|
2017-11-05 06:47:56 +08:00
|
|
|
static uint8_t
|
|
|
|
lws_poly_rand(struct lws_poly_gen *p)
|
|
|
|
{
|
2018-10-10 13:54:43 +08:00
|
|
|
p->cyc[0] = (p->cyc[0] & 1) ? (p->cyc[0] >> 1) ^ 0xb4bcd35c :
|
|
|
|
p->cyc[0] >> 1;
|
|
|
|
p->cyc[0] = (p->cyc[0] & 1) ? (p->cyc[0] >> 1) ^ 0xb4bcd35c :
|
|
|
|
p->cyc[0] >> 1;
|
|
|
|
p->cyc[1] = (p->cyc[1] & 1) ? (p->cyc[1] >> 1) ^ 0x7a5bc2e3 :
|
|
|
|
p->cyc[1] >> 1;
|
2017-11-05 06:47:56 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
return (uint8_t)(p->cyc[0] ^ p->cyc[1]);
|
2017-11-05 06:47:56 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 09:10:57 +08:00
|
|
|
static void show_http_content(const char *p, size_t l)
|
|
|
|
{
|
|
|
|
if (lwsl_visible(LLL_INFO)) {
|
|
|
|
while (l--)
|
|
|
|
if (*p < 0x7f)
|
|
|
|
putchar(*p++);
|
|
|
|
else
|
|
|
|
putchar('.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2015-12-14 08:52:03 +08:00
|
|
|
/*
|
2015-12-09 08:07:38 +08:00
|
|
|
* dumb_increment protocol
|
2015-12-14 08:52:03 +08:00
|
|
|
*
|
2015-12-09 08:07:38 +08:00
|
|
|
* since this also happens to be protocols[0], some callbacks that are not
|
|
|
|
* bound to a specific protocol also turn up here.
|
|
|
|
*/
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
static int
|
2015-12-17 07:54:44 +08:00
|
|
|
callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
|
2015-12-09 07:10:03 +08:00
|
|
|
void *user, void *in, size_t len)
|
2011-01-22 12:51:57 +00:00
|
|
|
{
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2017-11-05 06:47:56 +08:00
|
|
|
union lws_tls_cert_info_results ci;
|
2021-03-25 10:33:05 +03:00
|
|
|
#if defined(LWS_HAVE_CTIME_R) && !defined(LWS_WITH_NO_LOGS)
|
|
|
|
char date[32];
|
|
|
|
#endif
|
2017-11-05 06:47:56 +08:00
|
|
|
#endif
|
2016-07-01 08:54:39 +08:00
|
|
|
const char *which = "http";
|
2022-04-17 07:46:38 +01:00
|
|
|
char which_wsi[50], buf[80 + LWS_PRE];
|
2016-08-07 08:33:08 +08:00
|
|
|
int n;
|
2016-07-01 08:54:39 +08:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
switch (reason) {
|
|
|
|
|
2013-09-20 20:26:12 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
2015-12-09 08:07:38 +08:00
|
|
|
lwsl_info("dumb: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
|
2013-09-20 20:26:12 +08:00
|
|
|
break;
|
|
|
|
|
2011-05-24 22:06:17 +01:00
|
|
|
case LWS_CALLBACK_CLOSED:
|
2015-12-09 07:10:03 +08:00
|
|
|
lwsl_notice("dumb: LWS_CALLBACK_CLOSED\n");
|
|
|
|
wsi_dumb = NULL;
|
2011-05-24 22:06:17 +01:00
|
|
|
break;
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2011-01-27 06:26:52 +00:00
|
|
|
((char *)in)[len] = '\0';
|
2015-12-09 08:07:38 +08:00
|
|
|
lwsl_info("rx %d '%s'\n", (int)len, (char *)in);
|
2011-01-22 12:51:57 +00:00
|
|
|
break;
|
|
|
|
|
2011-03-06 13:32:53 +00:00
|
|
|
/* because we are protocols[0] ... */
|
|
|
|
|
2015-12-09 08:07:38 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
2016-07-15 13:41:38 +08:00
|
|
|
if (wsi == wsi_dumb) {
|
2016-07-01 08:54:39 +08:00
|
|
|
which = "dumb";
|
2016-07-15 13:41:38 +08:00
|
|
|
wsi_dumb = NULL;
|
|
|
|
}
|
|
|
|
if (wsi == wsi_mirror) {
|
2016-07-01 08:54:39 +08:00
|
|
|
which = "mirror";
|
2016-07-15 13:41:38 +08:00
|
|
|
wsi_mirror = NULL;
|
|
|
|
}
|
2016-07-01 08:54:39 +08:00
|
|
|
|
2018-08-16 19:10:32 +08:00
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(wsi_multi); n++)
|
2016-08-07 08:33:08 +08:00
|
|
|
if (wsi == wsi_multi[n]) {
|
|
|
|
sprintf(which_wsi, "multi %d", n);
|
|
|
|
which = which_wsi;
|
|
|
|
wsi_multi[n] = NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-04 13:09:00 +01:00
|
|
|
lwsl_err("CLIENT_CONNECTION_ERROR: %s: %s\n", which,
|
|
|
|
in ? (char *)in : "(null)");
|
2015-12-09 08:07:38 +08:00
|
|
|
break;
|
|
|
|
|
2011-03-06 13:32:53 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
|
2017-11-06 10:05:04 +08:00
|
|
|
if ((strcmp((const char *)in, "deflate-stream") == 0) &&
|
|
|
|
deny_deflate) {
|
2015-12-09 08:07:38 +08:00
|
|
|
lwsl_notice("denied deflate-stream extension\n");
|
2011-05-24 22:06:17 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-07 20:09:34 +01:00
|
|
|
if ((strcmp((const char *)in, "x-webkit-deflate-frame") == 0))
|
2013-03-16 12:34:36 +08:00
|
|
|
return 1;
|
2017-02-07 20:09:34 +01:00
|
|
|
if ((strcmp((const char *)in, "deflate-frame") == 0))
|
2011-05-24 22:06:17 +01:00
|
|
|
return 1;
|
2011-03-06 13:32:53 +00:00
|
|
|
break;
|
|
|
|
|
2016-10-21 23:12:21 +08:00
|
|
|
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
|
|
|
|
lwsl_notice("lws_http_client_http_response %d\n",
|
|
|
|
lws_http_client_http_response(wsi));
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2017-11-05 06:47:56 +08:00
|
|
|
if (!lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_COMMON_NAME,
|
|
|
|
&ci, sizeof(ci.ns.name)))
|
|
|
|
lwsl_notice(" Peer Cert CN : %s\n", ci.ns.name);
|
|
|
|
|
|
|
|
if (!lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_ISSUER_NAME,
|
|
|
|
&ci, sizeof(ci.ns.name)))
|
|
|
|
lwsl_notice(" Peer Cert issuer : %s\n", ci.ns.name);
|
|
|
|
|
|
|
|
if (!lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_VALIDITY_FROM,
|
|
|
|
&ci, 0))
|
2021-03-25 10:33:05 +03:00
|
|
|
#if defined(LWS_HAVE_CTIME_R)
|
|
|
|
lwsl_notice(" Peer Cert Valid from: %s",
|
|
|
|
ctime_r(&ci.time, date));
|
|
|
|
#else
|
|
|
|
lwsl_notice(" Peer Cert Valid from: %s",
|
|
|
|
ctime(&ci.time));
|
|
|
|
#endif
|
2017-11-05 06:47:56 +08:00
|
|
|
if (!lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_VALIDITY_TO,
|
|
|
|
&ci, 0))
|
2021-03-25 10:33:05 +03:00
|
|
|
#if defined(LWS_HAVE_CTIME_R)
|
|
|
|
lwsl_notice(" Peer Cert Valid to : %s",
|
|
|
|
ctime_r(&ci.time, date));
|
|
|
|
#else
|
|
|
|
lwsl_notice(" Peer Cert Valid to : %s",
|
|
|
|
ctime(&ci.time));
|
|
|
|
#endif
|
2017-11-05 06:47:56 +08:00
|
|
|
if (!lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_USAGE,
|
|
|
|
&ci, 0))
|
|
|
|
lwsl_notice(" Peer Cert usage bits: 0x%x\n", ci.usage);
|
|
|
|
#endif
|
2016-10-21 23:12:21 +08:00
|
|
|
break;
|
|
|
|
|
2017-02-09 09:10:57 +08:00
|
|
|
/* chunked content */
|
|
|
|
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
|
|
|
|
lwsl_notice("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ: %ld\n",
|
|
|
|
(long)len);
|
|
|
|
show_http_content(in, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* unchunked content */
|
2016-02-29 13:18:30 +08:00
|
|
|
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
|
2016-04-18 17:26:14 +08:00
|
|
|
{
|
|
|
|
char buffer[1024 + LWS_PRE];
|
|
|
|
char *px = buffer + LWS_PRE;
|
|
|
|
int lenx = sizeof(buffer) - LWS_PRE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Often you need to flow control this by something
|
|
|
|
* else being writable. In that case call the api
|
|
|
|
* to get a callback when writable here, and do the
|
|
|
|
* pending client read in the writeable callback of
|
|
|
|
* the output.
|
2017-02-09 09:10:57 +08:00
|
|
|
*
|
|
|
|
* In the case of chunked content, this will call back
|
|
|
|
* LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ once per
|
|
|
|
* chunk or partial chunk in the buffer, and report
|
|
|
|
* zero length back here.
|
2016-04-18 17:26:14 +08:00
|
|
|
*/
|
|
|
|
if (lws_http_client_read(wsi, &px, &lenx) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
break;
|
|
|
|
|
2016-08-07 08:33:08 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
2016-08-08 21:54:30 +08:00
|
|
|
lwsl_info("Client wsi %p writable\n", wsi);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
|
|
|
|
if (test_post) {
|
|
|
|
unsigned char **p = (unsigned char **)in, *end = (*p) + len;
|
|
|
|
|
|
|
|
if (lws_add_http_header_by_token(wsi,
|
|
|
|
WSI_TOKEN_HTTP_CONTENT_LENGTH,
|
|
|
|
(unsigned char *)"29", 2, p, end))
|
|
|
|
return -1;
|
|
|
|
if (lws_add_http_header_by_token(wsi,
|
|
|
|
WSI_TOKEN_HTTP_CONTENT_TYPE,
|
2017-11-06 10:05:04 +08:00
|
|
|
(unsigned char *)"application/x-www-form-urlencoded",
|
|
|
|
33, p, end))
|
2016-08-08 21:54:30 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* inform lws we have http body to send */
|
|
|
|
lws_client_http_body_pending(wsi, 1);
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE:
|
|
|
|
strcpy(buf + LWS_PRE, "text=hello&send=Send+the+form");
|
2017-11-06 10:05:04 +08:00
|
|
|
n = lws_write(wsi, (unsigned char *)&buf[LWS_PRE],
|
|
|
|
strlen(&buf[LWS_PRE]), LWS_WRITE_HTTP);
|
2016-08-08 21:54:30 +08:00
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
/* we only had one thing to send, so inform lws we are done
|
|
|
|
* if we had more to send, call lws_callback_on_writable(wsi);
|
|
|
|
* and just return 0 from callback. On having sent the last
|
|
|
|
* part, call the below api instead.*/
|
|
|
|
lws_client_http_body_pending(wsi, 0);
|
2016-08-07 08:33:08 +08:00
|
|
|
break;
|
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
|
|
|
|
wsi_dumb = NULL;
|
|
|
|
force_exit = 1;
|
|
|
|
break;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param) && \
|
2017-11-06 10:05:04 +08:00
|
|
|
!defined(LWS_WITH_MBEDTLS)
|
2016-04-13 22:17:05 +02:00
|
|
|
case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS:
|
|
|
|
if (crl_path[0]) {
|
|
|
|
/* Enable CRL checking of the server certificate */
|
2019-11-06 09:00:28 +00:00
|
|
|
X509_STORE *store;
|
|
|
|
X509_LOOKUP *lookup;
|
|
|
|
int n;
|
2016-04-13 22:17:05 +02:00
|
|
|
X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
|
|
|
|
X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
|
|
|
|
SSL_CTX_set1_param((SSL_CTX*)user, param);
|
2019-11-06 09:00:28 +00:00
|
|
|
store = SSL_CTX_get_cert_store((SSL_CTX*)user);
|
|
|
|
lookup = X509_STORE_add_lookup(store,
|
2017-11-06 10:05:04 +08:00
|
|
|
X509_LOOKUP_file());
|
2019-11-06 09:00:28 +00:00
|
|
|
n = X509_load_cert_crl_file(lookup, crl_path,
|
2017-11-06 10:05:04 +08:00
|
|
|
X509_FILETYPE_PEM);
|
2016-04-13 22:17:05 +02:00
|
|
|
X509_VERIFY_PARAM_free(param);
|
|
|
|
if (n != 1) {
|
|
|
|
char errbuf[256];
|
2020-12-12 06:21:40 +00:00
|
|
|
const char *es;
|
|
|
|
|
|
|
|
n = (int)ERR_get_error();
|
|
|
|
es = ERR_error_string(
|
|
|
|
#if defined(LWS_WITH_BORINGSSL)
|
|
|
|
(uint32_t)
|
|
|
|
#else
|
|
|
|
(unsigned long)
|
|
|
|
#endif
|
|
|
|
n, errbuf);
|
2017-11-06 10:05:04 +08:00
|
|
|
lwsl_err("EXTRA_CLIENT_VERIFY_CERTS: "
|
2020-12-12 06:21:40 +00:00
|
|
|
"SSL error: %s (%d)\n", es, n);
|
2016-04-13 22:17:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* lws-mirror_protocol */
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2015-12-17 07:54:44 +08:00
|
|
|
callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
|
2015-12-09 08:07:38 +08:00
|
|
|
void *user, void *in, size_t len)
|
2011-01-22 12:51:57 +00:00
|
|
|
{
|
2017-11-05 06:47:56 +08:00
|
|
|
unsigned char buf[LWS_PRE + block_size], *p;
|
2015-12-09 08:07:38 +08:00
|
|
|
unsigned int rands[4];
|
2013-01-20 17:08:31 +08:00
|
|
|
int l = 0;
|
|
|
|
int n;
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
switch (reason) {
|
2011-01-27 06:26:52 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
|
|
|
|
2015-12-09 07:10:03 +08:00
|
|
|
lwsl_notice("mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
|
2013-09-20 20:26:12 +08:00
|
|
|
|
2017-11-05 06:47:56 +08:00
|
|
|
if (flag_echo) {
|
|
|
|
rxb = txb = 0;
|
|
|
|
rx.cyc[0] = tx.cyc[0] = 0xabcde;
|
|
|
|
rx.cyc[1] = tx.cyc[1] = 0x23456789;
|
|
|
|
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:25:25 +08:00
|
|
|
lws_get_random(lws_get_context(wsi), rands, sizeof(rands[0]));
|
2020-12-12 06:21:40 +00:00
|
|
|
mirror_lifetime = (int)(16384 + (rands[0] & 65535));
|
2013-09-20 20:26:12 +08:00
|
|
|
/* useful to test single connection stability */
|
|
|
|
if (longlived)
|
2015-12-09 07:10:03 +08:00
|
|
|
mirror_lifetime += 500000;
|
2013-09-20 20:26:12 +08:00
|
|
|
|
2018-04-04 09:57:32 +08:00
|
|
|
lwsl_notice("opened mirror connection with "
|
2015-12-09 07:10:03 +08:00
|
|
|
"%d lifetime\n", mirror_lifetime);
|
2013-09-20 20:26:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* mirror_lifetime is decremented each send, when it reaches
|
|
|
|
* zero the connection is closed in the send callback.
|
|
|
|
* When the close callback comes, wsi_mirror is set to NULL
|
|
|
|
* so a new connection will be opened
|
2015-12-09 08:07:38 +08:00
|
|
|
*
|
2011-01-27 06:26:52 +00:00
|
|
|
* start the ball rolling,
|
2011-01-27 06:36:39 +00:00
|
|
|
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
2011-01-27 06:26:52 +00:00
|
|
|
*/
|
2016-07-15 13:41:38 +08:00
|
|
|
if (!flag_no_mirror_traffic)
|
|
|
|
lws_callback_on_writable(wsi);
|
2011-01-27 06:26:52 +00:00
|
|
|
break;
|
|
|
|
|
2018-04-04 09:57:32 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_CLOSED:
|
2017-11-06 10:05:04 +08:00
|
|
|
lwsl_notice("mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d, "
|
|
|
|
"rxb %d, rx_count %d\n", mirror_lifetime, rxb,
|
|
|
|
rx_count);
|
2013-09-20 20:26:12 +08:00
|
|
|
wsi_mirror = NULL;
|
2017-11-14 11:25:54 +08:00
|
|
|
if (flag_echo || once)
|
2017-11-05 06:47:56 +08:00
|
|
|
force_exit = 1;
|
2013-09-20 20:26:12 +08:00
|
|
|
break;
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsl_user("LWS_CALLBACK_CLIENT_WRITEABLE\n");
|
2016-07-15 13:41:38 +08:00
|
|
|
if (flag_no_mirror_traffic)
|
|
|
|
return 0;
|
2017-11-05 06:47:56 +08:00
|
|
|
|
|
|
|
if (flag_echo) {
|
|
|
|
for (n = 0; n < (int)block_size; n++)
|
|
|
|
buf[LWS_PRE + n] = lws_poly_rand(&tx);
|
|
|
|
|
|
|
|
n = lws_write(wsi, &buf[LWS_PRE], block_size,
|
|
|
|
opts | LWS_WRITE_TEXT);
|
|
|
|
if (n < 0) {
|
|
|
|
lwsl_err("Error sending\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
txb++;
|
|
|
|
if (txb != count_blocks)
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
else {
|
|
|
|
lwsl_notice("send completed: %d x %d\n",
|
|
|
|
count_blocks, block_size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-30 13:53:19 +08:00
|
|
|
for (n = 0; n < 1; n++) {
|
2017-11-06 10:05:04 +08:00
|
|
|
lws_get_random(lws_get_context(wsi), rands,
|
|
|
|
sizeof(rands));
|
2016-01-11 11:34:01 +08:00
|
|
|
l += sprintf((char *)&buf[LWS_PRE + l],
|
2016-01-12 08:46:56 +08:00
|
|
|
"c #%06X %u %u %u;",
|
|
|
|
rands[0] & 0xffffff, /* colour */
|
|
|
|
rands[1] & 511, /* x */
|
|
|
|
rands[2] & 255, /* y */
|
|
|
|
(rands[3] & 31) + 1); /* radius */
|
2014-11-30 13:53:19 +08:00
|
|
|
}
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)lws_write(wsi, &buf[LWS_PRE], (unsigned int)l,
|
2015-12-09 08:07:38 +08:00
|
|
|
opts | LWS_WRITE_TEXT);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
if (n < l) {
|
|
|
|
lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-23 07:13:13 +08:00
|
|
|
if (!justmirror)
|
|
|
|
mirror_lifetime--;
|
2013-02-09 14:10:04 +08:00
|
|
|
if (!mirror_lifetime) {
|
2018-04-04 09:57:32 +08:00
|
|
|
lwsl_notice("closing mirror session\n");
|
2013-02-11 14:08:50 +08:00
|
|
|
return -1;
|
2015-12-09 08:07:38 +08:00
|
|
|
}
|
|
|
|
/* get notified as soon as we can write again */
|
2018-04-04 09:57:32 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2018-03-02 14:22:49 +08:00
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(WIN32)
|
2018-04-11 13:39:42 +08:00
|
|
|
usleep(50);
|
2018-03-02 14:22:49 +08:00
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
break;
|
|
|
|
|
2017-11-05 06:47:56 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
|
|
|
if (flag_echo) {
|
|
|
|
p = (unsigned char *)in;
|
|
|
|
for (n = 0; n < (int)len; n++)
|
|
|
|
if (*p++ != lws_poly_rand(&rx)) {
|
2020-12-12 06:21:40 +00:00
|
|
|
lwsl_err("mismatch at rxb %d offset %d\n", (int)rxb + (n / block_size), n % block_size);
|
2017-11-05 06:47:56 +08:00
|
|
|
errs++;
|
|
|
|
force_exit = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rx_count += (unsigned int)(unsigned long long)len;
|
|
|
|
while (rx_count >= block_size) {
|
|
|
|
rx_count -= block_size;
|
|
|
|
rxb++;
|
|
|
|
}
|
|
|
|
if (rx_count == 0 && rxb == count_blocks) {
|
2017-11-06 10:05:04 +08:00
|
|
|
lwsl_notice("Everything received: errs %d\n",
|
|
|
|
errs);
|
2017-11-05 06:47:56 +08:00
|
|
|
force_exit = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2011-01-22 12:51:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:06:05 +08:00
|
|
|
static int
|
|
|
|
callback_test_raw_client(struct lws *wsi, enum lws_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
|
|
|
{
|
|
|
|
switch (reason) {
|
|
|
|
case LWS_CALLBACK_RAW_ADOPT:
|
|
|
|
lwsl_notice("LWS_CALLBACK_RAW_ADOPT\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RAW_RX:
|
|
|
|
lwsl_notice("LWS_CALLBACK_RAW_RX %ld\n", (long)len);
|
|
|
|
puts(in);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RAW_CLOSE:
|
|
|
|
lwsl_notice("LWS_CALLBACK_RAW_CLOSE\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RAW_WRITEABLE:
|
|
|
|
lwsl_notice("LWS_CALLBACK_RAW_WRITEABLE\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
/* list of supported protocols and callbacks */
|
|
|
|
|
2017-03-07 16:06:05 +08:00
|
|
|
static const struct lws_protocols protocols[] = {
|
2011-03-02 22:03:47 +00:00
|
|
|
{
|
2016-12-15 13:25:25 +08:00
|
|
|
"dumb-increment-protocol",
|
2011-03-02 22:03:47 +00:00
|
|
|
callback_dumb_increment,
|
2021-06-28 11:51:44 +01:00
|
|
|
0, 20, 0, NULL, 0
|
2011-01-22 12:51:57 +00:00
|
|
|
},
|
2011-03-02 22:03:47 +00:00
|
|
|
{
|
2016-12-15 13:25:25 +08:00
|
|
|
"lws-mirror-protocol",
|
2011-03-02 22:03:47 +00:00
|
|
|
callback_lws_mirror,
|
2021-06-28 11:51:44 +01:00
|
|
|
0, 4096, 0, NULL, 0
|
2017-03-07 16:06:05 +08:00
|
|
|
}, {
|
|
|
|
"lws-test-raw-client",
|
|
|
|
callback_test_raw_client,
|
2021-06-28 11:51:44 +01:00
|
|
|
0, 128, 0, NULL, 0
|
2011-01-22 12:51:57 +00:00
|
|
|
},
|
2021-06-28 11:51:44 +01:00
|
|
|
LWS_PROTOCOL_LIST_TERM
|
2011-01-22 12:51:57 +00:00
|
|
|
};
|
|
|
|
|
2020-08-21 07:48:48 +01:00
|
|
|
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
|
2016-01-11 11:34:01 +08:00
|
|
|
static const struct lws_extension exts[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
2017-03-07 10:07:37 +08:00
|
|
|
"permessage-deflate; client_no_context_takeover"
|
2016-01-11 11:34:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"deflate-frame",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"deflate_frame"
|
|
|
|
},
|
|
|
|
{ NULL, NULL, NULL /* terminator */ }
|
|
|
|
};
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2016-01-11 11:34:01 +08:00
|
|
|
|
|
|
|
|
2013-02-11 14:32:02 +08:00
|
|
|
void sighandler(int sig)
|
|
|
|
{
|
|
|
|
force_exit = 1;
|
|
|
|
}
|
|
|
|
|
2019-01-11 16:48:53 +08:00
|
|
|
#if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
|
2011-01-22 12:51:57 +00:00
|
|
|
static struct option options[] = {
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2013-01-10 22:28:59 +08:00
|
|
|
{ "debug", required_argument, NULL, 'd' },
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2016-07-07 08:14:26 +08:00
|
|
|
{ "strict-ssl", no_argument, NULL, 'S' },
|
2011-02-09 08:49:14 +00:00
|
|
|
{ "version", required_argument, NULL, 'v' },
|
2011-03-06 13:32:53 +00:00
|
|
|
{ "undeflated", no_argument, NULL, 'u' },
|
2017-11-05 06:47:56 +08:00
|
|
|
{ "echo", no_argument, NULL, 'e' },
|
2016-08-07 08:33:08 +08:00
|
|
|
{ "multi-test", no_argument, NULL, 'm' },
|
2016-07-15 13:41:38 +08:00
|
|
|
{ "nomirror", no_argument, NULL, 'n' },
|
2017-09-23 07:13:13 +08:00
|
|
|
{ "justmirror", no_argument, NULL, 'j' },
|
2013-01-13 11:58:18 +08:00
|
|
|
{ "longlived", no_argument, NULL, 'l' },
|
2016-08-08 21:54:30 +08:00
|
|
|
{ "post", no_argument, NULL, 'o' },
|
2017-11-14 11:25:54 +08:00
|
|
|
{ "once", no_argument, NULL, 'O' },
|
2016-04-13 22:17:05 +02:00
|
|
|
{ "ssl-cert", required_argument, NULL, 'C' },
|
|
|
|
{ "ssl-key", required_argument, NULL, 'K' },
|
|
|
|
{ "ssl-ca", required_argument, NULL, 'A' },
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param)
|
2016-04-13 22:17:05 +02:00
|
|
|
{ "ssl-crl", required_argument, NULL, 'R' },
|
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
2019-01-11 16:48:53 +08:00
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2015-12-27 18:16:32 +08:00
|
|
|
static int ratelimit_connects(unsigned int *last, unsigned int secs)
|
2015-12-09 07:10:03 +08:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
2016-07-15 13:41:38 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if ((unsigned long)tv.tv_sec - (unsigned long)(*last) < (unsigned long)secs)
|
2015-12-09 07:10:03 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
*last = (unsigned int)tv.tv_sec;
|
2015-12-09 07:10:03 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2016-08-07 08:33:08 +08:00
|
|
|
int n = 0, m, ret = 0, port = 7681, use_ssl = 0, ietf_version = -1;
|
2020-05-13 19:23:26 +01:00
|
|
|
unsigned int rl_dumb = 0, rl_mirror = 0, do_ws = 1, do_multi = 0;
|
2015-12-09 07:10:03 +08:00
|
|
|
struct lws_context_creation_info info;
|
2016-01-11 11:34:01 +08:00
|
|
|
struct lws_client_connect_info i;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_context *context;
|
2016-02-07 07:28:21 +08:00
|
|
|
const char *prot, *p;
|
|
|
|
char path[300];
|
2016-04-13 22:17:05 +02:00
|
|
|
char cert_path[1024] = "";
|
|
|
|
char key_path[1024] = "";
|
|
|
|
char ca_path[1024] = "";
|
2017-11-05 06:47:56 +08:00
|
|
|
unsigned long last = lws_now_secs();
|
2013-02-09 14:01:09 +08:00
|
|
|
|
|
|
|
memset(&info, 0, sizeof info);
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2019-12-02 08:19:24 +00:00
|
|
|
lwsl_notice("libwebsockets test client - license MIT\n");
|
2018-04-04 09:57:32 +08:00
|
|
|
lwsl_notice("(C) Copyright 2010-2018 Andy Green <andy@warmcat.com>\n");
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
while (n >= 0) {
|
2019-01-11 16:48:53 +08:00
|
|
|
#if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
|
2020-05-13 19:23:26 +01:00
|
|
|
n = getopt_long(argc, argv, "Sjnuv:hsp:d:lC:K:A:moeO", options, NULL);
|
2019-01-11 16:48:53 +08:00
|
|
|
#else
|
2020-05-13 19:23:26 +01:00
|
|
|
n = getopt(argc, argv, "Sjnuv:hsp:d:lC:K:A:moeO");
|
2019-01-11 16:48:53 +08:00
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
2013-01-10 22:28:59 +08:00
|
|
|
case 'd':
|
2013-01-12 09:17:42 +08:00
|
|
|
lws_set_log_level(atoi(optarg), NULL);
|
2013-01-10 22:28:59 +08:00
|
|
|
break;
|
2016-07-07 08:14:26 +08:00
|
|
|
case 's': /* lax SSL, allow selfsigned, skip checking hostname */
|
|
|
|
use_ssl = LCCSCF_USE_SSL |
|
|
|
|
LCCSCF_ALLOW_SELFSIGNED |
|
|
|
|
LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
|
|
|
|
break;
|
|
|
|
case 'S': /* Strict SSL, no selfsigned, check server hostname */
|
|
|
|
use_ssl = LCCSCF_USE_SSL;
|
2011-01-22 12:51:57 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
|
|
|
break;
|
2017-11-05 06:47:56 +08:00
|
|
|
case 'e':
|
|
|
|
flag_echo = 1;
|
|
|
|
break;
|
2017-09-23 07:13:13 +08:00
|
|
|
case 'j':
|
|
|
|
justmirror = 1;
|
|
|
|
break;
|
2013-01-13 11:58:18 +08:00
|
|
|
case 'l':
|
|
|
|
longlived = 1;
|
|
|
|
break;
|
2011-02-09 08:49:14 +00:00
|
|
|
case 'v':
|
|
|
|
ietf_version = atoi(optarg);
|
|
|
|
break;
|
2011-03-06 13:32:53 +00:00
|
|
|
case 'u':
|
|
|
|
deny_deflate = 1;
|
|
|
|
break;
|
2016-08-07 08:33:08 +08:00
|
|
|
case 'm':
|
|
|
|
do_multi = 1;
|
|
|
|
break;
|
2016-08-08 21:54:30 +08:00
|
|
|
case 'o':
|
|
|
|
test_post = 1;
|
|
|
|
break;
|
2017-11-14 11:25:54 +08:00
|
|
|
case 'O':
|
|
|
|
once = 1;
|
|
|
|
break;
|
2011-05-24 22:06:17 +01:00
|
|
|
case 'n':
|
2016-07-15 13:41:38 +08:00
|
|
|
flag_no_mirror_traffic = 1;
|
|
|
|
lwsl_notice("Disabled sending mirror data (for pingpong testing)\n");
|
2011-05-24 22:06:17 +01:00
|
|
|
break;
|
2016-04-13 22:17:05 +02:00
|
|
|
case 'C':
|
2018-03-12 09:28:26 +08:00
|
|
|
lws_strncpy(cert_path, optarg, sizeof(cert_path));
|
2016-04-13 22:17:05 +02:00
|
|
|
break;
|
|
|
|
case 'K':
|
2018-03-12 09:28:26 +08:00
|
|
|
lws_strncpy(key_path, optarg, sizeof(key_path));
|
2016-04-13 22:17:05 +02:00
|
|
|
break;
|
|
|
|
case 'A':
|
2018-03-12 09:28:26 +08:00
|
|
|
lws_strncpy(ca_path, optarg, sizeof(ca_path));
|
2016-04-13 22:17:05 +02:00
|
|
|
break;
|
2017-02-22 09:54:47 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param)
|
2016-04-13 22:17:05 +02:00
|
|
|
case 'R':
|
2018-03-12 09:28:26 +08:00
|
|
|
lws_strncpy(crl_path, optarg, sizeof(crl_path));
|
2016-04-13 22:17:05 +02:00
|
|
|
break;
|
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
case 'h':
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:42:06 +00:00
|
|
|
if (optind >= argc)
|
|
|
|
goto usage;
|
|
|
|
|
2013-02-11 14:32:02 +08:00
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
|
2016-01-14 11:37:56 +08:00
|
|
|
memset(&i, 0, sizeof(i));
|
|
|
|
|
|
|
|
i.port = port;
|
2016-02-07 07:28:21 +08:00
|
|
|
if (lws_parse_uri(argv[optind], &prot, &i.address, &i.port, &p))
|
2016-01-14 11:37:56 +08:00
|
|
|
goto usage;
|
|
|
|
|
2016-02-07 07:28:21 +08:00
|
|
|
/* add back the leading / on path */
|
2018-09-04 08:06:46 +08:00
|
|
|
if (p[0] != '/') {
|
|
|
|
path[0] = '/';
|
|
|
|
lws_strncpy(path + 1, p, sizeof(path) - 1);
|
|
|
|
i.path = path;
|
|
|
|
} else
|
|
|
|
i.path = p;
|
2016-02-07 07:28:21 +08:00
|
|
|
|
|
|
|
if (!strcmp(prot, "http") || !strcmp(prot, "ws"))
|
2016-01-14 11:37:56 +08:00
|
|
|
use_ssl = 0;
|
2016-02-07 07:28:21 +08:00
|
|
|
if (!strcmp(prot, "https") || !strcmp(prot, "wss"))
|
2016-05-03 07:26:10 +08:00
|
|
|
if (!use_ssl)
|
2016-07-07 08:14:26 +08:00
|
|
|
use_ssl = LCCSCF_USE_SSL;
|
2011-02-06 17:42:06 +00:00
|
|
|
|
2018-09-04 08:06:46 +08:00
|
|
|
lwsl_debug("'%s' %p '%s' %p\n", i.address, i.address, i.path, i.path);
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* create the websockets context. This tracks open connections and
|
|
|
|
* knows how to route any traffic and which protocol version to use,
|
|
|
|
* and if each connection is client or server side.
|
|
|
|
*
|
|
|
|
* For this client-only demo, we tell it to not listen on any port.
|
|
|
|
*/
|
|
|
|
|
2013-02-09 14:01:09 +08:00
|
|
|
info.port = CONTEXT_PORT_NO_LISTEN;
|
|
|
|
info.protocols = protocols;
|
2020-12-12 06:21:40 +00:00
|
|
|
info.gid = (gid_t)-1;
|
|
|
|
info.uid = (uid_t)-1;
|
2020-08-21 07:48:48 +01:00
|
|
|
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
|
2017-02-03 10:39:37 +08:00
|
|
|
info.extensions = exts;
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2013-02-09 14:01:09 +08:00
|
|
|
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
/*
|
|
|
|
* since we know this lws context is only ever going to be used with
|
|
|
|
* a few client wsis / fds / sockets at a time, let lws know it doesn't
|
|
|
|
* have to use the default allocations for fd tables up to ulimit -n.
|
|
|
|
* It will just allocate for 2 internal and 4 that we might use.
|
|
|
|
*/
|
|
|
|
info.fd_limit_per_thread = 2 + 4;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2017-02-09 15:25:01 +08:00
|
|
|
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
|
|
#endif
|
2016-03-23 09:22:11 +08:00
|
|
|
|
2019-07-08 08:48:58 +01:00
|
|
|
info.options |= LWS_SERVER_OPTION_H2_JUST_FIX_WINDOW_UPDATE_OVERFLOW;
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2017-02-09 15:25:01 +08:00
|
|
|
if (use_ssl) {
|
2016-04-13 22:17:05 +02:00
|
|
|
/*
|
|
|
|
* If the server wants us to present a valid SSL client certificate
|
|
|
|
* then we can set it up here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (cert_path[0])
|
2017-09-14 07:37:10 +08:00
|
|
|
info.client_ssl_cert_filepath = cert_path;
|
2016-04-13 22:17:05 +02:00
|
|
|
if (key_path[0])
|
2017-09-14 07:37:10 +08:00
|
|
|
info.client_ssl_private_key_filepath = key_path;
|
2016-04-13 22:17:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A CA cert and CRL can be used to validate the cert send by the server
|
|
|
|
*/
|
|
|
|
if (ca_path[0])
|
2017-09-14 07:37:10 +08:00
|
|
|
info.client_ssl_ca_filepath = ca_path;
|
2017-02-22 09:54:47 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param)
|
2016-04-13 22:17:05 +02:00
|
|
|
else if (crl_path[0])
|
|
|
|
lwsl_notice("WARNING, providing a CRL requires a CA cert!\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-24 17:52:57 +01:00
|
|
|
if (use_ssl & LCCSCF_USE_SSL) {
|
2016-07-07 08:14:26 +08:00
|
|
|
lwsl_notice(" Using SSL\n");
|
2018-03-24 17:52:57 +01:00
|
|
|
#if defined(LWS_WITH_MBEDTLS)
|
|
|
|
lwsl_notice(" (NOTE: mbedtls needs to be given the remote\n");
|
|
|
|
lwsl_notice(" CA cert to trust (with -A) to validate it)\n");
|
|
|
|
#endif
|
|
|
|
}
|
2016-07-07 08:14:26 +08:00
|
|
|
else
|
|
|
|
lwsl_notice(" SSL disabled\n");
|
|
|
|
if (use_ssl & LCCSCF_ALLOW_SELFSIGNED)
|
|
|
|
lwsl_notice(" Selfsigned certs allowed\n");
|
|
|
|
else
|
|
|
|
lwsl_notice(" Cert must validate correctly (use -s to allow selfsigned)\n");
|
|
|
|
if (use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)
|
|
|
|
lwsl_notice(" Skipping peer cert hostname check\n");
|
|
|
|
else
|
|
|
|
lwsl_notice(" Requiring peer cert hostname matches\n");
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2015-12-04 08:43:54 +08:00
|
|
|
context = lws_create_context(&info);
|
2011-01-22 12:51:57 +00:00
|
|
|
if (context == NULL) {
|
|
|
|
fprintf(stderr, "Creating libwebsocket context failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
i.context = context;
|
|
|
|
i.ssl_connection = use_ssl;
|
2016-01-14 11:37:56 +08:00
|
|
|
i.host = i.address;
|
|
|
|
i.origin = i.address;
|
2016-01-11 11:34:01 +08:00
|
|
|
i.ietf_version_or_minus_one = ietf_version;
|
2016-02-29 13:18:30 +08:00
|
|
|
|
|
|
|
if (!strcmp(prot, "http") || !strcmp(prot, "https")) {
|
|
|
|
lwsl_notice("using %s mode (non-ws)\n", prot);
|
2016-08-08 21:54:30 +08:00
|
|
|
if (test_post) {
|
|
|
|
i.method = "POST";
|
|
|
|
lwsl_notice("POST mode\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i.method = "GET";
|
2016-02-29 13:18:30 +08:00
|
|
|
do_ws = 0;
|
|
|
|
} else
|
2017-03-07 16:06:05 +08:00
|
|
|
if (!strcmp(prot, "raw")) {
|
|
|
|
i.method = "RAW";
|
|
|
|
i.protocol = "lws-test-raw-client";
|
|
|
|
lwsl_notice("using RAW mode connection\n");
|
|
|
|
do_ws = 0;
|
|
|
|
} else
|
|
|
|
lwsl_notice("using %s mode (ws)\n", prot);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
/*
|
|
|
|
* sit there servicing the websocket context to handle incoming
|
|
|
|
* packets, and drawing random circles on the mirror protocol websocket
|
2015-12-09 08:07:38 +08:00
|
|
|
*
|
2013-09-20 20:26:12 +08:00
|
|
|
* nothing happens until the client websocket connection is
|
2015-12-09 08:07:38 +08:00
|
|
|
* asynchronously established... calling lws_client_connect() only
|
|
|
|
* instantiates the connection logically, lws_service() progresses it
|
|
|
|
* asynchronously.
|
2011-01-22 12:51:57 +00:00
|
|
|
*/
|
|
|
|
|
2016-08-07 08:33:08 +08:00
|
|
|
m = 0;
|
2015-12-09 07:10:03 +08:00
|
|
|
while (!force_exit) {
|
2013-02-09 14:10:04 +08:00
|
|
|
|
2016-08-07 08:33:08 +08:00
|
|
|
if (do_multi) {
|
2018-08-16 19:10:32 +08:00
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(wsi_multi); n++) {
|
2016-08-07 08:33:08 +08:00
|
|
|
if (!wsi_multi[n] && ratelimit_connects(&rl_multi[n], 2u)) {
|
|
|
|
lwsl_notice("dumb %d: connecting\n", n);
|
|
|
|
i.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;
|
|
|
|
i.pwsi = &wsi_multi[n];
|
|
|
|
lws_client_connect_via_info(&i);
|
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
}
|
2016-08-07 08:33:08 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (do_ws) {
|
2017-11-05 06:47:56 +08:00
|
|
|
if (!flag_echo && !justmirror && !wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
|
2016-08-07 08:33:08 +08:00
|
|
|
lwsl_notice("dumb: connecting\n");
|
|
|
|
i.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;
|
|
|
|
i.pwsi = &wsi_dumb;
|
|
|
|
lws_client_connect_via_info(&i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wsi_mirror && ratelimit_connects(&rl_mirror, 2u)) {
|
|
|
|
lwsl_notice("mirror: connecting\n");
|
|
|
|
i.protocol = protocols[PROTOCOL_LWS_MIRROR].name;
|
|
|
|
i.pwsi = &wsi_mirror;
|
|
|
|
wsi_mirror = lws_client_connect_via_info(&i);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
|
|
|
|
lwsl_notice("http: connecting\n");
|
|
|
|
i.pwsi = &wsi_dumb;
|
|
|
|
lws_client_connect_via_info(&i);
|
|
|
|
}
|
|
|
|
}
|
2011-05-24 22:06:17 +01:00
|
|
|
|
2015-12-09 07:10:03 +08:00
|
|
|
lws_service(context, 500);
|
2016-08-07 08:33:08 +08:00
|
|
|
|
|
|
|
if (do_multi) {
|
|
|
|
m++;
|
|
|
|
if (m == 10) {
|
|
|
|
m = 0;
|
|
|
|
lwsl_notice("doing lws_callback_on_writable_all_protocol\n");
|
2017-11-06 10:05:04 +08:00
|
|
|
lws_callback_on_writable_all_protocol(context,
|
|
|
|
&protocols[PROTOCOL_DUMB_INCREMENT]);
|
2016-08-07 08:33:08 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 06:47:56 +08:00
|
|
|
|
|
|
|
if (flag_echo && lws_now_secs() != last) {
|
|
|
|
lwsl_notice("rxb %d, rx_count %d\n", rxb, rx_count);
|
|
|
|
last = lws_now_secs();
|
|
|
|
}
|
2015-12-09 07:10:03 +08:00
|
|
|
}
|
2011-02-14 20:25:43 +00:00
|
|
|
|
2015-12-09 07:10:03 +08:00
|
|
|
lwsl_err("Exiting\n");
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_context_destroy(context);
|
2011-01-23 16:50:33 +00:00
|
|
|
|
2013-02-11 14:05:02 +08:00
|
|
|
return ret;
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
usage:
|
|
|
|
fprintf(stderr, "Usage: libwebsockets-test-client "
|
2013-01-10 22:28:59 +08:00
|
|
|
"<server address> [--port=<p>] "
|
|
|
|
"[--ssl] [-k] [-v <ver>] "
|
2013-01-13 11:58:18 +08:00
|
|
|
"[-d <log bitfield>] [-l]\n");
|
2011-01-22 12:51:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|