108#include <sys/types.h>
138static bool sharing_shall_block =
true;
140#define COLOR_RED "\33[01;31m"
141#define COLOR_GREEN "\33[32m"
142#define COLOR_BLUE "\33[34m"
143#define COLOR_MAGENTA "\33[35m"
144#define COLOR_NORMAL "\33[0m"
151static void trace(
const char *func,
const char direction,
const char *fmt, ...)
155 fprintf(stderr, COLOR_GREEN
"%c " COLOR_BLUE
"[%lX] " COLOR_GREEN
"%s ",
156 direction, pthread_self(), func);
158 fprintf(stderr, COLOR_MAGENTA);
160 vfprintf(stderr, fmt, args);
163 fprintf(stderr, COLOR_NORMAL
"\n");
166#define API_TRACE_IN(...) trace(__FUNCTION__, '<', __VA_ARGS__);
167#define API_TRACE_OUT(...) trace(__FUNCTION__, '>', __VA_ARGS__);
169#define API_TRACE_IN(...)
170#define API_TRACE_OUT(...)
175#define PROFILE_FILE "/tmp/pcsc_profile"
181pthread_t threads[MAX_THREADS];
182struct timeval profile_time_start[MAX_THREADS];
186#define PROFILE_START profile_start();
187#define PROFILE_END(rv) profile_end(__FUNCTION__, rv);
189static void profile_start(
void)
191 static bool initialized =
false;
200 sprintf(filename,
"%s-%d", PROFILE_FILE, getuid());
201 profile_fd = fopen(filename,
"a+");
202 if (NULL == profile_fd)
204 fprintf(stderr, COLOR_RED
"Can't open %s: %s" COLOR_NORMAL
"\n",
205 PROFILE_FILE, strerror(errno));
208 fprintf(profile_fd,
"\nStart a new profile\n");
210 if (isatty(fileno(stderr)))
217 for (i=0; i<MAX_THREADS; i++)
218 if (pthread_equal(0, threads[i]))
224 gettimeofday(&profile_time_start[i], NULL);
227static void profile_end(
const char *f, LONG rv)
229 struct timeval profile_time_end;
234 gettimeofday(&profile_time_end, NULL);
237 for (i=0; i<MAX_THREADS; i++)
238 if (pthread_equal(t, threads[i]))
243 fprintf(stderr, COLOR_BLUE
" WARNING: no start info for %s\n", f);
247 d =
time_sub(&profile_time_end, &profile_time_start[i]);
255 COLOR_RED
"RESULT %s " COLOR_MAGENTA
"%ld "
256 COLOR_BLUE
"0x%08lX" COLOR_NORMAL
"\n",
259 fprintf(profile_fd,
"%s %ld\n", f, d);
265#define PROFILE_END(rv)
280static int CHANNEL_MAP_seeker(
const void *el,
const void *key)
284 if ((el == NULL) || (key == NULL))
286 Log3(PCSC_LOG_CRITICAL,
287 "CHANNEL_MAP_seeker called with NULL pointer: el=%p, key=%p",
318static list_t contextMapList;
320static int SCONTEXTMAP_seeker(
const void *el,
const void *key)
324 if ((el == NULL) || (key == NULL))
326 Log3(PCSC_LOG_CRITICAL,
327 "SCONTEXTMAP_seeker called with NULL pointer: el=%p, key=%p",
342static pthread_once_t init_lib_control = PTHREAD_ONCE_INIT;
355static pthread_mutex_t readerStatesMutex = PTHREAD_MUTEX_INITIALIZER;
365static LONG SCardGetContextChannelAndLockFromHandle(
SCARDHANDLE,
367static LONG SCardGetContextAndChannelFromHandleTH(
SCARDHANDLE,
371static LONG SCardGetSetAttrib(
SCARDHANDLE hCard,
int command, DWORD dwAttrId,
372 LPBYTE pbAttr, LPDWORD pcbAttrLen);
374static LONG getReaderEvents(
SCONTEXTMAP * currentContextMap,
int *readerEvents);
375static LONG getReaderStates(
SCONTEXTMAP * currentContextMap);
376static LONG getReaderStatesAndRegisterForEvents(
SCONTEXTMAP * currentContextMap);
377static LONG unregisterFromEvents(
SCONTEXTMAP * currentContextMap);
420 return currentContextMap != NULL;
462 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
466 API_TRACE_IN(
"%ld, %p, %p", dwScope, pvReserved1, pvReserved2)
476 pvReserved2, phContext);
481 API_TRACE_OUT(
"%ld", *phContext)
487DESTRUCTOR
static void destructor(
void)
489 list_destroy(&contextMapList);
497static void init_lib(
void)
504 lrv = list_init(&contextMapList);
507 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d",
512 lrv = list_attributes_seeker(&contextMapList,
516 Log2(PCSC_LOG_CRITICAL,
517 "list_attributes_seeker failed with return value: %d", lrv);
518 list_destroy(&contextMapList);
524 Log1(PCSC_LOG_INFO,
"Disable shared blocking");
525 sharing_shall_block =
false;
560 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
564 uint32_t dwClientID = 0;
568 if (phContext == NULL)
573 pthread_once(&init_lib_control, init_lib);
599 Log1(PCSC_LOG_CRITICAL,
600 "Your pcscd is too old and does not support CMD_VERSION");
604 Log3(PCSC_LOG_INFO,
"Server is protocol version %d:%d",
618 scEstablishStruct.dwScope = dwScope;
619 scEstablishStruct.hContext = 0;
623 sizeof(scEstablishStruct), (
void *) &scEstablishStruct);
631 rv =
MessageReceive(&scEstablishStruct,
sizeof(scEstablishStruct),
639 rv = scEstablishStruct.rv;
649 *phContext = scEstablishStruct.hContext;
691 API_TRACE_IN(
"%ld", hContext)
699 if (NULL == currentContextMap)
705 scReleaseStruct.hContext = hContext;
709 currentContextMap->dwClientID,
710 sizeof(scReleaseStruct), (
void *) &scReleaseStruct);
719 currentContextMap->dwClientID);
724 rv = scReleaseStruct.rv;
726 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
798 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
799 LPDWORD pdwActiveProtocol)
806 API_TRACE_IN(
"%ld %s %ld %ld", hContext, szReader, dwShareMode, dwPreferredProtocols)
811 if (phCard == NULL || pdwActiveProtocol == NULL)
816 if (szReader == NULL)
822 if (strlen(szReader) > MAX_READERNAME)
829 if (NULL == currentContextMap)
832 memset(scConnectStruct.szReader, 0,
sizeof scConnectStruct.szReader);
833 strncpy(scConnectStruct.szReader, szReader,
sizeof scConnectStruct.szReader);
834 scConnectStruct.szReader[
sizeof scConnectStruct.szReader -1] =
'\0';
836 scConnectStruct.hContext = hContext;
837 scConnectStruct.dwShareMode = dwShareMode;
838 scConnectStruct.dwPreferredProtocols = dwPreferredProtocols;
839 scConnectStruct.hCard = 0;
840 scConnectStruct.dwActiveProtocol = 0;
844 sizeof(scConnectStruct), (
void *) &scConnectStruct);
853 currentContextMap->dwClientID);
858 *phCard = scConnectStruct.hCard;
859 *pdwActiveProtocol = scConnectStruct.dwActiveProtocol;
866 rv = SCardAddHandle(*phCard, currentContextMap, szReader);
869 rv = scConnectStruct.rv;
872 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
875 API_TRACE_OUT(
"%d", *pdwActiveProtocol)
953 DWORD dwPreferredProtocols, DWORD dwInitialization,
954 LPDWORD pdwActiveProtocol)
962 API_TRACE_IN(
"%ld %ld %ld", hCard, dwShareMode, dwPreferredProtocols)
964 if (pdwActiveProtocol == NULL)
973 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
978 scReconnectStruct.hCard = hCard;
979 scReconnectStruct.dwShareMode = dwShareMode;
980 scReconnectStruct.dwPreferredProtocols = dwPreferredProtocols;
981 scReconnectStruct.dwInitialization = dwInitialization;
982 scReconnectStruct.dwActiveProtocol = *pdwActiveProtocol;
986 sizeof(scReconnectStruct), (
void *) &scReconnectStruct);
994 rv =
MessageReceive(&scReconnectStruct,
sizeof(scReconnectStruct),
995 currentContextMap->dwClientID);
1000 rv = scReconnectStruct.rv;
1004 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1009 *pdwActiveProtocol = scReconnectStruct.dwActiveProtocol;
1012 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1015 API_TRACE_OUT(
"%ld", *pdwActiveProtocol)
1059 API_TRACE_IN(
"%ld %ld", hCard, dwDisposition)
1064 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1072 scDisconnectStruct.hCard = hCard;
1073 scDisconnectStruct.dwDisposition = dwDisposition;
1077 sizeof(scDisconnectStruct), (
void *) &scDisconnectStruct);
1085 rv =
MessageReceive(&scDisconnectStruct,
sizeof(scDisconnectStruct),
1086 currentContextMap->dwClientID);
1092 SCardRemoveHandle(hCard);
1093 rv = scDisconnectStruct.rv;
1096 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1150 API_TRACE_IN(
"%ld", hCard)
1162 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1167 scBeginStruct.hCard = hCard;
1171 currentContextMap->dwClientID,
1172 sizeof(scBeginStruct), (
void *) &scBeginStruct);
1181 currentContextMap->dwClientID);
1186 rv = scBeginStruct.rv;
1191 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1195 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1250 API_TRACE_IN(
"%ld", hCard)
1255 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1260 scEndStruct.hCard = hCard;
1261 scEndStruct.dwDisposition = dwDisposition;
1265 currentContextMap->dwClientID,
1266 sizeof(scEndStruct), (
void *) &scEndStruct);
1275 currentContextMap->dwClientID);
1280 rv = scEndStruct.rv;
1283 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1387 LPDWORD pcchReaderLen, LPDWORD pdwState,
1388 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1390 DWORD dwReaderLen, dwAtrLen;
1397 char *bufReader = NULL;
1398 LPBYTE bufAtr = NULL;
1411 if (pcchReaderLen == NULL)
1412 pcchReaderLen = &dummy;
1414 if (pcbAtrLen == NULL)
1418 dwReaderLen = *pcchReaderLen;
1419 dwAtrLen = *pcbAtrLen;
1430 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1436 (void)pthread_mutex_lock(&readerStatesMutex);
1439 rv = getReaderStates(currentContextMap);
1443 r = pChannelMap->readerName;
1458 memset(&scStatusStruct, 0,
sizeof(scStatusStruct));
1459 scStatusStruct.hCard = hCard;
1462 sizeof(scStatusStruct), (
void *) &scStatusStruct);
1471 currentContextMap->dwClientID);
1476 rv = scStatusStruct.rv;
1480 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1481 (void)pthread_mutex_unlock(&readerStatesMutex);
1498 *pcchReaderLen = strlen(pChannelMap->readerName) + 1;
1509 dwReaderLen = *pcchReaderLen;
1510 if (NULL == szReaderName)
1515 bufReader = malloc(dwReaderLen);
1516 if (NULL == bufReader)
1521 *(
char **)szReaderName = bufReader;
1524 bufReader = szReaderName;
1529 if (*pcchReaderLen > dwReaderLen)
1532 strncpy(bufReader, pChannelMap->readerName, dwReaderLen);
1537 dwAtrLen = *pcbAtrLen;
1543 bufAtr = malloc(dwAtrLen);
1549 *(LPBYTE *)pbAtr = bufAtr;
1556 if (*pcbAtrLen > dwAtrLen)
1559 memcpy(bufAtr,
readerStates[i].cardAtr, min(*pcbAtrLen, dwAtrLen));
1563 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1564 (void)pthread_mutex_unlock(&readerStatesMutex);
1688 DWORD dwBreakFlag = 0;
1691 int currentReaderCount = 0;
1693 int pnp_reader = -1;
1696 API_TRACE_IN(
"%ld %ld %d", hContext, dwTimeout, cReaders)
1698 for (j=0; j<cReaders; j++)
1700 API_TRACE_IN(
"[%d] %s %lX %lX (%d)", j, rgReaderStates[j].szReader,
1701 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState,
1702 rgReaderStates[j].cbAtr)
1706 if ((rgReaderStates == NULL && cReaders > 0)
1714 for (j = 0; j < cReaders; j++)
1716 if (rgReaderStates[j].szReader == NULL)
1723 int nbNonIgnoredReaders = cReaders;
1725 for (j=0; j<cReaders; j++)
1727 nbNonIgnoredReaders--;
1729 if (0 == nbNonIgnoredReaders)
1746 if (NULL == currentContextMap)
1753 (void)pthread_mutex_lock(&readerStatesMutex);
1756 rv = getReaderStatesAndRegisterForEvents(currentContextMap);
1760 (void)pthread_mutex_unlock(&readerStatesMutex);
1765 for (j=0; j<cReaders; j++)
1767 const char *readerName;
1770 readerName = rgReaderStates[j].szReader;
1773 if (strcmp(readerName,
readerStates[i].readerName) == 0)
1781 if (strcasecmp(readerName,
"\\\\?PnP?\\Notification") != 0)
1784 (void)pthread_mutex_unlock(&readerStatesMutex);
1791 (void)pthread_mutex_unlock(&readerStatesMutex);
1794 for (j = 0; j < cReaders; j++)
1795 rgReaderStates[j].dwEventState = 0;
1798 Log2(PCSC_LOG_DEBUG,
"Event Loop Start, dwTimeout: %ld", dwTimeout);
1801 if (pnp_reader >= 0)
1804 currReader = &rgReaderStates[pnp_reader];
1807 if (
SCARD_S_SUCCESS == getReaderEvents(currentContextMap, &readerEvents))
1809 int previousReaderEvents = currReader->dwCurrentState >> 16;
1812 currReader->dwEventState = (currReader->dwEventState & 0xFFFF) + (readerEvents << 16);
1815 (previousReaderEvents != readerEvents)
1818 && previousReaderEvents)
1830 currentReaderCount++;
1833 if ((DWORD)-1 == dwTimeout)
1843 currReader = &rgReaderStates[j];
1848 const char *readerName;
1852 (void)pthread_mutex_lock(&readerStatesMutex);
1855 readerName = currReader->szReader;
1858 if (strcmp(readerName,
readerStates[i].readerName) == 0)
1866 if (strcasecmp(readerName,
"\\\\?PnP?\\Notification") == 0)
1868 int k, newReaderCount = 0;
1874 if (newReaderCount != currentReaderCount)
1878 Log1(PCSC_LOG_INFO,
"Reader list changed");
1879 currentReaderCount = newReaderCount;
1881 if (
SCARD_S_SUCCESS == getReaderEvents(currentContextMap, &readerEvents))
1884 currReader->dwEventState = (currReader->dwEventState & 0xFFFF) + (readerEvents << 16);
1893 currReader->dwEventState =
1909 uint32_t readerState;
1915 currReader->dwEventState &= ~SCARD_STATE_UNKNOWN;
1916 Log0(PCSC_LOG_DEBUG);
1927 if (currReader->dwCurrentState & 0xFFFF0000)
1929 unsigned int currentCounter;
1931 currentCounter = (currReader->dwCurrentState >> 16) & 0xFFFF;
1937 Log0(PCSC_LOG_DEBUG);
1943 currReader->dwEventState = ((currReader->dwEventState & 0xffff )
1955 Log0(PCSC_LOG_DEBUG);
1964 currReader->dwEventState &= ~SCARD_STATE_UNAVAILABLE;
1966 Log0(PCSC_LOG_DEBUG);
1974#ifndef DISABLE_AUTO_POWER_ON
1978 (void)
SYS_USleep(PCSCLITE_STATUS_POLL_RATE + 10);
1982 memcpy(currReader->rgbAtr, rContext->
cardAtr,
1986 currReader->cbAtr = 0;
1992 currReader->dwEventState &= ~SCARD_STATE_PRESENT;
1993 currReader->dwEventState &= ~SCARD_STATE_UNAWARE;
1994 currReader->dwEventState &= ~SCARD_STATE_IGNORE;
1995 currReader->dwEventState &= ~SCARD_STATE_UNKNOWN;
1996 currReader->dwEventState &= ~SCARD_STATE_UNAVAILABLE;
1997 currReader->dwEventState &= ~SCARD_STATE_ATRMATCH;
1998 currReader->dwEventState &= ~SCARD_STATE_MUTE;
1999 currReader->dwEventState &= ~SCARD_STATE_INUSE;
2005 Log0(PCSC_LOG_DEBUG);
2013 currReader->dwEventState &= ~SCARD_STATE_EMPTY;
2014 currReader->dwEventState &= ~SCARD_STATE_UNAWARE;
2015 currReader->dwEventState &= ~SCARD_STATE_IGNORE;
2016 currReader->dwEventState &= ~SCARD_STATE_UNKNOWN;
2017 currReader->dwEventState &= ~SCARD_STATE_UNAVAILABLE;
2018 currReader->dwEventState &= ~SCARD_STATE_MUTE;
2023 Log0(PCSC_LOG_DEBUG);
2033 Log0(PCSC_LOG_DEBUG);
2043 Log0(PCSC_LOG_DEBUG);
2053 currReader->dwEventState &= ~SCARD_STATE_INUSE;
2057 Log0(PCSC_LOG_DEBUG);
2067 currReader->dwEventState &= ~SCARD_STATE_EXCLUSIVE;
2071 Log0(PCSC_LOG_DEBUG);
2078 currReader->dwEventState &= ~SCARD_STATE_INUSE;
2079 currReader->dwEventState &= ~SCARD_STATE_EXCLUSIVE;
2084 Log0(PCSC_LOG_DEBUG);
2087 else if (currReader-> dwCurrentState
2091 Log0(PCSC_LOG_DEBUG);
2103 Log0(PCSC_LOG_DEBUG);
2108 (void)pthread_mutex_unlock(&readerStatesMutex);
2121 if (dwBreakFlag == 1)
2127 struct timeval before, after;
2129 gettimeofday(&before, NULL);
2140 &waitStatusStruct,
sizeof(waitStatusStruct),
2151 rv = unregisterFromEvents(currentContextMap);
2160 rv = waitStatusStruct.rv;
2165 (void)pthread_mutex_lock(&readerStatesMutex);
2166 rv = getReaderStatesAndRegisterForEvents(currentContextMap);
2167 (void)pthread_mutex_unlock(&readerStatesMutex);
2175 gettimeofday(&after, NULL);
2177 dwTime -= diff/1000;
2197 Log1(PCSC_LOG_DEBUG,
"Event Loop End");
2202 (void)unregisterFromEvents(currentContextMap);
2204 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2209 for (j=0; j<cReaders; j++)
2211 API_TRACE_OUT(
"[%d] %s %lX %lX (%d)", j, rgReaderStates[j].szReader,
2212 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState,
2213 rgReaderStates[j].cbAtr)
2271 DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength,
2272 LPDWORD lpBytesReturned)
2282 if (NULL != lpBytesReturned)
2283 *lpBytesReturned = 0;
2288 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2302 scControlStruct.hCard = hCard;
2303 scControlStruct.dwControlCode = dwControlCode;
2304 scControlStruct.cbSendLength = cbSendLength;
2305 scControlStruct.cbRecvLength = cbRecvLength;
2306 scControlStruct.dwBytesReturned = 0;
2307 scControlStruct.rv = 0;
2310 sizeof(scControlStruct), &scControlStruct);
2316 rv =
MessageSend((
char *)pbSendBuffer, cbSendLength,
2317 currentContextMap->dwClientID);
2326 currentContextMap->dwClientID);
2333 if (scControlStruct.dwBytesReturned > cbRecvLength)
2335 if (NULL != lpBytesReturned)
2336 *lpBytesReturned = scControlStruct.dwBytesReturned;
2342 rv =
MessageReceive(pbRecvBuffer, scControlStruct.dwBytesReturned,
2343 currentContextMap->dwClientID);
2350 if (NULL != lpBytesReturned)
2351 *lpBytesReturned = scControlStruct.dwBytesReturned;
2353 rv = scControlStruct.rv;
2356 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
2485 unsigned char *buf = NULL;
2489 if (NULL == pcbAttrLen)
2501 buf = malloc(*pcbAttrLen);
2508 *(
unsigned char **)pbAttr = buf;
2571 if (NULL == pbAttr || 0 == cbAttrLen)
2582static LONG SCardGetSetAttrib(
SCARDHANDLE hCard,
int command, DWORD dwAttrId,
2583 LPBYTE pbAttr, LPDWORD pcbAttrLen)
2593 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2604 scGetSetStruct.hCard = hCard;
2605 scGetSetStruct.dwAttrId = dwAttrId;
2607 memset(scGetSetStruct.pbAttr, 0,
sizeof(scGetSetStruct.pbAttr));
2610 memcpy(scGetSetStruct.pbAttr, pbAttr, *pcbAttrLen);
2611 scGetSetStruct.cbAttrLen = *pcbAttrLen;
2615 scGetSetStruct.cbAttrLen =
sizeof scGetSetStruct.pbAttr;
2618 sizeof(scGetSetStruct), &scGetSetStruct);
2627 currentContextMap->dwClientID);
2637 if (*pcbAttrLen < scGetSetStruct.cbAttrLen)
2641 DWORD correct_value = scGetSetStruct.cbAttrLen;
2642 scGetSetStruct.cbAttrLen = *pcbAttrLen;
2643 *pcbAttrLen = correct_value;
2648 *pcbAttrLen = scGetSetStruct.cbAttrLen;
2651 memcpy(pbAttr, scGetSetStruct.pbAttr, scGetSetStruct.cbAttrLen);
2653 memset(scGetSetStruct.pbAttr, 0x00,
sizeof(scGetSetStruct.pbAttr));
2655 rv = scGetSetStruct.rv;
2658 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
2722 LPCBYTE pbSendBuffer, DWORD cbSendLength,
2724 LPDWORD pcbRecvLength)
2733 if (pbSendBuffer == NULL || pbRecvBuffer == NULL ||
2734 pcbRecvLength == NULL || pioSendPci == NULL)
2743 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2758 scTransmitStruct.hCard = hCard;
2759 scTransmitStruct.cbSendLength = cbSendLength;
2760 scTransmitStruct.pcbRecvLength = *pcbRecvLength;
2761 scTransmitStruct.ioSendPciProtocol = pioSendPci->
dwProtocol;
2762 scTransmitStruct.ioSendPciLength = pioSendPci->
cbPciLength;
2767 scTransmitStruct.ioRecvPciProtocol = pioRecvPci->
dwProtocol;
2768 scTransmitStruct.ioRecvPciLength = pioRecvPci->
cbPciLength;
2777 sizeof(scTransmitStruct), (
void *) &scTransmitStruct);
2783 rv =
MessageSend((
void *)pbSendBuffer, cbSendLength,
2800 if (scTransmitStruct.pcbRecvLength > *pcbRecvLength)
2802 *pcbRecvLength = scTransmitStruct.pcbRecvLength;
2808 rv =
MessageReceive(pbRecvBuffer, scTransmitStruct.pcbRecvLength,
2816 pioRecvPci->
dwProtocol = scTransmitStruct.ioRecvPciProtocol;
2817 pioRecvPci->
cbPciLength = scTransmitStruct.ioRecvPciLength;
2821 rv = scTransmitStruct.rv;
2825 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2830 *pcbRecvLength = scTransmitStruct.pcbRecvLength;
2833 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2903 LPSTR mszReaders, LPDWORD pcchReaders)
2905 DWORD dwReadersLen = 0;
2913 API_TRACE_IN(
"%ld", hContext)
2918 if (pcchReaders == NULL)
2925 if (NULL == currentContextMap)
2932 (void)pthread_mutex_lock(&readerStatesMutex);
2935 rv = getReaderStates(currentContextMap);
2942 dwReadersLen += strlen(
readerStates[i].readerName) + 1;
2947 if (1 == dwReadersLen)
2955 if (NULL == mszReaders)
2960 buf = malloc(dwReadersLen);
2966 *(
char **)mszReaders = buf;
2973 if ((NULL != mszReaders) && (*pcchReaders < dwReadersLen))
2980 if (mszReaders == NULL)
2998 *pcchReaders = dwReadersLen;
3000 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
3001 (void)pthread_mutex_unlock(&readerStatesMutex);
3004 API_TRACE_OUT(
"%d", *pcchReaders)
3034 free((
void *)pvMem);
3102 const char ReaderGroup[] =
"SCard$DefaultReaders\0";
3103 const unsigned int dwGroups =
sizeof(ReaderGroup);
3109 if (NULL == currentContextMap)
3114 if (NULL == mszGroups)
3119 buf = malloc(dwGroups);
3125 *(
char **)mszGroups = buf;
3131 if ((NULL != mszGroups) && (*pcchGroups < dwGroups))
3139 memcpy(buf, ReaderGroup, dwGroups);
3142 *pcchGroups = dwGroups;
3144 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
3186 uint32_t dwClientID = 0;
3191 API_TRACE_IN(
"%ld", hContext)
3199 if (NULL == currentContextMap)
3221 scCancelStruct.hContext = hContext;
3225 sizeof(scCancelStruct), (
void *) &scCancelStruct);
3233 rv =
MessageReceive(&scCancelStruct,
sizeof(scCancelStruct), dwClientID);
3238 rv = scCancelStruct.rv;
3277 API_TRACE_IN(
"%ld", hContext)
3315 if (NULL == newContextMap)
3318 Log2(PCSC_LOG_DEBUG,
"Allocating new SCONTEXTMAP @%p", newContextMap);
3319 newContextMap->
hContext = hContext;
3323 (void)pthread_mutex_init(&newContextMap->
mMutex, NULL);
3325 lrv = list_init(&newContextMap->channelMapList);
3328 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
3332 lrv = list_attributes_seeker(&newContextMap->channelMapList,
3333 CHANNEL_MAP_seeker);
3336 Log2(PCSC_LOG_CRITICAL,
3337 "list_attributes_seeker failed with return value: %d", lrv);
3338 list_destroy(&newContextMap->channelMapList);
3342 lrv = list_append(&contextMapList, newContextMap);
3345 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
3347 list_destroy(&newContextMap->channelMapList);
3355 (void)pthread_mutex_destroy(&newContextMap->
mMutex);
3356 free(newContextMap);
3385 if (NULL != currentContextMap)
3386 (void)pthread_mutex_lock(¤tContextMap->
mMutex);
3390 return currentContextMap;
3407 return list_seek(&contextMapList, &hContext);
3421 if (NULL != currentContextMap)
3422 SCardCleanContext(currentContextMap);
3425static void SCardCleanContext(
SCONTEXTMAP * targetContextMap)
3427 int list_index, lrv;
3434 (void)pthread_mutex_destroy(&targetContextMap->
mMutex);
3436 listSize = list_size(&targetContextMap->channelMapList);
3437 for (list_index = 0; list_index < listSize; list_index++)
3439 currentChannelMap = list_get_at(&targetContextMap->channelMapList,
3441 if (NULL == currentChannelMap)
3443 Log2(PCSC_LOG_CRITICAL,
"list_get_at failed for index %d",
3449 free(currentChannelMap->readerName);
3450 free(currentChannelMap);
3454 list_destroy(&targetContextMap->channelMapList);
3456 lrv = list_delete(&contextMapList, targetContextMap);
3459 Log2(PCSC_LOG_CRITICAL,
3460 "list_delete failed with return value: %d", lrv);
3463 free(targetContextMap);
3479 if (NULL == newChannelMap)
3482 newChannelMap->hCard = hCard;
3483 newChannelMap->readerName = strdup(readerName);
3485 lrv = list_append(¤tContextMap->channelMapList, newChannelMap);
3488 free(newChannelMap->readerName);
3489 free(newChannelMap);
3490 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
3505 rv = SCardGetContextAndChannelFromHandleTH(hCard, ¤tContextMap,
3506 ¤tChannelMap);
3510 free(currentChannelMap->readerName);
3512 lrv = list_delete(¤tContextMap->channelMapList, currentChannelMap);
3515 Log2(PCSC_LOG_CRITICAL,
3516 "list_delete failed with return value: %d", lrv);
3519 free(currentChannelMap);
3524static LONG SCardGetContextChannelAndLockFromHandle(
SCARDHANDLE hCard,
3533 rv = SCardGetContextAndChannelFromHandleTH(hCard, targetContextMap,
3537 (void)pthread_mutex_lock(&(*targetContextMap)->mMutex);
3544static LONG SCardGetContextAndChannelFromHandleTH(
SCARDHANDLE hCard,
3553 *targetContextMap = NULL;
3554 *targetChannelMap = NULL;
3556 listSize = list_size(&contextMapList);
3558 for (list_index = 0; list_index < listSize; list_index++)
3560 currentContextMap = list_get_at(&contextMapList, list_index);
3561 if (currentContextMap == NULL)
3563 Log2(PCSC_LOG_CRITICAL,
"list_get_at failed for index %d",
3567 currentChannelMap = list_seek(¤tContextMap->channelMapList,
3569 if (currentChannelMap != NULL)
3571 *targetContextMap = currentContextMap;
3572 *targetChannelMap = currentChannelMap;
3590 struct stat statBuffer;
3593 socketName = getSocketName();
3594 rv = stat(socketName, &statBuffer);
3598 Log3(PCSC_LOG_INFO,
"PCSC Not Running: %s: %s",
3599 socketName, strerror(errno));
3606static LONG getReaderEvents(
SCONTEXTMAP * currentContextMap,
int *readerEvents)
3608 int32_t dwClientID = currentContextMap->
dwClientID;
3626static LONG getReaderStates(
SCONTEXTMAP * currentContextMap)
3628 int32_t dwClientID = currentContextMap->
dwClientID;
3643static LONG getReaderStatesAndRegisterForEvents(
SCONTEXTMAP * currentContextMap)
3645 int32_t dwClientID = currentContextMap->
dwClientID;
3659static LONG unregisterFromEvents(
SCONTEXTMAP * currentContextMap)
3661 int32_t dwClientID = currentContextMap->
dwClientID;
3667 dwClientID, 0, NULL);
3684 rv = waitStatusStruct.rv;
This handles card insertion/removal events, updates ATR, protocol, and status information.
#define PCSCLITE_SHARING_EXCLUSIVE_CONTEXT
Reader used in exclusive mode.
#define PCSCLITE_SHARING_NO_CONTEXT
No application is using the reader.
#define PCSCLITE_SHARING_LAST_CONTEXT
One application is using the reader.
LONG SCardFreeMemory(SCARDCONTEXT hContext, LPCVOID pvMem)
Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length ...
LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
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 SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
Establishes a connection to the reader specified in * szReader.
LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.
LONG SCardIsValidContext(SCARDCONTEXT hContext)
Check if a SCARDCONTEXT is valid.
LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
Returns a list of currently available readers on the system.
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 SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
Returns a list of currently available reader groups on the system.
LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
LONG SCardCancel(SCARDCONTEXT hContext)
Cancels a specific blocking SCardGetStatusChange() function.
LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
Get an attribute from the IFD Handler (reader driver).
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 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 SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a series of commands in a transaction.
LONG SCardStatus(SCARDHANDLE hCard, LPSTR szReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
Returns the current status of the reader connected to by hCard.
LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define SCARD_E_UNKNOWN_READER
The specified reader name is not recognized.
#define SCARD_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
#define SCARD_E_CANCELLED
The action was cancelled by an SCardCancel request.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
#define SCARD_E_NO_READERS_AVAILABLE
Cannot find a smart card reader.
#define SCARD_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
#define SCARD_E_READER_UNAVAILABLE
The specified reader is not currently available for use.
#define SCARD_STATE_IGNORE
Ignore this reader.
#define SCARD_SWALLOWED
Card not powered.
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_PRESENT
Card is present.
#define SCARD_STATE_INUSE
Shared Mode.
#define SCARD_AUTOALLOCATE
see SCardFreeMemory()
#define SCARD_STATE_UNAVAILABLE
Status unavailable.
#define SCARD_STATE_PRESENT
Card inserted.
#define SCARD_ABSENT
Card is absent.
#define SCARD_UNKNOWN
Unknown state.
#define SCARD_STATE_UNKNOWN
Reader unknown.
#define INFINITE
Infinite timeout.
#define SCARD_STATE_EMPTY
Card removed.
#define SCARD_STATE_MUTE
Unresponsive card.
#define SCARD_STATE_CHANGED
State has changed.
#define SCARD_PROTOCOL_ANY
IFD determines prot.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
#define MAX_BUFFER_SIZE_EXTENDED
enhanced (64K + APDU + Lc + Le + SW) Tx/Rx Buffer
#define SCARD_STATE_EXCLUSIVE
Exclusive Mode.
#define SCARD_STATE_UNAWARE
App wants status.
LONG SCARDHANDLE
hCard returned by SCardConnect()
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
This keeps track of a list of currently available reader structures.
Protocol Control Information (PCI)
unsigned long dwProtocol
Protocol identifier.
unsigned long cbPciLength
Protocol Control Inf Length.
Represents an Application Context Channel.
Represents an Application Context on the Client side.
pthread_mutex_t mMutex
Mutex for this context.
SCARDCONTEXT hContext
Application Context ID.
DWORD dwClientID
Client Connection ID.
bool cancellable
We are in a cancellable call.
contained in SCARD_BEGIN_TRANSACTION Messages.
contained in SCARD_CANCEL Messages.
contained in SCARD_CONNECT Messages.
contained in SCARD_CONTROL Messages.
contained in SCARD_DISCONNECT Messages.
contained in SCARD_END_TRANSACTION Messages.
Information contained in SCARD_ESTABLISH_CONTEXT Messages.
contained in SCARD_GET_ATTRIB and Messages.
Define an exported public reader state structure so each application gets instant notification of cha...
_Atomic int32_t readerSharing
PCSCLITE_SHARING_* sharing status.
uint32_t cardProtocol
SCARD_PROTOCOL_* value.
UCHAR cardAtr[MAX_ATR_SIZE]
ATR.
uint32_t eventCounter
number of card events
_Atomic uint32_t cardAtrLength
ATR length.
uint32_t readerState
SCARD_* bit field.
contained in SCARD_RECONNECT Messages.
Information contained in SCARD_RELEASE_CONTEXT Messages.
contained in SCARD_STATUS Messages.
contained in SCARD_TRANSMIT Messages.
Information transmitted in CMD_VERSION Messages.
int32_t major
IPC major PROTOCOL_VERSION_MAJOR.
int32_t minor
IPC minor PROTOCOL_VERSION_MINOR.
Information contained in CMD_WAIT_READER_STATE_CHANGE Messages.
This handles abstract system level calls.
const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
long int time_sub(struct timeval *a, struct timeval *b)
return the difference (as long int) in µs between 2 struct timeval r = a - b
This handles smart card reader communications.
static bool isExecuted
Make sure the initialization code is executed only once.
static void SCardLockThread(void)
Locks a mutex so another thread must wait to use this function.
static void SCardRemoveContext(SCARDCONTEXT)
Removes an Application Context from a control vector.
static SCONTEXTMAP * SCardGetAndLockContext(SCARDCONTEXT)
Get the SCONTEXTMAP * from the Application Context vector _psContextMap for the passed context.
static bool SCardGetContextValidity(SCARDCONTEXT hContext)
Tell if a context index from the Application Context vector _psContextMap is valid or not.
static void SCardUnlockThread(void)
Unlocks a mutex so another thread may use the client.
static pthread_mutex_t clientMutex
Ensure that some functions be accessed in thread-safe mode.
LONG SCardCheckDaemonAvailability(void)
Checks if the server is running.
static SCONTEXTMAP * SCardGetContextTH(SCARDCONTEXT)
Get the address from the Application Context list _psContextMap for the passed context.
static LONG SCardEstablishContextTH(DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT)
Creates a communication context to the PC/SC Resource Manager.
static LONG SCardAddContext(SCARDCONTEXT, DWORD)
Functions for managing instances of SCardEstablishContext() These functions keep track of Context han...
static READER_STATE readerStates[PCSCLITE_MAX_READERS_CONTEXTS]
Area used to read status information about the readers.
INTERNAL int ClientSetupSession(uint32_t *pdwClientID)
Prepares a communication channel for the client to talk to the server.
INTERNAL LONG MessageReceiveTimeout(uint32_t command, void *buffer_void, uint64_t buffer_size, int32_t filedes, long timeOut)
Called by the Client to get the response from the server or vice-versa.
INTERNAL LONG MessageSendWithHeader(uint32_t command, uint32_t dwClientID, uint64_t size, void *data_void)
Wrapper for the MessageSend() function.
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Sends a menssage from client to server or vice-versa.
INTERNAL void ClientCloseSession(uint32_t dwClientID)
Closes the socket used by the client to communicate with the server.
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Called by the Client to get the response from the server or vice-versa.
This defines some structures and #defines to be used over the transport layer.
#define PROTOCOL_VERSION_MAJOR
Major version of the current message protocol.
#define PROTOCOL_VERSION_MINOR
Minor version of the current message protocol.
@ SCARD_DISCONNECT
used by SCardDisconnect()
@ SCARD_SET_ATTRIB
used by SCardSetAttrib()
@ SCARD_RELEASE_CONTEXT
used by SCardReleaseContext()
@ CMD_STOP_WAITING_READER_STATE_CHANGE
stop waiting for a reader state change
@ CMD_GET_READERS_STATE
get the readers state
@ SCARD_CONTROL
used by SCardControl()
@ CMD_VERSION
get the client/server protocol version
@ CMD_WAIT_READER_STATE_CHANGE
wait for a reader state change
@ SCARD_RECONNECT
used by SCardReconnect()
@ SCARD_STATUS
used by SCardStatus()
@ SCARD_GET_ATTRIB
used by SCardGetAttrib()
@ CMD_GET_READER_EVENTS
get the number of reader events
@ SCARD_BEGIN_TRANSACTION
used by SCardBeginTransaction()
@ SCARD_TRANSMIT
used by SCardTransmit()
@ SCARD_END_TRANSACTION
used by SCardEndTransaction()
@ SCARD_CANCEL
used by SCardCancel()
@ SCARD_CONNECT
used by SCardConnect()
@ SCARD_ESTABLISH_CONTEXT
used by SCardEstablishContext()