]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/unbound/services/listen_dnsport.h
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * 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
50 /**
51  * Listening for queries structure.
52  * Contains list of query-listen sockets.
53  */
54 struct listen_dnsport {
55         /** Base for select calls */
56         struct comm_base* base;
57
58         /** buffer shared by UDP connections, since there is only one
59             datagram at any time. */
60         ldns_buffer* udp_buff;
61
62         /** list of comm points used to get incoming events */
63         struct listen_list* cps;
64 };
65
66 /**
67  * Single linked list to store event points.
68  */
69 struct listen_list {
70         /** next in list */
71         struct listen_list* next;
72         /** event info */
73         struct comm_point* com;
74 };
75
76 /**
77  * type of ports
78  */
79 enum listen_type {
80         /** udp type */
81         listen_type_udp,
82         /** tcp type */
83         listen_type_tcp,
84         /** udp ipv6 (v4mapped) for use with ancillary data */
85         listen_type_udpancil,
86         /** ssl over tcp type */
87         listen_type_ssl
88 };
89
90 /**
91  * Single linked list to store shared ports that have been 
92  * opened for use by all threads.
93  */
94 struct listen_port {
95         /** next in list */
96         struct listen_port* next;
97         /** file descriptor, open and ready for use */
98         int fd;
99         /** type of file descriptor, udp or tcp */
100         enum listen_type ftype;
101 };
102
103 /**
104  * Create shared listening ports
105  * Getaddrinfo, create socket, bind and listen to zero or more 
106  * interfaces for IP4 and/or IP6, for UDP and/or TCP.
107  * On the given port number. It creates the sockets.
108  * @param cfg: settings on what ports to open.
109  * @return: linked list of ports or NULL on error.
110  */
111 struct listen_port* listening_ports_open(struct config_file* cfg);
112
113 /**
114  * Close and delete the (list of) listening ports.
115  */
116 void listening_ports_free(struct listen_port* list);
117
118 /**
119  * Create commpoints with for this thread for the shared ports.
120  * @param base: the comm_base that provides event functionality.
121  *      for default all ifs.
122  * @param ports: the list of shared ports.
123  * @param bufsize: size of datagram buffer.
124  * @param tcp_accept_count: max number of simultaneous TCP connections 
125  *      from clients.
126  * @param sslctx: nonNULL if ssl context.
127  * @param cb: callback function when a request arrives. It is passed
128  *        the packet and user argument. Return true to send a reply.
129  * @param cb_arg: user data argument for callback function.
130  * @return: the malloced listening structure, ready for use. NULL on error.
131  */
132 struct listen_dnsport* listen_create(struct comm_base* base,
133         struct listen_port* ports, size_t bufsize, int tcp_accept_count,
134         void* sslctx, comm_point_callback_t* cb, void* cb_arg);
135
136 /**
137  * delete the listening structure
138  * @param listen: listening structure.
139  */
140 void listen_delete(struct listen_dnsport* listen);
141
142 /**
143  * delete listen_list of commpoints. Calls commpointdelete() on items.
144  * This may close the fds or not depending on flags.
145  * @param list: to delete.
146  */
147 void listen_list_delete(struct listen_list* list);
148
149 /**
150  * get memory size used by the listening structs
151  * @param listen: listening structure.
152  * @return: size in bytes.
153  */
154 size_t listen_get_mem(struct listen_dnsport* listen);
155
156 /**
157  * stop accept handlers for TCP (until enabled again)
158  * @param listen: listening structure.
159  */
160 void listen_stop_accept(struct listen_dnsport* listen);
161
162 /**
163  * start accept handlers for TCP (was stopped before)
164  * @param listen: listening structure.
165  */
166 void listen_start_accept(struct listen_dnsport* listen);
167
168 /**
169  * Create and bind nonblocking UDP socket
170  * @param family: for socket call.
171  * @param socktype: for socket call.
172  * @param addr: for bind call.
173  * @param addrlen: for bind call.
174  * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
175  *      if enabled with value 2 IP6ONLY option is disabled.
176  * @param inuse: on error, this is set true if the port was in use.
177  * @param noproto: on error, this is set true if cause is that the
178         IPv6 proto (family) is not available.
179  * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
180  * @param snd: set size on sndbuf with socket option, if 0 it is not set.
181  * @return: the socket. -1 on error.
182  */
183 int create_udp_sock(int family, int socktype, struct sockaddr* addr, 
184         socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
185         int snd);
186
187 /**
188  * Create and bind TCP listening socket
189  * @param addr: address info ready to make socket.
190  * @param v6only: enable ip6 only flag on ip6 sockets.
191  * @param noproto: if error caused by lack of protocol support.
192  * @return: the socket. -1 on error.
193  */
194 int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto);
195
196 #endif /* LISTEN_DNSPORT_H */