diff src/lib/network.c @ 5865:22b300677807 HEAD

Added net_ip_hash().
author Timo Sirainen <tss@iki.fi>
date Mon, 02 Jul 2007 23:11:46 +0300
parents e383a6b4f4da
children 74a6130211c2
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