Skip to content

Commit 4998243

Browse files
committed
DNSServer status getters added
add isUp() method - returns 'true' if server is up and UDP socket is listening for UDP req's add isCaptive() method - returns 'true' if server runs in catch-all (captive portal mode) some doxygen comments added start() method now keeps existing IP address if any
1 parent 77fbc2a commit 4998243

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

libraries/DNSServer/src/DNSServer.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ DNSServer::DNSServer(const String &domainName) : _port(DNS_DEFAULT_PORT), _ttl(h
1919

2020

2121
bool DNSServer::start(){
22-
if (WiFi.getMode() & WIFI_AP){
23-
_resolvedIP = WiFi.softAPIP();
24-
} else return false; // won't run if WiFi is not in AP mode
22+
if (_resolvedIP.operator uint32_t() == 0){ // no address is set, try to obtain AP interface's IP
23+
if (WiFi.getMode() & WIFI_AP){
24+
_resolvedIP = WiFi.softAPIP();
25+
} else return false; // won't run if WiFi is not in AP mode
26+
}
2527

2628
_udp.close();
2729
_udp.onPacket([this](AsyncUDPPacket& pkt){ this->_handleUDP(pkt); });
2830
return _udp.listen(_port);
2931
}
3032

31-
bool DNSServer::start(const uint16_t &port, const String &domainName,
32-
const IPAddress &resolvedIP)
33-
{
33+
bool DNSServer::start(uint16_t port, const String &domainName, const IPAddress &resolvedIP){
3434
_port = port;
3535
if (domainName != "*"){
3636
_domainName = domainName;

libraries/DNSServer/src/DNSServer.h

+61-5
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,50 @@ struct DNSQuestion
7575
class DNSServer
7676
{
7777
public:
78+
/**
79+
* @brief Construct a new DNSServer object
80+
* by default server is configured to run in "Captive-portal" mode
81+
* it must be started with start() call to establish a listening socket
82+
*
83+
*/
7884
DNSServer();
85+
7986
/**
8087
* @brief Construct a new DNSServer object
8188
* builds DNS server with default parameters
8289
* @param domainName - domain name to serve
8390
*/
8491
DNSServer(const String &domainName);
8592
~DNSServer(){}; // default d-tor
86-
void processNextRequest(){}; // stub, left for compatibility with an old version
93+
94+
// Copy semantics not implemented (won't run on same UDP port anyway)
95+
DNSServer(const DNSServer&) = delete;
96+
DNSServer& operator=(const DNSServer&) = delete;
97+
98+
99+
/**
100+
* @brief stub, left for compatibility with an old version
101+
* does nothing actually
102+
*
103+
*/
104+
void processNextRequest(){};
105+
106+
/**
107+
* @brief Set the Error Reply Code for all req's not matching predifined domain
108+
*
109+
* @param replyCode
110+
*/
87111
void setErrorReplyCode(const DNSReplyCode &replyCode);
112+
113+
/**
114+
* @brief set TTL for successfull replies
115+
*
116+
* @param ttl in seconds
117+
*/
88118
void setTTL(const uint32_t &ttl);
89119

90120
/**
91-
* @brief Starts a server with current configuration or with default parameters
121+
* @brief (re)Starts a server with current configuration or with default parameters
92122
* if it's the first call.
93123
* Defaults are:
94124
* port: 53
@@ -100,13 +130,39 @@ class DNSServer
100130
*/
101131
bool start();
102132

103-
// Returns true if successful, false if there are no sockets available
104-
bool start(const uint16_t &port,
133+
/**
134+
* @brief (re)Starts a server with provided configuration
135+
*
136+
* @return true on success
137+
* @return false if IP or socket error
138+
*/
139+
bool start(uint16_t port,
105140
const String &domainName,
106141
const IPAddress &resolvedIP);
107-
// stops the DNS server
142+
143+
/**
144+
* @brief stops the server and close UDP socket
145+
*
146+
*/
108147
void stop();
109148

149+
/**
150+
* @brief returns true if DNS server runs in captive-portal mode
151+
* i.e. all requests are served with AP's ip address
152+
*
153+
* @return true if catch-all mode active
154+
* @return false otherwise
155+
*/
156+
inline bool isCaptive() const { return _domainName.isEmpty(); };
157+
158+
/**
159+
* @brief returns 'true' if server is up and UDP socket is listening for UDP req's
160+
*
161+
* @return true if server is up
162+
* @return false otherwise
163+
*/
164+
inline bool isUp() { return _udp.connected(); };
165+
110166
private:
111167
AsyncUDP _udp;
112168
uint16_t _port;

0 commit comments

Comments
 (0)