chiark / gitweb /
sd-dhcp-lease: use shared default prefixlen function
authorTom Gundersen <teg@jklm.no>
Sat, 1 Nov 2014 18:02:44 +0000 (19:02 +0100)
committerTom Gundersen <teg@jklm.no>
Sat, 1 Nov 2014 18:02:44 +0000 (19:02 +0100)
Also change the default prefixlen function to only access the first octet of the in_addr.

src/libsystemd-network/dhcp-lease-internal.h
src/libsystemd-network/sd-dhcp-lease.c
src/shared/in-addr-util.c

index d4675f3e473670d4d022e25bf4a2be8a8f4a2a12..9fb4f4b4e4e40a6e1e7d87c526b125fe05667d73 100644 (file)
@@ -35,7 +35,7 @@
 struct sd_dhcp_route {
         struct in_addr dst_addr;
         struct in_addr gw_addr;
-        uint8_t dst_prefixlen;
+        unsigned char dst_prefixlen;
 };
 
 struct sd_dhcp_lease {
index f4979f7da73e4a5dc66a0f11c5f65d6041b0139d..f6b572a996f8f324cbd800ae333c8ee92393801a 100644 (file)
@@ -310,23 +310,6 @@ static int lease_parse_in_addrs_pairs(const uint8_t *option, size_t len, struct
         return lease_parse_in_addrs_aux(option, len, ret, ret_size, 2);
 }
 
-static int class_prefixlen(uint8_t msb_octet, uint8_t *ret) {
-        if (msb_octet < 128)
-                /* Class A */
-                *ret = 8;
-        else if (msb_octet < 192)
-                /* Class B */
-                *ret = 16;
-        else if (msb_octet < 224)
-                /* Class C */
-                *ret = 24;
-        else
-                /* Class D or E -- no subnet mask */
-                return -ERANGE;
-
-        return 0;
-}
-
 static int lease_parse_routes(const uint8_t *option, size_t len, struct sd_dhcp_route **routes,
         size_t *routes_size, size_t *routes_allocated) {
 
@@ -348,8 +331,10 @@ static int lease_parse_routes(const uint8_t *option, size_t len, struct sd_dhcp_
 
         while (len >= 8) {
                 struct sd_dhcp_route *route = *routes + *routes_size;
+                int r;
 
-                if (class_prefixlen(*option, &route->dst_prefixlen) < 0) {
+                r = in_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen);
+                if (r < 0) {
                         log_error("Failed to determine destination prefix length from class based IP, ignoring");
                         continue;
                 }
index 5fbee6caf2fcbf0562c6b35b83eba3cb92f7ec3b..9dc9ec82b484eb4c97976ab1de7d50cf9561eff0 100644 (file)
@@ -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