-
Notifications
You must be signed in to change notification settings - Fork 1
/
oscam-ecm.c
2695 lines (2321 loc) · 74.5 KB
/
oscam-ecm.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
#include "globals.h"
#include "cscrypt/md5.h"
#include "module-anticasc.h"
#include "module-cacheex.h"
#include "module-led.h"
#include "module-stat.h"
#include "module-webif.h"
#include "module-ird-guess.h"
#include "module-cw-cycle-check.h"
#include "module-gbox.h"
#include "oscam-cache.h"
#include "oscam-chk.h"
#include "oscam-client.h"
#include "oscam-config.h"
#include "oscam-ecm.h"
#include "oscam-garbage.h"
#include "oscam-failban.h"
#include "oscam-net.h"
#include "oscam-time.h"
#include "oscam-lock.h"
#include "oscam-string.h"
#include "oscam-work.h"
#include "reader-common.h"
extern CS_MUTEX_LOCK ecmcache_lock;
extern struct ecm_request_t *ecmcwcache;
extern uint16_t len4caid[256];
extern uint32_t ecmcwcache_size;
extern int32_t exit_oscam;
extern CS_MUTEX_LOCK ecm_pushed_deleted_lock;
extern struct ecm_request_t *ecm_pushed_deleted;
static pthread_mutex_t cw_process_sleep_cond_mutex;
static pthread_cond_t cw_process_sleep_cond;
static int cw_process_wakeups;
static uint32_t auto_timeout(ECM_REQUEST *er, uint32_t timeout)
{
(void)er; // Prevent warning about unused er, when WITH_LB is disabled
#ifdef WITH_LB
if(cfg.lb_auto_timeout)
{ return lb_auto_timeout(er, timeout); }
#endif
return timeout;
}
#ifdef CS_CACHEEX
void cacheex_mode1_delay(ECM_REQUEST *er){
if(!er->cacheex_wait_time_expired
&& er->cacheex_mode1_delay
&& er->cacheex_reader_count > 0
&& !er->stage
&& er->rc >= E_UNHANDLED
)
{
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} cacheex_mode1_delay timeout! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
request_cw_from_readers(er, 1); // setting stop_stage=1, we request only cacheex mode 1 readers. Others are requested at cacheex timeout!
}
}
void cacheex_timeout(ECM_REQUEST *er)
{
if(!er->cacheex_wait_time_expired){
er->cacheex_wait_time_expired = 1;
if(er->rc >= E_UNHANDLED)
{
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} cacheex timeout! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
//if check_cw mode=0, first try to get cw from cache without check counter!
CWCHECK check_cw = get_cwcheck(er);
if(!check_cw.mode)
{
struct ecm_request_t *ecm=NULL;
ecm = check_cache(er, er->client);
if(ecm) //found in cache
{
struct s_write_from_cache *wfc=NULL;
if(!cs_malloc(&wfc, sizeof(struct s_write_from_cache)))
{
NULLFREE(ecm);
return;
}
wfc->er_new=er;
wfc->er_cache=ecm;
if(!add_job(er->client, ACTION_ECM_ANSWER_CACHE, wfc, sizeof(struct s_write_from_cache))) //write_ecm_answer_fromcache
{ NULLFREE(ecm); }
return;
}
}
//check if "normal" readers selected, if not send NOT FOUND!
//cacheex1-client (having always no "normal" reader), or not-cacheex-1 client with no normal readers available (or filtered by LB)
if( (er->reader_count + er->fallback_reader_count - er->cacheex_reader_count) <= 0 )
{
if(!cfg.wait_until_ctimeout){
er->rc = E_NOTFOUND;
er->selected_reader = NULL;
er->rcEx = 0;
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} cacheex timeout: NO \"normal\" readers... not_found! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
send_dcw(er->client, er);
return;
}
}
else
{
if(er->stage < 2){
debug_ecm(D_TRACE, "request for %s %s", username(er->client), buf);
request_cw_from_readers(er, 0);
}
}
}
}
}
#endif
void fallback_timeout(ECM_REQUEST *er)
{
if(er->rc >= E_UNHANDLED && er->stage < 4)
{
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} fallback timeout! (stage: %d)", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, er->stage);
debug_ecm(D_TRACE, "fallback for %s %s", username(er->client), buf);
while(er->stage < 4) //if preferlocalcards=1 and no answer from locals, initial stage will be 2! We need to reach stage=4 to call fallback's.
{
request_cw_from_readers(er, 0);
}
}
}
void ecm_timeout(ECM_REQUEST *er)
{
if(!er->readers_timeout_check)
{
er->readers_timeout_check = 1;
if(check_client(er->client) && er->rc >= E_UNHANDLED)
{
debug_ecm(D_TRACE, "timeout for %s %s", username(er->client), buf);
//set timeout for readers not answering
struct s_ecm_answer *ea_list;
for(ea_list = er->matching_rdr; ea_list; ea_list = ea_list->next)
{
if((ea_list->status & (REQUEST_SENT | REQUEST_ANSWERED)) == REQUEST_SENT) //Request sent, but no answer!
{
write_ecm_answer(ea_list->reader, er, E_TIMEOUT, 0, NULL, NULL); //set timeout for readers not answered!
}
}
//send timeout to client!
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} client timeout! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
er->rc = E_TIMEOUT;
er->rcEx = 0;
send_dcw(er->client, er);
}
}
}
void increment_n_request(struct s_client *cl){
if(check_client(cl)){
cl->n_request[1]++;
first_client->n_request[1]++;
}
}
void update_n_request(void){
struct s_client *cl;
cs_readlock(&clientlist_lock);
for(cl = first_client->next; cl; cl = cl->next)
{
#ifdef CS_CACHEEX
if(check_client(cl) && get_module(cl)->num != R_CSP && cl->typ == 'c' && !cl->dup && cl->account && cl->account->cacheex.mode<=1) //no cacheex 2/3 client
#else
if(check_client(cl) && get_module(cl)->num != R_CSP && cl->typ == 'c' && !cl->dup)
#endif
{
cl->n_request[0]=cl->n_request[1];
cl->n_request[1]=0;
}else{
cl->n_request[0]=0;
cl->n_request[1]=0;
}
}
first_client->n_request[0]=first_client->n_request[1];
first_client->n_request[1]=0;
cs_readunlock(&clientlist_lock);
}
static void *cw_process(void)
{
set_thread_name(__func__);
int64_t time_to_check_fbtimeout, time_to_check_ctimeout, next_check, ecmc_next, cache_next, n_request_next, msec_wait = 3000;
struct timeb t_now, tbc, ecmc_time, cache_time, n_request_time;
ECM_REQUEST *er = NULL;
time_t ecm_maxcachetime;
#ifdef CS_CACHEEX
int64_t time_to_check_cacheex_wait_time;
int64_t time_to_check_cacheex_mode1_delay;
#endif
cs_pthread_cond_init(&cw_process_sleep_cond_mutex, &cw_process_sleep_cond);
#ifdef CS_ANTICASC
int32_t ac_next;
struct timeb ac_time;
cs_ftime(&ac_time);
add_ms_to_timeb(&ac_time, cfg.ac_stime * 60 * 1000);
#endif
cs_ftime(&ecmc_time);
add_ms_to_timeb(&ecmc_time, 1000);
cs_ftime(&cache_time);
add_ms_to_timeb(&cache_time, 3000);
cs_ftime(&n_request_time);
add_ms_to_timeb(&n_request_time, 60 * 1000);
while(!exit_oscam)
{
if(cw_process_wakeups == 0) // No waiting wakeups, proceed to sleep
{
sleepms_on_cond(&cw_process_sleep_cond_mutex, &cw_process_sleep_cond, msec_wait);
}
cw_process_wakeups = 0; // We've been woken up, reset the counter
if(exit_oscam)
{ break; }
next_check = 0;
#ifdef CS_ANTICASC
ac_next = 0;
#endif
ecmc_next = 0;
cache_next = 0;
msec_wait = 0;
cs_ftime(&t_now);
cs_readlock(&ecmcache_lock);
for(er = ecmcwcache; er; er = er->next)
{
if(
(er->from_cacheex || er->from_csp) //ignore ecms from cacheex/csp
||
er->readers_timeout_check //ignore already checked
||
!check_client(er->client) //ignore ecm of killed clients
)
{ continue; }
if(er->rc >= E_UNHANDLED)
{
#ifdef CS_CACHEEX
//cacheex_wait_time
if(er->cacheex_wait_time && !er->cacheex_wait_time_expired)
{
tbc = er->tps;
time_to_check_cacheex_mode1_delay = 0;
time_to_check_cacheex_wait_time = add_ms_to_timeb_diff(&tbc, auto_timeout(er, er->cacheex_wait_time));
if(comp_timeb(&t_now, &tbc) >= 0)
{
add_job(er->client, ACTION_CACHEEX_TIMEOUT, (void *)er, 0);
time_to_check_cacheex_wait_time = 0;
}else if(er->cacheex_mode1_delay && !er->stage && er->cacheex_reader_count>0){
//check for cacheex_mode1_delay
tbc = er->tps;
time_to_check_cacheex_mode1_delay = add_ms_to_timeb_diff(&tbc, auto_timeout(er, er->cacheex_mode1_delay));
if(comp_timeb(&t_now, &tbc) >= 0)
{
add_job(er->client, ACTION_CACHEEX1_DELAY, (void *)er, 0);
time_to_check_cacheex_mode1_delay = 0;
}
}
if(!next_check || (time_to_check_cacheex_wait_time > 0 && time_to_check_cacheex_wait_time < next_check))
{ next_check = time_to_check_cacheex_wait_time; }
if(!next_check || (time_to_check_cacheex_mode1_delay > 0 && time_to_check_cacheex_mode1_delay < next_check))
{ next_check = time_to_check_cacheex_mode1_delay; }
}
#endif
if(er->stage < 4)
{
//fbtimeout
tbc = er->tps;
time_to_check_fbtimeout = add_ms_to_timeb_diff(&tbc, auto_timeout(er, get_fallbacktimeout(er->caid)));
if(comp_timeb(&t_now, &tbc) >= 0)
{
add_job(er->client, ACTION_FALLBACK_TIMEOUT, (void *)er, 0);
time_to_check_fbtimeout = 0;
}
if(!next_check || (time_to_check_fbtimeout > 0 && time_to_check_fbtimeout < next_check))
{ next_check = time_to_check_fbtimeout; }
}
}
//clienttimeout
if(!er->readers_timeout_check) //ecm stays in cache at least ctimeout+2seconds!
{
tbc = er->tps;
time_to_check_ctimeout = add_ms_to_timeb_diff(&tbc, auto_timeout(er, cfg.ctimeout));
if(comp_timeb(&t_now, &tbc) >= 0)
{
add_job(er->client, ACTION_CLIENT_TIMEOUT, (void *)er, 0);
time_to_check_ctimeout = 0;
}
if(!next_check || (time_to_check_ctimeout > 0 && time_to_check_ctimeout < next_check))
{ next_check = time_to_check_ctimeout; }
}
}
cs_readunlock(&ecmcache_lock);
#ifdef CS_ANTICASC
if(cfg.ac_enabled && (ac_next = comp_timeb(&ac_time, &t_now)) <= 10)
{
ac_do_stat();
cs_ftime(&ac_time);
ac_next = add_ms_to_timeb_diff(&ac_time, cfg.ac_stime * 60 * 1000);
}
#endif
if((ecmc_next = comp_timeb(&ecmc_time, &t_now)) <= 10)
{
uint32_t count = 0;
struct ecm_request_t *ecm, *ecmt = NULL, *prv;
cs_readlock(&ecmcache_lock);
for(ecm = ecmcwcache, prv = NULL; ecm; prv = ecm, ecm = ecm->next, count++)
{
ecm_maxcachetime = t_now.time - ((cfg.ctimeout+500)/1000+3); //to be sure no more access er!
if(ecm->tps.time < ecm_maxcachetime)
{
cs_readunlock(&ecmcache_lock);
cs_writelock(&ecmcache_lock);
ecmt = ecm;
if(prv)
{ prv->next = NULL; }
else
{ ecmcwcache = NULL; }
cs_writeunlock(&ecmcache_lock);
break;
}
}
if(!ecmt)
{ cs_readunlock(&ecmcache_lock); }
ecmcwcache_size = count;
while(ecmt)
{
ecm = ecmt->next;
free_ecm(ecmt);
ecmt = ecm;
}
#ifdef CS_CACHEEX
ecmt=NULL;
cs_readlock(&ecm_pushed_deleted_lock);
for(ecm = ecm_pushed_deleted, prv = NULL; ecm; prv = ecm, ecm = ecm->next)
{
ecm_maxcachetime = t_now.time - ((cfg.ctimeout+500)/1000+3);
if(ecm->tps.time < ecm_maxcachetime)
{
cs_readunlock(&ecm_pushed_deleted_lock);
cs_writelock(&ecm_pushed_deleted_lock);
ecmt = ecm;
if(prv)
{ prv->next = NULL; }
else
{ ecm_pushed_deleted = NULL; }
cs_writeunlock(&ecm_pushed_deleted_lock);
break;
}
}
if(!ecmt)
{ cs_readunlock(&ecm_pushed_deleted_lock); }
while(ecmt)
{
ecm = ecmt->next;
free_push_in_ecm(ecmt);
ecmt = ecm;
}
#endif
cs_ftime(&ecmc_time);
ecmc_next = add_ms_to_timeb_diff(&ecmc_time, 1000);
}
if((cache_next = comp_timeb(&cache_time, &t_now)) <= 10)
{
cleanup_cache();
#ifdef CS_CACHEEX
cleanup_hitcache();
#endif
cs_ftime(&cache_time);
cache_next = add_ms_to_timeb_diff(&cache_time, 3000);
}
if((n_request_next = comp_timeb(&n_request_time, &t_now)) <= 10)
{
update_n_request();
cs_ftime(&n_request_time);
n_request_next = add_ms_to_timeb_diff(&n_request_time, 60 * 1000);
}
msec_wait = next_check;
#ifdef CS_ANTICASC
if(!msec_wait || (ac_next > 0 && ac_next < msec_wait))
{ msec_wait = ac_next; }
#endif
if(!msec_wait || (ecmc_next > 0 && ecmc_next < msec_wait))
{ msec_wait = ecmc_next; }
if(!msec_wait || (cache_next > 0 && cache_next < msec_wait))
{ msec_wait = cache_next; }
if(!msec_wait || (n_request_next > 0 && n_request_next < msec_wait))
{ msec_wait = n_request_next; }
if(!msec_wait)
{ msec_wait = 3000; }
cleanupcwcycle();
}
return NULL;
}
void cw_process_thread_start(void)
{
start_thread((void *) &cw_process, "cw_process");
}
void cw_process_thread_wakeup(void)
{
cw_process_wakeups++; // Do not sleep...
pthread_cond_signal(&cw_process_sleep_cond);
}
#ifdef WITH_LB
/**
* search for same ecm hash with same readers
**/
struct ecm_request_t *check_same_ecm(ECM_REQUEST *er)
{
struct ecm_request_t *ecm;
time_t timeout;
struct s_ecm_answer *ea_ecm = NULL, *ea_er = NULL;
uint8_t rdrs = 0;
cs_readlock(&ecmcache_lock);
for(ecm = ecmcwcache; ecm; ecm = ecm->next)
{
timeout = time(NULL) - ((cfg.ctimeout + 500) / 1000);
if(ecm->tps.time <= timeout)
{ break; }
if(ecm == er) { continue; }
if(er->caid != ecm->caid || memcmp(ecm->ecmd5, er->ecmd5, CS_ECMSTORESIZE))
{ continue; }
if(!er->readers || !ecm->readers || er->readers != ecm->readers)
{ continue; }
ea_ecm = ecm->matching_rdr;
ea_er = er->matching_rdr;
rdrs = er->readers;
while(rdrs && ea_ecm && ea_er)
{
if(ea_ecm->reader != ea_er->reader)
{ break; }
ea_ecm = ea_ecm->next;
ea_er = ea_er->next;
rdrs--;
}
if(!rdrs)
{
cs_readunlock(&ecmcache_lock);
return ecm;
}
}
cs_readunlock(&ecmcache_lock);
return NULL; // nothing found so return null
}
void use_same_readers(ECM_REQUEST *er_new, ECM_REQUEST *er_cache)
{
struct s_ecm_answer *ea_new = er_new->matching_rdr;
struct s_ecm_answer *ea_cache = er_cache->matching_rdr;
uint8_t rdrs = er_new->readers;
while(rdrs)
{
ea_new->status &= ~(READER_ACTIVE | READER_FALLBACK);
if((ea_cache->status & READER_ACTIVE))
{
if(!(ea_cache->status & READER_FALLBACK))
{
ea_new->status |= READER_ACTIVE;
}
else
{
ea_new->status |= (READER_ACTIVE | READER_FALLBACK);
}
}
ea_new = ea_new->next;
ea_cache = ea_cache->next;
rdrs--;
}
}
#endif
void convert_to_beta(struct s_client *cl, ECM_REQUEST *er, uint16_t caidto)
{
static uint8_t headerN3[10] = {0xc7, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x87, 0x12};
static uint8_t headerN2[10] = {0xc9, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x48, 0x12};
er->ocaid = er->caid;
er->caid = caidto;
er->prid = 0;
er->ecmlen = er->ecm[2] + 3;
memmove(er->ecm + 13, er->ecm + 3, er->ecmlen - 3);
if(er->ecmlen > 0x88)
{
memcpy(er->ecm + 3, headerN3, 10);
if(er->ecm[0] == 0x81)
{ er->ecm[12] += 1; }
er->ecm[1] = 0x70;
}
else
{
memcpy(er->ecm + 3, headerN2, 10);
}
er->ecmlen += 10;
er->ecm[2] = er->ecmlen - 3;
er->btun = 1;
cl->cwtun++;
cl->account->cwtun++;
first_client->cwtun++;
cs_debug_mask(D_TRACE, "ECM converted ocaid from 0x%04X to BetaCrypt caid 0x%04X for service id 0x%04X",
er->ocaid, caidto, er->srvid);
}
void convert_to_nagra(struct s_client *cl, ECM_REQUEST *er, uint16_t caidto)
{
cs_debug_mask(D_TRACE, "convert_to_nagra");
er->ocaid = er->caid;
er->caid = caidto;
er->prid = 0;
er->ecmlen = er->ecm[2] + 3;
//not sure
if(er->ecmlen < 0x52)
{ er->ecm[1] = 0x30; }
memmove(er->ecm + 3, er->ecm + 13, er->ecmlen - 3);
er->ecmlen -= 10;
er->ecm[2] = er->ecmlen - 3;
er->btun = 1;
cl->cwtun++;
cl->account->cwtun++;
first_client->cwtun++;
cs_debug_mask(D_TRACE, "ECM converted ocaid from: 0x%04X to Nagra: 0x04%X for service id:0x04%X",
er->ocaid, caidto, er->srvid);
}
void cs_betatunnel(ECM_REQUEST *er)
{
int32_t n;
struct s_client *cl = cur_client();
uint32_t mask_all = 0xFFFF;
TUNTAB *ttab;
ttab = &cl->ttab;
for(n = 0; n < ttab->n; n++)
{
if((er->caid == ttab->bt_caidfrom[n]) && ((er->srvid == ttab->bt_srvid[n]) || (ttab->bt_srvid[n]) == mask_all))
{
if(chk_is_betatunnel_caid(er->caid) == 1 && er->ocaid == 0x0000)
{
convert_to_nagra(cl, er, ttab->bt_caidto[n]);
}
else if(er->ocaid == 0x0000)
{
convert_to_beta(cl, er, ttab->bt_caidto[n]);
}
return;
}
}
}
static void remove_ecm_from_reader(ECM_REQUEST *ecm)
{
int32_t i;
struct s_ecm_answer *ea = ecm->matching_rdr;
while(ea)
{
if((ea->status & REQUEST_SENT) && !(ea->status & REQUEST_ANSWERED))
{
//we found a outstanding reader, clean it:
struct s_reader *rdr = ea->reader;
if(rdr)
{
struct s_client *cl = rdr->client;
if(check_client(cl))
{
ECM_REQUEST *ecmtask = cl->ecmtask;
if(ecmtask)
{
for(i = 0; i < cfg.max_pending; ++i)
{
if(ecmtask[i].parent == ecm)
{
ecmtask[i].client = NULL;
cacheex_set_csp_lastnode(&ecmtask[i]);
}
}
}
}
}
}
ea = ea->next;
}
}
void free_ecm(ECM_REQUEST *ecm)
{
struct s_ecm_answer *ea, *nxt;
cacheex_free_csp_lastnodes(ecm);
gbox_free_cards_pending(ecm);
//remove this ecm from reader queue to avoid segfault on very late answers (when ecm is already disposed)
//first check for outstanding answers:
remove_ecm_from_reader(ecm);
//free matching_rdr list:
ea = ecm->matching_rdr;
ecm->matching_rdr = NULL;
while(ea)
{
nxt = ea->next;
cs_lock_destroy(&ea->ecmanswer_lock);
add_garbage(ea);
ea = nxt;
}
if(ecm->src_data)
{ add_garbage(ecm->src_data); }
add_garbage(ecm);
}
void free_push_in_ecm(ECM_REQUEST *ecm)
{
cacheex_free_csp_lastnodes(ecm);
gbox_free_cards_pending(ecm);
if(ecm->src_data)
{ NULLFREE(ecm->src_data); }
NULLFREE(ecm);
}
ECM_REQUEST *get_ecmtask(void)
{
ECM_REQUEST *er = NULL;
struct s_client *cl = cur_client();
if(!cl)
{ return NULL; }
if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
{ return NULL; }
cs_ftime(&er->tps);
#ifdef MODULE_GBOX
er->gbox_ecm_id = 0;
er->gbox_ecm_status = GBOX_ECM_NOT_ASKED;
#endif
er->rc = E_UNHANDLED;
er->client = cl;
er->grp = 0; //no readers/cacheex-clients answers yet
//cs_log("client %s ECMTASK %d module %s", username(cl), n, get_module(cl)->desc);
return er;
}
void cleanup_ecmtasks(struct s_client *cl)
{
if(cl && !cl->account->usr) { return; } //not for anonymous users!
ECM_REQUEST *ecm;
//remove this clients ecm from queue. because of cache, just null the client:
cs_readlock(&ecmcache_lock);
for(ecm = ecmcwcache; ecm; ecm = ecm->next)
{
if(ecm->client == cl)
{
ecm->client = NULL;
}
}
cs_readunlock(&ecmcache_lock);
//remove client from rdr ecm-queue:
cs_readlock(&readerlist_lock);
struct s_reader *rdr = first_active_reader;
while(rdr)
{
if(check_client(rdr->client) && rdr->client->ecmtask)
{
int i;
for(i = 0; i < cfg.max_pending; i++)
{
ecm = &rdr->client->ecmtask[i];
if(ecm->client == cl)
{
ecm->client = NULL;
}
}
}
rdr = rdr->next;
}
cs_readunlock(&readerlist_lock);
}
static void add_cascade_data(struct s_client *client, ECM_REQUEST *er)
{
if(!client->cascadeusers)
{ client->cascadeusers = ll_create("cascade_data"); }
LLIST *l = client->cascadeusers;
LL_ITER it = ll_iter_create(l);
time_t now = time(NULL);
struct s_cascadeuser *cu;
int8_t found = 0;
while((cu = ll_iter_next(&it)))
{
if(er->caid == cu->caid && er->prid == cu->prid && er->srvid == cu->srvid) //found it
{
if(cu->time < now)
{ cu->cwrate = now - cu->time; }
cu->time = now;
found = 1;
}
else if(cu->time + 60 < now) // old
{ ll_iter_remove_data(&it); }
}
if(!found) //add it if not found
{
if(!cs_malloc(&cu, sizeof(struct s_cascadeuser)))
{ return; }
cu->caid = er->caid;
cu->prid = er->prid;
cu->srvid = er->srvid;
cu->time = now;
ll_append(l, cu);
}
}
static int32_t is_double_check_caid(ECM_REQUEST *er)
{
if(!cfg.double_check_caid.caid[0]) //no caids defined: Check all
{ return 1; }
int32_t i;
for(i = 0; i < CS_MAXCAIDTAB; i++)
{
uint16_t tcaid = cfg.double_check_caid.caid[i];
if(!tcaid)
{ break; }
if(tcaid == er->caid || (tcaid < 0x0100 && (er->caid >> 8) == tcaid))
{
return 1;
}
}
return 0;
}
struct s_ecm_answer *get_ecm_answer(struct s_reader *reader, ECM_REQUEST *er)
{
if(!er || !reader) { return NULL; }
struct s_ecm_answer *ea;
for(ea = er->matching_rdr; ea; ea = ea->next)
{
if(ea->reader == reader)
{
return ea;
}
}
return NULL;
}
void distribute_ea(struct s_ecm_answer *ea)
{
struct s_ecm_answer *ea_temp;
for(ea_temp = ea->pending; ea_temp; ea_temp = ea_temp->pending_next)
{
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [distribute_ea] send ea (%s) by reader %s answering for client %s", (check_client(ea_temp->er->client) ? ea_temp->er->client->account->usr : "-"), ea_temp->er->caid, ea_temp->er->prid, ea_temp->er->srvid, ea->rc==E_FOUND?"OK":"NOK", ea_temp->reader->label, (check_client(ea->er->client) ? ea->er->client->account->usr : "-"));
//e.g. we cannot send timeout, because "ea_temp->er->client" could wait/ask other readers! Simply set not_found if different from E_FOUND!
write_ecm_answer(ea_temp->reader, ea_temp->er, (ea->rc==E_FOUND? E_FOUND : E_NOTFOUND), ea->rcEx, ea->cw, NULL);
}
}
int32_t send_dcw(struct s_client *client, ECM_REQUEST *er)
{
if(!check_client(client) || client->typ != 'c')
{ return 0; }
cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [send_dcw] rc %d from reader %s", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, er->rc, er->selected_reader ? er->selected_reader->label : "-");
static const char stageTxt[] = {'0', 'C', 'L', 'P', 'F', 'X'};
static const char *stxt[] = {"found", "cache1", "cache2", "cache3",
"not found", "timeout", "sleeping",
"fake", "invalid", "corrupt", "no card", "expdate", "disabled", "stopped"
};
static const char *stxtEx[16] = {"", "group", "caid", "ident", "class", "chid", "queue", "peer", "sid", "", "", "", "", "", "", ""};
static const char *stxtWh[16] = {"", "user ", "reader ", "server ", "lserver ", "", "", "", "", "", "", "", "" , "" , "", ""};
char sby[100] = "", sreason[32] = "", scwcinfo[32] = "", schaninfo[32] = "", srealecmtime[50]="";
char erEx[32] = "";
char usrname[38] = "";
char channame[32];
struct timeb tpe;
snprintf(usrname, sizeof(usrname) - 1, "%s", username(client));
#ifdef WITH_DEBUG
if(cs_dblevel & D_CLIENTECM)
{
char buf[ECM_FMT_LEN];
char ecmd5[17 * 3];
char cwstr[17 * 3];
format_ecm(er, buf, ECM_FMT_LEN);
cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
cs_hexdump(0, er->cw, 16, cwstr, sizeof(cwstr));
#ifdef CS_CACHEEX
char csphash[5 * 3];
cs_hexdump(0, (void *)&er->csp_hash, 4, csphash, sizeof(csphash));
cs_debug_mask(D_CLIENTECM, "Client %s csphash %s cw %s rc %d %s", username(client), csphash, cwstr, er->rc, buf);
#else
cs_debug_mask(D_CLIENTECM, "Client %s cw %s rc %d %s", username(client), cwstr, er->rc, buf);
#endif
}
#endif
struct s_reader *er_reader = er->selected_reader; //responding reader
struct s_ecm_answer *ea_orig = get_ecm_answer(er_reader, er);
//check if ecm_answer from pending's
if(ea_orig && ea_orig->is_pending && er->rc == E_FOUND)
{ er->rc = E_CACHE2; }
//check if answer from cacheex-1 reader
if(er->rc == E_FOUND && er_reader && cacheex_reader(er_reader)) //so add hit to cacheex mode 1 readers
{
er->rc = E_CACHEEX;
}
//real ecm time
if(ea_orig && !ea_orig->is_pending && er->rc == E_FOUND
&&
(
#ifdef CS_CACHEEX
er->cacheex_wait_time ||
#endif
(ea_orig->status & READER_FALLBACK))
)
{
snprintf(srealecmtime, sizeof(srealecmtime) - 1, " (real %d ms)", ea_orig->ecm_time);
}
if(er->rc == E_TIMEOUT)
{
#ifdef CS_CACHEEX
if(!er->from_cacheex1_client){ //cosmetic: show "by" readers only for "normal" clients
#endif
struct s_ecm_answer *ea_list;
int32_t ofs = 0;
for(ea_list = er->matching_rdr; ea_list; ea_list = ea_list->next)
{
if(ea_list->reader && ofs < (int32_t)sizeof(sby) && ((ea_list->status & REQUEST_SENT) && (ea_list->rc == E_TIMEOUT || ea_list->rc >= E_99))) //Request send, but no cw answered!
{
ofs += snprintf(sby + ofs, sizeof(sby) - ofs - 1, "%s%s", ofs ? "," : " by ", ea_list->reader->label);
}
}
if(er->ocaid && ofs < (int32_t)sizeof(sby))
{ ofs += snprintf(sby + ofs, sizeof(sby) - ofs - 1, "(btun %04X)", er->ocaid); }
#ifdef CS_CACHEEX
}
#endif
}
else if(er_reader)
{
// add marker to reader if ECM_REQUEST was betatunneled
if(er->ocaid)
{ snprintf(sby, sizeof(sby) - 1, " by %s(btun %04X)", er_reader->label, er->ocaid); }
else
{ snprintf(sby, sizeof(sby) - 1, " by %s", er_reader->label); }
}
#ifdef CS_CACHEEX
else if(er->cacheex_src) //only for cacheex mode-3 clients (no mode-1 or mode-2 because reader is set!) and csp
{
char *cex_name = "-";
if(check_client(er->cacheex_src) && er->cacheex_src->account){
if(er->cacheex_src->account->usr[0] != '\0')
cex_name = er->cacheex_src->account->usr;
else
cex_name = "csp";
}
if(er->ocaid){
snprintf(sby, sizeof(sby) - 1, " by %s(btun %04X)", cex_name, er->ocaid);
}else{
snprintf(sby, sizeof(sby) - 1, " by %s", cex_name);
}
}
#endif
if(er->rc < E_NOTFOUND)
{
er->rcEx = 0;
memset(er->msglog, 0, MSGLOGSIZE); // remove reader msglog from previous requests that failed, founds never give back msglog!
}
if(er->rcEx)
{ snprintf(erEx, sizeof(erEx) - 1, "rejected %s%s", stxtWh[er->rcEx >> 4], stxtEx[er->rcEx & 0xf]); }
get_servicename_or_null(client, er->srvid, er->caid, channame);
if(!channame[0])
{ schaninfo[0] = '\0'; }
else
{ snprintf(schaninfo, sizeof(schaninfo) - 1, " - %s", channame); }
if(er->msglog[0])
{ snprintf(sreason, sizeof(sreason) - 1, " (%s)", er->msglog); }
#ifdef CW_CYCLE_CHECK
if(er->cwc_msg_log[0])
{ snprintf(scwcinfo, sizeof(scwcinfo) - 1, " (%s)", er->cwc_msg_log); }
#endif
cs_ftime(&tpe);
#ifdef CS_CACHEEX
int cx = 0;
if(er->rc >= E_CACHEEX && er->cacheex_wait_time && er->cacheex_wait_time_expired){
cx = snprintf ( sreason, sizeof sreason, " (wait_time over)");
}
if(er->cw_count>1){
snprintf ( sreason+cx, (sizeof sreason)-cx, " (cw count %d)", er->cw_count);
}
#endif
client->cwlastresptime = comp_timeb(&tpe, &er->tps);
time_t now = time(NULL);
webif_client_add_lastresponsetime(client, client->cwlastresptime, now, er->rc); // add to ringbuffer
if(er_reader)
{
struct s_client *er_cl = er_reader->client;
if(check_client(er_cl))