Zephyr API Documentation 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
dns_resolve.h
Go to the documentation of this file.
1
6
7/*
8 * Copyright (c) 2017 Intel Corporation
9 * Copyright 2025 NXP
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
14#ifndef ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
15#define ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
16
17#include <zephyr/kernel.h>
18#include <zephyr/net/net_ip.h>
19#include <zephyr/net/net_if.h>
21#include <zephyr/net/net_core.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
35
53
71
73#if defined(CONFIG_DNS_RESOLVER_MAX_NAME_LEN)
74#define DNS_MAX_NAME_SIZE CONFIG_DNS_RESOLVER_MAX_NAME_LEN
75#else
76#define DNS_MAX_NAME_SIZE 20
77#endif /* CONFIG_DNS_RESOLVER_MAX_NAME_LEN */
78
80#if defined(CONFIG_DNS_RESOLVER_MAX_TEXT_LEN)
81#define DNS_MAX_TEXT_SIZE CONFIG_DNS_RESOLVER_MAX_TEXT_LEN
82#else
83#define DNS_MAX_TEXT_SIZE 64
84#endif /* CONFIG_DNS_RESOLVER_MAX_TEXT_LEN */
85
87
88#define DNS_BUF_TIMEOUT K_MSEC(500) /* ms */
89
90/* This value is recommended by RFC 1035 */
91#if defined(CONFIG_DNS_RESOLVER_MAX_ANSWER_SIZE)
92#define DNS_RESOLVER_MAX_BUF_SIZE CONFIG_DNS_RESOLVER_MAX_ANSWER_SIZE
93#else
94#define DNS_RESOLVER_MAX_BUF_SIZE 512
95#endif /* CONFIG_DNS_RESOLVER_MAX_ANSWER_SIZE */
96
97/* Make sure that we can compile things even if CONFIG_DNS_RESOLVER
98 * is not enabled.
99 */
100#if defined(CONFIG_DNS_RESOLVER_MAX_SERVERS)
101#define DNS_RESOLVER_MAX_SERVERS CONFIG_DNS_RESOLVER_MAX_SERVERS
102#else
103#define DNS_RESOLVER_MAX_SERVERS 0
104#endif
105
106#if defined(CONFIG_DNS_NUM_CONCUR_QUERIES)
107#define DNS_NUM_CONCUR_QUERIES CONFIG_DNS_NUM_CONCUR_QUERIES
108#else
109#define DNS_NUM_CONCUR_QUERIES 1
110#endif
111
112#if defined(CONFIG_NET_IF_MAX_IPV6_COUNT)
113#define MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
114#else
115#define MAX_IPV6_IFACE_COUNT 1
116#endif
117
118#if defined(CONFIG_NET_IF_MAX_IPV4_COUNT)
119#define MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
120#else
121#define MAX_IPV4_IFACE_COUNT 1
122#endif
123
124/* If mDNS is enabled, then add some extra well known multicast servers to the
125 * server list.
126 */
127#if defined(CONFIG_MDNS_RESOLVER)
128#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
129#define MDNS_SERVER_COUNT 2
130#else
131#define MDNS_SERVER_COUNT 1
132#endif /* CONFIG_NET_IPV6 && CONFIG_NET_IPV4 */
133#else
134#define MDNS_SERVER_COUNT 0
135#endif /* CONFIG_MDNS_RESOLVER */
136
137/* If LLMNR is enabled, then add some extra well known multicast servers to the
138 * server list.
139 */
140#if defined(CONFIG_LLMNR_RESOLVER)
141#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
142#define LLMNR_SERVER_COUNT 2
143#else
144#define LLMNR_SERVER_COUNT 1
145#endif /* CONFIG_NET_IPV6 && CONFIG_NET_IPV4 */
146#else
147#define LLMNR_SERVER_COUNT 0
148#endif /* CONFIG_MDNS_RESOLVER */
149
150#define DNS_MAX_MCAST_SERVERS (MDNS_SERVER_COUNT + LLMNR_SERVER_COUNT)
151
152#if defined(CONFIG_MDNS_RESPONDER)
153#if defined(CONFIG_NET_IPV6)
154#define MDNS_MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
155#else
156#define MDNS_MAX_IPV6_IFACE_COUNT 0
157#endif /* CONFIG_NET_IPV6 */
158
159#if defined(CONFIG_NET_IPV4)
160#define MDNS_MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
161#else
162#define MDNS_MAX_IPV4_IFACE_COUNT 0
163#endif /* CONFIG_NET_IPV4 */
164
165#define MDNS_MAX_POLL (MDNS_MAX_IPV4_IFACE_COUNT + MDNS_MAX_IPV6_IFACE_COUNT)
166#else
167#define MDNS_MAX_POLL 0
168#endif /* CONFIG_MDNS_RESPONDER */
169
170#if defined(CONFIG_LLMNR_RESPONDER)
171#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
172#define LLMNR_MAX_POLL 2
173#else
174#define LLMNR_MAX_POLL 1
175#endif
176#else
177#define LLMNR_MAX_POLL 0
178#endif /* CONFIG_LLMNR_RESPONDER */
179
180#define DNS_RESOLVER_MAX_POLL (DNS_RESOLVER_MAX_SERVERS + DNS_MAX_MCAST_SERVERS)
181
183#define DNS_DISPATCHER_MAX_POLL (DNS_RESOLVER_MAX_POLL + MDNS_MAX_POLL + LLMNR_MAX_POLL)
184
185#if defined(CONFIG_ZVFS_POLL_MAX)
186BUILD_ASSERT(CONFIG_ZVFS_POLL_MAX >= DNS_DISPATCHER_MAX_POLL,
187 "CONFIG_ZVFS_POLL_MAX must be larger than " STRINGIFY(DNS_DISPATCHER_MAX_POLL));
188#endif
189
193enum dns_socket_type {
194 DNS_SOCKET_RESOLVER = 1,
195 DNS_SOCKET_RESPONDER = 2
196};
197
199struct mdns_responder_context;
200struct dns_socket_dispatcher;
201
216typedef int (*dns_socket_dispatcher_cb)(struct dns_socket_dispatcher *ctx, int sock,
217 struct sockaddr *addr, size_t addrlen,
218 struct net_buf *buf, size_t data_len);
219
221struct dns_socket_dispatcher {
223 sys_snode_t node;
225 const struct net_socket_service_desc *svc;
229 union {
230 void *ctx;
231 struct dns_resolve_context *resolve_ctx;
232 struct mdns_responder_context *mdns_ctx;
233 };
234
236 enum dns_socket_type type;
238 struct sockaddr local_addr;
240 dns_socket_dispatcher_cb cb;
242 struct zsock_pollfd *fds;
244 int fds_len;
246 int sock;
248 int ifindex;
252 struct dns_socket_dispatcher *pair;
254 struct k_mutex lock;
256 k_timeout_t buf_timeout;
257};
258
268int dns_dispatcher_register(struct dns_socket_dispatcher *ctx);
269
279int dns_dispatcher_unregister(struct dns_socket_dispatcher *ctx);
280
282
291
293 size_t textlen;
295};
296
304
311
312 union {
313 struct {
316
317 /* AF_INET or AF_INET6 address info */
319
322 };
323
324 /* AF_UNSPEC extensions */
325 struct {
327
328 union {
331 };
332 };
333 };
334};
335
375
395typedef void (*dns_resolve_cb_t)(enum dns_resolve_status status,
396 struct dns_addrinfo *info,
397 void *user_data);
398
410typedef void (*dns_resolve_pkt_fw_cb_t)(struct net_buf *dns_data, size_t buf_len, void *user_data);
411
413
414enum dns_resolve_context_state {
415 DNS_RESOLVE_CONTEXT_UNINITIALIZED = 0,
416 DNS_RESOLVE_CONTEXT_ACTIVE,
417 DNS_RESOLVE_CONTEXT_DEACTIVATING,
418 DNS_RESOLVE_CONTEXT_INACTIVE,
419};
420
422
428 struct dns_server {
431
433 int sock;
434
439
442
445
448
451 struct dns_socket_dispatcher dispatcher;
453 } servers[DNS_RESOLVER_MAX_POLL];
454
457 struct zsock_pollfd fds[DNS_RESOLVER_MAX_POLL];
459
461 struct k_mutex lock;
462
467
520
522 enum dns_resolve_context_state state;
523
524#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING) || defined(__DOXYGEN__)
527#endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
528};
529
531
532struct mdns_probe_user_data {
533 struct mdns_responder_context *ctx;
534 char query[DNS_MAX_NAME_SIZE + 1];
535 uint16_t dns_id;
536};
537
538struct mdns_responder_context {
539 struct sockaddr server_addr;
540 struct dns_socket_dispatcher dispatcher;
541 struct zsock_pollfd fds[1];
542 int sock;
543 struct net_if *iface;
544#if defined(CONFIG_MDNS_RESPONDER_PROBE)
545 struct k_work_delayable probe_timer;
546 struct dns_resolve_context probe_ctx;
547 struct mdns_probe_user_data probe_data;
548#endif
549};
550
552
581 const char *dns_servers_str[],
582 const struct sockaddr *dns_servers_sa[]);
583
592
604
626 const char *servers_str[],
627 const struct sockaddr *servers_sa[],
628 enum dns_server_source source);
629
653 const char *servers_str[],
654 const struct sockaddr *servers_sa[],
655 int interfaces[],
656 enum dns_server_source source);
657
666int dns_resolve_remove(struct dns_resolve_context *ctx, int if_index);
667
678int dns_resolve_remove_source(struct dns_resolve_context *ctx, int if_index,
679 enum dns_server_source source);
680
692 uint16_t dns_id);
693
707 uint16_t dns_id,
708 const char *query_name,
709 enum dns_query_type query_type);
710
739 const char *query,
740 enum dns_query_type type,
741 uint16_t *dns_id,
743 void *user_data,
744 int32_t timeout);
745
773static inline int dns_resolve_service(struct dns_resolve_context *ctx,
774 const char *query,
775 uint16_t *dns_id,
777 void *user_data,
778 int32_t timeout)
779{
780 return dns_resolve_name(ctx, query, DNS_QUERY_TYPE_PTR,
781 dns_id, cb, user_data, timeout);
782}
783
795
796#if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING) || defined(__DOXYGEN__)
810{
811 ctx->pkt_fw_cb = cb;
812}
813#endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
814
842static inline int dns_get_addr_info(const char *query,
843 enum dns_query_type type,
844 uint16_t *dns_id,
846 void *user_data,
847 int32_t timeout)
848{
850 query,
851 type,
852 dns_id,
853 cb,
854 user_data,
855 timeout);
856}
857
867static inline int dns_cancel_addr_info(uint16_t dns_id)
868{
870}
871
875
877
885const char *dns_get_source_str(enum dns_server_source source);
886
890#if defined(CONFIG_DNS_RESOLVER_AUTO_INIT)
891void dns_init_resolver(void);
892
893#else
894#define dns_init_resolver(...)
895#endif /* CONFIG_DNS_RESOLVER_AUTO_INIT */
896
898
899#ifdef __cplusplus
900}
901#endif
902
903#endif /* ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_ */
int dns_resolve_reconfigure_with_interfaces(struct dns_resolve_context *ctx, const char *servers_str[], const struct 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...
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 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:808
static int dns_cancel_addr_info(uint16_t dns_id)
Cancel a pending DNS query.
Definition dns_resolve.h:867
int dns_resolve_remove(struct dns_resolve_context *ctx, int if_index)
Remove servers from the DNS resolving context.
int dns_resolve_reconfigure(struct dns_resolve_context *ctx, const char *servers_str[], const struct sockaddr *servers_sa[], enum dns_server_source source)
Reconfigure DNS resolving context.
dns_resolve_status
Status values for the callback.
Definition dns_resolve.h:339
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_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_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:410
#define DNS_MAX_TEXT_SIZE
Max size of the resolved txt record.
Definition dns_resolve.h:83
dns_resolve_extension
Enumerate the extensions that are available in the address info.
Definition dns_resolve.h:286
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:76
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:57
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:773
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:842
void(* dns_resolve_cb_t)(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data)
DNS resolve callback.
Definition dns_resolve.h:395
@ DNS_EAI_MEMORY
Memory allocation failure.
Definition dns_resolve.h:359
@ DNS_EAI_NOTCANCELED
Request not canceled.
Definition dns_resolve.h:369
@ DNS_EAI_IDN_ENCODE
IDN encoding failed.
Definition dns_resolve.h:373
@ DNS_EAI_ADDRFAMILY
Address family for NAME not supported.
Definition dns_resolve.h:357
@ DNS_EAI_INPROGRESS
Processing request in progress.
Definition dns_resolve.h:365
@ DNS_EAI_FAIL
Non-recoverable failure in name res.
Definition dns_resolve.h:347
@ DNS_EAI_AGAIN
Temporary failure in name resolution.
Definition dns_resolve.h:345
@ DNS_EAI_NODATA
No address associated with NAME.
Definition dns_resolve.h:349
@ DNS_EAI_NONAME
NAME or SERVICE is unknown.
Definition dns_resolve.h:343
@ DNS_EAI_FAMILY
‘ai_family’ not supported
Definition dns_resolve.h:351
@ DNS_EAI_OVERFLOW
Argument buffer overflow.
Definition dns_resolve.h:363
@ DNS_EAI_CANCELED
Request canceled.
Definition dns_resolve.h:367
@ DNS_EAI_BADFLAGS
Invalid value for ‘ai_flags’ field.
Definition dns_resolve.h:341
@ DNS_EAI_SOCKTYPE
‘ai_socktype’ not supported
Definition dns_resolve.h:353
@ DNS_EAI_ALLDONE
All requests done.
Definition dns_resolve.h:371
@ DNS_EAI_SYSTEM
System error returned in ‘errno’.
Definition dns_resolve.h:361
@ DNS_EAI_SERVICE
SRV not supported for ‘ai_socktype’.
Definition dns_resolve.h:355
@ 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_RESOLVE_TXT
Definition dns_resolve.h:288
@ DNS_RESOLVE_NONE
Definition dns_resolve.h:287
@ DNS_RESOLVE_SRV
Definition dns_resolve.h:289
@ DNS_SOURCE_MANUAL
Server information is added manually, for example by an application.
Definition dns_resolve.h:61
@ DNS_SOURCE_IPV6_RA
Server information is from IPv6 SLAAC (router advertisement)
Definition dns_resolve.h:67
@ DNS_SOURCE_DHCPV4
Server information is from DHCPv4 server.
Definition dns_resolve.h:63
@ DNS_SOURCE_UNKNOWN
Source is unknown.
Definition dns_resolve.h:59
@ DNS_SOURCE_PPP
Server information is from PPP.
Definition dns_resolve.h:69
@ DNS_SOURCE_DHCPV6
Server information is from DHCPv6 server.
Definition dns_resolve.h:65
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
#define STRINGIFY(s)
Definition common.h:165
#define BUILD_ASSERT(EXPR, MSG...)
Definition llvm.h:51
Public kernel APIs.
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:308
char ai_canonname[20+1]
AF_LOCAL Canonical name of the address.
Definition dns_resolve.h:321
struct sockaddr ai_addr
Definition dns_resolve.h:318
struct dns_resolve_srv ai_srv
Definition dns_resolve.h:330
struct dns_resolve_txt ai_txt
Definition dns_resolve.h:329
enum dns_resolve_extension ai_extension
Definition dns_resolve.h:326
socklen_t ai_addrlen
Length of the ai_addr field or ai_canonname.
Definition dns_resolve.h:315
uint8_t ai_family
Address family of the address information and discriminator.
Definition dns_resolve.h:310
Result callbacks.
Definition dns_resolve.h:474
const char * query
String containing the thing to resolve like www.example.com.
Definition dns_resolve.h:504
uint16_t query_hash
Hash of the DNS name + query type we are querying.
Definition dns_resolve.h:518
struct dns_resolve_context * ctx
Back pointer to ctx, needed in timeout handler.
Definition dns_resolve.h:479
void * user_data
User data.
Definition dns_resolve.h:488
struct k_work_delayable timer
Timeout timer.
Definition dns_resolve.h:476
uint16_t id
DNS id of this query.
Definition dns_resolve.h:510
k_timeout_t timeout
TX timeout.
Definition dns_resolve.h:491
dns_resolve_cb_t cb
Result callback.
Definition dns_resolve.h:485
enum dns_query_type query_type
Query type.
Definition dns_resolve.h:507
struct sockaddr dns_server
DNS server information.
Definition dns_resolve.h:430
enum dns_server_source source
Source of the DNS server, e.g., manual, DHCPv4/6, etc.
Definition dns_resolve.h:441
int if_index
Network interface index if the DNS resolving should be done via this interface.
Definition dns_resolve.h:438
int sock
Connection to the DNS server.
Definition dns_resolve.h:433
uint8_t is_mdns
Is this server mDNS one.
Definition dns_resolve.h:444
uint8_t is_llmnr
Is this server LLMNR one.
Definition dns_resolve.h:447
DNS resolve context structure.
Definition dns_resolve.h:426
dns_resolve_pkt_fw_cb_t pkt_fw_cb
DNS packet forwarding callback.
Definition dns_resolve.h:526
k_timeout_t buf_timeout
This timeout is also used when a buffer is required from the buffer pools.
Definition dns_resolve.h:466
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:522
struct k_mutex lock
Prevent concurrent access.
Definition dns_resolve.h:461
Definition dns_resolve.h:297
uint16_t priority
Definition dns_resolve.h:298
char target[20+1]
Definition dns_resolve.h:302
size_t targetlen
Definition dns_resolve.h:301
uint16_t weight
Definition dns_resolve.h:299
uint16_t port
Definition dns_resolve.h:300
Definition dns_resolve.h:292
size_t textlen
Definition dns_resolve.h:293
char text[64+1]
Definition dns_resolve.h:294
Mutex Structure.
Definition kernel.h:3194
Kernel timeout type.
Definition clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4322
Network buffer representation.
Definition net_buf.h:1006
uint8_t user_data[]
System metadata for this buffer.
Definition net_buf.h:1053
Generic sockaddr struct.
Definition net_ip.h:410
Definition of the monitored socket/file descriptor.
Definition socket_poll.h:31