Skip to content

Commit 872a59d

Browse files
committed
NetworkTools: Improve whois performance, Fix using DoH resolution when disabled
1 parent 4699d4d commit 872a59d

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

plugins/NetworkTools/whois.c

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Process Hacker Network Tools -
33
* Whois dialog
44
*
5-
* Copyright (C) 2013-2020 dmex
5+
* Copyright (C) 2013-2021 dmex
66
*
77
* This file is part of Process Hacker.
88
*
@@ -181,9 +181,9 @@ BOOLEAN WhoisExtractServerUrl(
181181
ULONG_PTR whoisServerHostnameLength;
182182
PPH_STRING whoisServerName;
183183

184-
if ((whoisServerHostnameIndex = PhFindStringInString(WhoisResponse, 0, L"whois:")) == -1)
184+
if ((whoisServerHostnameIndex = PhFindStringInString(WhoisResponse, 0, L"whois:")) == SIZE_MAX)
185185
return FALSE;
186-
if ((whoisServerHostnameLength = PhFindStringInString(WhoisResponse, whoisServerHostnameIndex, L"\n")) == -1)
186+
if ((whoisServerHostnameLength = PhFindStringInString(WhoisResponse, whoisServerHostnameIndex, L"\n")) == SIZE_MAX)
187187
return FALSE;
188188
if ((whoisServerHostnameLength = whoisServerHostnameLength - whoisServerHostnameIndex) == 0)
189189
return FALSE;
@@ -217,9 +217,9 @@ BOOLEAN WhoisExtractReferralServer(
217217
WCHAR urlPort[0x100] = L"";
218218
WCHAR urlPath[0x100] = L"";
219219

220-
if ((whoisServerHostnameIndex = PhFindStringInString(WhoisResponse, 0, L"ReferralServer:")) == -1)
220+
if ((whoisServerHostnameIndex = PhFindStringInString(WhoisResponse, 0, L"ReferralServer:")) == SIZE_MAX)
221221
return FALSE;
222-
if ((whoisServerHostnameLength = PhFindStringInString(WhoisResponse, whoisServerHostnameIndex, L"\n")) == -1)
222+
if ((whoisServerHostnameLength = PhFindStringInString(WhoisResponse, whoisServerHostnameIndex, L"\n")) == SIZE_MAX)
223223
return FALSE;
224224
if ((whoisServerHostnameLength = whoisServerHostnameLength - whoisServerHostnameIndex) == 0)
225225
return FALSE;
@@ -293,11 +293,23 @@ BOOLEAN WhoisConnectServer(
293293
SOCKET whoisSocketHandle = INVALID_SOCKET;
294294
PDNS_RECORD dnsRecordList;
295295

296-
dnsRecordList = PhDnsQuery(
297-
NULL,
298-
WhoisServerAddress,
299-
DnsQueryMessageType
300-
);
296+
if (PhGetIntegerSetting(L"EnableNetworkResolveDoH"))
297+
{
298+
dnsRecordList = PhDnsQuery(
299+
NULL,
300+
WhoisServerAddress,
301+
DnsQueryMessageType
302+
);
303+
}
304+
else
305+
{
306+
dnsRecordList = PhDnsQuery2(
307+
NULL,
308+
WhoisServerAddress,
309+
DnsQueryMessageType,
310+
DNS_QUERY_NO_HOSTS_FILE // DNS_QUERY_BYPASS_CACHE
311+
);
312+
}
301313

302314
if (!dnsRecordList)
303315
return FALSE;
@@ -320,6 +332,31 @@ BOOLEAN WhoisConnectServer(
320332

321333
if ((whoisSocketHandle = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0)) != INVALID_SOCKET)
322334
{
335+
ULONG bestInterfaceIndex;
336+
337+
if (GetBestInterfaceEx((PSOCKADDR)&remoteAddr, &bestInterfaceIndex) == ERROR_SUCCESS)
338+
{
339+
MIB_IPFORWARD_ROW2 bestAddressRoute = { 0 };
340+
SOCKADDR_INET destinationAddress = { 0 };
341+
SOCKADDR_INET bestSourceAddress = { 0 };
342+
343+
destinationAddress.si_family = AF_INET;
344+
destinationAddress.Ipv4 = remoteAddr;
345+
346+
if (GetBestRoute2(
347+
NULL,
348+
bestInterfaceIndex,
349+
NULL,
350+
&destinationAddress,
351+
0,
352+
&bestAddressRoute,
353+
&bestSourceAddress
354+
) == ERROR_SUCCESS)
355+
{
356+
bind(whoisSocketHandle, (PSOCKADDR)&bestSourceAddress.Ipv4, sizeof(bestSourceAddress.Ipv4));
357+
}
358+
}
359+
323360
if (WSAConnect(whoisSocketHandle, (PSOCKADDR)&remoteAddr, sizeof(SOCKADDR_IN), NULL, NULL, NULL, NULL) != SOCKET_ERROR)
324361
break;
325362

@@ -343,6 +380,31 @@ BOOLEAN WhoisConnectServer(
343380

344381
if ((whoisSocketHandle = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0)) != INVALID_SOCKET)
345382
{
383+
ULONG bestInterfaceIndex;
384+
385+
if (GetBestInterfaceEx((PSOCKADDR)&remoteAddr, &bestInterfaceIndex) == ERROR_SUCCESS)
386+
{
387+
MIB_IPFORWARD_ROW2 bestAddressRoute = { 0 };
388+
SOCKADDR_INET destinationAddress = { 0 };
389+
SOCKADDR_INET bestSourceAddress = { 0 };
390+
391+
destinationAddress.si_family = AF_INET6;
392+
destinationAddress.Ipv6 = remoteAddr;
393+
394+
if (GetBestRoute2(
395+
NULL,
396+
bestInterfaceIndex,
397+
NULL,
398+
&destinationAddress,
399+
0,
400+
&bestAddressRoute,
401+
&bestSourceAddress
402+
) == ERROR_SUCCESS)
403+
{
404+
bind(whoisSocketHandle, (PSOCKADDR)&bestSourceAddress.Ipv6, sizeof(bestSourceAddress.Ipv6));
405+
}
406+
}
407+
346408
if (WSAConnect(whoisSocketHandle, (PSOCKADDR)&remoteAddr, sizeof(SOCKADDR_IN6), NULL, NULL, NULL, NULL) != SOCKET_ERROR)
347409
break;
348410

0 commit comments

Comments
 (0)