1 | #define MODULE_LOG_PREFIX "gbox"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 | #ifdef MODULE_GBOX
|
---|
5 |
|
---|
6 | #include "module-gbox.h"
|
---|
7 | #include "module-gbox-helper.h"
|
---|
8 | #include "module-gbox-sms.h"
|
---|
9 | #include "module-gbox-cards.h"
|
---|
10 | #include "module-cccam.h"
|
---|
11 | #include "module-cccam-data.h"
|
---|
12 | #include "oscam-failban.h"
|
---|
13 | #include "oscam-client.h"
|
---|
14 | #include "oscam-ecm.h"
|
---|
15 | #include "oscam-lock.h"
|
---|
16 | #include "oscam-net.h"
|
---|
17 | #include "oscam-chk.h"
|
---|
18 | #include "oscam-string.h"
|
---|
19 | #include "oscam-time.h"
|
---|
20 | #include "oscam-reader.h"
|
---|
21 | #include "oscam-garbage.h"
|
---|
22 | #include "oscam-files.h"
|
---|
23 |
|
---|
24 | #define RECEIVE_BUFFER_SIZE 1024
|
---|
25 | #define MIN_GBOX_MESSAGE_LENGTH 10 //CMD + pw + pw. TODO: Check if is really min
|
---|
26 | #define MIN_ECM_LENGTH 8
|
---|
27 | #define HELLO_KEEPALIVE_TIME 120 //send hello to peer every 2 min in case no ecm received
|
---|
28 | #define STATS_WRITE_TIME 300 //write stats file every 5 min
|
---|
29 |
|
---|
30 | #define LOCAL_GBOX_MAJOR_VERSION 0x02
|
---|
31 |
|
---|
32 | static struct gbox_data local_gbox;
|
---|
33 | static uint8_t local_gbox_initialized = 0;
|
---|
34 | static time_t last_stats_written;
|
---|
35 |
|
---|
36 | static int32_t gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er, uchar *UNUSED(buf));
|
---|
37 |
|
---|
38 | char *get_gbox_tmp_fname(char *fext)
|
---|
39 | {
|
---|
40 | static char gbox_tmpfile_buf[64] = { 0 };
|
---|
41 | const char *slash = "/";
|
---|
42 | if(!cfg.gbox_tmp_dir)
|
---|
43 | {
|
---|
44 | snprintf(gbox_tmpfile_buf, sizeof(gbox_tmpfile_buf), "%s%s%s",get_tmp_dir(), slash, fext);
|
---|
45 | }
|
---|
46 | else
|
---|
47 | {
|
---|
48 | if(cfg.gbox_tmp_dir[strlen(cfg.gbox_tmp_dir) - 1] == '/') { slash = ""; }
|
---|
49 | snprintf(gbox_tmpfile_buf, sizeof(gbox_tmpfile_buf), "%s%s%s", cfg.gbox_tmp_dir, slash, fext);
|
---|
50 | }
|
---|
51 | return gbox_tmpfile_buf;
|
---|
52 | }
|
---|
53 |
|
---|
54 | uint16_t gbox_get_local_gbox_id(void)
|
---|
55 | {
|
---|
56 | return local_gbox.id;
|
---|
57 | }
|
---|
58 |
|
---|
59 | uint32_t gbox_get_local_gbox_password(void)
|
---|
60 | {
|
---|
61 | return local_gbox.password;
|
---|
62 | }
|
---|
63 |
|
---|
64 | static uint8_t gbox_get_my_vers (void)
|
---|
65 | {
|
---|
66 | uint8_t gbx_vers = a2i(cfg.gbox_my_vers,1);
|
---|
67 |
|
---|
68 | return gbx_vers;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static uint8_t gbox_get_my_cpu_api (void)
|
---|
72 | {
|
---|
73 | /* For configurable later adapt according to these functions:
|
---|
74 | unsigned char *GboxAPI( unsigned char a ) {
|
---|
75 | a = a & 7 ;
|
---|
76 | switch ( a ) {
|
---|
77 | case 0 : strcpy ( s_24,"No API");
|
---|
78 | break;
|
---|
79 | case 1 : strcpy ( s_24,"API 1");
|
---|
80 | break;
|
---|
81 | case 2 : strcpy ( s_24,"API 2");
|
---|
82 | break;
|
---|
83 | case 3 : strcpy ( s_24,"API 3");
|
---|
84 | break;
|
---|
85 | case 4 : strcpy ( s_24,"IBM API");
|
---|
86 | break;
|
---|
87 | default : strcpy ( s_24," ");
|
---|
88 | }
|
---|
89 | return s_24 ;
|
---|
90 | }
|
---|
91 |
|
---|
92 | unsigned char *GboxCPU( unsigned char a ) {
|
---|
93 | a = a & 112 ;
|
---|
94 | a = a >> 4 ;
|
---|
95 | switch ( a ) {
|
---|
96 | case 1 : strcpy ( s_23,"80X86 compatible CPU");
|
---|
97 | break;
|
---|
98 | case 2 : strcpy ( s_23,"Motorola PowerPC MPC823 CPU");
|
---|
99 | break;
|
---|
100 | case 3 : strcpy ( s_23,"IBM PowerPC STB CPU");
|
---|
101 | break;
|
---|
102 | default : strcpy ( s_23," ");
|
---|
103 | }
|
---|
104 | return s_23:
|
---|
105 | }
|
---|
106 | */
|
---|
107 | return a2i(cfg.gbox_my_cpu_api,1);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void write_goodnight_to_osd_file(struct s_client *cli)
|
---|
111 | {
|
---|
112 | char *fext= FILE_GOODNIGHT_OSD;
|
---|
113 | char *fname = get_gbox_tmp_fname(fext);
|
---|
114 | if (file_exists(fname))
|
---|
115 | {
|
---|
116 | char buf[50];
|
---|
117 | memset(buf, 0, sizeof(buf));
|
---|
118 | snprintf(buf, sizeof(buf), "%s %s %s", fname, username(cli), cli->reader->device);
|
---|
119 | cs_log_dbg(D_READER, "found file %s - write goodnight info from %s %s to OSD", fname, username(cli),cli->reader->device);
|
---|
120 | char *cmd = buf;
|
---|
121 | FILE *p;
|
---|
122 | if ((p = popen(cmd, "w")) == NULL)
|
---|
123 | {
|
---|
124 | cs_log("Error %s",fname);
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | pclose(p);
|
---|
128 | }
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void gbox_write_peer_onl(void)
|
---|
133 | {
|
---|
134 | char *fext= FILE_GBOX_PEER_ONL;
|
---|
135 | char *fname = get_gbox_tmp_fname(fext);
|
---|
136 | FILE *fhandle = fopen(fname, "w");
|
---|
137 | if(!fhandle)
|
---|
138 | {
|
---|
139 | cs_log("Couldn't open %s: %s", fname, strerror(errno));
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | cs_readlock(&clientlist_lock);
|
---|
143 | struct s_client *cl;
|
---|
144 | for(cl = first_client; cl; cl = cl->next)
|
---|
145 | {
|
---|
146 | if(cl->gbox && cl->typ == 'p')
|
---|
147 | {
|
---|
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); }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | cs_readunlock(&clientlist_lock);
|
---|
156 | fclose(fhandle);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void gbox_write_version(void)
|
---|
161 | {
|
---|
162 | char *fext= FILE_GBOX_VERSION;
|
---|
163 | char *fname = get_gbox_tmp_fname(fext);
|
---|
164 | FILE *fhandle = fopen(fname, "w");
|
---|
165 | if(!fhandle)
|
---|
166 | {
|
---|
167 | cs_log("Couldn't open %s: %s", get_gbox_tmp_fname(FILE_GBOX_VERSION), strerror(errno));
|
---|
168 | return;
|
---|
169 | }
|
---|
170 | fprintf(fhandle, "%02X.%02X\n", LOCAL_GBOX_MAJOR_VERSION, gbox_get_my_vers());
|
---|
171 | fclose(fhandle);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void hostname2ip(char *hostname, IN_ADDR_T *ip)
|
---|
175 | {
|
---|
176 | cs_resolve(hostname, ip, NULL, NULL);
|
---|
177 | }
|
---|
178 |
|
---|
179 | static uint16_t gbox_convert_password_to_id(uint32_t password)
|
---|
180 | {
|
---|
181 | return (((password >> 24) & 0xff) ^ ((password >> 8) & 0xff)) << 8 | (((password >> 16) & 0xff) ^ (password & 0xff));
|
---|
182 | }
|
---|
183 |
|
---|
184 | static int8_t gbox_remove_all_bad_sids(ECM_REQUEST *er, uint16_t sid)
|
---|
185 | {
|
---|
186 | if (!er) { return -1; }
|
---|
187 |
|
---|
188 | struct gbox_card_pending *pending = NULL;
|
---|
189 | LL_LOCKITER *li = ll_li_create(er->gbox_cards_pending, 0);
|
---|
190 | while ((pending = ll_li_next(li)))
|
---|
191 | { gbox_remove_bad_sid(pending->id.peer, pending->id.slot, sid); }
|
---|
192 | ll_li_destroy(li);
|
---|
193 | return 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | void gbox_free_cards_pending(ECM_REQUEST *er)
|
---|
197 | {
|
---|
198 | ll_destroy_free_data(&er->gbox_cards_pending);
|
---|
199 | }
|
---|
200 |
|
---|
201 | void gbox_init_ecm_request_ext(struct gbox_ecm_request_ext *ere)
|
---|
202 | {
|
---|
203 | ere->gbox_hops = 0;
|
---|
204 | ere->gbox_peer = 0;
|
---|
205 | ere->gbox_mypeer = 0;
|
---|
206 | ere->gbox_slot = 0;
|
---|
207 | ere->gbox_version = 0;
|
---|
208 | ere->gbox_unknown = 0;
|
---|
209 | ere->gbox_type = 0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | struct s_client *get_gbox_proxy(uint16_t gbox_id)
|
---|
213 | {
|
---|
214 | struct s_client *cl;
|
---|
215 | struct s_client *found = NULL;
|
---|
216 | cs_readlock(&clientlist_lock);
|
---|
217 | for(cl = first_client; cl; cl = cl->next)
|
---|
218 | {
|
---|
219 | if(cl->typ == 'p' && cl->gbox && cl->gbox_peer_id == gbox_id)
|
---|
220 | {
|
---|
221 | found = cl;
|
---|
222 | break;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | cs_readunlock(&clientlist_lock);
|
---|
226 | return found;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static int8_t gbox_peer_online(struct gbox_peer *peer, uint8_t online)
|
---|
230 | {
|
---|
231 | if (!peer) { return -1; }
|
---|
232 |
|
---|
233 | peer->online = online;
|
---|
234 | gbox_write_peer_onl();
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static int8_t gbox_reinit_peer(struct gbox_peer *peer)
|
---|
239 | {
|
---|
240 | if (!peer) { return -1; }
|
---|
241 |
|
---|
242 | peer->ecm_idx = 0;
|
---|
243 | peer->next_hello = 0;
|
---|
244 | gbox_delete_cards(GBOX_DELETE_FROM_PEER, peer->gbox.id);
|
---|
245 | gbox_peer_online(peer, GBOX_PEER_OFFLINE);
|
---|
246 |
|
---|
247 | return 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static int8_t gbox_reinit_proxy(struct s_client *proxy)
|
---|
251 | {
|
---|
252 | if (!proxy) { return -1; }
|
---|
253 |
|
---|
254 | struct gbox_peer *peer = proxy->gbox;
|
---|
255 | gbox_reinit_peer(peer);
|
---|
256 | if (!proxy->reader) { return -1; }
|
---|
257 | proxy->reader->tcp_connected = 0;
|
---|
258 | proxy->reader->card_status = NO_CARD;
|
---|
259 | proxy->reader->last_s = proxy->reader->last_g = 0;
|
---|
260 |
|
---|
261 | return 0;
|
---|
262 | }
|
---|
263 |
|
---|
264 | void gbox_send(struct s_client *cli, uchar *buf, int32_t l)
|
---|
265 | {
|
---|
266 | struct gbox_peer *peer = cli->gbox;
|
---|
267 |
|
---|
268 | cs_log_dump_dbg(D_READER, buf, l, "<- decrypted data (%d bytes):", l);
|
---|
269 |
|
---|
270 | hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
|
---|
271 | SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
|
---|
272 | SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)cli->reader->r_port);
|
---|
273 |
|
---|
274 | gbox_encrypt(buf, l, peer->gbox.password);
|
---|
275 | sendto(cli->udp_fd, buf, l, 0, (struct sockaddr *)&cli->udp_sa, cli->udp_sa_len);
|
---|
276 | cs_log_dump_dbg(D_READER, buf, l, "<- encrypted data (%d bytes):", l);
|
---|
277 | }
|
---|
278 |
|
---|
279 | void gbox_send_hello_packet(struct s_client *cli, int8_t number, uchar *outbuf, uchar *ptr, int32_t nbcards, uint8_t hello_stat)
|
---|
280 | {
|
---|
281 | struct gbox_peer *peer = cli->gbox;
|
---|
282 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
283 | int32_t len;
|
---|
284 | gbox_message_header(outbuf, MSG_HELLO, peer->gbox.password, local_gbox.password);
|
---|
285 | // initial HELLO = 0, subsequent = 1
|
---|
286 | if(hello_stat > GBOX_STAT_HELLOS)
|
---|
287 | { outbuf[10] = 1; }
|
---|
288 | else
|
---|
289 | { outbuf[10] = 0; }
|
---|
290 | outbuf[11] = number; // 0x80 (if last packet) else 0x00 | packet number
|
---|
291 |
|
---|
292 | if((number & 0x0F) == 0)
|
---|
293 | {
|
---|
294 | if(hello_stat != GBOX_STAT_HELLOL)
|
---|
295 | { memcpy(++ptr, gbox_get_checkcode(), 7); }
|
---|
296 | else
|
---|
297 | { memset(++ptr, 0, 7); }
|
---|
298 | ptr += 7;
|
---|
299 | *ptr = local_gbox.minor_version;
|
---|
300 | *(++ptr) = local_gbox.cpu_api;
|
---|
301 | memcpy(++ptr, cfg.gbox_hostname, hostname_len);
|
---|
302 | ptr += hostname_len;
|
---|
303 | *ptr = hostname_len;
|
---|
304 | }
|
---|
305 | len = ptr - outbuf + 1;
|
---|
306 | switch(hello_stat)
|
---|
307 | {
|
---|
308 | case GBOX_STAT_HELLOL:
|
---|
309 | cs_log("<- HelloL to %s", cli->reader->label);
|
---|
310 | break;
|
---|
311 | case GBOX_STAT_HELLOS:
|
---|
312 | cs_log("<- HelloS total cards %d to %s", nbcards, cli->reader->label);
|
---|
313 | break;
|
---|
314 | case GBOX_STAT_HELLOR:
|
---|
315 | cs_log("<- HelloR total cards %d to %s", nbcards, cli->reader->label);
|
---|
316 | break;
|
---|
317 | default:
|
---|
318 | cs_log("<- hello total cards %d to %s", nbcards, cli->reader->label);
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | cs_log_dump_dbg(D_READER, outbuf, len, "<- hello, (len=%d):", len);
|
---|
322 |
|
---|
323 | gbox_compress(outbuf, len, &len);
|
---|
324 |
|
---|
325 | gbox_send(cli, outbuf, len);
|
---|
326 | }
|
---|
327 |
|
---|
328 | void gbox_send_hello(struct s_client *proxy, uint8_t hello_stat)
|
---|
329 | {
|
---|
330 | if (!proxy)
|
---|
331 | {
|
---|
332 | cs_log("Invalid call to gbox_send_hello with proxy");
|
---|
333 | return;
|
---|
334 | }
|
---|
335 | uint16_t nbcards = 0;
|
---|
336 | uint8_t packet;
|
---|
337 | uchar buf[2048];
|
---|
338 | packet = 0;
|
---|
339 | uchar *ptr = buf + 11;
|
---|
340 | if(gbox_count_cards() != 0 && hello_stat > GBOX_STAT_HELLOL)
|
---|
341 | {
|
---|
342 | struct gbox_peer *peer = proxy->gbox;
|
---|
343 | if (!peer || !peer->my_user || !peer->my_user->account)
|
---|
344 | {
|
---|
345 | cs_log("Invalid call to gbox_send_hello with peer");
|
---|
346 | return;
|
---|
347 | }
|
---|
348 | memset(buf, 0, sizeof(buf));
|
---|
349 | struct gbox_card *card;
|
---|
350 | GBOX_CARDS_ITER *gci = gbox_cards_iter_create();
|
---|
351 | while((card = gbox_cards_iter_next(gci)))
|
---|
352 | {
|
---|
353 | //send to user only cards which matching CAID from account and lvl > 0
|
---|
354 | //do not send peer cards back
|
---|
355 | if(chk_ctab(gbox_get_caid(card->caprovid), &peer->my_user->account->ctab) && (card->lvl > 0) &&
|
---|
356 | (!card->origin_peer || (card->origin_peer && card->origin_peer->gbox.id != peer->gbox.id)))
|
---|
357 | {
|
---|
358 | *(++ptr) = card->caprovid >> 24;
|
---|
359 | *(++ptr) = card->caprovid >> 16;
|
---|
360 | *(++ptr) = card->caprovid >> 8;
|
---|
361 | *(++ptr) = card->caprovid & 0xff;
|
---|
362 | *(++ptr) = 1; //note: original gbx is more efficient and sends all cards of one caid as package
|
---|
363 | *(++ptr) = card->id.slot;
|
---|
364 | *(++ptr) = ((card->lvl - 1) << 4) + card->dist + 1;
|
---|
365 | *(++ptr) = card->id.peer >> 8;
|
---|
366 | *(++ptr) = card->id.peer & 0xff;
|
---|
367 | nbcards++;
|
---|
368 | if(nbcards == 100) //check if 100 is good or we need more sophisticated algorithm
|
---|
369 | {
|
---|
370 | gbox_send_hello_packet(proxy, packet, buf, ptr, nbcards, hello_stat);
|
---|
371 | packet++;
|
---|
372 | nbcards = 0;
|
---|
373 | ptr = buf + 11;
|
---|
374 | memset(buf, 0, sizeof(buf));
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 | gbox_cards_iter_destroy(gci);
|
---|
379 | } // end if local card exists
|
---|
380 | //last packet has bit 0x80 set
|
---|
381 | gbox_send_hello_packet(proxy, 0x80 | packet, buf, ptr, nbcards, hello_stat);
|
---|
382 | return;
|
---|
383 | }
|
---|
384 |
|
---|
385 | void gbox_reconnect_client(uint16_t gbox_id)
|
---|
386 | {
|
---|
387 | struct s_client *cl;
|
---|
388 | cs_readlock(&clientlist_lock);
|
---|
389 | for(cl = first_client; cl; cl = cl->next)
|
---|
390 | {
|
---|
391 | if(cl->gbox && cl->typ == 'p' && cl->gbox_peer_id == gbox_id)
|
---|
392 | {
|
---|
393 | hostname2ip(cl->reader->device, &SIN_GET_ADDR(cl->udp_sa));
|
---|
394 | SIN_GET_FAMILY(cl->udp_sa) = AF_INET;
|
---|
395 | SIN_GET_PORT(cl->udp_sa) = htons((uint16_t)cl->reader->r_port);
|
---|
396 | hostname2ip(cl->reader->device, &(cl->ip));
|
---|
397 | gbox_reinit_proxy(cl);
|
---|
398 | gbox_send_hello(cl, GBOX_STAT_HELLOL);
|
---|
399 | }
|
---|
400 | }
|
---|
401 | cs_readunlock(&clientlist_lock);
|
---|
402 | }
|
---|
403 |
|
---|
404 | static void *gbox_server(struct s_client *cli, uchar *UNUSED(b), int32_t l)
|
---|
405 | {
|
---|
406 | if(l > 0)
|
---|
407 | {
|
---|
408 | cs_log("gbox_server %s/%d", cli->reader->label, cli->port);
|
---|
409 | // gbox_check_header(cli, NULL, b, l);
|
---|
410 | }
|
---|
411 | return 0;
|
---|
412 | }
|
---|
413 |
|
---|
414 | char *gbox_username(struct s_client *client)
|
---|
415 | {
|
---|
416 | if(!client) { return "anonymous"; }
|
---|
417 | if(client->reader)
|
---|
418 | if(client->reader->r_usr[0])
|
---|
419 | { return client->reader->r_usr; }
|
---|
420 | return "anonymous";
|
---|
421 | }
|
---|
422 |
|
---|
423 | static int8_t gbox_disconnect_double_peers(struct s_client *cli)
|
---|
424 | {
|
---|
425 | struct s_client *cl;
|
---|
426 | cs_writelock(&clientlist_lock);
|
---|
427 | for(cl = first_client; cl; cl = cl->next)
|
---|
428 | {
|
---|
429 | if (cl->typ == 'c' && cl->gbox_peer_id == cli->gbox_peer_id && cl != cli)
|
---|
430 | {
|
---|
431 | cl->reader = NULL;
|
---|
432 | cl->gbox = NULL;
|
---|
433 | cs_log_dbg(D_READER, "disconnected double client %s",username(cl));
|
---|
434 | cs_disconnect_client(cl);
|
---|
435 | }
|
---|
436 | }
|
---|
437 | cs_writeunlock(&clientlist_lock);
|
---|
438 | return 0;
|
---|
439 | }
|
---|
440 |
|
---|
441 | static int8_t gbox_auth_client(struct s_client *cli, uint32_t gbox_password)
|
---|
442 | {
|
---|
443 | if (!cli) { return -1; }
|
---|
444 |
|
---|
445 | uint16_t gbox_id = gbox_convert_password_to_id(gbox_password);
|
---|
446 | struct s_client *cl = get_gbox_proxy(gbox_id);
|
---|
447 |
|
---|
448 | if(cl->typ == 'p' && cl->gbox && cl->reader)
|
---|
449 | {
|
---|
450 | struct gbox_peer *peer = cl->gbox;
|
---|
451 | struct s_auth *account = get_account_by_name(gbox_username(cl));
|
---|
452 |
|
---|
453 | if ((peer->gbox.password == gbox_password) && account)
|
---|
454 | {
|
---|
455 | cli->crypted = 1; //display as crypted
|
---|
456 | cli->gbox = cl->gbox; //point to the same gbox as proxy
|
---|
457 | cli->reader = cl->reader; //point to the same reader as proxy
|
---|
458 | cli->gbox_peer_id = cl->gbox_peer_id; //signal authenticated
|
---|
459 | gbox_disconnect_double_peers(cli);
|
---|
460 | cs_auth_client(cli, account, NULL);
|
---|
461 | cli->account = account;
|
---|
462 | cli->grp = account->grp;
|
---|
463 | cli->lastecm = time(NULL);
|
---|
464 | peer->my_user = cli;
|
---|
465 | return 0;
|
---|
466 | }
|
---|
467 | }
|
---|
468 | return -1;
|
---|
469 | }
|
---|
470 |
|
---|
471 | static void gbox_server_init(struct s_client *cl)
|
---|
472 | {
|
---|
473 | if(!cl->init_done)
|
---|
474 | {
|
---|
475 | if(IP_ISSET(cl->ip))
|
---|
476 | { cs_log("new connection from %s", cs_inet_ntoa(cl->ip)); }
|
---|
477 | //We cannot authenticate here, because we don't know gbox pw
|
---|
478 | cl->gbox_peer_id = NO_GBOX_ID;
|
---|
479 | cl->init_done = 1;
|
---|
480 | }
|
---|
481 | return;
|
---|
482 | }
|
---|
483 |
|
---|
484 | static uint16_t gbox_decode_cmd(uchar *buf)
|
---|
485 | {
|
---|
486 | return buf[0] << 8 | buf[1];
|
---|
487 | }
|
---|
488 |
|
---|
489 | int8_t gbox_message_header(uchar *buf, uint16_t cmd, uint32_t peer_password, uint32_t local_password)
|
---|
490 | {
|
---|
491 | if (!buf) { return -1; }
|
---|
492 | i2b_buf(2, cmd, buf);
|
---|
493 | if (cmd == MSG_GSMS_1) { return 0; }
|
---|
494 | i2b_buf(4, peer_password, buf + 2);
|
---|
495 | if (cmd == MSG_CW) { return 0; }
|
---|
496 | i2b_buf(4, local_password, buf + 6);
|
---|
497 | return 0;
|
---|
498 | }
|
---|
499 |
|
---|
500 | //returns number of cards in a hello packet or -1 in case of error
|
---|
501 | int16_t read_cards_from_hello(uint8_t *ptr, uint8_t *len, CAIDTAB *ctab, uint8_t maxdist, struct gbox_peer *peer)
|
---|
502 | {
|
---|
503 | uint8_t *current_ptr = 0;
|
---|
504 | uint32_t caprovid;
|
---|
505 | int16_t ncards_in_msg = 0;
|
---|
506 |
|
---|
507 | while(ptr < len)
|
---|
508 | {
|
---|
509 | caprovid = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
|
---|
510 |
|
---|
511 | ncards_in_msg += ptr[4];
|
---|
512 | //caid check
|
---|
513 | if(chk_ctab(gbox_get_caid(caprovid), ctab))
|
---|
514 | {
|
---|
515 | current_ptr = ptr;
|
---|
516 | ptr += 5;
|
---|
517 |
|
---|
518 | // for all cards of current caid/provid,
|
---|
519 | while (ptr < current_ptr + 5 + current_ptr[4] * 4)
|
---|
520 | {
|
---|
521 | if ((ptr[1] & 0xf) <= maxdist)
|
---|
522 | { gbox_add_card(ptr[2] << 8 | ptr[3], caprovid, ptr[0], ptr[1] >> 4, ptr[1] & 0xf, GBOX_CARD_TYPE_GBOX, peer); }
|
---|
523 | ptr += 4; //next card
|
---|
524 | } // end while cards for provider
|
---|
525 | }
|
---|
526 | else
|
---|
527 | { ptr += 5 + ptr[4] * 4; } //skip cards because caid
|
---|
528 | } // end while < len
|
---|
529 | return ncards_in_msg;
|
---|
530 | }
|
---|
531 |
|
---|
532 | //returns 1 if checkcode changed / 0 if not
|
---|
533 | static int32_t gbox_checkcode_recv(struct s_client *cli, uchar *checkcode)
|
---|
534 | {
|
---|
535 | struct gbox_peer *peer = cli->gbox;
|
---|
536 | char tmp[14];
|
---|
537 |
|
---|
538 | if(memcmp(peer->checkcode, checkcode, 7))
|
---|
539 | {
|
---|
540 | memcpy(peer->checkcode, checkcode, 7);
|
---|
541 | cs_log_dbg(D_READER, "-> new checkcode=%s", cs_hexdump(0, peer->checkcode, 14, tmp, sizeof(tmp)));
|
---|
542 | return 1;
|
---|
543 | }
|
---|
544 | return 0;
|
---|
545 | }
|
---|
546 |
|
---|
547 | static void gbox_send_checkcode(struct s_client *cli)
|
---|
548 | {
|
---|
549 | struct gbox_peer *peer = cli->gbox;
|
---|
550 | uchar outbuf[20];
|
---|
551 |
|
---|
552 | gbox_message_header(outbuf, MSG_CHECKCODE, peer->gbox.password, local_gbox.password);
|
---|
553 | memcpy(outbuf + 10, gbox_get_checkcode(), 7);
|
---|
554 |
|
---|
555 | gbox_send(cli, outbuf, 17);
|
---|
556 | }
|
---|
557 |
|
---|
558 | int32_t gbox_cmd_hello(struct s_client *cli, uchar *data, int32_t n)
|
---|
559 | {
|
---|
560 | if (!cli || !cli->gbox || !cli->reader || !data) { return -1; }
|
---|
561 |
|
---|
562 | struct gbox_peer *peer = cli->gbox;
|
---|
563 | int16_t cards_number = 0;
|
---|
564 | int32_t payload_len = n;
|
---|
565 | int32_t hostname_len = 0;
|
---|
566 | int32_t footer_len = 0;
|
---|
567 | uint8_t *ptr = 0;
|
---|
568 |
|
---|
569 | if(!(gbox_decode_cmd(data) == MSG_HELLO1))
|
---|
570 | {
|
---|
571 | gbox_decompress(data, &payload_len);
|
---|
572 | ptr = data + 12;
|
---|
573 | }
|
---|
574 | else
|
---|
575 | { ptr = data + 11; }
|
---|
576 | cs_log_dump_dbg(D_READER, data, payload_len, "decompressed data (%d bytes):", payload_len);
|
---|
577 |
|
---|
578 | if ((data[11] & 0xf) != peer->next_hello) //out of sync hellos
|
---|
579 | {
|
---|
580 | cs_log("-> out of sync hello from %s %s, expected: %02X, received: %02X"
|
---|
581 | ,username(cli), cli->reader->device, peer->next_hello, data[11] & 0xf);
|
---|
582 | peer->next_hello = 0;
|
---|
583 | gbox_send_hello(cli, GBOX_STAT_HELLOL);
|
---|
584 | return 0;
|
---|
585 | }
|
---|
586 |
|
---|
587 | if (!(data[11] & 0xf)) //is first packet
|
---|
588 | {
|
---|
589 | gbox_delete_cards(GBOX_DELETE_FROM_PEER, peer->gbox.id);
|
---|
590 | hostname_len = data[payload_len - 1];
|
---|
591 | footer_len = hostname_len + 2 + 7;
|
---|
592 | if(!peer->hostname || memcmp(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len))
|
---|
593 | {
|
---|
594 | NULLFREE(peer->hostname);
|
---|
595 | if(!cs_malloc(&peer->hostname, hostname_len + 1))
|
---|
596 | {
|
---|
597 | cs_writeunlock(&peer->lock);
|
---|
598 | return -1;
|
---|
599 | }
|
---|
600 | memcpy(peer->hostname, data + payload_len - 1 - hostname_len, hostname_len);
|
---|
601 | peer->hostname[hostname_len] = '\0';
|
---|
602 | }
|
---|
603 | gbox_checkcode_recv(cli, data + payload_len - footer_len - 1);
|
---|
604 | peer->gbox.minor_version = data[payload_len - footer_len - 1 + 7];
|
---|
605 | peer->gbox.cpu_api = data[payload_len - footer_len + 7];
|
---|
606 | peer->total_cards = 0;
|
---|
607 | }
|
---|
608 |
|
---|
609 | cs_log_dbg(D_READER, "-> Hello packet no. %d received", (data[11] & 0xF) + 1);
|
---|
610 | // read cards from hello
|
---|
611 | cards_number = read_cards_from_hello(ptr, data + payload_len - footer_len - 1, &cli->reader->ctab, cli->reader->gbox_maxdist, peer);
|
---|
612 | if (cards_number < 0)
|
---|
613 | { return -1; }
|
---|
614 | else
|
---|
615 | { peer->total_cards += cards_number; }
|
---|
616 |
|
---|
617 | if(data[11] & 0x80) //last packet
|
---|
618 | {
|
---|
619 | uchar tmpbuf[8];
|
---|
620 | memset(&tmpbuf[0], 0xff, 7);
|
---|
621 | if(data[10] == 0x01 && !memcmp(data+12,tmpbuf,7)) //good night message
|
---|
622 | {
|
---|
623 | //This is a good night / reset packet (good night data[0xA] / reset !data[0xA]
|
---|
624 | cs_log("-> Good Night from %s %s",username(cli), cli->reader->device);
|
---|
625 | write_goodnight_to_osd_file(cli);
|
---|
626 | gbox_reinit_proxy(cli);
|
---|
627 | }
|
---|
628 | else //last packet of Hello
|
---|
629 | {
|
---|
630 | peer->filtered_cards = gbox_count_peer_cards(peer->gbox.id);
|
---|
631 | if(!data[10])
|
---|
632 | {
|
---|
633 | memset(&tmpbuf[0], 0, 7);
|
---|
634 | if (data[11] == 0x80 && !memcmp(data+12,tmpbuf,7))
|
---|
635 | {
|
---|
636 | cs_log("-> HelloL 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, peer->total_cards, peer->filtered_cards);
|
---|
637 | gbox_peer_online(peer, GBOX_PEER_ONLINE);
|
---|
638 | }
|
---|
639 | else
|
---|
640 | { 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, peer->total_cards, peer->filtered_cards); }
|
---|
641 | gbox_send_hello(cli, GBOX_STAT_HELLOR);
|
---|
642 | }
|
---|
643 | else
|
---|
644 | {
|
---|
645 | 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, peer->total_cards, peer->filtered_cards);
|
---|
646 | gbox_send_checkcode(cli);
|
---|
647 | }
|
---|
648 | if(!peer->online)
|
---|
649 | {
|
---|
650 | gbox_send_hello(cli, GBOX_STAT_HELLOS);
|
---|
651 | gbox_peer_online(peer, GBOX_PEER_ONLINE);
|
---|
652 | }
|
---|
653 | cli->reader->tcp_connected = 2; //we have card
|
---|
654 | if(!peer->filtered_cards)
|
---|
655 | { cli->reader->card_status = NO_CARD; }
|
---|
656 | else
|
---|
657 | { cli->reader->card_status = CARD_INSERTED; }
|
---|
658 | }
|
---|
659 | peer->next_hello = 0;
|
---|
660 | gbox_write_share_cards_info();
|
---|
661 | cli->last = time((time_t *)0); //hello is activity on proxy
|
---|
662 | }
|
---|
663 | else { peer->next_hello++; }
|
---|
664 | return 0;
|
---|
665 | }
|
---|
666 |
|
---|
667 | static int8_t is_blocked_peer(uint16_t peer)
|
---|
668 | {
|
---|
669 | if (peer == NO_GBOX_ID) { return 1; }
|
---|
670 | else { return 0; }
|
---|
671 | }
|
---|
672 |
|
---|
673 | static int8_t gbox_incoming_ecm(struct s_client *cli, uchar *data, int32_t n)
|
---|
674 | {
|
---|
675 | if (!cli || !cli->gbox || !data || !cli->reader) { return -1; }
|
---|
676 |
|
---|
677 | struct gbox_peer *peer;
|
---|
678 | struct s_client *cl;
|
---|
679 | int32_t diffcheck = 0;
|
---|
680 |
|
---|
681 | peer = cli->gbox;
|
---|
682 | if (!peer || !peer->my_user) { return -1; }
|
---|
683 | cl = peer->my_user;
|
---|
684 |
|
---|
685 | // No ECMs with length < MIN_LENGTH expected
|
---|
686 | if ((((data[19] & 0x0f) << 8) | data[20]) < MIN_ECM_LENGTH) { return -1; }
|
---|
687 |
|
---|
688 | // GBOX_MAX_HOPS not violated
|
---|
689 | if (data[n - 15] + 1 > GBOX_MAXHOPS) { return -1; }
|
---|
690 |
|
---|
691 | // ECM must not take more hops than allowed by gbox_reshare
|
---|
692 | if (data[n - 15] + 1 > cli->reader->gbox_reshare)
|
---|
693 | {
|
---|
694 | cs_log("-> ECM took more hops than allowed from gbox_reshare. Hops stealing detected!");
|
---|
695 | return -1;
|
---|
696 | }
|
---|
697 |
|
---|
698 | //Check for blocked peers
|
---|
699 | uint16_t requesting_peer = data[(((data[19] & 0x0f) << 8) | data[20]) + 21] << 8 |
|
---|
700 | data[(((data[19] & 0x0f) << 8) | data[20]) + 22];
|
---|
701 | if (is_blocked_peer(requesting_peer))
|
---|
702 | {
|
---|
703 | cs_log_dbg(D_READER, "ECM from peer %04X blocked", requesting_peer);
|
---|
704 | return -1;
|
---|
705 | }
|
---|
706 |
|
---|
707 | ECM_REQUEST *er;
|
---|
708 | if(!(er = get_ecmtask())) { return -1; }
|
---|
709 |
|
---|
710 | struct gbox_ecm_request_ext *ere;
|
---|
711 | if(!cs_malloc(&ere, sizeof(struct gbox_ecm_request_ext)))
|
---|
712 | {
|
---|
713 | cs_writeunlock(&peer->lock);
|
---|
714 | return -1;
|
---|
715 | }
|
---|
716 |
|
---|
717 | uchar *ecm = data + 18; //offset of ECM in gbx message
|
---|
718 |
|
---|
719 | er->src_data = ere;
|
---|
720 | gbox_init_ecm_request_ext(ere);
|
---|
721 |
|
---|
722 | er->gbox_ecm_id = peer->gbox.id;
|
---|
723 |
|
---|
724 | if(peer->ecm_idx == 100) { peer->ecm_idx = 0; }
|
---|
725 |
|
---|
726 | er->idx = peer->ecm_idx++;
|
---|
727 | er->ecmlen = (((ecm[1] & 0x0f) << 8) | ecm[2]) + 3;
|
---|
728 |
|
---|
729 | er->pid = b2i(2, data + 10);
|
---|
730 | er->srvid = b2i(2, data + 12);
|
---|
731 |
|
---|
732 | if(ecm[er->ecmlen + 5] == 0x05)
|
---|
733 | { er->caid = (ecm[er->ecmlen + 5] << 8); }
|
---|
734 | else
|
---|
735 | { er->caid = b2i(2, ecm + er->ecmlen + 5); }
|
---|
736 |
|
---|
737 | // ei->extra = data[14] << 8 | data[15];
|
---|
738 | memcpy(er->ecm, data + 18, er->ecmlen);
|
---|
739 | ere->gbox_peer = b2i(2, ecm + er->ecmlen);
|
---|
740 | ere->gbox_version = ecm[er->ecmlen + 2];
|
---|
741 | ere->gbox_unknown = ecm[er->ecmlen + 3];
|
---|
742 | ere->gbox_type = ecm[er->ecmlen + 4];
|
---|
743 | uint32_t caprovid = b2i(4, ecm + er->ecmlen + 5);
|
---|
744 | ere->gbox_mypeer = b2i(2, ecm + er->ecmlen + 10);
|
---|
745 | ere->gbox_slot = ecm[er->ecmlen + 12];
|
---|
746 |
|
---|
747 | diffcheck = gbox_checkcode_recv(cl, data + n - 14);
|
---|
748 | //TODO: What do we do with our own checkcode @-7?
|
---|
749 | er->gbox_crc = gbox_get_ecmchecksum(&er->ecm[0], er->ecmlen);
|
---|
750 | ere->gbox_hops = data[n - 15] + 1;
|
---|
751 | memcpy(&ere->gbox_routing_info[0], &data[n - 15 - ere->gbox_hops + 1], ere->gbox_hops - 1);
|
---|
752 |
|
---|
753 | er->caid = gbox_get_caid(caprovid);
|
---|
754 | er->prid = gbox_get_provid(caprovid);
|
---|
755 | cs_log_dbg(D_READER, "<- ECM (distance: %d) from %04X via peer (%s:%d) for SID %04X", ere->gbox_hops, ere->gbox_peer, peer->hostname, cli->port, er->srvid);
|
---|
756 | get_cw(cl, er);
|
---|
757 |
|
---|
758 | //checkcode did not match gbox->peer checkcode
|
---|
759 | if(diffcheck)
|
---|
760 | {
|
---|
761 | // TODO: Send HelloS here?
|
---|
762 | // gbox->peer.hello_stat = GBOX_STAT_HELLOS;
|
---|
763 | // gbox_send_hello(cli);
|
---|
764 | }
|
---|
765 | return 0;
|
---|
766 | }
|
---|
767 |
|
---|
768 | static uint32_t gbox_get_pending_time(ECM_REQUEST *er, uint16_t peer_id, uint8_t slot)
|
---|
769 | {
|
---|
770 | if (!er) { return 0; }
|
---|
771 |
|
---|
772 | uint32_t ret_time = 0;
|
---|
773 | struct gbox_card_pending *pending = NULL;
|
---|
774 | LL_LOCKITER *li = ll_li_create(er->gbox_cards_pending, 0);
|
---|
775 | while ((pending = ll_li_next(li)))
|
---|
776 | {
|
---|
777 | if ((pending->id.peer == peer_id) && (pending->id.slot == slot))
|
---|
778 | {
|
---|
779 | ret_time = pending->pending_time;
|
---|
780 | break;
|
---|
781 | }
|
---|
782 | }
|
---|
783 | ll_li_destroy(li);
|
---|
784 | return ret_time;
|
---|
785 | }
|
---|
786 |
|
---|
787 | static int32_t gbox_recv_chk(struct s_client *cli, uchar *dcw, int32_t *rc, uchar *data, int32_t n)
|
---|
788 | {
|
---|
789 | if(!cli || gbox_decode_cmd(data) != MSG_CW || n < 44)
|
---|
790 | { return -1; }
|
---|
791 |
|
---|
792 | int i;
|
---|
793 | uint16_t id_card = 0;
|
---|
794 | struct s_client *proxy;
|
---|
795 | if(cli->typ != 'p')
|
---|
796 | { proxy = get_gbox_proxy(cli->gbox_peer_id); }
|
---|
797 | else
|
---|
798 | { proxy = cli; }
|
---|
799 | if (!proxy || !proxy->reader)
|
---|
800 | {
|
---|
801 | cs_log("error, gbox_recv_chk, proxy not found");
|
---|
802 | return -1;
|
---|
803 | }
|
---|
804 | proxy->last = time((time_t *)0);
|
---|
805 | *rc = 1;
|
---|
806 | memcpy(dcw, data + 14, 16);
|
---|
807 | uint32_t crc = b2i(4, data + 30);
|
---|
808 | char tmp[32];
|
---|
809 | cs_log_dbg(D_READER, "-> cws=%s, peer=%04X, ecm_pid=%04X, sid=%04X, crc=%08X, type=%02X, dist=%01X, unkn1=%01X, unkn2=%02X, chid/0x0000/0xffff=%04X",
|
---|
810 | cs_hexdump(0, dcw, 32, tmp, sizeof(tmp)),
|
---|
811 | data[10] << 8 | data[11], data[6] << 8 | data[7], data[8] << 8 | data[9], crc, data[41], data[42] & 0x0f, data[42] >> 4, data[43], data[37] << 8 | data[38]);
|
---|
812 | struct timeb t_now;
|
---|
813 | cs_ftime(&t_now);
|
---|
814 | int64_t cw_time = GBOX_DEFAULT_CW_TIME;
|
---|
815 | for(i = 0; i < cfg.max_pending; i++)
|
---|
816 | {
|
---|
817 | if(proxy->ecmtask[i].gbox_crc == crc)
|
---|
818 | {
|
---|
819 | id_card = b2i(2, data + 10);
|
---|
820 | cw_time = comp_timeb(&t_now, &proxy->ecmtask[i].tps) - gbox_get_pending_time(&proxy->ecmtask[i], id_card, data[36]);
|
---|
821 | gbox_add_good_sid(id_card, proxy->ecmtask[i].caid, data[36], proxy->ecmtask[i].srvid, cw_time);
|
---|
822 | proxy->reader->currenthops = data[42] & 0x0f;
|
---|
823 | gbox_remove_all_bad_sids(&proxy->ecmtask[i], proxy->ecmtask[i].srvid);
|
---|
824 | if(proxy->ecmtask[i].gbox_ecm_status == GBOX_ECM_NOT_ASKED || proxy->ecmtask[i].gbox_ecm_status == GBOX_ECM_ANSWERED)
|
---|
825 | { return -1; }
|
---|
826 | proxy->ecmtask[i].gbox_ecm_status = GBOX_ECM_ANSWERED;
|
---|
827 | proxy->ecmtask[i].gbox_ecm_id = id_card;
|
---|
828 | *rc = 1;
|
---|
829 | return proxy->ecmtask[i].idx;
|
---|
830 | }
|
---|
831 | }
|
---|
832 | //late answers from other peers,timing not possible
|
---|
833 | gbox_add_good_sid(id_card, data[34] << 8 | data[35], data[36], data[8] << 8 | data[9], GBOX_DEFAULT_CW_TIME);
|
---|
834 | cs_log_dbg(D_READER, "no task found for crc=%08x", crc);
|
---|
835 | return -1;
|
---|
836 | }
|
---|
837 |
|
---|
838 | static int8_t gbox_cw_received(struct s_client *cli, uchar *data, int32_t n)
|
---|
839 | {
|
---|
840 | int32_t rc = 0, i = 0, idx = 0;
|
---|
841 | uchar dcw[16];
|
---|
842 |
|
---|
843 | idx = gbox_recv_chk(cli, dcw, &rc, data, n);
|
---|
844 | if(idx < 0) { return -1; } // no dcw received
|
---|
845 | if(!idx) { idx = cli->last_idx; }
|
---|
846 | cli->reader->last_g = time((time_t *)0); // for reconnect timeout
|
---|
847 | for(i = 0; i < cfg.max_pending; i++)
|
---|
848 | {
|
---|
849 | if(cli->ecmtask[i].idx == idx)
|
---|
850 | {
|
---|
851 | cli->pending--;
|
---|
852 | casc_check_dcw(cli->reader, i, rc, dcw);
|
---|
853 | return 0;
|
---|
854 | }
|
---|
855 | }
|
---|
856 | return -1;
|
---|
857 | }
|
---|
858 |
|
---|
859 | int32_t gbox_cmd_switch(struct s_client *proxy, uchar *data, int32_t n)
|
---|
860 | {
|
---|
861 | if (!data || !proxy) { return -1; }
|
---|
862 | uint16_t cmd = gbox_decode_cmd(data);
|
---|
863 | switch(cmd)
|
---|
864 | {
|
---|
865 | case MSG_BOXINFO:
|
---|
866 | cs_log("-> BOXINFO from %s",username(proxy));
|
---|
867 | gbox_send_hello(proxy, GBOX_STAT_HELLOR);
|
---|
868 | break;
|
---|
869 | case MSG_GOODBYE:
|
---|
870 | cs_log("-> goodbye message from %s",username(proxy));
|
---|
871 | //needfix what to do after Goodbye?
|
---|
872 | //suspect: we get goodbye as signal of SID not found
|
---|
873 | break;
|
---|
874 | case MSG_UNKNWN:
|
---|
875 | cs_log("-> MSG_UNKNWN 48F9 from %s", username(proxy));
|
---|
876 | break;
|
---|
877 | case MSG_GSMS_1:
|
---|
878 | if (!cfg.gsms_dis)
|
---|
879 | {
|
---|
880 | cs_log("-> MSG_GSMS_1 from %s", username(proxy));
|
---|
881 | gbox_send_gsms_ack(proxy,1);
|
---|
882 | write_gsms_msg(proxy, data +4, data[3], data[2]);
|
---|
883 | }
|
---|
884 | else
|
---|
885 | {
|
---|
886 | gsms_unavail();
|
---|
887 | }
|
---|
888 | break;
|
---|
889 | case MSG_GSMS_2:
|
---|
890 | if (!cfg.gsms_dis)
|
---|
891 | {
|
---|
892 | cs_log("-> MSG_GSMS_2 from %s", username(proxy));
|
---|
893 | gbox_send_gsms_ack(proxy,2);
|
---|
894 | write_gsms_msg(proxy, data +16, data[14], data[15]);
|
---|
895 | }
|
---|
896 | else
|
---|
897 | {
|
---|
898 | gsms_unavail();
|
---|
899 | }
|
---|
900 | break;
|
---|
901 | case MSG_GSMS_ACK_1:
|
---|
902 | if (!cfg.gsms_dis)
|
---|
903 | {
|
---|
904 | cs_log("-> MSG_GSMS_ACK_1 from %s", username(proxy));
|
---|
905 | write_gsms_ack(proxy,1);
|
---|
906 | }
|
---|
907 | else
|
---|
908 | {
|
---|
909 | gsms_unavail();
|
---|
910 | }
|
---|
911 | break;
|
---|
912 | case MSG_GSMS_ACK_2:
|
---|
913 | if (!cfg.gsms_dis)
|
---|
914 | {
|
---|
915 | cs_log("-> MSG_GSMS_ACK_2 from %s", username(proxy));
|
---|
916 | write_gsms_ack(proxy,2);
|
---|
917 | }
|
---|
918 | else
|
---|
919 | {
|
---|
920 | gsms_unavail();
|
---|
921 | }
|
---|
922 | break;
|
---|
923 | case MSG_HELLO1:
|
---|
924 | case MSG_HELLO:
|
---|
925 | if (gbox_cmd_hello(proxy, data, n) < 0)
|
---|
926 | { return -1; }
|
---|
927 | break;
|
---|
928 | case MSG_CW:
|
---|
929 | gbox_cw_received(proxy, data, n);
|
---|
930 | break;
|
---|
931 | case MSG_CHECKCODE:
|
---|
932 | gbox_checkcode_recv(proxy, data + 10);
|
---|
933 | break;
|
---|
934 | case MSG_ECM:
|
---|
935 | gbox_incoming_ecm(proxy, data, n);
|
---|
936 | break;
|
---|
937 | default:
|
---|
938 | cs_log("-> unknown command %04X received from %s", cmd, username(proxy));
|
---|
939 | cs_log_dump_dbg(D_READER, data, n, "unknown data received (%d bytes):", n);
|
---|
940 | } // end switch
|
---|
941 | if ((time(NULL) - last_stats_written) > STATS_WRITE_TIME)
|
---|
942 | {
|
---|
943 | gbox_write_stats();
|
---|
944 | last_stats_written = time(NULL);
|
---|
945 | }
|
---|
946 | return 0;
|
---|
947 | }
|
---|
948 |
|
---|
949 | static void gbox_local_cards(struct s_reader *reader, TUNTAB *ttab)
|
---|
950 | {
|
---|
951 | int32_t i;
|
---|
952 | uint32_t prid = 0;
|
---|
953 | int8_t slot = 0;
|
---|
954 | #ifdef MODULE_CCCAM
|
---|
955 | LL_ITER it, it2;
|
---|
956 | struct cc_card *card = NULL;
|
---|
957 | struct cc_data *cc;
|
---|
958 | uint32_t checksum = 0;
|
---|
959 | uint16_t cc_peer_id = 0;
|
---|
960 | struct cc_provider *provider;
|
---|
961 | uint8_t *node1 = NULL;
|
---|
962 | uint8_t min_reshare = 0;
|
---|
963 | gbox_delete_cards(GBOX_DELETE_WITH_TYPE, GBOX_CARD_TYPE_CCCAM);
|
---|
964 | #endif
|
---|
965 | gbox_delete_cards(GBOX_DELETE_WITH_ID, local_gbox.id);
|
---|
966 | struct s_client *cl;
|
---|
967 | cs_readlock(&clientlist_lock);
|
---|
968 | for(cl = first_client; cl; cl = cl->next)
|
---|
969 | {
|
---|
970 | if(cl->typ == 'r' && cl->reader && cl->reader->card_status == 2)
|
---|
971 | {
|
---|
972 | slot = gbox_next_free_slot(local_gbox.id);
|
---|
973 | //SECA, Viaccess and Cryptoworks have multiple providers
|
---|
974 | if(caid_is_seca(cl->reader->caid) || caid_is_viaccess(cl->reader->caid) || caid_is_cryptoworks(cl->reader->caid))
|
---|
975 | {
|
---|
976 | for(i = 0; i < cl->reader->nprov; i++)
|
---|
977 | {
|
---|
978 | prid = cl->reader->prid[i][1] << 16 |
|
---|
979 | cl->reader->prid[i][2] << 8 | cl->reader->prid[i][3];
|
---|
980 | gbox_add_card(local_gbox.id, gbox_get_caprovid(cl->reader->caid, prid), slot, reader->gbox_reshare, 0, GBOX_CARD_TYPE_LOCAL, NULL);
|
---|
981 | }
|
---|
982 | }
|
---|
983 | else
|
---|
984 | {
|
---|
985 | gbox_add_card(local_gbox.id, gbox_get_caprovid(cl->reader->caid, 0), slot, reader->gbox_reshare, 0, GBOX_CARD_TYPE_LOCAL, NULL);
|
---|
986 |
|
---|
987 | //Check for Betatunnel on gbox account in oscam.user
|
---|
988 | if (chk_is_betatunnel_caid(cl->reader->caid) == 1 && ttab->ttdata && cl->reader->caid == ttab->ttdata[0].bt_caidto)
|
---|
989 | {
|
---|
990 | //For now only first entry in tunnel tab. No sense in iteration?
|
---|
991 | //Add betatunnel card to transmitted list
|
---|
992 | gbox_add_card(local_gbox.id, gbox_get_caprovid(ttab->ttdata[0].bt_caidfrom, 0), slot, reader->gbox_reshare, 0, GBOX_CARD_TYPE_BETUN, NULL);
|
---|
993 | cs_log_dbg(D_READER, "gbox created betatunnel card for caid: %04X->%04X", ttab->ttdata[0].bt_caidfrom, cl->reader->caid);
|
---|
994 | }
|
---|
995 | }
|
---|
996 | } //end local readers
|
---|
997 | #ifdef MODULE_CCCAM
|
---|
998 | if((cfg.cc_reshare > -1) && (reader->gbox_cccam_reshare) && cl->typ == 'p' && cl->reader && cl->reader->typ == R_CCCAM && cl->cc)
|
---|
999 | {
|
---|
1000 | cc = cl->cc;
|
---|
1001 | it = ll_iter_create(cc->cards);
|
---|
1002 | while((card = ll_iter_next(&it)))
|
---|
1003 | {
|
---|
1004 | //calculate gbox id from cc node
|
---|
1005 | node1 = ll_has_elements(card->remote_nodes);
|
---|
1006 | checksum = ((node1[0] ^ node1[7]) << 8) |
|
---|
1007 | ((node1[1] ^ node1[6]) << 24) |
|
---|
1008 | (node1[2] ^ node1[5]) |
|
---|
1009 | ((node1[3] ^ node1[4]) << 16);
|
---|
1010 | cc_peer_id = ((((checksum >> 24) & 0xFF) ^((checksum >> 8) & 0xFF)) << 8 |
|
---|
1011 | (((checksum >> 16) & 0xFF) ^(checksum & 0xFF)));
|
---|
1012 | slot = gbox_next_free_slot(cc_peer_id);
|
---|
1013 | min_reshare = cfg.cc_reshare;
|
---|
1014 | if (card->reshare < min_reshare)
|
---|
1015 | { min_reshare = card->reshare; }
|
---|
1016 | min_reshare++; //strange CCCam logic. 0 means direct peers
|
---|
1017 | if (reader->gbox_cccam_reshare < min_reshare)
|
---|
1018 | { min_reshare = reader->gbox_cccam_reshare; }
|
---|
1019 | if(caid_is_seca(card->caid) || caid_is_viaccess(card->caid) || caid_is_cryptoworks(card->caid))
|
---|
1020 | {
|
---|
1021 | it2 = ll_iter_create(card->providers);
|
---|
1022 | while((provider = ll_iter_next(&it2)))
|
---|
1023 | { gbox_add_card(cc_peer_id, gbox_get_caprovid(card->caid, provider->prov), slot, min_reshare, card->hop, GBOX_CARD_TYPE_CCCAM, NULL); }
|
---|
1024 | }
|
---|
1025 | else
|
---|
1026 | { gbox_add_card(cc_peer_id, gbox_get_caprovid(card->caid, 0), slot, min_reshare, card->hop, GBOX_CARD_TYPE_CCCAM, NULL); }
|
---|
1027 | }
|
---|
1028 | } //end cccam
|
---|
1029 | #endif
|
---|
1030 | } //end for clients
|
---|
1031 | cs_readunlock(&clientlist_lock);
|
---|
1032 |
|
---|
1033 | if (cfg.gbox_proxy_cards_num > 0)
|
---|
1034 | {
|
---|
1035 | for (i = 0; i < cfg.gbox_proxy_cards_num; i++)
|
---|
1036 | {
|
---|
1037 | slot = gbox_next_free_slot(local_gbox.id);
|
---|
1038 | gbox_add_card(local_gbox.id, cfg.gbox_proxy_card[i], slot, reader->gbox_reshare, 0, GBOX_CARD_TYPE_PROXY, NULL);
|
---|
1039 | if ((cfg.gbox_proxy_card[i] >> 24) == 0x05)
|
---|
1040 | {
|
---|
1041 | cs_log_dbg(D_READER,"add proxy card: slot %d %04lX:%06lX",slot, (cfg.gbox_proxy_card[i] >> 16) & 0xFFF0, cfg.gbox_proxy_card[i] & 0xFFFFF);
|
---|
1042 | } else
|
---|
1043 | {
|
---|
1044 | cs_log_dbg(D_READER,"add proxy card: slot %d %04lX:%06lX",slot, cfg.gbox_proxy_card[i] >> 16, cfg.gbox_proxy_card[i] & 0xFFFF);
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | } // end add proxy reader cards
|
---|
1048 | gbox_write_local_cards_info();
|
---|
1049 | } //end add local gbox cards
|
---|
1050 |
|
---|
1051 | //returns -1 in case of error, 1 if authentication was performed, 0 else
|
---|
1052 | static int8_t gbox_check_header(struct s_client *cli, struct s_client *proxy, uchar *data, int32_t l)
|
---|
1053 | {
|
---|
1054 | struct gbox_peer *peer = NULL;
|
---|
1055 | if (proxy) { peer = proxy->gbox; }
|
---|
1056 |
|
---|
1057 | char tmp[0x50];
|
---|
1058 | int32_t n = l;
|
---|
1059 | uint8_t authentication_done = 0;
|
---|
1060 | uint32_t my_received_pw = 0;
|
---|
1061 | uint32_t peer_received_pw = 0;
|
---|
1062 | cs_log_dump_dbg(D_READER, data, n, "-> encrypted data (%d bytes):", n);
|
---|
1063 |
|
---|
1064 | if(gbox_decode_cmd(data) == MSG_HELLO1)
|
---|
1065 | { cs_log("test cs2gbox"); }
|
---|
1066 | else
|
---|
1067 | { gbox_decrypt(data, n, local_gbox.password); }
|
---|
1068 |
|
---|
1069 | cs_log_dump_dbg(D_READER, data, n, "-> decrypted data (%d bytes):", n);
|
---|
1070 | //verify my pass received
|
---|
1071 | my_received_pw = b2i(4, data + 2);
|
---|
1072 | if (my_received_pw == local_gbox.password)
|
---|
1073 | {
|
---|
1074 | cs_log_dbg(D_READER, "-> data, peer : %04x data: %s", cli->gbox_peer_id, cs_hexdump(0, data, l, tmp, sizeof(tmp)));
|
---|
1075 |
|
---|
1076 | if (gbox_decode_cmd(data) != MSG_CW)
|
---|
1077 | {
|
---|
1078 | if (cli->gbox_peer_id == NO_GBOX_ID)
|
---|
1079 | {
|
---|
1080 | if (gbox_auth_client(cli, b2i(4, data + 6)) < 0)
|
---|
1081 | {
|
---|
1082 | cs_log_dbg(D_READER, "Authentication failed. Please check user in oscam.server and oscam.user");
|
---|
1083 | return -1;
|
---|
1084 | }
|
---|
1085 | authentication_done = 1;
|
---|
1086 | proxy = get_gbox_proxy(cli->gbox_peer_id);
|
---|
1087 | gbox_local_cards(proxy->reader, &cli->ttab);
|
---|
1088 | peer = proxy->gbox;
|
---|
1089 | }
|
---|
1090 | if (!peer) { return -1; }
|
---|
1091 | peer_received_pw = b2i(4, data + 6);
|
---|
1092 | if (peer_received_pw != peer->gbox.password)
|
---|
1093 | {
|
---|
1094 | cs_log("gbox peer: %04X sends wrong password", peer->gbox.id);
|
---|
1095 | return -1;
|
---|
1096 | //continue; // next client
|
---|
1097 | }
|
---|
1098 | } else
|
---|
1099 | {
|
---|
1100 | // if my pass ok verify CW | pass to peer
|
---|
1101 | if((data[39] != ((local_gbox.id >> 8) & 0xff)) || (data[40] != (local_gbox.id & 0xff)))
|
---|
1102 | {
|
---|
1103 | cs_log("gbox peer: %04X sends CW for other than my id: %04X", cli->gbox_peer_id, local_gbox.id);
|
---|
1104 | return -1;
|
---|
1105 | //continue; // next client
|
---|
1106 | }
|
---|
1107 | }
|
---|
1108 | } // error my pass
|
---|
1109 | else if (gbox_decode_cmd(data) == MSG_GSMS_1 || gbox_decode_cmd(data) == MSG_GSMS_ACK_1 )
|
---|
1110 | {
|
---|
1111 | // MSG_GSMS_1 dont have passw and would fail. Just let them pass through for processing later
|
---|
1112 | }
|
---|
1113 | else
|
---|
1114 | {
|
---|
1115 | cs_log("ATTACK ALERT from IP %s", cs_inet_ntoa(cli->ip));
|
---|
1116 | cs_log("received data, data: %s", cs_hexdump(0, data, n, tmp, sizeof(tmp)));
|
---|
1117 | return -1;
|
---|
1118 | //continue; // next client
|
---|
1119 | }
|
---|
1120 | if (!proxy) { return -1; }
|
---|
1121 |
|
---|
1122 | if (!IP_EQUAL(cli->ip, proxy->ip))
|
---|
1123 | {
|
---|
1124 | cs_log("Received IP %s did not match previous IP %s. Try to reconnect.", cs_inet_ntoa(cli->ip), cs_inet_ntoa(proxy->ip));
|
---|
1125 | gbox_reconnect_client(cli->gbox_peer_id);
|
---|
1126 | return -1;
|
---|
1127 | }
|
---|
1128 | if(!peer) { return -1; }
|
---|
1129 |
|
---|
1130 | return authentication_done;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | static int32_t gbox_recv(struct s_client *cli, uchar *buf, int32_t l)
|
---|
1134 | {
|
---|
1135 | uchar data[RECEIVE_BUFFER_SIZE];
|
---|
1136 | int32_t n = l;
|
---|
1137 | int8_t ret = 0;
|
---|
1138 |
|
---|
1139 | if(!cli->udp_fd || !cli->is_udp || cli->typ != 'c')
|
---|
1140 | { return -1; }
|
---|
1141 |
|
---|
1142 | n = recv_from_udpipe(buf);
|
---|
1143 | if (n < MIN_GBOX_MESSAGE_LENGTH || n >= RECEIVE_BUFFER_SIZE) //protect against too short or too long messages
|
---|
1144 | { return -1; }
|
---|
1145 |
|
---|
1146 | struct s_client *proxy = get_gbox_proxy(cli->gbox_peer_id);
|
---|
1147 |
|
---|
1148 | memcpy(&data[0], buf, n);
|
---|
1149 |
|
---|
1150 | ret = gbox_check_header(cli, proxy, &data[0], n);
|
---|
1151 | if (ret < 0) { return -1; }
|
---|
1152 |
|
---|
1153 | //in case of new authentication the proxy gbox can now be found
|
---|
1154 | if (ret) { proxy = get_gbox_proxy(cli->gbox_peer_id); }
|
---|
1155 |
|
---|
1156 | if (!proxy) { return -1; }
|
---|
1157 |
|
---|
1158 | cli->last = time((time_t *)0);
|
---|
1159 | //clients may timeout - attach to peer's gbox/reader
|
---|
1160 | cli->gbox = proxy->gbox; //point to the same gbox as proxy
|
---|
1161 | cli->reader = proxy->reader; //point to the same reader as proxy
|
---|
1162 | struct gbox_peer *peer = proxy->gbox;
|
---|
1163 |
|
---|
1164 | cs_writelock(&peer->lock);
|
---|
1165 | if(gbox_cmd_switch(proxy, data, n) < 0)
|
---|
1166 | { return -1; }
|
---|
1167 | cs_writeunlock(&peer->lock);
|
---|
1168 |
|
---|
1169 | //clients may timeout - dettach from peer's gbox/reader
|
---|
1170 | cli->gbox = NULL;
|
---|
1171 | cli->reader = NULL;
|
---|
1172 | return 0;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | static void gbox_send_dcw(struct s_client *cl, ECM_REQUEST *er)
|
---|
1176 | {
|
---|
1177 | if (!cl || !er) { return; }
|
---|
1178 |
|
---|
1179 | struct s_client *cli = get_gbox_proxy(cl->gbox_peer_id);
|
---|
1180 | if (!cli || !cli->gbox) { return; }
|
---|
1181 | struct gbox_peer *peer = cli->gbox;
|
---|
1182 |
|
---|
1183 | if(er->rc >= E_NOTFOUND)
|
---|
1184 | {
|
---|
1185 | cs_log_dbg(D_READER, "unable to decode!");
|
---|
1186 | return;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | uchar buf[60];
|
---|
1190 | memset(buf, 0, sizeof(buf));
|
---|
1191 |
|
---|
1192 | struct gbox_ecm_request_ext *ere = er->src_data;
|
---|
1193 |
|
---|
1194 | gbox_message_header(buf, MSG_CW , peer->gbox.password, 0);
|
---|
1195 | i2b_buf(2, er->pid, buf + 6); //PID
|
---|
1196 | i2b_buf(2, er->srvid, buf + 8); //SrvID
|
---|
1197 | i2b_buf(2, ere->gbox_mypeer, buf + 10); //From peer
|
---|
1198 | buf[12] = (ere->gbox_slot << 4) | (er->ecm[0] & 0x0f); //slot << 4 | even/odd
|
---|
1199 | buf[13] = er->caid >> 8; //CAID first byte
|
---|
1200 | memcpy(buf + 14, er->cw, 16); //CW
|
---|
1201 | i2b_buf(4, er->gbox_crc, buf + 30); //CRC
|
---|
1202 | i2b_buf(2, er->caid, buf + 34); //CAID
|
---|
1203 | buf[36] = ere->gbox_slot; //Slot
|
---|
1204 | if (buf[34] == 0x06) //if irdeto
|
---|
1205 | { i2b_buf(2, er->chid, buf + 37); } //CHID
|
---|
1206 | else
|
---|
1207 | {
|
---|
1208 | if (local_gbox.minor_version == 0x2A)
|
---|
1209 | {
|
---|
1210 | buf[37] = 0xff; //gbox.net sends 0xff
|
---|
1211 | buf[38] = 0xff; //gbox.net sends 0xff
|
---|
1212 | }
|
---|
1213 | else
|
---|
1214 | {
|
---|
1215 | buf[37] = 0; //gbox sends 0
|
---|
1216 | buf[38] = 0; //gbox sends 0
|
---|
1217 | }
|
---|
1218 | }
|
---|
1219 | i2b_buf(2, ere->gbox_peer, buf + 39); //Target peer
|
---|
1220 | if (er->rc == E_CACHE1 || er->rc == E_CACHE2 || er->rc == E_CACHEEX)
|
---|
1221 | { buf[41] = 0x03; } //cache
|
---|
1222 | else
|
---|
1223 | { buf[41] = 0x01; } //card, emu, needs probably further investigation
|
---|
1224 | buf[42] = 0x30; //1st nibble unknown / 2nd nibble distance
|
---|
1225 | buf[43] = ere->gbox_unknown; //meaning unknown, copied from ECM request
|
---|
1226 |
|
---|
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
|
---|
1231 | /*
|
---|
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)));
|
---|
1234 | */
|
---|
1235 | gbox_send(cli, buf, ere->gbox_hops + 44);
|
---|
1236 |
|
---|
1237 | cs_log_dbg(D_READER, "<- CW (distance: %d) to %04X via %s/%d", ere->gbox_hops, ere->gbox_peer, cli->reader->label, cli->port);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | void *gbox_rebroadcast_thread(struct gbox_rbc_thread_args *args)
|
---|
1241 | {
|
---|
1242 | if (!args) { return NULL; }
|
---|
1243 |
|
---|
1244 | struct s_client *cli = args->cli;
|
---|
1245 | ECM_REQUEST *er = args->er;
|
---|
1246 | uint32_t waittime = args->waittime;
|
---|
1247 |
|
---|
1248 | //NEEDFIX currently the next line avoids a second rebroadcast
|
---|
1249 | if (!is_valid_client(cli)) { return NULL; }
|
---|
1250 |
|
---|
1251 | pthread_mutex_lock(&cli->thread_lock);
|
---|
1252 | cli->thread_active = 1;
|
---|
1253 | pthread_setspecific(getclient, cli);
|
---|
1254 | set_thread_name(__func__);
|
---|
1255 | cli->thread_active = 0;
|
---|
1256 | pthread_mutex_unlock(&cli->thread_lock);
|
---|
1257 |
|
---|
1258 | cs_sleepms(waittime);
|
---|
1259 | if (!cli || cli->kill || !cli->gbox || !er) { return NULL; }
|
---|
1260 | pthread_mutex_lock(&cli->thread_lock);
|
---|
1261 | cli->thread_active = 1;
|
---|
1262 |
|
---|
1263 | struct gbox_peer *peer = cli->gbox;
|
---|
1264 |
|
---|
1265 | struct timeb t_now, tbc;
|
---|
1266 | cs_ftime(&t_now);
|
---|
1267 |
|
---|
1268 | tbc = er->tps;
|
---|
1269 | add_ms_to_timeb_diff(&tbc, cfg.ctimeout);
|
---|
1270 | int32_t time_to_timeout = (int32_t) comp_timeb(&tbc, &t_now);
|
---|
1271 |
|
---|
1272 | //ecm is not answered yet and still chance to get CW
|
---|
1273 | if (er->rc >= E_NOTFOUND && time_to_timeout > GBOX_DEFAULT_CW_TIME)
|
---|
1274 | {
|
---|
1275 | cs_writelock(&peer->lock);
|
---|
1276 | gbox_send_ecm(cli, er, NULL);
|
---|
1277 | cs_writeunlock(&peer->lock);
|
---|
1278 | }
|
---|
1279 | cli->thread_active = 0;
|
---|
1280 | pthread_mutex_unlock(&cli->thread_lock);
|
---|
1281 |
|
---|
1282 | return NULL;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | static int32_t gbox_send_ecm(struct s_client *cli, ECM_REQUEST *er, uchar *UNUSED(buf))
|
---|
1286 | {
|
---|
1287 | if(!cli || !er || !cli->reader)
|
---|
1288 | { return -1; }
|
---|
1289 |
|
---|
1290 | if(!cli->gbox || !cli->reader->tcp_connected)
|
---|
1291 | {
|
---|
1292 | cs_log_dbg(D_READER, "%s server not init!", cli->reader->label);
|
---|
1293 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1294 | return -1;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | struct gbox_peer *peer = cli->gbox;
|
---|
1298 | int32_t cont_1;
|
---|
1299 |
|
---|
1300 | if(!peer->filtered_cards)
|
---|
1301 | {
|
---|
1302 | cs_log_dbg(D_READER, "%s NO CARDS!", cli->reader->label);
|
---|
1303 | write_ecm_answer(cli->reader, er, E_NOTFOUND, E2_CCCAM_NOCARD, NULL, NULL);
|
---|
1304 | return -1;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | if(!peer->online)
|
---|
1308 | {
|
---|
1309 | cs_log_dbg(D_READER, "peer is OFFLINE!");
|
---|
1310 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1311 | // gbox_send_hello(cli,0);
|
---|
1312 | return -1;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | if(er->gbox_ecm_status == GBOX_ECM_ANSWERED)
|
---|
1316 | { cs_log_dbg(D_READER, "%s replied to this ecm already", cli->reader->label); }
|
---|
1317 |
|
---|
1318 | if(er->gbox_ecm_status == GBOX_ECM_NOT_ASKED)
|
---|
1319 | { er->gbox_cards_pending = ll_create("pending_gbox_cards"); }
|
---|
1320 |
|
---|
1321 | if(er->gbox_ecm_id == peer->gbox.id)
|
---|
1322 | {
|
---|
1323 | cs_log_dbg(D_READER, "%s provided ecm", cli->reader->label);
|
---|
1324 | write_ecm_answer(cli->reader, er, E_NOTFOUND, 0x27, NULL, NULL);
|
---|
1325 | return 0;
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | uchar send_buf_1[1024];
|
---|
1329 | int32_t len2;
|
---|
1330 |
|
---|
1331 | if(!er->ecmlen) { return 0; }
|
---|
1332 |
|
---|
1333 | len2 = er->ecmlen + 18;
|
---|
1334 | er->gbox_crc = gbox_get_ecmchecksum(&er->ecm[0], er->ecmlen);
|
---|
1335 |
|
---|
1336 | memset(send_buf_1, 0, sizeof(send_buf_1));
|
---|
1337 |
|
---|
1338 | uint8_t cont_card_1 = 0;
|
---|
1339 | uint8_t max_ecm_reached = 0;
|
---|
1340 | uint32_t current_avg_card_time = 0;
|
---|
1341 |
|
---|
1342 | gbox_message_header(send_buf_1, MSG_ECM , peer->gbox.password, local_gbox.password);
|
---|
1343 |
|
---|
1344 | i2b_buf(2, er->pid, send_buf_1 + 10);
|
---|
1345 | i2b_buf(2, er->srvid, send_buf_1 + 12);
|
---|
1346 | send_buf_1[14] = 0x00;
|
---|
1347 | send_buf_1[15] = 0x00;
|
---|
1348 |
|
---|
1349 | send_buf_1[16] = cont_card_1;
|
---|
1350 | send_buf_1[17] = 0x00;
|
---|
1351 |
|
---|
1352 | memcpy(send_buf_1 + 18, er->ecm, er->ecmlen);
|
---|
1353 |
|
---|
1354 | i2b_buf(2, local_gbox.id, send_buf_1 + len2);
|
---|
1355 |
|
---|
1356 | send_buf_1[len2 + 2] = gbox_get_my_vers();
|
---|
1357 | send_buf_1[len2 + 3] = 0x00;
|
---|
1358 | send_buf_1[len2 + 4] = gbox_get_my_cpu_api();
|
---|
1359 |
|
---|
1360 | uint32_t caprovid = gbox_get_caprovid(er->caid, er->prid);
|
---|
1361 | i2b_buf(4, caprovid, send_buf_1 + len2 + 5);
|
---|
1362 |
|
---|
1363 | send_buf_1[len2 + 9] = 0x00;
|
---|
1364 | cont_1 = len2 + 10;
|
---|
1365 |
|
---|
1366 | cont_card_1 = gbox_get_cards_for_ecm(&send_buf_1[0], len2 + 10, cli->reader->gbox_maxecmsend, er, ¤t_avg_card_time, peer->gbox.id);
|
---|
1367 | if (cont_card_1 == cli->reader->gbox_maxecmsend)
|
---|
1368 | { max_ecm_reached = 1; }
|
---|
1369 | cont_1 += cont_card_1 * 3;
|
---|
1370 |
|
---|
1371 | if(!cont_card_1 && er->gbox_ecm_status == GBOX_ECM_NOT_ASKED)
|
---|
1372 | {
|
---|
1373 | cs_log_dbg(D_READER, "no valid card found for CAID: %04X PROVID: %04X", er->caid, er->prid);
|
---|
1374 | write_ecm_answer(cli->reader, er, E_NOTFOUND, E2_CCCAM_NOCARD, NULL, NULL);
|
---|
1375 | return -1;
|
---|
1376 | }
|
---|
1377 | if(cont_card_1)
|
---|
1378 | {
|
---|
1379 | send_buf_1[16] = cont_card_1;
|
---|
1380 |
|
---|
1381 | //Hops
|
---|
1382 | send_buf_1[cont_1] = 0;
|
---|
1383 | cont_1++;
|
---|
1384 |
|
---|
1385 | memcpy(&send_buf_1[cont_1], gbox_get_checkcode(), 7);
|
---|
1386 | cont_1 = cont_1 + 7;
|
---|
1387 | memcpy(&send_buf_1[cont_1], peer->checkcode, 7);
|
---|
1388 | cont_1 = cont_1 + 7;
|
---|
1389 |
|
---|
1390 | cs_log_dbg(D_READER, "gbox sending ecm for %04X:%06X:%04X to %d cards -> %s", er->caid, er->prid , er->srvid, cont_card_1, cli->reader->label);
|
---|
1391 | uint32_t i = 0;
|
---|
1392 | struct gbox_card_pending *pending = NULL;
|
---|
1393 | struct timeb t_now;
|
---|
1394 | cs_ftime(&t_now);
|
---|
1395 | for (i = 0; i < cont_card_1; i++)
|
---|
1396 | {
|
---|
1397 | if(!cs_malloc(&pending, sizeof(struct gbox_card_pending)))
|
---|
1398 | {
|
---|
1399 | cs_log("Can't allocate gbox card pending");
|
---|
1400 | return -1;
|
---|
1401 | }
|
---|
1402 | pending->id.peer = (send_buf_1[len2+10+i*3] << 8) | send_buf_1[len2+11+i*3];
|
---|
1403 | pending->id.slot = send_buf_1[len2+12+i*3];
|
---|
1404 | pending->pending_time = comp_timeb(&t_now, &er->tps);
|
---|
1405 | ll_append(er->gbox_cards_pending, pending);
|
---|
1406 | cs_log_dbg(D_READER, "gbox card %d: ID: %04X, Slot: %02X", i+1, (send_buf_1[len2+10+i*3] << 8) | send_buf_1[len2+11+i*3], send_buf_1[len2+12+i*3]);
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | LL_LOCKITER *li = ll_li_create(er->gbox_cards_pending, 0);
|
---|
1410 | while ((pending = ll_li_next(li)))
|
---|
1411 | { cs_log_dbg(D_READER, "Pending Card ID: %04X Slot: %02X Time: %d", pending->id.peer, pending->id.slot, pending->pending_time); }
|
---|
1412 | ll_li_destroy(li);
|
---|
1413 |
|
---|
1414 | if(er->gbox_ecm_status > GBOX_ECM_NOT_ASKED)
|
---|
1415 | { er->gbox_ecm_status++; }
|
---|
1416 | else
|
---|
1417 | {
|
---|
1418 | if(max_ecm_reached)
|
---|
1419 | { er->gbox_ecm_status = GBOX_ECM_SENT; }
|
---|
1420 | else
|
---|
1421 | { er->gbox_ecm_status = GBOX_ECM_SENT_ALL; }
|
---|
1422 | cli->pending++;
|
---|
1423 | }
|
---|
1424 | gbox_send(cli, send_buf_1, cont_1);
|
---|
1425 | cli->reader->last_s = time((time_t *) 0);
|
---|
1426 |
|
---|
1427 | if(er->gbox_ecm_status < GBOX_ECM_ANSWERED)
|
---|
1428 | {
|
---|
1429 | //Create thread to rebroacast ecm after time
|
---|
1430 | pthread_t rbc_thread;
|
---|
1431 | pthread_attr_t attr;
|
---|
1432 | pthread_attr_init(&attr);
|
---|
1433 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
1434 | struct gbox_rbc_thread_args args;
|
---|
1435 | args.cli = cli;
|
---|
1436 | args.er = er;
|
---|
1437 | if ((current_avg_card_time > 0) && (cont_card_1 == 1))
|
---|
1438 | {
|
---|
1439 | args.waittime = current_avg_card_time + (current_avg_card_time / 2);
|
---|
1440 | if (args.waittime < GBOX_MIN_REBROADCAST_TIME)
|
---|
1441 | { args.waittime = GBOX_MIN_REBROADCAST_TIME; }
|
---|
1442 | }
|
---|
1443 | else
|
---|
1444 | { args.waittime = GBOX_REBROADCAST_TIMEOUT; }
|
---|
1445 | cs_log_dbg(D_READER, "Creating rebroadcast thread with waittime: %d", args.waittime);
|
---|
1446 | int32_t ret = pthread_create(&rbc_thread, &attr, (void *)gbox_rebroadcast_thread, &args);
|
---|
1447 | if(ret)
|
---|
1448 | {
|
---|
1449 | cs_log("Can't create gbox rebroadcast thread (errno=%d %s)", ret, strerror(ret));
|
---|
1450 | pthread_attr_destroy(&attr);
|
---|
1451 | return -1;
|
---|
1452 | }
|
---|
1453 | else
|
---|
1454 | { pthread_detach(rbc_thread); }
|
---|
1455 | pthread_attr_destroy(&attr);
|
---|
1456 | }
|
---|
1457 | else
|
---|
1458 | { er->gbox_ecm_status--; }
|
---|
1459 | }
|
---|
1460 | return 0;
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | static int32_t gbox_send_emm(EMM_PACKET *UNUSED(ep))
|
---|
1464 | {
|
---|
1465 | // emms not yet supported
|
---|
1466 |
|
---|
1467 | return 0;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | //init my gbox with id, password and cards crc
|
---|
1471 | static void init_local_gbox(void)
|
---|
1472 | {
|
---|
1473 | local_gbox.id = 0;
|
---|
1474 | local_gbox.password = 0;
|
---|
1475 | local_gbox.minor_version = gbox_get_my_vers();
|
---|
1476 | local_gbox.cpu_api = gbox_get_my_cpu_api();
|
---|
1477 | init_gbox_cards();
|
---|
1478 |
|
---|
1479 | if(!cfg.gbox_my_password || strlen(cfg.gbox_my_password) != 8) { return; }
|
---|
1480 |
|
---|
1481 | local_gbox.password = a2i(cfg.gbox_my_password, 4);
|
---|
1482 | cs_log_dbg(D_READER, "gbox my password: %s:", cfg.gbox_my_password);
|
---|
1483 |
|
---|
1484 | local_gbox.id = gbox_convert_password_to_id(local_gbox.password);
|
---|
1485 | if (local_gbox.id == NO_GBOX_ID)
|
---|
1486 | {
|
---|
1487 | cs_log("invalid local gbox id: %04X", local_gbox.id);
|
---|
1488 | }
|
---|
1489 | last_stats_written = time(NULL);
|
---|
1490 | gbox_write_version();
|
---|
1491 | local_gbox_initialized = 1;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | static int32_t gbox_client_init(struct s_client *cli)
|
---|
1495 | {
|
---|
1496 | if (!cli || cli->typ != 'p' || !cli->reader)
|
---|
1497 | {
|
---|
1498 | cs_log("error, wrong call to gbox_proxy_init!");
|
---|
1499 | return -1;
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | if (!local_gbox_initialized)
|
---|
1503 | { init_local_gbox(); }
|
---|
1504 |
|
---|
1505 | if(!cfg.gbx_port[0] || cfg.gbx_port[0] > 65535)
|
---|
1506 | {
|
---|
1507 | cs_log("error, no/invalid port=%d configured in oscam.conf!",
|
---|
1508 | cfg.gbx_port[0] ? cfg.gbx_port[0] : 0);
|
---|
1509 | return -1;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | if(!cfg.gbox_hostname || strlen(cfg.gbox_hostname) > 128)
|
---|
1513 | {
|
---|
1514 | cs_log("error, no/invalid hostname '%s' configured in oscam.conf!",
|
---|
1515 | cfg.gbox_hostname ? cfg.gbox_hostname : "");
|
---|
1516 | return -1;
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | if(!local_gbox.id)
|
---|
1520 | {
|
---|
1521 | cs_log("error, no/invalid password '%s' configured in oscam.conf!",
|
---|
1522 | cfg.gbox_my_password ? cfg.gbox_my_password : "");
|
---|
1523 | return -1;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | if(!cs_malloc(&cli->gbox, sizeof(struct gbox_peer)))
|
---|
1527 | { return -1; }
|
---|
1528 |
|
---|
1529 | struct s_reader *rdr = cli->reader;
|
---|
1530 | struct gbox_peer *peer = cli->gbox;
|
---|
1531 |
|
---|
1532 | memset(peer, 0, sizeof(struct gbox_peer));
|
---|
1533 |
|
---|
1534 | peer->gbox.password = a2i(rdr->r_pwd, 4);
|
---|
1535 | cs_log_dbg(D_READER, "gbox peer password: %s:", rdr->r_pwd);
|
---|
1536 |
|
---|
1537 | peer->gbox.id = gbox_convert_password_to_id(peer->gbox.password);
|
---|
1538 | if (get_gbox_proxy(peer->gbox.id) || peer->gbox.id == NO_GBOX_ID || peer->gbox.id == local_gbox.id)
|
---|
1539 | {
|
---|
1540 | cs_log("error, double/invalid gbox id: %04X", peer->gbox.id);
|
---|
1541 | return -1;
|
---|
1542 | }
|
---|
1543 | cs_lock_create(&peer->lock, "gbox_lock", 5000);
|
---|
1544 |
|
---|
1545 | gbox_reinit_peer(peer);
|
---|
1546 |
|
---|
1547 | cli->gbox_peer_id = peer->gbox.id;
|
---|
1548 |
|
---|
1549 | cli->pfd = 0;
|
---|
1550 | cli->crypted = 1;
|
---|
1551 |
|
---|
1552 | rdr->card_status = CARD_NEED_INIT;
|
---|
1553 | rdr->tcp_connected = 0;
|
---|
1554 |
|
---|
1555 | set_null_ip(&cli->ip);
|
---|
1556 |
|
---|
1557 | if((cli->udp_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
|
---|
1558 | {
|
---|
1559 | cs_log("socket creation failed (errno=%d %s)", errno, strerror(errno));
|
---|
1560 | cs_disconnect_client(cli);
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | int32_t opt = 1;
|
---|
1564 | setsockopt(cli->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
---|
1565 |
|
---|
1566 | set_so_reuseport(cli->udp_fd);
|
---|
1567 |
|
---|
1568 | set_socket_priority(cli->udp_fd, cfg.netprio);
|
---|
1569 |
|
---|
1570 | memset((char *)&cli->udp_sa, 0, sizeof(cli->udp_sa));
|
---|
1571 |
|
---|
1572 | if(!hostResolve(rdr))
|
---|
1573 | { return 0; }
|
---|
1574 |
|
---|
1575 | cli->port = rdr->r_port;
|
---|
1576 | SIN_GET_FAMILY(cli->udp_sa) = AF_INET;
|
---|
1577 | SIN_GET_PORT(cli->udp_sa) = htons((uint16_t)rdr->r_port);
|
---|
1578 | hostname2ip(cli->reader->device, &SIN_GET_ADDR(cli->udp_sa));
|
---|
1579 |
|
---|
1580 | cs_log("proxy %s (fd=%d, peer id=%04X, my id=%04X, my hostname=%s, peer's listen port=%d)",
|
---|
1581 | rdr->device, cli->udp_fd, peer->gbox.id, local_gbox.id, cfg.gbox_hostname, rdr->r_port);
|
---|
1582 |
|
---|
1583 | cli->pfd = cli->udp_fd;
|
---|
1584 |
|
---|
1585 | gbox_send_hello(cli, GBOX_STAT_HELLOL);
|
---|
1586 |
|
---|
1587 | if(!cli->reader->gbox_maxecmsend)
|
---|
1588 | { cli->reader->gbox_maxecmsend = DEFAULT_GBOX_MAX_ECM_SEND; }
|
---|
1589 |
|
---|
1590 | if(!cli->reader->gbox_maxdist)
|
---|
1591 | { cli->reader->gbox_maxdist = DEFAULT_GBOX_MAX_DIST; }
|
---|
1592 |
|
---|
1593 | //value > DEFAULT_GBOX_RESHARE not allowed in gbox network
|
---|
1594 | if(!cli->reader->gbox_reshare || cli->reader->gbox_reshare > DEFAULT_GBOX_RESHARE)
|
---|
1595 | { cli->reader->gbox_reshare = DEFAULT_GBOX_RESHARE; }
|
---|
1596 |
|
---|
1597 | if(!cli->reader->gbox_cccam_reshare || cli->reader->gbox_cccam_reshare > DEFAULT_GBOX_RESHARE)
|
---|
1598 | { cli->reader->gbox_cccam_reshare = DEFAULT_GBOX_RESHARE; }
|
---|
1599 |
|
---|
1600 | return 0;
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | static void gbox_s_idle(struct s_client *cl)
|
---|
1604 | {
|
---|
1605 | uint32_t time_since_last;
|
---|
1606 | struct s_client *proxy = get_gbox_proxy(cl->gbox_peer_id);
|
---|
1607 | struct gbox_peer *peer;
|
---|
1608 |
|
---|
1609 | if (proxy && proxy->gbox)
|
---|
1610 | {
|
---|
1611 | if (llabs(proxy->last - time(NULL)) > llabs(cl->lastecm - time(NULL)))
|
---|
1612 | { time_since_last = llabs(cl->lastecm - time(NULL)); }
|
---|
1613 | else { time_since_last = llabs(proxy->last - time(NULL)); }
|
---|
1614 | if (time_since_last > (HELLO_KEEPALIVE_TIME*3) && cl->gbox_peer_id != NO_GBOX_ID)
|
---|
1615 | {
|
---|
1616 | //gbox peer apparently died without saying goodbye
|
---|
1617 | peer = proxy->gbox;
|
---|
1618 | cs_writelock(&peer->lock);
|
---|
1619 | cs_log_dbg(D_READER, "time since last proxy activity in sec: %d => taking gbox peer offline",time_since_last);
|
---|
1620 | gbox_reinit_proxy(proxy);
|
---|
1621 | cs_writeunlock(&peer->lock);
|
---|
1622 | }
|
---|
1623 |
|
---|
1624 | time_since_last = llabs(cl->lastecm - time(NULL));
|
---|
1625 | if (time_since_last > HELLO_KEEPALIVE_TIME && cl->gbox_peer_id != NO_GBOX_ID)
|
---|
1626 | {
|
---|
1627 | peer = proxy->gbox;
|
---|
1628 | cs_writelock(&peer->lock);
|
---|
1629 | cs_log_dbg(D_READER, "time since last ecm in sec: %d => trigger keepalive hello",time_since_last);
|
---|
1630 | if (!peer->online)
|
---|
1631 | { gbox_send_hello(proxy, GBOX_STAT_HELLOL); }
|
---|
1632 | else
|
---|
1633 | { gbox_send_hello(proxy, GBOX_STAT_HELLOS); }
|
---|
1634 | cs_writeunlock(&peer->lock);
|
---|
1635 | }
|
---|
1636 | }
|
---|
1637 | //prevent users from timing out
|
---|
1638 | cs_log_dbg(D_READER, "client idle prevented: %s", username(cl));
|
---|
1639 | cl->last = time((time_t *)0);
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | static int8_t gbox_send_peer_good_night(struct s_client *proxy)
|
---|
1643 | {
|
---|
1644 | uchar outbuf[64];
|
---|
1645 | int32_t hostname_len = 0;
|
---|
1646 | if (cfg.gbox_hostname)
|
---|
1647 | hostname_len = strlen(cfg.gbox_hostname);
|
---|
1648 | int32_t len = hostname_len + 22;
|
---|
1649 | if(proxy->gbox && proxy->typ == 'p')
|
---|
1650 | {
|
---|
1651 | struct gbox_peer *peer = proxy->gbox;
|
---|
1652 | struct s_reader *rdr = proxy->reader;
|
---|
1653 | if (peer->online)
|
---|
1654 | {
|
---|
1655 | gbox_message_header(outbuf, MSG_HELLO, peer->gbox.password, local_gbox.password);
|
---|
1656 | outbuf[10] = 0x01;
|
---|
1657 | outbuf[11] = 0x80;
|
---|
1658 | memset(&outbuf[12], 0xff, 7);
|
---|
1659 | outbuf[19] = gbox_get_my_vers();
|
---|
1660 | outbuf[20] = gbox_get_my_cpu_api();
|
---|
1661 | memcpy(&outbuf[21], cfg.gbox_hostname, hostname_len);
|
---|
1662 | outbuf[21 + hostname_len] = hostname_len;
|
---|
1663 | cs_log("<- good night to %s:%d id: %04X", rdr->device, rdr->r_port, peer->gbox.id);
|
---|
1664 | gbox_compress(outbuf, len, &len);
|
---|
1665 | gbox_send(proxy, outbuf, len);
|
---|
1666 | gbox_reinit_proxy(proxy);
|
---|
1667 | }
|
---|
1668 | }
|
---|
1669 | return 0;
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | void gbox_send_good_night(void)
|
---|
1673 | {
|
---|
1674 | gbox_free_cardlist();
|
---|
1675 | struct s_client *cli;
|
---|
1676 | cs_readlock(&clientlist_lock);
|
---|
1677 | for(cli = first_client; cli; cli = cli->next)
|
---|
1678 | {
|
---|
1679 | if(cli->gbox && cli->typ == 'p')
|
---|
1680 | { gbox_send_peer_good_night(cli); }
|
---|
1681 | }
|
---|
1682 | cs_readunlock(&clientlist_lock);
|
---|
1683 | }
|
---|
1684 | /*
|
---|
1685 | void gbox_send_goodbye(uint16_t boxid) //to implement later
|
---|
1686 | {
|
---|
1687 | uchar outbuf[15];
|
---|
1688 | struct s_client *cli;
|
---|
1689 | cs_readlock(&clientlist_lock);
|
---|
1690 | for (cli = first_client; cli; cli = cli->next)
|
---|
1691 | {
|
---|
1692 | if(cli->gbox && cli->typ == 'p')
|
---|
1693 | {
|
---|
1694 | struct gbox_peer *peer = cli->gbox;
|
---|
1695 | if (peer->online && boxid == peer->gbox.id)
|
---|
1696 | {
|
---|
1697 | gbox_message_header(outbuf, MSG_GOODBYE, peer->gbox.password, local_gbox.password);
|
---|
1698 | cs_log("gbox send goodbye to boxid: %04X", peer->gbox.id);
|
---|
1699 | gbox_send(cli, outbuf, 0xA);
|
---|
1700 | }
|
---|
1701 | }
|
---|
1702 | }
|
---|
1703 | cs_readunlock(&clientlist_lock);
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | void gbox_send_HERE_query (uint16_t boxid) //gbox.net send this cmd
|
---|
1707 | {
|
---|
1708 | uchar outbuf[30];
|
---|
1709 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
1710 | struct s_client *cli;
|
---|
1711 | cs_readlock(&clientlist_lock);
|
---|
1712 | for (cli = first_client; cli; cli = cli->next)
|
---|
1713 | {
|
---|
1714 | if(cli->gbox && cli->typ == 'p')
|
---|
1715 | {
|
---|
1716 | struct gbox_peer *peer = cli->gbox;
|
---|
1717 | if (peer->online && boxid == peer->gbox.id)
|
---|
1718 | {
|
---|
1719 | gbox_message_header(outbuf, MSG_HERE, peer->gbox.password, local_gbox.password);
|
---|
1720 | outbuf[0xA] = gbox_get_my_vers();
|
---|
1721 | outbuf[0xB] = gbox_get_my_cpu_api();
|
---|
1722 | memcpy(&outbuf[0xC], cfg.gbox_hostname, hostname_len);
|
---|
1723 | cs_log("gbox send 'HERE?' to boxid: %04X", peer->gbox.id);
|
---|
1724 | gbox_send(cli, outbuf, hostname_len + 0xC);
|
---|
1725 | }
|
---|
1726 | }
|
---|
1727 | }
|
---|
1728 | cs_readunlock(&clientlist_lock);
|
---|
1729 | }
|
---|
1730 | //This is most likely the same as MSG_HERE. Don't know what would be the difference
|
---|
1731 | static void gbox_send_boxinfo(struct s_client *cli)
|
---|
1732 | {
|
---|
1733 | struct gbox_peer *peer = cli->gbox;
|
---|
1734 | uchar outbuf[256];
|
---|
1735 | int32_t hostname_len = strlen(cfg.gbox_hostname);
|
---|
1736 |
|
---|
1737 | gbox_message_header(outbuf, MSG_BOXINFO, peer);
|
---|
1738 | outbuf[0xA] = local_gbox.minor_version;
|
---|
1739 | outbuf[0xB] = local_gbox.type;
|
---|
1740 | memcpy(&outbuf[0xC], cfg.gbox_hostname, hostname_len);
|
---|
1741 | gbox_send(cli, outbuf, hostname_len + 0xC);
|
---|
1742 | }
|
---|
1743 | */
|
---|
1744 | void module_gbox(struct s_module *ph)
|
---|
1745 | {
|
---|
1746 | int32_t i;
|
---|
1747 | for(i = 0; i < CS_MAXPORTS; i++)
|
---|
1748 | {
|
---|
1749 | if(!cfg.gbx_port[i]) { break; }
|
---|
1750 | ph->ptab.nports++;
|
---|
1751 | ph->ptab.ports[i].s_port = cfg.gbx_port[i];
|
---|
1752 | }
|
---|
1753 | ph->desc = "gbox";
|
---|
1754 | ph->num = R_GBOX;
|
---|
1755 | ph->type = MOD_CONN_UDP;
|
---|
1756 | ph->large_ecm_support = 1;
|
---|
1757 | ph->listenertype = LIS_GBOX;
|
---|
1758 |
|
---|
1759 | ph->s_handler = gbox_server;
|
---|
1760 | ph->s_init = gbox_server_init;
|
---|
1761 |
|
---|
1762 | ph->send_dcw = gbox_send_dcw;
|
---|
1763 |
|
---|
1764 | ph->recv = gbox_recv;
|
---|
1765 | ph->c_init = gbox_client_init;
|
---|
1766 | ph->c_recv_chk = gbox_recv_chk;
|
---|
1767 | ph->c_send_ecm = gbox_send_ecm;
|
---|
1768 | ph->c_send_emm = gbox_send_emm;
|
---|
1769 |
|
---|
1770 | ph->s_idle = gbox_s_idle;
|
---|
1771 | }
|
---|
1772 | #endif
|
---|