Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:55

0001 //
0002 // impl/connect_pipe.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 // Copyright (c) 2021 Klemens D. Morgenstern
0007 //                    (klemens dot morgenstern at gmx dot net)
0008 //
0009 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0010 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0011 //
0012 
0013 #ifndef BOOST_ASIO_IMPL_CONNECT_PIPE_IPP
0014 #define BOOST_ASIO_IMPL_CONNECT_PIPE_IPP
0015 
0016 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 # pragma once
0018 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0019 
0020 #include <boost/asio/detail/config.hpp>
0021 
0022 #if defined(BOOST_ASIO_HAS_PIPE)
0023 
0024 #include <boost/asio/connect_pipe.hpp>
0025 
0026 #if defined(BOOST_ASIO_HAS_IOCP)
0027 # include <cstdio>
0028 # if _WIN32_WINNT >= 0x601
0029 #  include <bcrypt.h>
0030 #  if !defined(BOOST_ASIO_NO_DEFAULT_LINKED_LIBS)
0031 #   if defined(_MSC_VER)
0032 #    pragma comment(lib, "bcrypt.lib")
0033 #   endif // defined(_MSC_VER)
0034 #  endif // !defined(BOOST_ASIO_NO_DEFAULT_LINKED_LIBS)
0035 # endif // _WIN32_WINNT >= 0x601
0036 #else // defined(BOOST_ASIO_HAS_IOCP)
0037 # include <boost/asio/detail/descriptor_ops.hpp>
0038 #endif // defined(BOOST_ASIO_HAS_IOCP)
0039 
0040 #include <boost/asio/detail/push_options.hpp>
0041 
0042 namespace boost {
0043 namespace asio {
0044 namespace detail {
0045 
0046 void create_pipe(native_pipe_handle p[2], boost::system::error_code& ec)
0047 {
0048 #if defined(BOOST_ASIO_HAS_IOCP)
0049   using namespace std; // For sprintf and memcmp.
0050 
0051   static long counter1 = 0;
0052   static long counter2 = 0;
0053 
0054   long n1 = ::InterlockedIncrement(&counter1);
0055   long n2 = (static_cast<unsigned long>(n1) % 0x10000000) == 0
0056     ? ::InterlockedIncrement(&counter2)
0057     : ::InterlockedExchangeAdd(&counter2, 0);
0058 
0059   wchar_t pipe_name[128];
0060 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
0061   swprintf_s(
0062 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
0063   _snwprintf(
0064 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
0065       pipe_name, 128,
0066       L"\\\\.\\pipe\\asio-A0812896-741A-484D-AF23-BE51BF620E22-%u-%ld-%ld",
0067       static_cast<unsigned int>(::GetCurrentProcessId()), n1, n2);
0068 
0069   p[0] = ::CreateNamedPipeW(pipe_name,
0070       PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
0071       0, 1, 8192, 8192, 0, 0);
0072 
0073   if (p[0] == INVALID_HANDLE_VALUE)
0074   {
0075     DWORD last_error = ::GetLastError();
0076     ec.assign(last_error, boost::asio::error::get_system_category());
0077     return;
0078   }
0079 
0080   p[1] = ::CreateFileW(pipe_name, GENERIC_WRITE, 0,
0081     0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
0082 
0083   if (p[1] == INVALID_HANDLE_VALUE)
0084   {
0085     DWORD last_error = ::GetLastError();
0086     ::CloseHandle(p[0]);
0087     ec.assign(last_error, boost::asio::error::get_system_category());
0088     return;
0089   }
0090 
0091 # if _WIN32_WINNT >= 0x601
0092   unsigned char nonce[16];
0093   if (::BCryptGenRandom(0, nonce, sizeof(nonce),
0094         BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0)
0095   {
0096     ec = boost::asio::error::connection_aborted;
0097     ::CloseHandle(p[0]);
0098     ::CloseHandle(p[1]);
0099     return;
0100   }
0101 
0102   DWORD bytes_written = 0;
0103   BOOL ok = ::WriteFile(p[1], nonce, sizeof(nonce), &bytes_written, 0);
0104   if (!ok || bytes_written != sizeof(nonce))
0105   {
0106     ec = boost::asio::error::connection_aborted;
0107     ::CloseHandle(p[0]);
0108     ::CloseHandle(p[1]);
0109     return;
0110   }
0111 
0112   unsigned char nonce_check[sizeof(nonce)];
0113   DWORD bytes_read = 0;
0114   ok = ::ReadFile(p[0], nonce_check, sizeof(nonce), &bytes_read, 0);
0115   if (!ok || bytes_read != sizeof(nonce)
0116       || memcmp(nonce, nonce_check, sizeof(nonce)) != 0)
0117   {
0118     ec = boost::asio::error::connection_aborted;
0119     ::CloseHandle(p[0]);
0120     ::CloseHandle(p[1]);
0121     return;
0122   }
0123 #endif // _WIN32_WINNT >= 0x601
0124 
0125   boost::asio::error::clear(ec);
0126 #else // defined(BOOST_ASIO_HAS_IOCP)
0127   int result = ::pipe(p);
0128   detail::descriptor_ops::get_last_error(ec, result != 0);
0129 #endif // defined(BOOST_ASIO_HAS_IOCP)
0130 }
0131 
0132 void close_pipe(native_pipe_handle p)
0133 {
0134 #if defined(BOOST_ASIO_HAS_IOCP)
0135   ::CloseHandle(p);
0136 #else // defined(BOOST_ASIO_HAS_IOCP)
0137   boost::system::error_code ignored_ec;
0138   detail::descriptor_ops::state_type state = 0;
0139   detail::descriptor_ops::close(p, state, ignored_ec);
0140 #endif // defined(BOOST_ASIO_HAS_IOCP)
0141 }
0142 
0143 } // namespace detail
0144 } // namespace asio
0145 } // namespace boost
0146 
0147 #include <boost/asio/detail/pop_options.hpp>
0148 
0149 #endif // defined(BOOST_ASIO_HAS_PIPE)
0150 
0151 #endif // BOOST_ASIO_IMPL_CONNECT_PIPE_IPP