-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-gbox.c
2139 lines (1910 loc) · 58.6 KB
/
module-gbox.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"
#ifdef MODULE_GBOX
#include "module-gbox.h"
#include "module-gbox-helper.h"
#include "module-gbox-sms.h"
#include "module-cccam.h"
#include "module-cccam-data.h"
#include "oscam-failban.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-lock.h"
#include "oscam-net.h"
#include "oscam-chk.h"
#include "oscam-string.h"
#include "oscam-time.h"
#include "oscam-reader.h"
#include "oscam-garbage.h"
#include "oscam-files.h"
#define FILE_GBOX_VERSION "gbox.ver"
#define FILE_SHARED_CARDS_INFO "share.info"
#define FILE_ATTACK_INFO "attack.txt"
#define FILE_GBOX_PEER_ONL "share.onl"
#define FILE_STATS "stats.info"
#define FILE_GOODNIGHT_OSD "goodnight.osd"
#define FILE_Local_CARDS_INFO "sc.info"
#define GBOX_STAT_HELLOL 0
#define GBOX_STAT_HELLOS 1
#define GBOX_STAT_HELLOR 2
#define GBOX_STAT_HELLO3 3
#define GBOX_STAT_HELLO4 4
#define RECEIVE_BUFFER_SIZE 1024
#define MIN_GBOX_MESSAGE_LENGTH 10 //CMD + pw + pw. TODO: Check if is really min
#define MIN_ECM_LENGTH 8
#define HELLO_KEEPALIVE_TIME 120 //send hello to peer every 2 min in case no ecm received
#define ECM_BROADCAST_PAUSE 3600
#define STATS_WRITE_TIME 300 //write stats file every 5 min
#define LOCAL_GBOX_MAJOR_VERSION 0x02
static struct gbox_data local_gbox;
static time_t last_stats_written;
//static void gbox_send_boxinfo(struct s_client *cli);
static void gbox_send_hello(struct s_client *cli);
static void gbox_send_hello_packet(struct s_client *cli, int8_t number, uchar *outbuf, uchar *ptr, int32_t nbcards);
static void gbox_send_checkcode(struct s_client *cli);
static void gbox_local_cards(struct s_client *cli);
static void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere);
static int32_t gbox_client_init(struct s_client *cli);
static int8_t gbox_check_header(struct s_client *cli, uchar *data, int32_t l);
static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n);
static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t n);
static int32_t gbox_checkcode_recv(struct s_client *cli, uchar *checkcode);
static int32_t gbox_decode_cmd(uchar *buf);
static uint8_t gbox_compare_pw(uchar *my_pw, uchar *rec_pw);
static uint16_t gbox_convert_password_to_id(uchar *password);
uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er);
static void init_local_gbox(void);
char *get_gbox_tmp_fname(char *fext)
{
static char gbox_tmpfile_buf[64] = { 0 };
const char *slash = "/";
if(!cfg.gbox_tmp_dir)
{
snprintf(gbox_tmpfile_buf, sizeof(gbox_tmpfile_buf), "%s%s%s",get_tmp_dir(), slash, fext);
}
else
{
if(cfg.gbox_tmp_dir[strlen(cfg.gbox_tmp_dir) - 1] == '/') { slash = ""; }
snprintf(gbox_tmpfile_buf, sizeof(gbox_tmpfile_buf), "%s%s%s", cfg.gbox_tmp_dir, slash, fext);
}
return gbox_tmpfile_buf;
}
uint16_t gbox_get_local_gbox_id(void)
{
return local_gbox.id;
}
uint32_t gbox_get_local_gbox_password(void)
{
return local_gbox.password[0] << 24 | local_gbox.password[1] << 16 | local_gbox.password[2] << 8 | local_gbox.password[3];
}
static uint8_t gbox_get_my_vers (void)
{
uint8_t gbx_vers = a2i(cfg.gbox_my_vers,1);
return gbx_vers;
}
static uint8_t gbox_get_my_cpu_api (void)
{
/* For configurable later adapt according to these functions:
unsigned char *GboxAPI( unsigned char a ) {
a = a & 7 ;
switch ( a ) {
case 0 : strcpy ( s_24,"No API");
break;
case 1 : strcpy ( s_24,"API 1");
break;
case 2 : strcpy ( s_24,"API 2");
break;
case 3 : strcpy ( s_24,"API 3");
break;
case 4 : strcpy ( s_24,"IBM API");
break;
default : strcpy ( s_24," ");
}
return s_24 ;
}
unsigned char *GboxCPU( unsigned char a ) {
a = a & 112 ;
a = a >> 4 ;
switch ( a ) {
case 1 : strcpy ( s_23,"80X86 compatible CPU");
break;
case 2 : strcpy ( s_23,"Motorola PowerPC MPC823 CPU");
break;
case 3 : strcpy ( s_23,"IBM PowerPC STB CPU");
break;
default : strcpy ( s_23," ");
}
return s_23:
}
*/
return a2i(cfg.gbox_my_cpu_api,1);
}
static void write_goodnight_to_osd_file(struct s_client *cli)
{
char *fext= FILE_GOODNIGHT_OSD;
char *fname = get_gbox_tmp_fname(fext);
if (file_exists(fname))
{
char buf[50];
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf), "%s %s %s", fname, username(cli), cli->reader->device);
cs_debug_mask(D_READER, "found file %s - write goodnight info from %s %s to OSD", fname, username(cli),cli->reader->device);
char *cmd = buf;
FILE *p;
if ((p = popen(cmd, "w")) == NULL)
{
cs_log("Error %s",fname);
return;
}
pclose(p);
}
return;
}
void gbox_write_peer_onl(void)
{
char *fext= FILE_GBOX_PEER_ONL;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "w");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
struct s_client *cl;
for(cl = first_client; cl; cl = cl->next)
{
if(cl->gbox && cl->typ == 'p')
{
struct gbox_peer *peer = cl->gbox;
if (peer->online)
{ fprintf(fhandle, "1 %s %s %04X 2.%02X\n",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id, peer->gbox.minor_version); }
else
{ fprintf(fhandle, "0 %s %s %04X 0.00\n",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id); }
}
}
fclose(fhandle);
return;
}
void gbox_write_version(void)
{
char *fext= FILE_GBOX_VERSION;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "w");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
fprintf(fhandle, "%02X.%02X\n", LOCAL_GBOX_MAJOR_VERSION, gbox_get_my_vers());
fclose(fhandle);
}
void gbox_write_local_cards_info(void)
{
int32_t card_count = 0;
char *fext= FILE_Local_CARDS_INFO;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle;
fhandle = fopen(fname, "w");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
LL_ITER it;
struct gbox_card *card;
it = ll_iter_create(local_gbox.cards);
char *crdtype = NULL;
while((card = ll_iter_next(&it)))
{
if(card->type == 1)
{crdtype = "Local_Card";}
if(card->type == 2)
{crdtype = "Betun_Card";}
if(card->type == 3)
{crdtype = "CCcam_Card";}
if(card->type == 4)
{crdtype = "Proxy_Card";}
fprintf(fhandle, "CardID:%2d %s %08X Sl:%2d id:%04X\n",
card_count, crdtype, card->provid_1,card->slot, card->peer_id);
card_count++;
}
fclose(fhandle);
return;
}
void gbox_write_shared_cards_info(void)
{
int32_t card_count = 0;
//int32_t i = 0;
char *fext= FILE_SHARED_CARDS_INFO;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle;
fhandle = fopen(fname, "w");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
LL_ITER it;
struct gbox_card *card;
struct s_client *cl;
for(cl = first_client; cl; cl = cl->next)
//for(i = 0, cl = first_client; cl; cl = cl->next, i++)
{
if(cl->gbox && cl->reader->card_status == CARD_INSERTED && cl->typ == 'p')
{
struct gbox_peer *peer = cl->gbox;
it = ll_iter_create(peer->gbox.cards);
while((card = ll_iter_next(&it)))
{
fprintf(fhandle, "CardID %2d at %s Card %08X Sl:%2d Lev:%1d dist:%1d id:%04X\n",
card_count, cl->reader->device, card->provid_1,
card->slot, card->lvl, card->dist, card->peer_id);
card_count++;
}
}
}
fclose(fhandle);
return;
}
void gbox_write_stats(void)
{
int32_t card_count = 0;
int32_t i = 0;
struct gbox_srvid *srvid = NULL;
char *fext= FILE_STATS;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle;
fhandle = fopen(fname, "w");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
LL_ITER it;
struct gbox_card *card;
struct s_client *cl;
for(i = 0, cl = first_client; cl; cl = cl->next, i++)
{
if(cl->gbox && cl->reader->card_status == CARD_INSERTED && cl->typ == 'p')
{
struct gbox_peer *peer = cl->gbox;
it = ll_iter_create(peer->gbox.cards);
while((card = ll_iter_next(&it)))
{
fprintf(fhandle, "CardID %4d Card %08X id:%04X #CWs:%d AVGtime:%d ms\n",
card_count, card->provid_1, card->peer_id, card->no_cws_returned, card->average_cw_time);
fprintf(fhandle, "Good SIDs:\n");
LL_ITER it2 = ll_iter_create(card->goodsids);
while((srvid = ll_iter_next(&it2)))
{ fprintf(fhandle, "%04X\n", srvid->sid); }
fprintf(fhandle, "Bad SIDs:\n");
it2 = ll_iter_create(card->badsids);
while((srvid = ll_iter_next(&it2)))
{ fprintf(fhandle, "%04X\n", srvid->sid); }
card_count++;
} // end of while ll_iter_next
} // end of if cl->gbox INSERTED && 'p'
} // end of for cl->next
fclose(fhandle);
return;
}
void hostname2ip(char *hostname, IN_ADDR_T *ip)
{
cs_resolve(hostname, ip, NULL, NULL);
}
static uint8_t gbox_compare_pw(uchar *my_pw, uchar *rec_pw)
{
return my_pw[0] == rec_pw[0] && my_pw[1] == rec_pw[1] && my_pw[2] == rec_pw[2] && my_pw[3] == rec_pw[3];
}
static uint16_t gbox_convert_password_to_id(uchar *password)
{
return (password[0] ^ password[2]) << 8 | (password[1] ^ password[3]);
}
void gbox_add_good_card(struct s_client *cl, uint16_t id_card, uint16_t caid, uint8_t slot, uint16_t sid_ok, uint32_t cw_time)
{
struct gbox_peer *peer = cl->gbox;
struct gbox_card *card = NULL;
struct gbox_srvid *srvid = NULL;
uint8_t factor = 0;
LL_ITER it = ll_iter_create(peer->gbox.cards);
while((card = ll_iter_next(&it)))
{
if(card->peer_id == id_card && card->caid == caid && card->slot == slot)
{
card->no_cws_returned++;
if (!card->no_cws_returned)
{ card->no_cws_returned = 10; } //wrap around
if (card->no_cws_returned < 10)
{ factor = card->no_cws_returned; }
else
{ factor = 10; }
card->average_cw_time = ((card->average_cw_time * (factor-1)) + cw_time) / factor;
cl->reader->currenthops = card->dist;
LL_ITER it2 = ll_iter_create(card->goodsids);
while((srvid = ll_iter_next(&it2)))
{
if(srvid->sid == sid_ok)
{
srvid->last_cw_received = time(NULL);
return; // sid_ok is already in the list of goodsids
}
}
LL_ITER it3 = ll_iter_create(card->badsids);
while((srvid = ll_iter_next(&it3)))
{
if(srvid->sid == sid_ok)
{
ll_iter_remove_data(&it3); // remove sid_ok from badsids
break;
}
}
if(!cs_malloc(&srvid, sizeof(struct gbox_srvid)))
{ return; }
srvid->sid = sid_ok;
srvid->provid_id = card->provid;
srvid->last_cw_received = time(NULL);
cs_debug_mask(D_READER, "GBOX Adding good SID: %04X for CAID: %04X Provider: %04X on CardID: %04X\n", sid_ok, caid, card->provid, id_card);
ll_append(card->goodsids, srvid);
break;
}
}//end of ll_iter_next
//return dist_c;
}
void gbox_free_card(struct gbox_card *card)
{
ll_destroy_data_NULL(card->badsids);
ll_destroy_data_NULL(card->goodsids);
add_garbage(card);
return;
}
void gbox_free_cardlist(LLIST *card_list)
{
if(card_list)
{
LL_ITER it = ll_iter_create(card_list);
struct gbox_card *card;
while((card = ll_iter_next_remove(&it)))
{
gbox_free_card(card);
}
ll_destroy_NULL(card_list);
}
return;
}
void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere)
{
/*
ere->gbox_crc = 0;
ere->gbox_ecm_id = 0;
ere->gbox_ecm_ok = 0;
*/
ere->gbox_hops = 0;
ere->gbox_peer = 0;
ere->gbox_mypeer = 0;
ere->gbox_caid = 0;
ere->gbox_prid = 0;
ere->gbox_slot = 0;
ere->gbox_version = 0;
ere->gbox_unknown = 0;
ere->gbox_type = 0;
}
struct s_client *get_gbox_proxy(uint16_t gbox_id)
{
struct s_client *cl;
for(cl = first_client; cl; cl = cl->next)
{
if(cl->typ == 'p' && cl->gbox && cl->gbox_peer_id == gbox_id)
{
return cl;
}
}
return NULL;
}
// if input client is typ proxy get client and vice versa
struct s_client *switch_client_proxy(struct s_client *cli, uint16_t gbox_id)
{
struct s_client *cl;
int8_t typ;
if(cli->typ == 'c')
{ typ = 'p'; }
else
{ typ = 'c'; }
for(cl = first_client; cl; cl = cl->next)
{
if(cl->typ == typ && cl->gbox && cl->gbox_peer_id == gbox_id)
{
return cl;
}
}
return cli;
}
void gbox_reconnect_client(uint16_t gbox_id)
{
struct s_client *cl;
for(cl = first_client; cl; cl = cl->next)
{
if(cl->gbox && cl->typ == 'p' && cl->gbox_peer_id == gbox_id)
{
hostname2ip(cl->reader->device, &SIN_GET_ADDR(cl->udp_sa));
SIN_GET_FAMILY(cl->udp_sa) = AF_INET;
SIN_GET_PORT(cl->udp_sa) = htons((uint16_t)cl->reader->r_port);
hostname2ip(cl->reader->device, &(cl->ip));
cl->reader->tcp_connected = 0;
cl->reader->card_status = NO_CARD;
struct gbox_peer *peer = cl->gbox;
peer->online = 0;
peer->ecm_idx = 0;
peer->hello_stat = GBOX_STAT_HELLOL;
cl->reader->last_s = cl->reader->last_g = 0;
gbox_free_cardlist(peer->gbox.cards);
peer->gbox.cards = ll_create("peer.cards");
gbox_send_hello(cl);
}
}
}
static void *gbox_server(struct s_client *cli, uchar *b, int32_t l)
{
if(l > 0)
{
cs_log("gbox: gbox_server %s/%d", cli->reader->label, cli->port);
gbox_check_header(cli, b, l);
}
return 0;
}
char *gbox_username(struct s_client *client)
{
if(!client) { return "anonymous"; }
if(client->reader)
if(client->reader->r_usr[0])
{
return client->reader->r_usr;
}
return "anonymous";
}
static int8_t gbox_disconnect_double_peers(struct s_client *cli)
{
struct s_client *cl;
for(cl = first_client; cl; cl = cl->next)
{
if (cl->typ == 'c' && cl->gbox_peer_id == cli->gbox_peer_id && cl != cli)
{
cs_debug_mask(D_READER, "gbox: disconnected double client %s",username(cl));
cs_disconnect_client(cl);
}
}
return 0;
}
static int8_t gbox_auth_client(struct s_client *cli, uchar *gbox_password)
{
uint16_t gbox_id = gbox_convert_password_to_id(gbox_password);
struct s_client *cl = switch_client_proxy(cli, gbox_id);
if(cl->typ == 'p' && cl->gbox && cl->reader)
{
struct gbox_peer *peer = cl->gbox;
struct s_auth *account = get_account_by_name(gbox_username(cl));
if (gbox_compare_pw(&peer->gbox.password[0],gbox_password) && account)
{
cli->crypted = 1; //display as crypted
cli->gbox = cl->gbox; //point to the same gbox as proxy
cli->reader = cl->reader; //point to the same reader as proxy
cli->gbox_peer_id = cl->gbox_peer_id; //signal authenticated
gbox_disconnect_double_peers(cli);
cs_auth_client(cli, account, NULL);
cli->account = account;
cli->grp = account->grp;
cli->lastecm = time(NULL);
return 0;
}
}
return -1;
}
static void gbox_server_init(struct s_client *cl)
{
if(!cl->init_done)
{
if(IP_ISSET(cl->ip))
{ cs_log("gbox: new connection from %s", cs_inet_ntoa(cl->ip)); }
//We cannot authenticate here, because we don't know gbox pw
cl->gbox_peer_id = NO_GBOX_ID;
cl->init_done = 1;
}
return;
}
int8_t get_card_action(struct gbox_card *card, uint32_t provid1, uint16_t peer_id, uint8_t slot, struct gbox_peer *peer)
{
LL_ITER it;
struct gbox_card *card_s;
if (!card) { return 1; } //insert
if (card->provid_1 < provid1) { return -1; } //remove
if (card->peer_id == peer_id && card->provid_1 == provid1 && card->slot == slot)
{ return 0; } //keep
else
{
it = ll_iter_create(peer->gbox.cards);
while ((card_s = ll_iter_next(&it)))
{
//card is still somewhere else we need to remove current
if (card_s->peer_id == peer_id && card_s->provid_1 == provid1 && card_s->slot == slot)
{ return -1; } //remove
}
return 1; //insert
}
}
int32_t gbox_cmd_hello(struct s_client *cli, uchar *data, int32_t n)
{
struct gbox_peer *peer = cli->gbox;
int32_t ncards_in_msg = 0;
int32_t payload_len = n;
//TODO: checkcode_len can be made void
int32_t checkcode_len = 0;
int32_t hostname_len = 0;
int32_t footer_len = 0;
uint8_t *ptr = 0;
uint8_t *current_ptr = 0;
LL_ITER it,previous_it;
struct gbox_card *card_s;
struct gbox_card *card;
if(!(gbox_decode_cmd(data) == MSG_HELLO1))
{
gbox_decompress(data, &payload_len);
}
cs_ddump_mask(D_READER, data, payload_len, "gbox: decompressed data (%d bytes):", payload_len);
if((data[0xB] & 0xF) == 0) //is first packet
{
if(!peer->gbox.cards)
{ peer->gbox.cards = ll_create("peer.cards"); }
it = ll_iter_create(peer->gbox.cards);
checkcode_len = 7;
hostname_len = data[payload_len - 1];
footer_len = hostname_len + 2;
}
else
{ it = peer->last_it; }
if(gbox_decode_cmd(data) == MSG_HELLO1)
{ ptr = data + 11; }
else
{ ptr = data + 12; }
while(ptr < data + payload_len - footer_len - checkcode_len - 1)
{
uint16_t caid;
uint32_t provid;
uint32_t provid1;
switch(ptr[0])
{
//Viaccess
case 0x05:
caid = ptr[0] << 8;
provid = ptr[1] << 16 | ptr[2] << 8 | ptr[3];
break;
//Cryptoworks
case 0x0D:
caid = ptr[0] << 8 | ptr[1];
provid = ptr[2];
break;
default:
caid = ptr[0] << 8 | ptr[1];
provid = ptr[2] << 8 | ptr[3];
break;
}
ncards_in_msg += ptr[4];
//caid check
if(chk_ctab(caid, &cli->reader->ctab))
{
provid1 = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
current_ptr = ptr;
ptr += 5;
// for all cards of current caid/provid,
while (ptr < current_ptr + 5 + current_ptr[4] * 4)
{
previous_it = it;
card_s = ll_iter_next(&it);
switch (get_card_action(card_s,provid1,ptr[2] << 8 | ptr[3],ptr[0],peer))
{
case -1:
//IDEA: Later put card to a list of temporary not available cards
//reason: not loose good/bad sids
//can be later removed by daily garbage collector for example
cs_debug_mask(D_READER, "delete card: caid=%04X, provid=%06X, slot=%d, level=%d, dist=%d, peer=%04X",
card_s->caid, card_s->provid, card_s->slot, card_s->lvl, card_s->dist, card_s->peer_id);
//delete card because not send anymore
ll_iter_remove(&it);
gbox_free_card(card_s);
break;
case 0:
ptr += 4;
break;
case 1:
// create card info from data and add card to peer.cards
if(!cs_malloc(&card, sizeof(struct gbox_card)))
{ continue; }
card->caid = caid;
card->provid = provid;
card->provid_1 = provid1;
card->slot = ptr[0];
card->dist = ptr[1] & 0xf;
card->lvl = ptr[1] >> 4;
card->peer_id = ptr[2] << 8 | ptr[3];
card->badsids = ll_create("badsids");
card->goodsids = ll_create("goodsids");
card->no_cws_returned = 0;
card->average_cw_time = 0;
if (!card_s)
{ ll_append(peer->gbox.cards, card); }
else
{
ll_iter_insert(&previous_it, card);
it = previous_it;
}
ll_iter_next(&it);
cs_debug_mask(D_READER, "new card: caid=%04X, provid=%06X, slot=%d, level=%d, dist=%d, peer=%04X",
card->caid, card->provid, card->slot, card->lvl, card->dist, card->peer_id);
ptr += 4;
break;
default:
break;
} //switch
} // end while cards for provider
}
else
{
ptr += 5 + ptr[4] * 4; //skip cards because caid
}
} // end while caid/provid
if(!(data[0x0B] & 0xF)) // first packet. We've got peer hostname
{
NULLFREE(peer->hostname);
if(!cs_malloc(&peer->hostname, hostname_len + 1))
{
cs_writeunlock(&peer->lock);
return -1;
}
memcpy(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len);
peer->hostname[hostname_len] = '\0';
gbox_checkcode_recv(cli, data + payload_len - footer_len - checkcode_len - 1);
peer->gbox.minor_version = data[payload_len - footer_len - 1];
peer->gbox.cpu_api = data[payload_len - footer_len];
} // end if first hello packet
uchar tmpbuf[8];
memset(&tmpbuf[0], 0xff, 7);
if(data[10] == 0x01 && data[11] == 0x80 && !memcmp(data+12,tmpbuf,7)) //good night message
{
cs_log("->[gbx] received Good Night from %s %s",username(cli), cli->reader->device);
write_goodnight_to_osd_file(cli);
}
//This is a good night / reset packet (good night data[0xA] / reset !data[0xA]
if((data[0x0B] & 0x8F) == 0x80 && !ncards_in_msg) //first + last packet with no cards
{
gbox_free_cardlist(peer->gbox.cards);
peer->online = 0;
peer->hello_stat = GBOX_STAT_HELLOL;
cli->reader->tcp_connected = 0;
cli->reader->card_status = NO_CARD;
cli->reader->last_s = cli->reader->last_g = 0;
peer->gbox.cards = ll_create("peer.cards");
}
if(data[0x0B] & 0x80) //last packet
{
//delete cards at the end of the list if there are some
while ((card_s = ll_iter_next(&it)))
{
cs_debug_mask(D_READER, "delete card: caid=%04X, provid=%06X, slot=%d, level=%d, dist=%d, peer=%04X",
card_s->caid, card_s->provid, card_s->slot, card_s->lvl, card_s->dist, card_s->peer_id);
//delete card because not send anymore
ll_iter_remove(&it);
gbox_free_card(card_s);
}
peer->online = 1;
if(!data[0xA])
{
cs_log("<-HelloS in %d packets from %s (%s:%d) V2.%02X with %d cards filtered to %d cards", (data[0x0B] & 0x0f)+1, cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port, peer->gbox.minor_version, ncards_in_msg,ll_count(peer->gbox.cards));
peer->hello_stat = GBOX_STAT_HELLOR;
gbox_send_hello(cli);
}
else
{
cs_log("<-HelloR in %d packets from %s (%s:%d) V2.%02X with %d cards filtered to %d cards", (data[0x0B] & 0x0f)+1, cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port, peer->gbox.minor_version, ncards_in_msg,ll_count(peer->gbox.cards));
gbox_send_checkcode(cli);
}
if(peer->hello_stat == GBOX_STAT_HELLOS)
{
gbox_send_hello(cli);
}
cli->reader->tcp_connected = 2; //we have card
cli->reader->card_status = CARD_INSERTED;
if(ll_count(peer->gbox.cards) == 0)
{ cli->reader->card_status = NO_CARD; }
gbox_write_local_cards_info();
gbox_write_shared_cards_info();
gbox_write_peer_onl();
}
peer->last_it = it; //save position for next hello
return 0;
}
static int8_t is_blocked_peer(uint16_t peer)
{
if (peer == NO_GBOX_ID) { return 1; }
else { return 0; }
}
static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n)
{
struct gbox_peer *peer;
struct s_client *cl;
int32_t diffcheck = 0;
peer = cli->gbox;
if (!peer || !peer->my_user) { return -1; }
cl = peer->my_user;
// No ECMs with length < MIN_LENGTH expected
if ((((data[19] & 0x0f) << 8) | data[20]) < MIN_ECM_LENGTH) { return -1; }
// GBOX_MAX_HOPS not violated
if (data[n - 15] + 1 > GBOX_MAXHOPS) { return -1; }
//Check for blocked peers
uint16_t requesting_peer = data[(((data[19] & 0x0f) << 8) | data[20]) + 21] << 8 |
data[(((data[19] & 0x0f) << 8) | data[20]) + 22];
if (is_blocked_peer(requesting_peer))
{
cs_debug_mask(D_READER, "ECM from peer %04X blocked", requesting_peer);
return -1;
}
ECM_REQUEST *er;
if(!(er = get_ecmtask())) { return -1; }
struct gbox_ecm_request_ext *ere;
if(!cs_malloc(&ere, sizeof(struct gbox_ecm_request_ext)))
{
cs_writeunlock(&peer->lock);
return -1;
}
uchar *ecm = data + 18; //offset of ECM in gbx message
er->src_data = ere;
gbox_init_ecm_request_ext(ere);
peer->gbox_count_ecm++;
er->gbox_ecm_id = peer->gbox.id;
if(peer->ecm_idx == 100) { peer->ecm_idx = 0; }
er->idx = peer->ecm_idx++;
er->ecmlen = (((ecm[1] & 0x0f) << 8) | ecm[2]) + 3;
er->pid = data[10] << 8 | data[11];
er->srvid = data[12] << 8 | data[13];
if(ecm[er->ecmlen + 5] == 0x05)
{ er->caid = (ecm[er->ecmlen + 5] << 8); }
else
{ er->caid = (ecm[er->ecmlen + 5] << 8 | ecm[er->ecmlen + 6]); }
// ei->extra = data[14] << 8 | data[15];
memcpy(er->ecm, data + 18, er->ecmlen);
ere->gbox_peer = ecm[er->ecmlen] << 8 | ecm[er->ecmlen + 1];
ere->gbox_version = ecm[er->ecmlen + 2];
ere->gbox_unknown = ecm[er->ecmlen + 3];
ere->gbox_type = ecm[er->ecmlen + 4];
ere->gbox_caid = ecm[er->ecmlen + 5] << 8 | ecm[er->ecmlen + 6];
ere->gbox_prid = ecm[er->ecmlen + 7] << 8 | ecm[er->ecmlen + 8];
ere->gbox_mypeer = ecm[er->ecmlen + 10] << 8 | ecm[er->ecmlen + 11];
ere->gbox_slot = ecm[er->ecmlen + 12];
diffcheck = gbox_checkcode_recv(cl, data + n - 14);
//TODO: What do we do with our own checkcode @-7?
er->gbox_crc = gbox_get_ecmchecksum(er);
ere->gbox_hops = data[n - 15] + 1;
memcpy(&ere->gbox_routing_info[0], &data[n - 15 - ere->gbox_hops + 1], ere->gbox_hops - 1);
er->prid = chk_provid(er->ecm, er->caid);
cs_debug_mask(D_READER, "<- ECM (%d<-) from server (%s:%d) to cardserver (%04X) SID %04X", ere->gbox_hops, peer->hostname, cli->port, ere->gbox_peer, er->srvid);
get_cw(cl, er);
//checkcode did not match gbox->peer checkcode
if(diffcheck)
{
// TODO: Send HelloS here?
// gbox->peer.hello_stat = GBOX_STAT_HELLOS;
// gbox_send_hello(cli);
}
return 0;
}
int32_t gbox_cmd_switch(struct s_client *cli, uchar *data, int32_t n)
{
int32_t n1 = 0, rc1 = 0, i1, idx;
uchar dcw[16];
switch(gbox_decode_cmd(data))
{
case MSG_BOXINFO:
gbox_send_hello(cli);
break;
case MSG_GOODBYE:
cs_log("->[gbx] received goodbye message from %s",username(cli));
//needfix what to do after Goodbye?
//suspect: we get goodbye as signal of SID not found
break;
case MSG_UNKNWN:
cs_log("->[gbx] received MSG_UNKNWN 48F9 from %s", username(cli));
break;
case MSG_GSMS_1:
if (!cfg.gsms_dis)
{
cs_log("->[gbx] received MSG_GSMS_1 from %s", username(cli));
gbox_send_gsms_ack(cli,1);
write_gsms_msg(cli, data +4, data[3], data[2]);
}
else
{
gsms_unavail();
}
break;
case MSG_GSMS_2:
if (!cfg.gsms_dis)
{
cs_log("->[gbx] received MSG_GSMS_2 from %s", username(cli));
gbox_send_gsms_ack(cli,2);
write_gsms_msg(cli, data +16, data[14], data[15]);
}
else
{
gsms_unavail();
}
break;
case MSG_GSMS_ACK_1:
if (!cfg.gsms_dis)
{
cs_log("->[gbx] received MSG_GSMS_ACK_1 from %s", username(cli));
write_gsms_ack(cli,1);
}
else
{
gsms_unavail();
}
break;
case MSG_GSMS_ACK_2:
if (!cfg.gsms_dis)
{
cs_log("->[gbx] received MSG_GSMS_ACK_2 from %s", username(cli));
write_gsms_ack(cli,2);
}
else
{
gsms_unavail();
}
break;
case MSG_HELLO1:
case MSG_HELLO:
if(gbox_cmd_hello(cli, data, n) < 0)
{ return -1; }
break;
case MSG_CW:
cli->last = time((time_t *)0);
idx = gbox_recv_chk(cli, dcw, &rc1, data, n);
if(idx < 0) { break; } // no dcw received
if(!idx) { idx = cli->last_idx; }
cli->reader->last_g = time((time_t *)0); // for reconnect timeout
for(i1 = 0, n1 = 0; i1 < cfg.max_pending && n1 == 0; i1++)
{
if(cli->ecmtask[i1].idx == idx)
{
cli->pending--;
casc_check_dcw(cli->reader, i1, rc1, dcw);
n1++;
}
}
break;
case MSG_CHECKCODE:
gbox_checkcode_recv(cli, data + 10);
break;
case MSG_ECM:
{
gbox_incoming_ecm(cli, data, n);
break;
}
default:
cs_ddump_mask(D_READER, data, n, "gbox: unknown data received (%d bytes):", n);
} // end switch
if ((time(NULL) - last_stats_written) > STATS_WRITE_TIME)
{
gbox_write_stats();
last_stats_written = time(NULL);
}
return 0;
}
static int8_t gbox_check_header(struct s_client *cli, uchar *data, int32_t l)
{
struct s_client *cl = switch_client_proxy(cli, cli->gbox_peer_id);
//clients may timeout - attach to peer's gbox/reader
cli->gbox = cl->gbox; //point to the same gbox as proxy
cli->reader = cl->reader; //point to the same reader as proxy
struct gbox_peer *peer = cl->gbox;
char tmp[0x50];
int32_t n = l;
cs_ddump_mask(D_READER, data, n, "gbox: encrypted data received (%d bytes):", n);
if(gbox_decode_cmd(data) == MSG_HELLO1)
{ cs_log("test cs2gbox"); }
else
{ gbox_decrypt(data, n, local_gbox.password); }