]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/cdb.hh
dnsdist: Fix DNS over plain HTTP broken by `reloadAllCertificates()`
[thirdparty/pdns.git] / pdns / cdb.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23 #include <cdb.h>
24
25 #include "misc.hh"
26
27 // This class is responsible for the reading of a CDB file.
28 // The constructor opens the CDB file, the destructor closes it, so make sure you call that.
29 class CDB
30 {
31 public:
32 CDB(const string &cdbfile);
33 ~CDB();
34
35 /* Return negative value on error or non-negative value on success.
36 Values can be retrieved via readNext() */
37 int searchKey(const string &key);
38 bool searchSuffix(const string &key);
39 void searchAll();
40 bool readNext(pair<string, string> &value);
41 vector<string> findall(string &key);
42 bool keyExists(const string& key);
43 bool findOne(const string& key, string& value);
44
45 private:
46 bool moveToNext();
47
48 int d_fd{-1};
49 struct cdb d_cdb;
50 struct cdb_find d_cdbf;
51 std::string d_key;
52 unsigned d_seqPtr{0};
53 enum SearchType { SearchSuffix, SearchKey, SearchAll } d_searchType{SearchKey};
54 };
55
56 class CDBWriter
57 {
58 public:
59 /* we own the fd after this call, don't ever touch it */
60 CDBWriter(int fd);
61 ~CDBWriter();
62
63 bool addEntry(const std::string& key, const std::string& value);
64 /* finalize the database and close the fd, the only thing you can do now is to call the destructor */
65 void close();
66
67 private:
68 struct cdb_make d_cdbm;
69 int d_fd{-1};
70 };