"Fossies" - the Fresh Open Source Software Archive

Member "nsd-4.3.7/nsd.h" (22 Jul 2021, 10927 Bytes) of package /linux/misc/dns/nsd-4.3.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nsd.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.3.6_vs_4.3.7.

    1 /*
    2  * nsd.h -- nsd(8) definitions and prototypes
    3  *
    4  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
    5  *
    6  * See LICENSE for the license.
    7  *
    8  */
    9 
   10 #ifndef _NSD_H_
   11 #define _NSD_H_
   12 
   13 #include <signal.h>
   14 #include <net/if.h>
   15 #ifndef IFNAMSIZ
   16 #  ifdef IF_NAMESIZE
   17 #    define IFNAMSIZ IF_NAMESIZE
   18 #  else
   19 #    define IFNAMSIZ 16
   20 #  endif
   21 #endif
   22 #ifdef HAVE_OPENSSL_SSL_H
   23 #include <openssl/ssl.h>
   24 #endif
   25 
   26 #include "dns.h"
   27 #include "edns.h"
   28 #include "bitset.h"
   29 struct netio_handler;
   30 struct nsd_options;
   31 struct udb_base;
   32 struct daemon_remote;
   33 #ifdef USE_DNSTAP
   34 struct dt_collector;
   35 #endif
   36 
   37 /* The NSD runtime states and NSD ipc command values */
   38 #define NSD_RUN 0
   39 #define NSD_RELOAD 1
   40 #define NSD_SHUTDOWN 2
   41 #define NSD_STATS 3
   42 #define NSD_REAP_CHILDREN 4
   43 #define NSD_QUIT 5
   44 /*
   45  * PASS_TO_XFRD is followed by the u16(len in network order) and
   46  * then network packet contents.  packet is a notify(acl checked), or
   47  * xfr reply from a master(acl checked).
   48  * followed by u32(acl number that matched from notify/xfr acl).
   49  */
   50 #define NSD_PASS_TO_XFRD 6
   51 /*
   52  * RELOAD_REQ is sent when parent receives a SIGHUP and tells
   53  * xfrd that it wants to initiate a reload (and thus task swap).
   54  */
   55 #define NSD_RELOAD_REQ 7
   56 /*
   57  * RELOAD_DONE is sent at the end of a reload pass.
   58  * xfrd then knows that reload phase is over.
   59  */
   60 #define NSD_RELOAD_DONE 8
   61 /*
   62  * QUIT_SYNC is sent to signify a synchronisation of ipc
   63  * channel content during reload
   64  */
   65 #define NSD_QUIT_SYNC 9
   66 /*
   67  * QUIT_WITH_STATS is sent during a reload when BIND8_STATS is defined,
   68  * from parent to children.  The stats are transferred too from child to
   69  * parent with this commandvalue, when the child is exiting.
   70  */
   71 #define NSD_QUIT_WITH_STATS 10
   72 /*
   73  * QUIT_CHILD is sent at exit, to make sure the child has exited so that
   74  * port53 is free when all of nsd's processes have exited at shutdown time
   75  */
   76 #define NSD_QUIT_CHILD 11
   77 
   78 #define NSD_SERVER_MAIN 0x0U
   79 #define NSD_SERVER_UDP  0x1U
   80 #define NSD_SERVER_TCP  0x2U
   81 #define NSD_SERVER_BOTH (NSD_SERVER_UDP | NSD_SERVER_TCP)
   82 
   83 #ifdef INET6
   84 #define DEFAULT_AI_FAMILY AF_UNSPEC
   85 #else
   86 #define DEFAULT_AI_FAMILY AF_INET
   87 #endif
   88 
   89 #ifdef BIND8_STATS
   90 /* Counter for statistics */
   91 typedef unsigned long stc_type;
   92 
   93 #define LASTELEM(arr)   (sizeof(arr) / sizeof(arr[0]) - 1)
   94 
   95 #define STATUP(nsd, stc) nsd->st.stc++
   96 /* #define  STATUP2(nsd, stc, i)  ((i) <= (LASTELEM(nsd->st.stc) - 1)) ? nsd->st.stc[(i)]++ : \
   97                 nsd->st.stc[LASTELEM(nsd->st.stc)]++ */
   98 
   99 #define STATUP2(nsd, stc, i) nsd->st.stc[(i) <= (LASTELEM(nsd->st.stc) - 1) ? i : LASTELEM(nsd->st.stc)]++
  100 #else   /* BIND8_STATS */
  101 
  102 #define STATUP(nsd, stc) /* Nothing */
  103 #define STATUP2(nsd, stc, i) /* Nothing */
  104 
  105 #endif /* BIND8_STATS */
  106 
  107 #ifdef USE_ZONE_STATS
  108 /* increment zone statistic, checks if zone-nonNULL and zone array bounds */
  109 #define ZTATUP(nsd, zone, stc) ( \
  110     (zone && zone->zonestatid < nsd->zonestatsizenow) ? \
  111         nsd->zonestatnow[zone->zonestatid].stc++ \
  112         : 0)
  113 #define ZTATUP2(nsd, zone, stc, i) ( \
  114     (zone && zone->zonestatid < nsd->zonestatsizenow) ? \
  115         (nsd->zonestatnow[zone->zonestatid].stc[(i) <= (LASTELEM(nsd->zonestatnow[zone->zonestatid].stc) - 1) ? i : LASTELEM(nsd->zonestatnow[zone->zonestatid].stc)]++ ) \
  116         : 0)
  117 #else /* USE_ZONE_STATS */
  118 #define ZTATUP(nsd, zone, stc) /* Nothing */
  119 #define ZTATUP2(nsd, zone, stc, i) /* Nothing */
  120 #endif /* USE_ZONE_STATS */
  121 
  122 #define NSD_SOCKET_IS_OPTIONAL (1<<0)
  123 #define NSD_BIND_DEVICE (1<<1)
  124 
  125 struct nsd_addrinfo
  126 {
  127     int ai_flags;
  128     int ai_family;
  129     int ai_socktype;
  130     socklen_t ai_addrlen;
  131     struct sockaddr_storage ai_addr;
  132 };
  133 
  134 struct nsd_socket
  135 {
  136     struct nsd_addrinfo addr;
  137     int s;
  138     int flags;
  139     struct nsd_bitset *servers;
  140     char device[IFNAMSIZ];
  141     int fib;
  142 };
  143 
  144 struct nsd_child
  145 {
  146 #ifdef HAVE_CPUSET_T
  147     /* Processor(s) that child process must run on (if applicable). */
  148     cpuset_t *cpuset;
  149 #endif
  150 
  151     /* The type of child process (UDP or TCP handler). */
  152     int kind;
  153 
  154     /* The child's process id.  */
  155     pid_t pid;
  156 
  157     /* child number in child array */
  158     int child_num;
  159 
  160     /*
  161      * Socket used by the parent process to send commands and
  162      * receive responses to/from this child process.
  163      */
  164     int child_fd;
  165 
  166     /*
  167      * Socket used by the child process to receive commands and
  168      * send responses from/to the parent process.
  169      */
  170     int parent_fd;
  171 
  172     /*
  173      * IPC info, buffered for nonblocking writes to the child
  174      */
  175     uint8_t need_to_send_STATS, need_to_send_QUIT;
  176     uint8_t need_to_exit, has_exited;
  177 
  178     /*
  179      * The handler for handling the commands from the child.
  180      */
  181     struct netio_handler* handler;
  182 
  183 #ifdef  BIND8_STATS
  184     stc_type query_count;
  185 #endif
  186 };
  187 
  188 #define NSD_COOKIE_HISTORY_SIZE 2
  189 #define NSD_COOKIE_SECRET_SIZE 16
  190 
  191 typedef struct cookie_secret cookie_secret_type;
  192 struct cookie_secret {
  193     /** cookie secret */
  194     uint8_t cookie_secret[NSD_COOKIE_SECRET_SIZE];
  195 };
  196 
  197 /* NSD configuration and run-time variables */
  198 typedef struct nsd nsd_type;
  199 struct  nsd
  200 {
  201     /*
  202      * Global region that is not deallocated until NSD shuts down.
  203      */
  204     region_type    *region;
  205 
  206     /* Run-time variables */
  207     pid_t       pid;
  208     volatile sig_atomic_t mode;
  209     volatile sig_atomic_t signal_hint_reload_hup;
  210     volatile sig_atomic_t signal_hint_reload;
  211     volatile sig_atomic_t signal_hint_child;
  212     volatile sig_atomic_t signal_hint_quit;
  213     volatile sig_atomic_t signal_hint_shutdown;
  214     volatile sig_atomic_t signal_hint_stats;
  215     volatile sig_atomic_t signal_hint_statsusr;
  216     volatile sig_atomic_t quit_sync_done;
  217     unsigned        server_kind;
  218     struct namedb   *db;
  219     int             debug;
  220 
  221     size_t            child_count;
  222     struct nsd_child *children;
  223     int restart_children;
  224     int reload_failed;
  225 
  226     /* NULL if this is the parent process. */
  227     struct nsd_child *this_child;
  228 
  229     /* mmaps with data exchange from xfrd and reload */
  230     struct udb_base* task[2];
  231     int mytask;
  232     /* the base used by this (child)process */
  233     struct event_base* event_base;
  234     /* the server_region used by this (child)process */
  235     region_type* server_region;
  236     struct netio_handler* xfrd_listener;
  237     struct daemon_remote* rc;
  238 
  239     /* Configuration */
  240     const char      *dbfile;
  241     const char      *pidfile;
  242     const char      *log_filename;
  243     const char      *username;
  244     uid_t           uid;
  245     gid_t           gid;
  246     const char      *chrootdir;
  247     const char      *version;
  248     const char      *identity;
  249     uint16_t        nsid_len;
  250     unsigned char       *nsid;
  251     uint8_t         file_rotation_ok;
  252 
  253 #ifdef HAVE_CPUSET_T
  254     int         use_cpu_affinity;
  255     cpuset_t*       cpuset;
  256     cpuset_t*       xfrd_cpuset;
  257 #endif
  258 
  259     /* number of interfaces */
  260     size_t  ifs;
  261     /* non0 if so_reuseport is in use, if so, tcp, udp array increased */
  262     int reuseport;
  263 
  264     /* TCP specific configuration (array size ifs) */
  265     struct nsd_socket* tcp;
  266 
  267     /* UDP specific configuration (array size ifs) */
  268     struct nsd_socket* udp;
  269 
  270     edns_data_type edns_ipv4;
  271 #if defined(INET6)
  272     edns_data_type edns_ipv6;
  273 #endif
  274 
  275     int maximum_tcp_count;
  276     int current_tcp_count;
  277     int tcp_query_count;
  278     int tcp_timeout;
  279     int tcp_mss;
  280     int outgoing_tcp_mss;
  281     size_t ipv4_edns_size;
  282     size_t ipv6_edns_size;
  283 
  284 #ifdef  BIND8_STATS
  285 
  286     struct nsdst {
  287         time_t  boot;
  288         int period;     /* Produce statistics dump every st_period seconds */
  289         stc_type qtype[257];    /* Counters per qtype */
  290         stc_type qclass[4]; /* Class IN or Class CH or other */
  291         stc_type qudp, qudp6;   /* Number of queries udp and udp6 */
  292         stc_type ctcp, ctcp6;   /* Number of tcp and tcp6 connections */
  293         stc_type ctls, ctls6;   /* Number of tls and tls6 connections */
  294         stc_type rcode[17], opcode[6]; /* Rcodes & opcodes */
  295         /* Dropped, truncated, queries for nonconfigured zone, tx errors */
  296         stc_type dropped, truncated, wrongzone, txerr, rxerr;
  297         stc_type edns, ednserr, raxfr, nona;
  298         uint64_t db_disk, db_mem;
  299     } st;
  300     /* per zone stats, each an array per zone-stat-idx, stats per zone is
  301      * add of [0][zoneidx] and [1][zoneidx]. */
  302     struct nsdst* zonestat[2];
  303     /* fd for zonestat mapping (otherwise mmaps cannot be shared between
  304      * processes and resized) */
  305     int zonestatfd[2];
  306     /* filenames */
  307     char* zonestatfname[2];
  308     /* size of the mmapped zone stat array (number of array entries) */
  309     size_t zonestatsize[2], zonestatdesired, zonestatsizenow;
  310     /* current zonestat array to use */
  311     struct nsdst* zonestatnow;
  312 #endif /* BIND8_STATS */
  313 #ifdef USE_DNSTAP
  314     /* the dnstap collector process info */
  315     struct dt_collector* dt_collector;
  316     /* the pipes from server processes to the dt_collector,
  317      * arrays of size child_count * 2.  Kept open for (re-)forks. */
  318     int *dt_collector_fd_send, *dt_collector_fd_recv;
  319     /* the pipes from server processes to the dt_collector. Initially
  320      * these point halfway into dt_collector_fd_send, but during reload
  321      * the pointer is swapped with dt_collector_fd_send in order to
  322      * to prevent writing to the dnstap collector by old serve childs
  323      * simultaneous with new serve childs. */
  324     int *dt_collector_fd_swap;
  325 #endif /* USE_DNSTAP */
  326     /* ratelimit for errors, time value */
  327     time_t err_limit_time;
  328     /* ratelimit for errors, packet count */
  329     unsigned int err_limit_count;
  330 
  331     /** do answer with server cookie when request contained cookie option */
  332     int do_answer_cookie;
  333 
  334     /** how many cookies are there in the cookies array */
  335     size_t cookie_count;
  336 
  337     /* keep track of the last `NSD_COOKIE_HISTORY_SIZE`
  338      * cookies as per rfc requirement .*/
  339     cookie_secret_type cookie_secrets[NSD_COOKIE_HISTORY_SIZE];
  340 
  341     struct nsd_options* options;
  342 
  343 #ifdef HAVE_SSL
  344     /* TLS specific configuration */
  345     SSL_CTX *tls_ctx;
  346 #endif
  347 };
  348 
  349 extern struct nsd nsd;
  350 
  351 /* nsd.c */
  352 pid_t readpid(const char *file);
  353 int writepid(struct nsd *nsd);
  354 void unlinkpid(const char* file);
  355 void sig_handler(int sig);
  356 void bind8_stats(struct nsd *nsd);
  357 
  358 /* server.c */
  359 int server_init(struct nsd *nsd);
  360 int server_prepare(struct nsd *nsd);
  361 void server_main(struct nsd *nsd);
  362 void server_child(struct nsd *nsd);
  363 void server_shutdown(struct nsd *nsd) ATTR_NORETURN;
  364 void server_close_all_sockets(struct nsd_socket sockets[], size_t n);
  365 const char* nsd_event_vs(void);
  366 const char* nsd_event_method(void);
  367 struct event_base* nsd_child_event_base(void);
  368 void service_remaining_tcp(struct nsd* nsd);
  369 /* extra domain numbers for temporary domains */
  370 #define EXTRA_DOMAIN_NUMBERS 1024
  371 #define SLOW_ACCEPT_TIMEOUT 2 /* in seconds */
  372 /* ratelimit for error responses */
  373 #define ERROR_RATELIMIT 100 /* qps */
  374 /* allocate zonestat structures */
  375 void server_zonestat_alloc(struct nsd* nsd);
  376 /* remap the mmaps for zonestat isx, to bytesize sz.  Caller has to set
  377  * the zonestatsize */
  378 void zonestat_remap(struct nsd* nsd, int idx, size_t sz);
  379 /* allocate and init xfrd variables */
  380 void server_prepare_xfrd(struct nsd *nsd);
  381 /* start xfrdaemon (again) */
  382 void server_start_xfrd(struct nsd *nsd, int del_db, int reload_active);
  383 /* send SOA serial numbers to xfrd */
  384 void server_send_soa_xfrd(struct nsd *nsd, int shortsoa);
  385 #ifdef HAVE_SSL
  386 SSL_CTX* server_tls_ctx_setup(char* key, char* pem, char* verifypem);
  387 SSL_CTX* server_tls_ctx_create(struct nsd *nsd, char* verifypem, char* ocspfile);
  388 void perform_openssl_init(void);
  389 #endif
  390 ssize_t block_read(struct nsd* nsd, int s, void* p, ssize_t sz, int timeout);
  391 
  392 #endif  /* _NSD_H_ */