Skip to content

Commit 06cf787

Browse files
authored
Merge pull request #10602 from tymoteuszblochmobica/dns
Runtime DNS server addition implement
2 parents 4cf96b8 + cdd31a5 commit 06cf787

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
@@ -198,6 +198,39 @@ nsapi_error_t LWIP::get_dns_server(int index, SocketAddress *address, const char
198198
return NSAPI_ERROR_NO_ADDRESS;
199199
}
200200

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