-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
pal_networking.c
3204 lines (2696 loc) · 95.1 KB
/
pal_networking.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_config.h"
#include "pal_networking.h"
#include "pal_safecrt.h"
#include "pal_utilities.h"
#include <pal_networking_common.h>
#include <fcntl.h>
#include <stdlib.h>
#include <limits.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/time.h>
#if HAVE_EPOLL
#include <sys/epoll.h>
#elif HAVE_KQUEUE
#include <sys/types.h>
#include <sys/event.h>
#elif HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
#if HAVE_SYS_PROCINFO_H
#include <sys/proc_info.h>
#include <libproc.h>
#endif
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#if HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#include <sys/un.h>
#if defined(__APPLE__) && __APPLE__
#include <sys/socketvar.h>
#endif
#if !HAVE_GETDOMAINNAME && HAVE_UTSNAME_DOMAINNAME
#include <sys/utsname.h>
#include <stdio.h>
#endif
#include <unistd.h>
#include <pwd.h>
#if HAVE_SENDFILE_4
#include <sys/sendfile.h>
#elif HAVE_SENDFILE_6
#include <sys/uio.h>
#endif
#if HAVE_GETIFADDRS
#include <ifaddrs.h>
#endif
#if HAVE_LINUX_CAN_H
#include <linux/can.h>
#endif
#if HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#if HAVE_KQUEUE
#if KEVENT_HAS_VOID_UDATA
static void* GetKeventUdata(uintptr_t udata)
{
return (void*)udata;
}
static uintptr_t GetSocketEventData(void* udata)
{
return (uintptr_t)udata;
}
#else
static intptr_t GetKeventUdata(uintptr_t udata)
{
return (intptr_t)udata;
}
static uintptr_t GetSocketEventData(intptr_t udata)
{
return (uintptr_t)udata;
}
#endif
#if KEVENT_REQUIRES_INT_PARAMS
static int GetKeventNchanges(int nchanges)
{
return nchanges;
}
static int16_t GetKeventFilter(int16_t filter)
{
return filter;
}
static uint16_t GetKeventFlags(uint16_t flags)
{
return flags;
}
#else
static size_t GetKeventNchanges(int nchanges)
{
return (size_t)nchanges;
}
static int16_t GetKeventFilter(uint32_t filter)
{
return (int16_t)filter;
}
static uint16_t GetKeventFlags(uint32_t flags)
{
return (uint16_t)flags;
}
#endif
#endif
#if !HAVE_IN_PKTINFO
// On platforms, such as FreeBSD, where in_pktinfo
// is not available, fallback to custom definition
// with required members.
struct in_pktinfo
{
struct in_addr ipi_addr;
};
#define IP_PKTINFO IP_RECVDSTADDR
#endif
#if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP)
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif
#if !defined(IPV6_DROP_MEMBERSHIP) && defined(IPV6_LEAVE_GROUP)
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
#endif
enum
{
#if defined(__APPLE__) && __APPLE__
LINGER_OPTION_NAME = SO_LINGER_SEC
#else
LINGER_OPTION_NAME = SO_LINGER,
#endif
};
enum
{
INET6_ADDRSTRLEN_MANAGED = 65 // Managed code has a longer max IPv6 string length
};
c_static_assert(GetHostErrorCodes_HOST_NOT_FOUND == HOST_NOT_FOUND);
c_static_assert(GetHostErrorCodes_TRY_AGAIN == TRY_AGAIN);
c_static_assert(GetHostErrorCodes_NO_RECOVERY == NO_RECOVERY);
c_static_assert(GetHostErrorCodes_NO_DATA == NO_DATA);
c_static_assert(GetHostErrorCodes_NO_ADDRESS == NO_ADDRESS);
c_static_assert(sizeof(uint8_t) == sizeof(char)); // We make casts from uint8_t to char so make sure it's legal
// sizeof_member(struct foo, bar) is not valid C++.
// The fix is to remove struct. That is not valid C.
// Use typedefs to make it valid C -- which are redundant but valid C++.
typedef struct iovec iovec;
typedef struct sockaddr sockaddr;
typedef struct xsocket xsocket;
typedef struct linger linger;
// We require that IOVector have the same layout as iovec.
c_static_assert(sizeof(IOVector) == sizeof(iovec));
c_static_assert(sizeof_member(IOVector, Base) == sizeof_member(iovec, iov_base));
c_static_assert(offsetof(IOVector, Base) == offsetof(iovec, iov_base));
c_static_assert(sizeof_member(IOVector, Count) == sizeof_member(iovec, iov_len));
c_static_assert(offsetof(IOVector, Count) == offsetof(iovec, iov_len));
#define Min(left,right) (((left) < (right)) ? (left) : (right))
static bool TryConvertAddressFamilyPlatformToPal(sa_family_t platformAddressFamily, int32_t* palAddressFamily)
{
assert(palAddressFamily != NULL);
switch (platformAddressFamily)
{
case AF_UNSPEC:
*palAddressFamily = AddressFamily_AF_UNSPEC;
return true;
case AF_UNIX:
*palAddressFamily = AddressFamily_AF_UNIX;
return true;
case AF_INET:
*palAddressFamily = AddressFamily_AF_INET;
return true;
case AF_INET6:
*palAddressFamily = AddressFamily_AF_INET6;
return true;
#ifdef AF_PACKET
case AF_PACKET:
*palAddressFamily = AddressFamily_AF_PACKET;
return true;
#endif
#ifdef AF_CAN
case AF_CAN:
*palAddressFamily = AddressFamily_AF_CAN;
return true;
#endif
default:
*palAddressFamily = platformAddressFamily;
return false;
}
}
static bool TryConvertAddressFamilyPalToPlatform(int32_t palAddressFamily, sa_family_t* platformAddressFamily)
{
assert(platformAddressFamily != NULL);
switch (palAddressFamily)
{
case AddressFamily_AF_UNSPEC:
*platformAddressFamily = AF_UNSPEC;
return true;
case AddressFamily_AF_UNIX:
*platformAddressFamily = AF_UNIX;
return true;
case AddressFamily_AF_INET:
*platformAddressFamily = AF_INET;
return true;
case AddressFamily_AF_INET6:
*platformAddressFamily = AF_INET6;
return true;
#ifdef AF_PACKET
case AddressFamily_AF_PACKET:
*platformAddressFamily = AF_PACKET;
return true;
#endif
#ifdef AF_CAN
case AddressFamily_AF_CAN:
*platformAddressFamily = AF_CAN;
return true;
#endif
default:
*platformAddressFamily = (sa_family_t)palAddressFamily;
return false;
}
}
static void ConvertByteArrayToIn6Addr(struct in6_addr* addr, const uint8_t* buffer, int32_t bufferLength)
{
assert(bufferLength == NUM_BYTES_IN_IPV6_ADDRESS);
memcpy_s(addr->s6_addr, NUM_BYTES_IN_IPV6_ADDRESS, buffer, (uint32_t)bufferLength);
}
static void ConvertIn6AddrToByteArray(uint8_t* buffer, int32_t bufferLength, const struct in6_addr* addr)
{
assert(bufferLength == NUM_BYTES_IN_IPV6_ADDRESS);
memcpy_s(buffer, (uint32_t)bufferLength, addr->s6_addr, NUM_BYTES_IN_IPV6_ADDRESS);
}
static void ConvertByteArrayToSockAddrIn6(struct sockaddr_in6* addr, const uint8_t* buffer, int32_t bufferLength)
{
ConvertByteArrayToIn6Addr(&addr->sin6_addr, buffer, bufferLength);
// Mark that this is INET6
addr->sin6_family = AF_INET6;
}
static void ConvertByteArrayToInAddr(struct in_addr* addr, const uint8_t* buffer, int32_t bufferLength)
{
assert(bufferLength == NUM_BYTES_IN_IPV4_ADDRESS);
memcpy_s(&addr->s_addr, NUM_BYTES_IN_IPV4_ADDRESS, buffer, (uint32_t)bufferLength); // Send back in network byte order.
}
static void ConvertInAddrToByteArray(uint8_t* buffer, int32_t bufferLength, const struct in_addr* addr)
{
assert(bufferLength == NUM_BYTES_IN_IPV4_ADDRESS);
memcpy_s(buffer, (uint32_t)bufferLength, &addr->s_addr, NUM_BYTES_IN_IPV4_ADDRESS); // Send back in network byte order.
}
static void ConvertByteArrayToSockAddrIn(struct sockaddr_in* addr, const uint8_t* buffer, int32_t bufferLength)
{
ConvertByteArrayToInAddr(&addr->sin_addr, buffer, bufferLength);
addr->sin_family = AF_INET;
}
static int32_t ConvertGetAddrInfoAndGetNameInfoErrorsToPal(int32_t error)
{
switch (error)
{
case 0:
return 0;
case EAI_AGAIN:
return GetAddrInfoErrorFlags_EAI_AGAIN;
case EAI_BADFLAGS:
return GetAddrInfoErrorFlags_EAI_BADFLAGS;
#ifdef EAI_FAIL
case EAI_FAIL:
return GetAddrInfoErrorFlags_EAI_FAIL;
#endif
case EAI_FAMILY:
return GetAddrInfoErrorFlags_EAI_FAMILY;
case EAI_MEMORY:
return GetAddrInfoErrorFlags_EAI_MEMORY;
case EAI_NONAME:
#ifdef EAI_NODATA
case EAI_NODATA:
#endif
return GetAddrInfoErrorFlags_EAI_NONAME;
}
assert_err(0, "Unknown AddrInfo error flag", error);
return -1;
}
static int32_t CopySockAddrToIPAddress(sockaddr* addr, sa_family_t family, IPAddress* ipAddress)
{
if (family == AF_INET)
{
struct sockaddr_in* inetSockAddr = (struct sockaddr_in*)addr;
ConvertInAddrToByteArray(ipAddress->Address, NUM_BYTES_IN_IPV4_ADDRESS, &inetSockAddr->sin_addr);
ipAddress->IsIPv6 = 0;
return 0;
}
else if (family == AF_INET6)
{
struct sockaddr_in6* inet6SockAddr = (struct sockaddr_in6*)addr;
ConvertIn6AddrToByteArray(ipAddress->Address, NUM_BYTES_IN_IPV6_ADDRESS, &inet6SockAddr->sin6_addr);
ipAddress->IsIPv6 = 1;
ipAddress->ScopeId = inet6SockAddr->sin6_scope_id;
return 0;
}
return -1;
}
int32_t SystemNative_GetHostEntryForName(const uint8_t* address, int32_t addressFamily, HostEntry* entry)
{
if (address == NULL || entry == NULL)
{
return GetAddrInfoErrorFlags_EAI_BADARG;
}
int32_t ret = GetAddrInfoErrorFlags_EAI_SUCCESS;
struct addrinfo* info = NULL;
#if HAVE_GETIFADDRS
struct ifaddrs* addrs = NULL;
#endif
sa_family_t platformFamily;
if (!TryConvertAddressFamilyPalToPlatform(addressFamily, &platformFamily))
{
return GetAddrInfoErrorFlags_EAI_FAMILY;
}
struct addrinfo hint;
memset(&hint, 0, sizeof(struct addrinfo));
hint.ai_flags = AI_CANONNAME;
hint.ai_family = platformFamily;
int result = getaddrinfo((const char*)address, NULL, &hint, &info);
if (result != 0)
{
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
entry->CanonicalName = NULL;
entry->Aliases = NULL;
entry->IPAddressList = NULL;
entry->IPAddressCount = 0;
// Find the canonical name for this host (if any) and count the number of IP end points.
for (struct addrinfo* ai = info; ai != NULL; ai = ai->ai_next)
{
// If we haven't found a canonical name yet and this addrinfo has one, copy it
if ((entry->CanonicalName == NULL) && (ai->ai_canonname != NULL))
{
entry->CanonicalName = (uint8_t*)strdup(ai->ai_canonname);
if (entry->CanonicalName == NULL)
{
ret = ConvertGetAddrInfoAndGetNameInfoErrorsToPal(EAI_MEMORY);
goto cleanup;
}
}
if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6)
{
entry->IPAddressCount++;
}
}
#if HAVE_GETIFADDRS
char name[_POSIX_HOST_NAME_MAX];
result = gethostname((char*)name, _POSIX_HOST_NAME_MAX);
bool includeIPv4Loopback = true;
bool includeIPv6Loopback = true;
if (result == 0 && strcasecmp((const char*)address, name) == 0)
{
// Get all interface addresses if the host name corresponds to the local host.
result = getifaddrs(&addrs);
// If getifaddrs fails, just skip it, the data are not crucial for the result.
if (result == 0)
{
// Count the number of IP end points.
for (struct ifaddrs* ifa = addrs; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
{
continue;
}
// Skip the interface if it isn't UP.
if ((ifa->ifa_flags & IFF_UP) == 0)
{
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET)
{
// Remember if there's at least one non-loopback address for IPv4, so that they will be skipped.
if ((ifa->ifa_flags & IFF_LOOPBACK) == 0)
{
includeIPv4Loopback = false;
}
entry->IPAddressCount++;
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
// Remember if there's at least one non-loopback address for IPv6, so that they will be skipped.
if ((ifa->ifa_flags & IFF_LOOPBACK) == 0)
{
includeIPv6Loopback = false;
}
entry->IPAddressCount++;
}
}
}
}
#endif
if (entry->IPAddressCount > 0)
{
entry->IPAddressList = (IPAddress*)calloc((size_t)entry->IPAddressCount, sizeof(IPAddress));
if (entry->IPAddressList == NULL)
{
ret = ConvertGetAddrInfoAndGetNameInfoErrorsToPal(EAI_MEMORY);
goto cleanup;
}
IPAddress* ipAddressList = entry->IPAddressList;
for (struct addrinfo* ai = info; ai != NULL; ai = ai->ai_next)
{
if (CopySockAddrToIPAddress(ai->ai_addr, (sa_family_t)ai->ai_family, ipAddressList) == 0)
{
++ipAddressList;
}
}
#if HAVE_GETIFADDRS
if (addrs != NULL)
{
for (struct ifaddrs* ifa = addrs; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
{
continue;
}
// Skip the interface if it isn't UP.
if ((ifa->ifa_flags & IFF_UP) == 0)
{
continue;
}
// Skip loopback addresses if at least one interface has non-loopback one.
if ((!includeIPv4Loopback && ifa->ifa_addr->sa_family == AF_INET && (ifa->ifa_flags & IFF_LOOPBACK) != 0) ||
(!includeIPv6Loopback && ifa->ifa_addr->sa_family == AF_INET6 && (ifa->ifa_flags & IFF_LOOPBACK) != 0))
{
entry->IPAddressCount--;
continue;
}
if (CopySockAddrToIPAddress(ifa->ifa_addr, ifa->ifa_addr->sa_family, ipAddressList) == 0)
{
++ipAddressList;
}
}
}
#endif
}
cleanup:
if (info != NULL)
{
freeaddrinfo(info);
}
#if HAVE_GETIFADDRS
if (addrs != NULL)
{
freeifaddrs(addrs);
}
#endif
// If the returned code is not success, the FreeHostEntry will not be called from the managed code.
if (ret != GetAddrInfoErrorFlags_EAI_SUCCESS)
{
SystemNative_FreeHostEntry(entry);
}
return ret;
}
void SystemNative_FreeHostEntry(HostEntry* entry)
{
if (entry != NULL)
{
free(entry->CanonicalName);
free(entry->IPAddressList);
entry->CanonicalName = NULL;
entry->IPAddressList = NULL;
entry->IPAddressCount = 0;
}
}
// There were several versions of glibc that had the flags parameter of getnameinfo unsigned
#if HAVE_GETNAMEINFO_SIGNED_FLAGS
typedef int32_t NativeFlagsType;
#else
typedef uint32_t NativeFlagsType;
#endif
static inline NativeFlagsType ConvertGetNameInfoFlagsToNative(int32_t flags)
{
NativeFlagsType outFlags = 0;
if ((flags & GetAddrInfoErrorFlags_NI_NAMEREQD) == GetAddrInfoErrorFlags_NI_NAMEREQD)
{
outFlags |= NI_NAMEREQD;
}
if ((flags & GetAddrInfoErrorFlags_NI_NUMERICHOST) == GetAddrInfoErrorFlags_NI_NUMERICHOST)
{
outFlags |= NI_NUMERICHOST;
}
return outFlags;
}
int32_t SystemNative_GetNameInfo(const uint8_t* address,
int32_t addressLength,
int8_t isIPv6,
uint8_t* host,
int32_t hostLength,
uint8_t* service,
int32_t serviceLength,
int32_t flags)
{
assert(address != NULL);
assert(addressLength > 0);
assert((host != NULL) || (service != NULL));
assert((hostLength > 0) || (serviceLength > 0));
NativeFlagsType nativeFlags = ConvertGetNameInfoFlagsToNative(flags);
int32_t result;
if (isIPv6)
{
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(struct sockaddr_in6));
ConvertByteArrayToSockAddrIn6(&addr, address, addressLength);
result = getnameinfo((const struct sockaddr*)&addr,
sizeof(struct sockaddr_in6),
(char*)host,
(uint32_t)hostLength,
(char*)service,
(uint32_t)serviceLength,
(int)nativeFlags);
}
else
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
ConvertByteArrayToSockAddrIn(&addr, address, addressLength);
result = getnameinfo((const struct sockaddr*)&addr,
sizeof(struct sockaddr_in),
(char*)host,
(uint32_t)hostLength,
(char*)service,
(uint32_t)serviceLength,
(int)nativeFlags);
}
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
int32_t SystemNative_GetDomainName(uint8_t* name, int32_t nameLength)
{
assert(name != NULL);
assert(nameLength > 0);
#if HAVE_GETDOMAINNAME
#if HAVE_GETDOMAINNAME_SIZET
size_t namelen = (uint32_t)nameLength;
#else
int namelen = nameLength;
#endif
return getdomainname((char*)name, namelen);
#elif HAVE_UTSNAME_DOMAINNAME
// On Android, there's no getdomainname but we can use uname to fetch the domain name
// of the current device
size_t namelen = (uint32_t)nameLength;
struct utsname uts;
// If uname returns an error, bail out.
if (uname(&uts) == -1)
{
return -1;
}
// If we don't have enough space to copy the name, bail out.
if (strlen(uts.domainname) >= namelen)
{
errno = EINVAL;
return -1;
}
// Copy the domain name
SafeStringCopy((char*)name, namelen, uts.domainname);
return 0;
#else
// GetDomainName is not supported on this platform.
errno = ENOTSUP;
return -1;
#endif
}
int32_t SystemNative_GetHostName(uint8_t* name, int32_t nameLength)
{
assert(name != NULL);
assert(nameLength > 0);
size_t unsignedSize = (uint32_t)nameLength;
return gethostname((char*)name, unsignedSize);
}
static bool IsInBounds(const void* void_baseAddr, size_t len, const void* void_valueAddr, size_t valueSize)
{
const uint8_t* baseAddr = (const uint8_t*)void_baseAddr;
const uint8_t* valueAddr = (const uint8_t*)void_valueAddr;
return valueAddr >= baseAddr && (valueAddr + valueSize) <= (baseAddr + len);
}
int32_t SystemNative_GetIPSocketAddressSizes(int32_t* ipv4SocketAddressSize, int32_t* ipv6SocketAddressSize)
{
if (ipv4SocketAddressSize == NULL || ipv6SocketAddressSize == NULL)
{
return Error_EFAULT;
}
*ipv4SocketAddressSize = sizeof(struct sockaddr_in);
*ipv6SocketAddressSize = sizeof(struct sockaddr_in6);
return Error_SUCCESS;
}
int32_t SystemNative_GetAddressFamily(const uint8_t* socketAddress, int32_t socketAddressLen, int32_t* addressFamily)
{
if (socketAddress == NULL || addressFamily == NULL || socketAddressLen < 0)
{
return Error_EFAULT;
}
const struct sockaddr* sockAddr = (const struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
if (!TryConvertAddressFamilyPlatformToPal(sockAddr->sa_family, addressFamily))
{
*addressFamily = AddressFamily_AF_UNKNOWN;
}
return Error_SUCCESS;
}
int32_t SystemNative_SetAddressFamily(uint8_t* socketAddress, int32_t socketAddressLen, int32_t addressFamily)
{
struct sockaddr* sockAddr = (struct sockaddr*)socketAddress;
if (sockAddr == NULL || socketAddressLen < 0 ||
!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
if (!TryConvertAddressFamilyPalToPlatform(addressFamily, &sockAddr->sa_family))
{
return Error_EAFNOSUPPORT;
}
return Error_SUCCESS;
}
int32_t SystemNative_GetPort(const uint8_t* socketAddress, int32_t socketAddressLen, uint16_t* port)
{
if (socketAddress == NULL)
{
return Error_EFAULT;
}
const struct sockaddr* sockAddr = (const struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
switch (sockAddr->sa_family)
{
case AF_INET:
{
if (socketAddressLen < 0 || (size_t)socketAddressLen < sizeof(struct sockaddr_in))
{
return Error_EFAULT;
}
*port = ntohs(((const struct sockaddr_in*)socketAddress)->sin_port);
return Error_SUCCESS;
}
case AF_INET6:
{
if (socketAddressLen < 0 || (size_t)socketAddressLen < sizeof(struct sockaddr_in6))
{
return Error_EFAULT;
}
*port = ntohs(((const struct sockaddr_in6*)socketAddress)->sin6_port);
return Error_SUCCESS;
}
default:
return Error_EAFNOSUPPORT;
}
}
int32_t SystemNative_SetPort(uint8_t* socketAddress, int32_t socketAddressLen, uint16_t port)
{
if (socketAddress == NULL)
{
return Error_EFAULT;
}
const struct sockaddr* sockAddr = (const struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
switch (sockAddr->sa_family)
{
case AF_INET:
{
if (socketAddressLen < 0 || (size_t)socketAddressLen < sizeof(struct sockaddr_in))
{
return Error_EFAULT;
}
((struct sockaddr_in*)socketAddress)->sin_port = htons(port);
return Error_SUCCESS;
}
case AF_INET6:
{
if (socketAddressLen < 0 || (size_t)socketAddressLen < sizeof(struct sockaddr_in6))
{
return Error_EFAULT;
}
((struct sockaddr_in6*)socketAddress)->sin6_port = htons(port);
return Error_SUCCESS;
}
default:
return Error_EAFNOSUPPORT;
}
}
int32_t SystemNative_GetIPv4Address(const uint8_t* socketAddress, int32_t socketAddressLen, uint32_t* address)
{
if (socketAddress == NULL || address == NULL || socketAddressLen < 0 ||
(size_t)socketAddressLen < sizeof(struct sockaddr_in))
{
return Error_EFAULT;
}
const struct sockaddr* sockAddr = (const struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
if (sockAddr->sa_family != AF_INET)
{
return Error_EINVAL;
}
*address = ((const struct sockaddr_in*)socketAddress)->sin_addr.s_addr;
return Error_SUCCESS;
}
int32_t SystemNative_SetIPv4Address(uint8_t* socketAddress, int32_t socketAddressLen, uint32_t address)
{
if (socketAddress == NULL || socketAddressLen < 0 || (size_t)socketAddressLen < sizeof(struct sockaddr_in))
{
return Error_EFAULT;
}
struct sockaddr* sockAddr = (struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
if (sockAddr->sa_family != AF_INET)
{
return Error_EINVAL;
}
struct sockaddr_in* inetSockAddr = (struct sockaddr_in*)sockAddr;
inetSockAddr->sin_family = AF_INET;
inetSockAddr->sin_addr.s_addr = address;
return Error_SUCCESS;
}
int32_t SystemNative_GetIPv6Address(
const uint8_t* socketAddress, int32_t socketAddressLen, uint8_t* address, int32_t addressLen, uint32_t* scopeId)
{
if (socketAddress == NULL || address == NULL || scopeId == NULL || socketAddressLen < 0 ||
(size_t)socketAddressLen < sizeof(struct sockaddr_in6) || addressLen < NUM_BYTES_IN_IPV6_ADDRESS)
{
return Error_EFAULT;
}
const struct sockaddr* sockAddr = (const struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
if (sockAddr->sa_family != AF_INET6)
{
return Error_EINVAL;
}
const struct sockaddr_in6* inet6SockAddr = (const struct sockaddr_in6*)sockAddr;
ConvertIn6AddrToByteArray(address, addressLen, &inet6SockAddr->sin6_addr);
*scopeId = inet6SockAddr->sin6_scope_id;
return Error_SUCCESS;
}
int32_t
SystemNative_SetIPv6Address(uint8_t* socketAddress, int32_t socketAddressLen, uint8_t* address, int32_t addressLen, uint32_t scopeId)
{
if (socketAddress == NULL || address == NULL || socketAddressLen < 0 ||
(size_t)socketAddressLen < sizeof(struct sockaddr_in6) || addressLen < NUM_BYTES_IN_IPV6_ADDRESS)
{
return Error_EFAULT;
}
struct sockaddr* sockAddr = (struct sockaddr*)socketAddress;
if (!IsInBounds(sockAddr, (size_t)socketAddressLen, &sockAddr->sa_family, sizeof_member(sockaddr, sa_family)))
{
return Error_EFAULT;
}
if (sockAddr->sa_family != AF_INET6)
{
return Error_EINVAL;
}
struct sockaddr_in6* inet6SockAddr = (struct sockaddr_in6*)sockAddr;
ConvertByteArrayToSockAddrIn6(inet6SockAddr, address, addressLen);
inet6SockAddr->sin6_family = AF_INET6;
inet6SockAddr->sin6_flowinfo = 0;
inet6SockAddr->sin6_scope_id = scopeId;
return Error_SUCCESS;
}
static int8_t IsStreamSocket(int socket)
{
int type;
socklen_t length = sizeof(int);
return getsockopt(socket, SOL_SOCKET, SO_TYPE, &type, &length) == 0
&& type == SOCK_STREAM;
}
static void ConvertMessageHeaderToMsghdr(struct msghdr* header, const MessageHeader* messageHeader, int socket)
{
// sendmsg/recvmsg can return EMSGSIZE when msg_iovlen is greather than IOV_MAX.
// We avoid this for stream sockets by truncating msg_iovlen to IOV_MAX. This is ok since sendmsg is
// not required to send all data and recvmsg can be called again to receive more.
int iovlen = (int)messageHeader->IOVectorCount;
if (iovlen > IOV_MAX && IsStreamSocket(socket))
{
iovlen = (int)IOV_MAX;
}
header->msg_name = messageHeader->SocketAddress;
header->msg_namelen = (socklen_t)messageHeader->SocketAddressLen;
header->msg_iov = (struct iovec*)messageHeader->IOVectors;
header->msg_iovlen = (__typeof__(header->msg_iovlen))iovlen;
header->msg_control = messageHeader->ControlBuffer;
header->msg_controllen = (uint32_t)messageHeader->ControlBufferLen;
header->msg_flags = 0;
}
int32_t SystemNative_GetControlMessageBufferSize(int32_t isIPv4, int32_t isIPv6)
{
// Note: it is possible that the address family of the socket is neither
// AF_INET nor AF_INET6. In this case both inputs will be 0 and
// the controll message buffer size should be zero.
return (isIPv4 != 0 ? CMSG_SPACE(sizeof(struct in_pktinfo)) : 0) + (isIPv6 != 0 ? CMSG_SPACE(sizeof(struct in6_pktinfo)) : 0);
}
static int32_t GetIPv4PacketInformation(struct cmsghdr* controlMessage, IPPacketInformation* packetInfo)
{
assert(controlMessage != NULL);
assert(packetInfo != NULL);
if (controlMessage->cmsg_len < sizeof(struct in_pktinfo))
{
assert(false && "expected a control message large enough to hold an in_pktinfo value");
return 0;
}
struct in_pktinfo* pktinfo = (struct in_pktinfo*)CMSG_DATA(controlMessage);
ConvertInAddrToByteArray(&packetInfo->Address.Address[0], NUM_BYTES_IN_IPV4_ADDRESS, &pktinfo->ipi_addr);
#if HAVE_IN_PKTINFO
packetInfo->InterfaceIndex = (int32_t)pktinfo->ipi_ifindex;
#elif HAVE_GETIFADDRS
packetInfo->InterfaceIndex = 0;
struct ifaddrs* addrs;
if (getifaddrs(&addrs) == 0)
{
struct ifaddrs* addrs_head = addrs;
while (addrs != NULL)
{
if (addrs->ifa_addr->sa_family == AF_INET && ((struct sockaddr_in*)addrs->ifa_addr)->sin_addr.s_addr == pktinfo->ipi_addr.s_addr)
{
packetInfo->InterfaceIndex = (int32_t)if_nametoindex(addrs->ifa_name);
break;
}
addrs = addrs->ifa_next;
}
freeifaddrs(addrs_head);
}
#else
// assume the first interface, we have no other methods
packetInfo->InterfaceIndex = 0;
#endif
return 1;
}
static int32_t GetIPv6PacketInformation(struct cmsghdr* controlMessage, IPPacketInformation* packetInfo)
{
assert(controlMessage != NULL);
assert(packetInfo != NULL);
if (controlMessage->cmsg_len < sizeof(struct in6_pktinfo))
{
assert(false && "expected a control message large enough to hold an in6_pktinfo value");
return 0;
}
struct in6_pktinfo* pktinfo = (struct in6_pktinfo*)CMSG_DATA(controlMessage);
ConvertIn6AddrToByteArray(&packetInfo->Address.Address[0], NUM_BYTES_IN_IPV6_ADDRESS, &pktinfo->ipi6_addr);
packetInfo->Address.IsIPv6 = 1;
packetInfo->InterfaceIndex = (int32_t)pktinfo->ipi6_ifindex;
return 1;
}
static struct cmsghdr* GET_CMSG_NXTHDR(struct msghdr* mhdr, struct cmsghdr* cmsg)
{
#ifndef __GLIBC__
// Tracking issue: #6312
// In musl-libc, CMSG_NXTHDR typecasts char* to struct cmsghdr* which causes
// clang to throw sign-compare warning. This is to suppress the warning