1 /* $NetBSD: output_cdb.c,v 1.1 2010/04/25 00:54:46 joerg Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Joerg Sonnenberger.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/endian.h>
40 #include <stringlist.h>
45 static struct cdbw
*cdbw
;
46 static int cdbw_fd
= -1;
49 cdb_open(const char *tname
)
52 if ((cdbw
= cdbw_open()) == NULL
)
55 if ((cdbw_fd
= open(tname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) == -1) {
64 cdb_add(StringList
*sl
, size_t port
, const char *proto
, size_t *cnt
,
67 uint8_t key
[255 * 2 + 2];
68 uint8_t *data
, *data_iter
;
69 size_t len
, protolen
, datalen
, keylen
;
73 protolen
= strlen(proto
);
74 if (protolen
== 0 || protolen
> 255)
75 errx(1, "Invalid protocol ``%s'', entry skipped", proto
);
77 datalen
= 4 + protolen
;
78 for (i
= 0; i
< sl
->sl_cur
; ++i
) {
79 len
= strlen(sl
->sl_str
[i
]);
80 if (len
== 0 || len
> 255)
81 errx(1, "Service alias ``%s'' invalid", sl
->sl_str
[i
]);
85 data
= malloc(datalen
);
87 err(1, "malloc failed");
91 memcpy(data_iter
, proto
, protolen
+ 1);
92 data_iter
+= protolen
+ 1;
93 for (i
= 0; i
< sl
->sl_cur
; ++i
) {
94 len
= strlen(sl
->sl_str
[i
]);
96 memcpy(data_iter
, sl
->sl_str
[i
], len
+ 1);
100 if (cdbw_put_data(cdbw
, data
, datalen
, &idx
))
101 err(1, "cdbw_put_data failed");
107 be16enc(key
+ 2, port
);
108 memcpy(key
+ 4, proto
, protolen
);
109 keylen
= 4 + protolen
;
110 if (cdbw_put_key(cdbw
, key
, keylen
, idx
) && warndup
)
111 warnx("duplicate service: `%zu/%s'", port
, proto
);
115 if (cdbw_put_key(cdbw
, key
, keylen
, idx
) && warndup
)
116 warnx("duplicate service: `%zu'", port
);
118 /* add references for service and all aliases */
119 for (i
= 0; i
< sl
->sl_cur
; i
++) {
120 len
= strlen(sl
->sl_str
[i
]);
123 memcpy(key
+ 2, sl
->sl_str
[i
], len
);
124 memcpy(key
+ 2 + len
, proto
, protolen
);
125 keylen
= 2 + len
+ protolen
;
126 if (cdbw_put_key(cdbw
, key
, keylen
, idx
) && warndup
)
127 warnx("duplicate service: `%s/%s'", sl
->sl_str
[i
], proto
);
131 if (cdbw_put_key(cdbw
, key
, keylen
, idx
) && warndup
)
132 warnx("duplicate service: `%s'", sl
->sl_str
[i
]);
146 if (cdbw_output(cdbw
, cdbw_fd
, "services(5)", NULL
)) {
154 if (close(cdbw_fd
)) {