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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/shared_impl/private/net_address_private_impl.h"
#if defined(OS_WIN)
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#elif defined(OS_POSIX) && !defined(OS_NACL)
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif
#include <string.h>
#include <string>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "build/build_config.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/c/private/ppb_net_address_private.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/thunk.h"
#if defined(OS_MACOSX)
// This is a bit evil, but it's standard operating procedure for |s6_addr|....
#define s6_addr16 __u6_addr.__u6_addr16
#endif
#if defined(OS_WIN)
// The type of |sockaddr::sa_family|.
typedef ADDRESS_FAMILY sa_family_t;
#define s6_addr16 u.Word
#define ntohs(x) _byteswap_ushort(x)
#define htons(x) _byteswap_ushort(x)
#endif // defined(OS_WIN)
// The net address interface doesn't have a normal C -> C++ thunk since it
// doesn't actually have any proxy wrapping or associated objects; it's just a
// call into base. So we implement the entire interface here, using the thunk
// namespace so it magically gets hooked up in the proper places.
namespace ppapi {
namespace {
// Define our own net-host-net conversion, rather than reuse the one in
// base/sys_byteorder.h, to simplify the NaCl port. NaCl has no byte swap
// primitives.
uint16 ConvertNetEndian16(uint16 x) {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
return (x << 8) | (x >> 8);
#else
return x;
#endif
}
static const size_t kIPv4AddressSize = 4;
static const size_t kIPv6AddressSize = 16;
// This structure is a platform-independent representation of a network address.
// It is a private format that we embed in PP_NetAddress_Private and is NOT part
// of the stable Pepper API.
struct NetAddress {
bool is_valid;
bool is_ipv6; // if true, IPv6, otherwise IPv4.
uint16_t port; // host order, not network order.
int32_t flow_info; // 0 for IPv4
int32_t scope_id; // 0 for IPv4
// IPv4 addresses are 4 bytes. IPv6 are 16 bytes. Addresses are stored in net
// order (big-endian), which only affects IPv6 addresses, which consist of 8
// 16-bit components. These will be byte-swapped on small-endian hosts.
uint8_t address[kIPv6AddressSize];
};
// Make sure that sizeof(NetAddress) is the same for all compilers. This ensures
// that the alignment is the same on both sides of the NaCl proxy, which is
// important because we serialize and deserialize PP_NetAddress_Private by
// simply copying the raw bytes.
COMPILE_ASSERT(sizeof(NetAddress) == 28,
NetAddress_different_for_compiler);
// Make sure the storage in |PP_NetAddress_Private| is big enough. (Do it here
// since the data is opaque elsewhere.)
COMPILE_ASSERT(sizeof(reinterpret_cast<PP_NetAddress_Private*>(0)->data) >=
sizeof(NetAddress),
PP_NetAddress_Private_data_too_small);
size_t GetAddressSize(const NetAddress* net_addr) {
return net_addr->is_ipv6 ? kIPv6AddressSize : kIPv4AddressSize;
}
// Convert to embedded struct if it has been initialized.
NetAddress* ToNetAddress(PP_NetAddress_Private* addr) {
if (!addr || addr->size != sizeof(NetAddress))
return NULL;
return reinterpret_cast<NetAddress*>(addr->data);
}
const NetAddress* ToNetAddress(const PP_NetAddress_Private* addr) {
return ToNetAddress(const_cast<PP_NetAddress_Private*>(addr));
}
// Initializes the NetAddress struct embedded in a PP_NetAddress_Private struct.
// Zeroes the memory, so net_addr->is_valid == false.
NetAddress* InitNetAddress(PP_NetAddress_Private* addr) {
addr->size = sizeof(NetAddress);
NetAddress* net_addr = ToNetAddress(addr);
DCHECK(net_addr);
memset(net_addr, 0, sizeof(NetAddress));
return net_addr;
}
bool IsValid(const NetAddress* net_addr) {
return net_addr && net_addr->is_valid;
}
PP_NetAddressFamily_Private GetFamily(const PP_NetAddress_Private* addr) {
const NetAddress* net_addr = ToNetAddress(addr);
if (!IsValid(net_addr))
return PP_NETADDRESSFAMILY_UNSPECIFIED;
return net_addr->is_ipv6 ?
PP_NETADDRESSFAMILY_IPV6 : PP_NETADDRESSFAMILY_IPV4;
}
uint16_t GetPort(const PP_NetAddress_Private* addr) {
const NetAddress* net_addr = ToNetAddress(addr);
if (!IsValid(net_addr))
return 0;
return net_addr->port;
}
PP_Bool GetAddress(const PP_NetAddress_Private* addr,
void* address,
uint16_t address_size) {
const NetAddress* net_addr = ToNetAddress(addr);
if (!IsValid(net_addr))
return PP_FALSE;
size_t net_addr_size = GetAddressSize(net_addr);
// address_size must be big enough.
if (net_addr_size > address_size)
return PP_FALSE;
memcpy(address, net_addr->address, net_addr_size);
return PP_TRUE;
}
uint32_t GetScopeID(const PP_NetAddress_Private* addr) {
const NetAddress* net_addr = ToNetAddress(addr);
if (!IsValid(net_addr))
return 0;
return net_addr->scope_id;
}
PP_Bool AreHostsEqual(const PP_NetAddress_Private* addr1,
const PP_NetAddress_Private* addr2) {
const NetAddress* net_addr1 = ToNetAddress(addr1);
const NetAddress* net_addr2 = ToNetAddress(addr2);
if (!IsValid(net_addr1) || !IsValid(net_addr2))
return PP_FALSE;
if ((net_addr1->is_ipv6 != net_addr2->is_ipv6) ||
(net_addr1->flow_info != net_addr2->flow_info) ||
(net_addr1->scope_id != net_addr2->scope_id))
return PP_FALSE;
size_t net_addr_size = GetAddressSize(net_addr1);
for (size_t i = 0; i < net_addr_size; i++) {
if (net_addr1->address[i] != net_addr2->address[i])
return PP_FALSE;
}
return PP_TRUE;
}
PP_Bool AreEqual(const PP_NetAddress_Private* addr1,
const PP_NetAddress_Private* addr2) {
// |AreHostsEqual()| will also validate the addresses and return false if
// either is invalid.
if (!AreHostsEqual(addr1, addr2))
return PP_FALSE;
// AreHostsEqual has validated these net addresses.
const NetAddress* net_addr1 = ToNetAddress(addr1);
const NetAddress* net_addr2 = ToNetAddress(addr2);
return PP_FromBool(net_addr1->port == net_addr2->port);
}
std::string ConvertIPv4AddressToString(const NetAddress* net_addr,
bool include_port) {
std::string description = base::StringPrintf(
"%u.%u.%u.%u",
net_addr->address[0], net_addr->address[1],
net_addr->address[2], net_addr->address[3]);
if (include_port)
base::StringAppendF(&description, ":%u", net_addr->port);
return description;
}
// Format an IPv6 address for human consumption, basically according to RFC
// 5952.
// - If the scope is nonzero, it is appended to the address as "%<scope>" (this
// is not in RFC 5952, but consistent with |getnameinfo()| on Linux and
// Windows).
// - If |include_port| is true, the address (possibly including the scope) is
// enclosed in square brackets and ":<port>" is appended, i.e., the overall
// format is "[<address>]:<port>".
// - If the address is an IPv4 address embedded IPv6 (per RFC 4291), then the
// mixed format is used, e.g., "::ffff:192.168.1.2". This is optional per RFC
// 5952, but consistent with |getnameinfo()|.
std::string ConvertIPv6AddressToString(const NetAddress* net_addr,
bool include_port) {
std::string description(include_port ? "[" : "");
const uint16_t* address16 =
reinterpret_cast<const uint16_t*>(net_addr->address);
// IPv4 address embedded in IPv6.
if (address16[0] == 0 && address16[1] == 0 &&
address16[2] == 0 && address16[3] == 0 &&
address16[4] == 0 &&
(address16[5] == 0 || address16[5] == 0xffff)) {
base::StringAppendF(
&description,
address16[5] == 0 ? "::%u.%u.%u.%u" : "::ffff:%u.%u.%u.%u",
net_addr->address[12],
net_addr->address[13],
net_addr->address[14],
net_addr->address[15]);
// "Real" IPv6 addresses.
} else {
// Find the first longest run of 0s (of length > 1), to collapse to "::".
int longest_start = 0;
int longest_length = 0;
int curr_start = 0;
int curr_length = 0;
for (int i = 0; i < 8; i++) {
if (address16[i] != 0) {
curr_length = 0;
} else {
if (!curr_length)
curr_start = i;
curr_length++;
if (curr_length > longest_length) {
longest_start = curr_start;
longest_length = curr_length;
}
}
}
bool need_sep = false; // Whether the next item needs a ':' to separate.
for (int i = 0; i < 8;) {
if (longest_length > 1 && i == longest_start) {
description.append("::");
need_sep = false;
i += longest_length;
} else {
uint16_t v = ConvertNetEndian16(address16[i]);
base::StringAppendF(&description, need_sep ? ":%x" : "%x", v);
need_sep = true;
i++;
}
}
}
// Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123".
if (net_addr->scope_id != 0)
base::StringAppendF(&description, "%%%u", net_addr->scope_id);
if (include_port)
base::StringAppendF(&description, "]:%u", net_addr->port);
return description;
}
PP_Var Describe(PP_Module /*module*/,
const struct PP_NetAddress_Private* addr,
PP_Bool include_port) {
std::string str = NetAddressPrivateImpl::DescribeNetAddress(
*addr, PP_ToBool(include_port));
if (str.empty())
return PP_MakeUndefined();
return StringVar::StringToPPVar(str);
}
PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr,
uint16_t port,
struct PP_NetAddress_Private* dest_addr) {
const NetAddress* src_net_addr = ToNetAddress(src_addr);
if (!IsValid(src_net_addr) || !dest_addr)
return PP_FALSE;
dest_addr->size = sizeof(NetAddress); // make sure 'size' is valid.
NetAddress* dest_net_addr = ToNetAddress(dest_addr);
*dest_net_addr = *src_net_addr;
dest_net_addr->port = port;
return PP_TRUE;
}
void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) {
if (addr) {
NetAddress* net_addr = InitNetAddress(addr);
net_addr->is_valid = true;
net_addr->is_ipv6 = (is_ipv6 == PP_TRUE);
}
}
void CreateFromIPv4Address(const uint8_t ip[4],
uint16_t port,
struct PP_NetAddress_Private* addr) {
if (addr) {
NetAddress* net_addr = InitNetAddress(addr);
net_addr->is_valid = true;
net_addr->is_ipv6 = false;
net_addr->port = port;
memcpy(net_addr->address, ip, kIPv4AddressSize);
}
}
void CreateFromIPv6Address(const uint8_t ip[16],
uint32_t scope_id,
uint16_t port,
struct PP_NetAddress_Private* addr) {
if (addr) {
NetAddress* net_addr = InitNetAddress(addr);
net_addr->is_valid = true;
net_addr->is_ipv6 = true;
net_addr->port = port;
net_addr->scope_id = scope_id;
memcpy(net_addr->address, ip, kIPv6AddressSize);
}
}
const PPB_NetAddress_Private_0_1 net_address_private_interface_0_1 = {
&AreEqual,
&AreHostsEqual,
&Describe,
&ReplacePort,
&GetAnyAddress
};
const PPB_NetAddress_Private_1_0 net_address_private_interface_1_0 = {
&AreEqual,
&AreHostsEqual,
&Describe,
&ReplacePort,
&GetAnyAddress,
&GetFamily,
&GetPort,
&GetAddress
};
const PPB_NetAddress_Private_1_1 net_address_private_interface_1_1 = {
&AreEqual,
&AreHostsEqual,
&Describe,
&ReplacePort,
&GetAnyAddress,
&GetFamily,
&GetPort,
&GetAddress,
&GetScopeID,
&CreateFromIPv4Address,
&CreateFromIPv6Address
};
} // namespace
namespace thunk {
PPAPI_THUNK_EXPORT const PPB_NetAddress_Private_0_1*
GetPPB_NetAddress_Private_0_1_Thunk() {
return &net_address_private_interface_0_1;
}
PPAPI_THUNK_EXPORT const PPB_NetAddress_Private_1_0*
GetPPB_NetAddress_Private_1_0_Thunk() {
return &net_address_private_interface_1_0;
}
PPAPI_THUNK_EXPORT const PPB_NetAddress_Private_1_1*
GetPPB_NetAddress_Private_1_1_Thunk() {
return &net_address_private_interface_1_1;
}
} // namespace thunk
// For the NaCl target, all we need are the API functions and the thunk.
#if !defined(OS_NACL)
// static
const PP_NetAddress_Private NetAddressPrivateImpl::kInvalidNetAddress = { 0 };
// static
bool NetAddressPrivateImpl::ValidateNetAddress(
const PP_NetAddress_Private& addr) {
return IsValid(ToNetAddress(&addr));
}
// static
bool NetAddressPrivateImpl::SockaddrToNetAddress(
const sockaddr* sa,
uint32_t sa_length,
PP_NetAddress_Private* addr) {
if (!sa || sa_length == 0 || !addr)
return false;
// Our platform neutral format stores ports in host order, not net order,
// so convert them here.
NetAddress* net_addr = InitNetAddress(addr);
switch (sa->sa_family) {
case AF_INET: {
const struct sockaddr_in* addr4 =
reinterpret_cast<const struct sockaddr_in*>(sa);
net_addr->is_valid = true;
net_addr->is_ipv6 = false;
net_addr->port = ConvertNetEndian16(addr4->sin_port);
memcpy(net_addr->address, &addr4->sin_addr.s_addr, kIPv4AddressSize);
break;
}
case AF_INET6: {
const struct sockaddr_in6* addr6 =
reinterpret_cast<const struct sockaddr_in6*>(sa);
net_addr->is_valid = true;
net_addr->is_ipv6 = true;
net_addr->port = ConvertNetEndian16(addr6->sin6_port);
net_addr->flow_info = addr6->sin6_flowinfo;
net_addr->scope_id = addr6->sin6_scope_id;
memcpy(net_addr->address, addr6->sin6_addr.s6_addr, kIPv6AddressSize);
break;
}
default:
// InitNetAddress sets net_addr->is_valid to false.
return false;
}
return true;}
// static
bool NetAddressPrivateImpl::IPEndPointToNetAddress(
const std::vector<unsigned char>& address,
int port,
PP_NetAddress_Private* addr) {
if (!addr)
return false;
NetAddress* net_addr = InitNetAddress(addr);
switch (address.size()) {
case kIPv4AddressSize: {
net_addr->is_valid = true;
net_addr->is_ipv6 = false;
net_addr->port = static_cast<uint16_t>(port);
std::copy(address.begin(), address.end(), net_addr->address);
break;
}
case kIPv6AddressSize: {
net_addr->is_valid = true;
net_addr->is_ipv6 = true;
net_addr->port = static_cast<uint16_t>(port);
std::copy(address.begin(), address.end(), net_addr->address);
break;
}
default:
// InitNetAddress sets net_addr->is_valid to false.
return false;
}
return true;
}
// static
bool NetAddressPrivateImpl::NetAddressToIPEndPoint(
const PP_NetAddress_Private& addr,
std::vector<unsigned char>* address,
int* port) {
if (!address || !port)
return false;
const NetAddress* net_addr = ToNetAddress(&addr);
if (!IsValid(net_addr))
return false;
*port = net_addr->port;
size_t address_size = GetAddressSize(net_addr);
address->assign(&net_addr->address[0], &net_addr->address[address_size]);
return true;
}
#endif // !defined(OS_NACL)
// static
std::string NetAddressPrivateImpl::DescribeNetAddress(
const PP_NetAddress_Private& addr,
bool include_port) {
const NetAddress* net_addr = ToNetAddress(&addr);
if (!IsValid(net_addr))
return std::string();
// On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac,
// the output isn't consistent with RFC 5952, at least on Mac OS 10.6:
// |getnameinfo()| collapses length-one runs of zeros (and also doesn't
// display the scope).
if (net_addr->is_ipv6)
return ConvertIPv6AddressToString(net_addr, include_port);
return ConvertIPv4AddressToString(net_addr, include_port);
}
} // namespace ppapi
|