108 #include <sys/types.h> 114 #include <sys/time.h> 116 #include <sys/wait.h> 138 #define SCARD_PROTOCOL_ANY_OLD 0x1000 145 static char sharing_shall_block = TRUE;
147 #define COLOR_RED "\33[01;31m" 148 #define COLOR_GREEN "\33[32m" 149 #define COLOR_BLUE "\33[34m" 150 #define COLOR_MAGENTA "\33[35m" 151 #define COLOR_NORMAL "\33[0m" 158 static void trace(
const char *func,
const char direction,
const char *fmt, ...)
162 fprintf(stderr, COLOR_GREEN
"%c " COLOR_BLUE
"[%lX] " COLOR_GREEN
"%s ",
163 direction, pthread_self(), func);
165 fprintf(stderr, COLOR_MAGENTA);
167 vfprintf(stderr, fmt, args);
170 fprintf(stderr, COLOR_NORMAL
"\n");
173 #define API_TRACE_IN(...) trace(__FUNCTION__, '<', __VA_ARGS__); 174 #define API_TRACE_OUT(...) trace(__FUNCTION__, '>', __VA_ARGS__); 176 #define API_TRACE_IN(...) 177 #define API_TRACE_OUT(...) 182 #define PROFILE_FILE "/tmp/pcsc_profile" 184 #include <sys/time.h> 187 #define MAX_THREADS 5 188 pthread_t threads[MAX_THREADS];
189 struct timeval profile_time_start[MAX_THREADS];
193 #define PROFILE_START profile_start(); 194 #define PROFILE_END(rv) profile_end(__FUNCTION__, rv); 196 static void profile_start(
void)
198 static char initialized = FALSE;
207 sprintf(filename,
"%s-%d", PROFILE_FILE, getuid());
208 profile_fd = fopen(filename,
"a+");
209 if (NULL == profile_fd)
211 fprintf(stderr, COLOR_RED
"Can't open %s: %s" COLOR_NORMAL
"\n",
212 PROFILE_FILE, strerror(errno));
215 fprintf(profile_fd,
"\nStart a new profile\n");
217 if (isatty(fileno(stderr)))
224 for (i=0; i<MAX_THREADS; i++)
225 if (pthread_equal(0, threads[i]))
231 gettimeofday(&profile_time_start[i], NULL);
234 static void profile_end(
const char *f, LONG rv)
236 struct timeval profile_time_end;
241 gettimeofday(&profile_time_end, NULL);
244 for (i=0; i<MAX_THREADS; i++)
245 if (pthread_equal(t, threads[i]))
250 fprintf(stderr, COLOR_BLUE
" WARNING: no start info for %s\n", f);
254 d =
time_sub(&profile_time_end, &profile_time_start[i]);
263 COLOR_RED
"RESULT %s " COLOR_MAGENTA
"%ld " 264 COLOR_BLUE
"0x%08lX %s" COLOR_NORMAL
"\n",
267 fprintf(stderr, COLOR_RED
"RESULT %s " COLOR_MAGENTA
"%ld" 268 COLOR_NORMAL
"\n", f, d);
270 fprintf(profile_fd,
"%s %ld\n", f, d);
275 #define PROFILE_START 276 #define PROFILE_END(rv) 291 static int CHANNEL_MAP_seeker(
const void *el,
const void *key)
295 if ((el == NULL) || (key == NULL))
297 Log3(PCSC_LOG_CRITICAL,
298 "CHANNEL_MAP_seeker called with NULL pointer: el=%p, key=%p",
329 static list_t contextMapList;
331 static int SCONTEXTMAP_seeker(
const void *el,
const void *key)
335 if ((el == NULL) || (key == NULL))
337 Log3(PCSC_LOG_CRITICAL,
338 "SCONTEXTMAP_seeker called with NULL pointer: el=%p, key=%p",
381 static LONG SCardGetContextChannelAndLockFromHandle(
SCARDHANDLE,
383 static LONG SCardGetContextAndChannelFromHandleTH(
SCARDHANDLE,
387 static LONG SCardGetSetAttrib(
SCARDHANDLE hCard,
int command, DWORD dwAttrId,
388 LPBYTE pbAttr, LPDWORD pcbAttrLen);
390 static LONG getReaderStates(
SCONTEXTMAP * currentContextMap);
433 return currentContextMap != NULL;
475 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
479 API_TRACE_IN(
"%ld, %p, %p", dwScope, pvReserved1, pvReserved2)
489 pvReserved2, phContext);
494 API_TRACE_OUT(
"%ld", *phContext)
527 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
531 uint32_t dwClientID = 0;
535 if (phContext == NULL)
552 lrv = list_init(&contextMapList);
555 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d",
560 lrv = list_attributes_seeker(&contextMapList,
564 Log2(PCSC_LOG_CRITICAL,
565 "list_attributes_seeker failed with return value: %d", lrv);
566 list_destroy(&contextMapList);
570 if (getenv(
"PCSCLITE_NO_BLOCKING"))
572 Log1(PCSC_LOG_INFO,
"Disable shared blocking");
573 sharing_shall_block = FALSE;
602 Log1(PCSC_LOG_CRITICAL,
603 "Your pcscd is too old and does not support CMD_VERSION");
607 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),
638 return scEstablishStruct.rv;
646 *phContext = scEstablishStruct.hContext;
683 API_TRACE_IN(
"%ld", hContext)
691 if (NULL == currentContextMap)
697 scReleaseStruct.hContext = hContext;
701 currentContextMap->dwClientID,
702 sizeof(scReleaseStruct), (
void *) &scReleaseStruct);
711 currentContextMap->dwClientID);
716 rv = scReleaseStruct.rv;
718 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
790 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
791 LPDWORD pdwActiveProtocol)
798 API_TRACE_IN(
"%ld %s %ld %ld", hContext, szReader, dwShareMode, dwPreferredProtocols)
803 if (phCard == NULL || pdwActiveProtocol == NULL)
808 if (szReader == NULL)
814 if (strlen(szReader) > MAX_READERNAME)
821 if (NULL == currentContextMap)
824 memset(scConnectStruct.szReader, 0,
sizeof scConnectStruct.szReader);
825 strncpy(scConnectStruct.szReader, szReader,
sizeof scConnectStruct.szReader);
826 scConnectStruct.szReader[
sizeof scConnectStruct.szReader -1] =
'\0';
828 scConnectStruct.hContext = hContext;
829 scConnectStruct.dwShareMode = dwShareMode;
830 scConnectStruct.dwPreferredProtocols = dwPreferredProtocols;
831 scConnectStruct.hCard = 0;
832 scConnectStruct.dwActiveProtocol = 0;
836 sizeof(scConnectStruct), (
void *) &scConnectStruct);
845 currentContextMap->dwClientID);
850 *phCard = scConnectStruct.hCard;
851 *pdwActiveProtocol = scConnectStruct.dwActiveProtocol;
858 rv = SCardAddHandle(*phCard, currentContextMap, szReader);
861 rv = scConnectStruct.rv;
864 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
867 API_TRACE_OUT(
"%d", *pdwActiveProtocol)
945 DWORD dwPreferredProtocols, DWORD dwInitialization,
946 LPDWORD pdwActiveProtocol)
954 API_TRACE_IN(
"%ld %ld %ld", hCard, dwShareMode, dwPreferredProtocols)
956 if (pdwActiveProtocol == NULL)
965 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
970 scReconnectStruct.hCard = hCard;
971 scReconnectStruct.dwShareMode = dwShareMode;
972 scReconnectStruct.dwPreferredProtocols = dwPreferredProtocols;
973 scReconnectStruct.dwInitialization = dwInitialization;
974 scReconnectStruct.dwActiveProtocol = *pdwActiveProtocol;
978 sizeof(scReconnectStruct), (
void *) &scReconnectStruct);
986 rv =
MessageReceive(&scReconnectStruct,
sizeof(scReconnectStruct),
987 currentContextMap->dwClientID);
992 rv = scReconnectStruct.rv;
996 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1001 *pdwActiveProtocol = scReconnectStruct.dwActiveProtocol;
1004 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1007 API_TRACE_OUT(
"%ld", *pdwActiveProtocol)
1051 API_TRACE_IN(
"%ld %ld", hCard, dwDisposition)
1056 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1064 scDisconnectStruct.hCard = hCard;
1065 scDisconnectStruct.dwDisposition = dwDisposition;
1069 sizeof(scDisconnectStruct), (
void *) &scDisconnectStruct);
1077 rv =
MessageReceive(&scDisconnectStruct,
sizeof(scDisconnectStruct),
1078 currentContextMap->dwClientID);
1084 SCardRemoveHandle(hCard);
1085 rv = scDisconnectStruct.rv;
1088 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1141 API_TRACE_IN(
"%ld", hCard)
1153 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1158 scBeginStruct.hCard = hCard;
1162 currentContextMap->dwClientID,
1163 sizeof(scBeginStruct), (
void *) &scBeginStruct);
1172 currentContextMap->dwClientID);
1177 rv = scBeginStruct.rv;
1182 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1186 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1242 API_TRACE_IN(
"%ld", hCard)
1247 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1252 scEndStruct.hCard = hCard;
1253 scEndStruct.dwDisposition = dwDisposition;
1258 sizeof(scEndStruct), (
void *) &scEndStruct);
1277 rv = scEndStruct.rv;
1280 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
1384 LPDWORD pcchReaderLen, LPDWORD pdwState,
1385 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1387 DWORD dwReaderLen, dwAtrLen;
1394 char *bufReader = NULL;
1395 LPBYTE bufAtr = NULL;
1408 if (pcchReaderLen == NULL)
1409 pcchReaderLen = &dummy;
1411 if (pcbAtrLen == NULL)
1415 dwReaderLen = *pcchReaderLen;
1416 dwAtrLen = *pcbAtrLen;
1427 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
1433 rv = getReaderStates(currentContextMap);
1437 r = pChannelMap->readerName;
1452 memset(&scStatusStruct, 0,
sizeof(scStatusStruct));
1453 scStatusStruct.hCard = hCard;
1456 sizeof(scStatusStruct), (
void *) &scStatusStruct);
1465 currentContextMap->dwClientID);
1470 rv = scStatusStruct.rv;
1474 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1491 *pcchReaderLen = strlen(pChannelMap->readerName) + 1;
1502 dwReaderLen = *pcchReaderLen;
1503 if (NULL == szReaderName)
1508 bufReader = malloc(dwReaderLen);
1509 if (NULL == bufReader)
1514 *(
char **)szReaderName = bufReader;
1517 bufReader = szReaderName;
1522 if (*pcchReaderLen > dwReaderLen)
1525 strncpy(bufReader, pChannelMap->readerName, dwReaderLen);
1530 dwAtrLen = *pcbAtrLen;
1536 bufAtr = malloc(dwAtrLen);
1542 *(LPBYTE *)pbAtr = bufAtr;
1549 if (*pcbAtrLen > dwAtrLen)
1552 memcpy(bufAtr,
readerStates[i].cardAtr, min(*pcbAtrLen, dwAtrLen));
1556 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
1676 DWORD dwBreakFlag = 0;
1679 int currentReaderCount = 0;
1683 API_TRACE_IN(
"%ld %ld %d", hContext, dwTimeout, cReaders)
1685 for (j=0; j<cReaders; j++)
1687 API_TRACE_IN(
"[%d] %s %lX %lX", j, rgReaderStates[j].szReader,
1688 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState)
1692 if ((rgReaderStates == NULL && cReaders > 0)
1700 for (j = 0; j < cReaders; j++)
1702 if (rgReaderStates[j].szReader == NULL)
1709 int nbNonIgnoredReaders = cReaders;
1711 for (j=0; j<cReaders; j++)
1713 nbNonIgnoredReaders--;
1715 if (0 == nbNonIgnoredReaders)
1732 if (NULL == currentContextMap)
1739 rv = getReaderStates(currentContextMap);
1744 for (j=0; j<cReaders; j++)
1746 const char *readerName;
1749 readerName = rgReaderStates[j].szReader;
1752 if (strcmp(readerName,
readerStates[i].readerName) == 0)
1760 if (strcasecmp(readerName,
"\\\\?PnP?\\Notification") != 0)
1769 for (j = 0; j < cReaders; j++)
1770 rgReaderStates[j].dwEventState = 0;
1773 Log2(PCSC_LOG_DEBUG,
"Event Loop Start, dwTimeout: %ld", dwTimeout);
1778 currentReaderCount++;
1781 if ((DWORD)-1 == dwTimeout)
1791 currReader = &rgReaderStates[j];
1796 const char *readerName;
1800 readerName = currReader->szReader;
1803 if (strcmp(readerName,
readerStates[i].readerName) == 0)
1811 if (strcasecmp(readerName,
"\\\\?PnP?\\Notification") == 0)
1813 int k, newReaderCount = 0;
1819 if (newReaderCount != currentReaderCount)
1821 Log1(PCSC_LOG_INFO,
"Reader list changed");
1822 currentReaderCount = newReaderCount;
1830 currReader->dwEventState =
1846 uint32_t readerState;
1853 Log0(PCSC_LOG_DEBUG);
1864 if (currReader->dwCurrentState & 0xFFFF0000)
1866 unsigned int currentCounter;
1868 currentCounter = (currReader->dwCurrentState >> 16) & 0xFFFF;
1874 Log0(PCSC_LOG_DEBUG);
1880 currReader->dwEventState = ((currReader->dwEventState & 0xffff )
1892 Log0(PCSC_LOG_DEBUG);
1903 Log0(PCSC_LOG_DEBUG);
1917 memcpy(currReader->rgbAtr, rContext->
cardAtr,
1921 currReader->cbAtr = 0;
1940 Log0(PCSC_LOG_DEBUG);
1958 Log0(PCSC_LOG_DEBUG);
1968 Log0(PCSC_LOG_DEBUG);
1978 Log0(PCSC_LOG_DEBUG);
1992 Log0(PCSC_LOG_DEBUG);
2006 Log0(PCSC_LOG_DEBUG);
2019 Log0(PCSC_LOG_DEBUG);
2022 else if (currReader-> dwCurrentState
2026 Log0(PCSC_LOG_DEBUG);
2038 Log0(PCSC_LOG_DEBUG);
2054 if (dwBreakFlag == 1)
2060 struct timeval before, after;
2062 gettimeofday(&before, NULL);
2064 waitStatusStruct.
timeOut = dwTime;
2072 sizeof(waitStatusStruct), &waitStatusStruct);
2081 &waitStatusStruct,
sizeof(waitStatusStruct),
2094 sizeof(waitStatusStruct), &waitStatusStruct);
2101 sizeof(waitStatusStruct),
2114 rv = waitStatusStruct.rv;
2119 rv = getReaderStates(currentContextMap);
2127 gettimeofday(&after, NULL);
2129 dwTime -= diff/1000;
2149 Log1(PCSC_LOG_DEBUG,
"Event Loop End");
2151 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2156 for (j=0; j<cReaders; j++)
2158 API_TRACE_OUT(
"[%d] %s %X %X", j, rgReaderStates[j].szReader,
2159 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState)
2217 DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength,
2218 LPDWORD lpBytesReturned)
2228 if (NULL != lpBytesReturned)
2229 *lpBytesReturned = 0;
2234 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2249 scControlStruct.hCard = hCard;
2250 scControlStruct.dwControlCode = dwControlCode;
2251 scControlStruct.cbSendLength = cbSendLength;
2252 scControlStruct.cbRecvLength = cbRecvLength;
2253 scControlStruct.dwBytesReturned = 0;
2254 scControlStruct.rv = 0;
2257 sizeof(scControlStruct), &scControlStruct);
2263 rv =
MessageSend((
char *)pbSendBuffer, cbSendLength,
2264 currentContextMap->dwClientID);
2273 currentContextMap->dwClientID);
2281 rv =
MessageReceive(pbRecvBuffer, scControlStruct.dwBytesReturned,
2282 currentContextMap->dwClientID);
2289 if (NULL != lpBytesReturned)
2290 *lpBytesReturned = scControlStruct.dwBytesReturned;
2292 rv = scControlStruct.rv;
2295 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
2421 unsigned char *buf = NULL;
2425 if (NULL == pcbAttrLen)
2437 buf = malloc(*pcbAttrLen);
2444 *(
unsigned char **)pbAttr = buf;
2507 if (NULL == pbAttr || 0 == cbAttrLen)
2518 static LONG SCardGetSetAttrib(
SCARDHANDLE hCard,
int command, DWORD dwAttrId,
2519 LPBYTE pbAttr, LPDWORD pcbAttrLen)
2529 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2540 scGetSetStruct.hCard = hCard;
2541 scGetSetStruct.dwAttrId = dwAttrId;
2543 memset(scGetSetStruct.pbAttr, 0,
sizeof(scGetSetStruct.pbAttr));
2546 memcpy(scGetSetStruct.pbAttr, pbAttr, *pcbAttrLen);
2547 scGetSetStruct.cbAttrLen = *pcbAttrLen;
2551 scGetSetStruct.cbAttrLen =
sizeof scGetSetStruct.pbAttr;
2554 sizeof(scGetSetStruct), &scGetSetStruct);
2563 currentContextMap->dwClientID);
2573 if (*pcbAttrLen < scGetSetStruct.cbAttrLen)
2577 DWORD correct_value = scGetSetStruct.cbAttrLen;
2578 scGetSetStruct.cbAttrLen = *pcbAttrLen;
2579 *pcbAttrLen = correct_value;
2584 *pcbAttrLen = scGetSetStruct.cbAttrLen;
2587 memcpy(pbAttr, scGetSetStruct.pbAttr, scGetSetStruct.cbAttrLen);
2589 memset(scGetSetStruct.pbAttr, 0x00,
sizeof(scGetSetStruct.pbAttr));
2591 rv = scGetSetStruct.rv;
2594 (void)pthread_mutex_unlock(¤tContextMap->mMutex);
2658 LPCBYTE pbSendBuffer, DWORD cbSendLength,
2660 LPDWORD pcbRecvLength)
2669 if (pbSendBuffer == NULL || pbRecvBuffer == NULL ||
2670 pcbRecvLength == NULL || pioSendPci == NULL)
2679 rv = SCardGetContextChannelAndLockFromHandle(hCard, ¤tContextMap,
2695 scTransmitStruct.hCard = hCard;
2696 scTransmitStruct.cbSendLength = cbSendLength;
2697 scTransmitStruct.pcbRecvLength = *pcbRecvLength;
2698 scTransmitStruct.ioSendPciProtocol = pioSendPci->
dwProtocol;
2699 scTransmitStruct.ioSendPciLength = pioSendPci->
cbPciLength;
2704 scTransmitStruct.ioRecvPciProtocol = pioRecvPci->
dwProtocol;
2705 scTransmitStruct.ioRecvPciLength = pioRecvPci->
cbPciLength;
2714 sizeof(scTransmitStruct), (
void *) &scTransmitStruct);
2720 rv =
MessageSend((
void *)pbSendBuffer, cbSendLength,
2738 rv =
MessageReceive(pbRecvBuffer, scTransmitStruct.pcbRecvLength,
2746 pioRecvPci->
dwProtocol = scTransmitStruct.ioRecvPciProtocol;
2747 pioRecvPci->
cbPciLength = scTransmitStruct.ioRecvPciLength;
2751 rv = scTransmitStruct.rv;
2755 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2760 *pcbRecvLength = scTransmitStruct.pcbRecvLength;
2763 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2833 LPSTR mszReaders, LPDWORD pcchReaders)
2835 DWORD dwReadersLen = 0;
2843 API_TRACE_IN(
"%ld", hContext)
2848 if (pcchReaders == NULL)
2855 if (NULL == currentContextMap)
2862 rv = getReaderStates(currentContextMap);
2869 dwReadersLen += strlen(
readerStates[i].readerName) + 1;
2874 if (1 == dwReadersLen)
2882 if (NULL == mszReaders)
2887 buf = malloc(dwReadersLen);
2893 *(
char **)mszReaders = buf;
2900 if ((NULL != mszReaders) && (*pcchReaders < dwReadersLen))
2907 if (mszReaders == NULL)
2925 *pcchReaders = dwReadersLen;
2927 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
2930 API_TRACE_OUT(
"%d", *pcchReaders)
2960 free((
void *)pvMem);
3028 const char ReaderGroup[] =
"SCard$DefaultReaders\0";
3029 const unsigned int dwGroups =
sizeof(ReaderGroup);
3035 if (NULL == currentContextMap)
3040 if (NULL == mszGroups)
3045 buf = malloc(dwGroups);
3051 *(
char **)mszGroups = buf;
3057 if ((NULL != mszGroups) && (*pcchGroups < dwGroups))
3065 memcpy(buf, ReaderGroup, dwGroups);
3068 *pcchGroups = dwGroups;
3070 (void)pthread_mutex_unlock(¤tContextMap->
mMutex);
3112 uint32_t dwClientID = 0;
3117 API_TRACE_IN(
"%ld", hContext)
3125 if (NULL == currentContextMap)
3147 scCancelStruct.hContext = hContext;
3151 sizeof(scCancelStruct), (
void *) &scCancelStruct);
3159 rv =
MessageReceive(&scCancelStruct,
sizeof(scCancelStruct), dwClientID);
3164 rv = scCancelStruct.rv;
3203 API_TRACE_IN(
"%ld", hContext)
3241 if (NULL == newContextMap)
3244 Log2(PCSC_LOG_DEBUG,
"Allocating new SCONTEXTMAP @%p", newContextMap);
3245 newContextMap->
hContext = hContext;
3249 (void)pthread_mutex_init(&newContextMap->
mMutex, NULL);
3251 lrv = list_init(&newContextMap->channelMapList);
3254 Log2(PCSC_LOG_CRITICAL,
"list_init failed with return value: %d", lrv);
3258 lrv = list_attributes_seeker(&newContextMap->channelMapList,
3259 CHANNEL_MAP_seeker);
3262 Log2(PCSC_LOG_CRITICAL,
3263 "list_attributes_seeker failed with return value: %d", lrv);
3264 list_destroy(&newContextMap->channelMapList);
3268 lrv = list_append(&contextMapList, newContextMap);
3271 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
3273 list_destroy(&newContextMap->channelMapList);
3281 (void)pthread_mutex_destroy(&newContextMap->
mMutex);
3282 free(newContextMap);
3311 if (NULL != currentContextMap)
3312 (void)pthread_mutex_lock(¤tContextMap->
mMutex);
3316 return currentContextMap;
3333 return list_seek(&contextMapList, &hContext);
3350 if (NULL != currentContextMap)
3351 SCardCleanContext(currentContextMap);
3354 static void SCardCleanContext(
SCONTEXTMAP * targetContextMap)
3356 int list_index, lrv;
3363 (void)pthread_mutex_destroy(&targetContextMap->
mMutex);
3365 listSize = list_size(&targetContextMap->channelMapList);
3366 for (list_index = 0; list_index < listSize; list_index++)
3368 currentChannelMap = list_get_at(&targetContextMap->channelMapList,
3370 if (NULL == currentChannelMap)
3372 Log2(PCSC_LOG_CRITICAL,
"list_get_at failed for index %d",
3378 free(currentChannelMap->readerName);
3379 free(currentChannelMap);
3383 list_destroy(&targetContextMap->channelMapList);
3385 lrv = list_delete(&contextMapList, targetContextMap);
3388 Log2(PCSC_LOG_CRITICAL,
3389 "list_delete failed with return value: %d", lrv);
3392 free(targetContextMap);
3408 if (NULL == newChannelMap)
3411 newChannelMap->hCard = hCard;
3412 newChannelMap->readerName = strdup(readerName);
3414 lrv = list_append(¤tContextMap->channelMapList, newChannelMap);
3417 free(newChannelMap->readerName);
3418 free(newChannelMap);
3419 Log2(PCSC_LOG_CRITICAL,
"list_append failed with return value: %d",
3434 rv = SCardGetContextAndChannelFromHandleTH(hCard, ¤tContextMap,
3435 ¤tChannelMap);
3439 free(currentChannelMap->readerName);
3441 lrv = list_delete(¤tContextMap->channelMapList, currentChannelMap);
3444 Log2(PCSC_LOG_CRITICAL,
3445 "list_delete failed with return value: %d", lrv);
3448 free(currentChannelMap);
3453 static LONG SCardGetContextChannelAndLockFromHandle(
SCARDHANDLE hCard,
3462 rv = SCardGetContextAndChannelFromHandleTH(hCard, targetContextMap,
3466 (void)pthread_mutex_lock(&(*targetContextMap)->mMutex);
3473 static LONG SCardGetContextAndChannelFromHandleTH(
SCARDHANDLE hCard,
3482 *targetContextMap = NULL;
3483 *targetChannelMap = NULL;
3485 listSize = list_size(&contextMapList);
3487 for (list_index = 0; list_index < listSize; list_index++)
3489 currentContextMap = list_get_at(&contextMapList, list_index);
3490 if (currentContextMap == NULL)
3492 Log2(PCSC_LOG_CRITICAL,
"list_get_at failed for index %d",
3496 currentChannelMap = list_seek(¤tContextMap->channelMapList,
3498 if (currentChannelMap != NULL)
3500 *targetContextMap = currentContextMap;
3501 *targetChannelMap = currentChannelMap;
3519 struct stat statBuffer;
3522 socketName = getSocketName();
3523 rv = stat(socketName, &statBuffer);
3527 Log3(PCSC_LOG_INFO,
"PCSC Not Running: %s: %s",
3528 socketName, strerror(errno));
3535 static LONG getReaderStates(
SCONTEXTMAP * currentContextMap)
3537 int32_t dwClientID = currentContextMap->
dwClientID;
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
used by SCardBeginTransaction()
contained in SCARD_CONNECT Messages.
uint32_t cardAtrLength
ATR length.
static void SCardLockThread(void)
Locks a mutex so another thread must wait to use this function.
wait for a reader state change
contained in SCARD_CANCEL Messages.
contained in SCARD_TRANSMIT Messages.
#define SCARD_STATE_UNAVAILABLE
Status unavailable.
LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
Returns a list of currently available readers on the system.
#define SCARD_S_SUCCESS
No error was encountered.
contained in SCARD_END_TRANSACTION Messages.
#define PCSCLITE_SHARING_NO_CONTEXT
No application is using the reader.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
Returns a list of currently available reader groups on the system.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.
#define SCARD_STATE_EMPTY
Card removed.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
get the client/server protocol version
INTERNAL void ClientCloseSession(uint32_t dwClientID)
Closes the socket used by the client to communicate with the server.
static SCONTEXTMAP * SCardGetContextTH(SCARDCONTEXT)
Get the address from the Application Context list _psContextMap for the passed context.
#define SCARD_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
#define SCARD_STATE_IGNORE
Ignore this reader.
#define SCARD_UNKNOWN
Unknown state.
static short isExecuted
Make sure the initialization code is executed only once.
used by SCardEstablishContext()
PCSC_API const SCARD_IO_REQUEST g_rgSCardT1Pci
Protocol Control Information for T=1.
#define SCARD_E_NO_READERS_AVAILABLE
Cannot find a smart card reader.
INTERNAL LONG MessageSendWithHeader(uint32_t command, uint32_t dwClientID, uint64_t size, void *data_void)
Wrapper for the MessageSend() function.
int32_t minor
IPC minor PROTOCOL_VERSION_MINOR.
used by SCardEndTransaction()
LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
unsigned long cbPciLength
Protocol Control Inf Length.
int32_t readerSharing
PCSCLITE_SHARING_* sharing status.
#define SCARD_STATE_CHANGED
State has changed.
This handles abstract system level calls.
uint32_t eventCounter
number of card events
PCSC_API const SCARD_IO_REQUEST g_rgSCardRawPci
Protocol Control Information for raw access.
#define PROTOCOL_VERSION_MAJOR
Major version of the current message protocol.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
contained in SCARD_DISCONNECT Messages.
LONG SCardFreeMemory(SCARDCONTEXT hContext, LPCVOID pvMem)
Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length ...
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 SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
Get an attribute from the IFD Handler (reader driver).
#define SCARD_PRESENT
Card is present.
Information contained in SCARD_RELEASE_CONTEXT Messages.
LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
LONG SCardCancel(SCARDCONTEXT hContext)
Cancels a specific blocking SCardGetStatusChange() function.
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
contained in SCARD_BEGIN_TRANSACTION Messages.
#define PCSCLITE_SHARING_EXCLUSIVE_CONTEXT
Reader used in exclusive mode.
LONG SCardIsValidContext(SCARDCONTEXT hContext)
Check if a SCARDCONTEXT is valid.
#define INFINITE
Infinite timeout.
#define SCARD_STATE_UNKNOWN
Reader unknown.
Represents an Application Context on the Client side.
Information contained in SCARD_ESTABLISH_CONTEXT Messages.
static void SCardRemoveContext(SCARDCONTEXT)
Removes an Application Context from a control vector.
#define PCSCLITE_LOCK_POLL_RATE
Lock polling rate.
#define SCARD_AUTOALLOCATE
see SCardFreeMemory()
static int SCardGetContextValidity(SCARDCONTEXT hContext)
Tell if a context index from the Application Context vector _psContextMap is valid or not...
Information transmitted in CMD_VERSION Messages.
INTERNAL LONG MessageReceive(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Called by the Client to get the reponse from the server or vice-versa.
used by SCardReleaseContext()
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
contained in SCARD_STATUS Messages.
#define SCARD_E_READER_UNAVAILABLE
The specified reader is not currently available for use.
contained in SCARD_RECONNECT Messages.
unsigned long dwProtocol
Protocol identifier.
uint32_t timeOut
timeout in ms
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
contained in SCARD_GET_ATTRIB and Messages.
#define SCARD_STATE_PRESENT
Card inserted.
PCSC_API const SCARD_IO_REQUEST g_rgSCardT0Pci
Protocol Control Information for T=0.
This defines some structures and #defines to be used over the transport layer.
Information contained in CMD_WAIT_READER_STATE_CHANGE Messages.
DWORD dwClientID
Client Connection ID.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
#define SCARD_STATE_ATRMATCH
ATR matches card.
static SCONTEXTMAP * SCardGetAndLockContext(SCARDCONTEXT)
Get the SCONTEXTMAP * from the Application Context vector _psContextMap for the passed context...
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 int time_sub(struct timeval *a, struct timeval *b)
return the difference (as long int) in µs between 2 struct timeval r = a - b
#define MAX_BUFFER_SIZE_EXTENDED
enhanced (64K + APDU + Lc + Le + SW) Tx/Rx Buffer
static READER_STATE readerStates[PCSCLITE_MAX_READERS_CONTEXTS]
Area used to read status information about the readers.
#define PCSCLITE_STATUS_POLL_RATE
Status polling rate.
Represents an Application Context Channel.
This handles card insertion/removal events, updates ATR, protocol, and status information.
SCARDCONTEXT hContext
Application Context ID.
#define SCARD_PROTOCOL_ANY
IFD determines prot.
char cancellable
We are in a cancellable call.
stop waiting for a reader state change
LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
Establishes a connection to the reader specified in * szReader.
#define SCARD_F_COMM_ERROR
An internal communications error has been detected.
LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
#define SCARD_STATE_EXCLUSIVE
Exclusive Mode.
#define SCARD_SWALLOWED
Card not powered.
static LONG SCardAddContext(SCARDCONTEXT, DWORD)
Functions for managing instances of SCardEstablishContext() These functions keep track of Context han...
UCHAR cardAtr[MAX_ATR_SIZE]
ATR.
int SYS_RandomInt(int, int)
Generate a pseudo random number.
LONG SCARDHANDLE
hCard returned by SCardConnect()
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
LONG SCardCheckDaemonAvailability(void)
Checks if the server is running.
INTERNAL int ClientSetupSession(uint32_t *pdwClientID)
Prepares a communication channel for the client to talk to the server.
#define PROTOCOL_VERSION_MINOR
Minor version of the current message protocol.
LONG SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
This keeps a list of defines for pcsc-lite.
#define SCARD_PROTOCOL_RAW
Raw active protocol.
#define SCARD_STATE_INUSE
Shared Mode.
Protocol Control Information (PCI)
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...
#define SCARD_ABSENT
Card is absent.
uint32_t cardProtocol
SCARD_PROTOCOL_* value.
Define an exported public reader state structure so each application gets instant notification of cha...
#define SCARD_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
INTERNAL LONG MessageSend(void *buffer_void, uint64_t buffer_size, int32_t filedes)
Sends a menssage from client to server or vice-versa.
used by SCardDisconnect()
PCSC_API const char * pcsc_stringify_error(const LONG pcscError)
Returns a human readable text for the given PC/SC error code.
contained in SCARD_CONTROL Messages.
This keeps track of a list of currently available reader structures.
static LONG SCardEstablishContextTH(DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT)
Creates a communication context to the PC/SC Resource Manager.
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.
pthread_mutex_t mMutex
Mutex for this context.
#define SCARD_E_UNKNOWN_READER
The specified reader name is not recognized.
static pthread_mutex_t clientMutex
Ensure that some functions be accessed in thread-safe mode.
uint32_t readerState
SCARD_* bit field.
#define PCSCLITE_SHARING_LAST_CONTEXT
One application is using the reader.
int32_t major
IPC major PROTOCOL_VERSION_MAJOR.
LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
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().
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
This handles smart card reader communications.
LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.
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 reponse from the server or vice-versa.
#define SCARD_STATE_UNAWARE
App wants status.
static void SCardUnlockThread(void)
Unlocks a mutex so another thread may use the client.
#define SCARD_STATE_MUTE
Unresponsive card.