listen_dnsport.h revision 266114
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. 132238106Sdes * @param cb: callback function when a request arrives. It is passed 133238106Sdes * the packet and user argument. Return true to send a reply. 134238106Sdes * @param cb_arg: user data argument for callback function. 135238106Sdes * @return: the malloced listening structure, ready for use. NULL on error. 136238106Sdes */ 137238106Sdesstruct listen_dnsport* listen_create(struct comm_base* base, 138238106Sdes struct listen_port* ports, size_t bufsize, int tcp_accept_count, 139238106Sdes void* sslctx, comm_point_callback_t* cb, void* cb_arg); 140238106Sdes 141238106Sdes/** 142238106Sdes * delete the listening structure 143238106Sdes * @param listen: listening structure. 144238106Sdes */ 145238106Sdesvoid listen_delete(struct listen_dnsport* listen); 146238106Sdes 147238106Sdes/** 148238106Sdes * delete listen_list of commpoints. Calls commpointdelete() on items. 149238106Sdes * This may close the fds or not depending on flags. 150238106Sdes * @param list: to delete. 151238106Sdes */ 152238106Sdesvoid listen_list_delete(struct listen_list* list); 153238106Sdes 154238106Sdes/** 155238106Sdes * get memory size used by the listening structs 156238106Sdes * @param listen: listening structure. 157238106Sdes * @return: size in bytes. 158238106Sdes */ 159238106Sdessize_t listen_get_mem(struct listen_dnsport* listen); 160238106Sdes 161238106Sdes/** 162238106Sdes * stop accept handlers for TCP (until enabled again) 163238106Sdes * @param listen: listening structure. 164238106Sdes */ 165238106Sdesvoid listen_stop_accept(struct listen_dnsport* listen); 166238106Sdes 167238106Sdes/** 168238106Sdes * start accept handlers for TCP (was stopped before) 169238106Sdes * @param listen: listening structure. 170238106Sdes */ 171238106Sdesvoid listen_start_accept(struct listen_dnsport* listen); 172238106Sdes 173238106Sdes/** 174238106Sdes * Create and bind nonblocking UDP socket 175238106Sdes * @param family: for socket call. 176238106Sdes * @param socktype: for socket call. 177238106Sdes * @param addr: for bind call. 178238106Sdes * @param addrlen: for bind call. 179238106Sdes * @param v6only: if enabled, IP6 sockets get IP6ONLY option set. 180238106Sdes * if enabled with value 2 IP6ONLY option is disabled. 181238106Sdes * @param inuse: on error, this is set true if the port was in use. 182238106Sdes * @param noproto: on error, this is set true if cause is that the 183238106Sdes IPv6 proto (family) is not available. 184238106Sdes * @param rcv: set size on rcvbuf with socket option, if 0 it is not set. 185238106Sdes * @param snd: set size on sndbuf with socket option, if 0 it is not set. 186266114Sdes * @param listen: if true, this is a listening UDP port, eg port 53, and 187266114Sdes * set SO_REUSEADDR on it. 188266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 189266114Sdes * listening UDP port. Set to false on return if it failed to do so. 190238106Sdes * @return: the socket. -1 on error. 191238106Sdes */ 192238106Sdesint create_udp_sock(int family, int socktype, struct sockaddr* addr, 193238106Sdes socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv, 194266114Sdes int snd, int listen, int* reuseport); 195238106Sdes 196238106Sdes/** 197238106Sdes * Create and bind TCP listening socket 198238106Sdes * @param addr: address info ready to make socket. 199238106Sdes * @param v6only: enable ip6 only flag on ip6 sockets. 200238106Sdes * @param noproto: if error caused by lack of protocol support. 201266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 202266114Sdes * listening UDP port. Set to false on return if it failed to do so. 203238106Sdes * @return: the socket. -1 on error. 204238106Sdes */ 205266114Sdesint create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto, 206266114Sdes int* reuseport); 207238106Sdes 208238106Sdes#endif /* LISTEN_DNSPORT_H */ 209