1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Initialize SoftAP DhcpServer object on demand (#8546)

* Initialize SoftAP DhcpServer object on demand

Remove dependency on global ctor, and just construct the object when
someone asks us to do it. Only dependency right now is netif_git, which
is expected to be initialized by the lwip code some time before
dhcps_start happens.

Removing ip_info from begin(), since we never reference later on.
Also removing the specific check for netif id and simplify the ctors.

Update tests and recover old nonos-sdk dhcps functions that were not implemented.

* nonos helpers have a separate header

* wifi ap needs this anyway, simplify sketch includes

* missing example

* existing name :/

* trying to fix header dependency

* restyle

* not a c header

* no need to init

* move dhcp server getter to WiFi

more... arduino'ish? we ahve object as namespace, plus everything else
related to softAP is there
redundant includes, redundant mock impl (out-of-scope here to fix)

* ...move things back, still expose as WiFi method

* review fix

* include -nonos header in wifi lib though

* no more lwip include

* style

* need mock dhcpserver instance
This commit is contained in:
Max Prokhorov 2022-06-01 23:46:04 +03:00 committed by GitHub
parent 61e7605549
commit 502d9469fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 232 additions and 333 deletions

View File

@ -1,63 +1,95 @@
/*
lwIPDhcpServer-NonOS.cpp - DHCP server wrapper
NonOS DHCP server helpers
Copyright (c) 2020 esp8266 arduino. All rights reserved.
Copyright (c) 2020-2022 esp8266 arduino. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// STARTS/STOPS DHCP SERVER ON WIFI AP INTERFACE
// these functions must exists as-is with "C" interface,
// nonos-sdk calls them at boot time and later
#include <lwip/init.h> // LWIP_VERSION
#include "LwipDhcpServer-NonOS.h"
#include <lwip/init.h>
#include <lwip/netif.h>
#include "LwipDhcpServer.h"
extern netif netif_git[2];
// global DHCP instance for softAP interface
DhcpServer dhcpSoftAP(&netif_git[SOFTAP_IF]);
// Global static DHCP instance for softAP interface
// (since the netif object never goes away, even when AP is disabled)
// Initial version fully emulates nonos-sdk api in DhcpServer class,
// before trying to further change it and possibly break legacy behaviour
DhcpServer& getNonOSDhcpServer()
{
extern netif netif_git[2];
static DhcpServer server(&netif_git[SOFTAP_IF]);
return server;
}
extern "C"
{
void dhcps_start(struct ip_info* info, netif* apnetif)
// `ip_info` is useless, since we get the information from the netif directly
// `netif` would be netif_git[SOFTAP_IF], which we get from the lwip2 glue
void dhcps_start(ip_info*, netif*)
{
// apnetif is esp interface, replaced by lwip2's
// netif_git[SOFTAP_IF] interface in constructor
(void)apnetif;
#if 0
// can't use C++ now, global ctors are not initialized yet
dhcpSoftAP.begin(info);
#else
(void)info;
// initial version: emulate nonos-sdk in DhcpServer class before
// trying to change legacy behavor
// `fw_has_started_softap_dhcps` will be read in DhcpServer::DhcpServer
// which is called when c++ ctors are initialized, specifically
// dhcpSoftAP initialized with AP interface number above.
fw_has_started_softap_dhcps = 1;
#endif
auto& server = getNonOSDhcpServer();
if (!server.isRunning())
{
server.begin();
}
}
void dhcps_stop()
{
dhcpSoftAP.end();
auto& server = getNonOSDhcpServer();
if (server.isRunning())
{
server.end();
}
}
// providing the rest of the nonos-sdk API, which was originally removed in 3.0.0
bool wifi_softap_set_dhcps_lease(dhcps_lease* please)
{
auto& server = getNonOSDhcpServer();
return server.set_dhcps_lease(please);
}
bool wifi_softap_get_dhcps_lease(dhcps_lease* please)
{
auto& server = getNonOSDhcpServer();
return server.get_dhcps_lease(please);
}
uint32 wifi_softap_get_dhcps_lease_time()
{
auto& server = getNonOSDhcpServer();
return server.get_dhcps_lease_time();
}
bool wifi_softap_set_dhcps_lease_time(uint32 minutes)
{
auto& server = getNonOSDhcpServer();
return server.set_dhcps_lease_time(minutes);
}
bool wifi_softap_reset_dhcps_lease_time()
{
auto& server = getNonOSDhcpServer();
return server.reset_dhcps_lease_time();
}
bool wifi_softap_add_dhcps_lease(uint8* macaddr)
{
auto& server = getNonOSDhcpServer();
return server.add_dhcps_lease(macaddr);
}
} // extern "C"

View File

@ -0,0 +1,24 @@
/*
NonOS DHCP server helpers
Copyright (c) 2020-2022 esp8266 arduino. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "LwipDhcpServer.h"
// Global static DHCP instance for softAP interface
DhcpServer& getNonOSDhcpServer();

View File

@ -36,8 +36,6 @@
#include <lwip/init.h> // LWIP_VERSION
#define DHCPS_LEASE_TIME_DEF (120)
#define USE_DNS
#include "lwip/inet.h"
@ -166,7 +164,7 @@ const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
int ret = 1, errval = (err); \
if (errval != ERR_OK) \
{ \
os_printf("DHCPS ERROR: %s (lwip:%d)\n", what, errval); \
os_printf("DHCPS ERROR: %s (lwip:%s(%d))\n", what, lwip_strerr(errval), errval); \
ret = 0; \
} \
ret; \
@ -175,36 +173,9 @@ const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#define LWIP_IS_OK(what, err) ((err) == ERR_OK)
#endif
const uint32 DhcpServer::magic_cookie = 0x63538263; // https://tools.ietf.org/html/rfc1497
int fw_has_started_softap_dhcps = 0;
////////////////////////////////////////////////////////////////////////////////////
DhcpServer::DhcpServer(netif* netif) : _netif(netif)
{
pcb_dhcps = nullptr;
dns_address.addr = 0;
plist = nullptr;
offer = 0xFF;
renew = false;
dhcps_lease_time = DHCPS_LEASE_TIME_DEF; // minute
if (netif->num == SOFTAP_IF && fw_has_started_softap_dhcps == 1)
{
// When nonos-sdk starts DHCPS at boot:
// 1. `fw_has_started_softap_dhcps` is already initialized to 1
// 2. global ctor DhcpServer's `dhcpSoftAP(&netif_git[SOFTAP_IF])` is called
// 3. (that's here) => begin(legacy-values) is called
ip_info ip = {
{ 0x0104a8c0 }, // IP 192.168.4.1
{ 0x00ffffff }, // netmask 255.255.255.0
{ 0 } // gateway 0.0.0.0
};
begin(&ip);
fw_has_started_softap_dhcps = 2; // not 1, ending initial boot sequence
}
};
DhcpServer::DhcpServer(netif* netif) : _netif(netif) { }
// wifi_softap_set_station_info is missing in user_interface.h:
extern "C" void wifi_softap_set_station_info(uint8_t* mac, struct ipv4_addr*);
@ -516,7 +487,7 @@ void DhcpServer::create_msg(struct dhcps_msg* m)
memset((char*)m->sname, 0, sizeof(m->sname));
memset((char*)m->file, 0, sizeof(m->file));
memset((char*)m->options, 0, sizeof(m->options));
memcpy((char*)m->options, &magic_cookie, sizeof(magic_cookie));
memcpy((char*)m->options, &MagicCookie, sizeof(MagicCookie));
}
///////////////////////////////////////////////////////////////////////////////////
/*
@ -821,7 +792,7 @@ uint8_t DhcpServer::parse_options(uint8_t* optptr, sint16_t len)
///////////////////////////////////////////////////////////////////////////////////
sint16_t DhcpServer::parse_msg(struct dhcps_msg* m, u16_t len)
{
if (memcmp((char*)m->options, &magic_cookie, sizeof(magic_cookie)) == 0)
if (memcmp((char*)m->options, &MagicCookie, sizeof(MagicCookie)) == 0)
{
struct ipv4_addr ip;
memcpy(&ip.addr, m->ciaddr, sizeof(ip.addr));
@ -1008,7 +979,7 @@ void DhcpServer::init_dhcps_lease(uint32 ip)
}
///////////////////////////////////////////////////////////////////////////////////
bool DhcpServer::begin(struct ip_info* info)
bool DhcpServer::begin()
{
if (pcb_dhcps != nullptr)
{
@ -1016,9 +987,11 @@ bool DhcpServer::begin(struct ip_info* info)
}
pcb_dhcps = udp_new();
if (pcb_dhcps == nullptr || info == nullptr)
if (pcb_dhcps == nullptr)
{
#if DHCPS_DEBUG
os_printf("dhcps_start(): could not obtain pcb\n");
#endif
return false;
}
@ -1030,7 +1003,7 @@ bool DhcpServer::begin(struct ip_info* info)
ip_2_ip4(&broadcast_dhcps)->addr |= ~ip_2_ip4(&_netif->netmask)->addr;
// XXXFIXMEIPV6 broadcast address?
server_address = info->ip;
server_address = *ip_2_ip4(&_netif->ip_addr);
init_dhcps_lease(server_address.addr);
udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT);
@ -1040,12 +1013,6 @@ bool DhcpServer::begin(struct ip_info* info)
"pcb_dhcps\n");
#endif
if (_netif->num == SOFTAP_IF)
{
wifi_set_ip_info(SOFTAP_IF, info); // added for lwip-git, not sure whether useful
}
_netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP; // added for lwip-git
return true;
}
@ -1091,9 +1058,9 @@ void DhcpServer::end()
}
}
bool DhcpServer::isRunning()
bool DhcpServer::isRunning() const
{
return !!_netif->state;
return pcb_dhcps != nullptr;
}
/******************************************************************************
@ -1342,7 +1309,7 @@ bool DhcpServer::reset_dhcps_lease_time(void)
{
return false;
}
dhcps_lease_time = DHCPS_LEASE_TIME_DEF;
dhcps_lease_time = DefaultLeaseTime;
return true;
}

View File

@ -28,22 +28,24 @@
// nearly as-is. This is an initial version to guaranty legacy behavior
// with same default values.
#ifndef __DHCPS_H__
#define __DHCPS_H__
#pragma once
#include <lwip/init.h> // LWIP_VERSION
class DhcpServer
{
public:
DhcpServer(netif* netif);
static constexpr int DefaultLeaseTime = 720; // minutes
static constexpr uint32 MagicCookie = 0x63538263; // https://tools.ietf.org/html/rfc1497
DhcpServer(netif*);
~DhcpServer();
void setDns(int num, const ipv4_addr_t* dns);
bool begin(ip_info* info);
bool begin();
void end();
bool isRunning();
bool isRunning() const;
// this is the C interface encapsulated in a class
// (originally dhcpserver.c in lwIP-v1.4 in NonOS-SDK)
@ -91,25 +93,17 @@ protected:
void dhcps_client_leave(u8* bssid, struct ipv4_addr* ip, bool force);
uint32 dhcps_client_update(u8* bssid, struct ipv4_addr* ip);
netif* _netif;
netif* _netif = nullptr;
struct udp_pcb* pcb_dhcps;
ip_addr_t broadcast_dhcps;
struct ipv4_addr server_address;
struct ipv4_addr client_address;
struct ipv4_addr dns_address;
uint32 dhcps_lease_time;
udp_pcb* pcb_dhcps = nullptr;
ip_addr_t broadcast_dhcps {};
ipv4_addr server_address {};
ipv4_addr client_address {};
ipv4_addr dns_address {};
uint32 dhcps_lease_time = DefaultLeaseTime;
struct dhcps_lease dhcps_lease;
list_node* plist;
uint8 offer;
bool renew;
static const uint32 magic_cookie;
};
// SoftAP DHCP server always exists and is started on boot
extern DhcpServer dhcpSoftAP;
extern "C" int fw_has_started_softap_dhcps;
#endif // __DHCPS_H__

View File

@ -74,7 +74,6 @@
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>
#include <dhcpserver.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>

View File

@ -13,7 +13,6 @@
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>
#include <LwipDhcpServer.h>
#define NAPT 1000
#define NAPT_PORT 10
@ -54,8 +53,9 @@ void setup() {
Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());
// give DNS servers to AP side
dhcpSoftAP.dhcps_set_dns(0, WiFi.dnsIP(0));
dhcpSoftAP.dhcps_set_dns(1, WiFi.dnsIP(1));
auto& server = WiFi.softAPDhcpServer();
server.dhcps_set_dns(0, WiFi.dnsIP(0));
server.dhcps_set_dns(1, WiFi.dnsIP(1));
WiFi.softAPConfig( // enable AP, with android-compatible google domain
IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));

View File

@ -4,7 +4,6 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <LwipDhcpServer.h>
/* Set these to your desired credentials. */
const char *ssid = "ESPap";
@ -76,8 +75,9 @@ void setup() {
...
any client not listed will use next IP address available from the range (here 192.168.0.102 and more)
*/
dhcpSoftAP.add_dhcps_lease(mac_CAM); // always 192.168.0.100
dhcpSoftAP.add_dhcps_lease(mac_PC); // always 192.168.0.101
auto &dhcpServer = WiFi.softAPDhcpServer();
dhcpServer.add_dhcps_lease(mac_CAM); // always 192.168.0.100
dhcpServer.add_dhcps_lease(mac_PC); // always 192.168.0.101
/* Start Access Point. You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
Serial.print("AP IP address: ");

View File

@ -26,6 +26,8 @@
#include "ESP8266WiFiGeneric.h"
#include "ESP8266WiFiAP.h"
#include <LwipDhcpServer-NonOS.h>
extern "C" {
#include "c_types.h"
#include "ets_sys.h"
@ -37,7 +39,6 @@ extern "C" {
}
#include "debug.h"
#include "LwipDhcpServer.h"
// -----------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------- Private functions ------------------------------------------------
@ -166,16 +167,18 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
DEBUG_WIFI("[AP] softap config unchanged\n");
}
dhcpSoftAP.end();
auto& server = softAPDhcpServer();
server.end();
// check IP config
struct ip_info ip;
if(wifi_get_ip_info(SOFTAP_IF, &ip)) {
if(ip.ip.addr == 0x00000000) {
// Invalid config
DEBUG_WIFI("[AP] IP config Invalid resetting...\n");
//192.168.4.1 , 192.168.4.1 , 255.255.255.0
ret = softAPConfig(0x0104A8C0, 0x0104A8C0, 0x00FFFFFF);
ret = softAPConfig(
0x0104A8C0 /* 192.168.4.1 */,
0x0104A8C0 /* 192.168.4.1 */,
0x00FFFFFF /* 255.255.255.0 */);
if(!ret) {
DEBUG_WIFI("[AP] softAPConfig failed!\n");
ret = false;
@ -186,7 +189,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
ret = false;
}
dhcpSoftAP.begin(&ip);
server.begin();
return ret;
}
@ -244,21 +247,15 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
dhcp_lease.end_ip.addr = ip.v4();
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
if(!dhcpSoftAP.set_dhcps_lease(&dhcp_lease))
auto& server = softAPDhcpServer();
if(!server.set_dhcps_lease(&dhcp_lease))
{
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
ret = false;
}
// set lease time to 720min --> 12h
if(!dhcpSoftAP.set_dhcps_lease_time(720))
{
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
ret = false;
}
uint8 mode = info.gw.addr ? 1 : 0;
if(!dhcpSoftAP.set_dhcps_offer_option(OFFER_ROUTER, &mode))
if(!server.set_dhcps_offer_option(OFFER_ROUTER, &mode))
{
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
ret = false;
@ -389,3 +386,11 @@ String ESP8266WiFiAPClass::softAPPSK() const {
return psk;
}
/**
* Get the static DHCP server instance attached to the softAP interface
* @return DhcpServer instance.
*/
DhcpServer& ESP8266WiFiAPClass::softAPDhcpServer() {
return getNonOSDhcpServer();
}

View File

@ -27,6 +27,7 @@
#include "ESP8266WiFiType.h"
#include "ESP8266WiFiGeneric.h"
#include <LwipDhcpServer.h>
class ESP8266WiFiAPClass {
@ -51,6 +52,8 @@ class ESP8266WiFiAPClass {
String softAPSSID() const;
String softAPPSK() const;
static DhcpServer& softAPDhcpServer();
protected:
};

View File

@ -19,7 +19,6 @@
#include <lwip/napt.h>
#include <lwip/dns.h>
#include <PPPServer.h>
#include <dhcpserver.h>
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>

View File

@ -143,6 +143,7 @@ MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) \
ArduinoMainUdp.cpp \
ArduinoMainSpiffs.cpp \
ArduinoMainLittlefs.cpp \
DhcpServer.cpp \
user_interface.cpp \
)

View File

@ -0,0 +1,80 @@
#include <LwipDhcpServer.h>
#include <LwipDhcpServer-NonOS.h>
DhcpServer& getNonOSDhcpServer()
{
static DhcpServer instance(nullptr);
return instance;
}
bool DhcpServer::set_dhcps_lease(struct dhcps_lease* please)
{
(void)please;
return false;
}
bool DhcpServer::set_dhcps_lease_time(uint32 minute)
{
(void)minute;
return false;
}
bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg)
{
(void)level;
(void)optarg;
return false;
}
void DhcpServer::end() { }
bool DhcpServer::begin()
{
return false;
}
DhcpServer::DhcpServer(netif*) { }
DhcpServer::~DhcpServer()
{
end();
}
extern "C"
{
#include <user_interface.h>
bool wifi_softap_dhcps_start(void)
{
return true;
}
enum dhcp_status wifi_softap_dhcps_status(void)
{
return DHCP_STARTED;
}
bool wifi_softap_dhcps_stop(void)
{
return true;
}
bool wifi_softap_set_dhcps_lease(struct dhcps_lease* please)
{
(void)please;
return true;
}
bool wifi_softap_set_dhcps_lease_time(uint32 minute)
{
(void)minute;
return true;
}
bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg)
{
(void)level;
(void)optarg;
return true;
}
}

View File

@ -41,47 +41,6 @@
#include "MocklwIP.h"
#include <LwipDhcpServer.h>
bool DhcpServer::set_dhcps_lease(struct dhcps_lease* please)
{
(void)please;
return false;
}
bool DhcpServer::set_dhcps_lease_time(uint32 minute)
{
(void)minute;
return false;
}
bool DhcpServer::set_dhcps_offer_option(uint8 level, void* optarg)
{
(void)level;
(void)optarg;
return false;
}
void DhcpServer::end() { }
bool DhcpServer::begin(struct ip_info* info)
{
(void)info;
return false;
}
DhcpServer::DhcpServer(netif* netif)
{
(void)netif;
}
DhcpServer::~DhcpServer()
{
end();
}
DhcpServer dhcpSoftAP(nullptr);
extern "C"
{
#include <user_interface.h>
@ -400,21 +359,6 @@ extern "C"
(void)max_tpw;
}
bool wifi_softap_dhcps_start(void)
{
return true;
}
enum dhcp_status wifi_softap_dhcps_status(void)
{
return DHCP_STARTED;
}
bool wifi_softap_dhcps_stop(void)
{
return true;
}
bool wifi_softap_get_config(struct softap_config* config)
{
strcpy((char*)config->ssid, "apssid");
@ -450,25 +394,6 @@ extern "C"
return true;
}
bool wifi_softap_set_dhcps_lease(struct dhcps_lease* please)
{
(void)please;
return true;
}
bool wifi_softap_set_dhcps_lease_time(uint32 minute)
{
(void)minute;
return true;
}
bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg)
{
(void)level;
(void)optarg;
return true;
}
bool wifi_station_scan(struct scan_config* config, scan_done_cb_t cb)
{
(void)config;

View File

@ -382,17 +382,17 @@ void wifi_softap_free_station_info(void);
bool wifi_softap_dhcps_start(void);
bool wifi_softap_dhcps_stop(void);
#if 1 // dhcp server
// these functions are open-source, in dhcp server,
// which is now moved to lwIPDhcpServer.cpp (lwip2)
// (but still there with lwip1)
// esp8266/Arduino notice:
// these dhcp functions are no longer provided by the lwip lib
// only way to include them is to build our NonOS LwipDhcpServer helpers
// (ref. libraries/ESP8266WiFi/src/ESP8266WiFiAP-DhcpServer.cpp)
bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please);
bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please);
uint32 wifi_softap_get_dhcps_lease_time(void);
bool wifi_softap_set_dhcps_lease_time(uint32 minute);
bool wifi_softap_reset_dhcps_lease_time(void);
bool wifi_softap_add_dhcps_lease(uint8 *macaddr); // add static lease on the list, this will be the next available @
#endif // dhcp server
enum dhcp_status wifi_softap_dhcps_status(void);
bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg);

View File

@ -1,130 +0,0 @@
// adapted from dhcpserver.c distributed in esp8266 sdk 2.0.0
// same license may apply
#ifndef __DHCPS_H__
#define __DHCPS_H__
#include "glue.h" // for UDEBUG
#define USE_DNS
typedef struct dhcps_state{
sint16_t state;
} dhcps_state;
typedef struct dhcps_msg {
uint8_t op, htype, hlen, hops;
uint8_t xid[4];
uint16_t secs, flags;
uint8_t ciaddr[4];
uint8_t yiaddr[4];
uint8_t siaddr[4];
uint8_t giaddr[4];
uint8_t chaddr[16];
uint8_t sname[64];
uint8_t file[128];
uint8_t options[312];
}dhcps_msg;
#ifndef LWIP_OPEN_SRC
struct dhcps_lease {
bool enable;
struct ipv4_addr start_ip;
struct ipv4_addr end_ip;
};
enum dhcps_offer_option{
OFFER_START = 0x00,
OFFER_ROUTER = 0x01,
OFFER_END
};
#endif
typedef enum {
DHCPS_TYPE_DYNAMIC,
DHCPS_TYPE_STATIC
} dhcps_type_t;
typedef enum {
DHCPS_STATE_ONLINE,
DHCPS_STATE_OFFLINE
} dhcps_state_t;
struct dhcps_pool{
struct ipv4_addr ip;
uint8 mac[6];
uint32 lease_timer;
dhcps_type_t type;
dhcps_state_t state;
};
typedef struct _list_node{
void *pnode;
struct _list_node *pnext;
}list_node;
extern uint32 dhcps_lease_time;
#define DHCPS_LEASE_TIMER dhcps_lease_time //0x05A0
#define DHCPS_MAX_LEASE 0x64
#define BOOTP_BROADCAST 0x8000
#define DHCP_REQUEST 1
#define DHCP_REPLY 2
#define DHCP_HTYPE_ETHERNET 1
#define DHCP_HLEN_ETHERNET 6
#define DHCP_MSG_LEN 236
#define DHCPS_SERVER_PORT 67
#define DHCPS_CLIENT_PORT 68
#define DHCPDISCOVER 1
#define DHCPOFFER 2
#define DHCPREQUEST 3
#define DHCPDECLINE 4
#define DHCPACK 5
#define DHCPNAK 6
#define DHCPRELEASE 7
#define DHCP_OPTION_SUBNET_MASK 1
#define DHCP_OPTION_ROUTER 3
#define DHCP_OPTION_DNS_SERVER 6
#define DHCP_OPTION_REQ_IPADDR 50
#define DHCP_OPTION_LEASE_TIME 51
#define DHCP_OPTION_MSG_TYPE 53
#define DHCP_OPTION_SERVER_ID 54
#define DHCP_OPTION_INTERFACE_MTU 26
#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31
#define DHCP_OPTION_BROADCAST_ADDRESS 28
#define DHCP_OPTION_REQ_LIST 55
#define DHCP_OPTION_END 255
//#define USE_CLASS_B_NET 1
#define DHCPS_DEBUG UDEBUG
#define MAX_STATION_NUM 8
#define DHCPS_STATE_OFFER 1
#define DHCPS_STATE_DECLINE 2
#define DHCPS_STATE_ACK 3
#define DHCPS_STATE_NAK 4
#define DHCPS_STATE_IDLE 5
#define DHCPS_STATE_RELEASE 6
#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0)
#ifdef __cplusplus
extern "C"
{
#endif
void dhcps_set_dns (int num, const ipv4_addr_t* dns);
void dhcps_start(struct ip_info *info);
void dhcps_stop(void);
#ifdef __cplusplus
}
#endif
#endif