Changeset 1923
- Timestamp:
- 04/02/10 15:02:30 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/module-cccam.c
r1913 r1923 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 634 //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); 635 n = cc_cmd_send(ecmbuf, cur_er->l+13, MSG_CW_ECM); // send ecm 636 637 } else { 638 n = -1; 639 //cs_log("cccam: no suitable card on server"); 640 cur_er->rc = 0; 641 cur_er->rcEx = 0x27; 642 //cur_er->rc = 1; 643 //cur_er->rcEx = 0; 644 cs_sleepms(300); 645 write_ecm_answer(fd_c2m, cur_er); 646 //reader[ridx].last_s = reader[ridx].last_g; 624 647 625 648 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 649 while (card) { 650 if (card->caid == cur_er->caid) { // caid matches 651 LLIST_ITR sitr; 652 uint16 *sid = llist_itr_init(card->badsids, &sitr); 653 while (sid) { 654 if (*sid == cur_er->srvid) 655 sid = llist_itr_remove(&sitr); 656 else sid = llist_itr_next(&sitr); 657 } 658 // llist_itr_release(&sitr); 659 } 660 card = llist_itr_next(&itr); 661 } 662 // llist_itr_release(&itr); 663 664 pthread_mutex_unlock(&cc->ecm_busy); 665 } 666 667 return 0; 668 } 726 669 /* 727 670 static int cc_abort_user_ecms(){ … … 753 696 static cc_msg_type_t cc_parse_msg(uint8 *buf, int l) 754 697 { 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]) 698 int ret = buf[1]; 699 struct cc_data *cc; 700 701 if (reader[ridx].cc) 702 cc = reader[ridx].cc; 703 else 704 cc = client[cs_idx].cc; 705 706 switch (buf[1]) { 707 case MSG_CLI_DATA: 708 cs_debug("cccam: client data ack"); 709 break; 710 case MSG_SRV_DATA: 711 memcpy(cc->peer_node_id, buf+4, 8); 712 cs_log("cccam: srv %s running v%s (%s)", cs_hexdump(0, cc->peer_node_id, 8), buf+12, buf+44); 713 break; 714 case MSG_NEW_CARD: 764 715 { 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); 716 int i = 0; 717 if (buf[14] > reader[ridx].cc_maxhop) 718 break; 719 struct cc_card *card = malloc(sizeof(struct cc_card)); 720 if (!card) 721 break; 722 723 memset(card, 0, sizeof(struct cc_card)); 724 725 card->provs = llist_create(); 726 card->badsids = llist_create(); 727 card->id = b2i(4, buf+4); 728 card->sub_id = b2i (3, buf + 9); 729 card->caid = b2i(2, buf+12); 730 card->hop = buf[14]; 731 memcpy(card->key, buf+16, 8); 732 733 cs_debug("cccam: card %08x added, caid %04x, hop %d, key %s", 734 card->id, card->caid, card->hop, cs_hexdump(0, card->key, 8)); 735 736 737 for (i = 0; i < buf[24]; i++) { // providers 738 uint8 *prov = malloc(3); 739 if (prov) { 740 741 memcpy(prov, buf+25+(7*i), 3); 742 cs_debug(" prov %d, %06x", i+1, b2i(3, prov)); 743 744 llist_append(card->provs, prov); 745 } 746 } 747 748 749 llist_append(cc->cards, card); 750 if (!cc->cur_card) cc->cur_card = card; 751 } 752 break; 753 case MSG_CARD_REMOVED: 754 { 755 struct cc_card *card; 756 LLIST_ITR itr; 757 758 card = llist_itr_init(cc->cards, &itr); 759 while (card) { 760 if (card->id == b2i(4, buf+4) && card->sub_id == b2i (3, buf + 9)) { 761 cs_debug("cccam: card %08x removed, caid %04x", card->id, card->caid); 762 763 llist_destroy(card->provs); 764 llist_destroy(card->badsids); 765 free(card); 766 767 card = llist_itr_remove(&itr); 908 768 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; 769 } else { 770 card = llist_itr_next(&itr); 771 } 772 } 773 // llist_itr_release(&itr); 774 } 775 break; 776 case MSG_CW_NOK1: 777 case MSG_CW_NOK2: 778 cs_log("cccam: cw nok, sid = %x", cc->cur_sid); 779 780 int f = 0; 781 LLIST_ITR itr; 782 uint16 *sid = llist_itr_init(cc->cur_card->badsids, &itr); 783 while (sid && !f) { 784 if (*sid == cc->cur_sid) { 785 // llist_itr_release(&itr); 786 f = 1; 787 } 788 sid = llist_itr_next(&itr); 789 } 790 // llist_itr_release(&itr); 791 792 if (!f) { 793 sid = malloc(sizeof(uint16)); 794 if (sid) { 795 *sid = cc->cur_sid; 796 797 sid = llist_append(cc->cur_card->badsids, sid); 798 cs_debug(" added sid block for card %08x", cc->cur_card->id); 799 } 800 } 801 memset(cc->dcw, 0, 16); 802 pthread_mutex_unlock(&cc->ecm_busy); 803 cc_send_ecm(NULL, NULL); 804 ret = 0; 805 break; 806 case MSG_CW_ECM: 807 if (is_server) { 808 ECM_REQUEST *er; 809 810 cc->cur_card = malloc(sizeof(struct cc_card)); 811 if (!cc->cur_card) 812 break; 813 memset(cc->cur_card, 0, sizeof(struct cc_card)); 814 815 cc->cur_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8 | buf[13]; 816 817 if ((er=get_ecmtask())) { 818 er->caid = b2i(2, buf+4); 819 er->srvid = b2i(2, buf+14); 820 er->l = buf[16]; 821 memcpy(er->ecm, buf+17, er->l); 822 er->prid = b2i(4, buf+6); 823 get_cw(er); 824 } 825 } else { 826 cc_cw_crypt(buf+4); 827 memcpy(cc->dcw, buf+4, 16); 828 cs_debug("cccam: cws: %s", cs_hexdump(0, cc->dcw, 16)); 829 cc_crypt(&cc->block[DECRYPT], buf+4, l-4, ENCRYPT); // additional crypto step 830 pthread_mutex_unlock(&cc->ecm_busy); 831 //cc_abort_user_ecms(); 832 cc_send_ecm(NULL, NULL); 833 ret = 0; 834 } 835 break; 836 case MSG_KEEPALIVE: 837 if (!reader[ridx].cc) 838 cs_debug("cccam: keepalive ack"); 839 cc_cmd_send(NULL, 0, MSG_KEEPALIVE); 840 cs_debug("cccam: keepalive"); 841 break; 842 case MSG_BAD_ECM: 843 //cc->ecm_count = 1; 844 //cs_log("cccam: cmd 0x05 recvd, commencing ecm count"); 845 cc_cmd_send(NULL, 0, MSG_BAD_ECM); 846 break; 847 case MSG_CMD_0B: 848 // need to work out algo (reverse) for this... 849 cc_cycle_connection(); 850 default: 851 break; 852 } 853 854 if (cc->ecm_count > CC_MAX_ECMS) cc_cycle_connection(); 855 856 return ret; 920 857 } 921 858 922 859 static int cc_recv_chk(uchar *dcw, int *rc, uchar *buf) 923 860 { 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); 861 struct cc_data *cc = reader[ridx].cc; 862 863 if (buf[1] == MSG_CW_ECM) { 864 memcpy(dcw, cc->dcw, 16); 865 cs_debug("cccam: recv chk - MSG_CW %d - %s", cc->count, cs_hexdump(0, dcw, 16)); 866 *rc = 1; 867 return(cc->count); 868 } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) { 869 memset(dcw, 0, 16); 870 return *rc = 0; 871 } 872 873 return (-1); 943 874 } 944 875 945 876 static void cc_send_dcw(ECM_REQUEST *er) 946 877 { 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 } 878 uchar buf[16]; 879 struct cc_data *cc; 880 881 memset(buf, 0, sizeof(buf)); 882 883 if(er->rc<=3) { 884 cc = client[cs_idx].cc; 885 memcpy(buf, er->cw, sizeof(buf)); 886 cc_cw_crypt(buf); 887 NULLFREE(cc->cur_card); 888 cs_debug("cccam: send cw: er->cpti=%d", er->cpti); 889 cc_cmd_send(buf, 16, MSG_CW_ECM); 890 cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step 891 } else { 892 cs_debug("cccam: send cw NOK: er->cpti=%d!", er->cpti); 893 cc_cmd_send(NULL, 0, MSG_CW_NOK1); 894 } 967 895 } 968 896 969 897 int cc_recv(uchar *buf, int l) 970 898 { 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); 899 int n; 900 uchar *cbuf; 901 struct cc_data *cc; 902 903 if (reader[ridx].cc) 904 cc = reader[ridx].cc; 905 else 906 cc = client[cs_idx].cc; 907 908 if (buf==NULL) return -1; 909 cbuf = malloc(l); 910 if (cbuf==NULL) return -1; 911 912 memcpy(cbuf, buf, l); // make a copy of buf 913 914 pthread_mutex_lock(&cc->lock); 915 916 n = cc_msg_recv(cbuf); // recv and decrypt msg 917 918 cs_ddump(cbuf, n, "cccam: received %d bytes from %s", n, remote_txt()); 919 client[cs_idx].last = time((time_t *) 0); 920 921 if (n == 0) { 922 cs_log("cccam: connection closed to %s", remote_txt()); 923 n = -1; 924 } else if (n < 4) { 925 cs_log("cccam: packet to small (%d bytes)", n); 926 n = -1; 927 } else { 928 // parse it and write it back, if we have received something of value 929 cc_parse_msg(cbuf, n); 930 memcpy(buf, cbuf, l); 931 } 932 933 NULLFREE(cbuf); 934 935 pthread_mutex_unlock(&cc->lock); 936 937 if (!is_server && (n==-1)) { 938 cs_log("cccam: cycle connection"); 939 cc_cycle_connection(); 940 //cs_exit(1); 941 } 942 943 return(n); 1023 944 } 1024 945 1025 946 static int cc_cli_connect(void) 1026 947 { 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; 948 int handle, n; 949 uint8 data[20]; 950 uint8 hash[SHA_DIGEST_LENGTH]; 951 uint8 buf[CC_MAXMSGSIZE]; 952 char pwd[64]; 953 struct cc_data *cc; 954 955 if (reader[ridx].cc) NULLFREE(reader[ridx].cc); 956 957 // init internals data struct 958 cc = malloc(sizeof(struct cc_data)); 959 if (cc==NULL) { 960 cs_log("cccam: cannot allocate memory"); 961 return -1; 962 } 963 reader[ridx].cc = cc; 964 memset(reader[ridx].cc, 0, sizeof(struct cc_data)); 965 cc->cards = llist_create(); 966 967 cc->ecm_count = 0; 968 969 // check cred config 970 if(reader[ridx].device[0] == 0 || reader[ridx].r_pwd[0] == 0 || 971 reader[ridx].r_usr[0] == 0 || reader[ridx].r_port == 0) 972 return -5; 973 974 // connect 975 handle = network_tcp_connection_open(); 976 if(handle < 0) return -1; 977 978 // get init seed 979 if((n = recv(handle, data, 16, MSG_WAITALL)) != 16) { 980 cs_log("cccam: server does not return 16 bytes"); 981 network_tcp_connection_close(&reader[ridx], handle); 982 return -2; 983 } 984 cs_ddump(data, 16, "cccam: server init seed:"); 985 986 cc_xor(data); // XOR init bytes with 'CCcam' 987 988 SHA_CTX ctx; 989 SHA1_Init(&ctx); 990 SHA1_Update(&ctx, data, 16); 991 SHA1_Final(hash, &ctx); 992 993 cs_ddump(hash, sizeof(hash), "cccam: sha1 hash:"); 994 995 //initialisate crypto states 996 cc_init_crypt(&cc->block[DECRYPT], hash, 20); 997 cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT); 998 cc_init_crypt(&cc->block[ENCRYPT], data, 16); 999 cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT); 1000 1001 cc_cmd_send(hash, 20, MSG_NO_HEADER); // send crypted hash to server 1002 1003 memset(buf, 0, sizeof(buf)); 1004 memcpy(buf, reader[ridx].r_usr, strlen(reader[ridx].r_usr)); 1005 cs_ddump(buf, 20, "cccam: username '%s':", buf); 1006 cc_cmd_send(buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes 1007 1008 memset(buf, 0, sizeof(buf)); 1009 memset(pwd, 0, sizeof(pwd)); 1010 1011 cs_debug("cccam: 'CCcam' xor"); 1012 memcpy(buf, "CCcam", 5); 1013 strncpy(pwd, reader[ridx].r_pwd, sizeof(pwd)-1); 1014 cc_crypt(&cc->block[ENCRYPT], (uint8 *)pwd, strlen(pwd), ENCRYPT); 1015 cc_cmd_send(buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd 1016 1017 if ((n = recv(handle, data, 20, MSG_WAITALL)) != 20) { 1018 cs_log("cccam: login failed, pwd ack not received (n = %d)", n); 1019 return -2; 1020 } 1021 cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT); 1022 cs_ddump(data, 20, "cccam: pwd ack received:"); 1023 1024 if (memcmp(data, buf, 5)) { // check server response 1025 cs_log("cccam: login failed, usr/pwd invalid"); 1026 return -2; 1027 } else { 1028 cs_debug("cccam: login succeeded"); 1029 } 1030 1031 reader[ridx].tcp_connected = 1; 1032 reader[ridx].last_g = reader[ridx].last_s = time((time_t *)0); 1033 1034 cs_debug("cccam: last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g); 1035 1036 pfd=client[cs_idx].udp_fd; 1037 1038 if (cc_send_cli_data()<=0) { 1039 cs_log("cccam: login failed, could not send client data"); 1040 return -3; 1041 } 1042 1043 pthread_mutex_init(&cc->lock, NULL); 1044 pthread_mutex_init(&cc->ecm_busy, NULL); 1045 1046 reader[ridx].caid[0] = reader[ridx].ftab.filts[0].caid; 1047 reader[ridx].nprov = reader[ridx].ftab.filts[0].nprids; 1048 for (n=0; n<reader[ridx].nprov; n++) { 1049 reader[ridx].availkeys[n][0] = 1; 1050 reader[ridx].prid[n][0] = reader[ridx].ftab.filts[0].prids[n] >> 24; 1051 reader[ridx].prid[n][1] = reader[ridx].ftab.filts[0].prids[n] >> 16; 1052 reader[ridx].prid[n][2] = reader[ridx].ftab.filts[0].prids[n] >> 8; 1053 reader[ridx].prid[n][3] = reader[ridx].ftab.filts[0].prids[n] & 0xff; 1054 } 1055 1056 return 0; 1146 1057 } 1147 1058 … … 1309 1220 static int cc_srv_connect() 1310 1221 { 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) 1222 int i; 1223 ulong cmi; 1224 uint seed; 1225 uint8 buf[CC_MAXMSGSIZE]; 1226 uint8 data[16]; 1227 char usr[20], pwd[20]; 1228 struct s_auth *account; 1229 struct cc_data *cc; 1230 1231 memset(usr, 0, sizeof(usr)); 1232 memset(pwd, 0, sizeof(pwd)); 1233 1234 if (client[cs_idx].cc) NULLFREE(client[cs_idx].cc); 1235 1236 // init internals data struct 1237 cc = malloc(sizeof(struct cc_data)); 1238 if (cc==NULL) { 1239 cs_log("cccam: cannot allocate memory"); 1240 return -1; 1241 } 1242 1243 client[cs_idx].cc = cc; 1244 memset(client[cs_idx].cc, 0, sizeof(struct cc_data)); 1245 1246 // calc + send random seed 1247 seed = (unsigned int) time((time_t*)0); 1248 for(i=0; i<16; i++ ) data[i]=fast_rnd(); 1249 send(client[cs_idx].udp_fd, data, 16, 0); 1250 1251 cc_xor(data); // XOR init bytes with 'CCcam' 1252 1253 SHA_CTX ctx; 1254 SHA1_Init(&ctx); 1255 SHA1_Update(&ctx, data, 16); 1256 SHA1_Final(buf, &ctx); 1257 1258 //initialisate crypto states 1259 /* 1260 init_crypt(&cc->block[ENCRYPT], buf, 20); 1261 crypto_state__decrypt(&cc->block[ENCRYPT], data, data2, 16); 1262 init_crypt(&cc->block[DECRYPT], data2, 16); 1263 crypto_state__encrypt(&cc->block[DECRYPT], buf, buf2, 20);*/ 1264 1265 cc_init_crypt(&cc->block[ENCRYPT], buf, 20); 1266 cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT); 1267 cc_init_crypt(&cc->block[DECRYPT], data, 16); 1268 cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT); 1269 1270 if ((i=recv(pfd, buf, 20, MSG_WAITALL)) == 20) { 1271 cs_ddump(buf, 20, "cccam: recv:"); 1272 cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT); 1273 cs_ddump(buf, 20, "cccam: hash:"); 1274 } else return -1; 1275 1276 // receive username 1277 if ((i=recv(pfd, buf, 20, MSG_WAITALL)) == 20) { 1278 cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT); 1279 cs_ddump(buf, 20, "cccam: username '%s':", buf); 1280 strncpy(usr, (char *)buf, sizeof(usr)); 1281 } else return -1; 1282 1283 for (account=cfg->account; account; account=account->next) 1284 if (strcmp(usr, account->usr) == 0) { 1285 strncpy(pwd, account->pwd, sizeof(pwd)); 1286 break; 1287 } 1288 1289 // receive passwd / 'CCcam' 1290 cc_crypt(&cc->block[DECRYPT], (uint8 *)pwd, strlen(pwd), DECRYPT); 1291 if ((i=recv(pfd, buf, 6, MSG_WAITALL)) == 6) { 1292 cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT); 1293 cs_ddump(buf, 6, "cccam: pwd check '%s':", buf); 1294 } else return -1; 1295 1296 cs_auth_client(account, NULL); 1297 //cs_auth_client((struct s_auth *)(-1), NULL); 1298 1299 // send passwd ack 1300 memset(buf, 0, 20); 1301 memcpy(buf, "CCcam\0", 6); 1302 cs_ddump(buf, 20, "cccam: send ack:"); 1303 cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT); 1304 send(pfd, buf, 20, 0); 1305 1306 // recv cli data 1307 memset(buf, 0, sizeof(buf)); 1308 i = cc_msg_recv(buf); 1309 cs_ddump(buf, i, "cccam: cli data:"); 1310 memcpy(cc->peer_node_id, buf+24, 8); 1311 cs_log("cccam: client '%s' (%s) running v%s (%s)", buf+4, cs_hexdump(0, cc->peer_node_id, 8), buf+33, buf+65); 1312 1313 // send cli data ack 1314 cc_cmd_send(NULL, 0, MSG_CLI_DATA); 1315 1316 if (cc_send_srv_data()<0) return -1; 1317 1318 is_server = 1; 1319 1320 // report cards 1321 cc_srv_report_cards(); 1322 1323 cmi = 0; 1324 // check for clienttimeout, if timeout occurs try to send keepalive 1325 for (;;) { 1326 i=process_input(mbuf, sizeof(mbuf), 10); //cfg->cmaxidle); 1327 if (i == -9) { 1328 cc_srv_report_cards(); 1329 cmi += 10; 1330 if (cmi >= cfg->cmaxidle) { 1331 cmi = 0; 1332 if (cc_cmd_send(NULL, 0, MSG_KEEPALIVE) > 0) { 1333 cs_debug("cccam: keepalive after maxidle is reached"); 1334 i = 1; 1335 } 1336 } 1337 } else if (i <= 0) break; 1338 } 1339 1340 cs_disconnect_client(); 1341 1342 return 0; 1343 } 1344 1345 void cc_srv_init() 1346 { 1347 pfd=client[cs_idx].udp_fd; 1348 //cc_auth_client(client[cs_idx].ip); 1349 if (cc_srv_connect() < 0) 1350 cs_log("cccam:%d failed errno: %d (%s)", __LINE__, errno, strerror(errno)); 1351 cs_exit(0); 1352 } 1353 1354 int cc_cli_init() 1355 { 1356 if (!reader[ridx].tcp_connected) { 1357 static struct sockaddr_in loc_sa; 1358 struct protoent *ptrp; 1359 int p_proto; 1360 1361 pfd=0; 1362 if (reader[ridx].r_port<=0) 1328 1363 { 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) 1364 cs_log("cccam: invalid port %d for server %s", reader[ridx].r_port, reader[ridx].device); 1365 return(1); 1366 } 1367 if( (ptrp=getprotobyname("tcp")) ) 1368 p_proto=ptrp->p_proto; 1369 else 1370 p_proto=6; 1371 1372 client[cs_idx].ip=0; 1373 memset((char *)&loc_sa,0,sizeof(loc_sa)); 1374 loc_sa.sin_family = AF_INET; 1375 #ifdef LALL 1376 if (cfg->serverip[0]) 1377 loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip); 1378 else 1379 #endif 1380 loc_sa.sin_addr.s_addr = INADDR_ANY; 1381 loc_sa.sin_port = htons(reader[ridx].l_port); 1382 1383 if ((client[cs_idx].udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0) 1381 1384 { 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); 1385 cs_log("cccam: Socket creation failed (errno=%d)", errno); 1386 cs_exit(1); 1387 } 1388 1389 #ifdef SO_PRIORITY 1390 if (cfg->netprio) 1391 setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY, 1392 (void *)&cfg->netprio, sizeof(ulong)); 1393 #endif 1394 if (!reader[ridx].tcp_ito) { 1395 ulong keep_alive = reader[ridx].tcp_ito?1:0; 1396 setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_KEEPALIVE, 1397 (void *)&keep_alive, sizeof(ulong)); 1398 } 1399 1400 memset((char *)&client[cs_idx].udp_sa,0,sizeof(client[cs_idx].udp_sa)); 1401 client[cs_idx].udp_sa.sin_family = AF_INET; 1402 client[cs_idx].udp_sa.sin_port = htons((u_short)reader[ridx].r_port); 1403 1404 struct hostent *server; 1405 server = gethostbyname(reader[ridx].device); 1406 memmove((char *)&client[cs_idx].udp_sa.sin_addr.s_addr, (char *)server->h_addr, server->h_length); 1407 1408 if (reader[ridx].tcp_rto <= 0) reader[ridx].tcp_rto = 60 * 60 * 10; // timeout to 10 hours 1409 cs_debug("cccam: reconnect timeout set to: %d", reader[ridx].tcp_rto); 1410 if (!reader[ridx].cc_maxhop) reader[ridx].cc_maxhop = 5; // default maxhop to 5 if not configured 1411 cc_check_version (reader[ridx].cc_version, reader[ridx].cc_build); 1412 cs_log ("proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d", reader[ridx].label, 1413 reader[ridx].device, reader[ridx].r_port, 1414 reader[ridx].cc_version, reader[ridx].cc_build, reader[ridx].cc_maxhop); 1415 cc_cli_connect(); 1416 1417 return(0); 1418 } 1419 return(-1); 1540 1420 } 1541 1421 … … 1547 1427 void module_cccam(struct s_module *ph) 1548 1428 { 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 } 1429 strcpy(ph->desc, "cccam"); 1430 ph->type=MOD_CONN_TCP; 1431 ph->logtxt = ", crypted"; 1432 ph->watchdog=1; 1433 ph->recv=cc_recv; 1434 ph->cleanup=cc_cleanup; 1435 ph->c_multi=1; 1436 ph->c_init=cc_cli_init; 1437 ph->c_recv_chk=cc_recv_chk; 1438 ph->c_send_ecm=cc_send_ecm; 1439 ph->s_ip=cfg->cc_srvip; 1440 ph->s_handler=cc_srv_init; 1441 ph->send_dcw=cc_send_dcw; 1442 1443 static PTAB ptab; 1444 ptab.ports[0].s_port = cfg->cc_port; 1445 ph->ptab = &ptab; 1446 ph->ptab->nports = 1; 1447 }
Note:
See TracChangeset
for help on using the changeset viewer.