OpenDNSSEC-signer
1.3.4
|
00001 /* 00002 * $Id: adapter.c 5432 2011-08-22 12:55:04Z matthijs $ 00003 * 00004 * Copyright (c) 2009 NLNet Labs. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00034 #include "adapter/adapter.h" 00035 #include "shared/allocator.h" 00036 #include "shared/file.h" 00037 #include "shared/log.h" 00038 #include "shared/status.h" 00039 #include "signer/zone.h" 00040 00041 #include <stdio.h> 00042 #include <stdlib.h> 00043 00044 static const char* adapter_str = "adapter"; 00045 00046 00051 ods_status 00052 adapter_init(adapter_type* adapter) 00053 { 00054 ods_log_assert(adapter); 00055 ods_log_assert(adapter->type); 00056 ods_log_assert(adapter->configstr); 00057 00058 switch(adapter->type) { 00059 case ADAPTER_FILE: 00060 return adfile_init(); 00061 break; 00062 default: 00063 ods_log_error("[%s] unable to initialize adapter: " 00064 "unknown adapter", adapter_str); 00065 return ODS_STATUS_ERR; 00066 break; 00067 } 00068 00069 /* not reached */ 00070 return ODS_STATUS_ERR; 00071 } 00072 00073 00078 adapter_type* 00079 adapter_create(const char* str, adapter_mode type, int inbound) 00080 { 00081 allocator_type* allocator; 00082 adapter_type* adapter; 00083 00084 allocator = allocator_create(malloc, free); 00085 if (!allocator) { 00086 ods_log_error("[%s] unable to create adapter: create allocator failed", 00087 adapter_str); 00088 return NULL; 00089 } 00090 ods_log_assert(allocator); 00091 00092 adapter = (adapter_type*) allocator_alloc(allocator, sizeof(adapter_type)); 00093 if (!adapter) { 00094 ods_log_error("[%s] unable to create adapter: allocator failed", 00095 adapter_str); 00096 allocator_cleanup(allocator); 00097 return NULL; 00098 } 00099 00100 adapter->allocator = allocator; 00101 adapter->configstr = allocator_strdup(allocator, str); 00102 adapter->type = type; 00103 adapter->inbound = inbound; 00104 adapter->data = allocator_alloc(allocator, sizeof(adapter_data)); 00105 return adapter; 00106 } 00107 00108 00109 /* 00110 * Read zone from input adapter. 00111 * 00112 */ 00113 ods_status 00114 adapter_read(struct zone_struct* zone) 00115 { 00116 zone_type* adzone = (zone_type*) zone; 00117 ods_status status = ODS_STATUS_OK; 00118 00119 if (!adzone || !adzone->adinbound) { 00120 ods_log_error("[%s] unable to read zone: no input adapter", 00121 adapter_str); 00122 return ODS_STATUS_ASSERT_ERR; 00123 } 00124 ods_log_assert(adzone); 00125 ods_log_assert(adzone->adinbound); 00126 ods_log_assert(adzone->adinbound->configstr); 00127 00128 switch(adzone->adinbound->type) { 00129 case ADAPTER_FILE: 00130 ods_log_verbose("[%s] read zone %s from file input adapter %s", 00131 adapter_str, adzone->name, adzone->adinbound->configstr); 00132 status = adfile_read(zone, adzone->adinbound->configstr); 00133 return status; 00134 break; 00135 default: 00136 ods_log_error("[%s] unable to read zone %s from adapter: unknown " 00137 "adapter", adapter_str, adzone->name); 00138 return ODS_STATUS_ERR; 00139 break; 00140 } 00141 00142 /* not reached */ 00143 return ODS_STATUS_ERR; 00144 } 00145 00146 00151 ods_status 00152 adapter_write(struct zone_struct* zone) 00153 { 00154 zone_type* adzone = (zone_type*) zone; 00155 ods_status status = ODS_STATUS_OK; 00156 00157 if (!adzone || !adzone->adoutbound) { 00158 ods_log_error("[%s] unable to write zone: no output adapter", 00159 adapter_str); 00160 return ODS_STATUS_ASSERT_ERR; 00161 } 00162 ods_log_assert(adzone); 00163 ods_log_assert(adzone->adoutbound); 00164 ods_log_assert(adzone->adoutbound->configstr); 00165 if (!adzone->zonedata) { 00166 ods_log_error("[%s] unable to write zone %s: no zone data", 00167 adapter_str, adzone->name); 00168 return ODS_STATUS_ASSERT_ERR; 00169 } 00170 ods_log_assert(adzone->zonedata); 00171 00172 switch(adzone->adoutbound->type) { 00173 case ADAPTER_FILE: 00174 ods_log_verbose("[%s] write zone %s serial %u to output file " 00175 "adapter %s", adapter_str, adzone->name, 00176 adzone->zonedata->outbound_serial, 00177 adzone->adinbound->configstr); 00178 status = adfile_write(zone, adzone->adoutbound->configstr); 00179 return status; 00180 break; 00181 default: 00182 ods_log_error("[%s] unable to write zone %s to adapter: unknown " 00183 "adapter", adapter_str, adzone->name); 00184 return ODS_STATUS_ERR; 00185 break; 00186 } 00187 00188 /* NOT REACHED */ 00189 return ODS_STATUS_ERR; 00190 } 00191 00192 00197 int 00198 adapter_compare(adapter_type* a1, adapter_type* a2) 00199 { 00200 if (!a1 && !a2) { 00201 return 0; 00202 } else if (!a1) { 00203 return -1; 00204 } else if (!a2) { 00205 return 1; 00206 } else if (a1->inbound != a2->inbound) { 00207 return a1->inbound - a2->inbound; 00208 } else if (a1->type != a2->type) { 00209 return a1->type - a2->type; 00210 } 00211 return ods_strcmp(a1->configstr, a2->configstr); 00212 } 00213 00214 00219 void 00220 adapter_cleanup(adapter_type* adapter) 00221 { 00222 allocator_type* allocator; 00223 if (!adapter) { 00224 return; 00225 } 00226 allocator = adapter->allocator; 00227 allocator_deallocate(allocator, (void*) adapter->configstr); 00228 allocator_deallocate(allocator, (void*) adapter->data); 00229 allocator_deallocate(allocator, (void*) adapter); 00230 allocator_cleanup(allocator); 00231 return; 00232 }