aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2026-01-16 11:59:18 +1100
committerStefano Brivio <sbrivio@redhat.com>2026-01-18 12:47:44 +0100
commit0c611bcd312094a3882be44b759d92423ab28b61 (patch)
tree64499b931bb4286bd5d9fd7be7bfada1e63187ef
parentda49557bedfa515bc5943fccd257c0ae5306dc78 (diff)
downloadpasst-0c611bcd312094a3882be44b759d92423ab28b61.tar
passt-0c611bcd312094a3882be44b759d92423ab28b61.tar.gz
passt-0c611bcd312094a3882be44b759d92423ab28b61.tar.bz2
passt-0c611bcd312094a3882be44b759d92423ab28b61.tar.lz
passt-0c611bcd312094a3882be44b759d92423ab28b61.tar.xz
passt-0c611bcd312094a3882be44b759d92423ab28b61.tar.zst
passt-0c611bcd312094a3882be44b759d92423ab28b61.zip
ip: Add ipproto_name() function
Add a function to get the name of an IP protocol from its number. Usually this would be done by getprotobynumber(), but that requires access to /etc/protocols and might allocate. We can't do either of those once we've self-isolated. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Reviewed-by: Laurent Vivier <lvivier@redhat.com> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--ip.c27
-rw-r--r--ip.h1
2 files changed, 28 insertions, 0 deletions
diff --git a/ip.c b/ip.c
index 9a7f4c5..f1d224b 100644
--- a/ip.c
+++ b/ip.c
@@ -67,3 +67,30 @@ found:
*proto = nh;
return true;
}
+
+/**
+ * ipproto_name() - Get IP protocol name from number
+ * @proto: IP protocol number
+ *
+ * Return: pointer to name of protocol @proto
+ *
+ * Usually this would be done with getprotobynumber(3) but that reads
+ * /etc/protocols and might allocate, which isn't possible for us once
+ * self-isolated.
+ */
+/* cppcheck-suppress unusedFunction */
+const char *ipproto_name(uint8_t proto)
+{
+ switch (proto) {
+ case IPPROTO_ICMP:
+ return "ICMP";
+ case IPPROTO_TCP:
+ return "TCP";
+ case IPPROTO_UDP:
+ return "UDP";
+ case IPPROTO_ICMPV6:
+ return "ICMPv6";
+ default:
+ return "<unknown protocol>";
+ }
+}
diff --git a/ip.h b/ip.h
index 5830b92..a8043c2 100644
--- a/ip.h
+++ b/ip.h
@@ -116,6 +116,7 @@ static inline uint32_t ip6_get_flow_lbl(const struct ipv6hdr *ip6h)
}
bool ipv6_l4hdr(struct iov_tail *data, uint8_t *proto, size_t *dlen);
+const char *ipproto_name(uint8_t proto);
/* IPv6 link-local all-nodes multicast address, ff02::1 */
static const struct in6_addr in6addr_ll_all_nodes = {