]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/services/listen_dnsport.h
MFV r337212:
[FreeBSD/FreeBSD.git] / contrib / unbound / services / listen_dnsport.h
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"
46 struct listen_list;
47 struct config_file;
48 struct addrinfo;
49 struct sldns_buffer;
50
51 /**
52  * Listening for queries structure.
53  * Contains list of query-listen sockets.
54  */
55 struct listen_dnsport {
56         /** Base for select calls */
57         struct comm_base* base;
58
59         /** buffer shared by UDP connections, since there is only one
60             datagram at any time. */
61         struct sldns_buffer* udp_buff;
62 #ifdef USE_DNSCRYPT
63         struct sldns_buffer* dnscrypt_udp_buff;
64 #endif
65         /** list of comm points used to get incoming events */
66         struct listen_list* cps;
67 };
68
69 /**
70  * Single linked list to store event points.
71  */
72 struct listen_list {
73         /** next in list */
74         struct listen_list* next;
75         /** event info */
76         struct comm_point* com;
77 };
78
79 /**
80  * type of ports
81  */
82 enum listen_type {
83         /** udp type */
84         listen_type_udp,
85         /** tcp type */
86         listen_type_tcp,
87         /** udp ipv6 (v4mapped) for use with ancillary data */
88         listen_type_udpancil,
89         /** ssl over tcp type */
90         listen_type_ssl,
91         /** udp type  + dnscrypt*/
92         listen_type_udp_dnscrypt,
93         /** tcp type + dnscrypt */
94         listen_type_tcp_dnscrypt,
95         /** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/
96         listen_type_udpancil_dnscrypt
97
98 };
99
100 /**
101  * Single linked list to store shared ports that have been 
102  * opened for use by all threads.
103  */
104 struct listen_port {
105         /** next in list */
106         struct listen_port* next;
107         /** file descriptor, open and ready for use */
108         int fd;
109         /** type of file descriptor, udp or tcp */
110         enum listen_type ftype;
111 };
112
113 /**
114  * Create shared listening ports
115  * Getaddrinfo, create socket, bind and listen to zero or more 
116  * interfaces for IP4 and/or IP6, for UDP and/or TCP.
117  * On the given port number. It creates the sockets.
118  * @param cfg: settings on what ports to open.
119  * @param reuseport: set to true if you want reuseport, or NULL to not have it,
120  *   set to false on exit if reuseport failed to apply (because of no
121  *   kernel support).
122  * @return: linked list of ports or NULL on error.
123  */
124 struct listen_port* listening_ports_open(struct config_file* cfg,
125         int* reuseport);
126
127 /**
128  * Close and delete the (list of) listening ports.
129  */
130 void listening_ports_free(struct listen_port* list);
131
132 /**
133  * Create commpoints with for this thread for the shared ports.
134  * @param base: the comm_base that provides event functionality.
135  *      for default all ifs.
136  * @param ports: the list of shared ports.
137  * @param bufsize: size of datagram buffer.
138  * @param tcp_accept_count: max number of simultaneous TCP connections 
139  *      from clients.
140  * @param sslctx: nonNULL if ssl context.
141  * @param dtenv: nonNULL if dnstap enabled.
142  * @param cb: callback function when a request arrives. It is passed
143  *        the packet and user argument. Return true to send a reply.
144  * @param cb_arg: user data argument for callback function.
145  * @return: the malloced listening structure, ready for use. NULL on error.
146  */
147 struct listen_dnsport* listen_create(struct comm_base* base,
148         struct listen_port* ports, size_t bufsize, int tcp_accept_count,
149         void* sslctx, struct dt_env *dtenv, comm_point_callback_type* cb,
150         void* cb_arg);
151
152 /**
153  * delete the listening structure
154  * @param listen: listening structure.
155  */
156 void listen_delete(struct listen_dnsport* listen);
157
158 /**
159  * delete listen_list of commpoints. Calls commpointdelete() on items.
160  * This may close the fds or not depending on flags.
161  * @param list: to delete.
162  */
163 void listen_list_delete(struct listen_list* list);
164
165 /**
166  * get memory size used by the listening structs
167  * @param listen: listening structure.
168  * @return: size in bytes.
169  */
170 size_t listen_get_mem(struct listen_dnsport* listen);
171
172 /**
173  * stop accept handlers for TCP (until enabled again)
174  * @param listen: listening structure.
175  */
176 void listen_stop_accept(struct listen_dnsport* listen);
177
178 /**
179  * start accept handlers for TCP (was stopped before)
180  * @param listen: listening structure.
181  */
182 void listen_start_accept(struct listen_dnsport* listen);
183
184 /**
185  * Create and bind nonblocking UDP socket
186  * @param family: for socket call.
187  * @param socktype: for socket call.
188  * @param addr: for bind call.
189  * @param addrlen: for bind call.
190  * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
191  *      if enabled with value 2 IP6ONLY option is disabled.
192  * @param inuse: on error, this is set true if the port was in use.
193  * @param noproto: on error, this is set true if cause is that the
194         IPv6 proto (family) is not available.
195  * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
196  * @param snd: set size on sndbuf with socket option, if 0 it is not set.
197  * @param listen: if true, this is a listening UDP port, eg port 53, and 
198  *      set SO_REUSEADDR on it.
199  * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
200  *      listening UDP port.  Set to false on return if it failed to do so.
201  * @param transparent: set IP_TRANSPARENT socket option.
202  * @param freebind: set IP_FREEBIND socket option.
203  * @param use_systemd: if true, fetch sockets from systemd.
204  * @return: the socket. -1 on error.
205  */
206 int create_udp_sock(int family, int socktype, struct sockaddr* addr, 
207         socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
208         int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd);
209
210 /**
211  * Create and bind TCP listening socket
212  * @param addr: address info ready to make socket.
213  * @param v6only: enable ip6 only flag on ip6 sockets.
214  * @param noproto: if error caused by lack of protocol support.
215  * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
216  *      listening UDP port.  Set to false on return if it failed to do so.
217  * @param transparent: set IP_TRANSPARENT socket option.
218  * @param mss: maximum segment size of the socket. if zero, leaves the default. 
219  * @param freebind: set IP_FREEBIND socket option.
220  * @param use_systemd: if true, fetch sockets from systemd.
221  * @return: the socket. -1 on error.
222  */
223 int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
224         int* reuseport, int transparent, int mss, int freebind, int use_systemd);
225
226 /**
227  * Create and bind local listening socket
228  * @param path: path to the socket.
229  * @param noproto: on error, this is set true if cause is that local sockets
230  *      are not supported.
231  * @param use_systemd: if true, fetch sockets from systemd.
232  * @return: the socket. -1 on error.
233  */
234 int create_local_accept_sock(const char* path, int* noproto, int use_systemd);
235
236 #endif /* LISTEN_DNSPORT_H */