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