aboutgitcodebugslistschat
path: root/ip.c
diff options
context:
space:
mode:
Diffstat (limited to 'ip.c')
-rw-r--r--ip.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/ip.c b/ip.c
index 9a7f4c5..0ea6299 100644
--- a/ip.c
+++ b/ip.c
@@ -13,6 +13,8 @@
*/
#include <stddef.h>
+#include <netinet/in.h>
+
#include "util.h"
#include "ip.h"
@@ -67,3 +69,48 @@ 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.
+ */
+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>";
+ }
+}
+
+/**
+ * ip4_class_prefix_len() - Get class based prefix length for IPv4 address
+ * @addr: IPv4 address
+ *
+ * Return: prefix length based on address class, or 32 for other
+ */
+int ip4_class_prefix_len(const struct in_addr *addr)
+{
+ in_addr_t a = ntohl(addr->s_addr);
+
+ if (IN_CLASSA(a))
+ return 32 - IN_CLASSA_NSHIFT;
+ if (IN_CLASSB(a))
+ return 32 - IN_CLASSB_NSHIFT;
+ if (IN_CLASSC(a))
+ return 32 - IN_CLASSC_NSHIFT;
+ return 32;
+}