Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
dns_resolve.h
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2017 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
14#define ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_
15
16#include <zephyr/kernel.h>
17#include <zephyr/net/net_ip.h>
18#include <zephyr/net/net_if.h>
20#include <zephyr/net/net_core.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
44
46#ifndef DNS_MAX_NAME_SIZE
47#define DNS_MAX_NAME_SIZE 20
48#endif
49
52#define DNS_BUF_TIMEOUT K_MSEC(500) /* ms */
53
54/* This value is recommended by RFC 1035 */
55#define DNS_RESOLVER_MAX_BUF_SIZE 512
56
57/* Make sure that we can compile things even if CONFIG_DNS_RESOLVER
58 * is not enabled.
59 */
60#if defined(CONFIG_DNS_RESOLVER_MAX_SERVERS)
61#define DNS_RESOLVER_MAX_SERVERS CONFIG_DNS_RESOLVER_MAX_SERVERS
62#else
63#define DNS_RESOLVER_MAX_SERVERS 0
64#endif
65
66#if defined(CONFIG_DNS_NUM_CONCUR_QUERIES)
67#define DNS_NUM_CONCUR_QUERIES CONFIG_DNS_NUM_CONCUR_QUERIES
68#else
69#define DNS_NUM_CONCUR_QUERIES 1
70#endif
71
72#if defined(CONFIG_NET_IF_MAX_IPV6_COUNT)
73#define MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
74#else
75#define MAX_IPV6_IFACE_COUNT 1
76#endif
77
78#if defined(CONFIG_NET_IF_MAX_IPV4_COUNT)
79#define MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
80#else
81#define MAX_IPV4_IFACE_COUNT 1
82#endif
83
84/* If mDNS is enabled, then add some extra well known multicast servers to the
85 * server list.
86 */
87#if defined(CONFIG_MDNS_RESOLVER)
88#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
89#define MDNS_SERVER_COUNT 2
90#else
91#define MDNS_SERVER_COUNT 1
92#endif /* CONFIG_NET_IPV6 && CONFIG_NET_IPV4 */
93#else
94#define MDNS_SERVER_COUNT 0
95#endif /* CONFIG_MDNS_RESOLVER */
96
97/* If LLMNR is enabled, then add some extra well known multicast servers to the
98 * server list.
99 */
100#if defined(CONFIG_LLMNR_RESOLVER)
101#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
102#define LLMNR_SERVER_COUNT 2
103#else
104#define LLMNR_SERVER_COUNT 1
105#endif /* CONFIG_NET_IPV6 && CONFIG_NET_IPV4 */
106#else
107#define LLMNR_SERVER_COUNT 0
108#endif /* CONFIG_MDNS_RESOLVER */
109
110#define DNS_MAX_MCAST_SERVERS (MDNS_SERVER_COUNT + LLMNR_SERVER_COUNT)
111
112#if defined(CONFIG_MDNS_RESPONDER)
113#if defined(CONFIG_NET_IPV6)
114#define MDNS_MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
115#else
116#define MDNS_MAX_IPV6_IFACE_COUNT 0
117#endif /* CONFIG_NET_IPV6 */
118
119#if defined(CONFIG_NET_IPV4)
120#define MDNS_MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
121#else
122#define MDNS_MAX_IPV4_IFACE_COUNT 0
123#endif /* CONFIG_NET_IPV4 */
124
125#define MDNS_MAX_POLL (MDNS_MAX_IPV4_IFACE_COUNT + MDNS_MAX_IPV6_IFACE_COUNT)
126#else
127#define MDNS_MAX_POLL 0
128#endif /* CONFIG_MDNS_RESPONDER */
129
130#if defined(CONFIG_LLMNR_RESPONDER)
131#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
132#define LLMNR_MAX_POLL 2
133#else
134#define LLMNR_MAX_POLL 1
135#endif
136#else
137#define LLMNR_MAX_POLL 0
138#endif /* CONFIG_LLMNR_RESPONDER */
139
140#define DNS_RESOLVER_MAX_POLL (DNS_RESOLVER_MAX_SERVERS + DNS_MAX_MCAST_SERVERS)
141
143#define DNS_DISPATCHER_MAX_POLL (DNS_RESOLVER_MAX_POLL + MDNS_MAX_POLL + LLMNR_MAX_POLL)
144
145#if defined(CONFIG_ZVFS_POLL_MAX)
146BUILD_ASSERT(CONFIG_ZVFS_POLL_MAX >= DNS_DISPATCHER_MAX_POLL,
147 "CONFIG_ZVFS_POLL_MAX must be larger than " STRINGIFY(DNS_DISPATCHER_MAX_POLL));
148#endif
149
153enum dns_socket_type {
154 DNS_SOCKET_RESOLVER = 1,
155 DNS_SOCKET_RESPONDER = 2
156};
157
159struct mdns_responder_context;
160struct dns_socket_dispatcher;
161
176typedef int (*dns_socket_dispatcher_cb)(struct dns_socket_dispatcher *ctx, int sock,
177 struct sockaddr *addr, size_t addrlen,
178 struct net_buf *buf, size_t data_len);
179
181struct dns_socket_dispatcher {
183 sys_snode_t node;
185 const struct net_socket_service_desc *svc;
189 union {
190 void *ctx;
191 struct dns_resolve_context *resolve_ctx;
192 struct mdns_responder_context *mdns_ctx;
193 };
194
196 enum dns_socket_type type;
198 struct sockaddr local_addr;
200 dns_socket_dispatcher_cb cb;
202 struct zsock_pollfd *fds;
204 int fds_len;
206 int sock;
208 int ifindex;
212 struct dns_socket_dispatcher *pair;
214 struct k_mutex lock;
216 k_timeout_t buf_timeout;
217};
218
228int dns_dispatcher_register(struct dns_socket_dispatcher *ctx);
229
239int dns_dispatcher_unregister(struct dns_socket_dispatcher *ctx);
240
256
296
316typedef void (*dns_resolve_cb_t)(enum dns_resolve_status status,
317 struct dns_addrinfo *info,
318 void *user_data);
319
322enum dns_resolve_context_state {
323 DNS_RESOLVE_CONTEXT_ACTIVE,
324 DNS_RESOLVE_CONTEXT_DEACTIVATING,
325 DNS_RESOLVE_CONTEXT_INACTIVE,
326};
327
335 struct dns_server {
338
340 int sock;
341
346
349
352
355 struct dns_socket_dispatcher dispatcher;
357 } servers[DNS_RESOLVER_MAX_POLL];
358
361 struct zsock_pollfd fds[DNS_RESOLVER_MAX_POLL];
365 struct k_mutex lock;
366
371
424
426 enum dns_resolve_context_state state;
427};
428
431struct mdns_probe_user_data {
432 struct mdns_responder_context *ctx;
433 char query[DNS_MAX_NAME_SIZE + 1];
434 uint16_t dns_id;
435};
436
437struct mdns_responder_context {
438 struct sockaddr server_addr;
439 struct dns_socket_dispatcher dispatcher;
440 struct zsock_pollfd fds[1];
441 int sock;
442 struct net_if *iface;
443#if defined(CONFIG_MDNS_RESPONDER_PROBE)
444 struct k_work_delayable probe_timer;
445 struct dns_resolve_context probe_ctx;
446 struct mdns_probe_user_data probe_data;
447#endif
448};
449
480 const char *dns_servers_str[],
481 const struct sockaddr *dns_servers_sa[]);
482
491
503
524 const char *servers_str[],
525 const struct sockaddr *servers_sa[]);
526
538 uint16_t dns_id);
539
553 uint16_t dns_id,
554 const char *query_name,
555 enum dns_query_type query_type);
556
585 const char *query,
586 enum dns_query_type type,
587 uint16_t *dns_id,
589 void *user_data,
590 int32_t timeout);
591
603
631static inline int dns_get_addr_info(const char *query,
632 enum dns_query_type type,
633 uint16_t *dns_id,
635 void *user_data,
636 int32_t timeout)
637{
639 query,
640 type,
641 dns_id,
642 cb,
643 user_data,
644 timeout);
645}
646
656static inline int dns_cancel_addr_info(uint16_t dns_id)
657{
659}
660
670#if defined(CONFIG_DNS_RESOLVER_AUTO_INIT)
671void dns_init_resolver(void);
672
673#else
674#define dns_init_resolver(...)
675#endif /* CONFIG_DNS_RESOLVER_AUTO_INIT */
676
679#ifdef __cplusplus
680}
681#endif
682
683#endif /* ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_ */
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:656
dns_resolve_status
Status values for the callback.
Definition dns_resolve.h:260
dns_query_type
DNS query type enum.
Definition dns_resolve.h:38
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.
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:47
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:631
void(* dns_resolve_cb_t)(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data)
DNS resolve callback.
Definition dns_resolve.h:316
@ DNS_EAI_MEMORY
Memory allocation failure.
Definition dns_resolve.h:280
@ DNS_EAI_NOTCANCELED
Request not canceled.
Definition dns_resolve.h:290
@ DNS_EAI_IDN_ENCODE
IDN encoding failed.
Definition dns_resolve.h:294
@ DNS_EAI_ADDRFAMILY
Address family for NAME not supported.
Definition dns_resolve.h:278
@ DNS_EAI_INPROGRESS
Processing request in progress.
Definition dns_resolve.h:286
@ DNS_EAI_FAIL
Non-recoverable failure in name res.
Definition dns_resolve.h:268
@ DNS_EAI_AGAIN
Temporary failure in name resolution.
Definition dns_resolve.h:266
@ DNS_EAI_NODATA
No address associated with NAME.
Definition dns_resolve.h:270
@ DNS_EAI_NONAME
NAME or SERVICE is unknown.
Definition dns_resolve.h:264
@ DNS_EAI_FAMILY
‘ai_family’ not supported
Definition dns_resolve.h:272
@ DNS_EAI_OVERFLOW
Argument buffer overflow.
Definition dns_resolve.h:284
@ DNS_EAI_CANCELED
Request canceled.
Definition dns_resolve.h:288
@ DNS_EAI_BADFLAGS
Invalid value for ‘ai_flags’ field.
Definition dns_resolve.h:262
@ DNS_EAI_SOCKTYPE
‘ai_socktype’ not supported
Definition dns_resolve.h:274
@ DNS_EAI_ALLDONE
All requests done.
Definition dns_resolve.h:292
@ DNS_EAI_SYSTEM
System error returned in ‘errno’.
Definition dns_resolve.h:282
@ DNS_EAI_SERVICE
SRV not supported for ‘ai_socktype’.
Definition dns_resolve.h:276
@ DNS_QUERY_TYPE_A
IPv4 query.
Definition dns_resolve.h:40
@ DNS_QUERY_TYPE_AAAA
IPv6 query.
Definition dns_resolve.h:42
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:134
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:246
char ai_canonname[20+1]
Canonical name of the address.
Definition dns_resolve.h:254
struct sockaddr ai_addr
IP address information.
Definition dns_resolve.h:248
socklen_t ai_addrlen
Length of the ai_addr field.
Definition dns_resolve.h:250
uint8_t ai_family
Address family of the address information.
Definition dns_resolve.h:252
Result callbacks.
Definition dns_resolve.h:378
const char * query
String containing the thing to resolve like www.example.com.
Definition dns_resolve.h:408
uint16_t query_hash
Hash of the DNS name + query type we are querying.
Definition dns_resolve.h:422
struct dns_resolve_context * ctx
Back pointer to ctx, needed in timeout handler.
Definition dns_resolve.h:383
void * user_data
User data.
Definition dns_resolve.h:392
struct k_work_delayable timer
Timeout timer.
Definition dns_resolve.h:380
uint16_t id
DNS id of this query.
Definition dns_resolve.h:414
k_timeout_t timeout
TX timeout.
Definition dns_resolve.h:395
dns_resolve_cb_t cb
Result callback.
Definition dns_resolve.h:389
enum dns_query_type query_type
Query type.
Definition dns_resolve.h:411
List of configured DNS servers.
Definition dns_resolve.h:335
int if_index
Network interface index if the DNS resolving should be done via this interface.
Definition dns_resolve.h:345
int sock
Connection to the DNS server.
Definition dns_resolve.h:340
uint8_t is_mdns
Is this server mDNS one.
Definition dns_resolve.h:348
uint8_t is_llmnr
Is this server LLMNR one.
Definition dns_resolve.h:351
DNS resolve context structure.
Definition dns_resolve.h:333
k_timeout_t buf_timeout
This timeout is also used when a buffer is required from the buffer pools.
Definition dns_resolve.h:370
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:426
struct k_mutex lock
Prevent concurrent access.
Definition dns_resolve.h:365
Mutex Structure.
Definition kernel.h:3026
Kernel timeout type.
Definition sys_clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4035
Network buffer representation.
Definition net_buf.h:1006
Network Interface structure.
Definition net_if.h:714
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