Macros | Functions
listen_dnsport.c File Reference

This file has functions to get queries from clients. More...

#include "config.h"
#include <sys/time.h>
#include "services/listen_dnsport.h"
#include "services/outside_network.h"
#include "util/netevent.h"
#include "util/log.h"
#include "util/config_file.h"
#include "util/net_help.h"
#include "sldns/sbuffer.h"
#include <fcntl.h>

Macros

#define TCP_BACKLOG   256
 number of queued TCP connections for listen()
 

Functions

static void verbose_print_addr (struct addrinfo *addr)
 Debug print of the getaddrinfo returned address. More...
 
int create_udp_sock (int family, int socktype, struct sockaddr *addr, socklen_t addrlen, int v6only, int *inuse, int *noproto, int rcv, int snd, int listen, int *reuseport, int transparent, int freebind, int use_systemd)
 Create and bind nonblocking UDP socket. More...
 
int create_tcp_accept_sock (struct addrinfo *addr, int v6only, int *noproto, int *reuseport, int transparent, int mss, int freebind, int use_systemd)
 Create and bind TCP listening socket. More...
 
int create_local_accept_sock (const char *path, int *noproto, int use_systemd)
 Create and bind local listening socket. More...
 
static int make_sock (int stype, const char *ifname, const char *port, struct addrinfo *hints, int v6only, int *noip6, size_t rcv, size_t snd, int *reuseport, int transparent, int tcp_mss, int freebind, int use_systemd)
 Create socket from getaddrinfo results.
 
static int make_sock_port (int stype, const char *ifname, const char *port, struct addrinfo *hints, int v6only, int *noip6, size_t rcv, size_t snd, int *reuseport, int transparent, int tcp_mss, int freebind, int use_systemd)
 make socket and first see if ifname contains port override info
 
static int port_insert (struct listen_port **list, int s, enum listen_type ftype)
 Add port to open ports list. More...
 
static int set_recvpktinfo (int s, int family)
 set fd to receive source address packet info
 
static int ports_create_if (const char *ifname, int do_auto, int do_udp, int do_tcp, struct addrinfo *hints, const char *port, struct listen_port **list, size_t rcv, size_t snd, int ssl_port, int *reuseport, int transparent, int tcp_mss, int freebind, int use_systemd, int dnscrypt_port)
 Helper for ports_open. More...
 
static int listen_cp_insert (struct comm_point *c, struct listen_dnsport *front)
 Add items to commpoint list in front. More...
 
struct listen_dnsportlisten_create (struct comm_base *base, struct listen_port *ports, size_t bufsize, int tcp_accept_count, void *sslctx, struct dt_env *dtenv, comm_point_callback_type *cb, void *cb_arg)
 Create commpoints with for this thread for the shared ports. More...
 
void listen_list_delete (struct listen_list *list)
 delete listen_list of commpoints. More...
 
void listen_delete (struct listen_dnsport *front)
 delete the listening structure More...
 
struct listen_portlistening_ports_open (struct config_file *cfg, int *reuseport)
 Create shared listening ports Getaddrinfo, create socket, bind and listen to zero or more interfaces for IP4 and/or IP6, for UDP and/or TCP. More...
 
void listening_ports_free (struct listen_port *list)
 Close and delete the (list of) listening ports.
 
size_t listen_get_mem (struct listen_dnsport *listen)
 get memory size used by the listening structs More...
 
void listen_stop_accept (struct listen_dnsport *listen)
 stop accept handlers for TCP (until enabled again) More...
 
void listen_start_accept (struct listen_dnsport *listen)
 start accept handlers for TCP (was stopped before) More...
 

Detailed Description

This file has functions to get queries from clients.

Function Documentation

◆ verbose_print_addr()

static void verbose_print_addr ( struct addrinfo *  addr)
static

Debug print of the getaddrinfo returned address.

Parameters
addrthe address returned.

References log_err(), log_err_addr(), log_warn(), VERB_ALGO, verbose(), and verbosity.

Referenced by create_tcp_accept_sock(), and make_sock().

◆ create_udp_sock()

int create_udp_sock ( int  family,
int  socktype,
struct sockaddr *  addr,
socklen_t  addrlen,
int  v6only,
int *  inuse,
int *  noproto,
int  rcv,
int  snd,
int  listen,
int *  reuseport,
int  transparent,
int  freebind,
int  use_systemd 
)

Create and bind nonblocking UDP socket.

Parameters
familyfor socket call.
socktypefor socket call.
addrfor bind call.
addrlenfor bind call.
v6onlyif enabled, IP6 sockets get IP6ONLY option set. if enabled with value 2 IP6ONLY option is disabled.
inuseon error, this is set true if the port was in use.
noprotoon error, this is set true if cause is that the IPv6 proto (family) is not available.
rcvset size on rcvbuf with socket option, if 0 it is not set.
sndset size on sndbuf with socket option, if 0 it is not set.
listenif true, this is a listening UDP port, eg port 53, and set SO_REUSEADDR on it.
reuseportif nonNULL and true, try to set SO_REUSEPORT on listening UDP port. Set to false on return if it failed to do so.
transparentset IP_TRANSPARENT socket option.
freebindset IP_FREEBIND socket option.
use_systemdif true, fetch sockets from systemd.
Returns
: the socket. -1 on error.

References fd_set_nonblock(), log_err(), log_err_addr(), log_warn(), and verbosity.

Referenced by make_sock().

◆ create_tcp_accept_sock()

int create_tcp_accept_sock ( struct addrinfo *  addr,
int  v6only,
int *  noproto,
int *  reuseport,
int  transparent,
int  mss,
int  freebind,
int  use_systemd 
)

Create and bind TCP listening socket.

Parameters
addraddress info ready to make socket.
v6onlyenable ip6 only flag on ip6 sockets.
noprotoif error caused by lack of protocol support.
reuseportif nonNULL and true, try to set SO_REUSEPORT on listening UDP port. Set to false on return if it failed to do so.
transparentset IP_TRANSPARENT socket option.
mssmaximum segment size of the socket. if zero, leaves the default.
freebindset IP_FREEBIND socket option.
use_systemdif true, fetch sockets from systemd.
Returns
: the socket. -1 on error.

References fd_set_nonblock(), log_err(), log_err_addr(), log_warn(), TCP_BACKLOG, VERB_ALGO, verbose(), verbose_print_addr(), and verbosity.

Referenced by add_open(), and make_sock().

◆ create_local_accept_sock()

int create_local_accept_sock ( const char *  path,
int *  noproto,
int  use_systemd 
)

Create and bind local listening socket.

Parameters
pathpath to the socket.
noprotoon error, this is set true if cause is that local sockets are not supported.
use_systemdif true, fetch sockets from systemd.
Returns
: the socket. -1 on error.

References fd_set_nonblock(), log_err(), TCP_BACKLOG, VERB_ALGO, and verbose().

Referenced by add_open().

◆ port_insert()

static int port_insert ( struct listen_port **  list,
int  s,
enum listen_type  ftype 
)
static

Add port to open ports list.

Parameters
listlist head. changed.
sfd.
ftypeif fd is UDP.
Returns
false on failure. list in unchanged then.

References listen_port::fd, listen_port::ftype, and listen_port::next.

Referenced by ports_create_if().

◆ ports_create_if()

static int ports_create_if ( const char *  ifname,
int  do_auto,
int  do_udp,
int  do_tcp,
struct addrinfo *  hints,
const char *  port,
struct listen_port **  list,
size_t  rcv,
size_t  snd,
int  ssl_port,
int *  reuseport,
int  transparent,
int  tcp_mss,
int  freebind,
int  use_systemd,
int  dnscrypt_port 
)
static

Helper for ports_open.

Creates one interface (or NULL for default).

Parameters
ifnameThe interface ip address.
do_autouse automatic interface detection. If enabled, then ifname must be the wildcard name.
do_udpif udp should be used.
do_tcpif udp should be used.
hintsfor getaddrinfo. family and flags have to be set by caller.
portPort number to use (as string).
listlist of open ports, appended to, changed to point to list head.
rcvreceive buffer size for UDP
sndsend buffer size for UDP
ssl_portssl service port number
reuseporttry to set SO_REUSEPORT if nonNULL and true. set to false on exit if reuseport failed due to no kernel support.
transparentset IP_TRANSPARENT socket option.
tcp_mssmaximum segment size of tcp socket. default if zero.
freebindset IP_FREEBIND socket option.
use_systemdif true, fetch sockets from systemd.
dnscrypt_portdnscrypt service port number
Returns
: returns false on error.

References listen_type_ssl, listen_type_tcp, listen_type_tcp_dnscrypt, listen_type_udp, listen_type_udp_dnscrypt, listen_type_udpancil, listen_type_udpancil_dnscrypt, log_warn(), make_sock_port(), port_insert(), set_recvpktinfo(), VERB_ALGO, and verbose().

Referenced by listening_ports_open().

◆ listen_cp_insert()

static int listen_cp_insert ( struct comm_point c,
struct listen_dnsport front 
)
static

Add items to commpoint list in front.

Parameters
ccommpoint to add.
frontlisten struct.
Returns
: false on failure.

References listen_list::com, listen_dnsport::cps, and listen_list::next.

Referenced by listen_create().

◆ listen_create()

struct listen_dnsport* listen_create ( struct comm_base base,
struct listen_port ports,
size_t  bufsize,
int  tcp_accept_count,
void *  sslctx,
struct dt_env *  dtenv,
comm_point_callback_type cb,
void *  cb_arg 
)

Create commpoints with for this thread for the shared ports.

Parameters
basethe comm_base that provides event functionality. for default all ifs.
portsthe list of shared ports.
bufsizesize of datagram buffer.
tcp_accept_countmax number of simultaneous TCP connections from clients.
sslctxnonNULL if ssl context.
dtenvnonNULL if dnstap enabled.
cbcallback function when a request arrives. It is passed the packet and user argument. Return true to send a reply.
cb_arguser data argument for callback function.
Returns
: the malloced listening structure, ready for use. NULL on error.

References comm_point_create_tcp(), comm_point_create_udp(), comm_point_create_udp_ancil(), comm_point_delete(), listen_dnsport::cps, comm_point::do_not_close, comm_point::dtenv, listen_port::fd, listen_port::ftype, listen_cp_insert(), listen_delete(), listen_type_ssl, listen_type_tcp, listen_type_tcp_dnscrypt, listen_type_udp, listen_type_udp_dnscrypt, listen_type_udpancil, listen_type_udpancil_dnscrypt, log_err(), listen_port::next, sldns_buffer_new(), comm_point::ssl, and listen_dnsport::udp_buff.

◆ listen_list_delete()

void listen_list_delete ( struct listen_list list)

delete listen_list of commpoints.

Calls commpointdelete() on items. This may close the fds or not depending on flags.

Parameters
listto delete.

References listen_list::com, comm_point_delete(), and listen_list::next.

Referenced by daemon_remote_clear(), and listen_delete().

◆ listen_delete()

void listen_delete ( struct listen_dnsport listen)

delete the listening structure

Parameters
listenlistening structure.

Referenced by listen_create(), and worker_delete().

◆ listening_ports_open()

struct listen_port* listening_ports_open ( struct config_file cfg,
int *  reuseport 
)

Create shared listening ports Getaddrinfo, create socket, bind and listen to zero or more interfaces for IP4 and/or IP6, for UDP and/or TCP.

On the given port number. It creates the sockets.

Parameters
cfgsettings on what ports to open.
reuseportset to true if you want reuseport, or NULL to not have it, set to false on exit if reuseport failed to apply (because of no kernel support).
Returns
: linked list of ports or NULL on error.

References config_file::dnscrypt_port, config_file::do_ip4, config_file::do_ip6, config_file::do_tcp, config_file::do_udp, config_file::if_automatic, config_file::ifs, config_file::incoming_num_tcp, config_file::ip_freebind, config_file::ip_transparent, listening_ports_free(), config_file::num_ifs, config_file::port, ports_create_if(), config_file::so_rcvbuf, config_file::so_sndbuf, config_file::ssl_port, str_is_ip6(), config_file::tcp_mss, and config_file::use_systemd.

Referenced by daemon_open_shared_ports().

◆ listen_get_mem()

size_t listen_get_mem ( struct listen_dnsport listen)

get memory size used by the listening structs

Parameters
listenlistening structure.
Returns
: size in bytes.

References listen_dnsport::base, listen_dnsport::cps, listen_list::next, sldns_buffer_capacity(), and listen_dnsport::udp_buff.

◆ listen_stop_accept()

void listen_stop_accept ( struct listen_dnsport listen)

stop accept handlers for TCP (until enabled again)

Parameters
listenlistening structure.

References listen_list::com, listen_dnsport::cps, listen_list::next, comm_point::tcp_free, and comm_point::type.

Referenced by worker_stop_accept().

◆ listen_start_accept()

void listen_start_accept ( struct listen_dnsport listen)

start accept handlers for TCP (was stopped before)

Parameters
listenlistening structure.

References listen_list::com, listen_dnsport::cps, listen_list::next, comm_point::tcp_free, and comm_point::type.

Referenced by worker_start_accept().