Merge branch 'master' of github.com:FreeRDP/FreeRDP

Conflicts:
	libfreerdp/core/update.c
This commit is contained in:
Marc-André Moreau 2014-11-11 09:55:48 -05:00
commit a48ed7239d
26 changed files with 1518 additions and 379 deletions

View File

@ -44,7 +44,6 @@ include(CheckCmakeCompat)
include(CheckIncludeFiles)
include(CheckLibraryExists)
include(CheckStructHasMember)
include(CMakeDetermineSystem)
include(FindPkgConfig)
include(TestBigEndian)

View File

@ -1032,6 +1032,108 @@ static UINT32 smartcard_AccessStartedEvent_Call(SMARTCARD_DEVICE* smartcard, SMA
return status;
}
static UINT32 smartcard_LocateCardsByATRA_Decode(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation, LocateCardsByATRA_Call* call)
{
LONG status;
IRP* irp = operation->irp;
if (!call)
return STATUS_NO_MEMORY;
status = smartcard_unpack_locate_cards_by_atr_a_call(smartcard, irp->input, call);
smartcard_trace_locate_cards_by_atr_a_call(smartcard, call);
operation->hContext = smartcard_scard_context_native_from_redir(smartcard, &(call->hContext));
return status;
}
static UINT32 smartcard_LocateCardsByATRA_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation, LocateCardsByATRA_Call* call)
{
LONG status;
DWORD index, index2, index3;
BOOL equal;
GetStatusChange_Return ret;
LPSCARD_READERSTATEA rgReaderState2 = NULL;
LPSCARD_READERSTATEA states = NULL;
IRP* irp = operation->irp;
states = calloc(call->cReaders, sizeof(SCARD_READERSTATEA));
for (index = 0; index < call->cReaders; index++)
{
states[index].szReader = (LPCSTR) call->rgReaderStates[index].szReader;
states[index].dwCurrentState = call->rgReaderStates[index].Common.dwCurrentState;
states[index].dwEventState = call->rgReaderStates[index].Common.dwEventState;
states[index].cbAtr = call->rgReaderStates[index].Common.cbAtr;
CopyMemory(&(states[index].rgbAtr), &(call->rgReaderStates[index].Common.rgbAtr), 36);
}
status = ret.ReturnCode = SCardGetStatusChangeA(operation->hContext, 0x000001F4, states, call->cReaders);
if (status && (status != SCARD_E_TIMEOUT) && (status != SCARD_E_CANCELLED))
{
call->cReaders=0;
}
for (index = 0; index < call->cAtrs; index++)
{
for (index2 = 0; index2 < call->cReaders; index2++)
{
equal = TRUE;
for (index3 = 0; index3 < call->rgAtrMasks[index].cbAtr; index3++)
{
if ((call->rgAtrMasks[index].rgbAtr[index3] & call->rgAtrMasks[index].rgbMask[index3]) !=
(states[index2].rgbAtr[index3] & call->rgAtrMasks[index].rgbMask[index3]))
{
equal = FALSE;
break;
}
if (equal)
{
states[index2].dwEventState |= SCARD_STATE_ATRMATCH;
}
}
}
}
ret.cReaders = call->cReaders;
ret.rgReaderStates = (ReaderState_Return*) calloc(ret.cReaders, sizeof(ReaderState_Return));
for (index = 0; index < ret.cReaders; index++)
{
rgReaderState2 = &states[index];
ret.rgReaderStates[index].dwCurrentState = rgReaderState2->dwCurrentState;
ret.rgReaderStates[index].dwEventState = rgReaderState2->dwEventState;
ret.rgReaderStates[index].cbAtr = rgReaderState2->cbAtr;
CopyMemory(&(ret.rgReaderStates[index].rgbAtr), &(rgReaderState2->rgbAtr), 32);
}
free(states);
smartcard_trace_get_status_change_return(smartcard, &ret, FALSE);
status = smartcard_pack_get_status_change_return(smartcard, irp->output, &ret);
if (status)
return status;
if (call->rgReaderStates)
{
for (index = 0; index < call->cReaders; index++)
{
rgReaderState2 = (LPSCARD_READERSTATEA) &call->rgReaderStates[index];
if (rgReaderState2->szReader) {
free((void*) rgReaderState2->szReader);
rgReaderState2->szReader = NULL;
}
}
free(call->rgReaderStates);
call->rgReaderStates = NULL;
}
free(ret.rgReaderStates);
return ret.ReturnCode;
}
UINT32 smartcard_irp_device_control_decode(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation)
{
UINT32 status;
@ -1264,7 +1366,8 @@ UINT32 smartcard_irp_device_control_decode(SMARTCARD_DEVICE* smartcard, SMARTCAR
break;
case SCARD_IOCTL_LOCATECARDSBYATRA:
status = SCARD_F_INTERNAL_ERROR;
call = calloc(1, sizeof(LocateCardsByATRA_Call));
status = smartcard_LocateCardsByATRA_Decode(smartcard, operation, (LocateCardsByATRA_Call*) call);
break;
case SCARD_IOCTL_LOCATECARDSBYATRW:
@ -1530,7 +1633,7 @@ UINT32 smartcard_irp_device_control_call(SMARTCARD_DEVICE* smartcard, SMARTCARD_
break;
case SCARD_IOCTL_LOCATECARDSBYATRA:
result = SCARD_F_INTERNAL_ERROR;
result = smartcard_LocateCardsByATRA_Call(smartcard, operation, (LocateCardsByATRA_Call*) call);
break;
case SCARD_IOCTL_LOCATECARDSBYATRW:

View File

@ -2471,3 +2471,240 @@ void smartcard_trace_transmit_return(SMARTCARD_DEVICE* smartcard, Transmit_Retur
WLog_Print(smartcard->log, WLOG_DEBUG, "}");
}
UINT32 smartcard_unpack_locate_cards_by_atr_a_call(SMARTCARD_DEVICE* smartcard, wStream* s, LocateCardsByATRA_Call* call)
{
UINT32 index;
UINT32 count;
UINT32 status;
UINT32 offset;
UINT32 maxCount;
UINT32 szReaderNdrPtr;
UINT32 rgReaderStatesNdrPtr;
UINT32 rgAtrMasksNdrPtr;
LPSCARD_READERSTATEA readerState;
call->rgReaderStates = NULL;
status = smartcard_unpack_redir_scard_context(smartcard, s, &(call->hContext));
if (status)
return status;
if (Stream_GetRemainingLength(s) < 16)
{
WLog_Print(smartcard->log, WLOG_WARN, "LocateCardsByATRA_Call is too short: %d",
(int) Stream_GetRemainingLength(s));
return STATUS_BUFFER_TOO_SMALL;
}
Stream_Read_UINT32(s, call->cAtrs);
Stream_Read_UINT32(s, rgAtrMasksNdrPtr);
Stream_Read_UINT32(s, call->cReaders); /* cReaders (4 bytes) */
Stream_Read_UINT32(s, rgReaderStatesNdrPtr); /* rgReaderStatesNdrPtr (4 bytes) */
status = smartcard_unpack_redir_scard_context_ref(smartcard, s, &(call->hContext));
if (status)
return status;
if (Stream_GetRemainingLength(s) < 4)
{
WLog_Print(smartcard->log, WLOG_WARN, "LocateCardsByATRA_Call is too short: %d",
(int) Stream_GetRemainingLength(s));
return STATUS_BUFFER_TOO_SMALL;
}
if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
{
WLog_Print(smartcard->log, WLOG_WARN,
"LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08X) and cAtrs (0x%08X) inconsistency",
(int) rgAtrMasksNdrPtr, (int) call->cAtrs);
return STATUS_INVALID_PARAMETER;
}
if (rgAtrMasksNdrPtr)
{
Stream_Read_UINT32(s, count);
if (count != call->cAtrs)
{
WLog_Print(smartcard->log, WLOG_WARN,
"LocateCardsByATRA_Call NdrCount (0x%08X) and cAtrs (0x%08X) inconsistency",
(int) count, (int) call->cAtrs);
return STATUS_INVALID_PARAMETER;
}
if (Stream_GetRemainingLength(s) < call->cAtrs)
{
WLog_Print(smartcard->log, WLOG_WARN, "LocateCardsByATRA_Call is too short: Actual: %d, Expected: %d",
(int) Stream_GetRemainingLength(s), call->cAtrs);
return STATUS_BUFFER_TOO_SMALL;
}
call->rgAtrMasks = calloc(call->cAtrs, sizeof(SCARD_ATRMASK));
if (!call->rgAtrMasks)
{
WLog_Print(smartcard->log, WLOG_WARN, "LocateCardsByATRA_Call out of memory error (call->rgAtrMasks)");
return STATUS_NO_MEMORY;
}
for (index = 0; index < call->cAtrs; index++)
{
Stream_Read_UINT32(s, call->rgAtrMasks[index].cbAtr);
Stream_Read(s, call->rgAtrMasks[index].rgbAtr, 36);
Stream_Read(s, call->rgAtrMasks[index].rgbMask, 36);
}
}
Stream_Read_UINT32(s, count);
if (count != call->cReaders)
{
WLog_Print(smartcard->log, WLOG_WARN,
"GetStatusChangeA_Call unexpected reader count: Actual: %d, Expected: %d",
(int) count, call->cReaders);
return STATUS_INVALID_PARAMETER;
}
if (call->cReaders > 0)
{
call->rgReaderStates = calloc(call->cReaders, sizeof(SCARD_READERSTATEA));
if (!call->rgReaderStates)
{
WLog_Print(smartcard->log, WLOG_WARN, "LocateCardsByATRA_Call out of memory error (call->rgReaderStates)");
return STATUS_NO_MEMORY;
}
for (index = 0; index < call->cReaders; index++)
{
readerState = (LPSCARD_READERSTATEA) &call->rgReaderStates[index];
if (Stream_GetRemainingLength(s) < 52)
{
WLog_Print(smartcard->log, WLOG_WARN, "LocateCardsByATRA_Call is too short: %d",
(int) Stream_GetRemainingLength(s));
return STATUS_BUFFER_TOO_SMALL;
}
Stream_Read_UINT32(s, szReaderNdrPtr); /* szReaderNdrPtr (4 bytes) */
Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
Stream_Read_UINT32(s, readerState->dwEventState); /* dwEventState (4 bytes) */
Stream_Read_UINT32(s, readerState->cbAtr); /* cbAtr (4 bytes) */
Stream_Read(s, readerState->rgbAtr, 32); /* rgbAtr [0..32] (32 bytes) */
Stream_Seek(s, 4); /* rgbAtr [32..36] (4 bytes) */
}
for (index = 0; index < call->cReaders; index++)
{
readerState = (LPSCARD_READERSTATEA) &call->rgReaderStates[index];
if (Stream_GetRemainingLength(s) < 12)
{
WLog_Print(smartcard->log, WLOG_WARN, "GetStatusChangeA_Call is too short: %d",
(int) Stream_GetRemainingLength(s));
return STATUS_BUFFER_TOO_SMALL;
}
Stream_Read_UINT32(s, maxCount); /* NdrMaxCount (4 bytes) */
Stream_Read_UINT32(s, offset); /* NdrOffset (4 bytes) */
Stream_Read_UINT32(s, count); /* NdrActualCount (4 bytes) */
if (Stream_GetRemainingLength(s) < count)
{
WLog_Print(smartcard->log, WLOG_WARN, "GetStatusChangeA_Call is too short: %d",
(int) Stream_GetRemainingLength(s));
return STATUS_BUFFER_TOO_SMALL;
}
readerState->szReader = (LPCSTR) malloc(count + 1);
if (!readerState->szReader)
{
WLog_Print(smartcard->log, WLOG_WARN,
"GetStatusChangeA_Call out of memory error (readerState->szReader)");
return STATUS_NO_MEMORY;
}
Stream_Read(s, (void*) readerState->szReader, count);
smartcard_unpack_read_size_align(smartcard, s, count, 4);
((char*) readerState->szReader)[count] = '\0';
if (!readerState->szReader)
{
WLog_Print(smartcard->log, WLOG_WARN, "GetStatusChangeA_Call null reader name");
return STATUS_INVALID_PARAMETER;
}
}
}
return SCARD_S_SUCCESS;
}
void smartcard_trace_locate_cards_by_atr_a_call(SMARTCARD_DEVICE* smartcard, LocateCardsByATRA_Call* call)
{
BYTE* pb;
UINT32 index;
char* szEventState;
char* szCurrentState;
char* rgbAtr;
LPSCARD_READERSTATEA readerState;
if (!WLog_IsLevelActive(smartcard->log, WLOG_DEBUG))
return;
WLog_Print(smartcard->log, WLOG_DEBUG, "LocateCardsByATRA_Call {");
pb = (BYTE*) &(call->hContext.pbContext);
if (call->hContext.cbContext > 4)
{
WLog_Print(smartcard->log, WLOG_DEBUG, "hContext: 0x%02X%02X%02X%02X%02X%02X%02X%02X (%d)",
pb[0], pb[1], pb[2], pb[3], pb[4], pb[5], pb[6], pb[7], call->hContext.cbContext);
}
else
{
WLog_Print(smartcard->log, WLOG_DEBUG, "hContext: 0x%02X%02X%02X%02X (%d)",
pb[0], pb[1], pb[2], pb[3], call->hContext.cbContext);
}
for (index = 0; index < call->cReaders; index++)
{
readerState = (LPSCARD_READERSTATEA) &call->rgReaderStates[index];
WLog_Print(smartcard->log, WLOG_DEBUG,
"\t[%d]: szReader: %s cbAtr: %d",
index, readerState->szReader, readerState->cbAtr);
szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
szEventState = SCardGetReaderStateString(readerState->dwEventState);
rgbAtr = winpr_BinToHexString((BYTE*) &(readerState->rgbAtr), readerState->cbAtr, FALSE);
WLog_Print(smartcard->log, WLOG_DEBUG,
"\t[%d]: dwCurrentState: %s (0x%08X)",
index, szCurrentState, readerState->dwCurrentState);
WLog_Print(smartcard->log, WLOG_DEBUG,
"\t[%d]: dwEventState: %s (0x%08X)",
index, szEventState, readerState->dwEventState);
if (rgbAtr)
{
WLog_Print(smartcard->log, WLOG_DEBUG,
"\t[%d]: cbAtr: %d rgbAtr: %s",
index, readerState->cbAtr, rgbAtr);
}
else
{
WLog_Print(smartcard->log, WLOG_DEBUG,
"\t[%d]: cbAtr: %d rgbAtr: %s",
index, 0, "");
}
free(szCurrentState);
free(szEventState);
free(rgbAtr);
}
WLog_Print(smartcard->log, WLOG_DEBUG, "}");
}

View File

@ -532,4 +532,8 @@ void smartcard_trace_transmit_call(SMARTCARD_DEVICE* smartcard, Transmit_Call* c
UINT32 smartcard_pack_transmit_return(SMARTCARD_DEVICE* smartcard, wStream* s, Transmit_Return* ret);
void smartcard_trace_transmit_return(SMARTCARD_DEVICE* smartcard, Transmit_Return* ret);
UINT32 smartcard_unpack_locate_cards_by_atr_a_call(SMARTCARD_DEVICE* smartcard, wStream* s, LocateCardsByATRA_Call* call);
void smartcard_trace_locate_cards_by_atr_a_call(SMARTCARD_DEVICE* smartcard, LocateCardsByATRA_Call* call);
#endif /* FREERDP_CHANNEL_SMARTCARD_CLIENT_PACK_H */

View File

@ -109,6 +109,7 @@ option(WITH_DEBUG_SCARD "Print smartcard debug messages" ${DEFAULT_DEBUG_OPTION}
option(WITH_DEBUG_SND "Print rdpsnd debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_SVC "Print static virtual channel debug messages." ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_TRANSPORT "Print transport debug messages." ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_THREADS "Print thread debug messages, enables handle dump" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_TIMEZONE "Print timezone debug messages." ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_WND "Print window order debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_X11_CLIPRDR "Print X11 clipboard redirection debug messages" ${DEFAULT_DEBUG_OPTION})

View File

@ -83,6 +83,7 @@
#cmakedefine WITH_DEBUG_SVC
#cmakedefine WITH_DEBUG_RDPEI
#cmakedefine WITH_DEBUG_TIMEZONE
#cmakedefine WITH_DEBUG_THREADS
#cmakedefine WITH_DEBUG_TRANSPORT
#cmakedefine WITH_DEBUG_WND
#cmakedefine WITH_DEBUG_X11

View File

@ -43,7 +43,8 @@ struct rdp_autodetect
ALIGN64 UINT32 netCharBandwidth; /* 5 */
ALIGN64 UINT32 netCharBaseRTT; /* 6 */
ALIGN64 UINT32 netCharAverageRTT; /* 7 */
UINT64 paddingA[16 - 8]; /* 8 */
ALIGN64 BOOL bandwidthMeasureStarted; /* 8 */
UINT64 paddingA[16 - 9]; /* 9 */
ALIGN64 pRTTMeasureRequest RTTMeasureRequest; /* 16 */
ALIGN64 pRTTMeasureResponse RTTMeasureResponse; /* 17 */

View File

@ -68,6 +68,7 @@ struct _BITMAP_UPDATE
UINT32 count;
UINT32 number;
BITMAP_DATA* rectangles;
BOOL skipCompression;
};
typedef struct _BITMAP_UPDATE BITMAP_UPDATE;
@ -104,6 +105,7 @@ struct _SURFACE_BITS_COMMAND
UINT32 height;
UINT32 bitmapDataLength;
BYTE* bitmapData;
BOOL skipCompression;
};
typedef struct _SURFACE_BITS_COMMAND SURFACE_BITS_COMMAND;

View File

@ -21,10 +21,26 @@
#include "config.h"
#endif
//#define WITH_DEBUG_AUTODETECT
#include "autodetect.h"
#define RDP_RTT_REQUEST_TYPE_CONTINUOUS 0x0001
#define RDP_RTT_REQUEST_TYPE_CONNECTTIME 0x1001
#define RDP_RTT_RESPONSE_TYPE 0x0000
#define RDP_BW_START_REQUEST_TYPE_CONTINUOUS 0x0014
#define RDP_BW_START_REQUEST_TYPE_TUNNEL 0x0114
#define RDP_BW_START_REQUEST_TYPE_CONNECTTIME 0x1014
#define RDP_BW_PAYLOAD_REQUEST_TYPE 0x0002
#define RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME 0x002B
#define RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS 0x0429
#define RDP_BW_STOP_REQUEST_TYPE_TUNNEL 0x0629
#define RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME 0x0003
#define RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS 0x000B
#define RDP_NETCHAR_SYNC_RESPONSE_TYPE 0x0018
typedef struct
{
UINT8 headerLength;
@ -64,12 +80,12 @@ static BOOL autodetect_send_rtt_measure_request(rdpContext* context, UINT16 sequ
static BOOL autodetect_send_continuous_rtt_measure_request(rdpContext* context, UINT16 sequenceNumber)
{
return autodetect_send_rtt_measure_request(context, sequenceNumber, 0x0001);
return autodetect_send_rtt_measure_request(context, sequenceNumber, RDP_RTT_REQUEST_TYPE_CONTINUOUS);
}
BOOL autodetect_send_connecttime_rtt_measure_request(rdpContext* context, UINT16 sequenceNumber)
{
return autodetect_send_rtt_measure_request(context, sequenceNumber, 0x1001);
return autodetect_send_rtt_measure_request(context, sequenceNumber, RDP_RTT_REQUEST_TYPE_CONNECTTIME);
}
static BOOL autodetect_send_rtt_measure_response(rdpRdp* rdp, UINT16 sequenceNumber)
@ -88,7 +104,7 @@ static BOOL autodetect_send_rtt_measure_response(rdpRdp* rdp, UINT16 sequenceNum
Stream_Write_UINT8(s, 0x06); /* headerLength (1 byte) */
Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
Stream_Write_UINT16(s, 0x0000); /* responseType (1 byte) */
Stream_Write_UINT16(s, RDP_RTT_RESPONSE_TYPE); /* responseType (1 byte) */
return rdp_send_message_channel_pdu(rdp, s, SEC_AUTODETECT_RSP);
}
@ -114,12 +130,12 @@ static BOOL autodetect_send_bandwidth_measure_start(rdpContext* context, UINT16
static BOOL autodetect_send_continuous_bandwidth_measure_start(rdpContext* context, UINT16 sequenceNumber)
{
return autodetect_send_bandwidth_measure_start(context, sequenceNumber, 0x0014);
return autodetect_send_bandwidth_measure_start(context, sequenceNumber, RDP_BW_START_REQUEST_TYPE_CONTINUOUS);
}
BOOL autodetect_send_connecttime_bandwidth_measure_start(rdpContext* context, UINT16 sequenceNumber)
{
return autodetect_send_bandwidth_measure_start(context, sequenceNumber, 0x1014);
return autodetect_send_bandwidth_measure_start(context, sequenceNumber, RDP_BW_START_REQUEST_TYPE_CONNECTTIME);
}
BOOL autodetect_send_bandwidth_measure_payload(rdpContext* context, UINT16 payloadLength, UINT16 sequenceNumber)
@ -140,7 +156,7 @@ BOOL autodetect_send_bandwidth_measure_payload(rdpContext* context, UINT16 paylo
Stream_Write_UINT8(s, 0x08); /* headerLength (1 byte) */
Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
Stream_Write_UINT16(s, 0x0002); /* requestType (2 bytes) */
Stream_Write_UINT16(s, RDP_BW_PAYLOAD_REQUEST_TYPE); /* requestType (2 bytes) */
Stream_Write_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
Stream_EnsureRemainingCapacity(s, payloadLength);
/* Random data (better measurement in case the line is compressed) */
@ -167,11 +183,11 @@ static BOOL autodetect_send_bandwidth_measure_stop(rdpContext* context, UINT16 p
/* 4-bytes aligned */
payloadLength &= ~3;
Stream_Write_UINT8(s, requestType == 0x002B ? 0x08 : 0x06); /* headerLength (1 byte) */
Stream_Write_UINT8(s, requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME ? 0x08 : 0x06); /* headerLength (1 byte) */
Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
Stream_Write_UINT16(s, requestType); /* requestType (2 bytes) */
if (requestType == 0x002B)
if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
{
Stream_Write_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
if (payloadLength > 0)
@ -190,12 +206,12 @@ static BOOL autodetect_send_bandwidth_measure_stop(rdpContext* context, UINT16 p
static BOOL autodetect_send_continuous_bandwidth_measure_stop(rdpContext* context, UINT16 sequenceNumber)
{
return autodetect_send_bandwidth_measure_stop(context, 0, sequenceNumber, 0x0429);
return autodetect_send_bandwidth_measure_stop(context, 0, sequenceNumber, RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS);
}
BOOL autodetect_send_connecttime_bandwidth_measure_stop(rdpContext* context, UINT16 payloadLength, UINT16 sequenceNumber)
{
return autodetect_send_bandwidth_measure_stop(context, payloadLength, sequenceNumber, 0x002B);
return autodetect_send_bandwidth_measure_stop(context, payloadLength, sequenceNumber, RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME);
}
static BOOL autodetect_send_bandwidth_measure_results(rdpRdp* rdp, UINT16 responseType, UINT16 sequenceNumber)
@ -275,7 +291,7 @@ BOOL autodetect_send_netchar_sync(rdpRdp* rdp, UINT16 sequenceNumber)
Stream_Write_UINT8(s, 0x0E); /* headerLength (1 byte) */
Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
Stream_Write_UINT16(s, 0x0018); /* responseType (1 byte) */
Stream_Write_UINT16(s, RDP_NETCHAR_SYNC_RESPONSE_TYPE); /* responseType (1 byte) */
Stream_Write_UINT32(s, rdp->autodetect->netCharBandwidth); /* bandwidth (4 bytes) */
Stream_Write_UINT32(s, rdp->autodetect->netCharAverageRTT); /* rtt (4 bytes) */
@ -322,6 +338,12 @@ static BOOL autodetect_recv_bandwidth_measure_start(rdpRdp* rdp, wStream* s, AUT
rdp->autodetect->bandwidthMeasureStartTime = GetTickCount();
rdp->autodetect->bandwidthMeasureByteCount = 0;
/* Continuous Auto-Detection: mark the start of the measurement */
if (autodetectReqPdu->requestType == RDP_BW_START_REQUEST_TYPE_CONTINUOUS)
{
rdp->autodetect->bandwidthMeasureStarted = TRUE;
}
return TRUE;
}
@ -350,7 +372,7 @@ static BOOL autodetect_recv_bandwidth_measure_stop(rdpRdp* rdp, wStream* s, AUTO
UINT16 payloadLength;
UINT16 responseType;
if (autodetectReqPdu->requestType == 0x002B)
if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
{
if (autodetectReqPdu->headerLength != 0x08)
return FALSE;
@ -373,8 +395,15 @@ static BOOL autodetect_recv_bandwidth_measure_stop(rdpRdp* rdp, wStream* s, AUTO
/* Add the payload length to the bandwidth measurement parameters */
rdp->autodetect->bandwidthMeasureByteCount += payloadLength;
/* Continuous Auto-Detection: mark the stop of the measurement */
if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
{
rdp->autodetect->bandwidthMeasureStarted = FALSE;
}
/* Send a response the server */
responseType = autodetectReqPdu->requestType == 0x002B ? 0x0003 : 0x000B;
responseType = autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME ?
RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME : RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS;
return autodetect_send_bandwidth_measure_results(rdp, responseType, autodetectReqPdu->sequenceNumber);
}
@ -463,27 +492,27 @@ int rdp_recv_autodetect_request_packet(rdpRdp* rdp, wStream* s)
switch (autodetectReqPdu.requestType)
{
case 0x0001:
case 0x1001:
case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
/* RTT Measure Request (RDP_RTT_REQUEST) - MS-RDPBCGR 2.2.14.1.1 */
success = autodetect_recv_rtt_measure_request(rdp, s, &autodetectReqPdu);
break;
case 0x0014:
case 0x0114:
case 0x1014:
case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
case RDP_BW_START_REQUEST_TYPE_TUNNEL:
case RDP_BW_START_REQUEST_TYPE_CONNECTTIME:
/* Bandwidth Measure Start (RDP_BW_START) - MS-RDPBCGR 2.2.14.1.2 */
success = autodetect_recv_bandwidth_measure_start(rdp, s, &autodetectReqPdu);
break;
case 0x0002:
case RDP_BW_PAYLOAD_REQUEST_TYPE:
/* Bandwidth Measure Payload (RDP_BW_PAYLOAD) - MS-RDPBCGR 2.2.14.1.3 */
success = autodetect_recv_bandwidth_measure_payload(rdp, s, &autodetectReqPdu);
break;
case 0x002B:
case 0x0429:
case 0x0629:
case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
/* Bandwidth Measure Stop (RDP_BW_STOP) - MS-RDPBCGR 2.2.14.1.4 */
success = autodetect_recv_bandwidth_measure_stop(rdp, s, &autodetectReqPdu);
break;
@ -525,13 +554,13 @@ int rdp_recv_autodetect_response_packet(rdpRdp* rdp, wStream* s)
switch (autodetectRspPdu.responseType)
{
case 0x0000:
case RDP_RTT_RESPONSE_TYPE:
/* RTT Measure Response (RDP_RTT_RESPONSE) - MS-RDPBCGR 2.2.14.2.1 */
success = autodetect_recv_rtt_measure_response(rdp, s, &autodetectRspPdu);
break;
case 0x0003:
case 0x000B:
case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
/* Bandwidth Measure Results (RDP_BW_RESULTS) - MS-RDPBCGR 2.2.14.2.2 */
success = autodetect_recv_bandwidth_measure_results(rdp, s, &autodetectRspPdu);
break;

View File

@ -865,7 +865,7 @@ wStream* fastpath_update_pdu_init_new(rdpFastPath* fastpath)
return s;
}
BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s)
BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s, BOOL skipCompression)
{
int fragment;
UINT16 maxLength;
@ -886,7 +886,7 @@ BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s
maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
if (settings->CompressionEnabled)
if (settings->CompressionEnabled && !skipCompression)
{
CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
@ -955,7 +955,7 @@ BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s
if (rdp->sec_flags & SEC_SECURE_CHECKSUM)
fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
if (settings->CompressionEnabled)
if (settings->CompressionEnabled && !skipCompression)
{
if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize, &compressionFlags) >= 0)
{
@ -1054,18 +1054,21 @@ rdpFastPath* fastpath_new(rdpRdp* rdp)
{
rdpFastPath* fastpath;
fastpath = (rdpFastPath*) malloc(sizeof(rdpFastPath));
fastpath = (rdpFastPath*) calloc(1, sizeof(rdpFastPath));
if (!fastpath)
return NULL;
if (fastpath)
{
ZeroMemory(fastpath, sizeof(rdpFastPath));
fastpath->rdp = rdp;
fastpath->fragmentation = -1;
fastpath->fs = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
}
fastpath->rdp = rdp;
fastpath->fragmentation = -1;
fastpath->fs = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
if (!fastpath->fs)
goto out_free;
return fastpath;
out_free:
free(fastpath);
return NULL;
}
void fastpath_free(rdpFastPath* fastpath)

View File

@ -161,7 +161,7 @@ BOOL fastpath_send_input_pdu(rdpFastPath* fastpath, wStream* s);
wStream* fastpath_update_pdu_init(rdpFastPath* fastpath);
wStream* fastpath_update_pdu_init_new(rdpFastPath* fastpath);
BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s);
BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s, BOOL skipCompression);
BOOL fastpath_send_surfcmd_frame_marker(rdpFastPath* fastpath, UINT16 frameAction, UINT32 frameId);

View File

@ -379,6 +379,11 @@ static int peer_recv_tpkt_pdu(freerdp_peer* client, wStream* s)
return -1;
break;
case PDU_TYPE_FLOW_RESPONSE:
case PDU_TYPE_FLOW_STOP:
case PDU_TYPE_FLOW_TEST:
break;
default:
WLog_ERR(TAG, "Client sent pduType %d", pduType);
return -1;

View File

@ -112,6 +112,15 @@ BOOL rdp_read_share_control_header(wStream* s, UINT16* length, UINT16* type, UIN
/* Share Control Header */
Stream_Read_UINT16(s, *length); /* totalLength */
/* If length is 0x8000 then we actually got a flow control PDU that we should ignore
http://msdn.microsoft.com/en-us/library/cc240576.aspx */
if (*length == 0x8000)
{
rdp_read_flow_control_pdu(s, type);
*length = 8; /* Flow control PDU is 8 bytes */
return TRUE;
}
if (((size_t) *length - 2) > Stream_GetRemainingLength(s))
return FALSE;
@ -894,12 +903,36 @@ int rdp_recv_out_of_sequence_pdu(rdpRdp* rdp, wStream* s)
{
return rdp_recv_enhanced_security_redirection_packet(rdp, s);
}
else if (type == PDU_TYPE_FLOW_RESPONSE ||
type == PDU_TYPE_FLOW_STOP ||
type == PDU_TYPE_FLOW_TEST)
{
return 0;
}
else
{
return -1;
}
}
void rdp_read_flow_control_pdu(wStream* s, UINT16* type)
{
/*
* Read flow control PDU - documented in FlowPDU section in T.128
* http://www.itu.int/rec/T-REC-T.128-199802-S/en
* The specification for the PDU has pad8bits listed BEFORE pduTypeFlow.
* However, so far pad8bits has always been observed to arrive AFTER pduTypeFlow.
* Switched the order of these two fields to match this observation.
*/
UINT8 pduType;
Stream_Read_UINT8(s, pduType); /* pduTypeFlow */
*type = pduType;
Stream_Seek_UINT8(s); /* pad8bits */
Stream_Seek_UINT8(s); /* flowIdentifier */
Stream_Seek_UINT8(s); /* flowNumber */
Stream_Seek_UINT16(s); /* pduSource */
}
/**
* Decrypt an RDP packet.\n
* @param rdp RDP module
@ -1001,6 +1034,11 @@ static int rdp_recv_tpkt_pdu(rdpRdp* rdp, wStream* s)
if (rdp->disconnect)
return 0;
if (rdp->autodetect->bandwidthMeasureStarted)
{
rdp->autodetect->bandwidthMeasureByteCount += length;
}
if (rdp->settings->DisableEncryption)
{
if (!rdp_read_security_header(s, &securityFlags))
@ -1058,6 +1096,11 @@ static int rdp_recv_tpkt_pdu(rdpRdp* rdp, wStream* s)
case PDU_TYPE_SERVER_REDIRECTION:
return rdp_recv_enhanced_security_redirection_packet(rdp, s);
break;
case PDU_TYPE_FLOW_RESPONSE:
case PDU_TYPE_FLOW_STOP:
case PDU_TYPE_FLOW_TEST:
break;
default:
WLog_ERR(TAG, "incorrect PDU type: 0x%04X", pduType);
@ -1096,6 +1139,11 @@ static int rdp_recv_fastpath_pdu(rdpRdp* rdp, wStream* s)
return -1;
}
if (rdp->autodetect->bandwidthMeasureStarted)
{
rdp->autodetect->bandwidthMeasureByteCount += length;
}
if (fastpath->encryptionFlags & FASTPATH_OUTPUT_ENCRYPTED)
{
UINT16 flags = (fastpath->encryptionFlags & FASTPATH_OUTPUT_SECURE_CHECKSUM) ? SEC_SECURE_CHECKSUM : 0;

View File

@ -83,6 +83,10 @@
#define PDU_TYPE_DATA 0x7
#define PDU_TYPE_SERVER_REDIRECTION 0xA
#define PDU_TYPE_FLOW_TEST 0x41
#define PDU_TYPE_FLOW_RESPONSE 0x42
#define PDU_TYPE_FLOW_STOP 0x43
#define FINALIZE_SC_SYNCHRONIZE_PDU 0x01
#define FINALIZE_SC_CONTROL_COOPERATE_PDU 0x02
#define FINALIZE_SC_CONTROL_GRANTED_PDU 0x04
@ -206,6 +210,8 @@ int rdp_recv_message_channel_pdu(rdpRdp* rdp, wStream* s);
int rdp_recv_out_of_sequence_pdu(rdpRdp* rdp, wStream* s);
void rdp_read_flow_control_pdu(wStream* s, UINT16* type);
void rdp_set_blocking_mode(rdpRdp* rdp, BOOL blocking);
int rdp_check_fds(rdpRdp* rdp);

View File

@ -787,7 +787,7 @@ BOOL tcp_set_keep_alive_mode(rdpTcp* tcp)
#endif
#endif
#ifdef __MACOSX__
#if defined(__MACOSX__) || defined(__IOS__)
option_value = 1;
option_len = sizeof(option_value);
if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *) &option_value, option_len) < 0)

View File

@ -617,7 +617,7 @@ static void update_end_paint(rdpContext* context)
if (update->numberOrders > 0)
{
WLog_ERR(TAG, "sending %d orders", update->numberOrders);
fastpath_send_update_pdu(context->rdp->fastpath, FASTPATH_UPDATETYPE_ORDERS, s);
fastpath_send_update_pdu(context->rdp->fastpath, FASTPATH_UPDATETYPE_ORDERS, s, FALSE);
}
update->combineUpdates = FALSE;
@ -863,7 +863,7 @@ static void update_send_surface_command(rdpContext* context, wStream* s)
update = fastpath_update_pdu_init(rdp->fastpath);
Stream_EnsureRemainingCapacity(update, Stream_GetPosition(s));
Stream_Write(update, Stream_Buffer(s), Stream_GetPosition(s));
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, update);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, update, FALSE);
Stream_Release(update);
}
@ -878,7 +878,7 @@ static void update_send_surface_bits(rdpContext* context, SURFACE_BITS_COMMAND*
Stream_EnsureRemainingCapacity(s, SURFCMD_SURFACE_BITS_HEADER_LENGTH + (int) surfaceBitsCommand->bitmapDataLength);
update_write_surfcmd_surface_bits_header(s, surfaceBitsCommand);
Stream_Write(s, surfaceBitsCommand->bitmapData, surfaceBitsCommand->bitmapDataLength);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s, surfaceBitsCommand->skipCompression);
update_force_flush(context);
@ -894,7 +894,7 @@ static void update_send_surface_frame_marker(rdpContext* context, SURFACE_FRAME_
s = fastpath_update_pdu_init(rdp->fastpath);
update_write_surfcmd_frame_marker(s, surfaceFrameMarker->frameAction, surfaceFrameMarker->frameId);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s, FALSE);
update_force_flush(context);
@ -921,7 +921,7 @@ static void update_send_surface_frame_bits(rdpContext* context, SURFACE_BITS_COM
if (last)
update_write_surfcmd_frame_marker(s, SURFACECMD_FRAMEACTION_END, frameId);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s, cmd->skipCompression);
update_force_flush(context);
@ -949,7 +949,7 @@ static void update_send_synchronize(rdpContext* context)
s = fastpath_update_pdu_init(rdp->fastpath);
Stream_Zero(s, 2); /* pad2Octets (2 bytes) */
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SYNCHRONIZE, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SYNCHRONIZE, s, FALSE);
Stream_Release(s);
}
@ -968,7 +968,7 @@ static void update_send_bitmap_update(rdpContext* context, BITMAP_UPDATE* bitmap
s = fastpath_update_pdu_init(rdp->fastpath);
update_write_bitmap_update(update, s, bitmapUpdate);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_BITMAP, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_BITMAP, s, bitmapUpdate->skipCompression);
update_force_flush(context);
@ -1502,7 +1502,7 @@ static void update_send_pointer_system(rdpContext* context, POINTER_SYSTEM_UPDAT
else
updateCode = FASTPATH_UPDATETYPE_PTR_DEFAULT;
fastpath_send_update_pdu(rdp->fastpath, updateCode, s);
fastpath_send_update_pdu(rdp->fastpath, updateCode, s, FALSE);
Stream_Release(s);
}
@ -1550,7 +1550,7 @@ static void update_send_pointer_color(rdpContext* context, POINTER_COLOR_UPDATE*
s = fastpath_update_pdu_init(rdp->fastpath);
update_write_pointer_color(s, pointer_color);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_COLOR, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_COLOR, s, FALSE);
Stream_Release(s);
}
@ -1566,7 +1566,8 @@ static void update_send_pointer_new(rdpContext* context, POINTER_NEW_UPDATE* poi
Stream_Write_UINT16(s, pointer_new->xorBpp); /* xorBpp (2 bytes) */
update_write_pointer_color(s, &pointer_new->colorPtrAttr);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_POINTER, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_POINTER, s, FALSE);
Stream_Release(s);
}
@ -1577,7 +1578,7 @@ static void update_send_pointer_cached(rdpContext* context, POINTER_CACHED_UPDAT
s = fastpath_update_pdu_init(rdp->fastpath);
Stream_Write_UINT16(s, pointer_cached->cacheIndex); /* cacheIndex (2 bytes) */
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_CACHED, s);
fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_CACHED, s, FALSE);
Stream_Release(s);
}

View File

@ -163,7 +163,7 @@ WINPR_API HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T d
WINPR_API HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
DECLSPEC_NORETURN WINPR_API VOID ExitThread(DWORD dwExitCode);
WINPR_API DECLSPEC_NORETURN VOID ExitThread(DWORD dwExitCode);
WINPR_API BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode);
WINPR_API HANDLE _GetCurrentThread(void);
@ -200,7 +200,11 @@ WINPR_API BOOL TlsFree(DWORD dwTlsIndex);
/* CommandLineToArgvA is not present in the original Windows API, WinPR always exports it */
WINPR_API LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs);
WINPR_API LPSTR *CommandLineToArgvA(LPCSTR lpCmdLine, int *pNumArgs);
#if defined(WITH_DEBUG_THREADS)
WINPR_API VOID DumpThreadHandles(void);
#endif
#ifdef __cplusplus
}

View File

@ -127,20 +127,7 @@ BOOL CloseHandle(HANDLE hObject)
LeaveCriticalSection(&_HandleCloseCbsLock);
if (Type == HANDLE_TYPE_THREAD)
{
WINPR_THREAD* thread;
thread = (WINPR_THREAD*) Object;
if (thread->started)
{
pthread_detach(thread->thread);
}
free(thread);
return TRUE;
}
else if (Type == HANDLE_TYPE_PROCESS)
if (Type == HANDLE_TYPE_PROCESS)
{
WINPR_PROCESS* process;
process = (WINPR_PROCESS*) Object;

View File

@ -100,11 +100,13 @@ static void* thread_pool_work_func(void* arg)
}
}
ExitThread(0);
return NULL;
}
static void threads_close(void *thread)
{
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
}
@ -184,15 +186,6 @@ VOID CloseThreadpool(PTP_POOL ptpp)
SetEvent(ptpp->TerminateEvent);
index = ArrayList_Count(ptpp->Threads) - 1;
while (index >= 0)
{
thread = (HANDLE) ArrayList_GetItem(ptpp->Threads, index);
WaitForSingleObject(thread, INFINITE);
index--;
}
ArrayList_Free(ptpp->Threads);
Queue_Free(ptpp->PendingQueue);
CountdownEvent_Free(ptpp->WorkComplete);

View File

@ -1382,9 +1382,7 @@ WINSCARDAPI LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext
{
int i, j;
int* map;
DWORD dwEventState;
PCSC_DWORD cMappedReaders;
BOOL stateChanged = FALSE;
PCSC_SCARD_READERSTATE* states;
LONG status = SCARD_S_SUCCESS;
PCSC_DWORD pcsc_dwTimeout = (PCSC_DWORD) dwTimeout;
@ -1437,6 +1435,7 @@ WINSCARDAPI LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext
states[j].szReader = rgReaderStates[i].szReader;
states[j].dwCurrentState = rgReaderStates[i].dwCurrentState;
states[j].pvUserData = rgReaderStates[i].pvUserData;
states[j].dwEventState = rgReaderStates[i].dwEventState;
states[j].cbAtr = rgReaderStates[i].cbAtr;
CopyMemory(&(states[j].rgbAtr), &(rgReaderStates[i].rgbAtr), PCSC_MAX_ATR_SIZE);
@ -1470,6 +1469,11 @@ WINSCARDAPI LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext
rgReaderStates[i].dwCurrentState = states[j].dwCurrentState;
rgReaderStates[i].cbAtr = states[j].cbAtr;
CopyMemory(&(rgReaderStates[i].rgbAtr), &(states[j].rgbAtr), PCSC_MAX_ATR_SIZE);
/**
* Why we should interpret and modify the results of pcsc-lite ScardGetStatusChange ?
* Should not we just act as a pass-through between the client and the remote smartcard subsystem ?
*/
#if 0
/* pcsc-lite puts an event count in the higher bits of dwEventState */
states[j].dwEventState &= 0xFFFF;
dwEventState = states[j].dwEventState & ~SCARD_STATE_CHANGED;
@ -1493,12 +1497,19 @@ WINSCARDAPI LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext
if (rgReaderStates[i].dwCurrentState & SCARD_STATE_IGNORE)
rgReaderStates[i].dwEventState = SCARD_STATE_IGNORE;
#endif
rgReaderStates[i].dwEventState = states[j].dwEventState;
}
/**
* Why we should interpret and modify the results of pcsc-lite ScardGetStatusChange ?
* Should not we just act as a pass-through between the client and the remote smartcard subsystem ?
*/
#if 0
if ((status == SCARD_S_SUCCESS) && !stateChanged)
status = SCARD_E_TIMEOUT;
else if ((status == SCARD_E_TIMEOUT) && stateChanged)
return SCARD_S_SUCCESS;
#endif
free(states);
free(map);
@ -1610,7 +1621,15 @@ WINSCARDAPI LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext,
if (!szReaderPCSC)
szReaderPCSC = (char*) szReader;
pcsc_dwPreferredProtocols = (PCSC_DWORD) PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols);
/**
* As stated here : https://pcsclite.alioth.debian.org/api/group__API.html#ga4e515829752e0a8dbc4d630696a8d6a5
* SCARD_PROTOCOL_UNDEFINED is valid for dwPreferredProtocols (only) if dwShareMode == SCARD_SHARE_DIRECT
* and allows to send control commands to the reader (with SCardControl()) even if a card is not present in the reader
*/
if (pcsc_dwShareMode == SCARD_SHARE_DIRECT && dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED)
pcsc_dwPreferredProtocols = SCARD_PROTOCOL_UNDEFINED;
else
pcsc_dwPreferredProtocols = (PCSC_DWORD) PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols);
status = (LONG) g_PCSC.pfnSCardConnect(hPrivateContext, szReaderPCSC,
pcsc_dwShareMode, pcsc_dwPreferredProtocols, phCard, &pcsc_dwActiveProtocol);
status = PCSC_MapErrorCodeToWinSCard(status);
@ -2107,11 +2126,7 @@ WINSCARDAPI LONG WINAPI PCSC_SCardControl(SCARDHANDLE hCard,
{
ioCtlValue = _byteswap_ulong(tlv->value);
ioCtlValue -= 0x42000000; /* inverse of PCSC_SCARD_CTL_CODE() */
IoCtlMethod = METHOD_FROM_CTL_CODE(ioCtlValue);
IoCtlFunction = FUNCTION_FROM_CTL_CODE(ioCtlValue);
IoCtlAccess = ACCESS_FROM_CTL_CODE(ioCtlValue);
IoCtlDeviceType = DEVICE_TYPE_FROM_CTL_CODE(ioCtlValue);
ioCtlValue = SCARD_CTL_CODE(IoCtlFunction);
ioCtlValue = SCARD_CTL_CODE(ioCtlValue);
tlv->value = _byteswap_ulong(ioCtlValue);
}
}

View File

@ -12,6 +12,7 @@ set(${MODULE_PREFIX}_TESTS
TestSynchCritical.c
TestSynchSemaphore.c
TestSynchThread.c
TestSynchMultipleThreads.c
TestSynchTimerQueue.c
TestSynchWaitableTimer.c
TestSynchWaitableTimerAPC.c)

View File

@ -0,0 +1,154 @@
#include <stdlib.h>
#include <winpr/crt.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
static void *test_thread(void *arg)
{
long timeout = random();
timeout %= 1000;
timeout += 100;
Sleep(timeout);
ExitThread(0);
return NULL;
}
static int start_threads(DWORD count, HANDLE *threads)
{
DWORD i;
for (i=0; i<count; i++)
{
threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test_thread,
NULL, 0, NULL);
if (!threads[i])
{
printf("CreateThread [%i] failure\n", i);
return -1;
}
}
return 0;
}
static int close_threads(DWORD count, HANDLE *threads)
{
DWORD i;
for (i=0; i<count; i++)
{
if (!CloseHandle(threads[i]))
{
printf("CloseHandle [%d] failure\n", i);
return -1;
}
}
return 0;
}
int TestSynchMultipleThreads(int argc, char *argv[])
{
#define THREADS 24
DWORD rc = 0, ev, i;
HANDLE threads[THREADS];
/* WaitForAll, timeout */
if (start_threads(THREADS, threads))
return -1;
if (WaitForMultipleObjects(THREADS, threads, TRUE, 50) != WAIT_TIMEOUT)
{
printf("WaitForMultipleObjects bWaitAll, timeout 50 failed\n");
rc = -1;
}
if (WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE) != WAIT_OBJECT_0)
{
printf("WaitForMultipleObjects bWaitAll, INFINITE failed\n");
rc = -1;
}
if (close_threads(THREADS, threads))
return -1;
/* WaitOne, infinite */
if (rc)
return rc;
if (start_threads(THREADS, threads))
return -1;
ev = WaitForMultipleObjects(THREADS, threads, FALSE, INFINITE);
if ((ev < WAIT_OBJECT_0) || (ev > (WAIT_OBJECT_0 + THREADS)))
{
printf("WaitForMultipleObjects INFINITE failed\n");
rc = -1;
}
if (WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE) != WAIT_OBJECT_0)
{
printf("WaitForMultipleObjects bWaitAll, INFINITE failed\n");
rc = -1;
}
if (close_threads(THREADS, threads))
return -1;
if (rc)
return rc;
/* WaitOne, timeout */
if (start_threads(THREADS, threads))
return -1;
if (WaitForMultipleObjects(THREADS, threads, FALSE, 50) != WAIT_TIMEOUT)
{
printf("WaitForMultipleObjects timeout 50 failed\n");
rc = -1;
}
if (WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE) != WAIT_OBJECT_0)
{
printf("WaitForMultipleObjects bWaitAll, INFINITE failed\n");
rc = -1;
}
if (close_threads(THREADS, threads))
return -1;
/* WaitOne, timeout, multiple joins */
if (start_threads(THREADS, threads))
return -1;
for (i=0; i<THREADS; i++)
{
if (WaitForMultipleObjects(THREADS, threads, FALSE, 0) != WAIT_TIMEOUT)
{
printf("WaitForMultipleObjects timeout 50 failed\n");
rc = -1;
}
}
if (WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE) != WAIT_OBJECT_0)
{
printf("WaitForMultipleObjects bWaitAll, INFINITE failed\n");
rc = -1;
}
if (close_threads(THREADS, threads))
return -1;
/* Thread detach test */
if (start_threads(THREADS, threads))
return -1;
if (close_threads(THREADS, threads))
return -1;
return 0;
}

View File

@ -6,18 +6,17 @@
static void *test_thread(void *arg)
{
Sleep(1000);
ExitThread(0);
return NULL;
}
int TestSynchThread(int argc, char* argv[])
int TestSynchThread(int argc, char *argv[])
{
DWORD rc;
HANDLE thread;
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test_thread,
NULL, 0, NULL);
NULL, 0, NULL);
if (!thread)
{
printf("CreateThread failure\n");
@ -26,6 +25,7 @@ int TestSynchThread(int argc, char* argv[])
/* TryJoin should now fail. */
rc = WaitForSingleObject(thread, 0);
if (WAIT_TIMEOUT != rc)
{
printf("Timed WaitForSingleObject on running thread failed with %d\n", rc);
@ -34,6 +34,7 @@ int TestSynchThread(int argc, char* argv[])
/* Join the thread */
rc = WaitForSingleObject(thread, INFINITE);
if (WAIT_OBJECT_0 != rc)
{
printf("WaitForSingleObject on thread failed with %d\n", rc);
@ -42,13 +43,76 @@ int TestSynchThread(int argc, char* argv[])
/* TimedJoin should now succeed. */
rc = WaitForSingleObject(thread, 0);
if (WAIT_OBJECT_0 != rc)
{
printf("Timed WaitForSingleObject on dead thread failed with %d\n", rc);
return -5;
}
CloseHandle(thread);
if (!CloseHandle(thread))
{
printf("CloseHandle failed!");
return -1;
}
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test_thread,
NULL, 0, NULL);
if (!thread)
{
printf("CreateThread failure\n");
return -1;
}
/* TryJoin should now fail. */
rc = WaitForSingleObject(thread, 50);
if (WAIT_TIMEOUT != rc)
{
printf("Timed WaitForSingleObject on running thread failed with %d\n", rc);
return -3;
}
/* Join the thread */
rc = WaitForSingleObject(thread, INFINITE);
if (WAIT_OBJECT_0 != rc)
{
printf("WaitForSingleObject on thread failed with %d\n", rc);
return -2;
}
/* TimedJoin should now succeed. */
rc = WaitForSingleObject(thread, 0);
if (WAIT_OBJECT_0 != rc)
{
printf("Timed WaitForSingleObject on dead thread failed with %d\n", rc);
return -5;
}
if (!CloseHandle(thread))
{
printf("CloseHandle failed!");
return -1;
}
/* Thread detach test */
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test_thread,
NULL, 0, NULL);
if (!thread)
{
printf("CreateThread failure\n");
return -1;
}
if (!CloseHandle(thread))
{
printf("CloseHandle failed!");
return -1;
}
return 0;
}

View File

@ -76,7 +76,7 @@
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0
int clock_gettime(int clk_id, struct timespec* t)
int clock_gettime(int clk_id, struct timespec *t)
{
UINT64 time;
double seconds;
@ -93,60 +93,37 @@ int clock_gettime(int clk_id, struct timespec* t)
#endif
/* Drop in replacement for the linux pthread_timedjoin_np and
* pthread_mutex_timedlock functions.
*/
#if !defined(HAVE_PTHREAD_GNU_EXT)
#include <pthread.h>
static long long ts_difftime(const struct timespec* o,
const struct timespec* n)
static long long ts_difftime(const struct timespec *o,
const struct timespec *n)
{
long long oldValue = o->tv_sec * 1000000000LL + o->tv_nsec;
long long newValue = n->tv_sec * 1000000000LL + n->tv_nsec;
return newValue - oldValue;
}
static int pthread_timedjoin_np(pthread_t td, void** res,
struct timespec* timeout)
{
struct timespec timenow;
struct timespec sleepytime;
/* This is just to avoid a completely busy wait */
sleepytime.tv_sec = 0;
sleepytime.tv_nsec = 10000000; /* 10ms */
do
{
if (pthread_kill(td, 0))
return pthread_join(td, res);
nanosleep(&sleepytime, NULL);
clock_gettime(CLOCK_MONOTONIC, &timenow);
if (ts_difftime(timeout, &timenow) >= 0)
{
return ETIMEDOUT;
}
}
while (TRUE);
return ETIMEDOUT;
}
/* Drop in replacement for the linux pthread_timedjoin_np and
* pthread_mutex_timedlock functions.
*/
#if !defined(HAVE_PTHREAD_GNU_EXT)
#include <pthread.h>
#if defined(__FreeBSD__)
/*the only way to get it work is to remove the static*/
int pthread_mutex_timedlock(pthread_mutex_t* mutex, const struct timespec* timeout)
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeout)
#else
static int pthread_mutex_timedlock(pthread_mutex_t* mutex, const struct timespec* timeout)
static int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeout)
#endif
{
struct timespec timenow;
struct timespec sleepytime;
unsigned long long diff;
int retcode;
/* This is just to avoid a completely busy wait */
sleepytime.tv_sec = 0;
sleepytime.tv_nsec = 10000000; /* 10ms */
clock_gettime(CLOCK_MONOTONIC, &timenow);
diff = ts_difftime(&timenow, timeout);
sleepytime.tv_sec = diff / 1000000000LL;
sleepytime.tv_nsec = diff % 1000000000LL;
while ((retcode = pthread_mutex_trylock(mutex)) == EBUSY)
{
@ -164,7 +141,7 @@ static int pthread_mutex_timedlock(pthread_mutex_t* mutex, const struct timespec
}
#endif
static void ts_add_ms(struct timespec* ts, DWORD dwMilliseconds)
static void ts_add_ms(struct timespec *ts, DWORD dwMilliseconds)
{
ts->tv_sec += dwMilliseconds / 1000L;
ts->tv_nsec += (dwMilliseconds % 1000L) * 1000000L;
@ -223,48 +200,42 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
if (Type == HANDLE_TYPE_THREAD)
{
int status = 0;
WINPR_THREAD* thread;
void* thread_status = NULL;
thread = (WINPR_THREAD*) Object;
int status;
WINPR_THREAD *thread = (WINPR_THREAD *)Object;
status = waitOnFd(thread->pipe_fd[0], dwMilliseconds);
if (thread->started)
if (status < 0)
{
if (dwMilliseconds != INFINITE)
{
struct timespec timeout;
WLog_ERR(TAG, "waitOnFd() failure [%d] %s", errno, strerror(errno));
return WAIT_FAILED;
}
/* pthread_timedjoin_np returns ETIMEDOUT in case the timeout is 0,
* so set it to the smallest value to get a proper return value. */
if (dwMilliseconds == 0)
dwMilliseconds ++;
if (status != 1)
return WAIT_TIMEOUT;
clock_gettime(CLOCK_MONOTONIC, &timeout);
ts_add_ms(&timeout, dwMilliseconds);
status = pthread_timedjoin_np(thread->thread, &thread_status, &timeout);
pthread_mutex_lock(&thread->mutex);
if (ETIMEDOUT == status)
return WAIT_TIMEOUT;
}
else
status = pthread_join(thread->thread, &thread_status);
thread->started = FALSE;
if (!thread->joined)
{
status = pthread_join(thread->thread, NULL);
if (status != 0)
{
WLog_ERR(TAG, "pthread_join failure: [%d] %s",
status, strerror(status));
pthread_mutex_unlock(&thread->mutex);
return WAIT_FAILED;
}
if (thread_status)
thread->dwExitCode = ((DWORD)(size_t) thread_status);
else
thread->joined = TRUE;
}
pthread_mutex_unlock(&thread->mutex);
}
else if (Type == HANDLE_TYPE_PROCESS)
{
WINPR_PROCESS* process;
process = (WINPR_PROCESS*) Object;
WINPR_PROCESS *process;
process = (WINPR_PROCESS *) Object;
if (waitpid(process->pid, &(process->status), 0) != -1)
{
@ -276,8 +247,8 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
}
else if (Type == HANDLE_TYPE_MUTEX)
{
WINPR_MUTEX* mutex;
mutex = (WINPR_MUTEX*) Object;
WINPR_MUTEX *mutex;
mutex = (WINPR_MUTEX *) Object;
if (dwMilliseconds != INFINITE)
{
@ -298,9 +269,8 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
else if (Type == HANDLE_TYPE_EVENT)
{
int status;
WINPR_EVENT* event;
event = (WINPR_EVENT*) Object;
WINPR_EVENT *event;
event = (WINPR_EVENT *) Object;
status = waitOnFd(event->pipe_fd[0], dwMilliseconds);
if (status < 0)
@ -314,8 +284,8 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
}
else if (Type == HANDLE_TYPE_SEMAPHORE)
{
WINPR_SEMAPHORE* semaphore;
semaphore = (WINPR_SEMAPHORE*) Object;
WINPR_SEMAPHORE *semaphore;
semaphore = (WINPR_SEMAPHORE *) Object;
#ifdef WINPR_PIPE_SEMAPHORE
if (semaphore->pipe_fd[0] != -1)
@ -344,16 +314,16 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
#else
#if defined __APPLE__
semaphore_wait(*((winpr_sem_t*) semaphore->sem));
semaphore_wait(*((winpr_sem_t *) semaphore->sem));
#else
sem_wait((winpr_sem_t*) semaphore->sem);
sem_wait((winpr_sem_t *) semaphore->sem);
#endif
#endif
}
else if (Type == HANDLE_TYPE_TIMER)
{
WINPR_TIMER* timer;
timer = (WINPR_TIMER*) Object;
WINPR_TIMER *timer;
timer = (WINPR_TIMER *) Object;
#ifdef HAVE_EVENTFD_H
if (timer->fd != -1)
@ -371,7 +341,7 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
if (status != 1)
return WAIT_TIMEOUT;
status = read(timer->fd, (void*) &expirations, sizeof(UINT64));
status = read(timer->fd, (void *) &expirations, sizeof(UINT64));
if (status != 8)
{
@ -405,7 +375,7 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
{
int fd;
int status;
WINPR_NAMED_PIPE* pipe = (WINPR_NAMED_PIPE*) Object;
WINPR_NAMED_PIPE *pipe = (WINPR_NAMED_PIPE *) Object;
fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd;
if (fd == -1)
@ -444,15 +414,22 @@ DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertabl
#define MAXIMUM_WAIT_OBJECTS 64
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
{
struct timespec starttime;
struct timespec timenow;
unsigned long long diff;
DWORD signalled;
DWORD polled;
DWORD *poll_map;
BOOL *signalled_idx;
int fd = -1;
int index;
int status;
ULONG Type;
PVOID Object;
#ifdef HAVE_POLL_H
struct pollfd* pollfds;
struct pollfd *pollfds;
#else
int maxfd;
fd_set fds;
@ -465,202 +442,303 @@ DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAl
return WAIT_FAILED;
}
#ifdef HAVE_POLL_H
pollfds = alloca(nCount * sizeof(struct pollfd));
#else
maxfd = 0;
FD_ZERO(&fds);
ZeroMemory(&timeout, sizeof(timeout));
#endif
if (bWaitAll)
{
WLog_ERR(TAG, "bWaitAll not yet implemented");
assert(0);
signalled_idx = alloca(nCount * sizeof(BOOL));
memset(signalled_idx, FALSE, nCount * sizeof(BOOL));
poll_map = alloca(nCount * sizeof(DWORD));
memset(poll_map, 0, nCount * sizeof(DWORD));
}
for (index = 0; index < nCount; index++)
#ifdef HAVE_POLL_H
pollfds = alloca(nCount * sizeof(struct pollfd));
#endif
signalled = 0;
do
{
if (!winpr_Handle_GetInfo(lpHandles[index], &Type, &Object))
{
WLog_ERR(TAG, "invalid handle");
return WAIT_FAILED;
}
if (bWaitAll && (dwMilliseconds != INFINITE))
clock_gettime(CLOCK_MONOTONIC, &starttime);
if (Type == HANDLE_TYPE_EVENT)
{
fd = ((WINPR_EVENT*) Object)->pipe_fd[0];
#ifndef HAVE_POLL_H
maxfd = 0;
FD_ZERO(&fds);
ZeroMemory(&timeout, sizeof(timeout));
#endif
polled = 0;
if (fd == -1)
for (index = 0; index < nCount; index++)
{
if (bWaitAll)
{
if (signalled_idx[index])
continue;
poll_map[polled] = index;
}
if (!winpr_Handle_GetInfo(lpHandles[index], &Type, &Object))
{
WLog_ERR(TAG, "invalid event file descriptor");
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_SEMAPHORE)
{
#ifdef WINPR_PIPE_SEMAPHORE
fd = ((WINPR_SEMAPHORE*) Object)->pipe_fd[0];
#else
WLog_ERR(TAG, "semaphore not supported");
return WAIT_FAILED;
#endif
}
else if (Type == HANDLE_TYPE_TIMER)
{
WINPR_TIMER* timer = (WINPR_TIMER*) Object;
fd = timer->fd;
if (fd == -1)
if (Type == HANDLE_TYPE_EVENT)
{
WLog_ERR(TAG, "invalid timer file descriptor");
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_NAMED_PIPE)
{
WINPR_NAMED_PIPE* pipe = (WINPR_NAMED_PIPE*) Object;
fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd;
fd = ((WINPR_EVENT *) Object)->pipe_fd[0];
if (fd == -1)
{
WLog_ERR(TAG, "invalid timer file descriptor");
return WAIT_FAILED;
}
}
else
{
WLog_ERR(TAG, "unknown handle type %d", (int) Type);
return WAIT_FAILED;
}
if (fd == -1)
{
WLog_ERR(TAG, "invalid file descriptor");
return WAIT_FAILED;
}
#ifdef HAVE_POLL_H
pollfds[index].fd = fd;
pollfds[index].events = POLLIN;
pollfds[index].revents = 0;
#else
FD_SET(fd, &fds);
if (fd > maxfd)
maxfd = fd;
#endif
}
#ifdef HAVE_POLL_H
do
{
status = poll(pollfds, nCount, dwMilliseconds);
}
while (status < 0 && errno == EINTR);
#else
if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
{
timeout.tv_sec = dwMilliseconds / 1000;
timeout.tv_usec = (dwMilliseconds % 1000) * 1000;
}
do
{
status = select(maxfd + 1, &fds, 0, 0,
(dwMilliseconds == INFINITE) ? NULL : &timeout);
}
while (status < 0 && errno == EINTR);
#endif
if (status < 0)
{
WLog_ERR(TAG, "select() failure [%d] %s", errno, strerror(errno));
return WAIT_FAILED;
}
if (status == 0)
return WAIT_TIMEOUT;
for (index = 0; index < nCount; index++)
{
winpr_Handle_GetInfo(lpHandles[index], &Type, &Object);
if (Type == HANDLE_TYPE_EVENT)
{
fd = ((WINPR_EVENT*) Object)->pipe_fd[0];
}
else if (Type == HANDLE_TYPE_SEMAPHORE)
{
fd = ((WINPR_SEMAPHORE*) Object)->pipe_fd[0];
}
else if (Type == HANDLE_TYPE_TIMER)
{
WINPR_TIMER* timer = (WINPR_TIMER*) Object;
fd = timer->fd;
}
else if (Type == HANDLE_TYPE_NAMED_PIPE)
{
WINPR_NAMED_PIPE* pipe = (WINPR_NAMED_PIPE*) Object;
fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd;
}
#ifdef HAVE_POLL_H
if (pollfds[index].revents & POLLIN)
#else
if (FD_ISSET(fd, &fds))
#endif
{
if (Type == HANDLE_TYPE_SEMAPHORE)
{
int length;
length = read(fd, &length, 1);
if (length != 1)
if (fd == -1)
{
WLog_ERR(TAG, "semaphore read() failure [%d] %s", errno, strerror(errno));
WLog_ERR(TAG, "invalid event file descriptor");
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_SEMAPHORE)
{
#ifdef WINPR_PIPE_SEMAPHORE
fd = ((WINPR_SEMAPHORE *) Object)->pipe_fd[0];
#else
WLog_ERR(TAG, "semaphore not supported");
return WAIT_FAILED;
#endif
}
else if (Type == HANDLE_TYPE_TIMER)
{
int length;
UINT64 expirations;
length = read(fd, (void*) &expirations, sizeof(UINT64));
WINPR_TIMER *timer = (WINPR_TIMER *) Object;
fd = timer->fd;
if (length != 8)
if (fd == -1)
{
if (length == -1)
{
if (errno == ETIMEDOUT)
return WAIT_TIMEOUT;
WLog_ERR(TAG, "timer read() failure [%d] %s", errno, strerror(errno));
}
else
{
WLog_ERR(TAG, "timer read() failure - incorrect number of bytes read");
}
WLog_ERR(TAG, "invalid timer file descriptor");
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_THREAD)
{
WINPR_THREAD *thread = (WINPR_THREAD *) Object;
fd = thread->pipe_fd[0];
return (WAIT_OBJECT_0 + index);
if (fd == -1)
{
WLog_ERR(TAG, "invalid thread file descriptor");
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_NAMED_PIPE)
{
WINPR_NAMED_PIPE *pipe = (WINPR_NAMED_PIPE *) Object;
fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd;
if (fd == -1)
{
WLog_ERR(TAG, "invalid timer file descriptor");
return WAIT_FAILED;
}
}
else
{
WLog_ERR(TAG, "unknown handle type %d", (int) Type);
return WAIT_FAILED;
}
if (fd == -1)
{
WLog_ERR(TAG, "invalid file descriptor");
return WAIT_FAILED;
}
#ifdef HAVE_POLL_H
pollfds[polled].fd = fd;
pollfds[polled].events = POLLIN;
pollfds[polled].revents = 0;
#else
FD_SET(fd, &fds);
if (fd > maxfd)
maxfd = fd;
#endif
polled++;
}
#ifdef HAVE_POLL_H
do
{
status = poll(pollfds, polled, dwMilliseconds);
}
while (status < 0 && errno == EINTR);
#else
if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
{
timeout.tv_sec = dwMilliseconds / 1000;
timeout.tv_usec = (dwMilliseconds % 1000) * 1000;
}
do
{
status = select(maxfd + 1, &fds, 0, 0,
(dwMilliseconds == INFINITE) ? NULL : &timeout);
}
while (status < 0 && errno == EINTR);
#endif
if (status < 0)
{
#ifdef HAVE_POLL_H
WLog_ERR(TAG, "poll() failure [%d] %s", errno,
strerror(errno));
#else
WLog_ERR(TAG, "select() failure [%d] %s", errno,
strerror(errno));
#endif
return WAIT_FAILED;
}
if (status == 0)
return WAIT_TIMEOUT;
if (bWaitAll && (dwMilliseconds != INFINITE))
{
clock_gettime(CLOCK_MONOTONIC, &timenow);
diff = ts_difftime(&timenow, &starttime);
if (diff / 1000 > dwMilliseconds)
return WAIT_TIMEOUT;
else
dwMilliseconds -= (diff / 1000);
}
for (index = 0; index < polled; index++)
{
DWORD idx;
if (bWaitAll)
idx = poll_map[index];
else
idx = index;
winpr_Handle_GetInfo(lpHandles[idx], &Type, &Object);
if (Type == HANDLE_TYPE_EVENT)
{
fd = ((WINPR_EVENT *) Object)->pipe_fd[0];
}
else if (Type == HANDLE_TYPE_SEMAPHORE)
{
fd = ((WINPR_SEMAPHORE *) Object)->pipe_fd[0];
}
else if (Type == HANDLE_TYPE_TIMER)
{
WINPR_TIMER *timer = (WINPR_TIMER *) Object;
fd = timer->fd;
}
else if (Type == HANDLE_TYPE_THREAD)
{
WINPR_THREAD *thread = (WINPR_THREAD *) Object;
fd = thread->pipe_fd[0];
}
else if (Type == HANDLE_TYPE_NAMED_PIPE)
{
WINPR_NAMED_PIPE *pipe = (WINPR_NAMED_PIPE *) Object;
fd = (pipe->ServerMode) ? pipe->serverfd : pipe->clientfd;
}
#ifdef HAVE_POLL_H
if (pollfds[index].revents & POLLIN)
#else
if (FD_ISSET(fd, &fds))
#endif
{
if (Type == HANDLE_TYPE_SEMAPHORE)
{
int length;
length = read(fd, &length, 1);
if (length != 1)
{
WLog_ERR(TAG, "semaphore read() failure [%d] %s", errno, strerror(errno));
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_TIMER)
{
int length;
UINT64 expirations;
length = read(fd, (void *) &expirations, sizeof(UINT64));
if (length != 8)
{
if (length == -1)
{
if (errno == ETIMEDOUT)
return WAIT_TIMEOUT;
WLog_ERR(TAG, "timer read() failure [%d] %s", errno, strerror(errno));
}
else
{
WLog_ERR(TAG, "timer read() failure - incorrect number of bytes read");
}
return WAIT_FAILED;
}
}
else if (Type == HANDLE_TYPE_THREAD)
{
WINPR_THREAD *thread = (WINPR_THREAD *)Object;
pthread_mutex_lock(&thread->mutex);
if (!thread->joined)
{
int status;
status = pthread_join(thread->thread, NULL);
if (status != 0)
{
WLog_ERR(TAG, "pthread_join failure: [%d] %s",
status, strerror(status));
pthread_mutex_unlock(&thread->mutex);
return WAIT_FAILED;
}
else
thread->joined = TRUE;
}
pthread_mutex_unlock(&thread->mutex);
}
if (bWaitAll)
{
signalled_idx[idx] = TRUE;
/* Continue checks from last position. */
for (; signalled < nCount; signalled++)
{
if (!signalled_idx[signalled])
break;
}
}
if (!bWaitAll)
return (WAIT_OBJECT_0 + index);
if (bWaitAll && (signalled >= nCount))
return (WAIT_OBJECT_0);
}
}
}
while (bWaitAll);
WLog_ERR(TAG, "failed (unknown error)");
return WAIT_FAILED;
}
DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds, BOOL bAlertable)
DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds, BOOL bAlertable)
{
WLog_ERR(TAG, "[ERROR] %s: Function not implemented.");
assert(0);

View File

@ -21,13 +21,15 @@
#include "config.h"
#endif
#include <assert.h>
#include <winpr/handle.h>
#include <winpr/thread.h>
/**
* api-ms-win-core-processthreads-l1-1-1.dll
*
*
* CreateRemoteThread
* CreateRemoteThreadEx
* CreateThread
@ -70,133 +72,467 @@
#include <winpr/crt.h>
#include <winpr/platform.h>
#if defined(__linux__) && !defined(__ANDROID__)
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#endif
#ifdef HAVE_EVENTFD_H
#include <sys/eventfd.h>
#endif
#include <winpr/debug.h>
#include <errno.h>
#include <winpr/collections.h>
#include "thread.h"
#include "../handle/handle.h"
#include "../log.h"
#define TAG WINPR_TAG("thread")
static pthread_once_t thread_initialized = PTHREAD_ONCE_INIT;
static HANDLE_CLOSE_CB _ThreadHandleCloseCb;
static wListDictionary *thread_list = NULL;
static BOOL ThreadCloseHandle(HANDLE handle);
static void cleanup_handle(void *obj);
static BOOL ThreadIsHandled(HANDLE handle)
{
WINPR_THREAD *pThread = (WINPR_THREAD *)handle;
if (!pThread || pThread->Type != HANDLE_TYPE_THREAD)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
return TRUE;
}
static void ThreadInitialize(void)
{
_ThreadHandleCloseCb.IsHandled = ThreadIsHandled;
_ThreadHandleCloseCb.CloseHandle = ThreadCloseHandle;
RegisterHandleCloseCb(&_ThreadHandleCloseCb);
}
static void dump_thread(WINPR_THREAD* thread)
{
#if defined(WITH_DEBUG_THREADS)
void* stack = winpr_backtrace(20);
char** msg;
size_t used, i;
WLog_DBG(TAG, "Called from:");
msg = winpr_backtrace_symbols(stack, &used);
for (i=0; i<used; i++)
WLog_DBG(TAG, "[%d]: %s", i, msg[i]);
free(msg);
winpr_backtrace_free(stack);
WLog_DBG(TAG, "Thread handle created still not closed!");
msg = winpr_backtrace_symbols(thread->create_stack, &used);
for (i=0; i<used; i++)
WLog_DBG(TAG, "[%d]: %s", i, msg[i]);
free(msg);
if (thread->started)
WLog_DBG(TAG, "Thread still running!");
else if (!thread->exit_stack)
WLog_DBG(TAG, "Thread suspended.");
else
{
WLog_DBG(TAG, "Thread exited at:");
msg = winpr_backtrace_symbols(thread->exit_stack, &used);
for (i=0; i<used; i++)
WLog_DBG(TAG, "[%d]: %s", i, msg[i]);
free(msg);
}
#endif
}
/**
* TODO: implement thread suspend/resume using pthreads
* http://stackoverflow.com/questions/3140867/suspend-pthreads-without-using-condition
*/
static BOOL set_event(WINPR_THREAD *thread)
{
int length;
BOOL status = FALSE;
#ifdef HAVE_EVENTFD_H
eventfd_t val = 1;
void winpr_StartThread(WINPR_THREAD* thread)
do
{
length = eventfd_write(thread->pipe_fd[0], val);
}
while ((length < 0) && (errno == EINTR));
status = (length == 0) ? TRUE : FALSE;
#else
if (WaitForSingleObject(thread, 0) != WAIT_OBJECT_0)
{
length = write(thread->pipe_fd[1], "-", 1);
if (length == 1)
status = TRUE;
}
else
{
status = TRUE;
}
#endif
return status;
}
static BOOL reset_event(WINPR_THREAD *thread)
{
int length;
BOOL status = FALSE;
#ifdef HAVE_EVENTFD_H
eventfd_t value;
do
{
length = eventfd_read(thread->pipe_fd[0], &value);
}
while ((length < 0) && (errno == EINTR));
if ((length > 0) && (!status))
status = TRUE;
#else
length = read(thread->pipe_fd[0], &length, 1);
if ((length == 1) && (!status))
status = TRUE;
#endif
return status;
}
static int thread_compare(void *a, void *b)
{
pthread_t *p1 = a;
pthread_t *p2 = b;
int rc = pthread_equal(*p1, *p2);
return rc;
}
/* Thread launcher function responsible for registering
* cleanup handlers and calling pthread_exit, if not done
* in thread function. */
static void *thread_launcher(void *arg)
{
void *rc = NULL;
WINPR_THREAD *thread = (WINPR_THREAD *)arg;
if (!thread)
{
WLog_ERR(TAG, "Called with invalid argument %p", arg);
goto exit;
}
else
{
void *(*fkt)(void *) = (void *)thread->lpStartAddress;
if (!fkt)
{
WLog_ERR(TAG, "Thread function argument is %p\n", fkt);
goto exit;
}
rc = fkt(thread->lpParameter);
}
exit:
if (!thread->exited)
thread->dwExitCode = (DWORD)(size_t)rc;
set_event(thread);
if (thread->detached || !thread->started)
cleanup_handle(thread);
pthread_exit(thread->dwExitCode);
return rc;
}
static void winpr_StartThread(WINPR_THREAD *thread)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (thread->dwStackSize > 0)
pthread_attr_setstacksize(&attr, (size_t) thread->dwStackSize);
thread->started = TRUE;
pthread_create(&thread->thread, &attr, (pthread_start_routine) thread->lpStartAddress, thread->lpParameter);
pthread_create(&thread->thread, &attr, thread_launcher, thread);
pthread_attr_destroy(&attr);
reset_event(thread);
ListDictionary_Add(thread_list, &thread->thread, thread);
dump_thread(thread);
thread->started = TRUE;
}
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
{
HANDLE handle;
WINPR_THREAD* thread;
thread = (WINPR_THREAD*) calloc(1, sizeof(WINPR_THREAD));
WINPR_THREAD *thread;
thread = (WINPR_THREAD *) calloc(1, sizeof(WINPR_THREAD));
if (!thread)
return NULL;
thread->started = FALSE;
pthread_once(&thread_initialized, ThreadInitialize);
thread->dwStackSize = dwStackSize;
thread->lpParameter = lpParameter;
thread->lpStartAddress = lpStartAddress;
thread->lpThreadAttributes = lpThreadAttributes;
#if defined(WITH_DEBUG_THREADS)
thread->create_stack = winpr_backtrace(20);
dump_thread(thread);
#endif
#ifdef HAVE_EVENTFD_H
thread->pipe_fd[0] = eventfd(0, EFD_NONBLOCK);
if (thread->pipe_fd[0] < 0)
{
WLog_ERR(TAG, "failed to create thread");
free(thread);
return NULL;
}
#else
if (pipe(thread->pipe_fd) < 0)
{
WLog_ERR(TAG, "failed to create thread");
free(thread);
return NULL;
}
#endif
pthread_mutex_init(&thread->mutex, 0);
WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD);
handle = (HANDLE) thread;
if (NULL == thread_list)
{
thread_list = ListDictionary_New(TRUE);
thread_list->objectKey.fnObjectEquals = thread_compare;
}
if (!(dwCreationFlags & CREATE_SUSPENDED))
winpr_StartThread(thread);
else
set_event(thread);
return handle;
}
HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
void cleanup_handle(void *obj)
{
WINPR_THREAD *thread = (WINPR_THREAD *)obj;
int rc = pthread_mutex_destroy(&thread->mutex);
if (rc)
WLog_ERR(TAG, "failed to destroy mutex [%d] %s (%d)",
rc, strerror(errno), errno);
if (thread->pipe_fd[0])
close(thread->pipe_fd[0]);
if (thread->pipe_fd[1])
close(thread->pipe_fd[1]);
if (thread_list && ListDictionary_Contains(thread_list, &thread->thread))
ListDictionary_Remove(thread_list, &thread->thread);
#if defined(WITH_DEBUG_THREADS)
if (thread->create_stack)
winpr_backtrace_free(thread->create_stack);
if (thread->exit_stack)
winpr_backtrace_free(thread->exit_stack);
#endif
free(thread);
}
BOOL ThreadCloseHandle(HANDLE handle)
{
WINPR_THREAD *thread = (WINPR_THREAD *)handle;
if (!thread_list)
{
WLog_ERR(TAG, "Thread list does not exist, check call!");
dump_thread(thread);
}
else if (!ListDictionary_Contains(thread_list, &thread->thread))
{
WLog_ERR(TAG, "Thread list does not contain this thread! check call!");
dump_thread(thread);
}
else
{
ListDictionary_Lock(thread_list);
dump_thread(thread);
if ((thread->started) && (WaitForSingleObject(thread, 0) != WAIT_OBJECT_0))
{
WLog_ERR(TAG, "Thread running, setting to detached state!");
thread->detached = TRUE;
pthread_detach(thread->thread);
}
else
cleanup_handle(thread);
ListDictionary_Unlock(thread_list);
if (ListDictionary_Count(thread_list) < 1)
{
ListDictionary_Free(thread_list);
thread_list = NULL;
}
}
return TRUE;
}
HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
{
WLog_ERR(TAG, "not implemented");
return NULL;
}
VOID ExitThread(DWORD dwExitCode)
{
pthread_exit((void*) (size_t) dwExitCode);
pthread_t tid = pthread_self();
if (NULL == thread_list)
{
WLog_ERR(TAG, "function called without existing thread list!");
#if defined(WITH_DEBUG_THREADS)
DumpThreadHandles();
#endif
}
else if (!ListDictionary_Contains(thread_list, &tid))
{
WLog_ERR(TAG, "function called, but no matching entry in thread list!");
#if defined(WITH_DEBUG_THREADS)
DumpThreadHandles();
#endif
}
else
{
WINPR_THREAD *thread;
ListDictionary_Lock(thread_list);
thread = ListDictionary_GetItemValue(thread_list, &tid);
assert(thread);
thread->exited = TRUE;
thread->dwExitCode = dwExitCode;
#if defined(WITH_DEBUG_THREADS)
thread->exit_stack = winpr_backtrace(20);
#endif
ListDictionary_Unlock(thread_list);
set_event(thread);
if (thread->detached || !thread->started)
cleanup_handle(thread);
pthread_exit(thread->dwExitCode);
}
}
BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
{
ULONG Type;
PVOID Object;
WINPR_THREAD* thread;
WINPR_THREAD *thread;
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
return FALSE;
thread = (WINPR_THREAD*) Object;
thread = (WINPR_THREAD *) Object;
*lpExitCode = thread->dwExitCode;
return TRUE;
}
HANDLE _GetCurrentThread(VOID)
{
return NULL;
HANDLE hdl = NULL;
pthread_t tid = pthread_self();
if (NULL == thread_list)
{
WLog_ERR(TAG, "function called without existing thread list!");
#if defined(WITH_DEBUG_THREADS)
DumpThreadHandles();
#endif
}
else if (!ListDictionary_Contains(thread_list, &tid))
{
WLog_ERR(TAG, "function called, but no matching entry in thread list!");
#if defined(WITH_DEBUG_THREADS)
DumpThreadHandles();
#endif
}
else
{
hdl = ListDictionary_GetItemValue(thread_list, &tid);
}
return hdl;
}
DWORD GetCurrentThreadId(VOID)
{
#if defined(__linux__) && !defined(__ANDROID__)
pid_t tid;
tid = syscall(SYS_gettid);
return (DWORD) tid;
#else
pthread_t tid;
tid = pthread_self();
return (DWORD) tid;
#endif
}
DWORD ResumeThread(HANDLE hThread)
{
ULONG Type;
PVOID Object;
WINPR_THREAD* thread;
WINPR_THREAD *thread;
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
return 0;
thread = (WINPR_THREAD*) Object;
thread = (WINPR_THREAD *) Object;
pthread_mutex_lock(&thread->mutex);
if (!thread->started)
winpr_StartThread(thread);
else
WLog_WARN(TAG, "Thread already started!");
pthread_mutex_unlock(&thread->mutex);
return 0;
}
DWORD SuspendThread(HANDLE hThread)
{
WLog_ERR(TAG, "Function not implemented!");
return 0;
}
@ -209,23 +545,82 @@ BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode)
{
ULONG Type;
PVOID Object;
WINPR_THREAD* thread;
WINPR_THREAD *thread;
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
return 0;
thread = (WINPR_THREAD*) Object;
thread = (WINPR_THREAD *) Object;
thread->exited = TRUE;
thread->dwExitCode = dwExitCode;
pthread_mutex_lock(&thread->mutex);
#ifndef ANDROID
pthread_cancel(thread->thread);
#else
WLog_ERR(TAG, "Function not supported on this platform!");
#endif
pthread_mutex_unlock(&thread->mutex);
return TRUE;
}
#if defined(WITH_DEBUG_THREADS)
VOID DumpThreadHandles(void)
{
char** msg;
size_t used, i;
void* stack = winpr_backtrace(20);
WLog_DBG(TAG, "---------------- Called from ----------------------------");
msg = winpr_backtrace_symbols(stack, &used);
for (i=0; i<used; i++)
WLog_DBG(TAG, "[%d]: %s", i, msg[i]);
free(msg);
winpr_backtrace_free(stack);
WLog_DBG(TAG, "---------------- Start Dumping thread handles -----------");
if (!thread_list)
WLog_DBG(TAG, "All threads properly shut down and disposed of.");
else
{
ULONG_PTR *keys = NULL;
ListDictionary_Lock(thread_list);
int x, count = ListDictionary_GetKeys(thread_list, &keys);
WLog_DBG(TAG, "Dumping %d elements\n", count);
for (x=0; x<count; x++)
{
WINPR_THREAD* thread = ListDictionary_GetItemValue(thread_list, (void*)keys[x]);
WLog_DBG(TAG, "Thread [%d] handle created still not closed!", x);
msg = winpr_backtrace_symbols(thread->create_stack, &used);
for (i=0; i<used; i++)
WLog_DBG(TAG, "[%d]: %s", i, msg[i]);
free(msg);
if (thread->started)
WLog_DBG(TAG, "Thread [%d] still running!", x);
else
{
WLog_DBG(TAG, "Thread [%d] exited at:", x);
msg = winpr_backtrace_symbols(thread->exit_stack, &used);
for (i=0; i<used; i++)
WLog_DBG(TAG, "[%d]: %s", i, msg[i]);
free(msg);
}
}
if (keys)
free(keys);
ListDictionary_Unlock(thread_list);
}
WLog_DBG(TAG, "---------------- End Dumping thread handles -------------");
}
#endif
#endif

View File

@ -28,14 +28,18 @@
#include "../handle/handle.h"
typedef void *(*pthread_start_routine)(void*);
typedef void *(*pthread_start_routine)(void *);
struct winpr_thread
{
WINPR_HANDLE_DEF();
BOOL started;
int pipe_fd[2];
BOOL mainProcess;
BOOL detached;
BOOL joined;
BOOL exited;
DWORD dwExitCode;
pthread_t thread;
SIZE_T dwStackSize;
@ -43,6 +47,10 @@ struct winpr_thread
pthread_mutex_t mutex;
LPTHREAD_START_ROUTINE lpStartAddress;
LPSECURITY_ATTRIBUTES lpThreadAttributes;
#if defined(WITH_DEBUG_THREADS)
void *create_stack[20];
void *exit_stack[20];
#endif
};
typedef struct winpr_thread WINPR_THREAD;