-
Notifications
You must be signed in to change notification settings - Fork 16
/
module-gbox.c
2767 lines (2383 loc) · 73.3 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
#define MODULE_LOG_PREFIX "gbox"
#include "globals.h"
#ifdef MODULE_GBOX
#include "module-gbox.h"
#include "module-gbox-helper.h"
#include "module-gbox-sms.h"
#include "module-gbox-cards.h"
#include "module-cccam.h"
#include "module-cccam-data.h"
#include "ncam-failban.h"
#include "ncam-client.h"
#include "ncam-ecm.h"
#include "ncam-lock.h"
#include "ncam-net.h"
#include "ncam-chk.h"
#include "ncam-string.h"
#include "ncam-time.h"
#include "ncam-reader.h"
#include "ncam-files.h"
#include "module-gbox-remm.h"
#include "module-dvbapi.h"
#include "ncam-work.h"
static struct gbox_data local_gbox;
static int8_t local_gbox_initialized = 0;
static uint8_t local_cards_initialized = 0;
uint8_t local_gbx_rev = 0x30;
uint32_t startup = 0;
static uint32_t gbox_add_local_cards(void);
static int32_t gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er);
void start_gbx_ticker(void);
char *get_gbox_tmp_fname(char *fext)
{
static char gbox_tmpfile_buf[128];
memset(gbox_tmpfile_buf, 0, sizeof(gbox_tmpfile_buf));
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[cs_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;
}
static uint8_t gbox_get_my_cpu_api (void)
{
return(cfg.gbox_my_cpu_api);
}
static void write_attack_file (struct s_client *cli, uint8_t txt_id, uint16_t rcvd_id)
{
if (cfg.dis_attack_txt) {return;}
char tsbuf[28];
time_t walltime = cs_time();
cs_ctime_r(&walltime, tsbuf);
char *fext= FILE_ATTACK_INFO;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "a");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
if(txt_id == GBOX_ATTACK_UNKWN_HDR)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - peer sends unknown Header CMD - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
if(txt_id == GBOX_ATTACK_LOCAL_PW)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - peer sends wrong local password - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
if(txt_id == GBOX_ATTACK_PEER_IGNORE)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - peer ignored by conf - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
if(txt_id == GBOX_ATTACK_PEER_PW)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - peer sends unknown peer password - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
if(txt_id == GBOX_ATTACK_AUTH_FAIL)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - authentification failed - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
if(txt_id == GBOX_ATTACK_ECM_BLOCKED)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - ECM is blocked - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
if(txt_id == GBOX_ATTACK_REMM_REQ_BLOCKED)
{
fprintf(fhandle, "ATTACK ALERT FROM %04X %s - unaccepted peer sent REMM REQ - %s",
rcvd_id, cs_inet_ntoa(cli->ip), tsbuf);
}
fclose(fhandle);
return;
}
void write_msg_info(struct s_client *cli, uint8_t msg_id, uint8_t txt_id, uint16_t misc)
{
if (msg_id == MSGID_GSMS && misc == 0x31) {return;}
char *fext= FILE_MSG_INFO;
char *fname = get_gbox_tmp_fname(fext);
if (file_exists(fname))
{
char buf[120];
memset(buf, 0, sizeof(buf));
if (msg_id == MSGID_ATTACK)
{
snprintf(buf, sizeof(buf), "%s %d %04X %d %s %d",
fname, msg_id, misc, 0, cs_inet_ntoa(cli->ip), txt_id);
cs_log_dbg(D_READER, "found driver %s - write msg (msg_id = %d - txt-id = %d) Attack Alert from %s %04X",
fname, msg_id, txt_id, cs_inet_ntoa(cli->ip), misc);
}
else
{
snprintf(buf, sizeof(buf), "%.24s %d %.24s %.24s %s %d",
fname, msg_id, username(cli), cli->reader->device, cs_inet_ntoa(cli->ip), misc);
cs_log_dbg(D_READER, "found driver %s - write msg (id = %d) related to %s %s",
fname, msg_id, username(cli),cli->reader->device);
}
char *cmd = buf;
FILE *p;
if((p = popen(cmd, "w")) == NULL)
{
cs_log("Error popen: %s",fname);
return;
}
if(pclose(p) == -1)
{
cs_log("Error pclose(): %s",fname);
return;
}
}
return;
}
void handle_attack(struct s_client *cli, uint8_t txt_id, uint16_t rcvd_id)
{
write_attack_file(cli, txt_id, rcvd_id);
write_msg_info(cli, MSGID_ATTACK, txt_id, rcvd_id);
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;
}
cs_readlock(__func__, &clientlist_lock);
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 %s\n", cl->reader->device,
cs_inet_ntoa(cl->ip), peer->gbox.id, peer->gbox.minor_version, cl->reader->description ? cl->reader->description : "");
if (!peer->onlinestat)
{
peer->onlinestat = 1;
cs_log("comeONLINE: %s %s boxid: %04X (%s) v2.%02X cards:%d", cl->reader->device,
cs_inet_ntoa(cl->ip), peer->gbox.id, cl->reader->description ? cl->reader->description : "-", peer->gbox.minor_version, peer->filtered_cards);
write_msg_info(cl, MSGID_COMEONLINE, 0, peer->filtered_cards);
}
}
else
{
fprintf(fhandle, "0 %s %s %04X 0.00 %s\n", cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id, cl->reader->description ? cl->reader->description : "");
if (peer->onlinestat)
{
peer->onlinestat = 0;
cs_log("goneOFFLINE: %s %s boxid: %04X (%s)",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id, cl->reader->description ? cl->reader->description : "-");
write_msg_info(cl, MSGID_GONEOFFLINE, 0, 0);
}
}
}
}
cs_readunlock(__func__, &clientlist_lock);
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", get_gbox_tmp_fname(FILE_GBOX_VERSION), strerror(errno));
return;
}
fprintf(fhandle, "%02X.%02X my-id: %04X rev: %01X.%01X\n", LOCAL_GBOX_MAJOR_VERSION, cfg.gbox_my_vers, local_gbox.id, local_gbx_rev >> 4, local_gbx_rev & 0xf);
fclose(fhandle);
}
void hostname2ip(char *hostname, IN_ADDR_T *ip)
{
cs_resolve(hostname, ip, NULL, NULL);
}
uint16_t gbox_convert_password_to_id(uint32_t password)
{
return (((password >> 24) & 0xff) ^ ((password >> 8) & 0xff)) << 8 | (((password >> 16) & 0xff) ^ (password & 0xff));
}
static int8_t gbox_remove_all_bad_sids(ECM_REQUEST *er, uint16_t sid)
{
if (!er)
{
return -1;
}
struct gbox_card_pending *pending = NULL;
LL_LOCKITER *li = ll_li_create(er->gbox_cards_pending, 0);
while ((pending = ll_li_next(li)))
{
gbox_remove_bad_sid(pending->id.peer, pending->id.slot, sid);
}
ll_li_destroy(li);
return 0;
}
void gbox_free_cards_pending(ECM_REQUEST *er)
{
ll_destroy_free_data(&er->gbox_cards_pending);
}
void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere)
{
ere->gbox_slot = 0;
ere->gbox_version = 0;
ere->gbox_rev = 0;
ere->gbox_type = 0;
}
struct s_client *get_gbox_proxy(uint16_t gbox_id)
{
struct s_client *cl;
struct s_client *found = NULL;
cs_readlock(__func__, &clientlist_lock);
for(cl = first_client; cl; cl = cl->next)
{
if(cl->typ == 'p' && cl->gbox && cl->gbox_peer_id == gbox_id)
{
found = cl;
break;
}
}
cs_readunlock(__func__, &clientlist_lock);
return found;
}
void remove_peer_crd_file(struct s_client *proxy)
{
char buff[64];
snprintf(buff, sizeof(buff),"cards_to_%.24s", proxy->reader->label);
char *fname = get_gbox_tmp_fname(buff);
if(file_exists(fname))
{
if(unlink(fname) < 0)
{
cs_log("Error removing peer_crd_file %s (errno=%d %s)!", fname, errno, strerror(errno));
}
}
}
static int8_t gbox_peer_online(struct gbox_peer *peer, uint8_t online)
{
if (!peer) { return -1; }
peer->online = online;
gbox_write_peer_onl();
return 0;
}
static int8_t gbox_clear_peer(struct gbox_peer *peer)
{
if (!peer)
{
return -1;
}
peer->ecm_idx = 0;
peer->next_hello = 0;
peer->authstat = 0;
gbox_delete_cards(GBOX_DELETE_FROM_PEER, peer->gbox.id);
gbox_peer_online(peer, GBOX_PEER_OFFLINE);
return 0;
}
static int8_t gbox_reinit_proxy(struct s_client *proxy)
{
if (!proxy)
{
return -1;
}
struct gbox_peer *peer = proxy->gbox;
gbox_clear_peer(peer);
if (!proxy->reader)
{
return -1;
}
remove_peer_crd_file(proxy);
proxy->reader->tcp_connected = 0;
proxy->reader->card_status = CARD_NEED_INIT;
proxy->reader->last_s = proxy->reader->last_g = 0;
return 0;
}
//gbox.net doesn't accept slots >18
static uint8_t calc_slot(uint8_t slot)
{
int8_t i;
uint8_t cslot = slot;
for(i=0 ; i < 9 ; i++)
{
if(slot > i*18)
{
cslot = slot - i*18;
}
}
return cslot;
}
uint16_t count_send_cards(struct s_client *proxy)
{
uint16_t nbcards = 0;
struct gbox_peer *peer = proxy->gbox;
struct gbox_card *card;
if(gbox_count_cards() > 0)
{
GBOX_CARDS_ITER *gci = gbox_cards_iter_create();
while((card = gbox_cards_iter_next(gci)))
{
if(chk_ctab(gbox_get_caid(card->caprovid), &peer->my_user->account->ctab) && (card->lvl > 0) &&
#ifdef MODULE_CCCAM
(card->dist <= peer->my_user->account->cccmaxhops) &&
#endif
(!card->origin_peer || (card->origin_peer && card->origin_peer->gbox.id != peer->gbox.id)))
{
if(card->type == GBOX_CARD_TYPE_GBOX)
{
nbcards++;
continue;
}
else if(card->type == GBOX_CARD_TYPE_CCCAM) //&& cfg.cc_gbx_reshare_en
{
if(proxy->reader->gbox_cccam_reshare < 0)
{ continue; }
else
{
if(chk_ident_filter(gbox_get_caid(card->caprovid), gbox_get_provid(card->caprovid), &proxy->reader->ccc_gbx_reshare_ident))
{
nbcards++;
continue;
}
}
}
else if(card->type == GBOX_CARD_TYPE_LOCAL || card->type == GBOX_CARD_TYPE_BETUN || card->type == GBOX_CARD_TYPE_PROXY)
{
if(proxy->reader->gbox_reshare > 0)
{
nbcards++;
continue;
}
else
{
continue;
}
}
if(nbcards == MAX_GBOX_CARDS )
{
break;
}
}
} // while cards exist
gbox_cards_iter_destroy(gci);
}
return nbcards;
}
void gbox_send(struct s_client *cli, uint8_t *buf, int32_t l)
{
struct gbox_peer *peer = cli->gbox;
cs_log_dump_dbg(D_READER, buf, l, "<- data to %s (%d bytes):", cli->reader->label, l);
hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)cli->reader->r_port);
gbox_encrypt(buf, l, peer->gbox.password);
sendto(cli->udp_fd, buf, l, 0, (struct sockaddr *)&cli->udp_sa, cli->udp_sa_len);
cs_log_dump_dbg(D_READER, buf, l, "<- encrypted data to %s (%d bytes):", cli->reader->label, l);
}
void gbox_send_hello_packet(struct s_client *cli, int8_t packet, uint8_t *outbuf, uint8_t *ptr, int32_t nbcards, uint8_t hello_stat)
{
struct gbox_peer *peer = cli->gbox;
int32_t hostname_len = cs_strlen(cfg.gbox_hostname);
int32_t len;
gbox_message_header(outbuf, MSG_HELLO, peer->gbox.password, local_gbox.password);
if(hello_stat > GBOX_STAT_HELLOS) // hello_stat == HelloR
{
outbuf[10] = 1;
}
else
{
outbuf[10] = 0;
}
outbuf[11] = packet;
if((packet & 0x0F) == 0) // first packet
{
memcpy(++ptr, gbox_get_my_checkcode(), 7);
ptr += 7;
*ptr = local_gbox.minor_version;
*(++ptr) = local_gbox.cpu_api;
memcpy(++ptr, cfg.gbox_hostname, hostname_len);
ptr += hostname_len;
*ptr = hostname_len;
}
len = ptr - outbuf + 1;
switch(hello_stat)
{
case GBOX_STAT_HELLOL:
if(cfg.log_hello)
{ cs_log("<- HelloL to %s", cli->reader->label); }
else
{ cs_log_dbg(D_READER,"<- HelloL to %s", cli->reader->label); }
break;
case GBOX_STAT_HELLOS:
if(cfg.log_hello)
{ cs_log("<- HelloS #%d total cards %d to %s", (packet & 0xf) +1, nbcards, cli->reader->label); }
else
{ cs_log_dbg(D_READER,"<- HelloS #%d total cards %d to %s", (packet & 0xf) +1, nbcards, cli->reader->label); }
break;
case GBOX_STAT_HELLOR:
if(cfg.log_hello)
{ cs_log("<- HelloR #%d total cards %d to %s", (packet & 0xf) +1, nbcards, cli->reader->label); }
else
{ cs_log_dbg(D_READER,"<- HelloR #%d total cards %d to %s", (packet & 0xf) +1, nbcards, cli->reader->label); }
break;
default:
if(cfg.log_hello)
{ cs_log("<- hello #%d total cards %d to %s", (packet & 0xf) +1, nbcards, cli->reader->label); }
else
{ cs_log_dbg(D_READER,"<- hello #%d total cards %d to %s", (packet & 0xf) +1, nbcards, cli->reader->label); }
break;
}
cs_log_dump_dbg(D_READER, outbuf, len, "<- hello #%d to %s, (len=%d):", (packet & 0xf) +1, cli->reader->label, len);
gbox_compress(outbuf, len, &len);
gbox_send(cli, outbuf, len);
}
void gbox_send_hello(struct s_client *proxy, uint8_t hello_stat)
{
if(!proxy)
{
cs_log("ERROR: Invalid proxy try to call 'gbox_send_hello'");
return;
}
struct gbox_peer *peer = proxy->gbox;
if(!peer)
{
cs_log("ERROR: Invalid peer try to call 'gbox_send_hello'");
return;
}
if(hello_stat > GBOX_STAT_HELLOL && (!peer->my_user || !peer->my_user->account))
{
cs_log("ERROR: Invalid peer try to call 'gbox_send_hello'");
return;
}
uint16_t sendcrds = 0;
uint16_t nbcards = 0;
uint16_t nbcards_cnt = 0;
uint8_t packet = 0;
uint8_t buf[1024];
uint8_t *ptr = buf + 11;
struct gbox_card *card;
memset(buf, 0, sizeof(buf));
if(gbox_count_cards() > 0)
{
if(hello_stat > GBOX_STAT_HELLOL)
{
uint16_t nb_send_cards = count_send_cards(proxy);
char buff[64];
snprintf(buff, sizeof(buff),"cards_to_%.24s", proxy->reader->label);
char *fname = get_gbox_tmp_fname(buff);
FILE *fhandle = fopen(fname, "w");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
fprintf(fhandle, "Cards forwarded to peer %04X - %s\n\n", peer->gbox.id, proxy->reader->label);
GBOX_CARDS_ITER *gci = gbox_cards_iter_create();
while((card = gbox_cards_iter_next(gci)))
{
//send to user only cards which matching CAID from account and lvl > 0
//and cccmaxhops from account
//do not send peer cards back
if(chk_ctab(gbox_get_caid(card->caprovid), &peer->my_user->account->ctab) && (card->lvl > 0) &&
#ifdef MODULE_CCCAM
(card->dist <= peer->my_user->account->cccmaxhops) &&
#endif
(!card->origin_peer || (card->origin_peer && card->origin_peer->gbox.id != peer->gbox.id)))
{
if(card->type == GBOX_CARD_TYPE_GBOX)
{
// cs_log_dbg(D_READER,"send to peer gbox-card %04X - level=%d crd-owner=%04X", card->caprovid >> 16, card->lvl, card->id.peer);
*(++ptr) = card->caprovid >> 24;
*(++ptr) = card->caprovid >> 16;
*(++ptr) = card->caprovid >> 8;
*(++ptr) = card->caprovid & 0xff;
*(++ptr) = 1; // note: original gbx is more efficient and sends all cards of one caid as package
*(++ptr) = calc_slot(card->id.slot);
*(++ptr) = ((card->lvl - 1) << 4) + card->dist + 1;
fprintf(fhandle, "#%03d Peer Crd to %04X - crd %08X - level %d - dist %d - slot %02d - crd owner %04X\n", ++sendcrds, peer->gbox.id, card->caprovid, card->lvl -1, card->dist + 1, calc_slot(card->id.slot), card->id.peer);
}
else if(card->type == GBOX_CARD_TYPE_CCCAM)
{
if(proxy->reader->gbox_cccam_reshare < 0)
{ continue; }
else
{
if(chk_ident_filter(gbox_get_caid(card->caprovid), gbox_get_provid(card->caprovid), &proxy->reader->ccc_gbx_reshare_ident))
{
if(proxy->reader->gbox_cccam_reshare > proxy->reader->gbox_reshare)
{
proxy->reader->gbox_cccam_reshare = proxy->reader->gbox_reshare;
}
// cs_log_dbg(D_READER,"send to peer %04X - ccc-card %04X - level=%d crd-owner=%04X", peer->gbox.id, card->caprovid >> 16, proxy->reader->gbox_cccam_reshare, card->id.peer);
*(++ptr) = card->caprovid >> 24;
*(++ptr) = card->caprovid >> 16;
*(++ptr) = card->caprovid >> 8;
*(++ptr) = card->caprovid & 0xff;
*(++ptr) = 1;
*(++ptr) = calc_slot(card->id.slot);
*(++ptr) = ((proxy->reader->gbox_cccam_reshare) << 4) + card->dist + 1;
fprintf(fhandle, "#%03d CCCM crd to %04X - crd %08X - level %d - dist %d - slot %02d - crd owner %04X\n", ++sendcrds, peer->gbox.id, card->caprovid, proxy->reader->gbox_cccam_reshare, card->dist + 1, calc_slot(card->id.slot), card->id.peer);
}
else
{ continue; }
}
}
else if(card->type == GBOX_CARD_TYPE_LOCAL || card->type == GBOX_CARD_TYPE_BETUN || card->type == GBOX_CARD_TYPE_PROXY)
{
if(proxy->reader->gbox_reshare > 0)
{
//cs_log_dbg(D_READER,"send local crd %04X reshare=%d crd-owner=%04X", card->caprovid >> 16, proxy->reader->gbox_reshare, card->id.peer);
*(++ptr) = card->caprovid >> 24;
*(++ptr) = card->caprovid >> 16;
*(++ptr) = card->caprovid >> 8;
*(++ptr) = card->caprovid & 0xff;
*(++ptr) = 1;
*(++ptr) = calc_slot(card->id.slot);
*(++ptr) = ((proxy->reader->gbox_reshare - 1) << 4) + card->dist + 1;
fprintf(fhandle, "#%03d Locl Crd to %04X - crd %08X - level %d - dist %d - slot %02d - crd owner %04X\n", ++sendcrds, peer->gbox.id, card->caprovid, proxy->reader->gbox_reshare - 1, card->dist + 1, calc_slot(card->id.slot), card->id.peer);
}
else
{
cs_log_dbg(D_READER,"WARNING: local card %04X NOT be shared - !! reshare=%d !! crd-owner=%04X", card->caprovid >> 16, proxy->reader->gbox_reshare, card->id.peer);
continue;
}
}
*(++ptr) = card->id.peer >> 8;
*(++ptr) = card->id.peer & 0xff;
nbcards++;
nbcards_cnt++;
if(nbcards_cnt == MAX_GBOX_CARDS)
{
cs_log("max card limit [%d] send to peer %04X is exceeded", MAX_GBOX_CARDS, peer->gbox.id);
break;
}
if(nbcards_cnt == nb_send_cards)
{
break;
}
if(nbcards == 74)
{
gbox_send_hello_packet(proxy, packet, buf, ptr, nbcards, hello_stat);
packet++;
nbcards = 0;
ptr = buf + 11;
memset(buf, 0, sizeof(buf));
}
}
} // while cards exist
gbox_cards_iter_destroy(gci);
fclose(fhandle);
} // end if > HelloL
else
{
GBOX_CARDS_ITER *gci = gbox_cards_iter_create();
while((card = gbox_cards_iter_next(gci)))
{
if(card->lvl > 0 && card->type != GBOX_CARD_TYPE_CCCAM && card->type != GBOX_CARD_TYPE_GBOX)
{
if(proxy->reader->gbox_reshare > 0)
{
*(++ptr) = card->caprovid >> 24;
*(++ptr) = card->caprovid >> 16;
*(++ptr) = card->caprovid >> 8;
*(++ptr) = card->caprovid & 0xff;
*(++ptr) = 1;
*(++ptr) = calc_slot(card->id.slot);
*(++ptr) = ((proxy->reader->gbox_reshare - 1) << 4) + card->dist + 1;
*(++ptr) = card->id.peer >> 8;
*(++ptr) = card->id.peer & 0xff;
}
nbcards++;
if(nbcards >= GBOX_MAX_LOCAL_CARDS )
{
cs_log("gbox_send_HelloL - local crds = %d - max allowed = %d ", nbcards, GBOX_MAX_LOCAL_CARDS);
break;
}
}
} // end while local cards exist
gbox_cards_iter_destroy(gci);
} // end if HelloL
}// end if gbox_count_cards > 0
gbox_send_hello_packet(proxy, 0x80 | packet, buf, ptr, nbcards, hello_stat); //last packet has bit 0x80 set
}
void gbox_reconnect_peer(struct s_client *cl)
{
struct gbox_peer *peer = cl->gbox;
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));
gbox_reinit_proxy(cl);
cs_log("reconnect %s peer: %04X", username(cl), peer->gbox.id);
gbox_send_hello(cl, GBOX_STAT_HELLOS);
return;
}
void restart_gbox_peer(char *rdrlabel, uint8_t allrdr, uint16_t gbox_id)
{
struct s_client *cl;
cs_readlock(__func__, &clientlist_lock);
for(cl = first_client; cl; cl = cl->next)
{
if(cl->gbox && cl->typ == 'p' &&
((rdrlabel && !strcmp(rdrlabel, cl->reader->label)) ||
allrdr || (gbox_id && cl->gbox_peer_id == gbox_id)))
{ gbox_reconnect_peer(cl); }
}
cs_readunlock(__func__, &clientlist_lock);
}
static void *gbox_server(struct s_client *cli, uint8_t *UNUSED(b), int32_t l)
{
if(l > 0)
{
cs_log("gbox_server %s/%d", cli->reader->label, cli->port);
//gbox_check_header_recvd(cli, NULL, b, l);
}
return NULL;
}
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;
cs_writelock(__func__, &clientlist_lock);
for(cl = first_client; cl; cl = cl->next)
{
if(cl->typ == 'c' && cl->gbox_peer_id == cli->gbox_peer_id && cl != cli)
{
cl->reader = NULL;
cl->gbox = NULL;
cs_log_dbg(D_READER, "disconnected double client %s - %s", username(cl), cs_inet_ntoa(cli->ip));
//cs_log("disconnected double client %s - %s",username(cl), cs_inet_ntoa(cli->ip));
cs_disconnect_client(cl);
}
}
cs_writeunlock(__func__, &clientlist_lock);
return 0;
}
static int8_t gbox_auth_client(struct s_client *cli, uint32_t gbox_password)
{
if(!cli) { return -1; }
uint16_t gbox_id = gbox_convert_password_to_id(gbox_password);
struct s_client *cl = get_gbox_proxy(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 ((peer->gbox.password == 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);
peer->my_user = cli;
return 0;
}
}
return -1;
}
static void gbox_server_init(struct s_client *cl)
{
cs_writelock(__func__, &clientlist_lock);
if(!cl->init_done)
{
if(IP_ISSET(cl->ip))
{
cs_log("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;
cl->last = time((time_t *)0);
start_gbx_ticker();
}
cs_writeunlock(__func__, &clientlist_lock);
return;
}
static uint16_t gbox_decode_cmd(uint8_t *buf)
{
return buf[0] << 8 | buf[1];
}
int8_t gbox_message_header(uint8_t *buf, uint16_t cmd, uint32_t peer_password, uint32_t local_password)
{
if (!buf) { return -1; }
i2b_buf(2, cmd, buf);
i2b_buf(4, peer_password, buf + 2);
if (cmd == MSG_CW) { return 0; }
i2b_buf(4, local_password, buf + 6);
return 0;
}
// returns number of cards in a hello packet or -1 in case of error
int16_t read_cards_from_hello(uint8_t *ptr, uint8_t *len, CAIDTAB *ctab, uint8_t maxdist, struct gbox_peer *peer)
{
uint8_t *current_ptr = 0;
uint32_t caprovid;
int16_t ncards_in_msg = 0;
while(ptr < len)
{
caprovid = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
ncards_in_msg += ptr[4];
//caid check
if(chk_ctab(gbox_get_caid(caprovid), ctab))
{
current_ptr = ptr;
ptr += 5;
// for all cards of current caid/provid,
while (ptr < current_ptr + 5 + current_ptr[4] * 4)
{
if ((ptr[1] & 0xf) <= maxdist)
{
gbox_add_card(ptr[2] << 8 | ptr[3], caprovid, ptr[0], ptr[1] >> 4, ptr[1] & 0xf, GBOX_CARD_TYPE_GBOX, peer);
}
ptr += 4; // next card
} // end while cards for provider
}
else
{
ptr += 5 + ptr[4] * 4; // skip cards because caid
}
} // end while < len
return ncards_in_msg;
}
// returns 1 if checkcode changed / 0 if not
static uint8_t gbox_checkcode_recvd(struct s_client *cli, uint8_t *checkcode, uint8_t updcrc)
{
struct gbox_peer *peer = cli->gbox;
if(memcmp(peer->checkcode, checkcode, 7))
{
if (updcrc)
{
cs_log_dump_dbg(D_READER, peer->checkcode, 7, "-> old checkcode from %04X %s:", peer->gbox.id, cli->reader->label);
cs_log_dump_dbg(D_READER, checkcode, 7, "-> new checkcode from %04X %s:", peer->gbox.id, cli->reader->label);
memcpy(peer->checkcode, checkcode, 7);
}
return 1;
}
return 0;
}
static void disable_remm(struct s_client *cli)
{
if (cli->reader->blockemm & 0x80) // if remm marker bit set
{
struct gbox_peer *peer = cli->gbox;
cs_log("-> Disable REMM Req for %04X %s %s", peer->gbox.id, cli->reader->label, cli->reader->device);
cli->reader->gbox_remm_peer = 0;
cli->reader->blockemm = 15;
write_msg_info(cli, MSGID_REMM, 0, 0);
}
return;
}
static void gbox_revd_goodnight(struct s_client *cli)
{
cs_log("-> Good Night received from %s %s", cli->reader->label, cli->reader->device);
disable_remm(cli);
write_msg_info(cli, MSGID_GOODNIGHT, 0, 0);
gbox_reinit_proxy(cli);
gbox_write_share_cards_info();
gbox_update_my_checkcode();
//gbox_send_peer_crd_update();
cli->last = time((time_t *)0);
return;
}
static void gbox_send_my_checkcode(struct s_client *cli)
{
struct gbox_peer *peer = cli->gbox;
uint8_t outbuf[20];
gbox_message_header(outbuf, MSG_CHECKCODE, peer->gbox.password, local_gbox.password);
memcpy(outbuf + 10, gbox_get_my_checkcode(), 7);
gbox_send(cli, outbuf, 17);
cs_log_dump_dbg(D_READER, gbox_get_my_checkcode(), 7, "<- my checkcode to %s:", cli->reader->label);
if (cfg.log_hello)
{ cs_log("<- HelloC my checkcode to %s (%s:%d)", cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port);}
else
{ cs_log_dbg(D_READER,"<- HelloC my checkcode to %s (%s:%d)", cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port);}
return;
}
int32_t gbox_cmd_hello_rcvd(struct s_client *cli, uint8_t *data, int32_t n)
{
if (!cli || !cli->gbox || !cli->reader || !data) { return -1; }
struct gbox_peer *peer = cli->gbox;
int16_t cards_number = 0;
int32_t payload_len = n;
int32_t hostname_len = 0;
int32_t footer_len = 0;
uint8_t *ptr = 0;
uint8_t diffcheck = 0;
uint8_t is_helloL = 0;
if(!(gbox_decode_cmd(data) == MSG_HELLO1))
{
gbox_decompress(data, &payload_len);
cs_log_dump_dbg(D_READER, data, payload_len, "-> data decompressed (%d bytes):", payload_len);
ptr = data + 12;
}
else
{
ptr = data + 11;
cs_log_dump_dbg(D_READER, data, payload_len, "decrypted data (%d bytes):", payload_len);
}
if ((data[11] & 0xf) != peer->next_hello) // out of sync hellos
{
cs_log("-> out of sync hello from %s %s, expected: %02X, received: %02X",
username(cli), cli->reader->device, peer->next_hello, data[11] & 0xf);
peer->next_hello = 0;
gbox_send_hello(cli, GBOX_STAT_HELLOL);
return 0;
}
if (!(data[11] & 0xf)) // is first packet
{
gbox_delete_cards(GBOX_DELETE_FROM_PEER, peer->gbox.id);
hostname_len = data[payload_len - 1];
footer_len = hostname_len + 2 + 7;
if(peer->hostname && memcmp(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len))
{
cs_log("WARNING - Received Hello from Peer %04X - hostname in cfg is different to received hostname", peer->gbox.id);
}
if(!peer->hostname || memcmp(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len))
{
NULLFREE(peer->hostname);
if(!cs_malloc(&peer->hostname, hostname_len + 1))
{
return -1;
}
memcpy(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len);
peer->hostname[hostname_len] = '\0';
}