-
-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathsocket.c
5142 lines (4879 loc) · 118 KB
/
socket.c
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include <platform.h>
#include "../asio/asio.h"
#include "../asio/event.h"
#include "ponyassert.h"
#include <stdbool.h>
#include <string.h>
#ifdef PLATFORM_IS_WINDOWS
// Disable warnings about deprecated non-unicode WSA functions.
#pragma warning(disable:4996)
#include "../mem/pool.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <mswsock.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
typedef int SOCKET;
#endif
#ifdef PLATFORM_IS_POSIX_BASED
#include <signal.h>
#endif
// headers for get/setsockopt constants
#ifdef PLATFORM_IS_MACOSX
#include <net/if.h>
#include <net/ndrv.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#endif
#ifdef PLATFORM_IS_LINUX
#include <asm-generic/socket.h>
#include <linux/atm.h>
#include <linux/rds.h>
#if defined(__GLIBC__)
#include <netatalk/at.h>
#include <netax25/ax25.h>
#include <netax25/ax25.h>
#include <netipx/ipx.h>
#include <netrom/netrom.h>
#include <netrose/rose.h>
#endif
#include <linux/dccp.h>
#include <linux/netlink.h>
#include <linux/icmp.h>
#include <linux/tipc.h>
#include <linux/in6.h>
#include <linux/udp.h>
#endif
#ifdef PLATFORM_IS_BSD
#ifdef PLATFORM_IS_FREEBSD
#include <netinet/ip_mroute.h>
#include <netinet/sctp.h>
#elif defined(PLATFORM_IS_OPENBSD)
// Taken from FreeBSD
#define TCP_KEEPCNT 1024
#define TCP_KEEPIDLE 256
#define TCP_KEEPINTVL 512
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#endif
#ifdef PLATFORM_IS_WINDOWS
// TODO
#endif
PONY_EXTERN_C_BEGIN
PONY_API void pony_os_socket_close(int fd);
// This must match the pony NetAddress type in packages/net.
typedef struct
{
pony_type_t* type;
struct sockaddr_storage addr;
} ipaddress_t;
PONY_API socklen_t ponyint_address_length(ipaddress_t* ipaddr)
{
switch(ipaddr->addr.ss_family)
{
case AF_INET:
return sizeof(struct sockaddr_in);
case AF_INET6:
return sizeof(struct sockaddr_in6);
}
return (socklen_t)-1;
}
// Transform "any" addresses into loopback addresses.
static bool map_any_to_loopback(struct sockaddr* addr)
{
switch(addr->sa_family)
{
case AF_INET:
{
struct sockaddr_in* in = (struct sockaddr_in*)addr;
if(in->sin_addr.s_addr == INADDR_ANY)
{
in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
return true;
}
break;
}
case AF_INET6:
{
struct sockaddr_in6* in = (struct sockaddr_in6*)addr;
if(memcmp(&in->sin6_addr, &in6addr_any, sizeof(struct in6_addr)) == 0)
{
memcpy(&in->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr));
return true;
}
}
default: {}
}
return false;
}
static struct addrinfo* os_addrinfo_intern(int family, int socktype,
int proto, const char* host, const char* service, bool passive)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = family;
hints.ai_socktype = socktype;
hints.ai_protocol = proto;
if(passive)
hints.ai_flags |= AI_PASSIVE;
if((host != NULL) && (host[0] == '\0'))
host = NULL;
struct addrinfo *result;
if(getaddrinfo(host, service, &hints, &result) != 0)
return NULL;
return result;
}
#if defined(PLATFORM_IS_MACOSX) || defined(PLATFORM_IS_BSD)
static int set_nonblocking(int s)
{
int flags = fcntl(s, F_GETFL, 0);
return fcntl(s, F_SETFL, flags | O_NONBLOCK);
}
#endif
#ifdef PLATFORM_IS_WINDOWS
#define IOCP_ACCEPT_ADDR_LEN (sizeof(struct sockaddr_storage) + 16)
static LPFN_CONNECTEX g_ConnectEx;
static LPFN_ACCEPTEX g_AcceptEx;
typedef enum
{
IOCP_CONNECT,
IOCP_ACCEPT,
IOCP_SEND,
IOCP_RECV,
IOCP_NOP
} iocp_op_t;
typedef struct iocp_t
{
OVERLAPPED ov;
iocp_op_t op;
int from_len;
asio_event_t* ev;
} iocp_t;
typedef struct iocp_accept_t
{
iocp_t iocp;
SOCKET ns;
char buf[IOCP_ACCEPT_ADDR_LEN * 2];
} iocp_accept_t;
static iocp_t* iocp_create(iocp_op_t op, asio_event_t* ev)
{
iocp_t* iocp = POOL_ALLOC(iocp_t);
memset(&iocp->ov, 0, sizeof(OVERLAPPED));
iocp->op = op;
iocp->ev = ev;
return iocp;
}
static void iocp_destroy(iocp_t* iocp)
{
POOL_FREE(iocp_t, iocp);
}
static iocp_accept_t* iocp_accept_create(SOCKET s, asio_event_t* ev)
{
iocp_accept_t* iocp = POOL_ALLOC(iocp_accept_t);
memset(&iocp->iocp.ov, 0, sizeof(OVERLAPPED));
iocp->iocp.op = IOCP_ACCEPT;
iocp->iocp.ev = ev;
iocp->ns = s;
return iocp;
}
static void iocp_accept_destroy(iocp_accept_t* iocp)
{
POOL_FREE(iocp_accept_t, iocp);
}
static void CALLBACK iocp_callback(DWORD err, DWORD bytes, OVERLAPPED* ov)
{
iocp_t* iocp = (iocp_t*)ov;
switch(iocp->op)
{
case IOCP_CONNECT:
{
if(err == ERROR_SUCCESS)
{
// Update the connect context.
setsockopt((SOCKET)iocp->ev->fd, SOL_SOCKET,
SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
}
// Dispatch a write event.
pony_asio_event_send(iocp->ev, ASIO_WRITE, 0);
iocp_destroy(iocp);
break;
}
case IOCP_ACCEPT:
{
iocp_accept_t* acc = (iocp_accept_t*)iocp;
if(err == ERROR_SUCCESS)
{
// Update the accept context.
SOCKET s = (SOCKET)iocp->ev->fd;
setsockopt(acc->ns, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
(char*)&s, sizeof(SOCKET));
// Dispatch a read event with the new socket as the argument.
pony_asio_event_send(iocp->ev, ASIO_READ, (int)acc->ns);
} else {
// Close the new socket.
pony_os_socket_close((int)acc->ns);
acc->ns = INVALID_SOCKET;
}
iocp_accept_destroy(acc);
break;
}
case IOCP_SEND:
{
if(err == ERROR_SUCCESS)
{
// Dispatch a write event with the number of bytes written.
pony_asio_event_send(iocp->ev, ASIO_WRITE, bytes);
} else {
// Dispatch a write event with zero bytes to indicate a close.
pony_asio_event_send(iocp->ev, ASIO_WRITE, 0);
}
iocp_destroy(iocp);
break;
}
case IOCP_RECV:
{
if(err == ERROR_SUCCESS)
{
// Dispatch a read event with the number of bytes read.
pony_asio_event_send(iocp->ev, ASIO_READ, bytes);
} else {
// Dispatch a read event with zero bytes to indicate a close.
pony_asio_event_send(iocp->ev, ASIO_READ, 0);
}
iocp_destroy(iocp);
break;
}
case IOCP_NOP:
// Don't care, do nothing
iocp_destroy(iocp);
break;
}
}
static bool iocp_connect(asio_event_t* ev, struct addrinfo *p)
{
SOCKET s = (SOCKET)ev->fd;
iocp_t* iocp = iocp_create(IOCP_CONNECT, ev);
if(!g_ConnectEx(s, p->ai_addr, (int)p->ai_addrlen, NULL, 0, NULL, &iocp->ov))
{
if(GetLastError() != ERROR_IO_PENDING)
{
iocp_destroy(iocp);
return false;
}
}
return true;
}
static bool iocp_accept(asio_event_t* ev)
{
SOCKET s = (SOCKET)ev->fd;
WSAPROTOCOL_INFO proto;
if(WSADuplicateSocket(s, GetCurrentProcessId(), &proto) != 0)
return false;
SOCKET ns = WSASocket(proto.iAddressFamily, proto.iSocketType,
proto.iProtocol, NULL, 0, WSA_FLAG_OVERLAPPED);
if((ns == INVALID_SOCKET) ||
!BindIoCompletionCallback((HANDLE)ns, iocp_callback, 0))
{
return false;
}
iocp_accept_t* iocp = iocp_accept_create(ns, ev);
DWORD bytes;
if(!g_AcceptEx(s, ns, iocp->buf, 0, IOCP_ACCEPT_ADDR_LEN,
IOCP_ACCEPT_ADDR_LEN, &bytes, &iocp->iocp.ov))
{
if(GetLastError() != ERROR_IO_PENDING)
{
iocp_accept_destroy(iocp);
return false;
}
}
return true;
}
static bool iocp_recv(asio_event_t* ev, char* data, size_t len)
{
SOCKET s = (SOCKET)ev->fd;
iocp_t* iocp = iocp_create(IOCP_RECV, ev);
DWORD received;
DWORD flags = 0;
WSABUF buf;
buf.buf = data;
buf.len = (u_long)len;
if(WSARecv(s, &buf, 1, &received, &flags, &iocp->ov, NULL) != 0)
{
if(GetLastError() != WSA_IO_PENDING)
{
iocp_destroy(iocp);
return false;
}
}
return true;
}
static bool iocp_sendto(int fd, const char* data, size_t len,
ipaddress_t* ipaddr)
{
socklen_t socklen = ponyint_address_length(ipaddr);
if(socklen == (socklen_t)-1)
return false;
iocp_t* iocp = iocp_create(IOCP_NOP, NULL);
WSABUF buf;
buf.buf = (char*)data;
buf.len = (u_long)len;
if(WSASendTo((SOCKET)fd, &buf, 1, NULL, 0, (struct sockaddr*)&ipaddr->addr,
socklen, &iocp->ov, NULL) != 0)
{
if(GetLastError() != WSA_IO_PENDING)
{
iocp_destroy(iocp);
return false;
}
}
return true;
}
static bool iocp_recvfrom(asio_event_t* ev, char* data, size_t len,
ipaddress_t* ipaddr)
{
SOCKET s = (SOCKET)ev->fd;
iocp_t* iocp = iocp_create(IOCP_RECV, ev);
DWORD flags = 0;
WSABUF buf;
buf.buf = data;
buf.len = (u_long)len;
iocp->from_len = sizeof(ipaddr->addr);
if(WSARecvFrom(s, &buf, 1, NULL, &flags, (struct sockaddr*)&ipaddr->addr,
&iocp->from_len, &iocp->ov, NULL) != 0)
{
if(GetLastError() != WSA_IO_PENDING)
{
iocp_destroy(iocp);
return false;
}
}
return true;
}
#endif
static int socket_from_addrinfo(struct addrinfo* p, bool reuse)
{
#if defined(PLATFORM_IS_LINUX) || defined(PLATFORM_IS_EMSCRIPTEN)
int fd = socket(p->ai_family, p->ai_socktype | SOCK_NONBLOCK,
p->ai_protocol);
#elif defined(PLATFORM_IS_WINDOWS)
UINT_PTR skt = WSASocket(p->ai_family, p->ai_socktype, p->ai_protocol, NULL,
0, WSA_FLAG_OVERLAPPED);
pony_assert((skt == INVALID_SOCKET) || ((skt >> 31) == 0));
int fd = (int)skt;
#else
int fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
#endif
if(fd < 0)
return -1;
int r = 0;
if(reuse)
{
int reuseaddr = 1;
r |= setsockopt((SOCKET)fd, SOL_SOCKET, SO_REUSEADDR,
(const char*)&reuseaddr, sizeof(int));
}
#if defined(PLATFORM_IS_MACOSX) || defined(PLATFORM_IS_BSD)
r |= set_nonblocking(fd);
#endif
#ifdef PLATFORM_IS_WINDOWS
if(!BindIoCompletionCallback((HANDLE)(UINT_PTR)fd, iocp_callback, 0))
r = 1;
#endif
if(r == 0)
return fd;
pony_os_socket_close(fd);
return -1;
}
static asio_event_t* os_listen(pony_actor_t* owner, int fd,
struct addrinfo *p, int proto)
{
if(bind((SOCKET)fd, p->ai_addr, (int)p->ai_addrlen) != 0)
{
pony_os_socket_close(fd);
return NULL;
}
if(p->ai_socktype == SOCK_STREAM)
{
if(listen((SOCKET)fd, SOMAXCONN) != 0)
{
pony_os_socket_close(fd);
return NULL;
}
}
// Create an event and subscribe it.
asio_event_t* ev = pony_asio_event_create(owner, fd, ASIO_READ, 0, true);
#ifdef PLATFORM_IS_WINDOWS
// Start accept for TCP connections, but not for UDP.
if(proto == IPPROTO_TCP)
{
if(!iocp_accept(ev))
{
pony_asio_event_unsubscribe(ev);
pony_os_socket_close(fd);
return NULL;
}
}
#else
(void)proto;
#endif
return ev;
}
static bool os_connect(pony_actor_t* owner, int fd, struct addrinfo *p,
const char* from, uint32_t asio_flags)
{
map_any_to_loopback(p->ai_addr);
bool need_bind = (from != NULL) && (from[0] != '\0');
if(need_bind)
{
struct addrinfo* result = os_addrinfo_intern(p->ai_family, 0, 0, from,
NULL, false);
if(result == NULL)
return false;
struct addrinfo* lp = result;
bool bound = false;
while(lp != NULL)
{
if(bind((SOCKET)fd, lp->ai_addr, (int)lp->ai_addrlen) == 0)
{
bound = true;
break;
}
lp = lp->ai_next;
}
freeaddrinfo(result);
if(!bound)
{
pony_os_socket_close(fd);
return false;
}
}
#ifdef PLATFORM_IS_WINDOWS
if(!need_bind)
{
// ConnectEx requires bind.
struct sockaddr_storage addr = {0};
addr.ss_family = p->ai_family;
if(bind((SOCKET)fd, (struct sockaddr*)&addr, (int)p->ai_addrlen) != 0)
{
pony_os_socket_close(fd);
return false;
}
}
// Create an event and subscribe it.
asio_event_t* ev = pony_asio_event_create(owner, fd, asio_flags, 0, true);
if(!iocp_connect(ev, p))
{
pony_asio_event_unsubscribe(ev);
pony_os_socket_close(fd);
return false;
}
#else
int r = connect(fd, p->ai_addr, (int)p->ai_addrlen);
if((r != 0) && (errno != EINPROGRESS))
{
pony_os_socket_close(fd);
return false;
}
// Create an event and subscribe it.
pony_asio_event_create(owner, fd, asio_flags, 0, true);
#endif
return true;
}
/**
* This finds an address to listen on and returns either an asio_event_t or
* null.
*/
static asio_event_t* os_socket_listen(pony_actor_t* owner, const char* host,
const char* service, int family, int socktype, int proto)
{
struct addrinfo* result = os_addrinfo_intern(family, socktype, proto, host,
service, true);
if(result == NULL)
return NULL;
struct addrinfo* p = result;
while(p != NULL)
{
int fd = socket_from_addrinfo(p, true);
if(fd != -1)
{
asio_event_t* ev = os_listen(owner, fd, p, proto);
freeaddrinfo(result);
return ev;
}
p = p->ai_next;
}
freeaddrinfo(result);
return NULL;
}
/**
* This starts Happy Eyeballs and returns * the number of connection attempts
* in-flight, which may be 0.
*/
static int os_socket_connect(pony_actor_t* owner, const char* host,
const char* service, const char* from, int family, int socktype, int proto,
uint32_t asio_flags)
{
bool reuse = (from == NULL) || (from[0] != '\0');
struct addrinfo* result = os_addrinfo_intern(family, socktype, proto, host,
service, false);
if(result == NULL)
return 0;
struct addrinfo* p = result;
int count = 0;
while(p != NULL)
{
int fd = socket_from_addrinfo(p, reuse);
if(fd != -1)
{
if(os_connect(owner, fd, p, from, asio_flags))
count++;
}
p = p->ai_next;
}
freeaddrinfo(result);
return count;
}
PONY_API asio_event_t* pony_os_listen_tcp(pony_actor_t* owner, const char* host,
const char* service)
{
return os_socket_listen(owner, host, service, AF_UNSPEC, SOCK_STREAM,
IPPROTO_TCP);
}
PONY_API asio_event_t* pony_os_listen_tcp4(pony_actor_t* owner,
const char* host, const char* service)
{
return os_socket_listen(owner, host, service, AF_INET, SOCK_STREAM,
IPPROTO_TCP);
}
PONY_API asio_event_t* pony_os_listen_tcp6(pony_actor_t* owner,
const char* host, const char* service)
{
return os_socket_listen(owner, host, service, AF_INET6, SOCK_STREAM,
IPPROTO_TCP);
}
PONY_API asio_event_t* pony_os_listen_udp(pony_actor_t* owner, const char* host,
const char* service)
{
return os_socket_listen(owner, host, service, AF_UNSPEC, SOCK_DGRAM,
IPPROTO_UDP);
}
PONY_API asio_event_t* pony_os_listen_udp4(pony_actor_t* owner,
const char* host, const char* service)
{
return os_socket_listen(owner, host, service, AF_INET, SOCK_DGRAM,
IPPROTO_UDP);
}
PONY_API asio_event_t* pony_os_listen_udp6(pony_actor_t* owner,
const char* host, const char* service)
{
return os_socket_listen(owner, host, service, AF_INET6, SOCK_DGRAM,
IPPROTO_UDP);
}
PONY_API int pony_os_connect_tcp(pony_actor_t* owner, const char* host,
const char* service, const char* from, uint32_t asio_flags)
{
return os_socket_connect(owner, host, service, from, AF_UNSPEC, SOCK_STREAM,
IPPROTO_TCP, asio_flags);
}
PONY_API int pony_os_connect_tcp4(pony_actor_t* owner, const char* host,
const char* service, const char* from, uint32_t asio_flags)
{
return os_socket_connect(owner, host, service, from, AF_INET, SOCK_STREAM,
IPPROTO_TCP, asio_flags);
}
PONY_API int pony_os_connect_tcp6(pony_actor_t* owner, const char* host,
const char* service, const char* from, uint32_t asio_flags)
{
return os_socket_connect(owner, host, service, from, AF_INET6, SOCK_STREAM,
IPPROTO_TCP, asio_flags);
}
PONY_API int pony_os_accept(asio_event_t* ev)
{
#if defined(PLATFORM_IS_WINDOWS)
// Queue an IOCP accept and return an INVALID_SOCKET.
SOCKET ns = INVALID_SOCKET;
iocp_accept(ev);
#elif defined(PLATFORM_IS_LINUX) || defined(PLATFORM_IS_EMSCRIPTEN)
int ns = accept4(ev->fd, NULL, NULL, SOCK_NONBLOCK);
if(ns == -1 && (errno == EWOULDBLOCK || errno == EAGAIN))
ns = 0;
#else
int ns = accept(ev->fd, NULL, NULL);
if(ns != -1)
set_nonblocking(ns);
else if(errno == EWOULDBLOCK || errno == EAGAIN)
ns = 0;
#endif
return (int)ns;
}
// Check this when a connection gets its first writeable event.
// API deprecated: use TCPConnection._is_sock_connected or UDPSocket.get_so_error
PONY_API bool pony_os_connected(int fd)
{
int val = 0;
socklen_t len = sizeof(int);
if(getsockopt((SOCKET)fd, SOL_SOCKET, SO_ERROR, (char*)&val, &len) == -1)
return false;
return val == 0;
}
static int address_family(int length)
{
switch(length)
{
case 4:
return AF_INET;
case 16:
return AF_INET6;
}
return -1;
}
PONY_API bool pony_os_nameinfo(ipaddress_t* ipaddr, char** rhost, char** rserv,
bool reversedns, bool servicename)
{
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
socklen_t len = ponyint_address_length(ipaddr);
if(len == (socklen_t)-1)
return false;
int flags = 0;
if(!reversedns)
flags |= NI_NUMERICHOST;
if(!servicename)
flags |= NI_NUMERICSERV;
int r = getnameinfo((struct sockaddr*)&ipaddr->addr, len, host, NI_MAXHOST,
serv, NI_MAXSERV, flags);
if(r != 0)
return false;
pony_ctx_t* ctx = pony_ctx();
size_t hostlen = strlen(host);
*rhost = (char*)pony_alloc(ctx, hostlen + 1);
memcpy(*rhost, host, hostlen + 1);
size_t servlen = strlen(serv);
*rserv = (char*)pony_alloc(ctx, servlen + 1);
memcpy(*rserv, serv, servlen + 1);
return true;
}
PONY_API struct addrinfo* pony_os_addrinfo(int family, const char* host,
const char* service)
{
switch(family)
{
case 0: family = AF_UNSPEC; break;
case 1: family = AF_INET; break;
case 2: family = AF_INET6; break;
default: return NULL;
}
return os_addrinfo_intern(family, 0, 0, host, service, true);
}
PONY_API void pony_os_getaddr(struct addrinfo* addr, ipaddress_t* ipaddr)
{
memcpy(&ipaddr->addr, addr->ai_addr, addr->ai_addrlen);
map_any_to_loopback((struct sockaddr*)&ipaddr->addr);
}
PONY_API struct addrinfo* pony_os_nextaddr(struct addrinfo* addr)
{
return addr->ai_next;
}
PONY_API char* pony_os_ip_string(void* src, int len)
{
char dst[INET6_ADDRSTRLEN];
int family = address_family(len);
if(family == -1)
return NULL;
if(inet_ntop(family, src, dst, INET6_ADDRSTRLEN))
return NULL;
size_t dstlen = strlen(dst);
char* result = (char*)pony_alloc(pony_ctx(), dstlen + 1);
memcpy(result, dst, dstlen + 1);
return result;
}
PONY_API bool pony_os_ipv4(ipaddress_t* ipaddr)
{
return ipaddr->addr.ss_family == AF_INET;
}
PONY_API bool pony_os_ipv6(ipaddress_t* ipaddr)
{
return ipaddr->addr.ss_family == AF_INET6;
}
PONY_API bool pony_os_sockname(int fd, ipaddress_t* ipaddr)
{
socklen_t len = sizeof(struct sockaddr_storage);
if(getsockname((SOCKET)fd, (struct sockaddr*)&ipaddr->addr, &len) != 0)
return false;
map_any_to_loopback((struct sockaddr*)&ipaddr->addr);
return true;
}
PONY_API bool pony_os_peername(int fd, ipaddress_t* ipaddr)
{
socklen_t len = sizeof(struct sockaddr_storage);
if(getpeername((SOCKET)fd, (struct sockaddr*)&ipaddr->addr, &len) != 0)
return false;
map_any_to_loopback((struct sockaddr*)&ipaddr->addr);
return true;
}
PONY_API bool pony_os_host_ip4(const char* host)
{
struct in_addr addr;
return inet_pton(AF_INET, host, &addr) == 1;
}
PONY_API bool pony_os_host_ip6(const char* host)
{
struct in6_addr addr;
return inet_pton(AF_INET6, host, &addr) == 1;
}
#ifdef PLATFORM_IS_WINDOWS
PONY_API size_t pony_os_writev(asio_event_t* ev, LPWSABUF wsa, int wsacnt)
{
SOCKET s = (SOCKET)ev->fd;
iocp_t* iocp = iocp_create(IOCP_SEND, ev);
DWORD sent;
if(WSASend(s, wsa, wsacnt, &sent, 0, &iocp->ov, NULL) != 0)
{
switch (GetLastError())
{
case WSA_IO_PENDING:
return wsacnt;
case WSAEWOULDBLOCK :
return 0;
default:
iocp_destroy(iocp);
pony_error();
return 0;
}
}
return wsacnt;
}
#else
PONY_API size_t pony_os_writev(asio_event_t* ev, const struct iovec *iov, int iovcnt)
{
ssize_t sent = writev(ev->fd, iov, iovcnt);
if(sent < 0)
{
if(errno == EWOULDBLOCK || errno == EAGAIN)
return 0;
pony_error();
}
return (size_t)sent;
}
#endif
PONY_API size_t pony_os_send(asio_event_t* ev, const char* buf, size_t len)
{
#ifdef PLATFORM_IS_WINDOWS
SOCKET s = (SOCKET)ev->fd;
iocp_t* iocp = iocp_create(IOCP_SEND, ev);
DWORD sent;
WSABUF b;
b.buf = (char*)buf;
b.len = (u_long)len;
if(WSASend(s, &b, 1, &sent, 0, &iocp->ov, NULL) != 0)
{
switch (GetLastError())
{
case WSA_IO_PENDING:
return len;
case WSAEWOULDBLOCK :
return 0;
default:
iocp_destroy(iocp);
pony_error();
return 0;
}
}
return sent;
#else
ssize_t sent = send(ev->fd, buf, len, 0);
if(sent < 0)
{
if(errno == EWOULDBLOCK || errno == EAGAIN)
return 0;
pony_error();
}
return (size_t)sent;
#endif
}
PONY_API size_t pony_os_recv(asio_event_t* ev, char* buf, size_t len)
{