2017-03-07 12:03:37 +00:00
|
|
|
#if defined(CONF_WEBSOCKETS)
|
2015-02-07 22:15:58 +00:00
|
|
|
|
2020-09-12 15:12:00 +00:00
|
|
|
#include <map>
|
2015-02-07 22:15:58 +00:00
|
|
|
#include <stdlib.h>
|
2020-09-12 15:12:00 +00:00
|
|
|
#include <string>
|
2015-02-07 22:15:58 +00:00
|
|
|
|
|
|
|
#include "base/system.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "ringbuffer.h"
|
2020-09-15 12:54:33 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2015-02-07 22:15:58 +00:00
|
|
|
#include <arpa/inet.h>
|
2020-09-15 12:54:33 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
2020-09-17 11:44:47 +00:00
|
|
|
#include <ws2tcpip.h>
|
2020-09-15 12:54:33 +00:00
|
|
|
#endif
|
2020-09-11 17:06:45 +00:00
|
|
|
#include <libwebsockets.h>
|
2015-02-07 22:15:58 +00:00
|
|
|
|
|
|
|
#include "websockets.h"
|
|
|
|
|
|
|
|
// not sure why would anyone need more than one but well...
|
|
|
|
#define WS_CONTEXTS 4
|
|
|
|
// ddnet client opens two connections for whatever reason
|
2020-09-11 17:06:45 +00:00
|
|
|
#define WS_CLIENTS (MAX_CLIENTS * 2)
|
2015-02-07 22:15:58 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
typedef CStaticRingBuffer<unsigned char, WS_CLIENTS * 4 * 1024,
|
2020-09-11 17:06:45 +00:00
|
|
|
CRingBufferBase::FLAG_RECYCLE>
|
|
|
|
TRecvBuffer;
|
2020-10-27 17:57:14 +00:00
|
|
|
typedef CStaticRingBuffer<unsigned char, 4 * 1024,
|
2020-09-11 17:06:45 +00:00
|
|
|
CRingBufferBase::FLAG_RECYCLE>
|
|
|
|
TSendBuffer;
|
2015-02-07 22:15:58 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
size_t read;
|
|
|
|
sockaddr_in addr;
|
|
|
|
unsigned char data[0];
|
|
|
|
} websocket_chunk;
|
|
|
|
|
|
|
|
struct per_session_data
|
|
|
|
{
|
2018-02-06 23:07:06 +00:00
|
|
|
struct lws *wsi;
|
2020-09-12 15:12:00 +00:00
|
|
|
std::string addr_str;
|
2015-02-07 22:15:58 +00:00
|
|
|
sockaddr_in addr;
|
|
|
|
TSendBuffer send_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct context_data
|
|
|
|
{
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context *context;
|
2020-09-12 15:12:00 +00:00
|
|
|
std::map<std::string, per_session_data *> port_map;
|
2015-02-07 22:15:58 +00:00
|
|
|
TRecvBuffer recv_buffer;
|
|
|
|
};
|
|
|
|
|
2020-09-11 17:06:45 +00:00
|
|
|
static int receive_chunk(context_data *ctx_data, struct per_session_data *pss,
|
|
|
|
void *in, size_t len)
|
2015-02-07 22:15:58 +00:00
|
|
|
{
|
2020-09-11 17:06:45 +00:00
|
|
|
websocket_chunk *chunk = (websocket_chunk *)ctx_data->recv_buffer.Allocate(
|
|
|
|
len + sizeof(websocket_chunk));
|
2015-02-07 22:15:58 +00:00
|
|
|
if(chunk == 0)
|
|
|
|
return 1;
|
|
|
|
chunk->size = len;
|
|
|
|
chunk->read = 0;
|
|
|
|
memcpy(&chunk->addr, &pss->addr, sizeof(sockaddr_in));
|
|
|
|
memcpy(&chunk->data[0], in, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:45 +00:00
|
|
|
static int websocket_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
2015-02-07 22:15:58 +00:00
|
|
|
{
|
|
|
|
struct per_session_data *pss = (struct per_session_data *)user;
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context *context = lws_get_context(wsi);
|
|
|
|
context_data *ctx_data = (context_data *)lws_context_user(context);
|
2015-02-07 22:15:58 +00:00
|
|
|
switch(reason)
|
|
|
|
{
|
2020-09-12 15:12:00 +00:00
|
|
|
case LWS_CALLBACK_WSI_CREATE:
|
|
|
|
if(pss == NULL)
|
2015-02-07 22:15:58 +00:00
|
|
|
{
|
2020-09-12 15:12:00 +00:00
|
|
|
return 0;
|
2015-02-07 22:15:58 +00:00
|
|
|
}
|
2020-09-12 15:12:00 +00:00
|
|
|
/* FALLTHRU */
|
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
|
|
{
|
2015-02-07 22:15:58 +00:00
|
|
|
pss->wsi = wsi;
|
2018-02-06 23:07:06 +00:00
|
|
|
int fd = lws_get_socket_fd(wsi);
|
2015-02-07 22:15:58 +00:00
|
|
|
socklen_t addr_size = sizeof(pss->addr);
|
|
|
|
getpeername(fd, (struct sockaddr *)&pss->addr, &addr_size);
|
|
|
|
int orig_port = ntohs(pss->addr.sin_port);
|
|
|
|
pss->send_buffer.Init();
|
|
|
|
char addr_str[NETADDR_MAXSTRSIZE];
|
2020-09-17 15:23:16 +00:00
|
|
|
int ip_uint32 = pss->addr.sin_addr.s_addr;
|
|
|
|
str_format(addr_str, sizeof(addr_str), "%d.%d.%d.%d", (ip_uint32)&0xff, (ip_uint32 >> 8) & 0xff, (ip_uint32 >> 16) & 0xff, (ip_uint32 >> 24) & 0xff);
|
2020-09-11 17:06:45 +00:00
|
|
|
dbg_msg("websockets",
|
2020-09-12 15:12:00 +00:00
|
|
|
"connection established with %s:%d",
|
|
|
|
addr_str, orig_port);
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "%s:%d", addr_str, orig_port);
|
|
|
|
pss->addr_str = std::string(buf);
|
|
|
|
ctx_data->port_map[pss->addr_str] = pss;
|
2015-02-07 22:15:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
{
|
2020-09-12 15:12:00 +00:00
|
|
|
dbg_msg("websockets", "connection with addr string %s closed", pss->addr_str.c_str());
|
|
|
|
if(!pss->addr_str.empty())
|
2020-09-11 17:06:45 +00:00
|
|
|
{
|
|
|
|
unsigned char close_packet[] = {0x10, 0x0e, 0x00, 0x04};
|
2015-02-07 22:15:58 +00:00
|
|
|
receive_chunk(ctx_data, pss, &close_packet, sizeof(close_packet));
|
|
|
|
pss->wsi = 0;
|
2020-09-12 15:12:00 +00:00
|
|
|
ctx_data->port_map.erase(pss->addr_str);
|
2015-02-07 22:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-09-12 15:12:00 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
|
/* FALLTHRU */
|
2015-02-07 22:15:58 +00:00
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
{
|
|
|
|
websocket_chunk *chunk = (websocket_chunk *)pss->send_buffer.First();
|
|
|
|
if(chunk == NULL)
|
|
|
|
break;
|
|
|
|
int len = chunk->size - chunk->read;
|
2020-09-11 17:06:45 +00:00
|
|
|
int n =
|
|
|
|
lws_write(wsi, &chunk->data[LWS_SEND_BUFFER_PRE_PADDING + chunk->read],
|
|
|
|
chunk->size - chunk->read, LWS_WRITE_BINARY);
|
2015-02-07 22:15:58 +00:00
|
|
|
if(n < 0)
|
|
|
|
return 1;
|
|
|
|
if(n < len)
|
|
|
|
{
|
|
|
|
chunk->read += n;
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_callback_on_writable(wsi);
|
2015-02-07 22:15:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pss->send_buffer.PopFirst();
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_callback_on_writable(wsi);
|
2015-02-07 22:15:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-09-12 15:12:00 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
|
|
|
/* FALLTHRU */
|
2015-02-07 22:15:58 +00:00
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2020-09-12 15:12:00 +00:00
|
|
|
if(pss->addr_str.empty())
|
2015-02-07 22:15:58 +00:00
|
|
|
return -1;
|
2020-09-11 16:53:15 +00:00
|
|
|
if(receive_chunk(ctx_data, pss, in, len))
|
2015-02-07 22:15:58 +00:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:45 +00:00
|
|
|
static struct lws_protocols protocols[] = {
|
|
|
|
{
|
|
|
|
"binary", /* name */
|
|
|
|
websocket_callback, /* callback */
|
|
|
|
sizeof(struct per_session_data) /* per_session_data_size */
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NULL, NULL, 0 /* End of list */
|
|
|
|
}};
|
2015-02-07 22:15:58 +00:00
|
|
|
|
|
|
|
static context_data contexts[WS_CONTEXTS];
|
|
|
|
|
|
|
|
int websocket_create(const char *addr, int port)
|
|
|
|
{
|
|
|
|
struct lws_context_creation_info info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.port = port;
|
|
|
|
info.iface = addr;
|
|
|
|
info.protocols = protocols;
|
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
|
|
|
|
|
|
|
// find free context
|
|
|
|
int first_free = -1;
|
|
|
|
for(int i = 0; i < WS_CONTEXTS; i++)
|
|
|
|
{
|
|
|
|
if(contexts[i].context == NULL)
|
|
|
|
{
|
|
|
|
first_free = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(first_free == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
context_data *ctx_data = &contexts[first_free];
|
|
|
|
info.user = (void *)ctx_data;
|
|
|
|
|
2018-02-06 23:07:06 +00:00
|
|
|
ctx_data->context = lws_create_context(&info);
|
2015-02-07 22:15:58 +00:00
|
|
|
if(ctx_data->context == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ctx_data->recv_buffer.Init();
|
|
|
|
return first_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
int websocket_destroy(int socket)
|
|
|
|
{
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context *context = contexts[socket].context;
|
2015-02-07 22:15:58 +00:00
|
|
|
if(context == NULL)
|
|
|
|
return -1;
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context_destroy(context);
|
2015-02-07 22:15:58 +00:00
|
|
|
contexts[socket].context = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:45 +00:00
|
|
|
int websocket_recv(int socket, unsigned char *data, size_t maxsize,
|
|
|
|
struct sockaddr_in *sockaddrbuf, size_t fromLen)
|
2015-02-07 22:15:58 +00:00
|
|
|
{
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context *context = contexts[socket].context;
|
2015-02-07 22:15:58 +00:00
|
|
|
if(context == NULL)
|
|
|
|
return -1;
|
2020-09-11 16:53:15 +00:00
|
|
|
int n = lws_service(context, -1);
|
2015-02-07 22:15:58 +00:00
|
|
|
if(n < 0)
|
|
|
|
return n;
|
2018-02-06 23:07:06 +00:00
|
|
|
context_data *ctx_data = (context_data *)lws_context_user(context);
|
2015-02-07 22:15:58 +00:00
|
|
|
websocket_chunk *chunk = (websocket_chunk *)ctx_data->recv_buffer.First();
|
|
|
|
if(chunk == 0)
|
|
|
|
return 0;
|
|
|
|
if(maxsize >= chunk->size - chunk->read)
|
|
|
|
{
|
|
|
|
int len = chunk->size - chunk->read;
|
|
|
|
memcpy(data, &chunk->data[chunk->read], len);
|
|
|
|
memcpy(sockaddrbuf, &chunk->addr, fromLen);
|
|
|
|
ctx_data->recv_buffer.PopFirst();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(data, &chunk->data[chunk->read], maxsize);
|
|
|
|
memcpy(sockaddrbuf, &chunk->addr, fromLen);
|
|
|
|
chunk->read += maxsize;
|
|
|
|
return maxsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:45 +00:00
|
|
|
int websocket_send(int socket, const unsigned char *data, size_t size,
|
2020-09-12 15:12:00 +00:00
|
|
|
const char *addr_str, int port)
|
2015-02-07 22:15:58 +00:00
|
|
|
{
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context *context = contexts[socket].context;
|
2015-02-07 22:15:58 +00:00
|
|
|
if(context == NULL)
|
2020-09-12 15:12:00 +00:00
|
|
|
{
|
2015-02-07 22:15:58 +00:00
|
|
|
return -1;
|
2020-09-12 15:12:00 +00:00
|
|
|
}
|
2018-02-06 23:07:06 +00:00
|
|
|
context_data *ctx_data = (context_data *)lws_context_user(context);
|
2020-09-12 15:12:00 +00:00
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "%s:%d", addr_str, port);
|
|
|
|
std::string addr_str_with_port = std::string(buf);
|
|
|
|
struct per_session_data *pss = ctx_data->port_map[addr_str_with_port];
|
2015-02-07 22:15:58 +00:00
|
|
|
if(pss == NULL)
|
2020-09-12 15:12:00 +00:00
|
|
|
{
|
|
|
|
struct lws_client_connect_info ccinfo = {0};
|
|
|
|
ccinfo.context = context;
|
|
|
|
ccinfo.address = addr_str;
|
|
|
|
ccinfo.port = port;
|
|
|
|
ccinfo.protocol = protocols[0].name;
|
|
|
|
lws *wsi = lws_client_connect_via_info(&ccinfo);
|
|
|
|
if(wsi == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lws_service(context, -1);
|
|
|
|
pss = ctx_data->port_map[addr_str_with_port];
|
|
|
|
if(pss == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 17:06:45 +00:00
|
|
|
websocket_chunk *chunk = (websocket_chunk *)pss->send_buffer.Allocate(
|
|
|
|
size + sizeof(websocket_chunk) + LWS_SEND_BUFFER_PRE_PADDING +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING);
|
2015-02-07 22:15:58 +00:00
|
|
|
if(chunk == NULL)
|
|
|
|
return -1;
|
|
|
|
chunk->size = size;
|
|
|
|
chunk->read = 0;
|
|
|
|
memcpy(&chunk->addr, &pss->addr, sizeof(sockaddr_in));
|
|
|
|
memcpy(&chunk->data[LWS_SEND_BUFFER_PRE_PADDING], data, size);
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_callback_on_writable(pss->wsi);
|
2020-09-12 15:12:00 +00:00
|
|
|
lws_service(context, -1);
|
2015-02-07 22:15:58 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int websocket_fd_set(int socket, fd_set *set)
|
|
|
|
{
|
2018-02-06 23:07:06 +00:00
|
|
|
lws_context *context = contexts[socket].context;
|
2015-02-07 22:15:58 +00:00
|
|
|
if(context == NULL)
|
|
|
|
return -1;
|
2020-09-18 13:18:31 +00:00
|
|
|
lws_service(context, -1);
|
2018-02-06 23:07:06 +00:00
|
|
|
context_data *ctx_data = (context_data *)lws_context_user(context);
|
2015-02-07 22:15:58 +00:00
|
|
|
int max = 0;
|
2020-09-12 15:12:00 +00:00
|
|
|
for(auto const &x : ctx_data->port_map)
|
2015-02-07 22:15:58 +00:00
|
|
|
{
|
2020-09-12 15:12:00 +00:00
|
|
|
if(x.second == NULL)
|
2015-02-07 22:15:58 +00:00
|
|
|
continue;
|
2020-09-12 15:12:00 +00:00
|
|
|
int fd = lws_get_socket_fd(x.second->wsi);
|
2015-02-07 22:15:58 +00:00
|
|
|
if(fd > max)
|
|
|
|
max = fd;
|
|
|
|
FD_SET(fd, set);
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
2021-06-15 01:24:23 +00:00
|
|
|
|
2020-09-17 11:44:47 +00:00
|
|
|
#endif
|