From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- netwerk/base/IPv6Utils.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 netwerk/base/IPv6Utils.h (limited to 'netwerk/base/IPv6Utils.h') diff --git a/netwerk/base/IPv6Utils.h b/netwerk/base/IPv6Utils.h new file mode 100644 index 0000000000..437c0befec --- /dev/null +++ b/netwerk/base/IPv6Utils.h @@ -0,0 +1,50 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef NETWORK_IPV6_UTILS_H_ +#define NETWORK_IPV6_UTILS_H_ + +namespace mozilla { +namespace net { +namespace utils { + +// IPv6 address scopes. +#define IPV6_SCOPE_GLOBAL 0 // Global scope. +#define IPV6_SCOPE_LINKLOCAL 1 // Link-local scope. +#define IPV6_SCOPE_SITELOCAL 2 // Site-local scope (deprecated). +#define IPV6_SCOPE_UNIQUELOCAL 3 // Unique local +#define IPV6_SCOPE_NODELOCAL 4 // Loopback + +// Return the scope of the given address. +static int ipv6_scope(const unsigned char addr[16]) { + const unsigned char* b = addr; + unsigned short w = (unsigned short)((b[0] << 8) | b[1]); + + if ((b[0] & 0xFE) == 0xFC) { + return IPV6_SCOPE_UNIQUELOCAL; + } + switch (w & 0xFFC0) { + case 0xFE80: + return IPV6_SCOPE_LINKLOCAL; + case 0xFEC0: + return IPV6_SCOPE_SITELOCAL; + case 0x0000: + w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] | b[10] | + b[11] | b[12] | b[13] | b[14]; + if (w || b[15] != 0x01) { + break; + } + return IPV6_SCOPE_NODELOCAL; + default: + break; + } + + return IPV6_SCOPE_GLOBAL; +} + +} // namespace utils +} // namespace net +} // namespace mozilla + +#endif // NETWORK_IPV6_UTILS_H_ -- cgit v1.2.3