From 0eb0cff3678b57b04fb928dca1e1f9728aeebac3 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Wed, 4 Sep 2024 11:03:20 +0200 Subject: [PATCH 1/3] [winpr,utils] fix integer size warnings --- winpr/libwinpr/utils/collections/ArrayList.c | 19 +++++++++++-------- winpr/libwinpr/utils/collections/BufferPool.c | 6 ++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/winpr/libwinpr/utils/collections/ArrayList.c b/winpr/libwinpr/utils/collections/ArrayList.c index 5595edb84..2f8ff9182 100644 --- a/winpr/libwinpr/utils/collections/ArrayList.c +++ b/winpr/libwinpr/utils/collections/ArrayList.c @@ -430,22 +430,24 @@ BOOL ArrayList_RemoveAt(wArrayList* arrayList, size_t index) SSIZE_T ArrayList_IndexOf(wArrayList* arrayList, const void* obj, SSIZE_T startIndex, SSIZE_T count) { - SSIZE_T sindex = 0; - SSIZE_T cindex = 0; BOOL found = FALSE; WINPR_ASSERT(arrayList); ArrayList_Lock_Conditional(arrayList); - sindex = (size_t)startIndex; + SSIZE_T sindex = startIndex; if (startIndex < 0) sindex = 0; - cindex = (size_t)count; - if (count < 0) - cindex = arrayList->size; - SSIZE_T index = sindex; + SSIZE_T cindex = count; + if (count < 0) + { + if (arrayList->size > SSIZE_MAX) + goto fail; + cindex = (SSIZE_T)arrayList->size; + } + for (; index < sindex + cindex; index++) { if (arrayList->object.fnObjectEquals(arrayList->array[index], obj)) @@ -455,6 +457,7 @@ SSIZE_T ArrayList_IndexOf(wArrayList* arrayList, const void* obj, SSIZE_T startI } } +fail: if (!found) index = -1; @@ -486,7 +489,7 @@ SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList, const void* obj, SSIZE_T st WINPR_ASSERT(arrayList); ArrayList_Lock_Conditional(arrayList); - sindex = (size_t)startIndex; + sindex = startIndex; if (startIndex < 0) sindex = 0; diff --git a/winpr/libwinpr/utils/collections/BufferPool.c b/winpr/libwinpr/utils/collections/BufferPool.c index ebdf4f13b..1d10cefa3 100644 --- a/winpr/libwinpr/utils/collections/BufferPool.c +++ b/winpr/libwinpr/utils/collections/BufferPool.c @@ -314,13 +314,15 @@ void* BufferPool_Take(wBufferPool* pool, SSIZE_T size) if (pool->uSize + 1 > pool->uCapacity) { - size_t newUCapacity = pool->uCapacity * 2; + size_t newUCapacity = pool->uCapacity * 2ULL; + if (newUCapacity > SSIZE_MAX) + goto out_error; wBufferPoolItem* newUArray = (wBufferPoolItem*)realloc(pool->uArray, sizeof(wBufferPoolItem) * newUCapacity); if (!newUArray) goto out_error; - pool->uCapacity = newUCapacity; + pool->uCapacity = (SSIZE_T)newUCapacity; pool->uArray = newUArray; } From e37dffaca494d49f44e694669105967eb2ca4ddf Mon Sep 17 00:00:00 2001 From: akallabeth Date: Wed, 4 Sep 2024 11:26:13 +0200 Subject: [PATCH 2/3] [crypto,tls] disable linter warning the argument is #ifdefed, so some configurations produce a warning --- libfreerdp/crypto/tls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libfreerdp/crypto/tls.c b/libfreerdp/crypto/tls.c index a9a086b03..85aa738ed 100644 --- a/libfreerdp/crypto/tls.c +++ b/libfreerdp/crypto/tls.c @@ -840,7 +840,7 @@ static BOOL tls_prepare(rdpTls* tls, BIO* underlying, SSL_METHOD* method, int op return TRUE; } -static void adjustSslOptions(int* options) +static void adjustSslOptions(int* options) // NOLINT(readability-non-const-parameter) { WINPR_ASSERT(options); #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) From 9c6126c470c03f334d25fd5599d297833da35188 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Tue, 3 Sep 2024 21:31:09 +0200 Subject: [PATCH 3/3] [winpr,comm] build generic serial-port support * Disable linux specific code if build on *BSD * Build stub to just return NULL for unsupported platforms --- winpr/include/winpr/comm.h | 61 ------------------------ winpr/libwinpr/comm/CMakeLists.txt | 32 ++++++++----- winpr/libwinpr/comm/comm.c | 15 ++++++ winpr/libwinpr/comm/comm.h | 16 +++++-- winpr/libwinpr/comm/comm_io.c | 10 ++++ winpr/libwinpr/comm/comm_ioctl.c | 68 +++++++++++++++++++++++---- winpr/libwinpr/comm/comm_ioctl.h | 4 -- winpr/libwinpr/comm/comm_sercx2_sys.c | 6 +-- winpr/libwinpr/comm/comm_sercx2_sys.h | 6 +-- winpr/libwinpr/comm/comm_sercx_sys.c | 8 +--- winpr/libwinpr/comm/comm_sercx_sys.h | 6 +-- winpr/libwinpr/comm/comm_serial_sys.c | 31 +++++++----- winpr/libwinpr/comm/comm_serial_sys.h | 6 +-- winpr/libwinpr/file/generic.c | 8 ++-- 14 files changed, 143 insertions(+), 134 deletions(-) diff --git a/winpr/include/winpr/comm.h b/winpr/include/winpr/comm.h index c05548b8c..a58cc4a71 100644 --- a/winpr/include/winpr/comm.h +++ b/winpr/include/winpr/comm.h @@ -28,8 +28,6 @@ #include #include -#if defined __linux__ && !defined ANDROID - #define NOPARITY 0 #define ODDPARITY 1 #define EVENPARITY 2 @@ -463,63 +461,6 @@ extern "C" const char* name; } _SERIAL_IOCTL_NAME; - static const _SERIAL_IOCTL_NAME _SERIAL_IOCTL_NAMES[] = { - { IOCTL_SERIAL_SET_BAUD_RATE, "IOCTL_SERIAL_SET_BAUD_RATE" }, - { IOCTL_SERIAL_GET_BAUD_RATE, "IOCTL_SERIAL_GET_BAUD_RATE" }, - { IOCTL_SERIAL_SET_LINE_CONTROL, "IOCTL_SERIAL_SET_LINE_CONTROL" }, - { IOCTL_SERIAL_GET_LINE_CONTROL, "IOCTL_SERIAL_GET_LINE_CONTROL" }, - { IOCTL_SERIAL_SET_TIMEOUTS, "IOCTL_SERIAL_SET_TIMEOUTS" }, - { IOCTL_SERIAL_GET_TIMEOUTS, "IOCTL_SERIAL_GET_TIMEOUTS" }, - { IOCTL_SERIAL_GET_CHARS, "IOCTL_SERIAL_GET_CHARS" }, - { IOCTL_SERIAL_SET_CHARS, "IOCTL_SERIAL_SET_CHARS" }, - { IOCTL_SERIAL_SET_DTR, "IOCTL_SERIAL_SET_DTR" }, - { IOCTL_SERIAL_CLR_DTR, "IOCTL_SERIAL_CLR_DTR" }, - { IOCTL_SERIAL_RESET_DEVICE, "IOCTL_SERIAL_RESET_DEVICE" }, - { IOCTL_SERIAL_SET_RTS, "IOCTL_SERIAL_SET_RTS" }, - { IOCTL_SERIAL_CLR_RTS, "IOCTL_SERIAL_CLR_RTS" }, - { IOCTL_SERIAL_SET_XOFF, "IOCTL_SERIAL_SET_XOFF" }, - { IOCTL_SERIAL_SET_XON, "IOCTL_SERIAL_SET_XON" }, - { IOCTL_SERIAL_SET_BREAK_ON, "IOCTL_SERIAL_SET_BREAK_ON" }, - { IOCTL_SERIAL_SET_BREAK_OFF, "IOCTL_SERIAL_SET_BREAK_OFF" }, - { IOCTL_SERIAL_SET_QUEUE_SIZE, "IOCTL_SERIAL_SET_QUEUE_SIZE" }, - { IOCTL_SERIAL_GET_WAIT_MASK, "IOCTL_SERIAL_GET_WAIT_MASK" }, - { IOCTL_SERIAL_SET_WAIT_MASK, "IOCTL_SERIAL_SET_WAIT_MASK" }, - { IOCTL_SERIAL_WAIT_ON_MASK, "IOCTL_SERIAL_WAIT_ON_MASK" }, - { IOCTL_SERIAL_IMMEDIATE_CHAR, "IOCTL_SERIAL_IMMEDIATE_CHAR" }, - { IOCTL_SERIAL_PURGE, "IOCTL_SERIAL_PURGE" }, - { IOCTL_SERIAL_GET_HANDFLOW, "IOCTL_SERIAL_GET_HANDFLOW" }, - { IOCTL_SERIAL_SET_HANDFLOW, "IOCTL_SERIAL_SET_HANDFLOW" }, - { IOCTL_SERIAL_GET_MODEMSTATUS, "IOCTL_SERIAL_GET_MODEMSTATUS" }, - { IOCTL_SERIAL_GET_DTRRTS, "IOCTL_SERIAL_GET_DTRRTS" }, - { IOCTL_SERIAL_GET_COMMSTATUS, "IOCTL_SERIAL_GET_COMMSTATUS" }, - { IOCTL_SERIAL_GET_PROPERTIES, "IOCTL_SERIAL_GET_PROPERTIES" }, - // {IOCTL_SERIAL_XOFF_COUNTER, "IOCTL_SERIAL_XOFF_COUNTER"}, - // {IOCTL_SERIAL_LSRMST_INSERT, "IOCTL_SERIAL_LSRMST_INSERT"}, - { IOCTL_SERIAL_CONFIG_SIZE, "IOCTL_SERIAL_CONFIG_SIZE" }, - // {IOCTL_SERIAL_GET_STATS, "IOCTL_SERIAL_GET_STATS"}, - // {IOCTL_SERIAL_CLEAR_STATS, "IOCTL_SERIAL_CLEAR_STATS"}, - // {IOCTL_SERIAL_GET_MODEM_CONTROL,"IOCTL_SERIAL_GET_MODEM_CONTROL"}, - // {IOCTL_SERIAL_SET_MODEM_CONTROL,"IOCTL_SERIAL_SET_MODEM_CONTROL"}, - // {IOCTL_SERIAL_SET_FIFO_CONTROL, "IOCTL_SERIAL_SET_FIFO_CONTROL"}, - - // {IOCTL_PAR_QUERY_INFORMATION, "IOCTL_PAR_QUERY_INFORMATION"}, - // {IOCTL_PAR_SET_INFORMATION, "IOCTL_PAR_SET_INFORMATION"}, - // {IOCTL_PAR_QUERY_DEVICE_ID, "IOCTL_PAR_QUERY_DEVICE_ID"}, - // {IOCTL_PAR_QUERY_DEVICE_ID_SIZE,"IOCTL_PAR_QUERY_DEVICE_ID_SIZE"}, - // {IOCTL_IEEE1284_GET_MODE, "IOCTL_IEEE1284_GET_MODE"}, - // {IOCTL_IEEE1284_NEGOTIATE, "IOCTL_IEEE1284_NEGOTIATE"}, - // {IOCTL_PAR_SET_WRITE_ADDRESS, "IOCTL_PAR_SET_WRITE_ADDRESS"}, - // {IOCTL_PAR_SET_READ_ADDRESS, "IOCTL_PAR_SET_READ_ADDRESS"}, - // {IOCTL_PAR_GET_DEVICE_CAPS, "IOCTL_PAR_GET_DEVICE_CAPS"}, - // {IOCTL_PAR_GET_DEFAULT_MODES, "IOCTL_PAR_GET_DEFAULT_MODES"}, - // {IOCTL_PAR_QUERY_RAW_DEVICE_ID, "IOCTL_PAR_QUERY_RAW_DEVICE_ID"}, - // {IOCTL_PAR_IS_PORT_FREE, "IOCTL_PAR_IS_PORT_FREE"}, - - { IOCTL_USBPRINT_GET_1284_ID, "IOCTL_USBPRINT_GET_1284_ID" }, - - { 0, NULL } - }; - /** * FIXME: got a proper function name and place */ @@ -561,6 +502,4 @@ extern "C" } #endif -#endif /* __linux__ */ - #endif /* WINPR_COMM_H */ diff --git a/winpr/libwinpr/comm/CMakeLists.txt b/winpr/libwinpr/comm/CMakeLists.txt index 8da2906ed..93535f43f 100644 --- a/winpr/libwinpr/comm/CMakeLists.txt +++ b/winpr/libwinpr/comm/CMakeLists.txt @@ -18,23 +18,31 @@ set(MODULE_NAME "winpr-comm") set(MODULE_PREFIX "WINPR_COMM") -if(UNIX AND NOT ANDROID AND NOT APPLE AND NOT EMSCRIPTEN) +if (NOT WIN32) set(${MODULE_PREFIX}_SRCS comm.c comm.h - comm_io.c - comm_ioctl.c - comm_ioctl.h - comm_serial_sys.c - comm_serial_sys.h - comm_sercx_sys.c - comm_sercx_sys.h - comm_sercx2_sys.c - comm_sercx2_sys.h) + ) + if(UNIX AND NOT IOS AND NOT EMSCRIPTEN) + add_definitions(-DWINPR_HAVE_SERIAL_SUPPORT) + list(APPEND ${MODULE_PREFIX}_SRCS + comm_io.c + comm_ioctl.c + comm_ioctl.h + comm_serial_sys.c + comm_serial_sys.h + comm_sercx_sys.c + comm_sercx_sys.h + comm_sercx2_sys.c + comm_sercx2_sys.h + ) + endif() winpr_module_add(${${MODULE_PREFIX}_SRCS}) - if(BUILD_TESTING AND BUILD_COMM_TESTS) - add_subdirectory(test) + if(UNIX AND NOT IOS AND NOT EMSCRIPTEN) + if(BUILD_TESTING AND BUILD_COMM_TESTS) + add_subdirectory(test) + endif() endif() endif() diff --git a/winpr/libwinpr/comm/comm.c b/winpr/libwinpr/comm/comm.c index f41ff0a9f..97af7936f 100644 --- a/winpr/libwinpr/comm/comm.c +++ b/winpr/libwinpr/comm/comm.c @@ -27,6 +27,9 @@ #include #include #include +#if defined(WINPR_HAVE_SYS_EVENTFD_H) +#include +#endif #include #include #include @@ -83,9 +86,13 @@ static int CommGetFd(HANDLE handle) HANDLE_CREATOR* GetCommHandleCreator(void) { +#if defined(WINPR_HAVE_SERIAL_SUPPORT) sCommHandleCreator.IsHandled = IsCommDevice; sCommHandleCreator.CreateFileA = CommCreateFileA; return &sCommHandleCreator; +#else + return NULL; +#endif } static void CommInit(void) @@ -1261,8 +1268,10 @@ HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShare goto error_handle; } +#if defined(WINPR_HAVE_SYS_EVENTFD_H) pComm->fd_read_event = eventfd( 0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */ +#endif if (pComm->fd_read_event < 0) { @@ -1281,8 +1290,10 @@ HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShare goto error_handle; } +#if defined(WINPR_HAVE_SYS_EVENTFD_H) pComm->fd_write_event = eventfd( 0, EFD_NONBLOCK); /* EFD_NONBLOCK required because a read() is not always expected */ +#endif if (pComm->fd_write_event < 0) { @@ -1296,6 +1307,7 @@ HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShare pComm->serverSerialDriverId = SerialDriverUnknown; InitializeCriticalSection(&pComm->EventsLock); +#if defined(WINPR_HAVE_COMM_COUNTERS) if (ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters)) < 0) { char ebuffer[256] = { 0 }; @@ -1310,6 +1322,7 @@ HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShare */ ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct)); } +#endif /* The binary/raw mode is required for the redirection but * only flags that are not handle somewhere-else, except @@ -1402,6 +1415,7 @@ BOOL CommCloseHandle(HANDLE handle) return TRUE; } +#if defined(WINPR_HAVE_SYS_EVENTFD_H) #ifndef WITH_EVENTFD_READ_WRITE int eventfd_read(int fd, eventfd_t* value) { @@ -1413,3 +1427,4 @@ int eventfd_write(int fd, eventfd_t value) return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1; } #endif +#endif diff --git a/winpr/libwinpr/comm/comm.h b/winpr/libwinpr/comm/comm.h index c7884cbbf..8f95bdf08 100644 --- a/winpr/libwinpr/comm/comm.h +++ b/winpr/libwinpr/comm/comm.h @@ -21,16 +21,20 @@ #ifndef WINPR_COMM_PRIVATE_H #define WINPR_COMM_PRIVATE_H -#if defined __linux__ && !defined ANDROID - +#if defined(__linux__) +#define WINPR_HAVE_COMM_COUNTERS #include -#include +#endif #include #include "../handle/handle.h" #include +#if defined(WINPR_HAVE_SYS_EVENTFD_H) +#include +#endif + struct winpr_comm { WINPR_HANDLE common; @@ -65,7 +69,9 @@ struct winpr_comm CRITICAL_SECTION EventsLock; /* protects counters, WaitEventMask and PendingEvents */ +#if defined(WINPR_HAVE_COMM_COUNTERS) struct serial_icounter_struct counters; +#endif ULONG WaitEventMask; ULONG PendingEvents; @@ -101,11 +107,11 @@ BOOL CommIsHandleValid(HANDLE handle); BOOL CommCloseHandle(HANDLE handle); HANDLE_CREATOR* GetCommHandleCreator(void); +#if defined(WINPR_HAVE_SYS_EVENTFD_H) #ifndef WITH_EVENTFD_READ_WRITE int eventfd_read(int fd, eventfd_t* value); int eventfd_write(int fd, eventfd_t value); #endif - -#endif /* __linux__ */ +#endif #endif /* WINPR_COMM_PRIVATE_H */ diff --git a/winpr/libwinpr/comm/comm_io.c b/winpr/libwinpr/comm/comm_io.c index ee0c3cd07..d1c18c28b 100644 --- a/winpr/libwinpr/comm/comm_io.c +++ b/winpr/libwinpr/comm/comm_io.c @@ -225,7 +225,9 @@ BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, /* FIXME: had expected eventfd_write() to return EAGAIN when * there is no eventfd_read() but this not the case. */ /* discard a possible and no more relevant event */ +#if defined(WINPR_HAVE_SYS_EVENTFD_H) eventfd_read(pComm->fd_read_event, NULL); +#endif biggestFd = pComm->fd_read; if (pComm->fd_read_event > biggestFd) @@ -258,6 +260,7 @@ BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, if (FD_ISSET(pComm->fd_read_event, &read_set)) { +#if defined(WINPR_HAVE_SYS_EVENTFD_H) eventfd_t event = 0; if (eventfd_read(pComm->fd_read_event, &event) < 0) @@ -286,6 +289,7 @@ BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, } WINPR_ASSERT(event == WINPR_PURGE_RXABORT); /* no other expected event so far */ +#endif } if (FD_ISSET(pComm->fd_read, &read_set)) @@ -394,7 +398,11 @@ BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite /* FIXME: had expected eventfd_write() to return EAGAIN when * there is no eventfd_read() but this not the case. */ /* discard a possible and no more relevant event */ + +#if defined(WINPR_HAVE_SYS_EVENTFD_H) eventfd_read(pComm->fd_write_event, NULL); +#endif + /* ms */ ULONGLONG Tmax = 1ull * nNumberOfBytesToWrite * pComm->timeouts.WriteTotalTimeoutMultiplier + 1ull * pComm->timeouts.WriteTotalTimeoutConstant; @@ -456,6 +464,7 @@ BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite if (FD_ISSET(pComm->fd_write_event, &event_set)) { +#if defined(WINPR_HAVE_SYS_EVENTFD_H) eventfd_t event = 0; if (eventfd_read(pComm->fd_write_event, &event) < 0) @@ -484,6 +493,7 @@ BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite } WINPR_ASSERT(event == WINPR_PURGE_TXABORT); /* no other expected event so far */ +#endif } /* write_set */ diff --git a/winpr/libwinpr/comm/comm_ioctl.c b/winpr/libwinpr/comm/comm_ioctl.c index 4f208ef43..ff408a309 100644 --- a/winpr/libwinpr/comm/comm_ioctl.c +++ b/winpr/libwinpr/comm/comm_ioctl.c @@ -22,8 +22,6 @@ #include -#if defined __linux__ && !defined ANDROID - #include #include @@ -50,15 +48,67 @@ * found in [MSFT-W2KDDK] Volume 2, Part 2—Serial and Parallel * Drivers, and in [MSDN-PORTS]. */ +static const _SERIAL_IOCTL_NAME S_SERIAL_IOCTL_NAMES[] = { + { IOCTL_SERIAL_SET_BAUD_RATE, "IOCTL_SERIAL_SET_BAUD_RATE" }, + { IOCTL_SERIAL_GET_BAUD_RATE, "IOCTL_SERIAL_GET_BAUD_RATE" }, + { IOCTL_SERIAL_SET_LINE_CONTROL, "IOCTL_SERIAL_SET_LINE_CONTROL" }, + { IOCTL_SERIAL_GET_LINE_CONTROL, "IOCTL_SERIAL_GET_LINE_CONTROL" }, + { IOCTL_SERIAL_SET_TIMEOUTS, "IOCTL_SERIAL_SET_TIMEOUTS" }, + { IOCTL_SERIAL_GET_TIMEOUTS, "IOCTL_SERIAL_GET_TIMEOUTS" }, + { IOCTL_SERIAL_GET_CHARS, "IOCTL_SERIAL_GET_CHARS" }, + { IOCTL_SERIAL_SET_CHARS, "IOCTL_SERIAL_SET_CHARS" }, + { IOCTL_SERIAL_SET_DTR, "IOCTL_SERIAL_SET_DTR" }, + { IOCTL_SERIAL_CLR_DTR, "IOCTL_SERIAL_CLR_DTR" }, + { IOCTL_SERIAL_RESET_DEVICE, "IOCTL_SERIAL_RESET_DEVICE" }, + { IOCTL_SERIAL_SET_RTS, "IOCTL_SERIAL_SET_RTS" }, + { IOCTL_SERIAL_CLR_RTS, "IOCTL_SERIAL_CLR_RTS" }, + { IOCTL_SERIAL_SET_XOFF, "IOCTL_SERIAL_SET_XOFF" }, + { IOCTL_SERIAL_SET_XON, "IOCTL_SERIAL_SET_XON" }, + { IOCTL_SERIAL_SET_BREAK_ON, "IOCTL_SERIAL_SET_BREAK_ON" }, + { IOCTL_SERIAL_SET_BREAK_OFF, "IOCTL_SERIAL_SET_BREAK_OFF" }, + { IOCTL_SERIAL_SET_QUEUE_SIZE, "IOCTL_SERIAL_SET_QUEUE_SIZE" }, + { IOCTL_SERIAL_GET_WAIT_MASK, "IOCTL_SERIAL_GET_WAIT_MASK" }, + { IOCTL_SERIAL_SET_WAIT_MASK, "IOCTL_SERIAL_SET_WAIT_MASK" }, + { IOCTL_SERIAL_WAIT_ON_MASK, "IOCTL_SERIAL_WAIT_ON_MASK" }, + { IOCTL_SERIAL_IMMEDIATE_CHAR, "IOCTL_SERIAL_IMMEDIATE_CHAR" }, + { IOCTL_SERIAL_PURGE, "IOCTL_SERIAL_PURGE" }, + { IOCTL_SERIAL_GET_HANDFLOW, "IOCTL_SERIAL_GET_HANDFLOW" }, + { IOCTL_SERIAL_SET_HANDFLOW, "IOCTL_SERIAL_SET_HANDFLOW" }, + { IOCTL_SERIAL_GET_MODEMSTATUS, "IOCTL_SERIAL_GET_MODEMSTATUS" }, + { IOCTL_SERIAL_GET_DTRRTS, "IOCTL_SERIAL_GET_DTRRTS" }, + { IOCTL_SERIAL_GET_COMMSTATUS, "IOCTL_SERIAL_GET_COMMSTATUS" }, + { IOCTL_SERIAL_GET_PROPERTIES, "IOCTL_SERIAL_GET_PROPERTIES" }, + // {IOCTL_SERIAL_XOFF_COUNTER, "IOCTL_SERIAL_XOFF_COUNTER"}, + // {IOCTL_SERIAL_LSRMST_INSERT, "IOCTL_SERIAL_LSRMST_INSERT"}, + { IOCTL_SERIAL_CONFIG_SIZE, "IOCTL_SERIAL_CONFIG_SIZE" }, + // {IOCTL_SERIAL_GET_STATS, "IOCTL_SERIAL_GET_STATS"}, + // {IOCTL_SERIAL_CLEAR_STATS, "IOCTL_SERIAL_CLEAR_STATS"}, + // {IOCTL_SERIAL_GET_MODEM_CONTROL,"IOCTL_SERIAL_GET_MODEM_CONTROL"}, + // {IOCTL_SERIAL_SET_MODEM_CONTROL,"IOCTL_SERIAL_SET_MODEM_CONTROL"}, + // {IOCTL_SERIAL_SET_FIFO_CONTROL, "IOCTL_SERIAL_SET_FIFO_CONTROL"}, + // {IOCTL_PAR_QUERY_INFORMATION, "IOCTL_PAR_QUERY_INFORMATION"}, + // {IOCTL_PAR_SET_INFORMATION, "IOCTL_PAR_SET_INFORMATION"}, + // {IOCTL_PAR_QUERY_DEVICE_ID, "IOCTL_PAR_QUERY_DEVICE_ID"}, + // {IOCTL_PAR_QUERY_DEVICE_ID_SIZE,"IOCTL_PAR_QUERY_DEVICE_ID_SIZE"}, + // {IOCTL_IEEE1284_GET_MODE, "IOCTL_IEEE1284_GET_MODE"}, + // {IOCTL_IEEE1284_NEGOTIATE, "IOCTL_IEEE1284_NEGOTIATE"}, + // {IOCTL_PAR_SET_WRITE_ADDRESS, "IOCTL_PAR_SET_WRITE_ADDRESS"}, + // {IOCTL_PAR_SET_READ_ADDRESS, "IOCTL_PAR_SET_READ_ADDRESS"}, + // {IOCTL_PAR_GET_DEVICE_CAPS, "IOCTL_PAR_GET_DEVICE_CAPS"}, + // {IOCTL_PAR_GET_DEFAULT_MODES, "IOCTL_PAR_GET_DEFAULT_MODES"}, + // {IOCTL_PAR_QUERY_RAW_DEVICE_ID, "IOCTL_PAR_QUERY_RAW_DEVICE_ID"}, + // {IOCTL_PAR_IS_PORT_FREE, "IOCTL_PAR_IS_PORT_FREE"}, + + { IOCTL_USBPRINT_GET_1284_ID, "IOCTL_USBPRINT_GET_1284_ID" } +}; const char* _comm_serial_ioctl_name(ULONG number) { - for (int i = 0; _SERIAL_IOCTL_NAMES[i].number != 0; i++) + for (size_t x = 0; x < ARRAYSIZE(S_SERIAL_IOCTL_NAMES); x++) { - if (_SERIAL_IOCTL_NAMES[i].number == number) - { - return _SERIAL_IOCTL_NAMES[i].name; - } + const _SERIAL_IOCTL_NAME* const cur = &S_SERIAL_IOCTL_NAMES[x]; + if (cur->number == number) + return cur->name; } return "(unknown ioctl name)"; @@ -69,7 +119,7 @@ static BOOL s_CommDeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped) { WINPR_COMM* pComm = (WINPR_COMM*)hDevice; - SERIAL_DRIVER* pServerSerialDriver = NULL; + const SERIAL_DRIVER* pServerSerialDriver = NULL; if (!CommIsHandleValid(hDevice)) return FALSE; @@ -727,5 +777,3 @@ int _comm_ioctl_tcsetattr(int fd, int optional_actions, const struct termios* te return 0; } - -#endif /* __linux__ */ diff --git a/winpr/libwinpr/comm/comm_ioctl.h b/winpr/libwinpr/comm/comm_ioctl.h index 6467a4313..11061dc70 100644 --- a/winpr/libwinpr/comm/comm_ioctl.h +++ b/winpr/libwinpr/comm/comm_ioctl.h @@ -22,8 +22,6 @@ #ifndef WINPR_COMM_IOCTL_H_ #define WINPR_COMM_IOCTL_H_ -#if defined __linux__ && !defined ANDROID - #include #include @@ -231,6 +229,4 @@ extern "C" } #endif -#endif /* __linux__ */ - #endif /* WINPR_COMM_IOCTL_H_ */ diff --git a/winpr/libwinpr/comm/comm_sercx2_sys.c b/winpr/libwinpr/comm/comm_sercx2_sys.c index 5726c1b89..983cb5e39 100644 --- a/winpr/libwinpr/comm/comm_sercx2_sys.c +++ b/winpr/libwinpr/comm/comm_sercx2_sys.c @@ -160,11 +160,11 @@ static SERIAL_DRIVER SerCx2Sys = { .reset_device = NULL, /* not supported by SerCx2.sys */ }; -SERIAL_DRIVER* SerCx2Sys_s(void) +const SERIAL_DRIVER* SerCx2Sys_s(void) { /* SerCx2Sys completed with inherited functions from SerialSys or SerCxSys */ - SERIAL_DRIVER* pSerialSys = SerialSys_s(); - SERIAL_DRIVER* pSerCxSys = SerCxSys_s(); + const SERIAL_DRIVER* pSerialSys = SerialSys_s(); + const SERIAL_DRIVER* pSerCxSys = SerCxSys_s(); if (!pSerialSys || !pSerCxSys) return NULL; diff --git a/winpr/libwinpr/comm/comm_sercx2_sys.h b/winpr/libwinpr/comm/comm_sercx2_sys.h index a2906538a..e2feb2a2f 100644 --- a/winpr/libwinpr/comm/comm_sercx2_sys.h +++ b/winpr/libwinpr/comm/comm_sercx2_sys.h @@ -20,8 +20,6 @@ #ifndef COMM_SERCX2_SYS_H #define COMM_SERCX2_SYS_H -#if defined __linux__ && !defined ANDROID - #include "comm_ioctl.h" #ifdef __cplusplus @@ -29,12 +27,10 @@ extern "C" { #endif - SERIAL_DRIVER* SerCx2Sys_s(void); + const SERIAL_DRIVER* SerCx2Sys_s(void); #ifdef __cplusplus } #endif -#endif /* __linux__ */ - #endif /* COMM_SERCX2_SYS_H */ diff --git a/winpr/libwinpr/comm/comm_sercx_sys.c b/winpr/libwinpr/comm/comm_sercx_sys.c index 805ef76c8..7b345d659 100644 --- a/winpr/libwinpr/comm/comm_sercx_sys.c +++ b/winpr/libwinpr/comm/comm_sercx_sys.c @@ -20,8 +20,6 @@ * limitations under the License. */ -#if defined __linux__ && !defined ANDROID - #include #include @@ -212,10 +210,10 @@ static SERIAL_DRIVER SerCxSys = { .reset_device = NULL, /* not supported by SerCx.sys */ }; -SERIAL_DRIVER* SerCxSys_s(void) +const SERIAL_DRIVER* SerCxSys_s(void) { /* _SerCxSys completed with inherited functions from SerialSys */ - SERIAL_DRIVER* pSerialSys = SerialSys_s(); + const SERIAL_DRIVER* pSerialSys = SerialSys_s(); if (!pSerialSys) return NULL; @@ -262,5 +260,3 @@ SERIAL_DRIVER* SerCxSys_s(void) return &SerCxSys; } - -#endif /* __linux__ */ diff --git a/winpr/libwinpr/comm/comm_sercx_sys.h b/winpr/libwinpr/comm/comm_sercx_sys.h index 2268c0cc0..0ab6f1738 100644 --- a/winpr/libwinpr/comm/comm_sercx_sys.h +++ b/winpr/libwinpr/comm/comm_sercx_sys.h @@ -20,8 +20,6 @@ #ifndef COMM_SERCX_SYS_H #define COMM_SERCX_SYS_H -#if defined __linux__ && !defined ANDROID - #include "comm_ioctl.h" #ifdef __cplusplus @@ -29,12 +27,10 @@ extern "C" { #endif - SERIAL_DRIVER* SerCxSys_s(void); + const SERIAL_DRIVER* SerCxSys_s(void); #ifdef __cplusplus } #endif -#endif /* __linux__ */ - #endif /* COMM_SERCX_SYS_H */ diff --git a/winpr/libwinpr/comm/comm_serial_sys.c b/winpr/libwinpr/comm/comm_serial_sys.c index 92d30c675..d2e72faa8 100644 --- a/winpr/libwinpr/comm/comm_serial_sys.c +++ b/winpr/libwinpr/comm/comm_serial_sys.c @@ -28,9 +28,7 @@ #include #include "comm_serial_sys.h" -#ifdef __UCLIBC__ #include "comm.h" -#endif #include #include @@ -1085,7 +1083,7 @@ static BOOL set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask) if (*pWaitMask == 0) { /* clearing pending events */ - +#if defined(WINPR_HAVE_COMM_COUNTERS) if (ioctl(pComm->fd, TIOCGICOUNT, &(pComm->counters)) < 0) { if (!commstatus_error(pComm, "TIOCGICOUNT")) @@ -1095,7 +1093,7 @@ static BOOL set_wait_mask(WINPR_COMM* pComm, const ULONG* pWaitMask) } ZeroMemory(&(pComm->counters), sizeof(struct serial_icounter_struct)); } - +#endif pComm->PendingEvents = 0; } @@ -1177,7 +1175,7 @@ static BOOL purge(WINPR_COMM* pComm, const ULONG* pPurgeMask) if (*pPurgeMask & SERIAL_PURGE_TXABORT) { /* Purges all write (IRP_MJ_WRITE) requests. */ - +#if defined(WINPR_HAVE_SYS_EVENTFD_H) if (eventfd_write(pComm->fd_write_event, WINPR_PURGE_TXABORT) < 0) { if (errno != EAGAIN) @@ -1189,12 +1187,13 @@ static BOOL purge(WINPR_COMM* pComm, const ULONG* pPurgeMask) WINPR_ASSERT(errno == EAGAIN); /* no reader <=> no pending IRP_MJ_WRITE */ } +#endif } if (*pPurgeMask & SERIAL_PURGE_RXABORT) { /* Purges all read (IRP_MJ_READ) requests. */ - +#if defined(WINPR_HAVE_SYS_EVENTFD_H) if (eventfd_write(pComm->fd_read_event, WINPR_PURGE_RXABORT) < 0) { if (errno != EAGAIN) @@ -1206,6 +1205,7 @@ static BOOL purge(WINPR_COMM* pComm, const ULONG* pPurgeMask) WINPR_ASSERT(errno == EAGAIN); /* no reader <=> no pending IRP_MJ_READ */ } +#endif } if (*pPurgeMask & SERIAL_PURGE_TXCLEAR) @@ -1261,9 +1261,9 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) { BOOL rc = FALSE; /* http://msdn.microsoft.com/en-us/library/jj673022%28v=vs.85%29.aspx */ - +#if defined(WINPR_HAVE_COMM_COUNTERS) struct serial_icounter_struct currentCounters = { 0 }; - +#endif WINPR_ASSERT(pComm); WINPR_ASSERT(pCommstatus); @@ -1284,6 +1284,7 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) status = 0; } +#if defined(WINPR_HAVE_COMM_COUNTERS) if (ioctl(pComm->fd, TIOCGICOUNT, ¤tCounters) < 0) { if (!commstatus_error(pComm, "TIOCGICOUNT")) @@ -1324,6 +1325,7 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) pCommstatus->Errors |= SERIAL_ERROR_FRAMING; pComm->PendingEvents |= SERIAL_EV_ERR; } +#endif /* HoldReasons */ @@ -1341,11 +1343,13 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) /* AmountInInQueue */ +#if defined(__linux__) if (ioctl(pComm->fd, TIOCINQ, &(pCommstatus->AmountInInQueue)) < 0) { if (!commstatus_error(pComm, "TIOCINQ")) goto fail; } +#endif /* AmountInOutQueue */ @@ -1360,7 +1364,7 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) /* BOOLEAN WaitForImmediate; TODO: once IOCTL_SERIAL_IMMEDIATE_CHAR fully supported */ /* other events based on counters */ - +#if defined(WINPR_HAVE_COMM_COUNTERS) if (currentCounters.rx != pComm->counters.rx) { pComm->PendingEvents |= SERIAL_EV_RXFLAG | SERIAL_EV_RXCHAR; @@ -1398,6 +1402,9 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) pComm->PendingEvents |= SERIAL_EV_RING; } + pComm->counters = currentCounters; +#endif + if (pCommstatus->AmountInInQueue > (0.8 * N_TTY_BUF_SIZE)) { pComm->PendingEvents |= SERIAL_EV_RX80FULL; @@ -1409,8 +1416,6 @@ static BOOL get_commstatus(WINPR_COMM* pComm, SERIAL_STATUS* pCommstatus) pComm->PendingEvents &= ~SERIAL_EV_RX80FULL; } - pComm->counters = currentCounters; - rc = TRUE; fail: LeaveCriticalSection(&pComm->EventsLock); @@ -1639,7 +1644,7 @@ static BOOL reset_device(WINPR_COMM* pComm) return TRUE; } -static SERIAL_DRIVER SerialSys = { +static const SERIAL_DRIVER SerialSys = { .id = SerialDriverSerialSys, .name = _T("Serial.sys"), .set_baud_rate = set_baud_rate, @@ -1674,7 +1679,7 @@ static SERIAL_DRIVER SerialSys = { .reset_device = reset_device, }; -SERIAL_DRIVER* SerialSys_s(void) +const SERIAL_DRIVER* SerialSys_s(void) { return &SerialSys; } diff --git a/winpr/libwinpr/comm/comm_serial_sys.h b/winpr/libwinpr/comm/comm_serial_sys.h index abb3f01db..52b17fb20 100644 --- a/winpr/libwinpr/comm/comm_serial_sys.h +++ b/winpr/libwinpr/comm/comm_serial_sys.h @@ -20,8 +20,6 @@ #ifndef COMM_SERIAL_SYS_H #define COMM_SERIAL_SYS_H -#if defined __linux__ && !defined ANDROID - #include "comm_ioctl.h" #ifdef __cplusplus @@ -29,12 +27,10 @@ extern "C" { #endif - SERIAL_DRIVER* SerialSys_s(void); + const SERIAL_DRIVER* SerialSys_s(void); #ifdef __cplusplus } #endif -#endif /* __linux__ */ - #endif /* COMM_SERIAL_SYS_H */ diff --git a/winpr/libwinpr/file/generic.c b/winpr/libwinpr/file/generic.c index b0d2e170a..c2c354101 100644 --- a/winpr/libwinpr/file/generic.c +++ b/winpr/libwinpr/file/generic.c @@ -180,9 +180,7 @@ static pthread_once_t HandleCreatorsInitialized = PTHREAD_ONCE_INIT; extern HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void); -#if defined __linux__ && !defined ANDROID #include "../comm/comm.h" -#endif /* __linux__ && !defined ANDROID */ static void HandleCreatorsInit(void) { @@ -196,9 +194,9 @@ static void HandleCreatorsInit(void) * Register all file handle creators. */ ArrayList_Append(HandleCreators, GetNamedPipeClientHandleCreator()); -#if defined __linux__ && !defined ANDROID - ArrayList_Append(HandleCreators, GetCommHandleCreator()); -#endif /* __linux__ && !defined ANDROID */ + HANDLE_CREATOR* serial = GetCommHandleCreator(); + if (serial) + ArrayList_Append(HandleCreators, serial); ArrayList_Append(HandleCreators, GetFileHandleCreator()); }