Able to fetch leases from dhcpserver.

This commit is contained in:
brucehsu 2017-10-03 19:02:37 +08:00
parent b6509629ad
commit e9135866f2
2 changed files with 35 additions and 1 deletions

29
extras/dhcpserver/dhcpserver.c Normal file → Executable file
View file

@ -106,6 +106,29 @@ void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases)
xTaskCreate(dhcpserver_task, "DHCP Server", 448, NULL, 2, &dhcpserver_task_handle); xTaskCreate(dhcpserver_task, "DHCP Server", 448, NULL, 2, &dhcpserver_task_handle);
} }
int dhcpserver_get_leases(dhcpserver_lease_t *leases, uint32_t capacity) {
int i=0, count=0;
taskENTER_CRITICAL();
for(i=0;i<state->max_leases;i++) {
if(count>=capacity) {
count=capacity;
break;
}
if(state->leases[i].active) {
leases[count].hwaddr=state->leases[i].hwaddr;
leases[count].ipaddr=*state->first_client_addr;
leases[count].ipaddr.addr+=count;
count++;
}
}
taskLEAVE_CRITICAL();
return count;
}
void dhcpserver_stop(void) void dhcpserver_stop(void)
{ {
if (dhcpserver_task_handle) { if (dhcpserver_task_handle) {
@ -150,6 +173,8 @@ static void dhcpserver_task(void *pxParameter)
printf("DHCP Server Error: Failed to receive DHCP packet. err=%d\r\n", err); printf("DHCP Server Error: Failed to receive DHCP packet. err=%d\r\n", err);
continue; continue;
} }
taskENTER_CRITICAL();
/* expire any leases that have passed */ /* expire any leases that have passed */
uint32_t now = xTaskGetTickCount(); uint32_t now = xTaskGetTickCount();
@ -169,6 +194,7 @@ static void dhcpserver_task(void *pxParameter)
if (netbuf_len(netbuf) < offsetof(struct dhcp_msg, options)) { if (netbuf_len(netbuf) < offsetof(struct dhcp_msg, options)) {
/* too short to be a valid DHCP client message */ /* too short to be a valid DHCP client message */
netbuf_delete(netbuf); netbuf_delete(netbuf);
taskLEAVE_CRITICAL();
continue; continue;
} }
if (netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) { if (netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) {
@ -182,6 +208,7 @@ static void dhcpserver_task(void *pxParameter)
DHCP_OPTION_MESSAGE_TYPE_LEN, NULL); DHCP_OPTION_MESSAGE_TYPE_LEN, NULL);
if (!message_type) { if (!message_type) {
printf("DHCP Server Error: No message type field found"); printf("DHCP Server Error: No message type field found");
taskLEAVE_CRITICAL();
continue; continue;
} }
@ -207,6 +234,8 @@ static void dhcpserver_task(void *pxParameter)
printf("DHCP Server Error: Unsupported message type %d\r\n", *message_type); printf("DHCP Server Error: Unsupported message type %d\r\n", *message_type);
break; break;
} }
taskLEAVE_CRITICAL();
} }
} }

7
extras/dhcpserver/include/dhcpserver.h Normal file → Executable file
View file

@ -18,6 +18,11 @@
extern "C" { extern "C" {
#endif #endif
typedef struct {
uint8_t hwaddr[NETIF_MAX_HWADDR_LEN];
ip4_addr_t ipaddr;
} dhcpserver_lease_t;
/* Start DHCP server. /* Start DHCP server.
Static IP of server should already be set and network interface enabled. Static IP of server should already be set and network interface enabled.
@ -28,7 +33,7 @@ extern "C" {
*/ */
void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases); void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases);
void dhcpserver_get_lease(const ip4_addr_t *first_client_addr, uint8_t max_leases); int dhcpserver_get_leases(dhcpserver_lease_t *leases, uint32_t capacity);
/* Stop DHCP server. /* Stop DHCP server.
*/ */