-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslave.cc
306 lines (205 loc) · 7.63 KB
/
slave.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright (C) 2011-2013 Fredrik Danerklint
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "luabackend.hh"
#include "pdns/logger.hh"
#include "pdns/arguments.hh"
/*
virtual bool startTransaction(const string &qname, int id);
virtual bool commitTransaction();
virtual bool abortTransaction();
virtual bool feedRecord(const DNSResourceRecord &rr);
virtual bool getDomainInfo(const string &domain, DomainInfo &di);
virtual bool isMaster(const string &name, const string &ip);
virtual void getUnfreshSlaveInfos(vector<DomainInfo>* domains);
virtual void setFresh(int id);
*/
bool LUABackend::startTransaction(const string &qname, int id) {
if (f_lua_starttransaction == 0)
return false;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(startTransaction) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_starttransaction);
lua_pushstring(lua, qname.c_str());
lua_pushnumber(lua, id);
if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(startTransaction)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
size_t returnedwhat = lua_type(lua, -1);
bool ok = false;
if (returnedwhat == LUA_TBOOLEAN)
ok = lua_toboolean(lua, -1);
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(startTransaction) END" << endl;
return ok;
}
bool LUABackend::commitTransaction() {
if (f_lua_committransaction == 0)
return false;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(commitTransaction) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_committransaction);
if(lua_pcall(lua, 0, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(commitTransaction)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
size_t returnedwhat = lua_type(lua, -1);
bool ok = false;
if (returnedwhat == LUA_TBOOLEAN)
ok = lua_toboolean(lua, -1);
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(commitTransaction) END" << endl;
return ok;
}
bool LUABackend::abortTransaction() {
if (f_lua_aborttransaction == 0)
return false;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(abortTransaction) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_aborttransaction);
if(lua_pcall(lua, 0, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(abortTransaction)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
size_t returnedwhat = lua_type(lua, -1);
bool ok = false;
if (returnedwhat == LUA_TBOOLEAN)
ok = lua_toboolean(lua, -1);
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(abortTransaction) END" << endl;
return ok;
}
bool LUABackend::feedRecord(const DNSResourceRecord &rr) {
if (f_lua_feedrecord == 0)
return false;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(feedRecord) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_feedrecord);
dnsrr_to_table(lua, &rr);
if(lua_pcall(lua, 1, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(feedRecord)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
size_t returnedwhat = lua_type(lua, -1);
bool ok = false;
if (returnedwhat == LUA_TBOOLEAN)
ok = lua_toboolean(lua, -1);
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(feedRecord) END" << endl;
return ok;
}
void LUABackend::setFresh(int id) {
if (f_lua_setfresh == 0)
return;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(setFresh) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_setfresh);
lua_pushnumber(lua, id);
if(lua_pcall(lua, 1, 0, f_lua_exec_error) != 0) {
string e = backend_name + "(setFresh)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return;
}
if (logging)
L << Logger::Info << backend_name << "(setFresh) END" << endl;
}
void LUABackend::getUnfreshSlaveInfos(vector<DomainInfo>* domains) {
if (f_lua_getunfreshslaveinfos == 0)
return;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(getUnfreshSlaveInfos) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_getunfreshslaveinfos);
if(lua_pcall(lua, 0, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(getUnfreshSlaveInfos)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return;
}
size_t returnedwhat = lua_type(lua, -1);
if (returnedwhat != LUA_TTABLE) {
lua_pop(lua, 1 );
return;
}
domains_from_table(domains);
if (logging)
L << Logger::Info << backend_name << "(getUnfreshSlaveInfos) END" << endl;
}
bool LUABackend::isMaster(const string &domain, const string &ip) {
if (f_lua_ismaster == 0)
return false;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Error << backend_name << "(isMaster) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_ismaster);
lua_pushstring(lua, domain.c_str());
lua_pushstring(lua, ip.c_str());
if(lua_pcall(lua, 2, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(isMaster)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
size_t returnedwhat = lua_type(lua, -1);
bool ok = false;
if (returnedwhat == LUA_TBOOLEAN)
ok = lua_toboolean(lua, -1);
lua_pop(lua, 1);
if (logging)
L << Logger::Info << backend_name << "(isMaster) END" << endl;
return ok;
}
bool LUABackend::getDomainInfo(const string &domain, DomainInfo &di) {
if (f_lua_getdomaininfo == 0)
return false;
Lock l(&lock_the_lock);
if (logging)
L << Logger::Info << backend_name << "(getDomainInfo) BEGIN" << endl;
lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_getdomaininfo);
lua_pushlstring(lua, domain.c_str(), domain.size());
if(lua_pcall(lua, 1, 1, f_lua_exec_error) != 0) {
string e = backend_name + "(getDomainInfo)" + lua_tostring(lua, -1);
lua_pop(lua, 1);
throw runtime_error(e);
return false;
}
size_t returnedwhat = lua_type(lua, -1);
if (returnedwhat != LUA_TTABLE) {
lua_pop(lua, 1 );
if (logging)
L << Logger::Info << backend_name << "(getDomainInfo) END - Didn't get a table as a expected return value" << endl;
return false;
}
if (logging)
L << Logger::Info << backend_name << "(getDomainInfo) END" << endl;
return domaininfo_from_table(&di);
}