-
Notifications
You must be signed in to change notification settings - Fork 1
/
module-cccshare.c
1753 lines (1556 loc) · 44.1 KB
/
module-cccshare.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
#define MODULE_LOG_PREFIX "cccam"
#include "globals.h"
#if defined(MODULE_CCCAM) && defined(MODULE_CCCSHARE)
#include "module-cccam.h"
#include "module-cccam-data.h"
#include "module-cccshare.h"
#include "ncam-chk.h"
#include "ncam-client.h"
#include "ncam-lock.h"
#include "ncam-string.h"
#include "ncam-time.h"
#include "ncam-work.h"
extern uint32_t cfg_sidtab_generation;
static uint32_t cc_share_id = 0x64;
static LLIST *reported_carddatas_list[CAID_KEY];
static CS_MUTEX_LOCK cc_shares_lock;
static int32_t card_added_count;
static int32_t card_removed_count;
static int32_t card_dup_count;
static pthread_t share_updater_thread;
static bool share_updater_thread_active;
static bool share_updater_refresh;
int32_t card_valid_for_client(struct s_client *cl, struct cc_card *card);
LLIST *get_cardlist(uint16_t caid, LLIST **list)
{
caid = (caid >> 8) % CAID_KEY;
if(!list[caid])
{ list[caid] = ll_create("card_list"); }
return list[caid];
}
LLIST **get_and_lock_sharelist(void)
{
cs_readlock(__func__, &cc_shares_lock);
return reported_carddatas_list;
}
void unlock_sharelist(void)
{
cs_readunlock(__func__, &cc_shares_lock);
}
void add_good_sids(struct s_sidtab *ptr, struct cc_card *card)
{
int32_t l;
for(l = 0; l < ptr->num_srvid; l++)
{
struct cc_srvid *srvid;
if(!cs_malloc(&srvid, sizeof(struct cc_srvid)))
{ return; }
srvid->sid = ptr->srvid[l];
srvid->chid = 0;
srvid->ecmlen = 0; // 0=undefined, also not used with "O" CCcam
if(!ll_contains_data(card->goodsids, srvid, sizeof(struct cc_srvid)))
{ ll_append(card->goodsids, srvid); }
else { NULLFREE(srvid);}
}
}
void add_bad_sids(struct s_sidtab *ptr, struct cc_card *card)
{
int32_t l;
for(l = 0; l < ptr->num_srvid; l++)
{
struct cc_srvid_block *srvid;
if(!cs_malloc(&srvid, sizeof(struct cc_srvid_block)))
{ return; }
srvid->sid = ptr->srvid[l];
srvid->chid = 0;
srvid->ecmlen = 0; // 0=undefined, also not used with "O" CCcam
srvid->blocked_till = 0;
if(!ll_contains_data(card->badsids, srvid, sizeof(struct cc_srvid_block)))
{ ll_append(card->badsids, srvid); }
else { NULLFREE(srvid); }
}
}
void add_good_bad_sids_by_rdr(struct s_reader *rdr, struct cc_card *card)
{
struct s_sidtab *ptr;
int32_t n, i;
for(n = 0, ptr = cfg.sidtab; ptr; ptr = ptr->next, n++)
{
if(rdr->sidtabs.ok & ((SIDTABBITS)1 << n))
{
for(i = 0; i < ptr->num_caid; i++)
{
if(ptr->caid[i] == card->caid)
{ add_good_sids(ptr, card); }
}
}
else if(rdr->sidtabs.no & ((SIDTABBITS)1 << n))
{
for(i = 0; i < ptr->num_caid; i++)
{
if(ptr->caid[i] == card->caid)
{ add_bad_sids(ptr, card); }
}
}
}
}
int32_t can_use_ext(struct cc_card *card)
{
if(card->card_type == CT_REMOTECARD)
{ return card->is_ext; }
if(card->sidtab)
{ return (card->sidtab->num_srvid > 0); }
else
{ return ll_count(card->goodsids) || ll_count(card->badsids); }
return 0;
}
int32_t write_card(struct cc_data *cc, uint8_t *buf, struct cc_card *card, int32_t add_own, int32_t ext, int32_t au_allowed, struct s_client *cl)
{
memset(buf, 0, CC_MAXMSGSIZE);
buf[0] = card->id >> 24;
buf[1] = card->id >> 16;
buf[2] = card->id >> 8;
buf[3] = card->id & 0xff;
buf[4] = card->remote_id >> 24;
buf[5] = card->remote_id >> 16;
buf[6] = card->remote_id >> 8;
buf[7] = card->remote_id & 0xFF;
buf[8] = card->caid >> 8;
buf[9] = card->caid & 0xff;
buf[10] = card->hop;
buf[11] = card->reshare;
if(au_allowed)
{ memcpy(buf + 12, card->hexserial, 8); }
// with cccam 2.2.0 we have assigned and rejected sids:
int32_t ofs = ext ? 23 : 21;
// write providers:
LL_ITER it = ll_iter_create(card->providers);
struct cc_provider *prov;
while((prov = ll_iter_next(&it)))
{
uint32_t prid = prov->prov;
buf[ofs + 0] = prid >> 16;
buf[ofs + 1] = prid >> 8;
buf[ofs + 2] = prid & 0xFF;
if(au_allowed)
{ memcpy(buf + ofs + 3, prov->sa, 4); }
buf[20]++;
ofs += 7;
}
// write sids only if cccam 2.2.x:
if(ext)
{
if(card->sidtab)
{
// good sids:
struct s_sidtab *ptr = card->sidtab;
int32_t l;
for(l = 0; l < ptr->num_srvid; l++)
{
buf[ofs + 0] = ptr->srvid[l] >> 8;
buf[ofs + 1] = ptr->srvid[l] & 0xFF;
ofs += 2;
buf[21]++; // nassign
if(buf[21] >= 240)
{ break; }
}
// bad sids:
int32_t n;
for(n = 0, ptr = cfg.sidtab; ptr; ptr = ptr->next, n++)
{
if((cl->sidtabs.no & ((SIDTABBITS)1 << n)) || (card->sidtabno & ((SIDTABBITS)1 << n)))
{
int32_t m;
int32_t ok_caid = 0;
for(m = 0; m < ptr->num_caid; m++) // search bad sids for this caid:
{
if(ptr->caid[m] == card->caid)
{
ok_caid = 1;
break;
}
}
if(ok_caid)
{
for(l = 0; l < ptr->num_srvid; l++)
{
buf[ofs + 0] = ptr->srvid[l] >> 8;
buf[ofs + 1] = ptr->srvid[l] & 0xFF;
ofs += 2;
buf[22]++; // nreject
if(buf[22] >= 240)
{ break; }
}
}
}
if(buf[22] >= 240)
{ break; }
}
}
else
{
// assigned sids:
it = ll_iter_create(card->goodsids);
struct cc_srvid *srvid;
struct cc_srvid_block *srvidblock;
while((srvid = ll_iter_next(&it)))
{
buf[ofs + 0] = srvid->sid >> 8;
buf[ofs + 1] = srvid->sid & 0xFF;
ofs += 2;
buf[21]++; // nassign
if(buf[21] >= 200)
{ break; }
}
// reject sids:
it = ll_iter_create(card->badsids);
while((srvidblock = ll_iter_next(&it)))
{
if(srvidblock->blocked_till > 0)
{
continue;
}
buf[ofs + 0] = srvidblock->sid >> 8;
buf[ofs + 1] = srvidblock->sid & 0xFF;
ofs += 2;
buf[22]++; // nreject
if(buf[22] >= 200)
{ break; }
}
}
}
// write remote nodes
int32_t nremote_ofs = ofs;
ofs++;
it = ll_iter_create(card->remote_nodes);
uint8_t *remote_node;
while((remote_node = ll_iter_next(&it)))
{
memcpy(buf + ofs, remote_node, 8);
ofs += 8;
buf[nremote_ofs]++;
}
if(add_own)
{
memcpy(buf + ofs, cc->node_id, 8);
ofs += 8;
buf[nremote_ofs]++;
}
return ofs;
}
static int32_t is_client_au_allowed(struct cc_card *card, struct s_client *cl)
{
if(!card || !card->origin_reader)
{
return 0;
}
if(!cl || !cl->aureader_list || !ll_count(cl->aureader_list))
{
return 0;
}
struct s_reader *rdr = NULL;
LL_ITER itr = ll_iter_create(cl->aureader_list);
while((rdr = ll_iter_next(&itr)))
{
if(rdr == card->origin_reader)
{
return 1;
}
}
return 0;
}
static int32_t send_card_to_client(struct cc_card *card, struct s_client *cl)
{
uint8_t buf[CC_MAXMSGSIZE];
if(!card_valid_for_client(cl, card))
{ return 0; }
int8_t usr_reshare = cl->account->cccreshare;
if(usr_reshare == -1)
{ usr_reshare = cfg.cc_reshare; }
int8_t ignorereshare = cl->account->cccignorereshare;
if(ignorereshare == -1)
{ ignorereshare = cfg.cc_ignore_reshare; }
int8_t reader_reshare = card->origin_reader ? card->rdr_reshare : usr_reshare;
if(reader_reshare == -1)
{ reader_reshare = cfg.cc_reshare; }
int8_t reshare = (reader_reshare < usr_reshare) ? reader_reshare : usr_reshare;
int8_t new_reshare;
if(card->card_type == CT_CARD_BY_SERVICE_USER)
{
new_reshare = usr_reshare;
}
else if(ignorereshare)
{
new_reshare = reshare;
}
else
{
new_reshare = card->reshare;
if(card->card_type == CT_REMOTECARD)
{ new_reshare--; }
if(new_reshare > reshare)
{ new_reshare = reshare; }
}
if(new_reshare < 0)
{ return 0; }
if(!card->id)
{ card->id = cc_share_id++; }
struct cc_data *cc = cl->cc;
int32_t is_ext = cc->cccam220 && can_use_ext(card);
int32_t len = write_card(cc, buf, card, 1, is_ext, is_client_au_allowed(card, cl), cl);
//buf[10] = card->hop-1;
buf[11] = new_reshare;
struct s_clientmsg *clientmsg;
if(cs_malloc(&clientmsg, sizeof(struct s_clientmsg)))
{
memcpy(clientmsg->msg, buf, len);
clientmsg->len = len;
clientmsg->cmd = is_ext ? MSG_NEW_CARD_SIDINFO : MSG_NEW_CARD;
add_job(cl, ACTION_CLIENT_SEND_MSG, clientmsg, sizeof(struct s_clientmsg));
}
return 1;
}
int32_t hide_card_to_client(struct cc_card *card, struct s_client *cl)
{
if(!card || !card->id)
{ return 0; }
uint8_t buf[4];
buf[0] = card->id >> 24;
buf[1] = card->id >> 16;
buf[2] = card->id >> 8;
buf[3] = card->id & 0xFF;
struct s_clientmsg *clientmsg;
struct cc_data *cc = cl->cc;
if(cc && (cl->typ == 'c') && !cl->kill && (get_module(cl)->num == R_CCCAM)) //CCCam-Client!
{
if(card_valid_for_client(cl, card))
{
if(cs_malloc(&clientmsg, sizeof(struct s_clientmsg)))
{
memcpy(clientmsg->msg, buf, sizeof(buf));
clientmsg->len = sizeof(buf);
clientmsg->cmd = MSG_CARD_REMOVED;
add_job(cl, ACTION_CLIENT_SEND_MSG, clientmsg, sizeof(struct s_clientmsg));
return 1;
}
}
}
return 0;
}
int32_t unhide_card_to_client(struct cc_card *card, struct s_client *cl)
{
return send_card_to_client(card, cl);
}
int32_t hidecards_card_valid_for_client(struct s_client *cl, struct cc_card *card)
{
return card_valid_for_client(cl, card);
}
int32_t send_card_to_all_clients(struct cc_card *card)
{
int32_t count = 0;
struct s_client *cl;
cs_readlock(__func__, &clientlist_lock);
for(cl = first_client; cl; cl = cl->next)
{
if(cl->cc && cl->typ == 'c' && !cl->kill && get_module(cl)->num == R_CCCAM) // CCCam-Client!
{
count += send_card_to_client(card, cl);
}
}
cs_readunlock(__func__, &clientlist_lock);
return count;
}
void send_remove_card_to_clients(struct cc_card *card)
{
if(!card || !card->id)
{ return; }
uint8_t buf[4];
buf[0] = card->id >> 24;
buf[1] = card->id >> 16;
buf[2] = card->id >> 8;
buf[3] = card->id & 0xFF;
struct s_client *cl;
struct s_clientmsg *clientmsg;
cs_readlock(__func__, &clientlist_lock);
for(cl = first_client; cl; cl = cl->next)
{
struct cc_data *cc = cl->cc;
if(cc && cl->typ == 'c' && !cl->kill && get_module(cl)->num == R_CCCAM) // CCCam-Client!
{
if(card_valid_for_client(cl, card))
{
if(cs_malloc(&clientmsg, sizeof(struct s_clientmsg)))
{
memcpy(clientmsg->msg, buf, sizeof(buf));
clientmsg->len = sizeof(buf);
clientmsg->cmd = MSG_CARD_REMOVED;
add_job(cl, ACTION_CLIENT_SEND_MSG, clientmsg, sizeof(struct s_clientmsg));
}
}
}
}
cs_readunlock(__func__, &clientlist_lock);
}
/**
* if idents defined on an cccam reader, the cards caid+provider are checked.
* return 1 a) if no ident defined b) card is in identlist
* 0 if card is not in identlist
*
* a card is in the identlist, if the cards caid is matching and mininum a provider is matching
**/
int32_t chk_ident(FTAB *ftab, struct cc_card *card)
{
int32_t j, k;
int32_t res = 1;
if(ftab && ftab->filts)
{
for(j = 0; j < ftab->nfilts; j++)
{
if(ftab->filts[j].caid)
{
res = 0;
if(ftab->filts[j].caid == card->caid) // caid matches!
{
int32_t nprids = ftab->filts[j].nprids;
if(!nprids) // No Provider ->Ok
{ return 1; }
LL_ITER it = ll_iter_create(card->providers);
struct cc_provider *prov;
while((prov = ll_iter_next(&it)))
{
for(k = 0; k < nprids; k++)
{
uint32_t prid = ftab->filts[j].prids[k];
if(prid == prov->prov) // Provider matches
{
return 1;
}
}
}
}
}
}
}
return res;
}
int32_t cc_clear_reported_carddata(LLIST *reported_carddatas, LLIST *except, int32_t send_removed)
{
int32_t i = 0;
LL_ITER it = ll_iter_create(reported_carddatas);
struct cc_card *card;
while((card = ll_iter_next(&it)))
{
struct cc_card *card2 = NULL;
if(except)
{
LL_ITER it2 = ll_iter_create(except);
while((card2 = ll_iter_next(&it2)))
{
if(card == card2)
{ break; }
}
}
if(!card2 && ll_iter_remove(&it)) // check result of ll_iter_remove, because another thread could removed it
{
if(send_removed)
{
cs_log_dbg(D_TRACE, "s-card removed: id %8X remoteid %8X caid %4X hop %d reshare %d originid %8X cardtype %d",
card->id, card->remote_id, card->caid, card->hop, card->reshare, card->origin_id, card->card_type);
send_remove_card_to_clients(card);
}
cc_free_card(card);
i++;
}
}
return i;
}
int32_t cc_free_reported_carddata(LLIST *reported_carddatas, LLIST *except, int32_t send_removed)
{
int32_t i = 0;
if(reported_carddatas)
{
i = cc_clear_reported_carddata(reported_carddatas, except, send_removed);
ll_destroy(&reported_carddatas);
}
return i;
}
int32_t card_valid_for_client(struct s_client *cl, struct cc_card *card)
{
// Check group:
if(card->grp && !(card->grp & cl->grp))
{ return 0; }
// Check idents:
if(!chk_ident(&cl->ftab, card))
{ return 0; }
// Check caids:
if(!chk_ctab(card->caid, &cl->ctab))
{ return 0; }
// Check reshare
if(card->card_type == CT_REMOTECARD)
{
int8_t ignorereshare = cl->account->cccignorereshare;
if(ignorereshare == -1) { ignorereshare = cfg.cc_ignore_reshare; }
if(!ignorereshare && !card->reshare)
{ return 0; }
}
// Check account maxhops:
if(cl->account->cccmaxhops < card->hop)
{ return 0; }
// Check remote node id, if card is from there, ignore it!
LL_ITER it = ll_iter_create(card->remote_nodes);
uint8_t *node;
struct cc_data *cc = cl->cc;
while((node = ll_iter_next(&it)))
{
if(!memcmp(node, cc->peer_node_id, 8))
{
return 0;
}
}
// Check Services:
if(ll_count(card->providers))
{
it = ll_iter_create(card->providers);
struct cc_provider *prov;
int8_t found = 0;
while((prov = ll_iter_next(&it)))
{
uint32_t prid = prov->prov;
if(chk_srvid_by_caid_prov(cl, card->caid, prid))
{
found = 1;
break;
}
}
if(!found) { return 0; }
}
else
{
if(!chk_srvid_by_caid_prov(cl, card->caid, 0))
{ return 0; }
}
// Check Card created by Service:
if(card->sidtab)
{
struct s_sidtab *ptr;
int32_t j;
int32_t ok = !cl->sidtabs.ok && !cl->sidtabs.no; // default valid if no positive services and no negative services
if(!ok)
{
if(!cl->sidtabs.ok) // no positive services, so ok by default if no negative found
{ ok = 1; }
for(j = 0, ptr = cfg.sidtab; ptr; ptr = ptr->next, j++)
{
if(ptr == card->sidtab)
{
if(cl->sidtabs.no & ((SIDTABBITS)1 << j))
{ return 0; }
if(cl->sidtabs.ok & ((SIDTABBITS)1 << j))
{ ok = 1; }
break;
}
}
}
if(!ok)
{ return 0; }
}
return 1;
}
uint32_t get_reader_prid(struct s_reader *rdr, int32_t j)
{
return b2i(3, &rdr->prid[j][1]);
}
//uint32_t get_reader_prid(struct s_reader *rdr, int32_t j) {
// uint32_t prid;
// if (!is_cascading_reader(rdr)) { // Real cardreaders have 4-byte Providers
// prid = b2i(4, &rdr->prid[j][0]);
// //prid = (rdr->prid[j][0] << 24) | (rdr->prid[j][1] << 16)
// // | (rdr->prid[j][2] << 8) | (rdr->prid[j][3] & 0xFF);
// } else { // Cascading/Network-reader 3-bytes Providers
// prid = b2i(3, &rdr->prid[j][0]);
// //prid = (rdr->prid[j][0] << 16) | (rdr->prid[j][1] << 8)
// // | (rdr->prid[j][2] & 0xFF);
//
// }
// return prid;
//}
void copy_good_sids(LLIST *dst, LLIST *src)
{
LL_ITER it_src = ll_iter_create(src);
LL_ITER it_dst = ll_iter_create(dst);
struct cc_srvid *srvid_src;
struct cc_srvid *srvid_dst;
while((srvid_src = ll_iter_next(&it_src)))
{
ll_iter_reset(&it_dst);
while((srvid_dst = ll_iter_next(&it_dst)))
{
if(sid_eq(srvid_src, srvid_dst))
{ break; }
}
if(!srvid_dst)
{
if(!cs_malloc(&srvid_dst, sizeof(struct cc_srvid)))
{ break; }
memcpy(srvid_dst, srvid_src, sizeof(struct cc_srvid));
ll_iter_insert(&it_dst, srvid_dst);
}
}
}
void copy_bad_sids(LLIST *dst, LLIST *src)
{
LL_ITER it_src = ll_iter_create(src);
LL_ITER it_dst = ll_iter_create(dst);
struct cc_srvid_block *srvid_src;
struct cc_srvid_block *srvid_dst;
while((srvid_src = ll_iter_next(&it_src)))
{
ll_iter_reset(&it_dst);
while((srvid_dst = ll_iter_next(&it_dst)))
{
if(sid_eq_bb(srvid_src, srvid_dst))
{ break; }
}
if(!srvid_dst)
{
if(!cs_malloc(&srvid_dst, sizeof(struct cc_srvid_block)))
{ break; }
memcpy(srvid_dst, srvid_src, sizeof(struct cc_srvid_block));
ll_iter_insert(&it_dst, srvid_dst);
}
}
}
int32_t add_card_providers(struct cc_card *dest_card, struct cc_card *card, int32_t copy_remote_nodes)
{
int32_t modified = 0;
// 1. Copy nonexisting providers, ignore double:
struct cc_provider *prov_info;
LL_ITER it_src = ll_iter_create(card->providers);
LL_ITER it_dst = ll_iter_create(dest_card->providers);
struct cc_provider *provider;
while((provider = ll_iter_next(&it_src)))
{
ll_iter_reset(&it_dst);
while((prov_info = ll_iter_next(&it_dst)))
{
if(prov_info->prov == provider->prov)
{ break; }
}
if(!prov_info)
{
struct cc_provider *prov_new;
if(!cs_malloc(&prov_new, sizeof(struct cc_provider)))
{ break; }
memcpy(prov_new, provider, sizeof(struct cc_provider));
ll_iter_insert(&it_dst, prov_new);
modified = 1;
}
}
if(copy_remote_nodes)
{
// 2. Copy nonexisting remote_nodes, ignoring existing:
it_src = ll_iter_create(card->remote_nodes);
it_dst = ll_iter_create(dest_card->remote_nodes);
uint8_t *remote_node;
uint8_t *remote_node2;
while((remote_node = ll_iter_next(&it_src)))
{
ll_iter_reset(&it_dst);
while((remote_node2 = ll_iter_next(&it_dst)))
{
if(memcmp(remote_node, remote_node2, 8) == 0)
{ break; }
}
if(!remote_node2)
{
uint8_t *remote_node_new;
if(!cs_malloc(&remote_node_new, 8))
{ break; }
memcpy(remote_node_new, remote_node, 8);
ll_iter_insert(&it_dst, remote_node_new);
modified = 1;
}
}
}
return modified;
}
#define TIMEOUT_SECONDS 3600
void set_card_timeout(struct cc_card *card)
{
card->timeout = time(NULL) + TIMEOUT_SECONDS + ((rand() & 0xff) - 128) * 2;
}
struct cc_card *create_card(struct cc_card *card)
{
struct cc_card *card2;
if(!cs_malloc(&card2, sizeof(struct cc_card)))
{ return NULL; }
if(card)
{ memcpy(card2, card, sizeof(struct cc_card)); }
else
{ memset(card2, 0, sizeof(struct cc_card)); }
card2->providers = ll_create("providers");
card2->badsids = ll_create("badsids");
card2->goodsids = ll_create("goodsids");
card2->remote_nodes = ll_create("remote_nodes");
if(card)
{
copy_good_sids(card2->goodsids, card->goodsids);
copy_bad_sids(card2->badsids, card->badsids);
card2->id = 0;
}
else
{ set_card_timeout(card2); }
return card2;
}
struct cc_card *create_card2(struct s_reader *rdr, int32_t j, uint16_t caid, uint8_t reshare)
{
struct cc_card *card = create_card(NULL);
if(!card)
{ return NULL; }
card->remote_id = (rdr ? (rdr->cc_id << 16) : 0x7F7F8000) | j;
card->caid = caid;
card->reshare = reshare;
card->origin_reader = rdr;
if(rdr)
{
card->grp = rdr->grp;
card->rdr_reshare = rdr->cc_reshare > -1 ? rdr->cc_reshare : cfg.cc_reshare; //copy reshare because reader could go offline
card->sidtabno = rdr->sidtabs.no;
card->hop = rdr->cc_hop;
}
else { card->rdr_reshare = reshare; }
return card;
}
/**
* num_same_providers checks if card1 has exactly the same providers as card2
* returns same provider count
**/
int32_t num_same_providers(struct cc_card *card1, struct cc_card *card2)
{
int32_t found = 0;
LL_ITER it1 = ll_iter_create(card1->providers);
LL_ITER it2 = ll_iter_create(card2->providers);
struct cc_provider *prov1, *prov2;
while((prov1 = ll_iter_next(&it1)))
{
ll_iter_reset(&it2);
while((prov2 = ll_iter_next(&it2)))
{
if(prov1->prov == prov2->prov)
{
found++;
break;
}
}
}
return found;
}
/**
* equal_providers checks if card1 has exactly the same providers as card2
* returns 1=equal 0=different
**/
int32_t equal_providers(struct cc_card *card1, struct cc_card *card2)
{
if(ll_count(card1->providers) != ll_count(card2->providers))
{ return 0; }
if(ll_count(card1->providers) == 0)
{ return 1; }
LL_ITER it1 = ll_iter_create(card1->providers);
LL_ITER it2 = ll_iter_create(card2->providers);
struct cc_provider *prov1, *prov2;
while((prov1 = ll_iter_next(&it1)))
{
ll_iter_reset(&it2);
while((prov2 = ll_iter_next(&it2)))
{
if(prov1->prov == prov2->prov)
{
break;
}
}
if(!prov2) { break; }
}
return (prov1 == NULL);
}
int32_t is_au_card(struct cc_card *card)
{
if(card && card->origin_reader)
{ return !card->origin_reader->audisabled && cc_UA_valid(card->hexserial); }
return 0;
}
void merge_sids(struct cc_card *carddst, struct cc_card *cardsrc)
{
LL_ITER it;
struct cc_srvid *srvid;
struct cc_srvid_block *srvidb;
int32_t goodSidCountSrc = ll_count(cardsrc->goodsids);
int32_t goodSidCountDst = ll_count(carddst->goodsids);
if(goodSidCountDst == 0)
{
// remove sid blocks good+notbad from src
it = ll_iter_create(cardsrc->goodsids);
while((srvid = ll_iter_next(&it)))
{
if(!is_sid_blocked(cardsrc, srvid))
{ remove_sid_block(carddst, srvid); }
}
}
else
{
if(goodSidCountSrc == 0)
{
// del goods from dst
ll_clear(carddst->goodsids);
// del bads from dst
ll_clear(carddst->badsids);
// add bads from src
it = ll_iter_create(cardsrc->badsids);
while((srvidb = ll_iter_next(&it)))
{
{ add_sid_block(carddst, (struct cc_srvid*)srvidb, false); }
}
}
else
{
// add good sid good+notbad from src
it = ll_iter_create(cardsrc->goodsids);
while((srvid = ll_iter_next(&it)))
{
if(!is_sid_blocked(cardsrc, srvid))
{ add_good_sid(carddst, srvid); }
}
}
}
}
/**
* Adds a new card to a cardlist.
*/
int32_t add_card_to_serverlist(LLIST *cardlist, struct cc_card *card, int8_t free_card)
{
int32_t modified = 0;
if(!card)
{ return modified; }
LL_ITER it = ll_iter_create(cardlist);
struct cc_card *card2;
// Minimize all, transmit just CAID, merge providers:
if(cfg.cc_minimize_cards == MINIMIZE_CAID && !cfg.cc_forward_origin_card)
{
while((card2 = ll_iter_next(&it)))
{
// compare caid, hexserial, cardtype and sidtab (if any):
if(same_card2(card, card2, 0))
{
// Merge cards only if resulting providercount is smaller than CS_MAXPROV
int32_t nsame, ndiff, nnew;
nsame = num_same_providers(card, card2); // count same cards
ndiff = ll_count(card->providers) - nsame; // cound different cards, this cound will be added
nnew = ndiff + ll_count(card2->providers); // new card count after add. because its limited to CS_MAXPROV, dont add it
if(nnew <= CS_MAXPROV)
{ break; }
}
}
if(!card2) // Not found->add it:
{
if(free_card) // Use this card
{
free_card = 0;
ll_iter_insert(&it, card);
}
else
{
card2 = create_card(card); // Copy card
if(!card2)
{ return modified; }
card2->hop = 0;
ll_iter_insert(&it, card2);
add_card_providers(card2, card, 1); // copy providers to new card. Copy remote nodes to new card
}
modified = 1;
}
else // found, merge providers:
{
card_dup_count++;
card2->grp |= card->grp; // add group to the card
add_card_providers(card2, card, 0); // merge all providers
ll_clear_data(card2->remote_nodes); // clear remote nodes
merge_sids(card2, card);
}
}
// Removed duplicate cards, keeping card with lower hop:
else if(cfg.cc_minimize_cards == MINIMIZE_HOPS && !cfg.cc_forward_origin_card)
{
while((card2 = ll_iter_next(&it)))
{
// compare caid, hexserial, cardtype, sidtab (if any), providers:
if(same_card2(card, card2, 0) && equal_providers(card, card2))
{