summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-11-01 19:02:44 +0100
committerTom Gundersen <teg@jklm.no>2014-11-01 19:02:44 +0100
commit1caa12d0a8a818d5f2545aab809b3fdfec73871d (patch)
treeb125e9822a152ddbe253cf1c852cf65ee2dd8871 /src/shared
parentbab47929613f9e930dd241a01483b37d14b59c69 (diff)
sd-dhcp-lease: use shared default prefixlen function
Also change the default prefixlen function to only access the first octet of the in_addr.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/in-addr-util.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/shared/in-addr-util.c b/src/shared/in-addr-util.c
index 5fbee6caf2..9dc9ec82b4 100644
--- a/src/shared/in-addr-util.c
+++ b/src/shared/in-addr-util.c
@@ -250,21 +250,20 @@ unsigned in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
}
int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
- uint32_t address;
+ uint8_t msb_octet = *(uint8_t*) addr;
+
+ /* addr may not be aligned, so make sure we only access it byte-wise */
assert(addr);
- assert(addr->s_addr != INADDR_ANY);
assert(prefixlen);
- address = be32toh(addr->s_addr);
-
- if ((address >> 31) == 0x0)
+ if (msb_octet < 128)
/* class A, leading bits: 0 */
*prefixlen = 8;
- else if ((address >> 30) == 0x2)
+ else if (msb_octet < 192)
/* class B, leading bits 10 */
*prefixlen = 16;
- else if ((address >> 29) == 0x6)
+ else if (msb_octet < 224)
/* class C, leading bits 110 */
*prefixlen = 24;
else