OpenDNSSEC-signer 1.3.0
/build/buildd/opendnssec-1.3.0/signer/src/adapter/adapter.c
Go to the documentation of this file.
00001 /*
00002  * $Id: adapter.c 5320 2011-07-12 10:42:26Z jakob $
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", adapter_str);
00121         return ODS_STATUS_ASSERT_ERR;
00122     }
00123     ods_log_assert(adzone);
00124     ods_log_assert(adzone->adinbound);
00125     ods_log_assert(adzone->adinbound->configstr);
00126 
00127     switch(adzone->adinbound->type) {
00128         case ADAPTER_FILE:
00129             ods_log_verbose("[%s] read zone %s from file input adapter %s",
00130                 adapter_str, adzone->name, adzone->adinbound->configstr);
00131             status = adfile_read(zone, adzone->adinbound->configstr);
00132             return status;
00133             break;
00134         default:
00135             ods_log_error("[%s] unable to read zone %s from adapter: unknown "
00136                 "adapter", adapter_str, adzone->name);
00137             return ODS_STATUS_ERR;
00138             break;
00139     }
00140 
00141     /* not reached */
00142     return ODS_STATUS_ERR;
00143 }
00144 
00145 
00150 ods_status
00151 adapter_write(struct zone_struct* zone)
00152 {
00153     zone_type* adzone = (zone_type*) zone;
00154     ods_status status = ODS_STATUS_OK;
00155 
00156     if (!adzone || !adzone->adoutbound) {
00157         ods_log_error("[%s] unable to write zone: no output adapter", adapter_str);
00158         return ODS_STATUS_ASSERT_ERR;
00159     }
00160     ods_log_assert(adzone);
00161     ods_log_assert(adzone->adoutbound);
00162     ods_log_assert(adzone->adoutbound->configstr);
00163     if (!adzone->zonedata) {
00164         ods_log_error("[%s] unable to write zone: no zone data", adapter_str);
00165         return ODS_STATUS_ASSERT_ERR;
00166     }
00167     ods_log_assert(adzone->zonedata);
00168 
00169     switch(adzone->adoutbound->type) {
00170         case ADAPTER_FILE:
00171             ods_log_verbose("[%s] write zone %s serial %u to output file "
00172                 "adapter %s", adapter_str, adzone->name,
00173                 adzone->zonedata->outbound_serial,
00174                 adzone->adinbound->configstr);
00175             status = adfile_write(zone, adzone->adoutbound->configstr);
00176             return status;
00177             break;
00178         default:
00179             ods_log_error("[%s] unable to write zone %s to adapter: unknown "
00180                 "adapter", adapter_str, adzone->name);
00181             return ODS_STATUS_ERR;
00182             break;
00183     }
00184 
00185     /* NOT REACHED */
00186     return ODS_STATUS_ERR;
00187 }
00188 
00189 
00194 int
00195 adapter_compare(adapter_type* a1, adapter_type* a2)
00196 {
00197     if (!a1 && !a2) {
00198         return 0;
00199     } else if (!a1) {
00200         return -1;
00201     } else if (!a2) {
00202         return 1;
00203     } else if (a1->inbound != a2->inbound) {
00204         return a1->inbound - a2->inbound;
00205     } else if (a1->type != a2->type) {
00206         return a1->type - a2->type;
00207     }
00208     return ods_strcmp(a1->configstr, a2->configstr);
00209 }
00210 
00211 
00216 void
00217 adapter_cleanup(adapter_type* adapter)
00218 {
00219     allocator_type* allocator;
00220     if (!adapter) {
00221         return;
00222     }
00223     allocator = adapter->allocator;
00224     allocator_deallocate(allocator, (void*) adapter->configstr);
00225     allocator_deallocate(allocator, (void*) adapter->data);
00226     allocator_deallocate(allocator, (void*) adapter);
00227     allocator_cleanup(allocator);
00228     return;
00229 }