OpenVPN 3 Core Library
Loading...
Searching...
No Matches
handlecomm.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <windows.h>
15
18
20
21OPENVPN_EXCEPTION(handle_comm);
22
23// Duplicate a local handle into the address space of a
24// remote process and return as a hex string that can be
25// communicated across a process boundary.
26inline std::string send_handle(const HANDLE handle,
27 const HANDLE remote_process)
28{
29 HANDLE remote_handle;
30 if (!::DuplicateHandle(GetCurrentProcess(),
31 handle,
32 remote_process,
33 &remote_handle,
34 0,
35 FALSE,
36 DUPLICATE_SAME_ACCESS))
37 {
38 const Win::LastError err;
39 OPENVPN_THROW(handle_comm, "send_handle: DuplicateHandle failed: " << err.message());
40 }
41 return BufHex::render(remote_handle);
42}
43
44// Duplicate a remote handle (specified as a hex string) into
45// the address space of the local process.
46inline HANDLE receive_handle(const std::string &remote_handle_hex,
47 const HANDLE remote_process)
48{
49 const HANDLE remote_handle = BufHex::parse<HANDLE>(remote_handle_hex, "receive_handle");
50 HANDLE local_handle;
51 if (!::DuplicateHandle(remote_process,
52 remote_handle,
53 GetCurrentProcess(),
54 &local_handle,
55 0,
56 FALSE,
57 DUPLICATE_SAME_ACCESS))
58 {
59 const Win::LastError err;
60 OPENVPN_THROW(handle_comm, "receive_handle: DuplicateHandle failed: " << err.message());
61 }
62 return local_handle;
63}
64
65} // namespace openvpn::Win::HandleComm
#define OPENVPN_EXCEPTION(C)
#define OPENVPN_THROW(exc, stuff)
std::string render(const T obj)
Definition bufhex.hpp:24
HANDLE receive_handle(const std::string &remote_handle_hex, const HANDLE remote_process)
std::string send_handle(const HANDLE handle, const HANDLE remote_process)