forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_networking.cpp
More file actions
394 lines (339 loc) · 13.5 KB
/
Copy pathpal_networking.cpp
File metadata and controls
394 lines (339 loc) · 13.5 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "pal_config.h"
#include "pal_networking.h"
#include "pal_utilities.h"
#include <arpa/inet.h>
#include <assert.h>
#include <functional>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>
const int NUM_BYTES_IN_IPV4_ADDRESS = 4;
const int NUM_BYTES_IN_IPV6_ADDRESS = 16;
const int INET6_ADDRSTRLEN_MANAGED = 65; // The C# code has a longer max string length
static_assert(PAL_HOST_NOT_FOUND == HOST_NOT_FOUND, "");
static_assert(PAL_TRY_AGAIN == TRY_AGAIN, "");
static_assert(PAL_NO_RECOVERY == NO_RECOVERY, "");
static_assert(PAL_NO_DATA == NO_DATA, "");
static_assert(PAL_NO_ADDRESS == NO_ADDRESS, "");
static_assert(sizeof(uint8_t) == sizeof(char), ""); // We make casts from uint8_t to char for OS functions, make sure it's legal
static void IpStringToAddressHelper(const uint8_t* address,
const uint8_t* port,
bool isIpV6,
int32_t* err,
const std::function<void(const addrinfo& info)>& lambda)
{
assert(address != nullptr);
assert(err != nullptr);
addrinfo hint = {
.ai_family = isIpV6 ? AF_INET6 : AF_INET,
.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
};
addrinfo* info = nullptr;
int result = getaddrinfo(reinterpret_cast<const char*>(address), reinterpret_cast<const char*>(port), &hint, &info);
if (result == 0)
{
*err = 0;
lambda(*info);
freeaddrinfo(info);
}
else
{
*err = result;
}
}
static void ConvertByteArrayToV6SockAddrIn(sockaddr_in6& addr, const uint8_t* buffer, int32_t bufferLength)
{
#if HAVE_IN6_U
assert(bufferLength == ARRAY_SIZE(addr.sin6_addr.__in6_u.__u6_addr8));
memcpy(addr.sin6_addr.__in6_u.__u6_addr8, buffer, UnsignedCast(bufferLength));
#else
assert(bufferLength == ARRAY_SIZE(addr.sin6_addr.__u6_addr.__u6_addr8));
memcpy(addr.sin6_addr.__u6_addr.__u6_addr8, buffer, UnsignedCast(bufferLength));
#endif
// Mark that this is INET6
addr.sin6_family = AF_INET6;
}
static void ConvertV6SockAddrInToByteArray(uint8_t* buffer, int32_t bufferLength, const sockaddr_in6& addr)
{
#if HAVE_IN6_U
assert(bufferLength == ARRAY_SIZE(addr.sin6_addr.__in6_u.__u6_addr8));
memcpy(buffer, addr.sin6_addr.__in6_u.__u6_addr8, UnsignedCast(bufferLength));
#else
assert(bufferLength == ARRAY_SIZE(addr.sin6_addr.__u6_addr.__u6_addr8));
memcpy(buffer, addr.sin6_addr.__u6_addr.__u6_addr8, UnsignedCast(bufferLength));
#endif
}
static void ConvertByteArrayToSockAddrIn(sockaddr_in& addr, const uint8_t* buffer, int32_t bufferLength)
{
assert(bufferLength == NUM_BYTES_IN_IPV4_ADDRESS);
(void)bufferLength; // Silence compiler warnings about unused variables on release mode
addr.sin_addr.s_addr = *reinterpret_cast<const uint32_t*>(buffer); // The address comes as network byte order
addr.sin_family = AF_INET;
}
static void ConvertSockAddrInToByteArray(uint8_t* buffer, int32_t bufferLength, const sockaddr_in& addr)
{
assert(bufferLength == NUM_BYTES_IN_IPV4_ADDRESS);
(void)bufferLength; // Silence compiler warnings about unused variables on release mode
uint32_t* output = reinterpret_cast<uint32_t*>(buffer);
*output = addr.sin_addr.s_addr; // Send back in network byte order
}
static int32_t ConvertGetAddrInfoAndGetNameInfoErrorsToPal(int32_t error)
{
switch (error)
{
case 0:
return 0;
case EAI_AGAIN:
return PAL_EAI_AGAIN;
case EAI_BADFLAGS:
return PAL_EAI_BADFLAGS;
case EAI_FAIL:
return PAL_EAI_FAIL;
case EAI_FAMILY:
return PAL_EAI_FAMILY;
case EAI_NONAME:
return PAL_EAI_NONAME;
}
assert(false && "Unknown AddrInfo error flag");
return -1;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
extern "C" int32_t
Ipv6StringToAddress(const uint8_t* address, const uint8_t* port, uint8_t* buffer, int32_t bufferLength, uint32_t* scope)
{
assert(buffer != nullptr);
assert(bufferLength == NUM_BYTES_IN_IPV6_ADDRESS);
assert(scope != nullptr);
// Call our helper to do the getaddrinfo and freeaddrinfo calls for us; once we have the info, copy what we need
int32_t result;
IpStringToAddressHelper(address,
port,
true,
&result,
[buffer, scope, bufferLength](const addrinfo& info)
{
sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info.ai_addr);
ConvertV6SockAddrInToByteArray(buffer, bufferLength, *addr);
*scope = addr->sin6_scope_id;
});
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
extern "C" int32_t Ipv4StringToAddress(const uint8_t* address, uint8_t* buffer, int32_t bufferLength, uint16_t* port)
{
assert(buffer != nullptr);
assert(bufferLength == NUM_BYTES_IN_IPV4_ADDRESS);
assert(port != nullptr);
// Call our helper to do the getaddrinfo and freeaddrinfo calls for us; once we have the info, copy what we need
int32_t result;
IpStringToAddressHelper(address,
nullptr,
false,
&result,
[buffer, port, bufferLength](const addrinfo& info)
{
sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info.ai_addr);
ConvertSockAddrInToByteArray(buffer, bufferLength, *addr);
*port = addr->sin_port;
});
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
#pragma clang diagnostic pop
extern "C" int32_t IpAddressToString(const uint8_t* address,
int32_t addressLength,
bool isIpv6,
uint8_t* string,
int32_t stringLength,
uint32_t scope /* = 0*/)
{
assert(address != nullptr);
assert((addressLength == NUM_BYTES_IN_IPV6_ADDRESS) || (addressLength == NUM_BYTES_IN_IPV4_ADDRESS));
assert(string != nullptr);
// These constants differ per platform so the managed side uses the bigger value; therefore, check that
// the length is between the two lengths
assert((stringLength >= INET_ADDRSTRLEN) && (stringLength <= INET6_ADDRSTRLEN_MANAGED));
(void)addressLength; // Silence compiler warnings about unused variables on release mode
(void)INET6_ADDRSTRLEN_MANAGED; // Silence compiler warnings about unused variables on release mode
int32_t result;
socklen_t len = UnsignedCast(stringLength);
if (isIpv6)
{
sockaddr_in6 addr = {
.sin6_scope_id = scope
};
ConvertByteArrayToV6SockAddrIn(addr, address, addressLength);
result = getnameinfo(
reinterpret_cast<const sockaddr*>(&addr),
sizeof(sockaddr_in6),
reinterpret_cast<char*>(string),
len,
nullptr,
0,
NI_NUMERICHOST);
}
else
{
sockaddr_in addr = {};
ConvertByteArrayToSockAddrIn(addr, address, addressLength);
result = getnameinfo(
reinterpret_cast<const sockaddr*>(&addr),
sizeof(sockaddr_in),
reinterpret_cast<char*>(string),
len,
nullptr,
0,
NI_NUMERICHOST);
}
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
extern "C" int32_t GetHostEntriesForName(const uint8_t* address, HostEntry** entry)
{
assert(address != nullptr);
assert(entry != nullptr);
// Get all address families and the canonical name
addrinfo hint = {
.ai_family = AF_UNSPEC,
.ai_flags = AI_CANONNAME
};
addrinfo* info = nullptr;
int result = getaddrinfo(reinterpret_cast<const char*>(address), nullptr, &hint, &info);
if (result != 0)
{
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
// Allocate our data and ensure everything is 0
*entry = new HostEntry;
HostEntry* he = *entry;
memset(he, 0, sizeof(HostEntry));
// Cache the first so we can walk the linked list again
addrinfo* first = info;
// Walk the linked-list of entries and get the IP Addresses
for (addrinfo* ai = info; ai != nullptr; ai = ai->ai_next)
{
// If we haven't found a canonical name yet and this addrinfo has one, copy it
if ((he->CanonicalName == nullptr) && (ai->ai_canonname != nullptr))
{
size_t len = strlen(ai->ai_canonname) + 1; // Name + null
he->CanonicalName = new uint8_t[len];
SafeStringCopy(reinterpret_cast<char*>(he->CanonicalName), len, ai->ai_canonname);
}
// Skip non-IPv4 and non-IPv6 addresses
if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6))
{
continue;
}
// Only increment the count of addresses here; don't try to parse on the first
// walk since we would (most likely) need to realloc the array, which is more
// expensive than just incrementing the limited number of IPs twice.
he->Count++;
}
// Now that we have the final count of all IP Addresses for this host name, allocate them
he->Addresses = new PalIpAddress[he->Count];
// Walk the addrinfo's that we want and construct the IP Addresses for the managed side
int i = 0;
for (addrinfo* ai = first; ai != nullptr; ai = ai->ai_next)
{
// Skip non-IPv4 and non-IPv6 addresses
if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6))
{
continue;
}
bool isv6 = ai->ai_family == AF_INET6;
PalIpAddress* ip = &he->Addresses[i];
ip->IsIpv6 = isv6;
ip->Count = isv6 ? NUM_BYTES_IN_IPV6_ADDRESS : NUM_BYTES_IN_IPV4_ADDRESS;
ip->Address = new uint8_t[ip->Count];
// If this is an IPv6 address then get the addrinfo as a v6 address and
// copy the 128-bit value to our buffer. If this is an IPv4 address then
// get the addrinfo as a v4 address and copy the lone 32-bit value to our buffer.
if (isv6)
{
sockaddr_in6* addrin = reinterpret_cast<sockaddr_in6*>(ai->ai_addr);
ConvertV6SockAddrInToByteArray(ip->Address, ip->Count, *addrin);
}
else
{
sockaddr_in* addrin = reinterpret_cast<sockaddr_in*>(ai->ai_addr);
ConvertSockAddrInToByteArray(ip->Address, ip->Count, *addrin);
}
i++;
}
freeaddrinfo(info);
return 0;
}
extern "C" void FreeHostEntriesForName(HostEntry* entry)
{
assert(entry != nullptr);
for (int i = 0; i < entry->Count; i++)
{
delete[] entry->Addresses[i].Address;
}
delete[] entry->Addresses;
delete[] entry->CanonicalName;
delete entry;
}
inline int32_t ConvertGetNameInfoFlagsToPal(int32_t flags)
{
int32_t outFlags = 0;
if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
{
outFlags |= PAL_NI_NAMEREQD;
}
if ((flags & NI_NUMERICHOST) == NI_NUMERICHOST)
{
outFlags |= PAL_NI_NUMERICHOST;
}
return outFlags;
}
extern "C" int32_t GetNameInfo(const uint8_t* address,
int32_t addressLength,
bool isIpv6,
uint8_t* host,
int32_t hostLength,
uint8_t* service,
int32_t serviceLength,
int32_t flags)
{
assert(address != nullptr);
assert(addressLength > 0);
assert((host != nullptr) || (service != nullptr));
assert((hostLength > 0) || (serviceLength > 0));
int32_t nativeFlags = ConvertGetNameInfoFlagsToPal(flags);
int32_t result;
if (isIpv6)
{
sockaddr_in6 addr = {};
ConvertByteArrayToV6SockAddrIn(addr, address, addressLength);
result = getnameinfo(reinterpret_cast<const sockaddr*>(&addr),
sizeof(sockaddr_in6),
reinterpret_cast<char*>(host),
UnsignedCast(hostLength),
reinterpret_cast<char*>(service),
UnsignedCast(serviceLength),
nativeFlags);
}
else
{
sockaddr_in addr = {};
ConvertByteArrayToSockAddrIn(addr, address, addressLength);
result = getnameinfo(reinterpret_cast<const sockaddr*>(&addr),
sizeof(sockaddr_in),
reinterpret_cast<char*>(host),
UnsignedCast(hostLength),
reinterpret_cast<char*>(service),
UnsignedCast(serviceLength),
nativeFlags);
}
return ConvertGetAddrInfoAndGetNameInfoErrorsToPal(result);
}
extern "C" int32_t GetHostName(uint8_t* name, int32_t nameLength)
{
assert(name != nullptr);
assert(nameLength > 0);
size_t unsignedSize = UnsignedCast(nameLength);
return gethostname(reinterpret_cast<char*>(name), unsignedSize);
}