sockunion: add hash function

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
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 @@
     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 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 *);