2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* godot.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2017-01-22 04:51:34 +00:00
|
|
|
/**
|
2017-03-23 12:27:41 +00:00
|
|
|
@file godot.cpp
|
2017-01-22 04:51:34 +00:00
|
|
|
@brief ENet Godot specific functions
|
|
|
|
*/
|
|
|
|
|
2020-01-13 12:10:12 +00:00
|
|
|
#include "core/io/dtls_server.h"
|
2017-01-22 04:51:34 +00:00
|
|
|
#include "core/io/ip.h"
|
2018-10-18 20:35:11 +00:00
|
|
|
#include "core/io/net_socket.h"
|
2020-01-13 12:10:12 +00:00
|
|
|
#include "core/io/packet_peer_dtls.h"
|
|
|
|
#include "core/io/udp_server.h"
|
2017-01-22 04:51:34 +00:00
|
|
|
#include "core/os/os.h"
|
|
|
|
|
|
|
|
// This must be last for windows to compile (tested with MinGW)
|
|
|
|
#include "enet/enet.h"
|
|
|
|
|
2020-01-13 12:10:12 +00:00
|
|
|
/// Abstract ENet interface for UDP/DTLS.
|
|
|
|
class ENetGodotSocket {
|
|
|
|
public:
|
2021-05-06 00:48:18 +00:00
|
|
|
virtual Error bind(IPAddress p_ip, uint16_t p_port) = 0;
|
|
|
|
virtual Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) = 0;
|
|
|
|
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) = 0;
|
|
|
|
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) = 0;
|
2020-01-13 12:10:12 +00:00
|
|
|
virtual int set_option(ENetSocketOption p_option, int p_value) = 0;
|
|
|
|
virtual void close() = 0;
|
2020-07-21 09:38:16 +00:00
|
|
|
virtual void set_refuse_new_connections(bool p_enable) {} /* Only used by dtls server */
|
2021-06-14 09:51:53 +00:00
|
|
|
virtual bool can_upgrade() { return false; } /* Only true in ENetUDP */
|
2020-07-21 09:38:16 +00:00
|
|
|
virtual ~ENetGodotSocket() {}
|
2020-01-13 12:10:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ENetDTLSClient;
|
|
|
|
class ENetDTLSServer;
|
|
|
|
|
|
|
|
/// NetSocket interface
|
|
|
|
class ENetUDP : public ENetGodotSocket {
|
|
|
|
friend class ENetDTLSClient;
|
|
|
|
friend class ENetDTLSServer;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Ref<NetSocket> sock;
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress local_address;
|
2020-07-21 09:38:16 +00:00
|
|
|
bool bound = false;
|
2020-01-13 12:10:12 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ENetUDP() {
|
|
|
|
sock = Ref<NetSocket>(NetSocket::create());
|
|
|
|
IP::Type ip_type = IP::TYPE_ANY;
|
|
|
|
sock->open(NetSocket::TYPE_UDP, ip_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ENetUDP() {
|
|
|
|
sock->close();
|
|
|
|
}
|
|
|
|
|
2021-06-14 09:51:53 +00:00
|
|
|
bool can_upgrade() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error bind(IPAddress p_ip, uint16_t p_port) {
|
2021-03-29 16:14:50 +00:00
|
|
|
local_address = p_ip;
|
2020-01-13 12:10:12 +00:00
|
|
|
bound = true;
|
2021-03-29 16:14:50 +00:00
|
|
|
return sock->bind(p_ip, p_port);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) {
|
2021-06-14 09:51:53 +00:00
|
|
|
Error err = sock->get_socket_address(r_ip, r_port);
|
|
|
|
if (bound) {
|
|
|
|
*r_ip = local_address;
|
|
|
|
}
|
|
|
|
return err;
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return sock->sendto(p_buffer, p_len, r_sent, p_ip, p_port);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) {
|
2020-01-13 12:10:12 +00:00
|
|
|
Error err = sock->poll(NetSocket::POLL_TYPE_IN, 0);
|
2020-07-21 09:38:16 +00:00
|
|
|
if (err != OK) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return err;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2020-01-13 12:10:12 +00:00
|
|
|
return sock->recvfrom(p_buffer, p_len, r_read, r_ip, r_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
int set_option(ENetSocketOption p_option, int p_value) {
|
|
|
|
switch (p_option) {
|
|
|
|
case ENET_SOCKOPT_NONBLOCK: {
|
|
|
|
sock->set_blocking_enabled(p_value ? false : true);
|
|
|
|
return 0;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_BROADCAST: {
|
|
|
|
sock->set_broadcasting_enabled(p_value ? true : false);
|
|
|
|
return 0;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_REUSEADDR: {
|
|
|
|
sock->set_reuse_address_enabled(p_value ? true : false);
|
|
|
|
return 0;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_RCVBUF: {
|
|
|
|
return -1;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_SNDBUF: {
|
|
|
|
return -1;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_RCVTIMEO: {
|
|
|
|
return -1;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_SNDTIMEO: {
|
|
|
|
return -1;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_NODELAY: {
|
|
|
|
sock->set_tcp_no_delay_enabled(p_value ? true : false);
|
|
|
|
return 0;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
sock->close();
|
2021-03-29 16:14:50 +00:00
|
|
|
local_address.clear();
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// DTLS Client ENet interface
|
|
|
|
class ENetDTLSClient : public ENetGodotSocket {
|
2020-07-21 09:38:16 +00:00
|
|
|
bool connected = false;
|
2020-01-13 12:10:12 +00:00
|
|
|
Ref<PacketPeerUDP> udp;
|
|
|
|
Ref<PacketPeerDTLS> dtls;
|
2023-01-20 00:51:35 +00:00
|
|
|
Ref<TLSOptions> tls_options;
|
2020-01-13 12:10:12 +00:00
|
|
|
String for_hostname;
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress local_address;
|
2020-01-13 12:10:12 +00:00
|
|
|
|
|
|
|
public:
|
2023-01-20 00:51:35 +00:00
|
|
|
ENetDTLSClient(ENetUDP *p_base, String p_for_hostname, Ref<TLSOptions> p_options) {
|
2020-01-13 12:10:12 +00:00
|
|
|
for_hostname = p_for_hostname;
|
2023-01-20 00:51:35 +00:00
|
|
|
tls_options = p_options;
|
2021-06-17 22:03:09 +00:00
|
|
|
udp.instantiate();
|
2020-01-13 12:10:12 +00:00
|
|
|
dtls = Ref<PacketPeerDTLS>(PacketPeerDTLS::create());
|
|
|
|
if (p_base->bound) {
|
2021-03-29 16:14:50 +00:00
|
|
|
uint16_t port;
|
|
|
|
p_base->get_socket_address(&local_address, &port);
|
|
|
|
p_base->close();
|
|
|
|
bind(local_address, port);
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ENetDTLSClient() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error bind(IPAddress p_ip, uint16_t p_port) {
|
2021-03-29 16:14:50 +00:00
|
|
|
local_address = p_ip;
|
2021-03-26 16:56:08 +00:00
|
|
|
return udp->bind(p_port, p_ip);
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) {
|
2021-03-29 16:14:50 +00:00
|
|
|
if (!udp->is_bound()) {
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
}
|
|
|
|
*r_ip = local_address;
|
|
|
|
*r_port = udp->get_local_port();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
|
2020-01-13 12:10:12 +00:00
|
|
|
if (!connected) {
|
|
|
|
udp->connect_to_host(p_ip, p_port);
|
2023-01-20 00:51:35 +00:00
|
|
|
if (dtls->connect_to_peer(udp, for_hostname, tls_options)) {
|
2021-06-14 09:51:53 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
2020-01-13 12:10:12 +00:00
|
|
|
connected = true;
|
|
|
|
}
|
|
|
|
dtls->poll();
|
2020-07-21 09:38:16 +00:00
|
|
|
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return ERR_BUSY;
|
2020-07-21 09:38:16 +00:00
|
|
|
} else if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return FAILED;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2020-01-13 12:10:12 +00:00
|
|
|
r_sent = p_len;
|
|
|
|
return dtls->put_packet(p_buffer, p_len);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) {
|
2020-01-13 12:10:12 +00:00
|
|
|
dtls->poll();
|
2020-07-21 09:38:16 +00:00
|
|
|
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return ERR_BUSY;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
|
|
|
if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return FAILED;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2020-01-13 12:10:12 +00:00
|
|
|
int pc = dtls->get_available_packet_count();
|
2020-07-21 09:38:16 +00:00
|
|
|
if (pc == 0) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return ERR_BUSY;
|
2020-07-21 09:38:16 +00:00
|
|
|
} else if (pc < 0) {
|
2020-01-13 12:10:12 +00:00
|
|
|
return FAILED;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2020-01-13 12:10:12 +00:00
|
|
|
|
|
|
|
const uint8_t *buffer;
|
|
|
|
Error err = dtls->get_packet(&buffer, r_read);
|
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
ERR_FAIL_COND_V(p_len < r_read, ERR_OUT_OF_MEMORY);
|
|
|
|
|
2021-04-27 14:19:21 +00:00
|
|
|
memcpy(p_buffer, buffer, r_read);
|
2020-01-13 12:10:12 +00:00
|
|
|
r_ip = udp->get_packet_address();
|
|
|
|
r_port = udp->get_packet_port();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int set_option(ENetSocketOption p_option, int p_value) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
dtls->disconnect_from_peer();
|
|
|
|
udp->close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// DTLSServer - ENet interface
|
|
|
|
class ENetDTLSServer : public ENetGodotSocket {
|
|
|
|
Ref<DTLSServer> server;
|
|
|
|
Ref<UDPServer> udp_server;
|
2022-05-13 13:04:37 +00:00
|
|
|
HashMap<String, Ref<PacketPeerDTLS>> peers;
|
2020-07-21 09:38:16 +00:00
|
|
|
int last_service = 0;
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress local_address;
|
2020-01-13 12:10:12 +00:00
|
|
|
|
|
|
|
public:
|
2023-01-20 00:51:35 +00:00
|
|
|
ENetDTLSServer(ENetUDP *p_base, Ref<TLSOptions> p_options) {
|
2021-06-17 22:03:09 +00:00
|
|
|
udp_server.instantiate();
|
2020-01-13 12:10:12 +00:00
|
|
|
if (p_base->bound) {
|
2021-03-29 16:14:50 +00:00
|
|
|
uint16_t port;
|
|
|
|
p_base->get_socket_address(&local_address, &port);
|
|
|
|
p_base->close();
|
|
|
|
bind(local_address, port);
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
server = Ref<DTLSServer>(DTLSServer::create());
|
2023-01-20 00:51:35 +00:00
|
|
|
server->setup(p_options);
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~ENetDTLSServer() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:14:27 +00:00
|
|
|
void set_refuse_new_connections(bool p_refuse) {
|
|
|
|
udp_server->set_max_pending_connections(p_refuse ? 0 : 16);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error bind(IPAddress p_ip, uint16_t p_port) {
|
2021-03-29 16:14:50 +00:00
|
|
|
local_address = p_ip;
|
2020-01-13 12:10:12 +00:00
|
|
|
return udp_server->listen(p_port, p_ip);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) {
|
2021-03-29 16:14:50 +00:00
|
|
|
if (!udp_server->is_listening()) {
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
}
|
|
|
|
*r_ip = local_address;
|
|
|
|
*r_port = udp_server->get_local_port();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
|
2020-01-13 12:10:12 +00:00
|
|
|
String key = String(p_ip) + ":" + itos(p_port);
|
|
|
|
ERR_FAIL_COND_V(!peers.has(key), ERR_UNAVAILABLE);
|
|
|
|
Ref<PacketPeerDTLS> peer = peers[key];
|
|
|
|
Error err = peer->put_packet(p_buffer, p_len);
|
2020-07-21 09:38:16 +00:00
|
|
|
if (err == OK) {
|
2020-01-13 12:10:12 +00:00
|
|
|
r_sent = p_len;
|
2020-07-21 09:38:16 +00:00
|
|
|
} else if (err == ERR_BUSY) {
|
2020-01-13 12:10:12 +00:00
|
|
|
r_sent = 0;
|
2020-07-21 09:38:16 +00:00
|
|
|
} else {
|
2020-01-13 12:10:12 +00:00
|
|
|
r_sent = -1;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2020-01-13 12:10:12 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) {
|
2020-07-13 17:14:27 +00:00
|
|
|
udp_server->poll();
|
2020-01-13 12:10:12 +00:00
|
|
|
// TODO limits? Maybe we can better enforce allowed connections!
|
|
|
|
if (udp_server->is_connection_available()) {
|
|
|
|
Ref<PacketPeerUDP> udp = udp_server->take_connection();
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress peer_ip = udp->get_packet_address();
|
2020-01-13 12:10:12 +00:00
|
|
|
int peer_port = udp->get_packet_port();
|
|
|
|
Ref<PacketPeerDTLS> peer = server->take_connection(udp);
|
|
|
|
PacketPeerDTLS::Status status = peer->get_status();
|
|
|
|
if (status == PacketPeerDTLS::STATUS_HANDSHAKING || status == PacketPeerDTLS::STATUS_CONNECTED) {
|
|
|
|
String key = String(peer_ip) + ":" + itos(peer_port);
|
|
|
|
peers[key] = peer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> remove;
|
|
|
|
Error err = ERR_BUSY;
|
|
|
|
// TODO this needs to be fair!
|
2022-05-13 13:04:37 +00:00
|
|
|
|
|
|
|
for (KeyValue<String, Ref<PacketPeerDTLS>> & E : peers) {
|
|
|
|
Ref<PacketPeerDTLS> peer = E.value;
|
2020-01-13 12:10:12 +00:00
|
|
|
peer->poll();
|
|
|
|
|
2020-07-21 09:38:16 +00:00
|
|
|
if (peer->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
|
2020-01-13 12:10:12 +00:00
|
|
|
continue;
|
2020-07-21 09:38:16 +00:00
|
|
|
} else if (peer->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
|
2020-01-13 12:10:12 +00:00
|
|
|
// Peer disconnected, removing it.
|
2022-05-13 13:04:37 +00:00
|
|
|
remove.push_back(E.key);
|
2020-01-13 12:10:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer->get_available_packet_count() > 0) {
|
|
|
|
const uint8_t *buffer;
|
|
|
|
err = peer->get_packet(&buffer, r_read);
|
|
|
|
if (err != OK || p_len < r_read) {
|
|
|
|
// Something wrong with this peer, removing it.
|
2022-05-13 13:04:37 +00:00
|
|
|
remove.push_back(E.key);
|
2020-01-13 12:10:12 +00:00
|
|
|
err = FAILED;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
Vector<String> s = E.key.rsplit(":", false, 1);
|
2020-01-13 12:10:12 +00:00
|
|
|
ERR_CONTINUE(s.size() != 2); // BUG!
|
|
|
|
|
2021-04-27 14:19:21 +00:00
|
|
|
memcpy(p_buffer, buffer, r_read);
|
2020-01-13 12:10:12 +00:00
|
|
|
r_ip = s[0];
|
|
|
|
r_port = s[1].to_int();
|
|
|
|
break; // err = OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disconnected peers from map.
|
2021-07-16 03:45:57 +00:00
|
|
|
for (String &E : remove) {
|
|
|
|
peers.erase(E);
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err; // OK, ERR_BUSY, or possibly an error.
|
|
|
|
}
|
|
|
|
|
|
|
|
int set_option(ENetSocketOption p_option, int p_value) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
2022-05-13 13:04:37 +00:00
|
|
|
for (KeyValue<String, Ref<PacketPeerDTLS>> &E : peers) {
|
|
|
|
E.value->disconnect_from_peer();
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
peers.clear();
|
|
|
|
udp_server->stop();
|
|
|
|
server->stop();
|
2021-03-29 16:14:50 +00:00
|
|
|
local_address.clear();
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-22 04:51:34 +00:00
|
|
|
static enet_uint32 timeBase = 0;
|
|
|
|
|
|
|
|
int enet_initialize(void) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enet_deinitialize(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_uint32 enet_host_random_seed(void) {
|
|
|
|
return (enet_uint32)OS::get_singleton()->get_unix_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_uint32 enet_time_get(void) {
|
|
|
|
return OS::get_singleton()->get_ticks_msec() - timeBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enet_time_set(enet_uint32 newTimeBase) {
|
|
|
|
timeBase = OS::get_singleton()->get_ticks_msec() - newTimeBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_address_set_host(ENetAddress *address, const char *name) {
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress ip = IP::get_singleton()->resolve_hostname(name);
|
2017-01-22 04:51:34 +00:00
|
|
|
ERR_FAIL_COND_V(!ip.is_valid(), -1);
|
|
|
|
|
|
|
|
enet_address_set_ip(address, ip.get_ipv6(), 16);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enet_address_set_ip(ENetAddress *address, const uint8_t *ip, size_t size) {
|
|
|
|
int len = size > 16 ? 16 : size;
|
|
|
|
memset(address->host, 0, 16);
|
|
|
|
memcpy(address->host, ip, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_address_get_host_ip(const ENetAddress *address, char *name, size_t nameLength) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLength) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-18 20:35:11 +00:00
|
|
|
ENetSocket enet_socket_create(ENetSocketType type) {
|
2020-01-13 12:10:12 +00:00
|
|
|
ENetUDP *socket = memnew(ENetUDP);
|
2018-10-18 20:35:11 +00:00
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
int enet_host_dtls_server_setup(ENetHost *host, void *p_options) {
|
2023-05-18 17:22:23 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!DTLSServer::is_available(), -1, "DTLS server is not available in this build.");
|
2021-06-14 09:51:53 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)host->socket;
|
|
|
|
if (!sock->can_upgrade()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-01-20 00:51:35 +00:00
|
|
|
host->socket = memnew(ENetDTLSServer(static_cast<ENetUDP *>(sock), Ref<TLSOptions>(static_cast<TLSOptions *>(p_options))));
|
2020-01-13 12:10:12 +00:00
|
|
|
memdelete(sock);
|
2021-06-14 09:51:53 +00:00
|
|
|
return 0;
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
int enet_host_dtls_client_setup(ENetHost *host, const char *p_for_hostname, void *p_options) {
|
2023-05-18 17:22:23 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!PacketPeerDTLS::is_available(), -1, "DTLS is not available in this build.");
|
2021-06-14 09:51:53 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)host->socket;
|
|
|
|
if (!sock->can_upgrade()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-01-20 00:51:35 +00:00
|
|
|
host->socket = memnew(ENetDTLSClient(static_cast<ENetUDP *>(sock), String::utf8(p_for_hostname), Ref<TLSOptions>(static_cast<TLSOptions *>(p_options))));
|
2020-01-13 12:10:12 +00:00
|
|
|
memdelete(sock);
|
2021-06-14 09:51:53 +00:00
|
|
|
return 0;
|
2020-01-13 12:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 17:14:27 +00:00
|
|
|
void enet_host_refuse_new_connections(ENetHost *host, int p_refuse) {
|
|
|
|
ERR_FAIL_COND(!host->socket);
|
|
|
|
((ENetGodotSocket *)host->socket)->set_refuse_new_connections(p_refuse);
|
|
|
|
}
|
|
|
|
|
2017-01-22 04:51:34 +00:00
|
|
|
int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress ip;
|
2017-01-22 04:51:34 +00:00
|
|
|
if (address->wildcard) {
|
2021-05-06 00:48:18 +00:00
|
|
|
ip = IPAddress("*");
|
2017-01-22 04:51:34 +00:00
|
|
|
} else {
|
|
|
|
ip.set_ipv6(address->host);
|
|
|
|
}
|
|
|
|
|
2020-01-13 12:10:12 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
|
2018-10-18 20:35:11 +00:00
|
|
|
if (sock->bind(ip, address->port) != OK) {
|
2017-01-22 04:51:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enet_socket_destroy(ENetSocket socket) {
|
2020-01-13 12:10:12 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
|
2017-01-22 04:51:34 +00:00
|
|
|
sock->close();
|
|
|
|
memdelete(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
|
2020-07-21 09:38:16 +00:00
|
|
|
ERR_FAIL_COND_V(address == nullptr, -1);
|
2017-01-22 04:51:34 +00:00
|
|
|
|
2020-01-13 12:10:12 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress dest;
|
2017-01-22 04:51:34 +00:00
|
|
|
Error err;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
dest.set_ipv6(address->host);
|
|
|
|
|
|
|
|
// Create a single packet.
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> out;
|
2020-07-21 09:38:16 +00:00
|
|
|
uint8_t *w;
|
2017-01-22 04:51:34 +00:00
|
|
|
int size = 0;
|
|
|
|
int pos = 0;
|
|
|
|
for (i = 0; i < bufferCount; i++) {
|
|
|
|
size += buffers[i].dataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.resize(size);
|
2020-02-17 21:06:54 +00:00
|
|
|
w = out.ptrw();
|
2017-01-22 04:51:34 +00:00
|
|
|
for (i = 0; i < bufferCount; i++) {
|
|
|
|
memcpy(&w[pos], buffers[i].data, buffers[i].dataLength);
|
|
|
|
pos += buffers[i].dataLength;
|
|
|
|
}
|
|
|
|
|
2018-10-18 20:35:11 +00:00
|
|
|
int sent = 0;
|
|
|
|
err = sock->sendto((const uint8_t *)&w[0], size, sent, dest, address->port);
|
2017-01-22 04:51:34 +00:00
|
|
|
if (err != OK) {
|
2018-09-02 03:32:12 +00:00
|
|
|
if (err == ERR_BUSY) { // Blocking call
|
2017-03-08 16:14:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-22 04:51:34 +00:00
|
|
|
WARN_PRINT("Sending failed!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-18 20:35:11 +00:00
|
|
|
return sent;
|
2017-01-22 04:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
|
|
|
|
ERR_FAIL_COND_V(bufferCount != 1, -1);
|
|
|
|
|
2020-01-13 12:10:12 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
|
2017-01-22 04:51:34 +00:00
|
|
|
|
2018-10-18 20:35:11 +00:00
|
|
|
int read;
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress ip;
|
2017-01-22 04:51:34 +00:00
|
|
|
|
2018-10-18 20:35:11 +00:00
|
|
|
Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port);
|
2020-07-21 09:38:16 +00:00
|
|
|
if (err == ERR_BUSY) {
|
2018-10-18 20:35:11 +00:00
|
|
|
return 0;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2018-10-18 20:35:11 +00:00
|
|
|
|
2020-07-21 09:38:16 +00:00
|
|
|
if (err != OK) {
|
2018-10-18 20:35:11 +00:00
|
|
|
return -1;
|
2020-07-21 09:38:16 +00:00
|
|
|
}
|
2018-10-18 20:35:11 +00:00
|
|
|
|
|
|
|
enet_address_set_ip(address, ip.get_ipv6(), 16);
|
2017-01-22 04:51:34 +00:00
|
|
|
|
2018-10-18 20:35:11 +00:00
|
|
|
return read;
|
2017-01-22 04:51:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 16:14:50 +00:00
|
|
|
int enet_socket_get_address (ENetSocket socket, ENetAddress * address) {
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress ip;
|
2021-03-29 16:14:50 +00:00
|
|
|
uint16_t port;
|
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
|
|
|
|
|
|
|
|
if (sock->get_socket_address(&ip, &port) != OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_address_set_ip(address, ip.get_ipv6(), 16);
|
|
|
|
address->port = port;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-22 04:51:34 +00:00
|
|
|
// Not implemented
|
|
|
|
int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
|
|
|
|
return 0; // do we need this function?
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_listen(ENetSocket socket, int backlog) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
|
2020-01-13 12:10:12 +00:00
|
|
|
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
|
|
|
|
return sock->set_option(option, value);
|
2017-01-22 04:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
|
2020-07-21 09:38:16 +00:00
|
|
|
return nullptr;
|
2017-01-22 04:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
|
|
|
|
return -1;
|
|
|
|
}
|