Home | History | Annotate | Download | only in src
      1 /* dnsmasq is Copyright (c) 2000-2009 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-2009 Simon Kelley"
     18 
     19 #ifndef NO_LARGEFILE
     20 /* Ensure we can use files >2GB (log files may grow this big) */
     21 #  define _LARGEFILE_SOURCE 1
     22 #  define _FILE_OFFSET_BITS 64
     23 #endif
     24 
     25 /* Get linux C library versions. */
     26 #ifdef __linux__
     27 #  ifndef __ANDROID__
     28 #      define _GNU_SOURCE
     29 #  endif
     30 #  include <features.h>
     31 #endif
     32 
     33 /* get these before config.h  for IPv6 stuff... */
     34 #include <sys/types.h>
     35 #include <sys/socket.h>
     36 #include <netinet/in.h>
     37 
     38 #ifdef __APPLE__
     39 #  include <nameser.h>
     40 #  include <arpa/nameser_compat.h>
     41 #else
     42 #  ifdef __ANDROID__
     43 #    include "nameser.h"
     44 #  else
     45 #    include <arpa/nameser.h>
     46 #  endif
     47 #endif
     48 
     49 /* and this. */
     50 #include <getopt.h>
     51 
     52 #include "config.h"
     53 
     54 #define gettext_noop(S) (S)
     55 #ifndef LOCALEDIR
     56 #  define _(S) (S)
     57 #else
     58 #  include <libintl.h>
     59 #  include <locale.h>
     60 #  define _(S) gettext(S)
     61 #endif
     62 
     63 #include <arpa/inet.h>
     64 #include <sys/stat.h>
     65 #include <sys/ioctl.h>
     66 #if defined(HAVE_SOLARIS_NETWORK)
     67 #include <sys/sockio.h>
     68 #endif
     69 #include <sys/select.h>
     70 #include <sys/wait.h>
     71 #include <sys/time.h>
     72 #include <sys/un.h>
     73 #include <limits.h>
     74 #include <net/if.h>
     75 #include <unistd.h>
     76 #include <stdio.h>
     77 #include <string.h>
     78 #include <stdlib.h>
     79 #include <fcntl.h>
     80 #include <ctype.h>
     81 #include <signal.h>
     82 #include <stddef.h>
     83 #include <time.h>
     84 #include <errno.h>
     85 #include <pwd.h>
     86 #include <grp.h>
     87 #include <stdarg.h>
     88 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sun__) || defined (__sun) || defined (__ANDROID__)
     89 #  include <netinet/if_ether.h>
     90 #else
     91 #  include <net/ethernet.h>
     92 #endif
     93 #include <net/if_arp.h>
     94 #include <netinet/in_systm.h>
     95 #include <netinet/ip.h>
     96 #include <netinet/ip_icmp.h>
     97 #include <sys/uio.h>
     98 #include <syslog.h>
     99 #include <dirent.h>
    100 #ifndef HAVE_LINUX_NETWORK
    101 #  include <net/if_dl.h>
    102 #endif
    103 
    104 #if defined(HAVE_LINUX_NETWORK)
    105 #include <linux/capability.h>
    106 /* There doesn't seem to be a universally-available
    107    userpace header for these. */
    108 extern int capset(cap_user_header_t header, cap_user_data_t data);
    109 extern int capget(cap_user_header_t header, cap_user_data_t data);
    110 #define LINUX_CAPABILITY_VERSION_1  0x19980330
    111 #define LINUX_CAPABILITY_VERSION_2  0x20071026
    112 #define LINUX_CAPABILITY_VERSION_3  0x20080522
    113 
    114 #include <sys/prctl.h>
    115 #elif defined(HAVE_SOLARIS_NETWORK)
    116 #include <priv.h>
    117 #endif
    118 
    119 /* daemon is function in the C library.... */
    120 #define daemon dnsmasq_daemon
    121 
    122 /* Async event queue */
    123 struct event_desc {
    124   int event, data;
    125 };
    126 
    127 #define EVENT_RELOAD    1
    128 #define EVENT_DUMP      2
    129 #define EVENT_ALARM     3
    130 #define EVENT_TERM      4
    131 #define EVENT_CHILD     5
    132 #define EVENT_REOPEN    6
    133 #define EVENT_EXITED    7
    134 #define EVENT_KILLED    8
    135 #define EVENT_EXEC_ERR  9
    136 #define EVENT_PIPE_ERR  10
    137 #define EVENT_USER_ERR  11
    138 #define EVENT_CAP_ERR   12
    139 #define EVENT_PIDFILE   13
    140 #define EVENT_HUSER_ERR 14
    141 #define EVENT_GROUP_ERR 15
    142 #define EVENT_DIE       16
    143 #define EVENT_LOG_ERR   17
    144 #define EVENT_FORK_ERR  18
    145 
    146 /* Exit codes. */
    147 #define EC_GOOD        0
    148 #define EC_BADCONF     1
    149 #define EC_BADNET      2
    150 #define EC_FILE        3
    151 #define EC_NOMEM       4
    152 #define EC_MISC        5
    153 #define EC_INIT_OFFSET 10
    154 
    155 /* Min buffer size: we check after adding each record, so there must be
    156    memory for the largest packet, and the largest record so the
    157    min for DNS is PACKETSZ+MAXDNAME+RRFIXEDSZ which is < 1000.
    158    This might be increased is EDNS packet size if greater than the minimum.
    159 */
    160 #define DNSMASQ_PACKETSZ PACKETSZ+MAXDNAME+RRFIXEDSZ
    161 
    162 #define OPT_BOGUSPRIV      (1u<<0)
    163 #define OPT_FILTER         (1u<<1)
    164 #define OPT_LOG            (1u<<2)
    165 #define OPT_SELFMX         (1u<<3)
    166 #define OPT_NO_HOSTS       (1u<<4)
    167 #define OPT_NO_POLL        (1u<<5)
    168 #define OPT_DEBUG          (1u<<6)
    169 #define OPT_ORDER          (1u<<7)
    170 #define OPT_NO_RESOLV      (1u<<8)
    171 #define OPT_EXPAND         (1u<<9)
    172 #define OPT_LOCALMX        (1u<<10)
    173 #define OPT_NO_NEG         (1u<<11)
    174 #define OPT_NODOTS_LOCAL   (1u<<12)
    175 #define OPT_NOWILD         (1u<<13)
    176 #define OPT_ETHERS         (1u<<14)
    177 #define OPT_RESOLV_DOMAIN  (1u<<15)
    178 #define OPT_NO_FORK        (1u<<16)
    179 #define OPT_AUTHORITATIVE  (1u<<17)
    180 #define OPT_LOCALISE       (1u<<18)
    181 #define OPT_DBUS           (1u<<19)
    182 #define OPT_DHCP_FQDN      (1u<<20)
    183 #define OPT_NO_PING        (1u<<21)
    184 #define OPT_LEASE_RO       (1u<<22)
    185 #define OPT_ALL_SERVERS    (1u<<23)
    186 #define OPT_RELOAD         (1u<<24)
    187 #define OPT_TFTP           (1u<<25)
    188 #define OPT_TFTP_SECURE    (1u<<26)
    189 #define OPT_TFTP_NOBLOCK   (1u<<27)
    190 #define OPT_LOG_OPTS       (1u<<28)
    191 #define OPT_TFTP_APREF     (1u<<29)
    192 #define OPT_NO_OVERRIDE    (1u<<30)
    193 #define OPT_NO_REBIND      (1u<<31)
    194 
    195 /* extra flags for my_syslog, we use a couple of facilities since they are known
    196    not to occupy the same bits as priorities, no matter how syslog.h is set up. */
    197 #define MS_TFTP LOG_USER
    198 #define MS_DHCP LOG_DAEMON
    199 
    200 struct all_addr {
    201   union {
    202     struct in_addr addr4;
    203 #ifdef HAVE_IPV6
    204     struct in6_addr addr6;
    205 #endif
    206   } addr;
    207 };
    208 
    209 struct bogus_addr {
    210   struct in_addr addr;
    211   struct bogus_addr *next;
    212 };
    213 
    214 /* dns doctor param */
    215 struct doctor {
    216   struct in_addr in, end, out, mask;
    217   struct doctor *next;
    218 };
    219 
    220 struct mx_srv_record {
    221   char *name, *target;
    222   int issrv, srvport, priority, weight;
    223   unsigned int offset;
    224   struct mx_srv_record *next;
    225 };
    226 
    227 struct naptr {
    228   char *name, *replace, *regexp, *services, *flags;
    229   unsigned int order, pref;
    230   struct naptr *next;
    231 };
    232 
    233 struct txt_record {
    234   char *name, *txt;
    235   unsigned short class, len;
    236   struct txt_record *next;
    237 };
    238 
    239 struct ptr_record {
    240   char *name, *ptr;
    241   struct ptr_record *next;
    242 };
    243 
    244 struct cname {
    245   char *alias, *target;
    246   struct cname *next;
    247 };
    248 
    249 struct interface_name {
    250   char *name; /* domain name */
    251   char *intr; /* interface name */
    252   struct interface_name *next;
    253 };
    254 
    255 union bigname {
    256   char name[MAXDNAME];
    257   union bigname *next; /* freelist */
    258 };
    259 
    260 struct crec {
    261   struct crec *next, *prev, *hash_next;
    262   time_t ttd; /* time to die */
    263   int uid;
    264   union {
    265     struct all_addr addr;
    266     struct {
    267       struct crec *cache;
    268       int uid;
    269     } cname;
    270   } addr;
    271   unsigned short flags;
    272   union {
    273     char sname[SMALLDNAME];
    274     union bigname *bname;
    275     char *namep;
    276   } name;
    277 };
    278 
    279 #define F_IMMORTAL  1
    280 #define F_CONFIG    2
    281 #define F_REVERSE   4
    282 #define F_FORWARD   8
    283 #define F_DHCP      16
    284 #define F_NEG       32
    285 #define F_HOSTS     64
    286 #define F_IPV4      128
    287 #define F_IPV6      256
    288 #define F_BIGNAME   512
    289 #define F_UPSTREAM  1024
    290 #define F_SERVER    2048
    291 #define F_NXDOMAIN  4096
    292 #define F_QUERY     8192
    293 #define F_CNAME     16384
    294 #define F_NOERR     32768
    295 
    296 /* struct sockaddr is not large enough to hold any address,
    297    and specifically not big enough to hold an IPv6 address.
    298    Blech. Roll our own. */
    299 union mysockaddr {
    300   struct sockaddr sa;
    301   struct sockaddr_in in;
    302 #if defined(HAVE_IPV6)
    303   struct sockaddr_in6 in6;
    304 #endif
    305 };
    306 
    307 #define SERV_FROM_RESOLV       1  /* 1 for servers from resolv, 0 for command line. */
    308 #define SERV_NO_ADDR           2  /* no server, this domain is local only */
    309 #define SERV_LITERAL_ADDRESS   4  /* addr is the answer, not the server */
    310 #define SERV_HAS_DOMAIN        8  /* server for one domain only */
    311 #define SERV_HAS_SOURCE       16  /* source address defined */
    312 #define SERV_FOR_NODOTS       32  /* server for names with no domain part only */
    313 #define SERV_WARNED_RECURSIVE 64  /* avoid warning spam */
    314 #define SERV_FROM_DBUS       128  /* 1 if source is DBus */
    315 #define SERV_MARK            256  /* for mark-and-delete */
    316 #define SERV_TYPE    (SERV_HAS_DOMAIN | SERV_FOR_NODOTS)
    317 #define SERV_COUNTED         512  /* workspace for log code */
    318 
    319 struct serverfd {
    320   int fd;
    321   union mysockaddr source_addr;
    322   char interface[IF_NAMESIZE+1];
    323   struct serverfd *next;
    324   uint32_t mark;
    325 };
    326 
    327 struct randfd {
    328   int fd;
    329   unsigned short refcount, family;
    330 };
    331 
    332 struct server {
    333   union mysockaddr addr, source_addr;
    334   char interface[IF_NAMESIZE+1];
    335   struct serverfd *sfd;
    336   char *domain; /* set if this server only handles a domain. */
    337   int flags, tcpfd;
    338   unsigned int queries, failed_queries;
    339   uint32_t mark;
    340   struct server *next;
    341 };
    342 
    343 struct irec {
    344   union mysockaddr addr;
    345   struct in_addr netmask; /* only valid for IPv4 */
    346   int dhcp_ok, mtu;
    347   struct irec *next;
    348 };
    349 
    350 struct listener {
    351   int fd, tcpfd, tftpfd, family;
    352   struct irec *iface; /* only valid for non-wildcard */
    353   struct listener *next;
    354 };
    355 
    356 /* interface and address parms from command line. */
    357 struct iname {
    358   char *name;
    359   union mysockaddr addr;
    360   int isloop, used;
    361   struct iname *next;
    362 };
    363 
    364 /* resolv-file parms from command-line */
    365 struct resolvc {
    366   struct resolvc *next;
    367   int is_default, logged;
    368   time_t mtime;
    369   char *name;
    370 };
    371 
    372 /* adn-hosts parms from command-line */
    373 #define AH_DIR      1
    374 #define AH_INACTIVE 2
    375 struct hostsfile {
    376   struct hostsfile *next;
    377   int flags;
    378   char *fname;
    379   int index; /* matches to cache entries for logging */
    380 };
    381 
    382 struct frec {
    383   union mysockaddr source;
    384   struct all_addr dest;
    385   struct server *sentto; /* NULL means free */
    386   struct randfd *rfd4;
    387 #ifdef HAVE_IPV6
    388   struct randfd *rfd6;
    389 #endif
    390   unsigned int iface;
    391   unsigned short orig_id, new_id;
    392   int fd, forwardall;
    393   unsigned int crc;
    394   time_t time;
    395   struct frec *next;
    396 };
    397 
    398 /* actions in the daemon->helper RPC */
    399 #define ACTION_DEL           1
    400 #define ACTION_OLD_HOSTNAME  2
    401 #define ACTION_OLD           3
    402 #define ACTION_ADD           4
    403 
    404 #define DHCP_CHADDR_MAX 16
    405 
    406 struct dhcp_lease {
    407   int clid_len;          /* length of client identifier */
    408   unsigned char *clid;   /* clientid */
    409   char *hostname, *fqdn; /* name from client-hostname option or config */
    410   char *old_hostname;    /* hostname before it moved to another lease */
    411   char auth_name;        /* hostname came from config, not from client */
    412   char new;              /* newly created */
    413   char changed;          /* modified */
    414   char aux_changed;      /* CLID or expiry changed */
    415   time_t expires;        /* lease expiry */
    416 #ifdef HAVE_BROKEN_RTC
    417   unsigned int length;
    418 #endif
    419   int hwaddr_len, hwaddr_type;
    420   unsigned char hwaddr[DHCP_CHADDR_MAX];
    421   struct in_addr addr, override, giaddr;
    422   unsigned char *vendorclass, *userclass, *supplied_hostname;
    423   unsigned int vendorclass_len, userclass_len, supplied_hostname_len;
    424   int last_interface;
    425   struct dhcp_lease *next;
    426 };
    427 
    428 struct dhcp_netid {
    429   char *net;
    430   struct dhcp_netid *next;
    431 };
    432 
    433 struct dhcp_netid_list {
    434   struct dhcp_netid *list;
    435   struct dhcp_netid_list *next;
    436 };
    437 
    438 struct hwaddr_config {
    439   int hwaddr_len, hwaddr_type;
    440   unsigned char hwaddr[DHCP_CHADDR_MAX];
    441   unsigned int wildcard_mask;
    442   struct hwaddr_config *next;
    443 };
    444 
    445 struct dhcp_config {
    446   unsigned int flags;
    447   int clid_len;          /* length of client identifier */
    448   unsigned char *clid;   /* clientid */
    449   char *hostname, *domain;
    450   struct dhcp_netid netid;
    451   struct in_addr addr;
    452   time_t decline_time;
    453   unsigned int lease_time;
    454   struct hwaddr_config *hwaddr;
    455   struct dhcp_config *next;
    456 };
    457 
    458 #define CONFIG_DISABLE           1
    459 #define CONFIG_CLID              2
    460 #define CONFIG_TIME              8
    461 #define CONFIG_NAME             16
    462 #define CONFIG_ADDR             32
    463 #define CONFIG_NETID            64
    464 #define CONFIG_NOCLID          128
    465 #define CONFIG_FROM_ETHERS     256    /* entry created by /etc/ethers */
    466 #define CONFIG_ADDR_HOSTS      512    /* address added by from /etc/hosts */
    467 #define CONFIG_DECLINED       1024    /* address declined by client */
    468 #define CONFIG_BANK           2048    /* from dhcp hosts file */
    469 
    470 struct dhcp_opt {
    471   int opt, len, flags;
    472   union {
    473     int encap;
    474     unsigned int wildcard_mask;
    475     unsigned char *vendor_class;
    476   } u;
    477   unsigned char *val;
    478   struct dhcp_netid *netid;
    479   struct dhcp_opt *next;
    480 };
    481 
    482 #define DHOPT_ADDR               1
    483 #define DHOPT_STRING             2
    484 #define DHOPT_ENCAPSULATE        4
    485 #define DHOPT_ENCAP_MATCH        8
    486 #define DHOPT_FORCE             16
    487 #define DHOPT_BANK              32
    488 #define DHOPT_ENCAP_DONE        64
    489 #define DHOPT_MATCH            128
    490 #define DHOPT_VENDOR           256
    491 #define DHOPT_HEX              512
    492 #define DHOPT_VENDOR_MATCH    1024
    493 
    494 struct dhcp_boot {
    495   char *file, *sname;
    496   struct in_addr next_server;
    497   struct dhcp_netid *netid;
    498   struct dhcp_boot *next;
    499 };
    500 
    501 struct pxe_service {
    502   unsigned short CSA, type;
    503   char *menu, *basename;
    504   struct in_addr server;
    505   struct dhcp_netid *netid;
    506   struct pxe_service *next;
    507 };
    508 
    509 #define MATCH_VENDOR     1
    510 #define MATCH_USER       2
    511 #define MATCH_CIRCUIT    3
    512 #define MATCH_REMOTE     4
    513 #define MATCH_SUBSCRIBER 5
    514 
    515 /* vendorclass, userclass, remote-id or cicuit-id */
    516 struct dhcp_vendor {
    517   int len, match_type, option;
    518   char *data;
    519   struct dhcp_netid netid;
    520   struct dhcp_vendor *next;
    521 };
    522 
    523 struct dhcp_mac {
    524   unsigned int mask;
    525   int hwaddr_len, hwaddr_type;
    526   unsigned char hwaddr[DHCP_CHADDR_MAX];
    527   struct dhcp_netid netid;
    528   struct dhcp_mac *next;
    529 };
    530 
    531 struct dhcp_bridge {
    532   char iface[IF_NAMESIZE];
    533   struct dhcp_bridge *alias, *next;
    534 };
    535 
    536 struct cond_domain {
    537   char *domain;
    538   struct in_addr start, end;
    539   struct cond_domain *next;
    540 };
    541 
    542 struct dhcp_context {
    543   unsigned int lease_time, addr_epoch;
    544   struct in_addr netmask, broadcast;
    545   struct in_addr local, router;
    546   struct in_addr start, end; /* range of available addresses */
    547   int flags;
    548   struct dhcp_netid netid, *filter;
    549   struct dhcp_context *next, *current;
    550 };
    551 
    552 #define CONTEXT_STATIC    1
    553 #define CONTEXT_NETMASK   2
    554 #define CONTEXT_BRDCAST   4
    555 #define CONTEXT_PROXY     8
    556 
    557 
    558 typedef unsigned char u8;
    559 typedef unsigned short u16;
    560 typedef unsigned int u32;
    561 
    562 
    563 struct dhcp_packet {
    564   u8 op, htype, hlen, hops;
    565   u32 xid;
    566   u16 secs, flags;
    567   struct in_addr ciaddr, yiaddr, siaddr, giaddr;
    568   u8 chaddr[DHCP_CHADDR_MAX], sname[64], file[128];
    569   u8 options[312];
    570 };
    571 
    572 struct ping_result {
    573   struct in_addr addr;
    574   time_t time;
    575   struct ping_result *next;
    576 };
    577 
    578 struct tftp_file {
    579   int refcount, fd;
    580   off_t size;
    581   dev_t dev;
    582   ino_t inode;
    583   char filename[];
    584 };
    585 
    586 struct tftp_transfer {
    587   int sockfd;
    588   time_t timeout;
    589   int backoff;
    590   unsigned int block, blocksize, expansion;
    591   off_t offset;
    592   struct sockaddr_in peer;
    593   char opt_blocksize, opt_transize, netascii, carrylf;
    594   struct tftp_file *file;
    595   struct tftp_transfer *next;
    596 };
    597 
    598 extern struct daemon {
    599   /* datastuctures representing the command-line and
    600      config file arguments. All set (including defaults)
    601      in option.c */
    602 
    603   unsigned int options;
    604   struct resolvc default_resolv, *resolv_files;
    605   time_t last_resolv;
    606   struct mx_srv_record *mxnames;
    607   struct naptr *naptr;
    608   struct txt_record *txt;
    609   struct ptr_record *ptr;
    610   struct cname *cnames;
    611   struct interface_name *int_names;
    612   char *mxtarget;
    613   char *lease_file;
    614   char *username, *groupname, *scriptuser;
    615   int group_set, osport;
    616   char *domain_suffix;
    617   struct cond_domain *cond_domain;
    618   char *runfile;
    619   char *lease_change_command;
    620   struct iname *if_names, *if_addrs, *if_except, *dhcp_except;
    621   struct bogus_addr *bogus_addr;
    622   struct server *servers;
    623   int log_fac; /* log facility */
    624   char *log_file; /* optional log file */
    625   int max_logs;  /* queue limit */
    626   int cachesize, ftabsize;
    627   int port, query_port, min_port;
    628   unsigned long local_ttl, neg_ttl;
    629   struct hostsfile *addn_hosts;
    630   struct dhcp_context *dhcp;
    631   struct dhcp_config *dhcp_conf;
    632   struct dhcp_opt *dhcp_opts, *dhcp_match;
    633   struct dhcp_vendor *dhcp_vendors;
    634   struct dhcp_mac *dhcp_macs;
    635   struct dhcp_boot *boot_config;
    636   struct pxe_service *pxe_services;
    637   int enable_pxe;
    638   struct dhcp_netid_list *dhcp_ignore, *dhcp_ignore_names, *force_broadcast, *bootp_dynamic;
    639   char *dhcp_hosts_file, *dhcp_opts_file;
    640   int dhcp_max, tftp_max;
    641   int dhcp_server_port, dhcp_client_port;
    642   int start_tftp_port, end_tftp_port;
    643   unsigned int min_leasetime;
    644   struct doctor *doctors;
    645   unsigned short edns_pktsz;
    646   char *tftp_prefix;
    647   uint32_t listen_mark;
    648 
    649   /* globally used stuff for DNS */
    650   char *packet; /* packet buffer */
    651   int packet_buff_sz; /* size of above */
    652   char *namebuff; /* MAXDNAME size buffer */
    653   unsigned int local_answer, queries_forwarded;
    654   struct frec *frec_list;
    655   struct serverfd *sfds;
    656   struct irec *interfaces;
    657   struct listener *listeners;
    658   struct server *last_server;
    659   time_t forwardtime;
    660   int forwardcount;
    661   struct server *srv_save; /* Used for resend on DoD */
    662   size_t packet_len;       /*      "        "        */
    663   struct randfd *rfd_save; /*      "        "        */
    664   pid_t tcp_pids[MAX_PROCS];
    665   struct randfd randomsocks[RANDOM_SOCKS];
    666 
    667   /* DHCP state */
    668   int dhcpfd, helperfd;
    669 #if defined(HAVE_LINUX_NETWORK)
    670   int netlinkfd;
    671 #elif defined(HAVE_BSD_NETWORK)
    672   int dhcp_raw_fd, dhcp_icmp_fd;
    673 #endif
    674   struct iovec dhcp_packet;
    675   char *dhcp_buff, *dhcp_buff2;
    676   struct ping_result *ping_results;
    677   FILE *lease_stream;
    678   struct dhcp_bridge *bridges;
    679 
    680   /* DBus stuff */
    681   /* void * here to avoid depending on dbus headers outside dbus.c */
    682   void *dbus;
    683 #ifdef HAVE_DBUS
    684   struct watch *watches;
    685 #endif
    686 
    687   /* TFTP stuff */
    688   struct tftp_transfer *tftp_trans;
    689 
    690 } *daemon;
    691 
    692 /* cache.c */
    693 void cache_init(void);
    694 void log_query(unsigned short flags, char *name, struct all_addr *addr, char *arg);
    695 char *record_source(int index);
    696 void querystr(char *str, unsigned short type);
    697 struct crec *cache_find_by_addr(struct crec *crecp,
    698 				struct all_addr *addr, time_t now,
    699 				unsigned short prot);
    700 struct crec *cache_find_by_name(struct crec *crecp,
    701 				char *name, time_t now, unsigned short  prot);
    702 void cache_end_insert(void);
    703 void cache_start_insert(void);
    704 struct crec *cache_insert(char *name, struct all_addr *addr,
    705 			  time_t now, unsigned long ttl, unsigned short flags);
    706 void cache_reload(void);
    707 void cache_add_dhcp_entry(char *host_name, struct in_addr *host_address, time_t ttd);
    708 void cache_unhash_dhcp(void);
    709 void dump_cache(time_t now);
    710 char *cache_get_name(struct crec *crecp);
    711 char *get_domain(struct in_addr addr);
    712 
    713 /* rfc1035.c */
    714 unsigned short extract_request(HEADER *header, size_t qlen,
    715 			       char *name, unsigned short *typep);
    716 size_t setup_reply(HEADER *header, size_t  qlen,
    717 		   struct all_addr *addrp, unsigned short flags,
    718 		   unsigned long local_ttl);
    719 int extract_addresses(HEADER *header, size_t qlen, char *namebuff, time_t now);
    720 size_t answer_request(HEADER *header, char *limit, size_t qlen,
    721 		   struct in_addr local_addr, struct in_addr local_netmask, time_t now);
    722 int check_for_bogus_wildcard(HEADER *header, size_t qlen, char *name,
    723 			     struct bogus_addr *addr, time_t now);
    724 unsigned char *find_pseudoheader(HEADER *header, size_t plen,
    725 				 size_t *len, unsigned char **p, int *is_sign);
    726 int check_for_local_domain(char *name, time_t now);
    727 unsigned int questions_crc(HEADER *header, size_t plen, char *buff);
    728 size_t resize_packet(HEADER *header, size_t plen,
    729 		  unsigned char *pheader, size_t hlen);
    730 
    731 /* util.c */
    732 void rand_init(void);
    733 unsigned short rand16(void);
    734 int legal_hostname(char *c);
    735 char *canonicalise(char *s, int *nomem);
    736 unsigned char *do_rfc1035_name(unsigned char *p, char *sval);
    737 void *safe_malloc(size_t size);
    738 void safe_pipe(int *fd, int read_noblock);
    739 void *whine_malloc(size_t size);
    740 int sa_len(union mysockaddr *addr);
    741 int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2);
    742 int hostname_isequal(char *a, char *b);
    743 time_t dnsmasq_time(void);
    744 int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask);
    745 int retry_send(void);
    746 int parse_addr(int family, const char *addrstr, union mysockaddr *addr);
    747 void prettyprint_time(char *buf, unsigned int t);
    748 int prettyprint_addr(const union mysockaddr *addr, char *buf);
    749 int parse_hex(char *in, unsigned char *out, int maxlen,
    750 	      unsigned int *wildcard_mask, int *mac_type);
    751 int memcmp_masked(unsigned char *a, unsigned char *b, int len,
    752 		  unsigned int mask);
    753 int expand_buf(struct iovec *iov, size_t size);
    754 char *print_mac(char *buff, unsigned char *mac, int len);
    755 void bump_maxfd(int fd, int *max);
    756 int read_write(int fd, unsigned char *packet, int size, int rw);
    757 
    758 /* log.c */
    759 void die(char *message, char *arg1, int exit_code);
    760 int log_start(struct passwd *ent_pw, int errfd);
    761 int log_reopen(char *log_file);
    762 void my_syslog(int priority, const char *format, ...);
    763 void set_log_writer(fd_set *set, int *maxfdp);
    764 void check_log_writer(fd_set *set);
    765 void flush_log(void);
    766 
    767 /* option.c */
    768 void read_opts (int argc, char **argv, char *compile_opts);
    769 char *option_string(unsigned char opt, int *is_ip, int *is_name);
    770 void reread_dhcp(void);
    771 
    772 /* forward.c */
    773 void reply_query(int fd, int family, time_t now);
    774 void receive_query(struct listener *listen, time_t now);
    775 unsigned char *tcp_request(int confd, time_t now,
    776 			   struct in_addr local_addr, struct in_addr netmask);
    777 void server_gone(struct server *server);
    778 struct frec *get_new_frec(time_t now, int *wait);
    779 
    780 /* network.c */
    781 int indextoname(int fd, int index, char *name);
    782 int local_bind(int fd, union mysockaddr *addr, char *intname, uint32_t mark, int is_tcp);
    783 int random_sock(int family);
    784 void pre_allocate_sfds(void);
    785 int reload_servers(char *fname);
    786 #if defined(__ANDROID__) && !defined(__BRILLO__)
    787 int set_servers(const char *servers);
    788 void set_interfaces(const char *interfaces);
    789 #endif
    790 void check_servers(void);
    791 int enumerate_interfaces();
    792 struct listener *create_wildcard_listeners(void);
    793 struct listener *create_bound_listeners(void);
    794 int iface_check(int family, struct all_addr *addr, char *name, int *indexp);
    795 int fix_fd(int fd);
    796 struct in_addr get_ifaddr(char *intr);
    797 
    798 /* dhcp.c */
    799 #ifdef HAVE_DHCP
    800 void dhcp_init(void);
    801 void dhcp_packet(time_t now);
    802 struct dhcp_context *address_available(struct dhcp_context *context,
    803 				       struct in_addr addr,
    804 				       struct dhcp_netid *netids);
    805 struct dhcp_context *narrow_context(struct dhcp_context *context,
    806 				    struct in_addr taddr,
    807 				    struct dhcp_netid *netids);
    808 int match_netid(struct dhcp_netid *check, struct dhcp_netid *pool, int negonly);int address_allocate(struct dhcp_context *context,
    809 		     struct in_addr *addrp, unsigned char *hwaddr, int hw_len,
    810 		     struct dhcp_netid *netids, time_t now);
    811 int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type);
    812 struct dhcp_config *find_config(struct dhcp_config *configs,
    813 				struct dhcp_context *context,
    814 				unsigned char *clid, int clid_len,
    815 				unsigned char *hwaddr, int hw_len,
    816 				int hw_type, char *hostname);
    817 void dhcp_update_configs(struct dhcp_config *configs);
    818 void dhcp_read_ethers(void);
    819 void check_dhcp_hosts(int fatal);
    820 struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr);
    821 char *strip_hostname(char *hostname);
    822 char *host_from_dns(struct in_addr addr);
    823 char *get_domain(struct in_addr addr);
    824 #endif
    825 
    826 /* lease.c */
    827 #ifdef HAVE_DHCP
    828 void lease_update_file(time_t now);
    829 void lease_update_dns();
    830 void lease_init(time_t now);
    831 struct dhcp_lease *lease_allocate(struct in_addr addr);
    832 void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
    833 		      unsigned char *clid, int hw_len, int hw_type, int clid_len);
    834 void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth);
    835 void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now);
    836 void lease_set_interface(struct dhcp_lease *lease, int interface);
    837 struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
    838 					unsigned char *clid, int clid_len);
    839 struct dhcp_lease *lease_find_by_addr(struct in_addr addr);
    840 void lease_prune(struct dhcp_lease *target, time_t now);
    841 void lease_update_from_configs(void);
    842 int do_script_run(time_t now);
    843 void rerun_scripts(void);
    844 #endif
    845 
    846 /* rfc2131.c */
    847 #ifdef HAVE_DHCP
    848 size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
    849 		  size_t sz, time_t now, int unicast_dest, int *is_inform);
    850 unsigned char *extended_hwaddr(int hwtype, int hwlen, unsigned char *hwaddr,
    851 			       int clid_len, unsigned char *clid, int *len_out);
    852 #endif
    853 
    854 /* dnsmasq.c */
    855 #ifdef HAVE_DHCP
    856 int make_icmp_sock(void);
    857 int icmp_ping(struct in_addr addr);
    858 #endif
    859 void send_event(int fd, int event, int data);
    860 void clear_cache_and_reload(time_t now);
    861 
    862 /* netlink.c */
    863 #ifdef HAVE_LINUX_NETWORK
    864 void netlink_init(void);
    865 void netlink_multicast(void);
    866 #endif
    867 
    868 /* bpf.c */
    869 #ifdef HAVE_BSD_NETWORK
    870 void init_bpf(void);
    871 void send_via_bpf(struct dhcp_packet *mess, size_t len,
    872 		  struct in_addr iface_addr, struct ifreq *ifr);
    873 #endif
    874 
    875 /* bpf.c or netlink.c */
    876 int iface_enumerate(void *parm, int (*ipv4_callback)(), int (*ipv6_callback)());
    877 
    878 /* dbus.c */
    879 #ifdef HAVE_DBUS
    880 char *dbus_init(void);
    881 void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset);
    882 void set_dbus_listeners(int *maxfdp, fd_set *rset, fd_set *wset, fd_set *eset);
    883 void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname);
    884 #endif
    885 
    886 /* helper.c */
    887 #if defined(HAVE_DHCP) && !defined(NO_FORK)
    888 int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd);
    889 void helper_write(void);
    890 void queue_script(int action, struct dhcp_lease *lease,
    891 		  char *hostname, time_t now);
    892 int helper_buf_empty(void);
    893 #endif
    894 
    895 /* tftp.c */
    896 #ifdef HAVE_TFTP
    897 void tftp_request(struct listener *listen, time_t now);
    898 void check_tftp_listeners(fd_set *rset, time_t now);
    899 #endif
    900