2013-01-30 11:03:01 +08:00
|
|
|
/*
|
2016-02-08 08:44:21 +08:00
|
|
|
* libwebsockets-test-echo
|
2013-01-30 11:03:01 +08:00
|
|
|
*
|
2016-01-11 11:34:01 +08:00
|
|
|
* Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
|
2013-01-30 11:03:01 +08: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.
|
2013-01-30 11:03:01 +08: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.
|
2013-01-30 11:03:01 +08:00
|
|
|
*
|
2016-02-08 08:44:21 +08:00
|
|
|
* The test apps are intended to be adapted for use in your code, which
|
2016-09-09 06:50:41 +08:00
|
|
|
* may be proprietary. So unlike the library itself, they are licensed
|
2016-02-08 08:44:21 +08:00
|
|
|
* Public Domain.
|
2013-01-30 11:03:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-03-29 07:43:38 +01:00
|
|
|
#include <signal.h>
|
|
|
|
|
2015-10-23 08:10:55 +02:00
|
|
|
#include "../lib/libwebsockets.h"
|
|
|
|
|
2014-03-29 07:43:38 +01:00
|
|
|
#ifndef _WIN32
|
2013-01-30 11:03:01 +08:00
|
|
|
#include <syslog.h>
|
2014-03-29 07:43:38 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2015-10-23 08:10:55 +02:00
|
|
|
#else
|
|
|
|
#include "gettimeofday.h"
|
|
|
|
#include <process.h>
|
2013-04-30 06:53:56 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
|
2014-01-27 12:37:47 +01:00
|
|
|
static volatile int force_exit = 0;
|
2014-12-02 08:42:47 +08:00
|
|
|
static int versa, state;
|
2015-12-28 11:11:06 +08:00
|
|
|
static int times = -1;
|
2013-01-30 11:03:01 +08:00
|
|
|
|
|
|
|
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
#define MAX_ECHO_PAYLOAD 1024
|
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
struct per_session_data__echo {
|
2016-01-11 11:34:01 +08:00
|
|
|
size_t rx, tx;
|
|
|
|
unsigned char buf[LWS_PRE + MAX_ECHO_PAYLOAD];
|
2013-01-30 11:03:01 +08:00
|
|
|
unsigned int len;
|
|
|
|
unsigned int index;
|
2015-12-28 12:45:05 +08:00
|
|
|
int final;
|
|
|
|
int continuation;
|
|
|
|
int binary;
|
2013-01-30 11:03:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2015-12-17 07:54:44 +08:00
|
|
|
callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len)
|
2013-01-30 11:03:01 +08:00
|
|
|
{
|
2016-01-11 11:34:01 +08:00
|
|
|
struct per_session_data__echo *pss =
|
|
|
|
(struct per_session_data__echo *)user;
|
2013-01-30 11:03:01 +08:00
|
|
|
int n;
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2013-01-30 11:03:01 +08:00
|
|
|
|
2016-12-16 07:02:59 +08:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
|
|
pss->index = 0;
|
|
|
|
pss->len = -1;
|
|
|
|
break;
|
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2014-12-01 21:46:35 +08:00
|
|
|
do_tx:
|
2016-01-11 11:34:01 +08:00
|
|
|
|
2015-12-28 12:45:05 +08:00
|
|
|
n = LWS_WRITE_CONTINUATION;
|
|
|
|
if (!pss->continuation) {
|
|
|
|
if (pss->binary)
|
|
|
|
n = LWS_WRITE_BINARY;
|
|
|
|
else
|
|
|
|
n = LWS_WRITE_TEXT;
|
|
|
|
pss->continuation = 1;
|
|
|
|
}
|
|
|
|
if (!pss->final)
|
|
|
|
n |= LWS_WRITE_NO_FIN;
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_info("+++ test-echo: writing %d, with final %d\n",
|
|
|
|
pss->len, pss->final);
|
2015-12-28 12:45:05 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
pss->tx += pss->len;
|
|
|
|
n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, n);
|
2013-01-30 11:03:01 +08:00
|
|
|
if (n < 0) {
|
|
|
|
lwsl_err("ERROR %d writing to socket, hanging up\n", n);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-05-02 07:04:54 +08:00
|
|
|
if (n < (int)pss->len) {
|
2013-02-23 10:50:10 +08:00
|
|
|
lwsl_err("Partial write\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-28 12:45:05 +08:00
|
|
|
pss->len = -1;
|
|
|
|
if (pss->final)
|
|
|
|
pss->continuation = 0;
|
|
|
|
lws_rx_flow_control(wsi, 1);
|
2013-01-30 11:03:01 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2014-12-01 21:46:35 +08:00
|
|
|
do_rx:
|
2015-12-28 12:45:05 +08:00
|
|
|
pss->final = lws_is_final_fragment(wsi);
|
|
|
|
pss->binary = lws_frame_is_binary(wsi);
|
2017-02-05 22:07:34 +08:00
|
|
|
lwsl_info("+++ test-echo: RX len %ld final %ld, pss->len=%ld\n",
|
|
|
|
(long)len, (long)pss->final, (long)pss->len);
|
2016-01-11 11:34:01 +08:00
|
|
|
|
|
|
|
memcpy(&pss->buf[LWS_PRE], in, len);
|
|
|
|
assert((int)pss->len == -1);
|
|
|
|
pss->len = (unsigned int)len;
|
|
|
|
pss->rx += len;
|
|
|
|
|
2015-12-28 12:45:05 +08:00
|
|
|
lws_rx_flow_control(wsi, 0);
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2013-01-30 11:03:01 +08:00
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
/* when the callback is used for client operations --> */
|
|
|
|
|
2014-12-02 08:42:47 +08:00
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
2015-12-28 12:45:05 +08:00
|
|
|
lwsl_debug("closed\n");
|
2014-12-02 08:42:47 +08:00
|
|
|
state = 0;
|
|
|
|
break;
|
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
2015-12-28 12:45:05 +08:00
|
|
|
lwsl_debug("Client has connected\n");
|
2013-01-30 11:03:01 +08:00
|
|
|
pss->index = 0;
|
2016-01-11 11:34:01 +08:00
|
|
|
pss->len = -1;
|
2014-12-02 08:42:47 +08:00
|
|
|
state = 2;
|
2013-01-30 11:03:01 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2014-12-01 22:16:17 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2014-12-01 21:46:35 +08:00
|
|
|
if (versa)
|
|
|
|
goto do_rx;
|
2014-12-01 22:16:17 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
lwsl_notice("Client RX: %s", (char *)in);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
2014-12-01 22:16:17 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2015-12-28 12:45:05 +08:00
|
|
|
if (versa) {
|
|
|
|
if (pss->len != (unsigned int)-1)
|
|
|
|
goto do_tx;
|
2016-01-11 11:34:01 +08:00
|
|
|
break;
|
2015-12-28 12:45:05 +08:00
|
|
|
}
|
2014-12-01 22:16:17 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
/* we will send our packet... */
|
2016-01-11 11:34:01 +08:00
|
|
|
pss->len = sprintf((char *)&pss->buf[LWS_PRE],
|
|
|
|
"hello from libwebsockets-test-echo client pid %d index %d\n",
|
|
|
|
getpid(), pss->index++);
|
|
|
|
lwsl_notice("Client TX: %s", &pss->buf[LWS_PRE]);
|
|
|
|
n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, LWS_WRITE_TEXT);
|
2013-01-30 11:03:01 +08:00
|
|
|
if (n < 0) {
|
|
|
|
lwsl_err("ERROR %d writing to socket, hanging up\n", n);
|
2013-02-23 10:50:10 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-05-02 07:04:54 +08:00
|
|
|
if (n < (int)pss->len) {
|
2013-02-23 10:50:10 +08:00
|
|
|
lwsl_err("Partial write\n");
|
|
|
|
return -1;
|
2013-01-30 11:03:01 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2016-01-11 11:34:01 +08:00
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
static struct lws_protocols protocols[] = {
|
2013-01-30 11:03:01 +08:00
|
|
|
/* first protocol must always be HTTP handler */
|
|
|
|
|
|
|
|
{
|
2016-10-02 02:21:03 +03:00
|
|
|
"", /* name - can be overridden with -e */
|
2016-01-11 11:34:01 +08:00
|
|
|
callback_echo,
|
|
|
|
sizeof(struct per_session_data__echo), /* per_session_data_size */
|
|
|
|
MAX_ECHO_PAYLOAD,
|
2013-01-30 11:03:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
NULL, NULL, 0 /* End of list */
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
static const struct lws_extension exts[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"permessage-deflate; client_no_context_takeover; client_max_window_bits"
|
|
|
|
},
|
|
|
|
{ NULL, NULL, NULL /* terminator */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
void sighandler(int sig)
|
|
|
|
{
|
|
|
|
force_exit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "debug", required_argument, NULL, 'd' },
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
2016-09-09 06:50:41 +08:00
|
|
|
{ "ssl-cert", required_argument, NULL, 'C' },
|
2014-11-18 09:28:35 +08:00
|
|
|
{ "ssl-key", required_argument, NULL, 'k' },
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
{ "client", required_argument, NULL, 'c' },
|
|
|
|
{ "ratems", required_argument, NULL, 'r' },
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2014-12-01 21:46:35 +08:00
|
|
|
{ "versa", no_argument, NULL, 'v' },
|
|
|
|
{ "uri", required_argument, NULL, 'u' },
|
2014-11-18 09:28:35 +08:00
|
|
|
{ "passphrase", required_argument, NULL, 'P' },
|
2016-09-09 06:50:41 +08:00
|
|
|
{ "interface", required_argument, NULL, 'i' },
|
2015-12-28 11:11:06 +08:00
|
|
|
{ "times", required_argument, NULL, 'n' },
|
2016-01-11 11:34:01 +08:00
|
|
|
{ "echogen", no_argument, NULL, 'e' },
|
2013-01-30 11:03:01 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2016-09-09 06:50:41 +08:00
|
|
|
{ "daemonize", no_argument, NULL, 'D' },
|
2013-01-30 11:03:01 +08:00
|
|
|
#endif
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int port = 7681;
|
|
|
|
int use_ssl = 0;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_context *context;
|
2013-01-30 11:03:01 +08:00
|
|
|
int opts = 0;
|
|
|
|
char interface_name[128] = "";
|
2015-10-23 08:10:55 +02:00
|
|
|
const char *_interface = NULL;
|
2014-11-18 09:28:35 +08:00
|
|
|
char ssl_cert[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
|
|
|
|
char ssl_key[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2016-09-09 06:50:41 +08:00
|
|
|
/* LOG_PERROR is not POSIX standard, and may not be portable */
|
|
|
|
#ifdef __sun
|
|
|
|
int syslog_options = LOG_PID;
|
|
|
|
#else
|
2013-01-30 11:03:01 +08:00
|
|
|
int syslog_options = LOG_PID | LOG_PERROR;
|
2016-09-09 06:50:41 +08:00
|
|
|
#endif
|
2013-05-02 07:04:54 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
int client = 0;
|
2014-11-26 14:36:17 +08:00
|
|
|
int listen_port = 80;
|
2013-02-09 14:01:09 +08:00
|
|
|
struct lws_context_creation_info info;
|
2014-11-18 09:28:35 +08:00
|
|
|
char passphrase[256];
|
2014-12-01 21:46:35 +08:00
|
|
|
char uri[256] = "/";
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2014-12-01 21:02:55 +08:00
|
|
|
char address[256], ads_port[256 + 30];
|
2013-01-30 11:03:01 +08:00
|
|
|
int rate_us = 250000;
|
2015-11-17 09:30:36 +08:00
|
|
|
unsigned long long oldus;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws *wsi;
|
2014-11-18 09:28:35 +08:00
|
|
|
int disallow_selfsigned = 0;
|
2015-11-17 09:30:36 +08:00
|
|
|
struct timeval tv;
|
2016-01-11 11:34:01 +08:00
|
|
|
const char *connect_protocol = NULL;
|
|
|
|
struct lws_client_connect_info i;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
|
|
|
|
int debug_level = 7;
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
int daemonize = 0;
|
|
|
|
#endif
|
|
|
|
|
2013-02-09 14:01:09 +08:00
|
|
|
memset(&info, 0, sizeof info);
|
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
lwsl_notice("Built to support client operations\n");
|
|
|
|
#endif
|
|
|
|
#ifndef LWS_NO_SERVER
|
|
|
|
lwsl_notice("Built to support server operations\n");
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
while (n >= 0) {
|
2016-01-11 11:34:01 +08:00
|
|
|
n = getopt_long(argc, argv, "i:hsp:d:DC:k:P:vu:n:e"
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
"c:r:"
|
|
|
|
#endif
|
|
|
|
, options, NULL);
|
2013-01-30 11:03:01 +08:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
2014-11-18 09:28:35 +08:00
|
|
|
case 'P':
|
|
|
|
strncpy(passphrase, optarg, sizeof(passphrase));
|
|
|
|
passphrase[sizeof(passphrase) - 1] = '\0';
|
|
|
|
info.ssl_private_key_password = passphrase;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
strncpy(ssl_cert, optarg, sizeof(ssl_cert));
|
|
|
|
ssl_cert[sizeof(ssl_cert) - 1] = '\0';
|
|
|
|
disallow_selfsigned = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
strncpy(ssl_key, optarg, sizeof(ssl_key));
|
|
|
|
ssl_key[sizeof(ssl_key) - 1] = '\0';
|
|
|
|
break;
|
2014-12-01 21:46:35 +08:00
|
|
|
case 'u':
|
|
|
|
strncpy(uri, optarg, sizeof(uri));
|
|
|
|
uri[sizeof(uri) - 1] = '\0';
|
|
|
|
break;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
2016-09-09 06:50:41 +08:00
|
|
|
#if !defined(_WIN32) && !defined(__sun)
|
2013-01-30 11:03:01 +08:00
|
|
|
syslog_options &= ~LOG_PERROR;
|
2013-05-02 07:04:54 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
case 'c':
|
|
|
|
client = 1;
|
2014-11-30 12:42:14 +08:00
|
|
|
strncpy(address, optarg, sizeof(address) - 1);
|
|
|
|
address[sizeof(address) - 1] = '\0';
|
2013-01-30 11:03:01 +08:00
|
|
|
port = 80;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rate_us = atoi(optarg) * 1000;
|
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
|
|
|
case 'd':
|
|
|
|
debug_level = atoi(optarg);
|
|
|
|
break;
|
2013-01-30 11:03:01 +08:00
|
|
|
case 's':
|
|
|
|
use_ssl = 1; /* 1 = take care about cert verification, 2 = allow anything */
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
|
|
|
break;
|
2014-12-01 21:46:35 +08:00
|
|
|
case 'v':
|
|
|
|
versa = 1;
|
|
|
|
break;
|
2016-01-11 11:34:01 +08:00
|
|
|
case 'e':
|
|
|
|
protocols[0].name = "lws-echogen";
|
|
|
|
connect_protocol = protocols[0].name;
|
|
|
|
lwsl_err("using lws-echogen\n");
|
|
|
|
break;
|
2013-01-30 11:03:01 +08:00
|
|
|
case 'i':
|
|
|
|
strncpy(interface_name, optarg, sizeof interface_name);
|
|
|
|
interface_name[(sizeof interface_name) - 1] = '\0';
|
2015-10-23 08:10:55 +02:00
|
|
|
_interface = interface_name;
|
2013-01-30 11:03:01 +08:00
|
|
|
break;
|
2015-12-28 11:11:06 +08:00
|
|
|
case 'n':
|
|
|
|
times = atoi(optarg);
|
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
case '?':
|
2013-01-30 11:03:01 +08:00
|
|
|
case 'h':
|
2014-11-18 09:28:35 +08:00
|
|
|
fprintf(stderr, "Usage: libwebsockets-test-echo\n"
|
2016-09-09 06:50:41 +08:00
|
|
|
" --debug / -d <debug bitfield>\n"
|
|
|
|
" --port / -p <port>\n"
|
|
|
|
" --ssl-cert / -C <cert path>\n"
|
|
|
|
" --ssl-key / -k <key path>\n"
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2016-09-09 06:50:41 +08:00
|
|
|
" --client / -c <server IP>\n"
|
|
|
|
" --ratems / -r <rate in ms>\n"
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2016-09-09 06:50:41 +08:00
|
|
|
" --ssl / -s\n"
|
2014-11-18 09:28:35 +08:00
|
|
|
" --passphrase / -P <passphrase>\n"
|
2016-09-09 06:50:41 +08:00
|
|
|
" --interface / -i <interface>\n"
|
|
|
|
" --uri / -u <uri path>\n"
|
|
|
|
" --times / -n <-1 unlimited or times to echo>\n"
|
2014-11-18 09:28:35 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2016-09-09 06:50:41 +08:00
|
|
|
" --daemonize / -D\n"
|
2014-11-18 09:28:35 +08:00
|
|
|
#endif
|
|
|
|
);
|
2013-01-30 11:03:01 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 08:52:03 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-01-30 11:03:01 +08:00
|
|
|
/*
|
|
|
|
* normally lock path would be /var/lock/lwsts or similar, to
|
|
|
|
* simplify getting started without having to take care about
|
|
|
|
* permissions or running as root, set to /tmp/.lwsts-lock
|
|
|
|
*/
|
2014-01-22 18:17:03 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
#else
|
2013-02-12 22:44:43 +08:00
|
|
|
if (!client && daemonize && lws_daemonize("/tmp/.lwstecho-lock")) {
|
2013-01-30 11:03:01 +08:00
|
|
|
fprintf(stderr, "Failed to daemonize\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-22 18:17:03 +00:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2013-01-30 11:03:01 +08:00
|
|
|
/* we will only try to log things according to our debug_level */
|
|
|
|
setlogmask(LOG_UPTO (LOG_DEBUG));
|
|
|
|
openlog("lwsts", syslog_options, LOG_DAEMON);
|
2015-10-23 08:10:55 +02:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
|
|
|
|
/* tell the library what debug level to emit and to send it to syslog */
|
|
|
|
lws_set_log_level(debug_level, lwsl_emit_syslog);
|
2015-10-23 08:10:55 +02:00
|
|
|
|
2016-01-29 09:35:58 +08:00
|
|
|
lwsl_notice("libwebsockets test server echo - license LGPL2.1+SLE\n");
|
|
|
|
lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
|
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
if (client) {
|
|
|
|
lwsl_notice("Running in client mode\n");
|
|
|
|
listen_port = CONTEXT_PORT_NO_LISTEN;
|
2014-11-18 09:28:35 +08:00
|
|
|
if (use_ssl && !disallow_selfsigned) {
|
|
|
|
lwsl_info("allowing selfsigned\n");
|
2013-01-30 11:03:01 +08:00
|
|
|
use_ssl = 2;
|
2014-11-30 12:50:12 +08:00
|
|
|
} else {
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_info("requiring server cert validation against %s\n",
|
|
|
|
ssl_cert);
|
2014-11-18 09:28:35 +08:00
|
|
|
info.ssl_ca_filepath = ssl_cert;
|
2014-11-30 12:50:12 +08:00
|
|
|
}
|
2013-01-30 11:03:01 +08:00
|
|
|
} else {
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_NO_SERVER
|
2013-01-30 11:03:01 +08:00
|
|
|
lwsl_notice("Running in server mode\n");
|
|
|
|
listen_port = port;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
}
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-02-09 14:01:09 +08:00
|
|
|
|
|
|
|
info.port = listen_port;
|
2015-10-23 08:10:55 +02:00
|
|
|
info.iface = _interface;
|
2013-02-09 14:01:09 +08:00
|
|
|
info.protocols = protocols;
|
|
|
|
if (use_ssl && !client) {
|
2014-11-18 09:28:35 +08:00
|
|
|
info.ssl_cert_filepath = ssl_cert;
|
|
|
|
info.ssl_private_key_filepath = ssl_key;
|
|
|
|
} else
|
|
|
|
if (use_ssl && client) {
|
|
|
|
info.ssl_cert_filepath = NULL;
|
|
|
|
info.ssl_private_key_filepath = NULL;
|
|
|
|
}
|
2013-02-09 14:01:09 +08:00
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
2017-02-03 10:39:37 +08:00
|
|
|
info.extensions = exts;
|
2015-12-29 11:20:09 +08:00
|
|
|
info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
|
2016-03-23 09:22:11 +08:00
|
|
|
|
|
|
|
if (use_ssl)
|
|
|
|
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
2016-01-11 11:34:01 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
|
|
|
info.extensions = exts;
|
|
|
|
#endif
|
2013-02-09 14:01:09 +08:00
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
context = lws_create_context(&info);
|
2013-01-30 11:03:01 +08:00
|
|
|
if (context == NULL) {
|
|
|
|
lwsl_err("libwebsocket init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-02 08:42:47 +08:00
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
|
2015-11-17 09:30:36 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 11:03:01 +08:00
|
|
|
n = 0;
|
|
|
|
while (n >= 0 && !force_exit) {
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2015-12-28 11:11:06 +08:00
|
|
|
if (client && !state && times) {
|
2014-12-02 08:42:47 +08:00
|
|
|
state = 1;
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_notice("Client connecting to %s:%u....\n",
|
|
|
|
address, port);
|
2014-12-02 08:42:47 +08:00
|
|
|
/* we are in client mode */
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2014-12-02 08:42:47 +08:00
|
|
|
address[sizeof(address) - 1] = '\0';
|
2015-04-07 07:52:23 +08:00
|
|
|
sprintf(ads_port, "%s:%u", address, port & 65535);
|
2015-12-28 11:11:06 +08:00
|
|
|
if (times > 0)
|
|
|
|
times--;
|
2016-01-11 11:34:01 +08:00
|
|
|
|
|
|
|
memset(&i, 0, sizeof(i));
|
|
|
|
|
|
|
|
i.context = context;
|
|
|
|
i.address = address;
|
|
|
|
i.port = port;
|
|
|
|
i.ssl_connection = use_ssl;
|
|
|
|
i.path = uri;
|
|
|
|
i.host = ads_port;
|
|
|
|
i.origin = ads_port;
|
|
|
|
i.protocol = connect_protocol;
|
|
|
|
|
2016-01-12 17:22:06 +08:00
|
|
|
wsi = lws_client_connect_via_info(&i);
|
2014-12-02 08:42:47 +08:00
|
|
|
if (!wsi) {
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_err("Client failed to connect to %s:%u\n",
|
|
|
|
address, port);
|
2014-12-02 08:42:47 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 11:11:06 +08:00
|
|
|
if (client && !versa && times) {
|
2013-01-30 11:03:01 +08:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
2015-11-17 09:30:36 +08:00
|
|
|
if (((((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec) - oldus) > rate_us) {
|
2015-12-11 10:45:35 +08:00
|
|
|
lws_callback_on_writable_all_protocol(context,
|
|
|
|
&protocols[0]);
|
2015-11-17 09:30:36 +08:00
|
|
|
oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
|
2015-12-28 11:11:06 +08:00
|
|
|
if (times > 0)
|
|
|
|
times--;
|
2013-01-30 11:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 11:34:01 +08:00
|
|
|
|
2015-12-28 11:11:06 +08:00
|
|
|
if (client && !state && !times)
|
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_service(context, 10);
|
2013-01-30 11:03:01 +08:00
|
|
|
}
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
bail:
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_context_destroy(context);
|
2013-01-30 11:03:01 +08:00
|
|
|
|
|
|
|
lwsl_notice("libwebsockets-test-echo exited cleanly\n");
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2013-01-30 11:03:01 +08:00
|
|
|
closelog();
|
2013-04-30 06:53:56 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|