OpenDNSSEC-signer  1.3.16
adapter.c
Go to the documentation of this file.
1 /*
2  * $Id: adapter.c 5432 2011-08-22 12:55:04Z matthijs $
3  *
4  * Copyright (c) 2009 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "adapter/adapter.h"
35 #include "shared/allocator.h"
36 #include "shared/file.h"
37 #include "shared/log.h"
38 #include "shared/status.h"
39 #include "signer/zone.h"
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 
44 static const char* adapter_str = "adapter";
45 
46 
53 {
54  ods_log_assert(adapter);
55  ods_log_assert(adapter->type);
56  ods_log_assert(adapter->configstr);
57 
58  switch(adapter->type) {
59  case ADAPTER_FILE:
60  return adfile_init();
61  break;
62  default:
63  ods_log_error("[%s] unable to initialize adapter: "
64  "unknown adapter", adapter_str);
65  return ODS_STATUS_ERR;
66  break;
67  }
68 
69  /* not reached */
70  return ODS_STATUS_ERR;
71 }
72 
73 
79 adapter_create(const char* str, adapter_mode type, int inbound)
80 {
81  allocator_type* allocator;
82  adapter_type* adapter;
83 
84  allocator = allocator_create(malloc, free);
85  if (!allocator) {
86  ods_log_error("[%s] unable to create adapter: create allocator failed",
87  adapter_str);
88  return NULL;
89  }
90  ods_log_assert(allocator);
91 
92  adapter = (adapter_type*) allocator_alloc(allocator, sizeof(adapter_type));
93  if (!adapter) {
94  ods_log_error("[%s] unable to create adapter: allocator failed",
95  adapter_str);
96  allocator_cleanup(allocator);
97  return NULL;
98  }
99 
100  adapter->allocator = allocator;
101  adapter->configstr = allocator_strdup(allocator, str);
102  adapter->type = type;
103  adapter->inbound = inbound;
104  adapter->data = allocator_alloc(allocator, sizeof(adapter_data));
105  return adapter;
106 }
107 
108 
109 /*
110  * Read zone from input adapter.
111  *
112  */
115 {
116  zone_type* adzone = (zone_type*) zone;
117  ods_status status = ODS_STATUS_OK;
118 
119  if (!adzone || !adzone->adinbound) {
120  ods_log_error("[%s] unable to read zone: no input adapter",
121  adapter_str);
122  return ODS_STATUS_ASSERT_ERR;
123  }
124  ods_log_assert(adzone);
125  ods_log_assert(adzone->adinbound);
127 
128  switch(adzone->adinbound->type) {
129  case ADAPTER_FILE:
130  ods_log_verbose("[%s] read zone %s from file input adapter %s",
131  adapter_str, adzone->name, adzone->adinbound->configstr);
132  status = adfile_read(zone, adzone->adinbound->configstr);
133  return status;
134  break;
135  default:
136  ods_log_error("[%s] unable to read zone %s from adapter: unknown "
137  "adapter", adapter_str, adzone->name);
138  return ODS_STATUS_ERR;
139  break;
140  }
141 
142  /* not reached */
143  return ODS_STATUS_ERR;
144 }
145 
146 
153 {
154  zone_type* adzone = (zone_type*) zone;
155  ods_status status = ODS_STATUS_OK;
156 
157  if (!adzone || !adzone->adoutbound) {
158  ods_log_error("[%s] unable to write zone: no output adapter",
159  adapter_str);
160  return ODS_STATUS_ASSERT_ERR;
161  }
162  ods_log_assert(adzone);
163  ods_log_assert(adzone->adoutbound);
165  if (!adzone->zonedata) {
166  ods_log_error("[%s] unable to write zone %s: no zone data",
167  adapter_str, adzone->name);
168  return ODS_STATUS_ASSERT_ERR;
169  }
170  ods_log_assert(adzone->zonedata);
171 
172  switch(adzone->adoutbound->type) {
173  case ADAPTER_FILE:
174  ods_log_verbose("[%s] write zone %s serial %u to output file "
175  "adapter %s", adapter_str, adzone->name,
176  adzone->zonedata->outbound_serial,
177  adzone->adinbound->configstr);
178  status = adfile_write(zone, adzone->adoutbound->configstr);
179  return status;
180  break;
181  default:
182  ods_log_error("[%s] unable to write zone %s to adapter: unknown "
183  "adapter", adapter_str, adzone->name);
184  return ODS_STATUS_ERR;
185  break;
186  }
187 
188  /* NOT REACHED */
189  return ODS_STATUS_ERR;
190 }
191 
192 
197 int
199 {
200  if (!a1 && !a2) {
201  return 0;
202  } else if (!a1) {
203  return -1;
204  } else if (!a2) {
205  return 1;
206  } else if (a1->inbound != a2->inbound) {
207  return a1->inbound - a2->inbound;
208  } else if (a1->type != a2->type) {
209  return a1->type - a2->type;
210  }
211  return ods_strcmp(a1->configstr, a2->configstr);
212 }
213 
214 
219 void
221 {
222  allocator_type* allocator;
223  if (!adapter) {
224  return;
225  }
226  allocator = adapter->allocator;
227  allocator_deallocate(allocator, (void*) adapter->configstr);
228  allocator_deallocate(allocator, (void*) adapter->data);
229  allocator_deallocate(allocator, (void*) adapter);
230  allocator_cleanup(allocator);
231  return;
232 }
uint32_t outbound_serial
Definition: zonedata.h:68
int adapter_compare(adapter_type *a1, adapter_type *a2)
Definition: adapter.c:198
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:67
const char * configstr
Definition: adapter.h:65
ods_status adfile_read(struct zone_struct *zone, const char *filename)
Definition: adfile.c:303
enum ods_enum_status ods_status
Definition: status.h:64
void ods_log_error(const char *format,...)
Definition: log.c:349
adapter_mode type
Definition: adapter.h:66
int ods_strcmp(const char *s1, const char *s2)
Definition: file.c:317
adapter_type * adoutbound
Definition: zone.h:78
allocator_type * allocator
Definition: adapter.h:68
adapter_data * data
Definition: adapter.h:69
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:48
ods_status adapter_read(struct zone_struct *zone)
Definition: adapter.c:114
adapter_type * adinbound
Definition: zone.h:77
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:122
ods_status adapter_write(struct zone_struct *zone)
Definition: adapter.c:152
adapter_type * adapter_create(const char *str, adapter_mode type, int inbound)
Definition: adapter.c:79
enum adapter_mode_enum adapter_mode
Definition: adapter.h:50
ods_status adfile_init(void)
Definition: adfile.c:58
void ods_log_verbose(const char *format,...)
Definition: log.c:301
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:153
const char * name
Definition: zone.h:67
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:136
zonedata_type * zonedata
Definition: zone.h:85
#define ods_log_assert(x)
Definition: log.h:141
ods_status adfile_write(struct zone_struct *zone, const char *filename)
Definition: adfile.c:430
void adapter_cleanup(adapter_type *adapter)
Definition: adapter.c:220
ods_status adapter_init(adapter_type *adapter)
Definition: adapter.c:52