api: serve the websocket with wslay instead of headsocket (#887)
`HeadSocket` library we are currently using is full of bugs and no longer maintained. Swap it out with `wslay`.
This commit is contained in:
@@ -254,6 +254,7 @@ add_subdirectory(external/discord-rpc EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(external/hash-library EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(external/imgui EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(external/minhook EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(external/wslay EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(external/cpu_features EXCLUDE_FROM_ALL)
|
||||
|
||||
# libjpeg-turbo, prebuilt into the deps image. The WinXP toolchains have their own
|
||||
@@ -797,7 +798,7 @@ endfunction()
|
||||
add_library(spicetools_spice_objs OBJECT ${SOURCE_FILES})
|
||||
target_link_libraries(spicetools_spice_objs
|
||||
PUBLIC d3d9 ws2_32 version comctl32 shlwapi iphlpapi hid secur32 setupapi psapi winmm winhttp
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook wslay imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
target_link_libraries(spicetools_spice_objs PUBLIC winscard)
|
||||
|
||||
if(NOT MSVC)
|
||||
@@ -837,7 +838,7 @@ set(RESOURCE_FILES build/manifest.manifest build/manifest.rc build/icon.rc cfg/W
|
||||
add_executable(spicetools_spice_linux ${SOURCE_FILES} ${RESOURCE_FILES})
|
||||
target_link_libraries(spicetools_spice_linux
|
||||
PUBLIC d3d9 ws2_32 version comctl32 shlwapi iphlpapi hid secur32 setupapi psapi winmm winhttp
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook wslay imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
set_target_properties(spicetools_spice_linux PROPERTIES PREFIX "")
|
||||
set_target_properties(spicetools_spice_linux PROPERTIES OUTPUT_NAME "spice_linux")
|
||||
target_compile_definitions(spicetools_spice_linux PRIVATE NO_SCARD=1 PRIVATE SPICE_LINUX=1)
|
||||
@@ -855,7 +856,7 @@ add_executable(spicetools_spice64 ${SOURCE_FILES} ${RESOURCE_FILES})
|
||||
# do NOT link against: mf, mfplat, mfreadwrite; otherwise unity games will break
|
||||
target_link_libraries(spicetools_spice64
|
||||
PUBLIC d3d9 ws2_32 version comctl32 shlwapi iphlpapi hid secur32 setupapi psapi winmm winhttp mfuuid strmiids dxva2
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook wslay imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
target_link_libraries(spicetools_spice64 PUBLIC winscard)
|
||||
set_target_properties(spicetools_spice64 PROPERTIES PREFIX "")
|
||||
set_target_properties(spicetools_spice64 PROPERTIES OUTPUT_NAME "spice64")
|
||||
@@ -878,7 +879,7 @@ add_executable(spicetools_spice64_linux ${SOURCE_FILES} ${RESOURCE_FILES})
|
||||
# do NOT link against: mf, mfplat, mfreadwrite; otherwise unity games will break
|
||||
target_link_libraries(spicetools_spice64_linux
|
||||
PUBLIC d3d9 ws2_32 version comctl32 shlwapi iphlpapi hid secur32 setupapi psapi winmm winhttp mfuuid strmiids dxva2
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook wslay imm32 dwmapi CpuFeatures::cpu_features spice_jpeg spice_x264)
|
||||
set_target_properties(spicetools_spice64_linux PROPERTIES PREFIX "")
|
||||
set_target_properties(spicetools_spice64_linux PROPERTIES OUTPUT_NAME "spice64_linux")
|
||||
target_compile_definitions(spicetools_spice64_linux PRIVATE SPICE64=1)
|
||||
@@ -900,7 +901,7 @@ add_executable(spicetools_cfg WIN32 ${SOURCE_FILES} ${RESOURCE_FILES})
|
||||
# the configurator serves neither the API nor the video stream, so it needs no codecs
|
||||
target_link_libraries(spicetools_cfg
|
||||
PUBLIC d3d9 ws2_32 version comctl32 shlwapi iphlpapi hid secur32 setupapi psapi winmm winhttp strmiids
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook imm32 dwmapi CpuFeatures::cpu_features)
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook wslay imm32 dwmapi CpuFeatures::cpu_features)
|
||||
target_link_libraries(spicetools_cfg PUBLIC winscard)
|
||||
set_target_properties(spicetools_cfg PROPERTIES PREFIX "")
|
||||
set_target_properties(spicetools_cfg PROPERTIES OUTPUT_NAME "spicecfg")
|
||||
@@ -918,7 +919,7 @@ set(RESOURCE_FILES cfg/manifest.manifest cfg/manifest.rc cfg/icon.rc cfg/Win32D.
|
||||
add_executable(spicetools_cfg_linux WIN32 ${SOURCE_FILES} ${RESOURCE_FILES})
|
||||
target_link_libraries(spicetools_cfg_linux
|
||||
PUBLIC d3d9 ws2_32 version comctl32 shlwapi iphlpapi hid secur32 setupapi psapi winmm winhttp strmiids
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook imm32 dwmapi CpuFeatures::cpu_features)
|
||||
PRIVATE fmt::fmt-header-only discord-rpc imgui hash-library minhook wslay imm32 dwmapi CpuFeatures::cpu_features)
|
||||
set_target_properties(spicetools_cfg_linux PROPERTIES PREFIX "")
|
||||
set_target_properties(spicetools_cfg_linux PROPERTIES OUTPUT_NAME "spicecfg_linux")
|
||||
target_compile_definitions(spicetools_cfg_linux PRIVATE SPICETOOLS_SPICECFG_STANDALONE=1)
|
||||
|
||||
+510
-147
@@ -1,216 +1,579 @@
|
||||
#define HEADSOCKET_IMPLEMENTATION
|
||||
#include "external/headsocket.h"
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#include "websocket.h"
|
||||
#include "util/utils.h"
|
||||
#include "util/rc4.h"
|
||||
#include "util/logging.h"
|
||||
#include "overlay/notifications.h"
|
||||
#include "controller.h"
|
||||
|
||||
using namespace headsocket;
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "external/wslay/msvc_compat.h"
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
#include "controller.h"
|
||||
#include "external/hash-library/sha1.h"
|
||||
#include "overlay/notifications.h"
|
||||
#include "util/crypt.h"
|
||||
#include "util/logging.h"
|
||||
|
||||
namespace api {
|
||||
|
||||
namespace {
|
||||
|
||||
// how long a single handshake read may stall before the connection is dropped;
|
||||
// headsocket reads the request a byte at a time, so this is an idle timeout between
|
||||
// bytes rather than a deadline for the whole handshake
|
||||
constexpr int handshake_timeout_ms = 5000;
|
||||
constexpr int server_backlog = 4;
|
||||
constexpr size_t client_limit = 8;
|
||||
|
||||
void set_recv_timeout(connection &conn, int milliseconds) {
|
||||
DWORD timeout = static_cast<DWORD>(milliseconds);
|
||||
setsockopt(conn.impl()->socket, SOL_SOCKET, SO_RCVTIMEO,
|
||||
// a peer that connects and then says nothing must not hold a slot forever
|
||||
constexpr int handshake_timeout_ms = 5000;
|
||||
constexpr size_t request_size_limit = 8 * 1024;
|
||||
constexpr uint64_t message_size_limit = 64 * 1024;
|
||||
|
||||
// how long a quiet connection waits before the loop rechecks whether we are stopping
|
||||
constexpr int idle_poll_ms = 500;
|
||||
|
||||
// RFC 6455 appends this to the client key before hashing
|
||||
constexpr const char *websocket_guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||
|
||||
constexpr double notification_throttle_seconds = 0.5;
|
||||
|
||||
std::string trim(const std::string &text) {
|
||||
const auto begin = text.find_first_not_of(" \t");
|
||||
if (begin == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
return text.substr(begin, text.find_last_not_of(" \t") - begin + 1);
|
||||
}
|
||||
|
||||
std::string to_lower(std::string text) {
|
||||
std::transform(text.begin(), text.end(), text.begin(), [](unsigned char c) {
|
||||
return static_cast<char>(std::tolower(c));
|
||||
});
|
||||
return text;
|
||||
}
|
||||
|
||||
bool contains_token(const std::string &value, const std::string &expected) {
|
||||
size_t pos = 0;
|
||||
while (pos < value.size()) {
|
||||
const size_t end = value.find(',', pos);
|
||||
if (to_lower(trim(value.substr(pos, end - pos))) == expected) {
|
||||
return true;
|
||||
}
|
||||
if (end == std::string::npos) {
|
||||
break;
|
||||
}
|
||||
pos = end + 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool valid_websocket_key(const std::string &key) {
|
||||
if (key.size() != 24 || key[22] != '=' || key[23] != '=') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string alphabet =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
for (size_t i = 0; i < 22; i++) {
|
||||
if (alphabet.find(key[i]) == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool send_all(SOCKET socket, const std::string &text) {
|
||||
size_t remaining = text.size();
|
||||
const char *cursor = text.data();
|
||||
|
||||
while (remaining > 0) {
|
||||
const int sent = send(socket, cursor, static_cast<int>(remaining), 0);
|
||||
if (sent <= 0) {
|
||||
return false;
|
||||
}
|
||||
cursor += sent;
|
||||
remaining -= static_cast<size_t>(sent);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void set_recv_timeout(SOCKET socket, int milliseconds) {
|
||||
const DWORD timeout = static_cast<DWORD>(milliseconds);
|
||||
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO,
|
||||
reinterpret_cast<const char *>(&timeout), sizeof(timeout));
|
||||
}
|
||||
|
||||
std::string accept_key(const std::string &client_key) {
|
||||
SHA1 sha1;
|
||||
sha1.add(client_key.data(), client_key.size());
|
||||
sha1.add(websocket_guid, strlen(websocket_guid));
|
||||
|
||||
unsigned char digest[SHA1::HashBytes] {};
|
||||
sha1.getHash(digest);
|
||||
|
||||
return crypt::base64_encode(digest, sizeof(digest));
|
||||
}
|
||||
|
||||
/*
|
||||
* Client class declaration
|
||||
*/
|
||||
class WebSocketClient : public web_socket_client {
|
||||
bool handshake(SOCKET socket) {
|
||||
std::string request;
|
||||
char byte = 0;
|
||||
bool complete = false;
|
||||
|
||||
// required class header
|
||||
HEADSOCKET_CLIENT(WebSocketClient, web_socket_client);
|
||||
while (request.size() < request_size_limit) {
|
||||
const int read = recv(socket, &byte, 1, 0);
|
||||
if (read <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
request.push_back(byte);
|
||||
if (request.size() >= 4
|
||||
&& request.compare(request.size() - 4, 4, "\r\n\r\n") == 0) {
|
||||
complete = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string key;
|
||||
std::string version;
|
||||
bool connection_upgrade = false;
|
||||
bool upgrade = false;
|
||||
const size_t request_line_end = request.find("\r\n");
|
||||
const std::string request_line = request.substr(0, request_line_end);
|
||||
const size_t target_end = request_line.find(' ', 4);
|
||||
const bool valid_request_line = request_line_end != std::string::npos
|
||||
&& request_line.compare(0, 4, "GET ") == 0
|
||||
&& target_end != std::string::npos
|
||||
&& target_end > 4
|
||||
&& target_end == request_line.rfind(' ')
|
||||
&& request_line.substr(target_end + 1) == "HTTP/1.1";
|
||||
size_t pos = request_line_end;
|
||||
|
||||
while (pos != std::string::npos) {
|
||||
const size_t end = request.find("\r\n", pos + 2);
|
||||
if (end == std::string::npos || end == pos + 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
const std::string line = request.substr(pos + 2, end - pos - 2);
|
||||
const size_t colon = line.find(':');
|
||||
if (colon != std::string::npos) {
|
||||
const std::string name = to_lower(trim(line.substr(0, colon)));
|
||||
const std::string value = trim(line.substr(colon + 1));
|
||||
|
||||
if (name == "sec-websocket-key") {
|
||||
key = value;
|
||||
} else if (name == "sec-websocket-version") {
|
||||
version = value;
|
||||
} else if (name == "connection") {
|
||||
connection_upgrade = connection_upgrade
|
||||
|| contains_token(value, "upgrade");
|
||||
} else if (name == "upgrade") {
|
||||
upgrade = upgrade || contains_token(value, "websocket");
|
||||
}
|
||||
}
|
||||
|
||||
pos = end;
|
||||
}
|
||||
|
||||
if (!complete || !valid_request_line || !connection_upgrade || !upgrade
|
||||
|| version != "13" || !valid_websocket_key(key)) {
|
||||
send_all(socket,
|
||||
"HTTP/1.1 400 Bad Request\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// deliberately echoes back no extension or subprotocol: naming one the client did
|
||||
// not offer is a handshake failure, and none of them are wanted here
|
||||
return send_all(socket,
|
||||
"HTTP/1.1 101 Switching Protocols\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Accept: " + accept_key(key) + "\r\n"
|
||||
"\r\n");
|
||||
}
|
||||
|
||||
// everything one connection needs; wslay hands this back to the callbacks
|
||||
struct Session {
|
||||
SOCKET socket = INVALID_SOCKET;
|
||||
Controller *controller = nullptr;
|
||||
ClientState *state = nullptr;
|
||||
|
||||
// headsocket doesn't expose the peer address on its own client API, but the
|
||||
// sockaddr_in captured at accept time is sitting right there in the impl
|
||||
std::string remote_address() const {
|
||||
char address_data[INET_ADDRSTRLEN] {};
|
||||
inet_ntop(AF_INET, &this->_p->conn.impl()->from.sin_addr,
|
||||
address_data, INET_ADDRSTRLEN);
|
||||
return std::string(address_data);
|
||||
}
|
||||
|
||||
protected:
|
||||
bool async_received_data(const data_block &db, uint8_t *ptr, size_t length) override;
|
||||
|
||||
void on_accept() override;
|
||||
void on_disconnect() override;
|
||||
bool failed = false;
|
||||
};
|
||||
|
||||
/*
|
||||
* Server class declaration
|
||||
*/
|
||||
class WebSocketServer : public web_socket_server<WebSocketClient> {
|
||||
HEADSOCKET_SERVER(WebSocketServer, web_socket_server);
|
||||
public:
|
||||
WebSocketController *websocket;
|
||||
ssize_t recv_callback(wslay_event_context_ptr ctx, uint8_t *buffer, size_t length,
|
||||
int flags, void *user_data) {
|
||||
|
||||
protected:
|
||||
bool handshake(connection &conn) override {
|
||||
(void) flags;
|
||||
|
||||
// headsocket runs the handshake on its single accept thread with a blocking
|
||||
// recv, so a peer that connects and then says nothing would park that thread and
|
||||
// leave every later connection sitting unaccepted in the backlog
|
||||
set_recv_timeout(conn, handshake_timeout_ms);
|
||||
const bool accepted = base_t::handshake(conn);
|
||||
auto *session = static_cast<Session *>(user_data);
|
||||
const int read = recv(session->socket, reinterpret_cast<char *>(buffer),
|
||||
static_cast<int>(length), 0);
|
||||
|
||||
// from here the client thread owns the socket and wants to block on reads
|
||||
set_recv_timeout(conn, 0);
|
||||
|
||||
return accepted;
|
||||
if (read > 0) {
|
||||
return read;
|
||||
}
|
||||
};
|
||||
|
||||
void api::WebSocketServer::init() {}
|
||||
// the socket is non-blocking, so an empty one has to read as "nothing yet"
|
||||
// rather than as a dead peer
|
||||
if (read < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wslay_event_set_error(ctx,
|
||||
read == 0 ? WSLAY_ERR_NO_MORE_MSG : WSLAY_ERR_CALLBACK_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t send_callback(wslay_event_context_ptr ctx, const uint8_t *data, size_t length,
|
||||
int flags, void *user_data) {
|
||||
|
||||
(void) flags;
|
||||
|
||||
auto *session = static_cast<Session *>(user_data);
|
||||
const int sent = send(session->socket, reinterpret_cast<const char *>(data),
|
||||
static_cast<int>(length), 0);
|
||||
|
||||
if (sent > 0) {
|
||||
return sent;
|
||||
}
|
||||
|
||||
if (sent < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void on_msg_recv(wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_on_msg_recv_arg *arg, void *user_data) {
|
||||
|
||||
auto *session = static_cast<Session *>(user_data);
|
||||
|
||||
// pings and closes are wslay's business, it answers them itself
|
||||
if (wslay_is_ctrl_frame(arg->opcode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (arg->opcode != WSLAY_BINARY_FRAME) {
|
||||
log_warning("api::websocket", "ignoring a non-binary message");
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<char> in(arg->msg, arg->msg + arg->msg_length);
|
||||
std::vector<char> out;
|
||||
|
||||
if (session->state->cipher) {
|
||||
session->state->cipher->crypt(
|
||||
reinterpret_cast<uint8_t *>(in.data()), in.size());
|
||||
}
|
||||
|
||||
session->controller->process_request(session->state, &in, &out);
|
||||
|
||||
if (session->state->cipher) {
|
||||
session->state->cipher->crypt(
|
||||
reinterpret_cast<uint8_t *>(out.data()), out.size());
|
||||
}
|
||||
|
||||
wslay_event_msg reply {};
|
||||
reply.opcode = WSLAY_BINARY_FRAME;
|
||||
reply.msg = reinterpret_cast<const uint8_t *>(out.data());
|
||||
reply.msg_length = out.size();
|
||||
|
||||
if (wslay_event_queue_msg(ctx, &reply) != 0) {
|
||||
session->failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
Controller::process_password_change(session->state);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Controller state so we don't have to import headsocket stuff in our header
|
||||
*/
|
||||
struct WebSocketControllerState {
|
||||
std::shared_ptr<WebSocketServer> server;
|
||||
struct Client {
|
||||
std::thread thread;
|
||||
SOCKET socket = INVALID_SOCKET;
|
||||
bool active = false;
|
||||
};
|
||||
|
||||
WebSocketController::WebSocketController(Controller *controller, uint16_t port) {
|
||||
this->controller = controller;
|
||||
Controller *controller = nullptr;
|
||||
unsigned short port = 0;
|
||||
SOCKET listener = INVALID_SOCKET;
|
||||
bool wsa_started = false;
|
||||
std::atomic_bool running { false };
|
||||
std::thread acceptor;
|
||||
std::mutex clients_m;
|
||||
std::array<Client, client_limit> clients;
|
||||
|
||||
// create state
|
||||
this->state = new WebSocketControllerState();
|
||||
bool open_listener();
|
||||
void accept_worker();
|
||||
void client_worker(int slot, SOCKET socket, std::string address);
|
||||
void stop();
|
||||
};
|
||||
|
||||
// start server
|
||||
this->state->server = WebSocketServer::create(port);
|
||||
this->state->server->websocket = this;
|
||||
if (this->state->server->is_running()) {
|
||||
log_info("api::websocket", "server listening on port: {}", port);
|
||||
} else {
|
||||
log_warning("api::websocket", "server failed to listen on port: {}", port);
|
||||
bool WebSocketControllerState::open_listener() {
|
||||
WSADATA wsa_data;
|
||||
const int error = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
||||
if (error != 0) {
|
||||
log_warning("api::websocket", "WSAStartup() returned {}", error);
|
||||
return false;
|
||||
}
|
||||
this->wsa_started = true;
|
||||
|
||||
this->listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (this->listener == INVALID_SOCKET) {
|
||||
log_warning("api::websocket", "socket() returned {}", WSAGetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
sockaddr_in address {};
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons(this->port);
|
||||
|
||||
if (bind(this->listener, reinterpret_cast<sockaddr *>(&address), sizeof(address))
|
||||
== SOCKET_ERROR) {
|
||||
log_warning("api::websocket", "bind() returned {}", WSAGetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (listen(this->listener, server_backlog) == SOCKET_ERROR) {
|
||||
log_warning("api::websocket", "listen() returned {}", WSAGetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebSocketControllerState::accept_worker() {
|
||||
|
||||
while (this->running) {
|
||||
sockaddr_in client_address {};
|
||||
int client_address_size = sizeof(sockaddr_in);
|
||||
|
||||
const SOCKET client = accept(
|
||||
this->listener, reinterpret_cast<sockaddr *>(&client_address),
|
||||
&client_address_size);
|
||||
|
||||
if (client == INVALID_SOCKET) {
|
||||
// on shutdown the listener is closed under us; otherwise do not spin
|
||||
if (this->running) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!this->running) {
|
||||
closesocket(client);
|
||||
break;
|
||||
}
|
||||
|
||||
// formatted by hand rather than with inet_ntop, which needs a newer Windows than
|
||||
// the XP toolchain targets, or inet_ntoa, which answers from a shared buffer
|
||||
const uint32_t raw = ntohl(client_address.sin_addr.s_addr);
|
||||
const std::string address = fmt::format("{}.{}.{}.{}",
|
||||
(raw >> 24) & 0xff, (raw >> 16) & 0xff, (raw >> 8) & 0xff, raw & 0xff);
|
||||
|
||||
int slot = -1;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->clients_m);
|
||||
for (size_t i = 0; i < this->clients.size(); i++) {
|
||||
if (!this->clients[i].active) {
|
||||
this->clients[i].active = true;
|
||||
this->clients[i].socket = client;
|
||||
slot = static_cast<int>(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WebSocketController::~WebSocketController() {
|
||||
|
||||
// stop server
|
||||
this->state->server->stop();
|
||||
|
||||
// delete state
|
||||
delete this->state;
|
||||
if (slot < 0) {
|
||||
log_warning("api::websocket", "client limit of {} hit", client_limit);
|
||||
overlay::notifications::add_throttled(
|
||||
overlay::notifications::Severity::Warning,
|
||||
"api::websocket.client_limit",
|
||||
notification_throttle_seconds,
|
||||
fmt::format("API websocket refused: client limit reached ({})", address));
|
||||
closesocket(client);
|
||||
continue;
|
||||
}
|
||||
|
||||
void WebSocketController::free_socket() {
|
||||
this->state->server->stop();
|
||||
// this thread is the only one that touches the thread objects, so the slot's
|
||||
// previous occupant gets reaped here rather than being detached
|
||||
if (this->clients[slot].thread.joinable()) {
|
||||
this->clients[slot].thread.join();
|
||||
}
|
||||
|
||||
void WebSocketClient::on_accept() {
|
||||
web_socket_client::on_accept();
|
||||
|
||||
// get pointer to server
|
||||
auto srv = reinterpret_cast<WebSocketServer *>(server().get());
|
||||
if (!srv || !srv->websocket) {
|
||||
log_fatal("api::websocket", "on_accept has no server");
|
||||
// the handshake runs on the client thread on purpose: doing it here would put
|
||||
// every later connection behind whatever this one is waiting for
|
||||
this->clients[slot].thread = std::thread([this, slot, client, address] {
|
||||
this->client_worker(slot, client, address);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// check for init
|
||||
state = new ClientState();
|
||||
srv->websocket->controller->init_state(state);
|
||||
void WebSocketControllerState::client_worker(int slot, SOCKET socket, std::string address) {
|
||||
|
||||
// wslay writes a frame header and its payload as separate sends, so leaving Nagle on
|
||||
// holds the payload back until the peer acknowledges the header, costing a delayed
|
||||
// ack per message; requests here are small and latency sensitive
|
||||
int nodelay = 1;
|
||||
setsockopt(socket, IPPROTO_TCP, TCP_NODELAY,
|
||||
reinterpret_cast<const char *>(&nodelay), sizeof(nodelay));
|
||||
|
||||
set_recv_timeout(socket, handshake_timeout_ms);
|
||||
|
||||
if (handshake(socket)) {
|
||||
// wslay reads until the socket would block, so leaving it blocking would make
|
||||
// every reply wait out a receive timeout before the send got a turn
|
||||
u_long non_blocking = 1;
|
||||
ioctlsocket(socket, FIONBIO, &non_blocking);
|
||||
|
||||
Session session;
|
||||
session.socket = socket;
|
||||
session.controller = this->controller;
|
||||
session.state = new ClientState();
|
||||
this->controller->init_state(session.state);
|
||||
|
||||
// log connection
|
||||
const auto address = this->remote_address();
|
||||
log_info("api::websocket", "client connected: {}", address);
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Success,
|
||||
fmt::format("API websocket client connected ({})", address));
|
||||
|
||||
wslay_event_callbacks callbacks {};
|
||||
callbacks.recv_callback = recv_callback;
|
||||
callbacks.send_callback = send_callback;
|
||||
callbacks.on_msg_recv_callback = on_msg_recv;
|
||||
|
||||
wslay_event_context_ptr ctx = nullptr;
|
||||
if (wslay_event_context_server_init(&ctx, &callbacks, &session) == 0) {
|
||||
wslay_event_config_set_max_recv_msg_length(ctx, message_size_limit);
|
||||
while (this->running && !session.failed
|
||||
&& (wslay_event_want_read(ctx) || wslay_event_want_write(ctx))) {
|
||||
|
||||
fd_set read_set;
|
||||
fd_set write_set;
|
||||
FD_ZERO(&read_set);
|
||||
FD_ZERO(&write_set);
|
||||
|
||||
if (wslay_event_want_read(ctx)) {
|
||||
FD_SET(socket, &read_set);
|
||||
}
|
||||
if (wslay_event_want_write(ctx)) {
|
||||
FD_SET(socket, &write_set);
|
||||
}
|
||||
|
||||
void WebSocketClient::on_disconnect() {
|
||||
// bounded so a silent connection still notices us shutting down
|
||||
timeval timeout {};
|
||||
timeout.tv_usec = idle_poll_ms * 1000;
|
||||
|
||||
if (select(0, &read_set, &write_set, nullptr, &timeout) < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (FD_ISSET(socket, &read_set) && wslay_event_recv(ctx) != 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// unconditional, so a reply queued by the read above goes out now
|
||||
if (wslay_event_send(ctx) != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wslay_event_context_free(ctx);
|
||||
}
|
||||
|
||||
Controller::free_state(session.state);
|
||||
delete session.state;
|
||||
|
||||
// log disconnection
|
||||
const auto address = this->remote_address();
|
||||
log_info("api::websocket", "client disconnected: {}", address);
|
||||
overlay::notifications::add(
|
||||
overlay::notifications::Severity::Info,
|
||||
fmt::format("API websocket client disconnected ({})", address));
|
||||
|
||||
// get pointer to server
|
||||
auto srv = reinterpret_cast<WebSocketServer *>(server().get());
|
||||
if (!srv || !srv->websocket) {
|
||||
log_fatal("api::websocket", "on_disconnect has no server");
|
||||
}
|
||||
|
||||
// clean up state
|
||||
srv->websocket->controller->free_state(state);
|
||||
delete state;
|
||||
state = nullptr;
|
||||
closesocket(socket);
|
||||
|
||||
// call super
|
||||
web_socket_client::on_disconnect();
|
||||
std::lock_guard<std::mutex> lock(this->clients_m);
|
||||
this->clients[slot].socket = INVALID_SOCKET;
|
||||
this->clients[slot].active = false;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is where business actually happens, gets called on every datablock receive
|
||||
*/
|
||||
bool WebSocketClient::async_received_data(const data_block &db, uint8_t *ptr, size_t length) {
|
||||
void WebSocketControllerState::stop() {
|
||||
this->running = false;
|
||||
|
||||
// get pointer to server
|
||||
auto srv = reinterpret_cast<WebSocketServer *>(server().get());
|
||||
if (!srv || !srv->websocket) {
|
||||
log_fatal("api::websocket", "received datablock without server");
|
||||
if (this->listener != INVALID_SOCKET) {
|
||||
closesocket(this->listener);
|
||||
this->listener = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
// check state
|
||||
if (!state) {
|
||||
log_fatal("api::websocket", "client with no state received datablock");
|
||||
// drops the client threads out of their blocking send/recv
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->clients_m);
|
||||
for (auto &client : this->clients) {
|
||||
if (client.socket != INVALID_SOCKET) {
|
||||
::shutdown(client.socket, SD_BOTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check datablock type
|
||||
switch (db.op) {
|
||||
case opcode::binary: {
|
||||
|
||||
// allocate buffers
|
||||
std::vector<char> in(ptr, ptr + length);
|
||||
std::vector<char> out;
|
||||
|
||||
// crypt in-data
|
||||
if (state->cipher) {
|
||||
state->cipher->crypt(reinterpret_cast<uint8_t *>(in.data()), in.size());
|
||||
if (this->acceptor.joinable()) {
|
||||
this->acceptor.join();
|
||||
}
|
||||
|
||||
// process request
|
||||
srv->websocket->controller->process_request(state, &in, &out);
|
||||
|
||||
// crypt out-data
|
||||
if (state->cipher) {
|
||||
state->cipher->crypt(reinterpret_cast<uint8_t *>(out.data()), out.size());
|
||||
// joining is what guarantees no client thread outlives this object
|
||||
for (auto &client : this->clients) {
|
||||
if (client.thread.joinable()) {
|
||||
client.thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
// send answer
|
||||
push(out.data(), out.size());
|
||||
|
||||
// check for password change
|
||||
srv->websocket->controller->process_password_change(state);
|
||||
|
||||
break;
|
||||
if (this->wsa_started) {
|
||||
WSACleanup();
|
||||
this->wsa_started = false;
|
||||
}
|
||||
default:
|
||||
log_warning("api::websocket", "datablock received with non-binary type");
|
||||
break;
|
||||
}
|
||||
|
||||
// always consume the datablock, nomnom
|
||||
return true;
|
||||
WebSocketController::WebSocketController(Controller *controller, uint16_t port) {
|
||||
this->controller = controller;
|
||||
|
||||
this->state = new WebSocketControllerState();
|
||||
this->state->controller = controller;
|
||||
this->state->port = port;
|
||||
|
||||
if (!this->state->open_listener()) {
|
||||
log_warning("api::websocket", "server failed to listen on port: {}", port);
|
||||
return;
|
||||
}
|
||||
|
||||
this->state->running = true;
|
||||
this->state->acceptor = std::thread([this] {
|
||||
this->state->accept_worker();
|
||||
});
|
||||
|
||||
log_info("api::websocket", "server listening on port: {}", port);
|
||||
}
|
||||
|
||||
WebSocketController::~WebSocketController() {
|
||||
this->state->stop();
|
||||
delete this->state;
|
||||
this->state = nullptr;
|
||||
}
|
||||
|
||||
void WebSocketController::free_socket() {
|
||||
this->state->stop();
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
-2195
File diff suppressed because it is too large
Load Diff
+33
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
project(wslay C)
|
||||
|
||||
set(WSLAY_HEADERS
|
||||
msvc_compat.h
|
||||
wslay_event.h
|
||||
wslay_frame.h
|
||||
wslay_macro.h
|
||||
wslay_net.h
|
||||
wslay_queue.h
|
||||
includes/wslay/wslay.h
|
||||
includes/wslay/wslayver.h
|
||||
)
|
||||
set(WSLAY_SOURCES
|
||||
wslay_event.c
|
||||
wslay_frame.c
|
||||
wslay_net.c
|
||||
wslay_queue.c
|
||||
)
|
||||
|
||||
add_library(wslay STATIC ${WSLAY_HEADERS} ${WSLAY_SOURCES})
|
||||
|
||||
# config.h here is checked in rather than generated, see the note in it
|
||||
target_compile_definitions(wslay PRIVATE HAVE_CONFIG_H)
|
||||
target_include_directories(wslay PRIVATE ${PROJECT_SOURCE_DIR})
|
||||
target_include_directories(wslay PUBLIC ${PROJECT_SOURCE_DIR}/includes)
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(wslay PRIVATE "/FI${PROJECT_SOURCE_DIR}/msvc_compat.h")
|
||||
else()
|
||||
# upstream code, and not ours to keep warning clean
|
||||
target_compile_options(wslay PRIVATE -w)
|
||||
endif()
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2011, 2012, 2015 Tatsuhiro Tsujikawa
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/* Hand written stand-in for the autotools/CMake generated config.h. Spice only
|
||||
builds this for Windows on x86, so the probes have single known answers. */
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#define HAVE_WINSOCK2_H
|
||||
/* x86 and x86_64 are little endian, so WORDS_BIGENDIAN stays undefined */
|
||||
|
||||
#endif /* CONFIG_H */
|
||||
+841
@@ -0,0 +1,841 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAY_H
|
||||
#define WSLAY_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/*
|
||||
* wslay/wslayver.h is generated from wslay/wslayver.h.in by
|
||||
* configure. The projects which do not use autotools can set
|
||||
* WSLAY_VERSION macro from outside to avoid to generating wslayver.h
|
||||
*/
|
||||
#ifndef WSLAY_VERSION
|
||||
# include <wslay/wslayver.h>
|
||||
#endif /* WSLAY_VERSION */
|
||||
|
||||
enum wslay_error {
|
||||
WSLAY_ERR_WANT_READ = -100,
|
||||
WSLAY_ERR_WANT_WRITE = -101,
|
||||
WSLAY_ERR_PROTO = -200,
|
||||
WSLAY_ERR_INVALID_ARGUMENT = -300,
|
||||
WSLAY_ERR_INVALID_CALLBACK = -301,
|
||||
WSLAY_ERR_NO_MORE_MSG = -302,
|
||||
WSLAY_ERR_CALLBACK_FAILURE = -400,
|
||||
WSLAY_ERR_WOULDBLOCK = -401,
|
||||
WSLAY_ERR_NOMEM = -500
|
||||
};
|
||||
|
||||
/*
|
||||
* Status codes defined in RFC6455
|
||||
*/
|
||||
enum wslay_status_code {
|
||||
WSLAY_CODE_NORMAL_CLOSURE = 1000,
|
||||
WSLAY_CODE_GOING_AWAY = 1001,
|
||||
WSLAY_CODE_PROTOCOL_ERROR = 1002,
|
||||
WSLAY_CODE_UNSUPPORTED_DATA = 1003,
|
||||
WSLAY_CODE_NO_STATUS_RCVD = 1005,
|
||||
WSLAY_CODE_ABNORMAL_CLOSURE = 1006,
|
||||
WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA = 1007,
|
||||
WSLAY_CODE_POLICY_VIOLATION = 1008,
|
||||
WSLAY_CODE_MESSAGE_TOO_BIG = 1009,
|
||||
WSLAY_CODE_MANDATORY_EXT = 1010,
|
||||
WSLAY_CODE_INTERNAL_SERVER_ERROR = 1011,
|
||||
WSLAY_CODE_TLS_HANDSHAKE = 1015
|
||||
};
|
||||
|
||||
enum wslay_io_flags {
|
||||
/*
|
||||
* There is more data to send.
|
||||
*/
|
||||
WSLAY_MSG_MORE = 1
|
||||
};
|
||||
|
||||
/*
|
||||
* Callback function used by wslay_frame_send() function when it needs
|
||||
* to send data. The implementation of this function must send at most
|
||||
* len bytes of data in data. flags is the bitwise OR of zero or more
|
||||
* of the following flag:
|
||||
*
|
||||
* WSLAY_MSG_MORE
|
||||
* There is more data to send
|
||||
*
|
||||
* It provides some hints to tune performance and behaviour. user_data
|
||||
* is one given in wslay_frame_context_init() function. The
|
||||
* implementation of this function must return the number of bytes
|
||||
* sent. If there is an error, return -1. The return value 0 is also
|
||||
* treated an error by the library.
|
||||
*/
|
||||
typedef ssize_t (*wslay_frame_send_callback)(const uint8_t *data, size_t len,
|
||||
int flags, void *user_data);
|
||||
/*
|
||||
* Callback function used by wslay_frame_recv() function when it needs
|
||||
* more data. The implementation of this function must fill at most
|
||||
* len bytes of data into buf. The memory area of buf is allocated by
|
||||
* library and not be freed by the application code. flags is always 0
|
||||
* in this version. user_data is one given in
|
||||
* wslay_frame_context_init() function. The implementation of this
|
||||
* function must return the number of bytes filled. If there is an
|
||||
* error, return -1. The return value 0 is also treated an error by
|
||||
* the library.
|
||||
*/
|
||||
typedef ssize_t (*wslay_frame_recv_callback)(uint8_t *buf, size_t len,
|
||||
int flags, void *user_data);
|
||||
/*
|
||||
* Callback function used by wslay_frame_send() function when it needs
|
||||
* new mask key. The implementation of this function must write
|
||||
* exactly len bytes of mask key to buf. user_data is one given in
|
||||
* wslay_frame_context_init() function. The implementation of this
|
||||
* function return 0 on success. If there is an error, return -1.
|
||||
*/
|
||||
typedef int (*wslay_frame_genmask_callback)(uint8_t *buf, size_t len,
|
||||
void *user_data);
|
||||
|
||||
struct wslay_frame_callbacks {
|
||||
wslay_frame_send_callback send_callback;
|
||||
wslay_frame_recv_callback recv_callback;
|
||||
wslay_frame_genmask_callback genmask_callback;
|
||||
};
|
||||
|
||||
/*
|
||||
* The opcode defined in RFC6455.
|
||||
*/
|
||||
enum wslay_opcode {
|
||||
WSLAY_CONTINUATION_FRAME = 0x0u,
|
||||
WSLAY_TEXT_FRAME = 0x1u,
|
||||
WSLAY_BINARY_FRAME = 0x2u,
|
||||
WSLAY_CONNECTION_CLOSE = 0x8u,
|
||||
WSLAY_PING = 0x9u,
|
||||
WSLAY_PONG = 0xau
|
||||
};
|
||||
|
||||
/*
|
||||
* Macro that returns 1 if opcode is control frame opcode, otherwise
|
||||
* returns 0.
|
||||
*/
|
||||
#define wslay_is_ctrl_frame(opcode) ((opcode >> 3) & 1)
|
||||
|
||||
/*
|
||||
* Macros that represent and return reserved bits: RSV1, RSV2, RSV3.
|
||||
* These macros assume that rsv is constructed by ((RSV1 << 2) |
|
||||
* (RSV2 << 1) | RSV3)
|
||||
*/
|
||||
#define WSLAY_RSV_NONE ((uint8_t)0)
|
||||
#define WSLAY_RSV1_BIT (((uint8_t)1) << 2)
|
||||
#define WSLAY_RSV2_BIT (((uint8_t)1) << 1)
|
||||
#define WSLAY_RSV3_BIT (((uint8_t)1) << 0)
|
||||
|
||||
#define wslay_get_rsv1(rsv) ((rsv >> 2) & 1)
|
||||
#define wslay_get_rsv2(rsv) ((rsv >> 1) & 1)
|
||||
#define wslay_get_rsv3(rsv) (rsv & 1)
|
||||
|
||||
struct wslay_frame_iocb {
|
||||
/* 1 for fragmented final frame, 0 for otherwise */
|
||||
uint8_t fin;
|
||||
/*
|
||||
* reserved 3 bits. rsv = ((RSV1 << 2) | (RSV << 1) | RSV3).
|
||||
* RFC6455 requires 0 unless extensions are negotiated.
|
||||
*/
|
||||
uint8_t rsv;
|
||||
/* 4 bit opcode */
|
||||
uint8_t opcode;
|
||||
/* payload length [0, 2**63-1] */
|
||||
uint64_t payload_length;
|
||||
/* 1 for masked frame, 0 for unmasked */
|
||||
uint8_t mask;
|
||||
/* part of payload data */
|
||||
const uint8_t *data;
|
||||
/* bytes of data defined above */
|
||||
size_t data_length;
|
||||
};
|
||||
|
||||
struct wslay_frame_context;
|
||||
typedef struct wslay_frame_context *wslay_frame_context_ptr;
|
||||
|
||||
/*
|
||||
* Initializes ctx using given callbacks and user_data. This function
|
||||
* allocates memory for struct wslay_frame_context and stores the
|
||||
* result to *ctx. The callback functions specified in callbacks are
|
||||
* copied to ctx. user_data is stored in ctx and it will be passed to
|
||||
* callback functions. When the user code finished using ctx, it must
|
||||
* call wslay_frame_context_free to deallocate memory.
|
||||
*/
|
||||
int wslay_frame_context_init(wslay_frame_context_ptr *ctx,
|
||||
const struct wslay_frame_callbacks *callbacks,
|
||||
void *user_data);
|
||||
|
||||
/*
|
||||
* Deallocates memory pointed by ctx.
|
||||
*/
|
||||
void wslay_frame_context_free(wslay_frame_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Send WebSocket frame specified in iocb. ctx must be initialized
|
||||
* using wslay_frame_context_init() function. iocb->fin must be 1 if
|
||||
* this is a fin frame, otherwise 0. iocb->rsv is reserved bits.
|
||||
* iocb->opcode must be the opcode of this frame. iocb->mask must be
|
||||
* 1 if this is masked frame, otherwise 0. iocb->payload_length is
|
||||
* the payload_length of this frame. iocb->data must point to the
|
||||
* payload data to be sent. iocb->data_length must be the length of
|
||||
* the data. This function calls send_callback function if it needs
|
||||
* to send bytes. This function calls gen_mask_callback function if
|
||||
* it needs new mask key. This function returns the number of payload
|
||||
* bytes sent. Please note that it does not include any number of
|
||||
* header bytes. If it cannot send any single bytes of payload, it
|
||||
* returns WSLAY_ERR_WANT_WRITE. If the library detects error in iocb,
|
||||
* this function returns WSLAY_ERR_INVALID_ARGUMENT. If callback
|
||||
* functions report a failure, this function returns
|
||||
* WSLAY_ERR_INVALID_CALLBACK. This function does not always send all
|
||||
* given data in iocb. If there are remaining data to be sent, adjust
|
||||
* data and data_length in iocb accordingly and call this function
|
||||
* again.
|
||||
*/
|
||||
ssize_t wslay_frame_send(wslay_frame_context_ptr ctx,
|
||||
struct wslay_frame_iocb *iocb);
|
||||
|
||||
/*
|
||||
* Write WebSocket frame specified in iocb to buf of length
|
||||
* buflen. ctx must be initialized using wslay_frame_context_init()
|
||||
* function. iocb->fin must be 1 if this is a fin frame, otherwise 0.
|
||||
* iocb->rsv is reserved bits. iocb->opcode must be the opcode of
|
||||
* this frame. iocb->mask must be 1 if this is masked frame,
|
||||
* otherwise 0. iocb->payload_length is the payload_length of this
|
||||
* frame. iocb->data must point to the payload data to be
|
||||
* sent. iocb->data_length must be the length of the data. Unlike
|
||||
* wslay_frame_send, this function does not call send_callback
|
||||
* function. This function calls gen_mask_callback function if it
|
||||
* needs new mask key. This function returns the number of bytes
|
||||
* written to a buffer. Unlike wslay_frame_send, it includes the
|
||||
* number of header bytes. Instead, the number of payload bytes
|
||||
* written is assigned to *pwpayloadlen if this function succeeds. If
|
||||
* there is not enough space left in a buffer, it returns 0. If the
|
||||
* library detects error in iocb, this function returns
|
||||
* WSLAY_ERR_INVALID_ARGUMENT. If callback functions report a
|
||||
* failure, this function returns WSLAY_ERR_INVALID_CALLBACK. This
|
||||
* function does not always send all given data in iocb. If there are
|
||||
* remaining data to be sent, adjust data and data_length in iocb
|
||||
* accordingly and call this function again.
|
||||
*/
|
||||
ssize_t wslay_frame_write(wslay_frame_context_ptr ctx,
|
||||
struct wslay_frame_iocb *iocb, uint8_t *buf,
|
||||
size_t buflen, size_t *pwpayloadlen);
|
||||
|
||||
/*
|
||||
* Receives WebSocket frame and stores it in iocb. This function
|
||||
* returns the number of payload bytes received. This does not
|
||||
* include header bytes. In this case, iocb will be populated as
|
||||
* follows: iocb->fin is 1 if received frame is fin frame, otherwise
|
||||
* 0. iocb->rsv is reserved bits of received frame. iocb->opcode is
|
||||
* opcode of received frame. iocb->mask is 1 if received frame is
|
||||
* masked, otherwise 0. iocb->payload_length is the payload length of
|
||||
* received frame. iocb->data is pointed to the buffer containing
|
||||
* received payload data. This buffer is allocated by the library and
|
||||
* must be read-only. iocb->data_length is the number of payload
|
||||
* bytes recieved. This function calls recv_callback if it needs to
|
||||
* receive additional bytes. If it cannot receive any single bytes of
|
||||
* payload, it returns WSLAY_ERR_WANT_READ. If the library detects
|
||||
* protocol violation in a received frame, this function returns
|
||||
* WSLAY_ERR_PROTO. If callback functions report a failure, this
|
||||
* function returns WSLAY_ERR_INVALID_CALLBACK. This function does
|
||||
* not always receive whole frame in a single call. If there are
|
||||
* remaining data to be received, call this function again. This
|
||||
* function ensures frame alignment.
|
||||
*/
|
||||
ssize_t wslay_frame_recv(wslay_frame_context_ptr ctx,
|
||||
struct wslay_frame_iocb *iocb);
|
||||
|
||||
struct wslay_event_context;
|
||||
/* Pointer to the event-based API context */
|
||||
typedef struct wslay_event_context *wslay_event_context_ptr;
|
||||
|
||||
struct wslay_event_on_msg_recv_arg {
|
||||
/* reserved bits: rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3 */
|
||||
uint8_t rsv;
|
||||
/* opcode */
|
||||
uint8_t opcode;
|
||||
/* received message */
|
||||
const uint8_t *msg;
|
||||
/* message length */
|
||||
size_t msg_length;
|
||||
/*
|
||||
* Status code iff opcode == WSLAY_CONNECTION_CLOSE. If no status
|
||||
* code is included in the close control frame, it is set to 0.
|
||||
*/
|
||||
uint16_t status_code;
|
||||
};
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_recv() when a message is
|
||||
* completely received.
|
||||
*/
|
||||
typedef void (*wslay_event_on_msg_recv_callback)(
|
||||
wslay_event_context_ptr ctx, const struct wslay_event_on_msg_recv_arg *arg,
|
||||
void *user_data);
|
||||
|
||||
struct wslay_event_on_frame_recv_start_arg {
|
||||
/* fin bit; 1 for final frame, or 0. */
|
||||
uint8_t fin;
|
||||
/* reserved bits: rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3 */
|
||||
uint8_t rsv;
|
||||
/* opcode of the frame */
|
||||
uint8_t opcode;
|
||||
/* payload length of ths frame */
|
||||
uint64_t payload_length;
|
||||
};
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_recv() when a new frame
|
||||
* starts to be received. This callback function is only invoked once
|
||||
* for each frame.
|
||||
*/
|
||||
typedef void (*wslay_event_on_frame_recv_start_callback)(
|
||||
wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data);
|
||||
|
||||
struct wslay_event_on_frame_recv_chunk_arg {
|
||||
/* chunk of payload data */
|
||||
const uint8_t *data;
|
||||
/* length of data */
|
||||
size_t data_length;
|
||||
};
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_recv() when a chunk of
|
||||
* frame payload is received.
|
||||
*/
|
||||
typedef void (*wslay_event_on_frame_recv_chunk_callback)(
|
||||
wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_on_frame_recv_chunk_arg *arg, void *user_data);
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_recv() when a frame is
|
||||
* completely received.
|
||||
*/
|
||||
typedef void (*wslay_event_on_frame_recv_end_callback)(
|
||||
wslay_event_context_ptr ctx, void *user_data);
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_recv() when it wants to
|
||||
* receive more data from peer. The implementation of this callback
|
||||
* function must read data at most len bytes from peer and store them
|
||||
* in buf and return the number of bytes read. flags is always 0 in
|
||||
* this version.
|
||||
*
|
||||
* If there is an error, return -1 and set error code
|
||||
* WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay
|
||||
* event-based API on the whole assumes non-blocking I/O. If the cause
|
||||
* of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK
|
||||
* instead. This is important because it tells wslay_event_recv() to
|
||||
* stop receiving further data and return.
|
||||
*/
|
||||
typedef ssize_t (*wslay_event_recv_callback)(wslay_event_context_ptr ctx,
|
||||
uint8_t *buf, size_t len,
|
||||
int flags, void *user_data);
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_send() when it wants to
|
||||
* send more data to peer. The implementation of this callback
|
||||
* function must send data at most len bytes to peer and return the
|
||||
* number of bytes sent. flags is the bitwise OR of zero or more of
|
||||
* the following flag:
|
||||
*
|
||||
* WSLAY_MSG_MORE
|
||||
* There is more data to send
|
||||
*
|
||||
* It provides some hints to tune performance and behaviour.
|
||||
*
|
||||
* If there is an error, return -1 and set error code
|
||||
* WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay
|
||||
* event-based API on the whole assumes non-blocking I/O. If the cause
|
||||
* of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK
|
||||
* instead. This is important because it tells wslay_event_send() to
|
||||
* stop sending data and return.
|
||||
*/
|
||||
typedef ssize_t (*wslay_event_send_callback)(wslay_event_context_ptr ctx,
|
||||
const uint8_t *data, size_t len,
|
||||
int flags, void *user_data);
|
||||
|
||||
/*
|
||||
* Callback function invoked by wslay_event_send() when it wants new
|
||||
* mask key. As described in RFC6455, only the traffic from WebSocket
|
||||
* client is masked, so this callback function is only needed if an
|
||||
* event-based API is initialized for WebSocket client use.
|
||||
*/
|
||||
typedef int (*wslay_event_genmask_callback)(wslay_event_context_ptr ctx,
|
||||
uint8_t *buf, size_t len,
|
||||
void *user_data);
|
||||
|
||||
struct wslay_event_callbacks {
|
||||
wslay_event_recv_callback recv_callback;
|
||||
wslay_event_send_callback send_callback;
|
||||
wslay_event_genmask_callback genmask_callback;
|
||||
wslay_event_on_frame_recv_start_callback on_frame_recv_start_callback;
|
||||
wslay_event_on_frame_recv_chunk_callback on_frame_recv_chunk_callback;
|
||||
wslay_event_on_frame_recv_end_callback on_frame_recv_end_callback;
|
||||
wslay_event_on_msg_recv_callback on_msg_recv_callback;
|
||||
};
|
||||
|
||||
/*
|
||||
* Initializes ctx as WebSocket Server. user_data is an arbitrary
|
||||
* pointer, which is directly passed to each callback functions as
|
||||
* user_data argument.
|
||||
*
|
||||
* On success, returns 0. On error, returns one of following negative
|
||||
* values:
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*/
|
||||
int wslay_event_context_server_init(
|
||||
wslay_event_context_ptr *ctx, const struct wslay_event_callbacks *callbacks,
|
||||
void *user_data);
|
||||
|
||||
/*
|
||||
* Initializes ctx as WebSocket client. user_data is an arbitrary
|
||||
* pointer, which is directly passed to each callback functions as
|
||||
* user_data argument.
|
||||
*
|
||||
* On success, returns 0. On error, returns one of following negative
|
||||
* values:
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*/
|
||||
int wslay_event_context_client_init(
|
||||
wslay_event_context_ptr *ctx, const struct wslay_event_callbacks *callbacks,
|
||||
void *user_data);
|
||||
|
||||
/*
|
||||
* Releases allocated resources for ctx.
|
||||
*/
|
||||
void wslay_event_context_free(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Sets a bit mask of allowed reserved bits.
|
||||
* Currently only permitted values are WSLAY_RSV1_BIT to allow PMCE
|
||||
* extension (see RFC-7692) or WSLAY_RSV_NONE to disable.
|
||||
*
|
||||
* Default: WSLAY_RSV_NONE
|
||||
*/
|
||||
void wslay_event_config_set_allowed_rsv_bits(wslay_event_context_ptr ctx,
|
||||
uint8_t rsv);
|
||||
|
||||
/*
|
||||
* Enables or disables buffering of an entire message for non-control
|
||||
* frames. If val is 0, buffering is enabled. Otherwise, buffering is
|
||||
* disabled. If wslay_event_on_msg_recv_callback is invoked when
|
||||
* buffering is disabled, the msg_length member of struct
|
||||
* wslay_event_on_msg_recv_arg is set to 0.
|
||||
*
|
||||
* The control frames are always buffered regardless of this function call.
|
||||
*
|
||||
* This function must not be used after the first invocation of
|
||||
* wslay_event_recv() function.
|
||||
*/
|
||||
void wslay_event_config_set_no_buffering(wslay_event_context_ptr ctx, int val);
|
||||
|
||||
/*
|
||||
* Sets maximum length of a message that can be received. The length
|
||||
* of message is checked by wslay_event_recv() function. If the length
|
||||
* of a message is larger than this value, reading operation is
|
||||
* disabled (same effect with wslay_event_shutdown_read() call) and
|
||||
* close control frame with WSLAY_CODE_MESSAGE_TOO_BIG is queued. If
|
||||
* buffering for non-control frames is disabled, the library checks
|
||||
* each frame payload length and does not check length of entire
|
||||
* message.
|
||||
*
|
||||
* The default value is (1u << 31)-1.
|
||||
*/
|
||||
void wslay_event_config_set_max_recv_msg_length(wslay_event_context_ptr ctx,
|
||||
uint64_t val);
|
||||
|
||||
/*
|
||||
* Sets callbacks to ctx. The callbacks previously set by this function
|
||||
* or wslay_event_context_server_init() or
|
||||
* wslay_event_context_client_init() are replaced with callbacks.
|
||||
*/
|
||||
void wslay_event_config_set_callbacks(
|
||||
wslay_event_context_ptr ctx, const struct wslay_event_callbacks *callbacks);
|
||||
|
||||
/*
|
||||
* Receives messages from peer. When receiving
|
||||
* messages, it uses wslay_event_recv_callback function. Single call
|
||||
* of this function receives multiple messages until
|
||||
* wslay_event_recv_callback function sets error code
|
||||
* WSLAY_ERR_WOULDBLOCK.
|
||||
*
|
||||
* When close control frame is received, this function automatically
|
||||
* queues close control frame. Also this function calls
|
||||
* wslay_event_set_read_enabled() with second argument 0 to disable
|
||||
* further read from peer.
|
||||
*
|
||||
* When ping control frame is received, this function automatically
|
||||
* queues pong control frame.
|
||||
*
|
||||
* In case of a fatal errror which leads to negative return code, this
|
||||
* function calls wslay_event_set_read_enabled() with second argument
|
||||
* 0 to disable further read from peer.
|
||||
*
|
||||
* wslay_event_recv() returns 0 if it succeeds, or one of the
|
||||
* following negative error codes:
|
||||
*
|
||||
* WSLAY_ERR_CALLBACK_FAILURE
|
||||
* User defined callback function is failed.
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*
|
||||
* When negative error code is returned, application must not make any
|
||||
* further call of wslay_event_recv() and must close WebSocket
|
||||
* connection.
|
||||
*/
|
||||
int wslay_event_recv(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Sends queued messages to peer. When sending a
|
||||
* message, it uses wslay_event_send_callback function. Single call of
|
||||
* wslay_event_send() sends multiple messages until
|
||||
* wslay_event_send_callback sets error code WSLAY_ERR_WOULDBLOCK.
|
||||
*
|
||||
* If ctx is initialized for WebSocket client use, wslay_event_send()
|
||||
* uses wslay_event_genmask_callback to get new mask key.
|
||||
*
|
||||
* When a message queued using wslay_event_queue_fragmented_msg() is
|
||||
* sent, wslay_event_send() invokes
|
||||
* wslay_event_fragmented_msg_callback for that message.
|
||||
*
|
||||
* After close control frame is sent, this function calls
|
||||
* wslay_event_set_write_enabled() with second argument 0 to disable
|
||||
* further transmission to peer.
|
||||
*
|
||||
* If there are any pending messages, wslay_event_want_write() returns
|
||||
* 1, otherwise returns 0.
|
||||
*
|
||||
* In case of a fatal errror which leads to negative return code, this
|
||||
* function calls wslay_event_set_write_enabled() with second argument
|
||||
* 0 to disable further transmission to peer.
|
||||
*
|
||||
* wslay_event_send() returns 0 if it succeeds, or one of the
|
||||
* following negative error codes:
|
||||
*
|
||||
* WSLAY_ERR_CALLBACK_FAILURE
|
||||
* User defined callback function is failed.
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*
|
||||
* When negative error code is returned, application must not make any
|
||||
* further call of wslay_event_send() and must close WebSocket
|
||||
* connection.
|
||||
*/
|
||||
int wslay_event_send(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Writes queued messages to a buffer. Unlike wslay_event_send(), this
|
||||
* function writes messages into the given buffer. It does not use
|
||||
* wslay_event_send_callback function. Single call of
|
||||
* wslay_event_write() writes multiple messages until there is not
|
||||
* enough space left in a buffer.
|
||||
*
|
||||
* If ctx is initialized for WebSocket client use, wslay_event_write()
|
||||
* uses wslay_event_genmask_callback to get new mask key.
|
||||
*
|
||||
* buf is a pointer to buffer and its capacity is given in buflen. It
|
||||
* should have at least 14 bytes.
|
||||
*
|
||||
* When a message queued using wslay_event_queue_fragmented_msg() is
|
||||
* sent, wslay_event_write() invokes
|
||||
* wslay_event_fragmented_msg_callback for that message.
|
||||
*
|
||||
* After close control frame is sent, this function calls
|
||||
* wslay_event_set_write_enabled() with second argument 0 to disable
|
||||
* further transmission to peer.
|
||||
*
|
||||
* If there are any pending messages, wslay_event_want_write() returns
|
||||
* 1, otherwise returns 0.
|
||||
*
|
||||
* In case of a fatal errror which leads to negative return code, this
|
||||
* function calls wslay_event_set_write_enabled() with second argument
|
||||
* 0 to disable further transmission to peer.
|
||||
*
|
||||
* wslay_event_write() returns the number of bytes written to a buffer
|
||||
* if it succeeds, or one of the following negative error codes:
|
||||
*
|
||||
* WSLAY_ERR_CALLBACK_FAILURE
|
||||
* User defined callback function is failed.
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*
|
||||
* When negative error code is returned, application must not make any
|
||||
* further call of wslay_event_write() and must close WebSocket
|
||||
* connection.
|
||||
*/
|
||||
ssize_t wslay_event_write(wslay_event_context_ptr ctx, uint8_t *buf,
|
||||
size_t buflen);
|
||||
|
||||
struct wslay_event_msg {
|
||||
uint8_t opcode;
|
||||
const uint8_t *msg;
|
||||
size_t msg_length;
|
||||
};
|
||||
|
||||
/*
|
||||
* Queues message specified in arg.
|
||||
*
|
||||
* This function supports both control and non-control messages and
|
||||
* the given message is sent without fragmentation. If fragmentation
|
||||
* is needed, use wslay_event_queue_fragmented_msg() function instead.
|
||||
*
|
||||
* This function just queues a message and does not send
|
||||
* it. wslay_event_send() function call sends these queued messages.
|
||||
*
|
||||
* wslay_event_queue_msg() returns 0 if it succeeds, or returns the
|
||||
* following negative error codes:
|
||||
*
|
||||
* WSLAY_ERR_NO_MORE_MSG
|
||||
* Could not queue given message. The one of possible reason is that
|
||||
* close control frame has been queued/sent and no further queueing
|
||||
* message is not allowed.
|
||||
*
|
||||
* WSLAY_ERR_INVALID_ARGUMENT
|
||||
* The given message is invalid.
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*/
|
||||
int wslay_event_queue_msg(wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_msg *arg);
|
||||
|
||||
/*
|
||||
* Extended version of wslay_event_queue_msg which allows to set reserved bits.
|
||||
*/
|
||||
int wslay_event_queue_msg_ex(wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_msg *arg, uint8_t rsv);
|
||||
|
||||
/*
|
||||
* Specify "source" to generate message.
|
||||
*/
|
||||
union wslay_event_msg_source {
|
||||
int fd;
|
||||
void *data;
|
||||
};
|
||||
|
||||
/*
|
||||
* Callback function called by wslay_event_send() to read message data
|
||||
* from source. The implementation of
|
||||
* wslay_event_fragmented_msg_callback must store at most len bytes of
|
||||
* data to buf and return the number of stored bytes. If all data is
|
||||
* read (i.e., EOF), set *eof to 1. If no data can be generated at the
|
||||
* moment, return 0. If there is an error, return -1 and set error
|
||||
* code WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error().
|
||||
*/
|
||||
typedef ssize_t (*wslay_event_fragmented_msg_callback)(
|
||||
wslay_event_context_ptr ctx, uint8_t *buf, size_t len,
|
||||
const union wslay_event_msg_source *source, int *eof, void *user_data);
|
||||
|
||||
struct wslay_event_fragmented_msg {
|
||||
/* opcode */
|
||||
uint8_t opcode;
|
||||
/* "source" to generate message data */
|
||||
union wslay_event_msg_source source;
|
||||
/* Callback function to read message data from source. */
|
||||
wslay_event_fragmented_msg_callback read_callback;
|
||||
};
|
||||
|
||||
/*
|
||||
* Queues a fragmented message specified in arg.
|
||||
*
|
||||
* This function supports non-control messages only. For control frames,
|
||||
* use wslay_event_queue_msg() or wslay_event_queue_close().
|
||||
*
|
||||
* This function just queues a message and does not send
|
||||
* it. wslay_event_send() function call sends these queued messages.
|
||||
*
|
||||
* wslay_event_queue_fragmented_msg() returns 0 if it succeeds, or
|
||||
* returns the following negative error codes:
|
||||
*
|
||||
* WSLAY_ERR_NO_MORE_MSG
|
||||
* Could not queue given message. The one of possible reason is that
|
||||
* close control frame has been queued/sent and no further queueing
|
||||
* message is not allowed.
|
||||
*
|
||||
* WSLAY_ERR_INVALID_ARGUMENT
|
||||
* The given message is invalid.
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*/
|
||||
int wslay_event_queue_fragmented_msg(
|
||||
wslay_event_context_ptr ctx, const struct wslay_event_fragmented_msg *arg);
|
||||
|
||||
/*
|
||||
* Extended version of wslay_event_queue_fragmented_msg which allows to set
|
||||
* reserved bits.
|
||||
*/
|
||||
int wslay_event_queue_fragmented_msg_ex(
|
||||
wslay_event_context_ptr ctx, const struct wslay_event_fragmented_msg *arg,
|
||||
uint8_t rsv);
|
||||
|
||||
/*
|
||||
* Queues close control frame. This function is provided just for
|
||||
* convenience. wslay_event_queue_msg() can queue a close control
|
||||
* frame as well. status_code is the status code of close control
|
||||
* frame. reason is the close reason encoded in UTF-8. reason_length
|
||||
* is the length of reason in bytes. reason_length must be less than
|
||||
* 123 bytes.
|
||||
*
|
||||
* If status_code is 0, reason and reason_length is not used and close
|
||||
* control frame with zero-length payload will be queued.
|
||||
*
|
||||
* This function just queues a message and does not send
|
||||
* it. wslay_event_send() function call sends these queued messages.
|
||||
*
|
||||
* wslay_event_queue_close() returns 0 if it succeeds, or returns the
|
||||
* following negative error codes:
|
||||
*
|
||||
* WSLAY_ERR_NO_MORE_MSG
|
||||
* Could not queue given message. The one of possible reason is that
|
||||
* close control frame has been queued/sent and no further queueing
|
||||
* message is not allowed.
|
||||
*
|
||||
* WSLAY_ERR_INVALID_ARGUMENT
|
||||
* The given message is invalid.
|
||||
*
|
||||
* WSLAY_ERR_NOMEM
|
||||
* Out of memory.
|
||||
*/
|
||||
int wslay_event_queue_close(wslay_event_context_ptr ctx, uint16_t status_code,
|
||||
const uint8_t *reason, size_t reason_length);
|
||||
|
||||
/*
|
||||
* Sets error code to tell the library there is an error. This
|
||||
* function is typically used in user defined callback functions. See
|
||||
* the description of callback function to know which error code
|
||||
* should be used.
|
||||
*/
|
||||
void wslay_event_set_error(wslay_event_context_ptr ctx, int val);
|
||||
|
||||
/*
|
||||
* Query whehter the library want to read more data from peer.
|
||||
*
|
||||
* wslay_event_want_read() returns 1 if the library want to read more
|
||||
* data from peer, or returns 0.
|
||||
*/
|
||||
int wslay_event_want_read(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Query whehter the library want to send more data to peer.
|
||||
*
|
||||
* wslay_event_want_write() returns 1 if the library want to send more
|
||||
* data to peer, or returns 0.
|
||||
*/
|
||||
int wslay_event_want_write(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Prevents the event-based API context from reading any further data
|
||||
* from peer.
|
||||
*
|
||||
* This function may be used with wslay_event_queue_close() if the
|
||||
* application detects error in the data received and wants to fail
|
||||
* WebSocket connection.
|
||||
*/
|
||||
void wslay_event_shutdown_read(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Prevents the event-based API context from sending any further data
|
||||
* to peer.
|
||||
*/
|
||||
void wslay_event_shutdown_write(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns 1 if the event-based API context allows read operation, or
|
||||
* return 0.
|
||||
*
|
||||
* After wslay_event_shutdown_read() is called,
|
||||
* wslay_event_get_read_enabled() returns 0.
|
||||
*/
|
||||
int wslay_event_get_read_enabled(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns 1 if the event-based API context allows write operation, or
|
||||
* return 0.
|
||||
*
|
||||
* After wslay_event_shutdown_write() is called,
|
||||
* wslay_event_get_write_enabled() returns 0.
|
||||
*/
|
||||
int wslay_event_get_write_enabled(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns 1 if a close control frame has been received from peer, or
|
||||
* returns 0.
|
||||
*/
|
||||
int wslay_event_get_close_received(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns 1 if a close control frame has been sent to peer, or
|
||||
* returns 0.
|
||||
*/
|
||||
int wslay_event_get_close_sent(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns status code received in close control frame. If no close
|
||||
* control frame has not been received, returns
|
||||
* WSLAY_CODE_ABNORMAL_CLOSURE. If received close control frame has no
|
||||
* status code, returns WSLAY_CODE_NO_STATUS_RCVD.
|
||||
*/
|
||||
uint16_t wslay_event_get_status_code_received(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns status code sent in close control frame. If no close
|
||||
* control frame has not been sent, returns
|
||||
* WSLAY_CODE_ABNORMAL_CLOSURE. If sent close control frame has no
|
||||
* status code, returns WSLAY_CODE_NO_STATUS_RCVD.
|
||||
*/
|
||||
uint16_t wslay_event_get_status_code_sent(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns the number of queued messages.
|
||||
*/
|
||||
size_t wslay_event_get_queued_msg_count(wslay_event_context_ptr ctx);
|
||||
|
||||
/*
|
||||
* Returns the sum of queued message length. It only counts the
|
||||
* message length queued using wslay_event_queue_msg() or
|
||||
* wslay_event_queue_close().
|
||||
*/
|
||||
size_t wslay_event_get_queued_msg_length(wslay_event_context_ptr ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* WSLAY_H */
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAYVER_H
|
||||
#define WSLAYVER_H
|
||||
|
||||
/* normally substituted by the build system; pinned to the vendored release */
|
||||
#define WSLAY_VERSION "1.1.1"
|
||||
|
||||
#endif /* WSLAYVER_H */
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <BaseTsd.h>
|
||||
#ifndef _SSIZE_T_DEFINED
|
||||
#define _SSIZE_T_DEFINED
|
||||
typedef SSIZE_T ssize_t;
|
||||
#endif
|
||||
#endif
|
||||
+1082
File diff suppressed because it is too large
Load Diff
+138
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAY_EVENT_H
|
||||
#define WSLAY_EVENT_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
#include "wslay_queue.h"
|
||||
|
||||
struct wslay_event_byte_chunk {
|
||||
struct wslay_queue_entry qe;
|
||||
uint8_t *data;
|
||||
size_t data_length;
|
||||
};
|
||||
|
||||
struct wslay_event_imsg {
|
||||
uint8_t fin;
|
||||
uint8_t rsv;
|
||||
uint8_t opcode;
|
||||
uint32_t utf8state;
|
||||
struct wslay_queue chunks;
|
||||
size_t msg_length;
|
||||
};
|
||||
|
||||
enum wslay_event_msg_type { WSLAY_NON_FRAGMENTED, WSLAY_FRAGMENTED };
|
||||
|
||||
struct wslay_event_omsg {
|
||||
struct wslay_queue_entry qe;
|
||||
uint8_t fin;
|
||||
uint8_t opcode;
|
||||
uint8_t rsv;
|
||||
enum wslay_event_msg_type type;
|
||||
|
||||
uint8_t *data;
|
||||
size_t data_length;
|
||||
|
||||
union wslay_event_msg_source source;
|
||||
wslay_event_fragmented_msg_callback read_callback;
|
||||
};
|
||||
|
||||
struct wslay_event_frame_user_data {
|
||||
wslay_event_context_ptr ctx;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
enum wslay_event_close_status {
|
||||
WSLAY_CLOSE_RECEIVED = 1 << 0,
|
||||
WSLAY_CLOSE_QUEUED = 1 << 1,
|
||||
WSLAY_CLOSE_SENT = 1 << 2
|
||||
};
|
||||
|
||||
enum wslay_event_config { WSLAY_CONFIG_NO_BUFFERING = 1 << 0 };
|
||||
|
||||
struct wslay_event_context {
|
||||
/* config status, bitwise OR of enum wslay_event_config values*/
|
||||
uint32_t config;
|
||||
/* maximum message length that can be received */
|
||||
uint64_t max_recv_msg_length;
|
||||
/* 1 if initialized for server, otherwise 0 */
|
||||
uint8_t server;
|
||||
/* bitwise OR of enum wslay_event_close_status values */
|
||||
uint8_t close_status;
|
||||
/* status code in received close control frame */
|
||||
uint16_t status_code_recv;
|
||||
/* status code in sent close control frame */
|
||||
uint16_t status_code_sent;
|
||||
wslay_frame_context_ptr frame_ctx;
|
||||
/* 1 if reading is enabled, otherwise 0. Upon receiving close
|
||||
control frame this value set to 0. If any errors in read
|
||||
operation will also set this value to 0. */
|
||||
uint8_t read_enabled;
|
||||
/* 1 if writing is enabled, otherwise 0 Upon completing sending
|
||||
close control frame, this value set to 0. If any errors in write
|
||||
opration will also set this value to 0. */
|
||||
uint8_t write_enabled;
|
||||
/* imsg buffer to allow interleaved control frame between
|
||||
non-control frames. */
|
||||
struct wslay_event_imsg imsgs[2];
|
||||
/* Pointer to imsgs to indicate current used buffer. */
|
||||
struct wslay_event_imsg *imsg;
|
||||
/* payload length of frame currently being received. */
|
||||
uint64_t ipayloadlen;
|
||||
/* next byte offset of payload currently being received. */
|
||||
uint64_t ipayloadoff;
|
||||
/* error value set by user callback */
|
||||
int error;
|
||||
/* Pointer to the message currently being sent. NULL if no message
|
||||
is currently sent. */
|
||||
struct wslay_event_omsg *omsg;
|
||||
/* Queue for non-control frames */
|
||||
struct wslay_queue /*<wslay_omsg*>*/ send_queue;
|
||||
/* Queue for control frames */
|
||||
struct wslay_queue /*<wslay_omsg*>*/ send_ctrl_queue;
|
||||
/* Size of send_queue + size of send_ctrl_queue */
|
||||
size_t queued_msg_count;
|
||||
/* The sum of message length in send_queue */
|
||||
size_t queued_msg_length;
|
||||
/* Buffer used for fragmented messages */
|
||||
uint8_t obuf[4096];
|
||||
uint8_t *obuflimit;
|
||||
uint8_t *obufmark;
|
||||
/* payload length of frame currently being sent. */
|
||||
uint64_t opayloadlen;
|
||||
/* next byte offset of payload currently being sent. */
|
||||
uint64_t opayloadoff;
|
||||
struct wslay_event_callbacks callbacks;
|
||||
struct wslay_event_frame_user_data frame_user_data;
|
||||
void *user_data;
|
||||
uint8_t allowed_rsv_bits;
|
||||
};
|
||||
|
||||
#endif /* WSLAY_EVENT_H */
|
||||
+438
@@ -0,0 +1,438 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "wslay_frame.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "wslay_net.h"
|
||||
|
||||
#define wslay_min(A, B) (((A) < (B)) ? (A) : (B))
|
||||
|
||||
int wslay_frame_context_init(wslay_frame_context_ptr *ctx,
|
||||
const struct wslay_frame_callbacks *callbacks,
|
||||
void *user_data) {
|
||||
*ctx = malloc(sizeof(struct wslay_frame_context));
|
||||
if (*ctx == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memset(*ctx, 0, sizeof(struct wslay_frame_context));
|
||||
(*ctx)->istate = RECV_HEADER1;
|
||||
(*ctx)->ireqread = 2;
|
||||
(*ctx)->ostate = PREP_HEADER;
|
||||
(*ctx)->user_data = user_data;
|
||||
(*ctx)->ibufmark = (*ctx)->ibuflimit = (*ctx)->ibuf;
|
||||
(*ctx)->callbacks = *callbacks;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void wslay_frame_context_free(wslay_frame_context_ptr ctx) { free(ctx); }
|
||||
|
||||
ssize_t wslay_frame_send(wslay_frame_context_ptr ctx,
|
||||
struct wslay_frame_iocb *iocb) {
|
||||
if (iocb->data_length > iocb->payload_length) {
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (ctx->ostate == PREP_HEADER) {
|
||||
uint8_t *hdptr = ctx->oheader;
|
||||
memset(ctx->oheader, 0, sizeof(ctx->oheader));
|
||||
*hdptr |= (uint8_t)((uint8_t)(iocb->fin << 7) & 0x80u);
|
||||
*hdptr |= (uint8_t)((uint8_t)(iocb->rsv << 4) & 0x70u);
|
||||
/* Suppress stubborn gcc-10 warning */
|
||||
*hdptr |= (uint8_t)((uint8_t)(iocb->opcode << 0) & 0xfu);
|
||||
++hdptr;
|
||||
*hdptr |= (uint8_t)((uint8_t)(iocb->mask << 7) & 0x80u);
|
||||
if (wslay_is_ctrl_frame(iocb->opcode) && iocb->payload_length > 125) {
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (iocb->payload_length < 126) {
|
||||
*hdptr |= (uint8_t)iocb->payload_length;
|
||||
++hdptr;
|
||||
} else if (iocb->payload_length < (1 << 16)) {
|
||||
uint16_t len = htons((uint16_t)iocb->payload_length);
|
||||
*hdptr |= 126;
|
||||
++hdptr;
|
||||
memcpy(hdptr, &len, 2);
|
||||
hdptr += 2;
|
||||
} else if (iocb->payload_length < (1ull << 63)) {
|
||||
uint64_t len = hton64(iocb->payload_length);
|
||||
*hdptr |= 127;
|
||||
++hdptr;
|
||||
memcpy(hdptr, &len, 8);
|
||||
hdptr += 8;
|
||||
} else {
|
||||
/* Too large payload length */
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (iocb->mask) {
|
||||
if (ctx->callbacks.genmask_callback(ctx->omaskkey, 4, ctx->user_data) !=
|
||||
0) {
|
||||
return WSLAY_ERR_INVALID_CALLBACK;
|
||||
} else {
|
||||
ctx->omask = 1;
|
||||
memcpy(hdptr, ctx->omaskkey, 4);
|
||||
hdptr += 4;
|
||||
}
|
||||
}
|
||||
ctx->ostate = SEND_HEADER;
|
||||
ctx->oheadermark = ctx->oheader;
|
||||
ctx->oheaderlimit = hdptr;
|
||||
ctx->opayloadlen = iocb->payload_length;
|
||||
ctx->opayloadoff = 0;
|
||||
}
|
||||
if (ctx->ostate == SEND_HEADER) {
|
||||
ptrdiff_t len = ctx->oheaderlimit - ctx->oheadermark;
|
||||
ssize_t r;
|
||||
int flags = 0;
|
||||
if (iocb->data_length > 0) {
|
||||
flags |= WSLAY_MSG_MORE;
|
||||
}
|
||||
r = ctx->callbacks.send_callback(ctx->oheadermark, (size_t)len, flags,
|
||||
ctx->user_data);
|
||||
if (r > 0) {
|
||||
if (r > len) {
|
||||
return WSLAY_ERR_INVALID_CALLBACK;
|
||||
} else {
|
||||
ctx->oheadermark += r;
|
||||
if (ctx->oheadermark == ctx->oheaderlimit) {
|
||||
ctx->ostate = SEND_PAYLOAD;
|
||||
} else {
|
||||
return WSLAY_ERR_WANT_WRITE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return WSLAY_ERR_WANT_WRITE;
|
||||
}
|
||||
}
|
||||
if (ctx->ostate == SEND_PAYLOAD) {
|
||||
size_t totallen = 0;
|
||||
if (iocb->data_length > 0) {
|
||||
if (ctx->omask) {
|
||||
uint8_t temp[4096];
|
||||
const uint8_t *datamark = iocb->data,
|
||||
*datalimit = iocb->data + iocb->data_length;
|
||||
while (datamark < datalimit) {
|
||||
size_t datalen = (size_t)(datalimit - datamark);
|
||||
const uint8_t *writelimit =
|
||||
datamark + wslay_min(sizeof(temp), datalen);
|
||||
size_t writelen = (size_t)(writelimit - datamark);
|
||||
ssize_t r;
|
||||
size_t i;
|
||||
for (i = 0; i < writelen; ++i) {
|
||||
temp[i] = datamark[i] ^ ctx->omaskkey[(ctx->opayloadoff + i) % 4];
|
||||
}
|
||||
r = ctx->callbacks.send_callback(temp, writelen, 0, ctx->user_data);
|
||||
if (r > 0) {
|
||||
if ((size_t)r > writelen) {
|
||||
return WSLAY_ERR_INVALID_CALLBACK;
|
||||
} else {
|
||||
datamark += r;
|
||||
ctx->opayloadoff += (uint64_t)r;
|
||||
totallen += (size_t)r;
|
||||
}
|
||||
} else {
|
||||
if (totallen > 0) {
|
||||
break;
|
||||
} else {
|
||||
return WSLAY_ERR_WANT_WRITE;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ssize_t r;
|
||||
r = ctx->callbacks.send_callback(iocb->data, iocb->data_length, 0,
|
||||
ctx->user_data);
|
||||
if (r > 0) {
|
||||
if ((size_t)r > iocb->data_length) {
|
||||
return WSLAY_ERR_INVALID_CALLBACK;
|
||||
} else {
|
||||
ctx->opayloadoff += (uint64_t)r;
|
||||
totallen = (size_t)r;
|
||||
}
|
||||
} else {
|
||||
return WSLAY_ERR_WANT_WRITE;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ctx->opayloadoff == ctx->opayloadlen) {
|
||||
ctx->ostate = PREP_HEADER;
|
||||
}
|
||||
return (ssize_t)totallen;
|
||||
}
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
ssize_t wslay_frame_write(wslay_frame_context_ptr ctx,
|
||||
struct wslay_frame_iocb *iocb, uint8_t *buf,
|
||||
size_t buflen, size_t *pwpayloadlen) {
|
||||
uint8_t *buf_last = buf;
|
||||
size_t i;
|
||||
size_t hdlen;
|
||||
|
||||
*pwpayloadlen = 0;
|
||||
|
||||
if (iocb->data_length > iocb->payload_length) {
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
switch (ctx->ostate) {
|
||||
case PREP_HEADER:
|
||||
case PREP_HEADER_NOBUF:
|
||||
hdlen = 2;
|
||||
if (iocb->payload_length < 126) {
|
||||
/* nothing to do */
|
||||
} else if (iocb->payload_length < (1 << 16)) {
|
||||
hdlen += 2;
|
||||
} else if (iocb->payload_length < (1ull << 63)) {
|
||||
hdlen += 8;
|
||||
}
|
||||
if (iocb->mask) {
|
||||
hdlen += 4;
|
||||
}
|
||||
|
||||
if (buflen < hdlen) {
|
||||
ctx->ostate = PREP_HEADER_NOBUF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(buf_last, 0, hdlen);
|
||||
*buf_last |= (uint8_t)((uint8_t)(iocb->fin << 7) & 0x80u);
|
||||
*buf_last |= (uint8_t)((uint8_t)(iocb->rsv << 4) & 0x70u);
|
||||
/* Suppress stubborn gcc-10 warning */
|
||||
*buf_last |= (uint8_t)((uint8_t)(iocb->opcode << 0) & 0xfu);
|
||||
++buf_last;
|
||||
*buf_last |= (uint8_t)((uint8_t)(iocb->mask << 7) & 0x80u);
|
||||
if (wslay_is_ctrl_frame(iocb->opcode) && iocb->payload_length > 125) {
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (iocb->payload_length < 126) {
|
||||
*buf_last |= (uint8_t)iocb->payload_length;
|
||||
++buf_last;
|
||||
} else if (iocb->payload_length < (1 << 16)) {
|
||||
uint16_t len = htons((uint16_t)iocb->payload_length);
|
||||
*buf_last |= 126;
|
||||
++buf_last;
|
||||
memcpy(buf_last, &len, 2);
|
||||
buf_last += 2;
|
||||
} else if (iocb->payload_length < (1ull << 63)) {
|
||||
uint64_t len = hton64(iocb->payload_length);
|
||||
*buf_last |= 127;
|
||||
++buf_last;
|
||||
memcpy(buf_last, &len, 8);
|
||||
buf_last += 8;
|
||||
} else {
|
||||
/* Too large payload length */
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (iocb->mask) {
|
||||
if (ctx->callbacks.genmask_callback(ctx->omaskkey, 4, ctx->user_data) !=
|
||||
0) {
|
||||
return WSLAY_ERR_INVALID_CALLBACK;
|
||||
} else {
|
||||
ctx->omask = 1;
|
||||
memcpy(buf_last, ctx->omaskkey, 4);
|
||||
buf_last += 4;
|
||||
}
|
||||
}
|
||||
ctx->ostate = SEND_PAYLOAD;
|
||||
ctx->opayloadlen = iocb->payload_length;
|
||||
ctx->opayloadoff = 0;
|
||||
|
||||
buflen -= (size_t)(buf_last - buf);
|
||||
/* fall through */
|
||||
case SEND_PAYLOAD:
|
||||
if (iocb->data_length > 0) {
|
||||
size_t writelen = wslay_min(buflen, iocb->data_length);
|
||||
|
||||
if (ctx->omask) {
|
||||
for (i = 0; i < writelen; ++i) {
|
||||
*buf_last++ =
|
||||
iocb->data[i] ^ ctx->omaskkey[(ctx->opayloadoff + i) % 4];
|
||||
}
|
||||
} else {
|
||||
memcpy(buf_last, iocb->data, writelen);
|
||||
buf_last += writelen;
|
||||
}
|
||||
|
||||
ctx->opayloadoff += writelen;
|
||||
*pwpayloadlen = writelen;
|
||||
}
|
||||
|
||||
if (ctx->opayloadoff == ctx->opayloadlen) {
|
||||
ctx->ostate = PREP_HEADER;
|
||||
}
|
||||
|
||||
return buf_last - buf;
|
||||
default:
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
static void wslay_shift_ibuf(wslay_frame_context_ptr ctx) {
|
||||
ptrdiff_t len = ctx->ibuflimit - ctx->ibufmark;
|
||||
memmove(ctx->ibuf, ctx->ibufmark, (size_t)len);
|
||||
ctx->ibuflimit = ctx->ibuf + len;
|
||||
ctx->ibufmark = ctx->ibuf;
|
||||
}
|
||||
|
||||
static ssize_t wslay_recv(wslay_frame_context_ptr ctx) {
|
||||
ssize_t r;
|
||||
if (ctx->ibufmark != ctx->ibuf) {
|
||||
wslay_shift_ibuf(ctx);
|
||||
}
|
||||
r = ctx->callbacks.recv_callback(
|
||||
ctx->ibuflimit, (size_t)(ctx->ibuf + sizeof(ctx->ibuf) - ctx->ibuflimit),
|
||||
0, ctx->user_data);
|
||||
if (r > 0) {
|
||||
ctx->ibuflimit += r;
|
||||
} else {
|
||||
r = WSLAY_ERR_WANT_READ;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
#define WSLAY_AVAIL_IBUF(ctx) ((size_t)(ctx->ibuflimit - ctx->ibufmark))
|
||||
|
||||
ssize_t wslay_frame_recv(wslay_frame_context_ptr ctx,
|
||||
struct wslay_frame_iocb *iocb) {
|
||||
ssize_t r;
|
||||
if (ctx->istate == RECV_HEADER1) {
|
||||
uint8_t fin, opcode, rsv, payloadlen;
|
||||
if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
|
||||
if ((r = wslay_recv(ctx)) <= 0) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
|
||||
return WSLAY_ERR_WANT_READ;
|
||||
}
|
||||
fin = (ctx->ibufmark[0] >> 7) & 1;
|
||||
rsv = (ctx->ibufmark[0] >> 4) & 7;
|
||||
opcode = ctx->ibufmark[0] & 0xfu;
|
||||
ctx->iom.opcode = opcode;
|
||||
ctx->iom.fin = fin;
|
||||
ctx->iom.rsv = rsv;
|
||||
++ctx->ibufmark;
|
||||
ctx->imask = (ctx->ibufmark[0] >> 7) & 1;
|
||||
payloadlen = ctx->ibufmark[0] & 0x7fu;
|
||||
++ctx->ibufmark;
|
||||
if (wslay_is_ctrl_frame(opcode) && (payloadlen > 125 || !fin)) {
|
||||
return WSLAY_ERR_PROTO;
|
||||
}
|
||||
if (payloadlen == 126) {
|
||||
ctx->istate = RECV_EXT_PAYLOADLEN;
|
||||
ctx->ireqread = 2;
|
||||
} else if (payloadlen == 127) {
|
||||
ctx->istate = RECV_EXT_PAYLOADLEN;
|
||||
ctx->ireqread = 8;
|
||||
} else {
|
||||
ctx->ipayloadlen = payloadlen;
|
||||
ctx->ipayloadoff = 0;
|
||||
if (ctx->imask) {
|
||||
ctx->istate = RECV_MASKKEY;
|
||||
ctx->ireqread = 4;
|
||||
} else {
|
||||
ctx->istate = RECV_PAYLOAD;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ctx->istate == RECV_EXT_PAYLOADLEN) {
|
||||
if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
|
||||
if ((r = wslay_recv(ctx)) <= 0) {
|
||||
return r;
|
||||
}
|
||||
if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
|
||||
return WSLAY_ERR_WANT_READ;
|
||||
}
|
||||
}
|
||||
ctx->ipayloadlen = 0;
|
||||
ctx->ipayloadoff = 0;
|
||||
memcpy((uint8_t *)&ctx->ipayloadlen + (8 - ctx->ireqread), ctx->ibufmark,
|
||||
ctx->ireqread);
|
||||
ctx->ipayloadlen = ntoh64(ctx->ipayloadlen);
|
||||
ctx->ibufmark += ctx->ireqread;
|
||||
if (ctx->ireqread == 8) {
|
||||
if (ctx->ipayloadlen < (1 << 16) || ctx->ipayloadlen & (1ull << 63)) {
|
||||
return WSLAY_ERR_PROTO;
|
||||
}
|
||||
} else if (ctx->ipayloadlen < 126) {
|
||||
return WSLAY_ERR_PROTO;
|
||||
}
|
||||
if (ctx->imask) {
|
||||
ctx->istate = RECV_MASKKEY;
|
||||
ctx->ireqread = 4;
|
||||
} else {
|
||||
ctx->istate = RECV_PAYLOAD;
|
||||
}
|
||||
}
|
||||
if (ctx->istate == RECV_MASKKEY) {
|
||||
if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
|
||||
if ((r = wslay_recv(ctx)) <= 0) {
|
||||
return r;
|
||||
}
|
||||
if (WSLAY_AVAIL_IBUF(ctx) < ctx->ireqread) {
|
||||
return WSLAY_ERR_WANT_READ;
|
||||
}
|
||||
}
|
||||
memcpy(ctx->imaskkey, ctx->ibufmark, 4);
|
||||
ctx->ibufmark += 4;
|
||||
ctx->istate = RECV_PAYLOAD;
|
||||
}
|
||||
if (ctx->istate == RECV_PAYLOAD) {
|
||||
uint8_t *readlimit, *readmark;
|
||||
uint64_t rempayloadlen = ctx->ipayloadlen - ctx->ipayloadoff;
|
||||
if (WSLAY_AVAIL_IBUF(ctx) == 0 && rempayloadlen > 0) {
|
||||
if ((r = wslay_recv(ctx)) <= 0) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
readmark = ctx->ibufmark;
|
||||
readlimit = WSLAY_AVAIL_IBUF(ctx) < rempayloadlen
|
||||
? ctx->ibuflimit
|
||||
: ctx->ibufmark + rempayloadlen;
|
||||
if (ctx->imask) {
|
||||
for (; ctx->ibufmark != readlimit; ++ctx->ibufmark, ++ctx->ipayloadoff) {
|
||||
ctx->ibufmark[0] ^= ctx->imaskkey[ctx->ipayloadoff % 4];
|
||||
}
|
||||
} else {
|
||||
ctx->ibufmark = readlimit;
|
||||
ctx->ipayloadoff += (uint64_t)(readlimit - readmark);
|
||||
}
|
||||
iocb->fin = ctx->iom.fin;
|
||||
iocb->rsv = ctx->iom.rsv;
|
||||
iocb->opcode = ctx->iom.opcode;
|
||||
iocb->payload_length = ctx->ipayloadlen;
|
||||
iocb->mask = ctx->imask;
|
||||
iocb->data = readmark;
|
||||
iocb->data_length = (size_t)(ctx->ibufmark - readmark);
|
||||
if (ctx->ipayloadlen == ctx->ipayloadoff) {
|
||||
ctx->istate = RECV_HEADER1;
|
||||
ctx->ireqread = 2;
|
||||
}
|
||||
return (ssize_t)iocb->data_length;
|
||||
}
|
||||
return WSLAY_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAY_FRAME_H
|
||||
#define WSLAY_FRAME_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
enum wslay_frame_state {
|
||||
PREP_HEADER,
|
||||
PREP_HEADER_NOBUF,
|
||||
SEND_HEADER,
|
||||
SEND_PAYLOAD,
|
||||
RECV_HEADER1,
|
||||
RECV_PAYLOADLEN,
|
||||
RECV_EXT_PAYLOADLEN,
|
||||
RECV_MASKKEY,
|
||||
RECV_PAYLOAD
|
||||
};
|
||||
|
||||
struct wslay_frame_opcode_memo {
|
||||
uint8_t fin;
|
||||
uint8_t opcode;
|
||||
uint8_t rsv;
|
||||
};
|
||||
|
||||
struct wslay_frame_context {
|
||||
uint8_t ibuf[4096];
|
||||
uint8_t *ibufmark;
|
||||
uint8_t *ibuflimit;
|
||||
struct wslay_frame_opcode_memo iom;
|
||||
uint64_t ipayloadlen;
|
||||
uint64_t ipayloadoff;
|
||||
uint8_t imask;
|
||||
uint8_t imaskkey[4];
|
||||
enum wslay_frame_state istate;
|
||||
size_t ireqread;
|
||||
|
||||
uint8_t oheader[14];
|
||||
uint8_t *oheadermark;
|
||||
uint8_t *oheaderlimit;
|
||||
uint64_t opayloadlen;
|
||||
uint64_t opayloadoff;
|
||||
uint8_t omask;
|
||||
uint8_t omaskkey[4];
|
||||
enum wslay_frame_state ostate;
|
||||
|
||||
struct wslay_frame_callbacks callbacks;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#endif /* WSLAY_FRAME_H */
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2020 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAY_MACRO_H
|
||||
#define WSLAY_MACRO_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define wslay_struct_of(ptr, type, member) \
|
||||
((type *)(void *)((char *)(ptr)-offsetof(type, member)))
|
||||
|
||||
#endif /* WSLAY_MACRO_H */
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "wslay_net.h"
|
||||
|
||||
#ifndef WORDS_BIGENDIAN
|
||||
|
||||
uint64_t wslay_byteswap64(uint64_t x) {
|
||||
uint64_t u = ntohl(x & 0xffffffffllu);
|
||||
uint64_t l = ntohl((uint32_t)(x >> 32));
|
||||
return (u << 32) | l;
|
||||
}
|
||||
|
||||
#endif /* !WORDS_BIGENDIAN */
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAY_NET_H
|
||||
#define WSLAY_NET_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
#ifdef HAVE_ARPA_INET_H
|
||||
# include <arpa/inet.h>
|
||||
#endif /* HAVE_ARPA_INET_H */
|
||||
#ifdef HAVE_NETINET_IN_H
|
||||
# include <netinet/in.h>
|
||||
#endif /* HAVE_NETINET_IN_H */
|
||||
/* For Mingw build */
|
||||
#ifdef HAVE_WINSOCK2_H
|
||||
# include <winsock2.h>
|
||||
#endif /* HAVE_WINSOCK2_H */
|
||||
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
# define ntoh64(x) (x)
|
||||
# define hton64(x) (x)
|
||||
#else /* !WORDS_BIGENDIAN */
|
||||
uint64_t wslay_byteswap64(uint64_t x);
|
||||
# define ntoh64(x) wslay_byteswap64(x)
|
||||
# define hton64(x) wslay_byteswap64(x)
|
||||
#endif /* !WORDS_BIGENDIAN */
|
||||
|
||||
#endif /* WSLAY_NET_H */
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "wslay_queue.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "wslay_macro.h"
|
||||
|
||||
void wslay_queue_init(struct wslay_queue *queue) {
|
||||
queue->top = NULL;
|
||||
queue->tail = &queue->top;
|
||||
}
|
||||
|
||||
void wslay_queue_deinit(struct wslay_queue *queue) { (void)queue; }
|
||||
|
||||
void wslay_queue_push(struct wslay_queue *queue,
|
||||
struct wslay_queue_entry *ent) {
|
||||
ent->next = NULL;
|
||||
*queue->tail = ent;
|
||||
queue->tail = &ent->next;
|
||||
}
|
||||
|
||||
void wslay_queue_push_front(struct wslay_queue *queue,
|
||||
struct wslay_queue_entry *ent) {
|
||||
ent->next = queue->top;
|
||||
queue->top = ent;
|
||||
|
||||
if (ent->next == NULL) {
|
||||
queue->tail = &ent->next;
|
||||
}
|
||||
}
|
||||
|
||||
void wslay_queue_pop(struct wslay_queue *queue) {
|
||||
assert(queue->top);
|
||||
queue->top = queue->top->next;
|
||||
if (queue->top == NULL) {
|
||||
queue->tail = &queue->top;
|
||||
}
|
||||
}
|
||||
|
||||
struct wslay_queue_entry *wslay_queue_top(struct wslay_queue *queue) {
|
||||
assert(queue->top);
|
||||
return queue->top;
|
||||
}
|
||||
|
||||
struct wslay_queue_entry *wslay_queue_tail(struct wslay_queue *queue) {
|
||||
assert(queue->top);
|
||||
return wslay_struct_of(queue->tail, struct wslay_queue_entry, next);
|
||||
}
|
||||
|
||||
int wslay_queue_empty(struct wslay_queue *queue) {
|
||||
assert(queue->top || queue->tail == &queue->top);
|
||||
return queue->top == NULL;
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Wslay - The WebSocket Library
|
||||
*
|
||||
* Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef WSLAY_QUEUE_H
|
||||
#define WSLAY_QUEUE_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
struct wslay_queue_entry {
|
||||
struct wslay_queue_entry *next;
|
||||
};
|
||||
|
||||
struct wslay_queue {
|
||||
struct wslay_queue_entry *top;
|
||||
struct wslay_queue_entry **tail;
|
||||
};
|
||||
|
||||
void wslay_queue_init(struct wslay_queue *queue);
|
||||
void wslay_queue_deinit(struct wslay_queue *queue);
|
||||
void wslay_queue_push(struct wslay_queue *queue, struct wslay_queue_entry *ent);
|
||||
void wslay_queue_push_front(struct wslay_queue *queue,
|
||||
struct wslay_queue_entry *ent);
|
||||
void wslay_queue_pop(struct wslay_queue *queue);
|
||||
struct wslay_queue_entry *wslay_queue_top(struct wslay_queue *queue);
|
||||
struct wslay_queue_entry *wslay_queue_tail(struct wslay_queue *queue);
|
||||
int wslay_queue_empty(struct wslay_queue *queue);
|
||||
|
||||
#endif /* WSLAY_QUEUE_H */
|
||||
@@ -1402,6 +1402,52 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Wslay (MIT)
|
||||
-------------------------------------------
|
||||
Copyright (c) 2011, 2012, 2015 Tatsuhiro Tsujikawa
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Wslay UTF-8 decoder (MIT)
|
||||
-------------------------------------------
|
||||
Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
|
||||
Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Contributions
|
||||
-------------------------------------------
|
||||
cardio - Felix - MIT License
|
||||
|
||||
Reference in New Issue
Block a user