libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-async-dns.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#if defined(LWS_WITH_UDP) && defined(LWS_WITH_NETWORK)
26
27typedef enum dns_query_type {
28 LWS_ADNS_RECORD_A = 0x01,
29 LWS_ADNS_RECORD_CNAME = 0x05,
30 LWS_ADNS_RECORD_SOA = 0x06,
31 LWS_ADNS_RECORD_MX = 0x0f,
32 LWS_ADNS_RECORD_TXT = 0x10,
33 LWS_ADNS_RECORD_AAAA = 0x1c,
34 LWS_ADNS_RECORD_DS = 0x2b,
35 LWS_ADNS_RECORD_RRSIG = 0x2e,
36 LWS_ADNS_RECORD_NSEC = 0x2f,
37 LWS_ADNS_RECORD_DNSKEY = 0x30,
38 LWS_ADNS_RECORD_NSEC3 = 0x32,
39 LWS_ADNS_RECORD_HTTPS = 0x41,
40
41 /*
42 * Query types are combined by OR with the flag bits defined below
43 * the enum (LWS_ADNS_SYNTHETIC .. LWS_ADNS_IGNORE_HOSTS_FILE; the
44 * largest possible combination is 0x1f0041). A C enum can only
45 * hold values within the range of its listed constants, so this
46 * sentinel extends the enum's range over the whole flag space...
47 * without it, any OR'd combination is undefined behaviour, which
48 * -fsanitize=enum trips on.
49 */
50
51 LWS_ADNS_RECORD_TYPE_AND_FLAGS_MAX = 0x1f0041,
52} adns_query_type_t;
53
54typedef enum {
55 LADNS_RET_FAILED_WSI_CLOSED = -4,
56 LADNS_RET_NXDOMAIN = -3,
57 LADNS_RET_TIMEDOUT = -2,
58 LADNS_RET_FAILED = -1,
59 LADNS_RET_FOUND,
60 LADNS_RET_CONTINUING
61} lws_async_dns_retcode_t;
62
63/*
64 * The values are those lws_system_ops_t .async_dns_dnssec_mode documents.
65 * Before 2026-10 REQUIRE and TOLERATE were the other way around (1 was
66 * TOLERATE), so binaries built against older headers that use the enum names
67 * now get the other mode: rebuild them.
68 */
69typedef enum {
70 LWS_ADNS_DNSSEC_OFF = 0,
72 LWS_ADNS_DNSSEC_REQUIRE = 1,
76 LWS_ADNS_DNSSEC_TOLERATE = 2,
79} lws_async_dns_dnssec_mode_t;
80
81#define LWS_ADNS_DNSSEC_VALID (1 << 8)
82#define LWS_ADNS_DNSSEC_INVALID (1 << 9)
83
84#define LWS_ADNS_SYNTHETIC 0x10000 /* don't send, synthetic response will
85 * be injected for testing */
86#define LWS_ADNS_INDICATE_LACKS_DNSSEC 0x20000 /* tolerate missing DNSSEC on this specific lookup */
87#define LWS_ADNS_NOCACHE 0x40000 /* force network query, bypass cache */
88#define LWS_ADNS_WANT_DNSSEC 0x80000 /* Explicitly set DO bit in EDNS0 OPT record */
89#define LWS_ADNS_IGNORE_HOSTS_FILE 0x100000 /* Bypass checking /etc/hosts and force network DNS lookup */
90#define LADNS_NO_WSI_BUT_OK ((struct lws *)(intptr_t)0x1)
91
92struct addrinfo;
93
94/*
95 * The completion callback: ads is the name the lookup was asked for, whatever
96 * CNAME or search list domain it was resolved through; result is the addrinfo
97 * chain to release with lws_async_dns_freeaddrinfo(), or NULL; n is the
98 * lws_async_dns_retcode_t, or'd with LWS_ADNS_DNSSEC_VALID if validated
99 */
100typedef struct lws * (*lws_async_dns_cb_t)(struct lws *wsi, const char *ads,
101 const struct addrinfo *result, int n, void *opaque);
102
103struct lws_adns_q;
104struct lws_async_dns;
105struct lws_async_dns_server;
106
134LWS_VISIBLE LWS_EXTERN lws_async_dns_retcode_t
135lws_async_dns_query(struct lws_context *context, int tsi, const char *name,
136 adns_query_type_t qtype, lws_async_dns_cb_t cb,
137 struct lws *wsi, void *opaque, struct lws_adns_q **pq);
138
162lws_async_dns_cancel_by_opaque(struct lws_context *context, void *opaque);
163
175lws_async_dns_freeaddrinfo(const struct addrinfo **ai);
176
192lws_async_dns_get_rr_cache(struct lws_context *context, const char *name,
193 adns_query_type_t qtype, uint16_t *paylen);
194
206lws_async_dns_get_alpn(struct lws_context *context, const char *name, const char *alpn);
207
216typedef struct lws_adns_direct_rr {
217 const uint8_t *rdata;
218 uint32_t ttl;
219 uint16_t type;
220 uint16_t len;
221} lws_adns_direct_rr_t;
222
223#define LWS_ADNS_DIRECT_MAX_RRS 16
224
239typedef struct lws_adns_direct_result {
240 lws_async_dns_retcode_t ret;
241 uint8_t rcode;
242 uint8_t authoritative;
243 const lws_adns_direct_rr_t *rrs;
244 int count;
245} lws_adns_direct_result_t;
246
247typedef void (*lws_async_dns_direct_cb_t)(void *opaque,
248 const lws_adns_direct_result_t *r);
249
250struct lws_adns_direct;
251
274LWS_VISIBLE LWS_EXTERN struct lws_adns_direct *
275lws_async_dns_query_direct(struct lws_context *context,
276 const lws_sockaddr46 *server, const char *name,
277 adns_query_type_t qtype,
278 lws_async_dns_direct_cb_t cb, void *opaque);
279
291lws_async_dns_query_direct_cancel(struct lws_adns_direct **pd);
292
304lws_async_dns_server_add(struct lws_context *cx, const lws_sockaddr46 *sa46);
305
318lws_async_dns_server_remove(struct lws_context *cx, const lws_sockaddr46 *sa46);
319
320/* only needed for testing */
321
322/* the tid of q's A question, or of its only question if not an address */
324lws_adns_get_tid(struct lws_adns_q *q);
325/* the tid of an address lookup's AAAA question */
327lws_adns_get_tid_aaaa(struct lws_adns_q *q);
328LWS_VISIBLE LWS_EXTERN struct lws_async_dns *
329lws_adns_get_async_dns(struct lws_adns_q *q);
330
331LWS_VISIBLE LWS_EXTERN struct lws_async_dns_server *
332lws_adns_get_server(struct lws_adns_q *q);
333
335lws_adns_parse_udp(struct lws_async_dns *dns, const uint8_t *pkt, size_t len,
336 struct lws_async_dns_server *dsrv);
337
350lws_plat_asyncdns_get_server(struct lws_context *context, int n,
351 lws_sockaddr46 *sa46);
352
369lws_plat_asyncdns_get_search(struct lws_context *context, char *buf,
370 size_t len, int *ndots);
371
381lws_async_dns_server_reload(struct lws_context *context);
382
383
396lws_async_dns_dnssec_set_mode(struct lws_context *context, lws_async_dns_dnssec_mode_t mode);
397
398/*
399 * A DS record for the root zone's key signing key, ie, a DNSSEC trust anchor
400 */
401typedef struct lws_adns_ds_anchor {
402 const uint8_t *digest;
403 size_t digest_len;
404 uint16_t key_tag;
405 uint8_t algorithm; /* eg, 8 = RSASHA256 */
406 uint8_t digest_type; /* 2 = SHA-256, 4 = SHA-384 */
407} lws_adns_ds_anchor_t;
408
427lws_async_dns_dnssec_set_root_anchors(struct lws_context *context,
428 const lws_adns_ds_anchor_t *anchors,
429 size_t count);
430
431#endif
unsigned short uint16_t
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
#define LWS_VISIBLE