-
Notifications
You must be signed in to change notification settings - Fork 1
/
module-cacheex.c
1547 lines (1380 loc) · 42.3 KB
/
module-cacheex.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 "cacheex"
#include "globals.h"
#ifdef CS_CACHEEX
#include "cscrypt/md5.h"
#include "module-cacheex.h"
#include "module-cw-cycle-check.h"
#include "ncam-cache.h"
#include "ncam-chk.h"
#include "ncam-client.h"
#include "ncam-conf.h"
#include "ncam-ecm.h"
#include "ncam-hashtable.h"
#include "ncam-lock.h"
#include "ncam-net.h"
#include "ncam-string.h"
#include "ncam-time.h"
#include "ncam-work.h"
#ifdef CS_CACHEEX_AIO
#include "ncam-array.h"
#endif
#define cs_cacheex_matcher "ncam.cacheex"
extern uint8_t cc_node_id[8];
extern uint8_t camd35_node_id[8];
uint8_t cacheex_peer_id[8];
extern CS_MUTEX_LOCK ecm_pushed_deleted_lock;
extern struct ecm_request_t *ecm_pushed_deleted;
extern CS_MUTEX_LOCK ecmcache_lock;
extern struct ecm_request_t *ecmcwcache;
// HIT CACHE functions **************************************************************
typedef struct hit_key_t {
uint16_t caid;
uint32_t prid;
uint16_t srvid;
} HIT_KEY;
typedef struct cache_hit_t {
HIT_KEY key;
struct timeb time;
struct timeb max_hitcache_time;
uint64_t grp;
uint64_t grp_last_max_hitcache_time;
#ifdef CS_CACHEEX_AIO
int32_t waittime_block;
#endif
node ht_node;
node ll_node;
} CACHE_HIT;
static pthread_rwlock_t hitcache_lock;
static hash_table ht_hitcache;
static list ll_hitcache;
static bool cacheex_running;
void cacheex_init_hitcache(void)
{
init_hash_table(&ht_hitcache, &ll_hitcache);
if (pthread_rwlock_init(&hitcache_lock,NULL) != 0)
cs_log("Error creating lock hitcache_lock!");
cacheex_running = true;
}
void cacheex_free_hitcache(void)
{
cacheex_running = false;
cacheex_cleanup_hitcache(true);
deinitialize_hash_table(&ht_hitcache);
pthread_rwlock_destroy(&hitcache_lock);
}
static int cacheex_compare_hitkey(const void *arg, const void *obj)
{
if(((const HIT_KEY*)arg)->caid==((const CACHE_HIT*)obj)->key.caid
&& ((const HIT_KEY*)arg)->prid==((const CACHE_HIT*)obj)->key.prid
&& ((const HIT_KEY*)arg)->srvid==((const CACHE_HIT*)obj)->key.srvid)
{
return 0;
}
return 1;
}
static int32_t cacheex_check_hitcache(ECM_REQUEST *er, struct s_client *cl)
{
CACHE_HIT *result;
HIT_KEY search;
memset(&search, 0, sizeof(HIT_KEY));
search.caid = er->caid;
search.prid = er->prid;
search.srvid = er->srvid;
SAFE_RWLOCK_RDLOCK(&hitcache_lock);
result = find_hash_table(&ht_hitcache, &search, sizeof(HIT_KEY), &cacheex_compare_hitkey);
if(result){
struct timeb now;
cs_ftime(&now);
int64_t gone = comp_timeb(&now, &result->time);
uint64_t grp = cl?cl->grp:0;
if(
gone <= (cfg.max_hitcache_time*1000)
&&
(!grp || !result->grp || (grp & result->grp))
#ifdef CS_CACHEEX_AIO
&&
result->waittime_block <= cfg.waittime_block_start
#endif
)
{
SAFE_RWLOCK_UNLOCK(&hitcache_lock);
return 1;
}
}
SAFE_RWLOCK_UNLOCK(&hitcache_lock);
return 0;
}
static void cacheex_add_hitcache(struct s_client *cl, ECM_REQUEST *er)
{
if (!cfg.max_hitcache_time) // we don't want check/save hitcache
return;
if (!cfg.cacheex_wait_timetab.cevnum)
return;
uint32_t cacheex_wait_time = get_cacheex_wait_time(er,NULL);
if (!cacheex_wait_time)
return;
CACHE_HIT *result;
HIT_KEY search;
memset(&search, 0, sizeof(HIT_KEY));
search.caid = er->caid;
search.prid = er->prid;
search.srvid = er->srvid;
SAFE_RWLOCK_WRLOCK(&hitcache_lock);
result = find_hash_table(&ht_hitcache, &search, sizeof(HIT_KEY), &cacheex_compare_hitkey);
if(!result) // not found, add it!
{
if(cs_malloc(&result, sizeof(CACHE_HIT)))
{
memset(result, 0, sizeof(CACHE_HIT));
result->key.caid = er->caid;
result->key.prid = er->prid;
result->key.srvid = er->srvid;
cs_ftime(&result->max_hitcache_time);
#ifdef CS_CACHEEX_AIO
result->waittime_block = 0;
#endif
add_hash_table(&ht_hitcache, &result->ht_node, &ll_hitcache, &result->ll_node, result, &result->key, sizeof(HIT_KEY));
}
}
if(result)
{
if(cl)
{
result->grp |= cl->grp;
result->grp_last_max_hitcache_time |= cl->grp;
}
cs_ftime(&result->time); //always update time;
}
SAFE_RWLOCK_UNLOCK(&hitcache_lock);
}
static void cacheex_del_hitcache(struct s_client *cl, ECM_REQUEST *er)
{
HIT_KEY search;
CACHE_HIT *result;
memset(&search, 0, sizeof(HIT_KEY));
search.caid = er->caid;
search.prid = er->prid;
search.srvid = er->srvid;
if(cl && cl->grp)
{
result = find_hash_table(&ht_hitcache, &search, sizeof(HIT_KEY), &cacheex_compare_hitkey);
while(result)
{
result->grp &= ~cl->grp;
result->grp_last_max_hitcache_time &= ~cl->grp;
result = find_hash_table(&ht_hitcache, &search, sizeof(HIT_KEY), &cacheex_compare_hitkey);
}
}
SAFE_RWLOCK_WRLOCK(&hitcache_lock);
search_remove_elem_hash_table(&ht_hitcache, &search, sizeof(HIT_KEY), &cacheex_compare_hitkey);
SAFE_RWLOCK_UNLOCK(&hitcache_lock);
}
void cacheex_cleanup_hitcache(bool force)
{
CACHE_HIT *cachehit;
node *i,*i_next;
struct timeb now;
int64_t gone, gone_max_hitcache_time;
int32_t timeout = (cfg.max_hitcache_time + (cfg.max_hitcache_time / 2)) * 1000; // 1,5
int32_t clean_grp = (cfg.max_hitcache_time * 1000);
SAFE_RWLOCK_WRLOCK(&hitcache_lock);
i = get_first_node_list(&ll_hitcache);
while (i)
{
i_next = i->next;
cachehit = get_data_from_node(i);
if(!cachehit)
{
i = i_next;
continue;
}
cs_ftime(&now);
gone = comp_timeb(&now, &cachehit->time);
gone_max_hitcache_time = comp_timeb(&now, &cachehit->max_hitcache_time);
if(force || gone>timeout
#ifdef CS_CACHEEX_AIO
|| (cachehit->waittime_block > (cfg.waittime_block_time / 3 + 1))
#endif
)
{
remove_elem_list(&ll_hitcache, &cachehit->ll_node);
remove_elem_hash_table(&ht_hitcache, &cachehit->ht_node);
NULLFREE(cachehit);
}
else if(gone_max_hitcache_time >= clean_grp){
cachehit->grp = cachehit->grp_last_max_hitcache_time;
cachehit->grp_last_max_hitcache_time = 0;
cs_ftime(&cachehit->max_hitcache_time);
}
#ifdef CS_CACHEEX_AIO
if(cfg.waittime_block_start && (cachehit && cachehit->waittime_block >= cfg.waittime_block_start))
{
cachehit->waittime_block++;
}
#endif
i = i_next;
}
SAFE_RWLOCK_UNLOCK(&hitcache_lock);
}
static int32_t cacheex_ecm_hash_calc(uint8_t *buf, int32_t n)
{
int32_t i, h = 0;
for(i = 0; i < n; i++)
{
h = 31 * h + buf[i];
}
return h;
}
void cacheex_update_hash(ECM_REQUEST *er)
{
er->csp_hash = cacheex_ecm_hash_calc(er->ecm + 3, er->ecmlen - 3);
}
void cacheex_free_csp_lastnodes(ECM_REQUEST *er)
{
ll_destroy_free_data(&er->csp_lastnodes);
}
void cacheex_set_csp_lastnode(ECM_REQUEST *er)
{
er->csp_lastnodes = NULL;
}
void cacheex_set_cacheex_src(ECM_REQUEST *ecm, struct s_client *cl)
{
if(ecm->cacheex_src == cl)
ecm->cacheex_src = NULL;
}
void cacheex_init_cacheex_src(ECM_REQUEST *ecm, ECM_REQUEST *er)
{
if(!ecm->cacheex_src)
ecm->cacheex_src = er->cacheex_src;
}
static void *chkcache_process(void)
{
set_thread_name(__func__);
time_t timeout;
struct ecm_request_t *er, *ecm;
uint8_t add_hitcache_er;
struct s_reader *cl_rdr;
struct s_reader *rdr;
struct s_ecm_answer *ea;
struct s_client *cex_src = NULL;
struct s_write_from_cache *wfc = NULL;
while(cacheex_running)
{
cs_readlock(__func__, &ecmcache_lock);
for(er = ecmcwcache; er; er = er->next)
{
timeout = time(NULL) - ((cfg.ctimeout + 500) / 1000 + 1);
if(er->tps.time < timeout)
{ break; }
if(er->rc < E_UNHANDLED || er->readers_timeout_check) // already answered
{ continue; }
// CHECK IF FOUND ECM IN CACHE
ecm = check_cache(er, er->client);
if(ecm) // found in cache
{
// check for add_hitcache
if(ecm->cacheex_src) // cw from cacheex
{
// only when no wait_time expires (or not wait_time)
if((er->cacheex_wait_time && !er->cacheex_wait_time_expired) || !er->cacheex_wait_time)
{
// add_hitcache already called, but we check if we have to call it for these (er) caid|prid|srvid
if(ecm->prid!=er->prid || ecm->srvid!=er->srvid)
{
// here we should be sure cex client has not been freed!
cex_src = ecm->cacheex_src && is_valid_client(ecm->cacheex_src) && !ecm->cacheex_src->kill ? ecm->cacheex_src : NULL;
if(cex_src) // add_hitcache only if client is really active
{
add_hitcache_er = 1;
cl_rdr = cex_src->reader;
if(cl_rdr && cl_rdr->cacheex.mode == 2)
{
for(ea = er->matching_rdr; ea; ea = ea->next)
{
rdr = ea->reader;
if(cl_rdr == rdr && ((ea->status & REQUEST_ANSWERED) == REQUEST_ANSWERED))
{
cs_log_dbg(D_CACHEEX | D_CSP | D_LB,"{client %s, caid %04X, prid %06X, srvid %04X} [CACHEEX] skip ADD self request!",
(check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
add_hitcache_er=0; // don't add hit cache, reader requested self
}
}
}
// USE cacheex client (to get correct group) and ecm
// from requesting client (to get correct caid|prid|srvid)!!!
if(add_hitcache_er)
{
cacheex_add_hitcache(cex_src, er);
}
}
}
}
else
{
// add_hitcache already called, but we have to remove it because cacheex not coming before wait_time
if(ecm->prid == er->prid && ecm->srvid == er->srvid)
{ cacheex_del_hitcache(er->client, ecm); }
}
}
// END check for add_hitcache
if(check_client(er->client))
{
wfc = NULL;
if(!cs_malloc(&wfc, sizeof(struct s_write_from_cache)))
{
NULLFREE(ecm);
continue;
}
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);
continue;
}
}
else
{
NULLFREE(ecm);
}
}
}
cs_readunlock(__func__, &ecmcache_lock);
cs_sleepms(10);
}
return NULL;
}
void checkcache_process_thread_start(void)
{
start_thread("chkcache_process", (void *)&chkcache_process, NULL, NULL, 1, 1);
}
void cacheex_init(void)
{
// Init random node id
get_random_bytes(cacheex_peer_id, 8);
#ifdef MODULE_CCCAM
memcpy(cacheex_peer_id, cc_node_id, 8);
#endif
#ifdef MODULE_CAMD35_TCP
memcpy(camd35_node_id, cacheex_peer_id, 8);
#endif
}
void cacheex_clear_account_stats(struct s_auth *account)
{
account->cwcacheexgot = 0;
account->cwcacheexpush = 0;
account->cwcacheexhit = 0;
#ifdef CS_CACHEEX_AIO
account->cwcacheexgotlg = 0;
account->cwcacheexpushlg = 0;
#endif
}
void cacheex_clear_client_stats(struct s_client *client)
{
client->cwcacheexgot = 0;
client->cwcacheexpush = 0;
client->cwcacheexhit = 0;
#ifdef CS_CACHEEX_AIO
client->cwcacheexgotlg = 0;
client->cwcacheexpushlg = 0;
#endif
}
int32_t cacheex_add_stats(struct s_client *cl, uint16_t caid, uint16_t srvid, uint32_t prid, uint8_t direction
#ifdef CS_CACHEEX_AIO
, uint8_t localgenerated
#endif
)
{
if(!cfg.cacheex_enable_stats)
{ return -1; }
// create list if doesn't exist
if(!cl->ll_cacheex_stats)
{ cl->ll_cacheex_stats = ll_create("ll_cacheex_stats"); }
time_t now = time((time_t *)0);
LL_ITER itr = ll_iter_create(cl->ll_cacheex_stats);
S_CACHEEX_STAT_ENTRY *cacheex_stats_entry;
// check for existing entry
while((cacheex_stats_entry = ll_iter_next(&itr)))
{
if(cacheex_stats_entry->cache_srvid == srvid &&
cacheex_stats_entry->cache_caid == caid &&
cacheex_stats_entry->cache_prid == prid &&
cacheex_stats_entry->cache_direction == direction)
{
// we already have this entry - just add count and time
cacheex_stats_entry->cache_count++;
#ifdef CS_CACHEEX_AIO
if(localgenerated)
cacheex_stats_entry->cache_count_lg++;
#endif
cacheex_stats_entry->cache_last = now;
return cacheex_stats_entry->cache_count;
}
}
// if we land here we have to add a new entry
if(cs_malloc(&cacheex_stats_entry, sizeof(S_CACHEEX_STAT_ENTRY)))
{
cacheex_stats_entry->cache_caid = caid;
cacheex_stats_entry->cache_srvid = srvid;
cacheex_stats_entry->cache_prid = prid;
cacheex_stats_entry->cache_count = 1;
#ifdef CS_CACHEEX_AIO
if(localgenerated)
cacheex_stats_entry->cache_count_lg = 1;
#endif
cacheex_stats_entry->cache_last = now;
cacheex_stats_entry->cache_direction = direction;
ll_iter_insert(&itr, cacheex_stats_entry);
return 1;
}
return 0;
}
int8_t cacheex_maxhop(struct s_client *cl)
{
int maxhop = 10;
if(cl->reader && cl->reader->cacheex.maxhop)
{ maxhop = cl->reader->cacheex.maxhop; }
else if(cl->account && cl->account->cacheex.maxhop)
{ maxhop = cl->account->cacheex.maxhop; }
return maxhop;
}
#ifdef CS_CACHEEX_AIO
int8_t cacheex_maxhop_lg(struct s_client *cl)
{
int max = 10;
int maxhop = cacheex_maxhop(cl);
int maxhop_lg = maxhop;
if(cl->reader && cl->reader->cacheex.maxhop_lg)
{
if(cl->reader->cacheex.maxhop_lg > max)
{
cl->reader->cacheex.maxhop_lg = max;
}
if(cl->reader->cacheex.maxhop_lg < maxhop)
{
maxhop_lg = maxhop;
}
else
{
maxhop_lg = cl->reader->cacheex.maxhop_lg;
}
cl->reader->cacheex.maxhop_lg = maxhop_lg;
}
else if(cl->account && cl->account->cacheex.maxhop_lg)
{
if(cl->account->cacheex.maxhop_lg > max)
{
cl->account->cacheex.maxhop_lg = max;
}
if(cl->account->cacheex.maxhop_lg < maxhop)
{
maxhop_lg = maxhop;
}
else
{
maxhop_lg = cl->account->cacheex.maxhop_lg;
}
cl->account->cacheex.maxhop_lg = maxhop_lg;
}
return maxhop_lg;
}
#endif
static void cacheex_cache_push_to_client(struct s_client *cl, ECM_REQUEST *er)
{
add_job(cl, ACTION_CACHE_PUSH_OUT, er, 0);
}
/**
* Check for NULL ecmd5
**/
static uint8_t checkECMD5(ECM_REQUEST *er)
{
int8_t i;
for(i = 0; i < CS_ECMSTORESIZE; i++)
if(er->ecmd5[i]) { return 1; }
return 0;
}
#ifdef CS_CACHEEX_AIO
static uint8_t chk_cwcheck(ECM_REQUEST *er, uint8_t cw_check_for_push)
{
if(!cw_check_for_push)
return 1;
CWCHECK check_cw;
check_cw = get_cwcheck(er);
if(check_cw.mode && check_cw.counter > 1)
{
if(er->cw_count >= check_cw.counter)
{
return 1;
}
else
{
cs_log_dbg(D_CACHEEX, "push denied - cacheex_check_cw.counter: %u > er->cw_count: %u", check_cw.counter, er->cw_count);
return 0;
}
}
else
{
return 1;
}
}
#endif
/**
* cacheex modes:
*
* cacheex=1 CACHE PULL:
* Situation: ncam A reader1 has cacheex=1, ncam B account1 has cacheex=1
* ncam A gets a ECM request, reader1 send this request to ncam B, ncam B checks his cache
* a. not found in cache: return NOK
* a. found in cache: return OK+CW
* b. not found in cache, but found pending request: wait max cacheexwaittime and check again
* ncam B never requests new ECMs
*
* CW-flow: B->A
*
* cacheex=2 CACHE PUSH:
* Situation: ncam A reader1 has cacheex=2, ncam B account1 has cacheex=2
* if ncam B gets a CW, its pushed to ncam A
* reader has normal functionality and can request ECMs
*
* Problem: ncam B can only push if ncam A is connected
* Problem or feature?: ncam A reader can request ecms from ncam B
*
* CW-flow: B->A
*
*/
void cacheex_cache_push(ECM_REQUEST *er)
{
if(er->rc >= E_NOTFOUND) { return; }
//cacheex=2 mode: push (server->remote)
struct s_client *cl;
cs_readlock(__func__, &clientlist_lock);
for(cl = first_client->next; cl; cl = cl->next)
{
if(check_client(cl) && er->cacheex_src != cl)
{
if(get_module(cl)->num == R_CSP) // always send to csp cl
{
if(!er->cacheex_src || cfg.csp.allow_reforward) { cacheex_cache_push_to_client(cl, er); } // but not if the origin was cacheex (might loop)
}
else if(cl->typ == 'c' && !cl->dup && cl->account && cl->account->cacheex.mode == 2) // send cache over user
{
if(get_module(cl)->c_cache_push // cache-push able
&& (!er->grp || (cl->grp & er->grp)
#ifdef CS_CACHEEX_AIO
|| (er->localgenerated && ((cl->grp & cfg.cacheex_push_lg_groups) && strcmp(username(cl), username(er->cacheex_src))))
#endif
) // Group-check
/**** OUTGOING FILTER CHECK ***/
&& (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_name || strcmp(username(cl), er->selected_reader->label)) // check reader mode-1 loopback by same name
&& (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_ip || (check_client(er->selected_reader->client) && !IP_EQUAL(cl->ip, er->selected_reader->client->ip))) // check reader mode-1 loopback by same ip
&& (!cl->account->cacheex.drop_csp || checkECMD5(er)) // cacheex_drop_csp-check
&& chk_ctab(er->caid, &cl->ctab) // Caid-check
&& (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &cl->ftab)) // Ident-check (not for csp: prid=0 always!)
&& chk_srvid(cl, er) // Service-check
&& chk_csp_ctab(er, &cl->account->cacheex.filter_caidtab) // cacheex_ecm_filter
#ifdef CS_CACHEEX_AIO
&& (er->localgenerated // lg-flag-check
|| chk_srvid_localgenerated_only_exception(er) // lg-only-service-exception
|| !(cl->account->cacheex.localgenerated_only // usr-lg-only
|| (
(cl->account->cacheex.feature_bitfield & 64) // cx-aio >= 9.2.6 => check ftab
&& (chk_lg_only(er, &cl->account->cacheex.lg_only_tab) // usr-lg-only-ftab (feature 64)
|| chk_lg_only(er, &cfg.cacheex_lg_only_tab)) // global-lg-only-ftab (feature 64)
)
)
)
&& (chk_cwcheck(er, cl->account->cacheex.cw_check_for_push)) // check cw_check-counter if enabled
&& chk_nopushafter(er->caid, &cl->account->cacheex.cacheex_nopushafter_tab, er->ecm_time) // no push after check
#endif
)
{
cacheex_cache_push_to_client(cl, er);
}
}
}
}
cs_readunlock(__func__, &clientlist_lock);
//cacheex=3 mode: reverse push (reader->server)
cs_readlock(__func__, &readerlist_lock);
cs_readlock(__func__, &clientlist_lock);
struct s_reader *rdr;
for(rdr = first_active_reader; rdr; rdr = rdr->next)
{
cl = rdr->client;
if(check_client(cl) && er->cacheex_src != cl && rdr->cacheex.mode == 3) // send cache over reader
{
if(rdr->ph.c_cache_push // cache-push able
&& (!er->grp || (rdr->grp & er->grp)
#ifdef CS_CACHEEX_AIO
|| (er->localgenerated && ((rdr->grp & cfg.cacheex_push_lg_groups) && strcmp(username(cl), username(er->cacheex_src))))
#endif
) // Group-check
/**** OUTGOING FILTER CHECK ***/
&& (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_name || strcmp(username(cl), er->selected_reader->label)) // check reader mode-1 loopback by same name
&& (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_ip || (check_client(er->selected_reader->client) && !IP_EQUAL(cl->ip, er->selected_reader->client->ip))) // check reader mode-1 loopback by same ip
&& (!rdr->cacheex.drop_csp || checkECMD5(er)) // cacheex_drop_csp-check
&& chk_ctab(er->caid, &rdr->ctab) // Caid-check
&& (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &rdr->ftab)) // Ident-check (not for csp: prid=0 always!)
&& chk_srvid(cl, er) // Service-check
&& chk_csp_ctab(er, &rdr->cacheex.filter_caidtab) // cacheex_ecm_filter
#ifdef CS_CACHEEX_AIO
&& (er->localgenerated // lg-only-check
|| chk_srvid_localgenerated_only_exception(er) // service-exception
|| !(rdr->cacheex.localgenerated_only // rdr-lg-only
|| (
(rdr->cacheex.feature_bitfield & 64) // cx-aio >= 9.2.6 => check ftab
&& (chk_lg_only(er, &rdr->cacheex.lg_only_tab) // rdr-lg-only-ftab (feature 64)
|| chk_lg_only(er, &cfg.cacheex_lg_only_tab)) // global-lg-only-ftab (feature 64)
)
)
)
&& (chk_cwcheck(er, rdr->cacheex.cw_check_for_push)) // check cw_check-counter if enabled
&& chk_nopushafter(er->caid, &rdr->cacheex.cacheex_nopushafter_tab, er->ecm_time)
#endif
) // no push after check
{
cacheex_cache_push_to_client(cl, er);
}
}
}
cs_readunlock(__func__, &clientlist_lock);
cs_readunlock(__func__, &readerlist_lock);
}
/**** INCOMING FILTER CHECK ***/
uint8_t check_cacheex_filter(struct s_client *cl, ECM_REQUEST *er)
{
if(check_client(cl) && cl->typ == 'p' && cl->reader && cl->reader->cacheex.mode == 2
&& (!cl->reader->cacheex.drop_csp || checkECMD5(er)) // cacheex_drop_csp-check
&& chk_ctab(er->caid, &cl->reader->ctab) // Caid-check
&& (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &cl->reader->ftab)) // Ident-check (not for csp: prid=0 always!)
&& chk_srvid(cl, er)) // Service-check
{
return 1;
}
if(check_client(cl) && cl->typ == 'c' && cl->account && cl->account->cacheex.mode == 3
&& (!cl->account->cacheex.drop_csp || checkECMD5(er)) // cacheex_drop_csp-check
&& chk_ctab(er->caid, &cl->ctab) // Caid-check
&& (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &cl->ftab)) // Ident-check (not for csp: prid=0 always!)
&& chk_srvid(cl, er)) // Service-check
{
return 1;
}
free_ecm(er);
return 0;
}
static struct s_cacheex_matcher *is_cacheex_matcher_matching(ECM_REQUEST *from_er, ECM_REQUEST *to_er)
{
struct s_cacheex_matcher *entry = cfg.cacheex_matcher;
int8_t v_ok = (from_er && to_er) ? 2 : 1;
while(entry)
{
int8_t ok = 0;
if(from_er
&& (!entry->caid || entry->caid == from_er->caid)
&& (!entry->provid || entry->provid == from_er->prid)
&& (!entry->srvid || entry->srvid == from_er->srvid)
&& (!entry->chid || entry->chid == from_er->chid)
&& (!entry->pid || entry->pid == from_er->pid)
&& (!entry->ecmlen || entry->ecmlen == from_er->ecmlen))
{ ok++; }
if(to_er
&& (!entry->to_caid || entry->to_caid == to_er->caid)
&& (!entry->to_provid || entry->to_provid == to_er->prid)
&& (!entry->to_srvid || entry->to_srvid == to_er->srvid)
&& (!entry->to_chid || entry->to_chid == to_er->chid)
&& (!entry->to_pid || entry->to_pid == to_er->pid)
&& (!entry->to_ecmlen || entry->to_ecmlen == to_er->ecmlen))
{ ok++; }
if(ok == v_ok)
{
if(!from_er || !to_er || from_er->srvid == to_er->srvid)
{ return entry; }
}
entry = entry->next;
}
return NULL;
}
bool cacheex_is_match_alias(struct s_client *cl, ECM_REQUEST *er)
{
return check_client(cl) && cl->account && cl->account->cacheex.mode == 1 && is_cacheex_matcher_matching(NULL, er);
}
#ifdef WITH_DEBUG
static void log_cacheex_cw(ECM_REQUEST *er, char *reason)
{
uint8_t *data;
uint8_t remotenodeid[8];
data = ll_last_element(er->csp_lastnodes);
if(data)
{ memcpy(remotenodeid, data, 8); }
else
{ memset(remotenodeid, 0 , 8); }
char buf_ecm[109];
format_ecm(er, buf_ecm, 109);
cs_log_dbg(D_CACHEEX,"got pushed ecm [%s]: %s - odd/even 0x%x - CSP cw: %s - pushed from %s, at hop %d, origin node-id %" PRIu64 "X",
reason, buf_ecm, er->ecm[0], (checkECMD5(er)?"NO":"YES"), er->from_csp ? "csp" : username((er->cacheex_src?er->cacheex_src:er->client)), ll_count(er->csp_lastnodes), er->csp_lastnodes ? cacheex_node_id(remotenodeid): 0);
}
#endif
// check if sky_ger 64 bit CW has valid checksum bytes and therefore is probably invalid
uint8_t check_nds_cwex(ECM_REQUEST *er)
{
uint8_t k, csum;
uint8_t hit = 0;
uint8_t oe = checkCWpart(er->cw, 0) ? 0 : 8;
for(k = 0; k < 8; k += 4)
{
csum = ((er->cw[k + oe] + er->cw[k + oe + 1] + er->cw[k + oe + 2]) & 0xff);
if(er->cw[k + oe + 3] == csum)
{
hit++;
}
}
if(hit > 1)
{
return 1;
}
return 0;
}
static int32_t cacheex_add_to_cache_int(struct s_client *cl, ECM_REQUEST *er, int8_t csp)
{
if(er->rc >= E_NOTFOUND) { return 0; }
if(!cl)
{ return 0; }
if(!csp && cl->reader && cl->reader->cacheex.mode != 2) //from reader
{
cs_log_dbg(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl));
return 0;
}
if(!csp && !cl->reader && cl->account && cl->account->cacheex.mode != 3) //from user
{
cs_log_dbg(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl));
return 0;
}
if(!csp && !cl->reader && !cl->account) //not active!
{
cs_log_dbg(D_CACHEEX, "CACHEX received, but invalid client state %s", username(cl));
return 0;
}
if(!cfg.disablecrccws && ((cl->typ == 'c' && cl->account && !cl->account->disablecrccacheex) || ( cl->typ == 'p' && cl->reader && !cl->reader->disablecrccws)))
{
uint8_t selectedForIgnChecksum = chk_if_ignore_checksum(er, &cfg.disablecrccws_only_for);
if(cl->typ == 'c')
{
selectedForIgnChecksum += chk_if_ignore_checksum(er, &cl->account->disablecrccacheex_only_for);
}
if(cl->typ == 'p')
{
selectedForIgnChecksum += chk_if_ignore_checksum(er, &cl->reader->disablecrccws_only_for);
}
if(!selectedForIgnChecksum)
{
uint8_t i, c;
for(i = 0; i < 16; i += 4)
{
c = ((er->cw[i] + er->cw[i + 1] + er->cw[i + 2]) & 0xff);
if(er->cw[i + 3] != c)
{
cs_log_dump_dbg(D_CACHEEX, er->cw, 16, "push received cw with chksum error from %s", csp ? "csp" : username(cl));
cl->cwcacheexerr++;
if(cl->account)
{ cl->account->cwcacheexerr++; }
return 0;
}
}
}
}
#ifdef CS_CACHEEX_AIO
if(caid_is_videoguard(er->caid))
{
if(cl->typ == 'p' && chk_if_ignore_checksum(er, &cl->reader->disablecrccws_only_for) && !chk_srvid_disablecrccws_only_for_exception(er))
{
if(check_nds_cwex(er))
{
if(check_client(cl) && cl->reader && cl->reader->dropbadcws)
{
if (((D_CACHEEX) & cs_dblevel)) // avoid useless operations if debug is not enabled
{
uint8_t remotenodeid[8];
cacheex_get_srcnodeid(er, remotenodeid);
cs_log_dbg(D_CACHEEX, "Probably got pushed bad CW from cacheex reader: %s, caid %04X, srvid %04X - dropping CW, lg: %i, hop: %i, src-nodeid %" PRIu64 "X", cl->reader->label, er->caid, er->srvid, er->localgenerated, ll_count(er->csp_lastnodes), er->csp_lastnodes ? cacheex_node_id(remotenodeid): 0);
}
return 0;
}
else
{
if (((D_CACHEEX) & cs_dblevel)) // avoid useless operations if debug is not enabled
{
uint8_t remotenodeid[8];
cacheex_get_srcnodeid(er, remotenodeid);
cs_log_dbg(D_CACHEEX, "Probably got pushed bad CW from cacheex reader: %s, caid %04X, srvid %04X, lg: %i, hop: %i, src-nodeid %" PRIu64 "X", cl->reader->label, er->caid, er->srvid, er->localgenerated, ll_count(er->csp_lastnodes), er->csp_lastnodes ? cacheex_node_id(remotenodeid): 0);
}
}
}
}
if(cl->typ == 'c' && chk_if_ignore_checksum(er, &cl->account->disablecrccacheex_only_for) && !chk_srvid_disablecrccws_only_for_exception(er))
{
if(check_nds_cwex(er))
{
if (((D_CACHEEX) & cs_dblevel)) // avoid useless operations if debug is not enabled
{
uint8_t remotenodeid[8];
cacheex_get_srcnodeid(er, remotenodeid);
cs_log_dbg(D_CACHEEX, "Probably got bad CW from cacheex user: %s, caid %04X, srvid %04X, lg: %i, hop: %i, src-nodeid %" PRIu64 "X", username(cl), er->caid, er->srvid, er->localgenerated, ll_count(er->csp_lastnodes), er->csp_lastnodes ? cacheex_node_id(remotenodeid): 0);
}
}
}
}
#endif
// Skip check for BISS1 - cw could be indeed zero
// Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
if(chk_is_null_CW(er->cw) && !caid_is_biss(er->caid))
{
cs_log_dump_dbg(D_CACHEEX, er->cw, 16, "push received null cw from %s", csp ? "csp" : username(cl));
cl->cwcacheexerr++;
if(cl->account)
{ cl->account->cwcacheexerr++; }
return 0;
}
// Don't check for BISS1 and BISS2 mode 1/E or fake caid (ECM is fake for them)
// Don't check for BISS2 mode CA (ECM table is always 0x80)
if(!caid_is_biss(er->caid) && !caid_is_fake(er->caid) && get_odd_even(er) == 0)
{
cs_log_dbg(D_CACHEEX, "push received ecm with null odd/even byte from %s", csp ? "csp" : username(cl));
cl->cwcacheexerr++;
if(cl->account)
{ cl->account->cwcacheexerr++; }
return 0;
}
if(!chk_halfCW(er, er->cw))
{
#ifdef WITH_DEBUG
if(cs_dblevel & D_CACHEEX)
{
log_cacheex_cw(er, "bad half cw");
}
#endif
cl->cwcacheexerr++;
if(cl->account)
{ cl->account->cwcacheexerr++; }
return 0;
}
if((csp && cfg.csp.block_fakecws) || (cl->reader && cl->reader->cacheex.block_fakecws)
|| (!cl->reader && cl->account && cl->account->cacheex.block_fakecws))
{
if(chk_is_fakecw(er->cw))
{
cs_log_dbg(D_CACHEEX, "push received fake cw from %s", csp ? "csp" : username(cl));
cl->cwcacheexerr++;
if(cl->account)
{ cl->account->cwcacheexerr++; }
return 0;
}
}
er->grp |= cl->grp; // ok for mode2 reader too: cl->reader->grp
er->rc = E_CACHEEX;
er->cacheex_src = cl;
er->selected_reader = cl->reader;
er->client = NULL; // No Owner! So no fallback!
if(check_client(cl))
{
cl->cwcacheexgot++;
if(cl->account)
{ cl->account->cwcacheexgot++; }
first_client->cwcacheexgot++;
#ifdef CS_CACHEEX_AIO
if(er->localgenerated)
{
cl->cwcacheexgotlg++;
if(cl->account)
cl->account->cwcacheexgotlg++;
first_client->cwcacheexgotlg++;
}
#endif
}
cacheex_add_hitcache(cl, er); // we have to call it before add_cache, because in chk_process we could remove it!
add_cache(er);
#ifdef CS_CACHEEX_AIO
cacheex_add_stats(cl, er->caid, er->srvid, er->prid, 1, er->localgenerated);
#else
cacheex_add_stats(cl, er->caid, er->srvid, er->prid, 1);