Decode DHCP netmask option correctly
Signed-off-by: Chris Dearman <chris@mips.com>
Change-Id: Ibc288458baf2f75c5b19a46e4c97ee59521cc9a5
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c
index b38e258..34500e7 100644
--- a/libnetutils/dhcpclient.c
+++ b/libnetutils/dhcpclient.c
@@ -197,7 +197,11 @@
}
switch(opt) {
case OPT_SUBNET_MASK:
- if (optlen >= 4) info->prefixLength = ipv4NetmaskToPrefixLength(*((uint32_t*)x));
+ if (optlen >= 4) {
+ in_addr_t mask;
+ memcpy(&mask, x, 4);
+ info->prefixLength = ipv4NetmaskToPrefixLength(mask);
+ }
break;
case OPT_GATEWAY:
if (optlen >= 4) memcpy(&info->gateway, x, 4);
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index 50095ab..eb33d06 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -75,9 +75,8 @@
int ipv4NetmaskToPrefixLength(in_addr_t mask)
{
- mask = ntohl(mask);
int prefixLength = 0;
- uint32_t m = (uint32_t)mask;
+ uint32_t m = (uint32_t)ntohl(mask);
while (m & 0x80000000) {
prefixLength++;
m = m << 1;
@@ -486,7 +485,7 @@
if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
*prefixLength = 0;
} else {
- *prefixLength = ipv4NetmaskToPrefixLength((int)
+ *prefixLength = ipv4NetmaskToPrefixLength(
((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
}
}