aboutgitcodebugslistschat
path: root/util.h
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-03-21 15:04:48 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-04-05 16:59:05 +0200
commit67a62589185f947a8757528818e090f9ae264738 (patch)
tree4f22aa987f0b68733c0a9dd8db5563dab8425d97 /util.h
parentf4e38b5cd232cefa63ec6ca901efb95aad87c2c3 (diff)
downloadpasst-67a62589185f947a8757528818e090f9ae264738.tar
passt-67a62589185f947a8757528818e090f9ae264738.tar.gz
passt-67a62589185f947a8757528818e090f9ae264738.tar.bz2
passt-67a62589185f947a8757528818e090f9ae264738.tar.lz
passt-67a62589185f947a8757528818e090f9ae264738.tar.xz
passt-67a62589185f947a8757528818e090f9ae264738.tar.zst
passt-67a62589185f947a8757528818e090f9ae264738.zip
util: Add helper to return name of address family
We have a few places where we want to include the name of the internet protocol version (IPv4 or IPv6) in a message, which we handle with an open-coded ?: expression. This seems like something that might be more widely useful, so make a trivial helper to return the correct string based on the address family. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.h')
-rw-r--r--util.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/util.h b/util.h
index 48f3560..7c261d7 100644
--- a/util.h
+++ b/util.h
@@ -157,6 +157,24 @@ int write_file(const char *path, const char *buf);
int write_remainder(int fd, const struct iovec *iov, int iovcnt, size_t skip);
/**
+ * af_name() - Return name of an address family
+ * @af: Address/protocol family (AF_INET or AF_INET6)
+ *
+ * Returns: Name of the protocol family as a string
+ */
+static inline const char *af_name(sa_family_t af)
+{
+ switch (af) {
+ case AF_INET:
+ return "IPv4";
+ case AF_INET6:
+ return "IPv6";
+ default:
+ return "<unknown address family>";
+ }
+}
+
+/**
* mod_sub() - Modular arithmetic subtraction
* @a: Minued, unsigned value < @m
* @b: Subtrahend, unsigned value < @m