Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
lwip_dns_resolver.cpp File Reference
#include "lwip/dns.h"
#include "lwip/ip_addr.h"
#include "lwip/err.h"
#include "pico/time.h"
#include <cstring>
#include "framework_config.h"
#include "DebugTrace.h"
+ Include dependency graph for lwip_dns_resolver.cpp:

Go to the source code of this file.

Functions

 TRACE_INIT (LwipDnsResolver)
 
static void dns_callback (const char *name, const ip_addr_t *ipaddr, void *callback_arg)
 
bool resolveHostnameBlocking (const char *hostname, ip_addr_t *result, uint32_t timeout_ms=5000)
 Blocking DNS resolution using lwIP.
 

Variables

static volatile bool dns_done = false
 
static ip_addr_t resolved_ip
 

Function Documentation

◆ dns_callback()

static void dns_callback ( const char *  name,
const ip_addr_t *  ipaddr,
void *  callback_arg 
)
static

Definition at line 13 of file lwip_dns_resolver.cpp.

13 {
14 if (ipaddr) {
15 resolved_ip = *ipaddr;
16 } else {
17 ip_addr_set_any(IPADDR_TYPE_V4, &resolved_ip);
18 }
19 dns_done = true;
20}
static volatile bool dns_done
static ip_addr_t resolved_ip

References dns_done, and resolved_ip.

Referenced by resolveHostnameBlocking().

+ Here is the caller graph for this function:

◆ resolveHostnameBlocking()

bool resolveHostnameBlocking ( const char *  hostname,
ip_addr_t *  result,
uint32_t  timeout_ms = 5000 
)

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 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:

◆ TRACE_INIT()

TRACE_INIT ( LwipDnsResolver  )

Variable Documentation

◆ dns_done

volatile bool dns_done = false
static

Definition at line 10 of file lwip_dns_resolver.cpp.

Referenced by dns_callback(), and resolveHostnameBlocking().

◆ resolved_ip

ip_addr_t resolved_ip
static

Definition at line 11 of file lwip_dns_resolver.cpp.

Referenced by dns_callback(), and resolveHostnameBlocking().