1 | #include "globals.h"
|
---|
2 | extern struct s_reader *reader;
|
---|
3 |
|
---|
4 | int g_flag = 0;
|
---|
5 | /******************************** */
|
---|
6 | /* LINKED LIST CODE - IF IT'S USEFUL ELSEWHERE, IT SHOULD BE SPLIT OFF INTO linkedlist.h/.c */
|
---|
7 | /******************************** */
|
---|
8 |
|
---|
9 | // Simple, doubly linked
|
---|
10 | // This is thread-safe, so requires pthread. Also expect locking if iterators are not destroyed.
|
---|
11 |
|
---|
12 | #include <pthread.h>
|
---|
13 |
|
---|
14 | struct llist_node {
|
---|
15 | void *obj;
|
---|
16 | struct llist_node *prv;
|
---|
17 | struct llist_node *nxt;
|
---|
18 | };
|
---|
19 |
|
---|
20 | typedef struct llist {
|
---|
21 | struct llist_node *first;
|
---|
22 | struct llist_node *last;
|
---|
23 | int items;
|
---|
24 | pthread_mutex_t lock;
|
---|
25 | } LLIST;
|
---|
26 |
|
---|
27 | typedef struct llist_itr {
|
---|
28 | LLIST *l;
|
---|
29 | struct llist_node *cur;
|
---|
30 | } LLIST_ITR;
|
---|
31 |
|
---|
32 | LLIST *llist_create(void); // init linked list
|
---|
33 | void llist_destroy(LLIST *l); // de-init linked list - frees all objects on the list
|
---|
34 |
|
---|
35 | void *llist_append(LLIST *l, void *o); // append object onto bottom of list, returns ptr to obj
|
---|
36 |
|
---|
37 | void *llist_itr_init(LLIST *l, LLIST_ITR *itr); // linked list iterator, returns ptr to first obj
|
---|
38 | //void llist_itr_release(LLIST_ITR *itr); // release iterator
|
---|
39 | void *llist_itr_next(LLIST_ITR *itr); // iterates, returns ptr to next obj
|
---|
40 |
|
---|
41 | void *llist_itr_insert(LLIST_ITR *itr, void *o); // insert object at itr point, iterates to and returns ptr to new obj
|
---|
42 | void *llist_itr_remove(LLIST_ITR *itr); // remove obj at itr, iterates to and returns ptr to next obj
|
---|
43 |
|
---|
44 | int llist_count(LLIST *l); // returns number of obj in list
|
---|
45 |
|
---|
46 | /******************************** */
|
---|
47 |
|
---|
48 | #include <string.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 |
|
---|
51 | #define NULLFREE(X) do { if (X) { free(X); X = NULL; } } while(0)
|
---|
52 |
|
---|
53 | int cc_cli_init();
|
---|
54 |
|
---|
55 | LLIST *llist_create(void)
|
---|
56 | {
|
---|
57 | LLIST *l = malloc(sizeof(LLIST));
|
---|
58 | if (!l)
|
---|
59 | return NULL;
|
---|
60 | memset(l, 0, sizeof(LLIST));
|
---|
61 |
|
---|
62 | pthread_mutex_init(&l->lock, NULL);
|
---|
63 |
|
---|
64 | l->items = 0;
|
---|
65 |
|
---|
66 | return l;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void llist_destroy(LLIST *l)
|
---|
70 | {
|
---|
71 | LLIST_ITR itr;
|
---|
72 | if (!l)
|
---|
73 | return;
|
---|
74 | void *o = llist_itr_init(l, &itr);
|
---|
75 | while (o) {
|
---|
76 | free(o);
|
---|
77 | o = llist_itr_remove(&itr);
|
---|
78 | }
|
---|
79 | // llist_itr_release(&itr);
|
---|
80 | }
|
---|
81 |
|
---|
82 | void *llist_append(LLIST *l, void *o)
|
---|
83 | {
|
---|
84 | if (!l)
|
---|
85 | return NULL;
|
---|
86 | pthread_mutex_lock(&l->lock);
|
---|
87 | if (o) {
|
---|
88 | struct llist_node *ln = malloc(sizeof(struct llist_node));
|
---|
89 | if (!ln) {
|
---|
90 | pthread_mutex_unlock(&l->lock);
|
---|
91 | return NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | memset(ln, 0, sizeof(struct llist_node));
|
---|
95 | ln->obj = o;
|
---|
96 |
|
---|
97 | if (l->last) {
|
---|
98 | ln->prv = l->last;
|
---|
99 | ln->prv->nxt = ln;
|
---|
100 | } else {
|
---|
101 | l->first = ln;
|
---|
102 | }
|
---|
103 | l->last = ln;
|
---|
104 |
|
---|
105 | l->items++;
|
---|
106 | }
|
---|
107 | pthread_mutex_unlock(&l->lock);
|
---|
108 |
|
---|
109 | return o;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void *llist_itr_init(LLIST *l, LLIST_ITR *itr)
|
---|
113 | {
|
---|
114 | if (!l || !itr)
|
---|
115 | return NULL;
|
---|
116 | // pthread_mutex_lock(&l->lock);
|
---|
117 | if (l->first) {
|
---|
118 |
|
---|
119 | memset(itr, 0, sizeof(LLIST_ITR));
|
---|
120 | itr->cur = l->first;
|
---|
121 | itr->l = l;
|
---|
122 |
|
---|
123 | return itr->cur->obj;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return NULL;
|
---|
127 | }
|
---|
128 | /*
|
---|
129 | void llist_itr_release(LLIST_ITR *itr)
|
---|
130 | {
|
---|
131 | // pthread_mutex_unlock(&itr->l->lock);
|
---|
132 | }
|
---|
133 | */
|
---|
134 | void *llist_itr_next(LLIST_ITR *itr)
|
---|
135 | {
|
---|
136 | if (itr->cur->nxt) {
|
---|
137 | itr->cur = itr->cur->nxt;
|
---|
138 | return itr->cur->obj;
|
---|
139 | }
|
---|
140 |
|
---|
141 | return NULL;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void *llist_itr_remove(LLIST_ITR *itr) // this needs cleaning - I was lazy
|
---|
145 | {
|
---|
146 | if (!itr || !itr->l || itr->l->items == 0)
|
---|
147 | return NULL;
|
---|
148 | itr->l->items--;
|
---|
149 | if ((itr->cur == itr->l->first) && (itr->cur == itr->l->last)) {
|
---|
150 | NULLFREE(itr->cur);
|
---|
151 | itr->l->first = NULL;
|
---|
152 | itr->l->last = NULL;
|
---|
153 | return NULL;
|
---|
154 | } else if (itr->cur == itr->l->first) {
|
---|
155 | struct llist_node *nxt = itr->cur->nxt;
|
---|
156 | NULLFREE(itr->cur);
|
---|
157 | nxt->prv = NULL;
|
---|
158 | itr->l->first = nxt;
|
---|
159 | itr->cur = nxt;
|
---|
160 | } else if (itr->cur == itr->l->last) {
|
---|
161 | itr->l->last = itr->cur->prv;
|
---|
162 | itr->l->last->nxt = NULL;
|
---|
163 | NULLFREE(itr->cur);
|
---|
164 | return NULL;
|
---|
165 | } else {
|
---|
166 | struct llist_node *nxt = itr->cur->nxt;
|
---|
167 | itr->cur->prv->nxt = itr->cur->nxt;
|
---|
168 | itr->cur->nxt->prv = itr->cur->prv;
|
---|
169 | NULLFREE(itr->cur);
|
---|
170 | itr->cur = nxt;
|
---|
171 | }
|
---|
172 |
|
---|
173 | return itr->cur->obj;
|
---|
174 | }
|
---|
175 |
|
---|
176 | int llist_count(LLIST *l)
|
---|
177 | {
|
---|
178 | return l->items;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /******************************** */
|
---|
182 |
|
---|
183 | #define CC_MAXMSGSIZE 512
|
---|
184 | #define CC_MAX_PROV 16
|
---|
185 | #define CC_MAX_ECMS 50 // before reconnect
|
---|
186 |
|
---|
187 | #define SWAPC(X, Y) do { char p; p = *X; *X = *Y; *Y = p; } while(0)
|
---|
188 |
|
---|
189 | #if (defined(WIN32) || defined(OS_CYGWIN32)) && !defined(MSG_WAITALL)
|
---|
190 | # define MSG_WAITALL 0
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | typedef enum {
|
---|
194 | DECRYPT,
|
---|
195 | ENCRYPT
|
---|
196 | } cc_crypt_mode_t;
|
---|
197 |
|
---|
198 | typedef enum
|
---|
199 | {
|
---|
200 | MSG_CLI_DATA,
|
---|
201 | MSG_CW_ECM,
|
---|
202 | MSG_CARD_REMOVED = 4,
|
---|
203 | MSG_BAD_ECM,
|
---|
204 | MSG_KEEPALIVE,
|
---|
205 | MSG_NEW_CARD,
|
---|
206 | MSG_SRV_DATA,
|
---|
207 | MSG_CMD_0B = 0x0b,
|
---|
208 | MSG_CW_NOK1 = 0xfe,
|
---|
209 | MSG_CW_NOK2 = 0xff,
|
---|
210 | MSG_NO_HEADER = 0xffff
|
---|
211 | } cc_msg_type_t;
|
---|
212 |
|
---|
213 | struct cc_crypt_block
|
---|
214 | {
|
---|
215 | uint8 keytable[256];
|
---|
216 | uint8 state;
|
---|
217 | uint8 counter;
|
---|
218 | uint8 sum;
|
---|
219 | };
|
---|
220 |
|
---|
221 | struct cc_card {
|
---|
222 | uint32 id; // cccam card (share) id
|
---|
223 | uint32 sub_id; // subshare id
|
---|
224 | uint16 caid;
|
---|
225 | uint8 hop;
|
---|
226 | uint8 key[8]; // card serial (for au)
|
---|
227 | LLIST *provs; // providers
|
---|
228 | LLIST *badsids; // sids that have failed to decode
|
---|
229 | };
|
---|
230 |
|
---|
231 | struct cc_data {
|
---|
232 | struct cc_crypt_block block[2]; // crypto state blocks
|
---|
233 |
|
---|
234 | uint8 node_id[8], // client node id
|
---|
235 | peer_node_id[8], // server node id
|
---|
236 | dcw[16]; // control words
|
---|
237 |
|
---|
238 | struct cc_card *cur_card; // ptr to selected card
|
---|
239 | LLIST *cards; // cards list
|
---|
240 |
|
---|
241 | uint32 count;
|
---|
242 | uint32 ecm_count;
|
---|
243 | uint16 cur_sid;
|
---|
244 |
|
---|
245 | int last_nok;
|
---|
246 | ECM_REQUEST *found;
|
---|
247 |
|
---|
248 | unsigned long crc;
|
---|
249 |
|
---|
250 | pthread_mutex_t lock;
|
---|
251 | pthread_mutex_t ecm_busy;
|
---|
252 | };
|
---|
253 |
|
---|
254 | static unsigned int seed;
|
---|
255 | static uchar fast_rnd()
|
---|
256 | {
|
---|
257 | unsigned int offset = 12923;
|
---|
258 | unsigned int multiplier = 4079;
|
---|
259 |
|
---|
260 | seed = seed * multiplier + offset;
|
---|
261 | return (uchar)(seed % 0xFF);
|
---|
262 | }
|
---|
263 |
|
---|
264 | static void cc_init_crypt(struct cc_crypt_block *block, uint8 *key, int len)
|
---|
265 | {
|
---|
266 | int i = 0 ;
|
---|
267 | uint8 j = 0;
|
---|
268 |
|
---|
269 | for (i=0; i<256; i++) {
|
---|
270 | block->keytable[i] = i;
|
---|
271 | }
|
---|
272 |
|
---|
273 | for (i=0; i<256; i++) {
|
---|
274 | j += key[i % len] + block->keytable[i];
|
---|
275 | SWAPC(&block->keytable[i], &block->keytable[j]);
|
---|
276 | }
|
---|
277 |
|
---|
278 | block->state = *key;
|
---|
279 | block->counter=0;
|
---|
280 | block->sum=0;
|
---|
281 | }
|
---|
282 |
|
---|
283 | static void cc_crypt(struct cc_crypt_block *block, uint8 *data, int len, cc_crypt_mode_t mode)
|
---|
284 | {
|
---|
285 | int i;
|
---|
286 | uint8 z;
|
---|
287 |
|
---|
288 | for (i = 0; i < len; i++) {
|
---|
289 | block->counter++;
|
---|
290 | block->sum += block->keytable[block->counter];
|
---|
291 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
292 | z = data[i];
|
---|
293 | data[i] = z ^ block->keytable[(block->keytable[block->counter] + block->keytable[block->sum]) & 0xff] ^ block->state;
|
---|
294 | if (!mode) z = data[i];
|
---|
295 | block->state = block->state ^ z;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | static void cc_xor(uint8 *buf)
|
---|
300 | {
|
---|
301 | const char cccam[] = "CCcam";
|
---|
302 | uint8 i;
|
---|
303 |
|
---|
304 | for ( i = 0; i < 8; i++ ) {
|
---|
305 | buf[8 + i] = i * buf[i];
|
---|
306 | if ( i <= 5 ) {
|
---|
307 | buf[i] ^= cccam[i];
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | static void cc_cw_crypt(uint8 *cws)
|
---|
313 | {
|
---|
314 | struct cc_data *cc;
|
---|
315 | uint64 node_id;
|
---|
316 | uint8 tmp;
|
---|
317 | int i;
|
---|
318 |
|
---|
319 | if (reader[ridx].cc) {
|
---|
320 | cc = reader[ridx].cc;
|
---|
321 | node_id = b2ll(8, cc->node_id);
|
---|
322 | }
|
---|
323 | else {
|
---|
324 | cc = client[cs_idx].cc;
|
---|
325 | node_id = b2ll(8, cc->peer_node_id);
|
---|
326 | }
|
---|
327 |
|
---|
328 | for (i = 0; i < 16; i++) {
|
---|
329 | tmp = cws[i] ^ (node_id >> (4 * i));
|
---|
330 | if (i & 1) tmp = ~tmp;
|
---|
331 | cws[i] = (cc->cur_card->id >> (2 * i)) ^ tmp;
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | static void cc_cycle_connection()
|
---|
336 | {
|
---|
337 | reader[ridx].tcp_connected = 0;
|
---|
338 | cs_sleepms(200);
|
---|
339 | close(pfd);
|
---|
340 | pfd = 0;
|
---|
341 | client[cs_idx].udp_fd = 0;
|
---|
342 | cs_sleepms(100);
|
---|
343 | cc_cli_init();
|
---|
344 | }
|
---|
345 |
|
---|
346 | static int cc_msg_recv(uint8 *buf)
|
---|
347 | {
|
---|
348 | int len;
|
---|
349 | uint8 netbuf[CC_MAXMSGSIZE + 4];
|
---|
350 | struct cc_data *cc;
|
---|
351 |
|
---|
352 | if (reader[ridx].cc)
|
---|
353 | cc = reader[ridx].cc;
|
---|
354 | else
|
---|
355 | cc = client[cs_idx].cc;
|
---|
356 |
|
---|
357 | int handle = client[cs_idx].udp_fd;
|
---|
358 |
|
---|
359 | if (handle < 0) return -1;
|
---|
360 |
|
---|
361 | len = recv(handle, netbuf, 4, MSG_WAITALL);
|
---|
362 |
|
---|
363 | if (!len) return 0;
|
---|
364 |
|
---|
365 | if (len != 4) { // invalid header length read
|
---|
366 | cs_log("cccam: invalid header length");
|
---|
367 | return -1;
|
---|
368 | }
|
---|
369 |
|
---|
370 | cc_crypt(&cc->block[DECRYPT], netbuf, 4, DECRYPT);
|
---|
371 | cs_ddump(netbuf, 4, "cccam: decrypted header:");
|
---|
372 |
|
---|
373 | g_flag = netbuf[0];
|
---|
374 |
|
---|
375 | if (((netbuf[2] << 8) | netbuf[3]) != 0) { // check if any data is expected in msg
|
---|
376 | if (((netbuf[2] << 8) | netbuf[3]) > CC_MAXMSGSIZE - 2) {
|
---|
377 | cs_log("cccam: message too big");
|
---|
378 | return -1;
|
---|
379 | }
|
---|
380 |
|
---|
381 | len = recv(handle, netbuf+4, (netbuf[2] << 8) | netbuf[3], MSG_WAITALL); // read rest of msg
|
---|
382 |
|
---|
383 | if (len != ((netbuf[2] << 8) | netbuf[3])) {
|
---|
384 | cs_log("cccam: invalid message length read");
|
---|
385 | return -1;
|
---|
386 | }
|
---|
387 |
|
---|
388 | cc_crypt(&cc->block[DECRYPT], netbuf+4, len, DECRYPT);
|
---|
389 | len += 4;
|
---|
390 | }
|
---|
391 |
|
---|
392 | cs_ddump(netbuf, len, "cccam: full decrypted msg, len=%d:", len);
|
---|
393 |
|
---|
394 | memcpy(buf, netbuf, len);
|
---|
395 | return len;
|
---|
396 | }
|
---|
397 |
|
---|
398 | static int cc_cmd_send(uint8 *buf, int len, cc_msg_type_t cmd)
|
---|
399 | {
|
---|
400 | int n;
|
---|
401 | uint8 netbuf[len+4];
|
---|
402 | struct cc_data *cc;
|
---|
403 |
|
---|
404 | if (reader[ridx].cc)
|
---|
405 | cc = reader[ridx].cc;
|
---|
406 | else
|
---|
407 | cc = client[cs_idx].cc;
|
---|
408 |
|
---|
409 | memset(netbuf, 0, len+4);
|
---|
410 |
|
---|
411 | if (cmd == MSG_NO_HEADER) {
|
---|
412 | memcpy(netbuf, buf, len);
|
---|
413 | } else {
|
---|
414 | // build command message
|
---|
415 | netbuf[0] = g_flag; // flags??
|
---|
416 | netbuf[1] = cmd & 0xff;
|
---|
417 | netbuf[2] = len >> 8;
|
---|
418 | netbuf[3] = len & 0xff;
|
---|
419 | if (buf) memcpy(netbuf+4, buf, len);
|
---|
420 | len += 4;
|
---|
421 | }
|
---|
422 |
|
---|
423 | cs_ddump(netbuf, len, "cccam: send:");
|
---|
424 | cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
|
---|
425 |
|
---|
426 | n = send(client[cs_idx].udp_fd, netbuf, len, 0);
|
---|
427 |
|
---|
428 | return n;
|
---|
429 | }
|
---|
430 |
|
---|
431 | #define CC_DEFAULT_VERSION 1
|
---|
432 | static void cc_check_version (char *cc_version, char *cc_build)
|
---|
433 | {
|
---|
434 | char *version[] = { "2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", "" };
|
---|
435 | char *build[] = { "2892", "2971", "3094", "3165", "3191", "" };
|
---|
436 | int i;
|
---|
437 |
|
---|
438 | if (strlen (cc_version) == 0) {
|
---|
439 | memcpy (cc_version, version[CC_DEFAULT_VERSION], strlen (version[CC_DEFAULT_VERSION]));
|
---|
440 | memcpy (cc_build, build[CC_DEFAULT_VERSION], strlen (build[CC_DEFAULT_VERSION]));
|
---|
441 | cs_debug ("cccam: auto version set: %s build: %s", cc_version, cc_build);
|
---|
442 | return;
|
---|
443 | }
|
---|
444 |
|
---|
445 | for (i = 0; strlen (version[i]); i++)
|
---|
446 | if (!memcmp (cc_version, version[i], strlen (version[i]))) {
|
---|
447 | memcpy (cc_build, build[i], strlen (build[i]));
|
---|
448 | cs_debug ("cccam: auto build set for version: %s build: %s", cc_version, cc_build);
|
---|
449 | break;
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | static int cc_send_cli_data()
|
---|
454 | {
|
---|
455 | int i;
|
---|
456 | struct cc_data *cc = reader[ridx].cc;
|
---|
457 |
|
---|
458 | cs_debug("cccam: send client data");
|
---|
459 |
|
---|
460 | seed = (unsigned int) time((time_t*)0);
|
---|
461 | for( i=0; i<8; i++ ) cc->node_id[i]=fast_rnd();
|
---|
462 |
|
---|
463 | uint8 buf[CC_MAXMSGSIZE];
|
---|
464 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
465 |
|
---|
466 | memcpy(buf, reader[ridx].r_usr, sizeof(reader[ridx].r_usr));
|
---|
467 | memcpy(buf + 20, cc->node_id, 8 );
|
---|
468 | memcpy(buf + 29, reader[ridx].cc_version, sizeof(reader[ridx].cc_version)); // cccam version (ascii)
|
---|
469 | memcpy(buf + 61, reader[ridx].cc_build, sizeof(reader[ridx].cc_build)); // build number (ascii)
|
---|
470 |
|
---|
471 | cs_log ("cccam: user: %s, version: %s, build: %s", reader[ridx].r_usr, reader[ridx].cc_version, reader[ridx].cc_build);
|
---|
472 |
|
---|
473 | i = cc_cmd_send(buf, 20 + 8 + 6 + 26 + 4 + 28 + 1, MSG_CLI_DATA);
|
---|
474 |
|
---|
475 | return i;
|
---|
476 | }
|
---|
477 |
|
---|
478 | static int cc_send_srv_data()
|
---|
479 | {
|
---|
480 | int i;
|
---|
481 | struct cc_data *cc = client[cs_idx].cc;
|
---|
482 |
|
---|
483 | cs_debug("cccam: send server data");
|
---|
484 |
|
---|
485 | seed = (unsigned int) time((time_t*)0);
|
---|
486 | for( i=0; i<8; i++ ) cc->node_id[i]=fast_rnd();
|
---|
487 |
|
---|
488 | uint8 buf[CC_MAXMSGSIZE];
|
---|
489 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
490 |
|
---|
491 | memcpy(buf, cc->node_id, 8 );
|
---|
492 | cc_check_version ((char *) cfg->cc_version, (char *) cfg->cc_build);
|
---|
493 | memcpy(buf + 8, cfg->cc_version, sizeof(reader[ridx].cc_version)); // cccam version (ascii)
|
---|
494 | memcpy(buf + 40, cfg->cc_build, sizeof(reader[ridx].cc_build)); // build number (ascii)
|
---|
495 |
|
---|
496 | cs_log ("cccam: version: %s, build: %s nodeid: %s", cfg->cc_version, cfg->cc_build, cs_hexdump(0, cc->peer_node_id,8));
|
---|
497 |
|
---|
498 | return cc_cmd_send(buf, 0x48, MSG_SRV_DATA);
|
---|
499 | }
|
---|
500 |
|
---|
501 | static int cc_get_nxt_ecm()
|
---|
502 | {
|
---|
503 | int n, i;
|
---|
504 | time_t t;
|
---|
505 | // struct cc_data *cc = reader[ridx].cc;
|
---|
506 |
|
---|
507 | t=time(NULL);
|
---|
508 | for (i = 1, n = 1; i < CS_MAXPENDING; i++)
|
---|
509 | {
|
---|
510 | if ((t-(ulong)ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
511 | (ecmtask[i].rc >= 10)) // drop timeouts
|
---|
512 | {
|
---|
513 | ecmtask[i].rc=0;
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (ecmtask[i].rc >= 10) { // stil active and waiting
|
---|
517 | // search for the ecm with the lowest time, this should be the next to go
|
---|
518 | if ((!n || ecmtask[n].tps.time-ecmtask[i].tps.time < 0) && &ecmtask[n]) n = i;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | return n;
|
---|
522 | }
|
---|
523 |
|
---|
524 | static int cc_send_ecm(ECM_REQUEST *er, uchar *buf)
|
---|
525 | {
|
---|
526 | int n, h = -1;
|
---|
527 | struct cc_data *cc = reader[ridx].cc;
|
---|
528 | struct cc_card *card;
|
---|
529 | LLIST_ITR itr;
|
---|
530 | ECM_REQUEST *cur_er;
|
---|
531 |
|
---|
532 | if (cc->ecm_count) cc->ecm_count++;
|
---|
533 |
|
---|
534 | if (!cc || (pfd < 1)) {
|
---|
535 | if (er) {
|
---|
536 | er->rc = 0;
|
---|
537 | er->rcEx = 0x27;
|
---|
538 | cs_log("cccam: server not init!");
|
---|
539 | write_ecm_answer(fd_c2m, er);
|
---|
540 | }
|
---|
541 | return -1;
|
---|
542 | }
|
---|
543 |
|
---|
544 | // pthread_mutex_lock(&cc->ecm_busy);
|
---|
545 | if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) {
|
---|
546 | cs_debug("cccam: ecm trylock: failed to get lock");
|
---|
547 | return 0;
|
---|
548 | } else {
|
---|
549 | cs_debug("cccam: ecm trylock: got lock");
|
---|
550 | }
|
---|
551 | // pthread_mutex_lock(&cc->lock);
|
---|
552 |
|
---|
553 | if ((n = cc_get_nxt_ecm()) < 0) {
|
---|
554 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
555 | pthread_mutex_unlock(&cc->lock);
|
---|
556 | return 0; // no queued ecms
|
---|
557 | }
|
---|
558 | cur_er = &ecmtask[n];
|
---|
559 |
|
---|
560 | if (crc32(0, cur_er->ecm, cur_er->l) == cc->crc) cur_er->rc = 99;
|
---|
561 | cc->crc = crc32(0, cur_er->ecm, cur_er->l);
|
---|
562 |
|
---|
563 | cs_debug("cccam: ecm crc = 0x%lx", cc->crc);
|
---|
564 |
|
---|
565 | if (cur_er->rc == 99) {
|
---|
566 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
567 | pthread_mutex_unlock(&cc->lock);
|
---|
568 | return 0; // ecm already sent
|
---|
569 | }
|
---|
570 |
|
---|
571 | //cc->found = cur_er;
|
---|
572 |
|
---|
573 | if (buf) memcpy(buf, cur_er->ecm, cur_er->l);
|
---|
574 |
|
---|
575 | cc->cur_card = NULL;
|
---|
576 | cc->cur_sid = cur_er->srvid;
|
---|
577 |
|
---|
578 | card = llist_itr_init(cc->cards, &itr);
|
---|
579 |
|
---|
580 | while (card) {
|
---|
581 | if (card->caid == cur_er->caid) { // caid matches
|
---|
582 | int s = 0;
|
---|
583 |
|
---|
584 | LLIST_ITR sitr;
|
---|
585 | uint16 *sid = llist_itr_init(card->badsids, &sitr);
|
---|
586 | while (sid) {
|
---|
587 | if (*sid == cc->cur_sid) {
|
---|
588 | s = 1;
|
---|
589 | break;
|
---|
590 | }
|
---|
591 | sid = llist_itr_next(&sitr);
|
---|
592 | }
|
---|
593 | // llist_itr_release(&sitr);
|
---|
594 |
|
---|
595 | LLIST_ITR pitr;
|
---|
596 | uint8 *prov = llist_itr_init(card->provs, &pitr);
|
---|
597 | while (prov && !s) {
|
---|
598 | if (!cur_er->prid || b2i(3, prov) == cur_er->prid) { // provid matches
|
---|
599 | if (((h < 0) || (card->hop < h)) && (card->hop <= reader[ridx].cc_maxhop - 1)) { // card is closer and doesn't exceed max hop
|
---|
600 | cc->cur_card = card;
|
---|
601 | h = card->hop; // card has been matched
|
---|
602 | }
|
---|
603 | }
|
---|
604 | prov = llist_itr_next(&pitr);
|
---|
605 | }
|
---|
606 | // llist_itr_release(&pitr);
|
---|
607 | }
|
---|
608 | card = llist_itr_next(&itr);
|
---|
609 | }
|
---|
610 | // llist_itr_release(&itr);
|
---|
611 |
|
---|
612 | if (cc->cur_card) {
|
---|
613 | uint8 ecmbuf[CC_MAXMSGSIZE];
|
---|
614 | memset(ecmbuf, 0, CC_MAXMSGSIZE);
|
---|
615 |
|
---|
616 | // build ecm message
|
---|
617 | ecmbuf[0] = cc->cur_card->caid >> 8;
|
---|
618 | ecmbuf[1] = cc->cur_card->caid & 0xff;
|
---|
619 | ecmbuf[2] = cur_er->prid >> 24;
|
---|
620 | ecmbuf[3] = cur_er->prid >> 16;
|
---|
621 | ecmbuf[4] = cur_er->prid >> 8;
|
---|
622 | ecmbuf[5] = cur_er->prid & 0xff;
|
---|
623 | ecmbuf[6] = cc->cur_card->id >> 24;
|
---|
624 | ecmbuf[7] = cc->cur_card->id >> 16;
|
---|
625 | ecmbuf[8] = cc->cur_card->id >> 8;
|
---|
626 | ecmbuf[9] = cc->cur_card->id & 0xff;
|
---|
627 | ecmbuf[10] = cur_er->srvid >> 8;
|
---|
628 | ecmbuf[11] = cur_er->srvid & 0xff;
|
---|
629 | ecmbuf[12] = cur_er->l & 0xff;
|
---|
630 | memcpy(ecmbuf+13, cur_er->ecm, cur_er->l);
|
---|
631 |
|
---|
632 | cc->count = cur_er->idx;
|
---|
633 | reader[ridx].cc_currenthops = cc->cur_card->hop + 1;
|
---|
634 |
|
---|
635 | //cs_log("cccam: sending ecm for sid %04x to card %08x, hop %d", cur_er->srvid, cc->cur_card->id, cc->cur_card->hop + 1);
|
---|
636 | n = cc_cmd_send(ecmbuf, cur_er->l+13, MSG_CW_ECM); // send ecm
|
---|
637 |
|
---|
638 | } else {
|
---|
639 | n = -1;
|
---|
640 | //cs_log("cccam: no suitable card on server");
|
---|
641 | cur_er->rc = 0;
|
---|
642 | cur_er->rcEx = 0x27;
|
---|
643 | //cur_er->rc = 1;
|
---|
644 | //cur_er->rcEx = 0;
|
---|
645 | cs_sleepms(300);
|
---|
646 | write_ecm_answer(fd_c2m, cur_er);
|
---|
647 | //reader[ridx].last_s = reader[ridx].last_g;
|
---|
648 |
|
---|
649 | card = llist_itr_init(cc->cards, &itr);
|
---|
650 | while (card) {
|
---|
651 | if (card->caid == cur_er->caid) { // caid matches
|
---|
652 | LLIST_ITR sitr;
|
---|
653 | uint16 *sid = llist_itr_init(card->badsids, &sitr);
|
---|
654 | while (sid) {
|
---|
655 | if (*sid == cur_er->srvid)
|
---|
656 | sid = llist_itr_remove(&sitr);
|
---|
657 | else sid = llist_itr_next(&sitr);
|
---|
658 | }
|
---|
659 | // llist_itr_release(&sitr);
|
---|
660 | }
|
---|
661 | card = llist_itr_next(&itr);
|
---|
662 | }
|
---|
663 | // llist_itr_release(&itr);
|
---|
664 |
|
---|
665 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
666 | }
|
---|
667 |
|
---|
668 | return 0;
|
---|
669 | }
|
---|
670 | /*
|
---|
671 | static int cc_abort_user_ecms(){
|
---|
672 | int n, i;
|
---|
673 | time_t t;//, tls;
|
---|
674 | struct cc_data *cc = reader[ridx].cc;
|
---|
675 |
|
---|
676 | t=time((time_t *)0);
|
---|
677 | for (i=1,n=1; i<CS_MAXPENDING; i++)
|
---|
678 | {
|
---|
679 | if ((t-ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
680 | (ecmtask[i].rc>=10)) // drop timeouts
|
---|
681 | {
|
---|
682 | ecmtask[i].rc=0;
|
---|
683 | }
|
---|
684 | int td=abs(1000*(ecmtask[i].tps.time-cc->found->tps.time)+ecmtask[i].tps.millitm-cc->found->tps.millitm);
|
---|
685 | if (ecmtask[i].rc>=10 && ecmtask[i].cidx==cc->found->cidx && &ecmtask[i]!=cc->found){
|
---|
686 | cs_log("aborting idx:%d caid:%04x client:%d timedelta:%d",ecmtask[i].idx,ecmtask[i].caid,ecmtask[i].cidx,td);
|
---|
687 | ecmtask[i].rc=0;
|
---|
688 | ecmtask[i].rcEx=7;
|
---|
689 | write_ecm_answer(fd_c2m, &ecmtask[i]);
|
---|
690 | }
|
---|
691 | }
|
---|
692 | return n;
|
---|
693 |
|
---|
694 | }
|
---|
695 | */
|
---|
696 |
|
---|
697 | static cc_msg_type_t cc_parse_msg(uint8 *buf, int l)
|
---|
698 | {
|
---|
699 | int ret = buf[1];
|
---|
700 | struct cc_data *cc;
|
---|
701 |
|
---|
702 | if (reader[ridx].cc)
|
---|
703 | cc = reader[ridx].cc;
|
---|
704 | else
|
---|
705 | cc = client[cs_idx].cc;
|
---|
706 |
|
---|
707 | switch (buf[1]) {
|
---|
708 | case MSG_CLI_DATA:
|
---|
709 | cs_debug("cccam: client data ack");
|
---|
710 | break;
|
---|
711 | case MSG_SRV_DATA:
|
---|
712 | memcpy(cc->peer_node_id, buf+4, 8);
|
---|
713 | cs_log("cccam: srv %s running v%s (%s)", cs_hexdump(0, cc->peer_node_id, 8), buf+12, buf+44);
|
---|
714 | break;
|
---|
715 | case MSG_NEW_CARD:
|
---|
716 | {
|
---|
717 | int i = 0;
|
---|
718 | if (buf[14] > reader[ridx].cc_maxhop)
|
---|
719 | break;
|
---|
720 | struct cc_card *card = malloc(sizeof(struct cc_card));
|
---|
721 | if (!card)
|
---|
722 | break;
|
---|
723 |
|
---|
724 | memset(card, 0, sizeof(struct cc_card));
|
---|
725 |
|
---|
726 | card->provs = llist_create();
|
---|
727 | card->badsids = llist_create();
|
---|
728 | card->id = b2i(4, buf+4);
|
---|
729 | card->sub_id = b2i (3, buf + 9);
|
---|
730 | card->caid = b2i(2, buf+12);
|
---|
731 | card->hop = buf[14];
|
---|
732 | memcpy(card->key, buf+16, 8);
|
---|
733 |
|
---|
734 | cs_debug("cccam: card %08x added, caid %04x, hop %d, key %s",
|
---|
735 | card->id, card->caid, card->hop, cs_hexdump(0, card->key, 8));
|
---|
736 |
|
---|
737 |
|
---|
738 | for (i = 0; i < buf[24]; i++) { // providers
|
---|
739 | uint8 *prov = malloc(3);
|
---|
740 | if (prov) {
|
---|
741 |
|
---|
742 | memcpy(prov, buf+25+(7*i), 3);
|
---|
743 | cs_debug(" prov %d, %06x", i+1, b2i(3, prov));
|
---|
744 |
|
---|
745 | llist_append(card->provs, prov);
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 |
|
---|
750 | llist_append(cc->cards, card);
|
---|
751 | if (!cc->cur_card) cc->cur_card = card;
|
---|
752 | }
|
---|
753 | break;
|
---|
754 | case MSG_CARD_REMOVED:
|
---|
755 | {
|
---|
756 | struct cc_card *card;
|
---|
757 | LLIST_ITR itr;
|
---|
758 |
|
---|
759 | card = llist_itr_init(cc->cards, &itr);
|
---|
760 | while (card) {
|
---|
761 | if (card->id == b2i(4, buf+4) && card->sub_id == b2i (3, buf + 9)) {
|
---|
762 | cs_debug("cccam: card %08x removed, caid %04x", card->id, card->caid);
|
---|
763 |
|
---|
764 | llist_destroy(card->provs);
|
---|
765 | llist_destroy(card->badsids);
|
---|
766 | free(card);
|
---|
767 |
|
---|
768 | card = llist_itr_remove(&itr);
|
---|
769 | break;
|
---|
770 | } else {
|
---|
771 | card = llist_itr_next(&itr);
|
---|
772 | }
|
---|
773 | }
|
---|
774 | // llist_itr_release(&itr);
|
---|
775 | }
|
---|
776 | break;
|
---|
777 | case MSG_CW_NOK1:
|
---|
778 | case MSG_CW_NOK2:
|
---|
779 | cs_log("cccam: cw nok, sid = %x", cc->cur_sid);
|
---|
780 |
|
---|
781 | int f = 0;
|
---|
782 | LLIST_ITR itr;
|
---|
783 | uint16 *sid = llist_itr_init(cc->cur_card->badsids, &itr);
|
---|
784 | while (sid && !f) {
|
---|
785 | if (*sid == cc->cur_sid) {
|
---|
786 | // llist_itr_release(&itr);
|
---|
787 | f = 1;
|
---|
788 | }
|
---|
789 | sid = llist_itr_next(&itr);
|
---|
790 | }
|
---|
791 | // llist_itr_release(&itr);
|
---|
792 |
|
---|
793 | if (!f) {
|
---|
794 | sid = malloc(sizeof(uint16));
|
---|
795 | if (sid) {
|
---|
796 | *sid = cc->cur_sid;
|
---|
797 |
|
---|
798 | sid = llist_append(cc->cur_card->badsids, sid);
|
---|
799 | cs_debug(" added sid block for card %08x", cc->cur_card->id);
|
---|
800 | }
|
---|
801 | }
|
---|
802 | memset(cc->dcw, 0, 16);
|
---|
803 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
804 | cc_send_ecm(NULL, NULL);
|
---|
805 | ret = 0;
|
---|
806 | break;
|
---|
807 | case MSG_CW_ECM:
|
---|
808 | if (is_server) {
|
---|
809 | ECM_REQUEST *er;
|
---|
810 |
|
---|
811 | cc->cur_card = malloc(sizeof(struct cc_card));
|
---|
812 | if (!cc->cur_card)
|
---|
813 | break;
|
---|
814 | memset(cc->cur_card, 0, sizeof(struct cc_card));
|
---|
815 |
|
---|
816 | cc->cur_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8 | buf[13];
|
---|
817 |
|
---|
818 | if ((er=get_ecmtask())) {
|
---|
819 | er->caid = b2i(2, buf+4);
|
---|
820 | er->srvid = b2i(2, buf+14);
|
---|
821 | er->l = buf[16];
|
---|
822 | memcpy(er->ecm, buf+17, er->l);
|
---|
823 | er->prid = b2i(4, buf+6);
|
---|
824 | get_cw(er);
|
---|
825 | }
|
---|
826 | } else {
|
---|
827 | cc_cw_crypt(buf+4);
|
---|
828 | memcpy(cc->dcw, buf+4, 16);
|
---|
829 | cs_debug("cccam: cws: %s", cs_hexdump(0, cc->dcw, 16));
|
---|
830 | cc_crypt(&cc->block[DECRYPT], buf+4, l-4, ENCRYPT); // additional crypto step
|
---|
831 | pthread_mutex_unlock(&cc->ecm_busy);
|
---|
832 | //cc_abort_user_ecms();
|
---|
833 | cc_send_ecm(NULL, NULL);
|
---|
834 | ret = 0;
|
---|
835 | }
|
---|
836 | break;
|
---|
837 | case MSG_KEEPALIVE:
|
---|
838 | if (!reader[ridx].cc)
|
---|
839 | cs_debug("cccam: keepalive ack");
|
---|
840 | cc_cmd_send(NULL, 0, MSG_KEEPALIVE);
|
---|
841 | cs_debug("cccam: keepalive");
|
---|
842 | break;
|
---|
843 | case MSG_BAD_ECM:
|
---|
844 | //cc->ecm_count = 1;
|
---|
845 | //cs_log("cccam: cmd 0x05 recvd, commencing ecm count");
|
---|
846 | cc_cmd_send(NULL, 0, MSG_BAD_ECM);
|
---|
847 | break;
|
---|
848 | case MSG_CMD_0B:
|
---|
849 | // need to work out algo (reverse) for this...
|
---|
850 | cc_cycle_connection();
|
---|
851 | default:
|
---|
852 | break;
|
---|
853 | }
|
---|
854 |
|
---|
855 | if (cc->ecm_count > CC_MAX_ECMS) cc_cycle_connection();
|
---|
856 |
|
---|
857 | return ret;
|
---|
858 | }
|
---|
859 |
|
---|
860 | static int cc_recv_chk(uchar *dcw, int *rc, uchar *buf)
|
---|
861 | {
|
---|
862 | struct cc_data *cc = reader[ridx].cc;
|
---|
863 |
|
---|
864 | if (buf[1] == MSG_CW_ECM) {
|
---|
865 | memcpy(dcw, cc->dcw, 16);
|
---|
866 | cs_debug("cccam: recv chk - MSG_CW %d - %s", cc->count, cs_hexdump(0, dcw, 16));
|
---|
867 | *rc = 1;
|
---|
868 | return(cc->count);
|
---|
869 | } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) {
|
---|
870 | memset(dcw, 0, 16);
|
---|
871 | return *rc = 0;
|
---|
872 | }
|
---|
873 |
|
---|
874 | return (-1);
|
---|
875 | }
|
---|
876 |
|
---|
877 | static void cc_send_dcw(ECM_REQUEST *er)
|
---|
878 | {
|
---|
879 | uchar buf[16];
|
---|
880 | struct cc_data *cc;
|
---|
881 |
|
---|
882 | memset(buf, 0, sizeof(buf));
|
---|
883 |
|
---|
884 | if(er->rc<=3) {
|
---|
885 | cc = client[cs_idx].cc;
|
---|
886 | memcpy(buf, er->cw, sizeof(buf));
|
---|
887 | cc_cw_crypt(buf);
|
---|
888 | NULLFREE(cc->cur_card);
|
---|
889 | cs_debug("cccam: send cw: er->cpti=%d", er->cpti);
|
---|
890 | cc_cmd_send(buf, 16, MSG_CW_ECM);
|
---|
891 | cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step
|
---|
892 | } else {
|
---|
893 | cs_debug("cccam: send cw NOK: er->cpti=%d!", er->cpti);
|
---|
894 | cc_cmd_send(NULL, 0, MSG_CW_NOK1);
|
---|
895 | }
|
---|
896 | }
|
---|
897 |
|
---|
898 | int cc_recv(uchar *buf, int l)
|
---|
899 | {
|
---|
900 | int n;
|
---|
901 | uchar *cbuf;
|
---|
902 | struct cc_data *cc;
|
---|
903 |
|
---|
904 | if (reader[ridx].cc)
|
---|
905 | cc = reader[ridx].cc;
|
---|
906 | else
|
---|
907 | cc = client[cs_idx].cc;
|
---|
908 |
|
---|
909 | if (buf==NULL) return -1;
|
---|
910 | cbuf = malloc(l);
|
---|
911 | if (cbuf==NULL) return -1;
|
---|
912 |
|
---|
913 | memcpy(cbuf, buf, l); // make a copy of buf
|
---|
914 |
|
---|
915 | pthread_mutex_lock(&cc->lock);
|
---|
916 |
|
---|
917 | n = cc_msg_recv(cbuf); // recv and decrypt msg
|
---|
918 |
|
---|
919 | cs_ddump(cbuf, n, "cccam: received %d bytes from %s", n, remote_txt());
|
---|
920 | client[cs_idx].last = time((time_t *) 0);
|
---|
921 |
|
---|
922 | if (n == 0) {
|
---|
923 | cs_log("cccam: connection closed to %s", remote_txt());
|
---|
924 | n = -1;
|
---|
925 | } else if (n < 4) {
|
---|
926 | cs_log("cccam: packet to small (%d bytes)", n);
|
---|
927 | n = -1;
|
---|
928 | } else {
|
---|
929 | // parse it and write it back, if we have received something of value
|
---|
930 | cc_parse_msg(cbuf, n);
|
---|
931 | memcpy(buf, cbuf, l);
|
---|
932 | }
|
---|
933 |
|
---|
934 | NULLFREE(cbuf);
|
---|
935 |
|
---|
936 | pthread_mutex_unlock(&cc->lock);
|
---|
937 |
|
---|
938 | if (!is_server && (n==-1)) {
|
---|
939 | cs_log("cccam: cycle connection");
|
---|
940 | cc_cycle_connection();
|
---|
941 | //cs_exit(1);
|
---|
942 | }
|
---|
943 |
|
---|
944 | return(n);
|
---|
945 | }
|
---|
946 |
|
---|
947 | static int cc_cli_connect(void)
|
---|
948 | {
|
---|
949 | int handle, n;
|
---|
950 | uint8 data[20];
|
---|
951 | uint8 hash[SHA_DIGEST_LENGTH];
|
---|
952 | uint8 buf[CC_MAXMSGSIZE];
|
---|
953 | char pwd[64];
|
---|
954 | struct cc_data *cc;
|
---|
955 |
|
---|
956 | if (reader[ridx].cc) NULLFREE(reader[ridx].cc);
|
---|
957 |
|
---|
958 | // init internals data struct
|
---|
959 | cc = malloc(sizeof(struct cc_data));
|
---|
960 | if (cc==NULL) {
|
---|
961 | cs_log("cccam: cannot allocate memory");
|
---|
962 | return -1;
|
---|
963 | }
|
---|
964 | reader[ridx].cc = cc;
|
---|
965 | memset(reader[ridx].cc, 0, sizeof(struct cc_data));
|
---|
966 | cc->cards = llist_create();
|
---|
967 |
|
---|
968 | cc->ecm_count = 0;
|
---|
969 |
|
---|
970 | // check cred config
|
---|
971 | if(reader[ridx].device[0] == 0 || reader[ridx].r_pwd[0] == 0 ||
|
---|
972 | reader[ridx].r_usr[0] == 0 || reader[ridx].r_port == 0)
|
---|
973 | return -5;
|
---|
974 |
|
---|
975 | // connect
|
---|
976 | handle = network_tcp_connection_open();
|
---|
977 | if(handle < 0) return -1;
|
---|
978 |
|
---|
979 | // get init seed
|
---|
980 | if((n = recv(handle, data, 16, MSG_WAITALL)) != 16) {
|
---|
981 | cs_log("cccam: server does not return 16 bytes");
|
---|
982 | network_tcp_connection_close(&reader[ridx], handle);
|
---|
983 | return -2;
|
---|
984 | }
|
---|
985 | cs_ddump(data, 16, "cccam: server init seed:");
|
---|
986 |
|
---|
987 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
988 |
|
---|
989 | SHA_CTX ctx;
|
---|
990 | SHA1_Init(&ctx);
|
---|
991 | SHA1_Update(&ctx, data, 16);
|
---|
992 | SHA1_Final(hash, &ctx);
|
---|
993 |
|
---|
994 | cs_ddump(hash, sizeof(hash), "cccam: sha1 hash:");
|
---|
995 |
|
---|
996 | //initialisate crypto states
|
---|
997 | cc_init_crypt(&cc->block[DECRYPT], hash, 20);
|
---|
998 | cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT);
|
---|
999 | cc_init_crypt(&cc->block[ENCRYPT], data, 16);
|
---|
1000 | cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT);
|
---|
1001 |
|
---|
1002 | cc_cmd_send(hash, 20, MSG_NO_HEADER); // send crypted hash to server
|
---|
1003 |
|
---|
1004 | memset(buf, 0, sizeof(buf));
|
---|
1005 | memcpy(buf, reader[ridx].r_usr, strlen(reader[ridx].r_usr));
|
---|
1006 | cs_ddump(buf, 20, "cccam: username '%s':", buf);
|
---|
1007 | cc_cmd_send(buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes
|
---|
1008 |
|
---|
1009 | memset(buf, 0, sizeof(buf));
|
---|
1010 | memset(pwd, 0, sizeof(pwd));
|
---|
1011 |
|
---|
1012 | cs_debug("cccam: 'CCcam' xor");
|
---|
1013 | memcpy(buf, "CCcam", 5);
|
---|
1014 | strncpy(pwd, reader[ridx].r_pwd, sizeof(pwd)-1);
|
---|
1015 | cc_crypt(&cc->block[ENCRYPT], (uint8 *)pwd, strlen(pwd), ENCRYPT);
|
---|
1016 | cc_cmd_send(buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
|
---|
1017 |
|
---|
1018 | if ((n = recv(handle, data, 20, MSG_WAITALL)) != 20) {
|
---|
1019 | cs_log("cccam: login failed, pwd ack not received (n = %d)", n);
|
---|
1020 | return -2;
|
---|
1021 | }
|
---|
1022 | cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT);
|
---|
1023 | cs_ddump(data, 20, "cccam: pwd ack received:");
|
---|
1024 |
|
---|
1025 | if (memcmp(data, buf, 5)) { // check server response
|
---|
1026 | cs_log("cccam: login failed, usr/pwd invalid");
|
---|
1027 | return -2;
|
---|
1028 | } else {
|
---|
1029 | cs_debug("cccam: login succeeded");
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | reader[ridx].tcp_connected = 1;
|
---|
1033 | reader[ridx].last_g = reader[ridx].last_s = time((time_t *)0);
|
---|
1034 |
|
---|
1035 | cs_debug("cccam: last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g);
|
---|
1036 |
|
---|
1037 | pfd=client[cs_idx].udp_fd;
|
---|
1038 |
|
---|
1039 | if (cc_send_cli_data()<=0) {
|
---|
1040 | cs_log("cccam: login failed, could not send client data");
|
---|
1041 | return -3;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | pthread_mutex_init(&cc->lock, NULL);
|
---|
1045 | pthread_mutex_init(&cc->ecm_busy, NULL);
|
---|
1046 |
|
---|
1047 | reader[ridx].caid[0] = reader[ridx].ftab.filts[0].caid;
|
---|
1048 | reader[ridx].nprov = reader[ridx].ftab.filts[0].nprids;
|
---|
1049 | for (n=0; n<reader[ridx].nprov; n++) {
|
---|
1050 | reader[ridx].availkeys[n][0] = 1;
|
---|
1051 | reader[ridx].prid[n][0] = reader[ridx].ftab.filts[0].prids[n] >> 24;
|
---|
1052 | reader[ridx].prid[n][1] = reader[ridx].ftab.filts[0].prids[n] >> 16;
|
---|
1053 | reader[ridx].prid[n][2] = reader[ridx].ftab.filts[0].prids[n] >> 8;
|
---|
1054 | reader[ridx].prid[n][3] = reader[ridx].ftab.filts[0].prids[n] & 0xff;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | return 0;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | static void cc_srv_report_cards()
|
---|
1061 | {
|
---|
1062 | int j;
|
---|
1063 | uint id = 1, r, k;
|
---|
1064 | uint8 hop = 0, reshare, flt=0;
|
---|
1065 | uint8 buf[CC_MAXMSGSIZE];
|
---|
1066 | struct cc_data *cc = client[cs_idx].cc;
|
---|
1067 |
|
---|
1068 | for (r=0; r<CS_MAXREADER; r++)
|
---|
1069 | {
|
---|
1070 | reshare = cfg->cc_reshare;
|
---|
1071 | if (!reshare) continue;
|
---|
1072 |
|
---|
1073 | flt = 0;
|
---|
1074 | if (/*!reader[r].caid[0] && */reader[r].ftab.filts)
|
---|
1075 | {
|
---|
1076 | for (j=0; j<CS_MAXFILTERS; j++)
|
---|
1077 | {
|
---|
1078 | if (reader[r].ftab.filts[j].caid)
|
---|
1079 | {
|
---|
1080 | memset(buf, 0, sizeof(buf));
|
---|
1081 | buf[0] = id >> 24;
|
---|
1082 | buf[1] = id >> 16;
|
---|
1083 | buf[2] = id >> 8;
|
---|
1084 | buf[3] = id & 0xff;
|
---|
1085 | //if (!reader[r].cc_id)
|
---|
1086 | {
|
---|
1087 | buf[6] = 0x49 + r;
|
---|
1088 | buf[7] = 0x10 + j;
|
---|
1089 | reader[r].cc_id = b2i(3, buf+5);
|
---|
1090 | }
|
---|
1091 | //else
|
---|
1092 | //reader[r].cc_id++;
|
---|
1093 | buf[5] = reader[r].cc_id >> 16;
|
---|
1094 | buf[6] = reader[r].cc_id >> 8;
|
---|
1095 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
1096 | buf[8] = reader[r].ftab.filts[j].caid >> 8;
|
---|
1097 | buf[9] = reader[r].ftab.filts[j].caid & 0xff;
|
---|
1098 | buf[10] = hop;
|
---|
1099 | buf[11] = reshare;
|
---|
1100 | buf[20] = reader[r].ftab.filts[j].nprids;
|
---|
1101 | //cs_log("Ident CCcam card report caid: %04X readr %s subid: %06X", reader[r].ftab.filts[j].caid, reader[r].label, reader[r].cc_id);
|
---|
1102 | for (k=0; k<reader[r].ftab.filts[j].nprids; k++)
|
---|
1103 | {
|
---|
1104 | buf[21 + (k*7)] = reader[r].ftab.filts[j].prids[k] >> 16;
|
---|
1105 | buf[22 + (k*7)] = reader[r].ftab.filts[j].prids[k] >> 8;
|
---|
1106 | buf[23 + (k*7)] = reader[r].ftab.filts[j].prids[k] & 0xFF;
|
---|
1107 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
1108 | }
|
---|
1109 | buf[21 + (k*7)] = 1;
|
---|
1110 | memcpy(buf + 22 + (k*7), cc->node_id, 8);
|
---|
1111 | /*
|
---|
1112 | buf[21 + (k*7)+8] = 1;
|
---|
1113 | memcpy(buf + 22 + (k*7)+8, cc->node_id, 7);//8);
|
---|
1114 | cc_cmd_send(buf, 30 + (k*7) + 9, MSG_NEW_CARD);
|
---|
1115 | */
|
---|
1116 | cc_cmd_send(buf, 30 + (k*7), MSG_NEW_CARD);
|
---|
1117 | id++;
|
---|
1118 | flt = 1;
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | if (!reader[r].caid[0] && !flt)
|
---|
1124 | {
|
---|
1125 | flt = 0;
|
---|
1126 | for (j=0; j<CS_MAXCAIDTAB; j++)
|
---|
1127 | {
|
---|
1128 | //cs_log("CAID map CCcam card report caid: %04X cmap: %04X", reader[r].ctab.caid[j], reader[r].ctab.cmap[j]);
|
---|
1129 | ushort lcaid = reader[r].ctab.caid[j];
|
---|
1130 |
|
---|
1131 | if (!lcaid || (lcaid == 0xFFFF))
|
---|
1132 | lcaid = reader[r].ctab.cmap[j];
|
---|
1133 |
|
---|
1134 | if (lcaid && (lcaid != 0xFFFF))
|
---|
1135 | {
|
---|
1136 | memset(buf, 0, sizeof(buf));
|
---|
1137 | buf[0] = id >> 24;
|
---|
1138 | buf[1] = id >> 16;
|
---|
1139 | buf[2] = id >> 8;
|
---|
1140 | buf[3] = id & 0xff;
|
---|
1141 | //if (!reader[r].cc_id)
|
---|
1142 | {
|
---|
1143 | buf[6] = 0x48 + r;
|
---|
1144 | buf[7] = 0x63 + j;
|
---|
1145 | reader[r].cc_id = b2i(3, buf+5);
|
---|
1146 | }
|
---|
1147 | //else
|
---|
1148 | //reader[r].cc_id++;
|
---|
1149 | buf[5] = reader[r].cc_id >> 16;
|
---|
1150 | buf[6] = reader[r].cc_id >> 8;
|
---|
1151 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
1152 | buf[8] = lcaid >> 8;
|
---|
1153 | buf[9] = lcaid & 0xff;
|
---|
1154 | buf[10] = hop;
|
---|
1155 | buf[11] = reshare;
|
---|
1156 | buf[20] = 1;
|
---|
1157 | //cs_log("CAID map CCcam card report caid: %04X nodeid: %s subid: %06X", lcaid, cs_hexdump(0, cc->peer_node_id, 8), reader[r].cc_id);
|
---|
1158 | //buf[21] = 0;
|
---|
1159 | //buf[22] = 0;
|
---|
1160 | //buf[23] = 0;
|
---|
1161 | buf[21+7] = 1;
|
---|
1162 | memcpy(buf + 22+7, cc->node_id, 8);
|
---|
1163 | cc_cmd_send(buf, 30+7, MSG_NEW_CARD);
|
---|
1164 | id++;
|
---|
1165 | flt = 1;
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | if (reader[r].caid[0] && !flt)
|
---|
1171 | {
|
---|
1172 | //cs_log("tcp_connected: %d card_status: %d ", reader[r].tcp_connected, reader[r].card_status);
|
---|
1173 | memset(buf, 0, sizeof(buf));
|
---|
1174 | buf[0] = id >> 24;
|
---|
1175 | buf[1] = id >> 16;
|
---|
1176 | buf[2] = id >> 8;
|
---|
1177 | buf[3] = id & 0xff;
|
---|
1178 | buf[5] = reader[r].cc_id >> 16;
|
---|
1179 | buf[6] = reader[r].cc_id >> 8;
|
---|
1180 | buf[7] = reader[r].cc_id & 0xFF;
|
---|
1181 | if (!reader[r].cc_id)
|
---|
1182 | {
|
---|
1183 | buf[6] = 0x99;
|
---|
1184 | buf[7] = 0x63 + r;
|
---|
1185 | }
|
---|
1186 | buf[8] = reader[r].caid[0] >> 8;
|
---|
1187 | buf[9] = reader[r].caid[0] & 0xff;
|
---|
1188 | buf[10] = hop;
|
---|
1189 | buf[11] = reshare;
|
---|
1190 | buf[20] = reader[r].nprov;
|
---|
1191 | for (j=0; j<reader[r].nprov; j++)
|
---|
1192 | {
|
---|
1193 | if (reader[r].card_status == CARD_INSERTED)
|
---|
1194 | memcpy(buf + 21 + (j*7), reader[r].prid[j]+1, 3);
|
---|
1195 | else
|
---|
1196 | memcpy(buf + 21 + (j*7), reader[r].prid[j], 3);
|
---|
1197 | //cs_log("Main CCcam card report provider: %02X%02X%02X%02X", buf[21+(j*7)], buf[22+(j*7)], buf[23+(j*7)], buf[24+(j*7)]);
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | buf[21 + (j*7)] = 1;
|
---|
1201 | memcpy(buf + 22 + (j*7), cc->node_id, 8);
|
---|
1202 | id++;
|
---|
1203 |
|
---|
1204 | if ((reader[r].tcp_connected || reader[r].card_status == CARD_INSERTED) /*&& !reader[r].cc_id*/)
|
---|
1205 | {
|
---|
1206 | reader[r].cc_id = b2i(3, buf+5);
|
---|
1207 | cc_cmd_send(buf, 30 + (j*7), MSG_NEW_CARD);
|
---|
1208 | //cs_log("CCcam: local card or newcamd reader %02X report ADD caid: %02X%02X %d %d %s subid: %06X", buf[7], buf[8], buf[9], reader[r].card_status, reader[r].tcp_connected, reader[r].label, reader[r].cc_id);
|
---|
1209 | }
|
---|
1210 | else
|
---|
1211 | if ((reader[r].card_status != CARD_INSERTED) && (!reader[r].tcp_connected) && reader[r].cc_id)
|
---|
1212 | {
|
---|
1213 | reader[r].cc_id = 0;
|
---|
1214 | cc_cmd_send(buf, 30 + (j*7), MSG_CARD_REMOVED);
|
---|
1215 | //cs_log("CCcam: local card or newcamd reader %02X report REMOVE caid: %02X%02X %s", buf[7], buf[8], buf[9], reader[r].label);
|
---|
1216 | }
|
---|
1217 | }
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | static int cc_srv_connect()
|
---|
1222 | {
|
---|
1223 | int i;
|
---|
1224 | ulong cmi;
|
---|
1225 | uint seed;
|
---|
1226 | uint8 buf[CC_MAXMSGSIZE];
|
---|
1227 | uint8 data[16];
|
---|
1228 | char usr[20], pwd[20];
|
---|
1229 | struct s_auth *account;
|
---|
1230 | struct cc_data *cc;
|
---|
1231 |
|
---|
1232 | memset(usr, 0, sizeof(usr));
|
---|
1233 | memset(pwd, 0, sizeof(pwd));
|
---|
1234 |
|
---|
1235 | if (client[cs_idx].cc) NULLFREE(client[cs_idx].cc);
|
---|
1236 |
|
---|
1237 | // init internals data struct
|
---|
1238 | cc = malloc(sizeof(struct cc_data));
|
---|
1239 | if (cc==NULL) {
|
---|
1240 | cs_log("cccam: cannot allocate memory");
|
---|
1241 | return -1;
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | client[cs_idx].cc = cc;
|
---|
1245 | memset(client[cs_idx].cc, 0, sizeof(struct cc_data));
|
---|
1246 |
|
---|
1247 | // calc + send random seed
|
---|
1248 | seed = (unsigned int) time((time_t*)0);
|
---|
1249 | for(i=0; i<16; i++ ) data[i]=fast_rnd();
|
---|
1250 | send(client[cs_idx].udp_fd, data, 16, 0);
|
---|
1251 |
|
---|
1252 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
1253 |
|
---|
1254 | SHA_CTX ctx;
|
---|
1255 | SHA1_Init(&ctx);
|
---|
1256 | SHA1_Update(&ctx, data, 16);
|
---|
1257 | SHA1_Final(buf, &ctx);
|
---|
1258 |
|
---|
1259 | //initialisate crypto states
|
---|
1260 | /*
|
---|
1261 | init_crypt(&cc->block[ENCRYPT], buf, 20);
|
---|
1262 | crypto_state__decrypt(&cc->block[ENCRYPT], data, data2, 16);
|
---|
1263 | init_crypt(&cc->block[DECRYPT], data2, 16);
|
---|
1264 | crypto_state__encrypt(&cc->block[DECRYPT], buf, buf2, 20);*/
|
---|
1265 |
|
---|
1266 | cc_init_crypt(&cc->block[ENCRYPT], buf, 20);
|
---|
1267 | cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT);
|
---|
1268 | cc_init_crypt(&cc->block[DECRYPT], data, 16);
|
---|
1269 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
1270 |
|
---|
1271 | if ((i=recv(pfd, buf, 20, MSG_WAITALL)) == 20) {
|
---|
1272 | cs_ddump(buf, 20, "cccam: recv:");
|
---|
1273 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
1274 | cs_ddump(buf, 20, "cccam: hash:");
|
---|
1275 | } else return -1;
|
---|
1276 |
|
---|
1277 | // receive username
|
---|
1278 | if ((i=recv(pfd, buf, 20, MSG_WAITALL)) == 20) {
|
---|
1279 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
1280 | cs_ddump(buf, 20, "cccam: username '%s':", buf);
|
---|
1281 | strncpy(usr, (char *)buf, sizeof(usr));
|
---|
1282 | } else return -1;
|
---|
1283 |
|
---|
1284 | for (account=cfg->account; account; account=account->next)
|
---|
1285 | if (strcmp(usr, account->usr) == 0) {
|
---|
1286 | strncpy(pwd, account->pwd, sizeof(pwd));
|
---|
1287 | break;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | // receive passwd / 'CCcam'
|
---|
1291 | cc_crypt(&cc->block[DECRYPT], (uint8 *)pwd, strlen(pwd), DECRYPT);
|
---|
1292 | if ((i=recv(pfd, buf, 6, MSG_WAITALL)) == 6) {
|
---|
1293 | cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT);
|
---|
1294 | cs_ddump(buf, 6, "cccam: pwd check '%s':", buf);
|
---|
1295 | } else return -1;
|
---|
1296 |
|
---|
1297 | cs_auth_client(account, NULL);
|
---|
1298 | //cs_auth_client((struct s_auth *)(-1), NULL);
|
---|
1299 |
|
---|
1300 | // send passwd ack
|
---|
1301 | memset(buf, 0, 20);
|
---|
1302 | memcpy(buf, "CCcam\0", 6);
|
---|
1303 | cs_ddump(buf, 20, "cccam: send ack:");
|
---|
1304 | cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT);
|
---|
1305 | send(pfd, buf, 20, 0);
|
---|
1306 |
|
---|
1307 | // recv cli data
|
---|
1308 | memset(buf, 0, sizeof(buf));
|
---|
1309 | i = cc_msg_recv(buf);
|
---|
1310 | cs_ddump(buf, i, "cccam: cli data:");
|
---|
1311 | memcpy(cc->peer_node_id, buf+24, 8);
|
---|
1312 | cs_log("cccam: client '%s' (%s) running v%s (%s)", buf+4, cs_hexdump(0, cc->peer_node_id, 8), buf+33, buf+65);
|
---|
1313 |
|
---|
1314 | // send cli data ack
|
---|
1315 | cc_cmd_send(NULL, 0, MSG_CLI_DATA);
|
---|
1316 |
|
---|
1317 | if (cc_send_srv_data()<0) return -1;
|
---|
1318 |
|
---|
1319 | is_server = 1;
|
---|
1320 |
|
---|
1321 | // report cards
|
---|
1322 | cc_srv_report_cards();
|
---|
1323 |
|
---|
1324 | cmi = 0;
|
---|
1325 | // check for clienttimeout, if timeout occurs try to send keepalive
|
---|
1326 | for (;;) {
|
---|
1327 | i=process_input(mbuf, sizeof(mbuf), 10); //cfg->cmaxidle);
|
---|
1328 | if (i == -9) {
|
---|
1329 | cc_srv_report_cards();
|
---|
1330 | cmi += 10;
|
---|
1331 | if (cmi >= cfg->cmaxidle) {
|
---|
1332 | cmi = 0;
|
---|
1333 | if (cc_cmd_send(NULL, 0, MSG_KEEPALIVE) > 0) {
|
---|
1334 | cs_debug("cccam: keepalive after maxidle is reached");
|
---|
1335 | i = 1;
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 | } else if (i <= 0) break;
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | cs_disconnect_client();
|
---|
1342 |
|
---|
1343 | return 0;
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | void cc_srv_init()
|
---|
1347 | {
|
---|
1348 | pfd=client[cs_idx].udp_fd;
|
---|
1349 | //cc_auth_client(client[cs_idx].ip);
|
---|
1350 | if (cc_srv_connect() < 0)
|
---|
1351 | cs_log("cccam:%d failed errno: %d (%s)", __LINE__, errno, strerror(errno));
|
---|
1352 | cs_exit(0);
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | int cc_cli_init()
|
---|
1356 | {
|
---|
1357 | if (!reader[ridx].tcp_connected) {
|
---|
1358 | static struct sockaddr_in loc_sa;
|
---|
1359 | struct protoent *ptrp;
|
---|
1360 | int p_proto;
|
---|
1361 |
|
---|
1362 | pfd=0;
|
---|
1363 | if (reader[ridx].r_port<=0)
|
---|
1364 | {
|
---|
1365 | cs_log("cccam: invalid port %d for server %s", reader[ridx].r_port, reader[ridx].device);
|
---|
1366 | return(1);
|
---|
1367 | }
|
---|
1368 | if( (ptrp=getprotobyname("tcp")) )
|
---|
1369 | p_proto=ptrp->p_proto;
|
---|
1370 | else
|
---|
1371 | p_proto=6;
|
---|
1372 |
|
---|
1373 | client[cs_idx].ip=0;
|
---|
1374 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
1375 | loc_sa.sin_family = AF_INET;
|
---|
1376 | #ifdef LALL
|
---|
1377 | if (cfg->serverip[0])
|
---|
1378 | loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
1379 | else
|
---|
1380 | #endif
|
---|
1381 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
1382 | loc_sa.sin_port = htons(reader[ridx].l_port);
|
---|
1383 |
|
---|
1384 | if ((client[cs_idx].udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0)
|
---|
1385 | {
|
---|
1386 | cs_log("cccam: Socket creation failed (errno=%d)", errno);
|
---|
1387 | cs_exit(1);
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | #ifdef SO_PRIORITY
|
---|
1391 | if (cfg->netprio)
|
---|
1392 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
1393 | (void *)&cfg->netprio, sizeof(ulong));
|
---|
1394 | #endif
|
---|
1395 | if (!reader[ridx].tcp_ito) {
|
---|
1396 | ulong keep_alive = reader[ridx].tcp_ito?1:0;
|
---|
1397 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
1398 | (void *)&keep_alive, sizeof(ulong));
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | memset((char *)&client[cs_idx].udp_sa,0,sizeof(client[cs_idx].udp_sa));
|
---|
1402 | client[cs_idx].udp_sa.sin_family = AF_INET;
|
---|
1403 | client[cs_idx].udp_sa.sin_port = htons((u_short)reader[ridx].r_port);
|
---|
1404 |
|
---|
1405 | struct hostent *server;
|
---|
1406 | server = gethostbyname(reader[ridx].device);
|
---|
1407 | memmove((char *)&client[cs_idx].udp_sa.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
|
---|
1408 |
|
---|
1409 | if (reader[ridx].tcp_rto <= 0) reader[ridx].tcp_rto = 60 * 60 * 10; // timeout to 10 hours
|
---|
1410 | cs_debug("cccam: reconnect timeout set to: %d", reader[ridx].tcp_rto);
|
---|
1411 | if (!reader[ridx].cc_maxhop) reader[ridx].cc_maxhop = 5; // default maxhop to 5 if not configured
|
---|
1412 | cc_check_version (reader[ridx].cc_version, reader[ridx].cc_build);
|
---|
1413 | cs_log ("proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d", reader[ridx].label,
|
---|
1414 | reader[ridx].device, reader[ridx].r_port,
|
---|
1415 | reader[ridx].cc_version, reader[ridx].cc_build, reader[ridx].cc_maxhop);
|
---|
1416 | cc_cli_connect();
|
---|
1417 |
|
---|
1418 | return(0);
|
---|
1419 | }
|
---|
1420 | return(-1);
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | void cc_cleanup(void)
|
---|
1424 | {
|
---|
1425 |
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | void module_cccam(struct s_module *ph)
|
---|
1429 | {
|
---|
1430 | strcpy(ph->desc, "cccam");
|
---|
1431 | ph->type=MOD_CONN_TCP;
|
---|
1432 | ph->logtxt = ", crypted";
|
---|
1433 | ph->watchdog=1;
|
---|
1434 | ph->recv=cc_recv;
|
---|
1435 | ph->cleanup=cc_cleanup;
|
---|
1436 | ph->c_multi=1;
|
---|
1437 | ph->c_init=cc_cli_init;
|
---|
1438 | ph->c_recv_chk=cc_recv_chk;
|
---|
1439 | ph->c_send_ecm=cc_send_ecm;
|
---|
1440 | ph->s_ip=cfg->cc_srvip;
|
---|
1441 | ph->s_handler=cc_srv_init;
|
---|
1442 | ph->send_dcw=cc_send_dcw;
|
---|
1443 |
|
---|
1444 | static PTAB ptab;
|
---|
1445 | ptab.ports[0].s_port = cfg->cc_port;
|
---|
1446 | ph->ptab = &ptab;
|
---|
1447 | ph->ptab->nports = 1;
|
---|
1448 | }
|
---|