From 8ac411a9e41ce62a6a6340f693ba70a59550df75 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 12 Nov 2015 10:47:50 -0800 Subject: Stashing utun work for OSX -- abandoning for now since utun excludes mDNS among other issues. --- node/InetAddress.cpp | 41 ++++++++++++++++++++++------------------- node/InetAddress.hpp | 18 ++++++++++++++---- 2 files changed, 36 insertions(+), 23 deletions(-) (limited to 'node') diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp index abb46240..f35eb9c3 100644 --- a/node/InetAddress.cpp +++ b/node/InetAddress.cpp @@ -234,7 +234,6 @@ void InetAddress::fromString(const std::string &ipSlashPort) } InetAddress InetAddress::netmask() const - throw() { InetAddress r(*this); switch(r.ss_family) { @@ -242,36 +241,40 @@ InetAddress InetAddress::netmask() const reinterpret_cast(&r)->sin_addr.s_addr = Utils::hton((uint32_t)(0xffffffff << (32 - netmaskBits()))); break; case AF_INET6: { - unsigned char *bf = reinterpret_cast(reinterpret_cast(&r)->sin6_addr.s6_addr); - signed int bitsLeft = (signed int)netmaskBits(); - for(unsigned int i=0;i<16;++i) { - if (bitsLeft > 0) { - bf[i] |= (unsigned char)((bitsLeft >= 8) ? 0x00 : (0xff >> bitsLeft)); - bitsLeft -= 8; - } - } + uint64_t nm[2]; + const unsigned int bits = netmaskBits(); + nm[0] = Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits)))); + nm[1] = Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits)))); + memcpy(reinterpret_cast(&r)->sin6_addr.s6_addr,nm,16); } break; } return r; } InetAddress InetAddress::broadcast() const - throw() +{ + if (ss_family == AF_INET) { + InetAddress r(*this); + reinterpret_cast(&r)->sin_addr.s_addr |= Utils::hton((uint32_t)(0xffffffff >> netmaskBits())); + return r; + } + return InetAddress(); +} + +InetAddress InetAddress::network() const { InetAddress r(*this); switch(r.ss_family) { case AF_INET: - reinterpret_cast(&r)->sin_addr.s_addr |= Utils::hton((uint32_t)(0xffffffff >> netmaskBits())); + reinterpret_cast(&r)->sin_addr.s_addr &= Utils::hton((uint32_t)(0xffffffff << (32 - netmaskBits()))); break; case AF_INET6: { - unsigned char *bf = reinterpret_cast(reinterpret_cast(&r)->sin6_addr.s6_addr); - signed int bitsLeft = (signed int)netmaskBits(); - for(unsigned int i=0;i<16;++i) { - if (bitsLeft > 0) { - bf[i] |= (unsigned char)((bitsLeft >= 8) ? 0x00 : (0xff >> bitsLeft)); - bitsLeft -= 8; - } - } + uint64_t nm[2]; + const unsigned int bits = netmaskBits(); + memcpy(nm,reinterpret_cast(&r)->sin6_addr.s6_addr,16); + nm[0] &= Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits)))); + nm[1] &= Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits)))); + memcpy(reinterpret_cast(&r)->sin6_addr.s6_addr,nm,16); } break; } return r; diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp index 74efc943..2573e694 100644 --- a/node/InetAddress.hpp +++ b/node/InetAddress.hpp @@ -303,17 +303,27 @@ struct InetAddress : public sockaddr_storage /** * Construct a full netmask as an InetAddress + * + * @return Netmask such as 255.255.255.0 if this address is /24 (port field will be unchanged) */ - InetAddress netmask() const - throw(); + InetAddress netmask() const; /** * Constructs a broadcast address from a network/netmask address * + * This is only valid for IPv4 and will return a NULL InetAddress for other + * address families. + * * @return Broadcast address (only IP portion is meaningful) */ - InetAddress broadcast() const - throw(); + InetAddress broadcast() const; + + /** + * Return the network -- a.k.a. the IP ANDed with the netmask + * + * @return Network e.g. 10.0.1.0/24 from 10.0.1.200/24 + */ + InetAddress network() const; /** * @return True if this is an IPv4 address -- cgit v1.2.3