Skip to content

Instantly share code, notes, and snippets.

@JoshEngebretson
Created July 22, 2015 23:07
Show Gist options
  • Save JoshEngebretson/b421a88a4585d18445ec to your computer and use it in GitHub Desktop.
Save JoshEngebretson/b421a88a4585d18445ec to your computer and use it in GitHub Desktop.
static const wchar_t kPipePrefix[] = L"\\\\.\\pipe\\";
static const int kPipeBufferSz = 4 * 1024;
static LONG g_pipe_seq = 0;
bool checkIntegritySupport()
{
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
return osvi.dwMajorVersion > 5;
}
HANDLE PipePair::OpenPipeServer(const wchar_t* name, bool low_integrity) {
SECURITY_ATTRIBUTES sa = { 0 };
SECURITY_ATTRIBUTES *psa = 0;
static const bool is_integrity_supported = false;// checkIntegritySupport();
if (is_integrity_supported && low_integrity) {
psa = &sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
TEXT("S:(ML;;NWNR;;;LW)"), SDDL_REVISION_1, &sa.lpSecurityDescriptor, NULL))
return INVALID_HANDLE_VALUE;
}
IPCWString pipename(kPipePrefix);
pipename.append(name);
return ::CreateNamedPipeW(pipename.c_str(), PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, kPipeBufferSz, kPipeBufferSz, 200, psa);
}
HANDLE PipePair::OpenPipeClient(const wchar_t* name, bool inherit, bool impersonate) {
IPCWString pipename(kPipePrefix);
pipename.append(name);
SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, inherit ? TRUE : FALSE };
for (;;) {
DWORD attributes = impersonate ? 0 : SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION;
HANDLE pipe = ::CreateFileW(pipename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, &sa,
OPEN_EXISTING, attributes, NULL);
if (INVALID_HANDLE_VALUE == pipe) {
if (ERROR_PIPE_BUSY != ::GetLastError()) {
return pipe;
}
// wait and retry.
::Sleep(25);
}
else {
// success.
return pipe;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment