-
Notifications
You must be signed in to change notification settings - Fork 0
/
readerfactory.c
1500 lines (1255 loc) · 38.2 KB
/
readerfactory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MUSCLE SmartCard Development ( http:https://pcsclite.alioth.debian.org/pcsclite.html )
*
* Copyright (C) 1999-2004
* David Corcoran <[email protected]>
* Copyright (C) 2003-2004
* Damien Sauveron <[email protected]>
* Copyright (C) 2002-2011
* Ludovic Rousseau <[email protected]>
* Copyright (C) 2009
* Jean-Luc Giraud <[email protected]>
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: readerfactory.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @file
* @brief This keeps track of a list of currently available reader structures.
*/
#include <PCSC/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <PCSC/misc.h>
#include <PCSC/pcscd.h>
#include <PCSC/debuglog.h>
#include <PCSC/readerfactory.h>
#include <PCSC/dyn_generic.h>
#include <PCSC/sys_generic.h>
#include <PCSC/eventhandler.h>
#include <PCSC/ifdwrapper.h>
#include <PCSC/hotplug.h>
#include <PCSC/strlcpycat.h>
#include <PCSC/configfile.h>
#include <PCSC/utils.h>
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
static READER_CONTEXT * sReadersContexts[PCSCLITE_MAX_READERS_CONTEXTS];
READER_STATE readerStates[PCSCLITE_MAX_READERS_CONTEXTS];
static int maxReaderHandles = PCSC_MAX_READER_HANDLES;
static DWORD dwNumReadersContexts = 0;
#ifdef USE_SERIAL
static char *ConfigFile = NULL;
static int ConfigFileCRC = 0;
#endif
static pthread_mutex_t LockMutex = PTHREAD_MUTEX_INITIALIZER;
#define IDENTITY_SHIFT 16
static LONG removeReader(READER_CONTEXT * sContext);
static int RDR_CLIHANDLES_seeker(const void *el, const void *key)
{
const RDR_CLIHANDLES *rdrCliHandles = el;
if ((el == NULL) || (key == NULL))
{
Log3(PCSC_LOG_CRITICAL,
"RDR_CLIHANDLES_seeker called with NULL pointer: el=%p, key=%p",
el, key);
return 0;
}
if (rdrCliHandles->hCard == *(SCARDHANDLE *)key)
return 1;
return 0;
}
LONG _RefReader(READER_CONTEXT * sReader)
{
if (0 == sReader->reference)
return SCARD_E_READER_UNAVAILABLE;
pthread_mutex_lock(&sReader->reference_lock);
sReader->reference += 1;
pthread_mutex_unlock(&sReader->reference_lock);
return SCARD_S_SUCCESS;
}
LONG _UnrefReader(READER_CONTEXT * sReader)
{
if (0 == sReader->reference)
return SCARD_E_READER_UNAVAILABLE;
pthread_mutex_lock(&sReader->reference_lock);
sReader->reference -= 1;
pthread_mutex_unlock(&sReader->reference_lock);
if (0 == sReader->reference)
removeReader(sReader);
return SCARD_S_SUCCESS;
}
LONG RFAllocateReaderSpace(unsigned int customMaxReaderHandles)
{
int i; /* Counter */
if (customMaxReaderHandles != 0)
maxReaderHandles = customMaxReaderHandles;
/* Allocate each reader structure */
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
sReadersContexts[i] = malloc(sizeof(READER_CONTEXT));
sReadersContexts[i]->vHandle = NULL;
/* Zero out each value in the struct */
memset(readerStates[i].readerName, 0, MAX_READERNAME);
memset(readerStates[i].cardAtr, 0, MAX_ATR_SIZE);
readerStates[i].readerState = 0;
readerStates[i].readerSharing = 0;
readerStates[i].cardAtrLength = READER_NOT_INITIALIZED;
readerStates[i].cardProtocol = SCARD_PROTOCOL_UNDEFINED;
sReadersContexts[i]->readerState = &readerStates[i];
}
/* Create public event structures */
return EHInitializeEventStructures();
}
LONG RFAddReader(const char *readerNameLong, int port, const char *library,
const char *device)
{
DWORD dwContext = 0, dwGetSize;
UCHAR ucGetData[1], ucThread[1];
LONG rv, parentNode;
int i, j;
int lrv = 0;
char *readerName = NULL;
if ((readerNameLong == NULL) || (library == NULL) || (device == NULL))
return SCARD_E_INVALID_VALUE;
/* allocate memory that is automatically freed */
readerName = alloca(strlen(readerNameLong)+1);
strcpy(readerName, readerNameLong);
/* Reader name too long? also count " 00 00"*/
if (strlen(readerName) > MAX_READERNAME - sizeof(" 00 00"))
{
Log3(PCSC_LOG_ERROR,
"Reader name too long: %zd chars instead of max %zd. Truncating!",
strlen(readerName), MAX_READERNAME - sizeof(" 00 00"));
readerName[MAX_READERNAME - sizeof(" 00 00")] = '\0';
}
/* Same name, same port - duplicate reader cannot be used */
if (dwNumReadersContexts != 0)
{
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle != 0)
{
char lpcStripReader[MAX_READERNAME];
int tmplen;
/* get the reader name without the reader and slot numbers */
strncpy(lpcStripReader,
sReadersContexts[i]->readerState->readerName,
sizeof(lpcStripReader));
tmplen = strlen(lpcStripReader);
lpcStripReader[tmplen - 6] = 0;
if ((strcmp(readerName, lpcStripReader) == 0) &&
(port == sReadersContexts[i]->port))
{
Log1(PCSC_LOG_ERROR, "Duplicate reader found.");
return SCARD_E_DUPLICATE_READER;
}
}
}
}
/* We must find an empty slot to put the reader structure */
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle == 0)
{
dwContext = i;
break;
}
}
if (i == PCSCLITE_MAX_READERS_CONTEXTS)
{
/* No more spots left return */
return SCARD_E_NO_MEMORY;
}
/* Check and set the readername to see if it must be enumerated */
parentNode = RFSetReaderName(sReadersContexts[dwContext], readerName,
library, port);
if (parentNode < -1)
return SCARD_E_NO_MEMORY;
sReadersContexts[dwContext]->library = strdup(library);
sReadersContexts[dwContext]->device = strdup(device);
sReadersContexts[dwContext]->version = 0;
sReadersContexts[dwContext]->port = port;
sReadersContexts[dwContext]->mMutex = NULL;
sReadersContexts[dwContext]->contexts = 0;
sReadersContexts[dwContext]->pthThread = 0;
sReadersContexts[dwContext]->hLockId = 0;
sReadersContexts[dwContext]->LockCount = 0;
sReadersContexts[dwContext]->vHandle = NULL;
sReadersContexts[dwContext]->pFeeds = NULL;
sReadersContexts[dwContext]->pMutex = NULL;
sReadersContexts[dwContext]->pthCardEvent = NULL;
lrv = list_init(&sReadersContexts[dwContext]->handlesList);
if (lrv < 0)
{
Log2(PCSC_LOG_CRITICAL, "list_init failed with return value: %d", lrv);
return SCARD_E_NO_MEMORY;
}
lrv = list_attributes_seeker(&sReadersContexts[dwContext]->handlesList,
RDR_CLIHANDLES_seeker);
if (lrv < 0)
{
Log2(PCSC_LOG_CRITICAL,
"list_attributes_seeker failed with return value: %d", lrv);
return SCARD_E_NO_MEMORY;
}
(void)pthread_mutex_init(&sReadersContexts[dwContext]->handlesList_lock,
NULL);
(void)pthread_mutex_init(&sReadersContexts[dwContext]->powerState_lock,
NULL);
sReadersContexts[dwContext]->powerState = POWER_STATE_UNPOWERED;
/* reference count */
(void)pthread_mutex_init(&sReadersContexts[dwContext]->reference_lock,
NULL);
sReadersContexts[dwContext]->reference = 1;
/* If a clone to this reader exists take some values from that clone */
if (parentNode >= 0 && parentNode < PCSCLITE_MAX_READERS_CONTEXTS)
{
sReadersContexts[dwContext]->pFeeds =
sReadersContexts[parentNode]->pFeeds;
*(sReadersContexts[dwContext])->pFeeds += 1;
sReadersContexts[dwContext]->vHandle =
sReadersContexts[parentNode]->vHandle;
sReadersContexts[dwContext]->mMutex =
sReadersContexts[parentNode]->mMutex;
sReadersContexts[dwContext]->pMutex =
sReadersContexts[parentNode]->pMutex;
/* Call on the parent driver to see if it is thread safe */
dwGetSize = sizeof(ucThread);
rv = IFDGetCapabilities(sReadersContexts[parentNode],
TAG_IFD_THREAD_SAFE, &dwGetSize, ucThread);
if (rv == IFD_SUCCESS && dwGetSize == 1 && ucThread[0] == 1)
{
Log1(PCSC_LOG_INFO, "Driver is thread safe");
sReadersContexts[dwContext]->mMutex = NULL;
sReadersContexts[dwContext]->pMutex = NULL;
}
else
*(sReadersContexts[dwContext])->pMutex += 1;
}
if (sReadersContexts[dwContext]->pFeeds == NULL)
{
sReadersContexts[dwContext]->pFeeds = malloc(sizeof(int));
/* Initialize pFeeds to 1, otherwise multiple
cloned readers will cause pcscd to crash when
RFUnloadReader unloads the driver library
and there are still devices attached using it --mikeg*/
*(sReadersContexts[dwContext])->pFeeds = 1;
}
if (sReadersContexts[dwContext]->mMutex == 0)
{
sReadersContexts[dwContext]->mMutex =
malloc(sizeof(pthread_mutex_t));
(void)pthread_mutex_init(sReadersContexts[dwContext]->mMutex, NULL);
}
if (sReadersContexts[dwContext]->pMutex == NULL)
{
sReadersContexts[dwContext]->pMutex = malloc(sizeof(int));
*(sReadersContexts[dwContext])->pMutex = 1;
}
dwNumReadersContexts += 1;
rv = RFInitializeReader(sReadersContexts[dwContext]);
if (rv != SCARD_S_SUCCESS)
{
/* Cannot connect to reader. Exit gracefully */
Log2(PCSC_LOG_ERROR, "%s init failed.", readerName);
(void)RFRemoveReader(readerName, port);
return rv;
}
/* asynchronous card movement? */
{
RESPONSECODE (*fct)(DWORD, int) = NULL;
dwGetSize = sizeof(fct);
rv = IFDGetCapabilities(sReadersContexts[dwContext],
TAG_IFD_POLLING_THREAD_WITH_TIMEOUT, &dwGetSize, (PUCHAR)&fct);
if ((rv != SCARD_S_SUCCESS) || (dwGetSize != sizeof(fct)))
{
Log1(PCSC_LOG_INFO, "Using the pcscd polling thread");
}
else
{
sReadersContexts[dwContext]->pthCardEvent = fct;
Log1(PCSC_LOG_INFO, "Using the reader polling thread");
}
rv = EHSpawnEventHandler(sReadersContexts[dwContext]);
if (rv != SCARD_S_SUCCESS)
{
Log2(PCSC_LOG_ERROR, "%s init failed.", readerName);
(void)RFRemoveReader(readerName, port);
return rv;
}
}
/* Call on the driver to see if there are multiple slots */
dwGetSize = sizeof(ucGetData);
rv = IFDGetCapabilities((sReadersContexts[dwContext]),
TAG_IFD_SLOTS_NUMBER, &dwGetSize, ucGetData);
if (rv != IFD_SUCCESS || dwGetSize != 1 || ucGetData[0] == 0)
/* Reader does not have this defined. Must be a single slot
* reader so we can just return SCARD_S_SUCCESS. */
return SCARD_S_SUCCESS;
if (rv == IFD_SUCCESS && dwGetSize == 1 && ucGetData[0] == 1)
/* Reader has this defined and it only has one slot */
return SCARD_S_SUCCESS;
/*
* Check the number of slots and create a different
* structure for each one accordingly
*/
/* Initialize the rest of the slots */
for (j = 1; j < ucGetData[0]; j++)
{
char *tmpReader = NULL;
DWORD dwContextB = 0;
RESPONSECODE (*fct)(DWORD, int) = NULL;
/* We must find an empty spot to put the reader structure */
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle == 0)
{
dwContextB = i;
break;
}
}
if (i == PCSCLITE_MAX_READERS_CONTEXTS)
{
/* No more slot left return */
RFRemoveReader(readerName, port);
return SCARD_E_NO_MEMORY;
}
/* Copy the previous reader name and increment the slot number */
tmpReader = sReadersContexts[dwContextB]->readerState->readerName;
(void)strlcpy(tmpReader,
sReadersContexts[dwContext]->readerState->readerName,
sizeof(sReadersContexts[dwContextB]->readerState->readerName));
snprintf(tmpReader + strlen(tmpReader) - 2, 3, "%02X", j);
sReadersContexts[dwContextB]->library =
sReadersContexts[dwContext]->library;
sReadersContexts[dwContextB]->device =
sReadersContexts[dwContext]->device;
sReadersContexts[dwContextB]->version =
sReadersContexts[dwContext]->version;
sReadersContexts[dwContextB]->port =
sReadersContexts[dwContext]->port;
sReadersContexts[dwContextB]->vHandle =
sReadersContexts[dwContext]->vHandle;
sReadersContexts[dwContextB]->mMutex =
sReadersContexts[dwContext]->mMutex;
sReadersContexts[dwContextB]->pMutex =
sReadersContexts[dwContext]->pMutex;
sReadersContexts[dwContextB]->slot =
sReadersContexts[dwContext]->slot + j;
sReadersContexts[dwContextB]->pthCardEvent = NULL;
/*
* Added by Dave - slots did not have a pFeeds
* parameter so it was by luck they were working
*/
sReadersContexts[dwContextB]->pFeeds =
sReadersContexts[dwContext]->pFeeds;
/* Added by Dave for multiple slots */
*(sReadersContexts[dwContextB])->pFeeds += 1;
sReadersContexts[dwContextB]->contexts = 0;
sReadersContexts[dwContextB]->hLockId = 0;
sReadersContexts[dwContextB]->LockCount = 0;
lrv = list_init(&sReadersContexts[dwContextB]->handlesList);
if (lrv < 0)
{
Log2(PCSC_LOG_CRITICAL, "list_init failed with return value: %d", lrv);
return SCARD_E_NO_MEMORY;
}
lrv = list_attributes_seeker(&sReadersContexts[dwContextB]->handlesList,
RDR_CLIHANDLES_seeker);
if (lrv < 0)
{
Log2(PCSC_LOG_CRITICAL,
"list_attributes_seeker failed with return value: %d", lrv);
return SCARD_E_NO_MEMORY;
}
(void)pthread_mutex_init(&sReadersContexts[dwContextB]->handlesList_lock, NULL);
(void)pthread_mutex_init(&sReadersContexts[dwContextB]->powerState_lock,
NULL);
sReadersContexts[dwContextB]->powerState = POWER_STATE_UNPOWERED;
/* reference count */
(void)pthread_mutex_init(&sReadersContexts[dwContextB]->reference_lock,
NULL);
sReadersContexts[dwContextB]->reference = 1;
/* Call on the parent driver to see if the slots are thread safe */
dwGetSize = sizeof(ucThread);
rv = IFDGetCapabilities((sReadersContexts[dwContext]),
TAG_IFD_SLOT_THREAD_SAFE, &dwGetSize, ucThread);
if (rv == IFD_SUCCESS && dwGetSize == 1 && ucThread[0] == 1)
{
Log1(PCSC_LOG_INFO, "Driver is slot thread safe");
sReadersContexts[dwContextB]->library =
strdup(sReadersContexts[dwContext]->library);
sReadersContexts[dwContextB]->device =
strdup(sReadersContexts[dwContext]->device);
sReadersContexts[dwContextB]->mMutex =
malloc(sizeof(pthread_mutex_t));
(void)pthread_mutex_init(sReadersContexts[dwContextB]->mMutex,
NULL);
sReadersContexts[dwContextB]->pMutex = malloc(sizeof(int));
*(sReadersContexts[dwContextB])->pMutex = 1;
}
else
*(sReadersContexts[dwContextB])->pMutex += 1;
dwNumReadersContexts += 1;
rv = RFInitializeReader(sReadersContexts[dwContextB]);
if (rv != SCARD_S_SUCCESS)
{
/* Cannot connect to slot. Exit gracefully */
(void)RFRemoveReader(readerName, port);
return rv;
}
/* asynchronous card movement? */
dwGetSize = sizeof(fct);
rv = IFDGetCapabilities((sReadersContexts[dwContextB]),
TAG_IFD_POLLING_THREAD_WITH_TIMEOUT, &dwGetSize, (PUCHAR)&fct);
if ((rv != SCARD_S_SUCCESS) || (dwGetSize != sizeof(fct)))
{
Log1(PCSC_LOG_INFO, "Using the pcscd polling thread");
}
else
{
sReadersContexts[dwContextB]->pthCardEvent = fct;
Log1(PCSC_LOG_INFO, "Using the reader polling thread");
}
rv = EHSpawnEventHandler(sReadersContexts[dwContextB]);
if (rv != SCARD_S_SUCCESS)
{
Log2(PCSC_LOG_ERROR, "%s init failed.", readerName);
(void)RFRemoveReader(readerName, port);
return rv;
}
}
return SCARD_S_SUCCESS;
}
LONG RFRemoveReader(const char *readerName, int port)
{
char lpcStripReader[MAX_READERNAME];
int i;
if (readerName == NULL)
return SCARD_E_INVALID_VALUE;
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle != 0)
{
strncpy(lpcStripReader,
sReadersContexts[i]->readerState->readerName,
sizeof(lpcStripReader));
lpcStripReader[strlen(lpcStripReader) - 6] = 0;
/* Compare only the significant part of the reader name */
if ((strncmp(readerName, lpcStripReader, MAX_READERNAME - sizeof(" 00 00")) == 0)
&& (port == sReadersContexts[i]->port))
{
/* remove the reader */
UNREF_READER(sReadersContexts[i])
}
}
}
return SCARD_S_SUCCESS;
}
LONG removeReader(READER_CONTEXT * sContext)
{
LONG rv;
{
/* Try to destroy the thread */
if (sContext -> pthThread)
(void)EHDestroyEventHandler(sContext);
if ((NULL == sContext->pMutex) || (NULL == sContext->pFeeds))
{
Log1(PCSC_LOG_ERROR,
"Trying to remove an already removed driver");
return SCARD_E_INVALID_VALUE;
}
rv = RFUnInitializeReader(sContext);
if (rv != SCARD_S_SUCCESS)
return rv;
*sContext->pMutex -= 1;
/* free shared resources when the last slot is closed */
if (0 == *sContext->pMutex)
{
(void)pthread_mutex_destroy(sContext->mMutex);
free(sContext->mMutex);
sContext->mMutex = NULL;
free(sContext->library);
free(sContext->device);
free(sContext->pMutex);
sContext->pMutex = NULL;
}
*sContext->pFeeds -= 1;
/* Added by Dave to free the pFeeds variable */
if (*sContext->pFeeds == 0)
{
free(sContext->pFeeds);
sContext->pFeeds = NULL;
}
(void)pthread_mutex_destroy(&sContext->powerState_lock);
sContext->version = 0;
sContext->port = 0;
sContext->contexts = 0;
sContext->slot = 0;
sContext->hLockId = 0;
sContext->LockCount = 0;
sContext->vHandle = NULL;
(void)pthread_mutex_lock(&sContext->handlesList_lock);
while (list_size(&sContext->handlesList) != 0)
{
int lrv;
RDR_CLIHANDLES *currentHandle;
currentHandle = list_get_at(&sContext->handlesList, 0);
lrv = list_delete_at(&sContext->handlesList, 0);
if (lrv < 0)
Log2(PCSC_LOG_CRITICAL,
"list_delete_at failed with return value: %d", lrv);
free(currentHandle);
}
(void)pthread_mutex_unlock(&sContext->handlesList_lock);
(void)pthread_mutex_destroy(&sContext->handlesList_lock);
list_destroy(&sContext->handlesList);
dwNumReadersContexts -= 1;
/* signal an event to clients */
(void)EHSignalEventToClients();
}
return SCARD_S_SUCCESS;
}
LONG RFSetReaderName(READER_CONTEXT * rContext, const char *readerName,
const char *libraryName, int port)
{
LONG parent = -1; /* reader number of the parent of the clone */
DWORD valueLength;
int currentDigit = -1;
int supportedChannels = 0;
int usedDigits[PCSCLITE_MAX_READERS_CONTEXTS];
int i;
/* Clear the list */
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
usedDigits[i] = FALSE;
if (dwNumReadersContexts != 0)
{
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle != 0)
{
if (strcmp(sReadersContexts[i]->library, libraryName) == 0)
{
UCHAR tagValue[1];
LONG ret;
/* Ask the driver if it supports multiple channels */
valueLength = sizeof(tagValue);
ret = IFDGetCapabilities(sReadersContexts[i],
TAG_IFD_SIMULTANEOUS_ACCESS,
&valueLength, tagValue);
if ((ret == IFD_SUCCESS) && (valueLength == 1) &&
(tagValue[0] > 1))
{
supportedChannels = tagValue[0];
Log2(PCSC_LOG_INFO,
"Support %d simultaneous readers", tagValue[0]);
}
else
supportedChannels = 1;
/* Check to see if it is a hotplug reader and different */
if ((((sReadersContexts[i]->port & 0xFFFF0000) ==
PCSCLITE_HP_BASE_PORT)
&& (sReadersContexts[i]->port != port))
|| (supportedChannels > 1))
{
const char *reader = sReadersContexts[i]->readerState->readerName;
/*
* tells the caller who the parent of this
* clone is so it can use it's shared
* resources like mutex/etc.
*/
parent = i;
/*
* If the same reader already exists and it is
* hotplug then we must look for others and
* enumerate the readername
*/
currentDigit = strtol(reader + strlen(reader) - 5, NULL, 16);
/* This spot is taken */
usedDigits[currentDigit] = TRUE;
}
}
}
}
}
/* default value */
i = 0;
/* Other identical readers exist on the same bus */
if (currentDigit != -1)
{
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
/* get the first free digit */
if (usedDigits[i] == FALSE)
break;
}
if (i == PCSCLITE_MAX_READERS_CONTEXTS)
{
Log2(PCSC_LOG_ERROR, "Max number of readers reached: %d", PCSCLITE_MAX_READERS_CONTEXTS);
return -2;
}
if (i >= supportedChannels)
{
Log3(PCSC_LOG_ERROR, "Driver %s does not support more than "
"%d reader(s). Maybe the driver should support "
"TAG_IFD_SIMULTANEOUS_ACCESS", libraryName, supportedChannels);
return -2;
}
}
snprintf(rContext->readerState->readerName,
sizeof(rContext->readerState->readerName), "%s %02X 00",
readerName, i);
/* Set the slot in 0xDDDDCCCC */
rContext->slot = i << 16;
return parent;
}
LONG RFReaderInfo(const char *readerName, READER_CONTEXT ** sReader)
{
int i;
if (readerName == NULL)
return SCARD_E_UNKNOWN_READER;
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle != 0)
{
if (strcmp(readerName,sReadersContexts[i]->readerState->readerName) == 0)
{
/* Increase reference count */
REF_READER(sReadersContexts[i])
*sReader = sReadersContexts[i];
return SCARD_S_SUCCESS;
}
}
}
return SCARD_E_UNKNOWN_READER;
}
LONG RFReaderInfoById(SCARDHANDLE hCard, READER_CONTEXT * * sReader)
{
int i;
for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
{
if (sReadersContexts[i]->vHandle != 0)
{
RDR_CLIHANDLES * currentHandle;
(void)pthread_mutex_lock(&sReadersContexts[i]->handlesList_lock);
currentHandle = list_seek(&sReadersContexts[i]->handlesList,
&hCard);
(void)pthread_mutex_unlock(&sReadersContexts[i]->handlesList_lock);
if (currentHandle != NULL)
{
/* Increase reference count */
REF_READER(sReadersContexts[i])
*sReader = sReadersContexts[i];
return SCARD_S_SUCCESS;
}
}
}
return SCARD_E_INVALID_VALUE;
}
LONG RFLoadReader(READER_CONTEXT * rContext)
{
if (rContext->vHandle != 0)
{
Log2(PCSC_LOG_INFO, "Reusing already loaded driver for %s",
rContext->library);
/* Another reader exists with this library loaded */
return SCARD_S_SUCCESS;
}
return DYN_LoadLibrary(&rContext->vHandle, rContext->library);
}
LONG RFBindFunctions(READER_CONTEXT * rContext)
{
int rv;
void *f;
rv = DYN_GetAddress(rContext->vHandle, &f, "IFDHCreateChannelByName", TRUE);
if (SCARD_S_SUCCESS == rv)
{
/* Ifd Handler 3.0 found */
rContext->version = IFD_HVERSION_3_0;
}
else
{
rv = DYN_GetAddress(rContext->vHandle, &f, "IFDHCreateChannel", FALSE);
if (SCARD_S_SUCCESS == rv)
{
/* Ifd Handler 2.0 found */
rContext->version = IFD_HVERSION_2_0;
}
else
{
/* Neither version of the IFD Handler was found - exit */
Log1(PCSC_LOG_CRITICAL, "IFDHandler functions missing");
return SCARD_F_UNKNOWN_ERROR;
}
}
if (rContext->version == IFD_HVERSION_2_0)
{
/* The following binds version 2.0 of the IFD Handler specs */
#define GET_ADDRESS_OPTIONALv2(s, code) \
{ \
void *f1 = NULL; \
int rvl = DYN_GetAddress(rContext->vHandle, &f1, "IFDH" #s, FALSE); \
if (SCARD_S_SUCCESS != rvl) \
{ \
code \
} \
rContext->psFunctions.psFunctions_v2.pvf ## s = f1; \
}
#define GET_ADDRESSv2(s) \
GET_ADDRESS_OPTIONALv2(s, \
Log1(PCSC_LOG_CRITICAL, "IFDHandler functions missing: " #s ); \
return(rv); )
Log1(PCSC_LOG_INFO, "Loading IFD Handler 2.0");
GET_ADDRESSv2(CreateChannel)
GET_ADDRESSv2(CloseChannel)
GET_ADDRESSv2(GetCapabilities)
GET_ADDRESSv2(SetCapabilities)
GET_ADDRESSv2(PowerICC)
GET_ADDRESSv2(TransmitToICC)
GET_ADDRESSv2(ICCPresence)
GET_ADDRESS_OPTIONALv2(SetProtocolParameters, )
GET_ADDRESSv2(Control)
}
else if (rContext->version == IFD_HVERSION_3_0)
{
/* The following binds version 3.0 of the IFD Handler specs */
#define GET_ADDRESS_OPTIONALv3(s, code) \
{ \
void *f1 = NULL; \
int rvl = DYN_GetAddress(rContext->vHandle, &f1, "IFDH" #s, FALSE); \
if (SCARD_S_SUCCESS != rvl) \
{ \
code \
} \
rContext->psFunctions.psFunctions_v3.pvf ## s = f1; \
}
#define GET_ADDRESSv3(s) \
GET_ADDRESS_OPTIONALv3(s, \
Log1(PCSC_LOG_CRITICAL, "IFDHandler functions missing: " #s ); \
return(rv); )
Log1(PCSC_LOG_INFO, "Loading IFD Handler 3.0");
GET_ADDRESSv2(CreateChannel)
GET_ADDRESSv2(CloseChannel)
GET_ADDRESSv2(GetCapabilities)
GET_ADDRESSv2(SetCapabilities)
GET_ADDRESSv2(PowerICC)
GET_ADDRESSv2(TransmitToICC)
GET_ADDRESSv2(ICCPresence)
GET_ADDRESS_OPTIONALv2(SetProtocolParameters, )
GET_ADDRESSv3(CreateChannelByName)
GET_ADDRESSv3(Control)
}
else
{
/* Who knows what could have happenned for it to get here. */
Log1(PCSC_LOG_CRITICAL, "IFD Handler not 1.0/2.0 or 3.0");
return SCARD_F_UNKNOWN_ERROR;
}
return SCARD_S_SUCCESS;
}
LONG RFUnBindFunctions(READER_CONTEXT * rContext)
{
/* Zero out everything */
memset(&rContext->psFunctions, 0, sizeof(rContext->psFunctions));
return SCARD_S_SUCCESS;
}
LONG RFUnloadReader(READER_CONTEXT * rContext)
{
/* Make sure no one else is using this library */
if (*rContext->pFeeds == 1)
{
Log1(PCSC_LOG_INFO, "Unloading reader driver.");
(void)DYN_CloseLibrary(&rContext->vHandle);
}
rContext->vHandle = NULL;
return SCARD_S_SUCCESS;
}
LONG RFCheckSharing(SCARDHANDLE hCard, READER_CONTEXT * rContext)
{
if (rContext->hLockId == 0 || rContext->hLockId == hCard)
return SCARD_S_SUCCESS;
else
return SCARD_E_SHARING_VIOLATION;
}
LONG RFLockSharing(SCARDHANDLE hCard, READER_CONTEXT * rContext)
{
LONG rv;
(void)pthread_mutex_lock(&LockMutex);
rv = RFCheckSharing(hCard, rContext);
if (SCARD_S_SUCCESS == rv)
{
rContext->LockCount += 1;
rContext->hLockId = hCard;
}
(void)pthread_mutex_unlock(&LockMutex);
return rv;
}
LONG RFUnlockSharing(SCARDHANDLE hCard, READER_CONTEXT * rContext)
{
LONG rv;
(void)pthread_mutex_lock(&LockMutex);
rv = RFCheckSharing(hCard, rContext);
if (SCARD_S_SUCCESS == rv)
{
if (rContext->LockCount > 0)
{
rContext->LockCount -= 1;
if (0 == rContext->LockCount)
rContext->hLockId = 0;
}
else
/* rContext->LockCount == 0 */
rv = SCARD_E_NOT_TRANSACTED;
}
(void)pthread_mutex_unlock(&LockMutex);
return rv;
}
LONG RFUnlockAllSharing(SCARDHANDLE hCard, READER_CONTEXT * rContext)
{
LONG rv;
(void)pthread_mutex_lock(&LockMutex);
rv = RFCheckSharing(hCard, rContext);
if (SCARD_S_SUCCESS == rv)
{
rContext->LockCount = 0;
rContext->hLockId = 0;