2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation, in whole or in part, for any purpose, is hereby granted,
15 * 1. This copyright and permission notice appear in all copies of the
16 * software and its documentation. Notices of copyright or attribution
17 * which appear at the beginning of any file must remain unchanged.
19 * 2. The names of EUROPAGATE or the project partners may not be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * 3. Users of this software (implementors and gateway operators) agree to
24 * inform the EUROPAGATE consortium of their use of the software. This
25 * information will be used to evaluate the EUROPAGATE project and the
26 * software, and to plan further developments. The consortium may use
27 * the information in later publications.
29 * 4. Users of this software agree to make their best efforts, when
30 * documenting their use of the software, to acknowledge the EUROPAGATE
31 * consortium, and the role played by the software in their work.
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38 * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39 * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40 * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41 * USE OR PERFORMANCE OF THIS SOFTWARE.
48 * Revision 1.5 1996/01/24 17:00:12 adam
49 * Bug fix: missing argument to gw_log-call.
51 * Revision 1.4 1995/12/20 16:28:07 adam
52 * Extra parameter block to gw_db_open. If block is 0 gw_db_open
53 * returns NULL if lock couldn't be satisfied.
54 * Minor changes in iso2709.c.
56 * Revision 1.3 1995/05/16 09:40:53 adam
59 * Revision 1.2 1995/05/01 12:43:57 adam
60 * lgets function moved from kernel to util.
62 * Revision 1.1 1995/03/27 08:25:01 adam
63 * New module gip: Gateway IPc module.
64 * New module gw-db: Gateway hash-db module (user information table).
78 #define FILE_HEAD_LEN 128
82 struct file_head { /* file header info */
83 char magic[8]; /* start info */
84 int no_of_entries; /* no of entries in table */
85 int sequence_no; /* number of next to come */
87 int *hash_array; /* hash array pointers (DB_HASH in size) */
88 int fd; /* descriptor of file */
89 int dirty; /* any writes so far? */
92 struct db_bucket { /* information about individual entries */
93 int name_length; /* length of name */
94 int info_length; /* length of information */
95 int next; /* next in hash chain - possibly 0 */
96 int prev; /* prev in hach chain - possibly 0 */
99 static char *mod = "gwdb";
102 * write_head: write header of table
104 static int write_head (GW_DB db)
106 char file_head_buf[FILE_HEAD_LEN];
109 if (lseek (db->fd, 0L, SEEK_SET) == -1)
111 memcpy (file_head_buf, &db->head, sizeof(db->head));
112 r = write (db->fd, file_head_buf, FILE_HEAD_LEN);
115 r = write (db->fd, db->hash_array, DB_HASH * sizeof(*db->hash_array));
122 * hash: calculate hash value
124 static unsigned hash (const char *name, int length)
128 while (--length >= 0)
129 l = l*65599 + *name++;
134 * lock_file: lock entire file
136 static int lock_file (int fd, int block, int type)
140 area.l_whence = SEEK_SET;
143 return fcntl (fd, block ? F_SETLKW : F_SETLK, &area);
148 * gw_db_lookup: table lookup
150 int gw_db_lookup (GW_DB db, const char *name, int name_length,
151 void **buf, size_t *count)
153 struct db_bucket bucket;
154 unsigned l = hash (name, name_length);
160 for (pos = db->hash_array[l]; pos; pos = bucket.next)
164 if (lseek (db->fd, pos, SEEK_SET) == -1)
169 r = read (db->fd, &bucket, sizeof(bucket));
175 if (r != sizeof(bucket))
180 if (bucket.name_length <= 0 || bucket.info_length <= 0 ||
181 bucket.name_length >= 16384 || bucket.info_length >= 262144)
186 if (bucket.name_length != name_length)
188 blen = bucket.name_length + bucket.info_length;
193 if (!(dbuf = malloc (dsize)))
196 r = read (db->fd, dbuf, blen);
207 if (memcmp (name, dbuf, name_length))
210 *count = bucket.info_length;
211 *buf = malloc (*count);
217 memcpy (*buf, dbuf + name_length, *count);
226 * gw_db_insert: table insertion
228 int gw_db_insert (GW_DB db, const char *name, int name_length,
229 const void *buf, size_t count)
231 struct db_bucket n_bucket;
232 struct db_bucket bucket;
234 unsigned l = hash (name, name_length);
235 int pos = db->hash_array[l];
239 (db->head.no_of_entries)++;
240 (db->head.sequence_no)++;
241 n_bucket.name_length = name_length;
242 n_bucket.info_length = count;
243 n_pos = lseek (db->fd, 0, SEEK_END);
247 db->hash_array[l] = n_pos;
250 r = write (db->fd, &n_bucket, sizeof(n_bucket));
253 else if (r < sizeof(n_bucket))
256 r = write (db->fd, name, name_length);
259 else if (r < name_length)
262 r = write (db->fd, buf, count);
268 r_pos = lseek (db->fd, pos, SEEK_SET);
271 r = read (db->fd, &bucket, sizeof(bucket));
274 else if (r < sizeof(bucket))
278 r_pos = lseek (db->fd, pos, SEEK_SET);
281 r = write (db->fd, &bucket, sizeof(bucket));
284 else if (r < sizeof(bucket))
290 * gw_db_free: release memory associated with table
292 static GW_DB gw_db_free (GW_DB db)
294 free (db->hash_array);
300 * gw_db_open: open and lock table for reading (and writing)
302 GW_DB gw_db_open (const char *fname, int write_flag, int block)
304 char file_head_buf[FILE_HEAD_LEN];
308 db = malloc (sizeof(*db));
311 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
315 if (!(db->hash_array = malloc (DB_HASH * sizeof(*db->hash_array))))
317 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
322 if ((db->fd = open (fname, O_RDWR|O_CREAT, 0666)) == -1)
324 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", fname);
330 if ((db->fd = open (fname, O_RDONLY)) == -1)
332 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, " open %s", fname);
333 return gw_db_free (db);
337 lock_file (db->fd, 1, write_flag ? F_WRLCK : F_RDLCK);
340 r = lock_file (db->fd, 0, write_flag ? F_WRLCK : F_RDLCK);
343 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "flock %s", fname);
345 return gw_db_free (db);
348 r = read (db->fd, file_head_buf, FILE_HEAD_LEN);
350 return gw_db_free (db);
351 if (r < FILE_HEAD_LEN)
356 return gw_db_free (db);
357 db->head.no_of_entries = 0;
358 db->head.sequence_no = 1;
359 for (i=0; i<DB_HASH; i++)
360 db->hash_array[i] = 0;
362 return gw_db_free (db);
366 memcpy (&db->head, file_head_buf, sizeof(db->head));
367 r = read (db->fd, db->hash_array, sizeof(*db->hash_array)*DB_HASH);
368 if (r < sizeof(*db->hash_array)*DB_HASH)
369 return gw_db_free (db);
375 * gw_db_close: close table and flush
377 int gw_db_close (GW_DB db)
383 lock_file (db->fd, 1, F_UNLCK);
384 if (close (db->fd) == -1)
394 * gw_db_no_ent: return number of entries in table
396 int gw_db_no_ent (GW_DB db)
398 return db->head.no_of_entries;
402 * gw_db_seq_no: return sequence number
404 int gw_db_seq_no (GW_DB db)
406 return db->head.sequence_no;