FreeRDP/winpr/libwinpr/file/namedPipeClient.c

306 lines
7.4 KiB
C
Raw Normal View History

/**
* WinPR: Windows Portable Runtime
* File Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2014 Hewlett-Packard Development Company, L.P.
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 bernhard.miklautz@thincast.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.
*/
2022-02-16 12:08:00 +03:00
#include <winpr/config.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/file.h>
#ifdef WINPR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "../log.h"
#define TAG WINPR_TAG("file")
#ifndef _WIN32
#ifdef ANDROID
#include <sys/vfs.h>
#else
#include <sys/statvfs.h>
#endif
#include "../handle/handle.h"
#include "../pipe/pipe.h"
static HANDLE_CREATOR NamedPipeClientHandleCreator;
static BOOL NamedPipeClientIsHandled(HANDLE handle)
{
return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_NAMED_PIPE, TRUE);
}
2019-11-20 13:30:14 +03:00
static BOOL NamedPipeClientCloseHandle(HANDLE handle)
{
2019-11-06 17:24:51 +03:00
WINPR_NAMED_PIPE* pNamedPipe = (WINPR_NAMED_PIPE*)handle;
if (!NamedPipeClientIsHandled(handle))
return FALSE;
if (pNamedPipe->clientfd != -1)
{
2019-11-06 17:24:51 +03:00
// WLOG_DBG(TAG, "closing clientfd %d", pNamedPipe->clientfd);
close(pNamedPipe->clientfd);
}
if (pNamedPipe->serverfd != -1)
{
2019-11-06 17:24:51 +03:00
// WLOG_DBG(TAG, "closing serverfd %d", pNamedPipe->serverfd);
close(pNamedPipe->serverfd);
}
if (pNamedPipe->pfnUnrefNamedPipe)
pNamedPipe->pfnUnrefNamedPipe(pNamedPipe);
free(pNamedPipe->lpFileName);
free(pNamedPipe->lpFilePath);
free(pNamedPipe->name);
free(pNamedPipe);
return TRUE;
}
static int NamedPipeClientGetFd(HANDLE handle)
{
2018-08-24 10:54:25 +03:00
WINPR_NAMED_PIPE* file = (WINPR_NAMED_PIPE*)handle;
if (!NamedPipeClientIsHandled(handle))
return -1;
if (file->ServerMode)
return file->serverfd;
else
return file->clientfd;
}
2019-11-06 17:24:51 +03:00
static HANDLE_OPS ops = {
2018-08-24 10:54:25 +03:00
NamedPipeClientIsHandled,
NamedPipeClientCloseHandle,
NamedPipeClientGetFd,
NULL, /* CleanupHandle */
NamedPipeRead,
NULL, /* FileReadEx */
NULL, /* FileReadScatter */
NamedPipeWrite,
NULL, /* FileWriteEx */
NULL, /* FileWriteGather */
NULL, /* FileGetFileSize */
NULL, /* FlushFileBuffers */
NULL, /* FileSetEndOfFile */
NULL, /* FileSetFilePointer */
NULL, /* SetFilePointerEx */
NULL, /* FileLockFile */
NULL, /* FileLockFileEx */
NULL, /* FileUnlockFile */
NULL, /* FileUnlockFileEx */
2022-03-22 11:25:40 +03:00
NULL, /* SetFileTime */
NULL, /* FileGetFileInformationByHandle */
};
2018-08-24 10:54:25 +03:00
static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess,
2019-11-06 17:24:51 +03:00
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)
{
char* name = NULL;
int status = 0;
HANDLE hNamedPipe = NULL;
struct sockaddr_un s = { 0 };
WINPR_NAMED_PIPE* pNamedPipe = NULL;
if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
{
WLog_ERR(TAG, "WinPR does not support the FILE_FLAG_OVERLAPPED flag");
SetLastError(ERROR_NOT_SUPPORTED);
return INVALID_HANDLE_VALUE;
}
if (!lpFileName)
return INVALID_HANDLE_VALUE;
if (!IsNamedPipeFileNameA(lpFileName))
return INVALID_HANDLE_VALUE;
name = GetNamedPipeNameWithoutPrefixA(lpFileName);
if (!name)
return INVALID_HANDLE_VALUE;
free(name);
2019-11-06 17:24:51 +03:00
pNamedPipe = (WINPR_NAMED_PIPE*)calloc(1, sizeof(WINPR_NAMED_PIPE));
2018-08-24 10:54:25 +03:00
if (!pNamedPipe)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return INVALID_HANDLE_VALUE;
}
2019-11-06 17:24:51 +03:00
hNamedPipe = (HANDLE)pNamedPipe;
WINPR_HANDLE_SET_TYPE_AND_MODE(pNamedPipe, HANDLE_TYPE_NAMED_PIPE, WINPR_FD_READ);
pNamedPipe->name = _strdup(lpFileName);
2018-08-24 10:54:25 +03:00
if (!pNamedPipe->name)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
2018-08-24 10:54:25 +03:00
pNamedPipe->dwOpenMode = 0;
pNamedPipe->dwPipeMode = 0;
pNamedPipe->nMaxInstances = 0;
pNamedPipe->nOutBufferSize = 0;
pNamedPipe->nInBufferSize = 0;
pNamedPipe->nDefaultTimeOut = 0;
pNamedPipe->dwFlagsAndAttributes = dwFlagsAndAttributes;
pNamedPipe->lpFileName = GetNamedPipeNameWithoutPrefixA(lpFileName);
2018-08-24 10:54:25 +03:00
if (!pNamedPipe->lpFileName)
{
2018-08-24 10:54:25 +03:00
free((void*)pNamedPipe->name);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
2018-08-24 10:54:25 +03:00
pNamedPipe->lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpFileName);
2018-08-24 10:54:25 +03:00
if (!pNamedPipe->lpFilePath)
{
2018-08-24 10:54:25 +03:00
free((void*)pNamedPipe->lpFileName);
free((void*)pNamedPipe->name);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
2018-08-24 10:54:25 +03:00
pNamedPipe->clientfd = socket(PF_LOCAL, SOCK_STREAM, 0);
pNamedPipe->serverfd = -1;
pNamedPipe->ServerMode = FALSE;
s.sun_family = AF_UNIX;
(void)sprintf_s(s.sun_path, ARRAYSIZE(s.sun_path), "%s", pNamedPipe->lpFilePath);
2019-11-06 17:24:51 +03:00
status = connect(pNamedPipe->clientfd, (struct sockaddr*)&s, sizeof(struct sockaddr_un));
pNamedPipe->common.ops = &ops;
if (status != 0)
{
close(pNamedPipe->clientfd);
2024-08-29 12:11:11 +03:00
free(pNamedPipe->name);
free(pNamedPipe->lpFileName);
free(pNamedPipe->lpFilePath);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
{
#if 0
int flags = fcntl(pNamedPipe->clientfd, F_GETFL);
if (flags != -1)
(void)fcntl(pNamedPipe->clientfd, F_SETFL, flags | O_NONBLOCK);
#endif
}
return hNamedPipe;
}
extern HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void);
2018-08-24 10:54:25 +03:00
HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void)
{
NamedPipeClientHandleCreator.IsHandled = IsNamedPipeFileNameA;
NamedPipeClientHandleCreator.CreateFileA = NamedPipeClientCreateFileA;
return &NamedPipeClientHandleCreator;
}
#endif
/* Extended API */
2019-11-06 17:24:51 +03:00
#define NAMED_PIPE_PREFIX_PATH "\\\\.\\pipe\\"
BOOL IsNamedPipeFileNameA(LPCSTR lpName)
{
if (strncmp(lpName, NAMED_PIPE_PREFIX_PATH, sizeof(NAMED_PIPE_PREFIX_PATH) - 1) != 0)
return FALSE;
return TRUE;
}
char* GetNamedPipeNameWithoutPrefixA(LPCSTR lpName)
{
char* lpFileName = NULL;
if (!lpName)
return NULL;
if (!IsNamedPipeFileNameA(lpName))
return NULL;
lpFileName = _strdup(&lpName[strnlen(NAMED_PIPE_PREFIX_PATH, sizeof(NAMED_PIPE_PREFIX_PATH))]);
return lpFileName;
}
char* GetNamedPipeUnixDomainSocketBaseFilePathA(void)
{
char* lpTempPath = NULL;
char* lpPipePath = NULL;
lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
2018-08-24 10:54:25 +03:00
if (!lpTempPath)
return NULL;
2018-08-24 10:54:25 +03:00
lpPipePath = GetCombinedPath(lpTempPath, ".pipe");
free(lpTempPath);
return lpPipePath;
}
char* GetNamedPipeUnixDomainSocketFilePathA(LPCSTR lpName)
{
char* lpPipePath = NULL;
char* lpFileName = NULL;
char* lpFilePath = NULL;
lpPipePath = GetNamedPipeUnixDomainSocketBaseFilePathA();
lpFileName = GetNamedPipeNameWithoutPrefixA(lpName);
2024-08-29 12:11:11 +03:00
lpFilePath = GetCombinedPath(lpPipePath, lpFileName);
free(lpPipePath);
free(lpFileName);
return lpFilePath;
}
int GetNamePipeFileDescriptor(HANDLE hNamedPipe)
{
#ifndef _WIN32
int fd = 0;
WINPR_NAMED_PIPE* pNamedPipe = (WINPR_NAMED_PIPE*)hNamedPipe;
if (!NamedPipeClientIsHandled(hNamedPipe))
return -1;
fd = (pNamedPipe->ServerMode) ? pNamedPipe->serverfd : pNamedPipe->clientfd;
return fd;
#else
return -1;
#endif
}