summaryrefslogtreecommitdiff
path: root/node/InetAddress.hpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2016-11-18 12:59:04 -0800
committerAdam Ierymenko <adam.ierymenko@gmail.com>2016-11-18 12:59:04 -0800
commit2ea9f516e121ea6eb344a8d180a739a1d707aecb (patch)
tree5d1e02ed53e797f277de06f18fc64626cebe737e /node/InetAddress.hpp
parentab4021dd0ee37af0af4137dc772911ea8ec52bb2 (diff)
downloadinfinitytier-2ea9f516e121ea6eb344a8d180a739a1d707aecb.tar.gz
infinitytier-2ea9f516e121ea6eb344a8d180a739a1d707aecb.zip
Rate gate expensive validation of new identities in HELLO.
Diffstat (limited to 'node/InetAddress.hpp')
-rw-r--r--node/InetAddress.hpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp
index 6f070fbf..1dff710d 100644
--- a/node/InetAddress.hpp
+++ b/node/InetAddress.hpp
@@ -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); }