[DNSRSLVR] R_ResolverFlushCache should only flush cached records which were not read from the hosts file

This commit is contained in:
Eric Kohl 2020-01-06 00:45:41 +01:00
parent df84f41298
commit 17f932d78e
4 changed files with 47 additions and 23 deletions

View file

@ -44,7 +44,7 @@ DnsIntCacheFree(VOID)
if (!DnsCache.RecordList.Flink) if (!DnsCache.RecordList.Flink)
return; return;
DnsIntCacheFlush(); DnsIntCacheFlush(CACHE_FLUSH_ALL);
DeleteCriticalSection(&DnsCache.Lock); DeleteCriticalSection(&DnsCache.Lock);
DnsCacheInitialized = FALSE; DnsCacheInitialized = FALSE;
@ -65,13 +65,14 @@ DnsIntCacheRemoveEntryItem(PRESOLVER_CACHE_ENTRY CacheEntry)
HeapFree(GetProcessHeap(), 0, CacheEntry); HeapFree(GetProcessHeap(), 0, CacheEntry);
} }
VOID DNS_STATUS
DnsIntCacheFlush(VOID) DnsIntCacheFlush(
_In_ ULONG ulFlags)
{ {
PLIST_ENTRY Entry; PLIST_ENTRY Entry, NextEntry;
PRESOLVER_CACHE_ENTRY CacheEntry; PRESOLVER_CACHE_ENTRY CacheEntry;
DPRINT("DnsIntCacheFlush()\n"); DPRINT("DnsIntCacheFlush(%lu)\n", ulFlags);
/* Lock the cache */ /* Lock the cache */
DnsCacheLock(); DnsCacheLock();
@ -80,18 +81,24 @@ DnsIntCacheFlush(VOID)
Entry = DnsCache.RecordList.Flink; Entry = DnsCache.RecordList.Flink;
while (Entry != &DnsCache.RecordList) while (Entry != &DnsCache.RecordList)
{ {
NextEntry = Entry->Flink;
/* Get this entry */ /* Get this entry */
CacheEntry = CONTAINING_RECORD(Entry, RESOLVER_CACHE_ENTRY, CacheLink); CacheEntry = CONTAINING_RECORD(Entry, RESOLVER_CACHE_ENTRY, CacheLink);
/* Remove it from list */ /* Remove it from list */
if (((ulFlags & CACHE_FLUSH_HOSTS_FILE_ENTRIES) && (CacheEntry->bHostsFileEntry != FALSE)) ||
((ulFlags & CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES) && (CacheEntry->bHostsFileEntry == FALSE)))
DnsIntCacheRemoveEntryItem(CacheEntry); DnsIntCacheRemoveEntryItem(CacheEntry);
/* Move to the next entry */ /* Move to the next entry */
Entry = DnsCache.RecordList.Flink; Entry = NextEntry;
} }
/* Unlock the cache */ /* Unlock the cache */
DnsCacheUnlock(); DnsCacheUnlock();
return ERROR_SUCCESS;
} }
DNS_STATUS DNS_STATUS
@ -178,11 +185,14 @@ DnsIntCacheRemoveEntryByName(LPCWSTR Name)
} }
VOID VOID
DnsIntCacheAddEntry(PDNS_RECORDW Record) DnsIntCacheAddEntry(
_In_ PDNS_RECORDW Record,
_In_ BOOL bHostsFileEntry)
{ {
PRESOLVER_CACHE_ENTRY Entry; PRESOLVER_CACHE_ENTRY Entry;
DPRINT("DnsIntCacheAddEntry(%p)\n", Record); DPRINT("DnsIntCacheAddEntry(%p %u)\n",
Record, bHostsFileEntry);
DPRINT("Name: %S\n", Record->pName); DPRINT("Name: %S\n", Record->pName);
DPRINT("TTL: %lu\n", Record->dwTtl); DPRINT("TTL: %lu\n", Record->dwTtl);
@ -195,6 +205,7 @@ DnsIntCacheAddEntry(PDNS_RECORDW Record)
if (!Entry) if (!Entry)
return; return;
Entry->bHostsFileEntry = bHostsFileEntry;
Entry->Record = DnsRecordSetCopyEx(Record, DnsCharSetUnicode, DnsCharSetUnicode); Entry->Record = DnsRecordSetCopyEx(Record, DnsCharSetUnicode, DnsCharSetUnicode);
/* Insert it to our List */ /* Insert it to our List */

View file

@ -113,7 +113,7 @@ AddIpv4HostEntries(
/* Prepare the PTR record */ /* Prepare the PTR record */
swprintf(szReverseName, swprintf(szReverseName,
L"%u.%u.%u.%u.in-addr.arpa", L"%u.%u.%u.%u.in-addr.arpa.",
pAddress->S_un.S_un_b.s_b4, pAddress->S_un.S_un_b.s_b4,
pAddress->S_un.S_un_b.s_b3, pAddress->S_un.S_un_b.s_b3,
pAddress->S_un.S_un_b.s_b2, pAddress->S_un.S_un_b.s_b2,
@ -130,8 +130,8 @@ AddIpv4HostEntries(
PtrRecord.Data.PTR.pNameHost = pszHostName; PtrRecord.Data.PTR.pNameHost = pszHostName;
DnsIntCacheAddEntry(&ARecord); DnsIntCacheAddEntry(&ARecord, TRUE);
DnsIntCacheAddEntry(&PtrRecord); DnsIntCacheAddEntry(&PtrRecord, TRUE);
} }
@ -171,7 +171,7 @@ AddIpv6HostEntries(
szReverseName[j + 2] = szHexChar[(pAddress->u.Byte[k] >> 4) & 0xF]; szReverseName[j + 2] = szHexChar[(pAddress->u.Byte[k] >> 4) & 0xF];
szReverseName[j + 3] = L'.'; szReverseName[j + 3] = L'.';
} }
wcscat(szReverseName, L"ip6.arpa"); wcscat(szReverseName, L"ip6.arpa.");
ZeroMemory(&PtrRecord, sizeof(DNS_RECORDW)); ZeroMemory(&PtrRecord, sizeof(DNS_RECORDW));
@ -184,8 +184,8 @@ AddIpv6HostEntries(
PtrRecord.Data.PTR.pNameHost = pszHostName; PtrRecord.Data.PTR.pNameHost = pszHostName;
DnsIntCacheAddEntry(&AAAARecord); DnsIntCacheAddEntry(&AAAARecord, TRUE);
DnsIntCacheAddEntry(&PtrRecord); DnsIntCacheAddEntry(&PtrRecord, TRUE);
} }

View file

@ -27,6 +27,7 @@
typedef struct _RESOLVER_CACHE_ENTRY typedef struct _RESOLVER_CACHE_ENTRY
{ {
LIST_ENTRY CacheLink; LIST_ENTRY CacheLink;
BOOL bHostsFileEntry;
PDNS_RECORDW Record; PDNS_RECORDW Record;
} RESOLVER_CACHE_ENTRY, *PRESOLVER_CACHE_ENTRY; } RESOLVER_CACHE_ENTRY, *PRESOLVER_CACHE_ENTRY;
@ -42,7 +43,14 @@ typedef struct _RESOLVER_CACHE
VOID DnsIntCacheInitialize(VOID); VOID DnsIntCacheInitialize(VOID);
VOID DnsIntCacheRemoveEntryItem(PRESOLVER_CACHE_ENTRY CacheEntry); VOID DnsIntCacheRemoveEntryItem(PRESOLVER_CACHE_ENTRY CacheEntry);
VOID DnsIntCacheFree(VOID); VOID DnsIntCacheFree(VOID);
VOID DnsIntCacheFlush(VOID);
#define CACHE_FLUSH_HOSTS_FILE_ENTRIES 0x00000001
#define CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES 0x00000002
#define CACHE_FLUSH_ALL 0x00000003
DNS_STATUS
DnsIntCacheFlush(
_In_ ULONG ulFlags);
DNS_STATUS DNS_STATUS
DnsIntCacheGetEntryByName( DnsIntCacheGetEntryByName(
@ -51,8 +59,14 @@ DnsIntCacheGetEntryByName(
DWORD dwFlags, DWORD dwFlags,
PDNS_RECORDW *Record); PDNS_RECORDW *Record);
VOID DnsIntCacheAddEntry(PDNS_RECORDW Record); VOID
BOOL DnsIntCacheRemoveEntryByName(LPCWSTR Name); DnsIntCacheAddEntry(
_In_ PDNS_RECORDW Record,
_In_ BOOL bHostsFileEntry);
BOOL
DnsIntCacheRemoveEntryByName(
_In_ LPCWSTR Name);
DNS_STATUS DNS_STATUS
DnsIntCacheGetEntries( DnsIntCacheGetEntries(

View file

@ -62,11 +62,10 @@ __stdcall
R_ResolverFlushCache( R_ResolverFlushCache(
_In_ DNSRSLVR_HANDLE pwszServerName) _In_ DNSRSLVR_HANDLE pwszServerName)
{ {
DPRINT("R_ResolverFlushCache()\n"); DPRINT("R_ResolverFlushCache(%S)\n",
pwszServerName);
// FIXME Should store (and flush) entries by server handle return DnsIntCacheFlush(CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES);
DnsIntCacheFlush();
return ERROR_SUCCESS;
} }
@ -126,7 +125,7 @@ R_ResolverQuery(
if (Status == ERROR_SUCCESS) if (Status == ERROR_SUCCESS)
{ {
DPRINT("DNS query successful!\n"); DPRINT("DNS query successful!\n");
DnsIntCacheAddEntry(*ppResultRecords); DnsIntCacheAddEntry(*ppResultRecords, FALSE);
} }
} }
} }