Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef NETWORK_IPV6_UTILS_H_
6
#define NETWORK_IPV6_UTILS_H_
7
8
namespace mozilla {
9
namespace net {
10
namespace utils {
11
12
// IPv6 address scopes.
13
#define IPV6_SCOPE_GLOBAL 0 // Global scope.
14
#define IPV6_SCOPE_LINKLOCAL 1 // Link-local scope.
15
#define IPV6_SCOPE_SITELOCAL 2 // Site-local scope (deprecated).
16
#define IPV6_SCOPE_UNIQUELOCAL 3 // Unique local
17
#define IPV6_SCOPE_NODELOCAL 4 // Loopback
18
19
// Return the scope of the given address.
20
static int ipv6_scope(const unsigned char addr[16]) {
21
const unsigned char* b = addr;
22
unsigned short w = (unsigned short)((b[0] << 8) | b[1]);
23
24
if ((b[0] & 0xFE) == 0xFC) {
25
return IPV6_SCOPE_UNIQUELOCAL;
26
}
27
switch (w & 0xFFC0) {
28
case 0xFE80:
29
return IPV6_SCOPE_LINKLOCAL;
30
case 0xFEC0:
31
return IPV6_SCOPE_SITELOCAL;
32
case 0x0000:
33
w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] | b[10] |
34
b[11] | b[12] | b[13] | b[14];
35
if (w || b[15] != 0x01) {
36
break;
37
}
38
return IPV6_SCOPE_NODELOCAL;
39
default:
40
break;
41
}
42
43
return IPV6_SCOPE_GLOBAL;
44
}
45
46
} // namespace utils
47
} // namespace net
48
} // namespace mozilla
49
50
#endif // NETWORK_IPV6_UTILS_H_