-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmdnsResolverWindows.pas
312 lines (275 loc) · 8.47 KB
/
mdnsResolverWindows.pas
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
{$IFNDEF MDSN_UNIT_INCLUDE}
unit mdnsResolverWindows;
{$ENDIF}
{$IFDEF FPC}
{$mode Delphi}{$H+}
{$ENDIF}
interface
uses
Classes, SysUtils, windns, mdnsCore, SyncObjs, Generics.Collections
{$IFDEF FPC}, Forms{$ENDIF};
type
TMdnsResolver = class(TComponent)
protected
FServiceType: UnicodeString;
FSearchService: UnicodeString;
FOnResolved: TmdnsResolveEvent;
FRequest: TDNS_SERVICE_BROWSE_REQUEST;
FCancel: TDNS_SERVICE_CANCEL;
FMainThreadId: Cardinal;
FResultQueue: TList<PDNS_RECORDW>;
FErrorQueue: TList<DWORD>;
FErrorLock: TCriticalSection;
FResultLock: TCriticalSection;
procedure ProcessResults{$IFDEF FPC}(Data: PtrInt){$ENDIF};
procedure ProcessResult(const pDnsRecord: PDNS_RECORDW);
procedure ProcessError(Status: DWORD);
function GetServiceType: String;
procedure SetServiceType(NewType: String);
procedure AddResult(Result: PDNS_RECORDW);
procedure AddError(Error: DWORD);
public
procedure StartResolve;
procedure StopResolve;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property ServiceType: String read GetServiceType write SetServiceType;
property OnResolved: TmdnsResolveEvent read FOnResolved write FOnResolved;
end;
implementation
uses Windows;
type
PMdnsResolver = ^TMdnsResolver;
var
CallBack: TDNS_CALLBACK;
function formatIpV6Address(Address: TIP6_ADDRESS): String;
var
x: Integer;
begin
Result := '';
for x := 0 to 15 do begin
if (x > 0) and ((x and 1) = 0) then Result := Result + ':';
Result := Result + IntToHex(Address.IP6Byte[x]);
end;
end;
function formatIpV4Address(Address: TIP4_ADDRESS): String;
var
x: Integer;
begin
Result := '';
for x := 0 to 3 do begin
if (x > 0) then Result := Result + '.';
Result := Result + IntToStr(Address.IP4Byte[x]);
end;
end;
function dnsbrowsecallback(Status: DWord; pQueryContext: Pointer; pDnsRecord: PDNS_RECORDW): TDNS_STATUS; winapi;
begin
try
if status = 0 then begin
if Assigned(pDnsRecord) then
TMdnsResolver(pQueryContext).AddResult(pDnsRecord);
end else begin
TMdnsResolver(pQueryContext).AddError(Status);
end;
Result := 0;
except
Result := -1;
end;
end;
constructor TMdnsResolver.Create(AOwner: TComponent);
begin
inherited;
FResultQueue := TList<PDNS_RECORDW>.Create;
FResultLock := SyncObjs.TCriticalSection.Create;
FErrorQueue := TList<DWORD>.Create;
FErrorLock := SyncObjs.TCriticalSection.Create;
end;
destructor TMdnsResolver.Destroy;
begin
if Assigned(FResultQueue) then
FreeAndNil(FResultQueue);
if Assigned(FResultLock) then
FreeAndNil(FResultLock);
if Assigned(FErrorQueue) then
FreeAndNil(FErrorQueue);
if Assigned(FErrorLock) then
FreeAndNil(FErrorLock);
inherited;
end;
procedure TMdnsResolver.StartResolve;
var
Status: TDNS_STATUS;
begin
FMainThreadId := GetCurrentThreadId;
InitWindns;
CallBack.pBrowseCallback := @dnsbrowsecallback;
FSearchService := FServiceType;
if Copy(FSearchService, Length(FSearchService) - 6, 7) = '.local.' then
Delete(FSearchService, Length(FSearchService), 1)
else if FSearchService[Length(FSearchService)] = '.' then
FSearchService := FSearchService + 'local'
else if Copy(FSearchService, Length(FSearchService) - 5, 6) <> '.local' then
FSearchService := FSearchService + '.local';
FRequest.Version := 1;
FRequest.InterfaceIndex := 0;
FRequest.QueryName := @FSearchService[1];
FRequest.pQueryContext := self;
FRequest.Callback := CallBack;
FCancel.reserved := nil;
Status := DnsServiceBrowse(@FRequest, @FCancel);
if Status <> DNS_REQUEST_PENDING then
raise mdnsException.Create('DNS error ' + IntToStr(Status));
end;
function TMdnsResolver.GetServiceType: String;
begin
Result := String(FServiceType);
end;
procedure TMdnsResolver.SetServiceType(NewType: String);
begin
FServiceType := UnicodeString(NewType);
end;
procedure TMdnsResolver.ProcessResult(const pDnsRecord: PDNS_RECORDW);
var
Result: TmdnsResult;
x: Integer;
Str: PWideChar;
Host: String;
CurrentRecord: PDNS_RECORDW;
begin
CurrentRecord := pDnsRecord;
while Assigned(CurrentRecord) do begin
if CurrentRecord^.wType = DNS_TYPE_PTR then begin
Result.PTR.Name := CurrentRecord^.pName;
Result.PTR.NameHost := CurrentRecord^.data.PTR.pNameHost;
end else if CurrentRecord^.wType = DNS_TYPE_SRV then begin
Result.SRV.Name := CurrentRecord^.pName;
Result.SRV.NameTarget := CurrentRecord^.data.SRV.pNameTarget;
Result.SRV.Port := CurrentRecord^.data.SRV.wPort;
Result.SRV.Priority := CurrentRecord^.data.SRV.wPriority;
Result.SRV.Weight := CurrentRecord^.data.SRV.wWeight;
end else if CurrentRecord^.wType = DNS_TYPE_A then begin
Result.A.Name := CurrentRecord^.pName;
Result.A.IpAddress := formatIpV4Address(CurrentRecord^.data.A.IpAddress);
end else if CurrentRecord^.wType = DNS_TYPE_TEXT then begin
Result.TXT.Name := pDnsRecord^.pName;
{$IFDEF WIN32}
SetLength(Result.TXT.Strings, CurrentRecord^.data.TXT.dwStringCount);
for x := 0 to Integer(CurrentRecord^.data.TXT.dwStringCount) -1 do begin
Str := CurrentRecord^.data.TXT.pStringArray[x];
Result.TXT.Strings[x] := Str;
end;
{$ENDIF}
end else if CurrentRecord^.wType = DNS_TYPE_AAAA then begin
Result.AAAA.Name := CurrentRecord^.pName;
Result.AAAA.IpAddress := formatIpV6Address(CurrentRecord^.data.AAAA.Ip6Address);
end;
CurrentRecord := CurrentRecord^.Next;
end;
Result.Errorcode := 0;
Result.isError := False;
if Result.A.Name <> '' then
Host := Result.A.IpAddress
else if Result.AAAA.Name <> '' then
Host := Result.AAAA.IpAddress
else if Result.SRV.Name <> '' then
Host := Result.SRV.NameTarget;
if (Host <> '') and (Result.SRV.Name <> '') then begin
Result.Host := Host;
Result.Port := Result.SRV.Port;
end;
if Assigned(FOnResolved) and (Assigned(pDnsRecord)) then
FOnResolved(Self, Result);
DnsRecordListFree(pDnsRecord, DnsFreeRecordList);
end;
procedure TMdnsResolver.ProcessError(Status: DWORD);
var
Result: TmdnsResult;
begin
Result.PTR.Name := GetServiceType;
Result.Errorcode := Status;
Result.isError := True;
if Assigned(FOnResolved) then
FOnResolved(Self, Result);
end;
procedure TMdnsResolver.StopResolve;
var
ErrorCode: TDNS_STATUS;
begin
if FCancel.reserved <> nil then begin
ErrorCode := DnsServiceBrowseCancel(@FCancel);
if ErrorCode <> ERROR_SUCCESS then
RaiseLastOSError;
end;
end;
procedure TMdnsResolver.AddResult(Result: PDNS_RECORDW);
begin
FResultLock.Enter;
try
FResultQueue.Add(Result);
finally
FResultLock.Leave;
end;
{$IFDEF FPC}
Application.QueueAsyncCall(ProcessResults, 0);
{$ELSE}
TThread.ForceQueue(nil, ProcessResults{$IFNDEF FPC}, 250{$ENDIF});
{$ENDIF}
end;
procedure TMdnsResolver.AddError(Error: DWORD);
begin
FErrorLock.Enter;
try
FErrorQueue.Add(Error);
finally
FErrorLock.Leave;
end;
{$IFDEF FPC}
Application.QueueAsyncCall(ProcessResults, 0);
{$ELSE}
TThread.ForceQueue(nil, ProcessResults);
{$ENDIF}
end;
procedure TMdnsResolver.ProcessResults{$IFDEF FPC}(Data: PtrInt){$ENDIF};
var
continueProcessing: Boolean;
mdnsResult: PDNS_RECORDW;
mdnsError: DWORD;
hadResult: Boolean;
begin
mdnsResult := nil;
mdnsError := 0;
continueProcessing := True;
while continueProcessing do begin
FResultLock.Enter;
try
hadResult := FResultQueue.Count >= 1;
if hadResult then begin
mdnsResult := FResultQueue.Items[0];
FResultQueue.Delete(0);
end;
continueProcessing := FResultQueue.Count >= 1;
if hadResult then
ProcessResult(mdnsResult);
finally
FResultLock.Leave;
end;
end;
continueProcessing := True;
while continueProcessing do begin
FErrorLock.Enter;
try
hadResult := FErrorQueue.Count >= 1;
if hadResult then begin
mdnsError := FErrorQueue.Items[0];
FErrorQueue.Delete(0);
end;
continueProcessing := FErrorQueue.Count >= 1;
if hadResult then
ProcessError(mdnsError);
finally
FErrorLock.Leave;
end;
end;
end;
end.