listen_dnsport.h revision 366095
11590Srgrimes/* 21590Srgrimes * services/listen_dnsport.h - listen on port 53 for incoming DNS queries. 31590Srgrimes * 41590Srgrimes * Copyright (c) 2007, NLnet Labs. All rights reserved. 51590Srgrimes * 61590Srgrimes * This software is open source. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 121590Srgrimes * Redistributions of source code must retain the above copyright notice, 131590Srgrimes * this list of conditions and the following disclaimer. 141590Srgrimes * 151590Srgrimes * Redistributions in binary form must reproduce the above copyright notice, 161590Srgrimes * this list of conditions and the following disclaimer in the documentation 171590Srgrimes * and/or other materials provided with the distribution. 181590Srgrimes * 191590Srgrimes * Neither the name of the NLNET LABS nor the names of its contributors may 201590Srgrimes * be used to endorse or promote products derived from this software without 211590Srgrimes * specific prior written permission. 221590Srgrimes * 231590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 241590Srgrimes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 251590Srgrimes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 261590Srgrimes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 271590Srgrimes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 281590Srgrimes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 291590Srgrimes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 301590Srgrimes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3191792Smike * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 321590Srgrimes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 331590Srgrimes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 341590Srgrimes */ 351590Srgrimes 3691792Smike/** 371590Srgrimes * \file 381590Srgrimes * 3990131Smike * This file has functions to get queries from clients. 4028792Scharnier */ 411590Srgrimes 4290131Smike#ifndef LISTEN_DNSPORT_H 4390131Smike#define LISTEN_DNSPORT_H 4490131Smike 451590Srgrimes#include "util/netevent.h" 461590Srgrimesstruct listen_list; 471590Srgrimesstruct config_file; 4836913Speterstruct addrinfo; 4977368Sphkstruct sldns_buffer; 5028792Scharnierstruct tcl_list; 511590Srgrimes 5280050Smike/** 531590Srgrimes * Listening for queries structure. 5453291Sache * Contains list of query-listen sockets. 5528792Scharnier */ 5633626Swollmanstruct listen_dnsport { 5728792Scharnier /** Base for select calls */ 581590Srgrimes struct comm_base* base; 59130479Sbms 6053291Sache /** buffer shared by UDP connections, since there is only one 6154088Sache datagram at any time. */ 6243506Swollman struct sldns_buffer* udp_buff; 6333626Swollman#ifdef USE_DNSCRYPT 64106735Smike struct sldns_buffer* dnscrypt_udp_buff; 65138681Sceri#endif 6633626Swollman /** list of comm points used to get incoming events */ 6733626Swollman struct listen_list* cps; 6853291Sache}; 6953291Sache 7087536Smike/** 71112617Seivind * Single linked list to store event points. 72130466Sbms */ 73134294Smbrstruct listen_list { 74154710Sjhay /** next in list */ 7581165Smike struct listen_list* next; 7678581Sdes /** event info */ 77110159Sroberto struct comm_point* com; 781590Srgrimes}; 7953291Sache 8084852Smike/** 8153291Sache * type of ports 8284852Smike */ 8384852Smikeenum listen_type { 84227246Sed /** udp type */ 85227246Sed listen_type_udp, 86227246Sed /** tcp type */ 8778900Sdd listen_type_tcp, 8879835Smike /** udp ipv6 (v4mapped) for use with ancillary data */ 8980050Smike listen_type_udpancil, 9090163Skris /** ssl over tcp type */ 9178581Sdes listen_type_ssl, 9290131Smike /** udp type + dnscrypt*/ 9328792Scharnier listen_type_udp_dnscrypt, 9428792Scharnier /** tcp type + dnscrypt */ 9578581Sdes listen_type_tcp_dnscrypt, 961590Srgrimes /** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/ 9781165Smike listen_type_udpancil_dnscrypt 9853291Sache 9980050Smike}; 1001590Srgrimes 10115359Spst/** 10215359Spst * Single linked list to store shared ports that have been 10315359Spst * opened for use by all threads. 10415359Spst */ 10581165Smikestruct listen_port { 10681165Smike /** next in list */ 107202280Sedwin struct listen_port* next; 10878581Sdes /** file descriptor, open and ready for use */ 10933626Swollman int fd; 11033626Swollman /** type of file descriptor, udp or tcp */ 11133626Swollman enum listen_type ftype; 11281165Smike}; 11381165Smike 11481165Smike/** 115130479Sbms * Create shared listening ports 116130479Sbms * Getaddrinfo, create socket, bind and listen to zero or more 117130479Sbms * interfaces for IP4 and/or IP6, for UDP and/or TCP. 11881165Smike * On the given port number. It creates the sockets. 11981165Smike * @param cfg: settings on what ports to open. 12085067Smike * @param reuseport: set to true if you want reuseport, or NULL to not have it, 121154710Sjhay * set to false on exit if reuseport failed to apply (because of no 122154710Sjhay * kernel support). 123154710Sjhay * @return: linked list of ports or NULL on error. 12443506Swollman */ 12543506Swollmanstruct listen_port* listening_ports_open(struct config_file* cfg, 12643506Swollman int* reuseport); 1271590Srgrimes 1281590Srgrimes/** 1291590Srgrimes * Close and delete the (list of) listening ports. 13053048Sache */ 13153048Sachevoid listening_ports_free(struct listen_port* list); 13253048Sache 133130466Sbms/** 134130466Sbms * Create commpoints with for this thread for the shared ports. 135130466Sbms * @param base: the comm_base that provides event functionality. 136138681Sceri * for default all ifs. 137138681Sceri * @param ports: the list of shared ports. 138138681Sceri * @param bufsize: size of datagram buffer. 139106735Smike * @param tcp_accept_count: max number of simultaneous TCP connections 140106735Smike * from clients. 141106735Smike * @param tcp_idle_timeout: idle timeout for TCP connections in msec. 14253291Sache * @param tcp_conn_limit: TCP connection limit info. 14353291Sache * @param sslctx: nonNULL if ssl context. 14453291Sache * @param dtenv: nonNULL if dnstap enabled. 14533626Swollman * @param cb: callback function when a request arrives. It is passed 14681165Smike * the packet and user argument. Return true to send a reply. 14733626Swollman * @param cb_arg: user data argument for callback function. 14853291Sache * @return: the malloced listening structure, ready for use. NULL on error. 14953291Sache */ 15053291Sachestruct listen_dnsport* listen_create(struct comm_base* base, 15133626Swollman struct listen_port* ports, size_t bufsize, 15233626Swollman int tcp_accept_count, int tcp_idle_timeout, 15333626Swollman struct tcl_list* tcp_conn_limit, void* sslctx, 15443520Sache struct dt_env *dtenv, comm_point_callback_type* cb, void* cb_arg); 15581165Smike 15681165Smike/** 15743520Sache * delete the listening structure 158197725Sdougb * @param listen: listening structure. 15954172Sjoe */ 160197725Sdougbvoid listen_delete(struct listen_dnsport* listen); 161197725Sdougb 16254172Sjoe/** 1631590Srgrimes * delete listen_list of commpoints. Calls commpointdelete() on items. 1641590Srgrimes * This may close the fds or not depending on flags. 1651590Srgrimes * @param list: to delete. 16678581Sdes */ 1671590Srgrimesvoid listen_list_delete(struct listen_list* list); 16854227Sjoe 1691590Srgrimes/** 1701590Srgrimes * get memory size used by the listening structs 1711590Srgrimes * @param listen: listening structure. 17281165Smike * @return: size in bytes. 1731590Srgrimes */ 1741590Srgrimessize_t listen_get_mem(struct listen_dnsport* listen); 17553291Sache 17681165Smike/** 17781165Smike * stop accept handlers for TCP (until enabled again) 17878581Sdes * @param listen: listening structure. 17978581Sdes */ 18053291Sachevoid listen_stop_accept(struct listen_dnsport* listen); 18181165Smike 182268154Sume/** 183268154Sume * start accept handlers for TCP (was stopped before) 184268154Sume * @param listen: listening structure. 185268154Sume */ 186268154Sumevoid listen_start_accept(struct listen_dnsport* listen); 187268154Sume 18853291Sache/** 18990131Smike * Create and bind nonblocking UDP socket 19081165Smike * @param family: for socket call. 19181165Smike * @param socktype: for socket call. 19290131Smike * @param addr: for bind call. 19385067Smike * @param addrlen: for bind call. 19480050Smike * @param v6only: if enabled, IP6 sockets get IP6ONLY option set. 19590131Smike * if enabled with value 2 IP6ONLY option is disabled. 19680050Smike * @param inuse: on error, this is set true if the port was in use. 19790131Smike * @param noproto: on error, this is set true if cause is that the 19878581Sdes IPv6 proto (family) is not available. 19978581Sdes * @param rcv: set size on rcvbuf with socket option, if 0 it is not set. 20090131Smike * @param snd: set size on sndbuf with socket option, if 0 it is not set. 20153291Sache * @param listen: if true, this is a listening UDP port, eg port 53, and 20253291Sache * set SO_REUSEADDR on it. 20353291Sache * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 20453291Sache * listening UDP port. Set to false on return if it failed to do so. 20579835Smike * @param transparent: set IP_TRANSPARENT socket option. 20679835Smike * @param freebind: set IP_FREEBIND socket option. 20779835Smike * @param use_systemd: if true, fetch sockets from systemd. 20879835Smike * @param dscp: DSCP to use. 20979835Smike * @return: the socket. -1 on error. 21079835Smike */ 21179835Smikeint create_udp_sock(int family, int socktype, struct sockaddr* addr, 21279835Smike socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv, 21379835Smike int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp); 21479835Smike 21579835Smike/** 216202281Sedwin * Create and bind TCP listening socket 217202281Sedwin * @param addr: address info ready to make socket. 218202281Sedwin * @param v6only: enable ip6 only flag on ip6 sockets. 219202281Sedwin * @param noproto: if error caused by lack of protocol support. 22079835Smike * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 22179835Smike * listening UDP port. Set to false on return if it failed to do so. 22279835Smike * @param transparent: set IP_TRANSPARENT socket option. 22379835Smike * @param mss: maximum segment size of the socket. if zero, leaves the default. 224112617Seivind * @param freebind: set IP_FREEBIND socket option. 225112617Seivind * @param use_systemd: if true, fetch sockets from systemd. 226112617Seivind * @param dscp: DSCP to use. 227112617Seivind * @return: the socket. -1 on error. 228112617Seivind */ 229112617Seivindint create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto, 23079835Smike int* reuseport, int transparent, int mss, int freebind, int use_systemd, int dscp); 23179835Smike 23280155Smike/** 23380155Smike * Create and bind local listening socket 23480050Smike * @param path: path to the socket. 23580050Smike * @param noproto: on error, this is set true if cause is that local sockets 236117050Sache * are not supported. 23780050Smike * @param use_systemd: if true, fetch sockets from systemd. 23879835Smike * @return: the socket. -1 on error. 23979835Smike */ 24079835Smikeint create_local_accept_sock(const char* path, int* noproto, int use_systemd); 24185067Smike 24280050Smike/** 24380050Smike * TCP request info. List of requests outstanding on the channel, that 24480050Smike * are asked for but not yet answered back. 24580050Smike */ 24680050Smikestruct tcp_req_info { 24780050Smike /** the TCP comm point for this. Its buffer is used for read/write */ 24880050Smike struct comm_point* cp; 24980050Smike /** the buffer to use to spool reply from mesh into, 25080050Smike * it can then be copied to the result list and written. 25181165Smike * it is a pointer to the shared udp buffer. */ 25280050Smike struct sldns_buffer* spool_buffer; 25380050Smike /** are we in worker_handle function call (for recursion callback)*/ 25480050Smike int in_worker_handle; 25580050Smike /** is the comm point dropped (by worker handle). 25680050Smike * That means we have to disconnect the channel. */ 25780050Smike int is_drop; 25880050Smike /** is the comm point set to send_reply (by mesh new client in worker 25985067Smike * handle), if so answer is available in c.buffer */ 26080050Smike int is_reply; 26180050Smike /** read channel has closed, just write pending results */ 26280050Smike int read_is_closed; 26380050Smike /** read again */ 26453291Sache int read_again; 26580050Smike /** number of outstanding requests */ 26680050Smike int num_open_req; 26780050Smike /** list of outstanding requests */ 26880050Smike struct tcp_req_open_item* open_req_list; 26980050Smike /** number of pending writeable results */ 27080050Smike int num_done_req; 27180050Smike /** list of pending writable result packets, malloced one at a time */ 27280050Smike struct tcp_req_done_item* done_req_list; 27380050Smike}; 27480050Smike 27580050Smike/** 27680050Smike * List of open items in TCP channel 27780050Smike */ 27890131Smikestruct tcp_req_open_item { 27953291Sache /** next in list */ 28053291Sache struct tcp_req_open_item* next; 28190131Smike /** the mesh area of the mesh_state */ 28284852Smike struct mesh_area* mesh; 28384852Smike /** the mesh state */ 284103530Smike struct mesh_state* mesh_state; 28553291Sache}; 286146752Scharnier 28790131Smike/** 28890131Smike * List of done items in TCP channel 28977585Sume */ 29078581Sdesstruct tcp_req_done_item { 29177585Sume /** next in list */ 29278581Sdes struct tcp_req_done_item* next; 29377585Sume /** the buffer with packet contents */ 29477585Sume uint8_t* buf; 29554227Sjoe /** length of the buffer */ 29690131Smike size_t len; 29778581Sdes}; 29878581Sdes 29933626Swollman/** 3001590Srgrimes * Create tcp request info structure that keeps track of open 3011590Srgrimes * requests on the TCP channel that are resolved at the same time, 30278581Sdes * and the pending results that have to get written back to that client. 30378581Sdes * @param spoolbuf: shared buffer 304134294Smbr * @return new structure or NULL on alloc failure. 305134294Smbr */ 306166103Sphkstruct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf); 307166103Sphk 308134294Smbr/** 309134294Smbr * Delete tcp request structure. Called by owning commpoint. 310134294Smbr * Removes mesh entry references and stored results from the lists. 31178581Sdes * @param req: the tcp request info 31253291Sache */ 31378581Sdesvoid tcp_req_info_delete(struct tcp_req_info* req); 31484852Smike 31578581Sdes/** 31684852Smike * Clear tcp request structure. Removes list entries, sets it up ready 31753291Sache * for the next connection. 31878900Sdd * @param req: tcp request info structure. 31984852Smike */ 32084852Smikevoid tcp_req_info_clear(struct tcp_req_info* req); 32184852Smike 32284852Smike/** 32384852Smike * Remove mesh state entry from list in tcp_req_info. 32484852Smike * caller has to manage the mesh state reply entry in the mesh state. 32584852Smike * @param req: the tcp req info that has the entry removed from the list. 32684852Smike * @param m: the state removed from the list. 32778900Sdd */ 32884852Smikevoid tcp_req_info_remove_mesh_state(struct tcp_req_info* req, 32984852Smike struct mesh_state* m); 330111430Smike 331111430Smike/** 332111430Smike * Handle write done of the last result packet 333110159Sroberto * @param req: the tcp req info. 334110159Sroberto */ 335110159Srobertovoid tcp_req_info_handle_writedone(struct tcp_req_info* req); 336110159Sroberto 337110159Sroberto/** 338110159Sroberto * Handle read done of a new request from the client 339110159Sroberto * @param req: the tcp req info. 340111430Smike */ 341111430Smikevoid tcp_req_info_handle_readdone(struct tcp_req_info* req); 342103530Smike 343168721Sache/** 34478900Sdd * Add mesh state to the tcp req list of open requests. 34584852Smike * So the comm_reply can be removed off the mesh reply list when 34684852Smike * the tcp channel has to be closed (for other reasons then that that 34784852Smike * request was done, eg. channel closed by client or some format error). 34884852Smike * @param req: tcp req info structure. It keeps track of the simultaneous 34984852Smike * requests and results on a tcp (or TLS) channel. 35084852Smike * @param mesh: mesh area for the state. 35178900Sdd * @param m: mesh state to add. 35253291Sache * @return 0 on failure (malloc failure). 35353291Sache */ 35453291Sacheint tcp_req_info_add_meshstate(struct tcp_req_info* req, 35578581Sdes struct mesh_area* mesh, struct mesh_state* m); 35690131Smike 35778581Sdes/** 35853291Sache * Send reply on tcp simultaneous answer channel. May queue it up. 3591590Srgrimes * @param req: request info structure. 3601590Srgrimes */ 36128792Scharniervoid tcp_req_info_send_reply(struct tcp_req_info* req); 36278581Sdes 3631590Srgrimes/** the read channel has closed 36478581Sdes * @param req: request. remaining queries are looked up and answered. 365202280Sedwin * @return zero if nothing to do, just close the tcp. 36681165Smike */ 36733626Swollmanint tcp_req_info_handle_read_close(struct tcp_req_info* req); 3681590Srgrimes 369/** get the size of currently used tcp stream wait buffers (in bytes) */ 370size_t tcp_req_info_get_stream_buffer_size(void); 371 372char* set_ip_dscp(int socket, int addrfamily, int ds); 373char* sock_strerror(int errn); 374 375#endif /* LISTEN_DNSPORT_H */ 376