DHCP Allows selecting the DHCP server's network interface (#426)
This commit is contained in:
parent
260dd1bde8
commit
cb4ea206fa
4 changed files with 64 additions and 67 deletions
|
@ -1,11 +1,11 @@
|
||||||
/* Very basic example showing usage of access point mode and the DHCP server.
|
/**
|
||||||
|
* Very basic example showing usage of access point mode and the DHCP server.
|
||||||
The ESP in the example runs a telnet server on 172.16.0.1 (port 23) that
|
* The ESP in the example runs a telnet server on 172.16.0.1 (port 23) that
|
||||||
outputs some status information if you connect to it, then closes
|
* outputs some status information if you connect to it, then closes
|
||||||
the connection.
|
* the connection.
|
||||||
|
*
|
||||||
This example code is in the public domain.
|
* This example code is in the public domain.
|
||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <espressif/esp_common.h>
|
#include <espressif/esp_common.h>
|
||||||
|
@ -36,44 +36,39 @@ void user_init(void)
|
||||||
IP4_ADDR(&ap_ip.netmask, 255, 255, 0, 0);
|
IP4_ADDR(&ap_ip.netmask, 255, 255, 0, 0);
|
||||||
sdk_wifi_set_ip_info(1, &ap_ip);
|
sdk_wifi_set_ip_info(1, &ap_ip);
|
||||||
|
|
||||||
struct sdk_softap_config ap_config = {
|
struct sdk_softap_config ap_config = { .ssid = AP_SSID, .ssid_hidden = 0, .channel = 3, .ssid_len = strlen(AP_SSID), .authmode =
|
||||||
.ssid = AP_SSID,
|
AUTH_WPA_WPA2_PSK, .password = AP_PSK, .max_connection = 3, .beacon_interval = 100, };
|
||||||
.ssid_hidden = 0,
|
|
||||||
.channel = 3,
|
|
||||||
.ssid_len = strlen(AP_SSID),
|
|
||||||
.authmode = AUTH_WPA_WPA2_PSK,
|
|
||||||
.password = AP_PSK,
|
|
||||||
.max_connection = 3,
|
|
||||||
.beacon_interval = 100,
|
|
||||||
};
|
|
||||||
sdk_wifi_softap_set_config(&ap_config);
|
sdk_wifi_softap_set_config(&ap_config);
|
||||||
|
|
||||||
ip4_addr_t first_client_ip;
|
|
||||||
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
|
||||||
dhcpserver_start(&first_client_ip, 4);
|
|
||||||
|
|
||||||
xTaskCreate(telnetTask, "telnetTask", 512, NULL, 2, NULL);
|
xTaskCreate(telnetTask, "telnetTask", 512, NULL, 2, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Telnet task listens on port 23, returns some status information and then closes
|
/* Telnet task listens on port 23, returns some status information and then closes
|
||||||
the connection if you connect to it.
|
the connection if you connect to it.
|
||||||
*/
|
*/
|
||||||
static void telnetTask(void *pvParameters)
|
static void telnetTask(void *pvParameters)
|
||||||
{
|
{
|
||||||
struct netconn *nc = netconn_new (NETCONN_TCP);
|
ip_addr_t first_client_ip;
|
||||||
if(!nc) {
|
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
||||||
|
dhcpserver_start(sdk_system_get_netif(SOFTAP_IF), &first_client_ip, 4);
|
||||||
|
|
||||||
|
struct netconn *nc = netconn_new(NETCONN_TCP);
|
||||||
|
if (!nc)
|
||||||
|
{
|
||||||
printf("Status monitor: Failed to allocate socket.\r\n");
|
printf("Status monitor: Failed to allocate socket.\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
netconn_bind(nc, IP_ANY_TYPE, TELNET_PORT);
|
netconn_bind(nc, IP_ANY_TYPE, TELNET_PORT);
|
||||||
netconn_listen(nc);
|
netconn_listen(nc);
|
||||||
|
|
||||||
while(1) {
|
while (1)
|
||||||
|
{
|
||||||
struct netconn *client = NULL;
|
struct netconn *client = NULL;
|
||||||
err_t err = netconn_accept(nc, &client);
|
err_t err = netconn_accept(nc, &client);
|
||||||
|
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK)
|
||||||
if(client)
|
{
|
||||||
|
if (client)
|
||||||
netconn_delete(client);
|
netconn_delete(client);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -83,10 +78,9 @@ static void telnetTask(void *pvParameters)
|
||||||
netconn_peer(client, &client_addr, &port_ignore);
|
netconn_peer(client, &client_addr, &port_ignore);
|
||||||
|
|
||||||
char buf[80];
|
char buf[80];
|
||||||
snprintf(buf, sizeof(buf), "Uptime %d seconds\r\n",
|
snprintf(buf, sizeof(buf), "Uptime %d seconds\r\n", xTaskGetTickCount() * portTICK_PERIOD_MS / 1000);
|
||||||
xTaskGetTickCount()*portTICK_PERIOD_MS/1000);
|
|
||||||
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
|
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
|
||||||
snprintf(buf, sizeof(buf), "Free heap %d bytes\r\n", (int)xPortGetFreeHeapSize());
|
snprintf(buf, sizeof(buf), "Free heap %d bytes\r\n", (int) xPortGetFreeHeapSize());
|
||||||
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
|
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
|
||||||
char abuf[40];
|
char abuf[40];
|
||||||
snprintf(buf, sizeof(buf), "Your address is %s\r\n\r\n", ipaddr_ntoa_r(&client_addr, abuf, sizeof(abuf)));
|
snprintf(buf, sizeof(buf), "Your address is %s\r\n\r\n", ipaddr_ntoa_r(&client_addr, abuf, sizeof(abuf)));
|
||||||
|
|
|
@ -3,11 +3,7 @@
|
||||||
* Based on RFC2131 http://www.ietf.org/rfc/rfc2131.txt
|
* Based on RFC2131 http://www.ietf.org/rfc/rfc2131.txt
|
||||||
* ... although not fully RFC compliant yet.
|
* ... although not fully RFC compliant yet.
|
||||||
*
|
*
|
||||||
* TODO
|
* Probably allocates more memory than it should, it should be possible to reuse netbufs in most cases.
|
||||||
* * Allow binding on a single interface only (for mixed AP/client mode), lwip seems to make it hard to
|
|
||||||
* listen for or send broadcasts on a specific interface only.
|
|
||||||
*
|
|
||||||
* * Probably allocates more memory than it should, it should be possible to reuse netbufs in most cases.
|
|
||||||
*
|
*
|
||||||
* Part of esp-open-rtos
|
* Part of esp-open-rtos
|
||||||
* Copyright (C) 2015 Superhouse Automation Pty Ltd
|
* Copyright (C) 2015 Superhouse Automation Pty Ltd
|
||||||
|
@ -92,8 +88,12 @@ inline static void sprintf_ipaddr(const ip4_addr_t *addr, char *dest)
|
||||||
ip4_addr2(addr), ip4_addr3(addr), ip4_addr4(addr));
|
ip4_addr2(addr), ip4_addr3(addr), ip4_addr4(addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases)
|
void dhcpserver_start(struct netif *server_if, const ip_addr_t *first_client_addr, uint8_t max_leases)
|
||||||
{
|
{
|
||||||
|
if(!server_if){
|
||||||
|
printf("DHCP Server Error: server_if is NULL.\r\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
/* Stop any existing running dhcpserver */
|
/* Stop any existing running dhcpserver */
|
||||||
if (dhcpserver_task_handle)
|
if (dhcpserver_task_handle)
|
||||||
dhcpserver_stop();
|
dhcpserver_stop();
|
||||||
|
@ -109,7 +109,7 @@ void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases)
|
||||||
ip4_addr_set_zero(&state->router);
|
ip4_addr_set_zero(&state->router);
|
||||||
ip4_addr_set_zero(&state->dns);
|
ip4_addr_set_zero(&state->dns);
|
||||||
|
|
||||||
xTaskCreate(dhcpserver_task, "DHCP Server", 448, NULL, 2, &dhcpserver_task_handle);
|
xTaskCreate(dhcpserver_task, "DHCPServer", 768, server_if, 8, &dhcpserver_task_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dhcpserver_stop(void)
|
void dhcpserver_stop(void)
|
||||||
|
@ -134,7 +134,7 @@ void dhcpserver_set_dns(const ip4_addr_t *dns)
|
||||||
static void dhcpserver_task(void *pxParameter)
|
static void dhcpserver_task(void *pxParameter)
|
||||||
{
|
{
|
||||||
/* netif_list isn't assigned until after user_init completes, which is why we do it inside the task */
|
/* netif_list isn't assigned until after user_init completes, which is why we do it inside the task */
|
||||||
state->server_if = netif_list; /* TODO: Make this configurable */
|
state->server_if = pxParameter;
|
||||||
|
|
||||||
state->nc = netconn_new (NETCONN_UDP);
|
state->nc = netconn_new (NETCONN_UDP);
|
||||||
if(!state->nc) {
|
if(!state->nc) {
|
||||||
|
@ -143,7 +143,7 @@ static void dhcpserver_task(void *pxParameter)
|
||||||
}
|
}
|
||||||
|
|
||||||
netconn_bind(state->nc, IP4_ADDR_ANY, LWIP_IANA_PORT_DHCP_SERVER);
|
netconn_bind(state->nc, IP4_ADDR_ANY, LWIP_IANA_PORT_DHCP_SERVER);
|
||||||
netconn_bind_if (state->nc, netif_get_index(state->server_if));
|
netconn_bind_if(state->nc, netif_get_index(state->server_if));
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,8 +25,10 @@ extern "C" {
|
||||||
first_client_addr is the IP address of the first lease to be handed
|
first_client_addr is the IP address of the first lease to be handed
|
||||||
to a client. Subsequent lease addresses are calculated by
|
to a client. Subsequent lease addresses are calculated by
|
||||||
incrementing the final octet of the IPv4 address, up to max_leases.
|
incrementing the final octet of the IPv4 address, up to max_leases.
|
||||||
|
|
||||||
|
The server will wait for requests on server_if interface.
|
||||||
*/
|
*/
|
||||||
void dhcpserver_start(const ip4_addr_t *first_client_addr, uint8_t max_leases);
|
void dhcpserver_start(struct netif *server_if, const ip_addr_t *first_client_addr, uint8_t max_leases);
|
||||||
|
|
||||||
void dhcpserver_get_lease(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);
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,11 @@ DEFINE_TESTCASE(04_wifi_basic, DUAL)
|
||||||
|
|
||||||
static void server_task(void *pvParameters)
|
static void server_task(void *pvParameters)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
ip_addr_t first_client_ip;
|
||||||
|
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
||||||
|
dhcpserver_start(sdk_system_get_netif(SOFTAP_IF), &first_client_ip, 4);
|
||||||
|
|
||||||
char buf[BUF_SIZE];
|
char buf[BUF_SIZE];
|
||||||
struct netconn *nc = netconn_new(NETCONN_TCP);
|
struct netconn *nc = netconn_new(NETCONN_TCP);
|
||||||
TEST_ASSERT_TRUE_MESSAGE(nc != 0, "Failed to allocate socket");
|
TEST_ASSERT_TRUE_MESSAGE(nc != 0, "Failed to allocate socket");
|
||||||
|
@ -103,10 +108,6 @@ static void a_04_wifi_basic(void)
|
||||||
};
|
};
|
||||||
sdk_wifi_softap_set_config(&ap_config);
|
sdk_wifi_softap_set_config(&ap_config);
|
||||||
|
|
||||||
ip_addr_t first_client_ip;
|
|
||||||
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
|
||||||
dhcpserver_start(&first_client_ip, 4);
|
|
||||||
|
|
||||||
xTaskCreate(server_task, "setver_task", 1024, NULL, 2, NULL);
|
xTaskCreate(server_task, "setver_task", 1024, NULL, 2, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue