--- gdhcp/client.c | 17 ++++++----------- gdhcp/common.c | 21 +++++++++++++++++---- gdhcp/common.h | 17 +---------------- gdhcp/server.c | 7 +++---- 4 files changed, 27 insertions(+), 35 deletions(-)
diff --git a/gdhcp/client.c b/gdhcp/client.c index cf04ced..6632c2c 100644 --- a/gdhcp/client.c +++ b/gdhcp/client.c @@ -1340,8 +1340,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 = ntohl(lease_seconds); + lease_seconds = get_be32(option_u8); /* paranoia: must not be prone to overflows */ lease_seconds &= 0x0fffffff; if (lease_seconds < 10) @@ -1518,16 +1517,13 @@ 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); - dest += sprintf(dest, "%u", ntohs(val_u16)); + uint16_t val_u16 = get_be16(option); + dest += sprintf(dest, "%u", val_u16); break; } case OPTION_U32: { - uint32_t val_u32 = dhcp_get_unaligned( - (uint32_t *) option); - dest += sprintf(dest, type == OPTION_U32 ? "%lu" : - "%ld", (unsigned long) ntohl(val_u32)); + uint32_t val_u32 = get_be32(option); + dest += sprintf(dest, "%lu", (unsigned long)val_u32); break; } case OPTION_STRING: @@ -1924,8 +1920,7 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition, dhcp_client->retry_times = 0; option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID); - dhcp_client->server_ip = - dhcp_get_unaligned((uint32_t *) option_u8); + dhcp_client->server_ip = 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 8d5c284..f4e9625 100644 --- a/gdhcp/common.c +++ b/gdhcp/common.c @@ -280,11 +280,24 @@ void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, len = dhcp_option_lengths[type & OPTION_TYPE_MASK]; option[OPT_LEN] = len; -#if __BYTE_ORDER == __BIG_ENDIAN - data <<= 8 * (4 - len); -#endif + /* + * Note that the data is already in network byte order. + */ + switch (len) { + case 1: + option[OPT_DATA] = data; + break; + case 2: + put_unaligned(data, (uint16_t *)(option + OPT_DATA)); + break; + case 4: + put_unaligned(data, (uint32_t *)(option + OPT_DATA)); + break; + default: + printf("Invalid option len %d for code 0x%x\n", len, code); + return; + } - 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 13f49d8..e2bfc6c 100644 --- a/gdhcp/common.h +++ b/gdhcp/common.h @@ -24,24 +24,9 @@ #include <glib.h> +#include "unaligned.h" #include "gdhcp.h" -#define dhcp_get_unaligned(ptr) \ -({ \ - struct __attribute__((packed)) { \ - typeof(*(ptr)) __v; \ - } *__p = (void *) (ptr); \ - __p->__v; \ -}) - -#define dhcp_put_unaligned(val, ptr) \ -do { \ - struct __attribute__((packed)) { \ - typeof(*(ptr)) __v; \ - } *__p = (void *) (ptr); \ - __p->__v = (val); \ -} while (0) - #define CLIENT_PORT 68 #define SERVER_PORT 67 diff --git a/gdhcp/server.c b/gdhcp/server.c index a342985..ff69892 100644 --- a/gdhcp/server.c +++ b/gdhcp/server.c @@ -664,8 +664,8 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition, server_id_option = dhcp_get_option(&packet, DHCP_SERVER_ID); if (server_id_option) { - uint32_t server_nid = dhcp_get_unaligned( - (uint32_t *) server_id_option); + uint32_t server_nid = + get_unaligned((uint32_t *)server_id_option); if (server_nid != dhcp_server->server_nip) return TRUE; @@ -673,8 +673,7 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition, request_ip_option = dhcp_get_option(&packet, DHCP_REQUESTED_IP); if (request_ip_option) - requested_nip = dhcp_get_unaligned( - (uint32_t *) request_ip_option); + requested_nip = get_unaligned((uint32_t *)request_ip_option); lease = find_lease_by_mac(dhcp_server, packet.chaddr); -- 1.7.11.4 _______________________________________________ connman mailing list connman@connman.net http://lists.connman.net/listinfo/connman