listen_dnsport.h revision 356345
1/*
2 * services/listen_dnsport.h - listen on port 53 for incoming DNS queries.
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file has functions to get queries from clients.
40 */
41
42#ifndef LISTEN_DNSPORT_H
43#define LISTEN_DNSPORT_H
44
45#include "util/netevent.h"
46struct listen_list;
47struct config_file;
48struct addrinfo;
49struct sldns_buffer;
50struct tcl_list;
51
52/**
53 * Listening for queries structure.
54 * Contains list of query-listen sockets.
55 */
56struct listen_dnsport {
57	/** Base for select calls */
58	struct comm_base* base;
59
60	/** buffer shared by UDP connections, since there is only one
61	    datagram at any time. */
62	struct sldns_buffer* udp_buff;
63#ifdef USE_DNSCRYPT
64	struct sldns_buffer* dnscrypt_udp_buff;
65#endif
66	/** list of comm points used to get incoming events */
67	struct listen_list* cps;
68};
69
70/**
71 * Single linked list to store event points.
72 */
73struct listen_list {
74	/** next in list */
75	struct listen_list* next;
76	/** event info */
77	struct comm_point* com;
78};
79
80/**
81 * type of ports
82 */
83enum listen_type {
84	/** udp type */
85	listen_type_udp,
86	/** tcp type */
87	listen_type_tcp,
88	/** udp ipv6 (v4mapped) for use with ancillary data */
89	listen_type_udpancil,
90	/** ssl over tcp type */
91	listen_type_ssl,
92	/** udp type  + dnscrypt*/
93	listen_type_udp_dnscrypt,
94	/** tcp type + dnscrypt */
95	listen_type_tcp_dnscrypt,
96	/** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/
97	listen_type_udpancil_dnscrypt
98
99};
100
101/**
102 * Single linked list to store shared ports that have been
103 * opened for use by all threads.
104 */
105struct listen_port {
106	/** next in list */
107	struct listen_port* next;
108	/** file descriptor, open and ready for use */
109	int fd;
110	/** type of file descriptor, udp or tcp */
111	enum listen_type ftype;
112};
113
114/**
115 * Create shared listening ports
116 * Getaddrinfo, create socket, bind and listen to zero or more
117 * interfaces for IP4 and/or IP6, for UDP and/or TCP.
118 * On the given port number. It creates the sockets.
119 * @param cfg: settings on what ports to open.
120 * @param reuseport: set to true if you want reuseport, or NULL to not have it,
121 *   set to false on exit if reuseport failed to apply (because of no
122 *   kernel support).
123 * @return: linked list of ports or NULL on error.
124 */
125struct listen_port* listening_ports_open(struct config_file* cfg,
126	int* reuseport);
127
128/**
129 * Close and delete the (list of) listening ports.
130 */
131void listening_ports_free(struct listen_port* list);
132
133/**
134 * Create commpoints with for this thread for the shared ports.
135 * @param base: the comm_base that provides event functionality.
136 *	for default all ifs.
137 * @param ports: the list of shared ports.
138 * @param bufsize: size of datagram buffer.
139 * @param tcp_accept_count: max number of simultaneous TCP connections
140 * 	from clients.
141 * @param tcp_idle_timeout: idle timeout for TCP connections in msec.
142 * @param tcp_conn_limit: TCP connection limit info.
143 * @param sslctx: nonNULL if ssl context.
144 * @param dtenv: nonNULL if dnstap enabled.
145 * @param cb: callback function when a request arrives. It is passed
146 *	  the packet and user argument. Return true to send a reply.
147 * @param cb_arg: user data argument for callback function.
148 * @return: the malloced listening structure, ready for use. NULL on error.
149 */
150struct listen_dnsport* listen_create(struct comm_base* base,
151	struct listen_port* ports, size_t bufsize,
152	int tcp_accept_count, int tcp_idle_timeout,
153	struct tcl_list* tcp_conn_limit, void* sslctx,
154	struct dt_env *dtenv, comm_point_callback_type* cb, void* cb_arg);
155
156/**
157 * delete the listening structure
158 * @param listen: listening structure.
159 */
160void listen_delete(struct listen_dnsport* listen);
161
162/**
163 * delete listen_list of commpoints. Calls commpointdelete() on items.
164 * This may close the fds or not depending on flags.
165 * @param list: to delete.
166 */
167void listen_list_delete(struct listen_list* list);
168
169/**
170 * get memory size used by the listening structs
171 * @param listen: listening structure.
172 * @return: size in bytes.
173 */
174size_t listen_get_mem(struct listen_dnsport* listen);
175
176/**
177 * stop accept handlers for TCP (until enabled again)
178 * @param listen: listening structure.
179 */
180void listen_stop_accept(struct listen_dnsport* listen);
181
182/**
183 * start accept handlers for TCP (was stopped before)
184 * @param listen: listening structure.
185 */
186void listen_start_accept(struct listen_dnsport* listen);
187
188/**
189 * Create and bind nonblocking UDP socket
190 * @param family: for socket call.
191 * @param socktype: for socket call.
192 * @param addr: for bind call.
193 * @param addrlen: for bind call.
194 * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
195 * 	if enabled with value 2 IP6ONLY option is disabled.
196 * @param inuse: on error, this is set true if the port was in use.
197 * @param noproto: on error, this is set true if cause is that the
198	IPv6 proto (family) is not available.
199 * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
200 * @param snd: set size on sndbuf with socket option, if 0 it is not set.
201 * @param listen: if true, this is a listening UDP port, eg port 53, and
202 * 	set SO_REUSEADDR on it.
203 * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
204 * 	listening UDP port.  Set to false on return if it failed to do so.
205 * @param transparent: set IP_TRANSPARENT socket option.
206 * @param freebind: set IP_FREEBIND socket option.
207 * @param use_systemd: if true, fetch sockets from systemd.
208 * @return: the socket. -1 on error.
209 */
210int create_udp_sock(int family, int socktype, struct sockaddr* addr,
211	socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
212	int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd);
213
214/**
215 * Create and bind TCP listening socket
216 * @param addr: address info ready to make socket.
217 * @param v6only: enable ip6 only flag on ip6 sockets.
218 * @param noproto: if error caused by lack of protocol support.
219 * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
220 * 	listening UDP port.  Set to false on return if it failed to do so.
221 * @param transparent: set IP_TRANSPARENT socket option.
222 * @param mss: maximum segment size of the socket. if zero, leaves the default.
223 * @param freebind: set IP_FREEBIND socket option.
224 * @param use_systemd: if true, fetch sockets from systemd.
225 * @return: the socket. -1 on error.
226 */
227int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
228	int* reuseport, int transparent, int mss, int freebind, int use_systemd);
229
230/**
231 * Create and bind local listening socket
232 * @param path: path to the socket.
233 * @param noproto: on error, this is set true if cause is that local sockets
234 *	are not supported.
235 * @param use_systemd: if true, fetch sockets from systemd.
236 * @return: the socket. -1 on error.
237 */
238int create_local_accept_sock(const char* path, int* noproto, int use_systemd);
239
240/**
241 * TCP request info.  List of requests outstanding on the channel, that
242 * are asked for but not yet answered back.
243 */
244struct tcp_req_info {
245	/** the TCP comm point for this.  Its buffer is used for read/write */
246	struct comm_point* cp;
247	/** the buffer to use to spool reply from mesh into,
248	 * it can then be copied to the result list and written.
249	 * it is a pointer to the shared udp buffer. */
250	struct sldns_buffer* spool_buffer;
251	/** are we in worker_handle function call (for recursion callback)*/
252	int in_worker_handle;
253	/** is the comm point dropped (by worker handle).
254	 * That means we have to disconnect the channel. */
255	int is_drop;
256	/** is the comm point set to send_reply (by mesh new client in worker
257	 * handle), if so answer is available in c.buffer */
258	int is_reply;
259	/** read channel has closed, just write pending results */
260	int read_is_closed;
261	/** read again */
262	int read_again;
263	/** number of outstanding requests */
264	int num_open_req;
265	/** list of outstanding requests */
266	struct tcp_req_open_item* open_req_list;
267	/** number of pending writeable results */
268	int num_done_req;
269	/** list of pending writable result packets, malloced one at a time */
270	struct tcp_req_done_item* done_req_list;
271};
272
273/**
274 * List of open items in TCP channel
275 */
276struct tcp_req_open_item {
277	/** next in list */
278	struct tcp_req_open_item* next;
279	/** the mesh area of the mesh_state */
280	struct mesh_area* mesh;
281	/** the mesh state */
282	struct mesh_state* mesh_state;
283};
284
285/**
286 * List of done items in TCP channel
287 */
288struct tcp_req_done_item {
289	/** next in list */
290	struct tcp_req_done_item* next;
291	/** the buffer with packet contents */
292	uint8_t* buf;
293	/** length of the buffer */
294	size_t len;
295};
296
297/**
298 * Create tcp request info structure that keeps track of open
299 * requests on the TCP channel that are resolved at the same time,
300 * and the pending results that have to get written back to that client.
301 * @param spoolbuf: shared buffer
302 * @return new structure or NULL on alloc failure.
303 */
304struct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf);
305
306/**
307 * Delete tcp request structure.  Called by owning commpoint.
308 * Removes mesh entry references and stored results from the lists.
309 * @param req: the tcp request info
310 */
311void tcp_req_info_delete(struct tcp_req_info* req);
312
313/**
314 * Clear tcp request structure.  Removes list entries, sets it up ready
315 * for the next connection.
316 * @param req: tcp request info structure.
317 */
318void tcp_req_info_clear(struct tcp_req_info* req);
319
320/**
321 * Remove mesh state entry from list in tcp_req_info.
322 * caller has to manage the mesh state reply entry in the mesh state.
323 * @param req: the tcp req info that has the entry removed from the list.
324 * @param m: the state removed from the list.
325 */
326void tcp_req_info_remove_mesh_state(struct tcp_req_info* req,
327	struct mesh_state* m);
328
329/**
330 * Handle write done of the last result packet
331 * @param req: the tcp req info.
332 */
333void tcp_req_info_handle_writedone(struct tcp_req_info* req);
334
335/**
336 * Handle read done of a new request from the client
337 * @param req: the tcp req info.
338 */
339void tcp_req_info_handle_readdone(struct tcp_req_info* req);
340
341/**
342 * Add mesh state to the tcp req list of open requests.
343 * So the comm_reply can be removed off the mesh reply list when
344 * the tcp channel has to be closed (for other reasons then that that
345 * request was done, eg. channel closed by client or some format error).
346 * @param req: tcp req info structure.  It keeps track of the simultaneous
347 * 	requests and results on a tcp (or TLS) channel.
348 * @param mesh: mesh area for the state.
349 * @param m: mesh state to add.
350 * @return 0 on failure (malloc failure).
351 */
352int tcp_req_info_add_meshstate(struct tcp_req_info* req,
353	struct mesh_area* mesh, struct mesh_state* m);
354
355/**
356 * Send reply on tcp simultaneous answer channel.  May queue it up.
357 * @param req: request info structure.
358 */
359void tcp_req_info_send_reply(struct tcp_req_info* req);
360
361/** the read channel has closed
362 * @param req: request. remaining queries are looked up and answered.
363 * @return zero if nothing to do, just close the tcp.
364 */
365int tcp_req_info_handle_read_close(struct tcp_req_info* req);
366
367/** get the size of currently used tcp stream wait buffers (in bytes) */
368size_t tcp_req_info_get_stream_buffer_size(void);
369
370#endif /* LISTEN_DNSPORT_H */
371