aboutgitcodebugslistschat
path: root/ip.c
diff options
context:
space:
mode:
Diffstat (limited to 'ip.c')
-rw-r--r--ip.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/ip.c b/ip.c
index fc26dab..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"
@@ -93,3 +95,22 @@ const char *ipproto_name(uint8_t proto)
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;
+}