diff options
| author | Grant Limberg <grant.limberg@zerotier.com> | 2016-11-18 14:00:25 -0800 |
|---|---|---|
| committer | Grant Limberg <grant.limberg@zerotier.com> | 2016-11-18 14:00:25 -0800 |
| commit | 2231e878d5470d86f4c6543cc708dc78661da462 (patch) | |
| tree | 0bcd8215684b0091d7bd8856f75b4e433edef71e /node/InetAddress.hpp | |
| parent | 299a7cab200c0af4743ab36d41994fd7a582f900 (diff) | |
| parent | 673c0c811ea443c217b3a4ca17eeaed3ab596501 (diff) | |
| download | infinitytier-2231e878d5470d86f4c6543cc708dc78661da462.tar.gz infinitytier-2231e878d5470d86f4c6543cc708dc78661da462.zip | |
Merge branch 'dev' into systemtray
Diffstat (limited to 'node/InetAddress.hpp')
| -rw-r--r-- | node/InetAddress.hpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp index 6f070fbf..c37fa621 100644 --- a/node/InetAddress.hpp +++ b/node/InetAddress.hpp @@ -427,7 +427,7 @@ struct InetAddress : public sockaddr_storage } else { unsigned long tmp = reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port; const uint8_t *a = reinterpret_cast<const uint8_t *>(this); - for(long i=0;i<sizeof(InetAddress);++i) + for(long i=0;i<(long)sizeof(InetAddress);++i) reinterpret_cast<uint8_t *>(&tmp)[i % sizeof(tmp)] ^= a[i]; return tmp; } @@ -450,6 +450,30 @@ struct InetAddress : public sockaddr_storage throw(); /** + * @return 14-bit (0-16383) hash of this IP's first 24 or 48 bits (for V4 or V6) for rate limiting code, or 0 if non-IP + */ + inline unsigned long rateGateHash() const + { + unsigned long h = 0; + switch(ss_family) { + case AF_INET: + h = (Utils::ntoh((uint32_t)reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr) & 0xffffff00) >> 8; + h ^= (h >> 14); + break; + case AF_INET6: { + const uint8_t *ip = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr); + h = ((unsigned long)ip[0]); h <<= 1; + h += ((unsigned long)ip[1]); h <<= 1; + h += ((unsigned long)ip[2]); h <<= 1; + h += ((unsigned long)ip[3]); h <<= 1; + h += ((unsigned long)ip[4]); h <<= 1; + h += ((unsigned long)ip[5]); + } break; + } + return (h & 0x3fff); + } + + /** * @return True if address family is non-zero */ inline operator bool() const throw() { return (ss_family != 0); } |
