-
Notifications
You must be signed in to change notification settings - Fork 67
/
module-camd35-cacheex.c
621 lines (534 loc) · 15.9 KB
/
module-camd35-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
#define MODULE_LOG_PREFIX "camd35"
#include "globals.h"
#include "oscam-array.h"
#if defined(CS_CACHEEX) && (defined(MODULE_CAMD35) || defined(MODULE_CAMD35_TCP))
#include "module-cacheex.h"
#include "module-camd35.h"
#include "module-camd35-cacheex.h"
#include "oscam-cache.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-string.h"
#include "oscam-reader.h"
uint8_t camd35_node_id[8];
/**
* send push filter
*/
void camd35_cacheex_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->cevnum, buf + i);
i += 2;
int32_t max_filters = 15;
for(j=0; j<max_filters; j++)
{
if(filter->cevnum > j){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
i2b_buf(4, d->caid, buf + i);
}
i += 4;
}
for(j=0; j<max_filters; j++)
{
if(filter->cevnum > j){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
i2b_buf(4, d->cmask, buf + i);
}
i += 4;
}
for(j=0; j<max_filters; j++)
{
if(filter->cevnum > j){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
i2b_buf(4, d->prid, buf + i);
}
i += 4;
}
for(j=0; j<max_filters; j++)
{
if(filter->cevnum > j){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
i2b_buf(4, d->srvid, buf + i);
}
i += 4;
}
cs_log_dbg(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
*/
static void camd35_cacheex_push_filter(struct s_client *cl, uint8_t *buf, uint8_t mode)
{
struct s_reader *rdr = cl->reader;
int i = 20, j;
int32_t caid, cmask, provid, srvid;
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;
}
cecspvaluetab_clear(filter);
i += 2;
int32_t max_filters = 15;
for(j=0; j<max_filters; j++)
{
caid = b2i(4, buf + i);
if(caid > 0){
CECSPVALUETAB_DATA d;
memset(&d, 0, sizeof(d));
d.caid = b2i(4, buf + i);
cecspvaluetab_add(filter, &d);
}
i += 4;
}
for(j=0; j<max_filters; j++)
{
cmask = b2i(4, buf + i);
if(j<filter->cevnum){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
d->cmask = cmask;
}
i += 4;
}
for(j=0; j<max_filters; j++)
{
provid = b2i(4, buf + i);
if(j<filter->cevnum){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
d->prid = provid;
}
i += 4;
}
for(j=0; j<max_filters; j++)
{
srvid = b2i(4, buf + i);
if(j<filter->cevnum){
CECSPVALUETAB_DATA *d = &filter->cevdata[j];
d->srvid = srvid;
}
i += 4;
}
cs_log_dbg(D_CACHEEX, "cacheex: received push filter request from %s", username(cl));
}
static int32_t camd35_cacheex_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_log_dbg(D_CACHEEX, "cacheex: nodelist reached %d nodes, no push", cacheex_maxhop(cl));
return 0;
}
if(cl->reader)
{
if(!cl->reader->tcp_connected)
{
cs_log_dbg(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_log_dbg(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_log_dbg(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_log_dbg(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_log_dbg(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;
}
static int32_t camd35_cacheex_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(!camd35_tcp_connect(cl))
{
cs_log_dbg(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...?
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++; }
cs_log_dbg(D_CWC, "CWC (CE) push to %s 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;
}
static void camd35_cacheex_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_log_dbg(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;
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;
}
}
if (er->cwc_cycletime && er->cwc_next_cw_cycle < 2)
{
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_log_dbg(D_CWC, "CWC (CE) received from %s 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);
}
uint8_t *ofs = buf + 20;
//Read ecmd5
memcpy(er->ecmd5, ofs, sizeof(er->ecmd5)); //16
ofs += sizeof(er->ecmd5);
if(!check_cacheex_filter(cl, er))
{ return; }
//Read csp_hash:
er->csp_hash = ntohl(b2i(4, ofs));
ofs += 4;
//Read cw:
memcpy(er->cw, ofs, sizeof(er->cw)); //16
ofs += sizeof(er->cw);
//Check auf neues Format:
uint8_t *data;
if(size > (sizeof(er->ecmd5) + sizeof(er->csp_hash) + sizeof(er->cw)))
{
//Read lastnodes:
uint8_t count = *ofs;
ofs++;
//check max nodes:
if(count > cacheex_maxhop(cl))
{
cs_log_dbg(D_CACHEEX, "cacheex: received %d nodes (max=%d), ignored! %s", (int32_t)count, cacheex_maxhop(cl), username(cl));
NULLFREE(er);
return;
}
cs_log_dbg(D_CACHEEX, "cacheex: received %d nodes %s", (int32_t)count, username(cl));
if (er){
er->csp_lastnodes = ll_create("csp_lastnodes");
}
while(count)
{
if(!cs_malloc(&data, 8))
{ break; }
memcpy(data, ofs, 8);
ofs += 8;
ll_append(er->csp_lastnodes, data);
count--;
cs_log_dbg(D_CACHEEX, "cacheex: received node %" PRIu64 "X %s", cacheex_node_id(data), username(cl));
}
}
else
{
cs_log_dbg(D_CACHEEX, "cacheex: received old cachex from %s", username(cl));
er->csp_lastnodes = ll_create("csp_lastnodes");
}
//store remote node id if we got one. The remote node is the first node in the node list
data = ll_has_elements(er->csp_lastnodes);
if(data && !cl->ncd_skey[8]) //Ok, this is tricky, we use newcamd key storage for saving the remote node
{
memcpy(cl->ncd_skey, data, 8);
cl->ncd_skey[8] = 1; //Mark as valid node
}
cs_log_dbg(D_CACHEEX, "cacheex: received cacheex from remote node id %" PRIu64 "X", cacheex_node_id(cl->ncd_skey));
//for compatibility: add peer node if no node received (not working now, maybe later):
if(!ll_count(er->csp_lastnodes) && cl->ncd_skey[8])
{
if(!cs_malloc(&data, 8))
{ return; }
memcpy(data, cl->ncd_skey, 8);
ll_append(er->csp_lastnodes, data);
cs_log_dbg(D_CACHEEX, "cacheex: added missing remote node id %" PRIu64 "X", cacheex_node_id(data));
}
cacheex_add_to_cache(cl, er);
}
void camd35_cacheex_recv_ce1_cwc_info(struct s_client *cl, uchar *buf, int32_t idx)
{
if(!(buf[0] == 0x01 && buf[18] < 0xFF && buf[18] > 0x00)) // cwc info ; normal camd3 ecms send 0xFF but we need no cycletime of 255 ;)
return;
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_log_dbg(D_CWC, "CWC (CE1) received from %s 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);
}
/**
* 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
*/
static void camd35_cacheex_push_receive_remote_id(struct s_client *cl, uint8_t *buf)
{
memcpy(cl->ncd_skey, buf + 20, 8);
cl->ncd_skey[8] = 1;
cs_log_dbg(D_CACHEEX, "cacheex: received id answer from %s: %" PRIu64 "X", username(cl), cacheex_node_id(cl->ncd_skey));
}
void camd35_cacheex_init_dcw(struct s_client *client, ECM_REQUEST *er)
{
uint8_t *buf = er->src_data; // get orig request
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_log_dbg(D_CWC, "CWC (CE1) push to %s 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];
}
}
/**
* send own id
*/
void camd35_cacheex_push_send_own_id(struct s_client *cl, uint8_t *mbuf)
{
uint8_t rbuf[32]; //minimal size
if(!cl->crypted) { return; }
cs_log_dbg(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_log_dbg(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
}
bool camd35_cacheex_server(struct s_client *client, uint8_t *mbuf)
{
switch(mbuf[0])
{
case 0x3c: // Cache-push filter request
if(client->account && client->account->cacheex.mode==2){
camd35_cacheex_push_filter(client, mbuf, 2);
}
break;
case 0x3d: // Cache-push id request
camd35_cacheex_push_receive_remote_id(client, mbuf); //reader send request id with its nodeid, so we save it!
camd35_cacheex_push_send_own_id(client, mbuf);
if(client->cacheex_needfilter && client->account && client->account->cacheex.mode==3){
camd35_cacheex_send_push_filter(client, 3);
client->cacheex_needfilter = 0;
}
break;
case 0x3e: // Cache-push id answer
camd35_cacheex_push_receive_remote_id(client, mbuf);
break;
case 0x3f: // Cache-push
camd35_cacheex_push_in(client, mbuf);
break;
default:
return 0; // Not processed by cacheex
}
return 1; // Processed by cacheex
}
bool camd35_cacheex_recv_chk(struct s_client *client, uint8_t *buf)
{
struct s_reader *rdr = client->reader;
switch(buf[0])
{
case 0x3c: // Cache-push filter request
if(rdr->cacheex.mode==3){
camd35_cacheex_push_filter(client, buf, 3);
}
break;
case 0x3d: // Cache-push id request
camd35_cacheex_push_receive_remote_id(client, buf); //client send request id with its nodeid, so we save it!
camd35_cacheex_push_send_own_id(client, buf);
break;
case 0x3e: // Cache-push id answer
camd35_cacheex_push_receive_remote_id(client, buf);
break;
case 0x3f: //cache-push
camd35_cacheex_push_in(client, buf);
break;
default:
return 0; // Not processed by cacheex
}
return 1; // Processed by cacheex
}
/**
* request remote id
*/
void camd35_cacheex_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_log_dbg(D_CACHEEX, "cacheex: sending id request to %s", username(cl));
camd35_send(cl, rbuf, 12); //send adds +20
}
void camd35_cacheex_module_init(struct s_module *ph)
{
ph->c_cache_push = camd35_cacheex_push_out;
ph->c_cache_push_chk = camd35_cacheex_push_chk;
ph->s_init = camd35_server_client_init;
}
#endif