2018-06-05 09:14:38 +02:00
|
|
|
/** Simple WebSocket relay facilitating client-to-client connections.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2019-01-13 00:42:39 +01:00
|
|
|
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
2018-06-05 09:14:38 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
#include <iostream>
|
2018-06-05 09:14:38 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2018-06-20 20:55:17 +02:00
|
|
|
#include <queue>
|
2018-06-05 09:14:38 +02:00
|
|
|
#include <string>
|
2018-06-20 20:55:17 +02:00
|
|
|
#include <utility>
|
2018-06-05 09:14:38 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-07-17 17:56:56 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-01-12 13:48:29 +01:00
|
|
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
|
|
|
#include <villas/copyright.hpp>
|
2018-07-17 17:56:56 +02:00
|
|
|
|
2018-06-05 09:14:38 +02:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2018-07-17 17:56:56 +02:00
|
|
|
auto console = spdlog::stdout_color_mt("console");
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
/** The libwebsockets server context. */
|
|
|
|
static lws_context *context;
|
|
|
|
|
|
|
|
/** The libwebsockets vhost. */
|
|
|
|
static lws_vhost *vhost;
|
|
|
|
|
|
|
|
/* Forward declarations */
|
|
|
|
lws_callback_function protocol_cb;
|
2018-07-16 22:19:55 +02:00
|
|
|
class Session;
|
|
|
|
class Connection;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-12 20:31:16 +01:00
|
|
|
static std::map<std::string, Session *> sessions;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
class InvalidUrlException { };
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
bool loopback;
|
|
|
|
} opts;
|
|
|
|
|
|
|
|
class Frame : public std::vector<uint8_t> {
|
|
|
|
public:
|
|
|
|
Frame() {
|
2019-01-14 09:59:51 +01:00
|
|
|
/* lws_write() requires LWS_PRE bytes in front of the payload */
|
2018-06-20 20:55:17 +02:00
|
|
|
insert(end(), LWS_PRE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t * data() {
|
|
|
|
return std::vector<uint8_t>::data() + LWS_PRE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type size() {
|
|
|
|
return std::vector<uint8_t>::size() - LWS_PRE;
|
|
|
|
}
|
|
|
|
};
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
class Session {
|
|
|
|
public:
|
|
|
|
typedef std::string Identifier;
|
|
|
|
|
2019-01-12 20:31:16 +01:00
|
|
|
static Session * get(lws *wsi)
|
2018-06-05 09:14:38 +02:00
|
|
|
{
|
2018-06-20 20:55:17 +02:00
|
|
|
char uri[64];
|
|
|
|
|
|
|
|
/* We use the URI to associate this connection to a session
|
|
|
|
* Example: ws://example.com/node_1
|
|
|
|
* Will select the session with the name 'node_1'
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Get path of incoming request */
|
|
|
|
lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI);
|
|
|
|
if (strlen(uri) <= 0)
|
|
|
|
throw InvalidUrlException();
|
|
|
|
|
2019-01-12 19:05:57 +01:00
|
|
|
Identifier sid = uri;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2018-06-05 09:14:38 +02:00
|
|
|
auto it = sessions.find(sid);
|
|
|
|
if (it == sessions.end()) {
|
2019-01-12 20:31:16 +01:00
|
|
|
return new Session(sid);
|
2018-06-05 09:14:38 +02:00
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
else {
|
2019-01-12 20:31:16 +01:00
|
|
|
console->info("Found existing session: {}", sid);
|
2018-07-16 22:19:55 +02:00
|
|
|
|
2018-06-05 09:14:38 +02:00
|
|
|
return it->second;
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Session(Identifier sid) :
|
|
|
|
identifier(sid)
|
2019-01-12 20:31:16 +01:00
|
|
|
{
|
|
|
|
console->info("Session created: {}", identifier);
|
|
|
|
|
|
|
|
sessions[sid] = this;
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
~Session()
|
2019-01-12 20:31:16 +01:00
|
|
|
{
|
|
|
|
console->info("Session destroyed: {}", identifier);
|
|
|
|
|
|
|
|
sessions.erase(identifier);
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
Identifier identifier;
|
|
|
|
|
2019-01-12 20:31:16 +01:00
|
|
|
std::map<lws *, Connection *> connections;
|
2018-06-05 09:14:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Connection {
|
2018-06-20 20:55:17 +02:00
|
|
|
|
|
|
|
protected:
|
2018-06-05 09:14:38 +02:00
|
|
|
lws *wsi;
|
|
|
|
|
|
|
|
std::shared_ptr<Frame> currentFrame;
|
|
|
|
|
2018-07-16 22:19:55 +02:00
|
|
|
std::queue<std::shared_ptr<Frame>> outgoingFrames;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-12 20:31:16 +01:00
|
|
|
Session *session;
|
|
|
|
|
|
|
|
char name[128];
|
|
|
|
char ip[128];
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
public:
|
|
|
|
Connection(lws *w) :
|
|
|
|
wsi(w),
|
|
|
|
currentFrame(std::make_shared<Frame>()),
|
|
|
|
outgoingFrames()
|
|
|
|
{
|
2018-07-16 22:19:55 +02:00
|
|
|
session = Session::get(wsi);
|
2019-01-12 20:31:16 +01:00
|
|
|
session->connections[wsi] = this;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-12 20:31:16 +01:00
|
|
|
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), ip, sizeof(ip));
|
|
|
|
|
|
|
|
console->info("New connection established: session={}, remote={} ({})", session->identifier, name, ip);
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 09:59:51 +01:00
|
|
|
~Connection()
|
|
|
|
{
|
2019-01-12 20:31:16 +01:00
|
|
|
console->info("Connection closed: session={}, remote={} ({})", session->identifier, name, ip);
|
2018-06-20 20:55:17 +02:00
|
|
|
|
|
|
|
session->connections.erase(wsi);
|
2019-01-12 20:31:16 +01:00
|
|
|
|
|
|
|
if (session->connections.empty())
|
|
|
|
delete session;
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 09:59:51 +01:00
|
|
|
void write()
|
|
|
|
{
|
2019-01-14 10:00:09 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
while (!outgoingFrames.empty()) {
|
|
|
|
std::shared_ptr<Frame> fr = outgoingFrames.front();
|
|
|
|
|
2019-01-14 10:00:09 +01:00
|
|
|
ret = lws_write(wsi, fr->data(), fr->size(), LWS_WRITE_BINARY);
|
|
|
|
if (ret < 0)
|
|
|
|
return;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
|
|
|
outgoingFrames.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:59:51 +01:00
|
|
|
void read(void *in, size_t len)
|
|
|
|
{
|
2018-06-20 20:55:17 +02:00
|
|
|
currentFrame->insert(currentFrame->end(), (uint8_t *) in, (uint8_t *) in + len);
|
|
|
|
|
|
|
|
if (lws_is_final_fragment(wsi)) {
|
2018-07-17 17:56:56 +02:00
|
|
|
console->debug("Received frame, relaying to {} connections", session->connections.size() - (opts.loopback ? 0 : 1));
|
2018-07-16 22:19:55 +02:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
for (auto p : session->connections) {
|
|
|
|
auto c = p.second;
|
|
|
|
|
|
|
|
/* We skip the current connection in order
|
|
|
|
* to avoid receiving our own data */
|
2019-01-12 20:31:16 +01:00
|
|
|
if (opts.loopback == false && c == this)
|
2018-06-20 20:55:17 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
c->outgoingFrames.push(currentFrame);
|
|
|
|
|
|
|
|
lws_callback_on_writable(c->wsi);
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFrame = std::make_shared<Frame>();
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** List of libwebsockets protocols. */
|
|
|
|
lws_protocols protocols[] = {
|
|
|
|
{
|
|
|
|
.name = "live",
|
|
|
|
.callback = protocol_cb,
|
|
|
|
.per_session_data_size = sizeof(Connection),
|
|
|
|
.rx_buffer_size = 0
|
|
|
|
},
|
|
|
|
{ NULL /* terminator */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
/** List of libwebsockets extensions. */
|
|
|
|
static const lws_extension extensions[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"permessage-deflate"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"deflate-frame",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"deflate_frame"
|
|
|
|
},
|
|
|
|
{ NULL /* terminator */ }
|
|
|
|
};
|
|
|
|
|
2019-01-14 09:59:51 +01:00
|
|
|
static void logger(int level, const char *msg)
|
|
|
|
{
|
2018-07-17 17:56:56 +02:00
|
|
|
auto log = spdlog::get("lws");
|
|
|
|
|
2019-01-12 19:06:41 +01:00
|
|
|
char *nl = (char *) strchr(msg, '\n');
|
|
|
|
if (nl)
|
|
|
|
*nl = 0;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
/* Decrease severity for some errors. */
|
|
|
|
if (strstr(msg, "Unable to open") == msg)
|
|
|
|
level = LLL_WARN;
|
|
|
|
|
|
|
|
switch (level) {
|
2018-07-17 17:56:56 +02:00
|
|
|
case LLL_ERR: log->error("{}", msg); break;
|
|
|
|
case LLL_WARN: log->warn( "{}", msg); break;
|
|
|
|
case LLL_INFO: log->info( "{}", msg); break;
|
|
|
|
default: log->debug("{}", msg); break;
|
2018-06-05 09:14:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int protocol_cb(lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
|
|
|
|
{
|
|
|
|
Connection *c = reinterpret_cast<Connection *>(user);
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
2018-07-16 22:19:55 +02:00
|
|
|
case LWS_CALLBACK_ESTABLISHED: {
|
2018-06-20 20:55:17 +02:00
|
|
|
try {
|
|
|
|
new (c) Connection(wsi);
|
|
|
|
}
|
|
|
|
catch (InvalidUrlException e) {
|
2018-06-05 09:14:38 +02:00
|
|
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR, (unsigned char *) "Invalid URL", strlen("Invalid URL"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2018-07-16 22:19:55 +02:00
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
c->~Connection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2018-06-20 20:55:17 +02:00
|
|
|
c->write();
|
2018-06-05 09:14:38 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2018-06-20 20:55:17 +02:00
|
|
|
c->read(in, len);
|
2018-06-05 09:14:38 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cout << "Usage: villas-relay [OPTIONS]" << std::endl;
|
|
|
|
std::cout << " OPTIONS is one or more of the following options:" << std::endl;
|
|
|
|
std::cout << " -d LVL set debug level" << std::endl;
|
|
|
|
std::cout << " -p PORT the port number to listen on" << std::endl;
|
2018-07-17 17:56:56 +02:00
|
|
|
std::cout << " -P PROT the websocket protocol" << std::endl;
|
2018-06-20 20:55:17 +02:00
|
|
|
std::cout << " -V show version and exit" << std::endl;
|
|
|
|
std::cout << " -h show usage and exit" << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
2019-01-12 13:48:29 +01:00
|
|
|
villas::print_copyright();
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 09:14:38 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-01-12 19:05:57 +01:00
|
|
|
/* Initialize logging */
|
2018-07-17 17:56:56 +02:00
|
|
|
spdlog::stdout_color_mt("lws");
|
2018-06-05 09:14:38 +02:00
|
|
|
lws_set_log_level((1 << LLL_COUNT) - 1, logger);
|
|
|
|
|
|
|
|
/* Start server */
|
|
|
|
lws_context_creation_info ctx_info = { 0 };
|
|
|
|
|
|
|
|
ctx_info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
|
|
ctx_info.gid = -1;
|
|
|
|
ctx_info.uid = -1;
|
|
|
|
ctx_info.protocols = protocols;
|
|
|
|
ctx_info.extensions = extensions;
|
|
|
|
ctx_info.port = 8088;
|
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
char c, *endptr;
|
2019-01-12 19:07:03 +01:00
|
|
|
while ((c = getopt (argc, argv, "hVp:P:ld:")) != -1) {
|
2018-06-20 20:55:17 +02:00
|
|
|
switch (c) {
|
2019-01-12 19:07:03 +01:00
|
|
|
case 'd':
|
|
|
|
spdlog::set_level(spdlog::level::from_str(optarg));
|
|
|
|
break;
|
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
case 'p':
|
|
|
|
ctx_info.port = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
2019-01-12 19:05:57 +01:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
case 'P':
|
|
|
|
protocols[0].name = optarg;
|
|
|
|
break;
|
2019-01-12 19:05:57 +01:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
case 'l':
|
|
|
|
opts.loopback = true;
|
|
|
|
break;
|
2019-01-12 19:05:57 +01:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
case 'V':
|
2019-01-12 13:48:29 +01:00
|
|
|
villas::print_version();
|
2018-06-20 20:55:17 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2019-01-12 19:05:57 +01:00
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2019-01-12 13:48:29 +01:00
|
|
|
check: if (optarg == endptr) {
|
|
|
|
console->error("Failed to parse parse option argument '-{} {}'", c, optarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind < 0) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-06-05 09:14:38 +02:00
|
|
|
context = lws_create_context(&ctx_info);
|
2018-07-17 17:56:56 +02:00
|
|
|
if (context == NULL) {
|
|
|
|
console->error("WebSocket: failed to initialize server context");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
vhost = lws_create_vhost(context, &ctx_info);
|
2018-07-17 17:56:56 +02:00
|
|
|
if (vhost == NULL) {
|
|
|
|
console->error("WebSocket: failed to initialize virtual host");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
lws_service(context, 100);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|