1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci
26d4afb5ceSopenharmony_ci#define DNS_MAX			96	/* Maximum host name		*/
27d4afb5ceSopenharmony_ci#define DNS_RECURSION_LIMIT	3
28d4afb5ceSopenharmony_ci#define DNS_PACKET_LEN		1400	/* Buffer size for DNS packet	*/
29d4afb5ceSopenharmony_ci#define MAX_CACHE_ENTRIES	10	/* Dont cache more than that	*/
30d4afb5ceSopenharmony_ci#define DNS_QUERY_TIMEOUT	30	/* Query timeout, seconds	*/
31d4afb5ceSopenharmony_ci
32d4afb5ceSopenharmony_ci/*
33d4afb5ceSopenharmony_ci * ... when we completed a query then the query object is destroyed and a
34d4afb5ceSopenharmony_ci * cache object below is created with the results in getaddrinfo format
35d4afb5ceSopenharmony_ci * appended to the allocation
36d4afb5ceSopenharmony_ci */
37d4afb5ceSopenharmony_ci
38d4afb5ceSopenharmony_citypedef struct lws_adns_cache {
39d4afb5ceSopenharmony_ci	lws_sorted_usec_list_t	sul;	/* for cache TTL management */
40d4afb5ceSopenharmony_ci	lws_dll2_t		list;
41d4afb5ceSopenharmony_ci
42d4afb5ceSopenharmony_ci	struct lws_adns_cache	*firstcache;
43d4afb5ceSopenharmony_ci	struct lws_adns_cache	*chain;
44d4afb5ceSopenharmony_ci	struct addrinfo		*results;
45d4afb5ceSopenharmony_ci	const char		*name;
46d4afb5ceSopenharmony_ci	uint8_t			flags;	/* b0 = has ipv4, b1 = has ipv6 */
47d4afb5ceSopenharmony_ci	char			refcount;
48d4afb5ceSopenharmony_ci	char			incomplete;
49d4afb5ceSopenharmony_ci	/* addrinfo, lws_sa46, then name overallocated here */
50d4afb5ceSopenharmony_ci} lws_adns_cache_t;
51d4afb5ceSopenharmony_ci
52d4afb5ceSopenharmony_ci/*
53d4afb5ceSopenharmony_ci * these objects are used while a query is ongoing...
54d4afb5ceSopenharmony_ci */
55d4afb5ceSopenharmony_ci
56d4afb5ceSopenharmony_citypedef struct {
57d4afb5ceSopenharmony_ci	lws_sorted_usec_list_t	sul;	/* per-query write retry timer */
58d4afb5ceSopenharmony_ci	lws_sorted_usec_list_t	write_sul;	/* fail if unable to write by this time */
59d4afb5ceSopenharmony_ci	lws_dll2_t		list;
60d4afb5ceSopenharmony_ci
61d4afb5ceSopenharmony_ci	lws_metrics_caliper_compose(metcal)
62d4afb5ceSopenharmony_ci
63d4afb5ceSopenharmony_ci	lws_dll2_owner_t	wsi_adns;
64d4afb5ceSopenharmony_ci	lws_async_dns_cb_t	standalone_cb;	/* if not associated to wsi */
65d4afb5ceSopenharmony_ci	struct lws_context	*context;
66d4afb5ceSopenharmony_ci	void			*opaque;
67d4afb5ceSopenharmony_ci	struct addrinfo		**last;
68d4afb5ceSopenharmony_ci	lws_async_dns_t		*dns;
69d4afb5ceSopenharmony_ci
70d4afb5ceSopenharmony_ci	lws_adns_cache_t	*firstcache;
71d4afb5ceSopenharmony_ci
72d4afb5ceSopenharmony_ci	lws_async_dns_retcode_t	ret;
73d4afb5ceSopenharmony_ci	uint16_t		tid[3]; /* last 3 sent tid */
74d4afb5ceSopenharmony_ci	uint16_t		qtype;
75d4afb5ceSopenharmony_ci	uint16_t		retry;
76d4afb5ceSopenharmony_ci	uint8_t			tsi;
77d4afb5ceSopenharmony_ci
78d4afb5ceSopenharmony_ci#if defined(LWS_WITH_IPV6)
79d4afb5ceSopenharmony_ci	uint8_t			sent[2];
80d4afb5ceSopenharmony_ci#else
81d4afb5ceSopenharmony_ci	uint8_t			sent[1];
82d4afb5ceSopenharmony_ci#endif
83d4afb5ceSopenharmony_ci	uint8_t			asked;
84d4afb5ceSopenharmony_ci	uint8_t			responded;
85d4afb5ceSopenharmony_ci
86d4afb5ceSopenharmony_ci	uint8_t			recursion;
87d4afb5ceSopenharmony_ci	uint8_t			tids;
88d4afb5ceSopenharmony_ci	uint8_t			go_nogo;
89d4afb5ceSopenharmony_ci
90d4afb5ceSopenharmony_ci	uint8_t			is_retry:1;
91d4afb5ceSopenharmony_ci
92d4afb5ceSopenharmony_ci	/* name overallocated here */
93d4afb5ceSopenharmony_ci} lws_adns_q_t;
94d4afb5ceSopenharmony_ci
95d4afb5ceSopenharmony_ci#define LADNS_MOST_RECENT_TID(_q) \
96d4afb5ceSopenharmony_ci		q->tid[(int)(_q->tids - 1) % (int)LWS_ARRAY_SIZE(q->tid)]
97d4afb5ceSopenharmony_ci
98d4afb5ceSopenharmony_cienum {
99d4afb5ceSopenharmony_ci	DHO_TID,
100d4afb5ceSopenharmony_ci	DHO_FLAGS = 2,
101d4afb5ceSopenharmony_ci	DHO_NQUERIES = 4,
102d4afb5ceSopenharmony_ci	DHO_NANSWERS = 6,
103d4afb5ceSopenharmony_ci	DHO_NAUTH = 8,
104d4afb5ceSopenharmony_ci	DHO_NOTHER = 10,
105d4afb5ceSopenharmony_ci
106d4afb5ceSopenharmony_ci	DHO_SIZEOF = 12 /* last */
107d4afb5ceSopenharmony_ci};
108d4afb5ceSopenharmony_ci
109d4afb5ceSopenharmony_civoid
110d4afb5ceSopenharmony_cilws_adns_q_destroy(lws_adns_q_t *q);
111d4afb5ceSopenharmony_ci
112d4afb5ceSopenharmony_civoid
113d4afb5ceSopenharmony_cisul_cb_expire(struct lws_sorted_usec_list *sul);
114d4afb5ceSopenharmony_ci
115d4afb5ceSopenharmony_civoid
116d4afb5ceSopenharmony_cilws_adns_cache_destroy(lws_adns_cache_t *c);
117d4afb5ceSopenharmony_ci
118d4afb5ceSopenharmony_ciint
119d4afb5ceSopenharmony_cilws_async_dns_complete(lws_adns_q_t *q, lws_adns_cache_t *c);
120d4afb5ceSopenharmony_ci
121d4afb5ceSopenharmony_cilws_adns_cache_t *
122d4afb5ceSopenharmony_cilws_adns_get_cache(lws_async_dns_t *dns, const char *name);
123d4afb5ceSopenharmony_ci
124d4afb5ceSopenharmony_civoid
125d4afb5ceSopenharmony_cilws_adns_parse_udp(lws_async_dns_t *dns, const uint8_t *pkt, size_t len);
126d4afb5ceSopenharmony_ci
127d4afb5ceSopenharmony_cilws_adns_q_t *
128d4afb5ceSopenharmony_cilws_adns_get_query(lws_async_dns_t *dns, adns_query_type_t qtype,
129d4afb5ceSopenharmony_ci		   lws_dll2_owner_t *owner, uint16_t tid, const char *name);
130d4afb5ceSopenharmony_ci
131d4afb5ceSopenharmony_civoid
132d4afb5ceSopenharmony_cilws_async_dns_trim_cache(lws_async_dns_t *dns);
133d4afb5ceSopenharmony_ci
134d4afb5ceSopenharmony_ciint
135d4afb5ceSopenharmony_cilws_async_dns_get_new_tid(struct lws_context *context, lws_adns_q_t *q);
136d4afb5ceSopenharmony_ci
137d4afb5ceSopenharmony_ci
138d4afb5ceSopenharmony_ci#if defined(_DEBUG)
139d4afb5ceSopenharmony_civoid
140d4afb5ceSopenharmony_cilws_adns_dump(lws_async_dns_t *dns);
141d4afb5ceSopenharmony_ci#else
142d4afb5ceSopenharmony_ci#define lws_adns_dump(_d)
143d4afb5ceSopenharmony_ci#endif
144