108#include <sys/types.h>
137static bool sharing_shall_block =
true;
138static int Protocol_version;
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)
282 const CHANNEL_MAP * channelMap = el;
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;
321static int SCONTEXTMAP_seeker(
const void *el,
const void *key)
325 if ((el == NULL) || (key == NULL))
327 Log3(PCSC_LOG_CRITICAL,
328 "SCONTEXTMAP_seeker called with NULL pointer: el=%p, key=%p",
343static pthread_once_t init_lib_control = PTHREAD_ONCE_INIT;
356static pthread_mutex_t readerStatesMutex = PTHREAD_MUTEX_INITIALIZER;
366static LONG SCardGetContextChannelAndLockFromHandle(
SCARDHANDLE,
368static LONG SCardGetContextAndChannelFromHandleTH(
SCARDHANDLE,
372static LONG SCardGetSetAttrib(
SCARDHANDLE hCard,
int command, DWORD dwAttrId,
373 LPBYTE pbAttr, LPDWORD pcbAttrLen);
375static LONG getReaderEvents(
SCONTEXTMAP * currentContextMap,
int *readerEvents);
376static LONG getReaderStates(
SCONTEXTMAP * currentContextMap);
377static LONG getReaderStatesAndRegisterForEvents(
SCONTEXTMAP * currentContextMap);
378static LONG unregisterFromEvents(
SCONTEXTMAP * currentContextMap);
421 return currentContextMap != NULL;
464 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
468 API_TRACE_IN(
"%ld, %p, %p", dwScope, pvReserved1, pvReserved2)
478 pvReserved2, phContext);
483 API_TRACE_OUT(
"%ld", *phContext)
489DESTRUCTOR
static void destructor(
void)
492 list_destroy(&contextMapList);
503static void init_lib(
void)
510 lrv = list_init(&contextMapList);
513 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d",
518 lrv = list_attributes_seeker(&contextMapList,
522 Log2(PCSC_LOG_CRITICAL,
523 "list_attributes_seeker failed with return value: %d", lrv);
524 list_destroy(&contextMapList);
530 Log1(PCSC_LOG_INFO,
"Disable shared blocking");
531 sharing_shall_block =
false;
568 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
572 uint32_t dwClientID = 0;
577 if (phContext == NULL)
582 pthread_once(&init_lib_control, init_lib);
609 Log1(PCSC_LOG_CRITICAL,
610 "Your pcscd is too old and does not support CMD_VERSION");
614 Log3(PCSC_LOG_INFO,
"Server is protocol version %d:%d",
616 Log3(PCSC_LOG_INFO,
"Client is protocol version %d:%d",
628 Log1(PCSC_LOG_INFO,
"Using backward compatibility");
641 Protocol_version = veStr.
major * 1000 + veStr.
minor;
647 scEstablishStruct.dwScope = dwScope;
648 scEstablishStruct.hContext = 0;
652 sizeof(scEstablishStruct), (
void *) &scEstablishStruct);
660 rv =
MessageReceive(&scEstablishStruct,
sizeof(scEstablishStruct),
668 rv = scEstablishStruct.rv;
678 *phContext = scEstablishStruct.hContext;
720 API_TRACE_IN(
"%ld", hContext)
728 if (NULL == currentContextMap)
734 scReleaseStruct.hContext = hContext;
738 currentContextMap->dwClientID,
739 sizeof(scReleaseStruct), (
void *) &scReleaseStruct);
748 currentContextMap->dwClientID);
753 rv = scReleaseStruct.rv;
755 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
827 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
828 LPDWORD pdwActiveProtocol)
835 API_TRACE_IN(
"%ld %s %ld %ld", hContext, szReader, dwShareMode, dwPreferredProtocols)
840 if (phCard == NULL || pdwActiveProtocol == NULL)
845 if (szReader == NULL)
851 if (strlen(szReader) > MAX_READERNAME)
858 if (NULL == currentContextMap)
861 memset(scConnectStruct.szReader, 0,
sizeof scConnectStruct.szReader);
862 strncpy(scConnectStruct.szReader, szReader,
sizeof scConnectStruct.szReader);
863 scConnectStruct.szReader[
sizeof scConnectStruct.szReader -1] =
'\0';
865 scConnectStruct.hContext = hContext;
866 scConnectStruct.dwShareMode = dwShareMode;
867 scConnectStruct.dwPreferredProtocols = dwPreferredProtocols;
868 scConnectStruct.hCard = 0;
869 scConnectStruct.dwActiveProtocol = 0;
873 sizeof(scConnectStruct), (
void *) &scConnectStruct);
882 currentContextMap->dwClientID);
887 *phCard = scConnectStruct.hCard;
888 *pdwActiveProtocol = scConnectStruct.dwActiveProtocol;
895 rv = SCardAddHandle(*phCard, currentContextMap, szReader);
898 rv = scConnectStruct.rv;
901 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
904 API_TRACE_OUT(
"%d", *pdwActiveProtocol)
982 DWORD dwPreferredProtocols, DWORD dwInitialization,
983 LPDWORD pdwActiveProtocol)
988 CHANNEL_MAP * pChannelMap;
991 API_TRACE_IN(
"%ld %ld %ld", hCard, dwShareMode, dwPreferredProtocols)
993 if (pdwActiveProtocol == NULL)
1002 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1007 scReconnectStruct.hCard = hCard;
1008 scReconnectStruct.dwShareMode = dwShareMode;
1009 scReconnectStruct.dwPreferredProtocols = dwPreferredProtocols;
1010 scReconnectStruct.dwInitialization = dwInitialization;
1011 scReconnectStruct.dwActiveProtocol = *pdwActiveProtocol;
1015 sizeof(scReconnectStruct), (
void *) &scReconnectStruct);
1023 rv =
MessageReceive(&scReconnectStruct,
sizeof(scReconnectStruct),
1024 currentContextMap->dwClientID);
1029 rv = scReconnectStruct.rv;
1033 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1038 *pdwActiveProtocol = scReconnectStruct.dwActiveProtocol;
1041 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1044 API_TRACE_OUT(
"%ld", *pdwActiveProtocol)
1085 CHANNEL_MAP * pChannelMap;
1088 API_TRACE_IN(
"%ld %ld", hCard, dwDisposition)
1093 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1101 scDisconnectStruct.hCard = hCard;
1102 scDisconnectStruct.dwDisposition = dwDisposition;
1106 sizeof(scDisconnectStruct), (
void *) &scDisconnectStruct);
1114 rv =
MessageReceive(&scDisconnectStruct,
sizeof(scDisconnectStruct),
1115 currentContextMap->dwClientID);
1121 SCardRemoveHandle(hCard);
1122 rv = scDisconnectStruct.rv;
1125 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1176 CHANNEL_MAP * pChannelMap;
1179 API_TRACE_IN(
"%ld", hCard)
1191 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1196 scBeginStruct.hCard = hCard;
1200 currentContextMap->dwClientID,
1201 sizeof(scBeginStruct), (
void *) &scBeginStruct);
1210 currentContextMap->dwClientID);
1215 rv = scBeginStruct.rv;
1220 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1224 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1276 CHANNEL_MAP * pChannelMap;
1279 API_TRACE_IN(
"%ld", hCard)
1284 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1289 scEndStruct.hCard = hCard;
1290 scEndStruct.dwDisposition = dwDisposition;
1294 currentContextMap->dwClientID,
1295 sizeof(scEndStruct), (
void *) &scEndStruct);
1304 currentContextMap->dwClientID);
1309 rv = scEndStruct.rv;
1312 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1416 LPDWORD pcchReaderLen, LPDWORD pdwState,
1417 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1419 DWORD dwReaderLen, dwAtrLen;
1424 CHANNEL_MAP * pChannelMap;
1426 char *bufReader = NULL;
1427 LPBYTE bufAtr = NULL;
1440 if (pcchReaderLen == NULL)
1441 pcchReaderLen = &dummy;
1443 if (pcbAtrLen == NULL)
1447 dwReaderLen = *pcchReaderLen;
1448 dwAtrLen = *pcbAtrLen;
1459 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1465 (void)pthread_mutex_lock(&readerStatesMutex);
1468 rv = getReaderStates(currentContextMap);
1472 r = pChannelMap->readerName;
1476 if (r && strcmp(r, readerStates[i].readerName) == 0)
1487 memset(&scStatusStruct, 0,
sizeof(scStatusStruct));
1488 scStatusStruct.hCard = hCard;
1491 sizeof(scStatusStruct), (
void *) &scStatusStruct);
1500 currentContextMap->dwClientID);
1505 rv = scStatusStruct.rv;
1509 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1510 (void)pthread_mutex_unlock(&readerStatesMutex);
1527 *pcchReaderLen = strlen(pChannelMap->readerName) + 1;
1528 *pcbAtrLen = readerStates[i].cardAtrLength;
1531 *pdwState = (readerStates[i].eventCounter << 16) + readerStates[i].readerState;
1534 *pdwProtocol = readerStates[i].cardProtocol;
1538 dwReaderLen = *pcchReaderLen;
1539 if (NULL == szReaderName)
1544 bufReader = malloc(dwReaderLen);
1545 if (NULL == bufReader)
1550 *(
char **)szReaderName = bufReader;
1553 bufReader = szReaderName;
1558 if (*pcchReaderLen > dwReaderLen)
1561 strncpy(bufReader, pChannelMap->readerName, dwReaderLen);
1566 dwAtrLen = *pcbAtrLen;
1572 bufAtr = malloc(dwAtrLen);
1578 *(LPBYTE *)pbAtr = bufAtr;
1585 if (*pcbAtrLen > dwAtrLen)
1588 memcpy(bufAtr, readerStates[i].cardAtr, min(*pcbAtrLen, dwAtrLen));
1592 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1593 (void)pthread_mutex_unlock(&readerStatesMutex);
1717 DWORD dwBreakFlag = 0;
1720 int currentReaderCount = 0;
1722 int pnp_reader = -1;
1725 API_TRACE_IN(
"%ld %ld %d", hContext, dwTimeout, cReaders)
1727 for (j=0; j<cReaders; j++)
1729 API_TRACE_IN(
"[%d] %s %lX %lX (%d)", j, rgReaderStates[j].szReader,
1730 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState,
1731 rgReaderStates[j].cbAtr)
1735 if ((rgReaderStates == NULL && cReaders > 0)
1743 for (j = 0; j < cReaders; j++)
1745 if (rgReaderStates[j].szReader == NULL)
1752 int nbNonIgnoredReaders = cReaders;
1754 for (j=0; j<cReaders; j++)
1756 nbNonIgnoredReaders--;
1758 if (0 == nbNonIgnoredReaders)
1775 if (NULL == currentContextMap)
1782 (void)pthread_mutex_lock(&readerStatesMutex);
1785 rv = getReaderStatesAndRegisterForEvents(currentContextMap);
1789 (void)pthread_mutex_unlock(&readerStatesMutex);
1794 for (j=0; j<cReaders; j++)
1796 const char *readerName;
1799 readerName = rgReaderStates[j].szReader;
1802 if (strcmp(readerName, readerStates[i].readerName) == 0)
1810 if (strcasecmp(readerName,
"\\\\?PnP?\\Notification") != 0)
1813 (void)pthread_mutex_unlock(&readerStatesMutex);
1820 (void)pthread_mutex_unlock(&readerStatesMutex);
1823 for (j = 0; j < cReaders; j++)
1824 rgReaderStates[j].dwEventState = 0;
1827 Log2(PCSC_LOG_DEBUG,
"Event Loop Start, dwTimeout: %ld", dwTimeout);
1830 if (pnp_reader >= 0)
1833 currReader = &rgReaderStates[pnp_reader];
1836 if (
SCARD_S_SUCCESS == getReaderEvents(currentContextMap, &readerEvents))
1838 int previousReaderEvents = currReader->dwCurrentState >> 16;
1841 currReader->dwEventState = (currReader->dwEventState & 0xFFFF) + (readerEvents << 16);
1844 (previousReaderEvents != readerEvents)
1847 && previousReaderEvents)
1858 if (readerStates[j].readerName[0] !=
'\0')
1859 currentReaderCount++;
1862 if ((DWORD)-1 == dwTimeout)
1872 currReader = &rgReaderStates[j];
1877 const char *readerName;
1881 (void)pthread_mutex_lock(&readerStatesMutex);
1884 readerName = currReader->szReader;
1887 if (strcmp(readerName, readerStates[i].readerName) == 0)
1895 if (strcasecmp(readerName,
"\\\\?PnP?\\Notification") == 0)
1897 int k, newReaderCount = 0;
1900 if (readerStates[k].readerName[0] !=
'\0')
1903 if (newReaderCount != currentReaderCount)
1907 Log1(PCSC_LOG_INFO,
"Reader list changed");
1908 currentReaderCount = newReaderCount;
1910 if (
SCARD_S_SUCCESS == getReaderEvents(currentContextMap, &readerEvents))
1913 currReader->dwEventState = (currReader->dwEventState & 0xFFFF) + (readerEvents << 16);
1922 currReader->dwEventState =
1938 uint32_t readerState;
1945 Log0(PCSC_LOG_DEBUG);
1950 rContext = &readerStates[i];
1956 if (currReader->dwCurrentState & 0xFFFF0000)
1958 unsigned int currentCounter;
1960 currentCounter = (currReader->dwCurrentState >> 16) & 0xFFFF;
1966 Log0(PCSC_LOG_DEBUG);
1972 currReader->dwEventState = ((currReader->dwEventState & 0xffff )
1984 Log0(PCSC_LOG_DEBUG);
1995 Log0(PCSC_LOG_DEBUG);
2003#ifndef DISABLE_AUTO_POWER_ON
2007 (void)
SYS_USleep(PCSCLITE_STATUS_POLL_RATE + 10);
2011 memcpy(currReader->rgbAtr, rContext->
cardAtr,
2015 currReader->cbAtr = 0;
2034 Log0(PCSC_LOG_DEBUG);
2052 Log0(PCSC_LOG_DEBUG);
2062 Log0(PCSC_LOG_DEBUG);
2072 Log0(PCSC_LOG_DEBUG);
2086 Log0(PCSC_LOG_DEBUG);
2100 Log0(PCSC_LOG_DEBUG);
2113 Log0(PCSC_LOG_DEBUG);
2116 else if (currReader-> dwCurrentState
2120 Log0(PCSC_LOG_DEBUG);
2132 Log0(PCSC_LOG_DEBUG);
2137 (void)pthread_mutex_unlock(&readerStatesMutex);
2150 if (dwBreakFlag == 1)
2156 struct timeval before, after;
2158 gettimeofday(&before, NULL);
2169 &waitStatusStruct,
sizeof(waitStatusStruct),
2180 rv = unregisterFromEvents(currentContextMap);
2189 rv = waitStatusStruct.rv;
2194 (void)pthread_mutex_lock(&readerStatesMutex);
2195 rv = getReaderStatesAndRegisterForEvents(currentContextMap);
2196 (void)pthread_mutex_unlock(&readerStatesMutex);
2204 gettimeofday(&after, NULL);
2206 dwTime -= diff/1000;
2226 Log1(PCSC_LOG_DEBUG,
"Event Loop End");
2231 (void)unregisterFromEvents(currentContextMap);
2233 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2238 for (j=0; j<cReaders; j++)
2240 API_TRACE_OUT(
"[%d] %s %lX %lX (%d)", j, rgReaderStates[j].szReader,
2241 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState,
2242 rgReaderStates[j].cbAtr)
2300 DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength,
2301 LPDWORD lpBytesReturned)
2306 CHANNEL_MAP * pChannelMap;
2311 if (NULL != lpBytesReturned)
2312 *lpBytesReturned = 0;
2317 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2331 scControlStruct.hCard = hCard;
2332 scControlStruct.dwControlCode = dwControlCode;
2333 scControlStruct.cbSendLength = cbSendLength;
2334 scControlStruct.cbRecvLength = cbRecvLength;
2335 scControlStruct.dwBytesReturned = 0;
2336 scControlStruct.rv = 0;
2339 sizeof(scControlStruct), &scControlStruct);
2345 rv =
MessageSend((
char *)pbSendBuffer, cbSendLength,
2346 currentContextMap->dwClientID);
2355 currentContextMap->dwClientID);
2362 if (scControlStruct.dwBytesReturned > cbRecvLength)
2364 if (NULL != lpBytesReturned)
2365 *lpBytesReturned = scControlStruct.dwBytesReturned;
2371 rv =
MessageReceive(pbRecvBuffer, scControlStruct.dwBytesReturned,
2372 currentContextMap->dwClientID);
2379 if (NULL != lpBytesReturned)
2380 *lpBytesReturned = scControlStruct.dwBytesReturned;
2382 rv = scControlStruct.rv;
2385 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
2514 unsigned char *buf = NULL;
2518 if (NULL == pcbAttrLen)
2530 buf = malloc(*pcbAttrLen);
2537 *(
unsigned char **)pbAttr = buf;
2600 if (NULL == pbAttr || 0 == cbAttrLen)
2611static LONG SCardGetSetAttrib(
SCARDHANDLE hCard,
int command, DWORD dwAttrId,
2612 LPBYTE pbAttr, LPDWORD pcbAttrLen)
2617 CHANNEL_MAP * pChannelMap;
2622 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2633 scGetSetStruct.hCard = hCard;
2634 scGetSetStruct.dwAttrId = dwAttrId;
2636 memset(scGetSetStruct.pbAttr, 0,
sizeof(scGetSetStruct.pbAttr));
2639 memcpy(scGetSetStruct.pbAttr, pbAttr, *pcbAttrLen);
2640 scGetSetStruct.cbAttrLen = *pcbAttrLen;
2644 scGetSetStruct.cbAttrLen =
sizeof scGetSetStruct.pbAttr;
2647 sizeof(scGetSetStruct), &scGetSetStruct);
2656 currentContextMap->dwClientID);
2666 if (*pcbAttrLen < scGetSetStruct.cbAttrLen)
2670 DWORD correct_value = scGetSetStruct.cbAttrLen;
2671 scGetSetStruct.cbAttrLen = *pcbAttrLen;
2672 *pcbAttrLen = correct_value;
2677 *pcbAttrLen = scGetSetStruct.cbAttrLen;
2680 memcpy(pbAttr, scGetSetStruct.pbAttr, scGetSetStruct.cbAttrLen);
2682 memset(scGetSetStruct.pbAttr, 0x00,
sizeof(scGetSetStruct.pbAttr));
2684 rv = scGetSetStruct.rv;
2687 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
2751 LPCBYTE pbSendBuffer, DWORD cbSendLength,
2753 LPDWORD pcbRecvLength)
2757 CHANNEL_MAP * pChannelMap;
2762 if (pbSendBuffer == NULL || pbRecvBuffer == NULL ||
2763 pcbRecvLength == NULL || pioSendPci == NULL)
2772 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2787 scTransmitStruct.hCard = hCard;
2788 scTransmitStruct.cbSendLength = cbSendLength;
2789 scTransmitStruct.pcbRecvLength = *pcbRecvLength;
2790 scTransmitStruct.ioSendPciProtocol = pioSendPci->
dwProtocol;
2791 scTransmitStruct.ioSendPciLength = pioSendPci->
cbPciLength;
2796 scTransmitStruct.ioRecvPciProtocol = pioRecvPci->
dwProtocol;
2797 scTransmitStruct.ioRecvPciLength = pioRecvPci->
cbPciLength;
2806 sizeof(scTransmitStruct), (
void *) &scTransmitStruct);
2812 rv =
MessageSend((
void *)pbSendBuffer, cbSendLength,
2829 if (scTransmitStruct.pcbRecvLength > *pcbRecvLength)
2831 *pcbRecvLength = scTransmitStruct.pcbRecvLength;
2837 rv =
MessageReceive(pbRecvBuffer, scTransmitStruct.pcbRecvLength,
2845 pioRecvPci->
dwProtocol = scTransmitStruct.ioRecvPciProtocol;
2846 pioRecvPci->
cbPciLength = scTransmitStruct.ioRecvPciLength;
2850 rv = scTransmitStruct.rv;
2854 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2859 *pcbRecvLength = scTransmitStruct.pcbRecvLength;
2862 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2936 LPSTR mszReaders, LPDWORD pcchReaders)
2938 DWORD dwReadersLen = 0;
2946 API_TRACE_IN(
"%ld", hContext)
2951 if (pcchReaders == NULL)
2958 if (NULL == currentContextMap)
2965 (void)pthread_mutex_lock(&readerStatesMutex);
2968 rv = getReaderStates(currentContextMap);
2974 if (readerStates[i].readerName[0] !=
'\0')
2975 dwReadersLen += strlen(readerStates[i].readerName) + 1;
2980 if (1 == dwReadersLen)
2988 if (NULL == mszReaders)
2993 buf = malloc(dwReadersLen);
2999 *(
char **)mszReaders = buf;
3006 if ((NULL != mszReaders) && (*pcchReaders < dwReadersLen))
3013 if (mszReaders == NULL)
3018 if (readerStates[i].readerName[0] !=
'\0')
3023 strcpy(buf, readerStates[i].readerName);
3024 buf += strlen(readerStates[i].readerName)+1;
3031 *pcchReaders = dwReadersLen;
3033 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
3034 (void)pthread_mutex_unlock(&readerStatesMutex);
3037 API_TRACE_OUT(
"%d", *pcchReaders)
3067 free((
void *)pvMem);
3135 const char ReaderGroup[] =
"SCard$DefaultReaders\0";
3136 const unsigned int dwGroups =
sizeof(ReaderGroup);
3142 if (NULL == currentContextMap)
3147 if (NULL == mszGroups)
3152 buf = malloc(dwGroups);
3158 *(
char **)mszGroups = buf;
3164 if ((NULL != mszGroups) && (*pcchGroups < dwGroups))
3172 memcpy(buf, ReaderGroup, dwGroups);
3175 *pcchGroups = dwGroups;
3177 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
3219 uint32_t dwClientID = 0;
3224 API_TRACE_IN(
"%ld", hContext)
3232 if (NULL == currentContextMap)
3254 scCancelStruct.hContext = hContext;
3258 sizeof(scCancelStruct), (
void *) &scCancelStruct);
3266 rv =
MessageReceive(&scCancelStruct,
sizeof(scCancelStruct), dwClientID);
3271 rv = scCancelStruct.rv;
3310 API_TRACE_IN(
"%ld", hContext)
3348 if (NULL == newContextMap)
3351 Log2(PCSC_LOG_DEBUG,
"Allocating new SCONTEXTMAP @%p", newContextMap);
3352 newContextMap->
hContext = hContext;
3356 (void)pthread_mutex_init(&newContextMap->
mMutex, NULL);
3358 lrv = list_init(&newContextMap->channelMapList);
3361 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
3365 lrv = list_attributes_seeker(&newContextMap->channelMapList,
3366 CHANNEL_MAP_seeker);
3369 Log2(PCSC_LOG_CRITICAL,
3370 "list_attributes_seeker failed with return value: %d", lrv);
3371 list_destroy(&newContextMap->channelMapList);
3376 lrv = list_append(&contextMapList, newContextMap);
3380 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
3382 list_destroy(&newContextMap->channelMapList);
3390 (void)pthread_mutex_destroy(&newContextMap->
mMutex);
3391 free(newContextMap);
3420 if (NULL != currentContextMap)
3421 (void)pthread_mutex_lock(¤tContextMap->
mMutex);
3425 return currentContextMap;
3445 currentContextMap = list_seek(&contextMapList, &hContext);
3448 return currentContextMap;
3462 if (NULL != currentContextMap)
3463 SCardCleanContext(currentContextMap);
3466static void SCardCleanContext(
SCONTEXTMAP * targetContextMap)
3468 int list_index, lrv;
3470 CHANNEL_MAP * currentChannelMap;
3475 (void)pthread_mutex_destroy(&targetContextMap->
mMutex);
3477 listSize = list_size(&targetContextMap->channelMapList);
3478 for (list_index = 0; list_index < listSize; list_index++)
3480 currentChannelMap = list_get_at(&targetContextMap->channelMapList,
3482 if (NULL == currentChannelMap)
3484 Log2(PCSC_LOG_CRITICAL,
"list_get_at failed for index %d",
3490 free(currentChannelMap->readerName);
3491 free(currentChannelMap);
3495 list_destroy(&targetContextMap->channelMapList);
3498 lrv = list_delete(&contextMapList, targetContextMap);
3502 Log2(PCSC_LOG_CRITICAL,
3503 "list_delete failed with return value: %d", lrv);
3506 free(targetContextMap);
3518 CHANNEL_MAP * newChannelMap;
3521 newChannelMap = malloc(
sizeof(CHANNEL_MAP));
3522 if (NULL == newChannelMap)
3525 newChannelMap->hCard = hCard;
3526 newChannelMap->readerName = strdup(readerName);
3528 lrv = list_append(¤tContextMap->channelMapList, newChannelMap);
3531 free(newChannelMap->readerName);
3532 free(newChannelMap);
3533 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
3544 CHANNEL_MAP * currentChannelMap;
3548 rv = SCardGetContextAndChannelFromHandleTH(hCard, ¤tContextMap,
3549 ¤tChannelMap);
3553 free(currentChannelMap->readerName);
3555 lrv = list_delete(¤tContextMap->channelMapList, currentChannelMap);
3558 Log2(PCSC_LOG_CRITICAL,
3559 "list_delete failed with return value: %d", lrv);
3562 free(currentChannelMap);
3567static LONG SCardGetContextChannelAndLockFromHandle(
SCARDHANDLE hCard,
3568 SCONTEXTMAP **targetContextMap, CHANNEL_MAP ** targetChannelMap)
3576 rv = SCardGetContextAndChannelFromHandleTH(hCard, targetContextMap,
3580 (void)pthread_mutex_lock(&(*targetContextMap)->mMutex);
3587static LONG SCardGetContextAndChannelFromHandleTH(
SCARDHANDLE hCard,
3588 SCONTEXTMAP **targetContextMap, CHANNEL_MAP ** targetChannelMap)
3594 CHANNEL_MAP * currentChannelMap;
3597 *targetContextMap = NULL;
3598 *targetChannelMap = NULL;
3601 listSize = list_size(&contextMapList);
3603 for (list_index = 0; list_index < listSize; list_index++)
3605 currentContextMap = list_get_at(&contextMapList, list_index);
3606 if (currentContextMap == NULL)
3608 Log2(PCSC_LOG_CRITICAL,
"list_get_at failed for index %d",
3612 currentChannelMap = list_seek(¤tContextMap->channelMapList,
3614 if (currentChannelMap != NULL)
3616 *targetContextMap = currentContextMap;
3617 *targetChannelMap = currentChannelMap;
3638 struct stat statBuffer;
3641 socketName = getSocketName();
3642 rv = stat(socketName, &statBuffer);
3646 Log3(PCSC_LOG_INFO,
"PCSC Not Running: %s: %s",
3647 socketName, strerror(errno));
3654static LONG getReaderEvents(
SCONTEXTMAP * currentContextMap,
int *readerEvents)
3656 int32_t dwClientID = currentContextMap->
dwClientID;
3661 if (Protocol_version < 4005)
3678static LONG getReaderStates(
SCONTEXTMAP * currentContextMap)
3680 int32_t dwClientID = currentContextMap->
dwClientID;
3688 rv =
MessageReceive(&readerStates,
sizeof(readerStates), dwClientID);
3695static LONG getReaderStatesAndRegisterForEvents(
SCONTEXTMAP * currentContextMap)
3697 int32_t dwClientID = currentContextMap->
dwClientID;
3707 rv =
MessageReceive(&readerStates,
sizeof(readerStates), dwClientID);
3711static LONG unregisterFromEvents(
SCONTEXTMAP * currentContextMap)
3713 int32_t dwClientID = currentContextMap->
dwClientID;
3719 dwClientID, 0, NULL);
3736 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.
struct pubReaderStatesList READER_STATE
Define an exported public reader state structure so each application gets instant notification of cha...
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_SERVICE_STOPPED
The Smart card resource manager has shut down.
#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_E_UNSUPPORTED_FEATURE
This smart card does not support the requested feature.
#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_ATRMATCH
ATR matches card.
#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).
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.
_Atomic int32_t readerSharing
PCSCLITE_SHARING_* sharing status.
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.
pthread_mutex_t contextMapList_lock
lock for the above list
struct _psContextMap SCONTEXTMAP
Represents an Application Context on the Client side.
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...
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.
#define PROTOCOL_VERSION_MINOR_CLIENT_BACKWARD
Minor version the client also supports.
@ 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()