4 * Copyright (C) 2002 Nuno M. Rodrigues.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND NUNO M. RODRIGUES
11 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Id: zone2bdb.c,v 1.3 2009/09/01 00:22:26 jinmei Exp */
25 #include <isc/result.h>
26 #include <isc/types.h>
30 #include <dns/dbiterator.h>
31 #include <dns/fixedname.h>
33 #include <dns/rdata.h>
34 #include <dns/rdataset.h>
35 #include <dns/rdatasetiter.h>
36 #include <dns/rdatatype.h>
38 #include <dns/types.h>
42 #define MAX_RDATATEXT 63 + 4 + 65535 + 2 /* ttl + type + rdata + sep */
45 * Returns a valid 'DB' handle.
48 * 'file' is a valid non-existant path.
51 bdb_init(const char *file
)
55 REQUIRE(db_create(&db
, NULL
, 0) == 0);
56 REQUIRE(db
->set_flags(db
, DB_DUP
) == 0);
57 REQUIRE(db
->open(db
, file
, NULL
, DB_HASH
, DB_CREATE
| DB_EXCL
, 0) == 0);
63 * Puts resource record data on 'db'.
66 bdb_putrdata(DB
*db
, dns_name_t
*name
, dns_ttl_t ttl
, dns_rdata_t
*rdata
)
69 isc_buffer_t keybuf
, databuf
;
70 char nametext
[DNS_NAME_MAXTEXT
];
71 char rdatatext
[MAX_RDATATEXT
];
73 isc_buffer_init(&keybuf
, nametext
, DNS_NAME_MAXTEXT
);
75 dns_name_totext(name
, ISC_TRUE
, &keybuf
);
77 key
.data
= isc_buffer_base(&keybuf
);
78 key
.size
= isc_buffer_usedlength(&keybuf
);
80 isc_buffer_init(&databuf
, rdatatext
, MAX_RDATATEXT
);
82 dns_ttl_totext(ttl
, ISC_FALSE
, &databuf
);
83 *(char *)isc_buffer_used(&databuf
) = ' ';
84 isc_buffer_add(&databuf
, 1);
86 dns_rdatatype_totext(rdata
->type
, &databuf
); /* XXX private data */
87 *(char *)isc_buffer_used(&databuf
) = ' ';
88 isc_buffer_add(&databuf
, 1);
90 dns_rdata_totext(rdata
, NULL
, &databuf
);
92 data
.data
= isc_buffer_base(&databuf
);
93 data
.size
= isc_buffer_usedlength(&databuf
);
95 REQUIRE(db
->put(db
, NULL
, &key
, &data
, 0) == 0);
104 return (db
->close(db
, 0) == 0) ? ISC_R_SUCCESS
: ISC_R_FAILURE
;
108 usage(const char *prog
)
111 fprintf(stderr
, "Usage: %s <origin> <zonefile> <db>\n", prog
);
116 main(int argc
, char *argv
[])
118 isc_mem_t
*mctx
= NULL
;
121 dns_fixedname_t origin
, name
;
123 dns_dbiterator_t
*dbiter
= NULL
;
125 dns_dbnode_t
*node
= NULL
;
126 dns_rdataset_t rdataset
;
127 dns_rdatasetiter_t
*rdatasetiter
= NULL
;
131 if (argc
!= 4) usage(*argv
);
133 REQUIRE(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
136 isc_buffer_init(&b
, argv
[1], n
);
137 isc_buffer_add(&b
, n
);
139 dns_fixedname_init(&origin
);
141 REQUIRE(dns_name_fromtext(dns_fixedname_name(&origin
), &b
, dns_rootname
,
142 0, NULL
) == ISC_R_SUCCESS
);
143 REQUIRE(dns_db_create(mctx
, "rbt", dns_fixedname_name(&origin
),
144 dns_dbtype_zone
, dns_rdataclass_in
, 0, NULL
,
145 &db
) == ISC_R_SUCCESS
);
147 REQUIRE(dns_db_load(db
, argv
[2]) == ISC_R_SUCCESS
);
149 REQUIRE(dns_db_createiterator(db
, 0, &dbiter
) == ISC_R_SUCCESS
);
151 dns_rdataset_init(&rdataset
);
152 dns_rdata_init(&rdata
);
153 dns_fixedname_init(&name
);
154 bdb
= bdb_init(argv
[3]);
156 for (res
= dns_dbiterator_first(dbiter
); res
== ISC_R_SUCCESS
;
157 res
= dns_dbiterator_next(dbiter
)) {
158 dns_dbiterator_current(dbiter
, &node
, dns_fixedname_name(&name
));
159 REQUIRE(dns_db_allrdatasets(db
, node
, NULL
, 0, &rdatasetiter
)
162 for (res
= dns_rdatasetiter_first(rdatasetiter
);
163 res
== ISC_R_SUCCESS
;
164 res
= dns_rdatasetiter_next(rdatasetiter
)) {
165 dns_rdatasetiter_current(rdatasetiter
, &rdataset
);
167 res
= dns_rdataset_first(&rdataset
);
168 while (res
== ISC_R_SUCCESS
) {
169 dns_rdataset_current(&rdataset
, &rdata
);
170 REQUIRE(bdb_putrdata(bdb
,
171 dns_fixedname_name(&name
),
172 rdataset
.ttl
, &rdata
)
175 dns_rdata_reset(&rdata
);
176 res
= dns_rdataset_next(&rdataset
);
179 dns_rdataset_disassociate(&rdataset
);
181 dns_rdatasetiter_destroy(&rdatasetiter
);
182 dns_db_detachnode(db
, &node
);
184 dns_dbiterator_destroy(&dbiter
);
186 REQUIRE(bdb_destroy(bdb
) == ISC_R_SUCCESS
);