Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/enet/enet_godot.cpp
21345 views
1
/**************************************************************************/
2
/* enet_godot.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
/**
32
@file enet_godot.cpp
33
@brief ENet Godot specific functions
34
*/
35
36
#include "core/io/dtls_server.h"
37
#include "core/io/ip.h"
38
#include "core/io/net_socket.h"
39
#include "core/io/packet_peer_dtls.h"
40
#include "core/io/udp_server.h"
41
#include "core/os/os.h"
42
43
// This must be last for windows to compile (tested with MinGW)
44
#include "enet/enet.h"
45
46
/// Abstract ENet interface for UDP/DTLS.
47
class ENetGodotSocket {
48
public:
49
virtual Error bind(IPAddress p_ip, uint16_t p_port) = 0;
50
virtual Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) = 0;
51
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) = 0;
52
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) = 0;
53
virtual int set_option(ENetSocketOption p_option, int p_value) = 0;
54
virtual void close() = 0;
55
virtual void set_refuse_new_connections(bool p_enable) {} /* Only used by dtls server */
56
virtual bool can_upgrade() { return false; } /* Only true in ENetUDP */
57
virtual void disconnect_peer(ENetPeer *p_peer) {}
58
virtual void bind_peer(ENetPeer *p_peer) {}
59
virtual ~ENetGodotSocket() {}
60
};
61
62
class ENetDTLSClient;
63
class ENetDTLSServer;
64
65
/// NetSocket interface
66
class ENetUDP : public ENetGodotSocket {
67
friend class ENetDTLSClient;
68
friend class ENetDTLSServer;
69
70
private:
71
Ref<NetSocket> sock;
72
IPAddress local_address;
73
bool bound = false;
74
75
public:
76
ENetUDP() {
77
sock = Ref<NetSocket>(NetSocket::create());
78
IP::Type ip_type = IP::TYPE_ANY;
79
sock->open(NetSocket::Family::INET, NetSocket::TYPE_UDP, ip_type);
80
}
81
82
~ENetUDP() {
83
sock->close();
84
}
85
86
bool can_upgrade() {
87
return true;
88
}
89
90
Error bind(IPAddress p_ip, uint16_t p_port) {
91
local_address = p_ip;
92
bound = true;
93
NetSocket::Address addr(p_ip, p_port);
94
return sock->bind(addr);
95
}
96
97
Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) {
98
NetSocket::Address addr;
99
Error err = sock->get_socket_address(&addr);
100
*r_ip = addr.ip();
101
*r_port = addr.port();
102
if (bound) {
103
*r_ip = local_address;
104
}
105
return err;
106
}
107
108
Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
109
return sock->sendto(p_buffer, p_len, r_sent, p_ip, p_port);
110
}
111
112
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) {
113
Error err = sock->poll(NetSocket::POLL_TYPE_IN, 0);
114
if (err != OK) {
115
return err;
116
}
117
return sock->recvfrom(p_buffer, p_len, r_read, r_ip, r_port);
118
}
119
120
int set_option(ENetSocketOption p_option, int p_value) {
121
switch (p_option) {
122
case ENET_SOCKOPT_NONBLOCK: {
123
sock->set_blocking_enabled(p_value ? false : true);
124
return 0;
125
} break;
126
127
case ENET_SOCKOPT_BROADCAST: {
128
sock->set_broadcasting_enabled(p_value ? true : false);
129
return 0;
130
} break;
131
132
case ENET_SOCKOPT_REUSEADDR: {
133
sock->set_reuse_address_enabled(p_value ? true : false);
134
return 0;
135
} break;
136
137
case ENET_SOCKOPT_RCVBUF: {
138
return -1;
139
} break;
140
141
case ENET_SOCKOPT_SNDBUF: {
142
return -1;
143
} break;
144
145
case ENET_SOCKOPT_RCVTIMEO: {
146
return -1;
147
} break;
148
149
case ENET_SOCKOPT_SNDTIMEO: {
150
return -1;
151
} break;
152
153
case ENET_SOCKOPT_NODELAY: {
154
sock->set_tcp_no_delay_enabled(p_value ? true : false);
155
return 0;
156
} break;
157
}
158
159
return -1;
160
}
161
162
void close() {
163
sock->close();
164
local_address.clear();
165
}
166
};
167
168
/// DTLS Client ENet interface
169
class ENetDTLSClient : public ENetGodotSocket {
170
bool connected = false;
171
Ref<PacketPeerUDP> udp;
172
Ref<PacketPeerDTLS> dtls;
173
Ref<TLSOptions> tls_options;
174
String for_hostname;
175
IPAddress local_address;
176
uint16_t local_port = 0;
177
bool bound = false;
178
179
public:
180
ENetDTLSClient(ENetUDP *p_base, String p_for_hostname, Ref<TLSOptions> p_options) {
181
for_hostname = p_for_hostname;
182
tls_options = p_options;
183
udp.instantiate();
184
dtls = Ref<PacketPeerDTLS>(PacketPeerDTLS::create());
185
if (p_base->bound) {
186
p_base->get_socket_address(&local_address, &local_port);
187
p_base->close();
188
bind(local_address, local_port);
189
}
190
}
191
192
~ENetDTLSClient() {
193
close();
194
}
195
196
Error bind(IPAddress p_ip, uint16_t p_port) {
197
bound = true;
198
local_address = p_ip;
199
local_port = p_port;
200
return udp->bind(p_port, p_ip);
201
}
202
203
Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) {
204
if (!udp->is_bound()) {
205
return ERR_UNCONFIGURED;
206
}
207
*r_ip = local_address;
208
*r_port = udp->get_local_port();
209
return OK;
210
}
211
212
Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
213
if (!connected) {
214
udp->connect_to_host(p_ip, p_port);
215
if (dtls->connect_to_peer(udp, for_hostname, tls_options)) {
216
close();
217
if (bound) {
218
bind(local_address, local_port);
219
}
220
return FAILED; // May still retry...
221
}
222
connected = true;
223
}
224
dtls->poll();
225
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
226
return ERR_BUSY;
227
} else if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
228
close();
229
if (bound) {
230
bind(local_address, local_port);
231
}
232
return FAILED; // May still retry...
233
}
234
r_sent = p_len;
235
return dtls->put_packet(p_buffer, p_len);
236
}
237
238
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) {
239
dtls->poll();
240
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
241
return ERR_BUSY;
242
}
243
if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
244
close();
245
if (bound) {
246
bind(local_address, local_port);
247
}
248
return FAILED;
249
}
250
int pc = dtls->get_available_packet_count();
251
if (pc == 0) {
252
return ERR_BUSY;
253
} else if (pc < 0) {
254
return FAILED;
255
}
256
257
const uint8_t *buffer;
258
Error err = dtls->get_packet(&buffer, r_read);
259
ERR_FAIL_COND_V(err != OK, err);
260
ERR_FAIL_COND_V(p_len < r_read, ERR_OUT_OF_MEMORY);
261
262
memcpy(p_buffer, buffer, r_read);
263
r_ip = udp->get_packet_address();
264
r_port = udp->get_packet_port();
265
return err;
266
}
267
268
int set_option(ENetSocketOption p_option, int p_value) {
269
return -1;
270
}
271
272
void close() {
273
dtls->disconnect_from_peer();
274
udp->close();
275
connected = false;
276
}
277
};
278
279
/// DTLSServer - ENet interface
280
class ENetDTLSServer : public ENetGodotSocket {
281
Ref<DTLSServer> server;
282
Ref<UDPServer> udp_server;
283
284
enum {
285
ENET_DTLS_TIMEOUT_MS = 8000,
286
};
287
288
struct Peer {
289
Ref<PacketPeerDTLS> conn;
290
bool bound = false;
291
uint64_t timeout = 0;
292
};
293
294
HashMap<String, Peer> peers;
295
IPAddress local_address;
296
size_t max_clients = 0;
297
298
public:
299
ENetDTLSServer(ENetUDP *p_base, Ref<TLSOptions> p_options, size_t p_max_clients) {
300
max_clients = p_max_clients;
301
udp_server.instantiate();
302
if (p_base->bound) {
303
uint16_t port;
304
p_base->get_socket_address(&local_address, &port);
305
p_base->close();
306
bind(local_address, port);
307
}
308
server = Ref<DTLSServer>(DTLSServer::create());
309
server->setup(p_options);
310
}
311
312
~ENetDTLSServer() {
313
close();
314
}
315
316
void set_refuse_new_connections(bool p_refuse) {
317
udp_server->set_max_pending_connections(p_refuse ? 0 : 16);
318
}
319
320
Error bind(IPAddress p_ip, uint16_t p_port) {
321
local_address = p_ip;
322
return udp_server->listen(p_port, p_ip);
323
}
324
325
Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) {
326
if (!udp_server->is_listening()) {
327
return ERR_UNCONFIGURED;
328
}
329
*r_ip = local_address;
330
*r_port = udp_server->get_local_port();
331
return OK;
332
}
333
334
Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
335
String key = String(p_ip) + ":" + itos(p_port);
336
Peer *peer = peers.getptr(key);
337
if (unlikely(peer == nullptr)) {
338
// The peer might have been disconnected due to a DTLS error.
339
// We need to wait for it to time out, just mark the packet as sent.
340
r_sent = p_len;
341
return OK;
342
}
343
Error err = peer->conn->put_packet(p_buffer, p_len);
344
if (err == OK) {
345
r_sent = p_len;
346
} else if (err == ERR_BUSY) {
347
r_sent = 0;
348
} else {
349
// The peer might have been disconnected due to a DTLS error.
350
// We need to wait for it to time out, just mark the packet as sent.
351
r_sent = p_len;
352
return OK;
353
}
354
return err;
355
}
356
357
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port) {
358
udp_server->poll();
359
// TODO limits? Maybe we can better enforce allowed connections!
360
if (udp_server->is_connection_available()) {
361
Ref<PacketPeerUDP> udp = udp_server->take_connection();
362
IPAddress peer_ip = udp->get_packet_address();
363
int peer_port = udp->get_packet_port();
364
Ref<PacketPeerDTLS> peer = server->take_connection(udp);
365
PacketPeerDTLS::Status status = peer->get_status();
366
if (peers.size() < max_clients && status == PacketPeerDTLS::STATUS_HANDSHAKING || status == PacketPeerDTLS::STATUS_CONNECTED) {
367
String key = String(peer_ip) + ":" + itos(peer_port);
368
Peer p;
369
p.conn = peer;
370
p.timeout = OS::get_singleton()->get_ticks_msec() + ENET_DTLS_TIMEOUT_MS;
371
peers[key] = p;
372
}
373
}
374
375
List<String> remove;
376
Error err = ERR_BUSY;
377
// TODO this needs to be fair!
378
379
for (KeyValue<String, Peer> &E : peers) {
380
if (unlikely(!E.value.bound && OS::get_singleton()->get_ticks_msec() >= E.value.timeout)) {
381
remove.push_back(E.key);
382
continue;
383
}
384
Ref<PacketPeerDTLS> peer = E.value.conn;
385
peer->poll();
386
387
if (peer->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
388
continue;
389
} else if (peer->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
390
// Peer disconnected, removing it.
391
remove.push_back(E.key);
392
continue;
393
}
394
395
if (peer->get_available_packet_count() > 0) {
396
const uint8_t *buffer;
397
err = peer->get_packet(&buffer, r_read);
398
if (err != OK || p_len < r_read) {
399
// Something wrong with this peer, removing it.
400
remove.push_back(E.key);
401
err = ERR_BUSY;
402
r_read = 0;
403
continue;
404
}
405
406
Vector<String> s = E.key.rsplit(":", false, 1);
407
ERR_CONTINUE(s.size() != 2); // BUG!
408
409
memcpy(p_buffer, buffer, r_read);
410
r_ip = s[0];
411
r_port = s[1].to_int();
412
break; // err = OK
413
}
414
}
415
416
// Remove disconnected peers from map.
417
for (String &E : remove) {
418
peers.erase(E);
419
}
420
421
return err; // OK, ERR_BUSY, or possibly an error.
422
}
423
424
int set_option(ENetSocketOption p_option, int p_value) {
425
return -1;
426
}
427
428
void bind_peer(ENetPeer *p_peer) {
429
if (p_peer == nullptr) {
430
return;
431
}
432
IPAddress ip;
433
ip.set_ipv6((uint8_t *)&(p_peer->address.host));
434
int port = p_peer->address.port;
435
const String key = String(ip) + ":" + itos(port);
436
Peer *p = peers.getptr(key);
437
if (p == nullptr) {
438
return;
439
}
440
p->bound = true;
441
}
442
443
void disconnect_peer(ENetPeer *p_peer) {
444
if (p_peer == nullptr) {
445
return;
446
}
447
IPAddress ip;
448
ip.set_ipv6((uint8_t *)&(p_peer->address.host));
449
int port = p_peer->address.port;
450
const String key = String(ip) + ":" + itos(port);
451
peers.erase(key);
452
};
453
454
void close() {
455
for (KeyValue<String, Peer> &E : peers) {
456
E.value.conn->disconnect_from_peer();
457
}
458
peers.clear();
459
udp_server->stop();
460
server->stop();
461
local_address.clear();
462
}
463
};
464
465
static enet_uint32 timeBase = 0;
466
467
int enet_initialize(void) {
468
return 0;
469
}
470
471
void enet_deinitialize(void) {
472
}
473
474
enet_uint32 enet_host_random_seed(void) {
475
return (enet_uint32)OS::get_singleton()->get_unix_time();
476
}
477
478
enet_uint32 enet_time_get(void) {
479
return OS::get_singleton()->get_ticks_msec() - timeBase;
480
}
481
482
void enet_time_set(enet_uint32 newTimeBase) {
483
timeBase = OS::get_singleton()->get_ticks_msec() - newTimeBase;
484
}
485
486
int enet_address_set_host(ENetAddress *address, const char *name) {
487
IPAddress ip = IP::get_singleton()->resolve_hostname(name);
488
ERR_FAIL_COND_V(!ip.is_valid(), -1);
489
490
enet_address_set_ip(address, ip.get_ipv6(), 16);
491
return 0;
492
}
493
494
void enet_address_set_ip(ENetAddress *address, const uint8_t *ip, size_t size) {
495
int len = size > 16 ? 16 : size;
496
memset(address->host, 0, 16);
497
memcpy(address->host, ip, len);
498
}
499
500
int enet_address_get_host_ip(const ENetAddress *address, char *name, size_t nameLength) {
501
return -1;
502
}
503
504
int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLength) {
505
return -1;
506
}
507
508
ENetSocket enet_socket_create(ENetSocketType type) {
509
ENetUDP *socket = memnew(ENetUDP);
510
511
return socket;
512
}
513
514
void enet_peer_socket_bind(ENetPeer *p_peer) {
515
if (p_peer == nullptr || p_peer->host == nullptr) {
516
return;
517
}
518
ENetGodotSocket *sock = (ENetGodotSocket *)p_peer->host->socket;
519
if (sock == nullptr) {
520
return;
521
}
522
sock->bind_peer(p_peer);
523
}
524
525
void enet_peer_socket_destroy(ENetPeer *p_peer) {
526
if (p_peer == nullptr || p_peer->host == nullptr) {
527
return;
528
}
529
ENetGodotSocket *sock = (ENetGodotSocket *)p_peer->host->socket;
530
if (sock == nullptr) {
531
return;
532
}
533
sock->disconnect_peer(p_peer);
534
}
535
536
int enet_host_dtls_server_setup(ENetHost *host, void *p_options) {
537
ERR_FAIL_COND_V_MSG(!DTLSServer::is_available(), -1, "DTLS server is not available in this build.");
538
ENetGodotSocket *sock = (ENetGodotSocket *)host->socket;
539
if (!sock->can_upgrade()) {
540
return -1;
541
}
542
host->socket = memnew(ENetDTLSServer(static_cast<ENetUDP *>(sock), Ref<TLSOptions>(static_cast<TLSOptions *>(p_options)), host->peerCount));
543
memdelete(sock);
544
return 0;
545
}
546
547
int enet_host_dtls_client_setup(ENetHost *host, const char *p_for_hostname, void *p_options) {
548
ERR_FAIL_COND_V_MSG(!PacketPeerDTLS::is_available(), -1, "DTLS is not available in this build.");
549
ENetGodotSocket *sock = (ENetGodotSocket *)host->socket;
550
if (!sock->can_upgrade()) {
551
return -1;
552
}
553
host->socket = memnew(ENetDTLSClient(static_cast<ENetUDP *>(sock), String::utf8(p_for_hostname), Ref<TLSOptions>(static_cast<TLSOptions *>(p_options))));
554
memdelete(sock);
555
return 0;
556
}
557
558
void enet_host_refuse_new_connections(ENetHost *host, int p_refuse) {
559
ERR_FAIL_NULL(host->socket);
560
((ENetGodotSocket *)host->socket)->set_refuse_new_connections(p_refuse);
561
}
562
563
int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
564
IPAddress ip;
565
if (address->wildcard) {
566
ip = IPAddress("*");
567
} else {
568
ip.set_ipv6(address->host);
569
}
570
571
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
572
if (sock->bind(ip, address->port) != OK) {
573
return -1;
574
}
575
return 0;
576
}
577
578
void enet_socket_destroy(ENetSocket socket) {
579
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
580
sock->close();
581
memdelete(sock);
582
}
583
584
int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
585
ERR_FAIL_NULL_V(address, -1);
586
587
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
588
IPAddress dest;
589
Error err;
590
size_t i = 0;
591
592
dest.set_ipv6(address->host);
593
594
// Create a single packet.
595
Vector<uint8_t> out;
596
uint8_t *w;
597
int size = 0;
598
int pos = 0;
599
for (i = 0; i < bufferCount; i++) {
600
size += buffers[i].dataLength;
601
}
602
603
out.resize(size);
604
w = out.ptrw();
605
for (i = 0; i < bufferCount; i++) {
606
memcpy(&w[pos], buffers[i].data, buffers[i].dataLength);
607
pos += buffers[i].dataLength;
608
}
609
610
int sent = 0;
611
err = sock->sendto((const uint8_t *)&w[0], size, sent, dest, address->port);
612
if (err != OK) {
613
if (err == ERR_BUSY) { // Blocking call
614
return 0;
615
}
616
617
WARN_PRINT("Sending failed!");
618
return -1;
619
}
620
621
return sent;
622
}
623
624
int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
625
ERR_FAIL_COND_V(bufferCount != 1, -1);
626
627
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
628
629
int read;
630
IPAddress ip;
631
632
Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port);
633
if (err == ERR_BUSY) {
634
return 0;
635
}
636
if (err == ERR_OUT_OF_MEMORY) {
637
// A packet above the ENET_PROTOCOL_MAXIMUM_MTU was received.
638
return -2;
639
}
640
641
if (err != OK) {
642
return -1;
643
}
644
645
enet_address_set_ip(address, ip.get_ipv6(), 16);
646
647
return read;
648
}
649
650
int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
651
IPAddress ip;
652
uint16_t port;
653
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
654
655
if (sock->get_socket_address(&ip, &port) != OK) {
656
return -1;
657
}
658
659
enet_address_set_ip(address, ip.get_ipv6(), 16);
660
address->port = port;
661
662
return 0;
663
}
664
665
// Not implemented
666
int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
667
return 0; // do we need this function?
668
}
669
670
int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
671
return -1;
672
}
673
674
int enet_socket_listen(ENetSocket socket, int backlog) {
675
return -1;
676
}
677
678
int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
679
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
680
return sock->set_option(option, value);
681
}
682
683
int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
684
return -1;
685
}
686
687
int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
688
return -1;
689
}
690
691
ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
692
return nullptr;
693
}
694
695
int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
696
return -1;
697
}
698
699