aboutgitcodebugslistschat
path: root/inany.h
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-02-28 22:25:01 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-02-29 09:47:21 +0100
commitd31277e292281aa7e61d3785f56af1c956520786 (patch)
treea21519b7fce0e0b3edf8da1d592289263ccfb1c9 /inany.h
parent9a3fb5eb68eb41725b074d465248542ec4f82f1c (diff)
downloadpasst-d31277e292281aa7e61d3785f56af1c956520786.tar
passt-d31277e292281aa7e61d3785f56af1c956520786.tar.gz
passt-d31277e292281aa7e61d3785f56af1c956520786.tar.bz2
passt-d31277e292281aa7e61d3785f56af1c956520786.tar.lz
passt-d31277e292281aa7e61d3785f56af1c956520786.tar.xz
passt-d31277e292281aa7e61d3785f56af1c956520786.tar.zst
passt-d31277e292281aa7e61d3785f56af1c956520786.zip
inany: Helper to test for various address types
Add helpers to determine if an inany is loopback, unspecified or multicast, regardless of whether it's a "true" IPv6 address or an IPv4 address represented as v4-mapped. Use the loopback helper to simplify tcp_splice_conn_from_sock() slightly. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'inany.h')
-rw-r--r--inany.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/inany.h b/inany.h
index fe652ff..d110380 100644
--- a/inany.h
+++ b/inany.h
@@ -55,6 +55,56 @@ static inline bool inany_equals(const union inany_addr *a,
return IN6_ARE_ADDR_EQUAL(&a->a6, &b->a6);
}
+/** inany_is_loopback() - Check if address is loopback
+ * @a: IPv[46] address
+ *
+ * Return: true if @a is either ::1 or in 127.0.0.1/8
+ */
+static inline bool inany_is_loopback(const union inany_addr *a)
+{
+ const struct in_addr *v4 = inany_v4(a);
+
+ return IN6_IS_ADDR_LOOPBACK(&a->a6) || (v4 && IN4_IS_ADDR_LOOPBACK(v4));
+}
+
+/** inany_is_unspecified() - Check if address is unspecified
+ * @a: IPv[46] address
+ *
+ * Return: true if @a is either :: or 0.0.0.0
+ */
+static inline bool inany_is_unspecified(const union inany_addr *a)
+{
+ const struct in_addr *v4 = inany_v4(a);
+
+ return IN6_IS_ADDR_UNSPECIFIED(&a->a6) ||
+ (v4 && IN4_IS_ADDR_UNSPECIFIED(v4));
+}
+
+/** inany_is_multicast() - Check if address is multicast or broadcast
+ * @a: IPv[46] address
+ *
+ * Return: true if @a is IPv6 multicast or IPv4 multicast or broadcast
+ */
+static inline bool inany_is_multicast(const union inany_addr *a)
+{
+ const struct in_addr *v4 = inany_v4(a);
+
+ return IN6_IS_ADDR_MULTICAST(&a->a6) ||
+ (v4 && (IN4_IS_ADDR_MULTICAST(v4) ||
+ IN4_IS_ADDR_BROADCAST(v4)));
+}
+
+/** inany_is_unicast() - Check if address is specified and unicast
+ * @a: IPv[46] address
+ *
+ * Return: true if @a is specified and a unicast address
+ */
+/* cppcheck-suppress unusedFunction */
+static inline bool inany_is_unicast(const union inany_addr *a)
+{
+ return !inany_is_unspecified(a) && !inany_is_multicast(a);
+}
+
/** inany_from_af - Set IPv[46] address from IPv4 or IPv6 address
* @aa: Pointer to store IPv[46] address
* @af: Address family of @addr