gbx: apply excisting parameter 'disablecrccws = ' to gbx.
[oscam.git] / module-gbox.c
blob6e07295b90502dab2342d55f1fab45624765591a
1 #include "globals.h"
2 #ifdef MODULE_GBOX
4 // The following headers are used in parsing mg-encrypted parameter
5 #if defined(__APPLE__) || defined(__FreeBSD__)
6 #include <net/if_dl.h>
7 #include <ifaddrs.h>
8 #elif defined(__SOLARIS__)
9 #include <net/if.h>
10 #include <net/if_arp.h>
11 #include <sys/sockio.h>
12 #else
13 #include <net/if.h>
14 #endif
16 #include "module-gbox.h"
17 #include "module-gbox-helper.h"
18 #include "module-cccam.h"
19 #include "module-cccam-data.h"
20 #include "oscam-failban.h"
21 #include "oscam-client.h"
22 #include "oscam-ecm.h"
23 #include "oscam-lock.h"
24 #include "oscam-net.h"
25 #include "oscam-chk.h"
26 #include "oscam-string.h"
27 #include "oscam-time.h"
28 #include "oscam-reader.h"
29 #include "oscam-garbage.h"
31 #if defined(__CYGWIN__)
32 #define FILE_GBOX_VERSION "C:/tmp/gbx.ver"
33 #define FILE_SHARED_CARDS_INFO "C:/tmp/gbx_card.info"
34 #define FILE_ATTACK_INFO "C:/tmp/gbx_attack.txt"
35 #define FILE_GBOX_PEER_ONL "C:/tmp/gbx_peer.onl"
36 #else
37 #define FILE_GBOX_VERSION "/tmp/gbx.ver"
38 #define FILE_SHARED_CARDS_INFO "/tmp/gbx_card.info"
39 #define FILE_ATTACK_INFO "/tmp/gbx_attack.txt"
40 #define FILE_GBOX_PEER_ONL "/tmp/gbx_peer.onl"
41 #endif
43 #define GBOX_STAT_HELLOL 0
44 #define GBOX_STAT_HELLOS 1
45 #define GBOX_STAT_HELLOR 2
46 #define GBOX_STAT_HELLO3 3
47 #define GBOX_STAT_HELLO4 4
49 #define RECEIVE_BUFFER_SIZE 1024
51 #define LOCAL_GBOX_MAJOR_VERSION 0x02
52 #define LOCAL_GBOX_MINOR_VERSION 0x25
53 #define LOCAL_GBOX_TYPE 0x40
55 enum
57 MSG_ECM = 0x445c,
58 MSG_CW = 0x4844,
59 MSG_HELLO = 0xddab,
60 MSG_HELLO1 = 0x4849,
61 MSG_CHECKCODE = 0x41c0,
62 MSG_GOODBYE = 0x9091,
63 MSG_GSMS_ACK = 0x9098,
64 MSG_GSMS = 0xff0,
65 MSG_BOXINFO = 0xa0a1,
66 MSG_UNKNWN1 = 0x9099,
67 MSG_UNKNWN2 = 0x48F9,
70 struct gbox_srvid
72 uint16_t sid;
73 uint16_t peer_idcard;
74 uint32_t provid_id;
77 struct gbox_card
79 uint16_t peer_id;
80 uint16_t caid;
81 uint32_t provid;
82 uint32_t provid_1;
83 uint8_t slot;
84 uint8_t dist;
85 uint8_t lvl;
86 LLIST *badsids; // sids that have failed to decode (struct cc_srvid)
87 LLIST *goodsids; //sids that could be decoded (struct cc_srvid)
90 struct gbox_data
92 uint16_t id;
93 uchar password[4];
94 uchar checkcode[7];
95 uint8_t minor_version;
96 uint8_t type;
97 LLIST *cards;
100 struct gbox_peer
102 struct gbox_data gbox;
103 uchar *hostname;
104 int32_t online;
105 int32_t hello_stat;
106 int32_t goodbye_cont;
107 uchar ecm_idx;
108 uchar gbox_count_ecm;
109 CS_MUTEX_LOCK lock;
110 pthread_mutex_t hello_expire_mut;
111 pthread_cond_t hello_expire_cond;
112 struct s_client *my_user;
115 static struct gbox_data local_gbox;
117 static void gbox_send_boxinfo(struct s_client *cli);
118 static void gbox_send_hello(struct s_client *cli);
119 static void gbox_send_hello_packet(struct s_client *cli, int8_t number, uchar *outbuf, uchar *ptr, int32_t nbcards);
120 static void gbox_send_checkcode(struct s_client *cli);
121 static void gbox_expire_hello(struct s_client *cli);
122 static void gbox_local_cards(struct s_client *cli);
123 static void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere);
124 static int32_t gbox_client_init(struct s_client *cli);
125 static int8_t gbox_check_header(struct s_client *cli, uchar *data, int32_t l);
126 static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n);
127 static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t UNUSED(n));
128 static int32_t gbox_checkcode_recv(struct s_client *cli, uchar *checkcode);
129 static int32_t gbox_decode_cmd(uchar *buf);
130 static uint8_t gbox_compare_pw(uchar *my_pw, uchar *rec_pw);
131 static uint16_t gbox_convert_password_to_id(uchar *password);
132 uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er);
133 static void init_local_gbox(void);
135 void gbox_write_peer_onl(void)
137 FILE *fhandle = fopen(FILE_GBOX_PEER_ONL, "w");
138 if(!fhandle)
140 cs_log("Couldn't open %s: %s\n", FILE_GBOX_PEER_ONL, strerror(errno));
141 return;
143 struct s_client *cl;
144 for(cl = first_client; cl; cl = cl->next)
146 if(cl->gbox && cl->typ == 'p')
148 struct gbox_peer *peer = cl->gbox;
149 if (peer->online)
150 { fprintf(fhandle, "1 %s %s %04X 2.%02X\n",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id, peer->gbox.minor_version); }
151 else
152 { fprintf(fhandle, "0 %s %s %04X 0.00\n",cl->reader->device, cs_inet_ntoa(cl->ip),peer->gbox.id); }
155 fclose(fhandle);
156 return;
159 void gbox_write_version(void)
161 FILE *fhandle = fopen(FILE_GBOX_VERSION, "w");
162 if(!fhandle)
164 cs_log("Couldn't open %s: %s\n", FILE_GBOX_VERSION, strerror(errno));
165 return;
167 fprintf(fhandle, "%02X.%02X\n", LOCAL_GBOX_MAJOR_VERSION, LOCAL_GBOX_MINOR_VERSION);
168 fclose(fhandle);
171 void gbox_write_shared_cards_info(void)
173 int32_t card_count = 0;
174 int32_t i = 0;
176 FILE *fhandle;
177 fhandle = fopen(FILE_SHARED_CARDS_INFO, "w");
178 if(!fhandle)
180 cs_log("Couldn't open %s: %s\n", FILE_SHARED_CARDS_INFO, strerror(errno));
181 return;
184 LL_ITER it;
185 struct gbox_card *card;
187 //write local cards
188 it = ll_iter_create(local_gbox.cards);
189 while((card = ll_iter_next(&it)))
191 fprintf(fhandle, "CardID %4d at oscam Card %08X Sl:%2d Lev:%2d dist:%2d id:%04X\n",
192 card_count, card->provid_1,
193 card->slot, card->lvl, card->dist, card->peer_id);
194 card_count++;
195 } // end of while ll_iter_next
197 struct s_client *cl;
198 for(i = 0, cl = first_client; cl; cl = cl->next, i++)
200 if(cl->gbox)
202 struct gbox_peer *peer = cl->gbox;
204 if((cl->reader->card_status == CARD_INSERTED) && (cl->typ == 'p'))
206 it = ll_iter_create(peer->gbox.cards);
207 while((card = ll_iter_next(&it)))
209 fprintf(fhandle, "CardID %4d at %s Card %08X Sl:%2d Lev:%2d dist:%2d id:%04X\n",
210 card_count, cl->reader->device, card->provid_1,
211 card->slot, card->lvl, card->dist, card->peer_id);
212 card_count++;
213 } // end of while ll_iter_next
214 } // end of if INSERTED && 'p'
215 } // end of if cl->gbox
216 } // end of for cl->next
217 fclose(fhandle);
218 return;
221 void hostname2ip(char *hostname, IN_ADDR_T *ip)
223 cs_resolve(hostname, ip, NULL, NULL);
226 static uint8_t gbox_compare_pw(uchar *my_pw, uchar *rec_pw)
228 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];
231 static uint16_t gbox_convert_password_to_id(uchar *password)
233 return (password[0] ^ password[2]) << 8 | (password[1] ^ password[3]);
236 void gbox_add_good_card(struct s_client *cl, uint16_t id_card, uint16_t caid, uint32_t prov, uint16_t sid_ok)
238 struct gbox_peer *peer = cl->gbox;
239 struct gbox_card *card = NULL;
240 struct gbox_srvid *srvid = NULL;
241 LL_ITER it = ll_iter_create(peer->gbox.cards);
242 while((card = ll_iter_next(&it)))
244 if(card->peer_id == id_card && card->caid == caid && card->provid == prov)
246 cl->reader->currenthops = card->dist;
247 LL_ITER it2 = ll_iter_create(card->goodsids);
248 while((srvid = ll_iter_next(&it2)))
250 if(srvid->sid == sid_ok)
252 return; // sid_ok is already in the list of goodsids
256 LL_ITER it3 = ll_iter_create(card->badsids);
257 while((srvid = ll_iter_next(&it3)))
259 if(srvid->sid == sid_ok)
261 ll_iter_remove_data(&it3); // remove sid_ok from badsids
262 break;
266 if(!cs_malloc(&srvid, sizeof(struct gbox_srvid)))
267 { return; }
268 srvid->sid = sid_ok;
269 srvid->peer_idcard = id_card;
270 srvid->provid_id = card->provid;
271 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);
272 ll_append(card->goodsids, srvid);
273 break;
275 }//end of ll_iter_next
276 //return dist_c;
279 void gbox_free_card(struct gbox_card *card)
281 ll_destroy_data_NULL(card->badsids);
282 ll_destroy_data_NULL(card->goodsids);
283 add_garbage(card);
284 return;
287 void gbox_remove_cards_without_goodsids(LLIST *card_list)
289 if(card_list)
291 LL_ITER it = ll_iter_create(card_list);
292 struct gbox_card *card;
293 while((card = ll_iter_next(&it)))
295 if(ll_count(card->goodsids) == 0)
297 ll_iter_remove(&it);
298 gbox_free_card(card);
300 else
302 ll_destroy_data_NULL(card->badsids);
306 return;
309 void gbox_free_cardlist(LLIST *card_list)
311 if(card_list)
313 LL_ITER it = ll_iter_create(card_list);
314 struct gbox_card *card;
315 while((card = ll_iter_next_remove(&it)))
317 gbox_free_card(card);
319 ll_destroy_NULL(card_list);
321 return;
324 void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere)
327 ere->gbox_crc = 0;
328 ere->gbox_ecm_id = 0;
329 ere->gbox_ecm_ok = 0;
331 ere->gbox_hops = 0;
332 ere->gbox_peer = 0;
333 ere->gbox_mypeer = 0;
334 ere->gbox_caid = 0;
335 ere->gbox_prid = 0;
336 ere->gbox_slot = 0;
337 ere->gbox_version = 0;
338 ere->gbox_unknown = 0;
339 ere->gbox_type = 0;
342 struct s_client *get_gbox_proxy(uint16_t gbox_id)
344 struct s_client *cl;
345 for(cl = first_client; cl; cl = cl->next)
347 if(cl->typ == 'p' && cl->gbox && cl->gbox_peer_id == gbox_id)
349 return cl;
352 return NULL;
355 // if input client is typ proxy get client and vice versa
356 struct s_client *switch_client_proxy(struct s_client *cli, uint16_t gbox_id)
358 struct s_client *cl;
359 int8_t typ;
360 if(cli->typ == 'c')
361 { typ = 'p'; }
362 else
363 { typ = 'c'; }
364 for(cl = first_client; cl; cl = cl->next)
366 if(cl->typ == typ && cl->gbox && cl->gbox_peer_id == gbox_id)
368 return cl;
371 return cli;
374 void gbox_reconnect_client(uint16_t gbox_id)
376 struct s_client *cl;
377 for(cl = first_client; cl; cl = cl->next)
379 if(cl->gbox && cl->typ == 'p' && cl->gbox_peer_id == gbox_id)
381 hostname2ip(cl->reader->device, &SIN_GET_ADDR(cl->udp_sa));
382 SIN_GET_FAMILY(cl->udp_sa) = AF_INET;
383 SIN_GET_PORT(cl->udp_sa) = htons((uint16_t)cl->reader->r_port);
384 hostname2ip(cl->reader->device, &(cl->ip));
385 cl->reader->tcp_connected = 0;
386 cl->reader->card_status = CARD_NEED_INIT;
387 struct gbox_peer *peer = cl->gbox;
388 peer->online = 0;
389 peer->ecm_idx = 0;
390 peer->hello_stat = GBOX_STAT_HELLOL;
391 cl->reader->last_s = cl->reader->last_g = 0;
392 gbox_free_cardlist(peer->gbox.cards);
393 peer->gbox.cards = ll_create("peer.cards");
394 gbox_send_hello(cl);
399 static void *gbox_server(struct s_client *cli, uchar *b, int32_t l)
401 if(l > 0)
403 cs_log("gbox: gbox_server %s/%d", cli->reader->label, cli->port);
404 gbox_check_header(cli, b, l);
406 return 0;
409 char *gbox_username(struct s_client *client)
411 if(!client) { return "anonymous"; }
412 if(client->reader)
413 if(client->reader->r_usr[0])
415 return client->reader->r_usr;
417 return "anonymous";
420 static int8_t gbox_auth_client(struct s_client *cli, uchar *gbox_password)
422 uint16_t gbox_id = gbox_convert_password_to_id(gbox_password);
423 struct s_client *cl = switch_client_proxy(cli, gbox_id);
424 if (cl->gbox)
426 struct gbox_peer *peer = cl->gbox;
427 if (!gbox_compare_pw(&peer->gbox.password[0],gbox_password))
428 { return -1; }
430 if(cl->typ == 'p' && cl->gbox && cl->reader)
432 cli->crypted = 1; //display as crypted
433 cli->gbox = cl->gbox; //point to the same gbox as proxy
434 cli->reader = cl->reader; //point to the same reader as proxy
435 cli->gbox_peer_id = cl->gbox_peer_id; //signal authenticated
437 struct s_auth *account = get_account_by_name(gbox_username(cl));
438 if(account)
440 cs_auth_client(cli, account, NULL);
441 cli->account = account;
442 cli->grp = account->grp;
443 return 0;
446 return -1;
449 static void gbox_server_init(struct s_client *cl)
451 if(!cl->init_done)
453 if(IP_ISSET(cl->ip))
454 { cs_log("gbox: new connection from %s", cs_inet_ntoa(cl->ip)); }
455 //We cannot authenticate here, because we don't know gbox pw
456 cl->gbox_peer_id = NO_GBOX_ID;
457 cl->init_done = 1;
459 return;
462 int32_t gbox_cmd_hello(struct s_client *cli, uchar *data, int32_t n)
464 struct gbox_peer *peer = cli->gbox;
465 int32_t i;
466 int32_t ncards_in_msg = 0;
467 int32_t payload_len = n;
468 //TODO: checkcode_len can be made void
469 int32_t checkcode_len = 0;
470 int32_t hostname_len = 0;
471 int32_t footer_len = 0;
472 uint8_t *ptr = 0;
474 if(!(gbox_decode_cmd(data) == MSG_HELLO1))
476 gbox_decompress(data, &payload_len);
478 cs_ddump_mask(D_READER, data, payload_len, "gbox: decompressed data (%d bytes):", payload_len);
480 if((data[0x0B] == 0) | ((data[0x0A] == 1) && (data[0x0B] == 0x80)))
482 if(peer->gbox.cards)
483 { gbox_remove_cards_without_goodsids(peer->gbox.cards); }
484 else
485 { peer->gbox.cards = ll_create("peer.cards"); }
487 if((data[0xB] & 0xF) == 0)
489 checkcode_len = 7;
490 hostname_len = data[payload_len - 1];
491 footer_len = hostname_len + 2;
494 if(gbox_decode_cmd(data) == MSG_HELLO1)
495 { ptr = data + 11; }
496 else
497 { ptr = data + 12; }
499 while(ptr < data + payload_len - footer_len - checkcode_len - 1)
501 uint16_t caid;
502 uint32_t provid;
503 uint32_t provid1;
505 switch(ptr[0])
507 //Viaccess
508 case 0x05:
509 caid = ptr[0] << 8;
510 provid = ptr[1] << 16 | ptr[2] << 8 | ptr[3];
511 break;
512 //Cryptoworks
513 case 0x0D:
514 caid = ptr[0] << 8 | ptr[1];
515 provid = ptr[2];
516 break;
517 default:
518 caid = ptr[0] << 8 | ptr[1];
519 provid = ptr[2] << 8 | ptr[3];
520 break;
523 //caid check
524 if(chk_ctab(caid, &cli->reader->ctab))
527 provid1 = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
528 uint8_t ncards = ptr[4];
530 ptr += 5;
532 for(i = 0; i < ncards; i++)
534 // for all n cards and current caid/provid,
535 // create card info from data and add card to peer.cards
536 struct gbox_card *card;
537 if(!cs_malloc(&card, sizeof(struct gbox_card)))
538 { continue; }
540 card->caid = caid;
541 card->provid = provid;
542 card->provid_1 = provid1;
543 card->slot = ptr[0];
544 card->dist = ptr[1] & 0xf;
545 card->lvl = ptr[1] >> 4;
546 card->peer_id = ptr[2] << 8 | ptr[3];
547 ptr += 4;
549 if((cli->reader->gbox_maxdist >= card->dist) && (card->peer_id != local_gbox.id))
552 LL_ITER it = ll_iter_create(peer->gbox.cards);
553 struct gbox_card *card_s;
554 uint8_t v_card = 0;
555 while((card_s = ll_iter_next(&it))) // don't add card if already in peer.cards list
557 if(card_s->peer_id == card->peer_id && card_s->provid_1 == card->provid_1)
559 gbox_free_card(card);
560 card = NULL;
561 v_card = 1;
562 break;
566 if(v_card != 1) // new card - not in list
568 card->badsids = ll_create("badsids");
569 card->goodsids = ll_create("goodsids");
570 ll_append(peer->gbox.cards, card);
571 ncards_in_msg++;
572 cs_debug_mask(D_READER, " card: caid=%04x, provid=%06x, slot=%d, level=%d, dist=%d, peer=%04x",
573 card->caid, card->provid, card->slot, card->lvl, card->dist, card->peer_id);
576 else // don't add card
578 gbox_free_card(card);
579 card = NULL;
581 cli->reader->tcp_connected = 2; // we have card
582 } // end for ncards
584 else
586 ptr += 5 + ptr[4] * 4; //skip cards because caid
588 } // end while caid/provid
590 if(!(data[0x0B] & 0xF)) // first packet. We've got peer hostname
592 NULLFREE(peer->hostname);
593 if(!cs_malloc(&peer->hostname, hostname_len + 1))
595 cs_writeunlock(&peer->lock);
596 return -1;
598 memcpy(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len);
599 peer->hostname[hostname_len] = '\0';
601 gbox_checkcode_recv(cli, data + payload_len - footer_len - checkcode_len - 1);
602 peer->gbox.minor_version = data[payload_len - footer_len - 1];
603 peer->gbox.type = data[payload_len - footer_len];
604 } // end if first hello packet
606 if(data[0x0B] & 0x80) //last packet
608 peer->online = 1;
609 if(!data[0xA])
611 cs_log("<-HelloS from %s (%s:%d) V2.%02X with %d cards", cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port, peer->gbox.minor_version, ncards_in_msg);
612 peer->hello_stat = GBOX_STAT_HELLOR;
613 gbox_send_hello(cli);
615 else
617 cs_log("<-HelloR from %s (%s:%d) V2.%02X with %d cards", cli->reader->label, cs_inet_ntoa(cli->ip), cli->reader->r_port, peer->gbox.minor_version, ncards_in_msg);
618 gbox_send_checkcode(cli);
620 if(peer->hello_stat == GBOX_STAT_HELLOS)
622 gbox_send_hello(cli);
624 cli->reader->tcp_connected = 2; //we have card
625 cli->reader->card_status = CARD_INSERTED;
626 if(ll_count(peer->gbox.cards) == 0)
627 { cli->reader->card_status = NO_CARD; }
629 gbox_write_shared_cards_info();
630 gbox_write_version();
631 gbox_write_peer_onl();
633 return 0;
636 static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n)
638 struct gbox_peer *peer;
639 struct s_client *cl;
640 int32_t diffcheck = 0;
642 peer = cli->gbox;
643 if (!peer || !peer->my_user) { return -1; }
644 cl = peer->my_user;
646 // No ECMs with length < 8 expected
647 if ((((data[19] & 0x0f) << 8) | data[20]) < 8) { return -1; }
649 // GBOX_MAX_HOPS not violated
650 if (data[n - 15] + 1 > GBOX_MAXHOPS) { return -1; }
652 ECM_REQUEST *er;
653 if(!(er = get_ecmtask())) { return -1; }
655 struct gbox_ecm_request_ext *ere;
656 if(!cs_malloc(&ere, sizeof(struct gbox_ecm_request_ext)))
658 cs_writeunlock(&peer->lock);
659 return -1;
662 uchar *ecm = data + 18; //offset of ECM in gbx message
664 er->src_data = ere;
665 gbox_init_ecm_request_ext(ere);
667 peer->gbox_count_ecm++;
668 er->gbox_ecm_id = peer->gbox.id;
670 if(peer->ecm_idx == 100) { peer->ecm_idx = 0; }
672 er->idx = peer->ecm_idx++;
673 er->ecmlen = (((ecm[1] & 0x0f) << 8) | ecm[2]) + 3;
675 er->pid = data[10] << 8 | data[11];
676 er->srvid = data[12] << 8 | data[13];
678 int32_t adr_caid_1 = (((data[19] & 0x0f) << 8) | data[20]) + 26;
679 if(data[adr_caid_1] == 0x05)
680 { er->caid = (data[adr_caid_1] << 8); }
681 else
682 { er->caid = (data[adr_caid_1] << 8 | data[adr_caid_1 + 1]); }
684 // ei->extra = data[14] << 8 | data[15];
685 memcpy(er->ecm, data + 18, er->ecmlen);
686 ere->gbox_peer = ecm[er->ecmlen] << 8 | ecm[er->ecmlen + 1];
687 ere->gbox_version = ecm[er->ecmlen + 2];
688 ere->gbox_unknown = ecm[er->ecmlen + 3];
689 ere->gbox_type = ecm[er->ecmlen + 4];
690 ere->gbox_caid = ecm[er->ecmlen + 5] << 8 | ecm[er->ecmlen + 6];
691 ere->gbox_prid = ecm[er->ecmlen + 7] << 8 | ecm[er->ecmlen + 8];
692 ere->gbox_mypeer = ecm[er->ecmlen + 10] << 8 | ecm[er->ecmlen + 11];
693 ere->gbox_slot = ecm[er->ecmlen + 12];
695 diffcheck = gbox_checkcode_recv(cl, data + n - 14);
696 //TODO: What do we do with our own checkcode @-7?
697 er->gbox_crc = gbox_get_ecmchecksum(er);
698 ere->gbox_hops = data[n - 15] + 1;
699 memcpy(&ere->gbox_routing_info[0], &data[n - 15 - ere->gbox_hops + 1], ere->gbox_hops - 1);
701 er->prid = chk_provid(er->ecm, er->caid);
702 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);
703 get_cw(cl, er);
705 //checkcode did not match gbox->peer checkcode
706 if(diffcheck)
708 // TODO: Send HelloS here?
709 // gbox->peer.hello_stat = GBOX_STAT_HELLOS;
710 // gbox_send_hello(cli);
712 return 0;
715 int32_t gbox_cmd_switch(struct s_client *cli, uchar *data, int32_t n)
717 struct gbox_peer *peer = cli->gbox;
718 int32_t n1 = 0, rc1 = 0, i1, idx;
719 uchar dcw[16];
721 switch(gbox_decode_cmd(data))
723 case MSG_BOXINFO:
724 // Keep alive message
725 pthread_mutex_lock(&peer->hello_expire_mut);
726 pthread_cond_signal(&peer->hello_expire_cond);
727 pthread_mutex_unlock(&peer->hello_expire_mut);
728 gbox_send_hello(cli);
729 break;
730 case MSG_GOODBYE:
731 //needfix what to do after Goodbye?
732 //suspect: we get goodbye as signal of SID not found
733 break;
734 case MSG_HELLO1:
735 case MSG_HELLO:
736 if(gbox_cmd_hello(cli, data, n) < 0)
737 { return -1; }
738 pthread_mutex_lock(&peer->hello_expire_mut);
739 pthread_cond_signal(&peer->hello_expire_cond);
740 pthread_mutex_unlock(&peer->hello_expire_mut);
741 break;
742 case MSG_CW:
743 cli->last = time((time_t *)0);
744 idx = gbox_recv_chk(cli, dcw, &rc1, data, rc1);
745 if(idx < 0) { break; } // no dcw received
746 if(!idx) { idx = cli->last_idx; }
747 cli->reader->last_g = time((time_t *)0); // for reconnect timeout
748 for(i1 = 0, n1 = 0; i1 < cfg.max_pending && n1 == 0; i1++)
750 if(cli->ecmtask[i1].idx == idx)
752 cli->pending--;
753 casc_check_dcw(cli->reader, i1, rc1, dcw);
754 n1++;
757 pthread_mutex_lock(&peer->hello_expire_mut);
758 pthread_cond_signal(&peer->hello_expire_cond);
759 pthread_mutex_unlock(&peer->hello_expire_mut);
760 break;
761 case MSG_CHECKCODE:
762 gbox_checkcode_recv(cli, data + 10);
763 pthread_mutex_lock(&peer->hello_expire_mut);
764 pthread_cond_signal(&peer->hello_expire_cond);
765 pthread_mutex_unlock(&peer->hello_expire_mut);
766 break;
767 case MSG_ECM:
769 gbox_incoming_ecm(cli, data, n);
770 break;
772 default:
773 cs_ddump_mask(D_READER, data, n, "gbox: unknown data received (%d bytes):", n);
774 } // end switch
775 return 0;
778 static int8_t gbox_check_header(struct s_client *cli, uchar *data, int32_t l)
780 struct s_client *cl = switch_client_proxy(cli, cli->gbox_peer_id);
782 //clients may timeout - attach to peer's gbox/reader
783 cli->gbox = cl->gbox; //point to the same gbox as proxy
784 cli->reader = cl->reader; //point to the same reader as proxy
786 struct gbox_peer *peer = cl->gbox;
788 char tmp[0x50];
789 int32_t n = l;
790 cs_ddump_mask(D_READER, data, n, "gbox: encrypted data received (%d bytes):", n);
792 if(gbox_decode_cmd(data) == MSG_HELLO1)
793 { cs_log("test cs2gbox"); }
794 else
795 { gbox_decrypt(data, n, local_gbox.password); }
797 cs_ddump_mask(D_READER, data, n, "gbox: decrypted received data (%d bytes):", n);
799 //verify my pass received
800 if (gbox_compare_pw(&data[2],&local_gbox.password[0]))
802 cs_debug_mask(D_READER, "received data, peer : %04x data: %s", cli->gbox_peer_id, cs_hexdump(0, data, l, tmp, sizeof(tmp)));
804 if (gbox_decode_cmd(data) != MSG_CW)
806 if (cli->gbox_peer_id == NO_GBOX_ID)
808 if (gbox_auth_client(cli, &data[6]) < 0)
809 { return -1; }
810 //NEEDFIX: Pretty sure this should not be done here
811 gbox_local_cards(cli);
812 cl = switch_client_proxy(cli, cli->gbox_peer_id);
814 //clients may timeout - attach to peer's gbox/reader
815 cli->gbox = cl->gbox; //point to the same gbox as proxy
816 cli->reader = cl->reader; //point to the same reader as proxy
818 peer = cl->gbox;
819 if (peer) { peer->my_user = cli; }
821 if (!peer) { return -1; }
822 if (!gbox_compare_pw(&data[6],&peer->gbox.password[0]))
824 cs_log("gbox peer: %04X sends wrong password", peer->gbox.id);
825 return -1;
826 //continue; // next client
828 } else
830 // if my pass ok verify CW | pass to peer
831 if((data[39] != ((local_gbox.id >> 8) & 0xff)) || (data[40] != (local_gbox.id & 0xff)))
833 cs_log("gbox peer: %04X sends CW for other than my id: %04X", cli->gbox_peer_id, local_gbox.id);
834 return -1;
835 //continue; // next client
838 } // error my pass
839 else
841 cs_log("gbox: ATTACK ALERT: proxy %s:%d", cs_inet_ntoa(cli->ip), cli->reader->r_port);
842 cs_log("received data, peer : %04x data: %s", cli->gbox_peer_id, cs_hexdump(0, data, n, tmp, sizeof(tmp)));
843 return -1;
844 //continue; // next client
847 if (!IP_EQUAL(cli->ip, cl->ip))
849 gbox_reconnect_client(cli->gbox_peer_id);
850 return -1;
853 if(!peer) { return -1; }
855 cli->last = time((time_t *)0);
857 cs_writelock(&peer->lock);
858 if(gbox_cmd_switch(cl, data, n) < 0)
859 { return -1; }
860 cs_writeunlock(&peer->lock);
862 return 0;
865 static int32_t gbox_decode_cmd(uchar *buf)
867 return buf[0] << 8 | buf[1];
870 void gbox_code_cmd(uchar *buf, int16_t cmd)
872 buf[0] = cmd >> 8;
873 buf[1] = cmd & 0xff;
876 static void gbox_calc_checkcode(void)
878 int32_t i = 0;
879 struct s_client *cl;
881 local_gbox.checkcode[0] = 0x15;
882 local_gbox.checkcode[1] = 0x30;
883 local_gbox.checkcode[2] = 0x02;
884 local_gbox.checkcode[3] = 0x04;
885 local_gbox.checkcode[4] = 0x19;
886 local_gbox.checkcode[5] = 0x19;
887 local_gbox.checkcode[6] = 0x66;
889 LL_ITER it = ll_iter_create(local_gbox.cards);
890 struct gbox_card *card;
891 while((card = ll_iter_next(&it)))
893 local_gbox.checkcode[0] ^= (0xFF & (card->provid_1 >> 24));
894 local_gbox.checkcode[1] ^= (0xFF & (card->provid_1 >> 16));
895 local_gbox.checkcode[2] ^= (0xFF & (card->provid_1 >> 8));
896 local_gbox.checkcode[3] ^= (0xFF & (card->provid_1));
897 local_gbox.checkcode[4] ^= (0xFF & (card->slot));
898 local_gbox.checkcode[5] ^= (0xFF & (card->peer_id >> 8));
899 local_gbox.checkcode[6] ^= (0xFF & (card->peer_id));
901 for(i = 0, cl = first_client; cl; cl = cl->next, i++)
903 if (cl->gbox && cl->typ == 'p')
905 struct gbox_peer *peer = cl->gbox;
906 it = ll_iter_create(peer->gbox.cards);
907 while((card = ll_iter_next(&it)))
909 local_gbox.checkcode[0] ^= (0xFF & (card->provid_1 >> 24));
910 local_gbox.checkcode[1] ^= (0xFF & (card->provid_1 >> 16));
911 local_gbox.checkcode[2] ^= (0xFF & (card->provid_1 >> 8));
912 local_gbox.checkcode[3] ^= (0xFF & (card->provid_1));
913 local_gbox.checkcode[4] ^= (0xFF & (card->slot));
914 local_gbox.checkcode[5] ^= (0xFF & (card->peer_id >> 8));
915 local_gbox.checkcode[6] ^= (0xFF & (card->peer_id));
921 //returns 1 if checkcode changed / 0 if not
922 static int32_t gbox_checkcode_recv(struct s_client *cli, uchar *checkcode)
924 struct gbox_peer *peer = cli->gbox;
925 char tmp[7];
927 if(memcmp(peer->gbox.checkcode, checkcode, 7))
929 memcpy(peer->gbox.checkcode, checkcode, 7);
930 cs_debug_mask(D_READER, "gbox: received new checkcode=%s", cs_hexdump(0, peer->gbox.checkcode, 7, tmp, sizeof(tmp)));
931 return 1;
933 return 0;
936 uint32_t gbox_get_ecmchecksum(ECM_REQUEST *er)
939 uint8_t checksum[4];
940 int32_t counter;
942 checksum[3] = er->ecm[0];
943 checksum[2] = er->ecm[1];
944 checksum[1] = er->ecm[2];
945 checksum[0] = er->ecm[3];
947 for(counter = 1; counter < (er->ecmlen / 4) - 4; counter++)
949 checksum[3] ^= er->ecm[counter * 4];
950 checksum[2] ^= er->ecm[counter * 4 + 1];
951 checksum[1] ^= er->ecm[counter * 4 + 2];
952 checksum[0] ^= er->ecm[counter * 4 + 3];
955 return checksum[3] << 24 | checksum[2] << 16 | checksum[1] << 8 | checksum[0];
958 static void gbox_expire_hello(struct s_client *cli)
960 struct gbox_peer *peer = cli->gbox;
961 int32_t callback = 60;
963 set_thread_name(__func__);
965 cs_pthread_cond_init(&peer->hello_expire_mut, &peer->hello_expire_cond);
967 while(1)
969 struct timespec ts;
970 add_ms_to_timespec(&ts, callback * 1000);
972 pthread_mutex_lock(&peer->hello_expire_mut);
973 if(pthread_cond_timedwait(&peer->hello_expire_cond, &peer->hello_expire_mut, &ts) == ETIMEDOUT)
975 switch(peer->hello_stat)
977 case GBOX_STAT_HELLOS:
978 peer->hello_stat = GBOX_STAT_HELLOL;
979 gbox_send_hello(cli);
980 callback = 180;
981 break;
983 case GBOX_STAT_HELLOR:
984 callback = 300;
985 break;
987 case GBOX_STAT_HELLO3:
988 peer->hello_stat = GBOX_STAT_HELLOS;
989 // gbox_send_boxinfo(cli);
990 // gbox_send_hello(cli);
991 callback = 900;
992 peer->hello_stat = GBOX_STAT_HELLO3;
993 break;
995 case GBOX_STAT_HELLO4:
996 gbox_send_boxinfo(cli);
997 break;
1000 pthread_mutex_unlock(&peer->hello_expire_mut);
1004 static void gbox_send(struct s_client *cli, uchar *buf, int32_t l)
1006 struct gbox_peer *peer = cli->gbox;
1008 cs_ddump_mask(D_READER, buf, l, "gbox: decrypted data send (%d bytes):", l);
1010 hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
1011 SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
1012 SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)cli->reader->r_port);
1014 gbox_encrypt(buf, l, peer->gbox.password);
1015 sendto(cli->udp_fd, buf, l, 0, (struct sockaddr *)&cli->udp_sa, cli->udp_sa_len);
1016 cs_ddump_mask(D_READER, buf, l, "gbox: encrypted data send (%d bytes):", l);
1019 static void gbox_send_hello_packet(struct s_client *cli, int8_t number, uchar *outbuf, uchar *ptr, int32_t nbcards)
1021 struct gbox_peer *peer = cli->gbox;
1022 int32_t hostname_len = strlen(cfg.gbox_hostname);
1023 int32_t len;
1025 gbox_code_cmd(outbuf, MSG_HELLO);
1026 memcpy(outbuf + 2, peer->gbox.password, 4);
1027 memcpy(outbuf + 6, local_gbox.password, 4);
1028 // initial HELLO = 0, subsequent = 1
1029 if(peer->hello_stat > GBOX_STAT_HELLOS)
1030 { outbuf[10] = 1; }
1031 else
1032 { outbuf[10] = 0; }
1033 outbuf[11] = number; // 0x80 (if last packet) else 0x00 | packet number
1035 if((number & 0x0F) == 0)
1037 gbox_calc_checkcode();
1038 if(peer->hello_stat != GBOX_STAT_HELLOL)
1039 { memcpy(++ptr, local_gbox.checkcode, 7); }
1040 else
1041 { memset(++ptr, 0, 7); }
1042 ptr += 7;
1043 *ptr = local_gbox.minor_version;
1044 *(++ptr) = local_gbox.type;
1045 memcpy(++ptr, cfg.gbox_hostname, hostname_len);
1046 ptr += hostname_len;
1047 *ptr = hostname_len;
1049 len = ptr - outbuf + 1;
1050 switch(peer->hello_stat)
1052 case GBOX_STAT_HELLOL:
1053 cs_log("gbox: send HELLOL to %s", cli->reader->label);
1054 if((number & 0x80) == 0x80)
1055 { peer->hello_stat = GBOX_STAT_HELLOS; }
1056 break;
1057 case GBOX_STAT_HELLOS:
1058 cs_log("gbox: send HELLOS total cards %d to %s", nbcards, cli->reader->label);
1059 if((number & 0x80) == 0x80)
1060 { peer->hello_stat = GBOX_STAT_HELLO3; }
1061 break;
1062 case GBOX_STAT_HELLOR:
1063 cs_log("gbox: send HELLOR total cards %d to %s", nbcards, cli->reader->label);
1064 if((number & 0x80) == 0x80)
1065 { peer->hello_stat = GBOX_STAT_HELLO3; }
1066 break;
1067 default:
1068 cs_log("gbox: send hello total cards %d to %s", nbcards, cli->reader->label);
1069 break;
1071 cs_ddump_mask(D_READER, outbuf, len, "send hello, (len=%d):", len);
1073 gbox_compress(outbuf, len, &len);
1075 gbox_send(cli, outbuf, len);
1078 static void gbox_send_hello(struct s_client *cli)
1080 struct gbox_peer *peer = cli->gbox;
1082 int32_t nbcards = 0;
1083 int32_t packet;
1084 uchar buf[2048];
1086 int32_t ok = 0;
1087 #ifdef WEBIF
1088 ok = check_ip(cfg.http_allowed, cli->ip) ? 1 : 0;
1089 #endif
1091 packet = 0;
1092 uchar *ptr = buf + 11;
1093 if(ll_count(local_gbox.cards) != 0 && peer->hello_stat > GBOX_STAT_HELLOL)
1095 memset(buf, 0, sizeof(buf));
1097 LL_ITER it = ll_iter_create(local_gbox.cards);
1098 struct gbox_card *card;
1099 while((card = ll_iter_next(&it)))
1101 //send to user only cards which matching CAID from account and lvl > 0
1102 if(chk_ctab(card->caid, &peer->my_user->account->ctab) && card->lvl > 0)
1104 *(++ptr) = card->provid_1 >> 24;
1105 *(++ptr) = card->provid_1 >> 16;
1106 *(++ptr) = card->provid_1 >> 8;
1107 *(++ptr) = card->provid_1 & 0xff;
1108 *(++ptr) = 1; //note: original gbx is more efficient and sends all cards of one caid as package
1109 *(++ptr) = card->slot;
1110 //If you modify the next line you are going to destroy the community
1111 //It will be recognized by original gbx and you will get banned
1112 *(++ptr) = ((card->lvl - 1) << 4) + card->dist + 1;
1113 *(++ptr) = card->peer_id >> 8;
1114 *(++ptr) = card->peer_id & 0xff;
1115 nbcards++;
1116 if(nbcards == 100) //check if 100 is good or we need more sophisticated algorithm
1118 gbox_send_hello_packet(cli, packet, buf, ptr, nbcards);
1119 packet++;
1120 nbcards = 0;
1121 ptr = buf + 11;
1122 memset(buf, 0, sizeof(buf));
1126 } // end if local card exists
1128 //last packet has bit 0x80 set
1129 gbox_send_hello_packet(cli, 0x80 | packet, buf, ptr, nbcards);
1132 static void gbox_send_checkcode(struct s_client *cli)
1134 struct gbox_peer *peer = cli->gbox;
1135 uchar outbuf[20];
1137 gbox_calc_checkcode();
1138 gbox_code_cmd(outbuf, MSG_CHECKCODE);
1139 memcpy(outbuf + 2, peer->gbox.password, 4);
1140 memcpy(outbuf + 6, local_gbox.password, 4);
1141 memcpy(outbuf + 10, local_gbox.checkcode, 7);
1143 gbox_send(cli, outbuf, 17);
1146 static void gbox_send_boxinfo(struct s_client *cli)
1148 struct gbox_peer *peer = cli->gbox;
1149 uchar outbuf[256];
1150 int32_t hostname_len = strlen(cfg.gbox_hostname);
1152 gbox_code_cmd(outbuf, MSG_BOXINFO);
1153 memcpy(outbuf + 2, peer->gbox.password, 4);
1154 memcpy(outbuf + 6, local_gbox.password, 4);
1155 outbuf[0xA] = local_gbox.minor_version;
1156 outbuf[0xB] = local_gbox.type;
1157 memcpy(&outbuf[0xC], cfg.gbox_hostname, hostname_len);
1158 gbox_send(cli, outbuf, hostname_len + 0xC);
1161 static int32_t gbox_recv(struct s_client *cli, uchar *buf, int32_t l)
1163 uchar data[RECEIVE_BUFFER_SIZE];
1164 int32_t n = l;
1166 if(!cli->udp_fd || n > RECEIVE_BUFFER_SIZE) { return -1; }
1167 if(cli->is_udp && cli->typ == 'c')
1169 n = recv_from_udpipe(buf);
1170 memcpy(&data[0], buf, n);
1172 gbox_check_header(cli, &data[0], n);
1174 //clients may timeout - dettach from peer's gbox/reader
1175 cli->gbox = NULL;
1176 cli->reader = NULL;
1178 return 0;
1181 static void gbox_send_dcw(struct s_client *cl, ECM_REQUEST *er)
1183 struct s_client *cli = switch_client_proxy(cl, cl->gbox_peer_id);
1184 struct gbox_peer *peer = cli->gbox;
1186 peer->gbox_count_ecm--;
1187 if(er->rc >= E_NOTFOUND)
1189 cs_debug_mask(D_READER, "gbox: unable to decode!");
1190 return;
1193 uchar buf[60];
1194 memset(buf, 0, sizeof(buf));
1196 struct gbox_ecm_request_ext *ere = er->src_data;
1198 gbox_code_cmd(buf, MSG_CW);
1199 buf[2] = peer->gbox.password[0]; //Peer key
1200 buf[3] = peer->gbox.password[1]; //Peer key
1201 buf[4] = peer->gbox.password[2]; //Peer key
1202 buf[5] = peer->gbox.password[3]; //Peer key
1203 buf[6] = er->pid >> 8; //PID
1204 buf[7] = er->pid & 0xff; //PID
1205 buf[8] = er->srvid >> 8; //SrvID
1206 buf[9] = er->srvid & 0xff; //SrvID
1207 buf[10] = ere->gbox_mypeer >> 8; //From peer
1208 buf[11] = ere->gbox_mypeer & 0xff; //From peer
1209 buf[12] = (ere->gbox_slot << 4) | (er->ecm[0] & 0x0f); //slot << 4 | even/odd
1210 buf[13] = ere->gbox_caid >> 8; //CAID first byte
1211 memcpy(buf + 14, er->cw, 16); //CW
1212 buf[30] = er->gbox_crc >> 24; //CRC
1213 buf[31] = er->gbox_crc >> 16; //CRC
1214 buf[32] = er->gbox_crc >> 8; //CRC
1215 buf[33] = er->gbox_crc & 0xff; //CRC
1216 buf[34] = ere->gbox_caid >> 8; //CAID
1217 buf[35] = ere->gbox_caid & 0xff; //CAID
1218 buf[36] = ere->gbox_slot; //Slot
1219 buf[37] = ere->gbox_prid >> 8; //ProvID
1220 buf[38] = ere->gbox_prid & 0xff; //ProvID
1221 buf[39] = ere->gbox_peer >> 8; //Target peer
1222 buf[40] = ere->gbox_peer & 0xff; //Target peer
1223 buf[41] = 0x04; //don't know what this is
1224 buf[42] = 0x33; //don't know what this is
1225 buf[43] = ere->gbox_unknown; //meaning unknown, copied from ECM request
1227 //This copies the routing info from ECM to answer.
1228 //Each hop adds one byte and number of hops is in er->gbox_hops.
1229 memcpy(&buf[44], &ere->gbox_routing_info, ere->gbox_hops - 1);
1230 buf[44 + ere->gbox_hops - 1] = ere->gbox_hops - 1; //Hops
1232 char tmp[0x50];
1233 cs_log("sending dcw to peer : %04x data: %s", er->gbox_peer, cs_hexdump(0, buf, er->gbox_hops + 44, tmp, sizeof(tmp)));
1235 gbox_send(cli, buf, ere->gbox_hops + 44);
1237 cs_debug_mask(D_READER, "-> CW (->%d) from %s/%d (%04X) ", ere->gbox_hops, cli->reader->label, cli->port, ere->gbox_peer);
1240 static uint8_t gbox_next_free_slot(uint16_t id)
1242 LL_ITER it = ll_iter_create(local_gbox.cards);
1243 struct gbox_card *c;
1244 uint8_t lastslot = 0;
1246 while((c = ll_iter_next(&it)))
1248 if(id == c->peer_id && c->slot > lastslot)
1249 { lastslot = c->slot; }
1251 return ++lastslot;
1254 static void gbox_add_local_card(uint16_t id, uint16_t caid, uint32_t prid, uint8_t slot, uint8_t card_reshare, uint8_t dist)
1256 struct gbox_card *c;
1258 //don't insert 0100:000000
1259 if((caid >> 8 == 0x01) && (!prid))
1261 return;
1263 //skip CAID 18XX providers
1264 if((caid >> 8 == 0x18) && (prid))
1266 return;
1268 if(!cs_malloc(&c, sizeof(struct gbox_card)))
1270 return;
1272 c->caid = caid;
1273 switch(caid >> 8)
1275 // Viaccess
1276 case 0x05:
1277 c->provid_1 = (caid >> 8) << 24 | (prid & 0xFFFFFF);
1278 break;
1279 // Cryptoworks
1280 case 0x0D:
1281 c->provid_1 = (caid >> 8) << 24 | (caid & 0xFF) << 16 |
1282 ((prid << 8) & 0xFF00);
1283 break;
1284 default:
1285 c->provid_1 = (caid >> 8) << 24 | (caid & 0xFF) << 16 |
1286 (prid & 0xFFFF);
1287 break;
1289 c->provid = prid;
1290 c->peer_id = id;
1291 c->slot = slot;
1292 c->lvl = card_reshare;
1293 c->dist = dist;
1294 ll_append(local_gbox.cards, c);
1297 static void gbox_local_cards(struct s_client *cli)
1299 int32_t i;
1300 uint32_t prid = 0;
1301 int8_t slot = 0;
1302 char card_reshare;
1303 #ifdef MODULE_CCCAM
1304 LL_ITER it, it2;
1305 struct cc_card *card = NULL;
1306 struct cc_data *cc;
1307 uint32_t checksum = 0;
1308 uint16_t cc_peer_id = 0;
1309 struct cc_provider *provider;
1310 uint8_t *node1 = NULL;
1311 #endif
1313 if(!local_gbox.cards)
1315 gbox_free_cardlist(local_gbox.cards);
1317 local_gbox.cards = ll_create("local_cards");
1319 //value >5 not allowed in gbox network
1320 if(cli->reader->gbox_reshare > 5)
1321 { card_reshare = 5; }
1322 else
1323 { card_reshare = cli->reader->gbox_reshare; }
1325 struct s_client *cl;
1326 for(cl = first_client; cl; cl = cl->next)
1328 if(cl->typ == 'r' && cl->reader && cl->reader->card_status == 2)
1330 slot = gbox_next_free_slot(local_gbox.id);
1331 //SECA, Viaccess and Cryptoworks have multiple providers
1332 if((cl->reader->caid >> 8 == 0x01) || (cl->reader->caid >> 8 == 0x05) ||
1333 (cl->reader->caid >> 8 == 0x0D))
1335 for(i = 0; i < cl->reader->nprov; i++)
1337 prid = cl->reader->prid[i][1] << 16 |
1338 cl->reader->prid[i][2] << 8 | cl->reader->prid[i][3];
1339 gbox_add_local_card(local_gbox.id, cl->reader->caid, prid, slot, card_reshare, 0);
1342 else
1344 gbox_add_local_card(local_gbox.id, cl->reader->caid, 0, slot, card_reshare, 0);
1346 //Check for Betatunnel on gbox account in oscam.user
1347 if (chk_is_betatunnel_caid(cl->reader->caid) == 1 && cli->ttab.n && cl->reader->caid == cli->ttab.bt_caidto[0])
1349 //For now only first entry in tunnel tab. No sense in iteration?
1350 //Add betatunnel card to transmitted list
1351 gbox_add_local_card(local_gbox.id, cli->ttab.bt_caidfrom[0], 0, slot, card_reshare, 0);
1352 cs_debug_mask(D_READER, "gbox created betatunnel card for caid: %04X->%04X",cli->ttab.bt_caidfrom[0],cl->reader->caid);
1355 } //end local readers
1356 #ifdef MODULE_CCCAM
1357 if(cl->typ == 'p' && cl->reader
1358 && cl->reader->typ == R_CCCAM && cl->cc)
1360 cc = cl->cc;
1361 it = ll_iter_create(cc->cards);
1362 while((card = ll_iter_next(&it)))
1364 //calculate gbox id from cc node
1365 //1st node is orgin, shorten to 32Bit by CRC, the GBX-ID like from PW
1366 node1 = ll_has_elements(card->remote_nodes);
1367 checksum = (uint32_t)crc32(0L, node1, 8);
1368 cc_peer_id = ((((checksum >> 24) & 0xFF) ^((checksum >> 8) & 0xFF)) << 8 |
1369 (((checksum >> 16) & 0xFF) ^(checksum & 0xFF)));
1370 slot = gbox_next_free_slot(cc_peer_id);
1371 if((card->caid >> 8 == 0x01) || (card->caid >> 8 == 0x05) ||
1372 (card->caid >> 8 == 0x0D))
1374 it2 = ll_iter_create(card->providers);
1375 while((provider = ll_iter_next(&it2)))
1377 gbox_add_local_card(cc_peer_id, card->caid, provider->prov, slot, card->reshare, card->hop);
1380 else
1381 { gbox_add_local_card(cc_peer_id, card->caid, 0, slot, card->reshare, card->hop); }
1383 } //end cccam
1384 #endif
1385 } //end for clients
1388 static int32_t gbox_client_init(struct s_client *cli)
1390 if(!cfg.gbox_hostname || strlen(cfg.gbox_hostname) > 128)
1392 cs_log("gbox: error, no/invalid hostname '%s' configured in oscam.conf!",
1393 cfg.gbox_hostname ? cfg.gbox_hostname : "");
1394 return -1;
1397 if(!local_gbox.id)
1399 cs_log("gbox: error, no/invalid password '%s' configured in oscam.conf!",
1400 cfg.gbox_my_password ? cfg.gbox_my_password : "");
1401 return -1;
1404 if(!cs_malloc(&cli->gbox, sizeof(struct gbox_peer)))
1405 { return -1; }
1407 struct gbox_peer *peer = cli->gbox;
1408 struct s_reader *rdr = cli->reader;
1410 rdr->card_status = CARD_NEED_INIT;
1411 rdr->tcp_connected = 0;
1413 memset(peer, 0, sizeof(struct gbox_peer));
1415 uint32_t r_pwd = a2i(rdr->r_pwd, 4);
1416 int32_t i;
1417 for(i = 3; i >= 0; i--)
1419 peer->gbox.password[3 - i] = (r_pwd >> (8 * i)) & 0xff;
1422 cs_ddump_mask(D_READER, peer->gbox.password, 4, "Peer password: %s:", rdr->r_pwd);
1424 peer->gbox.id = gbox_convert_password_to_id(&peer->gbox.password[0]);
1425 if (get_gbox_proxy(peer->gbox.id) || peer->gbox.id == NO_GBOX_ID || peer->gbox.id == local_gbox.id)
1427 cs_log("gbox: error, double/invalid gbox id: %04X", peer->gbox.id);
1428 return -1;
1430 cli->gbox_peer_id = peer->gbox.id;
1432 cli->pfd = 0;
1433 cli->crypted = 1;
1435 set_null_ip(&cli->ip);
1437 if((cli->udp_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
1439 cs_log("socket creation failed (errno=%d %s)", errno, strerror(errno));
1440 cs_disconnect_client(cli);
1443 int32_t opt = 1;
1444 setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
1446 #ifdef SO_REUSEPORT
1447 setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEPORT, (void *)&opt, sizeof(opt));
1448 #endif
1450 set_socket_priority(cli->udp_fd, cfg.netprio);
1452 memset((char *)&cli->udp_sa, 0, sizeof(cli->udp_sa));
1454 if(!hostResolve(rdr))
1455 { return 0; }
1457 cli->port = rdr->r_port;
1458 SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
1459 SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)rdr->r_port);
1460 hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
1462 cs_log("proxy %s:%d (fd=%d, peer id=%04x, my id=%04x, my hostname=%s, listen port=%d)",
1463 rdr->device, rdr->r_port, cli->udp_fd, peer->gbox.id, local_gbox.id, cfg.gbox_hostname, rdr->r_port);
1465 cli->pfd = cli->udp_fd;
1467 cs_lock_create(&peer->lock, "gbox_lock", 5000);
1469 peer->online = 0;
1470 peer->ecm_idx = 0;
1471 peer->hello_stat = GBOX_STAT_HELLOL;
1472 peer->my_user = NULL;
1474 cli->reader->card_status = CARD_NEED_INIT;
1475 gbox_send_hello(cli);
1477 if(!cli->reader->gbox_maxecmsend)
1478 { cli->reader->gbox_maxecmsend = DEFAULT_GBOX_MAX_ECM_SEND; }
1480 if(!cli->reader->gbox_maxdist)
1481 { cli->reader->gbox_maxdist = DEFAULT_GBOX_MAX_DIST; }
1483 // create expire thread
1484 pthread_t t;
1485 pthread_attr_t attr;
1486 pthread_attr_init(&attr);
1487 pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
1488 int32_t ret = pthread_create(&t, &attr, (void *)gbox_expire_hello, cli);
1490 if(ret)
1492 cs_log("ERROR: can't create gbox expire thread (errno=%d %s)", ret, strerror(ret));
1493 pthread_attr_destroy(&attr);
1494 return -1;
1496 else
1497 { pthread_detach(t); }
1499 pthread_attr_destroy(&attr);
1501 return 0;
1504 static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t UNUSED(n))
1506 uint16_t id_card = 0;
1507 struct s_client *cl;
1508 if(cli->typ != 'p')
1510 cl = switch_client_proxy(cli, cli->gbox_peer_id);
1512 else
1514 cl = cli;
1516 if(gbox_decode_cmd(data) == MSG_CW)
1518 int i, n;
1519 *rc = 1;
1520 memcpy(dcw, data + 14, 16);
1521 uint32_t crc = data[30] << 24 | data[31] << 16 | data[32] << 8 | data[33];
1522 char tmp[16];
1523 cs_debug_mask(D_READER, "gbox: received cws=%s, peer=%04x, ecm_pid=%04x, sid=%04x, crc=%08x",
1524 cs_hexdump(0, dcw, 16, tmp, sizeof(tmp)), data[10] << 8 | data[11], data[6] << 8 | data[7], data[8] << 8 | data[9], crc);
1526 for(i = 0, n = 0; i < cfg.max_pending && n == 0; i++)
1528 if(!cl->reader->disablecrccws && cl->ecmtask[i].gbox_crc == crc)
1530 id_card = data[10] << 8 | data[11];
1531 gbox_add_good_card(cl, id_card, cl->ecmtask[i].caid, cl->ecmtask[i].prid, cl->ecmtask[i].srvid);
1532 if(cl->ecmtask[i].gbox_ecm_ok == 0 || cl->ecmtask[i].gbox_ecm_ok == 2)
1533 { return -1; }
1534 struct s_ecm_answer ea;
1535 memset(&ea, 0, sizeof(struct s_ecm_answer));
1536 cl->ecmtask[i].gbox_ecm_ok = 2;
1537 memcpy(ea.cw, dcw, 16);
1538 *rc = 1;
1539 return cl->ecmtask[i].idx;
1541 else
1543 id_card = data[10] << 8 | data[11];
1544 gbox_add_good_card(cl, id_card, cl->ecmtask[i].caid, cl->ecmtask[i].prid, cl->ecmtask[i].srvid);
1545 if(cl->ecmtask[i].gbox_ecm_ok == 0 || cl->ecmtask[i].gbox_ecm_ok == 2)
1546 { return -1; }
1547 struct s_ecm_answer ea;
1548 memset(&ea, 0, sizeof(struct s_ecm_answer));
1549 cl->ecmtask[i].gbox_ecm_ok = 2;
1550 memcpy(ea.cw, dcw, 16);
1551 *rc = 1;
1552 cs_debug_mask(D_READER,"WARNING: gbox dcw crc disabled by [reader] config, rcvd-crc=%08X calc-crc=%08X",crc, cl->ecmtask[i].gbox_crc);
1553 return cl->ecmtask[i].idx;
1556 cs_debug_mask(D_READER, "gbox: received corrupted dcw");
1558 return -1;
1561 static int32_t gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er, uchar *UNUSED(buf))
1563 struct gbox_peer *peer = cli->gbox;
1564 int32_t cont_1;
1565 uint32_t sid_verified = 0;
1566 /* struct gbox_ecm_request_ext *ere;
1568 if (!er->src_data) {
1569 if(!cs_malloc(&ere, sizeof(struct gbox_ecm_request_ext)))
1571 cs_writeunlock(&gbox->lock);
1572 return -1;
1574 er->src_data = ere;
1575 gbox_init_ecm_request_ext(ere);
1577 else
1578 ere = er->src_data;
1580 if(!peer || !cli->reader->tcp_connected)
1582 cs_debug_mask(D_READER, "gbox: %s server not init!", cli->reader->label);
1583 write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
1585 return 0;
1588 if(!ll_count(peer->gbox.cards))
1590 cs_debug_mask(D_READER, "gbox: %s NO CARDS!", cli->reader->label);
1591 write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
1592 return 0;
1595 if(!peer->online)
1597 cs_debug_mask(D_READER, "gbox: peer is OFFLINE!");
1598 write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
1599 // gbox_send_hello(cli,0);
1600 return 0;
1603 if(er->gbox_ecm_ok == 2)
1605 cs_debug_mask(D_READER, "gbox: %s replied to this ecm already", cli->reader->label);
1608 if(er->gbox_ecm_id == peer->gbox.id)
1610 cs_debug_mask(D_READER, "gbox: %s provided ecm", cli->reader->label);
1611 write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
1612 return 0;
1615 uint16_t ercaid = er->caid;
1616 uint32_t erprid = er->prid;
1618 if(cli->reader->gbox_maxecmsend == 0)
1620 cli->reader->gbox_maxecmsend = DEFAULT_GBOX_MAX_ECM_SEND;
1623 switch(ercaid >> 8)
1625 //Viaccess
1626 case 0x05:
1627 ercaid = (ercaid & 0xFF00) | ((erprid >> 16) & 0xFF);
1628 erprid = erprid & 0xFFFF;
1629 break;
1630 //Cryptoworks
1631 case 0x0D:
1632 erprid = erprid << 8;
1633 break;
1634 //Nagra
1635 case 0x18:
1636 erprid = 0;
1637 break;
1640 uchar send_buf_1[1024];
1641 int32_t len2;
1643 if(!er->ecmlen) { return 0; }
1645 len2 = er->ecmlen + 18;
1646 er->gbox_crc = gbox_get_ecmchecksum(er);
1648 memset(send_buf_1, 0, sizeof(send_buf_1));
1650 LL_ITER it = ll_iter_create(peer->gbox.cards);
1651 struct gbox_card *card;
1653 int32_t cont_send = 0;
1654 int32_t cont_card_1 = 0;
1656 send_buf_1[0] = MSG_ECM >> 8;
1657 send_buf_1[1] = MSG_ECM & 0xff;
1658 memcpy(send_buf_1 + 2, peer->gbox.password, 4);
1659 memcpy(send_buf_1 + 6, local_gbox.password, 4);
1661 send_buf_1[10] = (er->pid >> 8) & 0xFF;
1662 send_buf_1[11] = er->pid & 0xFF;
1664 send_buf_1[12] = (er->srvid >> 8) & 0xFF;
1665 send_buf_1[13] = er->srvid & 0xFF;
1666 send_buf_1[14] = 0x00;
1667 send_buf_1[15] = 0x00;
1669 send_buf_1[16] = cont_card_1;
1670 send_buf_1[17] = 0x00;
1672 memcpy(send_buf_1 + 18, er->ecm, er->ecmlen);
1674 send_buf_1[len2] = (local_gbox.id >> 8) & 0xff;
1675 send_buf_1[len2 + 1] = local_gbox.id & 0xff;
1676 send_buf_1[len2 + 2] = LOCAL_GBOX_MINOR_VERSION;
1677 send_buf_1[len2 + 3] = 0x00;
1678 send_buf_1[len2 + 4] = LOCAL_GBOX_TYPE;
1680 send_buf_1[len2 + 5] = ercaid >> 8;
1681 send_buf_1[len2 + 6] = ercaid & 0xFF;
1683 send_buf_1[len2 + 7] = (erprid >> 8) & 0xFF;
1684 send_buf_1[len2 + 8] = erprid & 0xFF;
1685 send_buf_1[len2 + 9] = 0x00;
1686 cont_1 = len2 + 10;
1688 struct gbox_srvid *srvid1 = NULL;
1689 while((card = ll_iter_next(&it)))
1691 if(card->caid == er->caid && card->provid == er->prid)
1693 sid_verified = 0;
1695 LL_ITER it2 = ll_iter_create(card->goodsids);
1696 while((srvid1 = ll_iter_next(&it2)))
1698 if(srvid1->provid_id == er->prid && srvid1->sid == er->srvid)
1700 send_buf_1[cont_1] = card->peer_id >> 8;
1701 send_buf_1[cont_1 + 1] = card->peer_id;
1702 send_buf_1[cont_1 + 2] = card->slot;
1703 cont_1 = cont_1 + 3;
1704 cont_card_1++;
1705 cont_send++;
1706 sid_verified = 1;
1707 break;
1711 if(cont_send == cli->reader->gbox_maxecmsend)
1712 { break; }
1714 if(sid_verified == 0)
1716 LL_ITER itt = ll_iter_create(card->badsids);
1717 while((srvid1 = ll_iter_next(&itt)))
1719 if(srvid1->provid_id == er->prid && srvid1->sid == er->srvid)
1721 sid_verified = 1;
1722 break;
1726 if(sid_verified != 1)
1728 send_buf_1[cont_1] = card->peer_id >> 8;
1729 send_buf_1[cont_1 + 1] = card->peer_id;
1730 send_buf_1[cont_1 + 2] = card->slot;
1731 cont_1 = cont_1 + 3;
1732 cont_card_1++;
1733 cont_send++;
1735 if(!cs_malloc(&srvid1, sizeof(struct gbox_srvid)))
1736 { return 0; }
1738 srvid1->sid = er->srvid;
1739 srvid1->peer_idcard = card->peer_id;
1740 srvid1->provid_id = card->provid;
1741 ll_append(card->badsids, srvid1);
1743 if(cont_send == cli->reader->gbox_maxecmsend)
1744 { break; }
1748 if(cont_send == cli->reader->gbox_maxecmsend)
1749 { break; }
1753 if(!cont_card_1)
1755 cs_debug_mask(D_READER, "GBOX: no valid card found for CAID: %04X PROVID: %04X", er->caid, er->prid);
1756 return 0;
1759 send_buf_1[16] = cont_card_1;
1761 //Hops
1762 send_buf_1[cont_1] = 0;
1763 cont_1++;
1765 memcpy(&send_buf_1[cont_1], local_gbox.checkcode, 7);
1766 cont_1 = cont_1 + 7;
1767 memcpy(&send_buf_1[cont_1], peer->gbox.checkcode, 7);
1768 cont_1 = cont_1 + 7;
1770 cs_debug_mask(D_READER, "Gbox sending ecm for %06x : %s", er->prid , cli->reader->label);
1771 er->gbox_ecm_ok = 1;
1772 gbox_send(cli, send_buf_1, cont_1);
1773 cli->pending++;
1774 cli->reader->last_s = time((time_t *) 0);
1776 return 0;
1779 static int32_t gbox_send_emm(EMM_PACKET *UNUSED(ep))
1781 // emms not yet supported
1783 return 0;
1786 //init my gbox with id, password and cards crc
1787 static void init_local_gbox(void)
1789 local_gbox.id = 0;
1790 memset(&local_gbox.password[0], 0, 4);
1791 memset(&local_gbox.checkcode[0], 0, 7);
1792 local_gbox.minor_version = LOCAL_GBOX_MINOR_VERSION;
1793 local_gbox.type = LOCAL_GBOX_TYPE;
1795 if(!cfg.gbox_my_password || strlen(cfg.gbox_my_password) != 8) { return; }
1797 uint32_t key = a2i(cfg.gbox_my_password, 4);
1798 int32_t i;
1799 for(i = 3; i >= 0; i--)
1801 local_gbox.password[3 - i] = (key >> (8 * i)) & 0xff;
1804 cs_ddump_mask(D_READER, local_gbox.password, 4, " My password: %s:", cfg.gbox_my_password);
1806 local_gbox.id = gbox_convert_password_to_id(&local_gbox.password[0]);
1807 if (local_gbox.id == NO_GBOX_ID)
1809 cs_log("gbox: invalid local gbox id: %04X", local_gbox.id);
1813 static void gbox_s_idle(struct s_client *cl)
1815 //prevent users from timing out
1816 //later handle more sophisticated
1817 cs_debug_mask(D_READER, "gbox client idle prevented: %s", username(cl));
1818 cl->last = time((time_t *)0);
1821 void module_gbox(struct s_module *ph)
1823 init_local_gbox();
1824 ph->ptab.nports = 1;
1825 ph->ptab.ports[0].s_port = cfg.gbox_port;
1827 ph->desc = "gbox";
1828 ph->num = R_GBOX;
1829 ph->type = MOD_CONN_UDP;
1830 ph->large_ecm_support = 1;
1831 ph->listenertype = LIS_GBOX;
1833 ph->s_handler = gbox_server;
1834 ph->s_init = gbox_server_init;
1836 ph->send_dcw = gbox_send_dcw;
1838 ph->recv = gbox_recv;
1839 ph->c_init = gbox_client_init;
1840 ph->c_recv_chk = gbox_recv_chk;
1841 ph->c_send_ecm = gbox_send_ecm;
1842 ph->c_send_emm = gbox_send_emm;
1844 ph->s_idle = gbox_s_idle;
1846 #endif