2012-10-08 00:13:30 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Pipe Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2013-07-23 02:20:34 +04:00
|
|
|
#include <winpr/path.h>
|
2012-10-08 04:25:50 +04:00
|
|
|
#include <winpr/handle.h>
|
2012-10-08 00:13:30 +04:00
|
|
|
|
|
|
|
#include <winpr/pipe.h>
|
|
|
|
|
2012-10-08 04:25:50 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-10-08 00:13:30 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "pipe.h"
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
/*
|
|
|
|
* Unnamed pipe
|
|
|
|
*/
|
|
|
|
|
2012-10-08 00:13:30 +04:00
|
|
|
BOOL CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
|
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
int pipe_fd[2];
|
2013-05-17 01:32:58 +04:00
|
|
|
WINPR_PIPE* pReadPipe;
|
|
|
|
WINPR_PIPE* pWritePipe;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
|
|
|
pipe_fd[0] = -1;
|
|
|
|
pipe_fd[1] = -1;
|
|
|
|
|
|
|
|
if (pipe(pipe_fd) < 0)
|
|
|
|
{
|
|
|
|
printf("CreatePipe: failed to create pipe\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
pReadPipe = (WINPR_PIPE*) malloc(sizeof(WINPR_PIPE));
|
|
|
|
pWritePipe = (WINPR_PIPE*) malloc(sizeof(WINPR_PIPE));
|
|
|
|
|
|
|
|
if (!pReadPipe || !pWritePipe)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
pReadPipe->fd = pipe_fd[0];
|
|
|
|
pWritePipe->fd = pipe_fd[1];
|
|
|
|
|
2013-05-17 02:27:26 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(pReadPipe, HANDLE_TYPE_ANONYMOUS_PIPE);
|
|
|
|
*((ULONG_PTR*) hReadPipe) = (ULONG_PTR) pReadPipe;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2013-05-17 02:27:26 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(pWritePipe, HANDLE_TYPE_ANONYMOUS_PIPE);
|
|
|
|
*((ULONG_PTR*) hWritePipe) = (ULONG_PTR) pWritePipe;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2012-10-08 00:13:30 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
/**
|
|
|
|
* Named pipe
|
|
|
|
*/
|
|
|
|
|
|
|
|
HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances,
|
|
|
|
DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
|
|
|
{
|
2013-07-23 06:53:44 +04:00
|
|
|
int status;
|
2013-07-23 02:20:34 +04:00
|
|
|
HANDLE hNamedPipe;
|
|
|
|
char* lpPipePath;
|
2013-07-23 06:53:44 +04:00
|
|
|
unsigned long flags;
|
|
|
|
struct sockaddr_un s;
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
|
|
|
|
if (!lpName)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) malloc(sizeof(WINPR_NAMED_PIPE));
|
|
|
|
hNamedPipe = (HANDLE) pNamedPipe;
|
|
|
|
|
|
|
|
WINPR_HANDLE_SET_TYPE(pNamedPipe, HANDLE_TYPE_NAMED_PIPE);
|
|
|
|
|
|
|
|
pNamedPipe->name = _strdup(lpName);
|
|
|
|
pNamedPipe->dwOpenMode = dwOpenMode;
|
|
|
|
pNamedPipe->dwPipeMode = dwPipeMode;
|
|
|
|
pNamedPipe->nMaxInstances = nMaxInstances;
|
|
|
|
pNamedPipe->nOutBufferSize = nOutBufferSize;
|
|
|
|
pNamedPipe->nInBufferSize = nInBufferSize;
|
|
|
|
pNamedPipe->nDefaultTimeOut = nDefaultTimeOut;
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
pNamedPipe->lpFileName = GetNamedPipeNameWithoutPrefixA(lpName);
|
|
|
|
pNamedPipe->lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpName);
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
lpPipePath = GetNamedPipeUnixDomainSocketBaseFilePathA();
|
|
|
|
|
|
|
|
if (!PathFileExistsA(lpPipePath))
|
|
|
|
CreateDirectoryA(lpPipePath, 0);
|
2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
free(lpPipePath);
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
pNamedPipe->clientfd = -1;
|
|
|
|
pNamedPipe->serverfd = socket(PF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (0)
|
|
|
|
{
|
|
|
|
flags = fcntl(pNamedPipe->serverfd, F_GETFL);
|
|
|
|
flags = flags | O_NONBLOCK;
|
|
|
|
fcntl(pNamedPipe->serverfd, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(&s, sizeof(struct sockaddr_un));
|
|
|
|
s.sun_family = AF_UNIX;
|
|
|
|
strcpy(s.sun_path, pNamedPipe->lpFilePath);
|
|
|
|
unlink(s.sun_path);
|
|
|
|
|
|
|
|
status = bind(pNamedPipe->serverfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un));
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
status = listen(pNamedPipe->serverfd, 2);
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
2013-07-30 18:57:54 +04:00
|
|
|
UnixChangeFileMode(pNamedPipe->lpFilePath, 0xFFFF);
|
2013-07-23 06:53:44 +04:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
return hNamedPipe;
|
2013-07-23 00:30:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateNamedPipeW(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances,
|
|
|
|
DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ConnectNamedPipe(HANDLE hNamedPipe, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
2013-07-23 06:53:44 +04:00
|
|
|
int status;
|
|
|
|
socklen_t length;
|
|
|
|
struct sockaddr_un s;
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
if (!hNamedPipe)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
length = sizeof(struct sockaddr_un);
|
|
|
|
ZeroMemory(&s, sizeof(struct sockaddr_un));
|
|
|
|
|
|
|
|
status = accept(pNamedPipe->serverfd, (struct sockaddr*) &s, &length);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
pNamedPipe->clientfd = status;
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DisconnectNamedPipe(HANDLE hNamedPipe)
|
|
|
|
{
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
|
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
|
|
|
|
2013-07-23 07:17:08 +04:00
|
|
|
if (pNamedPipe->clientfd != -1)
|
|
|
|
{
|
|
|
|
close(pNamedPipe->clientfd);
|
|
|
|
pNamedPipe->clientfd = -1;
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL PeekNamedPipe(HANDLE hNamedPipe, LPVOID lpBuffer, DWORD nBufferSize,
|
|
|
|
LPDWORD lpBytesRead, LPDWORD lpTotalBytesAvail, LPDWORD lpBytesLeftThisMessage)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TransactNamedPipe(HANDLE hNamedPipe, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer,
|
|
|
|
DWORD nOutBufferSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WaitNamedPipeA(LPCSTR lpNamedPipeName, DWORD nTimeOut)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WaitNamedPipeW(LPCWSTR lpNamedPipeName, DWORD nTimeOut)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetNamedPipeHandleState(HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
|
|
|
|
{
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
|
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
|
|
|
|
|
|
|
if (lpMode)
|
|
|
|
pNamedPipe->dwPipeMode = *lpMode;
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ImpersonateNamedPipeClient(HANDLE hNamedPipe)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetNamedPipeClientComputerNameA(HANDLE Pipe, LPCSTR ClientComputerName, ULONG ClientComputerNameLength)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetNamedPipeClientComputerNameW(HANDLE Pipe, LPCWSTR ClientComputerName, ULONG ClientComputerNameLength)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-08 00:13:30 +04:00
|
|
|
#endif
|