void rdgd_disconnect_srv(struct cs_server_data *srv) { static char msg[]= "Disconnected"; srv->statmsg = msg; // Disconnect server debugf(" radegast: server (%s:%d) disconnected\n", srv->host->name, srv->port); close(srv->handle); srv->handle = INVALID_SOCKET; if (srv->busy) ecm_setsrvflag(srv->busyecmid, srv->id, ECM_SRV_EXCLUDE); srv->uptime += GetTickCount()-srv->connected; srv->keepalivetime = 0; srv->keepalivesent = 0; srv->host->checkiptime = 0; // maybe ip changed }
void cc_disconnect_srv(struct cs_server_data *srv) { static char msg[]= "Disconnected"; srv->statmsg = msg; debugf(" CCcam: server (%s:%d) disconnected\n", srv->host->name,srv->port); close(srv->handle); srv->handle = INVALID_SOCKET; if (srv->busy) ecm_setsrvflag(srv->busyecmid, srv->id, ECM_SRV_EXCLUDE); srv->uptime += GetTickCount()-srv->connected; srv->keepalivetime = 0; srv->keepalivesent = 0; srv->host->checkiptime = 60; // maybe ip changed // Remove Cards while (srv->card) { struct cs_card_data *card = srv->card; srv->card = srv->card->next; free(card); } }
void cc_srv_recvmsg(struct cs_server_data *srv) { unsigned char buf[CC_MAXMSGSIZE]; struct cs_card_data *card; struct cardserver_data *cs; int i, len; ECM_DATA *ecm; if ( (srv->type==TYPE_CCCAM)&&(srv->handle>0) ) { len = cc_msg_chkrecv(srv->handle,&srv->recvblock); if (len==0) { debugf(" CCcam: server (%s:%d) read failed %d\n", srv->host->name, srv->port, len); cc_disconnect_srv(srv); } else if (len==-1) { if (!srv->chkrecvtime) srv->chkrecvtime = GetTickCount(); else if ( (srv->chkrecvtime+500)<GetTickCount() ) { debugf(" CCcam: server (%s:%d) read failed %d\n", srv->host->name, srv->port, len); cc_disconnect_srv(srv); } } else if (len>0) { srv->chkrecvtime = 0; len = cc_msg_recv(srv->handle, &srv->recvblock, buf, 3); if (len==0) { debugf(" CCcam: server (%s:%d) read failed %d\n", srv->host->name, srv->port, len); cc_disconnect_srv(srv); } else if (len<0) { debugf(" CCcam: server (%s:%d) read failed %d(%d)\n", srv->host->name, srv->port, len, errno); cc_disconnect_srv(srv); } else if (len>0) { switch (buf[1]) { case CC_MSG_CLI_INFO: debugf(" CCcam: Client data ACK from Server (%s:%d)\n", srv->host->name,srv->port); break; case CC_MSG_ECM_REQUEST: // Get CW if (!srv->busy) { debugf(" [!] dcw error from server (%s:%d), unknown ecm request\n",srv->host->name,srv->port); break; } uint8 dcw[16]; cc_crypt_cw( cfg.cccam.nodeid, srv->busycardid, &buf[4]); memcpy(dcw, &buf[4], 16); cc_decrypt(&srv->recvblock, buf+4, len-4); // additional crypto step srv->busy = 0; srv->lastdcwtime = GetTickCount(); pthread_mutex_lock(&prg.lockecm); //### ecm = getecmbyid(srv->busyecmid); if (!ecm) { debugf(" [!] error cw from server (%s:%d), ecm not found!!!\n",srv->host->name,srv->port); pthread_mutex_unlock(&prg.lockecm); //### srv->busy = 0; break; } // check for ECM??? if (ecm->hash!=srv->busyecmhash) { debugf(" [!] error cw from server (%s:%d), ecm deleted!!!\n",srv->host->name,srv->port); pthread_mutex_unlock(&prg.lockecm); //### srv->busy = 0; break; } cs = getcsbyid(ecm->csid); int cardcheck = istherecard( srv, srv->busycard ); // Check for DCW if (!acceptDCW(dcw)) { srv->ecmerrdcw ++; if (cs&&cardcheck) { cardsids_update( srv->busycard, ecm->provid, ecm->sid, -1); srv_cstatadd( srv, cs->id, 0 , 0); } ecm_setsrvflag(srv->busyecmid, srv->id, ECM_SRV_REPLY_FAIL); pthread_mutex_unlock(&prg.lockecm); //### break; } // else { srv->lastecmoktime = GetTickCount()-srv->lastecmtime; srv->ecmoktime += srv->lastecmoktime; srv->ecmok++; ecm_setsrvflagdcw(srv->busyecmid, srv->id, ECM_SRV_REPLY_GOOD,dcw); if (cs&&cardcheck) { cardsids_update( srv->busycard, ecm->provid, ecm->sid, 1); /// + Card nodeID srv_cstatadd( srv, cs->id, 1 , srv->lastecmoktime); } if (cardcheck) { srv->busycard->ecmoktime += GetTickCount()-srv->lastecmtime; srv->busycard->ecmok++; } if (ecm->dcwstatus!=STAT_DCW_SUCCESS) { static char msg[] = "Good dcw from CCcam server"; ecm->statusmsg = msg; ecm_setdcw( cs, ecm, dcw, DCW_SOURCE_SERVER, srv->id ); debugf(" <= cw from CCcam server (%s:%d) ch %04x:%06x:%04x (%dms)\n", srv->host->name,srv->port, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-srv->lastecmtime); } pthread_mutex_unlock(&prg.lockecm); //### break; case CC_MSG_ECM_NOK1: // EAGAIN, Retry /* if (!srv->busy) break; ecm = srv->busyecm; debugf(" <| decode1 failed from CCcam server (%s:%d) ch %04x:%06x:%04x (%dms)\n", srv->host->name,srv->port, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-srv->lastecmtime); if ( (GetTickCount()-srv->lastecmtime)<CC_ECMRETRY_TIMEOUT ) { if (srv->retry<CC_ECMRETRY_MAX) { srv->busy = 0; if (cc_sendecm_srv(srv, ecm)) { srv->lastecmtime = GetTickCount(); srv->busy = 1; srv->retry++; break; } } } if (srv->retry>=CC_ECMRETRY_MAX) { ecm_setsrvflag(ecm, srv->id, ECM_SRV_EXCLUDE); } srv->busy = 0; break; */ case CC_MSG_ECM_NOK2: // ecm decode failed if (!srv->busy) { debugf(" [!] dcw error from server (%s:%d), unknown ecm request\n",srv->host->name,srv->port); break; } pthread_mutex_lock(&prg.lockecm); //### ecm = getecmbyid(srv->busyecmid); if (!ecm) { debugf(" [!] dcw error from server (%s:%d), ecm not found!!!\n",srv->host->name,srv->port); pthread_mutex_unlock(&prg.lockecm); //### srv->busy = 0; break; } // check for ECM??? if (ecm->hash!=srv->busyecmhash) { debugf(" [!] dcw error from server (%s:%d), ecm deleted!!!\n",srv->host->name,srv->port); pthread_mutex_unlock(&prg.lockecm); //### srv->busy = 0; break; } debugf(" <| decode failed from CCcam server (%s:%d) ch %04x:%06x:%04x (%dms)\n", srv->host->name,srv->port, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-srv->lastecmtime); cs = getcsbyid(ecm->csid); if (ecm->dcwstatus!=STAT_DCW_SUCCESS) { if ( cs && (GetTickCount()-ecm->recvtime)<cs->cstimeout ) { if (srv->retry<cs->ccretry) { srv->busy = 0; if (cc_sendecm_srv(srv, ecm)) { srv->lastecmtime = GetTickCount(); srv->busy = 1; srv->retry++; debugf(" (RE%d) -> ecm to CCcam server (%s:%d) ch %04x:%06x:%04x\n",srv->retry,srv->host->name,srv->port,ecm->caid,ecm->provid,ecm->sid); pthread_mutex_unlock(&prg.lockecm); //### break; } } } } if (cs) { if ( istherecard( srv, srv->busycard ) ) cardsids_update( srv->busycard, ecm->provid, ecm->sid, -1); srv_cstatadd( srv, cs->id, 0 , 0); } ecm_setsrvflag(srv->busyecmid, srv->id, ECM_SRV_REPLY_FAIL); srv->busy = 0; pthread_mutex_unlock(&prg.lockecm); //### break; case CC_MSG_BAD_ECM: // Add Card cc_msg_send( srv->handle, &srv->sendblock, CC_MSG_BAD_ECM, 0, NULL); //debugf(" CCcam: cmd 0x05 from Server (%s:%d)\n",srv->host->name,srv->port); //currentecm.state = ECM_STATUS_FAILED; break; case CC_MSG_KEEPALIVE: srv->keepalivesent = 0; //debugf(" CCcam: Keepalive ACK from Server (%s:%d)\n",srv->host->name,srv->port); break; case CC_MSG_CARD_DEL: // Delete Card card = srv->card; uint32 k = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7]; struct cs_card_data *prevcard = NULL; while (card) { if (card->shareid==k) { debugf(" CCcam: server (%s:%d), remove share-id %d\n",srv->host->name,srv->port,k); if (prevcard) prevcard->next = card->next; else srv->card = card->next; //Free SIDs while (card->sids) { struct sid_data *sid = card->sids; card->sids = card->sids->next; free(sid); } free(card); // check for current ecm if (srv->busy && (srv->busycardid==k) ) ecm_setsrvflag(srv->busyecmid, srv->id, ECM_SRV_EXCLUDE); break; } prevcard = card; card = card->next; } break; case CC_MSG_CARD_ADD: // remove own cards -> same nodeid "cfg.cccam.nodeid" if ( (buf[14]<srv->uphops) && (buf[24]<=16) && memcmp(buf+26+buf[24]*7,cfg.cccam.nodeid,8) ) { // check Only the first 4 bytes // nodeid index = 26 + 7 * buf[24] struct cs_card_data *card = malloc( sizeof(struct cs_card_data) ); memset(card, 0, sizeof(struct cs_card_data) ); card->shareid = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7]; card->uphops = buf[14]+1; memcpy( card->nodeid, buf+26+buf[24]*7, 8); card->caid = (buf[12]<<8)+(buf[13]); card->nbprov = buf[24]; card->sids = NULL; i = 26+buf[24]*7; debugf(" CCcam: new card (%s:%d) %02x%02x%02x%02x%02x%02x%02x%02x_%x uphops %d caid %04x providers %d\n",srv->host->name,srv->port, buf[i],buf[i+1],buf[i+2],buf[i+3],buf[i+4],buf[i+5],buf[i+6],buf[i+7],card->shareid ,card->uphops, card->caid, card->nbprov); if (card->nbprov>CARD_MAXPROV) card->nbprov = CARD_MAXPROV; for (i=0;i<card->nbprov; i++) { card->prov[i] = (buf[25+i*7]<<16) | (buf[26+i*7]<<8) | (buf[27+i*7]); //debugf(" Provider %d = %06x\n",i, card->prov[i]); } card->next = srv->card; srv->card = card; } break; case CC_MSG_SRV_INFO: memcpy(srv->nodeid, buf+4, 8); memcpy(srv->version, buf+12, 31); for (i=12; i<53; i++) { if (!buf[i]) break; if ( (buf[i]<32)||(buf[i]>'z') ) { memset(srv->version, 0, 31); break; } } memcpy(srv->build, buf+44, 31); debugf(" CCcam: server (%s:%d), info: version %s build %s\n",srv->host->name,srv->port,buf+12, buf+44); break; //default: debugdump(buf,len," CCcam: unknown packet from server (%s:%d): ",srv->host->name,srv->port); } // switch } srv->keepalivetime = GetTickCount(); } } }
void rdgd_srv_recvmsg(struct cs_server_data *srv) { int len; ECM_DATA *ecm; unsigned char buf[CWS_NETMSGSIZE]; if (srv->handle>0) if (srv->type==TYPE_RADEGAST) { len = rdgd_check_message(srv->handle); if (len==0) { debugf(" radegast: server (%s:%d) read failed %d\n", srv->host->name, srv->port, len); rdgd_disconnect_srv(srv); } else if (len==-1) { if (!srv->chkrecvtime) srv->chkrecvtime = GetTickCount(); else if ( (srv->chkrecvtime+300)<GetTickCount() ) { debugf(" radegast: server (%s:%d) read failed %d\n", srv->host->name, srv->port, len); rdgd_disconnect_srv(srv); } } else if (len>0) { srv->chkrecvtime = 0; len = rdgd_message_receive(srv->handle, buf, 3); if (len==0) { debugf(" radegast: server (%s:%d) read failed %d\n", srv->host->name, srv->port, len); rdgd_disconnect_srv(srv); } else if (len<0) { debugf(" radegast: server (%s:%d) read failed %d(%d)\n", srv->host->name, srv->port, len, errno); rdgd_disconnect_srv(srv); } else if (len>0) { switch ( buf[0] ) { case 0x02: // DCW srv->lastdcwtime = GetTickCount(); if (!srv->busy) { debugf(" [!] dcw error from server (%s:%d), unknown ecm request\n",srv->host->name,srv->port); break; } srv->busy = 0; pthread_mutex_lock(&prg.lockecm); //### ecm = getecmbyid(srv->busyecmid); if (!ecm) { debugf(" [!] dcw error from server (%s:%d), ecm not found!!!\n",srv->host->name,srv->port); pthread_mutex_unlock(&prg.lockecm); //### break; } // check for ECM??? if (ecm->hash!=srv->busyecmhash) { debugf(" [!] dcw error from server (%s:%d), ecm deleted!!!\n",srv->host->name,srv->port); pthread_mutex_unlock(&prg.lockecm); //### break; } if ( (buf[1]==0x12)&&(buf[2]==0x05)&&(buf[3]==0x10) ) { // Check for DCW if (!acceptDCW(&buf[4])) { srv->ecmerrdcw ++; pthread_mutex_unlock(&prg.lockecm); //### break; } srv->ecmok++; srv->ecmoktime += GetTickCount()-srv->lastecmtime; srv->lastecmoktime = GetTickCount()-srv->lastecmtime; ecm_setsrvflagdcw(srv->busyecmid, srv->id, ECM_SRV_REPLY_GOOD, &buf[4]); debugf(" <= cw from server (%s:%d) ch %04x:%06x:%04x (%dms)\n", srv->host->name,srv->port, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-srv->lastecmtime); if (ecm->dcwstatus!=STAT_DCW_SUCCESS) { static char msg[] = "Good dcw from Radegast server"; ecm->statusmsg = msg; // Store ECM Answer ecm_setdcw( getcsbyid(ecm->csid), ecm, &buf[4], DCW_SOURCE_SERVER, srv->id ); } else { //TODO: check same dcw between cards srv->ecmerrdcw ++; if ( memcmp(&ecm->cw, &buf[3],16) ) debugf(" !!! different dcw from server (%s:%d)\n",srv->host->name,srv->port); } // ADD IN SID LIST struct cardserver_data *cs=getcsbyid(ecm->csid); if (cs) { cardsids_update( srv->busycard, ecm->provid, ecm->sid, 1); srv_cstatadd( srv, cs->id, 1 , srv->lastecmoktime); } } else { struct cardserver_data *cs=getcsbyid(ecm->csid); if ( cs && (ecm->dcwstatus!=STAT_DCW_SUCCESS) && (srv->retry<cs->rdgdretry) ) { if ( (GetTickCount()-ecm->recvtime)<cs->cstimeout ) if (rdgd_sendecm_srv(srv, ecm)>0) { srv->retry++; ecm->lastsendtime = GetTickCount(); debugf(" (RE) -> ecm to server (%s:%d) ch %04x:%06x:%04x\n",srv->host->name,srv->port,ecm->caid,ecm->provid,ecm->sid); srv->lastecmtime = GetTickCount(); srv->ecmnb++; srv->busy=1; //srv->busyecm = ecm; pthread_mutex_unlock(&prg.lockecm); //### break; } } ecm_setsrvflag(srv->busyecmid, srv->id, ECM_SRV_REPLY_FAIL); debugf(" <| decode failed from server (%s:%d) ch %04x:%06x:%04x (%dms)\n", srv->host->name,srv->port, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-srv->lastecmtime); // ADD IN SID LIST if (cs) { cardsids_update( srv->busycard, ecm->provid, ecm->sid, -1); srv_cstatadd( srv, cs->id, 0 , 0); } ecm_check_time = 0; } pthread_mutex_unlock(&prg.lockecm); //### break; default: buf[0] = 0x81; buf[1] = 0; rdgd_message_send(srv->handle,buf,2); } } } } }