]> git.ipfire.org Git - people/ms/dnsmasq.git/blame_incremental - src/dnsmasq.h
import of dnsmasq-2.23.tar.gz
[people/ms/dnsmasq.git] / src / dnsmasq.h
... / ...
CommitLineData
1/* dnsmasq is Copyright (c) 2000-2005 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.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11*/
12
13/* Author's email: simon@thekelleys.org.uk */
14
15#define COPYRIGHT "Copyright (C) 2000-2005 Simon Kelley"
16
17#ifdef __linux__
18/* for pselect.... */
19# define _XOPEN_SOURCE 600
20/* but then DNS headers don't compile without.... */
21#define _BSD_SOURCE
22#endif
23
24/* get these before config.h for IPv6 stuff... */
25#include <sys/types.h>
26#include <netinet/in.h>
27
28/* get this before config.h too. */
29#include <syslog.h>
30#ifdef __APPLE__
31/* need this before arpa/nameser.h */
32# define BIND_8_COMPAT
33#endif
34#include <arpa/nameser.h>
35
36/* and this. */
37#include <getopt.h>
38
39#include "config.h"
40
41#include <arpa/inet.h>
42#include <sys/stat.h>
43#include <sys/socket.h>
44#include <sys/ioctl.h>
45#include <sys/select.h>
46#include <sys/wait.h>
47#if defined(__sun) || defined(__sun__)
48# include <sys/sockio.h>
49#endif
50#include <sys/time.h>
51#include <limits.h>
52#include <net/if.h>
53#include <unistd.h>
54#include <stdio.h>
55#include <string.h>
56#include <stdlib.h>
57#include <fcntl.h>
58#include <ctype.h>
59#include <signal.h>
60#include <time.h>
61#include <errno.h>
62#include <pwd.h>
63#include <grp.h>
64#include <stdarg.h>
65#if defined(__OpenBSD__) || defined(__NetBSD__)
66# include <netinet/if_ether.h>
67#else
68# include <net/ethernet.h>
69#endif
70#include <net/if_arp.h>
71#include <netinet/in_systm.h>
72#include <netinet/ip.h>
73#include <netinet/ip_icmp.h>
74#ifdef HAVE_BPF
75# include <net/bpf.h>
76# include <net/if_dl.h>
77#else
78# include <netpacket/packet.h>
79#endif
80#include <sys/uio.h>
81
82/* Min buffer size: we check after adding each record, so there must be
83 memory for the largest packet, and the largest record so the
84 min for DNS is PACKETSZ+MAXDNAME+RRFIXEDSZ which is < 1000.
85 This might be increased is EDNS packet size if greater than the minimum.
86 The buffer is also used for NETLINK, which needs to be about 2000
87 on systems with many interfaces/addresses. */
88#ifdef HAVE_RTNETLINK
89# define DNSMASQ_PACKETSZ PACKETSZ+MAXDNAME+RRFIXEDSZ
90#else
91# define DNSMASQ_PACKETSZ 2000
92#endif
93
94#define OPT_BOGUSPRIV 1
95#define OPT_FILTER 2
96#define OPT_LOG 4
97#define OPT_SELFMX 8
98#define OPT_NO_HOSTS 16
99#define OPT_NO_POLL 32
100#define OPT_DEBUG 64
101#define OPT_ORDER 128
102#define OPT_NO_RESOLV 256
103#define OPT_EXPAND 512
104#define OPT_LOCALMX 1024
105#define OPT_NO_NEG 2048
106#define OPT_NODOTS_LOCAL 4096
107#define OPT_NOWILD 8192
108#define OPT_ETHERS 16384
109#define OPT_RESOLV_DOMAIN 32768
110#define OPT_NO_FORK 65536
111#define OPT_AUTHORITATIVE 131072
112#define OPT_LOCALISE 262144
113#define OPT_DBUS 524288
114#define OPT_BOOTP_DYNAMIC 1048576
115
116struct all_addr {
117 union {
118 struct in_addr addr4;
119#ifdef HAVE_IPV6
120 struct in6_addr addr6;
121#endif
122 } addr;
123};
124
125struct bogus_addr {
126 struct in_addr addr;
127 struct bogus_addr *next;
128};
129
130/* dns doctor param */
131struct doctor {
132 struct in_addr in, out, mask;
133 struct doctor *next;
134};
135
136struct mx_srv_record {
137 char *name, *target;
138 int issrv, srvport, priority, weight;
139 unsigned int offset;
140 struct mx_srv_record *next;
141};
142
143struct txt_record {
144 char *name, *txt;
145 unsigned short class, len;
146 struct txt_record *next;
147};
148
149union bigname {
150 char name[MAXDNAME];
151 union bigname *next; /* freelist */
152};
153
154struct crec {
155 struct crec *next, *prev, *hash_next;
156 time_t ttd; /* time to die */
157 int uid;
158 union {
159 struct all_addr addr;
160 struct {
161 struct crec *cache;
162 int uid;
163 } cname;
164 } addr;
165 unsigned short flags;
166 union {
167 char sname[SMALLDNAME];
168 union bigname *bname;
169 char *namep;
170 } name;
171};
172
173#define F_IMMORTAL 1
174#define F_CONFIG 2
175#define F_REVERSE 4
176#define F_FORWARD 8
177#define F_DHCP 16
178#define F_NEG 32
179#define F_HOSTS 64
180#define F_IPV4 128
181#define F_IPV6 256
182#define F_BIGNAME 512
183#define F_UPSTREAM 1024
184#define F_SERVER 2048
185#define F_NXDOMAIN 4096
186#define F_QUERY 8192
187#define F_CNAME 16384
188#define F_NOERR 32768
189
190/* struct sockaddr is not large enough to hold any address,
191 and specifically not big enough to hold and IPv6 address.
192 Blech. Roll our own. */
193union mysockaddr {
194 struct sockaddr sa;
195 struct sockaddr_in in;
196#ifdef HAVE_BROKEN_SOCKADDR_IN6
197 /* early versions of glibc don't include sin6_scope_id in sockaddr_in6
198 but latest kernels _require_ it to be set. The choice is to have
199 dnsmasq fail to compile on back-level libc or fail to run
200 on latest kernels with IPv6. Or to do this: sorry that it's so gross. */
201 struct my_sockaddr_in6 {
202 sa_family_t sin6_family; /* AF_INET6 */
203 uint16_t sin6_port; /* transport layer port # */
204 uint32_t sin6_flowinfo; /* IPv6 traffic class & flow info */
205 struct in6_addr sin6_addr; /* IPv6 address */
206 uint32_t sin6_scope_id; /* set of interfaces for a scope */
207 } in6;
208#elif defined(HAVE_IPV6)
209 struct sockaddr_in6 in6;
210#endif
211};
212
213#define SERV_FROM_RESOLV 1 /* 1 for servers from resolv, 0 for command line. */
214#define SERV_NO_ADDR 2 /* no server, this domain is local only */
215#define SERV_LITERAL_ADDRESS 4 /* addr is the answer, not the server */
216#define SERV_HAS_SOURCE 8 /* source address specified */
217#define SERV_HAS_DOMAIN 16 /* server for one domain only */
218#define SERV_FOR_NODOTS 32 /* server for names with no domain part only */
219#define SERV_WARNED_RECURSIVE 64 /* avoid warning spam */
220#define SERV_FROM_DBUS 128 /* 1 if source is DBus */
221#define SERV_MARK 256 /* for mark-and-delete */
222#define SERV_TYPE (SERV_HAS_DOMAIN | SERV_FOR_NODOTS)
223
224
225struct serverfd {
226 int fd;
227 union mysockaddr source_addr;
228 struct serverfd *next;
229};
230
231struct server {
232 union mysockaddr addr, source_addr;
233 struct serverfd *sfd; /* non-NULL if this server has its own fd bound to
234 a source port */
235 char *domain; /* set if this server only handles a domain. */
236 int flags, tcpfd;
237 struct server *next;
238};
239
240struct irec {
241 union mysockaddr addr;
242 struct in_addr netmask; /* only valid for IPv4 */
243 struct irec *next;
244};
245
246struct listener {
247 int fd, tcpfd, family;
248 struct irec *iface; /* only valid for non-wildcard */
249 struct listener *next;
250};
251
252/* interface and address parms from command line. */
253struct iname {
254 char *name;
255 union mysockaddr addr;
256 int isloop, used;
257 struct iname *next;
258};
259
260/* resolv-file parms from command-line */
261struct resolvc {
262 struct resolvc *next;
263 int is_default, logged;
264 time_t mtime;
265 char *name;
266};
267
268/* adn-hosts parms from command-line */
269struct hostsfile {
270 struct hostsfile *next;
271 char *fname;
272 int index; /* matches to cache entries fro logging */
273};
274
275struct frec {
276 union mysockaddr source;
277 struct all_addr dest;
278 struct server *sentto;
279 unsigned int iface;
280 unsigned short orig_id, new_id;
281 int fd, forwardall;
282 unsigned int crc;
283 time_t time;
284 struct frec *next;
285};
286
287struct dhcp_lease {
288 int clid_len; /* length of client identifier */
289 unsigned char *clid; /* clientid */
290 char *hostname, *fqdn; /* name from client-hostname option or config */
291 time_t expires; /* lease expiry */
292 unsigned char hwaddr[ETHER_ADDR_LEN];
293 struct in_addr addr;
294 struct dhcp_lease *next;
295};
296
297struct dhcp_netid {
298 char *net;
299 struct dhcp_netid *next;
300};
301
302struct dhcp_netid_list {
303 struct dhcp_netid *list;
304 struct dhcp_netid_list *next;
305};
306struct dhcp_config {
307 unsigned int flags;
308 int clid_len; /* length of client identifier */
309 unsigned char *clid; /* clientid */
310 unsigned char hwaddr[ETHER_ADDR_LEN];
311 char *hostname;
312 struct dhcp_netid netid;
313 struct in_addr addr;
314 unsigned int lease_time, wildcard_mask;
315 struct dhcp_config *next;
316};
317
318#define CONFIG_DISABLE 1
319#define CONFIG_CLID 2
320#define CONFIG_HWADDR 4
321#define CONFIG_TIME 8
322#define CONFIG_NAME 16
323#define CONFIG_ADDR 32
324#define CONFIG_NETID 64
325#define CONFIG_NOCLID 128
326
327struct dhcp_opt {
328 int opt, len, is_addr;
329 unsigned char *val, *vendor_class;
330 struct dhcp_netid *netid;
331 struct dhcp_opt *next;
332};
333
334struct dhcp_boot {
335 char *file, *sname;
336 struct in_addr next_server;
337 struct dhcp_netid *netid;
338 struct dhcp_boot *next;
339};
340
341struct dhcp_vendor {
342 int len, is_vendor;
343 char *data;
344 struct dhcp_netid netid;
345 struct dhcp_vendor *next;
346};
347
348struct dhcp_context {
349 unsigned int lease_time, addr_epoch;
350 struct in_addr netmask, broadcast;
351 struct in_addr local, router;
352 struct in_addr start, end; /* range of available addresses */
353 int flags;
354 struct dhcp_netid netid;
355 struct dhcp_context *next, *current;
356};
357
358#define CONTEXT_STATIC 1
359#define CONTEXT_FILTER 2
360#define CONTEXT_NETMASK 4
361#define CONTEXT_BRDCAST 8
362
363
364typedef unsigned char u8;
365typedef unsigned short u16;
366typedef unsigned int u32;
367
368
369struct udp_dhcp_packet {
370 struct ip ip;
371 struct udphdr {
372 u16 uh_sport; /* source port */
373 u16 uh_dport; /* destination port */
374 u16 uh_ulen; /* udp length */
375 u16 uh_sum; /* udp checksum */
376 } udp;
377 struct dhcp_packet {
378 u8 op, htype, hlen, hops;
379 u32 xid;
380 u16 secs, flags;
381 struct in_addr ciaddr, yiaddr, siaddr, giaddr;
382 u8 chaddr[16], sname[64], file[128];
383 u8 options[312];
384 } data;
385};
386
387struct ping_result {
388 struct in_addr addr;
389 time_t time;
390 struct ping_result *next;
391};
392
393struct daemon {
394 /* datastuctures representing the command-line and
395 config file arguments. All set (including defaults)
396 in option.c */
397
398 unsigned int options;
399 struct resolvc default_resolv, *resolv_files;
400 struct mx_srv_record *mxnames;
401 struct txt_record *txt;
402 char *mxtarget;
403 char *lease_file;
404 char *username, *groupname;
405 char *domain_suffix;
406 char *runfile;
407 struct iname *if_names, *if_addrs, *if_except, *dhcp_except;
408 struct bogus_addr *bogus_addr;
409 struct server *servers;
410 int cachesize;
411 int port, query_port;
412 unsigned long local_ttl;
413 struct hostsfile *addn_hosts;
414 struct dhcp_context *dhcp;
415 struct dhcp_config *dhcp_conf;
416 struct dhcp_opt *dhcp_opts, *vendor_opts;
417 struct dhcp_vendor *dhcp_vendors;
418 struct dhcp_boot *boot_config;
419 struct dhcp_netid_list *dhcp_ignore;
420 int dhcp_max;
421 unsigned int min_leasetime;
422 struct doctor *doctors;
423 unsigned short edns_pktsz;
424
425 /* globally used stuff for DNS */
426 char *packet; /* packet buffer */
427 int packet_buff_sz; /* size of above */
428 char *namebuff; /* MAXDNAME size buffer */
429 struct serverfd *sfds;
430 struct irec *interfaces;
431 struct listener *listeners;
432 struct server *last_server;
433 int uptime_fd;
434
435 /* DHCP state */
436 int dhcpfd, dhcp_raw_fd, dhcp_icmp_fd, lease_fd;
437#ifdef HAVE_RTNETLINK
438 int netlinkfd;
439#endif
440 struct udp_dhcp_packet *dhcp_packet;
441 char *dhcp_buff, *dhcp_buff2;
442 struct ping_result *ping_results;
443
444 /* DBus stuff */
445#ifdef HAVE_DBUS
446 /* void * here to avoid depending on dbus headers outside dbus.c */
447 void *dbus;
448 struct watch *watches;
449#endif
450
451};
452
453/* cache.c */
454void cache_init(int cachesize, int log);
455void log_query(unsigned short flags, char *name, struct all_addr *addr,
456 unsigned short type, struct hostsfile *addn_hosts, int index);
457struct crec *cache_find_by_addr(struct crec *crecp,
458 struct all_addr *addr, time_t now,
459 unsigned short prot);
460struct crec *cache_find_by_name(struct crec *crecp,
461 char *name, time_t now, unsigned short prot);
462void cache_end_insert(void);
463void cache_start_insert(void);
464struct crec *cache_insert(char *name, struct all_addr *addr,
465 time_t now, unsigned long ttl, unsigned short flags);
466void cache_reload(int opts, char *buff, char *domain_suffix, struct hostsfile *addn_hosts);
467void cache_add_dhcp_entry(struct daemon *daemon, char *host_name, struct in_addr *host_address, time_t ttd);
468void cache_unhash_dhcp(void);
469void dump_cache(struct daemon *daemon);
470char *cache_get_name(struct crec *crecp);
471
472/* rfc1035.c */
473unsigned short extract_request(HEADER *header, unsigned int qlen,
474 char *name, unsigned short *typep);
475int setup_reply(HEADER *header, unsigned int qlen,
476 struct all_addr *addrp, unsigned short flags,
477 unsigned long local_ttl);
478void extract_addresses(HEADER *header, unsigned int qlen, char *namebuff,
479 time_t now, struct daemon *daemon);
480int answer_request(HEADER *header, char *limit, unsigned int qlen, struct daemon *daemon,
481 struct in_addr local_addr, struct in_addr local_netmask, time_t now);
482int check_for_bogus_wildcard(HEADER *header, unsigned int qlen, char *name,
483 struct bogus_addr *addr, time_t now);
484unsigned char *find_pseudoheader(HEADER *header, unsigned int plen,
485 unsigned int *len, unsigned char **p);
486int check_for_local_domain(char *name, time_t now, struct daemon *daemon);
487unsigned int questions_crc(HEADER *header, unsigned int plen, char *buff);
488int resize_packet(HEADER *header, unsigned int plen,
489 unsigned char *pheader, unsigned int hlen);
490
491/* util.c */
492unsigned short rand16(void);
493int legal_char(char c);
494int canonicalise(char *s);
495unsigned char *do_rfc1035_name(unsigned char *p, char *sval);
496void die(char *message, char *arg1);
497void complain(char *message, int lineno, char *file);
498void *safe_malloc(size_t size);
499int sa_len(union mysockaddr *addr);
500int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2);
501int hostname_isequal(char *a, char *b);
502time_t dnsmasq_time(int fd);
503int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask);
504int retry_send(void);
505void prettyprint_time(char *buf, unsigned int t);
506int prettyprint_addr(union mysockaddr *addr, char *buf);
507int parse_hex(char *in, unsigned char *out, int maxlen,
508 unsigned int *wildcard_mask);
509
510/* option.c */
511struct daemon *read_opts (int argc, char **argv, char *compile_opts);
512
513/* forward.c */
514void forward_init(int first);
515void reply_query(struct serverfd *sfd, struct daemon *daemon, time_t now);
516void receive_query(struct listener *listen, struct daemon *daemon, time_t now);
517unsigned char *tcp_request(struct daemon *daemon, int confd, time_t now,
518 struct in_addr local_addr, struct in_addr netmask);
519
520/* network.c */
521struct serverfd *allocate_sfd(union mysockaddr *addr, struct serverfd **sfds);
522void reload_servers(char *fname, struct daemon *daemon);
523void check_servers(struct daemon *daemon);
524int enumerate_interfaces(struct daemon *daemon, struct irec **chainp,
525 union mysockaddr *test_addrp, struct in_addr *netmaskp);
526struct listener *create_wildcard_listeners(int port);
527struct listener *create_bound_listeners(struct irec *interfaces, int port);
528
529/* dhcp.c */
530void dhcp_init(struct daemon *daemon);
531void dhcp_packet(struct daemon *daemon, time_t now);
532
533struct dhcp_context *address_available(struct dhcp_context *context, struct in_addr addr);
534struct dhcp_context *narrow_context(struct dhcp_context *context, struct in_addr taddr);
535int match_netid(struct dhcp_netid *check, struct dhcp_netid *pool);
536int address_allocate(struct dhcp_context *context, struct daemon *daemon,
537 struct in_addr *addrp, unsigned char *hwaddr,
538 struct dhcp_netid *netids, time_t now);
539struct dhcp_config *find_config(struct dhcp_config *configs,
540 struct dhcp_context *context,
541 unsigned char *clid, int clid_len,
542 unsigned char *hwaddr, char *hostname);
543void dhcp_update_configs(struct dhcp_config *configs);
544void dhcp_read_ethers(struct daemon *daemon);
545struct dhcp_config *config_find_by_address(struct dhcp_config *configs, struct in_addr addr);
546char *strip_hostname(struct daemon *daemon, char *hostname);
547char *host_from_dns(struct daemon *daemon, struct in_addr addr);
548struct dhcp_context *complete_context(struct daemon *daemon, struct in_addr local,
549 struct dhcp_context *current, struct in_addr netmask,
550 struct in_addr broadcast, struct in_addr relay,
551 struct in_addr primary);
552
553/* lease.c */
554void lease_update_file(int force, time_t now);
555void lease_update_dns(struct daemon *daemon);
556void lease_init(struct daemon *daemon, time_t now);
557struct dhcp_lease *lease_allocate(unsigned char *hwaddr, unsigned char *clid,
558 int clid_len, struct in_addr addr);
559int lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
560 unsigned char *clid, int clid_len);
561void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix);
562void lease_set_expires(struct dhcp_lease *lease, time_t exp);
563struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr,
564 unsigned char *clid, int clid_len);
565struct dhcp_lease *lease_find_by_addr(struct in_addr addr);
566void lease_prune(struct dhcp_lease *target, time_t now);
567void lease_update_from_configs(struct dhcp_config *dhcp_configs, char *domain);
568
569/* rfc2131.c */
570int dhcp_reply(struct daemon *daemon, struct dhcp_context *context, char *iface_name, unsigned int sz, time_t now);
571
572/* dnsmasq.c */
573int icmp_ping(struct daemon *daemon, struct in_addr addr);
574void clear_cache_and_reload(struct daemon *daemon, time_t now);
575
576/* isc.c */
577#ifdef HAVE_ISC_READER
578void load_dhcp(struct daemon *daemon, time_t now);
579#endif
580
581/* netlink.c */
582#ifdef HAVE_RTNETLINK
583int netlink_init(void);
584int netlink_process(struct daemon *daemon, int index,
585 struct in_addr relay, struct in_addr primary,
586 struct dhcp_context **retp);
587#endif
588
589/* dbus.c */
590#ifdef HAVE_DBUS
591char *dbus_init(struct daemon *daemon);
592void check_dbus_listeners(struct daemon *daemon,
593 fd_set *rset, fd_set *wset, fd_set *eset);
594int set_dbus_listeners(struct daemon *daemon, int maxfd,
595 fd_set *rset, fd_set *wset, fd_set *eset);
596#endif