]> git.puffer.fish Git - matthieu/frr.git/commitdiff
sockunion: add accessors for sockunion address
authorTimo Teräs <timo.teras@iki.fi>
Fri, 22 May 2015 10:40:59 +0000 (13:40 +0300)
committerDaniel Walton <dwalton@cumulusnetworks.com>
Thu, 26 May 2016 15:33:30 +0000 (15:33 +0000)
Upcoming nhrp code will use this, and it can be used to remove
the sockunion2ip(X) macro.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
(cherry picked from commit 483abc037b0ac4b3ed168c4810bb14ea338fa80c)

lib/sockunion.c
lib/sockunion.h

index 2dbe34c21a0418f6ca6d27985dca8b05fc511b58..fdff61f542cc587d37b1afa230255a2d7d439445 100644 (file)
@@ -623,6 +623,62 @@ sockunion_hash (const union sockunion *su)
   return 0;
 }
 
+size_t
+family2addrsize(int family)
+{
+  switch (family)
+    {
+    case AF_INET:
+      return sizeof(struct in_addr);
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      return sizeof(struct in6_addr);
+#endif /* HAVE_IPV6 */
+    }
+  return 0;
+}
+
+size_t
+sockunion_get_addrlen(const union sockunion *su)
+{
+  return family2addrsize(sockunion_family(su));
+}
+
+const u_char *
+sockunion_get_addr(const union sockunion *su)
+{
+  switch (sockunion_family(su))
+    {
+    case AF_INET:
+      return (const u_char *) &su->sin.sin_addr.s_addr;
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      return (const u_char *) &su->sin6.sin6_addr;
+#endif /* HAVE_IPV6 */
+    }
+  return NULL;
+}
+
+void
+sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes)
+{
+  if (family2addrsize(family) != bytes)
+    return;
+
+  sockunion_family(su) = family;
+  switch (family)
+    {
+    case AF_INET:
+      memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
+      break;
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      memcpy(&su->sin6.sin6_addr, addr, bytes);
+      break;
+#endif /* HAVE_IPV6 */
+    }
+}
+
 /* After TCP connection is established.  Get local address and port. */
 union sockunion *
 sockunion_getsockname (int fd)
index f041f95a1023b7c2a93c32a852f55d58aa879c4d..c674cb8bb8b0804d4dcd4669cb6908a049c11755 100644 (file)
@@ -74,6 +74,11 @@ extern int sockunion_cmp (union sockunion *, union sockunion *);
 extern int sockunion_same (const union sockunion *, const union sockunion *);
 extern unsigned int sockunion_hash (const union sockunion *);
 
+extern size_t family2addrsize(int family);
+extern size_t sockunion_get_addrlen(const union sockunion *);
+extern const u_char *sockunion_get_addr(const union sockunion *);
+extern void sockunion_set(union sockunion *, int family, const u_char *addr, size_t bytes);
+
 extern union sockunion *sockunion_str2su (const char *str);
 extern int sockunion_accept (int sock, union sockunion *);
 extern int sockunion_stream_socket (union sockunion *);