[quagga-dev 12199] [PATCH 7/7] sockunion: add hash function

Timo Teräs timo.teras at iki.fi
Wed Apr 29 07:43:05 BST 2015


Signed-off-by: Timo Teräs <timo.teras at iki.fi>
---
 lib/sockunion.c | 16 ++++++++++++++++
 lib/sockunion.h |  1 +
 2 files changed, 17 insertions(+)

diff --git a/lib/sockunion.c b/lib/sockunion.c
index 5e6cf68..c7315f2 100644
--- a/lib/sockunion.c
+++ b/lib/sockunion.c
@@ -27,6 +27,7 @@
 #include "memory.h"
 #include "str.h"
 #include "log.h"
+#include "jhash.h"
 
 #ifndef HAVE_INET_ATON
 int
@@ -573,6 +574,21 @@ sockunion_same (const union sockunion *su1, const union sockunion *su2)
     return 0;
 }
 
+unsigned int
+sockunion_hash (const union sockunion *su)
+{
+  switch (sockunion_family(su))
+    {
+    case AF_INET:
+      return jhash_1word(su->sin.sin_addr.s_addr, 0);
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      return jhash2(su->sin6.sin6_addr.s6_addr32, ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
+#endif /* HAVE_IPV6 */
+    }
+  return 0;
+}
+
 /* After TCP connection is established.  Get local address and port. */
 union sockunion *
 sockunion_getsockname (int fd)
diff --git a/lib/sockunion.h b/lib/sockunion.h
index 2c79fb5..ea3b833 100644
--- a/lib/sockunion.h
+++ b/lib/sockunion.h
@@ -87,6 +87,7 @@ extern int str2sockunion (const char *, union sockunion *);
 extern const char *sockunion2str (const union sockunion *, char *, size_t);
 extern int sockunion_cmp (const union sockunion *, const union sockunion *);
 extern int sockunion_same (const union sockunion *, const union sockunion *);
+extern unsigned int sockunion_hash (const union sockunion *);
 
 extern union sockunion *sockunion_str2su (const char *str);
 extern int sockunion_accept (int sock, union sockunion *);
-- 
2.3.6





More information about the Quagga-dev mailing list