[go: up one dir, main page]

File: db_dnshistory.c

package info (click to toggle)
dnshistory 1.3-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 744 kB
  • sloc: ansic: 2,077; sh: 818; makefile: 72
file content (200 lines) | stat: -rw-r--r-- 9,338 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/***************************************************************************
 *   Copyright (C) 2004, 2005, 2006 by Stephen McInerney                   *
 *   spm@stedee.id.au                                                      *
 *                                                                         *
 *   $Id: db_dnshistory.c 58 2006-01-02 10:40:49Z steve $
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/***************************************************************************
 ***************************************************************************
 * db_dnshistory.c
 *
 * Create, Add and Delete actions for the Visitor Database
 ***************************************************************************
 ***************************************************************************/

/***************************************************************************
 ***************************************************************************
 * ModificationHistory:
 **********************
 * 17-Mar-2005 steve     Initial Creation - taken from visitors
 *
 ***************************************************************************
 ***************************************************************************/


#include "dnshistory.h"

/************************************************************************
 * open_dnshistory_db                                                           *
 *                                                                      *
 * Creates and Opens the Berkeley DB                                    *
 * Also creates a cache of a given size (in Mb), or none if <= 0        *
 ************************************************************************
 *  db_ptr      :- double pointer to the DB structure                   *
 *  cache_size  :- Size of cache in Mb                                  *
 *                      if <= 0 will not be created                     *
 ************************************************************************/
void
open_dnshistory_db(DB ** db_ptr,                /* The database to open */
                   char *db_dirfilename,        /* Filename of the database file on disk */
                   u_int32_t cache_size,        /* the size of the cache to create for this DB - in Mb */
                   u_int32_t flags              /* DB Open Flags */
    )
{
    int rtn_db;                                 /* Return value from the various opens/creates */

    VPRINT(VERBOSE2, "%s\n", msg_v1_hashdb);
    VPRINT(VERBOSE3, "%s%s\n", msg_v2_create_hashdb, db_dirfilename);

    /* Create the DB */
    rtn_db = db_create(db_ptr, NULL, 0);
    if (rtn_db != 0) {
        ERRVPRINT(VERBOSE0, msg_F_db_create, db_strerror(rtn_db));
        exit(V_EXIT_DB_CREATION);
    }

    /* Set a bigger cache. _Significantly_ speeds things up! */
    if (cache_size > 0) {
        cache_size *= 1024 * 1024;              /* Convert to Mb */
        VPRINT(VERBOSE3, "%s%d\n", msg_v2_create_dbcache, cache_size);
        rtn_db = (*db_ptr)->set_cachesize(*db_ptr, 0, cache_size, 1);
        if (rtn_db != 0) {
            ERRVPRINT(VERBOSE0, msg_F_db_cache, db_strerror(rtn_db));
            exit(V_EXIT_DB_CACHE_CREATION);
        }
    } else {
        VPRINT(VERBOSE3, "%s%d\n", msg_v2_no_dbcache, cache_size);
    }


    VPRINT(VERBOSE3, "Open the DB: %s\n", db_dirfilename);
    /* Open the DB itself */
    /* BTrees appear to be about 11% faster - based on: 119 Million log lines, 660 000 DB Adds */
    /* rtn_db = (*db_ptr)->open (*db_ptr, NULL, db_dirfilename, NULL, DB_HASH, DB_CREATE, 0664); */
    rtn_db = (*db_ptr)->open(*db_ptr, NULL, db_dirfilename, NULL, DB_BTREE, flags, DB_PERMISSIONS);
    if (rtn_db != 0) {
        (*db_ptr)->err(*db_ptr, rtn_db, "%s", db_dirfilename);
        exit(V_EXIT_DB_OPEN);
    }
}


/************************************************************************
 * close_dnshistory_db                                                  *
 *                                                                      *
 * Closes the Berkeley DB                                               *
 ************************************************************************/
int
close_dnshistory_db(DB ** db_ptr                /* The database to open */
    )
{
    int rtn_db_close;

    rtn_db_close = (*db_ptr)->close(*db_ptr, 0);
    if (rtn_db_close != 0) {
        ERRVPRINT(VERBOSE0, msg_F_db_close, rtn_db_close);
    }
    return (rtn_db_close);
}

/************************************************************************
 * add_record                                                             *
 *                                                                      *
 * Add a Key/Data pair to the cookie hash                               *
 * This version uses thread mutexes to control adding                   *
 ************************************************************************
 *  key_val     :- Pointer to key to store                              *
 *  size_key    :- Size of this key                                     *
 *  data_val    :- Pointer to data to store, for which key is the index *
 *  size_data   :- Size of the data                                     *
 ************************************************************************/
void
add_record(DB ** db_ptr,                        /* FIXME */
           void *key_val,                       /* The key to store as a string */
           int size_key,                        /* size of that key */
           void *data_val,                      /* data "bag" */
           int size_data                        /* size of the data bag */
    )
{
    int rtn;                                    /* Return value */
    DBT dbt_key, dbt_data;                      /* DBT's for Key and Data */

    VPRINT(VERBOSE4, "  ADD Record%s\n", "");

    /* Zero DBT's in line with BDB recommendations */
    memset(&dbt_key, 0, sizeof(dbt_key));
    memset(&dbt_data, 0, sizeof(dbt_data));

    /* Set up DBT's for storing */
    dbt_key.data = (char *) key_val;
    dbt_key.size = size_key;
    dbt_data.data = (char *) data_val;
    dbt_data.size = size_data;

    /* Store the key/data and verify */
    pthread_mutex_lock(&mutex_db_access);       /* Lock DB accesses */
    rtn = (*db_ptr)->put(*db_ptr, NULL, &dbt_key, &dbt_data, 0);
    if (rtn != 0) {
        (*db_ptr)->err(*db_ptr, rtn, "\tDB->put");
        close_dnshistory_db(db_ptr);
        /* XFREE (db_ptr); */
        exit(V_EXIT_DB_PUT);
    }
    pthread_mutex_unlock(&mutex_db_access);
}


/************************************************************************
 * delete_record                                                             *
 *                                                                      *
 * Delete a Key/Data pair from the hash                                 *
 ************************************************************************
 *  key_val     :- Pointer to key to remove                             *
 ************************************************************************/
void
delete_record(DB ** db_ptr,                     /* Database Pointer */
              char *key_val                     /* Key to remove */
    )
{
    int rtn;                                    /* Return value */
    DBT del_key;

    VPRINT(VERBOSE4, "  DELETE Record%s\n", "");

    /* Zero DBT's in line with BDB recommendations */
    memset(&del_key, 0, sizeof(del_key));

    del_key.data = key_val;
    del_key.size = (strlen(key_val) + 1) * sizeof(char);

    rtn = (*db_ptr)->del(*db_ptr, NULL, &del_key, 0);
    if (rtn != 0) {
        (*db_ptr)->err(*db_ptr, rtn, "\tDB->del");
        close_dnshistory_db(db_ptr);
        XFREE(db_ptr);
        exit(V_EXIT_DB_DELETE);
    }
}


/************************************************************************
 ************************************************************************
 *                      END OF FILE                                     *
 ************************************************************************
 ************************************************************************/