1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/pipe.h>
#include <winpr/file.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
#include <winpr/wlog.h>
#include <winpr/print.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
#define PIPE_BUFFER_SIZE 32
#define PIPE_TIMEOUT_MS 20000 // 20 seconds
static BYTE SERVER_MESSAGE[PIPE_BUFFER_SIZE];
static BYTE CLIENT_MESSAGE[PIPE_BUFFER_SIZE];
static BOOL bClientSuccess = FALSE;
static BOOL bServerSuccess = FALSE;
static HANDLE serverReadyEvent = NULL;
static LPTSTR lpszPipeName = _T("\\\\.\\pipe\\winpr_test_pipe_overlapped");
static DWORD WINAPI named_pipe_client_thread(LPVOID arg)
{
DWORD status = 0;
HANDLE hEvent = NULL;
HANDLE hNamedPipe = NULL;
BYTE* lpReadBuffer = NULL;
BOOL fSuccess = FALSE;
OVERLAPPED overlapped = { 0 };
DWORD nNumberOfBytesToRead = 0;
DWORD nNumberOfBytesToWrite = 0;
DWORD NumberOfBytesTransferred = 0;
WINPR_UNUSED(arg);
status = WaitForSingleObject(serverReadyEvent, PIPE_TIMEOUT_MS);
if (status != WAIT_OBJECT_0)
{
printf("client: failed to wait for server ready event: %" PRIu32 "\n", status);
goto finish;
}
/* 1: initialize overlapped structure */
if (!(hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
printf("client: CreateEvent failure: %" PRIu32 "\n", GetLastError());
goto finish;
}
overlapped.hEvent = hEvent;
/* 2: connect to server named pipe */
hNamedPipe = CreateFile(lpszPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
if (hNamedPipe == INVALID_HANDLE_VALUE)
{
printf("client: Named Pipe CreateFile failure: %" PRIu32 "\n", GetLastError());
goto finish;
}
/* 3: write to named pipe */
nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
NumberOfBytesTransferred = 0;
fSuccess = WriteFile(hNamedPipe, CLIENT_MESSAGE, nNumberOfBytesToWrite, NULL, &overlapped);
if (!fSuccess)
fSuccess = (GetLastError() == ERROR_IO_PENDING);
if (!fSuccess)
{
printf("client: NamedPipe WriteFile failure (initial): %" PRIu32 "\n", GetLastError());
goto finish;
}
status = WaitForSingleObject(hEvent, PIPE_TIMEOUT_MS);
if (status != WAIT_OBJECT_0)
{
printf("client: failed to wait for overlapped event (write): %" PRIu32 "\n", status);
goto finish;
}
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, FALSE);
if (!fSuccess)
{
printf("client: NamedPipe WriteFile failure (final): %" PRIu32 "\n", GetLastError());
goto finish;
}
printf("client: WriteFile transferred %" PRIu32 " bytes:\n", NumberOfBytesTransferred);
/* 4: read from named pipe */
if (!(lpReadBuffer = (BYTE*)calloc(1, PIPE_BUFFER_SIZE)))
{
printf("client: Error allocating read buffer\n");
goto finish;
}
nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
NumberOfBytesTransferred = 0;
fSuccess = ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, NULL, &overlapped);
if (!fSuccess)
fSuccess = (GetLastError() == ERROR_IO_PENDING);
if (!fSuccess)
{
printf("client: NamedPipe ReadFile failure (initial): %" PRIu32 "\n", GetLastError());
goto finish;
}
status = WaitForMultipleObjects(1, &hEvent, FALSE, PIPE_TIMEOUT_MS);
if (status != WAIT_OBJECT_0)
{
printf("client: failed to wait for overlapped event (read): %" PRIu32 "\n", status);
goto finish;
}
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, TRUE);
if (!fSuccess)
{
printf("client: NamedPipe ReadFile failure (final): %" PRIu32 "\n", GetLastError());
goto finish;
}
printf("client: ReadFile transferred %" PRIu32 " bytes:\n", NumberOfBytesTransferred);
winpr_HexDump("pipe.test", WLOG_DEBUG, lpReadBuffer, NumberOfBytesTransferred);
if (NumberOfBytesTransferred != PIPE_BUFFER_SIZE ||
memcmp(lpReadBuffer, SERVER_MESSAGE, PIPE_BUFFER_SIZE))
{
printf("client: received unexpected data from server\n");
goto finish;
}
printf("client: finished successfully\n");
bClientSuccess = TRUE;
finish:
free(lpReadBuffer);
if (hNamedPipe)
CloseHandle(hNamedPipe);
if (hEvent)
CloseHandle(hEvent);
return 0;
}
static DWORD WINAPI named_pipe_server_thread(LPVOID arg)
{
DWORD status = 0;
HANDLE hEvent = NULL;
HANDLE hNamedPipe = NULL;
BYTE* lpReadBuffer = NULL;
OVERLAPPED overlapped = { 0 };
BOOL fSuccess = FALSE;
BOOL fConnected = FALSE;
DWORD nNumberOfBytesToRead = 0;
DWORD nNumberOfBytesToWrite = 0;
DWORD NumberOfBytesTransferred = 0;
WINPR_UNUSED(arg);
/* 1: initialize overlapped structure */
if (!(hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
printf("server: CreateEvent failure: %" PRIu32 "\n", GetLastError());
SetEvent(serverReadyEvent); /* unblock client thread */
goto finish;
}
overlapped.hEvent = hEvent;
/* 2: create named pipe and set ready event */
hNamedPipe =
CreateNamedPipe(lpszPipeName, 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);
if (hNamedPipe == INVALID_HANDLE_VALUE)
{
printf("server: CreateNamedPipe failure: %" PRIu32 "\n", GetLastError());
SetEvent(serverReadyEvent); /* unblock client thread */
goto finish;
}
SetEvent(serverReadyEvent);
/* 3: connect named pipe */
#if 0
/* This sleep will most certainly cause ERROR_PIPE_CONNECTED below */
Sleep(2000);
#endif
fConnected = ConnectNamedPipe(hNamedPipe, &overlapped);
status = GetLastError();
/**
* At this point if fConnected is FALSE, we have to check GetLastError() for:
* ERROR_PIPE_CONNECTED:
* client has already connected before we have called ConnectNamedPipe.
* this is quite common depending on the timings and indicates success
* ERROR_IO_PENDING:
* Since we're using ConnectNamedPipe asynchronously here, the function returns
* immediately and this error code simply indicates that the operation is
* still in progress. Hence we have to wait for the completion event and use
* GetOverlappedResult to query the actual result of the operation (note that
* the lpNumberOfBytesTransferred parameter is undefined/useless for a
* ConnectNamedPipe operation)
*/
if (!fConnected)
fConnected = (status == ERROR_PIPE_CONNECTED);
printf("server: ConnectNamedPipe status: %" PRIu32 "\n", status);
if (!fConnected && status == ERROR_IO_PENDING)
{
DWORD dwDummy = 0;
printf("server: waiting up to %u ms for connection ...\n", PIPE_TIMEOUT_MS);
status = WaitForSingleObject(hEvent, PIPE_TIMEOUT_MS);
if (status == WAIT_OBJECT_0)
fConnected = GetOverlappedResult(hNamedPipe, &overlapped, &dwDummy, FALSE);
else
printf("server: failed to wait for overlapped event (connect): %" PRIu32 "\n", status);
}
if (!fConnected)
{
printf("server: ConnectNamedPipe failed: %" PRIu32 "\n", status);
goto finish;
}
printf("server: named pipe successfully connected\n");
/* 4: read from named pipe */
if (!(lpReadBuffer = (BYTE*)calloc(1, PIPE_BUFFER_SIZE)))
{
printf("server: Error allocating read buffer\n");
goto finish;
}
nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
NumberOfBytesTransferred = 0;
fSuccess = ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, NULL, &overlapped);
if (!fSuccess)
fSuccess = (GetLastError() == ERROR_IO_PENDING);
if (!fSuccess)
{
printf("server: NamedPipe ReadFile failure (initial): %" PRIu32 "\n", GetLastError());
goto finish;
}
status = WaitForSingleObject(hEvent, PIPE_TIMEOUT_MS);
if (status != WAIT_OBJECT_0)
{
printf("server: failed to wait for overlapped event (read): %" PRIu32 "\n", status);
goto finish;
}
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, FALSE);
if (!fSuccess)
{
printf("server: NamedPipe ReadFile failure (final): %" PRIu32 "\n", GetLastError());
goto finish;
}
printf("server: ReadFile transferred %" PRIu32 " bytes:\n", NumberOfBytesTransferred);
winpr_HexDump("pipe.test", WLOG_DEBUG, lpReadBuffer, NumberOfBytesTransferred);
if (NumberOfBytesTransferred != PIPE_BUFFER_SIZE ||
memcmp(lpReadBuffer, CLIENT_MESSAGE, PIPE_BUFFER_SIZE))
{
printf("server: received unexpected data from client\n");
goto finish;
}
/* 5: write to named pipe */
nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
NumberOfBytesTransferred = 0;
fSuccess = WriteFile(hNamedPipe, SERVER_MESSAGE, nNumberOfBytesToWrite, NULL, &overlapped);
if (!fSuccess)
fSuccess = (GetLastError() == ERROR_IO_PENDING);
if (!fSuccess)
{
printf("server: NamedPipe WriteFile failure (initial): %" PRIu32 "\n", GetLastError());
goto finish;
}
status = WaitForSingleObject(hEvent, PIPE_TIMEOUT_MS);
if (status != WAIT_OBJECT_0)
{
printf("server: failed to wait for overlapped event (write): %" PRIu32 "\n", status);
goto finish;
}
fSuccess = GetOverlappedResult(hNamedPipe, &overlapped, &NumberOfBytesTransferred, FALSE);
if (!fSuccess)
{
printf("server: NamedPipe WriteFile failure (final): %" PRIu32 "\n", GetLastError());
goto finish;
}
printf("server: WriteFile transferred %" PRIu32 " bytes:\n", NumberOfBytesTransferred);
// winpr_HexDump("pipe.test", WLOG_DEBUG, lpWriteBuffer, NumberOfBytesTransferred);
bServerSuccess = TRUE;
printf("server: finished successfully\n");
finish:
CloseHandle(hNamedPipe);
CloseHandle(hEvent);
free(lpReadBuffer);
return 0;
}
int TestPipeCreateNamedPipeOverlapped(int argc, char* argv[])
{
HANDLE ClientThread = NULL;
HANDLE ServerThread = NULL;
int result = -1;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
FillMemory(SERVER_MESSAGE, PIPE_BUFFER_SIZE, 0xAA);
FillMemory(CLIENT_MESSAGE, PIPE_BUFFER_SIZE, 0xBB);
if (!(serverReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
printf("CreateEvent failed: %" PRIu32 "\n", GetLastError());
goto out;
}
if (!(ClientThread = CreateThread(NULL, 0, named_pipe_client_thread, NULL, 0, NULL)))
{
printf("CreateThread (client) failed: %" PRIu32 "\n", GetLastError());
goto out;
}
if (!(ServerThread = CreateThread(NULL, 0, named_pipe_server_thread, NULL, 0, NULL)))
{
printf("CreateThread (server) failed: %" PRIu32 "\n", GetLastError());
goto out;
}
if (WAIT_OBJECT_0 != WaitForSingleObject(ClientThread, INFINITE))
{
printf("%s: Failed to wait for client thread: %" PRIu32 "\n", __func__, GetLastError());
goto out;
}
if (WAIT_OBJECT_0 != WaitForSingleObject(ServerThread, INFINITE))
{
printf("%s: Failed to wait for server thread: %" PRIu32 "\n", __func__, GetLastError());
goto out;
}
if (bClientSuccess && bServerSuccess)
result = 0;
out:
if (ClientThread)
CloseHandle(ClientThread);
if (ServerThread)
CloseHandle(ServerThread);
if (serverReadyEvent)
CloseHandle(serverReadyEvent);
#ifndef _WIN32
if (result == 0)
{
printf("%s: Error, this test is currently expected not to succeed on this platform.\n",
__func__);
result = -1;
}
else
{
printf("%s: This test is currently expected to fail on this platform.\n", __func__);
result = 0;
}
#endif
return result;
}
|