1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/*************************************************************************/
/* net_socket_posix.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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 "net_socket_posix.h"
#if defined(UNIX_ENABLED)
#include <errno.h>
#include <netdb.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef NO_FCNTL
#ifdef __HAIKU__
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif
#else
#include <sys/ioctl.h>
#endif
#include <netinet/in.h>
#include <sys/socket.h>
#ifdef JAVASCRIPT_ENABLED
#include <arpa/inet.h>
#endif
#include <netinet/tcp.h>
#if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif
// Some custom defines to minimize ifdefs
#define SOCK_EMPTY -1
#define SOCK_BUF(x) x
#define SOCK_CBUF(x) x
#define SOCK_IOCTL ioctl
#define SOCK_POLL ::poll
#define SOCK_CLOSE ::close
/* Windows */
#elif defined(WINDOWS_ENABLED)
#include <winsock2.h>
#include <ws2tcpip.h>
// Some custom defines to minimize ifdefs
#define SOCK_EMPTY INVALID_SOCKET
#define SOCK_BUF(x) (char *)(x)
#define SOCK_CBUF(x) (const char *)(x)
#define SOCK_IOCTL ioctlsocket
#define SOCK_POLL WSAPoll
#define SOCK_CLOSE closesocket
// Windows doesn't have this flag
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#endif
static size_t _set_addr_storage(struct sockaddr_storage *p_addr, const IP_Address &p_ip, uint16_t p_port, IP::Type p_ip_type) {
memset(p_addr, 0, sizeof(struct sockaddr_storage));
if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket
// IPv6 only socket with IPv4 address
ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(p_port);
if (p_ip.is_valid()) {
copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
} else {
addr6->sin6_addr = in6addr_any;
}
return sizeof(sockaddr_in6);
} else { // IPv4 socket
// IPv4 socket with IPv6 address
ERR_FAIL_COND_V(!p_ip.is_ipv4(), 0);
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(p_port); // short, network byte order
if (p_ip.is_valid()) {
copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
} else {
addr4->sin_addr.s_addr = INADDR_ANY;
}
copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16);
return sizeof(sockaddr_in);
}
}
static void _set_ip_port(IP_Address &r_ip, uint16_t &r_port, struct sockaddr_storage *p_addr) {
if (p_addr->ss_family == AF_INET) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
r_port = ntohs(addr4->sin_port);
} else if (p_addr->ss_family == AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
r_ip.set_ipv6(addr6->sin6_addr.s6_addr);
r_port = ntohs(addr6->sin6_port);
};
}
NetSocket *NetSocketPosix::_create_func() {
return memnew(NetSocketPosix);
}
void NetSocketPosix::make_default() {
#if defined(WINDOWS_ENABLED)
if (_create == NULL) {
WSADATA data;
WSAStartup(MAKEWORD(2, 2), &data);
}
#endif
_create = _create_func;
}
void NetSocketPosix::cleanup() {
#if defined(WINDOWS_ENABLED)
if (_create != NULL) {
WSACleanup();
}
_create = NULL;
#endif
}
NetSocketPosix::NetSocketPosix() {
_sock = SOCK_EMPTY;
_ip_type = IP::TYPE_NONE;
_is_stream = false;
}
NetSocketPosix::~NetSocketPosix() {
close();
}
NetSocketPosix::NetError NetSocketPosix::_get_socket_error() {
#if defined(WINDOWS_ENABLED)
int err = WSAGetLastError();
if (err == WSAEISCONN)
return ERR_NET_IS_CONNECTED;
if (err == WSAEINPROGRESS || err == WSAEALREADY)
return ERR_NET_IN_PROGRESS;
if (err == WSAEWOULDBLOCK)
return ERR_NET_WOULD_BLOCK;
ERR_PRINTS("Socket error: " + itos(err));
return ERR_NET_OTHER;
#else
if (errno == EISCONN)
return ERR_NET_IS_CONNECTED;
if (errno == EINPROGRESS || errno == EALREADY)
return ERR_NET_IN_PROGRESS;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return ERR_NET_WOULD_BLOCK;
ERR_PRINTS("Socket error: " + itos(errno));
return ERR_NET_OTHER;
#endif
}
bool NetSocketPosix::_can_use_ip(const IP_Address p_ip, const bool p_for_bind) const {
if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) {
return false;
} else if (!p_for_bind && !p_ip.is_valid()) {
return false;
}
// Check if socket support this IP type.
IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
if (_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type) {
return false;
}
return true;
}
void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream) {
_sock = p_sock;
_ip_type = p_ip_type;
_is_stream = p_is_stream;
}
Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
ERR_FAIL_COND_V(is_open(), ERR_ALREADY_IN_USE);
ERR_FAIL_COND_V(ip_type > IP::TYPE_ANY || ip_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
#if defined(__OpenBSD__)
// OpenBSD does not support dual stacking, fallback to IPv4 only.
if (ip_type == IP::TYPE_ANY)
ip_type = IP::TYPE_IPV4;
#endif
int family = ip_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
int protocol = p_sock_type == TYPE_TCP ? IPPROTO_TCP : IPPROTO_UDP;
int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
_sock = socket(family, type, protocol);
if (_sock == SOCK_EMPTY && ip_type == IP::TYPE_ANY) {
// Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
// in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
ip_type = IP::TYPE_IPV4;
family = AF_INET;
_sock = socket(family, type, protocol);
}
ERR_FAIL_COND_V(_sock == SOCK_EMPTY, FAILED);
_ip_type = ip_type;
if (family == AF_INET6) {
// Select IPv4 over IPv6 mapping
set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
}
if (protocol == IPPROTO_UDP && ip_type != IP::TYPE_IPV6) {
// Enable broadcasting for UDP sockets if it's not IPv6 only (IPv6 has no broadcast option).
set_broadcasting_enabled(true);
}
_is_stream = p_sock_type == TYPE_TCP;
return OK;
}
void NetSocketPosix::close() {
if (_sock != SOCK_EMPTY)
SOCK_CLOSE(_sock);
_sock = SOCK_EMPTY;
_ip_type = IP::TYPE_NONE;
_is_stream = false;
}
Error NetSocketPosix::bind(IP_Address p_addr, uint16_t p_port) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER);
sockaddr_storage addr;
size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type);
if (::bind(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) {
close();
ERR_FAIL_V(ERR_UNAVAILABLE);
}
return OK;
}
Error NetSocketPosix::listen(int p_max_pending) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
if (::listen(_sock, p_max_pending) == SOCK_EMPTY) {
close();
ERR_FAIL_V(FAILED);
};
return OK;
}
Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER);
struct sockaddr_storage addr;
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
if (::connect(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) {
NetError err = _get_socket_error();
switch (err) {
// We are already connected
case ERR_NET_IS_CONNECTED:
return OK;
// Still waiting to connect, try again in a while
case ERR_NET_WOULD_BLOCK:
case ERR_NET_IN_PROGRESS:
return ERR_BUSY;
default:
ERR_PRINT("Connection to remote host failed!");
close();
return FAILED;
}
}
return OK;
}
Error NetSocketPosix::poll(PollType p_type, int timeout) const {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
struct pollfd pfd;
pfd.fd = _sock;
pfd.events = POLLIN;
pfd.revents = 0;
switch (p_type) {
case POLL_TYPE_IN:
pfd.events = POLLIN;
break;
case POLL_TYPE_OUT:
pfd.events = POLLOUT;
break;
case POLL_TYPE_IN_OUT:
pfd.events = POLLOUT || POLLIN;
}
int ret = SOCK_POLL(&pfd, 1, timeout);
ERR_FAIL_COND_V(ret < 0, FAILED);
if (ret == 0)
return ERR_BUSY;
return OK;
}
Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
r_read = ::recv(_sock, SOCK_BUF(p_buffer), p_len, 0);
if (r_read < 0) {
NetError err = _get_socket_error();
if (err == ERR_NET_WOULD_BLOCK)
return ERR_BUSY;
return FAILED;
}
return OK;
}
Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
struct sockaddr_storage from;
socklen_t len = sizeof(struct sockaddr_storage);
memset(&from, 0, len);
r_read = ::recvfrom(_sock, SOCK_BUF(p_buffer), p_len, 0, (struct sockaddr *)&from, &len);
if (r_read < 0) {
NetError err = _get_socket_error();
if (err == ERR_NET_WOULD_BLOCK)
return ERR_BUSY;
return FAILED;
}
if (from.ss_family == AF_INET) {
struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
r_ip.set_ipv4((uint8_t *)&sin_from->sin_addr);
r_port = ntohs(sin_from->sin_port);
} else if (from.ss_family == AF_INET6) {
struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
r_ip.set_ipv6((uint8_t *)&s6_from->sin6_addr);
r_port = ntohs(s6_from->sin6_port);
} else {
// Unsupported socket family, should never happen.
ERR_FAIL_V(FAILED);
}
return OK;
}
Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
int flags = 0;
if (_is_stream)
flags = MSG_NOSIGNAL;
r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags);
if (r_sent < 0) {
NetError err = _get_socket_error();
if (err == ERR_NET_WOULD_BLOCK)
return ERR_BUSY;
return FAILED;
}
return OK;
}
Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
struct sockaddr_storage addr;
size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type);
r_sent = ::sendto(_sock, SOCK_CBUF(p_buffer), p_len, 0, (struct sockaddr *)&addr, addr_size);
if (r_sent < 0) {
NetError err = _get_socket_error();
if (err == ERR_NET_WOULD_BLOCK)
return ERR_BUSY;
return FAILED;
}
return OK;
}
void NetSocketPosix::set_broadcasting_enabled(bool p_enabled) {
ERR_FAIL_COND(!is_open());
// IPv6 has no broadcast support.
ERR_FAIL_COND(_ip_type == IP::TYPE_IPV6);
int par = p_enabled ? 1 : 0;
if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, SOCK_CBUF(&par), sizeof(int)) != 0) {
WARN_PRINT("Unable to change broadcast setting");
}
}
void NetSocketPosix::set_blocking_enabled(bool p_enabled) {
ERR_FAIL_COND(!is_open());
int ret = 0;
#if defined(WINDOWS_ENABLED) || defined(NO_FCNTL)
unsigned long par = p_enabled ? 0 : 1;
ret = SOCK_IOCTL(_sock, FIONBIO, &par);
#else
int opts = fcntl(_sock, F_GETFL);
if (p_enabled)
ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
else
ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
#endif
if (ret != 0)
WARN_PRINT("Unable to change non-block mode");
}
void NetSocketPosix::set_ipv6_only_enabled(bool p_enabled) {
ERR_FAIL_COND(!is_open());
// This option is only avaiable in IPv6 sockets.
ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
int par = p_enabled ? 1 : 0;
if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, SOCK_CBUF(&par), sizeof(int)) != 0) {
WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option");
}
}
void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) {
ERR_FAIL_COND(!is_open());
ERR_FAIL_COND(_ip_type != TYPE_TCP);
int par = p_enabled ? 1 : 0;
if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, SOCK_CBUF(&par), sizeof(int)) < 0) {
ERR_PRINT("Unable to set TCP no delay option");
}
}
void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) {
ERR_FAIL_COND(!is_open());
int par = p_enabled ? 1 : 0;
if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, SOCK_CBUF(&par), sizeof(int)) < 0) {
WARN_PRINT("Unable to set socket REUSEADDR option!");
}
}
void NetSocketPosix::set_reuse_port_enabled(bool p_enabled) {
// Windows does not have this option, as it is always ON when setting REUSEADDR.
#ifndef WINDOWS_ENABLED
ERR_FAIL_COND(!is_open());
int par = p_enabled ? 1 : 0;
if (setsockopt(_sock, SOL_SOCKET, SO_REUSEPORT, SOCK_CBUF(&par), sizeof(int)) < 0) {
WARN_PRINT("Unable to set socket REUSEPORT option!");
}
#endif
}
bool NetSocketPosix::is_open() const {
return _sock != SOCK_EMPTY;
}
int NetSocketPosix::get_available_bytes() const {
ERR_FAIL_COND_V(_sock == SOCK_EMPTY, -1);
unsigned long len;
int ret = SOCK_IOCTL(_sock, FIONREAD, &len);
ERR_FAIL_COND_V(ret == -1, 0);
return len;
}
Ref<NetSocket> NetSocketPosix::accept(IP_Address &r_ip, uint16_t &r_port) {
Ref<NetSocket> out;
ERR_FAIL_COND_V(!is_open(), out);
struct sockaddr_storage their_addr;
socklen_t size = sizeof(their_addr);
SOCKET_TYPE fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
ERR_FAIL_COND_V(fd == SOCK_EMPTY, out);
_set_ip_port(r_ip, r_port, &their_addr);
NetSocketPosix *ns = memnew(NetSocketPosix);
ns->_set_socket(fd, _ip_type, _is_stream);
ns->set_blocking_enabled(false);
return Ref<NetSocket>(ns);
}
|