1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <windows.h>
20 #undef ERROR_RETRY
21 #include <gatekeeper/gatekeeper_messages.h>
22 
23 #include <string>
24 
25 #include "common/libs/security/gatekeeper_channel.h"
26 
27 namespace cuttlefish {
28 
29 /*
30  * Interface for communication channels that synchronously communicate
31  * Gatekeeper IPC/RPC calls. Sends messages over a named pipe.
32  */
33 class GatekeeperWindowsChannel : public GatekeeperChannel {
34  public:
35   ~GatekeeperWindowsChannel();
36 
37   static std::unique_ptr<GatekeeperWindowsChannel> Create(HANDLE pipe_handle);
38   bool SendRequest(uint32_t command,
39                    const gatekeeper::GateKeeperMessage& message) override;
40   bool SendResponse(uint32_t command,
41                     const gatekeeper::GateKeeperMessage& message) override;
42   transport::ManagedMessage ReceiveMessage() override;
43 
44  protected:
45   GatekeeperWindowsChannel() = default;
46 
47  private:
48   bool WaitForConnection(HANDLE pipe_handle);
49   bool SendMessage(uint32_t command, bool response,
50                    const gatekeeper::GateKeeperMessage& message);
51   bool ReadFromPipe(LPVOID buffer, DWORD size);
52 
53   // Handle to the (asynchronous) named pipe.
54   HANDLE pipe_handle_ = NULL;
55   // OVERLAPPED struct for the named pipe. It contains an event object and is
56   // used to wait for asynchronous pipe operations.
57   OVERLAPPED pipe_overlapped_ = {};
58 };
59 
60 }  // namespace cuttlefish