OpenDNSSEC-signer  1.4.9
ixfr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #include "config.h"
33 #include "shared/util.h"
34 #include "signer/ixfr.h"
35 #include "signer/rrset.h"
36 #include "signer/zone.h"
37 
38 static const char* ixfr_str = "journal";
39 
40 
45 static part_type*
46 part_create(allocator_type* allocator)
47 {
48  part_type* part = NULL;
49  ods_log_assert(allocator);
50 
51  part = (part_type*) allocator_alloc(allocator, sizeof(part_type));
52  if (!part) {
53  ods_log_error("[%s] unable to create ixfr part: "
54  "allocator_alloc() failed", ixfr_str);
55  return NULL;
56  }
57  part->soaplus = NULL;
58  part->soamin = NULL;
59  part->plus = ldns_rr_list_new();
60  if (!part->plus) {
61  ods_log_error("[%s] unable to create ixfr part: "
62  "ldns_rr_list_new() failed", ixfr_str);
63  allocator_deallocate(allocator, (void*) part);
64  return NULL;
65  }
66  part->min = ldns_rr_list_new();
67  if (!part->min) {
68  ods_log_error("[%s] unable to create ixfr part: "
69  "ldns_rr_list_new() failed", ixfr_str);
70  ldns_rr_list_free(part->plus);
71  allocator_deallocate(allocator, (void*) part);
72  return NULL;
73  }
74  return part;
75 }
76 
77 
82 static void
83 part_cleanup(allocator_type* allocator, part_type* part)
84 {
85  if (!part || !allocator) {
86  return;
87  }
88  ldns_rr_list_deep_free(part->min);
89  ldns_rr_list_free(part->plus);
90  allocator_deallocate(allocator, (void*) part);
91  return;
92 }
93 
94 
99 ixfr_type*
100 ixfr_create(void* zone)
101 {
102  size_t i = 0;
103  ixfr_type* xfr = NULL;
104  zone_type* z = (zone_type*) zone;
105 
106  ods_log_assert(z);
107  ods_log_assert(z->name);
109 
110  xfr = (ixfr_type*) allocator_alloc(z->allocator, sizeof(ixfr_type));
111  if (!xfr) {
112  ods_log_error("[%s] unable to create ixfr for zone %s: "
113  "allocator_alloc() failed", ixfr_str, z->name);
114  return NULL;
115  }
116  for (i=0; i < IXFR_MAX_PARTS; i++) {
117  xfr->part[i] = NULL;
118  }
119  xfr->zone = zone;
120  lock_basic_init(&xfr->ixfr_lock);
121  return xfr;
122 }
123 
124 
129 void
130 ixfr_add_rr(ixfr_type* ixfr, ldns_rr* rr)
131 {
132  zone_type* zone = NULL;
133  if (!ixfr || !rr) {
134  return;
135  }
136  zone = (zone_type*) ixfr->zone;
137  ods_log_assert(zone);
138  ods_log_assert(zone->db);
139  if (!zone->db->is_initialized) {
140  /* no ixfr yet */
141  return;
142  }
143  ods_log_assert(ixfr->part[0]);
144  ods_log_assert(ixfr->part[0]->plus);
145  if (!ldns_rr_list_push_rr(ixfr->part[0]->plus, rr)) {
146  ods_fatal_exit("[%s] fatal unable to +RR: ldns_rr_list_push_rr() failed",
147  ixfr_str);
148  }
149  if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
150  ixfr->part[0]->soaplus = rr;
151  }
152  return;
153 }
154 
155 
160 void
161 ixfr_del_rr(ixfr_type* ixfr, ldns_rr* rr)
162 {
163  zone_type* zone = NULL;
164  if (!ixfr || !rr) {
165  return;
166  }
167  zone = (zone_type*) ixfr->zone;
168  ods_log_assert(zone);
169  ods_log_assert(zone->db);
170  if (!zone->db->is_initialized) {
171  /* no ixfr yet */
172  return;
173  }
174  ods_log_assert(ixfr->part[0]);
175  ods_log_assert(ixfr->part[0]->min);
176  if (!ldns_rr_list_push_rr(ixfr->part[0]->min, rr)) {
177  ods_fatal_exit("[%s] fatal unable to -RR: ldns_rr_list_push_rr() failed",
178  ixfr_str);
179  }
180  if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
181  ixfr->part[0]->soamin = rr;
182  }
183  return;
184 }
185 
186 
191 static int
192 part_rr_list_print_nonsoa(FILE* fd, ldns_rr_list* list)
193 {
194  size_t i = 0;
195  int error = 0;
196  if (!list || !fd) {
197  return 1;
198  }
199  for (i = 0; i < ldns_rr_list_rr_count(list); i++) {
200  if (ldns_rr_get_type(ldns_rr_list_rr(list, i)) != LDNS_RR_TYPE_SOA) {
201  if (util_rr_print(fd, ldns_rr_list_rr(list, i)) != ODS_STATUS_OK) {
202  error = 1;
203  }
204  }
205  }
206  return error;
207 }
208 
209 
214 static void
215 part_print(FILE* fd, ixfr_type* ixfr, size_t i)
216 {
217  zone_type* zone = NULL;
218  part_type* part = NULL;
219  int error = 0;
220  if (!ixfr || !fd) {
221  return;
222  }
223  zone = (zone_type*) ixfr->zone;
224  part = ixfr->part[i];
225  if (!part) {
226  return;
227  }
228  ods_log_assert(part->min);
229  ods_log_assert(part->plus);
230  ods_log_assert(part->soamin);
231  ods_log_assert(part->soaplus);
232  if (util_rr_print(fd, part->soamin) != ODS_STATUS_OK) {
233  zone->adoutbound->error = 1;
234  }
235  error = part_rr_list_print_nonsoa(fd, part->min);
236  if (error) {
237  zone->adoutbound->error = 1;
238  }
239  if (util_rr_print(fd, part->soaplus) != ODS_STATUS_OK) {
240  zone->adoutbound->error = 1;
241  }
242  error = part_rr_list_print_nonsoa(fd, part->plus);
243  if (error) {
244  zone->adoutbound->error = 1;
245  }
246  return;
247 }
248 
249 
254 void
255 ixfr_print(FILE* fd, ixfr_type* ixfr)
256 {
257  int i = 0;
258  if (!ixfr || !fd) {
259  return;
260  }
261  ods_log_debug("[%s] print ixfr", ixfr_str);
262  for (i = IXFR_MAX_PARTS - 1; i >= 0; i--) {
263  ods_log_deeebug("[%s] print ixfr part #%d", ixfr_str, i);
264  part_print(fd, ixfr, i);
265  }
266  return;
267 }
268 
269 
274 void
276 {
277  int i = 0;
278  zone_type* zone = NULL;
279  if (!ixfr) {
280  return;
281  }
282  zone = (zone_type*) ixfr->zone;
283  ods_log_assert(zone);
284  ods_log_assert(zone->allocator);
285  ods_log_debug("[%s] purge ixfr for zone %s", ixfr_str, zone->name);
286  for (i = IXFR_MAX_PARTS - 1; i >= 0; i--) {
287  if (i == (IXFR_MAX_PARTS - 1)) {
288  part_cleanup(zone->allocator, ixfr->part[i]);
289  ixfr->part[i] = NULL;
290  } else {
291  ixfr->part[i+1] = ixfr->part[i];
292  ixfr->part[i] = NULL;
293  }
294  }
295  ixfr->part[0] = part_create(zone->allocator);
296  if (!ixfr->part[0]) {
297  ods_fatal_exit("[%s] fatal unable to purge ixfr for zone %s: "
298  "part_create() failed", ixfr_str, zone->name);
299  }
300  return;
301 }
302 
303 
308 void
310 {
311  int i = 0;
312  zone_type* z = NULL;
313  lock_basic_type ixfr_lock;
314  if (!ixfr) {
315  return;
316  }
317  z = (zone_type*) ixfr->zone;
318  ixfr_lock = ixfr->ixfr_lock;
319  for (i = IXFR_MAX_PARTS - 1; i >= 0; i--) {
320  part_cleanup(z->allocator, ixfr->part[i]);
321  }
322  allocator_deallocate(z->allocator, (void*) ixfr);
323  lock_basic_destroy(&ixfr_lock);
324  return;
325 }
void ixfr_cleanup(ixfr_type *ixfr)
Definition: ixfr.c:309
void ixfr_print(FILE *fd, ixfr_type *ixfr)
Definition: ixfr.c:255
void ods_log_debug(const char *format,...)
Definition: log.c:270
#define lock_basic_destroy(lock)
Definition: locks.h:93
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:66
void ods_fatal_exit(const char *format,...)
Definition: log.c:382
void ixfr_add_rr(ixfr_type *ixfr, ldns_rr *rr)
Definition: ixfr.c:130
void ixfr_del_rr(ixfr_type *ixfr, ldns_rr *rr)
Definition: ixfr.c:161
unsigned error
Definition: adapter.h:63
part_type * part[IXFR_MAX_PARTS]
Definition: ixfr.h:61
void ods_log_error(const char *format,...)
Definition: log.c:334
ldns_rr_list * plus
Definition: ixfr.h:51
#define IXFR_MAX_PARTS
Definition: ixfr.h:40
adapter_type * adoutbound
Definition: zone.h:82
ods_status util_rr_print(FILE *fd, const ldns_rr *rr)
Definition: util.c:378
ldns_rr * soamin
Definition: ixfr.h:48
namedb_type * db
Definition: zone.h:86
ldns_rr * soaplus
Definition: ixfr.h:50
unsigned is_initialized
Definition: namedb.h:55
void * zone
Definition: ixfr.h:60
int lock_basic_type
Definition: locks.h:91
allocator_type * allocator
Definition: zone.h:67
query_state ixfr(query_type *q, engine_type *engine)
Definition: axfr.c:387
lock_basic_type ixfr_lock
Definition: ixfr.h:62
#define lock_basic_init(lock)
Definition: locks.h:92
const char * name
Definition: zone.h:76
void ods_log_deeebug(const char *format,...)
Definition: log.c:254
void ixfr_purge(ixfr_type *ixfr)
Definition: ixfr.c:275
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:135
ldns_rr_list * min
Definition: ixfr.h:49
#define ods_log_assert(x)
Definition: log.h:154
ixfr_type * ixfr_create(void *zone)
Definition: ixfr.c:100