2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* lws_peer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
2017-12-21 03:47:16 +01:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-12-21 03:13:23 +01:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2019-01-01 12:46:36 +01:00
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
#ifndef JAVASCRIPT_ENABLED
|
|
|
|
|
|
|
|
#include "lws_peer.h"
|
2018-09-27 11:24:41 +02:00
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
#include "core/io/ip.h"
|
|
|
|
|
2018-04-10 17:52:10 +02:00
|
|
|
// Needed for socket_helpers on Android at least. UNIXes has it, just include if not windows
|
|
|
|
#if !defined(WINDOWS_ENABLED)
|
|
|
|
#include <netinet/in.h>
|
2018-04-19 23:20:26 +02:00
|
|
|
#include <sys/socket.h>
|
2018-04-10 17:52:10 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-27 19:39:23 +02:00
|
|
|
#include "drivers/unix/net_socket_posix.h"
|
2018-04-10 17:52:10 +02:00
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
void LWSPeer::set_wsi(struct lws *p_wsi, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size, unsigned int p_out_pkt_size) {
|
2018-08-06 03:40:26 +02:00
|
|
|
ERR_FAIL_COND(wsi != NULL);
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
_in_buffer.resize(p_in_pkt_size, p_in_buf_size);
|
|
|
|
_out_buffer.resize(p_out_pkt_size, p_out_buf_size);
|
|
|
|
_packet_buffer.resize((1 << MAX(p_in_buf_size, p_out_buf_size)) + LWS_PRE);
|
2017-12-21 03:13:23 +01:00
|
|
|
wsi = p_wsi;
|
|
|
|
};
|
|
|
|
|
|
|
|
void LWSPeer::set_write_mode(WriteMode p_mode) {
|
|
|
|
write_mode = p_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWSPeer::WriteMode LWSPeer::get_write_mode() const {
|
|
|
|
return write_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LWSPeer::read_wsi(void *in, size_t len) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
if (lws_is_first_fragment(wsi))
|
|
|
|
_in_size = 0;
|
|
|
|
else if (_in_size == -1) // Trash this frame
|
|
|
|
return ERR_FILE_CORRUPT;
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
Error err = _in_buffer.write_packet((const uint8_t *)in, len, NULL);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
_in_buffer.discard_payload(_in_size);
|
|
|
|
_in_size = -1;
|
|
|
|
ERR_FAIL_V(err);
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
_in_size += len;
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
if (lws_is_final_fragment(wsi)) {
|
2018-11-03 05:46:38 +01:00
|
|
|
uint8_t is_string = lws_frame_is_binary(wsi) ? 0 : 1;
|
|
|
|
err = _in_buffer.write_packet(NULL, _in_size, &is_string);
|
|
|
|
if (err != OK) {
|
|
|
|
_in_buffer.discard_payload(_in_size);
|
|
|
|
_in_size = -1;
|
|
|
|
ERR_FAIL_V(err);
|
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LWSPeer::write_wsi() {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
|
|
|
PoolVector<uint8_t> tmp;
|
2018-11-03 05:46:38 +01:00
|
|
|
int count = _out_buffer.packets_left();
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
if (count == 0)
|
2017-12-21 03:13:23 +01:00
|
|
|
return OK;
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
int read = 0;
|
2019-02-18 20:28:40 +01:00
|
|
|
uint8_t is_string = 0;
|
2018-11-03 05:46:38 +01:00
|
|
|
PoolVector<uint8_t>::Write rw = _packet_buffer.write();
|
|
|
|
_out_buffer.read_packet(&(rw[LWS_PRE]), _packet_buffer.size() - LWS_PRE, &is_string, read);
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
enum lws_write_protocol mode = is_string ? LWS_WRITE_TEXT : LWS_WRITE_BINARY;
|
|
|
|
lws_write(wsi, &(rw[LWS_PRE]), read, mode);
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
if (count > 1)
|
2017-12-21 03:13:23 +01:00
|
|
|
lws_callback_on_writable(wsi); // we want to write more!
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
uint8_t is_string = write_mode == WRITE_MODE_TEXT;
|
|
|
|
_out_buffer.write_packet(p_buffer, p_buffer_size, &is_string);
|
2017-12-21 03:13:23 +01:00
|
|
|
lws_callback_on_writable(wsi); // notify that we want to write
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
Error LWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
r_buffer_size = 0;
|
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
if (_in_buffer.packets_left() == 0)
|
2017-12-21 03:13:23 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
int read = 0;
|
|
|
|
PoolVector<uint8_t>::Write rw = _packet_buffer.write();
|
|
|
|
_in_buffer.read_packet(rw.ptr(), _packet_buffer.size(), &_is_string, read);
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
*r_buffer = rw.ptr();
|
|
|
|
r_buffer_size = read;
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
int LWSPeer::get_available_packet_count() const {
|
|
|
|
|
|
|
|
if (!is_connected_to_host())
|
|
|
|
return 0;
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
return _in_buffer.packets_left();
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool LWSPeer::was_string_packet() const {
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
return _is_string;
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool LWSPeer::is_connected_to_host() const {
|
|
|
|
|
|
|
|
return wsi != NULL;
|
|
|
|
};
|
|
|
|
|
2018-09-23 21:14:20 +02:00
|
|
|
String LWSPeer::get_close_reason(void *in, size_t len, int &r_code) {
|
|
|
|
String s;
|
|
|
|
r_code = 0;
|
|
|
|
if (len < 2) // From docs this should not happen
|
|
|
|
return s;
|
|
|
|
|
|
|
|
const uint8_t *b = (const uint8_t *)in;
|
|
|
|
r_code = b[0] << 8 | b[1];
|
|
|
|
|
|
|
|
if (len > 2) {
|
|
|
|
const char *utf8 = (const char *)&b[2];
|
|
|
|
s.parse_utf8(utf8, len - 2);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWSPeer::send_close_status(struct lws *p_wsi) {
|
|
|
|
if (close_code == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int len = close_reason.size();
|
|
|
|
ERR_FAIL_COND(len > 123); // Maximum allowed reason size in bytes
|
|
|
|
|
|
|
|
lws_close_status code = (lws_close_status)close_code;
|
|
|
|
unsigned char *reason = len > 0 ? (unsigned char *)close_reason.utf8().ptrw() : NULL;
|
|
|
|
|
|
|
|
lws_close_reason(p_wsi, code, reason, len);
|
|
|
|
|
|
|
|
close_code = -1;
|
|
|
|
close_reason = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWSPeer::close(int p_code, String p_reason) {
|
2017-12-21 03:13:23 +01:00
|
|
|
if (wsi != NULL) {
|
2018-09-23 21:14:20 +02:00
|
|
|
close_code = p_code;
|
|
|
|
close_reason = p_reason;
|
2017-12-21 03:13:23 +01:00
|
|
|
PeerData *data = ((PeerData *)lws_wsi_user(wsi));
|
|
|
|
data->force_close = true;
|
2018-09-24 00:58:28 +02:00
|
|
|
data->clean_close = true;
|
2018-09-23 21:14:20 +02:00
|
|
|
lws_callback_on_writable(wsi); // Notify that we want to disconnect
|
|
|
|
} else {
|
|
|
|
close_code = -1;
|
|
|
|
close_reason = "";
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
2018-08-06 03:40:26 +02:00
|
|
|
wsi = NULL;
|
2018-11-03 05:46:38 +01:00
|
|
|
_in_buffer.clear();
|
|
|
|
_out_buffer.clear();
|
|
|
|
_in_size = 0;
|
|
|
|
_is_string = 0;
|
|
|
|
_packet_buffer.resize(0);
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
IP_Address LWSPeer::get_connected_host() const {
|
|
|
|
|
2018-04-10 17:52:10 +02:00
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), IP_Address());
|
|
|
|
|
|
|
|
IP_Address ip;
|
2018-09-27 19:39:23 +02:00
|
|
|
uint16_t port = 0;
|
2018-04-10 17:52:10 +02:00
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
2018-06-06 18:34:08 +02:00
|
|
|
socklen_t len = sizeof(addr);
|
2018-04-19 23:20:26 +02:00
|
|
|
|
2018-04-10 17:52:10 +02:00
|
|
|
int fd = lws_get_socket_fd(wsi);
|
2018-04-19 23:20:26 +02:00
|
|
|
ERR_FAIL_COND_V(fd == -1, IP_Address());
|
2018-04-10 17:52:10 +02:00
|
|
|
|
|
|
|
int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
|
|
|
|
ERR_FAIL_COND_V(ret != 0, IP_Address());
|
|
|
|
|
2018-09-27 19:39:23 +02:00
|
|
|
NetSocketPosix::_set_ip_port(&addr, ip, port);
|
2018-04-10 17:52:10 +02:00
|
|
|
|
|
|
|
return ip;
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
uint16_t LWSPeer::get_connected_port() const {
|
|
|
|
|
2018-04-10 17:52:10 +02:00
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), 0);
|
|
|
|
|
|
|
|
IP_Address ip;
|
2018-09-27 19:39:23 +02:00
|
|
|
uint16_t port = 0;
|
2018-04-10 17:52:10 +02:00
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
2018-06-06 18:34:08 +02:00
|
|
|
socklen_t len = sizeof(addr);
|
2018-04-19 23:20:26 +02:00
|
|
|
|
2018-04-10 17:52:10 +02:00
|
|
|
int fd = lws_get_socket_fd(wsi);
|
2018-04-19 23:20:26 +02:00
|
|
|
ERR_FAIL_COND_V(fd == -1, 0);
|
2018-04-10 17:52:10 +02:00
|
|
|
|
|
|
|
int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
|
|
|
|
ERR_FAIL_COND_V(ret != 0, 0);
|
|
|
|
|
2018-09-27 19:39:23 +02:00
|
|
|
NetSocketPosix::_set_ip_port(&addr, ip, port);
|
2018-04-10 17:52:10 +02:00
|
|
|
|
|
|
|
return port;
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
LWSPeer::LWSPeer() {
|
|
|
|
wsi = NULL;
|
|
|
|
write_mode = WRITE_MODE_BINARY;
|
2018-08-06 03:40:26 +02:00
|
|
|
close();
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
LWSPeer::~LWSPeer() {
|
|
|
|
|
|
|
|
close();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // JAVASCRIPT_ENABLED
|