-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-camd35.c
1506 lines (1317 loc) · 38.5 KB
/
module-camd35.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"
#if defined MODULE_CAMD35 || defined MODULE_CAMD35_TCP
#include "cscrypt/md5.h"
#include "module-cacheex.h"
#include "oscam-aes.h"
#include "oscam-chk.h"
#include "oscam-cache.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-emm.h"
#include "oscam-net.h"
#include "oscam-string.h"
#include "oscam-reader.h"
//CMD00 - ECM (request)
//CMD01 - ECM (response)
//CMD02 - EMM (in clientmode - set EMM, in server mode - EMM data) - obsolete
//CMD03 - ECM (cascading request)
//CMD04 - ECM (cascading response)
//CMD05 - EMM (emm request) send cardata/cardinfo to client
//CMD06 - EMM (incomming EMM in server mode)
//CMD19 - EMM (incomming EMM in server mode) only seen with caid 0x1830
//CMD08 - Stop sending requests to the server for current srvid,prvid,caid
//CMD44 - MPCS/OScam internal error notification
//CMD55 - connect_on_init/keepalive
//CMD0x3c - CACHEEX Cache-push filter request
//CMD0x3d - CACHEEX Cache-push id request
//CMD0x3e - CACHEEX Cache-push id answer
//CMD0x3f - CACHEEX cache-push
//used variable ncd_skey for storing remote node id: ncd_skey[0..7] : 8
//bytes node id ncd_skey[8] : 1=valid node id received
#define REQ_SIZE 584 // 512 + 20 + 0x34
static int32_t __camd35_send(struct s_client *cl, uchar *buf, int32_t buflen, int answer_awaited)
{
int32_t l;
unsigned char rbuf[REQ_SIZE + 15 + 4], *sbuf = rbuf + 4;
if(!cl->udp_fd || !cl->crypted) { return (-1); } //exit if no fd or aes key not set!
//Fix ECM len > 255
if(buflen <= 0)
{ buflen = ((buf[0] == 0) ? (((buf[21] & 0x0f) << 8) | buf[22]) + 3 : buf[1]); }
l = 20 + (((buf[0] == 3) || (buf[0] == 4)) ? 0x34 : 0) + buflen;
memcpy(rbuf, cl->ucrc, 4);
memcpy(sbuf, buf, l);
memset(sbuf + l, 0xff, 15); // set unused space to 0xff for newer camd3's
i2b_buf(4, crc32(0L, sbuf + 20, buflen), sbuf + 4);
l = boundary(4, l);
cs_ddump_mask(cl->typ == 'c' ? D_CLIENT : D_READER, sbuf, l, "send %d bytes to %s", l, username(cl));
aes_encrypt_idx(&cl->aes_keys, sbuf, l);
int32_t status;
if(cl->is_udp)
{
status = sendto(cl->udp_fd, rbuf, l + 4, 0, (struct sockaddr *)&cl->udp_sa, cl->udp_sa_len);
if(status == -1) { set_null_ip(&SIN_GET_ADDR(cl->udp_sa)); }
}
else
{
status = send(cl->udp_fd, rbuf, l + 4, 0);
if(cl->typ == 'p' && cl->reader)
{
if(status == -1) { network_tcp_connection_close(cl->reader, "can't send"); }
}
else if(cl->typ == 'c')
{
if(status == -1) { cs_disconnect_client(cl); }
}
}
if(status != -1)
{
if(cl->reader && answer_awaited)
{
cl->reader->last_s = time(NULL);
}
if(cl->reader && !answer_awaited)
{
cl->reader->last_s = cl->reader->last_g = time(NULL);
}
cl->last = time(NULL);
}
return status;
}
static int32_t camd35_send(struct s_client *cl, uchar *buf, int32_t buflen)
{
// send command and set sending time because we await response
return __camd35_send(cl, buf, buflen, 1);
}
static int32_t camd35_send_without_timeout(struct s_client *cl, uchar *buf, int32_t buflen)
{
// send command and do NOT set sending time because we DON'T await response
return __camd35_send(cl, buf, buflen, 0);
}
static int32_t camd35_auth_client(struct s_client *cl, uchar *ucrc)
{
int32_t rc = 1;
uint32_t crc;
struct s_auth *account;
unsigned char md5tmp[MD5_DIGEST_LENGTH];
if(cl->upwd[0])
{ return (memcmp(cl->ucrc, ucrc, 4) ? 1 : 0); }
cl->crypted = 1;
crc = (((ucrc[0] << 24) | (ucrc[1] << 16) | (ucrc[2] << 8) | ucrc[3]) & 0xffffffffL);
for(account = cfg.account; (account) && (!cl->upwd[0]); account = account->next)
if(crc == crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), md5tmp), MD5_DIGEST_LENGTH))
{
rc = cs_auth_client(cl, account, NULL);
if(!rc)
{
memcpy(cl->ucrc, ucrc, 4);
cs_strncpy((char *)cl->upwd, account->pwd, sizeof(cl->upwd));
aes_set_key(&cl->aes_keys, (char *) MD5(cl->upwd, strlen((char *)cl->upwd), md5tmp));
return 0;
}
}
return (rc);
}
static int32_t camd35_recv(struct s_client *client, uchar *buf, int32_t l)
{
int32_t rc, s, rs, n = 0, buflen = 0, len = 0;
for(rc = rs = s = 0; !rc; s++)
{
switch(s)
{
case 0:
if(!client->udp_fd) { return (-9); }
if(client->is_udp && client->typ == 'c')
{
rs = recv_from_udpipe(buf);
}
else
{
//read minimum packet size (4 byte ucrc + 32 byte data) to detect packet size (tcp only)
//rs = recv(client->udp_fd, buf, client->is_udp ? l : 36, 0);
if(client->is_udp){
while (1){
rs = recv(client->udp_fd, buf, l, 0);
if (rs < 0){
if(errno == EINTR) { continue; } // try again in case of interrupt
if(errno == EAGAIN) { continue; } //EAGAIN needs select procedure again
cs_debug_mask(client->typ == 'c' ? D_CLIENT : D_READER, "ERROR: %s (errno=%d %s)", __func__, errno, strerror(errno));
break;
}else {break;}
}
}else{
int32_t tot=36, readed=0;
rs = 0;
do
{
readed = recv(client->udp_fd, buf+rs, tot, 0);
if (readed < 0){
if(errno == EINTR) { continue; } // try again in case of interrupt
if(errno == EAGAIN) { continue; } //EAGAIN needs select procedure again
cs_debug_mask(client->typ == 'c' ? D_CLIENT : D_READER, "ERROR: %s (errno=%d %s)", __func__, errno, strerror(errno));
break;
}
if (readed == 0){ // nothing to read left!
break;
}
if (readed > 0){ // received something, add it!
tot-=readed;
rs+=readed;
}
}
while(tot!=0);
}
}
if(rs < 36)
{
rc = -1;
goto out;
}
break;
case 1:
switch(camd35_auth_client(client, buf))
{
case 0:
break; // ok
case 1:
rc = -2;
break; // unknown user
default:
rc = -9;
break; // error's from cs_auth()
}
memmove(buf, buf + 4, rs -= 4);
break;
case 2:
aes_decrypt(&client->aes_keys, buf, rs);
if(rs != boundary(4, rs))
cs_debug_mask(client->typ == 'c' ? D_CLIENT : D_READER,
"WARNING: packet size has wrong decryption boundary");
n = (buf[0] == 3) ? 0x34 : 0;
//Fix for ECM request size > 255 (use ecm length field)
if(buf[0] == 0)
{ buflen = (((buf[21] & 0x0f) << 8) | buf[22]) + 3; }
else if(buf[0] == 0x3d || buf[0] == 0x3e || buf[0] == 0x3f) //cacheex-push
{ buflen = buf[1] | (buf[2] << 8); }
else
{ buflen = buf[1]; }
n = boundary(4, n + 20 + buflen);
if(!(client->is_udp && client->typ == 'c') && (rs < n) && ((n - 32) > 0))
{
//len = recv(client->udp_fd, buf+32, n-32, 0); // read the rest of the packet
int32_t tot=n-32, readed=0;
len = 0;
do{
readed = recv(client->udp_fd, buf+32+len, tot, 0); // read the rest of the packet
if (readed < 0){
if(errno == EINTR) { continue; } // try again in case of interrupt
if(errno == EAGAIN) { continue; } //EAGAIN needs select procedure again
cs_debug_mask(client->typ == 'c' ? D_CLIENT : D_READER, "ERROR: %s (errno=%d %s)", __func__, errno, strerror(errno));
break;
}
if (readed == 0){ // nothing to read left!
break;
}
if (readed > 0){ // received something, add it!
tot-=readed;
len+=readed;
}
}
while(tot!=0);
if(len > 0)
{
rs += len;
aes_decrypt(&client->aes_keys, buf + 32, len);
}
if(len < 0)
{
rc = -1;
goto out;
}
}
cs_ddump_mask(client->typ == 'c' ? D_CLIENT : D_READER,
buf, rs, "received %d bytes from %s", rs, remote_txt());
if(n < rs)
cs_debug_mask(client->typ == 'c' ? D_CLIENT : D_READER,
"ignoring %d bytes of garbage", rs - n);
else if(n > rs) { rc = -3; }
break;
case 3:
if(crc32(0L, buf + 20, buflen) != b2i(4, buf + 4)) { rc = -4; }
if(!rc) { rc = n; }
break;
}
}
out:
if((rs > 0) && ((rc == -1) || (rc == -2)))
{
cs_ddump_mask(client->typ == 'c' ? D_CLIENT : D_READER, buf, rs,
"received %d bytes from %s (native)", rs, remote_txt());
}
if(rc >= 0) { client->last = time(NULL); } // last client action is now
switch(rc)
{
//case 0: break;
case -1:
cs_log("packet is too small (received %d bytes, expected %d bytes)", rs, l);
break;
case -2:
if(cs_auth_client(client, 0, "unknown user"))
{ cs_disconnect_client(client); }
break;
case -3:
cs_log("incomplete request !");
break;
case -4:
cs_log("checksum error (wrong password ?)");
break;
//default: cs_debug_mask(D_TRACE, "camd35_recv returns rc=%d", rc); break;
}
return (rc);
}
/*
* server functions
*/
static void camd35_request_emm(ECM_REQUEST *er)
{
int32_t i;
time_t now;
uchar mbuf[1024];
struct s_client *cl = cur_client();
struct s_reader *aureader = NULL, *rdr = NULL;
if(er->selected_reader && !er->selected_reader->audisabled && ll_contains(cl->aureader_list, er->selected_reader))
{ aureader = er->selected_reader; }
if(!aureader && cl->aureader_list)
{
LL_ITER itr = ll_iter_create(cl->aureader_list);
while((rdr = ll_iter_next(&itr)))
{
if(emm_reader_match(rdr, er->caid, er->prid))
{
aureader = rdr;
break;
}
}
}
if(!aureader)
{ return; } // TODO
uint16_t au_caid = aureader->caid;
// Bulcrypt has 2 caids and aureader->caid can't be used.
// Use 5581 for AU.
if(!au_caid && (er->caid == 0x5581 || er->caid == 0x4aee))
{ au_caid = 0x5581; }
time(&now);
if(!memcmp(cl->lastserial, aureader->hexserial, 8))
if(abs(now - cl->last) < 180) { return; }
memcpy(cl->lastserial, aureader->hexserial, 8);
cl->last = now;
if(au_caid)
{
cl->disable_counter = 0;
cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
username(cur_client()), aureader->label, aureader->caid,
aureader->auprovid ? aureader->auprovid : b2i(4, aureader->prid[0]));
}
else if(cl->disable_counter > 2)
{ return; }
else
{ cl->disable_counter++; }
memset(mbuf, 0, sizeof(mbuf));
mbuf[2] = mbuf[3] = 0xff; // must not be zero
i2b_buf(2, er->srvid, mbuf + 8);
//override request provid with auprovid if set in CMD05
if(aureader->auprovid)
{
if(aureader->auprovid != er->prid)
{ i2b_buf(4, aureader->auprovid, mbuf + 12); }
else
{ i2b_buf(4, er->prid, mbuf + 12); }
}
else
{
i2b_buf(4, er->prid, mbuf + 12);
}
i2b_buf(2, er->pid, mbuf + 16);
mbuf[0] = 5;
mbuf[1] = 111;
if(au_caid)
{
mbuf[39] = 1; // no. caids
mbuf[20] = au_caid >> 8; // caid's (max 8)
mbuf[21] = au_caid & 0xff;
if(au_caid == 0x5581)
{
// Bulcrypt have two CAIDs, add the second one
mbuf[39]++;
mbuf[22] = 0x4aee >> 8;
mbuf[23] = 0x4aee & 0xff;
}
memcpy(mbuf + 40, aureader->hexserial, 6); // serial now 6 bytes
mbuf[47] = aureader->nprov;
for(i = 0; i < aureader->nprov; i++)
{
if((au_caid >= 0x1700 && au_caid <= 0x1799) || // Betacrypt
(au_caid >= 0x0600 && au_caid <= 0x0699)) // Irdeto (don't know if this is correct, cause I don't own a IRDETO-Card)
{
mbuf[48 + (i * 5)] = aureader->prid[i][0];
memcpy(&mbuf[50 + (i * 5)], &aureader->prid[i][1], 3);
}
else
{
mbuf[48 + (i * 5)] = aureader->prid[i][2];
mbuf[49 + (i * 5)] = aureader->prid[i][3];
memcpy(&mbuf[50 + (i * 5)], &aureader->sa[i][0], 4); // for conax we need at least 4 Bytes
}
}
//we think client/server protocols should deliver all information, and only readers should discard EMM
mbuf[128] = (aureader->blockemm & EMM_GLOBAL && !(aureader->saveemm & EMM_GLOBAL)) ? 0 : 1;
mbuf[129] = (aureader->blockemm & EMM_SHARED && !(aureader->saveemm & EMM_SHARED)) ? 0 : 1;
mbuf[130] = (aureader->blockemm & EMM_UNIQUE && !(aureader->saveemm & EMM_UNIQUE)) ? 0 : 1;
//mbuf[131] = aureader->card_system; //Cardsystem for Oscam client
}
else // disable emm
{ mbuf[20] = mbuf[39] = mbuf[40] = mbuf[47] = mbuf[49] = 1; }
memcpy(mbuf + 10, mbuf + 20, 2);
camd35_send(cl, mbuf, 0); // send with data-len 111 for camd3 > 3.890
mbuf[1]++;
camd35_send(cl, mbuf, 0); // send with data-len 112 for camd3 < 3.890
}
static void camd35_send_dcw(struct s_client *client, ECM_REQUEST *er)
{
uchar *buf;
buf = er->src_data; // get orig request
if(!buf)
{
cs_log("%s: src_data missing.",client->reader->ph.desc);
return;
}
if(er->rc == E_INVALID && !client->c35_suppresscmd08) //send normal CMD08
{
buf[0] = 0x08;
buf[1] = 2;
memset(buf + 20, 0, buf[1]);
buf[22] = er->rc; //put rc in byte 22 - hopefully don't break legacy camd3
}
else if(er->rc == E_STOPPED) //send sleep CMD08
{
buf[0] = 0x08;
buf[1] = 2;
buf[20] = 0;
buf[21] = 0xFF;
cs_log("%s stop request send", client->account->usr);
}
else
{
// Send CW
if((er->rc < E_NOTFOUND) || (er->rc == E_FAKE))
{
if(buf[0] == 3)
{ memmove(buf + 20 + 16, buf + 20 + buf[1], 0x34); }
buf[0]++;
buf[1] = 16;
#ifdef CS_CACHEEX
if(((client->typ == 'c' && client->account && client->account->cacheex.mode)
|| ((client->typ == 'p' || client->typ == 'r') && (client->reader && client->reader->cacheex.mode)))
&& er->cwc_cycletime && er->cwc_next_cw_cycle < 2) // ce1
{
buf[18] = er->cwc_cycletime; // contains cwc stage3 cycletime
if(er->cwc_next_cw_cycle == 1)
{ buf[18] = (buf[18] | 0x80); } // set bit 8 to high
if(client->typ == 'c' && client->account && client->account->cacheex.mode)
{ client->account->cwc_info++; }
else if((client->typ == 'p' || client->typ == 'r') && (client->reader && client->reader->cacheex.mode))
{ client->cwc_info++; }
cs_debug_mask(D_CWC, "CWC (CE1) push to %s (camd3) cycletime: %isek - nextcwcycle: CW%i for %04X:%06X:%04X", username(client), er->cwc_cycletime, er->cwc_next_cw_cycle, er->caid, er->prid, er->srvid);
buf[19] = er->ecm[0];
}
#endif
memcpy(buf + 20, er->cw, buf[1]);
}
else
{
// Send old CMD44 to prevent cascading problems with older mpcs/oscam versions
buf[0] = 0x44;
buf[1] = 0;
}
}
camd35_send(client, buf, 0);
camd35_request_emm(er);
}
static void camd35_process_ecm(uchar *buf, int buflen)
{
ECM_REQUEST *er;
if(!buf || buflen < 23)
{ return; }
uint16_t ecmlen = (((buf[21] & 0x0f) << 8) | buf[22]) + 3;
if(ecmlen + 20 > buflen)
{ return; }
if(!(er = get_ecmtask()))
{ return; }
// er->l = buf[1];
//fix ECM LEN issue
er->ecmlen = ecmlen;
if(!cs_malloc(&er->src_data, 0x34 + 20 + er->ecmlen))
{ return; }
memcpy(er->src_data, buf, 0x34 + 20 + er->ecmlen); // save request
er->srvid = b2i(2, buf + 8);
er->caid = b2i(2, buf + 10);
er->prid = b2i(4, buf + 12);
//er->pid = b2i(2, buf+16); value is ecmtask idx see camd35_recv_chk 941
memcpy(er->ecm, buf + 20, er->ecmlen);
get_cw(cur_client(), er);
}
static void camd35_process_emm(uchar *buf, int buflen, int emmlen)
{
EMM_PACKET epg;
if(!buf || buflen < 20 || emmlen + 20 > buflen)
{ return; }
memset(&epg, 0, sizeof(epg));
epg.emmlen = emmlen;
memcpy(epg.caid, buf + 10, 2);
memcpy(epg.provid, buf + 12 , 4);
memcpy(epg.emm, buf + 20, epg.emmlen);
do_emm(cur_client(), &epg);
}
static int32_t tcp_connect(struct s_client *cl)
{
if(cl->is_udp) // check for udp client
{
if(!IP_ISSET(SIN_GET_ADDR(cl->udp_sa))) // check ip is set
{
if(!(hostResolve(cl->reader))) // no ip -> try to resolve ip of client
{
network_tcp_connection_close(cl->reader, "no ip");
return 0;
}
}
}
if(!cl->reader->tcp_connected) // client not connected
{
int32_t handle = 0;
handle = network_tcp_connection_open(cl->reader); // try to connect
if(handle < 0) // got no handle -> error!
{
cl->reader->last_s = 0; // set last send to zero
cl->reader->last_g = 0; // set last receive to zero
cl->last = 0; // set last client action to zero
return (0);
}
cl->reader->tcp_connected = 1;
cl->reader->card_status = CARD_INSERTED;
cl->reader->last_s = time(NULL); // reset last send
cl->reader->last_g = time(NULL); // reset last receive
cl->last = time(NULL); // reset last client action
cl->pfd = cl->udp_fd = handle;
}
if(!cl->udp_fd) { return (0); } // Check if client has no handle -> error
// check if client reached timeout, if reached and reader is not udp type disconnect reader
if(cl->reader->tcp_rto && (cl->reader->last_s - cl->reader->last_g > cl->reader->tcp_rto))
{
network_tcp_connection_close(cl->reader, "rto");
return 0;
}
return (1); // all ok
}
#ifdef CS_CACHEEX
uint8_t camd35_node_id[8];
/**
* send own id
*/
void camd35_cache_push_send_own_id(struct s_client *cl, uint8_t *mbuf)
{
uint8_t rbuf[32]; //minimal size
if(!cl->crypted) { return; }
cs_debug_mask(D_CACHEEX, "cacheex: received id request from node %" PRIu64 "X %s", cacheex_node_id(mbuf + 20), username(cl));
memset(rbuf, 0, sizeof(rbuf));
rbuf[0] = 0x3e;
rbuf[1] = 12;
rbuf[2] = 0;
memcpy(rbuf + 20, camd35_node_id, 8);
cs_debug_mask(D_CACHEEX, "cacheex: sending own id %" PRIu64 "X request %s", cacheex_node_id(camd35_node_id), username(cl));
camd35_send(cl, rbuf, 12); //send adds +20
}
/**
* request remote id
*/
void camd35_cache_push_request_remote_id(struct s_client *cl)
{
uint8_t rbuf[32];//minimal size
memset(rbuf, 0, sizeof(rbuf));
rbuf[0] = 0x3d;
rbuf[1] = 12;
rbuf[2] = 0;
memcpy(rbuf + 20, camd35_node_id, 8);
cs_debug_mask(D_CACHEEX, "cacheex: sending id request to %s", username(cl));
camd35_send(cl, rbuf, 12); //send adds +20
}
/**
* send push filter
*/
void camd35_cache_send_push_filter(struct s_client *cl, uint8_t mode)
{
struct s_reader *rdr = cl->reader;
int i = 20, j;
CECSPVALUETAB *filter;
//maximum size: 20+255
uint8_t buf[20+242];
memset(buf, 0, sizeof(buf));
buf[0] = 0x3c;
buf[1] = 0xf2;
//mode==2 send filters from rdr
if(mode == 2 && rdr)
{
filter = &rdr->cacheex.filter_caidtab;
}
//mode==3 send filters from acc
else if(mode == 3 && cl->typ == 'c' && cl->account)
{
filter = &cl->account->cacheex.filter_caidtab;
}
else {
return;
}
i2b_buf(2, filter->n, buf + i);
i += 2;
for(j=0; j<15; j++)
{
if(j<CS_MAXCAIDTAB)
{
i2b_buf(4, filter->caid[j], buf + i);
}
i += 4;
}
for(j=0; j<15 && j<CS_MAXCAIDTAB; j++)
{
if(j<CS_MAXCAIDTAB)
{
i2b_buf(4, filter->cmask[j], buf + i);
}
i += 4;
}
for(j=0; j<15 && j<CS_MAXCAIDTAB; j++)
{
if(j<CS_MAXCAIDTAB)
{
i2b_buf(4, filter->prid[j], buf + i);
}
i += 4;
}
for(j=0; j<15 && j<CS_MAXCAIDTAB; j++)
{
if(j<CS_MAXCAIDTAB)
{
i2b_buf(4, filter->srvid[j], buf + i);
}
i += 4;
}
cs_debug_mask(D_CACHEEX, "cacheex: sending push filter request to %s", username(cl));
camd35_send_without_timeout(cl, buf, 242); //send adds +20
}
/**
* store received push filter
*/
void camd35_cache_push_filter(struct s_client *cl, uint8_t *buf, uint8_t mode)
{
struct s_reader *rdr = cl->reader;
int i = 20, j;
CECSPVALUETAB *filter;
//mode==2 write filters to acc
if(mode == 2 && cl->typ == 'c' && cl->account && cl->account->cacheex.mode == 2
&& cl->account->cacheex.allow_filter == 1)
{
filter = &cl->account->cacheex.filter_caidtab;
}
//mode==3 write filters to rdr
else if(mode == 3 && rdr && rdr->cacheex.allow_filter == 1)
{
filter = &rdr->cacheex.filter_caidtab;
}
else {
return;
}
filter->n = b2i(2, buf + i);
i += 2;
if(filter->n > CS_MAXCAIDTAB)
{
filter->n = CS_MAXCAIDTAB;
}
for(j=0; j<15; j++)
{
if(j<CS_MAXCAIDTAB)
{
filter->caid[j] = b2i(4, buf + i);
}
i += 4;
}
for(j=0; j<15 && j<CS_MAXCAIDTAB; j++)
{
if(j<CS_MAXCAIDTAB)
{
filter->cmask[j] = b2i(4, buf + i);
}
i += 4;
}
for(j=0; j<15 && j<CS_MAXCAIDTAB; j++)
{
if(j<CS_MAXCAIDTAB)
{
filter->prid[j] = b2i(4, buf + i);
}
i += 4;
}
for(j=0; j<15 && j<CS_MAXCAIDTAB; j++)
{
if(j<CS_MAXCAIDTAB)
{
filter->srvid[j] = b2i(4, buf + i);
}
i += 4;
}
cs_debug_mask(D_CACHEEX, "cacheex: received push filter request from %s", username(cl));
}
/**
* when a server client connects
*/
static void camd35_server_client_init(struct s_client *cl)
{
if(!cl->init_done)
{
cl->cacheex_needfilter = 1;
}
}
/**
* store received remote id
*/
void camd35_cache_push_receive_remote_id(struct s_client *cl, uint8_t *buf)
{
memcpy(cl->ncd_skey, buf + 20, 8);
cl->ncd_skey[8] = 1;
cs_debug_mask(D_CACHEEX, "cacheex: received id answer from %s: %" PRIu64 "X", username(cl), cacheex_node_id(cl->ncd_skey));
}
int32_t camd35_cache_push_chk(struct s_client *cl, ECM_REQUEST *er)
{
if(ll_count(er->csp_lastnodes) >= cacheex_maxhop(cl)) //check max 10 nodes to push:
{
cs_debug_mask(D_CACHEEX, "cacheex: nodelist reached %d nodes, no push", cacheex_maxhop(cl));
return 0;
}
if(cl->reader)
{
if(!cl->reader->tcp_connected)
{
cs_debug_mask(D_CACHEEX, "cacheex: not connected %s -> no push", username(cl));
return 0;
}
}
//if(chk_is_null_nodeid(remote_node,8)){
if(!cl->ncd_skey[8])
{
cs_debug_mask(D_CACHEEX, "cacheex: NO peer_node_id got yet, skip!");
return 0;
}
uint8_t *remote_node = cl->ncd_skey; //it is sended by reader(mode 2) or client (mode 3) each 30s using keepalive msgs
//search existing peer nodes:
LL_LOCKITER *li = ll_li_create(er->csp_lastnodes, 0);
uint8_t *node;
while((node = ll_li_next(li)))
{
cs_debug_mask(D_CACHEEX, "cacheex: check node %" PRIu64 "X == %" PRIu64 "X ?", cacheex_node_id(node), cacheex_node_id(remote_node));
if(memcmp(node, remote_node, 8) == 0)
{
break;
}
}
ll_li_destroy(li);
//node found, so we got it from there, do not push:
if(node)
{
cs_debug_mask(D_CACHEEX,
"cacheex: node %" PRIu64 "X found in list => skip push!", cacheex_node_id(node));
return 0;
}
//check if cw is already pushed
if(check_is_pushed(er->cw_cache, cl))
{ return 0; }
cs_debug_mask(D_CACHEEX, "cacheex: push ok %" PRIu64 "X to %" PRIu64 "X %s", cacheex_node_id(camd35_node_id), cacheex_node_id(remote_node), username(cl));
return 1;
}
int32_t camd35_cache_push_out(struct s_client *cl, struct ecm_request_t *er)
{
int8_t rc = (er->rc < E_NOTFOUND) ? E_FOUND : er->rc;
if(rc != E_FOUND && rc != E_UNHANDLED) { return -1; } //Maybe later we could support other rcs
//E_FOUND : we have the CW,
//E_UNHANDLED : incoming ECM request
if(cl->reader)
{
if(!tcp_connect(cl))
{
cs_debug_mask(D_CACHEEX, "cacheex: not connected %s -> no push", username(cl));
return (-1);
}
}
uint32_t size = sizeof(er->ecmd5) + sizeof(er->csp_hash) + sizeof(er->cw) + sizeof(uint8_t) +
(ll_count(er->csp_lastnodes) + 1) * 8;
unsigned char *buf;
if(!cs_malloc(&buf, size + 20)) //camd35_send() adds +20
{ return -1; }
buf[0] = 0x3f; //New Command: Cache-push
buf[1] = size & 0xff;
buf[2] = size >> 8;
buf[3] = rc;
i2b_buf(2, er->srvid, buf + 8);
i2b_buf(2, er->caid, buf + 10);
i2b_buf(4, er->prid, buf + 12);
//i2b_buf(2, er->idx, buf + 16); // Not relevant...?
#ifdef CS_CACHEEX
if(er->cwc_cycletime && er->cwc_next_cw_cycle < 2)
{
buf[18] = er->cwc_cycletime; // contains cwc stage3 cycletime
if(er->cwc_next_cw_cycle == 1)
{ buf[18] = (buf[18] | 0x80); } // set bit 8 to high
if(cl->typ == 'c' && cl->account && cl->account->cacheex.mode)
{ cl->account->cwc_info++; }
else if((cl->typ == 'p' || cl->typ == 'r') && (cl->reader && cl->reader->cacheex.mode))
{ cl->cwc_info++; }
#endif
cs_debug_mask(D_CWC, "CWC (CE) push to %s (camd3) cycletime: %isek - nextcwcycle: CW%i for %04X:%06X:%04X", username(cl), er->cwc_cycletime, er->cwc_next_cw_cycle, er->caid, er->prid, er->srvid);
}
buf[19] = er->ecm[0] != 0x80 && er->ecm[0] != 0x81 ? 0 : er->ecm[0];
uint8_t *ofs = buf + 20;
//write oscam ecmd5:
memcpy(ofs, er->ecmd5, sizeof(er->ecmd5)); //16
ofs += sizeof(er->ecmd5);
//write csp hashcode:
i2b_buf(4, htonl(er->csp_hash), ofs);
ofs += 4;
//write cw:
memcpy(ofs, er->cw, sizeof(er->cw)); //16
ofs += sizeof(er->cw);
//write node count:
*ofs = ll_count(er->csp_lastnodes) + 1;
ofs++;
//write own node:
memcpy(ofs, camd35_node_id, 8);
ofs += 8;
//write other nodes:
LL_LOCKITER *li = ll_li_create(er->csp_lastnodes, 0);
uint8_t *node;
while((node = ll_li_next(li)))
{
memcpy(ofs, node, 8);
ofs += 8;
}
ll_li_destroy(li);
int32_t res = camd35_send(cl, buf, size);
NULLFREE(buf);
return res;
}
void camd35_recv_ce1_cwc_info(struct s_client *cl, uchar *buf, int32_t idx)
{
ECM_REQUEST *er = NULL;
int32_t i;
for(i = 0; i < cfg.max_pending; i++)
{
if (cl->ecmtask[i].idx == idx)
{
er = &cl->ecmtask[i];
break;
}
}
if(!er)
{ return; }
int8_t rc = buf[3];
if(rc != E_FOUND)
{ return; }
if(buf[18])
{
if(buf[18] & (0x01 << 7))
{
er->cwc_cycletime = (buf[18] & 0x7F); // remove bit 8 to get cycletime
er->parent->cwc_cycletime = er->cwc_cycletime;
er->cwc_next_cw_cycle = 1;
er->parent->cwc_next_cw_cycle = er->cwc_next_cw_cycle;
}
else
{
er->cwc_cycletime = buf[18];
er->parent->cwc_cycletime = er->cwc_cycletime;
er->cwc_next_cw_cycle = 0;
er->parent->cwc_next_cw_cycle = er->cwc_next_cw_cycle;
}
}
if(cl->typ == 'c' && cl->account && cl->account->cacheex.mode)
{ cl->account->cwc_info++; }
else if((cl->typ == 'p' || cl->typ == 'r') && (cl->reader && cl->reader->cacheex.mode))
{ cl->cwc_info++; }
cs_debug_mask(D_CWC, "CWC (CE1) received from %s (camd3) cycletime: %isek - nextcwcycle: CW%i for %04X:%06X:%04X", username(cl), er->cwc_cycletime, er->cwc_next_cw_cycle, er->caid, er->prid, er->srvid);
}
void camd35_cache_push_in(struct s_client *cl, uchar *buf)
{
int8_t rc = buf[3];
if(rc != E_FOUND && rc != E_UNHANDLED) //Maybe later we could support other rcs
{ return; }
ECM_REQUEST *er;
uint16_t size = buf[1] | (buf[2] << 8);
if(size < sizeof(er->ecmd5) + sizeof(er->csp_hash) + sizeof(er->cw))
{
cs_debug_mask(D_CACHEEX, "cacheex: %s received old cash-push format! data ignored!", username(cl));
return;
}
if(!(er = get_ecmtask()))
{ return; }
er->srvid = b2i(2, buf + 8);
er->caid = b2i(2, buf + 10);
er->prid = b2i(4, buf + 12);
er->pid = b2i(2, buf + 16);
er->ecm[0] = buf[19]!=0x80 && buf[19]!=0x81 ? 0 : buf[19]; //odd/even byte, usefull to send it over CSP and to check cw for swapping
er->rc = rc;
er->ecmlen = 0;
#ifdef CS_CACHEEX
if(buf[18])
{
if(buf[18] & (0x01 << 7))
{
er->cwc_cycletime = (buf[18] & 0x7F); // remove bit 8 to get cycletime
er->cwc_next_cw_cycle = 1;
}
else
{
er->cwc_cycletime = buf[18];
er->cwc_next_cw_cycle = 0;
}
}