xref: /reactos/base/services/dnsrslvr/rpcserver.c (revision 1734f297)
1 /*
2  * PROJECT:     ReactOS DNS Resolver
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/services/dnsrslvr/rpcserver.c
5  * PURPOSE:     RPC server interface
6  * COPYRIGHT:   Copyright 2016 Christoph von Wittich
7  */
8 
9 #include "precomp.h"
10 
11 #define NDEBUG
12 #include <debug.h>
13 
14 DWORD
15 WINAPI
16 RpcThreadRoutine(LPVOID lpParameter)
17 {
18     RPC_STATUS Status;
19 
20     Status = RpcServerUseProtseqEpW(L"ncalrpc", 20, L"DNSResolver", NULL);
21     if (Status != RPC_S_OK)
22     {
23         DPRINT("RpcServerUseProtseqEpW() failed (Status %lx)\n", Status);
24         return 0;
25     }
26 
27     Status = RpcServerRegisterIf(DnsResolver_v2_0_s_ifspec, NULL, NULL);
28     if (Status != RPC_S_OK)
29     {
30         DPRINT("RpcServerRegisterIf() failed (Status %lx)\n", Status);
31         return 0;
32     }
33 
34     Status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, 0);
35     if (Status != RPC_S_OK)
36     {
37         DPRINT("RpcServerListen() failed (Status %lx)\n", Status);
38     }
39 
40     DPRINT("RpcServerListen finished\n");
41     return 0;
42 }
43 
44 
45 /* Function: 0x00 */
46 DWORD
47 __stdcall
48 CRrReadCache(
49     _In_ DNSRSLVR_HANDLE pwszServerName,
50     _Out_ DNS_CACHE_ENTRY **ppCacheEntries)
51 {
52     DPRINT("CRrReadCache(%S %p)\n",
53            pwszServerName, ppCacheEntries);
54 
55     return DnsIntCacheGetEntries(ppCacheEntries);
56 }
57 
58 
59 /* Function: 0x04 */
60 DWORD
61 __stdcall
62 R_ResolverFlushCache(
63     _In_ DNSRSLVR_HANDLE pszServerName)
64 {
65     DPRINT("R_ResolverFlushCache(%S)\n",
66            pszServerName);
67 
68     return DnsIntCacheFlush(CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES);
69 }
70 
71 
72 /* Function: 0x05 */
73 DWORD
74 __stdcall
75 R_ResolverFlushCacheEntry(
76     _In_ DNSRSLVR_HANDLE pszServerName,
77     _In_ LPCWSTR pszName,
78     _In_ WORD wType)
79 {
80     DPRINT("R_ResolverFlushCacheEntry(%S %S %x)\n",
81            pszServerName, pszName, wType);
82 
83     if (pszName == NULL)
84         return ERROR_INVALID_PARAMETER;
85 
86     return DnsIntFlushCacheEntry(pszName, wType);
87 }
88 
89 
90 /* Function: 0x07 */
91 DWORD
92 __stdcall
93 R_ResolverQuery(
94     _In_ DNSRSLVR_HANDLE pszServerName,
95     _In_ LPCWSTR pszName,
96     _In_ WORD wType,
97     _In_ DWORD dwFlags,
98     _Inout_ DWORD *dwRecords,
99     _Out_ DNS_RECORDW **ppResultRecords)
100 {
101     PDNS_RECORDW Record;
102     DNS_STATUS Status = ERROR_SUCCESS;
103 
104     DPRINT("R_ResolverQuery(%S %S %x %lx %p %p)\n",
105            pszServerName, pszName, wType, dwFlags, dwRecords, ppResultRecords);
106 
107     if (pszName == NULL || wType == 0 || ppResultRecords == NULL)
108         return ERROR_INVALID_PARAMETER;
109 
110     if ((dwFlags & DNS_QUERY_WIRE_ONLY) != 0 && (dwFlags & DNS_QUERY_NO_WIRE_QUERY) != 0)
111         return ERROR_INVALID_PARAMETER;
112 
113     if (dwFlags & DNS_QUERY_WIRE_ONLY)
114     {
115         DPRINT("DNS query!\n");
116         Status = Query_Main(pszName,
117                             wType,
118                             dwFlags,
119                             ppResultRecords);
120     }
121     else if (dwFlags & DNS_QUERY_NO_WIRE_QUERY)
122     {
123         DPRINT("DNS cache query!\n");
124         Status = DnsIntCacheGetEntryByName(pszName,
125                                            wType,
126                                            dwFlags,
127                                            ppResultRecords);
128     }
129     else
130     {
131         DPRINT("DNS cache query!\n");
132         Status = DnsIntCacheGetEntryByName(pszName,
133                                            wType,
134                                            dwFlags,
135                                            ppResultRecords);
136         if (Status == DNS_INFO_NO_RECORDS)
137         {
138             DPRINT("DNS query!\n");
139             Status = Query_Main(pszName,
140                                 wType,
141                                 dwFlags,
142                                 ppResultRecords);
143             if (Status == ERROR_SUCCESS)
144             {
145                 DPRINT("DNS query successful!\n");
146                 DnsIntCacheAddEntry(*ppResultRecords, FALSE);
147             }
148         }
149     }
150 
151     if (dwRecords)
152     {
153         *dwRecords = 0;
154 
155         if (Status == ERROR_SUCCESS)
156         {
157             Record = *ppResultRecords;
158             while (Record)
159             {
160                 DPRINT("Record: %S\n", Record->pName);
161                 (*dwRecords)++;
162                 Record = Record->pNext;
163             }
164         }
165     }
166 
167     DPRINT("R_ResolverQuery result %ld %ld\n", Status, *dwRecords);
168 
169     return Status;
170 }
171 
172 void __RPC_FAR * __RPC_USER midl_user_allocate(SIZE_T len)
173 {
174     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
175 }
176 
177 void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
178 {
179     HeapFree(GetProcessHeap(), 0, ptr);
180 }
181 
182 void __RPC_USER DNSRSLVR_RPC_HANDLE_rundown(DNSRSLVR_HANDLE hClientHandle)
183 {
184 }
185