2019-10-27 12:45:52 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: base/services/dnsrslvr/cache.c
|
|
|
|
* PURPOSE: DNS cache functions
|
|
|
|
* PROGRAMMER: Peter Hater
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
|
|
|
|
//#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
static RESOLVER_CACHE DnsCache;
|
|
|
|
static BOOL DnsCacheInitialized = FALSE;
|
|
|
|
|
|
|
|
#define DnsCacheLock() do { EnterCriticalSection(&DnsCache.Lock); } while (0)
|
|
|
|
#define DnsCacheUnlock() do { LeaveCriticalSection(&DnsCache.Lock); } while (0)
|
|
|
|
|
|
|
|
VOID
|
|
|
|
DnsIntCacheInitialize(VOID)
|
|
|
|
{
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheInitialize()\n");
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Check if we're initialized */
|
|
|
|
if (DnsCacheInitialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Initialize the cache lock and namespace list */
|
|
|
|
InitializeCriticalSection((LPCRITICAL_SECTION)&DnsCache.Lock);
|
|
|
|
InitializeListHead(&DnsCache.RecordList);
|
|
|
|
DnsCacheInitialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
DnsIntCacheFree(VOID)
|
|
|
|
{
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheFree()\n");
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Check if we're initialized */
|
|
|
|
if (!DnsCacheInitialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!DnsCache.RecordList.Flink)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DnsIntCacheFlush();
|
|
|
|
|
|
|
|
DeleteCriticalSection(&DnsCache.Lock);
|
|
|
|
DnsCacheInitialized = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
DnsIntCacheRemoveEntryItem(PRESOLVER_CACHE_ENTRY CacheEntry)
|
|
|
|
{
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheRemoveEntryItem(%p)\n", CacheEntry);
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Remove the entry from the list */
|
|
|
|
RemoveEntryList(&CacheEntry->CacheLink);
|
|
|
|
|
|
|
|
/* Free record */
|
|
|
|
DnsRecordListFree(CacheEntry->Record, DnsFreeRecordList);
|
|
|
|
|
|
|
|
/* Delete us */
|
|
|
|
HeapFree(GetProcessHeap(), 0, CacheEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
DnsIntCacheFlush(VOID)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY Entry;
|
|
|
|
PRESOLVER_CACHE_ENTRY CacheEntry;
|
|
|
|
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheFlush()\n");
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Lock the cache */
|
|
|
|
DnsCacheLock();
|
|
|
|
|
|
|
|
/* Loop every entry */
|
|
|
|
Entry = DnsCache.RecordList.Flink;
|
|
|
|
while (Entry != &DnsCache.RecordList)
|
|
|
|
{
|
|
|
|
/* Get this entry */
|
|
|
|
CacheEntry = CONTAINING_RECORD(Entry, RESOLVER_CACHE_ENTRY, CacheLink);
|
|
|
|
|
|
|
|
/* Remove it from list */
|
|
|
|
DnsIntCacheRemoveEntryItem(CacheEntry);
|
|
|
|
|
|
|
|
/* Move to the next entry */
|
|
|
|
Entry = DnsCache.RecordList.Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the cache */
|
|
|
|
DnsCacheUnlock();
|
|
|
|
}
|
|
|
|
|
2019-11-23 16:07:59 +00:00
|
|
|
DNS_STATUS
|
|
|
|
DnsIntCacheGetEntryByName(
|
|
|
|
LPCWSTR Name,
|
|
|
|
WORD wType,
|
|
|
|
DWORD dwFlags,
|
|
|
|
PDNS_RECORDW *Record)
|
2019-10-27 12:45:52 +00:00
|
|
|
{
|
2019-11-23 16:07:59 +00:00
|
|
|
DNS_STATUS Status = DNS_INFO_NO_RECORDS;
|
2019-10-27 12:45:52 +00:00
|
|
|
PRESOLVER_CACHE_ENTRY CacheEntry;
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheGetEntryByName(%S %hu 0x%lx %p)\n",
|
|
|
|
Name, wType, dwFlags, Record);
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Assume failure */
|
|
|
|
*Record = NULL;
|
|
|
|
|
|
|
|
/* Lock the cache */
|
|
|
|
DnsCacheLock();
|
|
|
|
|
|
|
|
/* Match the Id with all the entries in the List */
|
|
|
|
NextEntry = DnsCache.RecordList.Flink;
|
|
|
|
while (NextEntry != &DnsCache.RecordList)
|
|
|
|
{
|
|
|
|
/* Get the Current Entry */
|
|
|
|
CacheEntry = CONTAINING_RECORD(NextEntry, RESOLVER_CACHE_ENTRY, CacheLink);
|
|
|
|
|
|
|
|
/* Check if this is the Catalog Entry ID we want */
|
|
|
|
if (_wcsicmp(CacheEntry->Record->pName, Name) == 0)
|
|
|
|
{
|
|
|
|
/* Copy the entry and return it */
|
|
|
|
*Record = DnsRecordSetCopyEx(CacheEntry->Record, DnsCharSetUnicode, DnsCharSetUnicode);
|
2019-11-23 16:07:59 +00:00
|
|
|
Status = ERROR_SUCCESS;
|
2019-10-27 12:45:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the cache */
|
|
|
|
DnsCacheUnlock();
|
|
|
|
|
2019-11-23 16:07:59 +00:00
|
|
|
return Status;
|
2019-10-27 12:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
DnsIntCacheRemoveEntryByName(LPCWSTR Name)
|
|
|
|
{
|
|
|
|
BOOL Ret = FALSE;
|
|
|
|
PRESOLVER_CACHE_ENTRY CacheEntry;
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheRemoveEntryByName(%S)\n", Name);
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Lock the cache */
|
|
|
|
DnsCacheLock();
|
|
|
|
|
|
|
|
/* Match the Id with all the entries in the List */
|
|
|
|
NextEntry = DnsCache.RecordList.Flink;
|
|
|
|
while (NextEntry != &DnsCache.RecordList)
|
|
|
|
{
|
|
|
|
/* Get the Current Entry */
|
|
|
|
CacheEntry = CONTAINING_RECORD(NextEntry, RESOLVER_CACHE_ENTRY, CacheLink);
|
|
|
|
|
|
|
|
/* Check if this is the Catalog Entry ID we want */
|
|
|
|
if (_wcsicmp(CacheEntry->Record->pName, Name) == 0)
|
|
|
|
{
|
|
|
|
/* Remove the entry */
|
|
|
|
DnsIntCacheRemoveEntryItem(CacheEntry);
|
|
|
|
Ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the cache */
|
|
|
|
DnsCacheUnlock();
|
|
|
|
|
|
|
|
/* Return */
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
DnsIntCacheAddEntry(PDNS_RECORDW Record)
|
|
|
|
{
|
|
|
|
PRESOLVER_CACHE_ENTRY Entry;
|
|
|
|
|
2019-11-23 16:07:59 +00:00
|
|
|
DPRINT("DnsIntCacheAddEntry(%p)\n", Record);
|
|
|
|
|
|
|
|
DPRINT("Name: %S\n", Record->pName);
|
|
|
|
DPRINT("TTL: %lu\n", Record->dwTtl);
|
2019-10-27 12:45:52 +00:00
|
|
|
|
|
|
|
/* Lock the cache */
|
|
|
|
DnsCacheLock();
|
|
|
|
|
|
|
|
/* Match the Id with all the entries in the List */
|
|
|
|
Entry = (PRESOLVER_CACHE_ENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(*Entry));
|
|
|
|
if (!Entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Entry->Record = DnsRecordSetCopyEx(Record, DnsCharSetUnicode, DnsCharSetUnicode);
|
|
|
|
|
|
|
|
/* Insert it to our List */
|
|
|
|
InsertTailList(&DnsCache.RecordList, &Entry->CacheLink);
|
|
|
|
|
|
|
|
/* Release the cache */
|
|
|
|
DnsCacheUnlock();
|
|
|
|
}
|