-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnssync.pas
More file actions
426 lines (355 loc) · 11.6 KB
/
dnssync.pas
File metadata and controls
426 lines (355 loc) · 11.6 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
{ Copyright (C) 2005 Bas Steendijk and Peter Green
For conditions of distribution and use, see copyright notice in zlib_license.txt
which is included in the package
----------------------------------------------------------------------------- }
unit dnssync;
{$ifdef fpc}
{$mode delphi}
{$endif}
{$include lcoreconfig.inc}
interface
uses
dnscore,
binipstuff,
{$ifdef mswindows}
winsock,
windows,
{$else}
{$ifdef VER1_0}
linux,
{$else}
baseunix,unix,unixutil,
{$endif}
sockets,
fd_utils,
{$endif}
lcorernd,
sysutils,
ltimevalstuff;
//convert a name to an IP
//will return v4 or v6 depending on what seems favorable, or manual preference setting
//on error the binip will have a family of 0 (other fields are also currently
//zeroed out but may be used for further error information in future)
//timeout is in milliseconds, it is ignored when using windows dns
function forwardlookup(name:ansistring;timeout:integer):tbinip;
//convert a name to a list of all IP's returned
//this returns both v4 and v6 IP's, or possibly only v4 or v6, depending on settings
//on error, returns an empty list
function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
//convert an IP to a name, on error a null string will be returned, other
//details as above
function reverselookup(ip:tbinip;timeout:integer):ansistring;
const
tswrap=$4000;
tsmask=tswrap-1;
numsock=1{$ifdef ipv6}+1{$endif};
defaulttimeout=10000;
const mintimeout=16;
toport='53';
implementation
{$ifdef mswindows}
uses dnswin;
{$endif}
{$ifndef mswindows}
{$define syncdnscore}
{$endif}
{$i unixstuff.inc}
type tdnsstatearr=array[0..numsock-1] of tdnsstate;
{$ifdef syncdnscore}
{$ifdef mswindows}
const
winsocket = 'wsock32.dll';
function sendto(s: TSocket; const Buf; len, flags: Integer; var addrto: TinetSockAddrV; tolen: Integer): Integer; stdcall; external winsocket name 'sendto';
function bind(s: TSocket; var addr: TinetSockAddrV; namelen: Integer): Longbool; stdcall; external winsocket name 'bind';
type
fdset=tfdset;
{$endif}
function getts:integer;
{$ifdef mswindows}
begin
result := GetTickCount and tsmask;
{$else}
var
temp:ttimeval;
begin
gettimeofday(temp);
result := ((temp.tv_usec div 1000) + (temp.tv_sec * 1000)) and tsmask;
{$endif}
end;
procedure resolveloop(timeout:integer;var state:tdnsstatearr;numsockused:integer);
var
selectresult : integer;
fds : fdset;
endtime : longint;
starttime : longint;
wrapmode : boolean;
currenttime : integer;
lag : ttimeval;
selecttimeout : ttimeval;
socknum:integer;
needprocessing:array[0..numsock-1] of boolean;
finished:array[0..numsock-1] of boolean;
a,b:integer;
Src : TInetSockAddrV;
Srcx : {$ifdef mswindows}sockaddr_in{$else}TInetSockAddrV{$endif} absolute Src;
SrcLen : Integer;
fromip:tbinip;
fromport:ansistring;
fd:array[0..numsock-1] of integer;
toaddr:array[0..numsock-1] of tbinip;
id:integer;
sendquerytime:array[0..numsock-1] of integer;
procedure setupsocket;
var
inAddrtemp : TInetSockAddrV;
biniptemp:tbinip;
a,retrycount,porttemp:integer;
bindresult:boolean;
begin
biniptemp := getcurrentsystemnameserverbin(id);
//must get the DNS server here so we know to init v4 or v6
if biniptemp.family = AF_INET6 then biniptemp := ipstrtobinf('::') else biniptemp := ipstrtobinf('0.0.0.0');
for a := 0 to numsockused-1 do begin
retrycount := 5;
repeat
if (retrycount <= 1) then begin
porttemp := 0; //for the last attempt let the OS decide
end else begin
porttemp := 1024 + randominteger(65536 - 1024);
end;
makeinaddrv(biniptemp,inttostr( porttemp ),inaddrtemp);
fd[a] := Socket(biniptemp.family,SOCK_DGRAM,0);
bindresult := {$ifdef mswindows}Not{$endif} Bind(fd[a],inAddrtemp,inaddrsize(inaddrtemp));
dec(retrycount);
until (retrycount <= 0) or (bindresult);
If (not bindresult) Then begin
{$ifdef mswindows}
raise Exception.create('unable to bind '+inttostr(WSAGetLastError));
{$else}
raise Exception.create('unable to bind '+inttostr(socketError));
{$endif}
end;
end;
end;
procedure cleanupsockets;
var
a:integer;
begin
for a := 0 to numsockused-1 do closesocket(fd[a]);
end;
function sendquery(socknum:integer;const packet:tdnspacket;len:integer):boolean;
var
ip : tbinip;
port : ansistring;
inaddr : TInetSockAddrV;
begin
{ writeln('sendquery ',decodename(state.packet,state.packetlen,12,0,a),' ',state.requesttype);}
result := false;
if len = 0 then exit; {no packet}
ip := getcurrentsystemnameserverbin(id);
{$ifdef ipv6}{$ifdef mswindows}
if toaddr[socknum].family = AF_INET6 then if (useaf = 0) then useaf := useaf_preferv6;
{$endif}{$endif}
port := toport;
toaddr[socknum] := ip;
makeinaddrv(toaddr[socknum],port,inaddr);
sendto(fd[socknum],packet,len,0,inaddr,inaddrsize(inaddr));
sendquerytime[socknum] := getts;
result := true;
end;
begin
if timeout < mintimeout then timeout := defaulttimeout;
starttime := getts;
endtime := starttime + timeout;
if (endtime and tswrap)=0 then begin
wrapmode := false;
end else begin
wrapmode := true;
end;
endtime := endtime and tsmask;
setupsocket;
for socknum := 0 to numsockused-1 do begin
needprocessing[socknum] := true;
finished[socknum] := false;
end;
repeat
for socknum := numsockused-1 downto 0 do if needprocessing[socknum] then begin
state_process(state[socknum]);
case state[socknum].resultaction of
action_ignore: begin
{do nothing}
end;
action_done: begin
finished[socknum] := true;
//exit if all resolvers are finished
b := 0;
for a := 0 to numsockused-1 do begin
if finished[a] then inc(b);
end;
if (b = numsockused) then begin
cleanupsockets;
exit;
end;
//onrequestdone(self,0);
end;
action_sendquery:begin
{ writeln('send query');}
sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
end;
end;
needprocessing[socknum] := false;
end;
currenttime := getts;
msectotimeval(selecttimeout, (endtime-currenttime) and tsmask);
fd_zero(fds);
for socknum := numsockused-1 downto 0 do if not finished[socknum] then fd_set(fd[socknum],fds);
if (selecttimeout.tv_sec > 0) or (selecttimeout.tv_usec > retryafter) then begin
selecttimeout.tv_sec := 0;
selecttimeout.tv_usec := retryafter;
end;
//find the highest of the used fd's
b := 0;
for socknum := numsockused-1 downto 0 do if fd[socknum] > b then b := fd[socknum];
selectresult := select(b+1,@fds,nil,nil,@selecttimeout);
if selectresult > 0 then begin
currenttime := getts;
for socknum := numsockused-1 downto 0 do if fd_isset(fd[socknum],fds) then begin
{ writeln('selectresult>0');}
//why the hell are we zeroing out the packet buffer before reading into it?! --plugwash
fillchar(state[socknum].recvpacket,sizeof(state[socknum].recvpacket),0);
msectotimeval(lag,(currenttime-sendquerytime[socknum]) and tsmask);
reportlag(id,(lag.tv_sec*1000000)+lag.tv_usec);
SrcLen := SizeOf(Src);
state[socknum].recvpacketlen := recvfrom(fd[socknum],state[socknum].recvpacket, SizeOf(state[socknum].recvpacket),0,Srcx,SrcLen);
if (state[socknum].recvpacketlen > 0) then begin
fromip := inaddrvtobinip(Src);
fromport := inttostr(htons(src.InAddr.port));
if ((not comparebinip(toaddr[socknum],fromip)) or (fromport <> toport)) then begin
// writeln('dnssync received from wrong IP:port ',ipbintostr(fromip),'#',fromport);
state[socknum].recvpacketlen := 0;
end else begin
state[socknum].parsepacket := true;
needprocessing[socknum] := true;
end;
end;
end;
end;
if selectresult < 0 then exit;
if selectresult = 0 then begin
currenttime := getts;
reportlag(id,-1);
if (currenttime >= endtime) and ((not wrapmode) or (currenttime < starttime)) then begin
cleanupsockets;
exit;
end else begin
//resend
for socknum := numsockused-1 downto 0 do begin
sendquery(socknum,state[socknum].sendpacket,state[socknum].sendpacketlen);
end;
end;
end;
until false;
end;
{$endif}
function forwardlookuplist(name:ansistring;timeout:integer):tbiniplist;
var
dummy : integer;
a:integer;
biniptemp:tbinip;
l:tbiniplist;
numsockused:integer;
state:tdnsstatearr;
begin
ipstrtobin(name,biniptemp);
if biniptemp.family <> 0 then begin
result := biniplist_new;
biniplist_add(result,biniptemp);
exit; //it was an IP address, no need for dns
end;
{$ifdef mswindows}
if usewindns then begin
if (useaf = useaf_v4) then a := af_inet else if (useaf = useaf_v6) then a := af_inet6 else a := 0;
result := winforwardlookuplist(name,a,dummy);
{$ifdef ipv6}
if (useaf = useaf_preferv4) then begin
{prefer mode: sort the IP's}
l := biniplist_new;
addipsoffamily(l,result,af_inet);
addipsoffamily(l,result,af_inet6);
result := l;
end;
if (useaf = useaf_preferv6) then begin
{prefer mode: sort the IP's}
l := biniplist_new;
addipsoffamily(l,result,af_inet6);
addipsoffamily(l,result,af_inet);
result := l;
end;
{$endif}
end else
{$endif}
begin
{$ifdef syncdnscore}
{$ifdef ipv6}initpreferredmode;{$endif}
numsockused := 0;
result := biniplist_new;
if (useaf <> useaf_v6) then begin
setstate_forward(name,state[numsockused],af_inet);
inc(numsockused);
end;
{$ifdef ipv6}
if (useaf <> useaf_v4) then begin
setstate_forward(name,state[numsockused],af_inet6);
inc(numsockused);
end;
{$endif}
resolveloop(timeout,state,numsockused);
if (numsockused = 1) then begin
biniplist_addlist(result,state[0].resultlist);
{$ifdef ipv6}
end else if (useaf = useaf_preferv6) then begin
biniplist_addlist(result,state[1].resultlist);
biniplist_addlist(result,state[0].resultlist);
end else begin
biniplist_addlist(result,state[0].resultlist);
biniplist_addlist(result,state[1].resultlist);
{$endif}
end;
{$endif}
end;
end;
function forwardlookup(name:ansistring;timeout:integer):tbinip;
var
listtemp:tbiniplist;
begin
listtemp := forwardlookuplist(name,timeout);
result := biniplist_get(listtemp,0);
end;
function reverselookup(ip:tbinip;timeout:integer):ansistring;
var
dummy : integer;
numsockused:integer;
state:tdnsstatearr;
begin
{$ifdef mswindows}
if usewindns then begin
result := winreverselookup(ip,dummy);
exit;
end;
{$endif}
{$ifdef syncdnscore}
setstate_reverse(ip,state[0]);
numsockused := 1;
resolveloop(timeout,state,numsockused);
result := state[0].resultstr;
{$endif}
end;
{$ifdef mswindows}
var
wsadata : twsadata;
initialization
WSAStartUp($2,wsadata);
finalization
WSACleanUp;
{$endif}
end.