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>
|
2013-09-17 01:10:27 +04:00
|
|
|
#include <winpr/synch.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>
|
2014-02-07 18:17:05 +04:00
|
|
|
#include <errno.h>
|
2013-07-23 06:53:44 +04:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
2014-05-28 15:54:08 +04:00
|
|
|
#include <assert.h>
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "pipe.h"
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("pipe")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-18 21:16:42 +04:00
|
|
|
/*
|
2014-05-09 01:17:39 +04:00
|
|
|
* Since the WinPR implementation of named pipes makes use of UNIX domain
|
2014-04-18 21:16:42 +04:00
|
|
|
* sockets, it is not possible to bind the same name more than once (i.e.,
|
|
|
|
* SO_REUSEADDR does not work with UNIX domain sockets). As a result, the
|
2014-05-28 15:54:08 +04:00
|
|
|
* first call to CreateNamedPipe with name n creates a "shared" UNIX domain
|
|
|
|
* socket descriptor that gets duplicated via dup() for the first and all
|
|
|
|
* subsequent calls to CreateNamedPipe with name n.
|
2014-04-18 21:16:42 +04:00
|
|
|
*
|
2014-05-28 15:54:08 +04:00
|
|
|
* The following array keeps track of the references to the shared socked
|
|
|
|
* descriptors. If an entry's reference count is zero the base socket
|
|
|
|
* descriptor gets closed and the entry is removed from the list.
|
2014-04-18 21:16:42 +04:00
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static wArrayList* g_NamedPipeServerSockets = NULL;
|
2014-04-18 22:02:44 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
typedef struct _NamedPipeServerSocketEntry
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* name;
|
2014-05-28 15:54:08 +04:00
|
|
|
int serverfd;
|
|
|
|
int references;
|
|
|
|
} NamedPipeServerSocketEntry;
|
2014-04-18 21:16:42 +04:00
|
|
|
|
|
|
|
static void InitWinPRPipeModule()
|
|
|
|
{
|
2014-05-28 15:54:08 +04:00
|
|
|
if (g_NamedPipeServerSockets)
|
2014-05-09 01:17:39 +04:00
|
|
|
return;
|
2014-04-18 21:16:42 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
g_NamedPipeServerSockets = ArrayList_New(FALSE);
|
2014-04-18 22:02:44 +04:00
|
|
|
}
|
2014-04-18 21:16:42 +04:00
|
|
|
|
|
|
|
|
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];
|
2014-08-18 21:34:47 +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)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "failed to create pipe");
|
2012-10-08 04:25:50 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
pReadPipe = (WINPR_PIPE*) malloc(sizeof(WINPR_PIPE));
|
|
|
|
pWritePipe = (WINPR_PIPE*) malloc(sizeof(WINPR_PIPE));
|
2013-05-17 01:32:58 +04:00
|
|
|
|
|
|
|
if (!pReadPipe || !pWritePipe)
|
2013-08-28 19:36:55 +04:00
|
|
|
{
|
|
|
|
if (pReadPipe)
|
|
|
|
free(pReadPipe);
|
2013-09-26 07:16:33 +04:00
|
|
|
|
2013-08-28 19:36:55 +04:00
|
|
|
if (pWritePipe)
|
|
|
|
free(pWritePipe);
|
2013-09-26 07:16:33 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
return FALSE;
|
2013-08-28 19:36:55 +04:00
|
|
|
}
|
2013-05-17 01:32:58 +04:00
|
|
|
|
|
|
|
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);
|
2014-08-18 21:34:47 +04:00
|
|
|
*((ULONG_PTR*) hReadPipe) = (ULONG_PTR) pReadPipe;
|
2013-05-17 02:27:26 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(pWritePipe, HANDLE_TYPE_ANONYMOUS_PIPE);
|
2014-08-18 21:34:47 +04:00
|
|
|
*((ULONG_PTR*) hWritePipe) = (ULONG_PTR) pWritePipe;
|
2012-10-08 00:13:30 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
/**
|
|
|
|
* Named pipe
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void winpr_unref_named_pipe(WINPR_NAMED_PIPE* pNamedPipe)
|
2013-07-23 00:30:25 +04:00
|
|
|
{
|
2014-05-28 18:22:32 +04:00
|
|
|
int index;
|
2014-08-18 21:34:47 +04:00
|
|
|
NamedPipeServerSocketEntry* baseSocket;
|
2014-05-28 18:22:32 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!pNamedPipe)
|
2014-05-28 18:22:32 +04:00
|
|
|
return;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
assert(pNamedPipe->name);
|
2014-05-28 18:22:32 +04:00
|
|
|
assert(g_NamedPipeServerSockets);
|
2014-08-18 20:57:08 +04:00
|
|
|
//WLog_VRB(TAG, "%s: %p (%s)", __FUNCTION__, pNamedPipe, pNamedPipe->name);
|
2014-05-28 18:22:32 +04:00
|
|
|
ArrayList_Lock(g_NamedPipeServerSockets);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 18:22:32 +04:00
|
|
|
for (index = 0; index < ArrayList_Count(g_NamedPipeServerSockets); index++)
|
2014-04-18 21:16:42 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
baseSocket = (NamedPipeServerSocketEntry*) ArrayList_GetItem(
|
2014-08-18 19:22:22 +04:00
|
|
|
g_NamedPipeServerSockets, index);
|
2014-05-28 18:22:32 +04:00
|
|
|
assert(baseSocket->name);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 18:22:32 +04:00
|
|
|
if (!strcmp(baseSocket->name, pNamedPipe->name))
|
2014-04-18 21:16:42 +04:00
|
|
|
{
|
2014-05-28 18:22:32 +04:00
|
|
|
assert(baseSocket->references > 0);
|
|
|
|
assert(baseSocket->serverfd != -1);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 18:22:32 +04:00
|
|
|
if (--baseSocket->references == 0)
|
2014-05-28 15:54:08 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
//WLog_DBG(TAG, "%s: removing shared server socked resource", __FUNCTION__);
|
|
|
|
//WLog_DBG(TAG, "%s: closing shared serverfd %d", __FUNCTION__, baseSocket->serverfd);
|
2014-05-28 18:22:32 +04:00
|
|
|
ArrayList_Remove(g_NamedPipeServerSockets, baseSocket);
|
|
|
|
close(baseSocket->serverfd);
|
|
|
|
free(baseSocket->name);
|
|
|
|
free(baseSocket);
|
2014-05-28 15:54:08 +04:00
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 18:22:32 +04:00
|
|
|
break;
|
2014-04-18 21:16:42 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 18:22:32 +04:00
|
|
|
ArrayList_Unlock(g_NamedPipeServerSockets);
|
2014-05-28 15:54:08 +04:00
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances,
|
2014-08-18 19:22:22 +04:00
|
|
|
DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
2014-05-28 15:54:08 +04:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
HANDLE hNamedPipe = INVALID_HANDLE_VALUE;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* lpPipePath;
|
2014-05-28 15:54:08 +04:00
|
|
|
struct sockaddr_un s;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe = NULL;
|
2014-05-28 15:54:08 +04:00
|
|
|
int serverfd = -1;
|
2014-08-18 21:34:47 +04:00
|
|
|
NamedPipeServerSocketEntry* baseSocket = NULL;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!lpName)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
InitWinPRPipeModule();
|
2014-08-18 21:34:47 +04:00
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) calloc(1, sizeof(WINPR_NAMED_PIPE));
|
2014-05-28 15:54:08 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(pNamedPipe, HANDLE_TYPE_NAMED_PIPE);
|
|
|
|
|
|
|
|
if (!(pNamedPipe->name = _strdup(lpName)))
|
|
|
|
goto out;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!(pNamedPipe->lpFileName = GetNamedPipeNameWithoutPrefixA(lpName)))
|
|
|
|
goto out;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!(pNamedPipe->lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpName)))
|
|
|
|
goto out;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
pNamedPipe->dwOpenMode = dwOpenMode;
|
|
|
|
pNamedPipe->dwPipeMode = dwPipeMode;
|
|
|
|
pNamedPipe->nMaxInstances = nMaxInstances;
|
|
|
|
pNamedPipe->nOutBufferSize = nOutBufferSize;
|
|
|
|
pNamedPipe->nInBufferSize = nInBufferSize;
|
|
|
|
pNamedPipe->nDefaultTimeOut = nDefaultTimeOut;
|
2013-08-23 06:36:37 +04:00
|
|
|
pNamedPipe->dwFlagsAndAttributes = dwOpenMode;
|
2014-04-18 21:16:42 +04:00
|
|
|
pNamedPipe->clientfd = -1;
|
|
|
|
pNamedPipe->ServerMode = TRUE;
|
2014-05-28 15:54:08 +04:00
|
|
|
ArrayList_Lock(g_NamedPipeServerSockets);
|
|
|
|
|
|
|
|
for (index = 0; index < ArrayList_Count(g_NamedPipeServerSockets); index++)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
baseSocket = (NamedPipeServerSocketEntry*) ArrayList_GetItem(
|
2014-08-18 19:22:22 +04:00
|
|
|
g_NamedPipeServerSockets, index);
|
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!strcmp(baseSocket->name, lpName))
|
|
|
|
{
|
|
|
|
serverfd = baseSocket->serverfd;
|
2014-08-18 20:57:08 +04:00
|
|
|
//WLog_DBG(TAG, "using shared socked resource for pipe %p (%s)", pNamedPipe, lpName);
|
2014-05-28 15:54:08 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 21:16:42 +04:00
|
|
|
|
|
|
|
/* If this is the first instance of the named pipe... */
|
2014-05-28 15:54:08 +04:00
|
|
|
if (serverfd == -1)
|
2013-09-26 03:13:39 +04:00
|
|
|
{
|
2014-04-18 21:16:42 +04:00
|
|
|
/* Create the UNIX domain socket and start listening. */
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!(lpPipePath = GetNamedPipeUnixDomainSocketBaseFilePathA()))
|
|
|
|
goto out;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-04-18 21:16:42 +04:00
|
|
|
if (!PathFileExistsA(lpPipePath))
|
|
|
|
{
|
|
|
|
CreateDirectoryA(lpPipePath, 0);
|
|
|
|
UnixChangeFileMode(lpPipePath, 0xFFFF);
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-04-18 21:16:42 +04:00
|
|
|
free(lpPipePath);
|
2013-10-01 20:03:08 +04:00
|
|
|
|
2014-04-18 21:16:42 +04:00
|
|
|
if (PathFileExistsA(pNamedPipe->lpFilePath))
|
|
|
|
{
|
|
|
|
DeleteFileA(pNamedPipe->lpFilePath);
|
|
|
|
}
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if ((serverfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2014-04-18 21:16:42 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "CreateNamedPipeA: socket error, %s", strerror(errno));
|
2014-05-28 15:54:08 +04:00
|
|
|
goto out;
|
2014-04-18 21:16:42 +04:00
|
|
|
}
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-04-18 21:16:42 +04:00
|
|
|
ZeroMemory(&s, sizeof(struct sockaddr_un));
|
|
|
|
s.sun_family = AF_UNIX;
|
|
|
|
strcpy(s.sun_path, pNamedPipe->lpFilePath);
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
if (bind(serverfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un)) == -1)
|
2014-04-18 21:16:42 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "CreateNamedPipeA: bind error, %s", strerror(errno));
|
2014-05-28 15:54:08 +04:00
|
|
|
goto out;
|
2014-04-18 21:16:42 +04:00
|
|
|
}
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (listen(serverfd, 2) == -1)
|
2014-04-18 21:16:42 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "CreateNamedPipeA: listen error, %s", strerror(errno));
|
2014-05-28 15:54:08 +04:00
|
|
|
goto out;
|
2014-04-18 21:16:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
UnixChangeFileMode(pNamedPipe->lpFilePath, 0xFFFF);
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
if (!(baseSocket = (NamedPipeServerSocketEntry*) malloc(sizeof(NamedPipeServerSocketEntry))))
|
2014-05-28 15:54:08 +04:00
|
|
|
goto out;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (!(baseSocket->name = _strdup(lpName)))
|
|
|
|
{
|
|
|
|
free(baseSocket);
|
|
|
|
goto out;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
baseSocket->serverfd = serverfd;
|
|
|
|
baseSocket->references = 0;
|
|
|
|
ArrayList_Add(g_NamedPipeServerSockets, baseSocket);
|
2014-08-18 20:57:08 +04:00
|
|
|
//WLog_DBG(TAG, "created shared socked resource for pipe %p (%s). base serverfd = %d", pNamedPipe, lpName, serverfd);
|
2014-04-18 21:16:42 +04:00
|
|
|
}
|
2013-10-01 20:03:08 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
pNamedPipe->serverfd = dup(baseSocket->serverfd);
|
2014-08-18 20:57:08 +04:00
|
|
|
//WLog_DBG(TAG, "using serverfd %d (duplicated from %d)", pNamedPipe->serverfd, baseSocket->serverfd);
|
2014-05-28 18:22:32 +04:00
|
|
|
pNamedPipe->pfnUnrefNamedPipe = winpr_unref_named_pipe;
|
2014-05-28 15:54:08 +04:00
|
|
|
baseSocket->references++;
|
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
if (dwOpenMode & FILE_FLAG_OVERLAPPED)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
int flags = fcntl(pNamedPipe->serverfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags != -1)
|
|
|
|
fcntl(pNamedPipe->serverfd, F_SETFL, flags | O_NONBLOCK);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
hNamedPipe = (HANDLE) pNamedPipe;
|
|
|
|
out:
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (hNamedPipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if (pNamedPipe)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
free((void*)pNamedPipe->name);
|
|
|
|
free((void*)pNamedPipe->lpFileName);
|
|
|
|
free((void*)pNamedPipe->lpFilePath);
|
2014-05-28 15:54:08 +04:00
|
|
|
free(pNamedPipe);
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
if (serverfd != -1)
|
|
|
|
close(serverfd);
|
2014-04-26 07:53:08 +04:00
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-28 15:54:08 +04:00
|
|
|
ArrayList_Unlock(g_NamedPipeServerSockets);
|
|
|
|
return hNamedPipe;
|
2013-07-23 00:30:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateNamedPipeW(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances,
|
2014-08-18 19:22:22 +04:00
|
|
|
DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
2013-07-23 00:30:25 +04:00
|
|
|
{
|
|
|
|
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;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
if (!hNamedPipe)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-09-26 07:16:33 +04:00
|
|
|
if (!(pNamedPipe->dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED))
|
|
|
|
{
|
|
|
|
length = sizeof(struct sockaddr_un);
|
|
|
|
ZeroMemory(&s, sizeof(struct sockaddr_un));
|
2014-08-18 21:34:47 +04:00
|
|
|
status = accept(pNamedPipe->serverfd, (struct sockaddr*) &s, &length);
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2013-09-26 07:16:33 +04:00
|
|
|
if (status < 0)
|
2013-10-01 20:03:08 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "ConnectNamedPipe: accept error");
|
2013-09-26 07:16:33 +04:00
|
|
|
return FALSE;
|
2013-10-01 20:03:08 +04:00
|
|
|
}
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2013-09-26 07:16:33 +04:00
|
|
|
pNamedPipe->clientfd = status;
|
|
|
|
pNamedPipe->ServerMode = FALSE;
|
|
|
|
}
|
|
|
|
else
|
2013-08-23 06:36:37 +04:00
|
|
|
{
|
|
|
|
if (!lpOverlapped)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-09-26 07:16:33 +04:00
|
|
|
if (pNamedPipe->serverfd == -1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
pNamedPipe->lpOverlapped = lpOverlapped;
|
|
|
|
/* synchronous behavior */
|
|
|
|
lpOverlapped->Internal = 2;
|
|
|
|
lpOverlapped->InternalHigh = (ULONG_PTR) 0;
|
|
|
|
lpOverlapped->Pointer = (PVOID) NULL;
|
2013-08-23 06:36:37 +04:00
|
|
|
SetEvent(lpOverlapped->hEvent);
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DisconnectNamedPipe(HANDLE hNamedPipe)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
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,
|
2014-08-18 19:22:22 +04:00
|
|
|
LPDWORD lpBytesRead, LPDWORD lpTotalBytesAvail, LPDWORD lpBytesLeftThisMessage)
|
2013-07-23 00:30:25 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "Not implemented");
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TransactNamedPipe(HANDLE hNamedPipe, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer,
|
2014-08-18 19:22:22 +04:00
|
|
|
DWORD nOutBufferSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
|
2013-07-23 00:30:25 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "Not implemented");
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WaitNamedPipeA(LPCSTR lpNamedPipeName, DWORD nTimeOut)
|
|
|
|
{
|
2013-09-24 02:10:21 +04:00
|
|
|
BOOL status;
|
|
|
|
DWORD nWaitTime;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* lpFilePath;
|
2013-09-24 02:10:21 +04:00
|
|
|
DWORD dwSleepInterval;
|
|
|
|
|
|
|
|
if (!lpNamedPipeName)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpNamedPipeName);
|
|
|
|
|
|
|
|
if (nTimeOut == NMPWAIT_USE_DEFAULT_WAIT)
|
|
|
|
nTimeOut = 50;
|
|
|
|
|
|
|
|
nWaitTime = 0;
|
|
|
|
status = TRUE;
|
|
|
|
dwSleepInterval = 10;
|
|
|
|
|
|
|
|
while (!PathFileExistsA(lpFilePath))
|
|
|
|
{
|
|
|
|
Sleep(dwSleepInterval);
|
|
|
|
nWaitTime += dwSleepInterval;
|
|
|
|
|
|
|
|
if (nWaitTime >= nTimeOut)
|
|
|
|
{
|
|
|
|
status = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-10-15 15:09:13 +04:00
|
|
|
free(lpFilePath);
|
2013-09-24 02:10:21 +04:00
|
|
|
return status;
|
2013-07-23 00:30:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WaitNamedPipeW(LPCWSTR lpNamedPipeName, DWORD nTimeOut)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "Not implemented");
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetNamedPipeHandleState(HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
|
|
|
|
{
|
2013-09-26 07:16:33 +04:00
|
|
|
int fd;
|
2014-04-16 12:05:53 +04:00
|
|
|
int flags;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
if (lpMode)
|
2013-09-26 07:16:33 +04:00
|
|
|
{
|
2013-07-23 02:20:34 +04:00
|
|
|
pNamedPipe->dwPipeMode = *lpMode;
|
2013-09-26 07:16:33 +04:00
|
|
|
fd = (pNamedPipe->ServerMode) ? pNamedPipe->serverfd : pNamedPipe->clientfd;
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
|
|
|
|
if (pNamedPipe->dwPipeMode & PIPE_NOWAIT)
|
|
|
|
flags = (flags | O_NONBLOCK);
|
|
|
|
else
|
|
|
|
flags = (flags & ~(O_NONBLOCK));
|
|
|
|
|
|
|
|
fcntl(fd, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpMaxCollectionCount)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpCollectDataTimeout)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:30:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ImpersonateNamedPipeClient(HANDLE hNamedPipe)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "Not implemented");
|
2013-07-23 00:30:25 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetNamedPipeClientComputerNameA(HANDLE Pipe, LPCSTR ClientComputerName, ULONG ClientComputerNameLength)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "Not implemented");
|
2013-07-23 00:30:25 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetNamedPipeClientComputerNameW(HANDLE Pipe, LPCWSTR ClientComputerName, ULONG ClientComputerNameLength)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "Not implemented");
|
2013-07-23 00:30:25 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-08 00:13:30 +04:00
|
|
|
#endif
|