]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdistdist/dnsdist-kvs.cc
Replace boost's placeholders with the ones from the std namespace
[thirdparty/pdns.git] / pdns / dnsdistdist / dnsdist-kvs.cc
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
23 #include "dnsdist-kvs.hh"
24 #include "dolog.hh"
25
26 #include <sys/stat.h>
27
28 std::vector<std::string> KeyValueLookupKeySourceIP::getKeys(const ComboAddress& addr)
29 {
30 std::vector<std::string> result;
31
32 if (addr.sin4.sin_family == AF_INET) {
33 result.emplace_back(reinterpret_cast<const char*>(&addr.sin4.sin_addr.s_addr), sizeof(addr.sin4.sin_addr.s_addr));
34 }
35 else if (addr.sin4.sin_family == AF_INET6) {
36 result.emplace_back(reinterpret_cast<const char*>(&addr.sin6.sin6_addr.s6_addr), sizeof(addr.sin6.sin6_addr.s6_addr));
37 }
38
39 return result;
40 }
41
42 std::vector<std::string> KeyValueLookupKeySuffix::getKeys(const DNSName& qname)
43 {
44 if (qname.empty() || qname.isRoot()) {
45 return {};
46 }
47
48 auto lowerQName = qname.makeLowerCase();
49 size_t labelsCount = lowerQName.countLabels();
50 if (d_minLabels != 0) {
51 if (labelsCount < d_minLabels) {
52 return {};
53 }
54 labelsCount -= (d_minLabels - 1);
55 }
56
57 std::vector<std::string> result;
58 result.reserve(labelsCount);
59
60 while(!lowerQName.isRoot()) {
61 result.emplace_back(d_wireFormat ? lowerQName.toDNSString() : lowerQName.toStringRootDot());
62 labelsCount--;
63 if (!lowerQName.chopOff() || labelsCount == 0) {
64 break;
65 }
66 }
67
68 return result;
69 }
70
71 #ifdef HAVE_LMDB
72
73 bool LMDBKVStore::getValue(const std::string& key, std::string& value)
74 {
75 try {
76 auto transaction = d_env.getROTransaction();
77 auto dbi = transaction->openDB(d_dbName, 0);
78 MDBOutVal result;
79 int rc = transaction->get(dbi, MDBInVal(key), result);
80 if (rc == 0) {
81 value = result.get<std::string>();
82 return true;
83 }
84 else if (rc == MDB_NOTFOUND) {
85 return false;
86 }
87 }
88 catch(const std::exception& e) {
89 warnlog("Error while looking up key '%s' from LMDB file '%s', database '%s': %s", key, d_fname, d_dbName, e.what());
90 }
91 return false;
92 }
93
94 bool LMDBKVStore::keyExists(const std::string& key)
95 {
96 try {
97 auto transaction = d_env.getROTransaction();
98 auto dbi = transaction->openDB(d_dbName, 0);
99 MDBOutVal result;
100 int rc = transaction->get(dbi, MDBInVal(key), result);
101 if (rc == 0) {
102 return true;
103 }
104 else if (rc == MDB_NOTFOUND) {
105 return false;
106 }
107 }
108 catch(const std::exception& e) {
109 warnlog("Error while looking up key '%s' from LMDB file '%s', database '%s': %s", key, d_fname, d_dbName, e.what());
110 }
111 return false;
112 }
113
114 #endif /* HAVE_LMDB */
115
116 #ifdef HAVE_CDB
117
118 CDBKVStore::CDBKVStore(const std::string& fname, time_t refreshDelay): d_fname(fname), d_refreshDelay(refreshDelay)
119 {
120 pthread_rwlock_init(&d_lock, nullptr);
121 d_refreshing.clear();
122
123 time_t now = time(nullptr);
124 if (d_refreshDelay > 0) {
125 d_nextCheck = now + d_refreshDelay;
126 }
127
128 refreshDBIfNeeded(now);
129 }
130
131 CDBKVStore::~CDBKVStore() {
132 pthread_rwlock_destroy(&d_lock);
133 }
134
135 bool CDBKVStore::reload(const struct stat& st)
136 {
137 auto newCDB = std::unique_ptr<CDB>(new CDB(d_fname));
138 {
139 WriteLock wl(&d_lock);
140 d_cdb = std::move(newCDB);
141 }
142 d_mtime = st.st_mtime;
143 return true;
144 }
145
146 bool CDBKVStore::reload()
147 {
148 struct stat st;
149 if (stat(d_fname.c_str(), &st) == 0) {
150 return reload(st);
151 }
152 else {
153 warnlog("Error while retrieving the last modification time of CDB database '%s': %s", d_fname, stringerror());
154 return false;
155 }
156 }
157
158 void CDBKVStore::refreshDBIfNeeded(time_t now)
159 {
160 if (d_refreshing.test_and_set()) {
161 /* someone else is already refreshing */
162 return;
163 }
164
165 try {
166 struct stat st;
167 if (stat(d_fname.c_str(), &st) == 0) {
168 if (st.st_mtime > d_mtime) {
169 reload(st);
170 }
171 }
172 else {
173 warnlog("Error while retrieving the last modification time of CDB database '%s': %s", d_fname, stringerror());
174 }
175 d_nextCheck = now + d_refreshDelay;
176 d_refreshing.clear();
177 }
178 catch(...) {
179 d_refreshing.clear();
180 throw;
181 }
182 }
183
184 bool CDBKVStore::getValue(const std::string& key, std::string& value)
185 {
186 time_t now = time(nullptr);
187
188 try {
189 if (d_nextCheck != 0 && now >= d_nextCheck) {
190 refreshDBIfNeeded(now);
191 }
192
193 {
194 ReadLock rl(&d_lock);
195 if (d_cdb && d_cdb->findOne(key, value)) {
196 return true;
197 }
198 }
199 }
200 catch(const std::exception& e) {
201 warnlog("Error while looking up key '%s' from CDB file '%s': %s", key, d_fname, e.what());
202 }
203 return false;
204 }
205
206 bool CDBKVStore::keyExists(const std::string& key)
207 {
208 time_t now = time(nullptr);
209
210 try {
211 if (d_nextCheck != 0 && now >= d_nextCheck) {
212 refreshDBIfNeeded(now);
213 }
214
215 {
216 ReadLock rl(&d_lock);
217 if (!d_cdb) {
218 return false;
219 }
220
221 return d_cdb->keyExists(key);
222 }
223 }
224 catch(const std::exception& e) {
225 warnlog("Error while looking up key '%s' from CDB file '%s': %s", key, d_fname, e.what());
226 }
227 return false;
228 }
229
230 #endif /* HAVE_CDB */