2013-07-23 23:03:08 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/pipe.h>
|
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/winpr.h>
|
2014-08-18 19:22:22 +04:00
|
|
|
#include <winpr/wlog.h>
|
2013-07-23 23:03:08 +04:00
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
#define PIPE_BUFFER_SIZE 32
|
|
|
|
|
|
|
|
static HANDLE ReadyEvent;
|
|
|
|
|
|
|
|
static LPTSTR lpszPipeName = _T("\\\\.\\pipe\\winpr_test_pipe_overlapped");
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void* named_pipe_client_thread(void* arg)
|
2013-07-23 23:03:08 +04:00
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
HANDLE hEvent;
|
|
|
|
HANDLE hNamedPipe;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* lpReadBuffer;
|
|
|
|
BYTE* lpWriteBuffer;
|
2013-07-23 23:03:08 +04:00
|
|
|
BOOL fSuccess = FALSE;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
DWORD nNumberOfBytesToRead;
|
|
|
|
DWORD nNumberOfBytesToWrite;
|
|
|
|
DWORD NumberOfBytesTransferred;
|
|
|
|
WaitForSingleObject(ReadyEvent, INFINITE);
|
|
|
|
hNamedPipe = CreateFile(lpszPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
|
|
|
|
|
|
|
if (!hNamedPipe)
|
|
|
|
{
|
|
|
|
printf("Named Pipe CreateFile failure: NULL handle\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hNamedPipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("Named Pipe CreateFile failure: INVALID_HANDLE_VALUE\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
lpReadBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
|
|
|
lpWriteBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!lpReadBuffer || !lpWriteBuffer)
|
|
|
|
{
|
|
|
|
printf("Error allocating memory\n");
|
2015-04-07 15:38:09 +03:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
2015-04-03 17:21:01 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-23 23:03:08 +04:00
|
|
|
ZeroMemory(&overlapped, sizeof(OVERLAPPED));
|
2015-04-28 18:00:41 +03:00
|
|
|
|
|
|
|
if (!(hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
printf("CreateEvent failure: (%d)\n", GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-23 23:03:08 +04:00
|
|
|
overlapped.hEvent = hEvent;
|
|
|
|
nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
|
|
|
|
FillMemory(lpWriteBuffer, PIPE_BUFFER_SIZE, 0x59);
|
|
|
|
fSuccess = WriteFile(hNamedPipe, lpWriteBuffer, nNumberOfBytesToWrite, NULL, &overlapped);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
fSuccess = (GetLastError() == ERROR_IO_PENDING);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
{
|
|
|
|
printf("Client NamedPipe WriteFile failure: %d\n", GetLastError());
|
2013-08-30 16:19:50 +04:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
2013-07-23 23:03:08 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(1, &hEvent, FALSE, INFINITE);
|
|
|
|
NumberOfBytesTransferred = 0;
|
|
|
|
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, TRUE);
|
|
|
|
printf("Client GetOverlappedResult: fSuccess: %d NumberOfBytesTransferred: %d\n", fSuccess, NumberOfBytesTransferred);
|
|
|
|
nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
|
|
|
|
ZeroMemory(lpReadBuffer, PIPE_BUFFER_SIZE);
|
|
|
|
fSuccess = ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, NULL, &overlapped);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
fSuccess = (GetLastError() == ERROR_IO_PENDING);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
{
|
|
|
|
printf("Client NamedPipe ReadFile failure: %d\n", GetLastError());
|
2013-08-30 16:19:50 +04:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
2013-07-23 23:03:08 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(1, &hEvent, FALSE, INFINITE);
|
|
|
|
NumberOfBytesTransferred = 0;
|
|
|
|
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, TRUE);
|
|
|
|
printf("Client GetOverlappedResult: fSuccess: %d NumberOfBytesTransferred: %d\n", fSuccess, NumberOfBytesTransferred);
|
|
|
|
printf("Client ReadFile (%d):\n", NumberOfBytesTransferred);
|
2014-08-18 19:22:22 +04:00
|
|
|
winpr_HexDump("pipe.test", WLOG_DEBUG, lpReadBuffer, NumberOfBytesTransferred);
|
2013-07-23 23:03:08 +04:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void* named_pipe_server_thread(void* arg)
|
2013-07-23 23:03:08 +04:00
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
HANDLE hEvent;
|
|
|
|
HANDLE hNamedPipe;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* lpReadBuffer;
|
|
|
|
BYTE* lpWriteBuffer;
|
2013-07-23 23:03:08 +04:00
|
|
|
OVERLAPPED overlapped;
|
|
|
|
BOOL fSuccess = FALSE;
|
|
|
|
BOOL fConnected = FALSE;
|
|
|
|
DWORD nNumberOfBytesToRead;
|
|
|
|
DWORD nNumberOfBytesToWrite;
|
|
|
|
DWORD NumberOfBytesTransferred;
|
|
|
|
hNamedPipe = CreateNamedPipe(lpszPipeName,
|
2014-08-18 19:22:22 +04:00
|
|
|
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
|
|
|
|
PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 0, NULL);
|
2013-07-23 23:03:08 +04:00
|
|
|
|
|
|
|
if (!hNamedPipe)
|
|
|
|
{
|
|
|
|
printf("CreateNamedPipe failure: NULL handle\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hNamedPipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("CreateNamedPipe failure: INVALID_HANDLE_VALUE (%d)\n", GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetEvent(ReadyEvent);
|
|
|
|
ZeroMemory(&overlapped, sizeof(OVERLAPPED));
|
2015-04-28 18:00:41 +03:00
|
|
|
|
|
|
|
if (!(hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
printf("CreateEvent failure: (%d)\n", GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-23 23:03:08 +04:00
|
|
|
overlapped.hEvent = hEvent;
|
|
|
|
fConnected = ConnectNamedPipe(hNamedPipe, &overlapped);
|
|
|
|
printf("ConnectNamedPipe status: %d\n", GetLastError());
|
|
|
|
|
|
|
|
if (!fConnected)
|
|
|
|
fConnected = (GetLastError() == ERROR_IO_PENDING);
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(1, &hEvent, FALSE, INFINITE);
|
|
|
|
NumberOfBytesTransferred = 0;
|
|
|
|
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, TRUE);
|
|
|
|
printf("Server GetOverlappedResult: fSuccess: %d NumberOfBytesTransferred: %d\n", fSuccess, NumberOfBytesTransferred);
|
|
|
|
|
|
|
|
if (!fConnected)
|
|
|
|
{
|
|
|
|
printf("ConnectNamedPipe failure: %d\n", GetLastError());
|
2013-08-30 16:19:50 +04:00
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
2013-07-23 23:03:08 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-14 01:55:09 +03:00
|
|
|
lpReadBuffer = (BYTE*) calloc(1, PIPE_BUFFER_SIZE);
|
2014-08-18 21:34:47 +04:00
|
|
|
lpWriteBuffer = (BYTE*) malloc(PIPE_BUFFER_SIZE);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!lpReadBuffer || !lpWriteBuffer)
|
|
|
|
{
|
|
|
|
printf("Error allocating memory\n");
|
2015-04-07 15:38:09 +03:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
2015-04-03 17:21:01 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-23 23:03:08 +04:00
|
|
|
nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
|
|
|
|
fSuccess = ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, NULL, &overlapped);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
fSuccess = (GetLastError() == ERROR_IO_PENDING);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
{
|
|
|
|
printf("Server NamedPipe ReadFile failure: %d\n", GetLastError());
|
2013-08-30 16:19:50 +04:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
2013-07-23 23:03:08 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(1, &hEvent, FALSE, INFINITE);
|
|
|
|
NumberOfBytesTransferred = 0;
|
|
|
|
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, TRUE);
|
|
|
|
printf("Server GetOverlappedResult: fSuccess: %d NumberOfBytesTransferred: %d\n", fSuccess, NumberOfBytesTransferred);
|
|
|
|
printf("Server ReadFile (%d):\n", NumberOfBytesTransferred);
|
2014-08-18 19:22:22 +04:00
|
|
|
winpr_HexDump("pipe.test", WLOG_DEBUG, lpReadBuffer, NumberOfBytesTransferred);
|
2013-07-23 23:03:08 +04:00
|
|
|
nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
|
|
|
|
FillMemory(lpWriteBuffer, PIPE_BUFFER_SIZE, 0x45);
|
|
|
|
fSuccess = WriteFile(hNamedPipe, lpWriteBuffer, nNumberOfBytesToWrite, NULL, &overlapped);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
fSuccess = (GetLastError() == ERROR_IO_PENDING);
|
|
|
|
|
|
|
|
if (!fSuccess)
|
|
|
|
{
|
|
|
|
printf("Server NamedPipe WriteFile failure: %d\n", GetLastError());
|
2013-08-30 16:19:50 +04:00
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
2013-07-23 23:03:08 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(1, &hEvent, FALSE, INFINITE);
|
|
|
|
NumberOfBytesTransferred = 0;
|
|
|
|
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, TRUE);
|
|
|
|
printf("Server GetOverlappedResult: fSuccess: %d NumberOfBytesTransferred: %d\n", fSuccess, NumberOfBytesTransferred);
|
|
|
|
free(lpReadBuffer);
|
|
|
|
free(lpWriteBuffer);
|
|
|
|
CloseHandle(hNamedPipe);
|
|
|
|
CloseHandle(hEvent);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int TestPipeCreateNamedPipeOverlapped(int argc, char* argv[])
|
2013-07-23 23:03:08 +04:00
|
|
|
{
|
|
|
|
HANDLE ClientThread;
|
|
|
|
HANDLE ServerThread;
|
2015-04-28 18:00:41 +03:00
|
|
|
|
|
|
|
if (!(ReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
printf("CreateEvent failed: %d\n", GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(ClientThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) named_pipe_client_thread, NULL, 0, NULL)))
|
|
|
|
{
|
|
|
|
printf("CreateThread (client) failed: %d\n", GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) named_pipe_server_thread, NULL, 0, NULL)))
|
|
|
|
{
|
|
|
|
printf("CreateThread (server) failed: %d\n", GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-23 23:03:08 +04:00
|
|
|
WaitForSingleObject(ClientThread, INFINITE);
|
|
|
|
WaitForSingleObject(ServerThread, INFINITE);
|
2015-04-28 18:00:41 +03:00
|
|
|
|
|
|
|
/* FIXME: Since this function always returns 0 this test is very much useless */
|
2013-07-23 23:03:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|