1 | #include <string.h>
|
---|
2 | #include <stdlib.h>
|
---|
3 | #include "globals.h"
|
---|
4 | #include "module-cccam.h"
|
---|
5 | #include <time.h>
|
---|
6 | #include "reader-common.h"
|
---|
7 | #include <poll.h>
|
---|
8 | #include "module-cccshare.h"
|
---|
9 |
|
---|
10 | //Mode names for CMD_05 command:
|
---|
11 | const char *cmd05_mode_name[] = { "UNKNOWN", "PLAIN", "AES", "CC_CRYPT", "RC4",
|
---|
12 | "LEN=0" };
|
---|
13 |
|
---|
14 | //Mode names for CMD_0C command:
|
---|
15 | const char *cmd0c_mode_name[] = { "NONE", "RC6", "RC4", "CC_CRYPT", "AES", "IDEA" };
|
---|
16 |
|
---|
17 | static uint8 cc_node_id[8];
|
---|
18 |
|
---|
19 | #define getprefix() ((struct cc_data *)(cl->cc))->prefix
|
---|
20 |
|
---|
21 | void cc_init_crypt(struct cc_crypt_block *block, uint8 *key, int len) {
|
---|
22 | int i = 0;
|
---|
23 | uint8 j = 0;
|
---|
24 |
|
---|
25 | for (i = 0; i < 256; i++) {
|
---|
26 | block->keytable[i] = i;
|
---|
27 | }
|
---|
28 |
|
---|
29 | for (i = 0; i < 256; i++) {
|
---|
30 | j += key[i % len] + block->keytable[i];
|
---|
31 | SWAPC(&block->keytable[i], &block->keytable[j]);
|
---|
32 | }
|
---|
33 |
|
---|
34 | block->state = *key;
|
---|
35 | block->counter = 0;
|
---|
36 | block->sum = 0;
|
---|
37 | }
|
---|
38 |
|
---|
39 | void cc_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
40 | cc_crypt_mode_t mode) {
|
---|
41 | int i;
|
---|
42 | uint8 z;
|
---|
43 |
|
---|
44 | for (i = 0; i < len; i++) {
|
---|
45 | block->counter++;
|
---|
46 | block->sum += block->keytable[block->counter];
|
---|
47 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
48 | z = data[i];
|
---|
49 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
50 | + block->keytable[block->sum]) & 0xff];
|
---|
51 | data[i] ^= block->state;
|
---|
52 | if (!mode)
|
---|
53 | z = data[i];
|
---|
54 | block->state = block->state ^ z;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | void cc_rc4_crypt(struct cc_crypt_block *block, uint8 *data, int len,
|
---|
59 | cc_crypt_mode_t mode) {
|
---|
60 | int i;
|
---|
61 | uint8 z;
|
---|
62 |
|
---|
63 | for (i = 0; i < len; i++) {
|
---|
64 | block->counter++;
|
---|
65 | block->sum += block->keytable[block->counter];
|
---|
66 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
67 | z = data[i];
|
---|
68 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
69 | + block->keytable[block->sum]) & 0xff];
|
---|
70 | if (!mode)
|
---|
71 | z = data[i];
|
---|
72 | block->state = block->state ^ z;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | void cc_xor(uint8 *buf) {
|
---|
77 | const char cccam[] = "CCcam";
|
---|
78 | uint8 i;
|
---|
79 |
|
---|
80 | for (i = 0; i < 8; i++) {
|
---|
81 | buf[8 + i] = i * buf[i];
|
---|
82 | if (i <= 5) {
|
---|
83 | buf[i] ^= cccam[i];
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | void cc_cw_crypt(struct s_client *cl, uint8 *cws, uint32 cardid) {
|
---|
89 | struct cc_data *cc = cl->cc;
|
---|
90 | int64 node_id;
|
---|
91 | uint8 tmp;
|
---|
92 | int i;
|
---|
93 |
|
---|
94 | if (cl->typ != 'c') {
|
---|
95 | node_id = b2ll(8, cc->node_id);
|
---|
96 | } else {
|
---|
97 | node_id = b2ll(8, cc->peer_node_id);
|
---|
98 | }
|
---|
99 |
|
---|
100 | for (i = 0; i < 16; i++) {
|
---|
101 | tmp = cws[i] ^ (node_id >> (4 * i));
|
---|
102 | if (i & 1)
|
---|
103 | tmp = ~tmp;
|
---|
104 | cws[i] = (cardid >> (2 * i)) ^ tmp;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** swap endianness (int) */
|
---|
109 | static void SwapLBi(unsigned char *buff, int len)
|
---|
110 | {
|
---|
111 | #if __BYTE_ORDER != __BIG_ENDIAN
|
---|
112 | return;
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | int i;
|
---|
116 | unsigned char swap[4];
|
---|
117 | for (i = 0; i < len / 4; i++) {
|
---|
118 | memcpy(swap, buff, 4);
|
---|
119 | buff[0] = swap[3];
|
---|
120 | buff[1] = swap[2];
|
---|
121 | buff[2] = swap[1];
|
---|
122 | buff[3] = swap[0];
|
---|
123 | buff += 4;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | void cc_crypt_cmd0c(struct s_client *cl, uint8 *buf, int len) {
|
---|
128 | struct cc_data *cc = cl->cc;
|
---|
129 | uint8 *out = cs_malloc(&out, len, QUITERROR);
|
---|
130 |
|
---|
131 | switch (cc->cmd0c_mode) {
|
---|
132 | case MODE_CMD_0x0C_NONE: { // none additional encryption
|
---|
133 | memcpy(out, buf, len);
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | case MODE_CMD_0x0C_RC6 : { //RC6
|
---|
137 | int i;
|
---|
138 | SwapLBi(buf, len);
|
---|
139 | for (i = 0; i < len / 16; i++)
|
---|
140 | rc6_block_decrypt((unsigned int*)(buf+i*16), (unsigned int*)(out+i*16), 1, cc->cmd0c_RC6_cryptkey);
|
---|
141 | SwapLBi(out, len);
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | case MODE_CMD_0x0C_RC4: { // RC4
|
---|
145 | cc_rc4_crypt(&cc->cmd0c_cryptkey, buf, len, ENCRYPT);
|
---|
146 | memcpy(out, buf, len);
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | case MODE_CMD_0x0C_CC_CRYPT: { // cc_crypt
|
---|
150 | cc_crypt(&cc->cmd0c_cryptkey, buf, len, DECRYPT);
|
---|
151 | memcpy(out, buf, len);
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | case MODE_CMD_0x0C_AES: { // AES
|
---|
155 | int i;
|
---|
156 | for (i = 0; i<len / 16; i++)
|
---|
157 | AES_decrypt((unsigned char *) buf + i * 16,
|
---|
158 | (unsigned char *) out + i * 16, &cc->cmd0c_AES_key);
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case MODE_CMD_0x0C_IDEA : { //IDEA
|
---|
162 | int i=0;
|
---|
163 | int j;
|
---|
164 |
|
---|
165 | while (i < len) {
|
---|
166 | idea_ecb_encrypt(buf + i, out + i, &cc->cmd0c_IDEA_dkey);
|
---|
167 | i += 8;
|
---|
168 | }
|
---|
169 |
|
---|
170 | i = 8;
|
---|
171 | while (i < len) {
|
---|
172 | for (j=0; j < 8; j++)
|
---|
173 | out[j+i] ^= buf[j+i-8];
|
---|
174 | i += 8;
|
---|
175 | }
|
---|
176 |
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | memcpy(buf, out, len);
|
---|
181 | free(out);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | void set_cmd0c_cryptkey(struct s_client *cl, uint8 *key, uint8 len) {
|
---|
187 | struct cc_data *cc = cl->cc;
|
---|
188 | uint8 key_buf[32];
|
---|
189 |
|
---|
190 | memset(&key_buf, 0, sizeof(key_buf));
|
---|
191 |
|
---|
192 | if (len > 32)
|
---|
193 | len = 32;
|
---|
194 |
|
---|
195 | memcpy(key_buf, key, len);
|
---|
196 |
|
---|
197 | switch (cc->cmd0c_mode) {
|
---|
198 |
|
---|
199 | case MODE_CMD_0x0C_NONE : { //NONE
|
---|
200 | break;
|
---|
201 | }
|
---|
202 |
|
---|
203 | case MODE_CMD_0x0C_RC6 : { //RC6
|
---|
204 | rc6_key_setup(key_buf, 32, cc->cmd0c_RC6_cryptkey);
|
---|
205 | break;
|
---|
206 | }
|
---|
207 |
|
---|
208 | case MODE_CMD_0x0C_RC4: //RC4
|
---|
209 | case MODE_CMD_0x0C_CC_CRYPT: { //CC_CRYPT
|
---|
210 | cc_init_crypt(&cc->cmd0c_cryptkey, key_buf, 32);
|
---|
211 | break;
|
---|
212 | }
|
---|
213 |
|
---|
214 | case MODE_CMD_0x0C_AES: { //AES
|
---|
215 | memset(&cc->cmd0c_AES_key, 0, sizeof(cc->cmd0c_AES_key));
|
---|
216 | AES_set_decrypt_key((unsigned char *) key_buf, 256, &cc->cmd0c_AES_key);
|
---|
217 | break;
|
---|
218 | }
|
---|
219 |
|
---|
220 | case MODE_CMD_0x0C_IDEA : { //IDEA
|
---|
221 | uint8 key_buf_idea[16];
|
---|
222 | memcpy(key_buf_idea, key_buf, 16);
|
---|
223 | IDEA_KEY_SCHEDULE ekey;
|
---|
224 |
|
---|
225 | idea_set_encrypt_key(key_buf_idea, &ekey);
|
---|
226 | idea_set_decrypt_key(&ekey,&cc->cmd0c_IDEA_dkey);
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | int sid_eq(struct cc_srvid *srvid1, struct cc_srvid *srvid2) {
|
---|
233 | return (srvid1->sid == srvid2->sid && (srvid1->ecmlen == srvid2->ecmlen || !srvid1->ecmlen || !srvid2->ecmlen));
|
---|
234 | }
|
---|
235 |
|
---|
236 | int is_sid_blocked(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
237 | LL_ITER *it = ll_iter_create(card->badsids);
|
---|
238 | struct cc_srvid *srvid;
|
---|
239 | while ((srvid = ll_iter_next(it))) {
|
---|
240 | if (sid_eq(srvid, srvid_blocked)) {
|
---|
241 | break;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | ll_iter_release(it);
|
---|
245 | return (srvid != 0);
|
---|
246 | }
|
---|
247 |
|
---|
248 | int is_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
249 | LL_ITER *it = ll_iter_create(card->goodsids);
|
---|
250 | struct cc_srvid *srvid;
|
---|
251 | while ((srvid = ll_iter_next(it))) {
|
---|
252 | if (sid_eq(srvid, srvid_good)) {
|
---|
253 | break;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | ll_iter_release(it);
|
---|
257 | return (srvid != 0);
|
---|
258 | }
|
---|
259 |
|
---|
260 | void add_sid_block(struct s_client *cl __attribute__((unused)), struct cc_card *card,
|
---|
261 | struct cc_srvid *srvid_blocked) {
|
---|
262 | if (is_sid_blocked(card, srvid_blocked))
|
---|
263 | return;
|
---|
264 |
|
---|
265 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
266 | *srvid = *srvid_blocked;
|
---|
267 | ll_append(card->badsids, srvid);
|
---|
268 | cs_debug_mask(D_READER, "%s added sid block %04X(%d) for card %08x",
|
---|
269 | getprefix(), srvid_blocked->sid, srvid_blocked->ecmlen,
|
---|
270 | card->id);
|
---|
271 | }
|
---|
272 |
|
---|
273 | void remove_sid_block(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
274 | LL_ITER *it = ll_iter_create(card->badsids);
|
---|
275 | struct cc_srvid *srvid;
|
---|
276 | while ((srvid = ll_iter_next(it)))
|
---|
277 | if (sid_eq(srvid, srvid_blocked))
|
---|
278 | ll_iter_remove_data(it);
|
---|
279 | ll_iter_release(it);
|
---|
280 | }
|
---|
281 |
|
---|
282 | void remove_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
283 | LL_ITER *it = ll_iter_create(card->goodsids);
|
---|
284 | struct cc_srvid *srvid;
|
---|
285 | while ((srvid = ll_iter_next(it)))
|
---|
286 | if (sid_eq(srvid, srvid_good))
|
---|
287 | ll_iter_remove_data(it);
|
---|
288 | ll_iter_release(it);
|
---|
289 | }
|
---|
290 |
|
---|
291 | void add_good_sid(struct s_client *cl __attribute__((unused)), struct cc_card *card,
|
---|
292 | struct cc_srvid *srvid_good) {
|
---|
293 | if (is_good_sid(card, srvid_good))
|
---|
294 | return;
|
---|
295 |
|
---|
296 | remove_sid_block(card, srvid_good);
|
---|
297 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
298 | memcpy(srvid, srvid_good, sizeof(struct cc_srvid));
|
---|
299 | ll_append(card->goodsids, srvid);
|
---|
300 | cs_debug_mask(D_READER, "%s added good sid %04X(%d) for card %08x",
|
---|
301 | getprefix(), srvid_good->sid, srvid_good->ecmlen, card->id);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * reader
|
---|
306 | * clears and frees values for reinit
|
---|
307 | */
|
---|
308 | void cc_cli_close(struct s_client *cl, int call_conclose) {
|
---|
309 | struct s_reader *rdr = cl->reader;
|
---|
310 | struct cc_data *cc = cl->cc;
|
---|
311 | if (!rdr || !cc)
|
---|
312 | return;
|
---|
313 |
|
---|
314 | rdr->tcp_connected = 0;
|
---|
315 | rdr->card_status = NO_CARD;
|
---|
316 | rdr->available = 0;
|
---|
317 | rdr->ncd_msgid = 0;
|
---|
318 | rdr->last_s = rdr->last_g = 0;
|
---|
319 |
|
---|
320 | if (cc->mode == CCCAM_MODE_NORMAL && call_conclose)
|
---|
321 | network_tcp_connection_close(cl, cl->udp_fd);
|
---|
322 | else {
|
---|
323 | if (cl->udp_fd)
|
---|
324 | close(cl->udp_fd);
|
---|
325 | cl->udp_fd = 0;
|
---|
326 | cl->pfd = 0;
|
---|
327 | }
|
---|
328 |
|
---|
329 | cc->just_logged_in = 0;
|
---|
330 | }
|
---|
331 |
|
---|
332 | struct cc_extended_ecm_idx *add_extended_ecm_idx(struct s_client *cl,
|
---|
333 | uint8 send_idx, ushort ecm_idx, struct cc_card *card,
|
---|
334 | struct cc_srvid srvid) {
|
---|
335 | struct cc_data *cc = cl->cc;
|
---|
336 | struct cc_extended_ecm_idx *eei =
|
---|
337 | cs_malloc(&eei, sizeof(struct cc_extended_ecm_idx), QUITERROR);
|
---|
338 | eei->send_idx = send_idx;
|
---|
339 | eei->ecm_idx = ecm_idx;
|
---|
340 | eei->card = card;
|
---|
341 | eei->srvid = srvid;
|
---|
342 | ll_append(cc->extended_ecm_idx, eei);
|
---|
343 | //cs_debug_mask(D_TRACE, "%s add extended ecm-idx: %d:%d", getprefix(), send_idx, ecm_idx);
|
---|
344 | return eei;
|
---|
345 | }
|
---|
346 |
|
---|
347 | struct cc_extended_ecm_idx *get_extended_ecm_idx(struct s_client *cl,
|
---|
348 | uint8 send_idx, int remove) {
|
---|
349 | struct cc_data *cc = cl->cc;
|
---|
350 | struct cc_extended_ecm_idx *eei;
|
---|
351 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
352 | while ((eei = ll_iter_next(it))) {
|
---|
353 | if (eei->send_idx == send_idx) {
|
---|
354 | if (remove)
|
---|
355 | ll_iter_remove(it);
|
---|
356 | //cs_debug_mask(D_TRACE, "%s get by send-idx: %d FOUND: %d",
|
---|
357 | // getprefix(), send_idx, eei->ecm_idx);
|
---|
358 | ll_iter_release(it);
|
---|
359 | return eei;
|
---|
360 | }
|
---|
361 | }
|
---|
362 | ll_iter_release(it);
|
---|
363 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s get by send-idx: %d NOT FOUND", getprefix(),
|
---|
364 | send_idx);
|
---|
365 | return NULL;
|
---|
366 | }
|
---|
367 |
|
---|
368 | struct cc_extended_ecm_idx *get_extended_ecm_idx_by_idx(struct s_client *cl,
|
---|
369 | ushort ecm_idx, int remove) {
|
---|
370 | struct cc_data *cc = cl->cc;
|
---|
371 | struct cc_extended_ecm_idx *eei;
|
---|
372 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
373 | while ((eei = ll_iter_next(it))) {
|
---|
374 | if (eei->ecm_idx == ecm_idx) {
|
---|
375 | if (remove)
|
---|
376 | ll_iter_remove(it);
|
---|
377 | //cs_debug_mask(D_TRACE, "%s get by ecm-idx: %d FOUND: %d",
|
---|
378 | // getprefix(), ecm_idx, eei->send_idx);
|
---|
379 | ll_iter_release(it);
|
---|
380 | return eei;
|
---|
381 | }
|
---|
382 | }
|
---|
383 | ll_iter_release(it);
|
---|
384 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s get by ecm-idx: %d NOT FOUND", getprefix(),
|
---|
385 | ecm_idx);
|
---|
386 | return NULL;
|
---|
387 | }
|
---|
388 |
|
---|
389 | void cc_reset_pending(struct s_client *cl, int ecm_idx) {
|
---|
390 | int i = 0;
|
---|
391 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
392 | if (cl->ecmtask[i].idx == ecm_idx && cl->ecmtask[i].rc == 101)
|
---|
393 | cl->ecmtask[i].rc = 100; //Mark unused
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | void free_extended_ecm_idx_by_card(struct s_client *cl, struct cc_card *card) {
|
---|
398 | struct cc_data *cc = cl->cc;
|
---|
399 | struct cc_extended_ecm_idx *eei;
|
---|
400 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
401 | while ((eei = ll_iter_next(it))) {
|
---|
402 | if (eei->card == card) {
|
---|
403 | cc_reset_pending(cl, eei->ecm_idx);
|
---|
404 | ll_iter_remove_data(it);
|
---|
405 | }
|
---|
406 | }
|
---|
407 | ll_iter_release(it);
|
---|
408 | }
|
---|
409 |
|
---|
410 | void free_extended_ecm_idx(struct cc_data *cc) {
|
---|
411 | struct cc_extended_ecm_idx *eei;
|
---|
412 | LL_ITER *it = ll_iter_create(cc->extended_ecm_idx);
|
---|
413 | while ((eei = ll_iter_next(it)))
|
---|
414 | ll_iter_remove_data(it);
|
---|
415 | ll_iter_release(it);
|
---|
416 | }
|
---|
417 |
|
---|
418 | int cc_recv_to(struct s_client *cl, uint8 *buf, int len) {
|
---|
419 | fd_set fds;
|
---|
420 | struct timeval timeout;
|
---|
421 | int rc;
|
---|
422 |
|
---|
423 | timeout.tv_sec = 2;
|
---|
424 | timeout.tv_usec = 0;
|
---|
425 |
|
---|
426 | while (1) {
|
---|
427 | FD_ZERO(&fds);
|
---|
428 | FD_SET(cl->udp_fd, &fds);
|
---|
429 |
|
---|
430 | rc=select(cl->udp_fd+1, &fds, 0, 0, &timeout);
|
---|
431 | if (rc<0) {
|
---|
432 | if (errno==EINTR) continue;
|
---|
433 | else return(-1); //error!!
|
---|
434 | }
|
---|
435 |
|
---|
436 | if(FD_ISSET(cl->udp_fd,&fds))
|
---|
437 | break;
|
---|
438 | else
|
---|
439 | return (-2); //timeout!!
|
---|
440 | }
|
---|
441 | return recv(cl->udp_fd, buf, len, MSG_WAITALL);
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * reader
|
---|
446 | * closes the connection and reopens it.
|
---|
447 | */
|
---|
448 | //static void cc_cycle_connection() {
|
---|
449 | // cc_cli_close();
|
---|
450 | // cc_cli_init();
|
---|
451 | //}
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * reader+server:
|
---|
455 | * receive a message
|
---|
456 | */
|
---|
457 | int cc_msg_recv(struct s_client *cl, uint8 *buf, int maxlen) {
|
---|
458 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
459 |
|
---|
460 | int len;
|
---|
461 | struct cc_data *cc = cl->cc;
|
---|
462 |
|
---|
463 | int handle = cl->udp_fd;
|
---|
464 |
|
---|
465 | if (handle <= 0 || maxlen < 4)
|
---|
466 | return -1;
|
---|
467 |
|
---|
468 | len = recv(handle, buf, 4, MSG_WAITALL);
|
---|
469 | if (rdr)
|
---|
470 | rdr->last_g = time(NULL);
|
---|
471 |
|
---|
472 | if (len != 4) { // invalid header length read
|
---|
473 | if (len <= 0)
|
---|
474 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s disconnected by remote server", getprefix());
|
---|
475 | else
|
---|
476 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s invalid header length (expected 4, read %d)", getprefix(), len);
|
---|
477 | return -1;
|
---|
478 | }
|
---|
479 |
|
---|
480 | pthread_mutex_lock(&cc->lockcmd);
|
---|
481 |
|
---|
482 | cc_crypt(&cc->block[DECRYPT], buf, 4, DECRYPT);
|
---|
483 | //cs_ddump_mask(D_CLIENT, buf, 4, "cccam: decrypted header:");
|
---|
484 |
|
---|
485 | cc->g_flag = buf[0];
|
---|
486 |
|
---|
487 | int size = (buf[2] << 8) | buf[3];
|
---|
488 | if (size) { // check if any data is expected in msg
|
---|
489 | if (size > maxlen) {
|
---|
490 | pthread_mutex_unlock(&cc->lockcmd);
|
---|
491 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s message too big (size=%d max=%d)", getprefix(), size, maxlen);
|
---|
492 | return 0;
|
---|
493 | }
|
---|
494 |
|
---|
495 | len = recv(handle, buf + 4, size, MSG_WAITALL); // read rest of msg
|
---|
496 | if (rdr)
|
---|
497 | rdr->last_g = time(NULL);
|
---|
498 |
|
---|
499 | if (len != size) {
|
---|
500 | pthread_mutex_unlock(&cc->lockcmd);
|
---|
501 | if (len <= 0)
|
---|
502 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s disconnected by remote", getprefix());
|
---|
503 | else
|
---|
504 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s invalid message length read (expected %d, read %d)",
|
---|
505 | getprefix(), size, len);
|
---|
506 | return -1;
|
---|
507 | }
|
---|
508 |
|
---|
509 | cc_crypt(&cc->block[DECRYPT], buf + 4, len, DECRYPT);
|
---|
510 | len += 4;
|
---|
511 | }
|
---|
512 |
|
---|
513 | pthread_mutex_unlock(&cc->lockcmd);
|
---|
514 |
|
---|
515 | //cs_ddump_mask(cl->typ=='c'?D_CLIENT:D_READER, buf, len, "cccam: full decrypted msg, len=%d:", len);
|
---|
516 |
|
---|
517 | return len;
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * reader+server
|
---|
522 | * send a message
|
---|
523 | */
|
---|
524 | int cc_cmd_send(struct s_client *cl, uint8 *buf, int len, cc_msg_type_t cmd) {
|
---|
525 | if (!cl->udp_fd) //disconnected
|
---|
526 | return -1;
|
---|
527 |
|
---|
528 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
529 |
|
---|
530 | int n;
|
---|
531 | uint8 netbuf[len + 4];
|
---|
532 | struct cc_data *cc = cl->cc;
|
---|
533 |
|
---|
534 | pthread_mutex_lock(&cc->lockcmd); //We need this because cc_cmd_send is called from cccshare
|
---|
535 |
|
---|
536 | memset(netbuf, 0, len + 4);
|
---|
537 |
|
---|
538 | if (cmd == MSG_NO_HEADER) {
|
---|
539 | memcpy(netbuf, buf, len);
|
---|
540 | } else {
|
---|
541 | // build command message
|
---|
542 | netbuf[0] = cc->g_flag; // flags??
|
---|
543 | netbuf[1] = cmd & 0xff;
|
---|
544 | netbuf[2] = len >> 8;
|
---|
545 | netbuf[3] = len & 0xff;
|
---|
546 | if (buf)
|
---|
547 | memcpy(netbuf + 4, buf, len);
|
---|
548 | len += 4;
|
---|
549 | }
|
---|
550 |
|
---|
551 | //cs_ddump_mask(D_CLIENT, netbuf, len, "cccam: send:");
|
---|
552 | cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
|
---|
553 |
|
---|
554 | n = send(cl->udp_fd, netbuf, len, 0);
|
---|
555 | if (rdr)
|
---|
556 | rdr->last_s = time(NULL);
|
---|
557 |
|
---|
558 | pthread_mutex_unlock(&cc->lockcmd);
|
---|
559 |
|
---|
560 | if (n != len) {
|
---|
561 | if (rdr)
|
---|
562 | cc_cli_close(cl, TRUE);
|
---|
563 | n = -1;
|
---|
564 | }
|
---|
565 |
|
---|
566 | return n;
|
---|
567 | }
|
---|
568 |
|
---|
569 | #define CC_DEFAULT_VERSION 1
|
---|
570 | char *version[] = { "2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", "2.2.0", "2.2.1", "" };
|
---|
571 | char *build[] = { "2892", "2971", "3094", "3165", "3191", "3290", "3316", "" };
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * reader+server
|
---|
575 | * checks the cccam-version in the configuration
|
---|
576 | */
|
---|
577 | void cc_check_version(char *cc_version, char *cc_build) {
|
---|
578 | int i;
|
---|
579 | for (i = 0; strlen(version[i]); i++) {
|
---|
580 | if (!memcmp(cc_version, version[i], strlen(version[i]))) {
|
---|
581 | memcpy(cc_build, build[i], strlen(build[i]) + 1);
|
---|
582 | cs_debug_mask(D_CLIENT, "cccam: auto build set for version: %s build: %s",
|
---|
583 | cc_version, cc_build);
|
---|
584 | return;
|
---|
585 | }
|
---|
586 | }
|
---|
587 | memcpy(cc_version, version[CC_DEFAULT_VERSION], strlen(
|
---|
588 | version[CC_DEFAULT_VERSION]));
|
---|
589 | memcpy(cc_build, build[CC_DEFAULT_VERSION], strlen(
|
---|
590 | build[CC_DEFAULT_VERSION]));
|
---|
591 |
|
---|
592 | cs_debug_mask(D_CLIENT, "cccam: auto version set: %s build: %s", cc_version, cc_build);
|
---|
593 | }
|
---|
594 |
|
---|
595 | /**
|
---|
596 | * reader
|
---|
597 | * sends own version information to the CCCam server
|
---|
598 | */
|
---|
599 | int cc_send_cli_data(struct s_client *cl) {
|
---|
600 | struct s_reader *rdr = cl->reader;
|
---|
601 |
|
---|
602 | int i;
|
---|
603 | struct cc_data *cc = cl->cc;
|
---|
604 | const int size = 20 + 8 + 6 + 26 + 4 + 28 + 1;
|
---|
605 |
|
---|
606 | cs_debug_mask(D_READER, "cccam: send client data");
|
---|
607 |
|
---|
608 | memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
|
---|
609 |
|
---|
610 | uint8 buf[size];
|
---|
611 | memset(buf, 0, size);
|
---|
612 |
|
---|
613 | memcpy(buf, rdr->r_usr, sizeof(rdr->r_usr));
|
---|
614 | memcpy(buf + 20, cc->node_id, 8);
|
---|
615 | buf[28] = rdr->cc_want_emu; // <-- Client want to have EMUs, 0 - NO; 1 - YES
|
---|
616 | memcpy(buf + 29, rdr->cc_version, sizeof(rdr->cc_version)); // cccam version (ascii)
|
---|
617 | memcpy(buf + 61, rdr->cc_build, sizeof(rdr->cc_build)); // build number (ascii)
|
---|
618 |
|
---|
619 | cs_debug_mask(D_READER, "%s sending own version: %s, build: %s", getprefix(),
|
---|
620 | rdr->cc_version, rdr->cc_build);
|
---|
621 |
|
---|
622 | i = cc_cmd_send(cl, buf, size, MSG_CLI_DATA);
|
---|
623 |
|
---|
624 | return i;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * server
|
---|
629 | * sends version information to the client
|
---|
630 | */
|
---|
631 | int cc_send_srv_data(struct s_client *cl) {
|
---|
632 | struct cc_data *cc = cl->cc;
|
---|
633 |
|
---|
634 | cs_debug_mask(D_CLIENT, "cccam: send server data");
|
---|
635 |
|
---|
636 | memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
|
---|
637 |
|
---|
638 | uint8 buf[0x48];
|
---|
639 | memset(buf, 0, 0x48);
|
---|
640 |
|
---|
641 | int stealth = cl->account->cccstealth;
|
---|
642 | if (stealth == -1)
|
---|
643 | stealth = cfg.cc_stealth;
|
---|
644 | if (stealth)
|
---|
645 | {
|
---|
646 | int i;
|
---|
647 | for (i=0;i<8;i++)
|
---|
648 | buf[i] = fast_rnd();
|
---|
649 | }
|
---|
650 | else
|
---|
651 | memcpy(buf, cc->node_id, 8);
|
---|
652 | char cc_build[7];
|
---|
653 | cc_check_version((char *) cfg.cc_version, cc_build);
|
---|
654 | memcpy(buf + 8, cfg.cc_version, sizeof(cfg.cc_version)); // cccam version (ascii)
|
---|
655 | memcpy(buf + 40, cc_build, sizeof(cc_build)); // build number (ascii)
|
---|
656 |
|
---|
657 | cs_debug_mask(D_CLIENT, "%s version: %s, build: %s nodeid: %s", getprefix(),
|
---|
658 | cfg.cc_version, cc_build, cs_hexdump(0, cc->peer_node_id, 8));
|
---|
659 |
|
---|
660 | return cc_cmd_send(cl, buf, 0x48, MSG_SRV_DATA);
|
---|
661 | }
|
---|
662 |
|
---|
663 | int loop_check(uint8 *myid, struct s_client *cl) {
|
---|
664 | if (!cl)
|
---|
665 | return 0;
|
---|
666 |
|
---|
667 | struct cc_data *cc = cl->cc;
|
---|
668 | if (!cc)
|
---|
669 | return 0;
|
---|
670 |
|
---|
671 | return !memcmp(myid, cc->peer_node_id, sizeof(cc->peer_node_id)); // same nodeid? ignore
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * reader
|
---|
676 | * retrieves the next waiting ecm request
|
---|
677 | */
|
---|
678 | int cc_get_nxt_ecm(struct s_client *cl) {
|
---|
679 | struct cc_data *cc = cl->cc;
|
---|
680 | ECM_REQUEST *er, *ern = NULL;
|
---|
681 | int n, i;
|
---|
682 | time_t t;
|
---|
683 |
|
---|
684 | t = time(NULL);
|
---|
685 | n = -1;
|
---|
686 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
687 | er = &cl->ecmtask[i];
|
---|
688 | if ((t - (ulong) er->tps.time > ((cfg.ctimeout + 500)
|
---|
689 | / 1000) + 1) && (er->rc >= 10)) // drop timeouts
|
---|
690 | {
|
---|
691 | er->rc = 0;
|
---|
692 | }
|
---|
693 |
|
---|
694 | else if (er->rc >= 10 && er->rc != 101) { // stil active and waiting
|
---|
695 | if (loop_check(cc->peer_node_id, er->client)) {
|
---|
696 | er->rc = E_RDR_NOTFOUND;
|
---|
697 | er->rcEx = E2_CCCAM_LOOP;
|
---|
698 | cs_debug_mask(D_READER, "%s ecm loop detected! client %s (%8X)",
|
---|
699 | getprefix(), er->client->account->usr, er->client->thread);
|
---|
700 | write_ecm_answer(cl->reader, &cl->ecmtask[i]);
|
---|
701 | }
|
---|
702 | else
|
---|
703 | // search for the ecm with the lowest time, this should be the next to go
|
---|
704 | if (n < 0 || (ern->tps.time - er->tps.time < 0)) {
|
---|
705 |
|
---|
706 | //check for already pending:
|
---|
707 | if (cc->extended_mode) {
|
---|
708 | int j,found;
|
---|
709 | ECM_REQUEST *erx;
|
---|
710 | for (found=j=0;j<CS_MAXPENDING;j++) {
|
---|
711 | erx = &cl->ecmtask[j];
|
---|
712 | if (i!=j && erx->rc == 101 &&
|
---|
713 | er->caid==erx->caid &&
|
---|
714 | er->ecmd5==erx->ecmd5) {
|
---|
715 | found=1;
|
---|
716 | break;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | if (!found) {
|
---|
720 | n = i;
|
---|
721 | ern = er;
|
---|
722 | }
|
---|
723 | }
|
---|
724 | else {
|
---|
725 | n = i;
|
---|
726 | ern = er;
|
---|
727 | }
|
---|
728 | }
|
---|
729 | }
|
---|
730 | }
|
---|
731 | return n;
|
---|
732 | }
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * sends the secret cmd05 answer to the server
|
---|
736 | */
|
---|
737 | int send_cmd05_answer(struct s_client *cl) {
|
---|
738 | struct s_reader *rdr = cl->reader;
|
---|
739 | struct cc_data *cc = cl->cc;
|
---|
740 | if (!cc->cmd05_active || !rdr->available) //exit if not in cmd05 or waiting for ECM answer
|
---|
741 | return 0;
|
---|
742 |
|
---|
743 | cc->cmd05_active--;
|
---|
744 | if (cc->cmd05_active)
|
---|
745 | return 0;
|
---|
746 |
|
---|
747 | uint8 *data = cc->cmd05_data;
|
---|
748 | cc_cmd05_mode cmd05_mode = MODE_UNKNOWN;
|
---|
749 |
|
---|
750 | // by Project:Keynation
|
---|
751 | switch (cc->cmd05_data_len) {
|
---|
752 | case 0: { //payload 0, return with payload 0!
|
---|
753 | cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
|
---|
754 | cmd05_mode = MODE_LEN0;
|
---|
755 | break;
|
---|
756 | }
|
---|
757 | case 256: {
|
---|
758 | cmd05_mode = cc->cmd05_mode;
|
---|
759 | switch (cmd05_mode) {
|
---|
760 | case MODE_PLAIN: { //Send plain unencrypted back
|
---|
761 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
762 | break;
|
---|
763 | }
|
---|
764 | case MODE_AES: { //encrypt with received aes128 key:
|
---|
765 | AES_KEY key;
|
---|
766 | uint8 aeskey[16];
|
---|
767 | uint8 out[256];
|
---|
768 |
|
---|
769 | memcpy(aeskey, cc->cmd05_aeskey, 16);
|
---|
770 | memset(&key, 0, sizeof(key));
|
---|
771 |
|
---|
772 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
773 | int i;
|
---|
774 | for (i = 0; i < 256; i += 16)
|
---|
775 | AES_encrypt((unsigned char *) data + i, (unsigned char *) &out
|
---|
776 | + i, &key);
|
---|
777 |
|
---|
778 | cc_cmd_send(cl, out, 256, MSG_CMD_05);
|
---|
779 | break;
|
---|
780 | }
|
---|
781 | case MODE_CC_CRYPT: { //encrypt with cc_crypt:
|
---|
782 | cc_crypt(&cc->cmd05_cryptkey, data, 256, ENCRYPT);
|
---|
783 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
784 | break;
|
---|
785 | }
|
---|
786 | case MODE_RC4_CRYPT: {//special xor crypt:
|
---|
787 | cc_rc4_crypt(&cc->cmd05_cryptkey, data, 256, DECRYPT);
|
---|
788 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
789 | break;
|
---|
790 | }
|
---|
791 | default:
|
---|
792 | cmd05_mode = MODE_UNKNOWN;
|
---|
793 | }
|
---|
794 | break;
|
---|
795 | }
|
---|
796 | default:
|
---|
797 | cmd05_mode = MODE_UNKNOWN;
|
---|
798 | }
|
---|
799 |
|
---|
800 | //unhandled types always needs cycle connection after 50 ECMs!!
|
---|
801 | if (cmd05_mode == MODE_UNKNOWN) {
|
---|
802 | cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
|
---|
803 | if (!cc->max_ecms) { //max_ecms already set?
|
---|
804 | cc->max_ecms = 50;
|
---|
805 | cc->ecm_counter = 0;
|
---|
806 | }
|
---|
807 | }
|
---|
808 | cs_debug_mask(D_READER, "%s sending CMD_05 back! MODE: %s len=%d",
|
---|
809 | getprefix(), cmd05_mode_name[cmd05_mode], cc->cmd05_data_len);
|
---|
810 |
|
---|
811 | cc->cmd05NOK = 1;
|
---|
812 | return 1;
|
---|
813 | }
|
---|
814 |
|
---|
815 | int get_UA_ofs(uint16 caid) {
|
---|
816 | int ofs = 0;
|
---|
817 | switch (caid >> 8) {
|
---|
818 | case 0x05: //VIACCESS:
|
---|
819 | case 0x0D: //CRYPTOWORKS:
|
---|
820 | ofs = 1;
|
---|
821 | break;
|
---|
822 | case 0x4B: //TONGFANG:
|
---|
823 | case 0x09: //VIDEOGUARD:
|
---|
824 | case 0x0B: //CONAX:
|
---|
825 | case 0x18: //NAGRA:
|
---|
826 | case 0x17: //BETACRYPT
|
---|
827 | case 0x06: //IRDETO:
|
---|
828 | ofs = 2;
|
---|
829 | break;
|
---|
830 | }
|
---|
831 | return ofs;
|
---|
832 | }
|
---|
833 |
|
---|
834 | int UA_len(uint8 *ua) {
|
---|
835 | int i, len=0;
|
---|
836 | for (i=0;i<8;i++)
|
---|
837 | if (ua[i]) len++;
|
---|
838 | return len;
|
---|
839 | }
|
---|
840 |
|
---|
841 | void UA_left(uint8 *in, uint8 *out, int len) {
|
---|
842 | int ofs = 0;
|
---|
843 | int maxlen = 8;
|
---|
844 | int orglen = len;
|
---|
845 | while (len) {
|
---|
846 | memset(out, 0, orglen);
|
---|
847 | memcpy(out, in+ofs, len);
|
---|
848 | if (out[0]) break;
|
---|
849 | ofs++;
|
---|
850 | maxlen--;
|
---|
851 | if (len>maxlen)
|
---|
852 | len=maxlen;
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 | void UA_right(uint8 *in, uint8 *out, int len) {
|
---|
857 | int ofs = 0;
|
---|
858 | while (len) {
|
---|
859 | memcpy(out+ofs, in, len);
|
---|
860 | len--;
|
---|
861 | if (out[len]) break;
|
---|
862 | ofs++;
|
---|
863 | out[0]=0;
|
---|
864 | }
|
---|
865 | }
|
---|
866 |
|
---|
867 | /**
|
---|
868 | * cccam uses UA right justified
|
---|
869 | **/
|
---|
870 | void cc_UA_oscam2cccam(uint8 *in, uint8 *out, uint16 caid) {
|
---|
871 | uint8 tmp[8];
|
---|
872 | memset(out, 0, 8);
|
---|
873 | memset(tmp, 0, 8);
|
---|
874 | //switch (caid>>8) {
|
---|
875 | // case 0x17: //IRDETO/Betacrypt:
|
---|
876 | // //oscam: AA BB CC DD 00 00 00 00
|
---|
877 | // //cccam: 00 00 00 00 DD AA BB CC
|
---|
878 | // out[4] = in[3]; //Hexbase
|
---|
879 | // out[5] = in[0];
|
---|
880 | // out[6] = in[1];
|
---|
881 | // out[7] = in[2];
|
---|
882 | // return;
|
---|
883 | //
|
---|
884 | // //Place here your own adjustments!
|
---|
885 | //}
|
---|
886 | hexserial_to_newcamd(in, tmp+2, caid);
|
---|
887 | UA_right(tmp, out, 8);
|
---|
888 | }
|
---|
889 |
|
---|
890 | /**
|
---|
891 | * oscam has a special format, depends on offset or type:
|
---|
892 | **/
|
---|
893 | void cc_UA_cccam2oscam(uint8 *in, uint8 *out, uint16 caid) {
|
---|
894 | uint8 tmp[8];
|
---|
895 | memset(out, 0, 8);
|
---|
896 | memset(tmp, 0, 8);
|
---|
897 | //switch(caid>>8) {
|
---|
898 | // case 0x17: //IRDETO/Betacrypt:
|
---|
899 | // //cccam: 00 00 00 00 DD AA BB CC
|
---|
900 | // //oscam: AA BB CC DD 00 00 00 00
|
---|
901 | // out[0] = in[5];
|
---|
902 | // out[1] = in[6];
|
---|
903 | // out[2] = in[7];
|
---|
904 | // out[3] = in[4]; //Hexbase
|
---|
905 | // return;
|
---|
906 |
|
---|
907 | // //Place here your own adjustments!
|
---|
908 | //}
|
---|
909 | int ofs = get_UA_ofs(caid);
|
---|
910 | int len = 8-ofs;
|
---|
911 | UA_left(in, tmp+ofs, len);
|
---|
912 | newcamd_to_hexserial(tmp, out, caid);
|
---|
913 | }
|
---|
914 |
|
---|
915 | void cc_SA_oscam2cccam(uint8 *in, uint8 *out) {
|
---|
916 | memcpy(out, in, 4);
|
---|
917 | }
|
---|
918 |
|
---|
919 | void cc_SA_cccam2oscam(uint8 *in, uint8 *out) {
|
---|
920 | memcpy(out, in, 4);
|
---|
921 | }
|
---|
922 |
|
---|
923 | int cc_UA_valid(uint8 *ua) {
|
---|
924 | int i;
|
---|
925 | for (i = 0; i < 8; i++)
|
---|
926 | if (ua[i])
|
---|
927 | return 1;
|
---|
928 | return 0;
|
---|
929 | }
|
---|
930 |
|
---|
931 | /**
|
---|
932 | * Updates AU Data: UA (Unique ID / Hexserial) und SA (Shared ID - Provider)
|
---|
933 | */
|
---|
934 | void set_au_data(struct s_client *cl, struct s_reader *rdr, struct cc_card *card, ECM_REQUEST *cur_er) {
|
---|
935 | if (rdr->audisabled || !cc_UA_valid(card->hexserial))
|
---|
936 | return;
|
---|
937 |
|
---|
938 | struct cc_data *cc = cl->cc;
|
---|
939 | cc->last_emm_card = card;
|
---|
940 |
|
---|
941 | cc_UA_cccam2oscam(card->hexserial, rdr->hexserial, rdr->caid);
|
---|
942 |
|
---|
943 | cs_debug_mask(D_EMM,
|
---|
944 | "%s au info: caid %04X UA: %s",
|
---|
945 | getprefix(), card->caid, cs_hexdump(0,
|
---|
946 | rdr->hexserial, 8));
|
---|
947 |
|
---|
948 | rdr->nprov = 0;
|
---|
949 | LL_ITER *it2 = ll_iter_create(card->providers);
|
---|
950 | struct cc_provider *provider;
|
---|
951 | int p = 0;
|
---|
952 | while ((provider = ll_iter_next(it2))) {
|
---|
953 | if (!cur_er || provider->prov == cur_er->prid || !provider->prov || !cur_er->prid) {
|
---|
954 | rdr->prid[p][0] = provider->prov >> 24;
|
---|
955 | rdr->prid[p][1] = provider->prov >> 16;
|
---|
956 | rdr->prid[p][2] = provider->prov >> 8;
|
---|
957 | rdr->prid[p][3] = provider->prov & 0xFF;
|
---|
958 | cc_SA_cccam2oscam(provider->sa, rdr->sa[p]);
|
---|
959 |
|
---|
960 | cs_debug_mask(D_EMM, "%s au info: provider: %06lX:%02X%02X%02X%02X", getprefix(),
|
---|
961 | provider->prov,
|
---|
962 | provider->sa[0], provider->sa[1], provider->sa[2], provider->sa[3]);
|
---|
963 |
|
---|
964 | p++;
|
---|
965 | rdr->nprov = p;
|
---|
966 | if (p >= CS_MAXPROV) break;
|
---|
967 | }
|
---|
968 | }
|
---|
969 | ll_iter_release(it2);
|
---|
970 |
|
---|
971 | if (!rdr->nprov) { //No Providers? Add null-provider:
|
---|
972 | memset(rdr->prid[0], 0, sizeof(rdr->prid[0]));
|
---|
973 | rdr->nprov = 1;
|
---|
974 | }
|
---|
975 |
|
---|
976 | rdr->caid = card->caid;
|
---|
977 | if (cur_er)
|
---|
978 | rdr->auprovid = cur_er->prid;
|
---|
979 | }
|
---|
980 |
|
---|
981 | int same_first_node(struct cc_card *card1, struct cc_card *card2) {
|
---|
982 | uint8 * node1 = ll_has_elements(card1->remote_nodes);
|
---|
983 | uint8 * node2 = ll_has_elements(card2->remote_nodes);
|
---|
984 |
|
---|
985 | if (!node1 && !node2) return 1; //both NULL, same!
|
---|
986 |
|
---|
987 | if (!node1 || !node2) return 0; //one NULL, not same!
|
---|
988 |
|
---|
989 | return !memcmp(node1, node2, 8); //same?
|
---|
990 | }
|
---|
991 |
|
---|
992 | int same_card2(struct cc_card *card1, struct cc_card *card2) {
|
---|
993 | return (card1->caid == card2->caid &&
|
---|
994 | card1->card_type == card2->card_type &&
|
---|
995 | card1->sidtab == card2->sidtab &&
|
---|
996 | !memcmp(card1->hexserial, card2->hexserial, sizeof(card1->hexserial)));
|
---|
997 | }
|
---|
998 |
|
---|
999 | int same_card(struct cc_card *card1, struct cc_card *card2) {
|
---|
1000 | return (card1->remote_id == card2->remote_id &&
|
---|
1001 | same_card2(card1, card2) &&
|
---|
1002 | same_first_node(card1, card2));
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | /**
|
---|
1006 | * reader
|
---|
1007 | * sends a ecm request to the connected CCCam Server
|
---|
1008 | */
|
---|
1009 | int cc_send_ecm(struct s_client *cl, ECM_REQUEST *er, uchar *buf) {
|
---|
1010 | struct s_reader *rdr = cl->reader;
|
---|
1011 |
|
---|
1012 | //cs_debug_mask(D_TRACE, "%s cc_send_ecm", getprefix());
|
---|
1013 | if (!rdr->tcp_connected)
|
---|
1014 | cc_cli_connect(cl);
|
---|
1015 |
|
---|
1016 | int n, h = -1;
|
---|
1017 | struct cc_data *cc = cl->cc;
|
---|
1018 | struct cc_card *card = NULL;
|
---|
1019 | LL_ITER *it;
|
---|
1020 | ECM_REQUEST *cur_er;
|
---|
1021 | struct timeb cur_time;
|
---|
1022 | cs_ftime(&cur_time);
|
---|
1023 |
|
---|
1024 | if (!cc || (cl->pfd < 1) || !rdr->tcp_connected) {
|
---|
1025 | if (er) {
|
---|
1026 | er->rc = E_RDR_NOTFOUND;
|
---|
1027 | er->rcEx = E2_CCCAM_NOCARD;
|
---|
1028 | cs_debug_mask(D_READER, "%s server not init! ccinit=%d pfd=%d",
|
---|
1029 | rdr->label, cc ? 1 : 0, cl->pfd);
|
---|
1030 | write_ecm_answer(rdr, er);
|
---|
1031 | }
|
---|
1032 | //cc_cli_close(cl);
|
---|
1033 | return 0;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | if (rdr->tcp_connected != 2) {
|
---|
1037 | cs_debug_mask(D_READER, "%s Waiting for CARDS", getprefix());
|
---|
1038 | return 0;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | //No Card? Waiting for shares
|
---|
1042 | if (!ll_has_elements(cc->cards)) {
|
---|
1043 | rdr->fd_error++;
|
---|
1044 | cs_debug_mask(D_READER, "%s NO CARDS!", getprefix());
|
---|
1045 | return 0;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | cc->just_logged_in = 0;
|
---|
1049 |
|
---|
1050 | if (!cc->extended_mode) {
|
---|
1051 | //Without extended mode, only one ecm at a time could be send
|
---|
1052 | //this is a limitation of "O" CCCam
|
---|
1053 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
1054 | cs_debug_mask(D_READER,
|
---|
1055 | "%s ecm trylock: ecm busy, retrying later after msg-receive",
|
---|
1056 | getprefix());
|
---|
1057 |
|
---|
1058 | struct timeb timeout;
|
---|
1059 | timeout = cc->ecm_time;
|
---|
1060 | unsigned int tt = cfg.ctimeout * 4;
|
---|
1061 | timeout.time += tt / 1000;
|
---|
1062 | timeout.millitm += tt % 1000;
|
---|
1063 |
|
---|
1064 | if (comp_timeb(&cur_time, &timeout) < 0) { //TODO: Configuration?
|
---|
1065 | return 0; //pending send...
|
---|
1066 | } else {
|
---|
1067 | cs_debug_mask(D_READER,
|
---|
1068 | "%s unlocked-cycleconnection! timeout %ds",
|
---|
1069 | getprefix(), tt / 1000);
|
---|
1070 | //cc_cycle_connection();
|
---|
1071 | cc_cli_close(cl, TRUE);
|
---|
1072 | return 0;
|
---|
1073 | }
|
---|
1074 | }
|
---|
1075 | cs_debug_mask(D_READER, "cccam: ecm trylock: got lock");
|
---|
1076 | }
|
---|
1077 | do {
|
---|
1078 | cc->ecm_time = cur_time;
|
---|
1079 | rdr->available = cc->extended_mode;
|
---|
1080 |
|
---|
1081 | //Search next ECM to send:
|
---|
1082 | if ((n = cc_get_nxt_ecm(cl)) < 0) {
|
---|
1083 | if (!cc->extended_mode) {
|
---|
1084 | rdr->available = 1;
|
---|
1085 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1086 | }
|
---|
1087 | cs_debug_mask(D_READER, "%s no ecm pending!", getprefix());
|
---|
1088 | if (!cc_send_pending_emms(cl))
|
---|
1089 | send_cmd05_answer(cl);
|
---|
1090 | return 0; // no queued ecms
|
---|
1091 | }
|
---|
1092 | cur_er = &cl->ecmtask[n];
|
---|
1093 | cur_er->rc = 101; //mark ECM as already send
|
---|
1094 | cs_debug_mask(D_READER, "cccam: ecm-task %d", cur_er->idx);
|
---|
1095 |
|
---|
1096 | if (buf)
|
---|
1097 | memcpy(buf, cur_er->ecm, cur_er->l);
|
---|
1098 |
|
---|
1099 | struct cc_srvid cur_srvid;
|
---|
1100 | cur_srvid.sid = cur_er->srvid;
|
---|
1101 | cur_srvid.ecmlen = cur_er->l;
|
---|
1102 |
|
---|
1103 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1104 | //forward_origin:
|
---|
1105 | if (cfg.cc_forward_origin_card && cur_er->origin_reader == rdr && cur_er->origin_card) {
|
---|
1106 | it = ll_iter_create(cc->cards);
|
---|
1107 | struct cc_card *ncard;
|
---|
1108 | while ((ncard = ll_iter_next(it))) {
|
---|
1109 | if (ncard == cur_er->origin_card) { //Search the origin card
|
---|
1110 | card = ncard; //found it, use it!
|
---|
1111 | break;
|
---|
1112 | }
|
---|
1113 | }
|
---|
1114 | ll_iter_release(it);
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | if (!card) {
|
---|
1118 | it = ll_iter_create(cc->cards);
|
---|
1119 | struct cc_card *ncard, *xcard = NULL;
|
---|
1120 | while ((ncard = ll_iter_next(it))) {
|
---|
1121 | if (ncard->caid == cur_er->caid) { // caid matches
|
---|
1122 | if (is_sid_blocked(ncard, &cur_srvid))
|
---|
1123 | continue;
|
---|
1124 |
|
---|
1125 | if (!xcard || ncard->hop < xcard->hop)
|
---|
1126 | xcard = ncard; //remember card (D+ / 1810 fix) if request has no provider, but card has
|
---|
1127 |
|
---|
1128 | if (!cur_er->prid && !ll_count(ncard->providers)) { //card has no providers:
|
---|
1129 | if (h < 0 || ncard->hop < h || (ncard->hop == h
|
---|
1130 | && cc_UA_valid(ncard->hexserial))) {
|
---|
1131 | // ncard is closer
|
---|
1132 | card = ncard;
|
---|
1133 | h = ncard->hop; // ncard has been matched
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | }
|
---|
1137 | else { //card has providers
|
---|
1138 | LL_ITER *it2 = ll_iter_create(ncard->providers);
|
---|
1139 | struct cc_provider *provider;
|
---|
1140 | while ((provider = ll_iter_next(it2))) {
|
---|
1141 | if (provider->prov == cur_er->prid) { // provid matches
|
---|
1142 | if (h < 0 || ncard->hop < h || (ncard->hop == h
|
---|
1143 | && cc_UA_valid(ncard->hexserial))) {
|
---|
1144 | // ncard is closer
|
---|
1145 | card = ncard;
|
---|
1146 | h = ncard->hop; // ncard has been matched
|
---|
1147 | }
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 | ll_iter_release(it2);
|
---|
1151 | }
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 | ll_iter_release(it);
|
---|
1155 | if (!card)
|
---|
1156 | card = xcard; //if request has no provider and we have no card, we try this card
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | if (card) {
|
---|
1160 | card->time = time((time_t) 0);
|
---|
1161 | uint8 ecmbuf[255+13];
|
---|
1162 | memset(ecmbuf, 0, 255+13);
|
---|
1163 |
|
---|
1164 | // build ecm message
|
---|
1165 | ecmbuf[0] = card->caid >> 8;
|
---|
1166 | ecmbuf[1] = card->caid & 0xff;
|
---|
1167 | ecmbuf[2] = cur_er->prid >> 24;
|
---|
1168 | ecmbuf[3] = cur_er->prid >> 16;
|
---|
1169 | ecmbuf[4] = cur_er->prid >> 8;
|
---|
1170 | ecmbuf[5] = cur_er->prid & 0xff;
|
---|
1171 | ecmbuf[6] = card->id >> 24;
|
---|
1172 | ecmbuf[7] = card->id >> 16;
|
---|
1173 | ecmbuf[8] = card->id >> 8;
|
---|
1174 | ecmbuf[9] = card->id & 0xff;
|
---|
1175 | ecmbuf[10] = cur_er->srvid >> 8;
|
---|
1176 | ecmbuf[11] = cur_er->srvid & 0xff;
|
---|
1177 | ecmbuf[12] = cur_er->l & 0xff;
|
---|
1178 | memcpy(ecmbuf + 13, cur_er->ecm, cur_er->l);
|
---|
1179 |
|
---|
1180 | uint8 send_idx = 1;
|
---|
1181 | if (cc->extended_mode) {
|
---|
1182 | cc->server_ecm_idx++;
|
---|
1183 | if (cc->server_ecm_idx >= 256)
|
---|
1184 | cc->server_ecm_idx = 1;
|
---|
1185 | cc->g_flag = cc->server_ecm_idx; //Flag is used as index!
|
---|
1186 | send_idx = cc->g_flag;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | add_extended_ecm_idx(cl, send_idx, cur_er->idx, card, cur_srvid);
|
---|
1190 |
|
---|
1191 | rdr->cc_currenthops = card->hop;
|
---|
1192 |
|
---|
1193 | cs_debug_mask(D_READER,
|
---|
1194 | "%s sending ecm for sid %04X(%d) to card %08x, hop %d, ecmtask %d",
|
---|
1195 | getprefix(), cur_er->srvid, cur_er->l, card->id, card->hop,
|
---|
1196 | cur_er->idx);
|
---|
1197 | cc_cmd_send(cl, ecmbuf, cur_er->l + 13, MSG_CW_ECM); // send ecm
|
---|
1198 |
|
---|
1199 | //For EMM
|
---|
1200 | set_au_data(cl, rdr, card, cur_er);
|
---|
1201 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1202 |
|
---|
1203 | if (cc->extended_mode)
|
---|
1204 | continue; //process next pending ecm!
|
---|
1205 | return 0;
|
---|
1206 | } else {
|
---|
1207 | //When connecting, it could happen than ecm requests come before all cards are received.
|
---|
1208 | //So if the last Message was a MSG_NEW_CARD, this "card receiving" is not already done
|
---|
1209 | //if this happens, we do not autoblock it and do not set rc status
|
---|
1210 | //So fallback could resolve it
|
---|
1211 | if (cc->last_msg != MSG_NEW_CARD && cc->last_msg != MSG_NEW_CARD_SIDINFO && !cc->just_logged_in) {
|
---|
1212 | cs_debug_mask(D_READER, "%s no suitable card on server", getprefix());
|
---|
1213 |
|
---|
1214 | cur_er->rc = E_RDR_NOTFOUND;
|
---|
1215 | cur_er->rcEx = E2_CCCAM_NOCARD;
|
---|
1216 | write_ecm_answer(rdr, cur_er);
|
---|
1217 | //cur_er->rc = 1;
|
---|
1218 | //cur_er->rcEx = 0;
|
---|
1219 | //cs_sleepms(300);
|
---|
1220 | rdr->last_s = rdr->last_g;
|
---|
1221 |
|
---|
1222 | //reopen all blocked sids for this srvid:
|
---|
1223 | it = ll_iter_create(cc->cards);
|
---|
1224 | while ((card = ll_iter_next(it))) {
|
---|
1225 | if (card->caid == cur_er->caid) { // caid matches
|
---|
1226 | LL_ITER *it2 = ll_iter_create(card->badsids);
|
---|
1227 | struct cc_srvid *srvid;
|
---|
1228 | while ((srvid = ll_iter_next(it2)))
|
---|
1229 | if (srvid->ecmlen > 0 && sid_eq(srvid, &cur_srvid)) //ecmlen==0: From remote peer, so do not remove
|
---|
1230 | ll_iter_remove_data(it2);
|
---|
1231 | ll_iter_release(it2);
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 | ll_iter_release(it);
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1238 |
|
---|
1239 | //process next pending ecm!
|
---|
1240 | } while (cc->extended_mode);
|
---|
1241 |
|
---|
1242 | if (!cc->extended_mode) {
|
---|
1243 | rdr->available = 1;
|
---|
1244 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | return -1;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | /*
|
---|
1251 | int cc_abort_user_ecms(){
|
---|
1252 | int n, i;
|
---|
1253 | time_t t;//, tls;
|
---|
1254 | struct cc_data *cc = rdr->cc;
|
---|
1255 |
|
---|
1256 | t=time((time_t *)0);
|
---|
1257 | for (i=1,n=1; i<CS_MAXPENDING; i++)
|
---|
1258 | {
|
---|
1259 | if ((t-cl->ecmtask[i].tps.time > ((cfg.ctimeout + 500) / 1000) + 1) &&
|
---|
1260 | (cl->ecmtask[i].rc>=10)) // drop timeouts
|
---|
1261 | {
|
---|
1262 | cl->ecmtask[i].rc=0;
|
---|
1263 | }
|
---|
1264 | int td=abs(1000*(ecmtask[i].tps.time-cc->found->tps.time)+ecmtask[i].tps.millitm-cc->found->tps.millitm);
|
---|
1265 | if (ecmtask[i].rc>=10 && ecmtask[i].cidx==cc->found->cidx && &ecmtask[i]!=cc->found){
|
---|
1266 | cs_log("aborting idx:%d caid:%04x client:%d timedelta:%d",ecmtask[i].idx,ecmtask[i].caid,ecmtask[i].cidx,td);
|
---|
1267 | ecmtask[i].rc=0;
|
---|
1268 | ecmtask[i].rcEx=7;
|
---|
1269 | write_ecm_answer(rdr, fd_c2m, &ecmtask[i]);
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 | return n;
|
---|
1273 |
|
---|
1274 | }
|
---|
1275 | */
|
---|
1276 |
|
---|
1277 | int cc_send_pending_emms(struct s_client *cl) {
|
---|
1278 | struct s_reader *rdr = cl->reader;
|
---|
1279 | struct cc_data *cc = cl->cc;
|
---|
1280 |
|
---|
1281 | LL_ITER *it = ll_iter_create(cc->pending_emms);
|
---|
1282 | uint8 *emmbuf;
|
---|
1283 | int size = 0;
|
---|
1284 | if ((emmbuf = ll_iter_next(it))) {
|
---|
1285 | if (!cc->extended_mode) {
|
---|
1286 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
1287 | ll_iter_release(it);
|
---|
1288 | return 0; //send later with cc_send_ecm
|
---|
1289 | }
|
---|
1290 | rdr->available = 0;
|
---|
1291 | }
|
---|
1292 | size = emmbuf[11] + 12;
|
---|
1293 |
|
---|
1294 | cc->just_logged_in = 0;
|
---|
1295 | cs_ftime(&cc->ecm_time);
|
---|
1296 |
|
---|
1297 | cs_debug_mask(D_EMM, "%s emm send for card %08X", getprefix(), b2i(4,
|
---|
1298 | emmbuf + 7));
|
---|
1299 |
|
---|
1300 | cc_cmd_send(cl, emmbuf, size, MSG_EMM_ACK); // send emm
|
---|
1301 |
|
---|
1302 | ll_iter_remove_data(it);
|
---|
1303 | }
|
---|
1304 | ll_iter_release(it);
|
---|
1305 |
|
---|
1306 | return size;
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | /**
|
---|
1310 | * READER only:
|
---|
1311 | * find card by hexserial
|
---|
1312 | * */
|
---|
1313 | struct cc_card *get_card_by_hexserial(struct s_client *cl, uint8 *hexserial,
|
---|
1314 | uint16 caid) {
|
---|
1315 | struct cc_data *cc = cl->cc;
|
---|
1316 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
1317 | struct cc_card *card;
|
---|
1318 | while ((card = ll_iter_next(it)))
|
---|
1319 | if (card->caid == caid && memcmp(card->hexserial, hexserial, 8) == 0) { //found it!
|
---|
1320 | break;
|
---|
1321 | }
|
---|
1322 | ll_iter_release(it);
|
---|
1323 | return card;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | /**
|
---|
1327 | * EMM Procession
|
---|
1328 | * Copied from http://85.17.209.13:6100/file/8ec3c0c5d257/systems/cardclient/cccam2.c
|
---|
1329 | * ProcessEmm
|
---|
1330 | * */
|
---|
1331 | int cc_send_emm(EMM_PACKET *ep) {
|
---|
1332 | struct s_client *cl = cur_client();
|
---|
1333 | struct s_reader *rdr = cl->reader;
|
---|
1334 |
|
---|
1335 | if (!rdr->tcp_connected)
|
---|
1336 | cc_cli_connect(cl);
|
---|
1337 |
|
---|
1338 | struct cc_data *cc = cl->cc;
|
---|
1339 |
|
---|
1340 | if (!cc || (cl->pfd < 1) || !rdr->tcp_connected) {
|
---|
1341 | cs_debug_mask(D_READER, "%s server not init! ccinit=%d pfd=%d", getprefix(), cc ? 1 : 0,
|
---|
1342 | cl->pfd);
|
---|
1343 | return 0;
|
---|
1344 | }
|
---|
1345 | if (rdr->audisabled) {
|
---|
1346 | cs_debug_mask(D_READER, "%s au is disabled", getprefix());
|
---|
1347 | return 0;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | ushort caid = b2i(2, ep->caid);
|
---|
1351 |
|
---|
1352 | //Last used card is first card of current_cards:
|
---|
1353 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1354 |
|
---|
1355 | struct cc_card *emm_card = cc->last_emm_card;
|
---|
1356 |
|
---|
1357 | if (!emm_card) {
|
---|
1358 | uint8 hs[8];
|
---|
1359 | cc_UA_oscam2cccam(ep->hexserial, hs, caid);
|
---|
1360 | cs_debug_mask(D_EMM,
|
---|
1361 | "%s au info: searching card for caid %04X oscam-UA: %s",
|
---|
1362 | getprefix(), b2i(2, ep->caid), cs_hexdump(0, ep->hexserial, 8));
|
---|
1363 | cs_debug_mask(D_EMM,
|
---|
1364 | "%s au info: searching card for caid %04X cccam-UA: %s",
|
---|
1365 | getprefix(), b2i(2, ep->caid), cs_hexdump(0, hs, 8));
|
---|
1366 |
|
---|
1367 | emm_card = get_card_by_hexserial(cl, hs, caid);
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | if (!emm_card) { //Card for emm not found!
|
---|
1371 | cs_debug_mask(D_EMM, "%s emm for client %8X not possible, no card found!",
|
---|
1372 | getprefix(), ep->client->thread);
|
---|
1373 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1374 | return 0;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | cs_debug_mask(D_EMM,
|
---|
1378 | "%s emm received for client %8X caid %04X for card %08X",
|
---|
1379 | getprefix(), ep->client->thread, caid, emm_card->id);
|
---|
1380 |
|
---|
1381 | int size = ep->l + 12;
|
---|
1382 | uint8 *emmbuf = cs_malloc(&emmbuf, size, QUITERROR);
|
---|
1383 | memset(emmbuf, 0, size);
|
---|
1384 |
|
---|
1385 | // build ecm message
|
---|
1386 | emmbuf[0] = ep->caid[0];
|
---|
1387 | emmbuf[1] = ep->caid[1];
|
---|
1388 | emmbuf[2] = 0;
|
---|
1389 | emmbuf[3] = ep->provid[0];
|
---|
1390 | emmbuf[4] = ep->provid[1];
|
---|
1391 | emmbuf[5] = ep->provid[2];
|
---|
1392 | emmbuf[6] = ep->provid[3];
|
---|
1393 | emmbuf[7] = emm_card->id >> 24;
|
---|
1394 | emmbuf[8] = emm_card->id >> 16;
|
---|
1395 | emmbuf[9] = emm_card->id >> 8;
|
---|
1396 | emmbuf[10] = emm_card->id & 0xff;
|
---|
1397 | emmbuf[11] = ep->l;
|
---|
1398 | memcpy(emmbuf + 12, ep->emm, ep->l);
|
---|
1399 |
|
---|
1400 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1401 |
|
---|
1402 | ll_append(cc->pending_emms, emmbuf);
|
---|
1403 | cc_send_pending_emms(cl);
|
---|
1404 |
|
---|
1405 | #ifdef WEBIF
|
---|
1406 | rdr->emmwritten[ep->type]++;
|
---|
1407 | #endif
|
---|
1408 | return 1;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | void cc_free_card(struct cc_card *card) {
|
---|
1412 | if (!card)
|
---|
1413 | return;
|
---|
1414 |
|
---|
1415 | ll_destroy_data(card->providers);
|
---|
1416 | ll_destroy_data(card->badsids);
|
---|
1417 | ll_destroy_data(card->goodsids);
|
---|
1418 | ll_destroy_data(card->remote_nodes);
|
---|
1419 |
|
---|
1420 | add_garbage(card);
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | struct cc_card *cc_get_card_by_id(uint32 card_id, LLIST *cards) {
|
---|
1424 | if (!cards)
|
---|
1425 | return NULL;
|
---|
1426 | LL_ITER *it = ll_iter_create(cards);
|
---|
1427 | struct cc_card *card;
|
---|
1428 | while ((card=ll_iter_next(it))) {
|
---|
1429 | if (card->id==card_id) {
|
---|
1430 | break;
|
---|
1431 | }
|
---|
1432 | }
|
---|
1433 | ll_iter_release(it);
|
---|
1434 | return card;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | void cc_free_cardlist(LLIST *card_list, int destroy_list) {
|
---|
1438 | if (card_list) {
|
---|
1439 | LL_ITER *it = ll_iter_create(card_list);
|
---|
1440 | struct cc_card *card;
|
---|
1441 | while ((card = ll_iter_next(it))) {
|
---|
1442 | cc_free_card(card);
|
---|
1443 | ll_iter_remove(it);
|
---|
1444 | }
|
---|
1445 | ll_iter_release(it);
|
---|
1446 | if (destroy_list)
|
---|
1447 | ll_destroy_data(card_list);
|
---|
1448 | }
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | /**
|
---|
1452 | * Clears and free the cc datas
|
---|
1453 | */
|
---|
1454 | void cc_free(struct s_client *cl) {
|
---|
1455 | struct cc_data *cc = cl->cc;
|
---|
1456 | if (!cc) return;
|
---|
1457 |
|
---|
1458 | pthread_mutex_trylock(&cc->cards_busy);
|
---|
1459 | if (!cl->cc) return;
|
---|
1460 | cl->cc=NULL;
|
---|
1461 | cc_free_cardlist(cc->cards, TRUE);
|
---|
1462 | ll_destroy_data(cc->pending_emms);
|
---|
1463 | if (cc->extended_ecm_idx)
|
---|
1464 | free_extended_ecm_idx(cc);
|
---|
1465 | ll_destroy_data(cc->extended_ecm_idx);
|
---|
1466 |
|
---|
1467 | pthread_mutex_unlock(&cc->lockcmd);
|
---|
1468 | pthread_mutex_destroy(&cc->lockcmd);
|
---|
1469 |
|
---|
1470 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
1471 | pthread_mutex_destroy(&cc->ecm_busy);
|
---|
1472 |
|
---|
1473 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1474 | pthread_mutex_destroy(&cc->cards_busy);
|
---|
1475 | add_garbage(cc->prefix);
|
---|
1476 | add_garbage(cc);
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | int is_null_dcw(uint8 *dcw) {
|
---|
1480 | int i;
|
---|
1481 | for (i = 0; i < 15; i++)
|
---|
1482 | if (dcw[i])
|
---|
1483 | return 0;
|
---|
1484 | return 1;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /*int is_dcw_corrupted(uchar *dcw)
|
---|
1488 | {
|
---|
1489 | int i;
|
---|
1490 | int c, cs;
|
---|
1491 |
|
---|
1492 | for (i=0; i<16; i+=4)
|
---|
1493 | {
|
---|
1494 | c = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1495 | cs = dcw[i+3];
|
---|
1496 | if (cs!=c) return (1);
|
---|
1497 | }
|
---|
1498 | return 0;
|
---|
1499 | }
|
---|
1500 | */
|
---|
1501 |
|
---|
1502 | int check_extended_mode(struct s_client *cl, char *msg) {
|
---|
1503 | //Extended mode: if PARTNER String is ending with [PARAM], extended mode is activated
|
---|
1504 | //For future compatibilty the syntax should be compatible with
|
---|
1505 | //[PARAM1,PARAM2...PARAMn]
|
---|
1506 | //
|
---|
1507 | // EXT: Extended ECM Mode: Multiple ECMs could be send and received
|
---|
1508 | // ECMs are numbered, Flag (byte[0] is the index
|
---|
1509 | //
|
---|
1510 | // SID: Exchange of good sids/bad sids activated (like cccam 2.2.x)
|
---|
1511 | // card exchange command MSG_NEW_CARD_SIDINFO instead MSG_NEW_CARD is used
|
---|
1512 | //
|
---|
1513 |
|
---|
1514 | struct cc_data *cc = cl->cc;
|
---|
1515 | int has_param = 0;
|
---|
1516 | char *p = strtok(msg, "[");
|
---|
1517 | while (p) {
|
---|
1518 | p = strtok(NULL, ",]");
|
---|
1519 | if (p && strncmp(p, "EXT", 3) == 0) {
|
---|
1520 | cc->extended_mode = 1;
|
---|
1521 | cs_debug_mask(D_CLIENT, "%s extended ECM mode", getprefix());
|
---|
1522 | has_param = 1;
|
---|
1523 | }
|
---|
1524 | else if (p && strncmp(p, "SID", 3)==0) {
|
---|
1525 | cc->cccam220 = 1;
|
---|
1526 | cs_debug_mask(D_CLIENT, "%s extra SID mode", getprefix());
|
---|
1527 | has_param = 1;
|
---|
1528 | }
|
---|
1529 | }
|
---|
1530 | return has_param;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | void cc_idle() {
|
---|
1534 | struct s_client *cl = cur_client();
|
---|
1535 | struct s_reader *rdr = cl->reader;
|
---|
1536 | struct cc_data *cc = cl->cc;
|
---|
1537 | if (!rdr || !rdr->tcp_connected || !cl || !cc)
|
---|
1538 | return;
|
---|
1539 |
|
---|
1540 | if (rdr->cc_keepalive) {
|
---|
1541 | if (cc->answer_on_keepalive + 55 <= time(NULL)) {
|
---|
1542 | cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE);
|
---|
1543 | cs_debug_mask(D_READER, "cccam: keepalive");
|
---|
1544 | cc->answer_on_keepalive = time(NULL);
|
---|
1545 | }
|
---|
1546 | }
|
---|
1547 | else
|
---|
1548 | {
|
---|
1549 | int rto = abs(rdr->last_s - rdr->last_g);
|
---|
1550 | if (rto >= (rdr->tcp_rto*60))
|
---|
1551 | network_tcp_connection_close(cl, cl->udp_fd);
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | struct cc_card *read_card(uint8 *buf, int ext) {
|
---|
1556 | struct cc_card *card = cs_malloc(&card, sizeof(struct cc_card), QUITERROR);
|
---|
1557 | memset(card, 0, sizeof(struct cc_card));
|
---|
1558 |
|
---|
1559 | int nprov, nassign = 0, nreject = 0, offset = 21;
|
---|
1560 |
|
---|
1561 | card->providers = ll_create();
|
---|
1562 | card->badsids = ll_create();
|
---|
1563 | card->goodsids = ll_create();
|
---|
1564 | card->remote_nodes = ll_create();
|
---|
1565 | card->id = b2i(4, buf);
|
---|
1566 | card->remote_id = b2i(4, buf + 4);
|
---|
1567 | card->caid = b2i(2, buf + 8);
|
---|
1568 | card->hop = buf[10];
|
---|
1569 | card->reshare = buf[11];
|
---|
1570 | memcpy(card->hexserial, buf + 12, 8); //HEXSERIAL!!
|
---|
1571 |
|
---|
1572 | //cs_debug_mask(D_CLIENT, "cccam: card %08x added, caid %04X, hop %d, key %s, count %d",
|
---|
1573 | // card->id, card->caid, card->hop, cs_hexdump(0, card->hexserial, 8),
|
---|
1574 | // ll_count(cc->cards));
|
---|
1575 |
|
---|
1576 | nprov = buf[20];
|
---|
1577 |
|
---|
1578 | if (ext) {
|
---|
1579 | nassign = buf[21];
|
---|
1580 | nreject = buf[22];
|
---|
1581 |
|
---|
1582 | offset += 2;
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | int i;
|
---|
1586 | for (i = 0; i < nprov; i++) { // providers
|
---|
1587 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
1588 | prov->prov = b2i(3, buf + offset);
|
---|
1589 | if (prov->prov == 0xFFFFFF && (card->caid >> 8) == 0x17)
|
---|
1590 | prov->prov = i;
|
---|
1591 | memcpy(prov->sa, buf + offset + 3, 4);
|
---|
1592 | //cs_debug_mask(D_CLIENT, " prov %d, %06x, sa %08x", i + 1, prov->prov, b2i(4,
|
---|
1593 | // prov->sa));
|
---|
1594 |
|
---|
1595 | ll_append(card->providers, prov);
|
---|
1596 | offset+=7;
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | uint8 *ptr = buf + offset;
|
---|
1600 |
|
---|
1601 | if (ext) {
|
---|
1602 | for (i = 0; i < nassign; i++) {
|
---|
1603 | uint16_t sid = b2i(2, ptr);
|
---|
1604 | //cs_debug_mask(D_CLIENT, " assigned sid = %04X, added to good sid list", sid);
|
---|
1605 |
|
---|
1606 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
1607 | srvid->sid = sid;
|
---|
1608 | srvid->ecmlen = 0;
|
---|
1609 | ll_append(card->goodsids, srvid);
|
---|
1610 | ptr+=2;
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | for (i = 0; i < nreject; i++) {
|
---|
1614 | uint16_t sid = b2i(2, ptr);
|
---|
1615 | //cs_debug_mask(D_CLIENT, " rejected sid = %04X, added to sid block list", sid);
|
---|
1616 |
|
---|
1617 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
1618 | srvid->sid = sid;
|
---|
1619 | srvid->ecmlen = 0;
|
---|
1620 | ll_append(card->badsids, srvid);
|
---|
1621 | ptr+=2;
|
---|
1622 | }
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | int remote_count = ptr[0];
|
---|
1626 | ptr++;
|
---|
1627 | for (i = 0; i < remote_count; i++) {
|
---|
1628 | uint8 *remote_node = cs_malloc(&remote_node, 8, QUITERROR);
|
---|
1629 | memcpy(remote_node, ptr, 8);
|
---|
1630 | ll_append(card->remote_nodes, remote_node);
|
---|
1631 | ptr+=8;
|
---|
1632 | }
|
---|
1633 | return card;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | void cc_card_removed(struct s_client *cl, uint32 shareid) {
|
---|
1637 | struct cc_data *cc = cl->cc;
|
---|
1638 | struct cc_card *card;
|
---|
1639 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
1640 |
|
---|
1641 | while ((card = ll_iter_next(it))) {
|
---|
1642 | if (card->id == shareid) {// && card->sub_id == b2i (3, buf + 9)) {
|
---|
1643 | //cs_debug_mask(D_CLIENT, "cccam: card %08x removed, caid %04X, count %d",
|
---|
1644 | // card->id, card->caid, ll_count(cc->cards));
|
---|
1645 | ll_iter_remove(it);
|
---|
1646 | if (cc->last_emm_card == card) {
|
---|
1647 | cc->last_emm_card = NULL;
|
---|
1648 | cs_debug_mask(D_READER, "%s current card %08x removed!",
|
---|
1649 | getprefix(), card->id);
|
---|
1650 | }
|
---|
1651 | free_extended_ecm_idx_by_card(cl, card);
|
---|
1652 | cc_free_card(card);
|
---|
1653 | cc->card_removed_count++;
|
---|
1654 | //break;
|
---|
1655 | }
|
---|
1656 | }
|
---|
1657 | ll_iter_release(it);
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | void move_card_to_end(struct s_client * cl, struct cc_card *card_to_move) {
|
---|
1661 |
|
---|
1662 | struct cc_data *cc = cl->cc;
|
---|
1663 |
|
---|
1664 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
1665 | struct cc_card *card;
|
---|
1666 | while ((card = ll_iter_next(it))) {
|
---|
1667 | if (card == card_to_move) {
|
---|
1668 | ll_iter_remove(it);
|
---|
1669 | break;
|
---|
1670 | }
|
---|
1671 | }
|
---|
1672 | ll_iter_release(it);
|
---|
1673 | if (card) {
|
---|
1674 | cs_debug_mask(D_READER, "%s CMD05: Moving card %08X to the end...", getprefix(), card_to_move->id);
|
---|
1675 | free_extended_ecm_idx_by_card(cl, card);
|
---|
1676 | ll_append(cc->cards, card_to_move);
|
---|
1677 | }
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 |
|
---|
1681 |
|
---|
1682 | /*void fix_dcw(uchar *dcw)
|
---|
1683 | {
|
---|
1684 | int i;
|
---|
1685 | for (i=0; i<16; i+=4)
|
---|
1686 | {
|
---|
1687 | dcw[i+3] = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1688 | }
|
---|
1689 | }*/
|
---|
1690 |
|
---|
1691 | void addParam(char *param, char *value)
|
---|
1692 | {
|
---|
1693 | if (strlen(param) < 4)
|
---|
1694 | strcat(param, value);
|
---|
1695 | else {
|
---|
1696 | strcat(param, ",");
|
---|
1697 | strcat(param, value);
|
---|
1698 | }
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | int cc_parse_msg(struct s_client *cl, uint8 *buf, int l) {
|
---|
1702 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
1703 | uint8 token[256];
|
---|
1704 | int ret = buf[1];
|
---|
1705 | struct cc_data *cc = cl->cc;
|
---|
1706 |
|
---|
1707 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s parse_msg=%d", getprefix(), buf[1]);
|
---|
1708 |
|
---|
1709 | uint8 *data = buf + 4;
|
---|
1710 | memcpy(&cc->receive_buffer, data, l - 4);
|
---|
1711 | cc->last_msg = buf[1];
|
---|
1712 | switch (buf[1]) {
|
---|
1713 | case MSG_CLI_DATA:
|
---|
1714 | cs_debug_mask(D_CLIENT, "cccam: client data ack");
|
---|
1715 | break;
|
---|
1716 | case MSG_SRV_DATA:
|
---|
1717 | l -= 4;
|
---|
1718 | cs_debug_mask(D_READER, "%s MSG_SRV_DATA (payload=%d, hex=%02X)", getprefix(), l, l);
|
---|
1719 | data = cc->receive_buffer;
|
---|
1720 |
|
---|
1721 | if (l == 0x48) { //72 bytes: normal server data
|
---|
1722 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1723 | cc_free_cardlist(cc->cards, FALSE);
|
---|
1724 | cc->last_emm_card = NULL;
|
---|
1725 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1726 |
|
---|
1727 | memcpy(cc->peer_node_id, data, 8);
|
---|
1728 | memcpy(cc->peer_version, data + 8, 8);
|
---|
1729 |
|
---|
1730 | memcpy(cc->cmd0b_aeskey, cc->peer_node_id, 8);
|
---|
1731 | memcpy(cc->cmd0b_aeskey + 8, cc->peer_version, 8);
|
---|
1732 |
|
---|
1733 | strncpy(cc->remote_version, (char*)data+8, sizeof(cc->remote_version)-1);
|
---|
1734 | strncpy(cc->remote_build, (char*)data+40, sizeof(cc->remote_build)-1);
|
---|
1735 |
|
---|
1736 | cs_debug_mask(D_READER, "%s remove server %s running v%s (%s)", getprefix(), cs_hexdump(0,
|
---|
1737 | cc->peer_node_id, 8), cc->remote_version, cc->remote_build);
|
---|
1738 |
|
---|
1739 | if (!cc->is_oscam_cccam) {//Allready discovered oscam-cccam:
|
---|
1740 | uint16 sum = 0x1234;
|
---|
1741 | uint16 recv_sum = (cc->peer_node_id[6] << 8)
|
---|
1742 | | cc->peer_node_id[7];
|
---|
1743 | int i;
|
---|
1744 | for (i = 0; i < 6; i++) {
|
---|
1745 | sum += cc->peer_node_id[i];
|
---|
1746 | }
|
---|
1747 | //Create special data to detect oscam-cccam:
|
---|
1748 | cc->is_oscam_cccam = sum == recv_sum;
|
---|
1749 | }
|
---|
1750 | //Trick: when discovered partner is an Oscam Client, then we send him our version string:
|
---|
1751 | if (cc->is_oscam_cccam) {
|
---|
1752 | snprintf((char *)token, sizeof(token),
|
---|
1753 | "PARTNER: OSCam v%s, build #%s (%s) [EXT,SID]", CS_VERSION,
|
---|
1754 | CS_SVN_VERSION, CS_OSTYPE);
|
---|
1755 | cc_cmd_send(cl, token, strlen((char *)token) + 1, MSG_CW_NOK1);
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1759 | //
|
---|
1760 | //Keyoffset is payload-size:
|
---|
1761 | //
|
---|
1762 | } else if (l >= 0x00 && l <= 0x0F) {
|
---|
1763 | cc->cmd05_offset = l;
|
---|
1764 | //
|
---|
1765 | //16..43 bytes: RC4 encryption:
|
---|
1766 | //
|
---|
1767 | } else if ((l >= 0x10 && l <= 0x1f) || (l >= 0x24 && l <= 0x2b)) {
|
---|
1768 | cc_init_crypt(&cc->cmd05_cryptkey, data, l);
|
---|
1769 | cc->cmd05_mode = MODE_RC4_CRYPT;
|
---|
1770 | //
|
---|
1771 | //32 bytes: set AES128 key for CMD_05, Key=16 bytes offset keyoffset
|
---|
1772 | //
|
---|
1773 | } else if (l == 0x20) {
|
---|
1774 | memcpy(cc->cmd05_aeskey, data + cc->cmd05_offset, 16);
|
---|
1775 | cc->cmd05_mode = MODE_AES;
|
---|
1776 | //
|
---|
1777 | //33 bytes: xor-algo mit payload-bytes, offset keyoffset
|
---|
1778 | //
|
---|
1779 | } else if (l == 0x21) {
|
---|
1780 | cc_init_crypt(&cc->cmd05_cryptkey, data + cc->cmd05_offset, l);
|
---|
1781 | cc->cmd05_mode = MODE_CC_CRYPT;
|
---|
1782 | //
|
---|
1783 | //34 bytes: cmd_05 plain back
|
---|
1784 | //
|
---|
1785 | } else if (l == 0x22) {
|
---|
1786 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1787 | //
|
---|
1788 | //35 bytes: Unknown!! 2 256 byte keys exchange
|
---|
1789 | //
|
---|
1790 | } else if (l == 0x23) {
|
---|
1791 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
1792 | //cycle_connection(); //Absolute unknown handling!
|
---|
1793 | cc_cli_close(cl, TRUE);
|
---|
1794 | //
|
---|
1795 | //44 bytes: set aes128 key, Key=16 bytes [Offset=len(password)]
|
---|
1796 | //
|
---|
1797 | } else if (l == 0x2c) {
|
---|
1798 | memcpy(cc->cmd05_aeskey, data + strlen(rdr->r_pwd), 16);
|
---|
1799 | cc->cmd05_mode = MODE_AES;
|
---|
1800 | //
|
---|
1801 | //45 bytes: set aes128 key, Key=16 bytes [Offset=len(username)]
|
---|
1802 | //
|
---|
1803 | } else if (l == 0x2d) {
|
---|
1804 | memcpy(cc->cmd05_aeskey, data + strlen(rdr->r_usr), 16);
|
---|
1805 | cc->cmd05_mode = MODE_AES;
|
---|
1806 | //
|
---|
1807 | //Unknown!!
|
---|
1808 | //
|
---|
1809 | } else {
|
---|
1810 | cs_debug_mask(D_READER,
|
---|
1811 | "%s received improper MSG_SRV_DATA! No change to current mode, mode=%d",
|
---|
1812 | getprefix(), cc->cmd05_mode);
|
---|
1813 | break;
|
---|
1814 | }
|
---|
1815 | cs_debug_mask(D_READER, "%s MSG_SRV_DATA MODE=%s, len=%d", getprefix(),
|
---|
1816 | cmd05_mode_name[cc->cmd05_mode], l);
|
---|
1817 |
|
---|
1818 | break;
|
---|
1819 | case MSG_NEW_CARD_SIDINFO:
|
---|
1820 | case MSG_NEW_CARD: {
|
---|
1821 | uint16 caid = b2i(2, buf + 12);
|
---|
1822 | //filter caid==0 and maxhop:
|
---|
1823 | if (!caid || buf[14] >= rdr->cc_maxhop+1)
|
---|
1824 | break;
|
---|
1825 |
|
---|
1826 | //filter mindown:
|
---|
1827 | if (buf[15] < rdr->cc_mindown)
|
---|
1828 | break;
|
---|
1829 |
|
---|
1830 | //caid check
|
---|
1831 | if (!chk_ctab(caid, &rdr->ctab))
|
---|
1832 | break;
|
---|
1833 |
|
---|
1834 | rdr->tcp_connected = 2; //we have card
|
---|
1835 | rdr->card_status = CARD_INSERTED;
|
---|
1836 |
|
---|
1837 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1838 |
|
---|
1839 | struct cc_card *card = read_card(data, buf[1]==MSG_NEW_CARD_SIDINFO);
|
---|
1840 | card->origin_reader = rdr;
|
---|
1841 | card->origin_id = card->id;
|
---|
1842 |
|
---|
1843 | //Check if this card is from us:
|
---|
1844 | LL_ITER *it = ll_iter_create(card->remote_nodes);
|
---|
1845 | uint8 *node_id;
|
---|
1846 | while ((node_id = ll_iter_next(it))) {
|
---|
1847 | if (memcmp(node_id, cc_node_id, sizeof(cc_node_id)) == 0) { //this card is from us!
|
---|
1848 | cs_debug_mask(D_READER, "filtered card because of recursive nodeid: id=%08X, caid=%04X", card->id, card->caid);
|
---|
1849 | cc_free_card(card);
|
---|
1850 | card=NULL;
|
---|
1851 | break;
|
---|
1852 | }
|
---|
1853 | }
|
---|
1854 | ll_iter_release(it);
|
---|
1855 |
|
---|
1856 | //Check Ident filter:
|
---|
1857 | if (card) {
|
---|
1858 | if (!chk_ident(&rdr->ftab, card)) {
|
---|
1859 | cc_free_card(card);
|
---|
1860 | card=NULL;
|
---|
1861 | }
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 | if (card) {
|
---|
1865 | //Check if we already have this card:
|
---|
1866 | it = ll_iter_create(cc->cards);
|
---|
1867 | struct cc_card *old_card;
|
---|
1868 | while ((old_card = ll_iter_next(it))) {
|
---|
1869 | if (old_card->id == card->id || //we aready have this card, delete it
|
---|
1870 | same_card(old_card, card)) {
|
---|
1871 | cc_free_card(card);
|
---|
1872 | card = old_card;
|
---|
1873 | break;
|
---|
1874 | }
|
---|
1875 | }
|
---|
1876 | ll_iter_release(it);
|
---|
1877 |
|
---|
1878 | card->time = time((time_t) 0);
|
---|
1879 | if (!old_card) {
|
---|
1880 | card->card_type = CT_REMOTECARD;
|
---|
1881 | card->hop++; //inkrementing hop
|
---|
1882 | ll_append(cc->cards, card);
|
---|
1883 | set_au_data(cl, rdr, card, NULL);
|
---|
1884 | cc->card_added_count++;
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1889 |
|
---|
1890 | break;
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | case MSG_CARD_REMOVED: {
|
---|
1894 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1895 | cc_card_removed(cl, b2i(4, buf + 4));
|
---|
1896 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1897 | break;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | case MSG_CW_NOK1:
|
---|
1901 | case MSG_CW_NOK2:
|
---|
1902 | if (l > 4) {
|
---|
1903 | //Received NOK with payload:
|
---|
1904 | char *msg = (char*) buf + 4;
|
---|
1905 |
|
---|
1906 | //Check for PARTNER connection:
|
---|
1907 | if (strncmp(msg, "PARTNER:", 8) == 0) {
|
---|
1908 | //When Data starts with "PARTNER:" we have an Oscam-cccam-compatible client/server!
|
---|
1909 |
|
---|
1910 | strncpy(cc->remote_oscam, msg+9, sizeof(cc->remote_oscam)-1);
|
---|
1911 | int has_param = check_extended_mode(cl, msg);
|
---|
1912 | if (!cc->is_oscam_cccam) {
|
---|
1913 | cc->is_oscam_cccam = 1;
|
---|
1914 |
|
---|
1915 | //send params back. At the moment there is only "EXT"
|
---|
1916 | char param[14];
|
---|
1917 | if (!has_param)
|
---|
1918 | param[0] = 0;
|
---|
1919 | else {
|
---|
1920 | cs_strncpy(param, " [", sizeof(param));
|
---|
1921 | if (cc->extended_mode)
|
---|
1922 | addParam(param, "EXT");
|
---|
1923 | if (cc->cccam220)
|
---|
1924 | addParam(param, "SID");
|
---|
1925 | strcat(param, "]");
|
---|
1926 | }
|
---|
1927 |
|
---|
1928 | snprintf((char *)token, sizeof(token),
|
---|
1929 | "PARTNER: OSCam v%s, build #%s (%s)%s",
|
---|
1930 | CS_VERSION, CS_SVN_VERSION, CS_OSTYPE, param);
|
---|
1931 | cc_cmd_send(cl, token, strlen((char *)token) + 1, MSG_CW_NOK1);
|
---|
1932 | }
|
---|
1933 | }
|
---|
1934 | return ret;
|
---|
1935 | }
|
---|
1936 |
|
---|
1937 | if (cl->typ == 'c') //for reader only
|
---|
1938 | return ret;
|
---|
1939 |
|
---|
1940 | cc->recv_ecmtask = -1;
|
---|
1941 |
|
---|
1942 | if (cc->just_logged_in)
|
---|
1943 | return -1; // reader restart needed
|
---|
1944 |
|
---|
1945 | pthread_mutex_lock(&cc->cards_busy);
|
---|
1946 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl,
|
---|
1947 | cc->extended_mode ? cc->g_flag : 1, TRUE);
|
---|
1948 | if (!eei) {
|
---|
1949 | cs_debug_mask(D_READER, "%s received extended ecm NOK id %d but not found!",
|
---|
1950 | getprefix(), cc->g_flag);
|
---|
1951 | }
|
---|
1952 | else
|
---|
1953 | {
|
---|
1954 | ushort ecm_idx = eei->ecm_idx;
|
---|
1955 | cc->recv_ecmtask = ecm_idx;
|
---|
1956 | struct cc_card *card = eei->card;
|
---|
1957 | struct cc_srvid srvid = eei->srvid;
|
---|
1958 | free(eei);
|
---|
1959 |
|
---|
1960 | if (card) {
|
---|
1961 | if (buf[1] == MSG_CW_NOK1) //MSG_CW_NOK1: share no more available
|
---|
1962 | cc_card_removed(cl, card->id);
|
---|
1963 | else if (cc->cmd05NOK) {
|
---|
1964 | move_card_to_end(cl, card);
|
---|
1965 | add_sid_block(cl, card, &srvid);
|
---|
1966 | }
|
---|
1967 | else if (!is_good_sid(card, &srvid)) //MSG_CW_NOK2: can't decode
|
---|
1968 | add_sid_block(cl, card, &srvid);
|
---|
1969 | else
|
---|
1970 | remove_good_sid(card, &srvid);
|
---|
1971 |
|
---|
1972 | if (cfg.cc_forward_origin_card && card->origin_reader == rdr) {
|
---|
1973 | //this card is from us but it can't decode this ecm
|
---|
1974 | //also origin card is only set on cccam clients
|
---|
1975 | //so wie send back the nok to the client
|
---|
1976 | int i = 0;
|
---|
1977 | for (i=0;i<CS_MAXPENDING;i++) {
|
---|
1978 | if (cl->ecmtask[i].idx == ecm_idx) {
|
---|
1979 | cs_debug_mask(D_TRACE, "%s forward card: %s", getprefix(), (buf[1]==MSG_CW_NOK1)?"NOK1":"NOK2");
|
---|
1980 | ECM_REQUEST *er = &cl->ecmtask[i];
|
---|
1981 | er->rc = E_RDR_NOTFOUND;
|
---|
1982 | er->rcEx = (buf[1] == MSG_CW_NOK1)?E2_CCCAM_NOK1:E2_CCCAM_NOK2;
|
---|
1983 | write_ecm_answer(rdr, er);
|
---|
1984 | break;
|
---|
1985 | }
|
---|
1986 | }
|
---|
1987 | }
|
---|
1988 | else {
|
---|
1989 | //retry ecm:
|
---|
1990 | cc_reset_pending(cl, ecm_idx);
|
---|
1991 | }
|
---|
1992 | } else
|
---|
1993 | cs_debug_mask(D_READER, "%S NOK: NO CARD!", getprefix());
|
---|
1994 | }
|
---|
1995 | cc->cmd05NOK = 0;
|
---|
1996 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
1997 |
|
---|
1998 | if (!cc->extended_mode) {
|
---|
1999 | rdr->available = 1;
|
---|
2000 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2001 | }
|
---|
2002 |
|
---|
2003 | cc_send_ecm(cl, NULL, NULL);
|
---|
2004 | break;
|
---|
2005 |
|
---|
2006 | case MSG_CW_ECM:
|
---|
2007 | cc->just_logged_in = 0;
|
---|
2008 | if (cl->typ == 'c') { //SERVER:
|
---|
2009 | ECM_REQUEST *er;
|
---|
2010 |
|
---|
2011 | struct cc_card *server_card = cs_malloc(&server_card, sizeof(struct cc_card), QUITERROR);
|
---|
2012 | memset(server_card, 0, sizeof(struct cc_card));
|
---|
2013 | server_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8
|
---|
2014 | | buf[13];
|
---|
2015 | server_card->caid = b2i(2, data);
|
---|
2016 |
|
---|
2017 | if ((er = get_ecmtask())) {
|
---|
2018 | er->caid = b2i(2, buf + 4);
|
---|
2019 | er->srvid = b2i(2, buf + 14);
|
---|
2020 | er->l = buf[16];
|
---|
2021 | memcpy(er->ecm, buf + 17, er->l);
|
---|
2022 | er->prid = b2i(4, buf + 6);
|
---|
2023 | cc->server_ecm_pending++;
|
---|
2024 | er->idx = ++cc->server_ecm_idx;
|
---|
2025 |
|
---|
2026 | if (cfg.cc_forward_origin_card) { //search my shares for this card:
|
---|
2027 | cs_debug_mask(D_TRACE, "%s forward card: %04X:%04x search share %d", getprefix(), er->caid, er->srvid, server_card->id);
|
---|
2028 | LL_ITER *itr = ll_iter_create(get_and_lock_sharelist());
|
---|
2029 | struct cc_card *card;
|
---|
2030 | struct cc_card *rcard = NULL;
|
---|
2031 | while ((card=ll_iter_next(itr))) {
|
---|
2032 | if (card->id == server_card->id) { //found it
|
---|
2033 | break;
|
---|
2034 | }
|
---|
2035 | }
|
---|
2036 | ll_iter_release(itr);
|
---|
2037 | cs_debug_mask(D_TRACE, "%s forward card: share %d found: %d", getprefix(), server_card->id, card?1:0);
|
---|
2038 | if (card && card->origin_reader) { // found own card, now search reader card:
|
---|
2039 | cs_debug_mask(D_TRACE, "%s forward card: share %d origin reader %s origin id %d", getprefix(), card->id, card->origin_reader->label, card->origin_id);
|
---|
2040 | struct s_reader *rdr = card->origin_reader;
|
---|
2041 | if (card->origin_id && rdr && rdr->client && rdr->client->cc) { //only if we have a origin from a cccam reader
|
---|
2042 | struct cc_data *rcc = rdr->client->cc;
|
---|
2043 |
|
---|
2044 | itr = ll_iter_create(rcc->cards);
|
---|
2045 | while ((rcard=ll_iter_next(itr))) {
|
---|
2046 | if (rcard->id == card->origin_id) //found it!
|
---|
2047 | break;
|
---|
2048 | }
|
---|
2049 | ll_iter_release(itr);
|
---|
2050 | }
|
---|
2051 | else
|
---|
2052 | rcard = card;
|
---|
2053 | er->origin_reader = rdr;
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | er->origin_card = rcard;
|
---|
2057 | if (!rcard) {
|
---|
2058 | cs_debug_mask(D_TRACE, "%s forward card: share %d not found!", getprefix(), server_card->id);
|
---|
2059 | er->rc = E_NOTFOUND;
|
---|
2060 | er->rcEx = E2_CCCAM_NOK1; //share not found!
|
---|
2061 | }
|
---|
2062 | else
|
---|
2063 | cs_debug_mask(D_TRACE, "%s forward card: share %d forwarded to %s origin as id %d", getprefix(),
|
---|
2064 | card->id, card->origin_reader->label, rcard->id);
|
---|
2065 | unlock_sharelist();
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | cs_debug_mask(
|
---|
2069 | D_CLIENT,
|
---|
2070 | "%s ECM request from client: caid %04x srvid %04x(%d) prid %06x",
|
---|
2071 | getprefix(), er->caid, er->srvid, er->l, er->prid);
|
---|
2072 |
|
---|
2073 | struct cc_srvid srvid;
|
---|
2074 | srvid.sid = er->srvid;
|
---|
2075 | srvid.ecmlen = er->l;
|
---|
2076 | add_extended_ecm_idx(cl, cc->extended_mode ? cc->g_flag : 1,
|
---|
2077 | er->idx, server_card, srvid);
|
---|
2078 |
|
---|
2079 | get_cw(cl, er);
|
---|
2080 |
|
---|
2081 | } else {
|
---|
2082 | cs_debug_mask(D_CLIENT, "%s NO ECMTASK!!!!", getprefix());
|
---|
2083 | free(server_card);
|
---|
2084 | }
|
---|
2085 |
|
---|
2086 | } else { //READER:
|
---|
2087 | pthread_mutex_lock(&cc->cards_busy);
|
---|
2088 | cc->recv_ecmtask = -1;
|
---|
2089 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl,
|
---|
2090 | cc->extended_mode ? cc->g_flag : 1, TRUE);
|
---|
2091 | if (!eei) {
|
---|
2092 | cs_debug_mask(D_READER, "%s received extended ecm id %d but not found!",
|
---|
2093 | getprefix(), cc->g_flag);
|
---|
2094 | }
|
---|
2095 | else
|
---|
2096 | {
|
---|
2097 | ushort ecm_idx = eei->ecm_idx;
|
---|
2098 | cc->recv_ecmtask = ecm_idx;
|
---|
2099 | struct cc_card *card = eei->card;
|
---|
2100 | struct cc_srvid srvid = eei->srvid;
|
---|
2101 | free(eei);
|
---|
2102 |
|
---|
2103 | if (card) {
|
---|
2104 |
|
---|
2105 | if (!cc->extended_mode) {
|
---|
2106 | cc_cw_crypt(cl, buf + 4, card->id);
|
---|
2107 | cc_crypt_cmd0c(cl, buf + 4, 16);
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | memcpy(cc->dcw, buf + 4, 16);
|
---|
2111 | //fix_dcw(cc->dcw);
|
---|
2112 | if (!cc->extended_mode)
|
---|
2113 | cc_crypt(&cc->block[DECRYPT], buf + 4, l - 4, ENCRYPT); // additional crypto step
|
---|
2114 |
|
---|
2115 | if (is_null_dcw(cc->dcw)) {
|
---|
2116 | cs_debug_mask(D_READER, "%s null dcw received! sid=%04X(%d)", getprefix(),
|
---|
2117 | srvid.sid, srvid.ecmlen);
|
---|
2118 | add_sid_block(cl, card, &srvid);
|
---|
2119 | //ecm retry:
|
---|
2120 | cc_reset_pending(cl, ecm_idx);
|
---|
2121 | buf[1] = MSG_CW_NOK2; //So it's really handled like a nok!
|
---|
2122 | } else {
|
---|
2123 | cs_debug_mask(D_READER, "%s cws: %d %s", getprefix(),
|
---|
2124 | ecm_idx, cs_hexdump(0, cc->dcw, 16));
|
---|
2125 | add_good_sid(cl, card, &srvid);
|
---|
2126 |
|
---|
2127 | //check response time, if > fallbacktime, switch cards!
|
---|
2128 | struct timeb tpe;
|
---|
2129 | cs_ftime(&tpe);
|
---|
2130 | ulong cwlastresptime = 1000*(tpe.time-cc->ecm_time.time)+tpe.millitm-cc->ecm_time.millitm;
|
---|
2131 | if (cwlastresptime > cfg.ftimeout && !cc->extended_mode) {
|
---|
2132 | cs_debug_mask(D_READER, "%s card %04X is too slow, moving to the end...", getprefix(), card->id);
|
---|
2133 | move_card_to_end(cl, card);
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | }
|
---|
2137 | } else {
|
---|
2138 | cs_debug_mask(D_READER,
|
---|
2139 | "%s warning: ECM-CWS respond by CCCam server without current card!",
|
---|
2140 | getprefix());
|
---|
2141 | }
|
---|
2142 | }
|
---|
2143 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
2144 |
|
---|
2145 | if (!cc->extended_mode) {
|
---|
2146 | rdr->available = 1;
|
---|
2147 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2148 | }
|
---|
2149 |
|
---|
2150 | //cc_abort_user_ecms();
|
---|
2151 |
|
---|
2152 | cc_send_ecm(cl, NULL, NULL);
|
---|
2153 |
|
---|
2154 | if (cc->max_ecms)
|
---|
2155 | cc->ecm_counter++;
|
---|
2156 | }
|
---|
2157 | break;
|
---|
2158 |
|
---|
2159 | case MSG_KEEPALIVE:
|
---|
2160 | if (cl->typ != 'c') {
|
---|
2161 | cs_debug_mask(D_READER, "cccam: keepalive ack");
|
---|
2162 | } else {
|
---|
2163 | //Checking if last answer is one minute ago:
|
---|
2164 | if (cc->just_logged_in || cc->answer_on_keepalive + 55 <= time(NULL)) {
|
---|
2165 | cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE);
|
---|
2166 | cs_debug_mask(D_CLIENT, "cccam: keepalive");
|
---|
2167 | cc->answer_on_keepalive = time(NULL);
|
---|
2168 | }
|
---|
2169 | }
|
---|
2170 | cc->just_logged_in = 0;
|
---|
2171 | break;
|
---|
2172 |
|
---|
2173 | case MSG_CMD_05:
|
---|
2174 | if (cl->typ != 'c') {
|
---|
2175 | cc->just_logged_in = 0;
|
---|
2176 | l = l - 4;//Header Length=4 Byte
|
---|
2177 |
|
---|
2178 | cs_debug_mask(D_READER, "%s MSG_CMD_05 recvd, payload length=%d mode=%d",
|
---|
2179 | getprefix(), l, cc->cmd05_mode);
|
---|
2180 | cc->cmd05_active = 1;
|
---|
2181 | cc->cmd05_data_len = l;
|
---|
2182 | memcpy(&cc->cmd05_data, buf + 4, l);
|
---|
2183 | if (rdr->available && ll_has_elements(cc->cards))
|
---|
2184 | send_cmd05_answer(cl);
|
---|
2185 | }
|
---|
2186 | break;
|
---|
2187 | case MSG_CMD_0B: {
|
---|
2188 | // by Project:Keynation
|
---|
2189 | cs_debug_mask(D_READER, "%s MSG_CMD_0B received (payload=%d)!",
|
---|
2190 | getprefix(), l - 4);
|
---|
2191 |
|
---|
2192 | AES_KEY key;
|
---|
2193 | uint8 aeskey[16];
|
---|
2194 | uint8 out[16];
|
---|
2195 |
|
---|
2196 | memcpy(aeskey, cc->cmd0b_aeskey, 16);
|
---|
2197 | memset(&key, 0, sizeof(key));
|
---|
2198 |
|
---|
2199 | //cs_ddump_mask(D_READER, aeskey, 16, "%s CMD_0B AES key:", getprefix());
|
---|
2200 | //cs_ddump_mask(D_READER, data, 16, "%s CMD_0B received data:", getprefix());
|
---|
2201 |
|
---|
2202 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
2203 | AES_encrypt((unsigned char *) data, (unsigned char *) &out, &key);
|
---|
2204 |
|
---|
2205 | cs_debug_mask(D_TRACE, "%s sending CMD_0B! ", getprefix());
|
---|
2206 | //cs_ddump_mask(D_READER, out, 16, "%s CMD_0B out:", getprefix());
|
---|
2207 | cc_cmd_send(cl, out, 16, MSG_CMD_0B);
|
---|
2208 |
|
---|
2209 | break;
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 | case MSG_CMD_0C: { //New CCCAM 2.2.0 Server/Client fake check!
|
---|
2213 | int len = l-4;
|
---|
2214 |
|
---|
2215 | if (cl->typ == 'c') { //Only im comming from "client"
|
---|
2216 | cs_debug_mask(D_CLIENT, "%s MSG_CMD_0C received (payload=%d)!", getprefix(), len);
|
---|
2217 |
|
---|
2218 | uint8 bytes[0x20];
|
---|
2219 | if (len < 0x20) //if less then 0x20 bytes, clear others:
|
---|
2220 | memset(data+len, 0, 0x20-len);
|
---|
2221 |
|
---|
2222 | //change first 0x10 bytes to the second:
|
---|
2223 | memcpy(bytes, data+0x10, 0x10);
|
---|
2224 | memcpy(bytes+0x10, data, 0x10);
|
---|
2225 |
|
---|
2226 | //xor data:
|
---|
2227 | int i;
|
---|
2228 | for (i=0;i<0x20;i++)
|
---|
2229 | bytes[i] ^= (data[i] & 0x7F);
|
---|
2230 |
|
---|
2231 | //key is now the 16bit hash of md5:
|
---|
2232 | uint8 md5hash[0x10];
|
---|
2233 | MD5(data, 0x20, md5hash);
|
---|
2234 | memcpy(bytes, md5hash, 0x10);
|
---|
2235 |
|
---|
2236 | cs_debug_mask(D_CLIENT, "%s sending CMD_0C! ", getprefix());
|
---|
2237 | //cs_ddump_mask(D_CLIENT, bytes, 0x20, "%s CMD_0C out:", getprefix());
|
---|
2238 | cc_cmd_send(cl, bytes, 0x20, MSG_CMD_0C);
|
---|
2239 | }
|
---|
2240 | else //reader
|
---|
2241 | {
|
---|
2242 | // by Project:Keynation + Oscam team
|
---|
2243 | cc_crypt_cmd0c(cl, data, len);
|
---|
2244 |
|
---|
2245 | uint8 CMD_0x0C_Command = data[0];
|
---|
2246 |
|
---|
2247 | switch (CMD_0x0C_Command) {
|
---|
2248 |
|
---|
2249 | case 0 : { //RC6
|
---|
2250 | cc->cmd0c_mode = MODE_CMD_0x0C_RC6;
|
---|
2251 | break;
|
---|
2252 | }
|
---|
2253 |
|
---|
2254 | case 1: { //RC4
|
---|
2255 | cc->cmd0c_mode = MODE_CMD_0x0C_RC4;
|
---|
2256 | break;
|
---|
2257 | }
|
---|
2258 |
|
---|
2259 | case 2: { //CC_CRYPT
|
---|
2260 | cc->cmd0c_mode = MODE_CMD_0x0C_CC_CRYPT;
|
---|
2261 | break;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | case 3: { //AES
|
---|
2265 | cc->cmd0c_mode = MODE_CMD_0x0C_AES;
|
---|
2266 | break;
|
---|
2267 | }
|
---|
2268 |
|
---|
2269 | case 4 : { //IDEA
|
---|
2270 | cc->cmd0c_mode = MODE_CMD_0x0C_IDEA;
|
---|
2271 | break;
|
---|
2272 | }
|
---|
2273 |
|
---|
2274 | default: {
|
---|
2275 | cc->cmd0c_mode = MODE_CMD_0x0C_NONE;
|
---|
2276 | }
|
---|
2277 | }
|
---|
2278 |
|
---|
2279 | set_cmd0c_cryptkey(cl, data, len);
|
---|
2280 |
|
---|
2281 | cs_debug_mask(D_READER, "%s received MSG_CMD_0C from server! CMD_0x0C_CMD=%d, MODE=%s",
|
---|
2282 | getprefix(), CMD_0x0C_Command, cmd0c_mode_name[cc->cmd0c_mode]);
|
---|
2283 | }
|
---|
2284 | break;
|
---|
2285 | }
|
---|
2286 |
|
---|
2287 | case MSG_CMD_0D: { //key update for the active cmd0x0c algo
|
---|
2288 | int len = l-4;
|
---|
2289 | if (cc->cmd0c_mode == MODE_CMD_0x0C_NONE)
|
---|
2290 | break;
|
---|
2291 |
|
---|
2292 | cc_crypt_cmd0c(cl, data, len);
|
---|
2293 | set_cmd0c_cryptkey(cl, data, len);
|
---|
2294 |
|
---|
2295 | cs_debug_mask(D_READER, "%s received MSG_CMD_0D from server! MODE=%s",
|
---|
2296 | getprefix(), cmd0c_mode_name[cc->cmd0c_mode]);
|
---|
2297 | break;
|
---|
2298 | }
|
---|
2299 |
|
---|
2300 | case MSG_CMD_0E: {
|
---|
2301 | cs_debug_mask(D_READER, "cccam 2.2.x commands not implemented: 0x%02X", buf[1]);
|
---|
2302 | //Unkwon commands...need workout algo
|
---|
2303 | if (cl->typ == 'c') //client connection
|
---|
2304 | {
|
---|
2305 | //switching to an oder version and then disconnect...
|
---|
2306 | cs_strncpy(cfg.cc_version, version[0], sizeof(cfg.cc_version));
|
---|
2307 | ret = -1;
|
---|
2308 | }
|
---|
2309 | else //reader connection
|
---|
2310 | {
|
---|
2311 | cs_strncpy(cl->reader->cc_version, version[0], sizeof(cl->reader->cc_version));
|
---|
2312 | cs_strncpy(cl->reader->cc_build, build[0], sizeof(cl->reader->cc_build));
|
---|
2313 | cc_cli_close(cl, TRUE);
|
---|
2314 | }
|
---|
2315 | break;
|
---|
2316 | }
|
---|
2317 |
|
---|
2318 | case MSG_EMM_ACK: {
|
---|
2319 | cc->just_logged_in = 0;
|
---|
2320 | if (cl->typ == 'c') { //EMM Request received
|
---|
2321 | cc_cmd_send(cl, NULL, 0, MSG_EMM_ACK); //Send back ACK
|
---|
2322 | if (l > 4) {
|
---|
2323 | cs_debug_mask(D_EMM, "%s EMM Request received!", getprefix());
|
---|
2324 |
|
---|
2325 | if (!ll_count(cl->aureader_list)) {
|
---|
2326 | cs_debug_mask(
|
---|
2327 | D_EMM,
|
---|
2328 | "%s EMM Request discarded because au is not assigned to an reader!",
|
---|
2329 | getprefix());
|
---|
2330 | return MSG_EMM_ACK;
|
---|
2331 | }
|
---|
2332 |
|
---|
2333 | EMM_PACKET *emm = cs_malloc(&emm, sizeof(EMM_PACKET), QUITERROR);
|
---|
2334 | memset(emm, 0, sizeof(EMM_PACKET));
|
---|
2335 | emm->caid[0] = buf[4];
|
---|
2336 | emm->caid[1] = buf[5];
|
---|
2337 | emm->provid[0] = buf[7];
|
---|
2338 | emm->provid[1] = buf[8];
|
---|
2339 | emm->provid[2] = buf[9];
|
---|
2340 | emm->provid[3] = buf[10];
|
---|
2341 | //emm->hexserial[0] = buf[11];
|
---|
2342 | //emm->hexserial[1] = buf[12];
|
---|
2343 | //emm->hexserial[2] = buf[13];
|
---|
2344 | //emm->hexserial[3] = buf[14];
|
---|
2345 | emm->l = buf[15];
|
---|
2346 | memcpy(emm->emm, buf + 16, emm->l);
|
---|
2347 | //emm->type = UNKNOWN;
|
---|
2348 | //emm->cidx = cs_idx;
|
---|
2349 | do_emm(cl, emm);
|
---|
2350 | free(emm);
|
---|
2351 | }
|
---|
2352 | } else { //Our EMM Request Ack!
|
---|
2353 | cs_debug_mask(D_EMM, "%s EMM ACK!", getprefix());
|
---|
2354 | if (!cc->extended_mode) {
|
---|
2355 | rdr->available = 1;
|
---|
2356 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2357 | }
|
---|
2358 | cc_send_ecm(cl, NULL, NULL);
|
---|
2359 | }
|
---|
2360 | break;
|
---|
2361 | }
|
---|
2362 | default:
|
---|
2363 | //cs_ddump_mask(D_CLIENT, buf, l, "%s unhandled msg: %d len=%d", getprefix(), buf[1], l);
|
---|
2364 | break;
|
---|
2365 | }
|
---|
2366 |
|
---|
2367 | if (cc->max_ecms && (cc->ecm_counter > cc->max_ecms)) {
|
---|
2368 | cs_debug_mask(D_READER, "%s max ecms (%d) reached, cycle connection!", getprefix(),
|
---|
2369 | cc->max_ecms);
|
---|
2370 | //cc_cycle_connection();
|
---|
2371 | cc_cli_close(cl, TRUE);
|
---|
2372 | //cc_send_ecm(NULL, NULL);
|
---|
2373 | }
|
---|
2374 | return ret;
|
---|
2375 | }
|
---|
2376 |
|
---|
2377 | /**
|
---|
2378 | * Reader: write dcw to receive
|
---|
2379 | */
|
---|
2380 | int cc_recv_chk(struct s_client *cl, uchar *dcw, int *rc, uchar *buf, int UNUSED(n)) {
|
---|
2381 | struct cc_data *cc = cl->cc;
|
---|
2382 |
|
---|
2383 | if (buf[1] == MSG_CW_ECM) {
|
---|
2384 | memcpy(dcw, cc->dcw, 16);
|
---|
2385 | //cs_debug_mask(D_CLIENT, "cccam: recv chk - MSG_CW %d - %s", cc->recv_ecmtask,
|
---|
2386 | // cs_hexdump(0, dcw, 16));
|
---|
2387 | *rc = 1;
|
---|
2388 | return (cc->recv_ecmtask);
|
---|
2389 | } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) {
|
---|
2390 | *rc = 0;
|
---|
2391 | //if (cc->is_oscam_cccam)
|
---|
2392 | if (cfg.cc_forward_origin_card)
|
---|
2393 | return (cc->recv_ecmtask);
|
---|
2394 | else
|
---|
2395 | return -1;
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | return (-1);
|
---|
2399 | }
|
---|
2400 |
|
---|
2401 | //int is_softfail(int rc)
|
---|
2402 | //{
|
---|
2403 | // //see oscam.c send_dcw() for a full list
|
---|
2404 | // switch(rc)
|
---|
2405 | // {
|
---|
2406 | // case 5: // 5 = timeout
|
---|
2407 | // case 6: // 6 = sleeping
|
---|
2408 | // case 7: // 7 = fake
|
---|
2409 | // case 10:// 10= no card
|
---|
2410 | // case 11:// 11= expdate
|
---|
2411 | // case 12:// 12= disabled
|
---|
2412 | // case 13:// 13= stopped
|
---|
2413 | // case 14:// 100= unhandled
|
---|
2414 | // return TRUE;
|
---|
2415 | // }
|
---|
2416 | // return FALSE;
|
---|
2417 | //}
|
---|
2418 |
|
---|
2419 |
|
---|
2420 |
|
---|
2421 | /**
|
---|
2422 | * Server: send DCW to client
|
---|
2423 | */
|
---|
2424 | void cc_send_dcw(struct s_client *cl, ECM_REQUEST *er) {
|
---|
2425 | uchar buf[16];
|
---|
2426 | struct cc_data *cc = cl->cc;
|
---|
2427 |
|
---|
2428 | memset(buf, 0, sizeof(buf));
|
---|
2429 |
|
---|
2430 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx_by_idx(cl, er->idx,
|
---|
2431 | TRUE);
|
---|
2432 |
|
---|
2433 | if (er->rc < E_NOTFOUND && eei && eei->card) { //found:
|
---|
2434 | memcpy(buf, er->cw, sizeof(buf));
|
---|
2435 | //fix_dcw(buf);
|
---|
2436 | //cs_debug_mask(D_TRACE, "%s send cw: %s cpti: %d", getprefix(),
|
---|
2437 | // cs_hexdump(0, buf, 16), er->cpti);
|
---|
2438 | if (!cc->extended_mode)
|
---|
2439 | cc_cw_crypt(cl, buf, eei->card->id);
|
---|
2440 | else
|
---|
2441 | cc->g_flag = eei->send_idx;
|
---|
2442 | cc_cmd_send(cl, buf, 16, MSG_CW_ECM);
|
---|
2443 | if (!cc->extended_mode)
|
---|
2444 | cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step
|
---|
2445 | free(eei->card);
|
---|
2446 | } else { //NOT found:
|
---|
2447 | //cs_debug_mask(D_TRACE, "%s send cw: NOK cpti: %d", getprefix(),
|
---|
2448 | // er->cpti);
|
---|
2449 |
|
---|
2450 | if (eei && cc->extended_mode)
|
---|
2451 | cc->g_flag = eei->send_idx;
|
---|
2452 |
|
---|
2453 | int nok;
|
---|
2454 | if (!eei || !eei->card)
|
---|
2455 | nok = MSG_CW_NOK1; //share no more available
|
---|
2456 | else {
|
---|
2457 | if (cfg.cc_forward_origin_card && er->origin_card == eei->card)
|
---|
2458 | nok = (er->rcEx==E2_CCCAM_NOK1)?MSG_CW_NOK1:MSG_CW_NOK2;
|
---|
2459 | else
|
---|
2460 | nok = MSG_CW_NOK2; //can't decode
|
---|
2461 | }
|
---|
2462 | cc_cmd_send(cl, NULL, 0, nok);
|
---|
2463 | }
|
---|
2464 | cc->server_ecm_pending--;
|
---|
2465 | free(eei);
|
---|
2466 | }
|
---|
2467 |
|
---|
2468 | int cc_recv(struct s_client *cl, uchar *buf, int l) {
|
---|
2469 | int n;
|
---|
2470 | uchar *cbuf;
|
---|
2471 |
|
---|
2472 | if (buf == NULL || l <= 0)
|
---|
2473 | return -1;
|
---|
2474 | cbuf = cs_malloc(&cbuf, l, QUITERROR);
|
---|
2475 | memcpy(cbuf, buf, l); // make a copy of buf
|
---|
2476 |
|
---|
2477 | n = cc_msg_recv(cl, cbuf, l); // recv and decrypt msg
|
---|
2478 |
|
---|
2479 | //cs_ddump_mask(D_CLIENT, cbuf, n, "cccam: received %d bytes from %s", n, remote_txt());
|
---|
2480 | cl->last = time((time_t *) 0);
|
---|
2481 |
|
---|
2482 | if (n <= 0) {
|
---|
2483 | cs_log("%s connection closed by %s", getprefix(), remote_txt());
|
---|
2484 | n = -1;
|
---|
2485 | } else if (n < 4) {
|
---|
2486 | cs_log("%s packet to small (%d bytes)", getprefix(), n);
|
---|
2487 | n = -1;
|
---|
2488 | } else {
|
---|
2489 | // parse it and write it back, if we have received something of value
|
---|
2490 | n = cc_parse_msg(cl, cbuf, n);
|
---|
2491 | memcpy(buf, cbuf, l);
|
---|
2492 | }
|
---|
2493 |
|
---|
2494 | NULLFREE(cbuf);
|
---|
2495 |
|
---|
2496 | if (n == -1) {
|
---|
2497 | if (cl->typ != 'c')
|
---|
2498 | cc_cli_close(cl, TRUE);
|
---|
2499 | }
|
---|
2500 |
|
---|
2501 | return n;
|
---|
2502 | }
|
---|
2503 |
|
---|
2504 | void cc_init_cc(struct cc_data *cc) {
|
---|
2505 | pthread_mutex_init(&cc->lockcmd, NULL); //No recursive lock
|
---|
2506 | pthread_mutex_init(&cc->ecm_busy, NULL); //No recusive lock
|
---|
2507 | pthread_mutex_init(&cc->cards_busy, NULL); //No (more) recursive lock
|
---|
2508 | }
|
---|
2509 |
|
---|
2510 | /**
|
---|
2511 | * Starting readers to get cards:
|
---|
2512 | **/
|
---|
2513 | int cc_srv_wakeup_readers(struct s_client *cl) {
|
---|
2514 | int wakeup = 0;
|
---|
2515 | struct s_reader *rdr;
|
---|
2516 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
2517 | if (rdr->typ != R_CCCAM)
|
---|
2518 | continue;
|
---|
2519 | if (!rdr->fd || rdr->tcp_connected == 2)
|
---|
2520 | continue;
|
---|
2521 | if (!(rdr->grp & cl->grp))
|
---|
2522 | continue;
|
---|
2523 | if (rdr->cc_keepalive) //if reader has keepalive but is NOT connected, reader can't connect. so don't ask him
|
---|
2524 | continue;
|
---|
2525 |
|
---|
2526 | //This wakeups the reader:
|
---|
2527 | uchar dummy;
|
---|
2528 | write_to_pipe(rdr->fd, PIP_ID_CIN, &dummy, sizeof(dummy));
|
---|
2529 | wakeup++;
|
---|
2530 | }
|
---|
2531 | return wakeup;
|
---|
2532 | }
|
---|
2533 |
|
---|
2534 |
|
---|
2535 | int check_cccam_compat(struct cc_data *cc) {
|
---|
2536 | int res = 0;
|
---|
2537 | if (strcmp(cfg.cc_version, "2.2.0") == 0 || strcmp(cfg.cc_version, "2.2.1") == 0) {
|
---|
2538 |
|
---|
2539 | if (strcmp(cc->remote_version, "2.2.0") == 0 || strcmp(cc->remote_version, "2.2.1") == 0) {
|
---|
2540 | res = 1;
|
---|
2541 | }
|
---|
2542 | }
|
---|
2543 | return res;
|
---|
2544 | }
|
---|
2545 |
|
---|
2546 | int cc_srv_connect(struct s_client *cl) {
|
---|
2547 | int i, wait_for_keepalive;
|
---|
2548 | ulong cmi;
|
---|
2549 | uint8 buf[CC_MAXMSGSIZE];
|
---|
2550 | uint8 data[16];
|
---|
2551 | char usr[21], pwd[65];
|
---|
2552 | struct s_auth *account;
|
---|
2553 | struct cc_data *cc = cl->cc;
|
---|
2554 | uchar mbuf[1024];
|
---|
2555 |
|
---|
2556 | memset(usr, 0, sizeof(usr));
|
---|
2557 | memset(pwd, 0, sizeof(pwd));
|
---|
2558 |
|
---|
2559 | //SS: Use last cc data for faster reconnects:
|
---|
2560 | if (!cc) {
|
---|
2561 | // init internals data struct
|
---|
2562 | cc = cs_malloc(&cc, sizeof(struct cc_data), QUITERROR);
|
---|
2563 | cl->cc = cc;
|
---|
2564 | memset(cl->cc, 0, sizeof(struct cc_data));
|
---|
2565 | cc->extended_ecm_idx = ll_create();
|
---|
2566 |
|
---|
2567 | cc_init_cc(cc);
|
---|
2568 | }
|
---|
2569 | cc->mode = CCCAM_MODE_NOTINIT;
|
---|
2570 | cc->server_ecm_pending = 0;
|
---|
2571 | cc->extended_mode = 0;
|
---|
2572 |
|
---|
2573 | int keep_alive = 1;
|
---|
2574 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
2575 | (void *)&keep_alive, sizeof(keep_alive));
|
---|
2576 |
|
---|
2577 | //Create checksum for "O" cccam:
|
---|
2578 | for (i = 0; i < 12; i++) {
|
---|
2579 | data[i] = fast_rnd();
|
---|
2580 | }
|
---|
2581 | for (i = 0; i < 4; i++) {
|
---|
2582 | data[12 + i] = (data[i] + data[4 + i] + data[8 + i]) & 0xff;
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | send(cl->udp_fd, data, 16, 0);
|
---|
2586 |
|
---|
2587 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
2588 |
|
---|
2589 | SHA_CTX ctx;
|
---|
2590 | SHA1_Init(&ctx);
|
---|
2591 | SHA1_Update(&ctx, data, 16);
|
---|
2592 | SHA1_Final(buf, &ctx);
|
---|
2593 |
|
---|
2594 | cc_init_crypt(&cc->block[ENCRYPT], buf, 20);
|
---|
2595 | cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT);
|
---|
2596 | cc_init_crypt(&cc->block[DECRYPT], data, 16);
|
---|
2597 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2598 |
|
---|
2599 | if ((i = cc_recv_to(cl, buf, 20)) == 20) {
|
---|
2600 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: recv:");
|
---|
2601 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2602 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: hash:");
|
---|
2603 | } else
|
---|
2604 | return -1;
|
---|
2605 |
|
---|
2606 | // receive username
|
---|
2607 | memset(buf, 0, sizeof(buf));
|
---|
2608 | if ((i = cc_recv_to(cl, buf, 20)) == 20) {
|
---|
2609 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2610 |
|
---|
2611 | strncpy(usr, (char *) buf, sizeof(usr));
|
---|
2612 |
|
---|
2613 | //test for nonprintable characters:
|
---|
2614 | for (i = 0; i < 20; i++) {
|
---|
2615 | if (usr[i] > 0 && usr[i] < 0x20) { //found nonprintable char
|
---|
2616 | cs_log("illegal username received");
|
---|
2617 | return -3;
|
---|
2618 | }
|
---|
2619 | }
|
---|
2620 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: username '%s':", usr);
|
---|
2621 | } else
|
---|
2622 | return -2;
|
---|
2623 |
|
---|
2624 | cl->crypted = 1;
|
---|
2625 |
|
---|
2626 | //CCCam only supports len=20 usr/pass. So we could have more than one user that matches the first 20 chars.
|
---|
2627 |
|
---|
2628 | //receive password-CCCam encrypted Hash:
|
---|
2629 | if (cc_recv_to(cl, buf, 6) != 6)
|
---|
2630 | return -2;
|
---|
2631 |
|
---|
2632 | account = cfg.account;
|
---|
2633 | struct cc_crypt_block *save_block = cs_malloc(&save_block, sizeof(struct cc_crypt_block), QUITERROR);
|
---|
2634 | memcpy(save_block, cc->block, sizeof(struct cc_crypt_block));
|
---|
2635 | int found = 0;
|
---|
2636 | while (1) {
|
---|
2637 | while (account) {
|
---|
2638 | if (strncmp(usr, account->usr, 20) == 0) {
|
---|
2639 | memset(pwd, 0, sizeof(pwd));
|
---|
2640 | strncpy(pwd, account->pwd, sizeof(pwd));
|
---|
2641 | found=1;
|
---|
2642 | break;
|
---|
2643 | }
|
---|
2644 | account = account->next;
|
---|
2645 | }
|
---|
2646 |
|
---|
2647 | if (!account)
|
---|
2648 | break;
|
---|
2649 |
|
---|
2650 | // receive passwd / 'CCcam'
|
---|
2651 | memcpy(cc->block, save_block, sizeof(struct cc_crypt_block));
|
---|
2652 | cc_crypt(&cc->block[DECRYPT], (uint8 *) pwd, strlen(pwd), ENCRYPT);
|
---|
2653 | cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT);
|
---|
2654 | //cs_ddump_mask(D_CLIENT, buf, 6, "cccam: pwd check '%s':", buf); //illegal buf-bytes could kill the logger!
|
---|
2655 | if (memcmp(buf, "CCcam\0", 6) == 0) //Password Hash OK!
|
---|
2656 | break; //account is set
|
---|
2657 |
|
---|
2658 | account = account->next;
|
---|
2659 | }
|
---|
2660 | free(save_block);
|
---|
2661 |
|
---|
2662 | if (cs_auth_client(cl, account, NULL)) { //cs_auth_client returns 0 if account is valid/active/accessible
|
---|
2663 | if (!found)
|
---|
2664 | cs_log("account '%s' not found!", usr);
|
---|
2665 | else
|
---|
2666 | cs_log("password for '%s' invalid!", usr);
|
---|
2667 | return -2;
|
---|
2668 | }
|
---|
2669 | if (cl->dup) {
|
---|
2670 | cs_log("account '%s' duplicate login, disconnect!", usr);
|
---|
2671 | return -3;
|
---|
2672 | }
|
---|
2673 | if (cl->disabled) {
|
---|
2674 | cs_log("account '%s' disabled, blocking+disconnect!", usr);
|
---|
2675 | return -2;
|
---|
2676 | }
|
---|
2677 | if (account->cccmaxhops<0) {
|
---|
2678 | cs_log("account '%s' has cccmaxhops<0, cccam can't handle this, disconnect!", usr);
|
---|
2679 | return -3;
|
---|
2680 | }
|
---|
2681 |
|
---|
2682 |
|
---|
2683 | if (!cc->prefix)
|
---|
2684 | cc->prefix = cs_malloc(&cc->prefix, strlen(cl->account->usr)+20, QUITERROR);
|
---|
2685 | snprintf(cc->prefix, strlen(cl->account->usr)+20, "cccam(s) %s: ", cl->account->usr);
|
---|
2686 |
|
---|
2687 |
|
---|
2688 | //Starting readers to get cards:
|
---|
2689 | cc_srv_wakeup_readers(cl);
|
---|
2690 |
|
---|
2691 | // send passwd ack
|
---|
2692 | memset(buf, 0, 20);
|
---|
2693 | memcpy(buf, "CCcam\0", 6);
|
---|
2694 | cs_ddump_mask(D_CLIENT, buf, 20, "cccam: send ack:");
|
---|
2695 | cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT);
|
---|
2696 | send(cl->pfd, buf, 20, 0);
|
---|
2697 |
|
---|
2698 | // recv cli data
|
---|
2699 | memset(buf, 0, sizeof(buf));
|
---|
2700 | i = cc_msg_recv(cl, buf, sizeof(buf));
|
---|
2701 | if (i < 0)
|
---|
2702 | return -1;
|
---|
2703 | cs_ddump_mask(D_CLIENT, buf, i, "cccam: cli data:");
|
---|
2704 | memcpy(cc->peer_node_id, buf + 24, 8);
|
---|
2705 |
|
---|
2706 | strncpy(cc->remote_version, (char*)buf+33, sizeof(cc->remote_version)-1);
|
---|
2707 | strncpy(cc->remote_build, (char*)buf+65, sizeof(cc->remote_build)-1);
|
---|
2708 |
|
---|
2709 | cs_debug_mask(D_CLIENT, "%s client '%s' (%s) running v%s (%s)", getprefix(), buf + 4,
|
---|
2710 | cs_hexdump(0, cc->peer_node_id, 8), cc->remote_version, cc->remote_build);
|
---|
2711 |
|
---|
2712 | // send cli data ack
|
---|
2713 | cc_cmd_send(cl, NULL, 0, MSG_CLI_DATA);
|
---|
2714 |
|
---|
2715 | if (cc_send_srv_data(cl) < 0)
|
---|
2716 | return -1;
|
---|
2717 |
|
---|
2718 | cc->cccam220 = check_cccam_compat(cc);
|
---|
2719 | cc->just_logged_in = TRUE;
|
---|
2720 | cc->answer_on_keepalive = time(NULL);
|
---|
2721 |
|
---|
2722 | //Wait for Partner detection (NOK1 with data) before reporting cards
|
---|
2723 | //When Partner is detected, cccam220=1 is set. then we can report extended card data
|
---|
2724 | i = process_input(mbuf, sizeof(mbuf), 1);
|
---|
2725 | if (i<=0 && i != -9)
|
---|
2726 | return 0; //disconnected
|
---|
2727 | if (cc->cccam220)
|
---|
2728 | cs_debug_mask(D_CLIENT, "%s extended sid mode activated", getprefix());
|
---|
2729 | else
|
---|
2730 | cs_debug_mask(D_CLIENT, "%s 2.1.x compatibility mode", getprefix());
|
---|
2731 |
|
---|
2732 | if (!cc_srv_report_cards(cl))
|
---|
2733 | return -1;
|
---|
2734 | cs_ftime(&cc->ecm_time);
|
---|
2735 |
|
---|
2736 | cmi = 0;
|
---|
2737 | wait_for_keepalive = 100;
|
---|
2738 | cc->mode = CCCAM_MODE_NORMAL;
|
---|
2739 | //some clients, e.g. mgcamd, does not support keepalive. So if not answered, keep connection
|
---|
2740 | // check for client timeout, if timeout occurs try to send keepalive
|
---|
2741 | while (cl->pfd && cl->udp_fd && cc->mode == CCCAM_MODE_NORMAL && !cl->dup)
|
---|
2742 | {
|
---|
2743 | i = process_input(mbuf, sizeof(mbuf), 10);
|
---|
2744 | if (i == -9) { //timeout 10s
|
---|
2745 | cmi+=10;
|
---|
2746 | if (cmi >= cfg.cmaxidle) {
|
---|
2747 | //cs_debug_mask(D_TRACE, "client timeout user %s idle=%d client max idle=%d", usr, cmi, cfg.cmaxidle);
|
---|
2748 | if (cfg.cc_keep_connected || cl->account->ncd_keepalive) {
|
---|
2749 | if (cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE) < 0)
|
---|
2750 | break;
|
---|
2751 | if (wait_for_keepalive<3 || wait_for_keepalive == 100) {
|
---|
2752 | cs_debug_mask(D_CLIENT, "cccam: keepalive");
|
---|
2753 | cc->answer_on_keepalive = time(NULL);
|
---|
2754 | wait_for_keepalive++;
|
---|
2755 | }
|
---|
2756 | else if (wait_for_keepalive<100) break;
|
---|
2757 | cmi = 0;
|
---|
2758 | } else {
|
---|
2759 | cs_debug_mask(D_CLIENT, "%s keepalive after maxidle is reached",
|
---|
2760 | getprefix());
|
---|
2761 | break; //Disconnect client
|
---|
2762 | }
|
---|
2763 | }
|
---|
2764 |
|
---|
2765 | } else if (i <= 0)
|
---|
2766 | break; //Disconnected by client
|
---|
2767 | else { //data is parsed!
|
---|
2768 | if (i == MSG_CW_ECM)
|
---|
2769 | cmi = 0;
|
---|
2770 | else if (i == MSG_KEEPALIVE) {
|
---|
2771 | wait_for_keepalive = 0;
|
---|
2772 | cmi = 0;
|
---|
2773 | }
|
---|
2774 | }
|
---|
2775 | }
|
---|
2776 | cc->mode = CCCAM_MODE_SHUTDOWN;
|
---|
2777 | return 0;
|
---|
2778 | }
|
---|
2779 |
|
---|
2780 | void * cc_srv_init(struct s_client *cl) {
|
---|
2781 | cl->thread = pthread_self();
|
---|
2782 | pthread_setspecific(getclient, cl);
|
---|
2783 |
|
---|
2784 | cl->pfd = cl->udp_fd;
|
---|
2785 | int ret;
|
---|
2786 | if ((ret=cc_srv_connect(cl)) < 0) {
|
---|
2787 | if (errno != 0)
|
---|
2788 | cs_debug_mask(D_CLIENT, "cccam: failed errno: %d (%s)", errno, strerror(errno));
|
---|
2789 | else
|
---|
2790 | cs_debug_mask(D_CLIENT, "cccam: failed ret: %d", ret);
|
---|
2791 | if (ret == -2)
|
---|
2792 | cs_add_violation((uint)cl->ip);
|
---|
2793 | }
|
---|
2794 | cs_disconnect_client(cl);
|
---|
2795 | return NULL; //suppress compiler warning
|
---|
2796 | }
|
---|
2797 |
|
---|
2798 | int cc_cli_connect(struct s_client *cl) {
|
---|
2799 | struct s_reader *rdr = cl->reader;
|
---|
2800 | struct cc_data *cc = cl->cc;
|
---|
2801 | rdr->card_status = CARD_FAILURE;
|
---|
2802 |
|
---|
2803 | if (cc && cc->mode == CCCAM_MODE_SHUTDOWN)
|
---|
2804 | return -99;
|
---|
2805 |
|
---|
2806 | if (!cl->udp_fd) {
|
---|
2807 | cc_cli_init_int(cl);
|
---|
2808 | return -1; // cc_cli_init_int calls cc_cli_connect, so exit here!
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 | if (is_connect_blocked(rdr)) {
|
---|
2812 | struct timeb cur_time;
|
---|
2813 | cs_ftime(&cur_time);
|
---|
2814 | int time = 1000*(rdr->tcp_block_connect_till.time-cur_time.time)
|
---|
2815 | +rdr->tcp_block_connect_till.millitm-cur_time.millitm;
|
---|
2816 | cs_log("%s connection blocked, retrying in %ds", rdr->label, time/1000);
|
---|
2817 | return -1;
|
---|
2818 | }
|
---|
2819 |
|
---|
2820 | int handle, n;
|
---|
2821 | uint8 data[20];
|
---|
2822 | uint8 hash[SHA_DIGEST_LENGTH];
|
---|
2823 | uint8 buf[CC_MAXMSGSIZE];
|
---|
2824 | char pwd[64];
|
---|
2825 |
|
---|
2826 | // check cred config
|
---|
2827 | if (rdr->device[0] == 0 || rdr->r_pwd[0] == 0 || rdr->r_usr[0] == 0
|
---|
2828 | || rdr->r_port == 0) {
|
---|
2829 | cs_log("%s configuration error!", rdr->label);
|
---|
2830 | return -5;
|
---|
2831 | }
|
---|
2832 |
|
---|
2833 | // connect
|
---|
2834 | handle = network_tcp_connection_open();
|
---|
2835 | if (handle <= 0) {
|
---|
2836 | cs_log("%s network connect error!", rdr->label);
|
---|
2837 | return -1;
|
---|
2838 | }
|
---|
2839 | if (errno == EISCONN) {
|
---|
2840 | cc_cli_close(cl, FALSE);
|
---|
2841 |
|
---|
2842 | block_connect(rdr);
|
---|
2843 | return -1;
|
---|
2844 | }
|
---|
2845 |
|
---|
2846 | // get init seed
|
---|
2847 | if ((n = cc_recv_to(cl, data, 16)) != 16) {
|
---|
2848 | int err = errno;
|
---|
2849 | if (n <= 0)
|
---|
2850 | cs_log("%s server blocked connection!", rdr->label);
|
---|
2851 | else
|
---|
2852 | cs_log("%s server does not return 16 bytes (n=%d, errno=%d %s)",
|
---|
2853 | rdr->label, n, err, strerror(errno));
|
---|
2854 | block_connect(rdr);
|
---|
2855 | return -2;
|
---|
2856 | }
|
---|
2857 |
|
---|
2858 | if (!cc) {
|
---|
2859 | // init internals data struct
|
---|
2860 | cc = cs_malloc(&cc, sizeof(struct cc_data), QUITERROR);
|
---|
2861 | memset(cc, 0, sizeof(struct cc_data));
|
---|
2862 | cc->cards = ll_create();
|
---|
2863 | cl->cc = cc;
|
---|
2864 | cc->pending_emms = ll_create();
|
---|
2865 | cc->extended_ecm_idx = ll_create();
|
---|
2866 | cc_init_cc(cc);
|
---|
2867 | } else {
|
---|
2868 | if (cc->cards) {
|
---|
2869 | pthread_mutex_lock(&cc->cards_busy);
|
---|
2870 | LL_ITER *it = ll_iter_create(cc->cards);
|
---|
2871 | struct cc_card *card;
|
---|
2872 | while ((card = ll_iter_next(it))) {
|
---|
2873 | cc_free_card(card);
|
---|
2874 | ll_iter_remove(it);
|
---|
2875 | }
|
---|
2876 | ll_iter_release(it);
|
---|
2877 | pthread_mutex_unlock(&cc->cards_busy);
|
---|
2878 | }
|
---|
2879 | if (cc->extended_ecm_idx)
|
---|
2880 | free_extended_ecm_idx(cc);
|
---|
2881 |
|
---|
2882 | pthread_mutex_trylock(&cc->ecm_busy);
|
---|
2883 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
2884 | }
|
---|
2885 | if (!cc->prefix)
|
---|
2886 | cc->prefix = cs_malloc(&cc->prefix, strlen(cl->reader->label)+20, QUITERROR);
|
---|
2887 | snprintf(cc->prefix, strlen(cl->reader->label)+20, "cccam(r) %s: ", cl->reader->label);
|
---|
2888 |
|
---|
2889 | cc->ecm_counter = 0;
|
---|
2890 | cc->max_ecms = 0;
|
---|
2891 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
2892 | cc->cmd05_offset = 0;
|
---|
2893 | cc->cmd05_active = 0;
|
---|
2894 | cc->cmd05_data_len = 0;
|
---|
2895 | cc->answer_on_keepalive = time(NULL);
|
---|
2896 | cc->extended_mode = 0;
|
---|
2897 | cc->last_emm_card = NULL;
|
---|
2898 |
|
---|
2899 | memset(&cc->cmd05_data, 0, sizeof(cc->cmd05_data));
|
---|
2900 | memset(&cc->receive_buffer, 0, sizeof(cc->receive_buffer));
|
---|
2901 | cc->cmd0c_mode = MODE_CMD_0x0C_NONE;
|
---|
2902 |
|
---|
2903 | cs_ddump_mask(D_CLIENT, data, 16, "cccam: server init seed:");
|
---|
2904 |
|
---|
2905 | uint16 sum = 0x1234;
|
---|
2906 | uint16 recv_sum = (data[14] << 8) | data[15];
|
---|
2907 | int i;
|
---|
2908 | for (i = 0; i < 14; i++) {
|
---|
2909 | sum += data[i];
|
---|
2910 | }
|
---|
2911 | //Create special data to detect oscam-cccam:
|
---|
2912 | cc->is_oscam_cccam = sum == recv_sum;
|
---|
2913 |
|
---|
2914 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
2915 |
|
---|
2916 | SHA_CTX ctx;
|
---|
2917 | SHA1_Init(&ctx);
|
---|
2918 | SHA1_Update(&ctx, data, 16);
|
---|
2919 | SHA1_Final(hash, &ctx);
|
---|
2920 |
|
---|
2921 | cs_ddump_mask(D_CLIENT, hash, sizeof(hash), "cccam: sha1 hash:");
|
---|
2922 |
|
---|
2923 | //initialisate crypto states
|
---|
2924 | cc_init_crypt(&cc->block[DECRYPT], hash, 20);
|
---|
2925 | cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT);
|
---|
2926 | cc_init_crypt(&cc->block[ENCRYPT], data, 16);
|
---|
2927 | cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT);
|
---|
2928 |
|
---|
2929 | cc_cmd_send(cl, hash, 20, MSG_NO_HEADER); // send crypted hash to server
|
---|
2930 |
|
---|
2931 | memset(buf, 0, sizeof(buf));
|
---|
2932 | memcpy(buf, rdr->r_usr, strlen(rdr->r_usr));
|
---|
2933 | cs_ddump_mask(D_CLIENT, buf, 20, "cccam: username '%s':", buf);
|
---|
2934 | cc_cmd_send(cl, buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes
|
---|
2935 |
|
---|
2936 | memset(buf, 0, sizeof(buf));
|
---|
2937 | memset(pwd, 0, sizeof(pwd));
|
---|
2938 |
|
---|
2939 | //cs_debug_mask(D_CLIENT, "cccam: 'CCcam' xor");
|
---|
2940 | memcpy(buf, "CCcam", 5);
|
---|
2941 | strncpy(pwd, rdr->r_pwd, sizeof(pwd) - 1);
|
---|
2942 | cc_crypt(&cc->block[ENCRYPT], (uint8 *) pwd, strlen(pwd), ENCRYPT);
|
---|
2943 | cc_cmd_send(cl, buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
|
---|
2944 |
|
---|
2945 | if ((n = cc_recv_to(cl, data, 20)) != 20) {
|
---|
2946 | cs_log("%s login failed, pwd ack not received (n = %d)", getprefix(), n);
|
---|
2947 | return -2;
|
---|
2948 | }
|
---|
2949 | cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT);
|
---|
2950 | cs_ddump_mask(D_CLIENT, data, 20, "cccam: pwd ack received:");
|
---|
2951 |
|
---|
2952 | if (memcmp(data, buf, 5)) { // check server response
|
---|
2953 | cs_log("%s login failed, usr/pwd invalid", getprefix());
|
---|
2954 | return -2;
|
---|
2955 | } else {
|
---|
2956 | cs_debug_mask(D_READER, "%s login succeeded", getprefix());
|
---|
2957 | }
|
---|
2958 |
|
---|
2959 | cs_debug_mask(D_READER, "cccam: last_s=%d, last_g=%d", rdr->last_s, rdr->last_g);
|
---|
2960 |
|
---|
2961 | cl->pfd = cl->udp_fd;
|
---|
2962 | cs_debug_mask(D_READER, "cccam: pfd=%d", cl->pfd);
|
---|
2963 |
|
---|
2964 | if (cc_send_cli_data(cl) <= 0) {
|
---|
2965 | cs_log("%s login failed, could not send client data", getprefix());
|
---|
2966 | return -3;
|
---|
2967 | }
|
---|
2968 |
|
---|
2969 | rdr->caid = rdr->ftab.filts[0].caid;
|
---|
2970 | rdr->nprov = rdr->ftab.filts[0].nprids;
|
---|
2971 | for (n = 0; n < rdr->nprov; n++) {
|
---|
2972 | rdr->availkeys[n][0] = 1;
|
---|
2973 | rdr->prid[n][0] = rdr->ftab.filts[0].prids[n] >> 24;
|
---|
2974 | rdr->prid[n][1] = rdr->ftab.filts[0].prids[n] >> 16;
|
---|
2975 | rdr->prid[n][2] = rdr->ftab.filts[0].prids[n] >> 8;
|
---|
2976 | rdr->prid[n][3] = rdr->ftab.filts[0].prids[n] & 0xff;
|
---|
2977 | }
|
---|
2978 |
|
---|
2979 | rdr->card_status = CARD_NEED_INIT;
|
---|
2980 | rdr->last_g = rdr->last_s = time((time_t *) 0);
|
---|
2981 | rdr->tcp_connected = 1;
|
---|
2982 | rdr->available = 1;
|
---|
2983 |
|
---|
2984 | cc->just_logged_in = 1;
|
---|
2985 | cc->mode = CCCAM_MODE_NORMAL;
|
---|
2986 | cl->crypted = 1;
|
---|
2987 |
|
---|
2988 | return 0;
|
---|
2989 | }
|
---|
2990 |
|
---|
2991 | int cc_cli_init_int(struct s_client *cl) {
|
---|
2992 | struct s_reader *rdr = cl->reader;
|
---|
2993 | if (rdr->tcp_connected)
|
---|
2994 | return 1;
|
---|
2995 |
|
---|
2996 | struct protoent *ptrp;
|
---|
2997 | int p_proto;
|
---|
2998 |
|
---|
2999 | if (cl->pfd) {
|
---|
3000 | close(cl->pfd);
|
---|
3001 | if (cl->pfd == cl->udp_fd)
|
---|
3002 | cl->udp_fd = 0;
|
---|
3003 | cl->pfd = 0;
|
---|
3004 | }
|
---|
3005 |
|
---|
3006 | if (cl->udp_fd) {
|
---|
3007 | close(cl->udp_fd);
|
---|
3008 | cl->udp_fd = 0;
|
---|
3009 | }
|
---|
3010 |
|
---|
3011 | if (rdr->r_port <= 0) {
|
---|
3012 | cs_log("%s invalid port %d for server %s", rdr->label, rdr->r_port,
|
---|
3013 | rdr->device);
|
---|
3014 | return 1;
|
---|
3015 | }
|
---|
3016 | if ((ptrp = getprotobyname("tcp")))
|
---|
3017 | p_proto = ptrp->p_proto;
|
---|
3018 | else
|
---|
3019 | p_proto = 6;
|
---|
3020 |
|
---|
3021 | // cl->ip = 0;
|
---|
3022 | // memset((char *) &loc_sa, 0, sizeof(loc_sa));
|
---|
3023 | // loc_sa.sin_family = AF_INET;
|
---|
3024 | //#ifdef LALL
|
---|
3025 | // if (cfg.serverip[0])
|
---|
3026 | // loc_sa.sin_addr.s_addr = inet_addr(cfg.serverip);
|
---|
3027 | // else
|
---|
3028 | //#endif
|
---|
3029 | // loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
3030 | // loc_sa.sin_port = htons(rdr->l_port);
|
---|
3031 |
|
---|
3032 |
|
---|
3033 | if ((cl->udp_fd = socket(PF_INET, SOCK_STREAM, p_proto)) <= 0) {
|
---|
3034 | cs_log("%s Socket creation failed (socket=%d, errno=%d %s)", rdr->label,
|
---|
3035 | cl->udp_fd, errno, strerror(errno));
|
---|
3036 | return 1;
|
---|
3037 | }
|
---|
3038 | //cs_log("%s 1 socket created: cs_idx=%d, fd=%d errno=%d", getprefix(), cs_idx, cl->udp_fd, errno);
|
---|
3039 |
|
---|
3040 | #ifdef SO_PRIORITY
|
---|
3041 | if (cfg.netprio)
|
---|
3042 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
3043 | (void *)&cfg.netprio, sizeof(ulong));
|
---|
3044 | #endif
|
---|
3045 | int keep_alive = 1;
|
---|
3046 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
3047 | (void *)&keep_alive, sizeof(keep_alive));
|
---|
3048 |
|
---|
3049 | rdr->tcp_ito = 1; //60sec...This now invokes ph_idle()
|
---|
3050 | if (rdr->cc_maxhop < 0)
|
---|
3051 | rdr->cc_maxhop = 10;
|
---|
3052 |
|
---|
3053 | memset((char *) &cl->udp_sa, 0, sizeof(cl->udp_sa));
|
---|
3054 | cl->udp_sa.sin_family = AF_INET;
|
---|
3055 | cl->udp_sa.sin_port = htons((u_short) rdr->r_port);
|
---|
3056 |
|
---|
3057 | if (rdr->tcp_rto <= 2)
|
---|
3058 | rdr->tcp_rto = 2; // timeout to 120s
|
---|
3059 | cs_debug_mask(D_READER, "cccam: timeout set to: %d", rdr->tcp_rto);
|
---|
3060 | cc_check_version(rdr->cc_version, rdr->cc_build);
|
---|
3061 | cs_debug_mask(D_READER, "proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d",
|
---|
3062 | rdr->label, rdr->device, rdr->r_port, rdr->cc_version,
|
---|
3063 | rdr->cc_build, rdr->cc_maxhop);
|
---|
3064 |
|
---|
3065 | return 0;
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | int cc_cli_init(struct s_client *cl) {
|
---|
3069 | struct cc_data *cc = cl->cc;
|
---|
3070 | struct s_reader *reader = cl->reader;
|
---|
3071 |
|
---|
3072 | if ((cc && cc->mode == CCCAM_MODE_SHUTDOWN))
|
---|
3073 | return -1;
|
---|
3074 |
|
---|
3075 | int res = cc_cli_init_int(cl); //Create socket
|
---|
3076 |
|
---|
3077 | if (res == 0 && reader && (reader->cc_keepalive || !cl->cc) && !reader->tcp_connected) {
|
---|
3078 |
|
---|
3079 | cc_cli_connect(cl); //connect to remote server
|
---|
3080 |
|
---|
3081 | while (!reader->tcp_connected && reader->cc_keepalive && cfg.reader_restart_seconds > 0) {
|
---|
3082 |
|
---|
3083 | if ((cc && cc->mode == CCCAM_MODE_SHUTDOWN))
|
---|
3084 | return -1;
|
---|
3085 |
|
---|
3086 | if (!reader->tcp_connected) {
|
---|
3087 | cc_cli_close(cl, FALSE);
|
---|
3088 | res = cc_cli_init_int(cl);
|
---|
3089 | if (res)
|
---|
3090 | return res;
|
---|
3091 | }
|
---|
3092 | cs_debug_mask(D_READER, "%s restarting reader in %d seconds", reader->label, cfg.reader_restart_seconds);
|
---|
3093 | cs_sleepms(cfg.reader_restart_seconds*1000);
|
---|
3094 | cs_debug_mask(D_READER, "%s restarting reader...", reader->label);
|
---|
3095 | cc_cli_connect(cl);
|
---|
3096 | }
|
---|
3097 | }
|
---|
3098 | return res;
|
---|
3099 | }
|
---|
3100 |
|
---|
3101 | /**
|
---|
3102 | * return 1 if we are able to send requests:
|
---|
3103 | *
|
---|
3104 | */
|
---|
3105 | int cc_available(struct s_reader *rdr, int checktype) {
|
---|
3106 | if (!rdr || !rdr->client) return 0;
|
---|
3107 |
|
---|
3108 | struct s_client *cl = rdr->client;
|
---|
3109 | //cs_debug_mask(D_TRACE, "checking reader %s availibility", rdr->label);
|
---|
3110 | if (!cl->cc || rdr->tcp_connected != 2 || rdr->card_status != CARD_INSERTED) {
|
---|
3111 | //Two cases:
|
---|
3112 | // 1. Keepalive ON but not connected: Do NOT send requests,
|
---|
3113 | // because we can't connect - problem of full running pipes
|
---|
3114 | // 2. Keepalive OFF but not connected: Send requests to connect
|
---|
3115 | // pipe won't run full, because we are reading from pipe to
|
---|
3116 | // get the ecm request
|
---|
3117 | if (rdr->cc_keepalive)
|
---|
3118 | return 0;
|
---|
3119 | }
|
---|
3120 |
|
---|
3121 | if (checktype == AVAIL_CHECK_LOADBALANCE && !rdr->available) {
|
---|
3122 | cs_debug_mask(D_TRACE, "checking reader %s availibility=0 (unavail)",
|
---|
3123 | rdr->label);
|
---|
3124 | return 0; //We are processing EMMs/ECMs
|
---|
3125 | }
|
---|
3126 |
|
---|
3127 | return 1;
|
---|
3128 | }
|
---|
3129 |
|
---|
3130 | /**
|
---|
3131 | *
|
---|
3132 | *
|
---|
3133 | **/
|
---|
3134 | void cc_card_info() {
|
---|
3135 | struct s_client *cl = cur_client();
|
---|
3136 | struct s_reader *rdr = cl->reader;
|
---|
3137 |
|
---|
3138 | if (rdr && !rdr->tcp_connected)
|
---|
3139 | cc_cli_connect(cl);
|
---|
3140 | }
|
---|
3141 |
|
---|
3142 | void cc_cleanup(struct s_client *cl) {
|
---|
3143 | struct cc_data *cc = cl->cc;
|
---|
3144 | if (cc) cc->mode = CCCAM_MODE_SHUTDOWN;
|
---|
3145 |
|
---|
3146 | if (cl->typ != 'c') {
|
---|
3147 | cc_cli_close(cl, FALSE); // we need to close open fd's
|
---|
3148 | }
|
---|
3149 | cc_free(cl);
|
---|
3150 | }
|
---|
3151 |
|
---|
3152 | void module_cccam(struct s_module *ph) {
|
---|
3153 | cs_strncpy(ph->desc, "cccam", sizeof(ph->desc));
|
---|
3154 | ph->type = MOD_CONN_TCP;
|
---|
3155 | ph->logtxt = ", crypted";
|
---|
3156 | ph->watchdog = 1;
|
---|
3157 | ph->recv = cc_recv;
|
---|
3158 | ph->cleanup = cc_cleanup;
|
---|
3159 | ph->multi = 1;
|
---|
3160 | ph->c_multi = 1;
|
---|
3161 | ph->c_init = cc_cli_init;
|
---|
3162 | ph->c_idle = cc_idle;
|
---|
3163 | ph->c_recv_chk = cc_recv_chk;
|
---|
3164 | ph->c_send_ecm = cc_send_ecm;
|
---|
3165 | ph->c_send_emm = cc_send_emm;
|
---|
3166 | ph->s_ip = cfg.cc_srvip;
|
---|
3167 | ph->s_handler = cc_srv_init;
|
---|
3168 | ph->send_dcw = cc_send_dcw;
|
---|
3169 | ph->c_available = cc_available;
|
---|
3170 | ph->c_card_info = cc_card_info;
|
---|
3171 | static PTAB ptab; //since there is always only 1 cccam server running, this is threadsafe
|
---|
3172 | ptab.ports[0].s_port = cfg.cc_port;
|
---|
3173 | ph->ptab = &ptab;
|
---|
3174 | ph->ptab->nports = 1;
|
---|
3175 | ph->num = R_CCCAM;
|
---|
3176 |
|
---|
3177 | //Partner Detection:
|
---|
3178 | init_rnd();
|
---|
3179 | uint16 sum = 0x1234; //This is our checksum
|
---|
3180 | int i;
|
---|
3181 | for (i = 0; i < 6; i++) {
|
---|
3182 | cc_node_id[i] = fast_rnd();
|
---|
3183 | sum += cc_node_id[i];
|
---|
3184 | }
|
---|
3185 | cc_node_id[6] = sum >> 8;
|
---|
3186 | cc_node_id[7] = sum & 0xff;
|
---|
3187 |
|
---|
3188 | if (cfg.cc_port)
|
---|
3189 | init_share();
|
---|
3190 | }
|
---|