]> git.ipfire.org Git - people/ms/dnsmasq.git/blob - src/dnsmasq.h
import of dnsmasq-2.1.tar.gz
[people/ms/dnsmasq.git] / src / dnsmasq.h
1 /* dnsmasq is Copyright (c) 2000-2003 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 /* for pselect.... */
16 #define _XOPEN_SOURCE 600
17 /* but then DNS headers don't compile without.... */
18 #define _BSD_SOURCE
19 /* and also, on FreeBSD 5.0 ..... */
20 #define __BSD_VISIBLE 1
21
22 /* get these before config.h for IPv6 stuff... */
23 #include <sys/types.h>
24 #include <netinet/in.h>
25
26 /* get this before config.h too. */
27 #include <syslog.h>
28
29 #include "config.h"
30
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
33 #include <sys/stat.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <sys/select.h>
38 #if defined(__sun) || defined(__sun__)
39 #include <sys/sockio.h>
40 #endif
41 #include <sys/time.h>
42 #include <net/if.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <fcntl.h>
48 #include <ctype.h>
49 #include <signal.h>
50 #ifdef HAVE_GETOPT_LONG
51 # include <getopt.h>
52 #endif
53 #include <time.h>
54 #include <errno.h>
55 #include <pwd.h>
56 #include <grp.h>
57 #include <net/ethernet.h>
58 #include <net/if_arp.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #ifdef HAVE_PF_PACKET
62 #include <netpacket/packet.h>
63 #endif
64 #ifdef HAVE_BPF
65 #include <net/bpf.h>
66 #include <net/if_dl.h>
67 #endif
68 #include <sys/uio.h>
69
70 #define OPT_BOGUSPRIV 1
71 #define OPT_FILTER 2
72 #define OPT_LOG 4
73 #define OPT_SELFMX 8
74 #define OPT_NO_HOSTS 16
75 #define OPT_NO_POLL 32
76 #define OPT_DEBUG 64
77 #define OPT_ORDER 128
78 #define OPT_NO_RESOLV 256
79 #define OPT_EXPAND 512
80 #define OPT_LOCALMX 1024
81 #define OPT_NO_NEG 2048
82 #define OPT_NODOTS_LOCAL 4096
83
84 struct all_addr {
85 union {
86 struct in_addr addr4;
87 #ifdef HAVE_IPV6
88 struct in6_addr addr6;
89 #endif
90 } addr;
91 };
92
93 struct bogus_addr {
94 struct in_addr addr;
95 struct bogus_addr *next;
96 };
97
98 union bigname {
99 char name[MAXDNAME];
100 union bigname *next; /* freelist */
101 };
102
103 struct crec {
104 struct crec *next, *prev, *hash_next;
105 time_t ttd; /* time to die */
106 struct all_addr addr;
107 unsigned short flags;
108 union {
109 char sname[SMALLDNAME];
110 union bigname *bname;
111 char *namep;
112 } name;
113 };
114
115 #define F_IMMORTAL 1
116 #define F_CONFIG 2
117 #define F_REVERSE 4
118 #define F_FORWARD 8
119 #define F_DHCP 16
120 #define F_NEG 32
121 #define F_HOSTS 64
122 #define F_IPV4 128
123 #define F_IPV6 256
124 #define F_BIGNAME 512
125 #define F_UPSTREAM 1024
126 #define F_SERVER 2048
127 #define F_NXDOMAIN 4096
128 #define F_QUERY 8192
129 #define F_ADDN 16384
130 #define F_NOERR 32768
131
132 /* struct sockaddr is not large enough to hold any address,
133 and specifically not big enough to hold and IPv6 address.
134 Blech. Roll our own. */
135 union mysockaddr {
136 struct sockaddr sa;
137 struct sockaddr_in in;
138 #ifdef HAVE_BROKEN_SOCKADDR_IN6
139 /* early versions of glibc don't include sin6_scope_id in sockaddr_in6
140 but latest kernels _require_ it to be set. The choice is to have
141 dnsmasq fail to compile on back-level libc or fail to run
142 on latest kernels with IPv6. Or to do this: sorry that it's so gross. */
143 struct my_sockaddr_in6 {
144 sa_family_t sin6_family; /* AF_INET6 */
145 uint16_t sin6_port; /* transport layer port # */
146 uint32_t sin6_flowinfo; /* IPv6 traffic class & flow info */
147 struct in6_addr sin6_addr; /* IPv6 address */
148 uint32_t sin6_scope_id; /* set of interfaces for a scope */
149 } in6;
150 #elif defined(HAVE_IPV6)
151 struct sockaddr_in6 in6;
152 #endif
153 };
154
155 #define SERV_FROM_RESOLV 1 /* 1 for servers from resolv, 0 for command line. */
156 #define SERV_NO_ADDR 2 /* no server, this domain is local only */
157 #define SERV_LITERAL_ADDRESS 4 /* addr is the answer, not the server */
158 #define SERV_HAS_SOURCE 8 /* source address specified */
159 #define SERV_HAS_DOMAIN 16 /* server for one domain only */
160 #define SERV_FOR_NODOTS 32 /* server for names with no domain part only */
161 #define SERV_TYPE (SERV_HAS_DOMAIN | SERV_FOR_NODOTS)
162
163 struct serverfd {
164 int fd;
165 union mysockaddr source_addr;
166 struct serverfd *next;
167 };
168
169 struct server {
170 union mysockaddr addr, source_addr;
171 struct serverfd *sfd; /* non-NULL if this server has its own fd bound to
172 a source port */
173 char *domain; /* set if this server only handles a domain. */
174 int flags;
175 struct server *next;
176 };
177
178 /* linked list of all the interfaces in the system and
179 the sockets we have bound to each one. */
180 struct irec {
181 union mysockaddr addr;
182 int fd;
183 int valid;
184 struct irec *next;
185 };
186
187 /* interface and address parms from command line. */
188 struct iname {
189 char *name;
190 union mysockaddr addr;
191 int found;
192 struct iname *next;
193 };
194
195 /* resolv-file parms from command-line */
196 struct resolvc {
197 struct resolvc *next;
198 int is_default;
199 int logged;
200 char *name;
201 };
202
203 struct frec {
204 union mysockaddr source;
205 struct server *sentto;
206 unsigned short orig_id, new_id;
207 int fd;
208 time_t time;
209 struct frec *next;
210 };
211
212 struct dhcp_lease {
213 int clid_len; /* length of client identifier */
214 unsigned char *clid; /* clientid */
215 char *hostname, *fqdn; /* name from client-hostname option or config */
216 time_t expires; /* lease expiry */
217 unsigned char hwaddr[ETHER_ADDR_LEN];
218 struct in_addr addr;
219 struct dhcp_lease *next;
220 };
221
222 struct dhcp_config {
223 int clid_len; /* length of client identifier */
224 unsigned char *clid; /* clientid */
225 unsigned char hwaddr[ETHER_ADDR_LEN];
226 char *hostname;
227 struct in_addr addr;
228 unsigned int lease_time;
229 struct dhcp_config *next;
230 };
231
232 struct dhcp_opt {
233 int opt, len, is_addr;
234 unsigned char *val;
235 struct dhcp_opt *next;
236 };
237
238 struct dhcp_context {
239 int fd, rawfd, ifindex;
240 char *iface;
241 unsigned char hwaddr[ETHER_ADDR_LEN];
242 unsigned int lease_time;
243 struct in_addr serv_addr, netmask, broadcast;
244 struct in_addr start, end, last; /* range of available addresses */
245 struct dhcp_context *next;
246 };
247
248 typedef unsigned char u8;
249 typedef unsigned short u16;
250 typedef unsigned int u32;
251
252
253 struct udp_dhcp_packet {
254 struct ip ip;
255 struct udphdr {
256 u16 uh_sport; /* source port */
257 u16 uh_dport; /* destination port */
258 u16 uh_ulen; /* udp length */
259 u16 uh_sum; /* udp checksum */
260 } udp;
261 struct dhcp_packet {
262 u8 op, htype, hlen, hops;
263 u32 xid;
264 u16 secs, flags;
265 struct in_addr ciaddr, yiaddr, siaddr, giaddr;
266 u8 chaddr[16], sname[64], file[128];
267 u32 cookie;
268 u8 options[308];
269 } data;
270 };
271
272
273 /* cache.c */
274 void cache_init(int cachesize, int log);
275 void log_query(unsigned short flags, char *name, struct all_addr *addr);
276 struct crec *cache_find_by_addr(struct crec *crecp,
277 struct all_addr *addr, time_t now,
278 unsigned short prot);
279 struct crec *cache_find_by_name(struct crec *crecp,
280 char *name, time_t now, unsigned short prot);
281 void cache_end_insert(void);
282 void cache_start_insert(void);
283 void cache_insert(char *name, struct all_addr *addr,
284 time_t now, unsigned long ttl, unsigned short flags);
285 void cache_reload(int opts, char *buff, char *domain_suffix, char *addn_hosts);
286 void cache_add_dhcp_entry(char *host_name, struct in_addr *host_address,
287 time_t ttd, unsigned short flags);
288 void cache_unhash_dhcp(void);
289 void dump_cache(int debug, int size);
290 char *cache_get_name(struct crec *crecp);
291
292 /* rfc1035.c */
293 unsigned short extract_request(HEADER *header, unsigned int qlen, char *name);
294 int setup_reply(HEADER *header, unsigned int qlen,
295 struct all_addr *addrp, unsigned short flags,
296 unsigned long local_ttl);
297 void extract_addresses(HEADER *header, unsigned int qlen, char *namebuff, time_t now);
298 void extract_neg_addrs(HEADER *header, unsigned int qlen, char *namebuff, time_t now);
299 int answer_request(HEADER *header, char *limit, unsigned int qlen, char *mxname,
300 char *mxtarget, unsigned int options, time_t now, unsigned long local_ttl,
301 char *namebuff);
302 int check_for_bogus_wildcard(HEADER *header, unsigned int qlen, char *name,
303 struct bogus_addr *addr, time_t now);
304
305 /* util.c */
306 unsigned short rand16(void);
307 int legal_char(char c);
308 int canonicalise(char *s);
309 void die(char *message, char *arg1);
310 void complain(char *message, char *arg1);
311 void *safe_malloc(int size);
312 char *safe_string_alloc(char *cp);
313 int sa_len(union mysockaddr *addr);
314 int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2);
315 int hostname_isequal(unsigned char *a, unsigned char *b);
316
317 /* option.c */
318 unsigned int read_opts(int argc, char **argv, char *buff, struct resolvc **resolv_file,
319 char **mxname, char **mxtarget, char **lease_file,
320 char **username, char **groupname,
321 char **domain_suffix, char **runfile,
322 struct iname **if_names, struct iname **if_addrs, struct iname **if_except,
323 struct bogus_addr **bogus_addr, struct server **serv_addrs, int *cachesize,
324 int *port, int *query_port, unsigned long *local_ttl, char **addn_hosts,
325 struct dhcp_context **dhcp, struct dhcp_config **dhcp_conf, struct dhcp_opt **opts,
326 char **dhcp_file, char **dhcp_sname, struct in_addr *dhcp_next_server);
327
328 /* forward.c */
329 void forward_init(int first);
330 void reap_forward(int fd);
331 struct server *forward_query(int udpfd, union mysockaddr *udpaddr, HEADER *header,
332 int plen, unsigned int options, char *dnamebuff,
333 struct server *servers, struct server *last_server,
334 time_t now, unsigned long local_ttl);
335 struct server *reply_query(int fd, int options, char *packet, time_t now,
336 char *dnamebuff, struct server *last_server,
337 struct bogus_addr *bogus_nxdomain);
338
339 /* network.c */
340 struct server *reload_servers(char *fname, char *buff, struct server *servers, int query_port);
341 struct server *check_servers(struct server *new, struct irec *interfaces, struct serverfd **sfds);
342 char *enumerate_interfaces(struct irec **interfaces,
343 struct iname *names,
344 struct iname *addrs,
345 struct iname *except,
346 struct dhcp_context *dhcp,
347 int port);
348
349 /* dhcp.c */
350 void dhcp_packet(struct dhcp_context *context, char *packet,
351 struct dhcp_opt *dhcp_opts,
352 struct dhcp_config *dhcp_configs,
353 time_t now, char *namebuff, char *domain_suffix,
354 char *dhcp_file, char *dhcp_sname, struct in_addr dhcp_next_server);
355 int address_available(struct dhcp_context *context, struct in_addr addr);
356 int address_allocate(struct dhcp_context *context, struct dhcp_config *configs,
357 struct in_addr *addrp);
358 struct dhcp_config *find_config(struct dhcp_config *configs,
359 struct dhcp_context *context,
360 unsigned char *clid, int clid_len,
361 unsigned char *hwaddr, char *hostname);
362
363 void set_configs_from_cache(struct dhcp_config *configs);
364 /* lease.c */
365 void lease_update_dns(int force_dns);
366 int lease_init(char *lease_file, char *domain, char *buff,
367 char *buff2, time_t now, struct dhcp_config *dhcp_configs);
368 struct dhcp_lease *lease_allocate(unsigned char *clid, int clid_len, struct in_addr addr);
369 void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr);
370 void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix);
371 void lease_set_expires(struct dhcp_lease *lease, time_t exp);
372 struct dhcp_lease *lease_find_by_client(unsigned char *clid, int clid_len);
373 struct dhcp_lease *lease_find_by_addr(struct in_addr addr);
374 void lease_prune(struct dhcp_lease *target, time_t now);
375
376 /* rfc2131.c */
377 int dhcp_reply(struct dhcp_context *context, struct dhcp_packet *mess,
378 unsigned int sz, time_t now, char *namebuff,
379 struct dhcp_opt *dhcp_opts, struct dhcp_config *dhcp_configs,
380 char *domain_suffix, char *dhcp_file, char *dhcp_sname,
381 struct in_addr dhcp_next_server);