[DNSRSLVR] R_ResolverFlushCache should only flush cached records which were not read...
authorEric Kohl <eric.kohl@reactos.org>
Sun, 5 Jan 2020 23:45:41 +0000 (00:45 +0100)
committerEric Kohl <eric.kohl@reactos.org>
Sun, 5 Jan 2020 23:46:37 +0000 (00:46 +0100)
base/services/dnsrslvr/cache.c
base/services/dnsrslvr/hostsfile.c
base/services/dnsrslvr/precomp.h
base/services/dnsrslvr/rpcserver.c

index af4cc2a..e61cc86 100644 (file)
@@ -44,7 +44,7 @@ DnsIntCacheFree(VOID)
     if (!DnsCache.RecordList.Flink)
         return;
 
-    DnsIntCacheFlush();
+    DnsIntCacheFlush(CACHE_FLUSH_ALL);
 
     DeleteCriticalSection(&DnsCache.Lock);
     DnsCacheInitialized = FALSE;
@@ -65,13 +65,14 @@ DnsIntCacheRemoveEntryItem(PRESOLVER_CACHE_ENTRY CacheEntry)
     HeapFree(GetProcessHeap(), 0, CacheEntry);
 }
 
-VOID
-DnsIntCacheFlush(VOID)
+DNS_STATUS
+DnsIntCacheFlush(
+    _In_ ULONG ulFlags)
 {
-    PLIST_ENTRY Entry;
+    PLIST_ENTRY Entry, NextEntry;
     PRESOLVER_CACHE_ENTRY CacheEntry;
 
-    DPRINT("DnsIntCacheFlush()\n");
+    DPRINT("DnsIntCacheFlush(%lu)\n", ulFlags);
 
     /* Lock the cache */
     DnsCacheLock();
@@ -80,18 +81,24 @@ DnsIntCacheFlush(VOID)
     Entry = DnsCache.RecordList.Flink;
     while (Entry != &DnsCache.RecordList)
     {
+        NextEntry = Entry->Flink;
+
         /* Get this entry */
         CacheEntry = CONTAINING_RECORD(Entry, RESOLVER_CACHE_ENTRY, CacheLink);
 
         /* Remove it from list */
-        DnsIntCacheRemoveEntryItem(CacheEntry);
+        if (((ulFlags & CACHE_FLUSH_HOSTS_FILE_ENTRIES) && (CacheEntry->bHostsFileEntry != FALSE)) ||
+            ((ulFlags & CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES) && (CacheEntry->bHostsFileEntry == FALSE)))
+            DnsIntCacheRemoveEntryItem(CacheEntry);
 
         /* Move to the next entry */
-        Entry = DnsCache.RecordList.Flink;
+        Entry = NextEntry;
     }
 
     /* Unlock the cache */
     DnsCacheUnlock();
+
+    return ERROR_SUCCESS;
 }
 
 DNS_STATUS
@@ -178,11 +185,14 @@ DnsIntCacheRemoveEntryByName(LPCWSTR Name)
 }
 
 VOID
-DnsIntCacheAddEntry(PDNS_RECORDW Record)
+DnsIntCacheAddEntry(
+    _In_ PDNS_RECORDW Record,
+    _In_ BOOL bHostsFileEntry)
 {
     PRESOLVER_CACHE_ENTRY Entry;
 
-    DPRINT("DnsIntCacheAddEntry(%p)\n", Record);
+    DPRINT("DnsIntCacheAddEntry(%p %u)\n",
+           Record, bHostsFileEntry);
 
     DPRINT("Name: %S\n", Record->pName);
     DPRINT("TTL: %lu\n", Record->dwTtl);
@@ -195,6 +205,7 @@ DnsIntCacheAddEntry(PDNS_RECORDW Record)
     if (!Entry)
         return;
 
+    Entry->bHostsFileEntry = bHostsFileEntry;
     Entry->Record = DnsRecordSetCopyEx(Record, DnsCharSetUnicode, DnsCharSetUnicode);
 
     /* Insert it to our List */
index 1c80dc0..62554b9 100644 (file)
@@ -113,7 +113,7 @@ AddIpv4HostEntries(
 
     /* Prepare the PTR record */
     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_b3,
              pAddress->S_un.S_un_b.s_b2,
@@ -130,8 +130,8 @@ AddIpv4HostEntries(
 
     PtrRecord.Data.PTR.pNameHost = pszHostName;
 
-    DnsIntCacheAddEntry(&ARecord);
-    DnsIntCacheAddEntry(&PtrRecord);
+    DnsIntCacheAddEntry(&ARecord, TRUE);
+    DnsIntCacheAddEntry(&PtrRecord, TRUE);
 }
 
 
@@ -171,7 +171,7 @@ AddIpv6HostEntries(
         szReverseName[j + 2] = szHexChar[(pAddress->u.Byte[k] >> 4) & 0xF];
         szReverseName[j + 3] = L'.';
     }
-    wcscat(szReverseName, L"ip6.arpa");
+    wcscat(szReverseName, L"ip6.arpa.");
 
     ZeroMemory(&PtrRecord, sizeof(DNS_RECORDW));
 
@@ -184,8 +184,8 @@ AddIpv6HostEntries(
 
     PtrRecord.Data.PTR.pNameHost = pszHostName;
 
-    DnsIntCacheAddEntry(&AAAARecord);
-    DnsIntCacheAddEntry(&PtrRecord);
+    DnsIntCacheAddEntry(&AAAARecord, TRUE);
+    DnsIntCacheAddEntry(&PtrRecord, TRUE);
 }
 
 
index 81189cd..2b7e5f3 100644 (file)
@@ -27,6 +27,7 @@
 typedef struct _RESOLVER_CACHE_ENTRY
 {
     LIST_ENTRY CacheLink;
+    BOOL bHostsFileEntry;
     PDNS_RECORDW Record;
 } RESOLVER_CACHE_ENTRY, *PRESOLVER_CACHE_ENTRY;
 
@@ -42,7 +43,14 @@ typedef struct _RESOLVER_CACHE
 VOID DnsIntCacheInitialize(VOID);
 VOID DnsIntCacheRemoveEntryItem(PRESOLVER_CACHE_ENTRY CacheEntry);
 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
 DnsIntCacheGetEntryByName(
@@ -51,8 +59,14 @@ DnsIntCacheGetEntryByName(
     DWORD dwFlags,
     PDNS_RECORDW *Record);
 
-VOID DnsIntCacheAddEntry(PDNS_RECORDW Record);
-BOOL DnsIntCacheRemoveEntryByName(LPCWSTR Name);
+VOID
+DnsIntCacheAddEntry(
+    _In_ PDNS_RECORDW Record,
+    _In_ BOOL bHostsFileEntry);
+
+BOOL
+DnsIntCacheRemoveEntryByName(
+    _In_ LPCWSTR Name);
 
 DNS_STATUS
 DnsIntCacheGetEntries(
index f79850d..59e91cb 100644 (file)
@@ -62,11 +62,10 @@ __stdcall
 R_ResolverFlushCache(
     _In_ DNSRSLVR_HANDLE pwszServerName)
 {
-    DPRINT("R_ResolverFlushCache()\n");
+    DPRINT("R_ResolverFlushCache(%S)\n",
+           pwszServerName);
 
-    // FIXME Should store (and flush) entries by server handle
-    DnsIntCacheFlush();
-    return ERROR_SUCCESS;
+    return DnsIntCacheFlush(CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES);
 }
 
 
@@ -126,7 +125,7 @@ R_ResolverQuery(
             if (Status == ERROR_SUCCESS)
             {
                 DPRINT("DNS query successful!\n");
-                DnsIntCacheAddEntry(*ppResultRecords);
+                DnsIntCacheAddEntry(*ppResultRecords, FALSE);
             }
         }
     }