Browse Source

lib: Add a trivial sockunion_get_port helper function

Paul Jakma 2 years ago
parent
commit
f408fa2c81
2 changed files with 16 additions and 0 deletions
  1. 15 0
      lib/sockunion.c
  2. 1 0
      lib/sockunion.h

+ 15 - 0
lib/sockunion.c

@@ -632,6 +632,21 @@ sockunion_get_addr(const union sockunion *su)
   return NULL;
 }
 
+unsigned short
+sockunion_get_port (const union sockunion *su)
+{
+  switch (sockunion_family (su))
+    {
+    case AF_INET:
+      return ntohs(su->sin.sin_port);
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      return ntohs(su->sin6.sin6_port);
+#endif /* HAVE_IPV6 */
+    }
+  return 0;
+}
+
 void
 sockunion_set(union sockunion *su, int family, const u_char *addr, size_t bytes)
 {

+ 1 - 0
lib/sockunion.h

@@ -94,6 +94,7 @@ 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 unsigned short sockunion_get_port (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);