Changeset 1926 for trunk/module-cccam.c
- Timestamp:
- 04/02/10 16:21:14 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/module-cccam.c
r1925 r1926 1 1 #include "globals.h" 2 2 extern struct s_reader *reader; 3 3 4 4 int g_flag = 0; … … 12 12 #include <pthread.h> 13 13 14 struct llist_node 15 { 14 struct llist_node { 16 15 void *obj; 17 16 struct llist_node *prv; … … 19 18 }; 20 19 21 typedef struct llist 22 { 20 typedef struct llist { 23 21 struct llist_node *first; 24 22 struct llist_node *last; … … 27 25 } LLIST; 28 26 29 typedef struct llist_itr 30 { 27 typedef struct llist_itr { 31 28 LLIST *l; 32 29 struct llist_node *cur; … … 35 32 LLIST *llist_create(void); // init linked list 36 33 void llist_destroy(LLIST *l); // de-init linked list - frees all objects on the list 34 37 35 void *llist_append(LLIST *l, void *o); // append object onto bottom of list, returns ptr to obj 36 38 37 void *llist_itr_init(LLIST *l, LLIST_ITR *itr); // linked list iterator, returns ptr to first obj 39 38 //void llist_itr_release(LLIST_ITR *itr); // release iterator 40 39 void *llist_itr_next(LLIST_ITR *itr); // iterates, returns ptr to next obj 40 41 41 void *llist_itr_insert(LLIST_ITR *itr, void *o); // insert object at itr point, iterates to and returns ptr to new obj 42 42 void *llist_itr_remove(LLIST_ITR *itr); // remove obj at itr, iterates to and returns ptr to next obj 43 43 44 int llist_count(LLIST *l); // returns number of obj in list 44 45 … … 47 48 #include <string.h> 48 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 /******************************** */ 49 182 50 183 #define CC_MAXMSGSIZE 512 … … 53 186 54 187 #define SWAPC(X, Y) do { char p; p = *X; *X = *Y; *Y = p; } while(0) 55 #define NULLFREE(X) do { if (X) { free(X); X = NULL; } } while(0)56 57 int cc_cli_init();58 59 LLIST *llist_create(void)60 {61 LLIST *l = calloc(1, sizeof(LLIST));62 if (!l) return NULL;63 //memset(l, 0, sizeof(LLIST));64 pthread_mutex_init(&l->lock, NULL);65 //l->items = 0; //stuct is zeroed66 return l;67 }68 69 void llist_destroy(LLIST *l)70 {71 LLIST_ITR itr;72 void *o;73 74 if (!l) return;75 76 o = llist_itr_init(l, &itr);77 while (o)78 {79 free(o);80 o = llist_itr_remove(&itr);81 }82 }83 84 void *llist_append(LLIST *l, void *o)85 {86 //pthread_mutex_lock(&l->lock);87 if (l && o)88 {89 struct llist_node *ln;90 91 pthread_mutex_lock(&l->lock);92 ln = calloc(1, sizeof(struct llist_node));93 if (!ln)94 {95 pthread_mutex_unlock(&l->lock);96 return NULL;97 }98 //memset(ln, 0, sizeof(struct llist_node));99 ln->obj = o;100 101 if (l->last)102 {103 ln->prv = l->last;104 ln->prv->nxt = ln;105 }106 else107 {108 l->first = ln;109 }110 l->last = ln;111 l->items++;112 pthread_mutex_unlock(&l->lock);113 return o;114 }115 //pthread_mutex_unlock(&l->lock);116 return NULL;117 }118 119 void *llist_itr_init(LLIST *l, LLIST_ITR *itr)120 {121 if (!l || !itr) return NULL;122 // pthread_mutex_lock(&l->lock);123 if (l->first)124 {125 memset(itr, 0, sizeof(LLIST_ITR));126 itr->cur = l->first;127 itr->l = l;128 return itr->cur->obj;129 }130 return NULL;131 }132 133 /*134 void llist_itr_release(LLIST_ITR *itr)135 {136 // pthread_mutex_unlock(&itr->l->lock);137 }138 */139 140 void *llist_itr_next(LLIST_ITR *itr)141 {142 if (itr->cur->nxt)143 {144 itr->cur = itr->cur->nxt;145 return itr->cur->obj;146 }147 148 return NULL;149 }150 151 void *llist_itr_remove(LLIST_ITR *itr) // this needs cleaning - I was lazy152 {153 if (!itr || !itr->l || itr->l->items==0) return NULL;154 itr->l->items--;155 156 if ((itr->cur == itr->l->first) && (itr->cur == itr->l->last))157 {158 NULLFREE(itr->cur);159 itr->l->first = NULL;160 itr->l->last = NULL;161 //itr->cur = NULL;162 return NULL;163 }164 else165 if (itr->cur == itr->l->first)166 {167 struct llist_node *nxt = itr->cur->nxt;168 NULLFREE(itr->cur);169 nxt->prv = NULL;170 itr->l->first = nxt;171 itr->cur = nxt;172 }173 else174 if (itr->cur == itr->l->last)175 {176 itr->l->last = itr->cur->prv;177 itr->l->last->nxt = NULL;178 NULLFREE(itr->cur);179 //itr->cur = NULL;180 return NULL;181 }182 else183 {184 struct llist_node *nxt = itr->cur->nxt;185 itr->cur->prv->nxt = itr->cur->nxt;186 itr->cur->nxt->prv = itr->cur->prv;187 NULLFREE(itr->cur);188 itr->cur = nxt;189 }190 191 return itr->cur->obj;192 }193 194 int llist_count(LLIST *l)195 {196 return l->items;197 }198 199 /******************************** */200 201 188 202 189 #if (defined(WIN32) || defined(OS_CYGWIN32)) && !defined(MSG_WAITALL) … … 232 219 }; 233 220 234 struct cc_card 235 { 221 struct cc_card { 236 222 uint32 id; // cccam card (share) id 237 uint32 sub_id; 223 uint32 sub_id; // subshare id 238 224 uint16 caid; 239 225 uint8 hop; … … 243 229 }; 244 230 245 struct cc_data 246 { 231 struct cc_data { 247 232 struct cc_crypt_block block[2]; // crypto state blocks 233 248 234 uint8 node_id[8], // client node id 249 peer_node_id[8], // server node id250 dcw[16]; // control words235 peer_node_id[8], // server node id 236 dcw[16]; // control words 251 237 252 238 struct cc_card *cur_card; // ptr to selected card … … 260 246 ECM_REQUEST *found; 261 247 262 unsigned intcrc;248 unsigned long crc; 263 249 264 250 pthread_mutex_t lock; … … 278 264 static void cc_init_crypt(struct cc_crypt_block *block, uint8 *key, int len) 279 265 { 280 int i = 0 ; 281 uint8 j = 0; 282 283 for (i=0; i<256; i++) 284 { 285 block->keytable[i] = i; 286 } 287 288 for (i=0; i<256; i++) 289 { 290 j += key[i % len] + block->keytable[i]; 291 SWAPC(&block->keytable[i], &block->keytable[j]); 292 } 293 294 block->state = *key; 295 block->counter=0; 296 block->sum=0; 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; 297 281 } 298 282 299 283 static void cc_crypt(struct cc_crypt_block *block, uint8 *data, int len, cc_crypt_mode_t mode) 300 284 { 301 int i; 302 uint8 z; 303 304 for (i = 0; i < len; i++) 305 { 306 block->counter++; 307 block->sum += block->keytable[block->counter]; 308 SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]); 309 z = data[i]; 310 data[i] = z ^ block->keytable[(block->keytable[block->counter] + block->keytable[block->sum]) & 0xff] ^ block->state; 311 if (!mode) 312 z = data[i]; 313 block->state = block->state ^ z; 314 } 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 } 315 297 } 316 298 317 299 static void cc_xor(uint8 *buf) 318 300 { 319 const char cccam[] = "CCcam"; 320 uint8 i; 321 322 for ( i = 0; i < 8; i++ ) 323 { 324 buf[8 + i] = i * buf[i]; 325 if ( i <= 5 ) 326 { 327 buf[i] ^= cccam[i]; 328 } 329 } 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 } 330 310 } 331 311 332 312 static void cc_cw_crypt(uint8 *cws) 333 313 { 334 struct cc_data *cc; 335 uint64 node_id; 336 uint8 tmp; 337 int i; 338 339 if (reader[ridx].cc) 340 { 341 cc = reader[ridx].cc; 342 node_id = b2ll(8, cc->node_id); 343 } 344 else 345 { 346 cc = client[cs_idx].cc; 347 node_id = b2ll(8, cc->peer_node_id); 348 } 349 350 for (i = 0; i < 16; i++) 351 { 352 tmp = cws[i] ^ (node_id >> (4 * i)); 353 if (i & 1) 354 tmp = ~tmp; 355 cws[i] = (cc->cur_card->id >> (2 * i)) ^ tmp; 356 } 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 } 357 333 } 358 334 359 335 static void cc_cycle_connection() 360 336 { 361 362 363 364 pfd=0;365 366 367 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(); 368 344 } 369 345 370 346 static int cc_msg_recv(uint8 *buf) 371 347 { 372 int len, handle; 373 uint8 netbuf[CC_MAXMSGSIZE+4]; 374 struct cc_data *cc; 375 376 if (reader[ridx].cc) 377 cc = reader[ridx].cc; 378 else 379 cc = client[cs_idx].cc; 380 381 handle = client[cs_idx].udp_fd; 382 if (handle < 0) 383 return -1; 384 385 len = recv(handle, netbuf, 4, MSG_WAITALL); 386 if (!len) 387 return 0; 388 if (len != 4) 389 { // invalid header length read 390 cs_log("cccam: invalid header length"); 391 return -1; 392 } 393 394 cc_crypt(&cc->block[DECRYPT], netbuf, 4, DECRYPT); 395 //cs_ddump(netbuf, 4, "cccam: decrypted header:"); 396 397 g_flag = netbuf[0]; 398 399 if (((netbuf[2] << 8) | netbuf[3]) != 0) 400 { // check if any data is expected in msg 401 if (((netbuf[2] << 8) | netbuf[3]) > CC_MAXMSGSIZE - 2) 402 { 403 cs_log("cccam: message too big"); 404 return -1; 405 } 406 407 len = recv(handle, netbuf+4, (netbuf[2] << 8) | netbuf[3], MSG_WAITALL); // read rest of msg 408 if (len != ((netbuf[2] << 8) | netbuf[3])) 409 { 410 cs_log("cccam: invalid message length read"); 411 return -1; 412 } 413 414 cc_crypt(&cc->block[DECRYPT], netbuf+4, len, DECRYPT); 415 len += 4; 416 } 417 418 cs_ddump(netbuf, len, "cccam: full decrypted msg, len=%d:", len); 419 memcpy(buf, netbuf, len); 420 return len; 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; 421 396 } 422 397 423 398 static int cc_cmd_send(uint8 *buf, int len, cc_msg_type_t cmd) 424 399 { 425 int n; 426 struct cc_data *cc; 427 //uint8 *netbuf = calloc(1, len+4); 428 uint8 netbuf[len+4]; 429 430 if (reader[ridx].cc) 431 cc = reader[ridx].cc; 432 else 433 cc = client[cs_idx].cc; 434 435 memset(netbuf, 0, len+4); 436 437 if (cmd == MSG_NO_HEADER) 438 { 439 memcpy(netbuf, buf, len); 440 } 441 else 442 { 443 // build command message 444 netbuf[0] = g_flag; // flags?? 445 netbuf[1] = cmd & 0xff; 446 netbuf[2] = len >> 8; 447 netbuf[3] = len & 0xff; 448 if (buf) 449 memcpy(netbuf+4, buf, len); 450 len += 4; 451 } 452 453 cs_ddump(netbuf, len, "cccam: send:"); 454 cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT); 455 456 n = send(client[cs_idx].udp_fd, netbuf, len, 0); 457 //NULLFREE(netbuf); 458 459 return n; 460 } 461 462 #define CC_DEFAULT_VERSION 1 463 static void cc_check_version(char *cc_version, char *cc_build) 464 { 465 char *version[] = {"2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", ""}; 466 char *build[] = {"2892", "2971", "3094", "3165", "3191", ""}; 467 int i; 468 469 if (strlen(cc_version) == 0) 470 { 471 memcpy(cc_version, version[CC_DEFAULT_VERSION], strlen(version[CC_DEFAULT_VERSION])); 472 memcpy(cc_build, build[CC_DEFAULT_VERSION], strlen(build[CC_DEFAULT_VERSION])); 473 cs_debug("cccam: auto version set: %s build: %s", cc_version, cc_build); 474 return; 475 } 476 477 for (i=0; strlen(version[i]); i++) 478 if (!memcmp(cc_version, version[i], strlen(version[i]))) 479 { 480 memcpy(cc_build, build[i], strlen(build[i])); 481 cs_debug("cccam: auto build set for version: %s build: %s", cc_version, cc_build); 482 break; 483 } 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 } 484 451 } 485 452 486 453 static int cc_send_cli_data() 487 454 { 488 int i; 489 struct cc_data *cc = reader[ridx].cc; 490 491 cs_debug("cccam: send client data"); 492 493 seed = (unsigned int) time((time_t*)0); 494 for( i=0; i<8; i++ ) 495 cc->node_id[i]=fast_rnd(); 496 497 uint8 buf[CC_MAXMSGSIZE]; 498 memset(buf, 0, CC_MAXMSGSIZE); 499 500 memcpy(buf, reader[ridx].r_usr, sizeof(reader[ridx].r_usr)); 501 memcpy(buf + 20, cc->node_id, 8 ); 502 503 504 memcpy(buf + 29, reader[ridx].cc_version, sizeof(reader[ridx].cc_version)); // cccam version (ascii) 505 memcpy(buf + 61, reader[ridx].cc_build, sizeof(reader[ridx].cc_build)); // build number (ascii) 506 507 cs_log ("cccam: user: %s, version: %s, build: %s", reader[ridx].r_usr, reader[ridx].cc_version, reader[ridx].cc_build); 508 509 i = cc_cmd_send(buf, 20 + 8 + 6 + 26 + 4 + 28 + 1, MSG_CLI_DATA); 510 511 return i; 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; 512 476 } 513 477 514 478 static int cc_send_srv_data() 515 479 { 516 int i; 517 struct cc_data *cc = client[cs_idx].cc; 518 519 cs_debug("cccam: send server data"); 520 521 seed = (unsigned int) time((time_t*)0); 522 for( i=0; i<8; i++ ) cc->node_id[i]=fast_rnd(); 523 524 uint8 buf[CC_MAXMSGSIZE]; 525 memset(buf, 0, CC_MAXMSGSIZE); 526 527 memcpy(buf, cc->node_id, 8 ); 528 529 cc_check_version((char *)cfg->cc_version, (char *)cfg->cc_build); 530 531 memcpy(buf + 8, cfg->cc_version, sizeof(reader[ridx].cc_version)); // cccam version (ascii) 532 memcpy(buf + 40, cfg->cc_build, sizeof(reader[ridx].cc_build)); // build number (ascii) 533 534 cs_log ("cccam: version: %s, build: %s nodeid: %s", cfg->cc_version, cfg->cc_build, cs_hexdump(0, cc->peer_node_id, 8)); 535 536 return cc_cmd_send(buf, 0x48, MSG_SRV_DATA); 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); 537 499 } 538 500 539 501 static int cc_get_nxt_ecm() 540 502 { 541 int n, i; 542 time_t t; 543 544 t=time((time_t *)0); 545 for (i = 1, n = 1; i < CS_MAXPENDING; i++) 546 { 547 if ((t-(ulong)ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) && 548 (ecmtask[i].rc >= 10)) // drop timeouts 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 549 512 { 550 513 ecmtask[i].rc=0; 551 514 } 552 515 553 if (ecmtask[i].rc >= 10) // stil active and waiting 554 { 555 // search for the ecm with the lowest time, this should be the next to go 556 if ((!n || ecmtask[n].tps.time-ecmtask[i].tps.time < 0) && &ecmtask[n]) n = i; 557 } 558 } 559 return n; 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; 560 522 } 561 523 562 524 static int cc_send_ecm(ECM_REQUEST *er, uchar *buf) 563 525 { 564 int n, h = -1; 565 struct cc_data *cc = reader[ridx].cc; 566 struct cc_card *card; 567 LLIST_ITR itr; 568 ECM_REQUEST *cur_er; 569 570 if (cc->ecm_count) cc->ecm_count++; 571 572 if (!cc || (pfd < 1)) 573 { 574 if (er) 575 { 576 er->rc = 0; 577 er->rcEx = 0x27; 578 cs_log("cccam: server not init!"); 579 write_ecm_answer(fd_c2m, er); 580 } 581 return -1; 582 } 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 } 583 543 584 544 // pthread_mutex_lock(&cc->ecm_busy); 585 if (pthread_mutex_trylock(&cc->ecm_busy) == EBUSY) 586 { 587 cs_debug("cccam: ecm trylock: failed to get lock"); 588 return 0; 589 } 590 else 591 { 592 //cs_debug("cccam: ecm trylock: got lock"); 593 } 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 } 594 551 // pthread_mutex_lock(&cc->lock); 595 552 596 n = cc_get_nxt_ecm(); 597 if (n < 0) 598 { 599 pthread_mutex_unlock(&cc->ecm_busy); 600 pthread_mutex_unlock(&cc->lock); 601 return 0; // no queued ecms 602 } 603 cur_er = &ecmtask[n]; 604 if (crc32(0, cur_er->ecm, cur_er->l) == cc->crc) cur_er->rc = 99; 605 cc->crc = crc32(0, cur_er->ecm, cur_er->l); 606 607 608 609 610 if (cur_er->rc == 99) 611 { 612 pthread_mutex_unlock(&cc->ecm_busy); 613 pthread_mutex_unlock(&cc->lock); 614 return 0; // ecm already sent 615 } 616 617 //cc->found = cur_er; 618 619 if (buf) 620 memcpy(buf, cur_er->ecm, cur_er->l); 621 622 cc->cur_card = NULL; 623 cc->cur_sid = cur_er->srvid; 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; 624 648 625 649 card = llist_itr_init(cc->cards, &itr); 626 627 while (card) 628 { 629 if (card->caid == cur_er->caid) 630 { // caid matches 631 int s = 0; 632 LLIST_ITR sitr; 633 uint16 *sid = llist_itr_init(card->badsids, &sitr); 634 635 while (sid) 636 { 637 if (*sid == cc->cur_sid) 638 { 639 s = 1; 640 break; 641 } 642 sid = llist_itr_next(&sitr); 643 } 644 645 LLIST_ITR pitr; 646 uint8 *prov = llist_itr_init(card->provs, &pitr); 647 while (prov && !s) 648 { 649 if (!cur_er->prid || b2i(3, prov) == cur_er->prid) // provid matches 650 { 651 if (((h < 0) || (card->hop < h)) && (card->hop <= reader[ridx].cc_maxhop - 1)) // card is closer and doesn't exceed max hop 652 { 653 cc->cur_card = card; 654 h = card->hop; // card has been matched 655 } 656 } 657 prov = llist_itr_next(&pitr); 658 } 659 } 660 card = llist_itr_next(&itr); 661 } 662 663 if (cc->cur_card) 664 { 665 //uint8 *ecmbuf = calloc(1, cur_er->l+13); 666 //if (ecmbuf==NULL) return -1; 667 uint8 ecmbuf[CC_MAXMSGSIZE]; 668 //memset(ecmbuf, 0, cur_er->l+13); 669 // build ecm message 670 ecmbuf[0] = cc->cur_card->caid >> 8; 671 ecmbuf[1] = cc->cur_card->caid & 0xff; 672 ecmbuf[2] = cur_er->prid >> 24; 673 ecmbuf[3] = cur_er->prid >> 16; 674 ecmbuf[4] = cur_er->prid >> 8; 675 ecmbuf[5] = cur_er->prid & 0xff; 676 ecmbuf[6] = cc->cur_card->id >> 24; 677 ecmbuf[7] = cc->cur_card->id >> 16; 678 ecmbuf[8] = cc->cur_card->id >> 8; 679 ecmbuf[9] = cc->cur_card->id & 0xff; 680 ecmbuf[10] = cur_er->srvid >> 8; 681 ecmbuf[11] = cur_er->srvid & 0xff; 682 ecmbuf[12] = cur_er->l & 0xff; 683 memcpy(ecmbuf+13, cur_er->ecm, cur_er->l); 684 685 cc->count = cur_er->idx; 686 reader[ridx].cc_currenthops = cc->cur_card->hop + 1; 687 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); 688 n = cc_cmd_send(ecmbuf, cur_er->l+13, MSG_CW_ECM); // send ecm 689 690 //NULLFREE(ecmbuf); 691 } 692 else 693 { 694 n = -1; 695 cs_log("cccam: no suitable card on server for caid %04X&%06X sid %04X", cur_er->caid, cur_er->prid, cur_er->srvid); 696 cur_er->rc = 0; 697 cur_er->rcEx = 0x27; 698 699 cs_sleepms(300); 700 write_ecm_answer(fd_c2m, cur_er); 701 //reader[ridx].last_s = reader[ridx].last_g; 702 703 card = llist_itr_init(cc->cards, &itr); 704 while (card) 705 { 706 if (card->caid == cur_er->caid) 707 { // caid matches 708 LLIST_ITR sitr; 709 uint16 *sid = llist_itr_init(card->badsids, &sitr); 710 while (sid) 711 { 712 if (*sid == cur_er->srvid) 713 sid = llist_itr_remove(&sitr); 714 else 715 sid = llist_itr_next(&sitr); 716 } 717 } 718 card = llist_itr_next(&itr); 719 } 720 pthread_mutex_unlock(&cc->ecm_busy); 721 } 722 723 return 0; 724 } 725 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 } 726 670 /* 727 671 static int cc_abort_user_ecms(){ … … 753 697 static cc_msg_type_t cc_parse_msg(uint8 *buf, int l) 754 698 { 755 int ret = buf[1]; 756 struct cc_data *cc; 757 758 if (reader[ridx].cc) 759 cc = reader[ridx].cc; 760 else 761 cc = client[cs_idx].cc; 762 763 switch (buf[1]) 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: 764 716 { 765 case MSG_CLI_DATA: 766 cs_debug("cccam: client data ack"); 767 break; 768 case MSG_SRV_DATA: 769 memcpy(cc->peer_node_id, buf+4, 8); 770 cs_log("cccam: srv %s running v%s (%s)", cs_hexdump(0, cc->peer_node_id, 8), buf+12, buf+44); 771 break; 772 case MSG_NEW_CARD: 773 { 774 int i = 0; 775 struct cc_card *card; 776 777 if (buf[14] > reader[ridx].cc_maxhop) break; 778 779 card = calloc(1, sizeof(struct cc_card)); 780 if (!card) break; 781 //memset(card, 0, sizeof(struct cc_card)); 782 card->provs = llist_create(); 783 card->badsids = llist_create(); 784 card->id = b2i(4, buf+4); 785 card->sub_id = b2i(3, buf+9); 786 card->caid = b2i(2, buf+12); 787 card->hop = buf[14]; 788 memcpy(card->key, buf+16, 8); 789 790 cs_log("cccam: card %08X added, caid %04X, hop %d, node %s_%x", card->id, card->caid, card->hop, cs_hexdump(0, buf+26+(7*buf[24]), 8), card->sub_id); 791 792 //cs_dump(buf+26+(7*buf[24]), 8, NULL); 793 //cs_dump(buf+8, 8, NULL); 794 795 // providers 796 for (i = 0; i < buf[24]; i++) 797 { 798 uint8 *prov = malloc(3); 799 if (prov) 800 { 801 memcpy(prov, buf+25+(7*i), 3); 802 cs_log(" prov %d, %06x", i+1, b2i(3, prov)); 803 llist_append(card->provs, prov); 804 } 805 } 806 807 llist_append(cc->cards, card); 808 if (!cc->cur_card) 809 cc->cur_card = card; 810 } 811 break; 812 case MSG_CARD_REMOVED: 813 { 814 struct cc_card *card; 815 LLIST_ITR itr; 816 817 card = llist_itr_init(cc->cards, &itr); 818 while (card) 819 { 820 if (card->id == b2i(4, buf+4) && card->sub_id == b2i(3, buf+9)) 821 { 822 cs_log("cccam: card %08X_%X removed, caid %04X", card->id, card->sub_id, card->caid); 823 llist_destroy(card->provs); 824 llist_destroy(card->badsids); 825 free(card); 826 card = llist_itr_remove(&itr); 827 break; 828 } 829 else 830 { 831 card = llist_itr_next(&itr); 832 } 833 } 834 } 835 break; 836 case MSG_CW_NOK1: 837 case MSG_CW_NOK2: 838 { 839 //cs_debug("cccam: cw nok, sid = %04X", cc->cur_sid); 840 int f = 0; 841 LLIST_ITR itr; 842 uint16 *sid = llist_itr_init(cc->cur_card->badsids, &itr); 843 while (sid && !f) 844 { 845 if (*sid == cc->cur_sid) 846 { 847 f = 1; 848 } 849 sid = llist_itr_next(&itr); 850 } 851 852 if (!f) 853 { 854 sid = malloc(sizeof(uint16)); 855 if (sid) 856 { 857 *sid = cc->cur_sid; 858 sid = llist_append(cc->cur_card->badsids, sid); 859 //cs_debug(" added sid block for card %08X", cc->cur_card->id); 860 } 861 } 862 memset(cc->dcw, 0, 16); 863 pthread_mutex_unlock(&cc->ecm_busy); 864 cc_send_ecm(NULL, NULL); 865 ret = 0; 866 } 867 break; 868 case MSG_CW_ECM: 869 if (is_server) 870 { 871 ECM_REQUEST *er; 872 cc->cur_card = calloc(1, sizeof(struct cc_card)); 873 if (!cc->cur_card) break; 874 //memset(cc->cur_card, 0, sizeof(struct cc_card)); 875 cc->cur_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8 | buf[13]; 876 877 er=get_ecmtask(); 878 if (er) 879 { 880 er->caid = b2i(2, buf+4); 881 er->srvid = b2i(2, buf+14); 882 er->l = buf[16]; 883 memcpy(er->ecm, buf+17, er->l); 884 er->prid = b2i(4, buf+6); 885 get_cw(er); 886 } 887 } 888 else 889 { 890 cc_cw_crypt(buf+4); 891 memcpy(cc->dcw, buf+4, 16); 892 //cs_debug("cccam: cws: %s", cs_hexdump(0, cc->dcw, 16)); 893 cc_crypt(&cc->block[DECRYPT], buf+4, l-4, ENCRYPT); // additional crypto step 894 pthread_mutex_unlock(&cc->ecm_busy); 895 //cc_abort_user_ecms(); 896 cc_send_ecm(NULL, NULL); 897 ret = 0; 898 } 899 break; 900 case MSG_KEEPALIVE: 901 cc_cmd_send(NULL, 0, MSG_KEEPALIVE); 902 cs_debug("cccam: keepalive"); 903 break; 904 case MSG_BAD_ECM: 905 //cc->ecm_count = 1; 906 //cs_log("cccam: cmd 0x05 recvd, commencing ecm count"); 907 cc_cmd_send(NULL, 0, MSG_BAD_ECM); 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); 908 769 break; 909 case MSG_CMD_0B: 910 // need to work out algo (reverse) for this... 911 cc_cycle_connection(); 912 default: 913 break; 914 } 915 916 if (cc->ecm_count > CC_MAX_ECMS) 917 cc_cycle_connection(); 918 919 return ret; 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; 920 858 } 921 859 922 860 static int cc_recv_chk(uchar *dcw, int *rc, uchar *buf) 923 861 { 924 struct cc_data *cc = reader[ridx].cc; 925 926 if (buf[1] == MSG_CW_ECM) 927 { 928 memcpy(dcw, cc->dcw, 16); 929 cs_debug("cccam: recv chk - MSG_CW %d - %s", cc->count, cs_hexdump(0, dcw, 16)); 930 *rc = 1; 931 return(cc->count); 932 } 933 else 934 if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) 935 { 936 memset(dcw, 0, 16); 937 *rc = 0; 938 return (0); 939 //return(cc->count); 940 } 941 942 return (-1); 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); 943 875 } 944 876 945 877 static void cc_send_dcw(ECM_REQUEST *er) 946 878 { 947 uchar buf[16]; 948 struct cc_data *cc; 949 950 memset(buf, 0, sizeof(buf)); 951 952 if(er->rc<=3) 953 { 954 cc = client[cs_idx].cc; 955 memcpy(buf, er->cw, sizeof(buf)); 956 cc_cw_crypt(buf); 957 NULLFREE(cc->cur_card); 958 //cs_debug("cccam: send cw: er->cpti=%d", er->cpti); 959 cc_cmd_send(buf, 16, MSG_CW_ECM); 960 cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step 961 } 962 else 963 { 964 //cs_debug("cccam: send cw NOK: er->cpti=%d!", er->cpti); 965 cc_cmd_send(NULL, 0, MSG_CW_NOK1); 966 } 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 } 967 896 } 968 897 969 898 int cc_recv(uchar *buf, int l) 970 899 { 971 int n; 972 uchar *cbuf; 973 struct cc_data *cc; 974 975 if (reader[ridx].cc) 976 cc = reader[ridx].cc; 977 else 978 cc = client[cs_idx].cc; 979 980 if (buf==NULL) return -1; 981 cbuf = malloc(l); 982 if (cbuf==NULL) return -1; 983 984 memcpy(cbuf, buf, l); // make a copy of buf 985 986 pthread_mutex_lock(&cc->lock); 987 988 n = cc_msg_recv(cbuf); // recv and decrypt msg 989 990 cs_ddump(cbuf, n, "cccam: received %d bytes from %s", n, remote_txt()); 991 client[cs_idx].last = time((time_t *) 0); 992 993 if (n == 0) 994 { 995 cs_log("cccam: connection closed to %s", remote_txt()); 996 n = -1; 997 } 998 else 999 { 1000 if (n < 4) 1001 { 1002 cs_log("cccam: packet to small (%d bytes)", n); 1003 n = -1; 1004 } 1005 else 1006 { 1007 // parse it and write it back, if we have received something of value 1008 cc_parse_msg(cbuf, n); 1009 memcpy(buf, cbuf, l); 1010 } 1011 } 1012 NULLFREE(cbuf); 1013 pthread_mutex_unlock(&cc->lock); 1014 1015 if (!is_server && (n==-1)) 1016 { 1017 cs_log("cccam: cycle connection"); 1018 cc_cycle_connection(); 1019 //cs_exit(1); 1020 } 1021 1022 return(n); 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); 1023 945 } 1024 946 1025 947 static int cc_cli_connect(void) 1026 948 { 1027 int handle, n; 1028 uint8 data[20]; 1029 uint8 hash[SHA_DIGEST_LENGTH]; 1030 uint8 buf[CC_MAXMSGSIZE]; 1031 char pwd[64]; 1032 struct cc_data *cc; 1033 1034 if (reader[ridx].cc) NULLFREE(reader[ridx].cc); 1035 1036 // init internals data struct 1037 cc = calloc(1, sizeof(struct cc_data)); 1038 if (cc==NULL) 1039 { 1040 cs_log("cccam: cannot allocate memory"); 1041 cs_exit(0); 1042 } 1043 reader[ridx].cc = cc; 1044 //memset(reader[ridx].cc, 0, sizeof(struct cc_data)); 1045 cc->cards = llist_create(); 1046 cc->ecm_count = 0; 1047 1048 // check cred config 1049 if (reader[ridx].device[0] == 0 || reader[ridx].r_pwd[0] == 0 || 1050 reader[ridx].r_usr[0] == 0 || reader[ridx].r_port == 0) 1051 return -5; 1052 1053 // connect 1054 handle = network_tcp_connection_open(); 1055 if(handle < 0) return -1; 1056 1057 // get init seed 1058 n = recv(handle, data, 16, MSG_WAITALL); 1059 if (n != 16) 1060 { 1061 cs_log("cccam: server does not return 16 bytes"); 1062 network_tcp_connection_close(handle); 1063 return -2; 1064 } 1065 cs_ddump(data, 16, "cccam: server init seed:"); 1066 1067 cc_xor(data); // XOR init bytes with 'CCcam' 1068 1069 SHA_CTX ctx; 1070 SHA1_Init(&ctx); 1071 SHA1_Update(&ctx, data, 16); 1072 SHA1_Final(hash, &ctx); 1073 1074 cs_ddump(hash, sizeof(hash), "cccam: sha1 hash:"); 1075 1076 //initialisate crypto states 1077 cc_init_crypt(&cc->block[DECRYPT], hash, 20); 1078 cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT); 1079 cc_init_crypt(&cc->block[ENCRYPT], data, 16); 1080 cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT); 1081 1082 cc_cmd_send(hash, 20, MSG_NO_HEADER); // send crypted hash to server 1083 1084 memset(buf, 0, sizeof(buf)); 1085 memcpy(buf, reader[ridx].r_usr, strlen(reader[ridx].r_usr)); 1086 cs_ddump(buf, 20, "cccam: username '%s':", buf); 1087 cc_cmd_send(buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes 1088 1089 memset(buf, 0, sizeof(buf)); 1090 memset(pwd, 0, sizeof(pwd)); 1091 1092 cs_debug("cccam: 'CCcam' xor"); 1093 memcpy(buf, "CCcam", 5); 1094 strncpy(pwd, reader[ridx].r_pwd, sizeof(pwd)-1); 1095 cc_crypt(&cc->block[ENCRYPT], (uint8 *)pwd, strlen(pwd), ENCRYPT); 1096 cc_cmd_send(buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd 1097 1098 n = recv(handle, data, 20, MSG_WAITALL); 1099 if (n != 20) 1100 { 1101 cs_log("cccam: login failed, pwd ack not received (n = %d)", n); 1102 return -2; 1103 } 1104 cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT); 1105 cs_ddump(data, 20, "cccam: pwd ack received:"); 1106 1107 // check server response 1108 if (memcmp(data, buf, 5)) 1109 { 1110 cs_log("cccam: login failed, usr/pwd invalid"); 1111 return -2; 1112 } 1113 else 1114 { 1115 cs_debug("cccam: login succeeded"); 1116 } 1117 1118 reader[ridx].tcp_connected = 1; 1119 reader[ridx].last_g = reader[ridx].last_s = time((time_t *)0); 1120 1121 cs_debug("cccam: last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g); 1122 1123 pfd=client[cs_idx].udp_fd; 1124 1125 if (cc_send_cli_data()<=0) 1126 { 1127 cs_log("cccam: login failed, could not send client data"); 1128 return -3; 1129 } 1130 1131 pthread_mutex_init(&cc->lock, NULL); 1132 pthread_mutex_init(&cc->ecm_busy, NULL); 1133 1134 reader[ridx].caid[0] = reader[ridx].ftab.filts[0].caid; 1135 reader[ridx].nprov = reader[ridx].ftab.filts[0].nprids; 1136 for (n=0; n<reader[ridx].nprov; n++) 1137 { 1138 reader[ridx].availkeys[n][0] = 1; 1139 reader[ridx].prid[n][0] = reader[ridx].ftab.filts[0].prids[n] >> 24; 1140 reader[ridx].prid[n][1] = reader[ridx].ftab.filts[0].prids[n] >> 16; 1141 reader[ridx].prid[n][2] = reader[ridx].ftab.filts[0].prids[n] >> 8; 1142 reader[ridx].prid[n][3] = reader[ridx].ftab.filts[0].prids[n] & 0xff; 1143 } 1144 1145 return 0; 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; 1146 1058 } 1147 1059 … … 1309 1221 static int cc_srv_connect() 1310 1222 { 1311 int i; 1312 uint cmi = 0; 1313 uint seed; 1314 uint8 buf[CC_MAXMSGSIZE]; 1315 uint8 data[16]; 1316 char usr[20], pwd[20]; 1317 struct s_auth *account; 1318 struct cc_data *cc; 1319 1320 memset(usr, 0, sizeof(usr)); 1321 memset(pwd, 0, sizeof(pwd)); 1322 1323 if (client[cs_idx].cc) NULLFREE(client[cs_idx].cc); 1324 1325 // init internals data struct 1326 cc = malloc(sizeof(struct cc_data)); 1327 if (cc==NULL) 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) 1328 1364 { 1329 cs_log("cccam: cannot allocate memory"); 1330 cs_exit(0); 1331 } 1332 1333 client[cs_idx].cc = cc; 1334 memset(client[cs_idx].cc, 0, sizeof(struct cc_data)); 1335 1336 // calc + send random seed 1337 seed = (unsigned int) time((time_t*)0); 1338 for(i=0; i<16; i++ ) data[i]=fast_rnd(); 1339 send(client[cs_idx].udp_fd, data, 16, 0); 1340 1341 cc_xor(data); // XOR init bytes with 'CCcam' 1342 1343 SHA_CTX ctx; 1344 SHA1_Init(&ctx); 1345 SHA1_Update(&ctx, data, 16); 1346 SHA1_Final(buf, &ctx); 1347 1348 //initialisate crypto states 1349 /* 1350 init_crypt(&cc->block[ENCRYPT], buf, 20); 1351 crypto_state__decrypt(&cc->block[ENCRYPT], data, data2, 16); 1352 init_crypt(&cc->block[DECRYPT], data2, 16); 1353 crypto_state__encrypt(&cc->block[DECRYPT], buf, buf2, 20); 1354 */ 1355 1356 cc_init_crypt(&cc->block[ENCRYPT], buf, 20); 1357 cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT); 1358 cc_init_crypt(&cc->block[DECRYPT], data, 16); 1359 cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT); 1360 1361 if ((i=recv(pfd, buf, 20, MSG_WAITALL)) == 20) { 1362 cs_ddump(buf, 20, "cccam: recv:"); 1363 cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT); 1364 cs_ddump(buf, 20, "cccam: hash:"); 1365 } else { 1366 cs_log("cccam:%d: receive failed errno: %d (%s)", __LINE__, errno, strerror(errno)); 1367 cs_exit(0); 1368 } 1369 1370 // receive username 1371 if ((i=recv(pfd, buf, 20, MSG_WAITALL)) == 20) { 1372 cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT); 1373 cs_ddump(buf, 20, "cccam: username '%s':", buf); 1374 strncpy(usr, (char *)buf, sizeof(usr)); 1375 } else { 1376 cs_log("cccam:%d: receive username failed errno: %d (%s)", __LINE__, errno, strerror(errno)); 1377 cs_exit(0); 1378 } 1379 1380 for (account=cfg->account; account; account=account->next) 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) 1381 1385 { 1382 if (strcmp(usr, account->usr) == 0) 1383 { 1384 strncpy(pwd, account->pwd, sizeof(pwd)); 1385 break; 1386 } 1387 } 1388 1389 // receive passwd / 'CCcam' 1390 cc_crypt(&cc->block[DECRYPT], (uint8 *)pwd, strlen(pwd), DECRYPT); 1391 if ((i=recv(pfd, buf, 6, MSG_WAITALL)) == 6) { 1392 cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT); 1393 cs_ddump(buf, 6, "cccam: pwd check '%s':", buf); 1394 } else { 1395 cs_log("cccam:%d: receive passwd failed errno: %d (%s)", __LINE__, errno, strerror(errno)); 1396 cs_exit(0); 1397 } 1398 1399 client[cs_idx].crypted = 1; 1400 cs_auth_client(account, NULL); 1401 //cs_auth_client((struct s_auth *)(-1), NULL); 1402 1403 // send passwd ack 1404 /* 1405 bzero(data, sizeof(data)); 1406 memcpy(data, "CCcam\0", 6); 1407 cs_ddump(data, 20, "cccam: send ack:"); 1408 cc_crypt(&cc->block[ENCRYPT], data, 20, ENCRYPT); 1409 send(pfd, data, 20, 0); 1410 */ 1411 memset(buf, 0, 20); 1412 memcpy(buf, "CCcam\0", 6); 1413 cs_ddump(buf, 20, "cccam: send ack:"); 1414 cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT); 1415 send(pfd, buf, 20, 0); 1416 1417 // recv cli data 1418 memset(buf, 0, sizeof(buf)); 1419 i = cc_msg_recv(buf); 1420 cs_ddump(buf, i, "cccam: cli data:"); 1421 memcpy(cc->peer_node_id, buf+24, 8); 1422 cs_log("cccam: client '%s' (%s) running v%s (%s)", buf+4, cs_hexdump(0, cc->peer_node_id, 8), buf+33, buf+65); 1423 1424 // send cli data ack 1425 cc_cmd_send(NULL, 0, MSG_CLI_DATA); 1426 1427 if (cc_send_srv_data()<0) { 1428 cs_log("cccam:%d: cc_send_srv_data() failed errno: %d (%s)", __LINE__, errno, strerror(errno)); 1429 cs_exit(0); 1430 } 1431 1432 is_server = 1; 1433 1434 // report cards 1435 cc_srv_report_cards(); 1436 1437 // check for clienttimeout, if timeout occurs try to send keepalive 1438 for(;;) 1439 { 1440 i=process_input(mbuf, sizeof(mbuf), 10); //cfg->cmaxidle); 1441 if (i == -9) 1442 { 1443 cc_srv_report_cards(); 1444 cmi += 10; 1445 if (cmi >=cfg->cmaxidle) 1446 { 1447 //cs_log("cccam: keepalive after %d sec", cfg->cmaxidle); 1448 cmi = 0; 1449 if (cc_cmd_send(NULL, 0, MSG_KEEPALIVE) > 0) 1450 { 1451 cs_debug("cccam: keepalive"); 1452 i = 1; 1453 } 1454 } 1455 } 1456 else 1457 if (i <= 0) break; 1458 } 1459 cs_disconnect_client(); 1460 1461 return 0; 1462 } 1463 1464 void cc_srv_init() 1465 { 1466 pfd=client[cs_idx].udp_fd; 1467 //cc_auth_client(client[cs_idx].ip); 1468 cc_srv_connect(); 1469 cs_exit(0); 1470 } 1471 1472 int cc_cli_init() 1473 { 1474 if (!reader[ridx].tcp_connected) 1475 { 1476 static struct sockaddr_in loc_sa; 1477 struct protoent *ptrp; 1478 int p_proto; 1479 1480 pfd=0; 1481 if (reader[ridx].r_port<=0) 1482 { 1483 cs_log("cccam: invalid port %d for server %s", reader[ridx].r_port, reader[ridx].device); 1484 return(1); 1485 } 1486 if ((ptrp=getprotobyname("tcp"))) 1487 p_proto=ptrp->p_proto; 1488 else 1489 p_proto=6; 1490 1491 client[cs_idx].ip=0; 1492 memset((char *)&loc_sa,0,sizeof(loc_sa)); 1493 loc_sa.sin_family = AF_INET; 1494 #ifdef LALL 1495 if (cfg->serverip[0]) 1496 loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip); 1497 else 1498 #endif 1499 loc_sa.sin_addr.s_addr = INADDR_ANY; 1500 loc_sa.sin_port = htons(reader[ridx].l_port); 1501 1502 if ((client[cs_idx].udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0) 1503 { 1504 cs_log("cccam: Socket creation failed (errno=%d)", errno); 1505 cs_exit(1); 1506 } 1507 1508 #ifdef SO_PRIORITY 1509 if (cfg->netprio) 1510 setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong)); 1511 #endif 1512 if (!reader[ridx].tcp_ito) 1513 { 1514 ulong keep_alive = reader[ridx].tcp_ito?1:0; 1515 setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keep_alive, sizeof(ulong)); 1516 } 1517 1518 memset((char *)&client[cs_idx].udp_sa,0,sizeof(client[cs_idx].udp_sa)); 1519 client[cs_idx].udp_sa.sin_family = AF_INET; 1520 client[cs_idx].udp_sa.sin_port = htons((u_short)reader[ridx].r_port); 1521 1522 struct hostent *server; 1523 server = gethostbyname(reader[ridx].device); 1524 memmove((char *)&client[cs_idx].udp_sa.sin_addr.s_addr, (char *)server->h_addr, server->h_length); 1525 1526 if (reader[ridx].tcp_rto <= 0) reader[ridx].tcp_rto = 60 * 60 * 10; // timeout to 10 hours 1527 cs_debug("cccam: reconnect timeout set to: %d", reader[ridx].tcp_rto); 1528 if (!reader[ridx].cc_maxhop) reader[ridx].cc_maxhop = 5; // default maxhop to 5 if not configured 1529 1530 cc_check_version(reader[ridx].cc_version, reader[ridx].cc_build); 1531 cs_log("proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d", reader[ridx].label, 1532 reader[ridx].device, reader[ridx].r_port, 1533 reader[ridx].cc_version, reader[ridx].cc_build, reader[ridx].cc_maxhop); 1534 1535 cc_cli_connect(); 1536 1537 return(0); 1538 } 1539 return(-1); 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); 1540 1421 } 1541 1422 … … 1547 1428 void module_cccam(struct s_module *ph) 1548 1429 { 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 } 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 }
Note:
See TracChangeset
for help on using the changeset viewer.