changeset 5865:22b300677807 HEAD

Added net_ip_hash().
author Timo Sirainen <tss@iki.fi>
date Mon, 02 Jul 2007 23:11:46 +0300
parents c554a52f1b3d
children 092b9d3b7f97
files src/lib/network.c src/lib/network.h
diffstat 2 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/network.c	Mon Jul 02 22:48:01 2007 +0300
+++ b/src/lib/network.c	Mon Jul 02 23:11:46 2007 +0300
@@ -39,6 +39,28 @@
 	return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
 }
 
+unsigned int net_ip_hash(const struct ip_addr *ip)
+{
+        const unsigned char *p = (const unsigned char *)&ip->ip;
+	unsigned int len, g, h = 0;
+
+#ifdef HAVE_IPV6
+	if (ip->family == AF_INET6)
+		len = sizeof(ip->ip);
+	else
+#endif
+		len = 4;
+
+	for (; len > 0; len--, p++) {
+		h = (h << 4) + *p;
+		if ((g = h & 0xf0000000UL)) {
+			h = h ^ (g >> 24);
+			h = h ^ g;
+		}
+	}
+
+	return h;
+}
 
 /* copy IP to sockaddr */
 static inline void
--- a/src/lib/network.h	Mon Jul 02 22:48:01 2007 +0300
+++ b/src/lib/network.h	Mon Jul 02 23:11:46 2007 +0300
@@ -41,6 +41,7 @@
 
 /* returns 1 if IPADDRs are the same */
 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
+unsigned int net_ip_hash(const struct ip_addr *ip);
 
 /* Connect to socket with ip address */
 int net_connect_ip(const struct ip_addr *ip, unsigned int port,