source: trunk/src/kernel32/npipe.cpp@ 10132

Last change on this file since 10132 was 10132, checked in by sandervl, 22 years ago

CreatePipe: create unique named pipe

File size: 22.3 KB
Line 
1/* $Id: npipe.cpp,v 1.12 2003-06-02 16:25:18 sandervl Exp $ */
2/*
3 * Win32 Named pipes API
4 *
5 * Copyright 1998 Sander van Leeuwen
6 * Copyright 2000 Przemyslaw Dobrowolski
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 */
10#include <odin.h>
11#include <odinwrap.h>
12#include <os2win.h>
13#include <stdlib.h>
14#include <unicode.h>
15#include <heapstring.h>
16#include <options.h>
17#include <HandleManager.h>
18#include "debugtools.h"
19#include "oslibdos.h"
20
21
22#define DBG_LOCALLOG DBG_npipe
23#include "dbglocal.h"
24
25ODINDEBUGCHANNEL(KERNEL32-NPIPE)
26
27//******************************************************************************
28//******************************************************************************
29BOOL WIN32API PeekNamedPipe(HANDLE hPipe, LPVOID lpvBuffer,
30 DWORD cbBuffer, LPDWORD lpcbRead,
31 LPDWORD lpcbAvail, LPDWORD lpcbMessage)
32{
33 return (HMPeekNamedPipe(hPipe,lpvBuffer,cbBuffer,lpcbRead,lpcbAvail,lpcbMessage));
34}
35//******************************************************************************
36//******************************************************************************
37BOOL WIN32API CreatePipe(PHANDLE phRead, PHANDLE phWrite,
38 LPSECURITY_ATTRIBUTES lpsa, DWORD cbPipe)
39{
40 char szPipeName[64];
41 HANDLE hPipeRead, hPipeWrite;
42
43 // create a unique pipe name
44 for(int i=0;i<16;i++)
45 {
46 sprintf(szPipeName, "\\\\.\\pipe\\Win32.Pipes.%08x.%08x", GetCurrentProcessId(), GetCurrentTime());
47 hPipeRead = ::CreateNamedPipeA(szPipeName, PIPE_ACCESS_DUPLEX,
48 PIPE_TYPE_BYTE | PIPE_WAIT, 1, cbPipe, cbPipe,
49 NMPWAIT_USE_DEFAULT_WAIT, lpsa);
50 if(hPipeRead != INVALID_HANDLE_VALUE) break;
51
52 Sleep(10);
53 }
54
55 if (hPipeRead == INVALID_HANDLE_VALUE) {
56 dprintf(("ERROR: Unable to create named pipe with unique name!! (%s)", szPipeName));
57 return FALSE;
58 }
59
60 ULONG mode = PIPE_NOWAIT|PIPE_READMODE_BYTE;
61
62 //Set pipe in non-blocking mode
63 SetNamedPipeHandleState(hPipeRead, &mode, NULL, NULL);
64
65 //Set pipe in listening mode (so the next CreateFile will succeed)
66 ConnectNamedPipe(hPipeRead, NULL);
67
68 //Put pipe back in blocking mode
69 mode = PIPE_WAIT|PIPE_READMODE_BYTE;
70 SetNamedPipeHandleState(hPipeRead, &mode, NULL, NULL);
71
72 //Create write pipe
73 hPipeWrite = CreateFileA(szPipeName, GENERIC_WRITE, 0, lpsa, OPEN_EXISTING, 0, 0);
74 if (hPipeWrite == INVALID_HANDLE_VALUE)
75 {
76 dprintf(("ERROR: Unable to create write handle for anonymous pipe!!"));
77 CloseHandle(hPipeRead);
78 return FALSE;
79 }
80
81 dprintf(("Created pipe with handles %x and %x", hPipeRead, hPipeWrite));
82 *phRead = hPipeRead;
83 *phWrite = hPipeWrite;
84 return TRUE;
85}
86//******************************************************************************
87//******************************************************************************
88HANDLE WIN32API CreateNamedPipeA(LPCTSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode,
89 DWORD nMaxInstances, DWORD nOutBufferSize,
90 DWORD nInBufferSize, DWORD nDefaultTimeOut,
91 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
92
93{
94 return (HMCreateNamedPipe(lpName,
95 dwOpenMode,
96 dwPipeMode,
97 nMaxInstances,
98 nOutBufferSize,
99 nInBufferSize,
100 nDefaultTimeOut,
101 lpSecurityAttributes));
102
103}
104//******************************************************************************
105//******************************************************************************
106HANDLE WIN32API CreateNamedPipeW(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode,
107 DWORD nMaxInstances, DWORD nOutBufferSize,
108 DWORD nInBufferSize, DWORD nDefaultTimeOut,
109 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
110{
111 char *asciiname;
112 HANDLE hPipe;
113
114 asciiname = UnicodeToAsciiString((LPWSTR)lpName);
115
116 hPipe=HMCreateNamedPipe(asciiname,
117 dwOpenMode,
118 dwPipeMode,
119 nMaxInstances,
120 nOutBufferSize,
121 nInBufferSize,
122 nDefaultTimeOut,
123 lpSecurityAttributes);
124
125 FreeAsciiString(asciiname);
126
127 return(hPipe);
128}
129//******************************************************************************
130//******************************************************************************
131
132
133/*****************************************************************************
134 * Name : BOOL WIN32API ConnectNamedPipe
135 * Purpose : The ConnectNamedPipe function enables a named pipe server process
136 * to wait for a client process to connect to an instance of a
137 * named pipe. A client process connects by calling either the
138 * CreateFile or CallNamedPipe function.
139 * Parameters: HANDLE hNamedPipe handle to named pipe to connect
140 * LPOVERLAPPED lpOverlapped pointer to overlapped structure
141 * Variables :
142 * Result : If the function succeeds, the return value is nonzero.
143 * If the function fails, the return value is zero.
144 * To get extended error information, call GetLastError.
145 * Remark :
146 * Status : NOT FULLY TESTED
147 *
148 * Author : Przemyslaw Dobrowolski [Sun, 2000/01/02 12:48]
149 *****************************************************************************/
150BOOL WIN32API ConnectNamedPipe(HANDLE hNamedPipe, LPOVERLAPPED lpOverlapped)
151{
152 return (HMConnectNamedPipe(hNamedPipe,lpOverlapped));
153}
154
155/*****************************************************************************
156 * Name : BOOL WIN32AOI CallNamedPipeA
157 * Purpose : The CallNamedPipe function connects to a message-type pipe
158 * (and waits if an instance of the pipe is not available),
159 * writes to and reads from the pipe, and then closes the pipe.
160 * Parameters: LPCSTR lpNamedPipeName pointer to pipe name
161 * LPVOID lpInBuffer pointer to write buffer
162 * DWORD nInBufferSize size, in bytes, of write buffer
163 * LPVOID lpOutBuffer pointer to read buffer
164 * DWORD nOutBufferSize size, in bytes, of read buffer
165 * LPDWORD lpBytesRead pointer to number of bytes read
166 * DWORD nTimeOut time-out time, in milliseconds
167 * Variables :
168 * Result : If the function succeeds, the return value is nonzero.
169 * If the function fails, the return value is zero.
170 * To get extended error information, call GetLastError.
171 * Remark : Calling CallNamedPipe is equivalent to calling the CreateFile
172 * (or WaitNamedPipe, if CreateFile cannot open the pipe immediately),
173 * TransactNamedPipe, and CloseHandle functions. CreateFile is called
174 * with an access flag of GENERIC_READ | GENERIC_WRITE, an inherit
175 * handle flag of FALSE, and a share mode of zero (indicating no
176 * sharing of this pipe instance).
177 * If the message written to the pipe by the server process is
178 * longer than nOutBufferSize, CallNamedPipe returns FALSE, and
179 * GetLastError returns ERROR_MORE_DATA. The remainder of the
180 * message is discarded, because CallNamedPipe closes the handle
181 * to the pipe before returning.
182 *
183 * CallNamedPipe fails if the pipe is a byte-type pipe.
184 * Status : NOT FULLY TESTED
185 *
186 * Author : Przemyslaw Dobrowolski [Mon, 2000/01/03 13:32]
187 *****************************************************************************/
188
189BOOL WIN32API CallNamedPipeA(LPCSTR lpNamedPipeName,
190 LPVOID lpInBuffer,
191 DWORD nInBufferSize,
192 LPVOID lpOutBuffer,
193 DWORD nOutBufferSize,
194 LPDWORD lpBytesRead,
195 DWORD nTimeOut)
196{
197 return(OSLibDosCallNamedPipe(lpNamedPipeName,
198 lpInBuffer,
199 nInBufferSize,
200 lpOutBuffer,
201 nOutBufferSize,
202 lpBytesRead,
203 nTimeOut ));
204}
205
206/*****************************************************************************
207 * Name : BOOL WIN32AOI CallNamedPipeW
208 * Purpose : The CallNamedPipe function connects to a message-type pipe
209 * (and waits if an instance of the pipe is not available),
210 * writes to and reads from the pipe, and then closes the pipe.
211 * Parameters: LPCWSTR lpNamedPipeName pointer to pipe name
212 * LPVOID lpInBuffer pointer to write buffer
213 * DWORD nInBufferSize size, in bytes, of write buffer
214 * LPVOID lpOutBuffer pointer to read buffer
215 * DWORD nOutBufferSize size, in bytes, of read buffer
216 * LPDWORD lpBytesRead pointer to number of bytes read
217 * DWORD nTimeOut time-out time, in milliseconds
218 * Variables :
219 * Result : If the function succeeds, the return value is nonzero.
220 * If the function fails, the return value is zero.
221 * To get extended error information, call GetLastError.
222 * Remark : Calling CallNamedPipe is equivalent to calling the CreateFile
223 * (or WaitNamedPipe, if CreateFile cannot open the pipe immediately),
224 * TransactNamedPipe, and CloseHandle functions. CreateFile is called
225 * with an access flag of GENERIC_READ | GENERIC_WRITE, an inherit
226 * handle flag of FALSE, and a share mode of zero (indicating no
227 * sharing of this pipe instance).
228 * If the message written to the pipe by the server process is
229 * longer than nOutBufferSize, CallNamedPipe returns FALSE, and
230 * GetLastError returns ERROR_MORE_DATA. The remainder of the
231 * message is discarded, because CallNamedPipe closes the handle
232 * to the pipe before returning.
233 *
234 * CallNamedPipe fails if the pipe is a byte-type pipe.
235 * Status : NOT FULLY TESTED
236 *
237 * Author : Przemyslaw Dobrowolski [Mon, 2000/01/03 13:33]
238 *****************************************************************************/
239BOOL WIN32API CallNamedPipeW(LPCWSTR lpNamedPipeName,
240 LPVOID lpInBuffer,
241 DWORD nInBufferSize,
242 LPVOID lpOutBuffer,
243 DWORD nOutBufferSize,
244 LPDWORD lpBytesRead,
245 DWORD nTimeOut)
246{
247 char *asciiname;
248 BOOL rc;
249
250 asciiname = UnicodeToAsciiString((LPWSTR)lpNamedPipeName);
251
252 rc=OSLibDosCallNamedPipe(asciiname,
253 lpInBuffer,
254 nInBufferSize,
255 lpOutBuffer,
256 nOutBufferSize,
257 lpBytesRead,
258 nTimeOut );
259
260 FreeAsciiString(asciiname);
261
262 return(rc);
263}
264
265/*****************************************************************************
266 * Name : BOOL WIN32API DisconnectNamedPipe
267 * Purpose : The DisconnectNamedPipe function disconnects the server end
268 * of a named pipe instance from a client process.
269 * Parameters: HANDLE hNamedPipe handle to named pipe
270 * Variables :
271 * Result : If the function succeeds, the return value is nonzero.
272 * If the function fails, the return value is zero
273 * Remark :
274 * Status : NOT FULLY TESTED
275 *
276 * Author : Przemyslaw Dobrowolski [Mon, 2000/01/03 13:34]
277 *****************************************************************************/
278
279BOOL WIN32API DisconnectNamedPipe(HANDLE hNamedPipe)
280{
281 return (HMDisconnectNamedPipe(hNamedPipe));
282}
283
284/*****************************************************************************
285 * Name : BOOL GetNamedPipeHandleStateA
286 * Purpose : The GetNamedPipeHandleStateA function retrieves information about
287 * a specified named pipe. The information returned can vary during
288 * the lifetime of an instance of the named pipe.
289 * Parameters: HANDLE hNamedPipe handle of named pipe
290 * LPDWORD lpState address of flags indicating pipe state
291 * LPDWORD lpCurInstances address of number of current pipe instances
292 * LPDWORD lpMaxCollectionCount address of max. bytes before remote transmission
293 * LPDWORD lpCollectDataTimeout address of max. time before remote transmission
294 * LPTSTR lpUserName address of user name of client process
295 * DWORD nMaxUserNameSize size, in characters, of user name buffer
296 * Variables :
297 * Result : TRUE / FALSE
298 * Remark :
299 * Status : UNTESTED STUB
300 *
301 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
302 *****************************************************************************/
303
304BOOL WIN32API GetNamedPipeHandleStateA(HANDLE hNamedPipe,
305 LPDWORD lpState,
306 LPDWORD lpCurInstances,
307 LPDWORD lpMaxCollectionCount,
308 LPDWORD lpCollectDataTimeout,
309 LPTSTR lpUserName,
310 DWORD nMaxUserNameSize)
311{
312 // Not implemented but waiting to implementation in hmnpipe.cpp
313 return ( HMGetNamedPipeHandleState( hNamedPipe,
314 lpState,
315 lpCurInstances,
316 lpMaxCollectionCount,
317 lpCollectDataTimeout,
318 lpUserName,
319 nMaxUserNameSize));
320}
321
322
323/*****************************************************************************
324 * Name : BOOL GetNamedPipeHandleStateW
325 * Purpose : The GetNamedPipeHandleStateW function retrieves information about
326 * a specified named pipe. The information returned can vary during
327 * the lifetime of an instance of the named pipe.
328 * Parameters: HANDLE hNamedPipe handle of named pipe
329 * LPDWORD lpState address of flags indicating pipe state
330 * LPDWORD lpCurInstances address of number of current pipe instances
331 * LPDWORD lpMaxCollectionCount address of max. bytes before remote transmission
332 * LPDWORD lpCollectDataTimeout address of max. time before remote transmission
333 * LPWSTR lpUserName address of user name of client process
334 * DWORD nMaxUserNameSize size, in characters, of user name buffer
335 * Variables :
336 * Result : TRUE / FALSE
337 * Remark :
338 * Status : UNTESTED STUB
339 *
340 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
341 *****************************************************************************/
342
343BOOL WIN32API GetNamedPipeHandleStateW(HANDLE hNamedPipe,
344 LPDWORD lpState,
345 LPDWORD lpCurInstances,
346 LPDWORD lpMaxCollectionCount,
347 LPDWORD lpCollectDataTimeout,
348 LPWSTR lpUserName,
349 DWORD nMaxUserNameSize)
350{
351 char *asciiname;
352 BOOL rc;
353
354 asciiname = UnicodeToAsciiString((LPWSTR)lpUserName);
355
356 // Not implemented but waiting to implementation in hmnpipe.cpp
357 rc= HMGetNamedPipeHandleState( hNamedPipe,
358 lpState,
359 lpCurInstances,
360 lpMaxCollectionCount,
361 lpCollectDataTimeout,
362 asciiname,
363 nMaxUserNameSize);
364
365
366 FreeAsciiString(asciiname);
367
368 return (rc);
369}
370
371
372/*****************************************************************************
373 * Name : BOOL GetNamedPipeInfo
374 * Purpose : The GetNamedPipeInfo function retrieves information about the specified named pipe.
375 * Parameters: HANDLE hNamedPipe handle of named pipe
376 * LPDWORD lpFlags address of flags indicating type of pipe
377 * LPDWORD lpOutBufferSize address of size, in bytes, of pipe's output buffer
378 * LPDWORD lpInBufferSize address of size, in bytes, of pipe's input buffer
379 * LPDWORD lpMaxInstances address of max. number of pipe instances
380 * Variables :
381 * Result : TRUE / FALSE
382 * Remark :
383 * Status : UNTESTED STUB
384 *
385 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
386 *****************************************************************************/
387
388BOOL WIN32API GetNamedPipeInfo(HANDLE hNamedPipe,
389 LPDWORD lpFlags,
390 LPDWORD lpOutBufferSize,
391 LPDWORD lpInBufferSize,
392 LPDWORD lpMaxInstances)
393{
394 // Not implemented but waiting to implementation in hmnpipe.cpp
395 return ( HMGetNamedPipeInfo( hNamedPipe,
396 lpFlags,
397 lpOutBufferSize,
398 lpInBufferSize,
399 lpMaxInstances));
400
401}
402
403/*****************************************************************************
404 * Name : BOOL SetNamedPipeHandleState
405 * Purpose : The SetNamedPipeHandleState function sets the read mode and the
406 * blocking mode of the specified named pipe. If the specified handle
407 * is to the client end of a named pipe and if the named pipe server
408 * process is on a remote computer, the function can also be used to
409 * control local buffering.
410 * Parameters: HANDLE hNamedPipe handle of named pipe
411 * LPDWORD lpdwMode address of new pipe mode
412 * LPDWORD lpcbMaxCollect address of max. bytes before remote transmission
413 * LPDWORD lpdwCollectDataTimeout address of max. time before remote transmission
414 * Variables :
415 * Result : TRUE / FALSE
416 * Remark :
417 * Status : UNTESTED STUB
418 *
419 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
420 *****************************************************************************/
421
422BOOL WIN32API SetNamedPipeHandleState(HANDLE hNamedPipe,
423 LPDWORD lpdwMode,
424 LPDWORD lpcbMaxCollect,
425 LPDWORD lpdwCollectDataTimeout)
426{
427 // Not implemented but waiting to implementation in hmnpipe.cpp
428 return ( HMSetNamedPipeHandleState( hNamedPipe,
429 lpdwMode,
430 lpcbMaxCollect,
431 lpdwCollectDataTimeout));
432}
433
434/*****************************************************************************
435 * Name : BOOL TransactNamedPipe
436 * Purpose : The TransactNamedPipe function combines into a single network
437 * operation the functions that write a message to and read a
438 * message from the specified named pipe.
439 * Parameters: HANDLE hNamedPipe handle of named pipe
440 * LPVOID lpvWriteBuf address of write buffer
441 * DWORD cbWriteBuf size of the write buffer, in bytes
442 * LPVOID lpvReadBuf address of read buffer
443 * DWORD cbReadBuf size of read buffer, in bytes
444 * LPDWORD lpcbRead address of variable for bytes actually read
445 * LPOVERLAPPED lpo address of overlapped structure
446 * Variables :
447 * Result : TRUE / FALSE
448 * Remark :
449 * Status : NOT FULLY TESTED (YET!)
450 *
451 * Author : Przemyslaw Dobrowolski [Mon, 2000/01/03 08:48]
452 *****************************************************************************/
453
454BOOL WIN32API TransactNamedPipe(HANDLE hNamedPipe,
455 LPVOID lpvWriteBuf,
456 DWORD cbWriteBuf,
457 LPVOID lpvReadBuf,
458 DWORD cbReadBuf,
459 LPDWORD lpcbRead,
460 LPOVERLAPPED lpo)
461{
462 return(HMTransactNamedPipe( hNamedPipe,
463 lpvWriteBuf,
464 cbWriteBuf,
465 lpvReadBuf,
466 cbReadBuf,
467 lpcbRead,
468 lpo));
469}
470
471/*****************************************************************************
472 * Name : BOOL WaitNamedPipeA
473 * Purpose : The WaitNamedPipe function waits until either a time-out interval
474 * elapses or an instance of the specified named pipe is available
475 * to be connected to (that is, the pipe's server process has a
476 * pending ConnectNamedPipe operation on the pipe).
477 * Parameters: LPCTSTR lpszNamedPipeName
478 * DWORD dwTimeout
479 * Variables :
480 * Result : TRUE / FALSE
481 * Remark :
482 * Status : UNTESTED (YET)
483 *
484 * Author : Przemyslaw Dobrowolski [Mon, 2000/01/03 13:52]
485 *****************************************************************************/
486
487BOOL WIN32API WaitNamedPipeA(LPCSTR lpszNamedPipeName, DWORD dwTimeout)
488{
489 return(OSLibDosWaitNamedPipe(lpszNamedPipeName, dwTimeout));
490}
491
492
493/*****************************************************************************
494 * Name : BOOL WaitNamedPipeW
495 * Purpose : The WaitNamedPipe function waits until either a time-out interval
496 * elapses or an instance of the specified named pipe is available
497 * to be connected to (that is, the pipe's server process has a
498 * pending ConnectNamedPipe operation on the pipe).
499 * Parameters: LPCWSTR lpszNamedPipeName
500 * DWORD dwTimeout
501 * Variables :
502 * Result : TRUE / FALSE
503 * Remark :
504 * Status : UNTESTED (YET)
505 *
506 * Author : Przemyslaw Dobrowolski [Mon, 2000/01/03 13:44]
507 *****************************************************************************/
508
509BOOL WIN32API WaitNamedPipeW(LPCWSTR lpszNamedPipeName, DWORD dwTimeout)
510{
511 char *asciiname;
512 DWORD rc;
513
514 asciiname = UnicodeToAsciiString((LPWSTR)lpszNamedPipeName);
515
516 rc=OSLibDosWaitNamedPipe(asciiname, dwTimeout);
517
518 FreeAsciiString(asciiname);
519
520 return(rc);
521}
Note: See TracBrowser for help on using the repository browser.