output_db.c revision 1.1 1 1.1 joerg /* $NetBSD: output_db.c,v 1.1 2010/04/25 00:54:46 joerg Exp $ */
2 1.1 joerg
3 1.1 joerg /*-
4 1.1 joerg * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 joerg * All rights reserved.
6 1.1 joerg *
7 1.1 joerg * This code is derived from software contributed to The NetBSD Foundation
8 1.1 joerg * by Luke Mewburn and Christos Zoulas.
9 1.1 joerg *
10 1.1 joerg * Redistribution and use in source and binary forms, with or without
11 1.1 joerg * modification, are permitted provided that the following conditions
12 1.1 joerg * are met:
13 1.1 joerg * 1. Redistributions of source code must retain the above copyright
14 1.1 joerg * notice, this list of conditions and the following disclaimer.
15 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 joerg * notice, this list of conditions and the following disclaimer in the
17 1.1 joerg * documentation and/or other materials provided with the distribution.
18 1.1 joerg *
19 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 joerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 joerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 joerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 joerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 joerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 joerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 joerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 joerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 joerg * POSSIBILITY OF SUCH DAMAGE.
30 1.1 joerg */
31 1.1 joerg
32 1.1 joerg #include <sys/cdefs.h>
33 1.1 joerg #ifndef lint
34 1.1 joerg __RCSID("$NetBSD: output_db.c,v 1.1 2010/04/25 00:54:46 joerg Exp $");
35 1.1 joerg #endif /* not lint */
36 1.1 joerg
37 1.1 joerg #include <sys/param.h>
38 1.1 joerg
39 1.1 joerg #include <assert.h>
40 1.1 joerg #include <db.h>
41 1.1 joerg #include <err.h>
42 1.1 joerg #include <fcntl.h>
43 1.1 joerg #include <netdb.h>
44 1.1 joerg #include <stdio.h>
45 1.1 joerg #include <stdlib.h>
46 1.1 joerg #include <string.h>
47 1.1 joerg #include <unistd.h>
48 1.1 joerg #include <util.h>
49 1.1 joerg #include <ctype.h>
50 1.1 joerg #include <errno.h>
51 1.1 joerg #include <stringlist.h>
52 1.1 joerg
53 1.1 joerg #include "extern.h"
54 1.1 joerg
55 1.1 joerg static DB *db;
56 1.1 joerg
57 1.1 joerg static const HASHINFO hinfo = {
58 1.1 joerg .bsize = 256,
59 1.1 joerg .ffactor = 4,
60 1.1 joerg .nelem = 32768,
61 1.1 joerg .cachesize = 1024,
62 1.1 joerg .hash = NULL,
63 1.1 joerg .lorder = 0
64 1.1 joerg };
65 1.1 joerg
66 1.1 joerg static void store(DBT *, DBT *, int);
67 1.1 joerg static void killproto(DBT *);
68 1.1 joerg static const char *mkaliases(StringList *, char *, size_t);
69 1.1 joerg
70 1.1 joerg int
71 1.1 joerg db_open(const char *tname)
72 1.1 joerg {
73 1.1 joerg db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
74 1.1 joerg (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
75 1.1 joerg
76 1.1 joerg return db != NULL ? 0 : -1;
77 1.1 joerg }
78 1.1 joerg
79 1.1 joerg int
80 1.1 joerg db_close(void)
81 1.1 joerg {
82 1.1 joerg int rv;
83 1.1 joerg
84 1.1 joerg rv = (db->close)(db);
85 1.1 joerg db = NULL;
86 1.1 joerg
87 1.1 joerg return rv;
88 1.1 joerg }
89 1.1 joerg
90 1.1 joerg void
91 1.1 joerg db_add(StringList *sl, size_t port, const char *proto, size_t *cnt,
92 1.1 joerg int warndup)
93 1.1 joerg {
94 1.1 joerg size_t i;
95 1.1 joerg char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
96 1.1 joerg DBT data, key;
97 1.1 joerg key.data = keyb;
98 1.1 joerg data.data = datab;
99 1.1 joerg
100 1.1 joerg /* key `indirect key', data `full line' */
101 1.1 joerg data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
102 1.1 joerg key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
103 1.1 joerg sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
104 1.1 joerg store(&data, &key, warndup);
105 1.1 joerg
106 1.1 joerg /* key `\377port/proto', data = `indirect key' */
107 1.1 joerg key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
108 1.1 joerg port, proto) + 1;
109 1.1 joerg store(&key, &data, warndup);
110 1.1 joerg
111 1.1 joerg /* key `\377port', data = `indirect key' */
112 1.1 joerg killproto(&key);
113 1.1 joerg store(&key, &data, warndup);
114 1.1 joerg
115 1.1 joerg /* add references for service and all aliases */
116 1.1 joerg for (i = 0; i < sl->sl_cur; i++) {
117 1.1 joerg /* key `\376service/proto', data = `indirect key' */
118 1.1 joerg key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
119 1.1 joerg sl->sl_str[i], proto) + 1;
120 1.1 joerg store(&key, &data, warndup);
121 1.1 joerg
122 1.1 joerg /* key `\376service', data = `indirect key' */
123 1.1 joerg killproto(&key);
124 1.1 joerg store(&key, &data, warndup);
125 1.1 joerg }
126 1.1 joerg sl_free(sl, 1);
127 1.1 joerg }
128 1.1 joerg
129 1.1 joerg static void
130 1.1 joerg killproto(DBT *key)
131 1.1 joerg {
132 1.1 joerg char *p, *d = key->data;
133 1.1 joerg
134 1.1 joerg if ((p = strchr(d, '/')) == NULL)
135 1.1 joerg abort();
136 1.1 joerg *p++ = '\0';
137 1.1 joerg key->size = p - d;
138 1.1 joerg }
139 1.1 joerg
140 1.1 joerg static void
141 1.1 joerg store(DBT *key, DBT *data, int warndup)
142 1.1 joerg {
143 1.1 joerg #ifdef DEBUG
144 1.1 joerg int k = key->size - 1;
145 1.1 joerg int d = data->size - 1;
146 1.1 joerg (void)printf("store [%*.*s] [%*.*s]\n",
147 1.1 joerg k, k, (char *)key->data + 1,
148 1.1 joerg d, d, (char *)data->data + 1);
149 1.1 joerg #endif
150 1.1 joerg switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
151 1.1 joerg case 0:
152 1.1 joerg break;
153 1.1 joerg case 1:
154 1.1 joerg if (warndup)
155 1.1 joerg warnx("duplicate service `%s'",
156 1.1 joerg &((char *)key->data)[1]);
157 1.1 joerg break;
158 1.1 joerg case -1:
159 1.1 joerg err(1, "put");
160 1.1 joerg break;
161 1.1 joerg default:
162 1.1 joerg abort();
163 1.1 joerg break;
164 1.1 joerg }
165 1.1 joerg }
166 1.1 joerg
167 1.1 joerg static const char *
168 1.1 joerg mkaliases(StringList *sl, char *buf, size_t len)
169 1.1 joerg {
170 1.1 joerg size_t nc, i, pos;
171 1.1 joerg
172 1.1 joerg buf[0] = 0;
173 1.1 joerg for (i = 1, pos = 0; i < sl->sl_cur; i++) {
174 1.1 joerg nc = strlcpy(buf + pos, sl->sl_str[i], len);
175 1.1 joerg if (nc >= len)
176 1.1 joerg goto out;
177 1.1 joerg pos += nc;
178 1.1 joerg len -= nc;
179 1.1 joerg nc = strlcpy(buf + pos, " ", len);
180 1.1 joerg if (nc >= len)
181 1.1 joerg goto out;
182 1.1 joerg pos += nc;
183 1.1 joerg len -= nc;
184 1.1 joerg }
185 1.1 joerg return buf;
186 1.1 joerg out:
187 1.1 joerg warn("aliases for `%s' truncated", sl->sl_str[0]);
188 1.1 joerg return buf;
189 1.1 joerg }
190