Skip to content

Commit cdd31a5

Browse files
Runtime DNS server addition implement
1 parent b21c278 commit cdd31a5

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

features/lwipstack/LWIPStack.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,39 @@ nsapi_error_t LWIP::get_dns_server(int index, SocketAddress *address, const char
195195
return NSAPI_ERROR_NO_ADDRESS;
196196
}
197197

198+
nsapi_error_t LWIP::add_dns_server(const SocketAddress &address, const char *interface_name)
199+
{
200+
int index;
201+
nsapi_addr_t addr = address.get_addr();
202+
const ip_addr_t *ip_addr_move;
203+
ip_addr_t ip_addr;
204+
205+
convert_mbed_addr_to_lwip(&ip_addr, &addr);
206+
207+
if (ip_addr_isany(&ip_addr)) {
208+
return NSAPI_ERROR_NO_ADDRESS;
209+
}
210+
211+
if (interface_name == NULL) {
212+
for (index = DNS_MAX_SERVERS - 1; index > 0; index--) {
213+
ip_addr_move = dns_getserver(index - 1, NULL);
214+
if (!ip_addr_isany(ip_addr_move)) {
215+
dns_setserver(index, ip_addr_move, NULL);
216+
}
217+
}
218+
dns_setserver(0, &ip_addr, NULL);
219+
} else {
220+
for (index = DNS_MAX_SERVERS - 1; index > 0; index--) {
221+
ip_addr_move = dns_get_interface_server(index - 1, interface_name);
222+
if (!ip_addr_isany(ip_addr_move)) {
223+
dns_add_interface_server(index, interface_name, ip_addr_move);
224+
}
225+
}
226+
dns_add_interface_server(0, interface_name, &ip_addr);
227+
}
228+
return NSAPI_ERROR_OK;
229+
}
230+
198231
void LWIP::tcpip_thread_callback(void *ptr)
199232
{
200233
lwip_callback *cb = static_cast<lwip_callback *>(ptr);

features/lwipstack/LWIPStack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
272272
*/
273273
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name);
274274

275+
/** Add a domain name server to list of servers to query
276+
*
277+
* @param address Destination for the host address
278+
* @param interface_name Network interface name
279+
* @return NSAPI_ERROR_OK on success, negative error code on failure
280+
*/
281+
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name);
282+
275283
/** Get the local IP address
276284
*
277285
* @return Null-terminated representation of the local IP address

features/lwipstack/lwip/src/core/lwip_dns.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ dns_get_interface_server(u8_t numdns, const char *interface_name)
487487
{
488488
struct dns_server_interface *interface_server;
489489

490-
if (numdns < DNS_MAX_SERVERS) {
490+
if (numdns >= DNS_MAX_SERVERS) {
491491
return IP_ADDR_ANY;
492492
}
493493

0 commit comments

Comments
 (0)