summaryrefslogtreecommitdiff
path: root/lib/prefix.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/prefix.c')
-rw-r--r--lib/prefix.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/prefix.c b/lib/prefix.c
index d9751e3f..3f3c4e8e 100644
--- a/lib/prefix.c
+++ b/lib/prefix.c
@@ -247,7 +247,7 @@ str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
/* Get prefix length. */
plen = (u_char) atoi (++pnt);
- if (plen > 32)
+ if (plen > IPV4_MAX_PREFIXLEN)
return 0;
p->family = AF_INET;
@@ -648,7 +648,7 @@ void apply_classful_mask_ipv4 (struct prefix_ipv4 *p)
destination = ntohl (p->prefix.s_addr);
- if (p->prefixlen == 32);
+ if (p->prefixlen == IPV4_MAX_PREFIXLEN);
/* do nothing for host routes */
else if (IN_CLASSC (destination))
{
@@ -667,6 +667,28 @@ void apply_classful_mask_ipv4 (struct prefix_ipv4 *p)
}
}
+in_addr_t
+ipv4_network_addr (in_addr_t hostaddr, int masklen)
+{
+ struct in_addr mask;
+
+ masklen2ip (masklen, &mask);
+ return hostaddr & mask.s_addr;
+}
+
+in_addr_t
+ipv4_broadcast_addr (in_addr_t hostaddr, int masklen)
+{
+ struct in_addr mask;
+
+ masklen2ip (masklen, &mask);
+ return (masklen != IPV4_MAX_PREFIXLEN-1) ?
+ /* normal case */
+ (hostaddr | ~mask.s_addr) :
+ /* special case for /31 */
+ (hostaddr ^ ~mask.s_addr);
+}
+
/* Utility function to convert ipv4 netmask to prefixes
ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
ex.) "1.0.0.0" NULL => "1.0.0.0/8" */