2013-01-30 11:03:01 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-echo - libwebsockets echo test implementation
|
|
|
|
*
|
|
|
|
* This implements both the client and server sides. It defaults to
|
|
|
|
* serving, use --client <remote address> to connect as client.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2013 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 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include "../lib/libwebsockets.h"
|
|
|
|
|
|
|
|
int force_exit = 0;
|
|
|
|
|
|
|
|
#define MAX_ECHO_PAYLOAD 1400
|
|
|
|
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
|
|
|
|
|
|
|
|
struct per_session_data__echo {
|
|
|
|
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_ECHO_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
|
|
|
|
unsigned int len;
|
|
|
|
unsigned int index;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
callback_echo(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len)
|
|
|
|
{
|
|
|
|
struct per_session_data__echo *pss = (struct per_session_data__echo *)user;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2013-01-30 11:03:01 +08:00
|
|
|
/* when the callback is used for server operations --> */
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
n = libwebsocket_write(wsi, &pss->buf[LWS_SEND_BUFFER_PRE_PADDING], pss->len, LWS_WRITE_TEXT);
|
|
|
|
if (n < 0) {
|
|
|
|
lwsl_err("ERROR %d writing to socket, hanging up\n", n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
|
|
|
if (len > MAX_ECHO_PAYLOAD) {
|
|
|
|
lwsl_err("Server received packet bigger than %u, hanging up\n", MAX_ECHO_PAYLOAD);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
memcpy(&pss->buf[LWS_SEND_BUFFER_PRE_PADDING], in, len);
|
|
|
|
pss->len = len;
|
|
|
|
libwebsocket_callback_on_writable(context, wsi);
|
|
|
|
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 --> */
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
|
|
|
lwsl_notice("Client has connected\n");
|
|
|
|
pss->index = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
|
|
|
lwsl_notice("Client RX: %s", (char *)in);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
|
/* we will send our packet... */
|
|
|
|
pss->len = sprintf((char *)&pss->buf[LWS_SEND_BUFFER_PRE_PADDING], "hello from libwebsockets-test-echo client pid %d index %d\n", getpid(), pss->index++);
|
|
|
|
lwsl_notice("Client TX: %s", &pss->buf[LWS_SEND_BUFFER_PRE_PADDING]);
|
|
|
|
n = libwebsocket_write(wsi, &pss->buf[LWS_SEND_BUFFER_PRE_PADDING], pss->len, LWS_WRITE_TEXT);
|
|
|
|
if (n < 0) {
|
|
|
|
lwsl_err("ERROR %d writing to socket, hanging up\n", n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct libwebsocket_protocols protocols[] = {
|
|
|
|
/* first protocol must always be HTTP handler */
|
|
|
|
|
|
|
|
{
|
|
|
|
"default", /* name */
|
|
|
|
callback_echo, /* callback */
|
|
|
|
sizeof(struct per_session_data__echo) /* per_session_data_size */
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NULL, NULL, 0 /* End of list */
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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' },
|
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' },
|
|
|
|
{ "interface", required_argument, NULL, 'i' },
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
{ "daemonize", no_argument, NULL, 'D' },
|
|
|
|
#endif
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int port = 7681;
|
|
|
|
int use_ssl = 0;
|
|
|
|
struct libwebsocket_context *context;
|
|
|
|
int opts = 0;
|
|
|
|
char interface_name[128] = "";
|
|
|
|
const char *interface = NULL;
|
|
|
|
int syslog_options = LOG_PID | LOG_PERROR;
|
|
|
|
int client = 0;
|
|
|
|
int listen_port;
|
2013-02-09 14:01:09 +08:00
|
|
|
struct lws_context_creation_info info;
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
char address[256];
|
|
|
|
int rate_us = 250000;
|
2013-01-31 09:57:05 +08:00
|
|
|
unsigned int oldus = 0;
|
|
|
|
struct libwebsocket *wsi;
|
|
|
|
#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) {
|
2013-01-31 09:57:05 +08:00
|
|
|
n = getopt_long(argc, argv, "i:hsp:d:D"
|
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
"c:r:"
|
|
|
|
#endif
|
|
|
|
, options, NULL);
|
2013-01-30 11:03:01 +08:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
|
|
|
syslog_options &= ~LOG_PERROR;
|
|
|
|
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;
|
|
|
|
strcpy(address, optarg);
|
|
|
|
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;
|
|
|
|
case 'i':
|
|
|
|
strncpy(interface_name, optarg, sizeof interface_name);
|
|
|
|
interface_name[(sizeof interface_name) - 1] = '\0';
|
|
|
|
interface = interface_name;
|
|
|
|
break;
|
2013-01-31 09:57:05 +08:00
|
|
|
case '?':
|
2013-01-30 11:03:01 +08:00
|
|
|
case 'h':
|
|
|
|
fprintf(stderr, "Usage: libwebsockets-test-echo "
|
2013-01-31 09:57:05 +08:00
|
|
|
"[--ssl] "
|
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
"[--client <remote ads>] "
|
2013-01-30 11:03:01 +08:00
|
|
|
"[--ratems <ms>] "
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
|
|
|
"[--port=<p>] "
|
2013-01-30 11:03:01 +08:00
|
|
|
"[-d <log bitfield>]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
if (!client && daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
|
|
|
|
fprintf(stderr, "Failed to daemonize\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* we will only try to log things according to our debug_level */
|
|
|
|
setlogmask(LOG_UPTO (LOG_DEBUG));
|
|
|
|
openlog("lwsts", syslog_options, LOG_DAEMON);
|
|
|
|
|
|
|
|
/* tell the library what debug level to emit and to send it to syslog */
|
|
|
|
lws_set_log_level(debug_level, lwsl_emit_syslog);
|
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
lwsl_notice("libwebsockets echo test - "
|
2013-01-30 11:03:01 +08:00
|
|
|
"(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
|
|
|
|
"licensed under LGPL2.1\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;
|
|
|
|
if (use_ssl)
|
|
|
|
use_ssl = 2;
|
|
|
|
} 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;
|
|
|
|
info.interface = interface;
|
|
|
|
info.protocols = protocols;
|
2013-01-30 11:03:01 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-02-09 14:01:09 +08:00
|
|
|
info.extensions = libwebsocket_internal_extensions;
|
2013-01-30 11:03:01 +08:00
|
|
|
#endif
|
2013-02-09 14:01:09 +08:00
|
|
|
if (use_ssl && !client) {
|
|
|
|
info.ssl_cert_filepath = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
|
|
|
|
info.ssl_private_key_filepath = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
|
|
|
|
}
|
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
|
|
|
info.options = opts;
|
|
|
|
|
|
|
|
context = libwebsocket_create_context(&info);
|
2013-01-30 11:03:01 +08:00
|
|
|
|
|
|
|
if (context == NULL) {
|
|
|
|
lwsl_err("libwebsocket init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
if (client) {
|
|
|
|
lwsl_notice("Client connecting to %s:%u....\n", address, port);
|
|
|
|
/* we are in client mode */
|
|
|
|
wsi = libwebsocket_client_connect(context, address,
|
|
|
|
port, use_ssl, "/", address,
|
|
|
|
"origin", NULL, -1);
|
|
|
|
if (!wsi) {
|
|
|
|
lwsl_err("Client failed to connect to %s:%u\n", address, port);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
lwsl_notice("Client connected to %s:%u\n", address, port);
|
|
|
|
}
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (n >= 0 && !force_exit) {
|
2013-01-31 09:57:05 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-30 11:03:01 +08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
if (((unsigned int)tv.tv_usec - oldus) > rate_us) {
|
|
|
|
libwebsocket_callback_on_writable_all_protocol(&protocols[0]);
|
|
|
|
oldus = tv.tv_usec;
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 09:57:05 +08:00
|
|
|
#endif
|
2013-01-30 11:03:01 +08:00
|
|
|
n = libwebsocket_service(context, 10);
|
|
|
|
}
|
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
|
2013-01-30 11:03:01 +08:00
|
|
|
libwebsocket_context_destroy(context);
|
|
|
|
|
|
|
|
lwsl_notice("libwebsockets-test-echo exited cleanly\n");
|
|
|
|
|
|
|
|
closelog();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|