pcsc-lite  1.7.4
Data Structures | Defines | Typedefs | Functions | Variables
winscard_clnt.c File Reference
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/wait.h>
#include "misc.h"
#include "pcscd.h"
#include "winscard.h"
#include "debuglog.h"
#include "strlcpycat.h"
#include "readerfactory.h"
#include "eventhandler.h"
#include "sys_generic.h"
#include "winscard_msg.h"
#include "utils.h"

Go to the source code of this file.

Data Structures

struct  _psChannelMap
 Represents an Application Context Channel. More...
struct  _psContextMap
 Represents the an Application Context on the Client side. More...

Defines

#define SCARD_PROTOCOL_ANY_OLD   0x1000
 used for backward compatibility
#define TRUE   1
#define FALSE   0
#define COLOR_RED   "\33[01;31m"
#define COLOR_GREEN   "\33[32m"
#define COLOR_BLUE   "\33[34m"
#define COLOR_MAGENTA   "\33[35m"
#define COLOR_NORMAL   "\33[0m"
#define API_TRACE_IN(...)
#define API_TRACE_OUT(...)
#define PROFILE_START
#define PROFILE_END(rv)
#define CHECK_SAME_PROCESS

Typedefs

typedef struct _psChannelMap CHANNEL_MAP
typedef struct _psContextMap SCONTEXTMAP

Functions

static int CHANNEL_MAP_seeker (const void *el, const void *key)
static int SCONTEXTMAP_seeker (const void *el, const void *key)
static LONG SCardAddContext (SCARDCONTEXT hContext, DWORD dwClientID)
 Functions for managing instances of SCardEstablishContext() These functions keep track of Context handles and associate the blocking variable contextBlockStatus to an hContext.
static SCONTEXTMAPSCardGetContext (SCARDCONTEXT hContext)
 Get the index from the Application Context vector _psContextMap for the passed context.
static SCONTEXTMAPSCardGetContextTH (SCARDCONTEXT hContext)
 Get the address from the Application Context list _psContextMap for the passed context.
static LONG SCardRemoveContext (SCARDCONTEXT hContext)
 Removes an Application Context from a control vector.
static LONG SCardCleanContext (SCONTEXTMAP *)
static LONG SCardAddHandle (SCARDHANDLE, SCONTEXTMAP *, LPCSTR)
static LONG SCardGetContextAndChannelFromHandle (SCARDHANDLE, SCONTEXTMAP **, CHANNEL_MAP **)
static LONG SCardGetContextAndChannelFromHandleTH (SCARDHANDLE, SCONTEXTMAP **, CHANNEL_MAP **)
static LONG SCardRemoveHandle (SCARDHANDLE)
static LONG SCardGetSetAttrib (SCARDHANDLE hCard, int command, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
static LONG getReaderStates (SCONTEXTMAP *currentContextMap)
static LONG SCardLockThread (void)
 Locks a mutex so another thread must wait to use this function.
static LONG SCardUnlockThread (void)
 Unlocks a mutex so another thread may use the client.
static LONG SCardEstablishContextTH (DWORD dwScope,LPCVOID pvReserved1,LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
 Creates a communication context to the PC/SC Resource Manager.
LONG SCardEstablishContext (DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
 Creates an Application Context to the PC/SC Resource Manager.
LONG SCardReleaseContext (SCARDCONTEXT hContext)
 Destroys a communication context to the PC/SC Resource Manager.
LONG SCardConnect (SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
 Establishes a connection to the reader specified in * szReader.
LONG SCardReconnect (SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
 Reestablishes a connection to a reader that was previously connected to using SCardConnect().
LONG SCardDisconnect (SCARDHANDLE hCard, DWORD dwDisposition)
 Terminates a connection made through SCardConnect().
LONG SCardBeginTransaction (SCARDHANDLE hCard)
 Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
LONG SCardEndTransaction (SCARDHANDLE hCard, DWORD dwDisposition)
 Ends a previously begun transaction.
LONG SCardStatus (SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
 Returns the current status of the reader connected to by hCard.
LONG SCardGetStatusChange (SCARDCONTEXT hContext, DWORD dwTimeout, SCARD_READERSTATE *rgReaderStates, DWORD cReaders)
 Blocks execution until the current availability of the cards in a specific set of readers changes.
LONG SCardControl (SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
 Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.
LONG SCardGetAttrib (SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
 Get an attribute from the IFD Handler (reader driver).
LONG SCardSetAttrib (SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
 Set an attribute of the IFD Handler.
LONG SCardTransmit (SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
 Sends an APDU to the smart card contained in the reader connected to by SCardConnect().
LONG SCardListReaders (SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
 This function returns a list of currently available readers on the system.
LONG SCardFreeMemory (SCARDCONTEXT hContext, LPCVOID pvMem)
 Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length designator.
LONG SCardListReaderGroups (SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
 Returns a list of currently available reader groups on the system.
LONG SCardCancel (SCARDCONTEXT hContext)
 This function cancels all pending blocking requests on the SCardGetStatusChange() function.
LONG SCardIsValidContext (SCARDCONTEXT hContext)
 Check if a SCARDCONTEXT is valid.
LONG SCardCheckDaemonAvailability (void)
 Checks if the server is running.

Variables

static char sharing_shall_block = TRUE
static list_t contextMapList
static short isExecuted = 0
 Make sure the initialization code is executed only once.
static pthread_mutex_t clientMutex = PTHREAD_MUTEX_INITIALIZER
 Ensure that some functions be accessed in thread-safe mode.
static READER_STATE readerStates [PCSCLITE_MAX_READERS_CONTEXTS]
 Area used to read status information about the readers.
PCSC_API const SCARD_IO_REQUEST g_rgSCardT0Pci = { SCARD_PROTOCOL_T0, sizeof(SCARD_IO_REQUEST) }
 Protocol Control Information for T=0.
PCSC_API const SCARD_IO_REQUEST g_rgSCardT1Pci = { SCARD_PROTOCOL_T1, sizeof(SCARD_IO_REQUEST) }
 Protocol Control Information for T=1.
PCSC_API const SCARD_IO_REQUEST g_rgSCardRawPci = { SCARD_PROTOCOL_RAW, sizeof(SCARD_IO_REQUEST) }
 Protocol Control Information for raw access.

Detailed Description

Definition in file winscard_clnt.c.


Function Documentation

static LONG SCardAddContext ( SCARDCONTEXT  hContext,
DWORD  dwClientID 
) [static]

Functions for managing instances of SCardEstablishContext() These functions keep track of Context handles and associate the blocking variable contextBlockStatus to an hContext.

Adds an Application Context to the vector _psContextMap.

Parameters:
[in]hContextApplication Context ID.
[in]dwClientIDClient connection ID.
Returns:
Error code.
Return values:
SCARD_S_SUCCESSSuccess (SCARD_S_SUCCESS)
SCARD_E_NO_MEMORYThere is no free slot to store hContext (SCARD_E_NO_MEMORY)

Definition at line 3382 of file winscard_clnt.c.

References SCARD_E_NO_MEMORY, _psContextMap::hContext, _psContextMap::dwClientID, _psContextMap::cancellable, _psContextMap::mMutex, and SCARD_S_SUCCESS.

Referenced by SCardEstablishContextTH().

LONG SCardCheckDaemonAvailability ( void  )

Checks if the server is running.

If the server has been restarted we invalidate all the PC/SC handles. The client has to call SCardEstablishContext() again.

Returns:
Error code.
Return values:
SCARD_S_SUCCESSServer is running (SCARD_S_SUCCESS)
SCARD_E_NO_SERVICEServer is not running (SCARD_E_NO_SERVICE)
SCARD_E_INVALID_HANDLEServer was restarted or after fork() (SCARD_E_INVALID_HANDLE)

Definition at line 3666 of file winscard_clnt.c.

References SCARD_E_NO_SERVICE, and SCARD_S_SUCCESS.

Referenced by SCardEstablishContext(), and MessageReceiveTimeout().

static LONG SCardEstablishContextTH ( DWORD  dwScope,
LPCVOID  pvReserved1,
LPCVOID  pvReserved2,
LPSCARDCONTEXT  phContext 
) [static]

Creates a communication context to the PC/SC Resource Manager.

This function should not be called directly. Instead, the thread-safe function SCardEstablishContext() should be called.

Parameters:
[in]dwScopeScope of the establishment. This can either be a local or remote connection.
[in]pvReserved1Reserved for future use. Can be used for remote connection.
[in]pvReserved2Reserved for future use.
[out]phContextReturned reference to this connection.
Returns:
Connection status.
Return values:
SCARD_S_SUCCESSSuccessful (SCARD_S_SUCCESS)
SCARD_E_INVALID_PARAMETERphContext is null. (SCARD_E_INVALID_PARAMETER)
SCARD_E_INVALID_VALUEInvalid scope type passed (SCARD_E_INVALID_VALUE)
SCARD_E_NO_MEMORYThere is no free slot to store hContext (SCARD_E_NO_MEMORY)
SCARD_E_NO_SERVICEThe server is not runing (SCARD_E_NO_SERVICE)
SCARD_F_COMM_ERRORAn internal communications error has been detected (SCARD_F_COMM_ERROR)
SCARD_F_INTERNAL_ERRORAn internal consistency check failed (SCARD_F_INTERNAL_ERROR)

Definition at line 577 of file winscard_clnt.c.

References SCARD_E_INVALID_PARAMETER, SCARD_E_NO_MEMORY, ClientSetupSession(), SCARD_E_NO_SERVICE, version_struct::major, PROTOCOL_VERSION_MAJOR, version_struct::minor, PROTOCOL_VERSION_MINOR, SCARD_S_SUCCESS, MessageSendWithHeader(), CMD_VERSION, MessageReceive(), SCARD_F_COMM_ERROR, SCARD_ESTABLISH_CONTEXT, SCardGetContextTH(), and SCardAddContext().

Referenced by SCardEstablishContext().

static SCONTEXTMAP * SCardGetContext ( SCARDCONTEXT  hContext) [static]

Get the index from the Application Context vector _psContextMap for the passed context.

This function is a thread-safe wrapper to the function SCardGetContextTH().

Parameters:
[in]hContextApplication Context whose index will be find.
Returns:
Index corresponding to the Application Context or -1 if it is not found.

Definition at line 3454 of file winscard_clnt.c.

References SCardLockThread(), SCardGetContextTH(), and SCardUnlockThread().

Referenced by SCardReleaseContext(), SCardConnect(), SCardGetStatusChange(), SCardListReaders(), SCardFreeMemory(), SCardListReaderGroups(), SCardCancel(), and SCardIsValidContext().

static SCONTEXTMAP * SCardGetContextTH ( SCARDCONTEXT  hContext) [static]

Get the address from the Application Context list _psContextMap for the passed context.

This functions is not thread-safe and should not be called. Instead, call the function SCardGetContext().

Parameters:
[in]hContextApplication Context whose index will be find.
Returns:
Address corresponding to the Application Context or NULL if it is not found.

Definition at line 3477 of file winscard_clnt.c.

Referenced by SCardEstablishContextTH(), SCardGetContext(), and SCardRemoveContext().

static LONG SCardLockThread ( void  ) [inline, static]

Locks a mutex so another thread must wait to use this function.

Wrapper to the function pthread_mutex_lock().

Definition at line 392 of file winscard_clnt.c.

Referenced by SCardEstablishContext(), SCardReleaseContext(), and SCardGetContext().

static LONG SCardRemoveContext ( SCARDCONTEXT  hContext) [static]

Removes an Application Context from a control vector.

Parameters:
[in]hContextApplication Context to be removed.
Returns:
Error code.
Return values:
SCARD_S_SUCCESSSuccess (SCARD_S_SUCCESS)
SCARD_E_INVALID_HANDLEThe context hContext was not found (SCARD_E_INVALID_HANDLE)

Definition at line 3491 of file winscard_clnt.c.

References SCardGetContextTH(), and SCARD_E_INVALID_HANDLE.

Referenced by SCardReleaseContext().

static LONG SCardUnlockThread ( void  ) [inline, static]

Unlocks a mutex so another thread may use the client.

Wrapper to the function pthread_mutex_unlock().

Definition at line 402 of file winscard_clnt.c.

Referenced by SCardEstablishContext(), SCardReleaseContext(), and SCardGetContext().


Variable Documentation

pthread_mutex_t clientMutex = PTHREAD_MUTEX_INITIALIZER [static]

Ensure that some functions be accessed in thread-safe mode.

These function's names finishes with "TH".

Definition at line 339 of file winscard_clnt.c.