14#ifndef ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
15#define ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
60#define DNS_RR_TYPE_PRIVATE_START_VALUE 65280
62#define DNS_RR_TYPE_PRIVATE_END_VALUE 65534
72 unsigned int val = (
unsigned int)type;
97#if defined(CONFIG_DNS_RESOLVER_MAX_NAME_LEN)
98#define DNS_MAX_NAME_SIZE CONFIG_DNS_RESOLVER_MAX_NAME_LEN
100#define DNS_MAX_NAME_SIZE 20
104#if defined(CONFIG_DNS_RESOLVER_MAX_TEXT_LEN)
105#define DNS_MAX_TEXT_SIZE CONFIG_DNS_RESOLVER_MAX_TEXT_LEN
107#define DNS_MAX_TEXT_SIZE 64
111#if defined(CONFIG_DNS_RESOLVER_MAX_PRIVATE_DATA_LEN)
112#define DNS_MAX_PRIVATE_DATA_SIZE CONFIG_DNS_RESOLVER_MAX_PRIVATE_DATA_LEN
114#define DNS_MAX_PRIVATE_DATA_SIZE 128
119#define DNS_BUF_TIMEOUT K_MSEC(500)
122#if defined(CONFIG_DNS_RESOLVER_MAX_ANSWER_SIZE)
123#define DNS_RESOLVER_MAX_BUF_SIZE CONFIG_DNS_RESOLVER_MAX_ANSWER_SIZE
125#define DNS_RESOLVER_MAX_BUF_SIZE 512
131#if defined(CONFIG_DNS_RESOLVER_MAX_SERVERS)
132#define DNS_RESOLVER_MAX_SERVERS CONFIG_DNS_RESOLVER_MAX_SERVERS
134#define DNS_RESOLVER_MAX_SERVERS 0
137#if defined(CONFIG_DNS_NUM_CONCUR_QUERIES)
138#define DNS_NUM_CONCUR_QUERIES CONFIG_DNS_NUM_CONCUR_QUERIES
140#define DNS_NUM_CONCUR_QUERIES 1
143#if defined(CONFIG_NET_IF_MAX_IPV6_COUNT)
144#define MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
146#define MAX_IPV6_IFACE_COUNT 1
149#if defined(CONFIG_NET_IF_MAX_IPV4_COUNT)
150#define MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
152#define MAX_IPV4_IFACE_COUNT 1
158#if defined(CONFIG_MDNS_RESOLVER)
159#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
160#define MDNS_SERVER_COUNT 2
162#define MDNS_SERVER_COUNT 1
165#define MDNS_SERVER_COUNT 0
171#if defined(CONFIG_LLMNR_RESOLVER)
172#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
173#define LLMNR_SERVER_COUNT 2
175#define LLMNR_SERVER_COUNT 1
178#define LLMNR_SERVER_COUNT 0
181#define DNS_MAX_MCAST_SERVERS (MDNS_SERVER_COUNT + LLMNR_SERVER_COUNT)
183#if defined(CONFIG_MDNS_RESPONDER)
184#if defined(CONFIG_NET_IPV6)
185#define MDNS_MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
187#define MDNS_MAX_IPV6_IFACE_COUNT 0
190#if defined(CONFIG_NET_IPV4)
191#define MDNS_MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
193#define MDNS_MAX_IPV4_IFACE_COUNT 0
196#define MDNS_MAX_POLL (MDNS_MAX_IPV4_IFACE_COUNT + MDNS_MAX_IPV6_IFACE_COUNT)
198#define MDNS_MAX_POLL 0
201#if defined(CONFIG_LLMNR_RESPONDER)
202#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
203#define LLMNR_MAX_POLL 2
205#define LLMNR_MAX_POLL 1
208#define LLMNR_MAX_POLL 0
211#define DNS_RESOLVER_MAX_POLL (DNS_RESOLVER_MAX_SERVERS + DNS_MAX_MCAST_SERVERS)
214#define DNS_DISPATCHER_MAX_POLL (DNS_RESOLVER_MAX_POLL + MDNS_MAX_POLL + LLMNR_MAX_POLL)
216#if defined(CONFIG_ZVFS_POLL_MAX)
217BUILD_ASSERT(CONFIG_ZVFS_POLL_MAX >= DNS_DISPATCHER_MAX_POLL,
218 "CONFIG_ZVFS_POLL_MAX must be larger than " STRINGIFY(DNS_DISPATCHER_MAX_POLL));
224enum dns_socket_type {
225 DNS_SOCKET_RESOLVER = 1,
226 DNS_SOCKET_RESPONDER = 2
230struct mdns_responder_context;
231struct dns_socket_dispatcher;
247typedef int (*dns_socket_dispatcher_cb)(
struct dns_socket_dispatcher *ctx,
int sock,
249 struct net_buf *buf,
size_t data_len);
252struct dns_socket_dispatcher {
256 const struct net_socket_service_desc *svc;
262 struct dns_resolve_context *resolve_ctx;
263 struct mdns_responder_context *mdns_ctx;
267 enum dns_socket_type type;
269 struct net_sockaddr local_addr;
271 dns_socket_dispatcher_cb cb;
273 struct zsock_pollfd *fds;
283 struct dns_socket_dispatcher *pair;
287 k_timeout_t buf_timeout;
299int dns_dispatcher_register(
struct dns_socket_dispatcher *ctx);
310int dns_dispatcher_unregister(
struct dns_socket_dispatcher *ctx);
386#if defined(CONFIG_DNS_RESOLVER_PRIVATE_RR_SUPPORT) || defined(__DOXYGEN__)
473enum dns_resolve_context_state {
474 DNS_RESOLVE_CONTEXT_UNINITIALIZED = 0,
475 DNS_RESOLVE_CONTEXT_ACTIVE,
476 DNS_RESOLVE_CONTEXT_DEACTIVATING,
477 DNS_RESOLVE_CONTEXT_INACTIVE,
510 struct dns_socket_dispatcher dispatcher;
589 enum dns_resolve_context_state
state;
591#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING) || defined(__DOXYGEN__)
604struct mdns_probe_user_data {
605 struct mdns_responder_context *ctx;
610struct mdns_responder_context {
611 struct net_sockaddr server_addr;
612 struct dns_socket_dispatcher dispatcher;
613 struct zsock_pollfd fds[1];
615 struct net_if *iface;
616#if defined(CONFIG_MDNS_RESPONDER_PROBE)
617 struct k_work_delayable probe_timer;
618 struct dns_resolve_context probe_ctx;
619 struct mdns_probe_user_data probe_data;
653 const char *dns_servers_str[],
698 const char *servers_str[],
725 const char *servers_str[],
797 const char *query_name,
869 dns_id, cb, user_data, timeout);
884#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING) || defined(__DOXYGEN__)
999#if defined(CONFIG_DNS_RESOLVER_AUTO_INIT)
1000void dns_init_resolver(
void);
1003#define dns_init_resolver(...)
static bool dns_query_type_is_private(enum dns_query_type type)
Check if query type is a private RR (RFC 6895: 65280-65534).
Definition dns_resolve.h:70
int dns_resolve_reconfigure(struct dns_resolve_context *ctx, const char *servers_str[], const struct net_sockaddr *servers_sa[], enum dns_server_source source)
Reconfigure DNS resolving context.
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.
int dns_resolve_init(struct dns_resolve_context *ctx, const char *dns_servers_str[], const struct net_sockaddr *dns_servers_sa[])
Init DNS resolving context.
static void dns_resolve_enable_packet_forwarding(struct dns_resolve_context *ctx, dns_resolve_pkt_fw_cb_t cb)
Installs the packet forwarding callback to the DNS resolving context.
Definition dns_resolve.h:896
int dns_resolve_reconfigure_with_interfaces(struct dns_resolve_context *ctx, const char *servers_str[], const struct net_sockaddr *servers_sa[], int interfaces[], enum dns_server_source source)
Reconfigure DNS resolving context with new server list and allowing servers to be specified to a spec...
#define DNS_RR_TYPE_PRIVATE_END_VALUE
Private RR type range end (RFC 6895).
Definition dns_resolve.h:62
static int dns_cancel_addr_info(uint16_t dns_id)
Cancel a pending DNS query.
Definition dns_resolve.h:955
int dns_resolve_remove(struct dns_resolve_context *ctx, int if_index)
Remove servers from the DNS resolving context.
dns_resolve_status
Status values for the callback.
Definition dns_resolve.h:398
dns_query_type
DNS query type enum.
Definition dns_resolve.h:39
int dns_resolve_init_default(struct dns_resolve_context *ctx)
Init DNS resolving context with default Kconfig options.
int dns_resolve_cancel(struct dns_resolve_context *ctx, uint16_t dns_id)
Cancel a pending DNS query.
void(* dns_resolve_pkt_fw_cb_t)(struct net_buf *dns_data, size_t buf_len, void *user_data)
DNS resolve callback which passes the received packet from DNS server to application.
Definition dns_resolve.h:469
#define DNS_MAX_TEXT_SIZE
Max size of the resolved txt record.
Definition dns_resolve.h:107
dns_resolve_extension
Enumerate the extensions that are available in the address info.
Definition dns_resolve.h:317
#define DNS_RR_TYPE_PRIVATE_START_VALUE
Private RR type range start (RFC 6895).
Definition dns_resolve.h:60
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:100
#define DNS_MAX_PRIVATE_DATA_SIZE
Max size of private RR data.
Definition dns_resolve.h:114
static int dns_cancel_addr_info_with_name(const char *query_name, enum dns_query_type query_type, uint16_t dns_id)
Cancel a pending DNS query using id, name and type.
Definition dns_resolve.h:971
int dns_resolve_remove_source(struct dns_resolve_context *ctx, int if_index, enum dns_server_source source)
Remove servers from the DNS resolving context that were added by a specific source.
struct dns_resolve_context * dns_resolve_get_default(void)
Get default DNS context.
dns_server_source
Entity that added the DNS server.
Definition dns_resolve.h:81
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_resolve_service(struct dns_resolve_context *ctx, const char *query, uint16_t *dns_id, dns_resolve_cb_t cb, void *user_data, int32_t timeout)
Resolve DNS service.
Definition dns_resolve.h:861
int dns_resolve_remove_server_addresses(struct dns_resolve_context *ctx, const struct net_sockaddr *servers_sa[], int interfaces[])
Remove servers from the DNS resolving context that have a specific IP address.
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:930
void(* dns_resolve_cb_t)(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data)
DNS resolve callback.
Definition dns_resolve.h:454
@ DNS_EAI_MEMORY
Memory allocation failure.
Definition dns_resolve.h:418
@ DNS_EAI_NOTCANCELED
Request not canceled.
Definition dns_resolve.h:428
@ DNS_EAI_IDN_ENCODE
IDN encoding failed.
Definition dns_resolve.h:432
@ DNS_EAI_ADDRFAMILY
Address family for NAME not supported.
Definition dns_resolve.h:416
@ DNS_EAI_INPROGRESS
Processing request in progress.
Definition dns_resolve.h:424
@ DNS_EAI_FAIL
Non-recoverable failure in name res.
Definition dns_resolve.h:406
@ DNS_EAI_AGAIN
Temporary failure in name resolution.
Definition dns_resolve.h:404
@ DNS_EAI_NODATA
No address associated with NAME.
Definition dns_resolve.h:408
@ DNS_EAI_NONAME
NAME or SERVICE is unknown.
Definition dns_resolve.h:402
@ DNS_EAI_FAMILY
ai_family not supported
Definition dns_resolve.h:410
@ DNS_EAI_OVERFLOW
Argument buffer overflow.
Definition dns_resolve.h:422
@ DNS_EAI_CANCELED
Request canceled.
Definition dns_resolve.h:426
@ DNS_EAI_BADFLAGS
Invalid value for ai_flags field.
Definition dns_resolve.h:400
@ DNS_EAI_SOCKTYPE
ai_socktype not supported
Definition dns_resolve.h:412
@ DNS_EAI_ALLDONE
All requests done.
Definition dns_resolve.h:430
@ DNS_EAI_SYSTEM
System error returned in errno.
Definition dns_resolve.h:420
@ DNS_EAI_SERVICE
SRV not supported for ai_socktype.
Definition dns_resolve.h:414
@ DNS_QUERY_TYPE_ANY
Request all record types query.
Definition dns_resolve.h:53
@ DNS_QUERY_TYPE_CNAME
Canonical name query.
Definition dns_resolve.h:43
@ DNS_QUERY_TYPE_SRV
Service location query.
Definition dns_resolve.h:51
@ DNS_QUERY_TYPE_TXT
Text query.
Definition dns_resolve.h:47
@ DNS_QUERY_TYPE_PTR
Pointer query.
Definition dns_resolve.h:45
@ DNS_QUERY_TYPE_A
IPv4 query.
Definition dns_resolve.h:41
@ DNS_QUERY_TYPE_AAAA
IPv6 query.
Definition dns_resolve.h:49
@ DNS_QUERY_TYPE_RESERVED
Reserved query type value.
Definition dns_resolve.h:55
@ DNS_RESOLVE_TXT
TXT field is returned.
Definition dns_resolve.h:319
@ DNS_RESOLVE_NONE
No extension in use.
Definition dns_resolve.h:318
@ DNS_RESOLVE_PRIVATE
Private RR is returned.
Definition dns_resolve.h:321
@ DNS_RESOLVE_SRV
SRV field is returned.
Definition dns_resolve.h:320
@ DNS_SOURCE_MANUAL
Server information is added manually, for example by an application.
Definition dns_resolve.h:85
@ DNS_SOURCE_IPV6_RA
Server information is from IPv6 SLAAC (router advertisement).
Definition dns_resolve.h:91
@ DNS_SOURCE_DHCPV4
Server information is from DHCPv4 server.
Definition dns_resolve.h:87
@ DNS_SOURCE_UNKNOWN
Source is unknown.
Definition dns_resolve.h:83
@ DNS_SOURCE_PPP
Server information is from PPP.
Definition dns_resolve.h:93
@ DNS_SOURCE_DHCPV6
Server information is from DHCPv6 server.
Definition dns_resolve.h:89
uint32_t net_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.
Public API for network interface.
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:359
char ai_canonname[20+1]
NET_AF_LOCAL Canonical name of the address.
Definition dns_resolve.h:373
struct dns_resolve_srv ai_srv
SRV record info.
Definition dns_resolve.h:385
struct dns_resolve_txt ai_txt
TXT record info.
Definition dns_resolve.h:383
struct dns_resolve_private ai_private
Private RR info.
Definition dns_resolve.h:388
enum dns_resolve_extension ai_extension
What kind of extension is returned.
Definition dns_resolve.h:379
struct net_sockaddr ai_addr
NET_AF_INET or NET_AF_INET6 address info.
Definition dns_resolve.h:370
net_socklen_t ai_addrlen
Length of the ai_addr field or ai_canonname.
Definition dns_resolve.h:367
uint8_t ai_family
Address family of the address information and discriminator.
Definition dns_resolve.h:361
Result callbacks.
Definition dns_resolve.h:533
const char * query
String containing the thing to resolve like www.example.com.
Definition dns_resolve.h:563
uint16_t query_hash
Hash of the DNS name + query type we are querying.
Definition dns_resolve.h:577
uint8_t additional_queries
Definition dns_resolve.h:582
struct dns_resolve_context * ctx
Back pointer to ctx, needed in timeout handler.
Definition dns_resolve.h:538
void * user_data
User data.
Definition dns_resolve.h:547
struct k_work_delayable timer
Timeout timer.
Definition dns_resolve.h:535
uint16_t id
DNS id of this query.
Definition dns_resolve.h:569
k_timeout_t timeout
TX timeout.
Definition dns_resolve.h:550
dns_resolve_cb_t cb
Result callback.
Definition dns_resolve.h:544
bool cb_called
Flag to indicate that the callback has been called at least once.
Definition dns_resolve.h:585
enum dns_query_type query_type
Query type.
Definition dns_resolve.h:566
enum dns_server_source source
Source of the DNS server, e.g., manual, DHCPv4/6, etc.
Definition dns_resolve.h:500
int if_index
Network interface index if the DNS resolving should be done via this interface.
Definition dns_resolve.h:497
int sock
Connection to the DNS server.
Definition dns_resolve.h:492
uint8_t is_mdns
Is this server mDNS one.
Definition dns_resolve.h:503
uint8_t is_llmnr
Is this server LLMNR one.
Definition dns_resolve.h:506
struct net_sockaddr dns_server
DNS server information.
Definition dns_resolve.h:489
DNS resolve context structure.
Definition dns_resolve.h:485
dns_resolve_pkt_fw_cb_t pkt_fw_cb
DNS packet forwarding callback.
Definition dns_resolve.h:593
k_timeout_t buf_timeout
This timeout is also used when a buffer is required from the buffer pools.
Definition dns_resolve.h:525
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:589
struct k_mutex lock
Prevent concurrent access.
Definition dns_resolve.h:520
Private RR record information.
Definition dns_resolve.h:347
uint8_t data[128]
Raw data from the private RR.
Definition dns_resolve.h:353
size_t datalen
Length of the data field.
Definition dns_resolve.h:351
uint16_t type
RR type value (in private use range 65280-65534).
Definition dns_resolve.h:349
SRV record information.
Definition dns_resolve.h:333
uint16_t priority
Priority of the server order, lower value means higher priority.
Definition dns_resolve.h:335
char target[20+1]
Target field (NULL terminated).
Definition dns_resolve.h:343
size_t targetlen
Length of the target field.
Definition dns_resolve.h:341
uint16_t weight
Weight of the server for load balancing.
Definition dns_resolve.h:337
uint16_t port
Port number of the service.
Definition dns_resolve.h:339
TXT record information.
Definition dns_resolve.h:325
size_t textlen
Length of the text field.
Definition dns_resolve.h:327
char text[64+1]
Text field (NULL terminated).
Definition dns_resolve.h:329
Kernel mutex structure.
Definition kernel.h:3437
Kernel timeout type.
Definition clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4603
Network buffer representation.
Definition net_buf.h:1015
uint8_t user_data[]
System metadata for this buffer.
Definition net_buf.h:1125
Generic sockaddr struct.
Definition net_ip.h:449
Definition of the monitored socket/file descriptor.
Definition socket_poll.h:31