13#ifndef ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
14#define ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
45#ifndef DNS_MAX_NAME_SIZE
46#define DNS_MAX_NAME_SIZE 20
51#define DNS_BUF_TIMEOUT K_MSEC(500)
54#define DNS_RESOLVER_MAX_BUF_SIZE 512
59#if defined(CONFIG_DNS_RESOLVER_MAX_SERVERS)
60#define DNS_RESOLVER_MAX_SERVERS CONFIG_DNS_RESOLVER_MAX_SERVERS
62#define DNS_RESOLVER_MAX_SERVERS 0
65#if defined(CONFIG_DNS_NUM_CONCUR_QUERIES)
66#define DNS_NUM_CONCUR_QUERIES CONFIG_DNS_NUM_CONCUR_QUERIES
68#define DNS_NUM_CONCUR_QUERIES 1
71#if defined(CONFIG_NET_IF_MAX_IPV6_COUNT)
72#define MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
74#define MAX_IPV6_IFACE_COUNT 1
77#if defined(CONFIG_NET_IF_MAX_IPV4_COUNT)
78#define MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
80#define MAX_IPV4_IFACE_COUNT 1
86#if defined(CONFIG_MDNS_RESOLVER)
87#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
88#define MDNS_SERVER_COUNT 2
90#define MDNS_SERVER_COUNT 1
93#define MDNS_SERVER_COUNT 0
99#if defined(CONFIG_LLMNR_RESOLVER)
100#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
101#define LLMNR_SERVER_COUNT 2
103#define LLMNR_SERVER_COUNT 1
106#define LLMNR_SERVER_COUNT 0
109#define DNS_MAX_MCAST_SERVERS (MDNS_SERVER_COUNT + LLMNR_SERVER_COUNT)
111#if defined(CONFIG_MDNS_RESPONDER)
112#if defined(CONFIG_NET_IPV6)
113#define MDNS_MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
115#define MDNS_MAX_IPV6_IFACE_COUNT 0
118#if defined(CONFIG_NET_IPV4)
119#define MDNS_MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
121#define MDNS_MAX_IPV4_IFACE_COUNT 0
124#define MDNS_MAX_POLL (MDNS_MAX_IPV4_IFACE_COUNT + MDNS_MAX_IPV6_IFACE_COUNT)
126#define MDNS_MAX_POLL 0
129#if defined(CONFIG_LLMNR_RESPONDER)
130#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
131#define LLMNR_MAX_POLL 2
133#define LLMNR_MAX_POLL 1
136#define LLMNR_MAX_POLL 0
139#define DNS_RESOLVER_MAX_POLL (DNS_RESOLVER_MAX_SERVERS + DNS_MAX_MCAST_SERVERS)
142#define DNS_DISPATCHER_MAX_POLL (DNS_RESOLVER_MAX_POLL + MDNS_MAX_POLL + LLMNR_MAX_POLL)
144#if defined(CONFIG_ZVFS_POLL_MAX)
145BUILD_ASSERT(CONFIG_ZVFS_POLL_MAX >= DNS_DISPATCHER_MAX_POLL,
146 "CONFIG_ZVFS_POLL_MAX must be larger than " STRINGIFY(DNS_DISPATCHER_MAX_POLL));
152enum dns_socket_type {
153 DNS_SOCKET_RESOLVER = 1,
154 DNS_SOCKET_RESPONDER = 2
158struct mdns_responder_context;
159struct dns_socket_dispatcher;
175typedef int (*dns_socket_dispatcher_cb)(
struct dns_socket_dispatcher *ctx,
int sock,
176 struct sockaddr *addr,
size_t addrlen,
177 struct net_buf *buf,
size_t data_len);
180struct dns_socket_dispatcher {
191 struct mdns_responder_context *mdns_ctx;
195 enum dns_socket_type type;
199 dns_socket_dispatcher_cb cb;
211 struct dns_socket_dispatcher *pair;
218struct mdns_responder_context {
220 struct dns_socket_dispatcher dispatcher;
234int dns_dispatcher_register(
struct dns_socket_dispatcher *ctx);
245int dns_dispatcher_unregister(
struct dns_socket_dispatcher *ctx);
328enum dns_resolve_context_state {
329 DNS_RESOLVE_CONTEXT_ACTIVE,
330 DNS_RESOLVE_CONTEXT_DEACTIVATING,
331 DNS_RESOLVE_CONTEXT_INACTIVE,
361 struct dns_socket_dispatcher dispatcher;
432 enum dns_resolve_context_state
state;
463 const char *dns_servers_str[],
464 const struct sockaddr *dns_servers_sa[]);
507 const char *servers_str[],
508 const struct sockaddr *servers_sa[]);
537 const char *query_name,
653#if defined(CONFIG_DNS_RESOLVER_AUTO_INIT)
654void dns_init_resolver(
void);
657#define dns_init_resolver(...)
int dns_resolve_name(struct dns_resolve_context *ctx, const char *query, enum dns_query_type type, uint16_t *dns_id, dns_resolve_cb_t cb, void *user_data, int32_t timeout)
Resolve DNS name.
static int dns_cancel_addr_info(uint16_t dns_id)
Cancel a pending DNS query.
Definition dns_resolve.h:639
dns_resolve_status
Status values for the callback.
Definition dns_resolve.h:266
dns_query_type
DNS query type enum.
Definition dns_resolve.h:37
int dns_resolve_init_default(struct dns_resolve_context *ctx)
Init DNS resolving context with default Kconfig options.
int dns_resolve_init(struct dns_resolve_context *ctx, const char *dns_servers_str[], const struct sockaddr *dns_servers_sa[])
Init DNS resolving context.
int dns_resolve_cancel(struct dns_resolve_context *ctx, uint16_t dns_id)
Cancel a pending DNS query.
void(* dns_resolve_cb_t)(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data)
DNS resolve callback.
Definition dns_resolve.h:322
int dns_resolve_reconfigure(struct dns_resolve_context *ctx, const char *servers_str[], const struct sockaddr *servers_sa[])
Reconfigure DNS resolving context.
int dns_resolve_close(struct dns_resolve_context *ctx)
Close DNS resolving context.
#define DNS_MAX_NAME_SIZE
Max size of the resolved name.
Definition dns_resolve.h:46
struct dns_resolve_context * dns_resolve_get_default(void)
Get default DNS context.
int dns_resolve_cancel_with_name(struct dns_resolve_context *ctx, uint16_t dns_id, const char *query_name, enum dns_query_type query_type)
Cancel a pending DNS query using id, name and type.
static int dns_get_addr_info(const char *query, enum dns_query_type type, uint16_t *dns_id, dns_resolve_cb_t cb, void *user_data, int32_t timeout)
Get IP address info from DNS.
Definition dns_resolve.h:614
@ DNS_EAI_MEMORY
Memory allocation failure.
Definition dns_resolve.h:286
@ DNS_EAI_NOTCANCELED
Request not canceled.
Definition dns_resolve.h:296
@ DNS_EAI_IDN_ENCODE
IDN encoding failed.
Definition dns_resolve.h:300
@ DNS_EAI_ADDRFAMILY
Address family for NAME not supported.
Definition dns_resolve.h:284
@ DNS_EAI_INPROGRESS
Processing request in progress.
Definition dns_resolve.h:292
@ DNS_EAI_FAIL
Non-recoverable failure in name res.
Definition dns_resolve.h:274
@ DNS_EAI_AGAIN
Temporary failure in name resolution.
Definition dns_resolve.h:272
@ DNS_EAI_NODATA
No address associated with NAME.
Definition dns_resolve.h:276
@ DNS_EAI_NONAME
NAME or SERVICE is unknown.
Definition dns_resolve.h:270
@ DNS_EAI_FAMILY
‘ai_family’ not supported
Definition dns_resolve.h:278
@ DNS_EAI_OVERFLOW
Argument buffer overflow.
Definition dns_resolve.h:290
@ DNS_EAI_CANCELED
Request canceled.
Definition dns_resolve.h:294
@ DNS_EAI_BADFLAGS
Invalid value for ‘ai_flags’ field.
Definition dns_resolve.h:268
@ DNS_EAI_SOCKTYPE
‘ai_socktype’ not supported
Definition dns_resolve.h:280
@ DNS_EAI_ALLDONE
All requests done.
Definition dns_resolve.h:298
@ DNS_EAI_SYSTEM
System error returned in ‘errno’.
Definition dns_resolve.h:288
@ DNS_EAI_SERVICE
SRV not supported for ‘ai_socktype’.
Definition dns_resolve.h:282
@ DNS_QUERY_TYPE_A
IPv4 query.
Definition dns_resolve.h:39
@ DNS_QUERY_TYPE_AAAA
IPv6 query.
Definition dns_resolve.h:41
size_t socklen_t
Length of a socket address.
Definition net_ip.h:172
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
Network core definitions.
IPv6 and IPv4 definitions.
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Address info struct is passed to callback that gets all the results.
Definition dns_resolve.h:252
char ai_canonname[20+1]
Canonical name of the address.
Definition dns_resolve.h:260
struct sockaddr ai_addr
IP address information.
Definition dns_resolve.h:254
socklen_t ai_addrlen
Length of the ai_addr field.
Definition dns_resolve.h:256
uint8_t ai_family
Address family of the address information.
Definition dns_resolve.h:258
Result callbacks.
Definition dns_resolve.h:384
const char * query
String containing the thing to resolve like www.example.com.
Definition dns_resolve.h:414
uint16_t query_hash
Hash of the DNS name + query type we are querying.
Definition dns_resolve.h:428
struct dns_resolve_context * ctx
Back pointer to ctx, needed in timeout handler.
Definition dns_resolve.h:389
void * user_data
User data.
Definition dns_resolve.h:398
struct k_work_delayable timer
Timeout timer.
Definition dns_resolve.h:386
uint16_t id
DNS id of this query.
Definition dns_resolve.h:420
k_timeout_t timeout
TX timeout.
Definition dns_resolve.h:401
dns_resolve_cb_t cb
Result callback.
Definition dns_resolve.h:395
enum dns_query_type query_type
Query type.
Definition dns_resolve.h:417
List of configured DNS servers.
Definition dns_resolve.h:341
int if_index
Network interface index if the DNS resolving should be done via this interface.
Definition dns_resolve.h:351
int sock
Connection to the DNS server.
Definition dns_resolve.h:346
uint8_t is_mdns
Is this server mDNS one.
Definition dns_resolve.h:354
uint8_t is_llmnr
Is this server LLMNR one.
Definition dns_resolve.h:357
DNS resolve context structure.
Definition dns_resolve.h:339
k_timeout_t buf_timeout
This timeout is also used when a buffer is required from the buffer pools.
Definition dns_resolve.h:376
struct dns_resolve_context::dns_pending_query queries[DNS_NUM_CONCUR_QUERIES]
struct dns_resolve_context::dns_server servers[DNS_RESOLVER_MAX_POLL]
enum dns_resolve_context_state state
Is this context in use.
Definition dns_resolve.h:432
struct k_mutex lock
Prevent concurrent access.
Definition dns_resolve.h:371
Mutex Structure.
Definition kernel.h:3025
Kernel timeout type.
Definition sys_clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4034
Network buffer representation.
Definition net_buf.h:1006
Main structure holding socket service configuration information.
Definition socket_service.h:70
Generic sockaddr struct.
Definition net_ip.h:408
Definition of the monitored socket/file descriptor.
Definition socket_poll.h:31