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