listen_dnsport.h revision 296415
1238106Sdes/*
2238106Sdes * services/listen_dnsport.h - listen on port 53 for incoming DNS queries.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24266114Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25266114Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26266114Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27266114Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28266114Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29266114Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30266114Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31266114Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32266114Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33266114Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file has functions to get queries from clients.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef LISTEN_DNSPORT_H
43238106Sdes#define LISTEN_DNSPORT_H
44238106Sdes
45238106Sdes#include "util/netevent.h"
46238106Sdesstruct listen_list;
47238106Sdesstruct config_file;
48238106Sdesstruct addrinfo;
49266114Sdesstruct sldns_buffer;
50238106Sdes
51238106Sdes/**
52238106Sdes * Listening for queries structure.
53238106Sdes * Contains list of query-listen sockets.
54238106Sdes */
55238106Sdesstruct listen_dnsport {
56238106Sdes	/** Base for select calls */
57238106Sdes	struct comm_base* base;
58238106Sdes
59238106Sdes	/** buffer shared by UDP connections, since there is only one
60238106Sdes	    datagram at any time. */
61266114Sdes	struct sldns_buffer* udp_buff;
62238106Sdes
63238106Sdes	/** list of comm points used to get incoming events */
64238106Sdes	struct listen_list* cps;
65238106Sdes};
66238106Sdes
67238106Sdes/**
68238106Sdes * Single linked list to store event points.
69238106Sdes */
70238106Sdesstruct listen_list {
71238106Sdes	/** next in list */
72238106Sdes	struct listen_list* next;
73238106Sdes	/** event info */
74238106Sdes	struct comm_point* com;
75238106Sdes};
76238106Sdes
77238106Sdes/**
78238106Sdes * type of ports
79238106Sdes */
80238106Sdesenum listen_type {
81238106Sdes	/** udp type */
82238106Sdes	listen_type_udp,
83238106Sdes	/** tcp type */
84238106Sdes	listen_type_tcp,
85238106Sdes	/** udp ipv6 (v4mapped) for use with ancillary data */
86238106Sdes	listen_type_udpancil,
87238106Sdes	/** ssl over tcp type */
88238106Sdes	listen_type_ssl
89238106Sdes};
90238106Sdes
91238106Sdes/**
92238106Sdes * Single linked list to store shared ports that have been
93238106Sdes * opened for use by all threads.
94238106Sdes */
95238106Sdesstruct listen_port {
96238106Sdes	/** next in list */
97238106Sdes	struct listen_port* next;
98238106Sdes	/** file descriptor, open and ready for use */
99238106Sdes	int fd;
100238106Sdes	/** type of file descriptor, udp or tcp */
101238106Sdes	enum listen_type ftype;
102238106Sdes};
103238106Sdes
104238106Sdes/**
105238106Sdes * Create shared listening ports
106238106Sdes * Getaddrinfo, create socket, bind and listen to zero or more
107238106Sdes * interfaces for IP4 and/or IP6, for UDP and/or TCP.
108238106Sdes * On the given port number. It creates the sockets.
109238106Sdes * @param cfg: settings on what ports to open.
110266114Sdes * @param reuseport: set to true if you want reuseport, or NULL to not have it,
111266114Sdes *   set to false on exit if reuseport failed to apply (because of no
112266114Sdes *   kernel support).
113238106Sdes * @return: linked list of ports or NULL on error.
114238106Sdes */
115266114Sdesstruct listen_port* listening_ports_open(struct config_file* cfg,
116266114Sdes	int* reuseport);
117238106Sdes
118238106Sdes/**
119238106Sdes * Close and delete the (list of) listening ports.
120238106Sdes */
121238106Sdesvoid listening_ports_free(struct listen_port* list);
122238106Sdes
123238106Sdes/**
124238106Sdes * Create commpoints with for this thread for the shared ports.
125238106Sdes * @param base: the comm_base that provides event functionality.
126238106Sdes *	for default all ifs.
127238106Sdes * @param ports: the list of shared ports.
128238106Sdes * @param bufsize: size of datagram buffer.
129238106Sdes * @param tcp_accept_count: max number of simultaneous TCP connections
130238106Sdes * 	from clients.
131238106Sdes * @param sslctx: nonNULL if ssl context.
132276605Sdes * @param dtenv: nonNULL if dnstap enabled.
133238106Sdes * @param cb: callback function when a request arrives. It is passed
134238106Sdes *	  the packet and user argument. Return true to send a reply.
135238106Sdes * @param cb_arg: user data argument for callback function.
136238106Sdes * @return: the malloced listening structure, ready for use. NULL on error.
137238106Sdes */
138238106Sdesstruct listen_dnsport* listen_create(struct comm_base* base,
139238106Sdes	struct listen_port* ports, size_t bufsize, int tcp_accept_count,
140276605Sdes	void* sslctx, struct dt_env *dtenv, comm_point_callback_t* cb,
141276605Sdes	void* cb_arg);
142238106Sdes
143238106Sdes/**
144238106Sdes * delete the listening structure
145238106Sdes * @param listen: listening structure.
146238106Sdes */
147238106Sdesvoid listen_delete(struct listen_dnsport* listen);
148238106Sdes
149238106Sdes/**
150238106Sdes * delete listen_list of commpoints. Calls commpointdelete() on items.
151238106Sdes * This may close the fds or not depending on flags.
152238106Sdes * @param list: to delete.
153238106Sdes */
154238106Sdesvoid listen_list_delete(struct listen_list* list);
155238106Sdes
156238106Sdes/**
157238106Sdes * get memory size used by the listening structs
158238106Sdes * @param listen: listening structure.
159238106Sdes * @return: size in bytes.
160238106Sdes */
161238106Sdessize_t listen_get_mem(struct listen_dnsport* listen);
162238106Sdes
163238106Sdes/**
164238106Sdes * stop accept handlers for TCP (until enabled again)
165238106Sdes * @param listen: listening structure.
166238106Sdes */
167238106Sdesvoid listen_stop_accept(struct listen_dnsport* listen);
168238106Sdes
169238106Sdes/**
170238106Sdes * start accept handlers for TCP (was stopped before)
171238106Sdes * @param listen: listening structure.
172238106Sdes */
173238106Sdesvoid listen_start_accept(struct listen_dnsport* listen);
174238106Sdes
175238106Sdes/**
176238106Sdes * Create and bind nonblocking UDP socket
177238106Sdes * @param family: for socket call.
178238106Sdes * @param socktype: for socket call.
179238106Sdes * @param addr: for bind call.
180238106Sdes * @param addrlen: for bind call.
181238106Sdes * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
182238106Sdes * 	if enabled with value 2 IP6ONLY option is disabled.
183238106Sdes * @param inuse: on error, this is set true if the port was in use.
184238106Sdes * @param noproto: on error, this is set true if cause is that the
185238106Sdes	IPv6 proto (family) is not available.
186238106Sdes * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
187238106Sdes * @param snd: set size on sndbuf with socket option, if 0 it is not set.
188266114Sdes * @param listen: if true, this is a listening UDP port, eg port 53, and
189266114Sdes * 	set SO_REUSEADDR on it.
190266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
191266114Sdes * 	listening UDP port.  Set to false on return if it failed to do so.
192287917Sdes * @param transparent: set IP_TRANSPARENT socket option.
193238106Sdes * @return: the socket. -1 on error.
194238106Sdes */
195238106Sdesint create_udp_sock(int family, int socktype, struct sockaddr* addr,
196238106Sdes	socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
197287917Sdes	int snd, int listen, int* reuseport, int transparent);
198238106Sdes
199238106Sdes/**
200238106Sdes * Create and bind TCP listening socket
201238106Sdes * @param addr: address info ready to make socket.
202238106Sdes * @param v6only: enable ip6 only flag on ip6 sockets.
203238106Sdes * @param noproto: if error caused by lack of protocol support.
204266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
205266114Sdes * 	listening UDP port.  Set to false on return if it failed to do so.
206287917Sdes * @param transparent: set IP_TRANSPARENT socket option.
207296415Sdes * @param mss: maximum segment size of the socket. if zero, leaves the default.
208238106Sdes * @return: the socket. -1 on error.
209238106Sdes */
210266114Sdesint create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
211296415Sdes	int* reuseport, int transparent, int mss);
212238106Sdes
213276699Sdes/**
214276699Sdes * Create and bind local listening socket
215276699Sdes * @param path: path to the socket.
216276699Sdes * @param noproto: on error, this is set true if cause is that local sockets
217276699Sdes *	are not supported.
218276699Sdes * @return: the socket. -1 on error.
219276699Sdes */
220277057Sdesint create_local_accept_sock(const char* path, int* noproto);
221276699Sdes
222238106Sdes#endif /* LISTEN_DNSPORT_H */
223