"Fossies" - the Fresh Open Source Software Archive

Member "dnsmasq-2.85/src/dnsmasq.h" (7 Apr 2021, 52260 Bytes) of package /linux/misc/dns/dnsmasq-2.85.tar.xz:


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 "dnsmasq.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.84_vs_2.85.

    1 /* dnsmasq is Copyright (c) 2000-2021 Simon Kelley
    2  
    3    This program is free software; you can redistribute it and/or modify
    4    it under the terms of the GNU General Public License as published by
    5    the Free Software Foundation; version 2 dated June, 1991, or
    6    (at your option) version 3 dated 29 June, 2007.
    7  
    8    This program is distributed in the hope that it will be useful,
    9    but WITHOUT ANY WARRANTY; without even the implied warranty of
   10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11    GNU General Public License for more details.
   12      
   13    You should have received a copy of the GNU General Public License
   14    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   15 */
   16 
   17 #define COPYRIGHT "Copyright (c) 2000-2021 Simon Kelley"
   18 
   19 /* We do defines that influence behavior of stdio.h, so complain
   20    if included too early. */
   21 #ifdef _STDIO_H
   22 #  error "Header file stdio.h included too early!"
   23 #endif 
   24 
   25 #ifndef NO_LARGEFILE
   26 /* Ensure we can use files >2GB (log files may grow this big) */
   27 #  define _LARGEFILE_SOURCE 1
   28 #  define _FILE_OFFSET_BITS 64
   29 #endif
   30 
   31 /* Get linux C library versions and define _GNU_SOURCE for kFreeBSD. */
   32 #if defined(__linux__) || defined(__GLIBC__)
   33 #  ifndef __ANDROID__
   34 #      define _GNU_SOURCE
   35 #  endif
   36 #  include <features.h> 
   37 #endif
   38 
   39 /* Need these defined early */
   40 #if defined(__sun) || defined(__sun__)
   41 #  define _XPG4_2
   42 #  define __EXTENSIONS__
   43 #endif
   44 
   45 #if (defined(__GNUC__) && __GNUC__ >= 3) || defined(__clang__)
   46 #define ATTRIBUTE_NORETURN __attribute__ ((noreturn))
   47 #else
   48 #define ATTRIBUTE_NORETURN
   49 #endif
   50 
   51 /* get these before config.h  for IPv6 stuff... */
   52 #include <sys/types.h> 
   53 #include <sys/socket.h>
   54 
   55 #ifdef __APPLE__
   56 /* Define before netinet/in.h to select API. OSX Lion onwards. */
   57 #  define __APPLE_USE_RFC_3542
   58 #endif
   59 #include <netinet/in.h>
   60 
   61 /* Also needed before config.h. */
   62 #include <getopt.h>
   63 
   64 #include "config.h"
   65 #include "ip6addr.h"
   66 #include "metrics.h"
   67 
   68 typedef unsigned char u8;
   69 typedef unsigned short u16;
   70 typedef unsigned int u32;
   71 typedef unsigned long long u64;
   72 
   73 #define countof(x)      (long)(sizeof(x) / sizeof(x[0]))
   74 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
   75 
   76 #include "dns-protocol.h"
   77 #include "dhcp-protocol.h"
   78 #ifdef HAVE_DHCP6
   79 #include "dhcp6-protocol.h"
   80 #include "radv-protocol.h"
   81 #endif
   82 
   83 #define gettext_noop(S) (S)
   84 #ifndef LOCALEDIR
   85 #  define _(S) (S)
   86 #else
   87 #  include <libintl.h>
   88 #  include <locale.h>   
   89 #  define _(S) gettext(S)
   90 #endif
   91 
   92 #include <arpa/inet.h>
   93 #include <sys/stat.h>
   94 #include <sys/ioctl.h>
   95 #if defined(HAVE_SOLARIS_NETWORK)
   96 #  include <sys/sockio.h>
   97 #endif
   98 #include <poll.h>
   99 #include <sys/wait.h>
  100 #include <sys/time.h>
  101 #include <sys/un.h>
  102 #include <limits.h>
  103 #include <net/if.h>
  104 #if defined(HAVE_SOLARIS_NETWORK) && !defined(ifr_mtu)
  105 /* Some solaris net/if./h omit this. */
  106 #  define ifr_mtu  ifr_ifru.ifru_metric
  107 #endif
  108 #include <unistd.h>
  109 #include <stdio.h>
  110 #include <string.h>
  111 #include <stdlib.h>
  112 #include <fcntl.h>
  113 #include <ctype.h>
  114 #include <signal.h>
  115 #include <stddef.h>
  116 #include <time.h>
  117 #include <errno.h>
  118 #include <pwd.h>
  119 #include <grp.h>
  120 #include <stdarg.h>
  121 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sun__) || defined (__sun) || defined (__ANDROID__)
  122 #  include <netinet/if_ether.h>
  123 #else
  124 #  include <net/ethernet.h>
  125 #endif
  126 #include <net/if_arp.h>
  127 #include <netinet/in_systm.h>
  128 #include <netinet/ip.h>
  129 #include <netinet/ip6.h>
  130 #include <netinet/ip_icmp.h>
  131 #include <netinet/tcp.h>
  132 #include <sys/uio.h>
  133 #include <syslog.h>
  134 #include <dirent.h>
  135 #ifndef HAVE_LINUX_NETWORK
  136 #  include <net/if_dl.h>
  137 #endif
  138 
  139 #if defined(HAVE_LINUX_NETWORK)
  140 #include <linux/version.h>
  141 #include <linux/sockios.h>
  142 #include <linux/capability.h>
  143 /* There doesn't seem to be a universally-available 
  144    userspace header for these. */
  145 extern int capset(cap_user_header_t header, cap_user_data_t data);
  146 extern int capget(cap_user_header_t header, cap_user_data_t data);
  147 #define LINUX_CAPABILITY_VERSION_1  0x19980330
  148 #define LINUX_CAPABILITY_VERSION_2  0x20071026
  149 #define LINUX_CAPABILITY_VERSION_3  0x20080522
  150 
  151 #include <sys/prctl.h>
  152 #elif defined(HAVE_SOLARIS_NETWORK)
  153 #include <priv.h>
  154 #endif
  155 
  156 /* Backwards compat with 2.83 */
  157 #if defined(HAVE_NETTLEHASH)
  158 #  define HAVE_CRYPTOHASH
  159 #endif
  160 #if defined(HAVE_DNSSEC) || defined(HAVE_CRYPTOHASH)
  161 #  include <nettle/nettle-meta.h>
  162 #endif
  163 
  164 /* daemon is function in the C library.... */
  165 #define daemon dnsmasq_daemon
  166 
  167 #define ADDRSTRLEN INET6_ADDRSTRLEN
  168 
  169 /* Async event queue */
  170 struct event_desc {
  171   int event, data, msg_sz;
  172 };
  173 
  174 #define EVENT_RELOAD     1
  175 #define EVENT_DUMP       2
  176 #define EVENT_ALARM      3
  177 #define EVENT_TERM       4
  178 #define EVENT_CHILD      5
  179 #define EVENT_REOPEN     6
  180 #define EVENT_EXITED     7
  181 #define EVENT_KILLED     8
  182 #define EVENT_EXEC_ERR   9
  183 #define EVENT_PIPE_ERR   10
  184 #define EVENT_USER_ERR   11
  185 #define EVENT_CAP_ERR    12
  186 #define EVENT_PIDFILE    13
  187 #define EVENT_HUSER_ERR  14
  188 #define EVENT_GROUP_ERR  15
  189 #define EVENT_DIE        16
  190 #define EVENT_LOG_ERR    17
  191 #define EVENT_FORK_ERR   18
  192 #define EVENT_LUA_ERR    19
  193 #define EVENT_TFTP_ERR   20
  194 #define EVENT_INIT       21
  195 #define EVENT_NEWADDR    22
  196 #define EVENT_NEWROUTE   23
  197 #define EVENT_TIME_ERR   24
  198 #define EVENT_SCRIPT_LOG 25
  199 #define EVENT_TIME       26
  200 
  201 /* Exit codes. */
  202 #define EC_GOOD        0
  203 #define EC_BADCONF     1
  204 #define EC_BADNET      2
  205 #define EC_FILE        3
  206 #define EC_NOMEM       4
  207 #define EC_MISC        5
  208 #define EC_INIT_OFFSET 10
  209 
  210 #define OPT_BOGUSPRIV      0
  211 #define OPT_FILTER         1
  212 #define OPT_LOG            2
  213 #define OPT_SELFMX         3
  214 #define OPT_NO_HOSTS       4
  215 #define OPT_NO_POLL        5
  216 #define OPT_DEBUG          6
  217 #define OPT_ORDER          7
  218 #define OPT_NO_RESOLV      8
  219 #define OPT_EXPAND         9
  220 #define OPT_LOCALMX        10
  221 #define OPT_NO_NEG         11
  222 #define OPT_NODOTS_LOCAL   12
  223 #define OPT_NOWILD         13
  224 #define OPT_ETHERS         14
  225 #define OPT_RESOLV_DOMAIN  15
  226 #define OPT_NO_FORK        16
  227 #define OPT_AUTHORITATIVE  17
  228 #define OPT_LOCALISE       18
  229 #define OPT_DBUS           19
  230 #define OPT_DHCP_FQDN      20
  231 #define OPT_NO_PING        21
  232 #define OPT_LEASE_RO       22
  233 #define OPT_ALL_SERVERS    23
  234 #define OPT_RELOAD         24
  235 #define OPT_LOCAL_REBIND   25  
  236 #define OPT_TFTP_SECURE    26
  237 #define OPT_TFTP_NOBLOCK   27
  238 #define OPT_LOG_OPTS       28
  239 #define OPT_TFTP_APREF_IP  29
  240 #define OPT_NO_OVERRIDE    30
  241 #define OPT_NO_REBIND      31
  242 #define OPT_ADD_MAC        32
  243 #define OPT_DNSSEC_PROXY   33
  244 #define OPT_CONSEC_ADDR    34
  245 #define OPT_CONNTRACK      35
  246 #define OPT_FQDN_UPDATE    36
  247 #define OPT_RA             37
  248 #define OPT_TFTP_LC        38
  249 #define OPT_CLEVERBIND     39
  250 #define OPT_TFTP           40
  251 #define OPT_CLIENT_SUBNET  41
  252 #define OPT_QUIET_DHCP     42
  253 #define OPT_QUIET_DHCP6    43
  254 #define OPT_QUIET_RA       44
  255 #define OPT_DNSSEC_VALID   45
  256 #define OPT_DNSSEC_TIME    46
  257 #define OPT_DNSSEC_DEBUG   47
  258 #define OPT_DNSSEC_IGN_NS  48 
  259 #define OPT_LOCAL_SERVICE  49
  260 #define OPT_LOOP_DETECT    50
  261 #define OPT_EXTRALOG       51
  262 #define OPT_TFTP_NO_FAIL   52
  263 #define OPT_SCRIPT_ARP     53
  264 #define OPT_MAC_B64        54
  265 #define OPT_MAC_HEX        55
  266 #define OPT_TFTP_APREF_MAC 56
  267 #define OPT_RAPID_COMMIT   57
  268 #define OPT_UBUS           58
  269 #define OPT_IGNORE_CLID    59
  270 #define OPT_SINGLE_PORT    60
  271 #define OPT_LEASE_RENEW    61
  272 #define OPT_LOG_DEBUG      62
  273 #define OPT_LAST           63
  274 
  275 #define OPTION_BITS (sizeof(unsigned int)*8)
  276 #define OPTION_SIZE ( (OPT_LAST/OPTION_BITS)+((OPT_LAST%OPTION_BITS)!=0) )
  277 #define option_var(x) (daemon->options[(x) / OPTION_BITS])
  278 #define option_val(x) ((1u) << ((x) % OPTION_BITS))
  279 #define option_bool(x) (option_var(x) & option_val(x))
  280 
  281 /* extra flags for my_syslog, we use facilities since they are known 
  282    not to occupy the same bits as priorities, no matter how syslog.h is set up. 
  283    MS_DEBUG messages are suppressed unless --log-debug is set. */
  284 #define MS_TFTP   LOG_USER
  285 #define MS_DHCP   LOG_DAEMON
  286 #define MS_SCRIPT LOG_MAIL
  287 #define MS_DEBUG  LOG_NEWS
  288 
  289 /* Note that this is used widely as a container for IPv4/IPv6 addresses,
  290    so for that reason, was well as to avoid wasting memory in almost every
  291    cache entry, the other variants should not be larger than
  292    sizeof(struct in6_addr) - 16 bytes.
  293 */
  294 union all_addr {
  295   struct in_addr addr4;
  296   struct in6_addr addr6;
  297   struct {
  298     union {
  299       struct crec *cache;
  300       char *name;
  301     } target;
  302     unsigned int uid;
  303     int is_name_ptr;  /* disciminates target union */
  304   } cname;
  305   struct {
  306     struct blockdata *keydata;
  307     unsigned short keylen, flags, keytag;
  308     unsigned char algo;
  309   } key; 
  310   struct {
  311     struct blockdata *keydata;
  312     unsigned short keylen, keytag;
  313     unsigned char algo;
  314     unsigned char digest; 
  315   } ds;
  316   struct {
  317     struct blockdata *target;
  318     unsigned short targetlen, srvport, priority, weight;
  319   } srv;
  320   /* for log_query */
  321   struct {
  322     unsigned short keytag, algo, digest, rcode;
  323   } log;
  324 };
  325 
  326 
  327 struct bogus_addr {
  328   struct in_addr addr, mask;
  329   struct bogus_addr *next;
  330 };
  331 
  332 /* dns doctor param */
  333 struct doctor {
  334   struct in_addr in, end, out, mask;
  335   struct doctor *next;
  336 };
  337 
  338 struct mx_srv_record {
  339   char *name, *target;
  340   int issrv, srvport, priority, weight;
  341   unsigned int offset;
  342   struct mx_srv_record *next;
  343 };
  344 
  345 struct naptr {
  346   char *name, *replace, *regexp, *services, *flags;
  347   unsigned int order, pref;
  348   struct naptr *next;
  349 };
  350 
  351 #ifndef NO_ID
  352 #define TXT_STAT_CACHESIZE     1
  353 #define TXT_STAT_INSERTS       2
  354 #define TXT_STAT_EVICTIONS     3
  355 #define TXT_STAT_MISSES        4
  356 #define TXT_STAT_HITS          5
  357 #define TXT_STAT_AUTH          6
  358 #define TXT_STAT_SERVERS       7
  359 #endif
  360 
  361 struct txt_record {
  362   char *name;
  363   unsigned char *txt;
  364   unsigned short class, len;
  365   int stat;
  366   struct txt_record *next;
  367 };
  368 
  369 struct ptr_record {
  370   char *name, *ptr;
  371   struct ptr_record *next;
  372 };
  373 
  374 struct cname {
  375   int ttl, flag;
  376   char *alias, *target;
  377   struct cname *next, *targetp;
  378 }; 
  379 
  380 struct ds_config {
  381   char *name, *digest;
  382   int digestlen, class, algo, keytag, digest_type;
  383   struct ds_config *next;
  384 };
  385 
  386 #define ADDRLIST_LITERAL  1
  387 #define ADDRLIST_IPV6     2
  388 #define ADDRLIST_REVONLY  4
  389 #define ADDRLIST_PREFIX   8
  390 #define ADDRLIST_WILDCARD 16
  391 #define ADDRLIST_DECLINED 32
  392 
  393 struct addrlist {
  394   union all_addr addr;
  395   int flags, prefixlen;
  396   time_t decline_time;
  397   struct addrlist *next;
  398 };
  399 
  400 #define AUTH6     1
  401 #define AUTH4     2
  402 
  403 struct auth_zone {
  404   char *domain;
  405   struct auth_name_list {
  406     char *name;
  407     int flags;
  408     struct auth_name_list *next;
  409   } *interface_names;
  410   struct addrlist *subnet;
  411   struct addrlist *exclude;
  412   struct auth_zone *next;
  413 };
  414 
  415 #define HR_6 1
  416 #define HR_4 2
  417 
  418 struct host_record {
  419   int ttl, flags;
  420   struct name_list {
  421     char *name;
  422     struct name_list *next;
  423   } *names;
  424   struct in_addr addr;
  425   struct in6_addr addr6;
  426   struct host_record *next;
  427 };
  428 
  429 #define IN4  1
  430 #define IN6  2
  431 #define INP4 4
  432 #define INP6 8
  433 
  434 struct interface_name {
  435   char *name; /* domain name */
  436   char *intr; /* interface name */
  437   int flags;
  438   struct in_addr proto4;
  439   struct in6_addr proto6;
  440   struct addrlist *addr;
  441   struct interface_name *next;
  442 };
  443 
  444 union bigname {
  445   char name[MAXDNAME];
  446   union bigname *next; /* freelist */
  447 };
  448 
  449 struct blockdata {
  450   struct blockdata *next;
  451   unsigned char key[KEYBLOCK_LEN];
  452 };
  453 
  454 struct crec { 
  455   struct crec *next, *prev, *hash_next;
  456   union all_addr addr;
  457   time_t ttd; /* time to die */
  458   /* used as class if DNSKEY/DS, index to source for F_HOSTS */
  459   unsigned int uid; 
  460   unsigned int flags;
  461   union {
  462     char sname[SMALLDNAME];
  463     union bigname *bname;
  464     char *namep;
  465   } name;
  466 };
  467 
  468 #define SIZEOF_BARE_CREC (sizeof(struct crec) - SMALLDNAME)
  469 #define SIZEOF_POINTER_CREC (sizeof(struct crec) + sizeof(char *) - SMALLDNAME)
  470 
  471 #define F_IMMORTAL  (1u<<0)
  472 #define F_NAMEP     (1u<<1)
  473 #define F_REVERSE   (1u<<2)
  474 #define F_FORWARD   (1u<<3)
  475 #define F_DHCP      (1u<<4)
  476 #define F_NEG       (1u<<5)       
  477 #define F_HOSTS     (1u<<6)
  478 #define F_IPV4      (1u<<7)
  479 #define F_IPV6      (1u<<8)
  480 #define F_BIGNAME   (1u<<9)
  481 #define F_NXDOMAIN  (1u<<10)
  482 #define F_CNAME     (1u<<11)
  483 #define F_DNSKEY    (1u<<12)
  484 #define F_CONFIG    (1u<<13)
  485 #define F_DS        (1u<<14)
  486 #define F_DNSSECOK  (1u<<15)
  487 #define F_UPSTREAM  (1u<<16)
  488 #define F_RRNAME    (1u<<17)
  489 #define F_SERVER    (1u<<18)
  490 #define F_QUERY     (1u<<19)
  491 #define F_NOERR     (1u<<20)
  492 #define F_AUTH      (1u<<21)
  493 #define F_DNSSEC    (1u<<22)
  494 #define F_KEYTAG    (1u<<23)
  495 #define F_SECSTAT   (1u<<24)
  496 #define F_NO_RR     (1u<<25)
  497 #define F_IPSET     (1u<<26)
  498 #define F_NOEXTRA   (1u<<27)
  499 #define F_SERVFAIL  (1u<<28) /* currently unused. */
  500 #define F_RCODE     (1u<<29)
  501 #define F_SRV       (1u<<30)
  502 
  503 #define UID_NONE      0
  504 /* Values of uid in crecs with F_CONFIG bit set. */
  505 #define SRC_CONFIG    1
  506 #define SRC_HOSTS     2
  507 #define SRC_AH        3
  508 
  509 
  510 /* struct sockaddr is not large enough to hold any address,
  511    and specifically not big enough to hold an IPv6 address.
  512    Blech. Roll our own. */
  513 union mysockaddr {
  514   struct sockaddr sa;
  515   struct sockaddr_in in;
  516   struct sockaddr_in6 in6;
  517 };
  518 
  519 /* bits in flag param to IPv6 callbacks from iface_enumerate() */
  520 #define IFACE_TENTATIVE   1
  521 #define IFACE_DEPRECATED  2
  522 #define IFACE_PERMANENT   4
  523 
  524 
  525 #define SERV_FROM_RESOLV       1  /* 1 for servers from resolv, 0 for command line. */
  526 #define SERV_NO_ADDR           2  /* no server, this domain is local only */
  527 #define SERV_LITERAL_ADDRESS   4  /* addr is the answer, not the server */ 
  528 #define SERV_HAS_DOMAIN        8  /* server for one domain only */
  529 #define SERV_HAS_SOURCE       16  /* source address defined */
  530 #define SERV_FOR_NODOTS       32  /* server for names with no domain part only */
  531 #define SERV_WARNED_RECURSIVE 64  /* avoid warning spam */
  532 #define SERV_FROM_DBUS       128  /* 1 if source is DBus */
  533 #define SERV_MARK            256  /* for mark-and-delete */
  534 #define SERV_TYPE    (SERV_HAS_DOMAIN | SERV_FOR_NODOTS)
  535 #define SERV_COUNTED         512  /* workspace for log code */
  536 #define SERV_USE_RESOLV     1024  /* forward this domain in the normal way */
  537 #define SERV_NO_REBIND      2048  /* inhibit dns-rebind protection */
  538 #define SERV_FROM_FILE      4096  /* read from --servers-file */
  539 #define SERV_LOOP           8192  /* server causes forwarding loop */
  540 #define SERV_DO_DNSSEC     16384  /* Validate DNSSEC when using this server */
  541 #define SERV_GOT_TCP       32768  /* Got some data from the TCP connection */
  542 
  543 struct serverfd {
  544   int fd;
  545   union mysockaddr source_addr;
  546   char interface[IF_NAMESIZE+1];
  547   unsigned int ifindex, used, preallocated;
  548   struct serverfd *next;
  549 };
  550 
  551 struct randfd {
  552   struct server *serv;
  553   int fd;
  554   unsigned short refcount; /* refcount == 0xffff means overflow record. */
  555 };
  556 
  557 struct randfd_list {
  558   struct randfd *rfd;
  559   struct randfd_list *next;
  560 };
  561 
  562 struct server {
  563   union mysockaddr addr, source_addr;
  564   char interface[IF_NAMESIZE+1];
  565   unsigned int ifindex; /* corresponding to interface, above */
  566   struct serverfd *sfd; 
  567   char *domain; /* set if this server only handles a domain. */ 
  568   int flags, tcpfd, edns_pktsz;
  569   time_t pktsz_reduced;
  570   unsigned int queries, failed_queries;
  571 #ifdef HAVE_LOOP
  572   u32 uid;
  573 #endif
  574   struct server *next; 
  575 };
  576 
  577 struct ipsets {
  578   char **sets;
  579   char *domain;
  580   struct ipsets *next;
  581 };
  582 
  583 struct irec {
  584   union mysockaddr addr;
  585   struct in_addr netmask; /* only valid for IPv4 */
  586   int tftp_ok, dhcp_ok, mtu, done, warned, dad, dns_auth, index, multicast_done, found, label;
  587   char *name; 
  588   struct irec *next;
  589 };
  590 
  591 struct listener {
  592   int fd, tcpfd, tftpfd, used;
  593   union mysockaddr addr;
  594   struct irec *iface; /* only sometimes valid for non-wildcard */
  595   struct listener *next;
  596 };
  597 
  598 /* interface and address parms from command line. */
  599 struct iname {
  600   char *name;
  601   union mysockaddr addr;
  602   int used;
  603   struct iname *next;
  604 };
  605 
  606 /* subnet parameters from command line */
  607 struct mysubnet {
  608   union mysockaddr addr;
  609   int addr_used;
  610   int mask;
  611 };
  612 
  613 /* resolv-file parms from command-line */
  614 struct resolvc {
  615   struct resolvc *next;
  616   int is_default, logged;
  617   time_t mtime;
  618   char *name;
  619 #ifdef HAVE_INOTIFY
  620   int wd; /* inotify watch descriptor */
  621   char *file; /* pointer to file part if path */
  622 #endif
  623 };
  624 
  625 /* adn-hosts parms from command-line (also dhcp-hostsfile and dhcp-optsfile and dhcp-hostsdir*/
  626 #define AH_DIR      1
  627 #define AH_INACTIVE 2
  628 #define AH_WD_DONE  4
  629 #define AH_HOSTS    8
  630 #define AH_DHCP_HST 16
  631 #define AH_DHCP_OPT 32
  632 struct hostsfile {
  633   struct hostsfile *next;
  634   int flags;
  635   char *fname;
  636 #ifdef HAVE_INOTIFY
  637   int wd; /* inotify watch descriptor */
  638 #endif
  639   unsigned int index; /* matches to cache entries for logging */
  640 };
  641 
  642 /* packet-dump flags */
  643 #define DUMP_QUERY     0x0001
  644 #define DUMP_REPLY     0x0002
  645 #define DUMP_UP_QUERY  0x0004
  646 #define DUMP_UP_REPLY  0x0008
  647 #define DUMP_SEC_QUERY 0x0010
  648 #define DUMP_SEC_REPLY 0x0020
  649 #define DUMP_BOGUS     0x0040
  650 #define DUMP_SEC_BOGUS 0x0080
  651 
  652 
  653 /* DNSSEC status values. */
  654 #define STAT_SECURE             1
  655 #define STAT_INSECURE           2
  656 #define STAT_BOGUS              3
  657 #define STAT_NEED_DS            4
  658 #define STAT_NEED_KEY           5
  659 #define STAT_TRUNCATED          6
  660 #define STAT_SECURE_WILDCARD    7
  661 #define STAT_OK                 8
  662 #define STAT_ABANDONED          9
  663 
  664 #define FREC_NOREBIND           1
  665 #define FREC_CHECKING_DISABLED  2
  666 #define FREC_HAS_SUBNET         4
  667 #define FREC_DNSKEY_QUERY       8
  668 #define FREC_DS_QUERY          16
  669 #define FREC_AD_QUESTION       32
  670 #define FREC_DO_QUESTION       64
  671 #define FREC_ADDED_PHEADER    128
  672 #define FREC_TEST_PKTSZ       256
  673 #define FREC_HAS_EXTRADATA    512
  674 #define FREC_HAS_PHEADER     1024
  675 #define FREC_NO_CACHE        2048
  676 
  677 #define HASH_SIZE 32 /* SHA-256 digest size */
  678 
  679 struct frec {
  680   struct frec_src {
  681     union mysockaddr source;
  682     union all_addr dest;
  683     unsigned int iface, log_id;
  684     int fd;
  685     unsigned short orig_id;
  686     struct frec_src *next;
  687   } frec_src;
  688   struct server *sentto; /* NULL means free */
  689   struct randfd_list *rfds;
  690   unsigned short new_id;
  691   int forwardall, flags;
  692   time_t time;
  693   unsigned char *hash[HASH_SIZE];
  694 #ifdef HAVE_DNSSEC 
  695   int class, work_counter;
  696   struct blockdata *stash; /* Saved reply, whilst we validate */
  697   size_t stash_len;
  698   struct frec *dependent; /* Query awaiting internally-generated DNSKEY or DS query */
  699   struct frec *blocking_query; /* Query which is blocking us. */
  700 #endif
  701   struct frec *next;
  702 };
  703 
  704 /* flags in top of length field for DHCP-option tables */
  705 #define OT_ADDR_LIST    0x8000
  706 #define OT_RFC1035_NAME 0x4000
  707 #define OT_INTERNAL     0x2000
  708 #define OT_NAME         0x1000
  709 #define OT_CSTRING      0x0800
  710 #define OT_DEC          0x0400 
  711 #define OT_TIME         0x0200
  712 
  713 /* actions in the daemon->helper RPC */
  714 #define ACTION_DEL           1
  715 #define ACTION_OLD_HOSTNAME  2
  716 #define ACTION_OLD           3
  717 #define ACTION_ADD           4
  718 #define ACTION_TFTP          5
  719 #define ACTION_ARP           6
  720 #define ACTION_ARP_DEL       7
  721 
  722 #define LEASE_NEW            1  /* newly created */
  723 #define LEASE_CHANGED        2  /* modified */
  724 #define LEASE_AUX_CHANGED    4  /* CLID or expiry changed */
  725 #define LEASE_AUTH_NAME      8  /* hostname came from config, not from client */
  726 #define LEASE_USED          16  /* used this DHCPv6 transaction */
  727 #define LEASE_NA            32  /* IPv6 no-temporary lease */
  728 #define LEASE_TA            64  /* IPv6 temporary lease */
  729 #define LEASE_HAVE_HWADDR  128  /* Have set hwaddress */
  730 #define LEASE_EXP_CHANGED  256  /* Lease expiry time changed */
  731 
  732 struct dhcp_lease {
  733   int clid_len;          /* length of client identifier */
  734   unsigned char *clid;   /* clientid */
  735   char *hostname, *fqdn; /* name from client-hostname option or config */
  736   char *old_hostname;    /* hostname before it moved to another lease */
  737   int flags;
  738   time_t expires;        /* lease expiry */
  739 #ifdef HAVE_BROKEN_RTC
  740   unsigned int length;
  741 #endif
  742   int hwaddr_len, hwaddr_type;
  743   unsigned char hwaddr[DHCP_CHADDR_MAX]; 
  744   struct in_addr addr, override, giaddr;
  745   unsigned char *extradata;
  746   unsigned int extradata_len, extradata_size;
  747   int last_interface;
  748   int new_interface;     /* save possible originated interface */
  749   int new_prefixlen;     /* and its prefix length */
  750 #ifdef HAVE_DHCP6
  751   struct in6_addr addr6;
  752   unsigned int iaid;
  753   struct slaac_address {
  754     struct in6_addr addr;
  755     time_t ping_time;
  756     int backoff; /* zero -> confirmed */
  757     struct slaac_address *next;
  758   } *slaac_address;
  759   int vendorclass_count;
  760 #endif
  761   struct dhcp_lease *next;
  762 };
  763 
  764 struct dhcp_netid {
  765   char *net;
  766   struct dhcp_netid *next;
  767 };
  768 
  769 struct dhcp_netid_list {
  770   struct dhcp_netid *list;
  771   struct dhcp_netid_list *next;
  772 };
  773 
  774 struct tag_if {
  775   struct dhcp_netid_list *set;
  776   struct dhcp_netid *tag;
  777   struct tag_if *next;
  778 };
  779 
  780 struct delay_config {
  781   int delay;
  782   struct dhcp_netid *netid;
  783   struct delay_config *next;
  784 };
  785 
  786 struct hwaddr_config {
  787   int hwaddr_len, hwaddr_type;
  788   unsigned char hwaddr[DHCP_CHADDR_MAX];
  789   unsigned int wildcard_mask;
  790   struct hwaddr_config *next;
  791 };
  792 
  793 struct dhcp_config {
  794   unsigned int flags;
  795   int clid_len;          /* length of client identifier */
  796   unsigned char *clid;   /* clientid */
  797   char *hostname, *domain;
  798   struct dhcp_netid_list *netid;
  799   struct dhcp_netid *filter;
  800 #ifdef HAVE_DHCP6
  801   struct addrlist *addr6;
  802 #endif
  803   struct in_addr addr;
  804   time_t decline_time;
  805   unsigned int lease_time;
  806   struct hwaddr_config *hwaddr;
  807   struct dhcp_config *next;
  808 };
  809 
  810 #define have_config(config, mask) ((config) && ((config)->flags & (mask))) 
  811 
  812 #define CONFIG_DISABLE           1
  813 #define CONFIG_CLID              2
  814 #define CONFIG_TIME              8
  815 #define CONFIG_NAME             16
  816 #define CONFIG_ADDR             32
  817 #define CONFIG_NOCLID          128
  818 #define CONFIG_FROM_ETHERS     256    /* entry created by /etc/ethers */
  819 #define CONFIG_ADDR_HOSTS      512    /* address added by from /etc/hosts */
  820 #define CONFIG_DECLINED       1024    /* address declined by client */
  821 #define CONFIG_BANK           2048    /* from dhcp hosts file */
  822 #define CONFIG_ADDR6          4096
  823 #define CONFIG_ADDR6_HOSTS   16384    /* address added by from /etc/hosts */
  824 
  825 struct dhcp_opt {
  826   int opt, len, flags;
  827   union {
  828     int encap;
  829     unsigned int wildcard_mask;
  830     unsigned char *vendor_class;
  831   } u;
  832   unsigned char *val;
  833   struct dhcp_netid *netid;
  834   struct dhcp_opt *next;
  835 };
  836 
  837 #define DHOPT_ADDR               1
  838 #define DHOPT_STRING             2
  839 #define DHOPT_ENCAPSULATE        4
  840 #define DHOPT_ENCAP_MATCH        8
  841 #define DHOPT_FORCE             16
  842 #define DHOPT_BANK              32
  843 #define DHOPT_ENCAP_DONE        64
  844 #define DHOPT_MATCH            128
  845 #define DHOPT_VENDOR           256
  846 #define DHOPT_HEX              512
  847 #define DHOPT_VENDOR_MATCH    1024
  848 #define DHOPT_RFC3925         2048
  849 #define DHOPT_TAGOK           4096
  850 #define DHOPT_ADDR6           8192
  851 #define DHOPT_VENDOR_PXE     16384
  852 
  853 struct dhcp_boot {
  854   char *file, *sname, *tftp_sname;
  855   struct in_addr next_server;
  856   struct dhcp_netid *netid;
  857   struct dhcp_boot *next;
  858 };
  859 
  860 struct dhcp_match_name {
  861   char *name;
  862   int wildcard;
  863   struct dhcp_netid *netid;
  864   struct dhcp_match_name *next;
  865 };
  866 
  867 struct pxe_service {
  868   unsigned short CSA, type; 
  869   char *menu, *basename, *sname;
  870   struct in_addr server;
  871   struct dhcp_netid *netid;
  872   struct pxe_service *next;
  873 };
  874 
  875 #define DHCP_PXE_DEF_VENDOR      "PXEClient"
  876 
  877 #define MATCH_VENDOR     1
  878 #define MATCH_USER       2
  879 #define MATCH_CIRCUIT    3
  880 #define MATCH_REMOTE     4
  881 #define MATCH_SUBSCRIBER 5
  882 
  883 /* vendorclass, userclass, remote-id or circuit-id */
  884 struct dhcp_vendor {
  885   int len, match_type;
  886   unsigned int enterprise;
  887   char *data;
  888   struct dhcp_netid netid;
  889   struct dhcp_vendor *next;
  890 };
  891 
  892 struct dhcp_pxe_vendor {
  893   char *data;
  894   struct dhcp_pxe_vendor *next;
  895 };
  896 
  897 struct dhcp_mac {
  898   unsigned int mask;
  899   int hwaddr_len, hwaddr_type;
  900   unsigned char hwaddr[DHCP_CHADDR_MAX];
  901   struct dhcp_netid netid;
  902   struct dhcp_mac *next;
  903 };
  904 
  905 struct dhcp_bridge {
  906   char iface[IF_NAMESIZE];
  907   struct dhcp_bridge *alias, *next;
  908 };
  909 
  910 struct cond_domain {
  911   char *domain, *prefix;
  912   struct in_addr start, end;
  913   struct in6_addr start6, end6;
  914   int is6, indexed;
  915   struct cond_domain *next;
  916 }; 
  917 
  918 struct ra_interface {
  919   char *name;
  920   char *mtu_name;
  921   int interval, lifetime, prio, mtu;
  922   struct ra_interface *next;
  923 };
  924 
  925 struct dhcp_context {
  926   unsigned int lease_time, addr_epoch;
  927   struct in_addr netmask, broadcast;
  928   struct in_addr local, router;
  929   struct in_addr start, end; /* range of available addresses */
  930 #ifdef HAVE_DHCP6
  931   struct in6_addr start6, end6; /* range of available addresses */
  932   struct in6_addr local6;
  933   int prefix, if_index;
  934   unsigned int valid, preferred, saved_valid;
  935   time_t ra_time, ra_short_period_start, address_lost_time;
  936   char *template_interface;
  937 #endif
  938   int flags;
  939   struct dhcp_netid netid, *filter;
  940   struct dhcp_context *next, *current;
  941 };
  942 
  943 struct shared_network {
  944   int if_index;
  945   struct in_addr match_addr, shared_addr;
  946 #ifdef HAVE_DHCP6
  947   /* shared_addr == 0 for IP6 entries. */
  948   struct in6_addr match_addr6, shared_addr6;
  949 #endif
  950   struct shared_network *next;
  951 };
  952 
  953 #define CONTEXT_STATIC         (1u<<0)
  954 #define CONTEXT_NETMASK        (1u<<1)
  955 #define CONTEXT_BRDCAST        (1u<<2)
  956 #define CONTEXT_PROXY          (1u<<3)
  957 #define CONTEXT_RA_ROUTER      (1u<<4)
  958 #define CONTEXT_RA_DONE        (1u<<5)
  959 #define CONTEXT_RA_NAME        (1u<<6)
  960 #define CONTEXT_RA_STATELESS   (1u<<7)
  961 #define CONTEXT_DHCP           (1u<<8)
  962 #define CONTEXT_DEPRECATE      (1u<<9)
  963 #define CONTEXT_TEMPLATE       (1u<<10)    /* create contexts using addresses */
  964 #define CONTEXT_CONSTRUCTED    (1u<<11)
  965 #define CONTEXT_GC             (1u<<12)
  966 #define CONTEXT_RA             (1u<<13)
  967 #define CONTEXT_CONF_USED      (1u<<14)
  968 #define CONTEXT_USED           (1u<<15)
  969 #define CONTEXT_OLD            (1u<<16)
  970 #define CONTEXT_V6             (1u<<17)
  971 #define CONTEXT_RA_OFF_LINK    (1u<<18)
  972 #define CONTEXT_SETLEASE       (1u<<19)
  973 
  974 struct ping_result {
  975   struct in_addr addr;
  976   time_t time;
  977   unsigned int hash;
  978   struct ping_result *next;
  979 };
  980 
  981 struct tftp_file {
  982   int refcount, fd;
  983   off_t size;
  984   dev_t dev;
  985   ino_t inode;
  986   char filename[];
  987 };
  988 
  989 struct tftp_transfer {
  990   int sockfd;
  991   time_t timeout;
  992   int backoff;
  993   unsigned int block, blocksize, expansion;
  994   off_t offset;
  995   union mysockaddr peer;
  996   union all_addr source;
  997   int if_index;
  998   char opt_blocksize, opt_transize, netascii, carrylf;
  999   struct tftp_file *file;
 1000   struct tftp_transfer *next;
 1001 };
 1002 
 1003 struct addr_list {
 1004   struct in_addr addr;
 1005   struct addr_list *next;
 1006 };
 1007 
 1008 struct tftp_prefix {
 1009   char *interface;
 1010   char *prefix;
 1011   int missing;
 1012   struct tftp_prefix *next;
 1013 };
 1014 
 1015 struct dhcp_relay {
 1016   union all_addr local, server;
 1017   char *interface; /* Allowable interface for replies from server, and dest for IPv6 multicast */
 1018   int iface_index; /* working - interface in which requests arrived, for return */
 1019   struct dhcp_relay *current, *next;
 1020 };
 1021 
 1022 extern struct daemon {
 1023   /* datastuctures representing the command-line and 
 1024      config file arguments. All set (including defaults)
 1025      in option.c */
 1026 
 1027   unsigned int options[OPTION_SIZE];
 1028   struct resolvc default_resolv, *resolv_files;
 1029   time_t last_resolv;
 1030   char *servers_file;
 1031   struct mx_srv_record *mxnames;
 1032   struct naptr *naptr;
 1033   struct txt_record *txt, *rr;
 1034   struct ptr_record *ptr;
 1035   struct host_record *host_records, *host_records_tail;
 1036   struct cname *cnames;
 1037   struct auth_zone *auth_zones;
 1038   struct interface_name *int_names;
 1039   char *mxtarget;
 1040   struct mysubnet *add_subnet4;
 1041   struct mysubnet *add_subnet6;
 1042   char *lease_file;
 1043   char *username, *groupname, *scriptuser;
 1044   char *luascript;
 1045   char *authserver, *hostmaster;
 1046   struct iname *authinterface;
 1047   struct name_list *secondary_forward_server;
 1048   int group_set, osport;
 1049   char *domain_suffix;
 1050   struct cond_domain *cond_domain, *synth_domains;
 1051   char *runfile; 
 1052   char *lease_change_command;
 1053   struct iname *if_names, *if_addrs, *if_except, *dhcp_except, *auth_peers, *tftp_interfaces;
 1054   struct bogus_addr *bogus_addr, *ignore_addr;
 1055   struct server *servers;
 1056   struct ipsets *ipsets;
 1057   int log_fac; /* log facility */
 1058   char *log_file; /* optional log file */
 1059   int max_logs;  /* queue limit */
 1060   int cachesize, ftabsize;
 1061   int port, query_port, min_port, max_port;
 1062   unsigned long local_ttl, neg_ttl, max_ttl, min_cache_ttl, max_cache_ttl, auth_ttl, dhcp_ttl, use_dhcp_ttl;
 1063   char *dns_client_id;
 1064   struct hostsfile *addn_hosts;
 1065   struct dhcp_context *dhcp, *dhcp6;
 1066   struct ra_interface *ra_interfaces;
 1067   struct dhcp_config *dhcp_conf;
 1068   struct dhcp_opt *dhcp_opts, *dhcp_match, *dhcp_opts6, *dhcp_match6;
 1069   struct dhcp_match_name *dhcp_name_match;
 1070   struct dhcp_pxe_vendor *dhcp_pxe_vendors;
 1071   struct dhcp_vendor *dhcp_vendors;
 1072   struct dhcp_mac *dhcp_macs;
 1073   struct dhcp_boot *boot_config;
 1074   struct pxe_service *pxe_services;
 1075   struct tag_if *tag_if; 
 1076   struct addr_list *override_relays;
 1077   struct dhcp_relay *relay4, *relay6;
 1078   struct delay_config *delay_conf;
 1079   int override;
 1080   int enable_pxe;
 1081   int doing_ra, doing_dhcp6;
 1082   struct dhcp_netid_list *dhcp_ignore, *dhcp_ignore_names, *dhcp_gen_names; 
 1083   struct dhcp_netid_list *force_broadcast, *bootp_dynamic;
 1084   struct hostsfile *dhcp_hosts_file, *dhcp_opts_file, *dynamic_dirs;
 1085   int dhcp_max, tftp_max, tftp_mtu;
 1086   int dhcp_server_port, dhcp_client_port;
 1087   int start_tftp_port, end_tftp_port; 
 1088   unsigned int min_leasetime;
 1089   struct doctor *doctors;
 1090   unsigned short edns_pktsz;
 1091   char *tftp_prefix; 
 1092   struct tftp_prefix *if_prefix; /* per-interface TFTP prefixes */
 1093   unsigned int duid_enterprise, duid_config_len;
 1094   unsigned char *duid_config;
 1095   char *dbus_name;
 1096   char *ubus_name;
 1097   char *dump_file;
 1098   int dump_mask;
 1099   unsigned long soa_sn, soa_refresh, soa_retry, soa_expiry;
 1100   u32 metrics[__METRIC_MAX];
 1101 #ifdef HAVE_DNSSEC
 1102   struct ds_config *ds;
 1103   char *timestamp_file;
 1104 #endif
 1105 
 1106   /* globally used stuff for DNS */
 1107   char *packet; /* packet buffer */
 1108   int packet_buff_sz; /* size of above */
 1109   char *namebuff; /* MAXDNAME size buffer */
 1110 #ifdef HAVE_DNSSEC
 1111   char *keyname; /* MAXDNAME size buffer */
 1112   char *workspacename; /* ditto */
 1113   unsigned long *rr_status; /* ceiling in TTL from DNSSEC or zero for insecure */
 1114   int rr_status_sz;
 1115   int dnssec_no_time_check;
 1116   int back_to_the_future;
 1117 #endif
 1118   struct frec *frec_list;
 1119   struct frec_src *free_frec_src;
 1120   int frec_src_count;
 1121   struct serverfd *sfds;
 1122   struct irec *interfaces;
 1123   struct listener *listeners;
 1124   struct server *last_server;
 1125   time_t forwardtime;
 1126   int forwardcount;
 1127   struct server *srv_save; /* Used for resend on DoD */
 1128   size_t packet_len;       /*      "        "        */
 1129   int    fd_save;          /*      "        "        */
 1130   pid_t tcp_pids[MAX_PROCS];
 1131   int tcp_pipes[MAX_PROCS];
 1132   int pipe_to_parent;
 1133   int numrrand;
 1134   struct randfd *randomsocks;
 1135   struct randfd_list *rfl_spare, *rfl_poll;
 1136   int v6pktinfo; 
 1137   struct addrlist *interface_addrs; /* list of all addresses/prefix lengths associated with all local interfaces */
 1138   int log_id, log_display_id; /* ids of transactions for logging */
 1139   union mysockaddr *log_source_addr;
 1140 
 1141   /* DHCP state */
 1142   int dhcpfd, helperfd, pxefd; 
 1143 #ifdef HAVE_INOTIFY
 1144   int inotifyfd;
 1145 #endif
 1146 #if defined(HAVE_LINUX_NETWORK)
 1147   int netlinkfd, kernel_version;
 1148 #elif defined(HAVE_BSD_NETWORK)
 1149   int dhcp_raw_fd, dhcp_icmp_fd, routefd;
 1150 #endif
 1151   struct iovec dhcp_packet;
 1152   char *dhcp_buff, *dhcp_buff2, *dhcp_buff3;
 1153   struct ping_result *ping_results;
 1154   FILE *lease_stream;
 1155   struct dhcp_bridge *bridges;
 1156   struct shared_network *shared_networks;
 1157 #ifdef HAVE_DHCP6
 1158   int duid_len;
 1159   unsigned char *duid;
 1160   struct iovec outpacket;
 1161   int dhcp6fd, icmp6fd;
 1162 #endif
 1163   /* DBus stuff */
 1164   /* void * here to avoid depending on dbus headers outside dbus.c */
 1165   void *dbus;
 1166 #ifdef HAVE_DBUS
 1167   struct watch *watches;
 1168 #endif
 1169   /* UBus stuff */
 1170 #ifdef HAVE_UBUS
 1171   /* void * here to avoid depending on ubus headers outside ubus.c */
 1172   void *ubus;
 1173 #endif
 1174 
 1175   /* TFTP stuff */
 1176   struct tftp_transfer *tftp_trans, *tftp_done_trans;
 1177 
 1178   /* utility string buffer, hold max sized IP address as string */
 1179   char *addrbuff;
 1180   char *addrbuff2; /* only allocated when OPT_EXTRALOG */
 1181 
 1182 #ifdef HAVE_DUMPFILE
 1183   /* file for packet dumps. */
 1184   int dumpfd;
 1185 #endif
 1186 } *daemon;
 1187 
 1188 /* cache.c */
 1189 void cache_init(void);
 1190 void next_uid(struct crec *crecp);
 1191 void log_query(unsigned int flags, char *name, union all_addr *addr, char *arg); 
 1192 char *record_source(unsigned int index);
 1193 char *querystr(char *desc, unsigned short type);
 1194 int cache_find_non_terminal(char *name, time_t now);
 1195 struct crec *cache_find_by_addr(struct crec *crecp,
 1196                 union all_addr *addr, time_t now, 
 1197                 unsigned int prot);
 1198 struct crec *cache_find_by_name(struct crec *crecp, 
 1199                 char *name, time_t now, unsigned int prot);
 1200 void cache_end_insert(void);
 1201 void cache_start_insert(void);
 1202 int cache_recv_insert(time_t now, int fd);
 1203 struct crec *cache_insert(char *name, union all_addr *addr, unsigned short class, 
 1204               time_t now, unsigned long ttl, unsigned int flags);
 1205 void cache_reload(void);
 1206 void cache_add_dhcp_entry(char *host_name, int prot, union all_addr *host_address, time_t ttd);
 1207 struct in_addr a_record_from_hosts(char *name, time_t now);
 1208 void cache_unhash_dhcp(void);
 1209 void dump_cache(time_t now);
 1210 #ifndef NO_ID
 1211 int cache_make_stat(struct txt_record *t);
 1212 #endif
 1213 char *cache_get_name(struct crec *crecp);
 1214 char *cache_get_cname_target(struct crec *crecp);
 1215 struct crec *cache_enumerate(int init);
 1216 int read_hostsfile(char *filename, unsigned int index, int cache_size, 
 1217            struct crec **rhash, int hashsz);
 1218 
 1219 /* blockdata.c */
 1220 void blockdata_init(void);
 1221 void blockdata_report(void);
 1222 struct blockdata *blockdata_alloc(char *data, size_t len);
 1223 void *blockdata_retrieve(struct blockdata *block, size_t len, void *data);
 1224 struct blockdata *blockdata_read(int fd, size_t len);
 1225 void blockdata_write(struct blockdata *block, size_t len, int fd);
 1226 void blockdata_free(struct blockdata *blocks);
 1227 
 1228 /* domain.c */
 1229 char *get_domain(struct in_addr addr);
 1230 char *get_domain6(struct in6_addr *addr);
 1231 int is_name_synthetic(int flags, char *name, union all_addr *addr);
 1232 int is_rev_synth(int flag, union all_addr *addr, char *name);
 1233 
 1234 /* rfc1035.c */
 1235 int extract_name(struct dns_header *header, size_t plen, unsigned char **pp, 
 1236                  char *name, int isExtract, int extrabytes);
 1237 unsigned char *skip_name(unsigned char *ansp, struct dns_header *header, size_t plen, int extrabytes);
 1238 unsigned char *skip_questions(struct dns_header *header, size_t plen);
 1239 unsigned char *skip_section(unsigned char *ansp, int count, struct dns_header *header, size_t plen);
 1240 unsigned int extract_request(struct dns_header *header, size_t qlen, 
 1241                    char *name, unsigned short *typep);
 1242 size_t setup_reply(struct dns_header *header, size_t  qlen,
 1243            union all_addr *addrp, unsigned int flags,
 1244            unsigned long ttl);
 1245 int extract_addresses(struct dns_header *header, size_t qlen, char *name,
 1246               time_t now, char **ipsets, int is_sign, int check_rebind,
 1247               int no_cache_dnssec, int secure, int *doctored);
 1248 size_t answer_request(struct dns_header *header, char *limit, size_t qlen,  
 1249               struct in_addr local_addr, struct in_addr local_netmask, 
 1250               time_t now, int ad_reqd, int do_bit, int have_pseudoheader);
 1251 int check_for_bogus_wildcard(struct dns_header *header, size_t qlen, char *name, 
 1252                  time_t now);
 1253 int check_for_ignored_address(struct dns_header *header, size_t qlen);
 1254 int check_for_local_domain(char *name, time_t now);
 1255 size_t resize_packet(struct dns_header *header, size_t plen, 
 1256           unsigned char *pheader, size_t hlen);
 1257 int add_resource_record(struct dns_header *header, char *limit, int *truncp,
 1258             int nameoffset, unsigned char **pp, unsigned long ttl, 
 1259             int *offset, unsigned short type, unsigned short class, char *format, ...);
 1260 int in_arpa_name_2_addr(char *namein, union all_addr *addrp);
 1261 int private_net(struct in_addr addr, int ban_localhost);
 1262 
 1263 /* auth.c */
 1264 #ifdef HAVE_AUTH
 1265 size_t answer_auth(struct dns_header *header, char *limit, size_t qlen, 
 1266            time_t now, union mysockaddr *peer_addr, int local_query,
 1267            int do_bit, int have_pseudoheader);
 1268 int in_zone(struct auth_zone *zone, char *name, char **cut);
 1269 #endif
 1270 
 1271 /* dnssec.c */
 1272 size_t dnssec_generate_query(struct dns_header *header, unsigned char *end, char *name, int class, int type, int edns_pktsz);
 1273 int dnssec_validate_by_ds(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class);
 1274 int dnssec_validate_ds(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class);
 1275 int dnssec_validate_reply(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int *class,
 1276               int check_unsigned, int *neganswer, int *nons, int *nsec_ttl);
 1277 int dnskey_keytag(int alg, int flags, unsigned char *key, int keylen);
 1278 size_t filter_rrsigs(struct dns_header *header, size_t plen);
 1279 int setup_timestamp(void);
 1280 
 1281 /* hash_questions.c */
 1282 void hash_questions_init(void);
 1283 unsigned char *hash_questions(struct dns_header *header, size_t plen, char *name);
 1284 
 1285 /* crypto.c */
 1286 const struct nettle_hash *hash_find(char *name);
 1287 int hash_init(const struct nettle_hash *hash, void **ctxp, unsigned char **digestp);
 1288 int verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
 1289        unsigned char *digest, size_t digest_len, int algo);
 1290 char *ds_digest_name(int digest);
 1291 char *algo_digest_name(int algo);
 1292 char *nsec3_digest_name(int digest);
 1293 
 1294 /* util.c */
 1295 void rand_init(void);
 1296 unsigned short rand16(void);
 1297 u32 rand32(void);
 1298 u64 rand64(void);
 1299 int legal_hostname(char *name);
 1300 char *canonicalise(char *in, int *nomem);
 1301 unsigned char *do_rfc1035_name(unsigned char *p, char *sval, char *limit);
 1302 void *safe_malloc(size_t size);
 1303 void safe_strncpy(char *dest, const char *src, size_t size);
 1304 void safe_pipe(int *fd, int read_noblock);
 1305 void *whine_malloc(size_t size);
 1306 int sa_len(union mysockaddr *addr);
 1307 int sockaddr_isequal(const union mysockaddr *s1, const union mysockaddr *s2);
 1308 int hostname_isequal(const char *a, const char *b);
 1309 int hostname_issubdomain(char *a, char *b);
 1310 time_t dnsmasq_time(void);
 1311 int netmask_length(struct in_addr mask);
 1312 int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask);
 1313 int is_same_net6(struct in6_addr *a, struct in6_addr *b, int prefixlen);
 1314 u64 addr6part(struct in6_addr *addr);
 1315 void setaddr6part(struct in6_addr *addr, u64 host);
 1316 int retry_send(ssize_t rc);
 1317 void prettyprint_time(char *buf, unsigned int t);
 1318 int prettyprint_addr(union mysockaddr *addr, char *buf);
 1319 int parse_hex(char *in, unsigned char *out, int maxlen, 
 1320           unsigned int *wildcard_mask, int *mac_type);
 1321 int memcmp_masked(unsigned char *a, unsigned char *b, int len, 
 1322           unsigned int mask);
 1323 int expand_buf(struct iovec *iov, size_t size);
 1324 char *print_mac(char *buff, unsigned char *mac, int len);
 1325 int read_write(int fd, unsigned char *packet, int size, int rw);
 1326 void close_fds(long max_fd, int spare1, int spare2, int spare3);
 1327 int wildcard_match(const char* wildcard, const char* match);
 1328 int wildcard_matchn(const char* wildcard, const char* match, int num);
 1329 #ifdef HAVE_LINUX_NETWORK
 1330 int kernel_version(void);
 1331 #endif
 1332 
 1333 /* log.c */
 1334 void die(char *message, char *arg1, int exit_code) ATTRIBUTE_NORETURN;
 1335 int log_start(struct passwd *ent_pw, int errfd);
 1336 int log_reopen(char *log_file);
 1337 
 1338 void my_syslog(int priority, const char *format, ...);
 1339 
 1340 void set_log_writer(void);
 1341 void check_log_writer(int force);
 1342 void flush_log(void);
 1343 
 1344 /* option.c */
 1345 void read_opts (int argc, char **argv, char *compile_opts);
 1346 char *option_string(int prot, unsigned int opt, unsigned char *val, 
 1347             int opt_len, char *buf, int buf_len);
 1348 void reread_dhcp(void);
 1349 void read_servers_file(void);
 1350 void set_option_bool(unsigned int opt);
 1351 void reset_option_bool(unsigned int opt);
 1352 struct hostsfile *expand_filelist(struct hostsfile *list);
 1353 char *parse_server(char *arg, union mysockaddr *addr, 
 1354            union mysockaddr *source_addr, char *interface, int *flags);
 1355 int option_read_dynfile(char *file, int flags);
 1356 
 1357 /* forward.c */
 1358 void reply_query(int fd, time_t now);
 1359 void receive_query(struct listener *listen, time_t now);
 1360 unsigned char *tcp_request(int confd, time_t now,
 1361                union mysockaddr *local_addr, struct in_addr netmask, int auth_dns);
 1362 void server_gone(struct server *server);
 1363 struct frec *get_new_frec(time_t now, int *wait, struct frec *force);
 1364 int send_from(int fd, int nowild, char *packet, size_t len, 
 1365            union mysockaddr *to, union all_addr *source,
 1366            unsigned int iface);
 1367 void resend_query(void);
 1368 int allocate_rfd(struct randfd_list **fdlp, struct server *serv);
 1369 void free_rfds(struct randfd_list **fdlp);
 1370 
 1371 /* network.c */
 1372 int indextoname(int fd, int index, char *name);
 1373 int local_bind(int fd, union mysockaddr *addr, char *intname, unsigned int ifindex, int is_tcp);
 1374 void pre_allocate_sfds(void);
 1375 int reload_servers(char *fname);
 1376 void mark_servers(int flag);
 1377 void cleanup_servers(void);
 1378 void add_update_server(int flags,
 1379                union mysockaddr *addr,
 1380                union mysockaddr *source_addr,
 1381                const char *interface,
 1382                const char *domain);
 1383 void check_servers(void);
 1384 int enumerate_interfaces(int reset);
 1385 void create_wildcard_listeners(void);
 1386 void create_bound_listeners(int dienow);
 1387 void warn_bound_listeners(void);
 1388 void warn_wild_labels(void);
 1389 void warn_int_names(void);
 1390 int is_dad_listeners(void);
 1391 int iface_check(int family, union all_addr *addr, char *name, int *auth);
 1392 int loopback_exception(int fd, int family, union all_addr *addr, char *name);
 1393 int label_exception(int index, int family, union all_addr *addr);
 1394 int fix_fd(int fd);
 1395 int tcp_interface(int fd, int af);
 1396 int set_ipv6pktinfo(int fd);
 1397 #ifdef HAVE_DHCP6
 1398 void join_multicast(int dienow);
 1399 #endif
 1400 #if defined(HAVE_LINUX_NETWORK) || defined(HAVE_BSD_NETWORK)
 1401 void newaddress(time_t now);
 1402 #endif
 1403 
 1404 
 1405 /* dhcp.c */
 1406 #ifdef HAVE_DHCP
 1407 void dhcp_init(void);
 1408 void dhcp_packet(time_t now, int pxe_fd);
 1409 struct dhcp_context *address_available(struct dhcp_context *context, 
 1410                        struct in_addr taddr,
 1411                        struct dhcp_netid *netids);
 1412 struct dhcp_context *narrow_context(struct dhcp_context *context, 
 1413                     struct in_addr taddr,
 1414                     struct dhcp_netid *netids);
 1415 struct ping_result *do_icmp_ping(time_t now, struct in_addr addr,
 1416                  unsigned int hash, int loopback);
 1417 int address_allocate(struct dhcp_context *context,
 1418              struct in_addr *addrp, unsigned char *hwaddr, int hw_len,
 1419              struct dhcp_netid *netids, time_t now, int loopback);
 1420 void dhcp_read_ethers(void);
 1421 struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr);
 1422 char *host_from_dns(struct in_addr addr);
 1423 #endif
 1424 
 1425 /* lease.c */
 1426 #ifdef HAVE_DHCP
 1427 void lease_update_file(time_t now);
 1428 void lease_update_dns(int force);
 1429 void lease_init(time_t now);
 1430 struct dhcp_lease *lease4_allocate(struct in_addr addr);
 1431 #ifdef HAVE_DHCP6
 1432 struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type);
 1433 struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len, 
 1434                    int lease_type, unsigned int iaid, struct in6_addr *addr);
 1435 void lease6_reset(void);
 1436 struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type,
 1437                      unsigned char *clid, int clid_len, unsigned int iaid);
 1438 struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr);
 1439 u64 lease_find_max_addr6(struct dhcp_context *context);
 1440 void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface);
 1441 void lease_update_slaac(time_t now);
 1442 void lease_set_iaid(struct dhcp_lease *lease, unsigned int iaid);
 1443 void lease_make_duid(time_t now);
 1444 #endif
 1445 void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
 1446               const unsigned char *clid, int hw_len, int hw_type,
 1447               int clid_len, time_t now, int force);
 1448 void lease_set_hostname(struct dhcp_lease *lease, const char *name, int auth, char *domain, char *config_domain);
 1449 void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now);
 1450 void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now);
 1451 struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,  
 1452                     unsigned char *clid, int clid_len);
 1453 struct dhcp_lease *lease_find_by_addr(struct in_addr addr);
 1454 struct in_addr lease_find_max_addr(struct dhcp_context *context);
 1455 void lease_prune(struct dhcp_lease *target, time_t now);
 1456 void lease_update_from_configs(void);
 1457 int do_script_run(time_t now);
 1458 void rerun_scripts(void);
 1459 void lease_find_interfaces(time_t now);
 1460 #ifdef HAVE_SCRIPT
 1461 void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, 
 1462              unsigned int len, int delim);
 1463 #endif
 1464 #endif
 1465 
 1466 /* rfc2131.c */
 1467 #ifdef HAVE_DHCP
 1468 size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
 1469           size_t sz, time_t now, int unicast_dest, int loopback,
 1470           int *is_inform, int pxe, struct in_addr fallback, time_t recvtime);
 1471 unsigned char *extended_hwaddr(int hwtype, int hwlen, unsigned char *hwaddr, 
 1472                    int clid_len, unsigned char *clid, int *len_out);
 1473 #endif
 1474 
 1475 /* dnsmasq.c */
 1476 #ifdef HAVE_DHCP
 1477 int make_icmp_sock(void);
 1478 int icmp_ping(struct in_addr addr);
 1479 int delay_dhcp(time_t start, int sec, int fd, uint32_t addr, unsigned short id);
 1480 #endif
 1481 void queue_event(int event);
 1482 void send_alarm(time_t event, time_t now);
 1483 void send_event(int fd, int event, int data, char *msg);
 1484 void clear_cache_and_reload(time_t now);
 1485 
 1486 /* netlink.c */
 1487 #ifdef HAVE_LINUX_NETWORK
 1488 char *netlink_init(void);
 1489 void netlink_multicast(void);
 1490 #endif
 1491 
 1492 /* bpf.c */
 1493 #ifdef HAVE_BSD_NETWORK
 1494 void init_bpf(void);
 1495 void send_via_bpf(struct dhcp_packet *mess, size_t len,
 1496           struct in_addr iface_addr, struct ifreq *ifr);
 1497 void route_init(void);
 1498 void route_sock(void);
 1499 #endif
 1500 
 1501 /* bpf.c or netlink.c */
 1502 int iface_enumerate(int family, void *parm, int (callback)());
 1503 
 1504 /* dbus.c */
 1505 #ifdef HAVE_DBUS
 1506 char *dbus_init(void);
 1507 void check_dbus_listeners(void);
 1508 void set_dbus_listeners(void);
 1509 #  ifdef HAVE_DHCP
 1510 void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname);
 1511 #  endif
 1512 #endif
 1513 
 1514 /* ubus.c */
 1515 #ifdef HAVE_UBUS
 1516 void ubus_init(void);
 1517 void set_ubus_listeners(void);
 1518 void check_ubus_listeners(void);
 1519 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface);
 1520 #endif
 1521 
 1522 /* ipset.c */
 1523 #ifdef HAVE_IPSET
 1524 void ipset_init(void);
 1525 int add_to_ipset(const char *setname, const union all_addr *ipaddr, int flags, int remove);
 1526 #endif
 1527 
 1528 /* helper.c */
 1529 #if defined(HAVE_SCRIPT)
 1530 int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd);
 1531 void helper_write(void);
 1532 void queue_script(int action, struct dhcp_lease *lease, 
 1533           char *hostname, time_t now);
 1534 #ifdef HAVE_TFTP
 1535 void queue_tftp(off_t file_len, char *filename, union mysockaddr *peer);
 1536 #endif
 1537 void queue_arp(int action, unsigned char *mac, int maclen,
 1538            int family, union all_addr *addr);
 1539 int helper_buf_empty(void);
 1540 #endif
 1541 
 1542 /* tftp.c */
 1543 #ifdef HAVE_TFTP
 1544 void tftp_request(struct listener *listen, time_t now);
 1545 void check_tftp_listeners(time_t now);
 1546 int do_tftp_script_run(void);
 1547 #endif
 1548 
 1549 /* conntrack.c */
 1550 #ifdef HAVE_CONNTRACK
 1551 int get_incoming_mark(union mysockaddr *peer_addr, union all_addr *local_addr,
 1552               int istcp, unsigned int *markp);
 1553 #endif
 1554 
 1555 /* dhcp6.c */
 1556 #ifdef HAVE_DHCP6
 1557 void dhcp6_init(void);
 1558 void dhcp6_packet(time_t now);
 1559 struct dhcp_context *address6_allocate(struct dhcp_context *context,  unsigned char *clid, int clid_len, int temp_addr,
 1560                        unsigned int iaid, int serial, struct dhcp_netid *netids, int plain_range, struct in6_addr *ans);
 1561 struct dhcp_context *address6_available(struct dhcp_context *context, 
 1562                     struct in6_addr *taddr,
 1563                     struct dhcp_netid *netids,
 1564                     int plain_range);
 1565 struct dhcp_context *address6_valid(struct dhcp_context *context, 
 1566                     struct in6_addr *taddr,
 1567                     struct dhcp_netid *netids,
 1568                     int plain_range);
 1569 struct dhcp_config *config_find_by_address6(struct dhcp_config *configs, struct in6_addr *net, 
 1570                         int prefix, struct in6_addr *addr);
 1571 void make_duid(time_t now);
 1572 void dhcp_construct_contexts(time_t now);
 1573 void get_client_mac(struct in6_addr *client, int iface, unsigned char *mac, 
 1574             unsigned int *maclenp, unsigned int *mactypep, time_t now);
 1575 #endif
 1576   
 1577 /* rfc3315.c */
 1578 #ifdef HAVE_DHCP6
 1579 unsigned short dhcp6_reply(struct dhcp_context *context, int interface, char *iface_name,  
 1580                struct in6_addr *fallback, struct in6_addr *ll_addr, struct in6_addr *ula_addr,
 1581                size_t sz, struct in6_addr *client_addr, time_t now);
 1582 void relay_upstream6(struct dhcp_relay *relay, ssize_t sz, struct in6_addr *peer_address, 
 1583              u32 scope_id, time_t now);
 1584 
 1585 unsigned short relay_reply6( struct sockaddr_in6 *peer, ssize_t sz, char *arrival_interface);
 1586 #endif
 1587 
 1588 /* dhcp-common.c */
 1589 #ifdef HAVE_DHCP
 1590 void dhcp_common_init(void);
 1591 ssize_t recv_dhcp_packet(int fd, struct msghdr *msg);
 1592 struct dhcp_netid *run_tag_if(struct dhcp_netid *tags);
 1593 struct dhcp_netid *option_filter(struct dhcp_netid *tags, struct dhcp_netid *context_tags,
 1594                  struct dhcp_opt *opts);
 1595 int match_netid(struct dhcp_netid *check, struct dhcp_netid *pool, int tagnotneeded);
 1596 char *strip_hostname(char *hostname);
 1597 void log_tags(struct dhcp_netid *netid, u32 xid);
 1598 int match_bytes(struct dhcp_opt *o, unsigned char *p, int len);
 1599 void dhcp_update_configs(struct dhcp_config *configs);
 1600 void display_opts(void);
 1601 int lookup_dhcp_opt(int prot, char *name);
 1602 int lookup_dhcp_len(int prot, int val);
 1603 struct dhcp_config *find_config(struct dhcp_config *configs,
 1604                 struct dhcp_context *context,
 1605                 unsigned char *clid, int clid_len,
 1606                 unsigned char *hwaddr, int hw_len, 
 1607                 int hw_type, char *hostname,
 1608                 struct dhcp_netid *filter);
 1609 int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type);
 1610 #ifdef HAVE_LINUX_NETWORK
 1611 char *whichdevice(void);
 1612 void bindtodevice(char *device, int fd);
 1613 #endif
 1614 #  ifdef HAVE_DHCP6
 1615 void display_opts6(void);
 1616 #  endif
 1617 void log_context(int family, struct dhcp_context *context);
 1618 void log_relay(int family, struct dhcp_relay *relay);
 1619 #endif
 1620 
 1621 /* outpacket.c */
 1622 #ifdef HAVE_DHCP6
 1623 void end_opt6(int container);
 1624 void reset_counter(void);
 1625 int save_counter(int newval);
 1626 void *expand(size_t headroom);
 1627 int new_opt6(int opt);
 1628 void *put_opt6(void *data, size_t len);
 1629 void put_opt6_long(unsigned int val);
 1630 void put_opt6_short(unsigned int val);
 1631 void put_opt6_char(unsigned int val);
 1632 void put_opt6_string(char *s);
 1633 #endif
 1634 
 1635 /* radv.c */
 1636 #ifdef HAVE_DHCP6
 1637 void ra_init(time_t now);
 1638 void icmp6_packet(time_t now);
 1639 time_t periodic_ra(time_t now);
 1640 void ra_start_unsolicited(time_t now, struct dhcp_context *context);
 1641 #endif
 1642 
 1643 /* slaac.c */ 
 1644 #ifdef HAVE_DHCP6
 1645 void slaac_add_addrs(struct dhcp_lease *lease, time_t now, int force);
 1646 time_t periodic_slaac(time_t now, struct dhcp_lease *leases);
 1647 void slaac_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface, struct dhcp_lease *leases);
 1648 #endif
 1649 
 1650 /* loop.c */
 1651 #ifdef HAVE_LOOP
 1652 void loop_send_probes(void);
 1653 int detect_loop(char *query, int type);
 1654 #endif
 1655 
 1656 /* inotify.c */
 1657 #ifdef HAVE_INOTIFY
 1658 void inotify_dnsmasq_init(void);
 1659 int inotify_check(time_t now);
 1660 void set_dynamic_inotify(int flag, int total_size, struct crec **rhash, int revhashsz);
 1661 #endif
 1662 
 1663 /* poll.c */
 1664 void poll_reset(void);
 1665 int poll_check(int fd, short event);
 1666 void poll_listen(int fd, short event);
 1667 int do_poll(int timeout);
 1668 
 1669 /* rrfilter.c */
 1670 size_t rrfilter(struct dns_header *header, size_t plen, int mode);
 1671 u16 *rrfilter_desc(int type);
 1672 int expand_workspace(unsigned char ***wkspc, int *szp, int new);
 1673 
 1674 /* edns0.c */
 1675 unsigned char *find_pseudoheader(struct dns_header *header, size_t plen,
 1676                    size_t *len, unsigned char **p, int *is_sign, int *is_last);
 1677 size_t add_pseudoheader(struct dns_header *header, size_t plen, unsigned char *limit, 
 1678             unsigned short udp_sz, int optno, unsigned char *opt, size_t optlen, int set_do, int replace);
 1679 size_t add_do_bit(struct dns_header *header, size_t plen, unsigned char *limit);
 1680 size_t add_edns0_config(struct dns_header *header, size_t plen, unsigned char *limit, 
 1681             union mysockaddr *source, time_t now, int *check_subnet, int *cacheable);
 1682 int check_source(struct dns_header *header, size_t plen, unsigned char *pseudoheader, union mysockaddr *peer);
 1683 
 1684 /* arp.c */
 1685 int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now);
 1686 int do_arp_script_run(void);
 1687 
 1688 /* dump.c */
 1689 #ifdef HAVE_DUMPFILE
 1690 void dump_init(void);
 1691 void dump_packet(int mask, void *packet, size_t len, union mysockaddr *src, union mysockaddr *dst);
 1692 #endif