From: leslie Date: Mon, 9 Jun 2008 04:29:30 +0000 (+0800) Subject: cardclient-CCcam: style cleanup X-Git-Tag: 0.9.1~78 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=6fe644ad46937096da945f14e8d6da30c70e952b;p=sasc-ng.git cardclient-CCcam: style cleanup --- diff --git a/systems/cardclient/cccam.c b/systems/cardclient/cccam.c index e229e6a..79f3f0a 100644 --- a/systems/cardclient/cccam.c +++ b/systems/cardclient/cccam.c @@ -18,101 +18,27 @@ */ #include -#include -#include -#include -#include -#include -#include -#include #include #include #include +#include +#include +#include #include #include #include -#include #include #include -#include #include #include "cc.h" -#include "network.h" -#include "parse.h" - -// START (C) 2002 by Andreas Oberritter - -#define MAX_PIDS 4 - -typedef struct descrambleservice_t -{ - unsigned char valid; - unsigned char started; - unsigned short status; - unsigned short onID; - unsigned short sID; - unsigned short Unkwn; - unsigned short caID; - unsigned short ecmPID; - unsigned char numpids; - unsigned short pid[MAX_PIDS]; - -} descrambleservice_s; - -typedef struct ca_descriptor_s -{ - unsigned char descriptor_tag : 8; - unsigned char descriptor_length : 8; - unsigned short ca_system_id : 16; - unsigned char reserved : 3; - unsigned short ca_pid : 13; - unsigned char * private_data_byte; -} __attribute__ ((packed)) ca_descriptor; - -typedef struct ca_pmt_program_info_s -{ - unsigned char ca_pmt_cmd_id : 8; - ca_descriptor * descriptor; -} __attribute__ ((packed)) ca_pmt_program_info; - -typedef struct ca_pmt_es_info_s -{ - unsigned char stream_type : 8; - unsigned char reserved : 3; - unsigned short elementary_pid : 13; - unsigned char reserved2 : 4; - unsigned short es_info_length : 12; - ca_pmt_program_info * program_info; -} __attribute__ ((packed)) ca_pmt_es_info; - -typedef struct ca_pmt_s -{ - unsigned char ca_pmt_list_management : 8; - unsigned short program_number : 16; - unsigned char reserved1 : 2; - unsigned char version_number : 5; - unsigned char current_next_indicator : 1; - unsigned char reserved2 : 4; - unsigned short program_info_length : 12; - ca_pmt_program_info * program_info; - ca_pmt_es_info * es_info; -} __attribute__ ((packed)) ca_pmt; -// END (C) 2002 by Andreas Oberritter +//#include "network.h" +//#include "parse.h" -#define LIST_MORE 0x00 /* CA application should append a 'MORE' CAPMT object the list, and start receiving the next object */ -#define LIST_FIRST 0x01 /* CA application should clear the list when a 'FIRST' CAPMT object is received, and start receiving the next object */ -#define LIST_LAST 0x02 /* CA application should append a 'LAST' CAPMT object to the list, and start working with the list */ #define LIST_ONLY 0x03 /* CA application should clear the list when an 'ONLY' CAPMT object is received, and start working with the object */ -#define LIST_ADD 0x04 /* CA application should append an 'ADD' CAPMT object to the current list, and start working with the updated list */ -#define LIST_UPDATE 0x05 /* CA application should replace an entry in the list with an 'UPDATE' CAPMT object, and start working with the updated list */ -/* ca_pmt_cmd_id's: */ -#define CMD_OK_DESCRAMBLING 0x01 /* CA application should start descrambling the service in this CAPMT object, as soon as the list of CAPMT objects is complete */ -int parse_ca_pmt ( unsigned char * buffer, unsigned int length); - -// -- cCCcamClient ----------------------------------------------------------- +// -- cCardClientCCcam --------------------------------------------------------- class cCardClientCCcam : public cCardClient , private cThread { private: @@ -131,7 +57,6 @@ private: void Writecapmt(int j); int CCSelect(int sd, bool forRead, int s); bool GetCCamCw(unsigned char *cw, int s); - int startportlistener(); protected: virtual bool lLogin(int cardnum); virtual void Action(void); @@ -139,39 +64,34 @@ public: cCardClientCCcam(const char *Name); virtual bool Init(const char *CfgDir); virtual bool ProcessECM(const cEcmInfo *ecm, const unsigned char *data, unsigned char *Cw, int cardnum); - bool CanHandle(unsigned short SysId) -{ -if ((SysId & 0xf000) != 0x5000) - return true; -else - return false; -} - -}; + virtual bool CanHandle(unsigned short SysId); + }; static cCardClientLinkReg __ncd("CCcam"); cCardClientCCcam::cCardClientCCcam(const char *Name) :cCardClient(Name) +,cThread("CCcam listener") { cwccam_fd=-1; pmtversion=0; - ccam_fd[0] = -1; - ccam_fd[1] = -1; - ccam_fd[2] = -1; - ccam_fd[3] = -1; + ccam_fd[0]=-1; ccam_fd[1]=-1; ccam_fd[2]=-1; ccam_fd[3]=-1; } - bool cCardClientCCcam::Init(const char *config) { cMutexLock lock(this); - int num = 0; + int num=0; signal(SIGPIPE,SIG_IGN); if(!ParseStdConfig(config,&num)) return false; return true; } +bool cCardClientCCcam::CanHandle(unsigned short SysId) +{ + if((SysId & 0xf000) != 0x5000) return true; + return false; +} bool cCardClientCCcam::lLogin(int cardnum) { @@ -191,46 +111,42 @@ bool cCardClientCCcam::lLogin(int cardnum) serv_addr_un.sun_family = AF_LOCAL; strcpy(serv_addr_un.sun_path, camdsock); res=connect(ccam_fd[cardnum], (const sockaddr*)&serv_addr_un, sizeof(serv_addr_un)); - if (res !=0) - { + if (res !=0) { PRINTF(L_CC_CCCAM,"Couldnt open camd.socket..... errno = %d Aggg",errno); close(ccam_fd[cardnum]); ccam_fd[cardnum] = -1; - } + } PRINTF(L_CC_CCCAM,"Opened camd.socket..... ccamd_fd = %d ",ccam_fd[cardnum]); if (cwccam_fd!=-1) return 1; PRINTF(L_CC_CCCAM,"logging in"); so=socket(AF_INET, SOCK_DGRAM, 0); - if(so<0) - { + if(so<0) { close(so); return false; - } + } bzero(&servAddr, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); servAddr.sin_port = htons(port); rc = bind (so, (struct sockaddr *) &servAddr,sizeof(servAddr)); - if(rc<0) - { + if(rc<0) { close(so); PRINTF(L_CC_CCCAM,"not logged in"); return false; - } + } cwccam_fd =so; PRINTF(L_CC_CCCAM,"logged in"); - startportlistener(); + PRINTF(L_CC_CCCAM,"starting UDP listener"); + Start(); return true; } - bool cCardClientCCcam::ProcessECM(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw, int cardnum) { - // if(((ccam_fd ==0) && !Login()) || !CanHandle(ecm->caId)) return false; //so.Flush(); -// cMutexLock lock(this); + // cMutexLock lock(this); //newcw[cardnum] =0; unsigned char capmt[2048]; PRINTF(L_CC_CCCAM,"Processing ECM.... for card %d",cardnum); @@ -245,36 +161,29 @@ bool cCardClientCCcam::ProcessECM(const cEcmInfo *ecm, const unsigned char *data //PRINTF(L_CC_CCCAM,"Card num beign used... - %d, pid %d progid %d provid %d caid %d",cardnum, pid,ecm->prgId, ecm->provId, ecm->caId ); memcpy(capmt,"\x9f\x80\x32\x82\x00\x00", 6); - - capmt[6]=0x01; - //prg-nr - capmt[7]=(unsigned char)((ecm->prgId>>8) & 0xff); - //prg-nr + capmt[6]=0x01; //prg-nr + capmt[7]=(unsigned char)((ecm->prgId>>8) & 0xff); //prg-nr capmt[8]=(unsigned char)(ecm->prgId & 0xff); - capmt[9]=pmtversion; //reserved - version - current/next pmtversion++; - pmtversion%=32; - //0x00; reserved - prg-info len - capmt[10]=(unsigned char)((ecm->prgId>>8) & 0xff); - //0x00; prg-info len + pmtversion%=32; //0x00; reserved - prg-info len + capmt[10]=(unsigned char)((ecm->prgId>>8) & 0xff); //0x00; prg-info len capmt[11]=(unsigned char)(ecm->prgId & 0xff); // this is ASN.1 this is NOT the way you do BER... capmt[12]=0x01; //CMD_OK_DESCRAMBLING; ca pmt command id capmt[13]=0x81; // private descr.. dvbnamespace capmt[14]=0x08; { - int dvb_namespace = cardnum<<8; - capmt[15]=dvb_namespace>>24; - capmt[16]=(dvb_namespace>>16)&0xFF; - capmt[17]=(dvb_namespace>>8)&0xFF; - capmt[18]=dvb_namespace&0xFF; + int dvb_namespace = cardnum<<8; + capmt[15]=dvb_namespace>>24; + capmt[16]=(dvb_namespace>>16)&0xFF; + capmt[17]=(dvb_namespace>>8)&0xFF; + capmt[18]=dvb_namespace&0xFF; } capmt[19]=ecm->transponder>>8; capmt[20]=ecm->transponder&0xFF; capmt[21]=ecm->provId>>8; capmt[22]=ecm->provId&0xFF; - capmt[23]=0x82; // demuxer kram.. capmt[24]=0x02; capmt[25]= 1<>8); capmt[lenpos+1]=(len & 0xff); capmt[4]=((wp-6)>>8) & 0xff; capmt[5]=(wp-6) & 0xff; - if (newcw[cardnum]) - { - cTimeMs k; - if ((k.Now() - timecw[cardnum]) > 3000) - // just too old lets bin em... - newcw[cardnum] = 0; - } - if ((pid != pids[cardnum]) || (ccam_fd[cardnum] == -1)) - { // channel change + if (newcw[cardnum]) { + if ((cTimeMs::Now() - timecw[cardnum]) > 3000) + // just too old lets bin em... + newcw[cardnum] = 0; + } + if ((pid != pids[cardnum]) || (ccam_fd[cardnum] == -1)) { // channel change PRINTF(L_CC_CCCAM,"sending capmts "); pids[cardnum] = pid; newcw[cardnum] =0; @@ -314,40 +219,35 @@ bool cCardClientCCcam::ProcessECM(const cEcmInfo *ecm, const unsigned char *data lLogin(cardnum); Writecapmt(cardnum); usleep(300000); - } + } lastcard = cardnum; lastpid = pid; failedcw=0; int u=0; while ((newcw[cardnum] == 0) && (u++<700)) usleep(1000); // give the card a chance to decode it... - if (newcw[cardnum] == 0) - { + if (newcw[cardnum] == 0) { // somethings up, so we will send capmt again. pids[cardnum] = pid; - // memcpy(capmt,sacapmt[cardnum],2048); + // memcpy(capmt,sacapmt[cardnum],2048); Writecapmt(cardnum); - } + } u=0; while ((newcw[cardnum] == 0) && (u++<1000)) usleep(1000); - if (newcw[cardnum] == 0) - { + if (newcw[cardnum] == 0) { PRINTF(L_CC_CCCAM,"FAILED ECM FOR CARD %d !!!!!!!!!!!!!!!!!!",cardnum); - sacapmt[cardnum][9]=pmtversion; //reserved - version - current/next - pmtversion++; - pmtversion%=32; + sacapmt[cardnum][9]=pmtversion; //reserved - version - current/next + pmtversion++; + pmtversion%=32; failedcw++; - if (failedcw == 10) - { + if (failedcw == 10) { // CCcam is having problems lets mark it for a restart.... - FILE *i = fopen("/tmp/killCCcam","w+"); - fclose(i); - - + FILE *i = fopen("/tmp/killCCcam","w+"); + fclose(i); } return false; - } + } memcpy(cw,savedcw[cardnum],16); newcw[cardnum] =0; PRINTF(L_CC_CCCAM," GOT CW FOR CARD %d !!!!!!!!!!!!!!!!!!",cardnum); @@ -355,11 +255,10 @@ bool cCardClientCCcam::ProcessECM(const cEcmInfo *ecm, const unsigned char *data pmtversion++; pmtversion%=32; failedcw=0; - Writecapmt(cardnum); + Writecapmt(cardnum); return true; } - void cCardClientCCcam::Writecapmt(int n) { int len; @@ -370,17 +269,15 @@ void cCardClientCCcam::Writecapmt(int n) len |= sacapmt[n][5]; len += 6; int retry = 1; - while (retry) - { - PRINTF(L_CC_CCCAM,"Writing capmt for card %d %d =================================================",n, sacapmt[n][6]); - if (ccam_fd[n]==-1) - lLogin(n); - if (ccam_fd[n]==-1) - return ; - - int r = write(ccam_fd[n], &sacapmt[n],len ); - if (r != len) - { + while (retry) { + PRINTF(L_CC_CCCAM,"Writing capmt for card %d %d =================================================",n, sacapmt[n][6]); + if (ccam_fd[n]==-1) + lLogin(n); + if (ccam_fd[n]==-1) + return ; + + int r = write(ccam_fd[n], &sacapmt[n],len ); + if (r != len) { PRINTF(L_CC_CCCAM,"CCcam probably has crashed or been killed..."); close(ccam_fd[n]); ccam_fd[n]=-1; @@ -388,59 +285,38 @@ void cCardClientCCcam::Writecapmt(int n) if (retry > 2) retry =0; } - else - retry =0; - } -} - - -int cCardClientCCcam::startportlistener() -{ - PRINTF(L_CC_CCCAM,"starting UDP listener"); - SetDescription("CCcamd listener..."); - Start(); - - return 1; + else + retry =0; + } } - void cCardClientCCcam::Action() { unsigned char cw[18]; - while (1) - { - if (GetCCamCw(cw,0)) - { - int pid; - cTimeMs k; - pid = cw[0] + (cw[1]<<8); + while(Running()) { + if (GetCCamCw(cw,0)) { PRINTF(L_CC_CCCAM," Got: %02hhx%02hhx %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", - cw[0], cw[1], cw[2], cw[3], - cw[4], cw[5], cw[6], cw[7], - cw[8], cw[9], cw[10], cw[11], - cw[12], cw[13], cw[14], cw[15], cw[16], cw[17]); + cw[0],cw[1], + cw[2],cw[3],cw[4],cw[5],cw[6],cw[7],cw[8],cw[9], + cw[10],cw[11],cw[12],cw[13],cw[14],cw[15],cw[16],cw[17]); - if (cw[1] == 0x0f) - { + if (cw[1] == 0x0f) { int n = cw[0]; - if (memcmp(savedcw[n],cw+2,16)) - { - if (newcw[n] == 1) - { + if (memcmp(savedcw[n],cw+2,16)) { + if (newcw[n] == 1) { close(ccam_fd[n]); ccam_fd[n]=-1; - } + } memcpy(savedcw[n],cw+2,16); newcw[n] =1; - timecw[n]= k.Now(); + timecw[n]= cTimeMs::Now(); PRINTF(L_CC_CCCAM,"SAVING KEYS USING PID FROM CCCAM %d !!!!!!!!!!!", n); + } } } } - } } - bool cCardClientCCcam::GetCCamCw(unsigned char *cw, int s) { unsigned char msg[18]; @@ -458,199 +334,17 @@ bool cCardClientCCcam::GetCCamCw(unsigned char *cw, int s) return(true); } - int cCardClientCCcam::CCSelect(int sd, bool forRead, int s) { - if(sd>=0) - { + if(sd>=0) { fd_set fds; FD_ZERO(&fds); FD_SET(sd,&fds); struct timeval tv; tv.tv_sec=4; tv.tv_usec=000; int r=::select(sd+1,forRead ? &fds:0,forRead ? 0:&fds,0,&tv); if(r>0) return 1; - else if(r<0) - { - return 0; + else if(r<0) return 0; + else return 0; } - else - { - return 0; - } - } return 0; } - - -int caid_count =1 ; -int parse_ca_pmt ( unsigned char * buffer, unsigned int length) -{ - unsigned short i, j; - ca_pmt * pmt; - descrambleservice_s service; - - memset(&service, 0, sizeof(descrambleservice_s)); - - pmt = (ca_pmt *) malloc(sizeof(ca_pmt)); - - pmt->ca_pmt_list_management = buffer[0]; - pmt->program_number = (buffer[1] << 8) | buffer[2]; - pmt->program_info_length = ((buffer[4] & 0x0F) << 8) | buffer[5]; - - printf("ca_pmt_list_management: %02x\n", pmt->ca_pmt_list_management); - printf("prugram number: %04x\n", pmt->program_number); - printf("program_info_length: %04x\n", pmt->program_info_length); - switch (pmt->ca_pmt_list_management) - { - case 0x01: /* first */ - printf("first\n"); - break; - case 0x03: /* only */ - printf("only\n"); - break; - - default: - printf("unknown\n"); - break; - } - - if (pmt->program_info_length != 0) - { - pmt->program_info = (ca_pmt_program_info *) malloc(sizeof(ca_pmt_program_info)); - pmt->program_info->ca_pmt_cmd_id = buffer[6]; - printf("ca_pmt_id: %04x\n", pmt->program_info->ca_pmt_cmd_id); - pmt->program_info->descriptor = (ca_descriptor *) malloc(sizeof(ca_descriptor)); - - for (i = 0; i < pmt->program_info_length - 1; i += pmt->program_info->descriptor->descriptor_length + 2) - { - pmt->program_info->descriptor->descriptor_length = buffer[i + 8]; - pmt->program_info->descriptor->ca_system_id = (buffer[i + 9] << 8) | buffer[i + 10]; - pmt->program_info->descriptor->ca_pid = ((buffer[i + 11] & 0x1F) << 8)| buffer[i + 12]; - printf("ca_descriptor_length %d\n",pmt->program_info->descriptor->descriptor_length); - printf("ca_system_id: %04x\n", pmt->program_info->descriptor->ca_system_id); - printf("ca_pid: %04x\n", pmt->program_info->descriptor->ca_pid); - - } - - free(pmt->program_info->descriptor); - free(pmt->program_info); - } - - pmt->es_info = (ca_pmt_es_info *) malloc(sizeof(ca_pmt_es_info)); - - for (i = pmt->program_info_length + 6; i < length; i += pmt->es_info->es_info_length + 5) - { - if (service.numpids == MAX_PIDS) - { - break; - } - - pmt->es_info->stream_type = buffer[i]; - pmt->es_info->elementary_pid = ((buffer[i + 1] & 0x1F) << 8) | buffer[i + 2]; - pmt->es_info->es_info_length = ((buffer[i + 3] & 0x0F) << 8) | buffer[i + 4]; - - printf("stream_type: %02x\n", pmt->es_info->stream_type); - printf("elementary_pid: %04x\n", pmt->es_info->elementary_pid); - printf("es_info_length: %04x\n", pmt->es_info->es_info_length); - - service.pid[service.numpids++] = pmt->es_info->elementary_pid; - - if (pmt->es_info->es_info_length != 0) - { - pmt->es_info->program_info = (ca_pmt_program_info *) malloc(sizeof(ca_pmt_program_info)); - - pmt->es_info->program_info->ca_pmt_cmd_id = buffer[i + 5]; - pmt->es_info->program_info->descriptor = (ca_descriptor *)malloc(sizeof(ca_descriptor)); - - for (j = 0; j < pmt->es_info->es_info_length - 1; j += pmt->es_info->program_info->descriptor->descriptor_length + 2) - { - pmt->es_info->program_info->descriptor->descriptor_length = buffer[i + j + 7]; - pmt->es_info->program_info->descriptor->ca_system_id = (buffer[i + j + 8] << 8) | buffer[i + j + 9]; - pmt->es_info->program_info->descriptor->ca_pid = ((buffer[i + j + 10] & 0x1F) << 8) | buffer[i + j + 11]; - - printf("ca_system_id: %04x\n", pmt->es_info->program_info->descriptor->ca_system_id); - printf("ca_pid: %04x\n", pmt->es_info->program_info->descriptor->ca_pid); - - if (service.caID != 0) - { - break; - } - } - - free(pmt->es_info->program_info->descriptor); - free(pmt->es_info->program_info); - } - } - - service.sID = pmt->program_number; - - free(pmt->es_info); - free(pmt); - - if ((service.numpids != 0) && (service.caID != 0)) - { - service.onID = 0x0085; - - service.Unkwn = 0x0104; - - return 0; // return adddescrambleservicestruct(&service); - } - else - { - return -1; - } -} - - -unsigned int parse_length_field (unsigned char * buffer) -{ - unsigned char size_indicator = (buffer[0] >> 7) & 0x01; - unsigned int length_value = 0; - - if (size_indicator == 0) - { - length_value = buffer[0] & 0x7F; - } - - else if (size_indicator == 1) - { - unsigned char length_field_size = buffer[0] & 0x7F; - unsigned int i; - - for (i = 0; i < length_field_size; i++) - { - length_value = (length_value << 8) | buffer[i + 1]; - } - } - - return length_value; -} - - -unsigned char get_length_field_size (unsigned int length) -{ - if (length < 0x80) - { - return 0x01; - } - - if (length < 0x100) - { - return 0x02; - } - - if (length < 0x10000) - { - return 0x03; - } - - if (length < 0x1000000) - { - return 0x04; - } - - else - { - return 0x05; - } -}