2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32 Kernel Library
4 * FILE: dll/win32/kernel32/client/file/npipe.c
5 * PURPOSE: Named Pipe Functions
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 * Ariadne ( ariadne@xs4all.nl)
10 /* INCLUDES *******************************************************************/
15 DEBUG_CHANNEL(kernel32file
);
17 /* GLOBALS ********************************************************************/
21 /* FUNCTIONS ******************************************************************/
28 NpGetUserNamep(HANDLE hNamedPipe
,
30 DWORD nMaxUserNameSize
)
36 BOOL (WINAPI
*pRevertToSelf
)(void);
37 BOOL (WINAPI
*pGetUserNameW
)(LPWSTR lpBuffer
, LPDWORD lpnSize
);
38 BOOL (WINAPI
*pImpersonateNamedPipeClient
)(HANDLE hNamedPipe
);
40 /* Open advapi, we'll funcs from it */
41 hAdvapi
= LoadLibraryW(L
"advapi32.dll");
47 /* Import the three required functions */
48 pRevertToSelf
= GetProcAddress(hAdvapi
, "RevertToSelf");
49 pGetUserNameW
= GetProcAddress(hAdvapi
, "GetUserNameW");
50 pImpersonateNamedPipeClient
= GetProcAddress(hAdvapi
, "ImpersonateNamedPipeClient");
51 /* If any couldn't be found, fail */
52 if (pRevertToSelf
== NULL
|| pGetUserNameW
== NULL
|| pImpersonateNamedPipeClient
== NULL
)
58 /* Now, open the thread token for impersonation */
59 Status
= NtOpenThreadToken(NtCurrentThread(), TOKEN_IMPERSONATE
, TRUE
, &hToken
);
60 /* Try to impersonate the pipe client */
61 if (pImpersonateNamedPipeClient(hNamedPipe
))
65 /* It worked, get the user name */
66 lpnSize
= nMaxUserNameSize
;
67 Ret
= pGetUserNameW(lpUserName
, &lpnSize
);
68 /* Failed to get the thread token? Revert to self */
69 if (!NT_SUCCESS(Status
))
77 /* Restore the thread token */
78 Status
= NtSetInformationThread(NtCurrentThread(), ThreadImpersonationToken
,
79 &hToken
, sizeof(HANDLE
));
80 /* We cannot fail closing the thread token! */
81 if (!CloseHandle(hToken
))
86 /* Set last error if it failed */
87 if (!NT_SUCCESS(Status
))
89 BaseSetLastNTError(Status
);
94 /* If opening the thread token succeed, close it */
95 if (NT_SUCCESS(Status
))
97 /* We cannot fail closing it! */
98 if (!CloseHandle(hToken
))
107 FreeLibrary(hAdvapi
);
117 CreatePipe(PHANDLE hReadPipe
,
119 LPSECURITY_ATTRIBUTES lpPipeAttributes
,
123 UNICODE_STRING PipeName
;
124 OBJECT_ATTRIBUTES ObjectAttributes
;
125 IO_STATUS_BLOCK StatusBlock
;
126 LARGE_INTEGER DefaultTimeout
;
128 HANDLE ReadPipeHandle
;
129 HANDLE WritePipeHandle
;
132 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
134 /* Set the timeout to 120 seconds */
135 DefaultTimeout
.QuadPart
= -1200000000;
137 /* Use default buffer size if desired */
138 if (!nSize
) nSize
= 0x1000;
140 /* Increase the Pipe ID */
141 PipeId
= InterlockedIncrement(&ProcessPipeId
);
143 /* Create the pipe name */
145 L
"\\Device\\NamedPipe\\Win32Pipes.%08x.%08x",
146 NtCurrentTeb()->ClientId
.UniqueProcess
,
148 RtlInitUnicodeString(&PipeName
, Buffer
);
150 /* Always use case insensitive */
151 Attributes
= OBJ_CASE_INSENSITIVE
;
153 /* Check if we got attributes */
154 if (lpPipeAttributes
)
156 /* Use the attributes' SD instead */
157 SecurityDescriptor
= lpPipeAttributes
->lpSecurityDescriptor
;
159 /* Set OBJ_INHERIT if requested */
160 if (lpPipeAttributes
->bInheritHandle
) Attributes
|= OBJ_INHERIT
;
163 /* Initialize the attributes */
164 InitializeObjectAttributes(&ObjectAttributes
,
170 /* Create the named pipe */
171 Status
= NtCreateNamedPipeFile(&ReadPipeHandle
,
172 GENERIC_READ
|FILE_WRITE_ATTRIBUTES
| SYNCHRONIZE
,
175 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
177 FILE_SYNCHRONOUS_IO_NONALERT
,
178 FILE_PIPE_BYTE_STREAM_TYPE
,
179 FILE_PIPE_BYTE_STREAM_MODE
,
180 FILE_PIPE_QUEUE_OPERATION
,
185 if (!NT_SUCCESS(Status
))
187 /* Convert error and fail */
188 WARN("Status: %lx\n", Status
);
189 BaseSetLastNTError(Status
);
193 /* Now try opening it for write access */
194 Status
= NtOpenFile(&WritePipeHandle
,
199 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
200 if (!NT_SUCCESS(Status
))
202 /* Convert error and fail */
203 WARN("Status: %lx\n", Status
);
204 NtClose(ReadPipeHandle
);
205 BaseSetLastNTError(Status
);
209 /* Return both handles */
210 *hReadPipe
= ReadPipeHandle
;
211 *hWritePipe
= WritePipeHandle
;
220 CreateNamedPipeA(LPCSTR lpName
,
224 DWORD nOutBufferSize
,
226 DWORD nDefaultTimeOut
,
227 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
229 /* Call the W(ide) function */
230 ConvertWin32AnsiChangeApiToUnicodeApi(CreateNamedPipe
,
238 lpSecurityAttributes
);
246 CreateNamedPipeW(LPCWSTR lpName
,
250 DWORD nOutBufferSize
,
252 DWORD nDefaultTimeOut
,
253 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
255 UNICODE_STRING NamedPipeName
;
258 OBJECT_ATTRIBUTES ObjectAttributes
;
260 ACCESS_MASK DesiredAccess
;
261 ULONG CreateOptions
= 0;
262 ULONG WriteModeMessage
;
263 ULONG ReadModeMessage
;
265 IO_STATUS_BLOCK Iosb
;
266 ULONG ShareAccess
= 0, Attributes
;
267 LARGE_INTEGER DefaultTimeOut
;
268 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
270 /* Check for valid instances */
271 if (nMaxInstances
== 0 || nMaxInstances
> PIPE_UNLIMITED_INSTANCES
)
274 SetLastError(ERROR_INVALID_PARAMETER
);
275 return INVALID_HANDLE_VALUE
;
278 /* Convert to NT syntax */
279 if (nMaxInstances
== PIPE_UNLIMITED_INSTANCES
)
282 /* Convert the name */
283 Result
= RtlDosPathNameToNtPathName_U(lpName
,
289 /* Conversion failed */
290 SetLastError(ERROR_PATH_NOT_FOUND
);
291 return INVALID_HANDLE_VALUE
;
294 TRACE("Pipe name: %wZ\n", &NamedPipeName
);
295 TRACE("Pipe name: %S\n", NamedPipeName
.Buffer
);
297 /* Always case insensitive, check if we got extra attributes */
298 Attributes
= OBJ_CASE_INSENSITIVE
;
299 if(lpSecurityAttributes
)
301 /* We did; get the security descriptor */
302 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
304 /* And check if this is pipe's handle will beinheritable */
305 if (lpSecurityAttributes
->bInheritHandle
)
306 Attributes
|= OBJ_INHERIT
;
309 /* Now we can initialize the object attributes */
310 InitializeObjectAttributes(&ObjectAttributes
,
316 /* Setup the default Desired Access */
317 DesiredAccess
= SYNCHRONIZE
| (dwOpenMode
& (WRITE_DAC
|
319 ACCESS_SYSTEM_SECURITY
));
321 /* Convert to NT Create Flags */
322 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
)
324 CreateOptions
|= FILE_WRITE_THROUGH
;
327 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
))
329 CreateOptions
|= FILE_SYNCHRONOUS_IO_NONALERT
;
332 /* Handle all open modes */
333 if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
)
335 ShareAccess
|= FILE_SHARE_READ
;
336 DesiredAccess
|= GENERIC_WRITE
;
339 if (dwOpenMode
& PIPE_ACCESS_INBOUND
)
341 ShareAccess
|= FILE_SHARE_WRITE
;
342 DesiredAccess
|= GENERIC_READ
;
345 /* Handle the type flags */
346 if (dwPipeMode
& PIPE_TYPE_MESSAGE
)
348 WriteModeMessage
= FILE_PIPE_MESSAGE_TYPE
;
352 WriteModeMessage
= FILE_PIPE_BYTE_STREAM_TYPE
;
355 /* Handle the mode flags */
356 if (dwPipeMode
& PIPE_READMODE_MESSAGE
)
358 ReadModeMessage
= FILE_PIPE_MESSAGE_MODE
;
362 ReadModeMessage
= FILE_PIPE_BYTE_STREAM_MODE
;
365 /* Handle the blocking mode */
366 if (dwPipeMode
& PIPE_NOWAIT
)
368 NonBlocking
= FILE_PIPE_COMPLETE_OPERATION
;
372 NonBlocking
= FILE_PIPE_QUEUE_OPERATION
;
375 /* Check if we have a timeout */
378 /* Convert the time to NT format */
379 DefaultTimeOut
.QuadPart
= nDefaultTimeOut
* -10000LL;
383 /* Use default timeout of 50 ms */
384 DefaultTimeOut
.QuadPart
= -500000;
387 /* Now create the pipe */
388 Status
= NtCreateNamedPipeFile(&PipeHandle
,
403 /* Normalize special error codes */
404 if ((Status
== STATUS_INVALID_DEVICE_REQUEST
) ||
405 (Status
== STATUS_NOT_SUPPORTED
))
407 Status
= STATUS_OBJECT_NAME_INVALID
;
411 RtlFreeHeap(RtlGetProcessHeap(),
413 NamedPipeName
.Buffer
);
416 if (!NT_SUCCESS(Status
))
418 /* Failed to create it */
419 WARN("NtCreateNamedPipe failed (Status %x)!\n", Status
);
420 BaseSetLastNTError (Status
);
421 return INVALID_HANDLE_VALUE
;
424 /* Return the handle */
433 WaitNamedPipeA(LPCSTR lpNamedPipeName
,
437 UNICODE_STRING NameU
;
439 /* Convert the name to Unicode */
440 if (Basep8BitStringToDynamicUnicodeString(&NameU
, lpNamedPipeName
))
442 /* Call the Unicode API */
443 r
= WaitNamedPipeW(NameU
.Buffer
, nTimeOut
);
445 /* Free the Unicode string */
446 RtlFreeUnicodeString(&NameU
);
458 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
461 UNICODE_STRING NamedPipeName
, NewName
, DevicePath
, PipePrefix
;
466 OBJECT_ATTRIBUTES ObjectAttributes
;
469 IO_STATUS_BLOCK IoStatusBlock
;
470 ULONG WaitPipeInfoSize
;
471 PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo
;
473 /* Start by making a unicode string of the name */
474 TRACE("Sent path: %S\n", lpNamedPipeName
);
475 if (!RtlCreateUnicodeString(&NamedPipeName
, lpNamedPipeName
))
477 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
480 NameLength
= NamedPipeName
.Length
/ sizeof(WCHAR
);
482 /* All slashes must become backslashes */
483 for (i
= 0; i
< NameLength
; i
++)
485 /* Check and convert */
486 if (NamedPipeName
.Buffer
[i
] == L
'/') NamedPipeName
.Buffer
[i
] = L
'\\';
489 /* Find the path type of the name we were given */
490 NewName
= NamedPipeName
;
491 Type
= RtlDetermineDosPathNameType_U(lpNamedPipeName
);
493 /* Check if this was a device path, ie : "\\.\pipe\name" */
494 if (Type
== RtlPathTypeLocalDevice
)
496 /* Make sure it's a valid prefix */
497 RtlInitUnicodeString(&PipePrefix
, L
"\\\\.\\pipe\\");
498 if (!RtlPrefixUnicodeString(&PipePrefix
, &NewName
, TRUE
))
500 /* The name is invalid */
501 WARN("Invalid name!\n");
502 RtlFreeUnicodeString(&NamedPipeName
);
503 BaseSetLastNTError(STATUS_OBJECT_PATH_SYNTAX_BAD
);
508 NewName
.Buffer
+= PipePrefix
.Length
/ sizeof(WCHAR
);
509 NewName
.Length
-= PipePrefix
.Length
;
510 NewName
.MaximumLength
-= PipePrefix
.Length
;
512 /* Initialize the Dos Devices name */
513 TRACE("NewName: %wZ\n", &NewName
);
514 RtlInitUnicodeString(&DevicePath
, L
"\\DosDevices\\pipe\\");
516 else if (Type
== RtlPathTypeUncAbsolute
)
518 /* The path is \\server\\pipe\name; find the pipename itself */
519 p
= &NewName
.Buffer
[2];
521 /* First loop to get past the server name */
524 /* Check if this is a backslash */
525 if (*p
== L
'\\') break;
531 /* Now make sure the full name contains "pipe\" */
532 if ((*p
) && !(_wcsnicmp(p
+ 1, L
"pipe\\", sizeof("pipe\\") - sizeof(ANSI_NULL
))))
534 /* Get to the pipe name itself now */
535 p
+= sizeof("pipe\\") - sizeof(ANSI_NULL
);
539 /* The name is invalid */
540 WARN("Invalid name!\n");
541 RtlFreeUnicodeString(&NamedPipeName
);
542 BaseSetLastNTError(STATUS_OBJECT_PATH_SYNTAX_BAD
);
546 /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
550 WARN("Invalid path type\n");
551 RtlFreeUnicodeString(&NamedPipeName
);
552 BaseSetLastNTError(STATUS_OBJECT_PATH_SYNTAX_BAD
);
556 /* Now calculate the total length of the structure and allocate it */
557 WaitPipeInfoSize
= FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[0]) +
559 WaitPipeInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize
);
560 if (WaitPipeInfo
== NULL
)
562 RtlFreeUnicodeString(&NamedPipeName
);
563 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
567 /* Initialize the object attributes */
568 TRACE("Opening: %wZ\n", &DevicePath
);
569 InitializeObjectAttributes(&ObjectAttributes
,
571 OBJ_CASE_INSENSITIVE
,
576 Status
= NtOpenFile(&FileHandle
,
577 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
580 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
581 FILE_SYNCHRONOUS_IO_NONALERT
);
582 if (!NT_SUCCESS(Status
))
584 /* Fail; couldn't open */
585 WARN("Status: %lx\n", Status
);
586 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
587 RtlFreeUnicodeString(&NamedPipeName
);
588 BaseSetLastNTError(Status
);
592 /* Check what timeout we got */
593 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
595 /* Don't use a timeout */
596 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
600 /* Check if we should wait forever */
601 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
604 WaitPipeInfo
->Timeout
.LowPart
= 0;
605 WaitPipeInfo
->Timeout
.HighPart
= 0x80000000;
609 /* Convert to NT format */
610 WaitPipeInfo
->Timeout
.QuadPart
= nTimeOut
* -10000LL;
613 /* In both cases, we do have a timeout */
614 WaitPipeInfo
->TimeoutSpecified
= TRUE
;
617 /* Set the length and copy the name */
618 WaitPipeInfo
->NameLength
= NewName
.Length
;
619 RtlCopyMemory(WaitPipeInfo
->Name
, NewName
.Buffer
, NewName
.Length
);
621 /* Get rid of the full name */
622 RtlFreeUnicodeString(&NamedPipeName
);
624 /* Let NPFS know of our request */
625 Status
= NtFsControlFile(FileHandle
,
636 /* Free our pipe info data and close the handle */
637 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
640 /* Check the status */
641 if (!NT_SUCCESS(Status
))
643 /* Failure to wait on the pipe */
644 WARN("Status: %lx\n", Status
);
645 BaseSetLastNTError(Status
);
658 ConnectNamedPipe(IN HANDLE hNamedPipe
,
659 IN LPOVERLAPPED lpOverlapped
)
663 if (lpOverlapped
!= NULL
)
667 lpOverlapped
->Internal
= STATUS_PENDING
;
668 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
670 Status
= NtFsControlFile(hNamedPipe
,
671 lpOverlapped
->hEvent
,
674 (PIO_STATUS_BLOCK
)lpOverlapped
,
681 /* return FALSE in case of failure and pending operations! */
682 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
684 BaseSetLastNTError(Status
);
690 IO_STATUS_BLOCK Iosb
;
692 Status
= NtFsControlFile(hNamedPipe
,
703 /* wait in case operation is pending */
704 if (Status
== STATUS_PENDING
)
706 Status
= NtWaitForSingleObject(hNamedPipe
,
709 if (NT_SUCCESS(Status
))
711 Status
= Iosb
.Status
;
715 if (!NT_SUCCESS(Status
))
717 BaseSetLastNTError(Status
);
731 SetNamedPipeHandleState(HANDLE hNamedPipe
,
733 LPDWORD lpMaxCollectionCount
,
734 LPDWORD lpCollectDataTimeout
)
736 IO_STATUS_BLOCK Iosb
;
739 /* Check if the Mode is being changed */
742 FILE_PIPE_INFORMATION Settings
;
744 /* Set the Completion Mode */
745 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
746 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
748 /* Set the Read Mode */
749 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
750 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
752 /* Send the changes to the Driver */
753 Status
= NtSetInformationFile(hNamedPipe
,
756 sizeof(FILE_PIPE_INFORMATION
),
757 FilePipeInformation
);
758 if (!NT_SUCCESS(Status
))
760 BaseSetLastNTError(Status
);
765 /* Check if the Collection count or Timeout are being changed */
766 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
768 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
770 /* Setting one without the other would delete it, so we read old one */
771 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
773 Status
= NtQueryInformationFile(hNamedPipe
,
776 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
777 FilePipeRemoteInformation
);
778 if (!NT_SUCCESS(Status
))
780 BaseSetLastNTError(Status
);
785 /* Now set the new settings */
786 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
787 *lpMaxCollectionCount
:
788 RemoteSettings
.MaximumCollectionCount
;
789 if (lpCollectDataTimeout
)
791 /* Convert it to Quad */
792 RemoteSettings
.CollectDataTime
.QuadPart
= *lpCollectDataTimeout
* -10000LL;
795 /* Tell the driver to change them */
796 Status
= NtSetInformationFile(hNamedPipe
,
799 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
800 FilePipeRemoteInformation
);
801 if (!NT_SUCCESS(Status
))
803 BaseSetLastNTError(Status
);
817 CallNamedPipeA(LPCSTR lpNamedPipeName
,
821 DWORD nOutBufferSize
,
825 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
826 ANSI_STRING AnsiPipe
;
828 /* Initialize the string as ANSI_STRING and convert to Unicode */
829 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
830 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
832 /* Call the Unicode function */
833 return CallNamedPipeW(PipeName
->Buffer
,
848 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
852 DWORD nOutBufferSize
,
863 /* Try creating it */
864 hPipe
= CreateFileW(lpNamedPipeName
,
865 GENERIC_READ
| GENERIC_WRITE
,
866 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
869 FILE_ATTRIBUTE_NORMAL
,
872 /* Success, break out */
873 if (hPipe
!= INVALID_HANDLE_VALUE
)
876 /* Already tried twice, give up */
881 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
883 /* Get ready to try again */
887 /* Set the pipe mode */
888 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
889 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
892 /* Couldn't change state, fail */
897 /* Do the transact */
898 bError
= TransactNamedPipe(hPipe
,
906 /* Close the handle */
918 DisconnectNamedPipe(HANDLE hNamedPipe
)
920 IO_STATUS_BLOCK Iosb
;
923 /* Send the FSCTL to the driver */
924 Status
= NtFsControlFile(hNamedPipe
,
929 FSCTL_PIPE_DISCONNECT
,
934 if (Status
== STATUS_PENDING
)
936 /* Wait on NPFS to finish and get updated status */
937 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
938 if (NT_SUCCESS(Status
))
939 Status
= Iosb
.Status
;
942 /* Check for error */
943 if (!NT_SUCCESS(Status
))
946 BaseSetLastNTError(Status
);
959 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
961 LPDWORD lpCurInstances
,
962 LPDWORD lpMaxCollectionCount
,
963 LPDWORD lpCollectDataTimeout
,
965 DWORD nMaxUserNameSize
)
967 IO_STATUS_BLOCK StatusBlock
;
972 FILE_PIPE_INFORMATION PipeInfo
;
974 Status
= NtQueryInformationFile(hNamedPipe
,
977 sizeof(FILE_PIPE_INFORMATION
),
978 FilePipeInformation
);
979 if (!NT_SUCCESS(Status
))
981 BaseSetLastNTError(Status
);
985 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
986 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
989 if(lpCurInstances
!= NULL
)
991 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
993 Status
= NtQueryInformationFile(hNamedPipe
,
996 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
997 FilePipeLocalInformation
);
998 if (!NT_SUCCESS(Status
))
1000 BaseSetLastNTError(Status
);
1004 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
1007 if (lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
1009 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
1011 Status
= NtQueryInformationFile(hNamedPipe
,
1014 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
1015 FilePipeRemoteInformation
);
1016 if (!NT_SUCCESS(Status
))
1018 BaseSetLastNTError(Status
);
1022 if (lpMaxCollectionCount
!= NULL
)
1024 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
1027 if (lpCollectDataTimeout
!= NULL
)
1029 LARGE_INTEGER CollectDataTime
;
1031 /* Convert time and return it */
1032 RemoteInfo
.CollectDataTime
.QuadPart
*= -1;
1033 CollectDataTime
= RtlExtendedLargeIntegerDivide(RemoteInfo
.CollectDataTime
, 10000, NULL
);
1034 /* In case of overflow, just return MAX - 1 */
1035 if (CollectDataTime
.HighPart
!= 0)
1037 *lpCollectDataTimeout
= -2;
1041 *lpCollectDataTimeout
= CollectDataTime
.LowPart
;
1046 if (lpUserName
!= NULL
)
1048 return NpGetUserNamep(hNamedPipe
, lpUserName
, nMaxUserNameSize
);
1060 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
1062 LPDWORD lpCurInstances
,
1063 LPDWORD lpMaxCollectionCount
,
1064 LPDWORD lpCollectDataTimeout
,
1066 DWORD nMaxUserNameSize
)
1068 UNICODE_STRING UserNameW
= { 0, 0, NULL
};
1069 ANSI_STRING UserNameA
;
1072 if(lpUserName
!= NULL
)
1074 UserNameW
.MaximumLength
= (USHORT
)nMaxUserNameSize
* sizeof(WCHAR
);
1075 UserNameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW
.MaximumLength
);
1076 if (UserNameW
.Buffer
== NULL
)
1078 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1082 UserNameA
.Buffer
= lpUserName
;
1083 UserNameA
.Length
= 0;
1084 UserNameA
.MaximumLength
= (USHORT
)nMaxUserNameSize
;
1087 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
1090 lpMaxCollectionCount
,
1091 lpCollectDataTimeout
,
1094 if (Ret
&& lpUserName
!= NULL
)
1098 RtlInitUnicodeString(&UserNameW
, UserNameW
.Buffer
);
1099 Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
1100 if (!NT_SUCCESS(Status
))
1102 BaseSetLastNTError(Status
);
1107 if (UserNameW
.Buffer
!= NULL
)
1109 RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW
.Buffer
);
1121 GetNamedPipeInfo(HANDLE hNamedPipe
,
1123 LPDWORD lpOutBufferSize
,
1124 LPDWORD lpInBufferSize
,
1125 LPDWORD lpMaxInstances
)
1127 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
1128 IO_STATUS_BLOCK StatusBlock
;
1131 Status
= NtQueryInformationFile(hNamedPipe
,
1133 &PipeLocalInformation
,
1134 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
1135 FilePipeLocalInformation
);
1136 if (!NT_SUCCESS(Status
))
1138 BaseSetLastNTError(Status
);
1142 if (lpFlags
!= NULL
)
1144 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
1145 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1148 if (lpOutBufferSize
!= NULL
)
1149 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
1151 if (lpInBufferSize
!= NULL
)
1152 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
1154 if (lpMaxInstances
!= NULL
)
1156 if (PipeLocalInformation
.MaximumInstances
>= 255)
1157 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
1159 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
1171 PeekNamedPipe(HANDLE hNamedPipe
,
1174 LPDWORD lpBytesRead
,
1175 LPDWORD lpTotalBytesAvail
,
1176 LPDWORD lpBytesLeftThisMessage
)
1178 PFILE_PIPE_PEEK_BUFFER Buffer
;
1179 IO_STATUS_BLOCK Iosb
;
1184 /* Calculate the buffer space that we'll need and allocate it */
1185 BufferSize
= FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[nBufferSize
]);
1186 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize
);
1189 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1193 /* Tell the driver to seek */
1194 Status
= NtFsControlFile(hNamedPipe
,
1204 if (Status
== STATUS_PENDING
)
1206 /* Wait for npfs to be done, and update the status */
1207 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
1208 if (NT_SUCCESS(Status
))
1209 Status
= Iosb
.Status
;
1212 /* Overflow is success for us */
1213 if (Status
== STATUS_BUFFER_OVERFLOW
)
1214 Status
= STATUS_SUCCESS
;
1217 if (!NT_SUCCESS(Status
))
1219 /* Free the buffer and return failure */
1220 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1221 BaseSetLastNTError(Status
);
1225 /* Check if caller requested bytes available */
1226 if (lpTotalBytesAvail
)
1228 /* Return bytes available */
1229 *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1232 /* Calculate the bytes returned, minus our structure overhead */
1233 BytesRead
= (ULONG
)(Iosb
.Information
-
1234 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1235 ASSERT(BytesRead
<= nBufferSize
);
1237 /* Check if caller requested bytes read */
1240 /* Return the bytes read */
1241 *lpBytesRead
= BytesRead
;
1244 /* Check if caller requested bytes left */
1245 if (lpBytesLeftThisMessage
)
1247 /* Calculate total minus what we returned and our structure overhead */
1248 *lpBytesLeftThisMessage
= Buffer
->MessageLength
- BytesRead
;
1251 /* Check if the caller wanted to see the actual data */
1254 /* Give him what he wants */
1255 RtlCopyMemory(lpBuffer
,
1260 /* Free the buffer */
1261 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1272 TransactNamedPipe(IN HANDLE hNamedPipe
,
1273 IN LPVOID lpInBuffer
,
1274 IN DWORD nInBufferSize
,
1275 OUT LPVOID lpOutBuffer
,
1276 IN DWORD nOutBufferSize
,
1277 OUT LPDWORD lpBytesRead OPTIONAL
,
1278 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1282 if (lpBytesRead
!= NULL
)
1287 if (lpOverlapped
!= NULL
)
1291 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1292 lpOverlapped
->Internal
= STATUS_PENDING
;
1294 Status
= NtFsControlFile(hNamedPipe
,
1295 lpOverlapped
->hEvent
,
1298 (PIO_STATUS_BLOCK
)lpOverlapped
,
1299 FSCTL_PIPE_TRANSCEIVE
,
1304 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1306 BaseSetLastNTError(Status
);
1310 if (lpBytesRead
!= NULL
)
1312 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1317 IO_STATUS_BLOCK Iosb
;
1319 Status
= NtFsControlFile(hNamedPipe
,
1324 FSCTL_PIPE_TRANSCEIVE
,
1329 if (Status
== STATUS_PENDING
)
1331 Status
= NtWaitForSingleObject(hNamedPipe
,
1334 if (NT_SUCCESS(Status
))
1335 Status
= Iosb
.Status
;
1338 if (NT_SUCCESS(Status
))
1340 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1341 check that case either and crashes (only after the operation
1343 *lpBytesRead
= Iosb
.Information
;
1347 BaseSetLastNTError(Status
);