listen_dnsport.h revision 238106
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 24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33238106Sdes * 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; 49238106Sdes 50238106Sdes/** 51238106Sdes * Listening for queries structure. 52238106Sdes * Contains list of query-listen sockets. 53238106Sdes */ 54238106Sdesstruct listen_dnsport { 55238106Sdes /** Base for select calls */ 56238106Sdes struct comm_base* base; 57238106Sdes 58238106Sdes /** buffer shared by UDP connections, since there is only one 59238106Sdes datagram at any time. */ 60238106Sdes ldns_buffer* udp_buff; 61238106Sdes 62238106Sdes /** list of comm points used to get incoming events */ 63238106Sdes struct listen_list* cps; 64238106Sdes}; 65238106Sdes 66238106Sdes/** 67238106Sdes * Single linked list to store event points. 68238106Sdes */ 69238106Sdesstruct listen_list { 70238106Sdes /** next in list */ 71238106Sdes struct listen_list* next; 72238106Sdes /** event info */ 73238106Sdes struct comm_point* com; 74238106Sdes}; 75238106Sdes 76238106Sdes/** 77238106Sdes * type of ports 78238106Sdes */ 79238106Sdesenum listen_type { 80238106Sdes /** udp type */ 81238106Sdes listen_type_udp, 82238106Sdes /** tcp type */ 83238106Sdes listen_type_tcp, 84238106Sdes /** udp ipv6 (v4mapped) for use with ancillary data */ 85238106Sdes listen_type_udpancil, 86238106Sdes /** ssl over tcp type */ 87238106Sdes listen_type_ssl 88238106Sdes}; 89238106Sdes 90238106Sdes/** 91238106Sdes * Single linked list to store shared ports that have been 92238106Sdes * opened for use by all threads. 93238106Sdes */ 94238106Sdesstruct listen_port { 95238106Sdes /** next in list */ 96238106Sdes struct listen_port* next; 97238106Sdes /** file descriptor, open and ready for use */ 98238106Sdes int fd; 99238106Sdes /** type of file descriptor, udp or tcp */ 100238106Sdes enum listen_type ftype; 101238106Sdes}; 102238106Sdes 103238106Sdes/** 104238106Sdes * Create shared listening ports 105238106Sdes * Getaddrinfo, create socket, bind and listen to zero or more 106238106Sdes * interfaces for IP4 and/or IP6, for UDP and/or TCP. 107238106Sdes * On the given port number. It creates the sockets. 108238106Sdes * @param cfg: settings on what ports to open. 109238106Sdes * @return: linked list of ports or NULL on error. 110238106Sdes */ 111238106Sdesstruct listen_port* listening_ports_open(struct config_file* cfg); 112238106Sdes 113238106Sdes/** 114238106Sdes * Close and delete the (list of) listening ports. 115238106Sdes */ 116238106Sdesvoid listening_ports_free(struct listen_port* list); 117238106Sdes 118238106Sdes/** 119238106Sdes * Create commpoints with for this thread for the shared ports. 120238106Sdes * @param base: the comm_base that provides event functionality. 121238106Sdes * for default all ifs. 122238106Sdes * @param ports: the list of shared ports. 123238106Sdes * @param bufsize: size of datagram buffer. 124238106Sdes * @param tcp_accept_count: max number of simultaneous TCP connections 125238106Sdes * from clients. 126238106Sdes * @param sslctx: nonNULL if ssl context. 127238106Sdes * @param cb: callback function when a request arrives. It is passed 128238106Sdes * the packet and user argument. Return true to send a reply. 129238106Sdes * @param cb_arg: user data argument for callback function. 130238106Sdes * @return: the malloced listening structure, ready for use. NULL on error. 131238106Sdes */ 132238106Sdesstruct listen_dnsport* listen_create(struct comm_base* base, 133238106Sdes struct listen_port* ports, size_t bufsize, int tcp_accept_count, 134238106Sdes void* sslctx, comm_point_callback_t* cb, void* cb_arg); 135238106Sdes 136238106Sdes/** 137238106Sdes * delete the listening structure 138238106Sdes * @param listen: listening structure. 139238106Sdes */ 140238106Sdesvoid listen_delete(struct listen_dnsport* listen); 141238106Sdes 142238106Sdes/** 143238106Sdes * delete listen_list of commpoints. Calls commpointdelete() on items. 144238106Sdes * This may close the fds or not depending on flags. 145238106Sdes * @param list: to delete. 146238106Sdes */ 147238106Sdesvoid listen_list_delete(struct listen_list* list); 148238106Sdes 149238106Sdes/** 150238106Sdes * get memory size used by the listening structs 151238106Sdes * @param listen: listening structure. 152238106Sdes * @return: size in bytes. 153238106Sdes */ 154238106Sdessize_t listen_get_mem(struct listen_dnsport* listen); 155238106Sdes 156238106Sdes/** 157238106Sdes * stop accept handlers for TCP (until enabled again) 158238106Sdes * @param listen: listening structure. 159238106Sdes */ 160238106Sdesvoid listen_stop_accept(struct listen_dnsport* listen); 161238106Sdes 162238106Sdes/** 163238106Sdes * start accept handlers for TCP (was stopped before) 164238106Sdes * @param listen: listening structure. 165238106Sdes */ 166238106Sdesvoid listen_start_accept(struct listen_dnsport* listen); 167238106Sdes 168238106Sdes/** 169238106Sdes * Create and bind nonblocking UDP socket 170238106Sdes * @param family: for socket call. 171238106Sdes * @param socktype: for socket call. 172238106Sdes * @param addr: for bind call. 173238106Sdes * @param addrlen: for bind call. 174238106Sdes * @param v6only: if enabled, IP6 sockets get IP6ONLY option set. 175238106Sdes * if enabled with value 2 IP6ONLY option is disabled. 176238106Sdes * @param inuse: on error, this is set true if the port was in use. 177238106Sdes * @param noproto: on error, this is set true if cause is that the 178238106Sdes IPv6 proto (family) is not available. 179238106Sdes * @param rcv: set size on rcvbuf with socket option, if 0 it is not set. 180238106Sdes * @param snd: set size on sndbuf with socket option, if 0 it is not set. 181238106Sdes * @return: the socket. -1 on error. 182238106Sdes */ 183238106Sdesint create_udp_sock(int family, int socktype, struct sockaddr* addr, 184238106Sdes socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv, 185238106Sdes int snd); 186238106Sdes 187238106Sdes/** 188238106Sdes * Create and bind TCP listening socket 189238106Sdes * @param addr: address info ready to make socket. 190238106Sdes * @param v6only: enable ip6 only flag on ip6 sockets. 191238106Sdes * @param noproto: if error caused by lack of protocol support. 192238106Sdes * @return: the socket. -1 on error. 193238106Sdes */ 194238106Sdesint create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto); 195238106Sdes 196238106Sdes#endif /* LISTEN_DNSPORT_H */ 197