From: Pekka Pessi <[email protected]>
---
gdhcp/client.c | 14 +++++++-------
gdhcp/common.c | 2 +-
gdhcp/common.h | 17 +++++++----------
3 files changed, 15 insertions(+), 18 deletions(-)
diff --git a/gdhcp/client.c b/gdhcp/client.c
index a7bd88a..ba43b85 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -546,7 +546,7 @@ static int dhcp_recv_l2_packet(struct dhcp_packet
*dhcp_pkt, int fd)
if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
return -1;
- if (bytes < ntohs(packet.ip.tot_len))
+ if ((unsigned)bytes < ntohs(packet.ip.tot_len))
/* packet is bigger than sizeof(packet), we did partial read */
return -1;
@@ -698,7 +698,7 @@ static uint32_t get_lease(struct dhcp_packet *packet)
if (option_u8 == NULL)
return 3600;
- lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
+ lease_seconds = dhcp_get_unaligned(uint32_t, option_u8);
lease_seconds = ntohl(lease_seconds);
/* paranoia: must not be prone to overflows */
lease_seconds &= 0x0fffffff;
@@ -857,14 +857,14 @@ static char *malloc_option_value_string(uint8_t *option,
GDHCPOptionType type)
dest += sprint_nip(dest, "", option);
break;
case OPTION_U16: {
- uint16_t val_u16 = dhcp_get_unaligned(
- (uint16_t *) option);
+ uint16_t val_u16;
+ val_u16 = dhcp_get_unaligned(uint16_t, option);
dest += sprintf(dest, "%u", ntohs(val_u16));
break;
}
case OPTION_U32: {
- uint32_t val_u32 = dhcp_get_unaligned(
- (uint32_t *) option);
+ uint32_t val_u32;
+ val_u32 = dhcp_get_unaligned(uint32_t, option);
dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
"%ld", (unsigned long) ntohl(val_u32));
break;
@@ -991,7 +991,7 @@ static gboolean listener_event(GIOChannel *channel,
GIOCondition condition,
option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
dhcp_client->server_ip =
- dhcp_get_unaligned((uint32_t *) option_u8);
+ dhcp_get_unaligned(uint32_t, option_u8);
dhcp_client->requested_ip = packet.yiaddr;
dhcp_client->state = REQUESTING;
diff --git a/gdhcp/common.c b/gdhcp/common.c
index fc95881..dd3224b 100644
--- a/gdhcp/common.c
+++ b/gdhcp/common.c
@@ -177,7 +177,7 @@ void dhcp_add_simple_option(struct dhcp_packet *packet,
uint8_t code,
data <<= 8 * (4 - len);
#endif
- dhcp_put_unaligned(data, (uint32_t *) &option[OPT_DATA]);
+ dhcp_put_unaligned(data, uint32_t, &option[OPT_DATA]);
dhcp_add_binary_option(packet, option);
return;
diff --git a/gdhcp/common.h b/gdhcp/common.h
index ade49cd..221f4e6 100644
--- a/gdhcp/common.h
+++ b/gdhcp/common.h
@@ -26,20 +26,17 @@
#include "gdhcp.h"
-#define dhcp_get_unaligned(ptr) \
+#define dhcp_get_unaligned(typ, ptr) \
({ \
- struct __attribute__((packed)) { \
- typeof(*(ptr)) __v; \
- } *__p = (void *) (ptr); \
- __p->__v; \
+ typ __v; \
+ memcpy(&__v, ptr, sizeof(__v)); \
+ __v; \
})
-#define dhcp_put_unaligned(val, ptr) \
+#define dhcp_put_unaligned(val, typ, ptr) \
do { \
- struct __attribute__((packed)) { \
- typeof(*(ptr)) __v; \
- } *__p = (void *) (ptr); \
- __p->__v = (val); \
+ typ __v = (val); \
+ memcpy((ptr), &__v, sizeof(__v)); \
} while (0)
#define CLIENT_PORT 68
--
1.7.0.4
_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman