net_lwip_webserver: efficiency tweaks

This commit is contained in:
Peter Lawrence 2020-10-14 20:26:40 -05:00
parent 41083cb67a
commit 56277ce216
4 changed files with 38 additions and 37 deletions

View File

@ -35,7 +35,7 @@
/* Prevent having to link sys_arch.c (we don't test the API layers in unit tests) */
#define NO_SYS 1
#define MEM_ALIGNMENT 4
#define LWIP_RAW 1
#define LWIP_RAW 0
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0
#define LWIP_DHCP 0
@ -54,4 +54,6 @@
#define LWIP_HTTPD_SSI 0
#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
#define LWIP_SINGLE_NETIF 1
#endif /* __LWIPOPTS_H__ */

View File

@ -71,22 +71,21 @@ static const ip_addr_t gateway = IPADDR4_INIT_BYTES(0, 0, 0, 0);
/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
static dhcp_entry_t entries[] =
{
/* mac ip address subnet mask lease time */
{ {0}, {192, 168, 7, 2}, {255, 255, 255, 0}, 24 * 60 * 60 },
{ {0}, {192, 168, 7, 3}, {255, 255, 255, 0}, 24 * 60 * 60 },
{ {0}, {192, 168, 7, 4}, {255, 255, 255, 0}, 24 * 60 * 60 }
/* mac ip address lease time */
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 2), 24 * 60 * 60 },
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 3), 24 * 60 * 60 },
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 4), 24 * 60 * 60 },
};
/* DHCP configuration parameters, leveraging "entries" above */
static const dhcp_config_t dhcp_config =
{
{192, 168, 7, 1}, 67, /* server address (self), port */
{192, 168, 7, 1}, /* dns server (self) */
"usb", /* dns suffix */
TU_ARRAY_SIZE(entries), /* number of entries */
entries /* pointer to entries */
.router = IPADDR4_INIT_BYTES(0, 0, 0, 0), /* router address (if any) */
.port = 67, /* listen port */
.dns = IPADDR4_INIT_BYTES(192, 168, 7, 1), /* dns server (if any) */
"usb", /* dns suffix */
TU_ARRAY_SIZE(entries), /* num entry */
entries /* entries */
};
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
{
(void)netif;

View File

@ -96,23 +96,23 @@ static const dhcp_config_t *config = NULL;
char magic_cookie[] = {0x63,0x82,0x53,0x63};
static uint32_t get_ip(const uint8_t *pnt)
static ip_addr_t get_ip(const uint8_t *pnt)
{
uint32_t result;
ip_addr_t result;
memcpy(&result, pnt, sizeof(result));
return result;
}
static void set_ip(uint8_t *pnt, uint32_t value)
static void set_ip(uint8_t *pnt, ip_addr_t value)
{
memcpy(pnt, &value, sizeof(value));
memcpy(pnt, &value.addr, sizeof(value.addr));
}
static dhcp_entry_t *entry_by_ip(uint32_t ip)
static dhcp_entry_t *entry_by_ip(ip_addr_t ip)
{
int i;
for (i = 0; i < config->num_entry; i++)
if (get_ip(config->entries[i].addr) == ip)
if (config->entries[i].addr.addr == ip.addr)
return &config->entries[i];
return NULL;
}
@ -162,11 +162,11 @@ uint8_t *find_dhcp_option(uint8_t *attrs, int size, uint8_t attr)
int fill_options(void *dest,
uint8_t msg_type,
const char *domain,
uint32_t dns,
ip_addr_t dns,
int lease_time,
uint32_t serverid,
uint32_t router,
uint32_t subnet)
ip_addr_t serverid,
ip_addr_t router,
ip_addr_t subnet)
{
uint8_t *ptr = (uint8_t *)dest;
/* ACK message type */
@ -195,7 +195,7 @@ int fill_options(void *dest,
ptr += 4;
/* router */
if (router != 0)
if (router.addr != 0)
{
*ptr++ = DHCP_ROUTER;
*ptr++ = 4;
@ -214,7 +214,7 @@ int fill_options(void *dest,
}
/* domain name server (DNS) */
if (dns != 0)
if (dns.addr != 0)
{
*ptr++ = DHCP_DNSSERVER;
*ptr++ = 4;
@ -232,6 +232,7 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
uint8_t *ptr;
dhcp_entry_t *entry;
struct pbuf *pp;
struct netif *netif = netif_get_by_index(p->if_idx);
(void)arg;
(void)addr;
@ -249,7 +250,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_secs = 0;
dhcp_data.dp_flags = 0;
set_ip(dhcp_data.dp_yiaddr, get_ip(entry->addr));
set_ip(dhcp_data.dp_yiaddr, entry->addr);
memcpy(dhcp_data.dp_magic, magic_cookie, 4);
memset(dhcp_data.dp_options, 0, sizeof(dhcp_data.dp_options));
@ -257,11 +258,11 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
fill_options(dhcp_data.dp_options,
DHCP_OFFER,
config->domain,
get_ip(config->dns),
config->dns,
entry->lease,
get_ip(config->addr),
get_ip(config->addr),
get_ip(entry->subnet));
*netif_ip4_addr(netif),
config->router,
*netif_ip4_netmask(netif));
pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
if (pp == NULL) break;
@ -299,11 +300,11 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const
fill_options(dhcp_data.dp_options,
DHCP_ACK,
config->domain,
get_ip(config->dns),
config->dns,
entry->lease,
get_ip(config->addr),
get_ip(config->addr),
get_ip(entry->subnet));
*netif_ip4_addr(netif),
config->router,
*netif_ip4_netmask(netif));
/* 6. send ACK */
pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);

View File

@ -42,16 +42,15 @@
typedef struct dhcp_entry
{
uint8_t mac[6];
uint8_t addr[4];
uint8_t subnet[4];
ip_addr_t addr;
uint32_t lease;
} dhcp_entry_t;
typedef struct dhcp_config
{
uint8_t addr[4];
ip_addr_t router;
uint16_t port;
uint8_t dns[4];
ip_addr_t dns;
const char *domain;
int num_entry;
dhcp_entry_t *entries;