[systemd-commits] 2 commits - src/libsystemd-network src/shared
Tom Gundersen
tomegun at kemper.freedesktop.org
Sat Nov 1 11:18:25 PDT 2014
src/libsystemd-network/dhcp-lease-internal.h | 2 -
src/libsystemd-network/sd-dhcp-lease.c | 32 +++++----------------------
src/shared/in-addr-util.c | 13 +++++-----
3 files changed, 13 insertions(+), 34 deletions(-)
New commits:
commit 1caa12d0a8a818d5f2545aab809b3fdfec73871d
Author: Tom Gundersen <teg at jklm.no>
Date: Sat Nov 1 19:02:44 2014 +0100
sd-dhcp-lease: use shared default prefixlen function
Also change the default prefixlen function to only access the first octet of the in_addr.
diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h
index d4675f3..9fb4f4b 100644
--- a/src/libsystemd-network/dhcp-lease-internal.h
+++ b/src/libsystemd-network/dhcp-lease-internal.h
@@ -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 {
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index f4979f7..f6b572a 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -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;
}
diff --git a/src/shared/in-addr-util.c b/src/shared/in-addr-util.c
index 5fbee6c..9dc9ec8 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
commit bab47929613f9e930dd241a01483b37d14b59c69
Author: Tom Gundersen <teg at jklm.no>
Date: Sat Nov 1 18:59:55 2014 +0100
sd-dhcp-lease: use unaligned read helpers
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 4fb01c0..f4979f7 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -30,6 +30,7 @@
#include "list.h"
#include "mkdir.h"
#include "fileio.h"
+#include "unaligned.h"
#include "in-addr-util.h"
#include "dhcp-protocol.h"
@@ -205,14 +206,11 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
}
static void lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) {
- be32_t val;
-
assert(option);
assert(ret);
if (len == 4) {
- memcpy(&val, option, 4);
- *ret = be32toh(val);
+ *ret = unaligned_read_be32((be32_t*) option);
if (*ret < min)
*ret = min;
@@ -224,14 +222,11 @@ static void lease_parse_s32(const uint8_t *option, size_t len, int32_t *ret) {
}
static void lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) {
- be16_t val;
-
assert(option);
assert(ret);
if (len == 2) {
- memcpy(&val, option, 2);
- *ret = be16toh(val);
+ *ret = unaligned_read_be16((be16_t*) option);
if (*ret < min)
*ret = min;
More information about the systemd-commits
mailing list