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/. */
5 #ifndef NETWORK_IPV6_UTILS_H_
6 #define NETWORK_IPV6_UTILS_H_
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
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]);
24 if ((b
[0] & 0xFE) == 0xFC) {
25 return IPV6_SCOPE_UNIQUELOCAL
;
29 return IPV6_SCOPE_LINKLOCAL
;
31 return IPV6_SCOPE_SITELOCAL
;
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) {
38 return IPV6_SCOPE_NODELOCAL
;
43 return IPV6_SCOPE_GLOBAL
;
48 } // namespace mozilla
50 #endif // NETWORK_IPV6_UTILS_H_