xref: /reactos/base/services/dnsrslvr/rpcserver.c (revision 2eb96f0c)
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 pwszServerName)
64 {
65     DPRINT("R_ResolverFlushCache(%S)\n",
66            pwszServerName);
67 
68     return DnsIntCacheFlush(CACHE_FLUSH_NON_HOSTS_FILE_ENTRIES);
69 }
70 
71 
72 /* Function: 0x07 */
73 DWORD
74 __stdcall
75 R_ResolverQuery(
76     _In_ DNSRSLVR_HANDLE pszServerName,
77     _In_ LPCWSTR pszName,
78     _In_ WORD wType,
79     _In_ DWORD dwFlags,
80     _Inout_ DWORD *dwRecords,
81     _Out_ DNS_RECORDW **ppResultRecords)
82 {
83     PDNS_RECORDW Record;
84     DNS_STATUS Status = ERROR_SUCCESS;
85 
86     DPRINT("R_ResolverQuery(%S %S %x %lx %p %p)\n",
87            pszServerName, pszName, wType, dwFlags, dwRecords, ppResultRecords);
88 
89     if (pszName == NULL || wType == 0 || ppResultRecords == NULL)
90         return ERROR_INVALID_PARAMETER;
91 
92     if ((dwFlags & DNS_QUERY_WIRE_ONLY) != 0 && (dwFlags & DNS_QUERY_NO_WIRE_QUERY) != 0)
93         return ERROR_INVALID_PARAMETER;
94 
95     if (dwFlags & DNS_QUERY_WIRE_ONLY)
96     {
97         DPRINT("DNS query!\n");
98         Status = Query_Main(pszName,
99                             wType,
100                             dwFlags,
101                             ppResultRecords);
102     }
103     else if (dwFlags & DNS_QUERY_NO_WIRE_QUERY)
104     {
105         DPRINT("DNS cache query!\n");
106         Status = DnsIntCacheGetEntryByName(pszName,
107                                            wType,
108                                            dwFlags,
109                                            ppResultRecords);
110     }
111     else
112     {
113         DPRINT("DNS cache query!\n");
114         Status = DnsIntCacheGetEntryByName(pszName,
115                                            wType,
116                                            dwFlags,
117                                            ppResultRecords);
118         if (Status == DNS_INFO_NO_RECORDS)
119         {
120             DPRINT("DNS query!\n");
121             Status = Query_Main(pszName,
122                                 wType,
123                                 dwFlags,
124                                 ppResultRecords);
125             if (Status == ERROR_SUCCESS)
126             {
127                 DPRINT("DNS query successful!\n");
128                 DnsIntCacheAddEntry(*ppResultRecords, FALSE);
129             }
130         }
131     }
132 
133     if (dwRecords)
134     {
135         *dwRecords = 0;
136 
137         if (Status == ERROR_SUCCESS)
138         {
139             Record = *ppResultRecords;
140             while (Record)
141             {
142                 DPRINT("Record: %S\n", Record->pName);
143                 (*dwRecords)++;
144                 Record = Record->pNext;
145             }
146         }
147     }
148 
149     DPRINT("R_ResolverQuery result %ld %ld\n", Status, *dwRecords);
150 
151     return Status;
152 }
153 
154 void __RPC_FAR * __RPC_USER midl_user_allocate(SIZE_T len)
155 {
156     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
157 }
158 
159 void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
160 {
161     HeapFree(GetProcessHeap(), 0, ptr);
162 }
163 
164 void __RPC_USER DNSRSLVR_RPC_HANDLE_rundown(DNSRSLVR_HANDLE hClientHandle)
165 {
166 }
167