Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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>
19#include <zephyr/net/net_core.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
40};
41
43#ifndef DNS_MAX_NAME_SIZE
44#define DNS_MAX_NAME_SIZE 20
45#endif
46
49#define DNS_BUF_TIMEOUT K_MSEC(500) /* ms */
50
51/* This value is recommended by RFC 1035 */
52#define DNS_RESOLVER_MAX_BUF_SIZE 512
53
54/* Make sure that we can compile things even if CONFIG_DNS_RESOLVER
55 * is not enabled.
56 */
57#if defined(CONFIG_DNS_RESOLVER_MAX_SERVERS)
58#define DNS_RESOLVER_MAX_SERVERS CONFIG_DNS_RESOLVER_MAX_SERVERS
59#else
60#define DNS_RESOLVER_MAX_SERVERS 0
61#endif
62
63#if defined(CONFIG_DNS_NUM_CONCUR_QUERIES)
64#define DNS_NUM_CONCUR_QUERIES CONFIG_DNS_NUM_CONCUR_QUERIES
65#else
66#define DNS_NUM_CONCUR_QUERIES 1
67#endif
68
69#if defined(CONFIG_NET_IF_MAX_IPV6_COUNT)
70#define MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
71#else
72#define MAX_IPV6_IFACE_COUNT 1
73#endif
74
75#if defined(CONFIG_NET_IF_MAX_IPV4_COUNT)
76#define MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
77#else
78#define MAX_IPV4_IFACE_COUNT 1
79#endif
80
81/* If mDNS is enabled, then add some extra well known multicast servers to the
82 * server list.
83 */
84#if defined(CONFIG_MDNS_RESOLVER)
85#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
86#define MDNS_SERVER_COUNT 2
87#else
88#define MDNS_SERVER_COUNT 1
89#endif /* CONFIG_NET_IPV6 && CONFIG_NET_IPV4 */
90#else
91#define MDNS_SERVER_COUNT 0
92#endif /* CONFIG_MDNS_RESOLVER */
93
94/* If LLMNR is enabled, then add some extra well known multicast servers to the
95 * server list.
96 */
97#if defined(CONFIG_LLMNR_RESOLVER)
98#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
99#define LLMNR_SERVER_COUNT 2
100#else
101#define LLMNR_SERVER_COUNT 1
102#endif /* CONFIG_NET_IPV6 && CONFIG_NET_IPV4 */
103#else
104#define LLMNR_SERVER_COUNT 0
105#endif /* CONFIG_MDNS_RESOLVER */
106
107#define DNS_MAX_MCAST_SERVERS (MDNS_SERVER_COUNT + LLMNR_SERVER_COUNT)
108
109#if defined(CONFIG_MDNS_RESPONDER)
110#if defined(CONFIG_NET_IPV6)
111#define MDNS_MAX_IPV6_IFACE_COUNT CONFIG_NET_IF_MAX_IPV6_COUNT
112#else
113#define MDNS_MAX_IPV6_IFACE_COUNT 0
114#endif /* CONFIG_NET_IPV6 */
115
116#if defined(CONFIG_NET_IPV4)
117#define MDNS_MAX_IPV4_IFACE_COUNT CONFIG_NET_IF_MAX_IPV4_COUNT
118#else
119#define MDNS_MAX_IPV4_IFACE_COUNT 0
120#endif /* CONFIG_NET_IPV4 */
121
122#define MDNS_MAX_POLL (MDNS_MAX_IPV4_IFACE_COUNT + MDNS_MAX_IPV6_IFACE_COUNT)
123#else
124#define MDNS_MAX_POLL 0
125#endif /* CONFIG_MDNS_RESPONDER */
126
127#if defined(CONFIG_LLMNR_RESPONDER)
128#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
129#define LLMNR_MAX_POLL 2
130#else
131#define LLMNR_MAX_POLL 1
132#endif
133#else
134#define LLMNR_MAX_POLL 0
135#endif /* CONFIG_LLMNR_RESPONDER */
136
137#define DNS_RESOLVER_MAX_POLL (DNS_RESOLVER_MAX_SERVERS + DNS_MAX_MCAST_SERVERS)
138
140#define DNS_DISPATCHER_MAX_POLL (DNS_RESOLVER_MAX_POLL + MDNS_MAX_POLL + LLMNR_MAX_POLL)
141
142#if defined(CONFIG_NET_SOCKETS_POLL_MAX)
143BUILD_ASSERT(CONFIG_NET_SOCKETS_POLL_MAX >= DNS_DISPATCHER_MAX_POLL,
144 "CONFIG_NET_SOCKETS_POLL_MAX must be larger than " STRINGIFY(DNS_DISPATCHER_MAX_POLL));
145#endif
146
150enum dns_socket_type {
151 DNS_SOCKET_RESOLVER = 1,
152 DNS_SOCKET_RESPONDER = 2
153};
154
156struct mdns_responder_context;
157
172typedef int (*dns_socket_dispatcher_cb)(void *ctx, int sock,
173 struct sockaddr *addr, size_t addrlen,
174 struct net_buf *buf, size_t data_len);
175
177struct dns_socket_dispatcher {
179 sys_snode_t node;
181 const struct net_socket_service_desc *svc;
185 union {
186 void *ctx;
187 struct dns_resolve_context *resolve_ctx;
188 struct mdns_responder_context *mdns_ctx;
189 };
190
192 enum dns_socket_type type;
194 struct sockaddr local_addr;
196 dns_socket_dispatcher_cb cb;
198 struct zsock_pollfd *fds;
200 int fds_len;
202 int sock;
206 struct dns_socket_dispatcher *pair;
208 struct k_mutex lock;
210 k_timeout_t buf_timeout;
211};
212
213struct mdns_responder_context {
214 struct sockaddr server_addr;
215 struct dns_socket_dispatcher dispatcher;
216 struct zsock_pollfd fds[1];
217 int sock;
218};
219
229int dns_dispatcher_register(struct dns_socket_dispatcher *ctx);
230
240int dns_dispatcher_unregister(struct dns_socket_dispatcher *ctx);
241
256};
257
296};
297
317typedef void (*dns_resolve_cb_t)(enum dns_resolve_status status,
318 struct dns_addrinfo *info,
319 void *user_data);
320
323enum dns_resolve_context_state {
324 DNS_RESOLVE_CONTEXT_ACTIVE,
325 DNS_RESOLVE_CONTEXT_DEACTIVATING,
326 DNS_RESOLVE_CONTEXT_INACTIVE,
327};
328
336 struct dns_server {
339
341 int sock;
342
345
348
351 struct dns_socket_dispatcher dispatcher;
353 } servers[DNS_RESOLVER_MAX_POLL];
354
357 struct zsock_pollfd fds[DNS_RESOLVER_MAX_POLL];
361 struct k_mutex lock;
362
367
377
380
386
389
392
404 const char *query;
405
408
411
419 } queries[DNS_NUM_CONCUR_QUERIES];
420
422 enum dns_resolve_context_state state;
423};
424
453 const char *dns_servers_str[],
454 const struct sockaddr *dns_servers_sa[]);
455
464
476
497 const char *servers_str[],
498 const struct sockaddr *servers_sa[]);
499
511 uint16_t dns_id);
512
526 uint16_t dns_id,
527 const char *query_name,
528 enum dns_query_type query_type);
529
558 const char *query,
559 enum dns_query_type type,
560 uint16_t *dns_id,
562 void *user_data,
563 int32_t timeout);
564
576
604static inline int dns_get_addr_info(const char *query,
605 enum dns_query_type type,
606 uint16_t *dns_id,
608 void *user_data,
609 int32_t timeout)
610{
612 query,
613 type,
614 dns_id,
615 cb,
616 user_data,
617 timeout);
618}
619
629static inline int dns_cancel_addr_info(uint16_t dns_id)
630{
632}
633
643#if defined(CONFIG_DNS_RESOLVER_AUTO_INIT)
644void dns_init_resolver(void);
645
646#else
647#define dns_init_resolver(...)
648#endif /* CONFIG_DNS_RESOLVER_AUTO_INIT */
649
652#ifdef __cplusplus
653}
654#endif
655
656#endif /* ZEPHYR_INCLUDE_NET_DNS_RESOLVE_H_ */
#define STRINGIFY(s)
Definition: common.h:134
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:629
dns_resolve_status
Status values for the callback.
Definition: dns_resolve.h:261
dns_query_type
DNS query type enum.
Definition: dns_resolve.h:35
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:317
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:44
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:604
@ DNS_EAI_MEMORY
Memory allocation failure.
Definition: dns_resolve.h:281
@ DNS_EAI_NOTCANCELED
Request not canceled.
Definition: dns_resolve.h:291
@ DNS_EAI_IDN_ENCODE
IDN encoding failed.
Definition: dns_resolve.h:295
@ DNS_EAI_ADDRFAMILY
Address family for NAME not supported.
Definition: dns_resolve.h:279
@ DNS_EAI_INPROGRESS
Processing request in progress.
Definition: dns_resolve.h:287
@ DNS_EAI_FAIL
Non-recoverable failure in name res.
Definition: dns_resolve.h:269
@ DNS_EAI_AGAIN
Temporary failure in name resolution.
Definition: dns_resolve.h:267
@ DNS_EAI_NODATA
No address associated with NAME.
Definition: dns_resolve.h:271
@ DNS_EAI_NONAME
NAME or SERVICE is unknown.
Definition: dns_resolve.h:265
@ DNS_EAI_FAMILY
‘ai_family’ not supported
Definition: dns_resolve.h:273
@ DNS_EAI_OVERFLOW
Argument buffer overflow.
Definition: dns_resolve.h:285
@ DNS_EAI_CANCELED
Request canceled.
Definition: dns_resolve.h:289
@ DNS_EAI_BADFLAGS
Invalid value for ‘ai_flags’ field.
Definition: dns_resolve.h:263
@ DNS_EAI_SOCKTYPE
‘ai_socktype’ not supported
Definition: dns_resolve.h:275
@ DNS_EAI_ALLDONE
All requests done.
Definition: dns_resolve.h:293
@ DNS_EAI_SYSTEM
System error returned in ‘errno’.
Definition: dns_resolve.h:283
@ DNS_EAI_SERVICE
SRV not supported for ‘ai_socktype’.
Definition: dns_resolve.h:277
@ DNS_QUERY_TYPE_A
IPv4 query.
Definition: dns_resolve.h:37
@ DNS_QUERY_TYPE_AAAA
IPv6 query.
Definition: dns_resolve.h:39
size_t socklen_t
Length of a socket address.
Definition: net_ip.h:168
struct _snode sys_snode_t
Single-linked list node structure.
Definition: slist.h:39
Public kernel APIs.
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:247
char ai_canonname[20+1]
Canonical name of the address.
Definition: dns_resolve.h:255
struct sockaddr ai_addr
IP address information.
Definition: dns_resolve.h:249
socklen_t ai_addrlen
Length of the ai_addr field.
Definition: dns_resolve.h:251
uint8_t ai_family
Address family of the address information.
Definition: dns_resolve.h:253
Result callbacks.
Definition: dns_resolve.h:374
const char * query
String containing the thing to resolve like www.example.com.
Definition: dns_resolve.h:404
uint16_t query_hash
Hash of the DNS name + query type we are querying.
Definition: dns_resolve.h:418
struct dns_resolve_context * ctx
Back pointer to ctx, needed in timeout handler.
Definition: dns_resolve.h:379
void * user_data
User data.
Definition: dns_resolve.h:388
struct k_work_delayable timer
Timeout timer.
Definition: dns_resolve.h:376
uint16_t id
DNS id of this query.
Definition: dns_resolve.h:410
k_timeout_t timeout
TX timeout.
Definition: dns_resolve.h:391
dns_resolve_cb_t cb
Result callback.
Definition: dns_resolve.h:385
enum dns_query_type query_type
Query type.
Definition: dns_resolve.h:407
List of configured DNS servers.
Definition: dns_resolve.h:336
int sock
Connection to the DNS server.
Definition: dns_resolve.h:341
uint8_t is_mdns
Is this server mDNS one.
Definition: dns_resolve.h:344
uint8_t is_llmnr
Is this server LLMNR one.
Definition: dns_resolve.h:347
DNS resolve context structure.
Definition: dns_resolve.h:334
k_timeout_t buf_timeout
This timeout is also used when a buffer is required from the buffer pools.
Definition: dns_resolve.h:366
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:422
struct k_mutex lock
Prevent concurrent access.
Definition: dns_resolve.h:361
Mutex Structure.
Definition: kernel.h:2914
Kernel timeout type.
Definition: sys_clock.h:65
A structure used to submit work after a delay.
Definition: kernel.h:3903
Network buffer representation.
Definition: buf.h:1004
Main structure holding socket service configuration information.
Definition: socket_service.h:60
Generic sockaddr struct.
Definition: net_ip.h:385
Definition of the monitored socket/file descriptor.
Definition: socket_poll.h:28