2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2013-09-17 19:46:56 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "InetAddress.hpp"
|
2013-08-30 21:05:43 +00:00
|
|
|
#include "Utils.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-04-14 21:49:34 +00:00
|
|
|
const InetAddress InetAddress::LO4((const void *)("\x7f\x00\x00\x01"),4,0);
|
|
|
|
const InetAddress InetAddress::LO6((const void *)("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"),16,0);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-04-07 17:57:59 +00:00
|
|
|
InetAddress::IpScope InetAddress::ipScope() const
|
|
|
|
{
|
|
|
|
switch(ss_family) {
|
|
|
|
|
|
|
|
case AF_INET: {
|
|
|
|
const uint32_t ip = Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
|
|
|
|
switch(ip >> 24) {
|
2019-03-28 20:58:19 +00:00
|
|
|
case 0x00: return IP_SCOPE_NONE; // 0.0.0.0/8 (reserved, never used)
|
|
|
|
case 0x06: return IP_SCOPE_PSEUDOPRIVATE; // 6.0.0.0/8 (US Army)
|
|
|
|
case 0x0a: return IP_SCOPE_PRIVATE; // 10.0.0.0/8
|
|
|
|
case 0x0b: return IP_SCOPE_PSEUDOPRIVATE; // 11.0.0.0/8 (US DoD)
|
|
|
|
case 0x15: return IP_SCOPE_PSEUDOPRIVATE; // 21.0.0.0/8 (US DDN-RVN)
|
|
|
|
case 0x16: return IP_SCOPE_PSEUDOPRIVATE; // 22.0.0.0/8 (US DISA)
|
|
|
|
case 0x19: return IP_SCOPE_PSEUDOPRIVATE; // 25.0.0.0/8 (UK Ministry of Defense)
|
|
|
|
case 0x1a: return IP_SCOPE_PSEUDOPRIVATE; // 26.0.0.0/8 (US DISA)
|
|
|
|
case 0x1c: return IP_SCOPE_PSEUDOPRIVATE; // 28.0.0.0/8 (US DSI-North)
|
|
|
|
case 0x1d: return IP_SCOPE_PSEUDOPRIVATE; // 29.0.0.0/8 (US DISA)
|
|
|
|
case 0x1e: return IP_SCOPE_PSEUDOPRIVATE; // 30.0.0.0/8 (US DISA)
|
|
|
|
case 0x33: return IP_SCOPE_PSEUDOPRIVATE; // 51.0.0.0/8 (UK Department of Social Security)
|
|
|
|
case 0x37: return IP_SCOPE_PSEUDOPRIVATE; // 55.0.0.0/8 (US DoD)
|
|
|
|
case 0x38: return IP_SCOPE_PSEUDOPRIVATE; // 56.0.0.0/8 (US Postal Service)
|
2015-04-07 17:57:59 +00:00
|
|
|
case 0x64:
|
2019-03-28 20:58:19 +00:00
|
|
|
if ((ip & 0xffc00000) == 0x64400000) return IP_SCOPE_PRIVATE; // 100.64.0.0/10
|
2015-04-07 17:57:59 +00:00
|
|
|
break;
|
2019-03-28 20:58:19 +00:00
|
|
|
case 0x7f: return IP_SCOPE_LOOPBACK; // 127.0.0.0/8
|
2015-04-07 17:57:59 +00:00
|
|
|
case 0xa9:
|
2019-03-28 20:58:19 +00:00
|
|
|
if ((ip & 0xffff0000) == 0xa9fe0000) return IP_SCOPE_LINK_LOCAL; // 169.254.0.0/16
|
2015-04-07 17:57:59 +00:00
|
|
|
break;
|
|
|
|
case 0xac:
|
2019-03-28 20:58:19 +00:00
|
|
|
if ((ip & 0xfff00000) == 0xac100000) return IP_SCOPE_PRIVATE; // 172.16.0.0/12
|
2015-04-07 17:57:59 +00:00
|
|
|
break;
|
|
|
|
case 0xc0:
|
2019-03-28 20:58:19 +00:00
|
|
|
if ((ip & 0xffff0000) == 0xc0a80000) return IP_SCOPE_PRIVATE; // 192.168.0.0/16
|
2020-09-16 17:24:36 +00:00
|
|
|
if ((ip & 0xffffff00) == 0xc0000200) return IP_SCOPE_PRIVATE; // 192.0.2.0/24
|
2015-04-07 17:57:59 +00:00
|
|
|
break;
|
2020-09-16 17:15:42 +00:00
|
|
|
case 0xc6:
|
2020-09-16 17:24:36 +00:00
|
|
|
if ((ip & 0xfffe0000) == 0xc6120000) return IP_SCOPE_PRIVATE; // 198.18.0.0/15
|
|
|
|
if ((ip & 0xffffff00) == 0xc6336400) return IP_SCOPE_PRIVATE; // 198.51.100.0/24
|
2020-09-16 17:15:42 +00:00
|
|
|
break;
|
2020-09-16 17:24:36 +00:00
|
|
|
case 0xcb:
|
|
|
|
if ((ip & 0xffffff00) == 0xcb007100) return IP_SCOPE_PRIVATE; // 203.0.113.0/24
|
2020-11-23 22:54:13 +00:00
|
|
|
break;
|
2019-03-28 20:58:19 +00:00
|
|
|
case 0xff: return IP_SCOPE_NONE; // 255.0.0.0/8 (broadcast, or unused/unusable)
|
2015-04-07 17:57:59 +00:00
|
|
|
}
|
2015-10-19 22:19:04 +00:00
|
|
|
switch(ip >> 28) {
|
2019-03-28 20:58:19 +00:00
|
|
|
case 0xe: return IP_SCOPE_MULTICAST; // 224.0.0.0/4
|
|
|
|
case 0xf: return IP_SCOPE_PSEUDOPRIVATE; // 240.0.0.0/4 ("reserved," usually unusable)
|
2015-10-19 22:19:04 +00:00
|
|
|
}
|
|
|
|
return IP_SCOPE_GLOBAL;
|
2015-04-07 17:57:59 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case AF_INET6: {
|
|
|
|
const unsigned char *ip = reinterpret_cast<const unsigned char *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
if ((ip[0] & 0xf0) == 0xf0) {
|
2019-03-28 20:58:19 +00:00
|
|
|
if (ip[0] == 0xff) return IP_SCOPE_MULTICAST; // ff00::/8
|
2015-07-06 23:32:34 +00:00
|
|
|
if ((ip[0] == 0xfe)&&((ip[1] & 0xc0) == 0x80)) {
|
|
|
|
unsigned int k = 2;
|
|
|
|
while ((!ip[k])&&(k < 15)) ++k;
|
|
|
|
if ((k == 15)&&(ip[15] == 0x01))
|
2019-03-28 20:58:19 +00:00
|
|
|
return IP_SCOPE_LOOPBACK; // fe80::1/128
|
2018-06-08 00:25:27 +00:00
|
|
|
else return IP_SCOPE_LINK_LOCAL; // fe80::/10
|
2015-07-06 23:32:34 +00:00
|
|
|
}
|
2019-03-28 20:58:19 +00:00
|
|
|
if ((ip[0] & 0xfe) == 0xfc) return IP_SCOPE_PRIVATE; // fc00::/7
|
2015-04-07 17:57:59 +00:00
|
|
|
}
|
|
|
|
unsigned int k = 0;
|
|
|
|
while ((!ip[k])&&(k < 15)) ++k;
|
|
|
|
if (k == 15) { // all 0's except last byte
|
2019-03-28 20:58:19 +00:00
|
|
|
if (ip[15] == 0x01) return IP_SCOPE_LOOPBACK; // ::1/128
|
|
|
|
if (ip[15] == 0x00) return IP_SCOPE_NONE; // ::/128
|
2015-04-07 17:57:59 +00:00
|
|
|
}
|
|
|
|
return IP_SCOPE_GLOBAL;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return IP_SCOPE_NONE;
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:59:08 +00:00
|
|
|
void InetAddress::set(const void *ipBytes,unsigned int ipLen,unsigned int port)
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
memset(this,0,sizeof(InetAddress));
|
2014-07-02 22:59:08 +00:00
|
|
|
if (ipLen == 4) {
|
2016-02-01 22:41:54 +00:00
|
|
|
uint32_t ipb[1];
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(ipb,ipBytes,4);
|
2015-04-14 21:49:34 +00:00
|
|
|
ss_family = AF_INET;
|
2016-02-01 22:41:54 +00:00
|
|
|
reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr = ipb[0];
|
2015-04-14 21:49:34 +00:00
|
|
|
reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
|
2014-07-02 22:59:08 +00:00
|
|
|
} else if (ipLen == 16) {
|
2015-04-14 21:49:34 +00:00
|
|
|
ss_family = AF_INET6;
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,ipBytes,16);
|
2015-04-14 21:49:34 +00:00
|
|
|
reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
|
2014-07-02 22:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
char *InetAddress::toString(char buf[64]) const
|
2014-08-13 00:20:34 +00:00
|
|
|
{
|
2017-07-06 23:11:11 +00:00
|
|
|
char *p = toIpString(buf);
|
|
|
|
if (*p) {
|
|
|
|
while (*p) ++p;
|
|
|
|
*(p++) = '/';
|
|
|
|
Utils::decimal(port(),p);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
2017-07-06 23:11:11 +00:00
|
|
|
return buf;
|
2014-08-13 00:20:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
char *InetAddress::toIpString(char buf[64]) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2018-09-15 19:13:06 +00:00
|
|
|
buf[0] = (char)0;
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
2017-07-06 23:11:11 +00:00
|
|
|
case AF_INET: {
|
2018-10-23 20:21:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
inet_ntop(AF_INET, (void*)&reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr, buf, INET_ADDRSTRLEN);
|
|
|
|
#else
|
2018-09-15 05:42:58 +00:00
|
|
|
inet_ntop(AF_INET, &reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr, buf, INET_ADDRSTRLEN);
|
2018-10-23 20:21:25 +00:00
|
|
|
#endif
|
2018-09-15 05:42:58 +00:00
|
|
|
} break;
|
2017-07-06 23:11:11 +00:00
|
|
|
|
|
|
|
case AF_INET6: {
|
2018-10-23 20:21:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
inet_ntop(AF_INET6, (void*)reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr, buf, INET6_ADDRSTRLEN);
|
|
|
|
#else
|
2018-09-15 05:42:58 +00:00
|
|
|
inet_ntop(AF_INET6, reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr, buf, INET6_ADDRSTRLEN);
|
2018-10-23 20:21:25 +00:00
|
|
|
#endif
|
2017-07-06 23:11:11 +00:00
|
|
|
} break;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2017-07-06 23:11:11 +00:00
|
|
|
return buf;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
bool InetAddress::fromString(const char *ipSlashPort)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-07-06 23:11:11 +00:00
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
|
|
|
|
if (!*ipSlashPort)
|
|
|
|
return true;
|
|
|
|
if (!Utils::scopy(buf,sizeof(buf),ipSlashPort))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *portAt = buf;
|
|
|
|
while ((*portAt)&&(*portAt != '/'))
|
|
|
|
++portAt;
|
|
|
|
unsigned int port = 0;
|
|
|
|
if (*portAt) {
|
|
|
|
*(portAt++) = (char)0;
|
|
|
|
port = Utils::strToUInt(portAt) & 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr(buf,':')) {
|
|
|
|
struct sockaddr_in6 *const in6 = reinterpret_cast<struct sockaddr_in6 *>(this);
|
2018-09-15 05:42:58 +00:00
|
|
|
inet_pton(AF_INET6, buf, &in6->sin6_addr.s6_addr);
|
2017-07-06 23:11:11 +00:00
|
|
|
in6->sin6_family = AF_INET6;
|
|
|
|
in6->sin6_port = Utils::hton((uint16_t)port);
|
|
|
|
return true;
|
|
|
|
} else if (strchr(buf,'.')) {
|
|
|
|
struct sockaddr_in *const in = reinterpret_cast<struct sockaddr_in *>(this);
|
2018-09-15 05:42:58 +00:00
|
|
|
inet_pton(AF_INET, buf, &in->sin_addr.s_addr);
|
2017-07-06 23:11:11 +00:00
|
|
|
in->sin_family = AF_INET;
|
|
|
|
in->sin_port = Utils::hton((uint16_t)port);
|
|
|
|
return true;
|
2015-04-14 21:49:34 +00:00
|
|
|
} else {
|
2017-07-06 23:11:11 +00:00
|
|
|
return false;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:59:08 +00:00
|
|
|
InetAddress InetAddress::netmask() const
|
|
|
|
{
|
|
|
|
InetAddress r(*this);
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(r.ss_family) {
|
2014-07-02 22:59:08 +00:00
|
|
|
case AF_INET:
|
2015-04-01 00:53:34 +00:00
|
|
|
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr = Utils::hton((uint32_t)(0xffffffff << (32 - netmaskBits())));
|
2014-07-02 22:59:08 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6: {
|
2015-11-12 18:47:50 +00:00
|
|
|
uint64_t nm[2];
|
|
|
|
const unsigned int bits = netmaskBits();
|
2017-07-06 23:11:11 +00:00
|
|
|
if(bits) {
|
|
|
|
nm[0] = Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
|
|
|
nm[1] = Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
|
|
|
} else {
|
|
|
|
nm[0] = 0;
|
|
|
|
nm[1] = 0;
|
|
|
|
}
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
|
2014-07-02 22:59:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-09-04 17:36:25 +00:00
|
|
|
InetAddress InetAddress::broadcast() const
|
2015-11-12 18:47:50 +00:00
|
|
|
{
|
|
|
|
if (ss_family == AF_INET) {
|
|
|
|
InetAddress r(*this);
|
|
|
|
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr |= Utils::hton((uint32_t)(0xffffffff >> netmaskBits()));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return InetAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
InetAddress InetAddress::network() const
|
2014-09-04 17:36:25 +00:00
|
|
|
{
|
|
|
|
InetAddress r(*this);
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(r.ss_family) {
|
2014-09-04 17:36:25 +00:00
|
|
|
case AF_INET:
|
2015-11-12 18:47:50 +00:00
|
|
|
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr &= Utils::hton((uint32_t)(0xffffffff << (32 - netmaskBits())));
|
2014-09-04 17:36:25 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6: {
|
2015-11-12 18:47:50 +00:00
|
|
|
uint64_t nm[2];
|
|
|
|
const unsigned int bits = netmaskBits();
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(nm,reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,16);
|
2015-11-12 18:47:50 +00:00
|
|
|
nm[0] &= Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
|
|
|
nm[1] &= Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
|
2014-09-04 17:36:25 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-10-25 19:42:14 +00:00
|
|
|
bool InetAddress::isEqualPrefix(const InetAddress &addr) const
|
|
|
|
{
|
|
|
|
if (addr.ss_family == ss_family) {
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET6: {
|
|
|
|
const InetAddress mask(netmask());
|
|
|
|
InetAddress addr_mask(addr.netmask());
|
|
|
|
const uint8_t *n = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr_mask)->sin6_addr.s6_addr);
|
|
|
|
const uint8_t *m = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&mask)->sin6_addr.s6_addr);
|
|
|
|
const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_addr.s6_addr);
|
|
|
|
const uint8_t *b = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
for(unsigned int i=0;i<16;++i) {
|
|
|
|
if ((a[i] & m[i]) != (b[i] & n[i]))
|
|
|
|
return false;
|
2017-05-04 22:25:48 +00:00
|
|
|
}
|
2017-10-25 19:42:14 +00:00
|
|
|
return true;
|
2017-05-04 22:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 19:42:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-29 17:44:57 +00:00
|
|
|
|
2016-01-11 18:17:44 +00:00
|
|
|
bool InetAddress::containsAddress(const InetAddress &addr) const
|
|
|
|
{
|
|
|
|
if (addr.ss_family == ss_family) {
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: {
|
|
|
|
const unsigned int bits = netmaskBits();
|
2016-06-17 01:23:33 +00:00
|
|
|
if (bits == 0)
|
|
|
|
return true;
|
2016-01-11 18:17:44 +00:00
|
|
|
return ( (Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(&addr)->sin_addr.s_addr) >> (32 - bits)) == (Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr) >> (32 - bits)) );
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
|
|
|
const InetAddress mask(netmask());
|
|
|
|
const uint8_t *m = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&mask)->sin6_addr.s6_addr);
|
|
|
|
const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_addr.s6_addr);
|
|
|
|
const uint8_t *b = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
for(unsigned int i=0;i<16;++i) {
|
|
|
|
if ((a[i] & m[i]) != b[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:21:28 +00:00
|
|
|
bool InetAddress::isNetwork() const
|
|
|
|
{
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: {
|
|
|
|
unsigned int bits = netmaskBits();
|
|
|
|
if (bits <= 0)
|
|
|
|
return false;
|
|
|
|
if (bits >= 32)
|
|
|
|
return false;
|
|
|
|
uint32_t ip = Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
|
|
|
|
return ((ip & (0xffffffff >> bits)) == 0);
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
|
|
|
unsigned int bits = netmaskBits();
|
|
|
|
if (bits <= 0)
|
|
|
|
return false;
|
|
|
|
if (bits >= 128)
|
|
|
|
return false;
|
|
|
|
const unsigned char *ip = reinterpret_cast<const unsigned char *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
unsigned int p = bits / 8;
|
|
|
|
if ((ip[p++] & (0xff >> (bits % 8))) != 0)
|
|
|
|
return false;
|
|
|
|
while (p < 16) {
|
|
|
|
if (ip[p++])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:49:34 +00:00
|
|
|
bool InetAddress::operator==(const InetAddress &a) const
|
|
|
|
{
|
|
|
|
if (ss_family == a.ss_family) {
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return (
|
|
|
|
(reinterpret_cast<const struct sockaddr_in *>(this)->sin_port == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_port)&&
|
|
|
|
(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
return (
|
|
|
|
(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_port)&&
|
|
|
|
(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_flowinfo == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_flowinfo)&&
|
|
|
|
(memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0)&&
|
|
|
|
(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_scope_id == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_scope_id));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (memcmp(this,&a,sizeof(InetAddress)) == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InetAddress::operator<(const InetAddress &a) const
|
|
|
|
{
|
2015-04-14 21:52:23 +00:00
|
|
|
if (ss_family < a.ss_family)
|
|
|
|
return true;
|
|
|
|
else if (ss_family == a.ss_family) {
|
2015-04-14 21:49:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (reinterpret_cast<const struct sockaddr_in *>(this)->sin_port < reinterpret_cast<const struct sockaddr_in *>(&a)->sin_port)
|
|
|
|
return true;
|
2015-04-14 21:52:23 +00:00
|
|
|
else if (reinterpret_cast<const struct sockaddr_in *>(this)->sin_port == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_port) {
|
|
|
|
if (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr < reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr)
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-14 21:49:34 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port < reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_port)
|
|
|
|
return true;
|
2015-04-14 21:52:23 +00:00
|
|
|
else if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_port) {
|
|
|
|
if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_flowinfo < reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_flowinfo)
|
|
|
|
return true;
|
|
|
|
else if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_flowinfo == reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_flowinfo) {
|
|
|
|
if (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) < 0)
|
|
|
|
return true;
|
|
|
|
else if (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0) {
|
|
|
|
if (reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_scope_id < reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_scope_id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 21:49:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (memcmp(this,&a,sizeof(InetAddress)) < 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:59:08 +00:00
|
|
|
InetAddress InetAddress::makeIpv6LinkLocal(const MAC &mac)
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_addr.s6_addr[0] = 0xfe;
|
|
|
|
sin6.sin6_addr.s6_addr[1] = 0x80;
|
|
|
|
sin6.sin6_addr.s6_addr[2] = 0x00;
|
|
|
|
sin6.sin6_addr.s6_addr[3] = 0x00;
|
|
|
|
sin6.sin6_addr.s6_addr[4] = 0x00;
|
|
|
|
sin6.sin6_addr.s6_addr[5] = 0x00;
|
|
|
|
sin6.sin6_addr.s6_addr[6] = 0x00;
|
|
|
|
sin6.sin6_addr.s6_addr[7] = 0x00;
|
|
|
|
sin6.sin6_addr.s6_addr[8] = mac[0] & 0xfd;
|
|
|
|
sin6.sin6_addr.s6_addr[9] = mac[1];
|
|
|
|
sin6.sin6_addr.s6_addr[10] = mac[2];
|
|
|
|
sin6.sin6_addr.s6_addr[11] = 0xff;
|
|
|
|
sin6.sin6_addr.s6_addr[12] = 0xfe;
|
|
|
|
sin6.sin6_addr.s6_addr[13] = mac[3];
|
|
|
|
sin6.sin6_addr.s6_addr[14] = mac[4];
|
|
|
|
sin6.sin6_addr.s6_addr[15] = mac[5];
|
|
|
|
sin6.sin6_port = Utils::hton((uint16_t)64);
|
|
|
|
return InetAddress(sin6);
|
2014-07-02 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 04:30:32 +00:00
|
|
|
InetAddress InetAddress::makeIpv6rfc4193(uint64_t nwid,uint64_t zeroTierAddress)
|
|
|
|
{
|
|
|
|
InetAddress r;
|
|
|
|
struct sockaddr_in6 *const sin6 = reinterpret_cast<struct sockaddr_in6 *>(&r);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr.s6_addr[0] = 0xfd;
|
|
|
|
sin6->sin6_addr.s6_addr[1] = (uint8_t)(nwid >> 56);
|
|
|
|
sin6->sin6_addr.s6_addr[2] = (uint8_t)(nwid >> 48);
|
|
|
|
sin6->sin6_addr.s6_addr[3] = (uint8_t)(nwid >> 40);
|
|
|
|
sin6->sin6_addr.s6_addr[4] = (uint8_t)(nwid >> 32);
|
|
|
|
sin6->sin6_addr.s6_addr[5] = (uint8_t)(nwid >> 24);
|
|
|
|
sin6->sin6_addr.s6_addr[6] = (uint8_t)(nwid >> 16);
|
|
|
|
sin6->sin6_addr.s6_addr[7] = (uint8_t)(nwid >> 8);
|
|
|
|
sin6->sin6_addr.s6_addr[8] = (uint8_t)nwid;
|
|
|
|
sin6->sin6_addr.s6_addr[9] = 0x99;
|
|
|
|
sin6->sin6_addr.s6_addr[10] = 0x93;
|
|
|
|
sin6->sin6_addr.s6_addr[11] = (uint8_t)(zeroTierAddress >> 32);
|
|
|
|
sin6->sin6_addr.s6_addr[12] = (uint8_t)(zeroTierAddress >> 24);
|
|
|
|
sin6->sin6_addr.s6_addr[13] = (uint8_t)(zeroTierAddress >> 16);
|
|
|
|
sin6->sin6_addr.s6_addr[14] = (uint8_t)(zeroTierAddress >> 8);
|
|
|
|
sin6->sin6_addr.s6_addr[15] = (uint8_t)zeroTierAddress;
|
|
|
|
sin6->sin6_port = Utils::hton((uint16_t)88); // /88 includes 0xfd + network ID, discriminating by device ID below that
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:40:50 +00:00
|
|
|
InetAddress InetAddress::makeIpv66plane(uint64_t nwid,uint64_t zeroTierAddress)
|
|
|
|
{
|
|
|
|
nwid ^= (nwid >> 32);
|
|
|
|
InetAddress r;
|
|
|
|
struct sockaddr_in6 *const sin6 = reinterpret_cast<struct sockaddr_in6 *>(&r);
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_addr.s6_addr[0] = 0xfc;
|
|
|
|
sin6->sin6_addr.s6_addr[1] = (uint8_t)(nwid >> 24);
|
|
|
|
sin6->sin6_addr.s6_addr[2] = (uint8_t)(nwid >> 16);
|
|
|
|
sin6->sin6_addr.s6_addr[3] = (uint8_t)(nwid >> 8);
|
|
|
|
sin6->sin6_addr.s6_addr[4] = (uint8_t)nwid;
|
|
|
|
sin6->sin6_addr.s6_addr[5] = (uint8_t)(zeroTierAddress >> 32);
|
|
|
|
sin6->sin6_addr.s6_addr[6] = (uint8_t)(zeroTierAddress >> 24);
|
|
|
|
sin6->sin6_addr.s6_addr[7] = (uint8_t)(zeroTierAddress >> 16);
|
|
|
|
sin6->sin6_addr.s6_addr[8] = (uint8_t)(zeroTierAddress >> 8);
|
|
|
|
sin6->sin6_addr.s6_addr[9] = (uint8_t)zeroTierAddress;
|
|
|
|
sin6->sin6_addr.s6_addr[15] = 0x01;
|
|
|
|
sin6->sin6_port = Utils::hton((uint16_t)40);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
} // namespace ZeroTier
|