Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
lwip_dns_resolver.h File Reference
#include "lwip/ip_addr.h"
#include <stdint.h>
+ Include dependency graph for lwip_dns_resolver.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool resolveHostnameBlocking (const char *hostname, ip_addr_t *result, uint32_t timeout_ms=5000)
 Resolve hostname to IP address using lwIP DNS (blocking).
 

Function Documentation

◆ resolveHostnameBlocking()

bool resolveHostnameBlocking ( const char *  hostname,
ip_addr_t *  result,
uint32_t  timeout_ms = 5000 
)
Parameters
hostnameThe hostname to resolve.
resultPointer to store resolved IP address.
timeout_msMax wait time in milliseconds.
Returns
true if resolved successfully, false otherwise.

Resolve hostname to IP address using lwIP DNS (blocking).

Parameters
hostnameThe host to resolve.
resultPointer to an ip_addr_t to store the result.
timeout_msMax time to wait for resolution.
Returns
true if successful, false otherwise.

Definition at line 29 of file lwip_dns_resolver.cpp.

29 {
30
31 TRACE("[DNS] Starting DNS lookup for %s\n", hostname);
32
33 dns_done = false;
34 err_t err = dns_gethostbyname(hostname, &resolved_ip, dns_callback, nullptr);
35
36 if (err == ERR_OK) {
37 TRACE("[DNS] Hostname resolved immediately: %s -> %s\n", hostname, ipaddr_ntoa(&resolved_ip));
38 *result = resolved_ip;
39 return true;
40 } else if (err == ERR_INPROGRESS) {
41 TRACE("[DNS] Lookup in progress for %s, waiting...\n", hostname);
42 } else {
43 TRACE("[DNS] dns_gethostbyname failed with error %d\n", err);
44 return false;
45 }
46
47 // Wait for async resolution
48 absolute_time_t deadline = make_timeout_time_ms(timeout_ms);
49 while (!dns_done && absolute_time_diff_us(get_absolute_time(), deadline) > 0) {
50 sleep_ms(10);
51 }
52
53 if (dns_done && !ip_addr_isany(&resolved_ip)) {
54 TRACE("[DNS] DNS resolved after wait: %s -> %s\n", hostname, ipaddr_ntoa(&resolved_ip));
55 *result = resolved_ip;
56 return true;
57 } else {
58 printf("[DNS] DNS resolution timed out or returned null\n");
59 return false;
60 }
61}
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187
static volatile bool dns_done
static ip_addr_t resolved_ip
static void dns_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)

References dns_callback(), dns_done, resolved_ip, and TRACE.

Referenced by Tcp::connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: