1 | /**
|
---|
2 | * dvbapi-support for Matrix Cam Air
|
---|
3 | *
|
---|
4 | * The code is based partially on module-dvbapi-azbox.
|
---|
5 | * the (closed source) oscam that comes with the MCA is
|
---|
6 | * apparently based on svn-revision 5124-5242.
|
---|
7 | * DEMUXMATRIX is essentially the old DEMUXTYPE which
|
---|
8 | * has changed since then.
|
---|
9 | *
|
---|
10 | * We may be able to implement add/remove-filter
|
---|
11 | * by adding/removing them from DEMUXMATRIX
|
---|
12 | * and reexecute mca_write_flt
|
---|
13 | * In some cases the MCA will send ECMs for multiple PIDS
|
---|
14 | * So it is apparently able to handle multiple filters
|
---|
15 | *
|
---|
16 | * @author dirtyharry123
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "globals.h"
|
---|
20 |
|
---|
21 | #if defined(HAVE_DVBAPI) && defined(WITH_MCA)
|
---|
22 |
|
---|
23 | //#include "extapi/openxcas/openxcas_api.h"
|
---|
24 | //#include "extapi/openxcas/openxcas_message.h"
|
---|
25 |
|
---|
26 | #include "module-dvbapi.h"
|
---|
27 | #include "module-dvbapi-mca.h"
|
---|
28 | #include "oscam-client.h"
|
---|
29 | #include "oscam-ecm.h"
|
---|
30 | #include "oscam-reader.h"
|
---|
31 | #include "oscam-string.h"
|
---|
32 | #include "oscam-time.h"
|
---|
33 |
|
---|
34 | #define LOG_PREFIX "mca: "
|
---|
35 | #define LOG_PREFIX_MSG "mcamsg: "
|
---|
36 |
|
---|
37 | int8_t dummy(void){ return 0;}
|
---|
38 | #define openxcas_start_filter_ex(...) dummy()
|
---|
39 | #define openxcas_stop_filter_ex(...) dummy()
|
---|
40 | #define openxcas_get_message mca_get_message
|
---|
41 | #define azbox_openxcas_ex_callback mca_ex_callback
|
---|
42 | #define openxcas_stop_filter(...) do { } while(0)
|
---|
43 | #define openxcas_remove_filter(...) do { } while(0)
|
---|
44 | #define openxcas_destory_cipher_ex(...) do { } while(0)
|
---|
45 |
|
---|
46 | // These variables are declared in module-dvbapi.c
|
---|
47 | extern void * dvbapi_client;
|
---|
48 | extern DEMUXTYPE demux[MAX_DEMUX];
|
---|
49 |
|
---|
50 | // These are used in module-dvbapi.c
|
---|
51 | int32_t openxcas_provid;
|
---|
52 | uint16_t openxcas_sid, openxcas_caid, openxcas_ecm_pid;
|
---|
53 |
|
---|
54 | static unsigned char openxcas_cw[16];
|
---|
55 | static int32_t openxcas_seq, openxcas_filter_idx, openxcas_stream_id, openxcas_cipher_idx, openxcas_busy;
|
---|
56 | static uint16_t openxcas_video_pid, openxcas_audio_pid, openxcas_data_pid;
|
---|
57 | static uint8_t found[MAX_DEMUX];
|
---|
58 |
|
---|
59 | int fd_mdvbi = -1;
|
---|
60 | int fd_mdesc = -1;
|
---|
61 | int fd_mflt = -1;
|
---|
62 |
|
---|
63 | int mca_open(void){
|
---|
64 | if((fd_mdvbi = open(MCA_DVBI, O_RDONLY)) < 0){
|
---|
65 | cs_log(LOG_PREFIX "can't open \"%s\" (err=%d %s)", MCA_DVBI, errno, strerror(errno));
|
---|
66 | return -1;
|
---|
67 | }
|
---|
68 | if((fd_mdesc = open(MCA_DESC, O_WRONLY)) < 0){
|
---|
69 | cs_log(LOG_PREFIX "can't open \"%s\" (err=%d %s)", MCA_DESC, errno, strerror(errno));
|
---|
70 | return -1;
|
---|
71 | }
|
---|
72 | if((fd_mflt = open(MCA_FLT, O_WRONLY)) < 0){
|
---|
73 | cs_log(LOG_PREFIX "can't open \"%s\" (err=%d %s)", MCA_FLT, errno, strerror(errno));
|
---|
74 | return -1;
|
---|
75 | }
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int mca_exit(void){
|
---|
80 | if((fd_mdvbi = close(fd_mdvbi)) < 0){
|
---|
81 | cs_log(LOG_PREFIX "can't close \"%s\" (err=%d %s)", MCA_DVBI, errno, strerror(errno));
|
---|
82 | return -1;
|
---|
83 | }
|
---|
84 | if((fd_mdvbi = close(fd_mdesc)) < 0){
|
---|
85 | cs_log(LOG_PREFIX "can't close \"%s\" (err=%d %s)", MCA_DESC, errno, strerror(errno));
|
---|
86 | return -1;
|
---|
87 | }
|
---|
88 | if((fd_mdvbi = close(fd_mflt)) < 0){
|
---|
89 | cs_log(LOG_PREFIX "can't close \"%s\" (err=%d %s)", MCA_FLT, errno, strerror(errno));
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void mca_init(void){
|
---|
96 | if (mca_open() < 0) {
|
---|
97 | cs_log(LOG_PREFIX "could not init");
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | void mca_close(void){
|
---|
102 | if (mca_exit() < 0) {
|
---|
103 | cs_log(LOG_PREFIX "could not close");
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | int mca_get_message(openxcas_msg_t * message, int timeout){
|
---|
108 | int rval=-1;
|
---|
109 | struct pollfd mdvbi_poll_fd;
|
---|
110 | mdvbi_poll_fd.fd = fd_mdvbi;
|
---|
111 | mdvbi_poll_fd.events = POLLIN | POLLPRI;
|
---|
112 | rval=poll(&mdvbi_poll_fd, 1, timeout == 0 ? -1 : timeout);
|
---|
113 | if((rval >= 1) && (mdvbi_poll_fd.revents & (POLLIN | POLLPRI))){
|
---|
114 | rval = read(fd_mdvbi, message, 568);
|
---|
115 | }
|
---|
116 | else rval=-1;
|
---|
117 | return rval;
|
---|
118 | }
|
---|
119 |
|
---|
120 | int mca_write_flt(DEMUXMATRIX * demux_matrix, int timeout){
|
---|
121 | int rval=-1;
|
---|
122 | struct pollfd mflt_poll_fd;
|
---|
123 | mflt_poll_fd.fd = fd_mflt;
|
---|
124 | mflt_poll_fd.events = POLLOUT;
|
---|
125 | rval=poll(&mflt_poll_fd, 1, timeout);
|
---|
126 | if((rval >= 1) && (mflt_poll_fd.revents & POLLOUT)){
|
---|
127 | rval = write(fd_mflt, demux_matrix, sizeof(DEMUXMATRIX));
|
---|
128 | }
|
---|
129 | else rval=-1;
|
---|
130 | return rval;
|
---|
131 | }
|
---|
132 |
|
---|
133 | int mca_set_key(unsigned char * mca_cw){
|
---|
134 | int rval=-1;
|
---|
135 | struct pollfd mdesc_poll_fd;
|
---|
136 | mdesc_poll_fd.fd = fd_mdesc;
|
---|
137 | mdesc_poll_fd.events = POLLOUT;
|
---|
138 | rval=poll(&mdesc_poll_fd, 1, 0);
|
---|
139 | if((rval >= 1) && (mdesc_poll_fd.revents & POLLOUT)){
|
---|
140 | rval = write(fd_mdesc, mca_cw, 16);
|
---|
141 | }
|
---|
142 | else rval=-1;
|
---|
143 | return rval;
|
---|
144 | }
|
---|
145 |
|
---|
146 | int mca_capmt_remove_duplicates(uchar *capmt, int len){
|
---|
147 | int i,newlen=len;
|
---|
148 | uint16_t descriptor_length = 0;
|
---|
149 | uint32_t program_info_length = ((capmt[4] & 0x0F) << 8) | capmt[5];
|
---|
150 | for (i=7; i<len; i+=descriptor_length +2){
|
---|
151 | descriptor_length = capmt[i+1];
|
---|
152 | if(capmt[i] != 0x09) continue;
|
---|
153 | if(!memcmp(&(capmt[i]), &(capmt[i+descriptor_length+2]), descriptor_length+2)){
|
---|
154 | memmove(&(capmt[i+descriptor_length+2]), &(capmt[i+(2*(descriptor_length+2))]), newlen-(descriptor_length+2));
|
---|
155 | newlen-=descriptor_length+2;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | program_info_length-=(len-newlen);
|
---|
159 | capmt[4] = (uchar)((capmt[4] & 0xF0) | ((program_info_length & 0xF00) >> 8));
|
---|
160 | capmt[5] = (uchar)(program_info_length & 0x0FF);
|
---|
161 | return newlen;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void mca_demux_convert(DEMUXTYPE * demux_orig, DEMUXMATRIX * demux_matrix){
|
---|
165 | int i=0;
|
---|
166 | memset(demux_matrix, 0, sizeof(DEMUXMATRIX));
|
---|
167 | demux_matrix->demux_index = (int32_t)demux_orig->demux_index;
|
---|
168 | for(i=0; i<MAX_FILTER_MATRIX; ++i){
|
---|
169 | demux_matrix->demux_fd[i].fd = (uint32_t) demux_orig->demux_fd[i].fd;
|
---|
170 | demux_matrix->demux_fd[i].pidindex = (int32_t) demux_orig->demux_fd[i].pidindex;
|
---|
171 | demux_matrix->demux_fd[i].pid = (int32_t) demux_orig->demux_fd[i].pid;
|
---|
172 | demux_matrix->demux_fd[i].type = (uint16_t) demux_orig->demux_fd[i].type;
|
---|
173 | demux_matrix->demux_fd[i].count = (int32_t) demux_orig->demux_fd[i].count;
|
---|
174 | }
|
---|
175 | demux_matrix->ca_mask = (int32_t)demux_orig->ca_mask;
|
---|
176 | demux_matrix->adapter_index = (int32_t)demux_orig->adapter_index;
|
---|
177 | demux_matrix->socket_fd = (int32_t)demux_orig->socket_fd;
|
---|
178 | demux_matrix->ECMpidcount = (int32_t)demux_orig->ECMpidcount;
|
---|
179 | for(i=0; i<demux_matrix->ECMpidcount; ++i){
|
---|
180 | demux_matrix->ECMpids[i].CAID = (uint16_t)demux_orig->ECMpids[i].CAID;
|
---|
181 | demux_matrix->ECMpids[i].PROVID = (uint32_t)demux_orig->ECMpids[i].PROVID;
|
---|
182 | demux_matrix->ECMpids[i].ECM_PID = (uint16_t)demux_orig->ECMpids[i].ECM_PID;
|
---|
183 | demux_matrix->ECMpids[i].EMM_PID = (uint16_t)demux_orig->ECMpids[i].EMM_PID;
|
---|
184 | demux_matrix->ECMpids[i].irdeto_numchids = (int32_t)demux_orig->ECMpids[i].irdeto_chids;
|
---|
185 | demux_matrix->ECMpids[i].irdeto_curchid = (int32_t)demux_orig->ECMpids[i].irdeto_chids;
|
---|
186 | demux_matrix->ECMpids[i].irdeto_chid = (int32_t)demux_orig->ECMpids[i].irdeto_chids;
|
---|
187 | demux_matrix->ECMpids[i].checked = (int32_t)demux_orig->ECMpids[i].checked;
|
---|
188 | demux_matrix->ECMpids[i].status = (int32_t)demux_orig->ECMpids[i].status;
|
---|
189 | demux_matrix->ECMpids[i].table = (unsigned char)demux_orig->ECMpids[i].table;
|
---|
190 | demux_matrix->ECMpids[i].streams = (uint32_t)demux_orig->ECMpids[i].streams;
|
---|
191 | }
|
---|
192 | demux_matrix->STREAMpidcount = (int32_t)demux->STREAMpidcount;
|
---|
193 | memcpy(&demux_matrix->STREAMpids, &demux_orig->STREAMpids, demux_matrix->STREAMpidcount * sizeof(uint16_t));
|
---|
194 | demux_matrix->pidindex = (int32_t)demux_orig->pidindex;
|
---|
195 | demux_matrix->curindex = (int32_t)demux_orig->curindex;
|
---|
196 | demux_matrix->tries = (int32_t)demux_orig->tries;
|
---|
197 | demux_matrix->max_status = (int32_t)demux_orig->max_status;
|
---|
198 | demux_matrix->program_number = (uint16_t)demux_orig->program_number;
|
---|
199 | memcpy(&demux_matrix->lastcw, &demux_orig->lastcw, 2 * 8 * sizeof(unsigned char));
|
---|
200 | demux_matrix->emm_filter = (int32_t)demux_orig->emm_filter;
|
---|
201 | memcpy(&demux_matrix->hexserial, &demux_orig->hexserial, 8 * sizeof(uchar));
|
---|
202 | demux_matrix->rdr = (struct s_reader *)demux_orig->rdr;
|
---|
203 | memcpy(&demux_matrix->pmt_file, &demux_orig->pmt_file, 30);
|
---|
204 | demux_matrix->pmt_time = (int32_t)demux_orig->pmt_time;
|
---|
205 | }
|
---|
206 |
|
---|
207 | void mca_ecm_callback(int32_t stream_id, uint32_t UNUSED(seq), int32_t cipher_index, uint32_t caid, unsigned char *ecm_data, int32_t l, uint16_t pid) {
|
---|
208 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ecm callback received");
|
---|
209 |
|
---|
210 | openxcas_stream_id = stream_id;
|
---|
211 | //openxcas_seq = seq;
|
---|
212 | //openxcas_caid = caid;
|
---|
213 | openxcas_ecm_pid = pid;
|
---|
214 | openxcas_busy = 1;
|
---|
215 | //char tmp[1024];
|
---|
216 |
|
---|
217 | //As soon as we have received a valid CW we lock onto that CAID, otherwise we will have freezers.
|
---|
218 | if (openxcas_caid && openxcas_caid != caid){
|
---|
219 | cs_log(LOG_PREFIX "ignoring caid: %04X, waiting for %04X", caid, openxcas_caid);
|
---|
220 | openxcas_busy = 0;
|
---|
221 | return;
|
---|
222 | }
|
---|
223 |
|
---|
224 | ECM_REQUEST *er;
|
---|
225 | if (!(er=get_ecmtask()))
|
---|
226 | return;
|
---|
227 |
|
---|
228 | er->srvid = openxcas_sid;
|
---|
229 | er->caid = openxcas_caid;
|
---|
230 | er->pid = openxcas_ecm_pid;
|
---|
231 | er->prid = openxcas_provid;
|
---|
232 |
|
---|
233 | er->ecmlen = l;
|
---|
234 | memcpy(er->ecm, ecm_data, er->ecmlen);
|
---|
235 |
|
---|
236 | request_cw(dvbapi_client, er);
|
---|
237 |
|
---|
238 | openxcas_stop_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
239 | openxcas_remove_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
240 |
|
---|
241 | openxcas_cipher_idx = cipher_index;
|
---|
242 |
|
---|
243 | struct timeb tp;
|
---|
244 | cs_ftime(&tp);
|
---|
245 | tp.time+=500;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | void mca_ex_callback(int32_t stream_id, uint32_t UNUSED(seq), int32_t idx, uint32_t pid, unsigned char *ecm_data, int32_t l) {
|
---|
250 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ex callback received");
|
---|
251 |
|
---|
252 | openxcas_stream_id = stream_id;
|
---|
253 | openxcas_ecm_pid = pid;
|
---|
254 | openxcas_cipher_idx = idx; // is this really cipher_idx?
|
---|
255 |
|
---|
256 | ECM_REQUEST *er;
|
---|
257 | if (!(er=get_ecmtask()))
|
---|
258 | return;
|
---|
259 |
|
---|
260 | er->srvid = openxcas_sid;
|
---|
261 | er->caid = openxcas_caid;
|
---|
262 | er->pid = openxcas_ecm_pid;
|
---|
263 | er->prid = openxcas_provid;
|
---|
264 |
|
---|
265 | er->ecmlen = l;
|
---|
266 | memcpy(er->ecm, ecm_data, er->ecmlen);
|
---|
267 |
|
---|
268 | request_cw(dvbapi_client, er);
|
---|
269 |
|
---|
270 | if (openxcas_stop_filter_ex(stream_id, seq, openxcas_filter_idx) < 0)
|
---|
271 | cs_log(LOG_PREFIX "unable to stop ex filter");
|
---|
272 | else
|
---|
273 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ex filter stopped");
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|
277 | unsigned char mask[12];
|
---|
278 | unsigned char comp[12];
|
---|
279 | memset(&mask, 0x00, sizeof(mask));
|
---|
280 | memset(&comp, 0x00, sizeof(comp));
|
---|
281 |
|
---|
282 | mask[0] = 0xff;
|
---|
283 | comp[0] = ecm_data[0] ^ 1;
|
---|
284 |
|
---|
285 | if ((openxcas_filter_idx = openxcas_start_filter_ex(stream_id, seq, openxcas_ecm_pid, mask, comp, (void *)azbox_openxcas_ex_callback)) < 0)
|
---|
286 | cs_log(LOG_PREFIX "unable to start ex filter");
|
---|
287 | else
|
---|
288 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ex filter started, pid = %x", openxcas_ecm_pid);
|
---|
289 | }
|
---|
290 |
|
---|
291 | void * mca_main_thread(void *cli) {
|
---|
292 | struct s_client * client = (struct s_client *) cli;
|
---|
293 | client->thread=pthread_self();
|
---|
294 | pthread_setspecific(getclient, cli);
|
---|
295 | dvbapi_client=cli;
|
---|
296 |
|
---|
297 | struct s_auth *account;
|
---|
298 | int32_t ok = 0;
|
---|
299 | for (account = cfg.account; account; account=account->next) {
|
---|
300 | if ((ok = streq(cfg.dvbapi_usr, account->usr)))
|
---|
301 | break;
|
---|
302 | }
|
---|
303 | cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
|
---|
304 |
|
---|
305 | dvbapi_read_priority();
|
---|
306 |
|
---|
307 | openxcas_msg_t msg;
|
---|
308 | int32_t ret;
|
---|
309 | while ((ret = openxcas_get_message(&msg, 0)) >= 0) {
|
---|
310 | cs_sleepms(10);
|
---|
311 |
|
---|
312 | if (ret) {
|
---|
313 | openxcas_stream_id = msg.stream_id;
|
---|
314 | openxcas_seq = msg.sequence;
|
---|
315 | struct stOpenXCAS_Data data;
|
---|
316 |
|
---|
317 | switch(msg.cmd) {
|
---|
318 | case OPENXCAS_SELECT_CHANNEL:
|
---|
319 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_SELECT_CHANNEL");
|
---|
320 |
|
---|
321 | // parse channel info
|
---|
322 | struct stOpenXCASChannel chan;
|
---|
323 | memcpy(&chan, msg.buf, msg.buf_len);
|
---|
324 |
|
---|
325 | cs_log(LOG_PREFIX "channel change: sid = %x, vpid = %x. apid = %x", chan.service_id, chan.v_pid, chan.a_pid);
|
---|
326 |
|
---|
327 | openxcas_video_pid = chan.v_pid;
|
---|
328 | openxcas_audio_pid = chan.a_pid;
|
---|
329 | openxcas_data_pid = chan.d_pid;
|
---|
330 | break;
|
---|
331 | case OPENXCAS_START_PMT_ECM:
|
---|
332 | //FIXME: Apparently this is what the original MCA-oscam does
|
---|
333 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_STOP_PMT_ECM");
|
---|
334 | memset(&demux, 0, sizeof(demux));
|
---|
335 | memset(&found, 0, sizeof(found));
|
---|
336 |
|
---|
337 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_START_PMT_ECM");
|
---|
338 |
|
---|
339 | // parse pmt
|
---|
340 | cs_ddump_mask(D_DVBAPI, msg.buf + 2, msg.buf_len -2, LOG_PREFIX "capmt:");
|
---|
341 | // For some reason the mca sometimes sends duplicate ECMpids,
|
---|
342 | // we remove them here so dvbapi will not try them twice.
|
---|
343 | int new_len = mca_capmt_remove_duplicates(msg.buf + 2, msg.buf_len -2);
|
---|
344 | if(new_len < msg.buf_len - 2)
|
---|
345 | cs_ddump_mask(D_DVBAPI, msg.buf + 2, new_len, LOG_PREFIX "capmt (duplicates removed):");
|
---|
346 | int demux_id = dvbapi_parse_capmt(msg.buf + 2, new_len, -1, NULL);
|
---|
347 |
|
---|
348 |
|
---|
349 | unsigned char mask[12];
|
---|
350 | unsigned char comp[12];
|
---|
351 | memset(&mask, 0x00, sizeof(mask));
|
---|
352 | memset(&comp, 0x00, sizeof(comp));
|
---|
353 |
|
---|
354 | mask[0] = 0xfe;
|
---|
355 | comp[0] = 0x80;
|
---|
356 |
|
---|
357 | if (demux_id < 0){
|
---|
358 | cs_log(LOG_PREFIX "could not parse pmt");
|
---|
359 | break;
|
---|
360 | }
|
---|
361 |
|
---|
362 | //if ((ret = openxcas_add_filter(msg.stream_id, OPENXCAS_FILTER_ECM, 0, 0xffff, openxcas_ecm_pid, mask, comp, (void *)mca_ecm_callback)) < 0)
|
---|
363 | DEMUXMATRIX demux_matrix;
|
---|
364 | mca_demux_convert(&demux[demux_id], &demux_matrix);
|
---|
365 | if ((ret = mca_write_flt(&demux_matrix, 0)) < 0)
|
---|
366 | cs_log(LOG_PREFIX "unable to add ecm filter");
|
---|
367 | else{
|
---|
368 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, 0);
|
---|
369 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ecm filter started");
|
---|
370 | }
|
---|
371 |
|
---|
372 | //if (!openxcas_create_cipher_ex(msg.stream_id, openxcas_seq, 0, openxcas_ecm_pid, openxcas_video_pid, 0xffff, openxcas_audio_pid, 0xffff, 0xffff, 0xffff))
|
---|
373 | // cs_log(LOG_PREFIX "failed to create cipher ex");
|
---|
374 | //else
|
---|
375 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "cipher created");
|
---|
376 | break;
|
---|
377 | case OPENXCAS_STOP_PMT_ECM:
|
---|
378 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_STOP_PMT_ECM");
|
---|
379 | openxcas_stop_filter(msg.stream_id, OPENXCAS_FILTER_ECM);
|
---|
380 | openxcas_remove_filter(msg.stream_id, OPENXCAS_FILTER_ECM);
|
---|
381 | openxcas_stop_filter_ex(msg.stream_id, msg.sequence, openxcas_filter_idx);
|
---|
382 | openxcas_destory_cipher_ex(msg.stream_id, msg.sequence);
|
---|
383 | memset(&demux, 0, sizeof(demux));
|
---|
384 | memset(&found, 0, sizeof(found));
|
---|
385 | break;
|
---|
386 | case OPENXCAS_ECM_CALLBACK:
|
---|
387 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_ECM_CALLBACK");
|
---|
388 | memcpy(&data, msg.buf, msg.buf_len);
|
---|
389 | if (!openxcas_busy)
|
---|
390 | //openxcas_filter_callback(msg.stream_id, msg.sequence, OPENXCAS_FILTER_ECM, &data);
|
---|
391 | mca_ecm_callback(msg.stream_id, msg.sequence, data.cipher_index, data.ca_system_id, (unsigned char*)&data.buf, data.len, data.pid);
|
---|
392 | break;
|
---|
393 | case OPENXCAS_PID_FILTER_CALLBACK:
|
---|
394 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_PID_FILTER_CALLBACK");
|
---|
395 | memcpy(&data, msg.buf, msg.buf_len);
|
---|
396 | //openxcas_filter_callback_ex(msg.stream_id, msg.sequence, (struct stOpenXCAS_Data *)msg.buf);
|
---|
397 | mca_ex_callback(msg.stream_id, msg.sequence, data.cipher_index, data.pid, (unsigned char*)&data.buf, data.len);
|
---|
398 | break;
|
---|
399 | case OPENXCAS_QUIT:
|
---|
400 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_QUIT");
|
---|
401 | mca_exit();
|
---|
402 | cs_log(LOG_PREFIX "exited");
|
---|
403 | return NULL;
|
---|
404 | break;
|
---|
405 | case OPENXCAS_UKNOWN_MSG:
|
---|
406 | default:
|
---|
407 | cs_debug_mask(D_DVBAPI, LOG_PREFIX_MSG "OPENXCAS_UKNOWN_MSG (%d)", msg.cmd);
|
---|
408 | //cs_ddump_mask(D_DVBAPI, &msg, sizeof(msg), "msg dump:");
|
---|
409 | break;
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 | cs_log(LOG_PREFIX "invalid message");
|
---|
414 | return NULL;
|
---|
415 | }
|
---|
416 |
|
---|
417 | void mca_send_dcw(struct s_client *client, ECM_REQUEST *er) {
|
---|
418 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "send_dcw");
|
---|
419 |
|
---|
420 | FILE *ecmtxt;
|
---|
421 | if ((ecmtxt = fopen(ECMINFO_FILE, "w"))) {
|
---|
422 | char tmp[25];
|
---|
423 | if(er->rc <= E_CACHEEX) {
|
---|
424 | fprintf(ecmtxt, "caid: 0x%04X\npid: 0x%04X\nprov: 0x%06X\n", er->caid, er->pid, (uint) er->prid);
|
---|
425 | fprintf(ecmtxt, "reader: %s\n", er->selected_reader->label);
|
---|
426 | if (is_cascading_reader(er->selected_reader))
|
---|
427 | fprintf(ecmtxt, "from: %s\n", er->selected_reader->device);
|
---|
428 | else
|
---|
429 | fprintf(ecmtxt, "from: local\n");
|
---|
430 | fprintf(ecmtxt, "protocol: %s\n", reader_get_type_desc(er->selected_reader, 1));
|
---|
431 | fprintf(ecmtxt, "hops: %d\n", er->selected_reader->currenthops);
|
---|
432 | fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime/1000);
|
---|
433 | fprintf(ecmtxt, "cw0: %s\n", cs_hexdump(1,demux[0].lastcw[0],8, tmp, sizeof(tmp)));
|
---|
434 | fprintf(ecmtxt, "cw1: %s\n", cs_hexdump(1,demux[0].lastcw[1],8, tmp, sizeof(tmp)));
|
---|
435 | fclose(ecmtxt);
|
---|
436 | ecmtxt = NULL;
|
---|
437 | } else {
|
---|
438 | fprintf(ecmtxt, "ECM information not found\n");
|
---|
439 | fclose(ecmtxt);
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | openxcas_busy = 0;
|
---|
444 |
|
---|
445 | int32_t i;
|
---|
446 | for (i=0; i < MAX_DEMUX; i++) {
|
---|
447 |
|
---|
448 | if (er->rc >= E_NOTFOUND && !found[i]) {
|
---|
449 | cs_debug_mask(D_DVBAPI, "cw not found");
|
---|
450 |
|
---|
451 | if (demux[i].pidindex==-1)
|
---|
452 | dvbapi_try_next_caid(i);
|
---|
453 |
|
---|
454 | openxcas_stop_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
455 | openxcas_remove_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
456 |
|
---|
457 | unsigned char mask[12];
|
---|
458 | unsigned char comp[12];
|
---|
459 | memset(&mask, 0x00, sizeof(mask));
|
---|
460 | memset(&comp, 0x00, sizeof(comp));
|
---|
461 |
|
---|
462 | mask[0] = 0xfe;
|
---|
463 | comp[0] = 0x80;
|
---|
464 |
|
---|
465 | DEMUXMATRIX demux_matrix;
|
---|
466 | mca_demux_convert(&demux[0], &demux_matrix);
|
---|
467 | if (mca_write_flt(&demux_matrix, 0) < 0)
|
---|
468 | cs_log(LOG_PREFIX "unable to add ecm filter (0)");
|
---|
469 | else{
|
---|
470 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, 0);
|
---|
471 | cs_debug_mask(D_DVBAPI, LOG_PREFIX "ecm filter started");
|
---|
472 | }
|
---|
473 |
|
---|
474 | return;
|
---|
475 | }
|
---|
476 | else{
|
---|
477 | found[i]=1;
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | unsigned char nullcw[8];
|
---|
482 | memset(nullcw, 0, 8);
|
---|
483 |
|
---|
484 | int32_t n;
|
---|
485 | for (n=0;n<2;n++) {
|
---|
486 | if (memcmp(er->cw + (n * 8), demux[0].lastcw[n], 8) && memcmp(er->cw + (n * 8), nullcw, 8)) {
|
---|
487 | memcpy(demux[0].lastcw[n], er->cw + (n * 8), 8);
|
---|
488 | memcpy(openxcas_cw + (n * 8), er->cw + (n * 8), 8);
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | if (mca_set_key(openxcas_cw) < 0)
|
---|
493 | cs_log(LOG_PREFIX "set cw failed");
|
---|
494 | else
|
---|
495 | cs_ddump_mask(D_DVBAPI, openxcas_cw, 16, LOG_PREFIX "write cws to descrambler");
|
---|
496 | }
|
---|
497 |
|
---|
498 | #endif
|
---|