Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/unix/net_socket_unix.cpp
9903 views
1
/**************************************************************************/
2
/* net_socket_unix.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
// Some proprietary Unix-derived platforms don't expose Unix sockets
32
// so this allows skipping this file to reimplement this API differently.
33
#if defined(UNIX_ENABLED) && !defined(UNIX_SOCKET_UNAVAILABLE)
34
35
#include "net_socket_unix.h"
36
37
#include <fcntl.h>
38
#include <netdb.h>
39
#include <netinet/in.h>
40
#include <netinet/tcp.h>
41
#include <poll.h>
42
#include <sys/ioctl.h>
43
#include <sys/socket.h>
44
#include <sys/types.h>
45
#include <unistd.h>
46
#include <cerrno>
47
#include <cstdio>
48
#include <cstdlib>
49
50
#ifdef WEB_ENABLED
51
#include <arpa/inet.h>
52
#endif
53
54
// BSD calls this flag IPV6_JOIN_GROUP
55
#if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP)
56
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
57
#endif
58
#if !defined(IPV6_DROP_MEMBERSHIP) && defined(IPV6_LEAVE_GROUP)
59
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
60
#endif
61
62
size_t NetSocketUnix::_set_addr_storage(struct sockaddr_storage *p_addr, const IPAddress &p_ip, uint16_t p_port, IP::Type p_ip_type) {
63
memset(p_addr, 0, sizeof(struct sockaddr_storage));
64
if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket.
65
66
// IPv6 only socket with IPv4 address.
67
ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
68
69
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
70
addr6->sin6_family = AF_INET6;
71
addr6->sin6_port = htons(p_port);
72
if (p_ip.is_valid()) {
73
memcpy(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
74
} else {
75
addr6->sin6_addr = in6addr_any;
76
}
77
return sizeof(sockaddr_in6);
78
} else { // IPv4 socket.
79
80
// IPv4 socket with IPv6 address.
81
ERR_FAIL_COND_V(!p_ip.is_wildcard() && !p_ip.is_ipv4(), 0);
82
83
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
84
addr4->sin_family = AF_INET;
85
addr4->sin_port = htons(p_port); // Short, network byte order.
86
87
if (p_ip.is_valid()) {
88
memcpy(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4);
89
} else {
90
addr4->sin_addr.s_addr = INADDR_ANY;
91
}
92
93
return sizeof(sockaddr_in);
94
}
95
}
96
97
void NetSocketUnix::_set_ip_port(struct sockaddr_storage *p_addr, IPAddress *r_ip, uint16_t *r_port) {
98
if (p_addr->ss_family == AF_INET) {
99
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
100
if (r_ip) {
101
r_ip->set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
102
}
103
if (r_port) {
104
*r_port = ntohs(addr4->sin_port);
105
}
106
} else if (p_addr->ss_family == AF_INET6) {
107
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
108
if (r_ip) {
109
r_ip->set_ipv6(addr6->sin6_addr.s6_addr);
110
}
111
if (r_port) {
112
*r_port = ntohs(addr6->sin6_port);
113
}
114
}
115
}
116
117
NetSocket *NetSocketUnix::_create_func() {
118
return memnew(NetSocketUnix);
119
}
120
121
void NetSocketUnix::make_default() {
122
_create = _create_func;
123
}
124
125
void NetSocketUnix::cleanup() {
126
}
127
128
NetSocketUnix::NetSocketUnix() {
129
}
130
131
NetSocketUnix::~NetSocketUnix() {
132
close();
133
}
134
135
// Silence a warning reported in GH-27594.
136
// EAGAIN and EWOULDBLOCK have the same value on most platforms, but it's not guaranteed.
137
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wlogical-op")
138
139
NetSocketUnix::NetError NetSocketUnix::_get_socket_error() const {
140
if (errno == EISCONN) {
141
return ERR_NET_IS_CONNECTED;
142
}
143
if (errno == EINPROGRESS || errno == EALREADY) {
144
return ERR_NET_IN_PROGRESS;
145
}
146
if (errno == EAGAIN || errno == EWOULDBLOCK) {
147
return ERR_NET_WOULD_BLOCK;
148
}
149
if (errno == EADDRINUSE || errno == EINVAL || errno == EADDRNOTAVAIL) {
150
return ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE;
151
}
152
if (errno == EACCES) {
153
return ERR_NET_UNAUTHORIZED;
154
}
155
if (errno == ENOBUFS) {
156
return ERR_NET_BUFFER_TOO_SMALL;
157
}
158
print_verbose("Socket error: " + itos(errno) + ".");
159
return ERR_NET_OTHER;
160
}
161
162
GODOT_GCC_WARNING_POP
163
164
bool NetSocketUnix::_can_use_ip(const IPAddress &p_ip, const bool p_for_bind) const {
165
if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) {
166
return false;
167
} else if (!p_for_bind && !p_ip.is_valid()) {
168
return false;
169
}
170
// Check if socket support this IP type.
171
IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
172
return !(_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type);
173
}
174
175
_FORCE_INLINE_ Error NetSocketUnix::_change_multicast_group(IPAddress p_ip, String p_if_name, bool p_add) {
176
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
177
ERR_FAIL_COND_V(!_can_use_ip(p_ip, false), ERR_INVALID_PARAMETER);
178
179
// Need to force level and af_family to IP(v4) when using dual stacking and provided multicast group is IPv4.
180
IP::Type type = _ip_type == IP::TYPE_ANY && p_ip.is_ipv4() ? IP::TYPE_IPV4 : _ip_type;
181
// This needs to be the proper level for the multicast group, no matter if the socket is dual stacking.
182
int level = type == IP::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
183
int ret = -1;
184
185
IPAddress if_ip;
186
uint32_t if_v6id = 0;
187
HashMap<String, IP::Interface_Info> if_info;
188
IP::get_singleton()->get_local_interfaces(&if_info);
189
for (KeyValue<String, IP::Interface_Info> &E : if_info) {
190
IP::Interface_Info &c = E.value;
191
if (c.name != p_if_name) {
192
continue;
193
}
194
195
if_v6id = (uint32_t)c.index.to_int();
196
if (type == IP::TYPE_IPV6) {
197
break; // IPv6 uses index.
198
}
199
200
for (const IPAddress &F : c.ip_addresses) {
201
if (!F.is_ipv4()) {
202
continue; // Wrong IP type.
203
}
204
if_ip = F;
205
break;
206
}
207
break;
208
}
209
210
if (level == IPPROTO_IP) {
211
ERR_FAIL_COND_V(!if_ip.is_valid(), ERR_INVALID_PARAMETER);
212
struct ip_mreq greq;
213
int sock_opt = p_add ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
214
memcpy(&greq.imr_multiaddr, p_ip.get_ipv4(), 4);
215
memcpy(&greq.imr_interface, if_ip.get_ipv4(), 4);
216
ret = setsockopt(_sock, level, sock_opt, (const char *)&greq, sizeof(greq));
217
} else {
218
struct ipv6_mreq greq;
219
int sock_opt = p_add ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
220
memcpy(&greq.ipv6mr_multiaddr, p_ip.get_ipv6(), 16);
221
greq.ipv6mr_interface = if_v6id;
222
ret = setsockopt(_sock, level, sock_opt, (const char *)&greq, sizeof(greq));
223
}
224
ERR_FAIL_COND_V(ret != 0, FAILED);
225
226
return OK;
227
}
228
229
void NetSocketUnix::_set_socket(int p_sock, IP::Type p_ip_type, bool p_is_stream) {
230
_sock = p_sock;
231
_ip_type = p_ip_type;
232
_is_stream = p_is_stream;
233
// Disable descriptor sharing with subprocesses.
234
_set_close_exec_enabled(true);
235
}
236
237
void NetSocketUnix::_set_close_exec_enabled(bool p_enabled) {
238
// Enable close on exec to avoid sharing with subprocesses. Off by default on Windows.
239
int opts = fcntl(_sock, F_GETFD);
240
fcntl(_sock, F_SETFD, opts | FD_CLOEXEC);
241
}
242
243
Error NetSocketUnix::open(Type p_sock_type, IP::Type &ip_type) {
244
ERR_FAIL_COND_V(is_open(), ERR_ALREADY_IN_USE);
245
ERR_FAIL_COND_V(ip_type > IP::TYPE_ANY || ip_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
246
247
#if defined(__OpenBSD__)
248
// OpenBSD does not support dual stacking, fallback to IPv4 only.
249
if (ip_type == IP::TYPE_ANY) {
250
ip_type = IP::TYPE_IPV4;
251
}
252
#endif
253
254
int family = ip_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
255
int protocol = p_sock_type == TYPE_TCP ? IPPROTO_TCP : IPPROTO_UDP;
256
int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM;
257
_sock = socket(family, type, protocol);
258
259
if (_sock == -1 && ip_type == IP::TYPE_ANY) {
260
// Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
261
// in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
262
ip_type = IP::TYPE_IPV4;
263
family = AF_INET;
264
_sock = socket(family, type, protocol);
265
}
266
267
ERR_FAIL_COND_V(_sock == -1, FAILED);
268
_ip_type = ip_type;
269
270
if (family == AF_INET6) {
271
// Select IPv4 over IPv6 mapping.
272
set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
273
}
274
275
if (protocol == IPPROTO_UDP) {
276
// Make sure to disable broadcasting for UDP sockets.
277
// Depending on the OS, this option might or might not be enabled by default. Let's normalize it.
278
set_broadcasting_enabled(false);
279
}
280
281
_is_stream = p_sock_type == TYPE_TCP;
282
283
// Disable descriptor sharing with subprocesses.
284
_set_close_exec_enabled(true);
285
286
#if defined(SO_NOSIGPIPE)
287
// Disable SIGPIPE (should only be relevant to stream sockets, but seems to affect UDP too on iOS).
288
int par = 1;
289
if (setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, &par, sizeof(int)) != 0) {
290
print_verbose("Unable to turn off SIGPIPE on socket.");
291
}
292
#endif
293
return OK;
294
}
295
296
void NetSocketUnix::close() {
297
if (_sock != -1) {
298
::close(_sock);
299
}
300
301
_sock = -1;
302
_ip_type = IP::TYPE_NONE;
303
_is_stream = false;
304
}
305
306
Error NetSocketUnix::bind(IPAddress p_addr, uint16_t p_port) {
307
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
308
ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER);
309
310
sockaddr_storage addr;
311
size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type);
312
313
if (::bind(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
314
NetError err = _get_socket_error();
315
print_verbose("Failed to bind socket. Error: " + itos(err) + ".");
316
close();
317
return ERR_UNAVAILABLE;
318
}
319
320
return OK;
321
}
322
323
Error NetSocketUnix::listen(int p_max_pending) {
324
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
325
326
if (::listen(_sock, p_max_pending) != 0) {
327
_get_socket_error();
328
print_verbose("Failed to listen from socket.");
329
close();
330
return FAILED;
331
}
332
333
return OK;
334
}
335
336
Error NetSocketUnix::connect_to_host(IPAddress p_host, uint16_t p_port) {
337
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
338
ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER);
339
340
struct sockaddr_storage addr;
341
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
342
343
if (::connect(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
344
NetError err = _get_socket_error();
345
346
switch (err) {
347
// We are already connected.
348
case ERR_NET_IS_CONNECTED:
349
return OK;
350
// Still waiting to connect, try again in a while.
351
case ERR_NET_WOULD_BLOCK:
352
case ERR_NET_IN_PROGRESS:
353
return ERR_BUSY;
354
default:
355
print_verbose("Connection to remote host failed.");
356
close();
357
return FAILED;
358
}
359
}
360
361
return OK;
362
}
363
364
Error NetSocketUnix::poll(PollType p_type, int p_timeout) const {
365
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
366
367
struct pollfd pfd;
368
pfd.fd = _sock;
369
pfd.events = POLLIN;
370
pfd.revents = 0;
371
372
switch (p_type) {
373
case POLL_TYPE_IN:
374
pfd.events = POLLIN;
375
break;
376
case POLL_TYPE_OUT:
377
pfd.events = POLLOUT;
378
break;
379
case POLL_TYPE_IN_OUT:
380
pfd.events = POLLOUT | POLLIN;
381
}
382
383
int ret = ::poll(&pfd, 1, p_timeout);
384
385
if (ret < 0 || pfd.revents & POLLERR) {
386
_get_socket_error();
387
print_verbose("Error when polling socket.");
388
return FAILED;
389
}
390
391
if (ret == 0) {
392
return ERR_BUSY;
393
}
394
395
return OK;
396
}
397
398
Error NetSocketUnix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
399
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
400
401
r_read = ::recv(_sock, p_buffer, p_len, 0);
402
403
if (r_read < 0) {
404
NetError err = _get_socket_error();
405
if (err == ERR_NET_WOULD_BLOCK) {
406
return ERR_BUSY;
407
}
408
409
if (err == ERR_NET_BUFFER_TOO_SMALL) {
410
return ERR_OUT_OF_MEMORY;
411
}
412
413
return FAILED;
414
}
415
416
return OK;
417
}
418
419
Error NetSocketUnix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek) {
420
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
421
422
struct sockaddr_storage from;
423
socklen_t len = sizeof(struct sockaddr_storage);
424
memset(&from, 0, len);
425
426
r_read = ::recvfrom(_sock, p_buffer, p_len, p_peek ? MSG_PEEK : 0, (struct sockaddr *)&from, &len);
427
428
if (r_read < 0) {
429
NetError err = _get_socket_error();
430
if (err == ERR_NET_WOULD_BLOCK) {
431
return ERR_BUSY;
432
}
433
434
if (err == ERR_NET_BUFFER_TOO_SMALL) {
435
return ERR_OUT_OF_MEMORY;
436
}
437
438
return FAILED;
439
}
440
441
if (from.ss_family == AF_INET) {
442
struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
443
r_ip.set_ipv4((uint8_t *)&sin_from->sin_addr);
444
r_port = ntohs(sin_from->sin_port);
445
} else if (from.ss_family == AF_INET6) {
446
struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
447
r_ip.set_ipv6((uint8_t *)&s6_from->sin6_addr);
448
r_port = ntohs(s6_from->sin6_port);
449
} else {
450
// Unsupported socket family, should never happen.
451
ERR_FAIL_V(FAILED);
452
}
453
454
return OK;
455
}
456
457
Error NetSocketUnix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
458
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
459
460
int flags = 0;
461
#ifdef MSG_NOSIGNAL
462
if (_is_stream) {
463
flags = MSG_NOSIGNAL;
464
}
465
#endif
466
r_sent = ::send(_sock, p_buffer, p_len, flags);
467
468
if (r_sent < 0) {
469
NetError err = _get_socket_error();
470
if (err == ERR_NET_WOULD_BLOCK) {
471
return ERR_BUSY;
472
}
473
if (err == ERR_NET_BUFFER_TOO_SMALL) {
474
return ERR_OUT_OF_MEMORY;
475
}
476
477
return FAILED;
478
}
479
480
return OK;
481
}
482
483
Error NetSocketUnix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
484
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
485
486
struct sockaddr_storage addr;
487
size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type);
488
r_sent = ::sendto(_sock, p_buffer, p_len, 0, (struct sockaddr *)&addr, addr_size);
489
490
if (r_sent < 0) {
491
NetError err = _get_socket_error();
492
if (err == ERR_NET_WOULD_BLOCK) {
493
return ERR_BUSY;
494
}
495
if (err == ERR_NET_BUFFER_TOO_SMALL) {
496
return ERR_OUT_OF_MEMORY;
497
}
498
499
return FAILED;
500
}
501
502
return OK;
503
}
504
505
Error NetSocketUnix::set_broadcasting_enabled(bool p_enabled) {
506
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
507
// IPv6 has no broadcast support.
508
if (_ip_type == IP::TYPE_IPV6) {
509
return ERR_UNAVAILABLE;
510
}
511
512
int par = p_enabled ? 1 : 0;
513
if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, &par, sizeof(int)) != 0) {
514
WARN_PRINT("Unable to change broadcast setting.");
515
return FAILED;
516
}
517
return OK;
518
}
519
520
void NetSocketUnix::set_blocking_enabled(bool p_enabled) {
521
ERR_FAIL_COND(!is_open());
522
523
int ret = 0;
524
int opts = fcntl(_sock, F_GETFL);
525
if (p_enabled) {
526
ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK);
527
} else {
528
ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK);
529
}
530
531
if (ret != 0) {
532
WARN_PRINT("Unable to change non-block mode.");
533
}
534
}
535
536
void NetSocketUnix::set_ipv6_only_enabled(bool p_enabled) {
537
ERR_FAIL_COND(!is_open());
538
// This option is only available in IPv6 sockets.
539
ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4);
540
541
int par = p_enabled ? 1 : 0;
542
if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, &par, sizeof(int)) != 0) {
543
WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option.");
544
}
545
}
546
547
void NetSocketUnix::set_tcp_no_delay_enabled(bool p_enabled) {
548
ERR_FAIL_COND(!is_open());
549
ERR_FAIL_COND(!_is_stream); // Not TCP.
550
551
int par = p_enabled ? 1 : 0;
552
if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &par, sizeof(int)) < 0) {
553
WARN_PRINT("Unable to set TCP no delay option.");
554
}
555
}
556
557
void NetSocketUnix::set_reuse_address_enabled(bool p_enabled) {
558
ERR_FAIL_COND(!is_open());
559
560
int par = p_enabled ? 1 : 0;
561
if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &par, sizeof(int)) < 0) {
562
WARN_PRINT("Unable to set socket REUSEADDR option.");
563
}
564
}
565
566
bool NetSocketUnix::is_open() const {
567
return _sock != -1;
568
}
569
570
int NetSocketUnix::get_available_bytes() const {
571
ERR_FAIL_COND_V(!is_open(), -1);
572
573
int len;
574
int ret = ioctl(_sock, FIONREAD, &len);
575
if (ret == -1) {
576
_get_socket_error();
577
print_verbose("Error when checking available bytes on socket.");
578
return -1;
579
}
580
return len;
581
}
582
583
Error NetSocketUnix::get_socket_address(IPAddress *r_ip, uint16_t *r_port) const {
584
ERR_FAIL_COND_V(!is_open(), FAILED);
585
586
struct sockaddr_storage saddr;
587
socklen_t len = sizeof(saddr);
588
if (getsockname(_sock, (struct sockaddr *)&saddr, &len) != 0) {
589
_get_socket_error();
590
print_verbose("Error when reading local socket address.");
591
return FAILED;
592
}
593
_set_ip_port(&saddr, r_ip, r_port);
594
return OK;
595
}
596
597
Ref<NetSocket> NetSocketUnix::accept(IPAddress &r_ip, uint16_t &r_port) {
598
Ref<NetSocket> out;
599
ERR_FAIL_COND_V(!is_open(), out);
600
601
struct sockaddr_storage their_addr;
602
socklen_t size = sizeof(their_addr);
603
int fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size);
604
if (fd == -1) {
605
_get_socket_error();
606
print_verbose("Error when accepting socket connection.");
607
return out;
608
}
609
610
_set_ip_port(&their_addr, &r_ip, &r_port);
611
612
NetSocketUnix *ns = memnew(NetSocketUnix);
613
ns->_set_socket(fd, _ip_type, _is_stream);
614
ns->set_blocking_enabled(false);
615
return Ref<NetSocket>(ns);
616
}
617
618
Error NetSocketUnix::join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) {
619
return _change_multicast_group(p_multi_address, p_if_name, true);
620
}
621
622
Error NetSocketUnix::leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) {
623
return _change_multicast_group(p_multi_address, p_if_name, false);
624
}
625
626
#endif // UNIX_ENABLED && !UNIX_SOCKET_UNAVAILABLE
627
628