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.3 1995/05/16 09:40:53 adam
51 * Revision 1.2 1995/05/01 12:43:57 adam
52 * lgets function moved from kernel to util.
54 * Revision 1.1 1995/03/27 08:25:01 adam
55 * New module gip: Gateway IPc module.
56 * New module gw-db: Gateway hash-db module (user information table).
69 #define FILE_HEAD_LEN 128
73 struct file_head { /* file header info */
74 char magic[8]; /* start info */
75 int no_of_entries; /* no of entries in table */
76 int sequence_no; /* number of next to come */
78 int *hash_array; /* hash array pointers (DB_HASH in size) */
79 int fd; /* descriptor of file */
80 int dirty; /* any writes so far? */
83 struct db_bucket { /* information about individual entries */
84 int name_length; /* length of name */
85 int info_length; /* length of information */
86 int next; /* next in hash chain - possibly 0 */
87 int prev; /* prev in hach chain - possibly 0 */
91 * write_head: write header of table
93 static int write_head (GW_DB db)
95 char file_head_buf[FILE_HEAD_LEN];
98 if (lseek (db->fd, 0L, SEEK_SET) == -1)
100 memcpy (file_head_buf, &db->head, sizeof(db->head));
101 r = write (db->fd, file_head_buf, FILE_HEAD_LEN);
104 r = write (db->fd, db->hash_array, DB_HASH * sizeof(*db->hash_array));
111 * hash: calculate hash value
113 static unsigned hash (const char *name, int length)
117 while (--length >= 0)
118 l = l*65599 + *name++;
123 * lock_file: lock entire file
125 static void lock_file (int fd, int type)
129 area.l_whence = SEEK_SET;
132 fcntl (fd, F_SETLKW, &area);
136 * gw_db_lookup: table lookup
138 int gw_db_lookup (GW_DB db, const char *name, int name_length,
139 void **buf, size_t *count)
141 struct db_bucket bucket;
142 unsigned l = hash (name, name_length);
148 for (pos = db->hash_array[l]; pos; pos = bucket.next)
152 if (lseek (db->fd, pos, SEEK_SET) == -1)
157 r = read (db->fd, &bucket, sizeof(bucket));
163 if (r != sizeof(bucket))
168 if (bucket.name_length <= 0 || bucket.info_length <= 0 ||
169 bucket.name_length >= 16384 || bucket.info_length >= 262144)
174 if (bucket.name_length != name_length)
176 blen = bucket.name_length + bucket.info_length;
181 if (!(dbuf = malloc (dsize)))
184 r = read (db->fd, dbuf, blen);
195 if (memcmp (name, dbuf, name_length))
198 *count = bucket.info_length;
199 *buf = malloc (*count);
205 memcpy (*buf, dbuf + name_length, *count);
214 * gw_db_insert: table insertion
216 int gw_db_insert (GW_DB db, const char *name, int name_length,
217 const void *buf, size_t count)
219 struct db_bucket n_bucket;
220 struct db_bucket bucket;
222 unsigned l = hash (name, name_length);
223 int pos = db->hash_array[l];
227 (db->head.no_of_entries)++;
228 (db->head.sequence_no)++;
229 n_bucket.name_length = name_length;
230 n_bucket.info_length = count;
231 n_pos = lseek (db->fd, 0, SEEK_END);
235 db->hash_array[l] = n_pos;
238 r = write (db->fd, &n_bucket, sizeof(n_bucket));
241 else if (r < sizeof(n_bucket))
244 r = write (db->fd, name, name_length);
247 else if (r < name_length)
250 r = write (db->fd, buf, count);
256 r_pos = lseek (db->fd, pos, SEEK_SET);
259 r = read (db->fd, &bucket, sizeof(bucket));
262 else if (r < sizeof(bucket))
266 r_pos = lseek (db->fd, pos, SEEK_SET);
269 r = write (db->fd, &bucket, sizeof(bucket));
272 else if (r < sizeof(bucket))
278 * gw_db_free: release memory associated with table
280 static GW_DB gw_db_free (GW_DB db)
282 free (db->hash_array);
288 * gw_db_open: open and lock table for reading (and writing)
290 GW_DB gw_db_open (const char *fname, int write_flag)
292 char file_head_buf[FILE_HEAD_LEN];
296 if (!(db = malloc (sizeof(*db))))
299 if (!(db->hash_array = malloc (DB_HASH * sizeof(*db->hash_array))))
300 return gw_db_free (db);
302 db->fd = open (fname, O_RDWR|O_CREAT, 0666);
304 db->fd = open (fname, O_RDONLY);
306 return gw_db_free (db);
307 lock_file (db->fd, write_flag ? F_WRLCK : F_RDLCK);
308 r = read (db->fd, file_head_buf, FILE_HEAD_LEN);
310 return gw_db_free (db);
311 if (r < FILE_HEAD_LEN)
316 return gw_db_free (db);
317 db->head.no_of_entries = 0;
318 db->head.sequence_no = 1;
319 for (i=0; i<DB_HASH; i++)
320 db->hash_array[i] = 0;
322 return gw_db_free (db);
326 memcpy (&db->head, file_head_buf, sizeof(db->head));
327 r = read (db->fd, db->hash_array, sizeof(*db->hash_array)*DB_HASH);
328 if (r < sizeof(*db->hash_array)*DB_HASH)
329 return gw_db_free (db);
335 * gw_db_close: close table and flush
337 int gw_db_close (GW_DB db)
343 lock_file (db->fd, F_UNLCK);
344 if (close (db->fd) == -1)
354 * gw_db_no_ent: return number of entries in table
356 int gw_db_no_ent (GW_DB db)
358 return db->head.no_of_entries;
362 * gw_db_seq_no: return sequence number
364 int gw_db_seq_no (GW_DB db)
366 return db->head.sequence_no;