xfrd.h revision 1.1
1/*
2 * xfrd.h - XFR (transfer) Daemon header file. Coordinates SOA updates.
3 *
4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5 *
6 * See LICENSE for the license.
7 *
8 */
9
10#ifndef XFRD_H
11#define XFRD_H
12
13#include <config.h>
14#include "netio.h"
15#include "rbtree.h"
16#include "namedb.h"
17#include "options.h"
18#include "dns.h"
19#include "tsig.h"
20
21struct nsd;
22struct region;
23struct buffer;
24struct xfrd_tcp;
25struct xfrd_tcp_set;
26struct notify_zone_t;
27typedef struct xfrd_state xfrd_state_t;
28typedef struct xfrd_zone xfrd_zone_t;
29typedef struct xfrd_soa xfrd_soa_t;
30/*
31 * The global state for the xfrd daemon process.
32 * The time_t times are epochs in secs since 1970, absolute times.
33 */
34struct xfrd_state {
35	/* time when daemon was last started */
36	time_t xfrd_start_time;
37	struct region* region;
38	netio_type* netio;
39	struct nsd* nsd;
40
41	struct xfrd_tcp_set* tcp_set;
42	/* packet buffer for udp packets */
43	struct buffer* packet;
44	/* udp waiting list */
45	struct xfrd_zone *udp_waiting_first, *udp_waiting_last;
46	/* number of udp sockets (for sending queries) in use */
47	size_t udp_use_num;
48
49	/* current time is cached */
50	uint8_t got_time;
51	time_t current_time;
52
53	/* timer for NSD reload */
54	struct timespec reload_timeout;
55	netio_handler_type reload_handler;
56	/* last reload must have caught all zone updates before this time */
57	time_t reload_cmd_last_sent;
58	uint8_t can_send_reload;
59
60	/* communication channel with server_main */
61	netio_handler_type ipc_handler;
62	uint8_t ipc_is_soa;
63	uint8_t parent_soa_info_pass;
64	struct xfrd_tcp *ipc_conn;
65	struct buffer* ipc_pass;
66	/* sending ipc to server_main */
67	struct xfrd_tcp *ipc_conn_write;
68	uint8_t need_to_send_reload;
69	uint8_t need_to_send_quit;
70	uint8_t sending_zone_state;
71	uint8_t	ipc_send_blocked;
72	stack_type* dirty_zones; /* stack of xfrd_zone* */
73
74	/* xfrd shutdown flag */
75	uint8_t shutdown;
76
77	/* tree of zones, by apex name, contains xfrd_zone_t*. Only secondary zones. */
78	rbtree_t *zones;
79
80	/* tree of zones, by apex name, contains notify_zone_t*. All zones. */
81	rbtree_t *notify_zones;
82	/* number of notify_zone_t active using UDP socket */
83	int notify_udp_num;
84	/* first and last notify_zone_t* entries waiting for a UDP socket */
85	struct notify_zone_t *notify_waiting_first, *notify_waiting_last;
86};
87
88/*
89 * XFR daemon SOA information kept in network format.
90 * This is in packet order.
91 */
92struct xfrd_soa {
93	/* name of RR is zone apex dname */
94	uint16_t type; /* = TYPE_SOA */
95	uint16_t klass; /* = CLASS_IN */
96	uint32_t ttl;
97	uint16_t rdata_count; /* = 7 */
98	/* format is 1 octet length, + wireformat dname.
99	   one more octet since parse_dname_wire_from_packet needs it.
100	   maximum size is allocated to avoid memory alloc/free. */
101	uint8_t prim_ns[MAXDOMAINLEN + 2];
102	uint8_t email[MAXDOMAINLEN + 2];
103	uint32_t serial;
104	uint32_t refresh;
105	uint32_t retry;
106	uint32_t expire;
107	uint32_t minimum;
108};
109
110
111/*
112 * XFRD state for a single zone
113 */
114struct xfrd_zone {
115	rbnode_t node;
116
117	/* name of the zone */
118	const dname_type* apex;
119	const char* apex_str;
120
121	/* Three types of soas:
122	 * NSD: in use by running server
123	 * disk: stored on disk in db/diff file
124	 * notified: from notification, could be available on a master.
125	 * And the time the soa was acquired (start time for timeouts).
126	 * If the time==0, no SOA is available.
127	 */
128	xfrd_soa_t soa_nsd;
129	time_t soa_nsd_acquired;
130	xfrd_soa_t soa_disk;
131	time_t soa_disk_acquired;
132	xfrd_soa_t soa_notified;
133	time_t soa_notified_acquired;
134
135	enum xfrd_zone_state {
136		xfrd_zone_ok,
137		xfrd_zone_refreshing,
138		xfrd_zone_expired
139	} state;
140
141	/* if state is dirty it needs to be sent to server_main.
142	 * it is also on the dirty_stack. Not saved on disk. */
143	uint8_t dirty;
144
145	/* master to try to transfer from, number for persistence */
146	acl_options_t* master;
147	int master_num;
148	int next_master; /* -1 or set by notify where to try next */
149	/* round of xfrattempts, -1 is waiting for timeout */
150	int round_num;
151	zone_options_t* zone_options;
152	int fresh_xfr_timeout;
153
154	/* handler for timeouts */
155	struct timespec timeout;
156	netio_handler_type zone_handler;
157
158	/* tcp connection zone is using, or -1 */
159	int tcp_conn;
160	/* zone is waiting for a tcp connection */
161	uint8_t tcp_waiting;
162	/* next zone in waiting list */
163	xfrd_zone_t* tcp_waiting_next;
164	/* zone is waiting for a udp connection (tcp is preferred) */
165	uint8_t udp_waiting;
166	/* next zone in waiting list for UDP */
167	xfrd_zone_t* udp_waiting_next;
168
169	/* xfr message handling data */
170	/* query id */
171	uint16_t query_id;
172	uint32_t msg_seq_nr; /* number of messages already handled */
173	uint32_t msg_old_serial, msg_new_serial; /* host byte order */
174	size_t msg_rr_count;
175	uint8_t msg_is_ixfr; /* 1:IXFR detected. 2:middle IXFR SOA seen. */
176#ifdef TSIG
177	tsig_record_type tsig; /* tsig state for IXFR/AXFR */
178#endif
179};
180
181enum xfrd_packet_result {
182	xfrd_packet_bad, /* drop the packet/connection */
183	xfrd_packet_more, /* more packets to follow on tcp */
184	xfrd_packet_notimpl, /* server responded with NOTIMPL or FORMATERR */
185	xfrd_packet_tcp, /* try tcp connection */
186	xfrd_packet_transfer, /* server responded with transfer*/
187	xfrd_packet_newlease /* no changes, soa OK */
188};
189
190/*
191   Division of the (portably: 1024) max number of sockets that can be open.
192   The sum of the below numbers should be below the user limit for sockets
193   open, or you see errors in your logfile.
194   And it should be below FD_SETSIZE, to be able to select() on replies.
195   Note that also some sockets are used for writing the ixfr.db, xfrd.state
196   files and for the pipes to the main parent process.
197*/
198#define XFRD_MAX_TCP 50 /* max number of TCP AXFR/IXFR concurrent connections.*/
199			/* Each entry has 64Kb buffer preallocated.*/
200#define XFRD_MAX_UDP 100 /* max number of UDP sockets at a time for IXFR */
201#define XFRD_MAX_UDP_NOTIFY 50 /* max concurrent UDP sockets for NOTIFY */
202
203extern xfrd_state_t* xfrd;
204
205/* start xfrd, new start. Pass socket to server_main. */
206void xfrd_init(int socket, struct nsd* nsd);
207
208/* get the current time epoch. Cached for speed. */
209time_t xfrd_time();
210
211/*
212 * Handle final received packet from network.
213 * returns enum of packet discovery results
214 */
215enum xfrd_packet_result xfrd_handle_received_xfr_packet(
216	xfrd_zone_t* zone, buffer_type* packet);
217
218/* set timer to specific value */
219void xfrd_set_timer(xfrd_zone_t* zone, time_t t);
220/* set refresh timer of zone to refresh at time now */
221void xfrd_set_refresh_now(xfrd_zone_t* zone);
222/* unset the timer - no more timeouts, for when zone is queued */
223void xfrd_unset_timer(xfrd_zone_t* zone);
224
225/*
226 * Make a new request to next master server.
227 * uses next_master if set (and a fresh set of rounds).
228 * otherwised, starts new round of requests if none started already.
229 * starts next round of requests if at last master.
230 * if too many rounds of requests, sets timer for next retry.
231 */
232void xfrd_make_request(xfrd_zone_t* zone);
233
234/*
235 * send packet via udp (returns UDP fd source socket) to acl addr.
236 * returns -1 on failure.
237 */
238int xfrd_send_udp(acl_options_t* acl, buffer_type* packet, acl_options_t* ifc);
239
240/*
241 * read from udp port packet into buffer, returns 0 on failure
242 */
243int xfrd_udp_read_packet(buffer_type* packet, int fd);
244
245/*
246 * Release udp socket that a zone is using
247 */
248void xfrd_udp_release(xfrd_zone_t* zone);
249
250/*
251 * Get a static buffer for temporary use (to build a packet).
252 */
253struct buffer* xfrd_get_temp_buffer();
254
255/*
256 * TSIG sign outgoing request. Call if acl has a key.
257 */
258#ifdef TSIG
259void xfrd_tsig_sign_request(buffer_type* packet, struct tsig_record* tsig,
260        acl_options_t* acl);
261#endif
262
263/* handle incoming soa information (NSD is running it, time acquired=guess).
264   Pass soa=NULL,acquired=now if NSD has nothing loaded for the zone
265   (i.e. zonefile was deleted). */
266void xfrd_handle_incoming_soa(xfrd_zone_t* zone, xfrd_soa_t* soa,
267	time_t acquired);
268/* handle a packet passed along ipc route. acl is the one that accepted
269   the packet. The packet is the network blob received. */
270void xfrd_handle_passed_packet(buffer_type* packet, int acl_num);
271
272/* send expiry notify for all zones to nsd (sets all dirty). */
273void xfrd_send_expy_all_zones();
274
275/* try to reopen the logfile. */
276void xfrd_reopen_logfile();
277
278/* copy SOA info from rr to soa struct. */
279void xfrd_copy_soa(xfrd_soa_t* soa, rr_type* rr);
280
281/* check for failed updates - it is assumed that now the reload has
282   finished, and all zone SOAs have been sent. */
283void xfrd_check_failed_updates();
284
285/*
286 * Prepare zones for a reload, this sets the times on the zones to be
287 * before the current time, so the reload happens after.
288 */
289void xfrd_prepare_zones_for_reload();
290
291/* Bind a local interface to a socket descriptor, return 1 on success */
292int xfrd_bind_local_interface(int sockd, acl_options_t* ifc,
293	acl_options_t* acl, int tcp);
294
295#endif /* XFRD_H */
296