summaryrefslogtreecommitdiff
path: root/src/shared/in-addr-util.c
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-10-27 17:38:03 +0100
committerTom Gundersen <teg@jklm.no>2014-10-27 17:40:24 +0100
commitdf40eee8edccb6bf09a57c2b96f69d233032ce52 (patch)
tree3b2c4fbf001cba0b9a5ea0bb6b152899525c25bd /src/shared/in-addr-util.c
parent1dfcee5985e8097fdbe0a77ca31fa23f683fadc2 (diff)
shared: in-addr-utils - add default_subnet_mask and default_prefixlen methods
These use the (deprecated) IPv4 address classes to deduce the corresponding subnet masks. This is useful when addresses without subnet masks and prefix lengths are given. Make use of these new functions from sd-dhcp-lease.
Diffstat (limited to 'src/shared/in-addr-util.c')
-rw-r--r--src/shared/in-addr-util.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/shared/in-addr-util.c b/src/shared/in-addr-util.c
index 457eedd6d8..5fbee6caf2 100644
--- a/src/shared/in-addr-util.c
+++ b/src/shared/in-addr-util.c
@@ -248,3 +248,46 @@ unsigned in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
return 32 - u32ctz(be32toh(addr->s_addr));
}
+
+int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
+ uint32_t address;
+
+ assert(addr);
+ assert(addr->s_addr != INADDR_ANY);
+ assert(prefixlen);
+
+ address = be32toh(addr->s_addr);
+
+ if ((address >> 31) == 0x0)
+ /* class A, leading bits: 0 */
+ *prefixlen = 8;
+ else if ((address >> 30) == 0x2)
+ /* class B, leading bits 10 */
+ *prefixlen = 16;
+ else if ((address >> 29) == 0x6)
+ /* class C, leading bits 110 */
+ *prefixlen = 24;
+ else
+ /* class D or E, no default prefixlen */
+ return -ERANGE;
+
+ return 0;
+}
+
+int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
+ unsigned char prefixlen;
+ int r;
+
+ assert(addr);
+ assert(mask);
+
+ r = in_addr_default_prefixlen(addr, &prefixlen);
+ if (r < 0)
+ return r;
+
+ assert(prefixlen > 0 && prefixlen < 32);
+
+ mask->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
+
+ return 0;
+}