mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-25 07:43:38 +00:00
3rd-party/lwip/dhcp-server: fix unaligned accesses
In the dhcp server, avoid casting a uint8_t* to uint32_t* when using ip addresses.
This commit is contained in:
parent
555f545d84
commit
e3ba22907f
38
3rd-party/lwip/dhcp-server/dhserver.c
vendored
38
3rd-party/lwip/dhcp-server/dhserver.c
vendored
@ -96,11 +96,19 @@ static dhcp_config_t *config = NULL;
|
|||||||
|
|
||||||
char magic_cookie[] = {0x63,0x82,0x53,0x63};
|
char magic_cookie[] = {0x63,0x82,0x53,0x63};
|
||||||
|
|
||||||
|
static uint32_t get_addr32(const uint8_t addr[4]) {
|
||||||
|
return PP_HTONL(LWIP_MAKEU32(addr[0],addr[1],addr[2],addr[3]));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_addr32(uint8_t *dst, uint32_t src) {
|
||||||
|
memcpy(dst, &src, 4);
|
||||||
|
}
|
||||||
|
|
||||||
static dhcp_entry_t *entry_by_ip(uint32_t ip)
|
static dhcp_entry_t *entry_by_ip(uint32_t ip)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < config->num_entry; i++)
|
for (i = 0; i < config->num_entry; i++)
|
||||||
if (*(uint32_t *)config->entries[i].addr == ip)
|
if (get_addr32(config->entries[i].addr) == ip)
|
||||||
return &config->entries[i];
|
return &config->entries[i];
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -165,7 +173,7 @@ static int fill_options(void *dest,
|
|||||||
/* dhcp server identifier */
|
/* dhcp server identifier */
|
||||||
*ptr++ = DHCP_SERVERID;
|
*ptr++ = DHCP_SERVERID;
|
||||||
*ptr++ = 4;
|
*ptr++ = 4;
|
||||||
*(uint32_t *)ptr = serverid;
|
set_addr32(ptr, serverid);
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
|
|
||||||
/* lease time */
|
/* lease time */
|
||||||
@ -179,7 +187,7 @@ static int fill_options(void *dest,
|
|||||||
/* subnet mask */
|
/* subnet mask */
|
||||||
*ptr++ = DHCP_SUBNETMASK;
|
*ptr++ = DHCP_SUBNETMASK;
|
||||||
*ptr++ = 4;
|
*ptr++ = 4;
|
||||||
*(uint32_t *)ptr = subnet;
|
set_addr32(ptr, subnet);
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
|
|
||||||
/* router */
|
/* router */
|
||||||
@ -187,7 +195,7 @@ static int fill_options(void *dest,
|
|||||||
{
|
{
|
||||||
*ptr++ = DHCP_ROUTER;
|
*ptr++ = DHCP_ROUTER;
|
||||||
*ptr++ = 4;
|
*ptr++ = 4;
|
||||||
*(uint32_t *)ptr = router;
|
set_addr32(ptr, router);
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +214,7 @@ static int fill_options(void *dest,
|
|||||||
{
|
{
|
||||||
*ptr++ = DHCP_DNSSERVER;
|
*ptr++ = DHCP_DNSSERVER;
|
||||||
*ptr++ = 4;
|
*ptr++ = 4;
|
||||||
*(uint32_t *)ptr = dns;
|
set_addr32(ptr, dns);
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,7 +245,7 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
|
|||||||
dhcp_data.dp_op = 2; /* reply */
|
dhcp_data.dp_op = 2; /* reply */
|
||||||
dhcp_data.dp_secs = 0;
|
dhcp_data.dp_secs = 0;
|
||||||
dhcp_data.dp_flags = 0;
|
dhcp_data.dp_flags = 0;
|
||||||
*(uint32_t *)dhcp_data.dp_yiaddr = *(uint32_t *)entry->addr;
|
set_addr32(dhcp_data.dp_yiaddr, get_addr32(entry->addr));
|
||||||
memcpy(dhcp_data.dp_magic, magic_cookie, 4);
|
memcpy(dhcp_data.dp_magic, magic_cookie, 4);
|
||||||
|
|
||||||
memset(dhcp_data.dp_options, 0, sizeof(dhcp_data.dp_options));
|
memset(dhcp_data.dp_options, 0, sizeof(dhcp_data.dp_options));
|
||||||
@ -245,11 +253,11 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
|
|||||||
fill_options(dhcp_data.dp_options,
|
fill_options(dhcp_data.dp_options,
|
||||||
DHCP_OFFER,
|
DHCP_OFFER,
|
||||||
config->domain,
|
config->domain,
|
||||||
*(uint32_t *)config->dns,
|
get_addr32(config->dns),
|
||||||
entry->lease,
|
entry->lease,
|
||||||
*(uint32_t *)config->addr,
|
get_addr32(config->addr),
|
||||||
*(uint32_t *)config->addr,
|
get_addr32(config->addr),
|
||||||
*(uint32_t *)entry->subnet);
|
get_addr32(entry->subnet));
|
||||||
|
|
||||||
pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
|
pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
|
||||||
if (pp == NULL) break;
|
if (pp == NULL) break;
|
||||||
@ -270,7 +278,7 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
|
|||||||
if (entry != NULL) free_entry(entry);
|
if (entry != NULL) free_entry(entry);
|
||||||
|
|
||||||
/* 3. find requested ipaddr */
|
/* 3. find requested ipaddr */
|
||||||
entry = entry_by_ip(*(uint32_t *)ptr);
|
entry = entry_by_ip(get_addr32(ptr));
|
||||||
if (entry == NULL) break;
|
if (entry == NULL) break;
|
||||||
if (!is_vacant(entry)) break;
|
if (!is_vacant(entry)) break;
|
||||||
|
|
||||||
@ -287,11 +295,11 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
|
|||||||
fill_options(dhcp_data.dp_options,
|
fill_options(dhcp_data.dp_options,
|
||||||
DHCP_ACK,
|
DHCP_ACK,
|
||||||
config->domain,
|
config->domain,
|
||||||
*(uint32_t *)config->dns,
|
get_addr32(config->dns),
|
||||||
entry->lease,
|
entry->lease,
|
||||||
*(uint32_t *)config->addr,
|
get_addr32(config->addr),
|
||||||
*(uint32_t *)config->addr,
|
get_addr32(config->addr),
|
||||||
*(uint32_t *)entry->subnet);
|
get_addr32(entry->subnet));
|
||||||
|
|
||||||
/* 6. send ACK */
|
/* 6. send ACK */
|
||||||
pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
|
pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user