source: trunk/module-dvbapi.c@ 10647

Last change on this file since 10647 was 10647, checked in by theparasol, 7 years ago
  • Fix for broken stapi timeshift introduced r10640
  • Property svn:eol-style set to LF
File size: 171.0 KB
Line 
1#define MODULE_LOG_PREFIX "dvbapi"
2
3#include "globals.h"
4
5#ifdef HAVE_DVBAPI
6
7#include "module-dvbapi.h"
8#include "module-cacheex.h"
9#include "module-dvbapi-azbox.h"
10#include "module-dvbapi-mca.h"
11#include "module-dvbapi-coolapi.h"
12#include "module-dvbapi-stapi.h"
13#include "module-dvbapi-chancache.h"
14#include "module-stat.h"
15#include "oscam-chk.h"
16#include "oscam-client.h"
17#include "oscam-config.h"
18#include "oscam-ecm.h"
19#include "oscam-emm.h"
20#include "oscam-files.h"
21#include "oscam-net.h"
22#include "oscam-reader.h"
23#include "oscam-string.h"
24#include "oscam-time.h"
25#include "oscam-work.h"
26#include "reader-irdeto.h"
27
28#if defined (__CYGWIN__)
29#define F_NOTIFY 0
30#define F_SETSIG 0
31#define DN_MODIFY 0
32#define DN_CREATE 0
33#define DN_DELETE 0
34#define DN_MULTISHOT 0
35#endif
36
37const char *streamtxt[] = {
38 "", // 00
39 "", // 01
40 "Videostream ", // 02
41 "Audiostream ", // 03
42 "", // 04
43 "", // 05
44 "Datastream ", // 06
45 "", // 07
46 "", // 08
47 "Conditional Access", // 09
48 "", // 0A
49 "", // 0B
50 "", // 0C
51 "", // 0D
52 "", // 0E
53 "", // 0F
54 "", // 10
55 "", // 11
56 "", // 12
57 "", // 13
58 "", // 14
59 "", // 15
60 "", // 16
61 "", // 17
62 "", // 18
63 "", // 19
64 "", // 1A
65 "MPEG-4 videostream", // 1B
66 "MPEG-4 audiostream",
67 };
68
69static int is_samygo;
70
71void flush_read_fd(int32_t demux_index, int32_t num, int fd)
72{
73 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
74 {
75 cs_log_dbg(D_DVBAPI,"Demuxer %d flushing stale input data of filter %d (fd:%d)", demux_index, num + 1, fd);
76 fd_set rd;
77 struct timeval t;
78 char buff[100];
79 t.tv_sec=0;
80 t.tv_usec=0;
81 FD_ZERO(&rd);
82 FD_SET(fd,&rd);
83 while(select(fd+1,&rd,NULL,NULL,&t) > 0)
84 {
85 if (read(fd,buff,100)){;}
86 }
87 }
88}
89
90static int dvbapi_ioctl(int fd, uint32_t request, ...)
91{
92 int ret = 0;
93 va_list args;
94 va_start(args, request);
95 if (!is_samygo)
96 {
97 void *param = va_arg(args, void *);
98 ret = ioctl(fd, request, param);
99 }
100 else
101 {
102 switch(request)
103 {
104 case DMX_SET_FILTER:
105 {
106 struct dmxSctFilterParams *sFP = va_arg(args, struct dmxSctFilterParams *);
107 // prepare packet
108 unsigned char packet[sizeof(request) + sizeof(struct dmxSctFilterParams)];
109 memcpy(&packet, &request, sizeof(request));
110 memcpy(&packet[sizeof(request)], sFP, sizeof(struct dmxSctFilterParams));
111 ret = send(fd, packet, sizeof(packet), 0);
112 break;
113 }
114 case DMX_SET_FILTER1:
115 {
116 struct dmx_sct_filter_params *sFP = va_arg(args, struct dmx_sct_filter_params *);
117 ret = send(fd, sFP, sizeof(struct dmx_sct_filter_params), 0);
118 break;
119 }
120 case DMX_STOP:
121 {
122 ret = send(fd, &request, sizeof(request), 0);
123 ret = 1;
124 break;
125 }
126 case CA_SET_PID:
127 {
128 ret = 1;
129 break;
130 }
131 case CA_SET_DESCR:
132 {
133 ret = 1;
134 break;
135 }
136 }
137 if (ret > 0) // send() may return larger than 1
138 ret = 1;
139 }
140#if defined(__powerpc__)
141 // Old dm500 boxes (ppc old) are using broken kernel, se we need some fixups
142 switch (request)
143 {
144 case DMX_STOP:
145 case CA_SET_DESCR:
146 case CA_SET_PID:
147 ret = 1;
148 }
149#endif
150 // FIXME: Workaround for su980 bug
151 // See: http://www.streamboard.tv/wbb2/thread.php?postid=533940
152 if(boxtype_is("su980"))
153 ret = 1;
154 va_end(args);
155 return ret;
156}
157
158// tunemm_caid_map
159#define FROM_TO 0
160#define TO_FROM 1
161
162int32_t pausecam = 0, disable_pmt_files = 0, pmt_stopmarking = 0, pmthandling = 0;
163DEMUXTYPE demux[MAX_DEMUX];
164struct s_dvbapi_priority *dvbapi_priority;
165struct s_client *dvbapi_client;
166
167const char *boxdesc[] = { "none", "dreambox", "duckbox", "ufs910", "dbox2", "ipbox", "ipbox-pmt", "dm7000", "qboxhd", "coolstream", "neumo", "pc", "pc-nodmx" };
168
169static const struct box_devices devices[BOX_COUNT] =
170{
171 /* QboxHD (dvb-api-3)*/ { "/tmp/virtual_adapter/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
172 /* dreambox (dvb-api-3)*/ { "/dev/dvb/adapter%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
173 /* dreambox (dvb-api-1)*/ { "/dev/dvb/card%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_1 },
174 /* neumo (dvb-api-1)*/ { "/dev/", "demuxapi", "demuxapi", "/tmp/camd.socket", DVBAPI_1 },
175 /* sh4 (stapi)*/ { "/dev/stapi/", "stpti4_ioctl", "stpti4_ioctl", "/tmp/camd.socket", STAPI },
176 /* coolstream*/ { "/dev/cnxt/", "null", "null", "/tmp/camd.socket", COOLAPI }
177};
178
179static int32_t selected_box = -1;
180static int32_t selected_api = -1;
181static int32_t maxfilter = MAX_FILTER;
182static int32_t dir_fd = -1;
183char *client_name = NULL;
184static uint16_t client_proto_version = 0;
185
186static int32_t ca_fd[MAX_DEMUX]; // holds fd handle of each ca device 0 = not in use
187static LLIST * ll_activestreampids; // list of all enabled streampids on ca devices
188
189static int32_t unassoc_fd[MAX_DEMUX];
190
191bool is_dvbapi_usr(char *usr) {
192 return streq(cfg.dvbapi_usr, usr);
193}
194
195struct s_emm_filter
196{
197 int32_t demux_id;
198 uchar filter[32];
199 uint16_t caid;
200 uint32_t provid;
201 uint16_t pid;
202 uint32_t num;
203 struct timeb time_started;
204};
205
206static LLIST *ll_emm_active_filter;
207static LLIST *ll_emm_inactive_filter;
208static LLIST *ll_emm_pending_filter;
209
210int32_t add_emmfilter_to_list(int32_t demux_id, uchar *filter, uint16_t caid, uint32_t provid, uint16_t emmpid, int32_t num, bool enable)
211{
212 if(!ll_emm_active_filter)
213 { ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
214
215 if(!ll_emm_inactive_filter)
216 { ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
217
218 if(!ll_emm_pending_filter)
219 { ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
220
221 struct s_emm_filter *filter_item;
222 if(!cs_malloc(&filter_item, sizeof(struct s_emm_filter)))
223 { return 0; }
224
225 filter_item->demux_id = demux_id;
226 memcpy(filter_item->filter, filter, 32);
227 filter_item->caid = caid;
228 filter_item->provid = provid;
229 filter_item->pid = emmpid;
230 filter_item->num = num;
231 if (enable)
232 {
233 cs_ftime(&filter_item->time_started);
234 }
235 else
236 {
237 memset(&filter_item->time_started, 0, sizeof(filter_item->time_started));
238 }
239
240 if(num > 0)
241 {
242 ll_append(ll_emm_active_filter, filter_item);
243 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d added to active emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
244 filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
245 }
246 else if(num < 0)
247 {
248 ll_append(ll_emm_pending_filter, filter_item);
249 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to pending emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
250 filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
251 }
252 else
253 {
254 ll_append(ll_emm_inactive_filter, filter_item);
255 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to inactive emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
256 filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
257 }
258 return 1;
259}
260
261int32_t is_emmfilter_in_list_internal(LLIST *ll, uchar *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
262{
263 struct s_emm_filter *filter_item;
264 LL_ITER itr;
265 if(ll_count(ll) > 0)
266 {
267 itr = ll_iter_create(ll);
268 while((filter_item = ll_iter_next(&itr)) != NULL)
269 {
270 if(!memcmp(filter_item->filter, filter, 32) && (filter_item->pid == emmpid) && (filter_item->provid == provid) && (filter_item->caid == caid))
271 { return 1; }
272 }
273 }
274 return 0;
275}
276
277int32_t is_emmfilter_in_list(uchar *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
278{
279 if(!ll_emm_active_filter)
280 { ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
281
282 if(!ll_emm_inactive_filter)
283 { ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
284
285 if(!ll_emm_pending_filter)
286 { ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
287
288 if(is_emmfilter_in_list_internal(ll_emm_active_filter, filter, emmpid, provid, caid))
289 { return 1; }
290 if(is_emmfilter_in_list_internal(ll_emm_inactive_filter, filter, emmpid, provid, caid))
291 { return 1; }
292 if(is_emmfilter_in_list_internal(ll_emm_pending_filter, filter, emmpid, provid, caid))
293 { return 1; }
294
295 return 0;
296}
297
298struct s_emm_filter *get_emmfilter_by_filternum_internal(LLIST *ll, int32_t demux_id, uint32_t num)
299{
300 struct s_emm_filter *filter;
301 LL_ITER itr;
302 if(ll_count(ll) > 0)
303 {
304 itr = ll_iter_create(ll);
305 while((filter = ll_iter_next(&itr)))
306 {
307 if(filter->demux_id == demux_id && filter->num == num)
308 { return filter; }
309 }
310 }
311 return NULL;
312}
313
314struct s_emm_filter *get_emmfilter_by_filternum(int32_t demux_id, uint32_t num)
315{
316 if(!ll_emm_active_filter)
317 { ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
318
319 if(!ll_emm_inactive_filter)
320 { ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
321
322 if(!ll_emm_pending_filter)
323 { ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
324
325 struct s_emm_filter *emm_filter = NULL;
326 emm_filter = get_emmfilter_by_filternum_internal(ll_emm_active_filter, demux_id, num);
327 if(emm_filter)
328 { return emm_filter; }
329 emm_filter = get_emmfilter_by_filternum_internal(ll_emm_inactive_filter, demux_id, num);
330 if(emm_filter)
331 { return emm_filter; }
332 emm_filter = get_emmfilter_by_filternum_internal(ll_emm_pending_filter, demux_id, num);
333 if(emm_filter)
334 { return emm_filter; }
335
336 return NULL;
337}
338
339int8_t remove_emmfilter_from_list_internal(LLIST *ll, int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
340{
341 struct s_emm_filter *filter;
342 LL_ITER itr;
343 if(ll_count(ll) > 0)
344 {
345 itr = ll_iter_create(ll);
346 while((filter = ll_iter_next(&itr)))
347 {
348 if(filter->demux_id == demux_id && filter->caid == caid && filter->provid == provid && filter->pid == pid && filter->num == num)
349 {
350 ll_iter_remove_data(&itr);
351 return 1;
352 }
353 }
354 }
355 return 0;
356}
357
358void remove_emmfilter_from_list(int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
359{
360 if(ll_emm_active_filter && remove_emmfilter_from_list_internal(ll_emm_active_filter, demux_id, caid, provid, pid, num))
361 { return; }
362 if(ll_emm_inactive_filter && remove_emmfilter_from_list_internal(ll_emm_inactive_filter, demux_id, caid, provid, pid, num))
363 { return; }
364 if(ll_emm_pending_filter && remove_emmfilter_from_list_internal(ll_emm_pending_filter, demux_id, caid, provid, pid, num))
365 { return; }
366}
367
368int32_t dvbapi_net_send(uint32_t request, int32_t socket_fd, int32_t demux_index, uint32_t filter_number, unsigned char *data)
369{
370 unsigned char packet[262]; //maximum possible packet size
371 int32_t size = 0;
372
373 // not connected?
374 if (socket_fd <= 0)
375 return 0;
376
377 // preparing packet - header
378 // in old protocol client expect this first byte as adapter index, changed in the new protocol
379 // to be always after request type (opcode)
380 if (client_proto_version <= 0)
381 packet[size++] = demux[demux_index].adapter_index; //adapter index - 1 byte
382
383 // type of request
384 uint32_t req = request;
385 if (client_proto_version >= 1)
386 req = htonl(req);
387 memcpy(&packet[size], &req, 4); //request - 4 bytes
388 size += 4;
389
390 // preparing packet - adapter index for proto >= 1
391 if ((request != DVBAPI_SERVER_INFO) && client_proto_version >= 1)
392 packet[size++] = demux[demux_index].adapter_index; //adapter index - 1 byte
393
394 // struct with data
395 switch (request)
396 {
397 case DVBAPI_SERVER_INFO:
398 {
399 int16_t proto_version = htons(DVBAPI_PROTOCOL_VERSION); //our protocol version
400 memcpy(&packet[size], &proto_version, 2);
401 size += 2;
402
403 unsigned char *info_len = &packet[size]; //info string length
404 size += 1;
405
406 *info_len = snprintf((char *) &packet[size], sizeof(packet) - size, "OSCam v%s, build r%s (%s)", CS_VERSION, CS_SVN_VERSION, CS_TARGET);
407 size += *info_len;
408 break;
409 }
410 case DVBAPI_CA_SET_PID:
411 {
412 int sct_capid_size = sizeof(ca_pid_t);
413
414 if (client_proto_version >= 1)
415 {
416 ca_pid_t *capid = (ca_pid_t *) data;
417 capid->pid = htonl(capid->pid);
418 capid->index = htonl(capid->index);
419 }
420 memcpy(&packet[size], data, sct_capid_size);
421
422 size += sct_capid_size;
423 break;
424 }
425 case DVBAPI_CA_SET_DESCR:
426 {
427 int sct_cadescr_size = sizeof(ca_descr_t);
428
429 if (client_proto_version >= 1)
430 {
431 ca_descr_t *cadesc = (ca_descr_t *) data;
432 cadesc->index = htonl(cadesc->index);
433 cadesc->parity = htonl(cadesc->parity);
434 }
435 memcpy(&packet[size], data, sct_cadescr_size);
436
437 size += sct_cadescr_size;
438 break;
439 }
440 case DVBAPI_DMX_SET_FILTER:
441 case DVBAPI_DMX_STOP:
442 {
443 int32_t sct_filter_size = sizeof(struct dmx_sct_filter_params);
444 packet[size++] = demux_index; //demux index - 1 byte
445 packet[size++] = filter_number; //filter number - 1 byte
446
447 if (data) // filter data when starting
448 {
449 if (client_proto_version >= 1)
450 {
451 struct dmx_sct_filter_params *fp = (struct dmx_sct_filter_params *) data;
452
453 // adding all dmx_sct_filter_params structure fields
454 // one by one to avoid padding problems
455 uint16_t pid = htons(fp->pid);
456 memcpy(&packet[size], &pid, 2);
457 size += 2;
458
459 memcpy(&packet[size], fp->filter.filter, 16);
460 size += 16;
461 memcpy(&packet[size], fp->filter.mask, 16);
462 size += 16;
463 memcpy(&packet[size], fp->filter.mode, 16);
464 size += 16;
465
466 uint32_t timeout = htonl(fp->timeout);
467 memcpy(&packet[size], &timeout, 4);
468 size += 4;
469
470 uint32_t flags = htonl(fp->flags);
471 memcpy(&packet[size], &flags, 4);
472 size += 4;
473 }
474 else
475 {
476 memcpy(&packet[size], data, sct_filter_size); //dmx_sct_filter_params struct
477 size += sct_filter_size;
478 }
479 }
480 else // pid when stopping
481 {
482 if (client_proto_version >= 1)
483 {
484 uint16_t pid = htons(demux[demux_index].demux_fd[filter_number].pid);
485 memcpy(&packet[size], &pid, 2);
486 size += 2;
487 }
488 else
489 {
490 uint16_t pid = demux[demux_index].demux_fd[filter_number].pid;
491 packet[size++] = pid >> 8;
492 packet[size++] = pid & 0xff;
493 }
494 }
495 break;
496 }
497 default: //unknown request
498 {
499 cs_log("ERROR: dvbapi_net_send: invalid request");
500 return 0;
501 }
502 }
503
504 // sending
505 cs_log_dump_dbg(D_DVBAPI, packet, size, "Sending packet to dvbapi client (fd=%d):", socket_fd);
506 send(socket_fd, &packet, size, MSG_DONTWAIT);
507
508 // always returning success as the client could close socket
509 return 0;
510}
511
512int32_t dvbapi_set_filter(int32_t demux_id, int32_t api, uint16_t pid, uint16_t caid, uint32_t provid, uchar *filt, uchar *mask, int32_t timeout, int32_t pidindex, int32_t type,
513 int8_t add_to_emm_list)
514{
515 openxcas_set_caid(demux[demux_id].ECMpids[pidindex].CAID);
516 openxcas_set_ecm_pid(pid);
517 if (USE_OPENXCAS)
518 return 1;
519
520 int32_t ret = -1, n = -1, i;
521
522 for(i = 0; i < maxfilter && demux[demux_id].demux_fd[i].fd > 0; i++) { ; }
523
524 if(i >= maxfilter)
525 {
526 cs_log_dbg(D_DVBAPI, "no free filter");
527 return -1;
528 }
529 n = i;
530
531 demux[demux_id].demux_fd[n].pidindex = pidindex;
532 demux[demux_id].demux_fd[n].pid = pid;
533 demux[demux_id].demux_fd[n].caid = caid;
534 demux[demux_id].demux_fd[n].provid = provid;
535 demux[demux_id].demux_fd[n].type = type;
536
537 switch(api)
538 {
539 case DVBAPI_3:
540 if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
541 ret = demux[demux_id].demux_fd[n].fd = DUMMY_FD;
542 else
543 ret = demux[demux_id].demux_fd[n].fd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
544 if(ret < 0) { return ret; } // return if device cant be opened!
545 struct dmx_sct_filter_params sFP2;
546
547 memset(&sFP2, 0, sizeof(sFP2));
548
549 sFP2.pid = pid;
550 sFP2.timeout = timeout;
551 sFP2.flags = DMX_IMMEDIATE_START;
552 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
553 {
554 //DeepThought: on dgs/cubestation and neumo images, perhaps others
555 //the following code is needed to descramble
556 sFP2.filter.filter[0] = filt[0];
557 sFP2.filter.mask[0] = mask[0];
558 sFP2.filter.filter[1] = 0;
559 sFP2.filter.mask[1] = 0;
560 sFP2.filter.filter[2] = 0;
561 sFP2.filter.mask[2] = 0;
562 memcpy(sFP2.filter.filter + 3, filt + 1, 16 - 3);
563 memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
564 //DeepThought: in the drivers of the dgs/cubestation and neumo images,
565 //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
566 //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
567 //due to a bug its sets the "positive mask" wrongly (they should be all 0).
568 //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
569 //structure, which is incorrect (it should be dmxSctFilterParams).
570 //The only way to get it right is to call DMX_SET_FILTER1 with the argument
571 //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
572 ret = dvbapi_ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER1, &sFP2);
573 }
574 else
575 {
576 memcpy(sFP2.filter.filter, filt, 16);
577 memcpy(sFP2.filter.mask, mask, 16);
578 if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
579 ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER, demux[demux_id].socket_fd, demux_id, n, (unsigned char *) &sFP2);
580 else
581 ret = dvbapi_ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER, &sFP2);
582 }
583 break;
584
585 case DVBAPI_1:
586 ret = demux[demux_id].demux_fd[n].fd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
587 if(ret < 0) { return ret; } // return if device cant be opened!
588 struct dmxSctFilterParams sFP1;
589
590 memset(&sFP1, 0, sizeof(sFP1));
591
592 sFP1.pid = pid;
593 sFP1.timeout = timeout;
594 sFP1.flags = DMX_IMMEDIATE_START;
595 memcpy(sFP1.filter.filter, filt, 16);
596 memcpy(sFP1.filter.mask, mask, 16);
597 ret = dvbapi_ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER1, &sFP1);
598
599 break;
600#ifdef WITH_STAPI
601 case STAPI:
602 ret = stapi_set_filter(demux_id, pid, filt, mask, n, demux[demux_id].pmt_file);
603 if(ret != 0)
604 { demux[demux_id].demux_fd[n].fd = ret; }
605 else
606 { ret = -1; } // error setting filter!
607 break;
608#endif
609#ifdef WITH_COOLAPI
610 case COOLAPI:
611 demux[demux_id].demux_fd[n].fd = coolapi_open_device(demux[demux_id].demux_index, demux_id);
612 if(demux[demux_id].demux_fd[n].fd > 0)
613 { ret = coolapi_set_filter(demux[demux_id].demux_fd[n].fd, n, pid, filt, mask, type); }
614 break;
615#endif
616 default:
617 break;
618 }
619 if(ret != -1) // filter set successful
620 {
621 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d started successfully (caid %04X provid %06X pid %04X)", demux_id, n + 1, caid, provid, pid);
622 if(type == TYPE_EMM && add_to_emm_list){
623 add_emmfilter_to_list(demux_id, filt, caid, provid, pid, n + 1, true);
624 }
625 }
626 else
627 {
628 cs_log("ERROR: Could not start demux filter (api: %d errno=%d %s)", selected_api, errno, strerror(errno));
629 }
630 return ret;
631}
632
633static int32_t dvbapi_detect_api(void)
634{
635#ifdef WITH_COOLAPI
636 selected_api = COOLAPI;
637 selected_box = 5;
638 disable_pmt_files = 1;
639 cfg.dvbapi_listenport = 0;
640 cs_log("Detected Coolstream API");
641 return 1;
642#else
643 if (cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_PC ) {
644 selected_api = DVBAPI_3;
645 selected_box = 1;
646 if (cfg.dvbapi_listenport)
647 {
648 cs_log("Using TCP listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d", selected_api, cfg.dvbapi_boxtype);
649 }
650 else
651 {
652 cs_log("Using %s listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d", devices[selected_box].cam_socket_path, selected_api, cfg.dvbapi_boxtype);
653 }
654 return 1;
655 }
656 else
657 {
658 cfg.dvbapi_listenport = 0;
659 }
660
661 int32_t i = 0, n = 0, devnum = -1, dmx_fd = 0, ret = 0, boxnum = sizeof(devices) / sizeof(struct box_devices);
662 char device_path[128], device_path2[128];
663
664 while (i < boxnum)
665 {
666 snprintf(device_path2, sizeof(device_path2), devices[i].demux_device, 0);
667 snprintf(device_path, sizeof(device_path), devices[i].path, n);
668 strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
669 // FIXME: *THIS SAMYGO CHECK IS UNTESTED*
670 // FIXME: Detect samygo, checking if default DVBAPI_3 device paths are sockets
671 if (i == 1) { // We need boxnum 1 only
672 struct stat sb;
673 if (stat(device_path, &sb) > 0 && S_ISSOCK(sb.st_mode)) {
674 selected_box = 0;
675 disable_pmt_files = 1;
676 is_samygo = 1;
677 devnum = i;
678 break;
679 }
680 }
681 if((dmx_fd = open(device_path, O_RDWR | O_NONBLOCK)) > 0)
682 {
683 devnum = i;
684 ret = close(dmx_fd);
685 break;
686 }
687 /* try at least 8 adapters */
688 if ((strchr(devices[i].path, '%') != NULL) && (n < 8)) n++; else { n = 0; i++; }
689 }
690
691 if(devnum == -1) { return 0; }
692 selected_box = devnum;
693 if(selected_box > -1)
694 { selected_api = devices[selected_box].api; }
695
696 if(ret < 0) { cs_log("ERROR: Could not close demuxer fd (errno=%d %s)", errno, strerror(errno)); } // log it here since some needed var are not inited before!
697 if(is_samygo){ cs_log("SAMYGO detected."); } // log it here since some needed var are not inited before!
698#ifdef WITH_STAPI
699 if(devnum == 4 && stapi_open() == 0)
700 {
701 cs_log("ERROR: stapi: setting up stapi failed.");
702 return 0;
703 }
704#endif
705 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
706 {
707 selected_api = DVBAPI_3; //DeepThought
708 }
709 cs_log("Detected %s Api: %d, userconfig boxtype: %d", device_path, selected_api, cfg.dvbapi_boxtype);
710#endif
711 return 1;
712}
713
714static int32_t dvbapi_read_device(int32_t dmx_fd, unsigned char *buf, int32_t length)
715{
716 int32_t len, rc;
717 struct pollfd pfd[1];
718
719 pfd[0].fd = dmx_fd;
720 pfd[0].events = (POLLIN | POLLPRI);
721
722 rc = poll(pfd, 1, 7000);
723 if(rc < 1)
724 {
725 cs_log("ERROR: Read on %d timed out (errno=%d %s)", dmx_fd, errno, strerror(errno));
726 return -1;
727 }
728
729 len = read(dmx_fd, buf, length);
730
731 if(len < 1)
732 {
733 if(errno == EOVERFLOW)
734 {
735 cs_log("fd %d no valid data present since receiver reported an internal bufferoverflow!", dmx_fd);
736 return 0;
737 }
738 else if(errno != EBADF && errno !=EINVAL) // dont throw errors on invalid fd or invalid argument
739 {
740 cs_log("ERROR: Read error on fd %d (errno=%d %s)", dmx_fd, errno, strerror(errno));
741 }
742 }
743 else { cs_log_dump_dbg(D_TRACE, buf, len, "Readed:"); }
744 return len;
745}
746
747int32_t dvbapi_open_device(int32_t type, int32_t num, int32_t adapter)
748{
749 int32_t dmx_fd, ret;
750 int32_t ca_offset = 0;
751 char device_path[128], device_path2[128];
752
753 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
754 return DUMMY_FD;
755
756 if(type == 0)
757 {
758 snprintf(device_path2, sizeof(device_path2), devices[selected_box].demux_device, num);
759 snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
760
761 strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
762 }
763 else
764 {
765 if(cfg.dvbapi_boxtype == BOXTYPE_DUCKBOX || cfg.dvbapi_boxtype == BOXTYPE_DBOX2 || cfg.dvbapi_boxtype == BOXTYPE_UFS910)
766 { ca_offset = 1; }
767
768 if(cfg.dvbapi_boxtype == BOXTYPE_QBOXHD)
769 { num = 0; }
770
771 if(cfg.dvbapi_boxtype == BOXTYPE_PC)
772 { num = 0; }
773
774 snprintf(device_path2, sizeof(device_path2), devices[selected_box].ca_device, num + ca_offset);
775 snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
776
777 strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
778 }
779
780 if (is_samygo) {
781 struct sockaddr_un saddr;
782 memset(&saddr, 0, sizeof(saddr));
783 saddr.sun_family = AF_UNIX;
784 strncpy(saddr.sun_path, device_path, sizeof(saddr.sun_path) - 1);
785 dmx_fd = socket(AF_UNIX, SOCK_STREAM, 0);
786 ret = connect(dmx_fd, (struct sockaddr *)&saddr, sizeof(saddr));
787 if (ret < 0)
788 close(dmx_fd);
789 } else {
790 dmx_fd = ret = open(device_path, O_RDWR | O_NONBLOCK);
791 }
792
793 if(ret < 0)
794 {
795 cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
796 return -1;
797 }
798
799 cs_log_dbg(D_DVBAPI, "Open device %s (fd %d)", device_path, dmx_fd);
800
801 return dmx_fd;
802}
803
804uint16_t tunemm_caid_map(uint8_t direct, uint16_t caid, uint16_t srvid)
805{
806 int32_t i;
807 struct s_client *cl = cur_client();
808 TUNTAB *ttab = &cl->ttab;
809
810 if (!ttab->ttnum)
811 return caid;
812
813 if(direct)
814 {
815 for(i = 0; i < ttab->ttnum; i++)
816 {
817 if(caid == ttab->ttdata[i].bt_caidto
818 && (srvid == ttab->ttdata[i].bt_srvid || ttab->ttdata[i].bt_srvid == 0xFFFF || !ttab->ttdata[i].bt_srvid))
819 { return ttab->ttdata[i].bt_caidfrom; }
820 }
821 }
822 else
823 {
824 for(i = 0; i < ttab->ttnum; i++)
825 {
826 if(caid == ttab->ttdata[i].bt_caidfrom
827 && (srvid == ttab->ttdata[i].bt_srvid || ttab->ttdata[i].bt_srvid == 0xFFFF || !ttab->ttdata[i].bt_srvid))
828 { return ttab->ttdata[i].bt_caidto; }
829 }
830 }
831 return caid;
832}
833
834int32_t dvbapi_stop_filter(int32_t demux_index, int32_t type)
835{
836 int32_t g, ret = -1;
837
838 for(g = 0; g < MAX_FILTER; g++) // just stop them all, we dont want to risk leaving any stale filters running due to lowering of maxfilters
839 {
840 if(demux[demux_index].demux_fd[g].type == type)
841 {
842 ret = dvbapi_stop_filternum(demux_index, g);
843 }
844 }
845 if(ret == -1) { return 0; } // on error return 0
846 else { return 1; }
847}
848
849int32_t dvbapi_stop_filternum(int32_t demux_index, int32_t num)
850{
851 int32_t retfilter = -1, retfd = -1, fd = demux[demux_index].demux_fd[num].fd;
852 if(fd > 0)
853 {
854 cs_log_dbg(D_DVBAPI, "Demuxer %d stop Filter %d (fd: %d api: %d, caid: %04X, provid: %06X, %spid: %04X)",
855 demux_index, num + 1, fd, selected_api, demux[demux_index].demux_fd[num].caid, demux[demux_index].demux_fd[num].provid,
856 (demux[demux_index].demux_fd[num].type == TYPE_ECM ? "ecm" : "emm"), demux[demux_index].demux_fd[num].pid);
857
858 switch(selected_api)
859 {
860 case DVBAPI_3:
861 if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
862 retfilter = dvbapi_net_send(DVBAPI_DMX_STOP, demux[demux_index].socket_fd, demux_index, num, NULL);
863 else
864 retfilter = dvbapi_ioctl(fd, DMX_STOP, NULL);
865 break;
866
867 case DVBAPI_1:
868 retfilter = dvbapi_ioctl(fd, DMX_STOP, NULL);
869 break;
870
871#ifdef WITH_STAPI
872 case STAPI:
873 retfilter = stapi_remove_filter(demux_index, num, demux[demux_index].pmt_file);
874 if(retfilter != 1) // stapi returns 0 for error, 1 for all ok
875 {
876 retfilter = -1;
877 }
878 break;
879#endif
880#ifdef WITH_COOLAPI
881 case COOLAPI:
882 retfilter = coolapi_remove_filter(fd, num);
883 retfd = coolapi_close_device(fd);
884 break;
885#endif
886 default:
887 break;
888 }
889 if(retfilter < 0)
890 {
891 cs_log("ERROR: Demuxer %d could not stop Filter %d (fd:%d api:%d errno=%d %s)", demux_index, num + 1, fd, selected_api, errno, strerror(errno));
892 }
893#ifndef WITH_COOLAPI // no fd close for coolapi and stapi, all others do close fd!
894 if (!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
895 {
896 if(selected_api == STAPI) { retfd = 0; } // stapi closes its own filter fd!
897 else
898 {
899 flush_read_fd(demux_index, num, fd); // flush filter input buffer in attempt to avoid overflow receivers internal buffer
900 retfd = close(fd);
901 if(errno == 9) { retfd = 0; } // no error on bad file descriptor
902 }
903 }
904 else
905 retfd = 0;
906#endif
907 if(retfd)
908 {
909 cs_log("ERROR: Demuxer %d could not close fd of Filter %d (fd=%d api:%d errno=%d %s)", demux_index, num + 1, fd,
910 selected_api, errno, strerror(errno));
911 }
912
913 if(demux[demux_index].demux_fd[num].type == TYPE_ECM) //ecm filter stopped: reset index!
914 {
915 int32_t oldpid = demux[demux_index].demux_fd[num].pidindex;
916 int32_t curpid = demux[demux_index].pidindex;
917 int32_t idx = demux[demux_index].ECMpids[oldpid].index;
918 demux[demux_index].ECMpids[oldpid].index = 0;
919 if(idx) // if in use
920 {
921 int32_t i;
922 for(i = 0; i < demux[demux_index].STREAMpidcount; i++)
923 {
924 int8_t match = 0;
925 // check streams of old disabled ecmpid
926 if(!demux[demux_index].ECMpids[oldpid].streams || ((demux[demux_index].ECMpids[oldpid].streams & (1 << i)) == (uint) (1 << i)))
927 {
928 // check if new ecmpid is using same streams
929 if(curpid != -1 && (!demux[demux_index].ECMpids[curpid].streams || ((demux[demux_index].ECMpids[curpid].streams & (1 << i)) == (uint) (1 << i))))
930 {
931 continue; // found same stream on old and new ecmpid -> skip! (and leave it enabled!)
932 }
933 int32_t pidtobestopped = demux[demux_index].STREAMpids[i];
934 int32_t j, k, otherdemuxpid, otherdemuxidx;
935
936 for(j = 0; j < MAX_DEMUX; j++) // check other demuxers for same streampid with same index
937 {
938 if(demux[j].program_number == 0) { continue; } // skip empty demuxers
939 if(demux_index == j) { continue; } // skip same demuxer
940 if(demux[j].ca_mask != demux[demux_index].ca_mask) { continue;} // skip streampid running on other ca device
941
942 otherdemuxpid = demux[j].pidindex;
943 if(otherdemuxpid == -1) { continue; } // Other demuxer not descrambling yet
944
945 otherdemuxidx = demux[j].ECMpids[otherdemuxpid].index;
946 if(!otherdemuxidx || otherdemuxidx != idx) { continue; } // Other demuxer has no index yet, or index is different
947
948 for(k = 0; k < demux[j].STREAMpidcount; k++)
949 {
950 if(!demux[j].ECMpids[otherdemuxpid].streams || ((demux[j].ECMpids[otherdemuxpid].streams & (1 << k)) == (uint) (1 << k)))
951 {
952 if(demux[j].STREAMpids[k] == pidtobestopped)
953 {
954 continue; // found same streampid enabled with same index on one or more other demuxers -> skip! (and leave it enabled!)
955 }
956 }
957 match = 1; // matching stream found
958 }
959 }
960
961 if(!match)
962 {
963 dvbapi_set_pid(demux_index, i, idx - 1, false); // disable streampid since its not used by this pid (or by the new ecmpid or any other demuxer!)
964 }
965 }
966 }
967 }
968 }
969
970 if(demux[demux_index].demux_fd[num].type == TYPE_EMM) // If emm type remove from emm filterlist
971 {
972 remove_emmfilter_from_list(demux_index, demux[demux_index].demux_fd[num].caid, demux[demux_index].demux_fd[num].provid, demux[demux_index].demux_fd[num].pid, num + 1);
973 }
974 demux[demux_index].demux_fd[num].fd = 0;
975 demux[demux_index].demux_fd[num].type = 0;
976 }
977 if(retfilter < 0) { return retfilter; } // error on remove filter
978 if(retfd < 0) { return retfd; } // error on close filter fd
979 return 1; // all ok!
980}
981
982void dvbapi_start_filter(int32_t demux_id, int32_t pidindex, uint16_t pid, uint16_t caid, uint32_t provid, uchar table, uchar mask, int32_t timeout, int32_t type)
983{
984 int32_t o;
985 for(o = 0; o < maxfilter; o++) // check if ecmfilter is in use & stop all ecmfilters of lower status pids
986 {
987 if(demux[demux_id].demux_fd[o].fd > 0 &&
988 demux[demux_id].demux_fd[o].pid == pid &&
989 demux[demux_id].demux_fd[o].type == type && type == TYPE_ECM)
990 {
991 return;
992 }
993 }
994 uchar filter[32];
995 memset(filter, 0, 32);
996
997 filter[0] = table;
998 filter[16] = mask;
999
1000 cs_log_dbg(D_DVBAPI, "Demuxer %d try to start new filter for caid: %04X, provid: %06X, pid: %04X", demux_id, caid, provid, pid);
1001 dvbapi_set_filter(demux_id, selected_api, pid, caid, provid, filter, filter + 16, timeout, pidindex, type, 0);
1002}
1003
1004void dvbapi_start_emm_filter(int32_t demux_index)
1005{
1006 unsigned int j;
1007 if(!demux[demux_index].EMMpidcount)
1008 { return; }
1009
1010 //if (demux[demux_index].emm_filter)
1011 // return;
1012
1013
1014 struct s_csystem_emm_filter *dmx_filter = NULL;
1015 unsigned int filter_count = 0;
1016 uint16_t caid, ncaid;
1017 uint32_t provid;
1018
1019 struct s_reader *rdr = NULL;
1020 struct s_client *cl = cur_client();
1021 if(!cl || !cl->aureader_list)
1022 { return; }
1023
1024 LL_ITER itr = ll_iter_create(cl->aureader_list);
1025 while((rdr = ll_iter_next(&itr)))
1026 {
1027 if(rdr->audisabled || !rdr->enable || (!is_network_reader(rdr) && rdr->card_status != CARD_INSERTED))
1028 { continue; }
1029
1030 const struct s_cardsystem *csystem;
1031 uint16_t c, match;
1032 cs_log_dbg(D_DVBAPI, "Demuxer %d matching reader %s against available emmpids -> START!", demux_index, rdr->label);
1033 for(c = 0; c < demux[demux_index].EMMpidcount; c++)
1034 {
1035 caid = ncaid = demux[demux_index].EMMpids[c].CAID;
1036 if(!caid) continue;
1037
1038 if(chk_is_betatunnel_caid(caid) == 2)
1039 {
1040 ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_index].program_number);
1041 }
1042 provid = demux[demux_index].EMMpids[c].PROVID;
1043 if (caid == ncaid)
1044 {
1045 match = emm_reader_match(rdr, caid, provid);
1046 }
1047 else
1048 {
1049 match = emm_reader_match(rdr, ncaid, provid);
1050 }
1051 if(match)
1052 {
1053 csystem = get_cardsystem_by_caid(caid);
1054 if(csystem)
1055 {
1056 if(caid != ncaid)
1057 {
1058 csystem = get_cardsystem_by_caid(ncaid);
1059 if(csystem && csystem->get_tunemm_filter)
1060 {
1061 csystem->get_tunemm_filter(rdr, &dmx_filter, &filter_count);
1062 cs_log_dbg(D_DVBAPI, "Demuxer %d setting emm filter for betatunnel: %04X -> %04X", demux_index, ncaid, caid);
1063 }
1064 else
1065 {
1066 cs_log_dbg(D_DVBAPI, "Demuxer %d cardsystem for emm filter for caid %04X of reader %s not found", demux_index, ncaid, rdr->label);
1067 continue;
1068 }
1069 }
1070 else if (csystem->get_emm_filter)
1071 {
1072 csystem->get_emm_filter(rdr, &dmx_filter, &filter_count);
1073 }
1074 }
1075 else
1076 {
1077 cs_log_dbg(D_DVBAPI, "Demuxer %d cardsystem for emm filter for caid %04X of reader %s not found", demux_index, caid, rdr->label);
1078 continue;
1079 }
1080
1081 for(j = 0; j < filter_count ; j++)
1082 {
1083 if(dmx_filter[j].enabled == 0)
1084 { continue; }
1085
1086 uchar filter[32];
1087 memset(filter, 0, sizeof(filter)); // reset filter
1088 uint32_t usefilterbytes = 16; // default use all filters
1089 memcpy(filter, dmx_filter[j].filter, usefilterbytes);
1090 memcpy(filter + 16, dmx_filter[j].mask, usefilterbytes);
1091 int32_t emmtype = dmx_filter[j].type;
1092
1093 if(filter[0] && (((1 << (filter[0] % 0x80)) & rdr->b_nano) && !((1 << (filter[0] % 0x80)) & rdr->s_nano)))
1094 {
1095 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d blocked by userconfig -> SKIP!", demux_index, rdr->label, j+1, filter_count);
1096 continue;
1097 }
1098
1099 if((rdr->blockemm & emmtype) && !(((1 << (filter[0] % 0x80)) & rdr->s_nano) || (rdr->saveemm & emmtype)))
1100 {
1101 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d blocked by userconfig -> SKIP!", demux_index, rdr->label, j+1, filter_count);
1102 continue;
1103 }
1104
1105 if(demux[demux_index].EMMpids[c].type & emmtype)
1106 {
1107 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d type match -> ENABLE!", demux_index, rdr->label, j+1, filter_count);
1108 check_add_emmpid(demux_index, filter, c, emmtype);
1109 }
1110 else
1111 {
1112 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d type mismatch -> SKIP!", demux_index, rdr->label, j+1, filter_count);
1113 }
1114 }
1115
1116 // dmx_filter not use below this point;
1117 NULLFREE(dmx_filter);
1118 }
1119 }
1120 cs_log_dbg(D_DVBAPI, "Demuxer %d matching reader %s against available emmpids -> DONE!", demux_index, rdr->label);
1121 }
1122 if(demux[demux_index].emm_filter == -1) // first run -1
1123 {
1124 demux[demux_index].emm_filter = 0;
1125 }
1126 cs_log_dbg(D_DVBAPI, "Demuxer %d handles %i emm filters", demux_index, demux[demux_index].emm_filter);
1127}
1128
1129void dvbapi_add_ecmpid_int(int32_t demux_id, uint16_t caid, uint16_t ecmpid, uint32_t provid)
1130{
1131 int32_t n, added = 0;
1132
1133 if(demux[demux_id].ECMpidcount >= ECM_PIDS)
1134 { return; }
1135
1136 int32_t stream = demux[demux_id].STREAMpidcount - 1;
1137 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
1138 {
1139 if(stream > -1 && demux[demux_id].ECMpids[n].CAID == caid && demux[demux_id].ECMpids[n].ECM_PID == ecmpid && demux[demux_id].ECMpids[n].PROVID == provid)
1140 {
1141 if(!demux[demux_id].ECMpids[n].streams)
1142 {
1143 //we already got this caid/ecmpid as global, no need to add the single stream
1144 cs_log("Demuxer %d skipped stream CAID: %04X ECM_PID: %04X PROVID: %06X (Same as ECMPID %d)", demux_id, caid, ecmpid, provid, n);
1145 continue;
1146 }
1147 added = 1;
1148 demux[demux_id].ECMpids[n].streams |= (1 << stream);
1149 cs_log("Demuxer %d added stream to ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X", demux_id, n, caid, ecmpid, provid);
1150 }
1151 }
1152
1153 if(added == 1)
1154 { return; }
1155 for(n = 0; n < demux[demux_id].ECMpidcount; n++) // check for existing pid
1156 {
1157 if(demux[demux_id].ECMpids[n].CAID == caid && demux[demux_id].ECMpids[n].ECM_PID == ecmpid && demux[demux_id].ECMpids[n].PROVID == provid)
1158 { return; } // found same pid -> skip
1159 }
1160 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].ECM_PID = ecmpid;
1161 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CAID = caid;
1162 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].PROVID = provid;
1163 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CHID = 0x10000; // reset CHID
1164 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].checked = 0;
1165 //demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].index = 0;
1166 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].status = 0;
1167 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].tries = 0xFE;
1168 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].streams = 0; // reset streams!
1169 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_curindex = 0xFE; // reset
1170 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_maxindex = 0; // reset
1171 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_cycle = 0xFE; // reset
1172 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].table = 0;
1173
1174 cs_log("Demuxer %d added new ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X", demux_id, demux[demux_id].ECMpidcount, caid, ecmpid, provid);
1175 if(caid_is_irdeto(caid)) { demux[demux_id].emmstart.time = 1; } // marker to fetch emms early irdeto needs them!
1176
1177 demux[demux_id].ECMpidcount++;
1178}
1179
1180void dvbapi_add_ecmpid(int32_t demux_id, uint16_t caid, uint16_t ecmpid, uint32_t provid)
1181{
1182 dvbapi_add_ecmpid_int(demux_id, caid, ecmpid, provid);
1183 struct s_dvbapi_priority *joinentry;
1184
1185 for(joinentry = dvbapi_priority; joinentry != NULL; joinentry = joinentry->next)
1186 {
1187 if((joinentry->type != 'j')
1188 || (joinentry->caid && joinentry->caid != caid)
1189 || (joinentry->provid && joinentry->provid != provid)
1190 || (joinentry->ecmpid && joinentry->ecmpid != ecmpid)
1191 || (joinentry->srvid && joinentry->srvid != demux[demux_id].program_number))
1192 { continue; }
1193 cs_log_dbg(D_DVBAPI, "Join ecmpid %04X:%06X:%04X to %04X:%06X:%04X",
1194 caid, provid, ecmpid, joinentry->mapcaid, joinentry->mapprovid, joinentry->mapecmpid);
1195 dvbapi_add_ecmpid_int(demux_id, joinentry->mapcaid, joinentry->mapecmpid, joinentry->mapprovid);
1196 }
1197}
1198
1199void dvbapi_add_emmpid(int32_t demux_id, uint16_t caid, uint16_t emmpid, uint32_t provid, uint8_t type)
1200{
1201 char typetext[40];
1202 cs_strncpy(typetext, ":", sizeof(typetext));
1203
1204 if(type & 0x01) { strcat(typetext, "UNIQUE:"); }
1205 if(type & 0x02) { strcat(typetext, "SHARED:"); }
1206 if(type & 0x04) { strcat(typetext, "GLOBAL:"); }
1207 if(type & 0xF8) { strcat(typetext, "UNKNOWN:"); }
1208
1209 uint16_t i;
1210 for(i = 0; i < demux[demux_id].EMMpidcount; i++)
1211 {
1212 if(demux[demux_id].EMMpids[i].PID == emmpid && demux[demux_id].EMMpids[i].CAID == caid && demux[demux_id].EMMpids[i].PROVID == provid)
1213 {
1214 if(!(demux[demux_id].EMMpids[i].type&type)){
1215 demux[demux_id].EMMpids[i].type |= type; // register this emm kind to this emmpid
1216 cs_log_dbg(D_DVBAPI, "Added to existing emmpid %d additional emmtype %s", demux[demux_id].EMMpidcount - 1, typetext);
1217 }
1218 return;
1219 }
1220 }
1221 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].PID = emmpid;
1222 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].CAID = caid;
1223 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].PROVID = provid;
1224 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount++].type = type;
1225 cs_log_dbg(D_DVBAPI, "Added new emmpid %d CAID: %04X EMM_PID: %04X PROVID: %06X TYPE %s", demux[demux_id].EMMpidcount - 1, caid, emmpid, provid, typetext);
1226}
1227
1228void dvbapi_parse_cat(int32_t demux_id, uchar *buf, int32_t len)
1229{
1230#ifdef WITH_COOLAPI
1231 // driver sometimes reports error if too many emm filter
1232 // but adding more ecm filter is no problem
1233 // ... so ifdef here instead of limiting MAX_FILTER
1234 demux[demux_id].max_emm_filter = 14;
1235#else
1236 if(cfg.dvbapi_requestmode == 1)
1237 {
1238 uint16_t ecm_filter_needed = 0, n;
1239 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
1240 {
1241 if(demux[demux_id].ECMpids[n].status > -1)
1242 { ecm_filter_needed++; }
1243 }
1244 if(maxfilter - ecm_filter_needed <= 0)
1245 { demux[demux_id].max_emm_filter = 0; }
1246 else
1247 { demux[demux_id].max_emm_filter = maxfilter - ecm_filter_needed; }
1248 }
1249 else
1250 {
1251 demux[demux_id].max_emm_filter = maxfilter - 1;
1252 }
1253#endif
1254 uint16_t i, k;
1255
1256 cs_log_dump_dbg(D_DVBAPI, buf, len, "cat:");
1257
1258 for(i = 8; i < (b2i(2, buf + 1)&0xFFF) - 1; i += buf[i + 1] + 2)
1259 {
1260 if(buf[i] != 0x09) { continue; }
1261 if(demux[demux_id].EMMpidcount >= ECM_PIDS) { break; }
1262
1263 uint16_t caid = b2i(2, buf + i + 2);
1264 uint16_t emm_pid = b2i(2, buf + i +4)&0x1FFF;
1265 uint32_t emm_provider = 0;
1266
1267 switch(caid >> 8)
1268 {
1269 case 0x01:
1270 dvbapi_add_emmpid(demux_id, caid, emm_pid, 0, EMM_UNIQUE | EMM_GLOBAL);
1271 for(k = i + 7; k < i + buf[i + 1] + 2; k += 4)
1272 {
1273 emm_provider = b2i(2, buf + k + 2);
1274 emm_pid = b2i(2, buf + k)&0xFFF;
1275 dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, EMM_SHARED);
1276 }
1277 break;
1278 case 0x05:
1279 for(k = i + 6; k < i + buf[i + 1] + 2; k += buf[k + 1] + 2)
1280 {
1281 if (buf[k] == 0x14)
1282 {
1283 emm_provider = (b2i(3, buf + k + 2) & 0xFFFFF0); // viaccess fixup last digit is a dont care!
1284 dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
1285 }
1286 }
1287 break;
1288 case 0x18:
1289 if(buf[i + 1] == 0x07 || buf[i + 1] == 0x0B)
1290 {
1291 for(k = i + 7; k < i + 7 + buf[i + 6]; k += 2)
1292 {
1293 emm_provider = b2i(2, buf + k);
1294 dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
1295 }
1296 }
1297 else
1298 {
1299 dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
1300 }
1301 break;
1302 default:
1303 dvbapi_add_emmpid(demux_id, caid, emm_pid, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
1304 break;
1305 }
1306 }
1307 return;
1308}
1309
1310static pthread_mutex_t lockindex;
1311int32_t dvbapi_get_descindex(int32_t demux_index)
1312{
1313 int32_t i, j, idx = 1, fail = 1;
1314 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
1315 {
1316 idx = 0;
1317 sscanf(demux[demux_index].pmt_file, "pmt%3d.tmp", &idx);
1318 idx++; // fixup
1319 return idx;
1320 }
1321 pthread_mutex_lock(&lockindex); // to avoid race when readers become responsive!
1322 while(fail)
1323 {
1324 fail = 0;
1325 for(i = 0; i < MAX_DEMUX && !fail; i++)
1326 {
1327 for(j = 0; j < demux[i].ECMpidcount && !fail; j++)
1328 {
1329 if(demux[i].ECMpids[j].index == idx)
1330 {
1331 fail = 1;
1332 idx++;
1333 }
1334 }
1335 }
1336 cs_sleepms(1);
1337 }
1338 pthread_mutex_unlock(&lockindex); // and release it!
1339 return idx;
1340}
1341
1342void dvbapi_set_pid(int32_t demux_id, int32_t num, int32_t idx, bool enable)
1343{
1344 int32_t i, currentfd;
1345 if(demux[demux_id].pidindex == -1 && enable) return; // no current pid on enable? --> exit
1346
1347 switch(selected_api)
1348 {
1349#ifdef WITH_STAPI
1350 case STAPI:
1351 if(!enable) idx = -1;
1352 stapi_set_pid(demux_id, num, idx, demux[demux_id].STREAMpids[num], demux[demux_id].pmt_file); // only used to disable pids!!!
1353 break;
1354#endif
1355#ifdef WITH_COOLAPI
1356 case COOLAPI:
1357 break;
1358#endif
1359 default:
1360 for(i = 0; i < MAX_DEMUX; i++)
1361 {
1362 if(((demux[demux_id].ca_mask & (1 << i)) == (uint) (1 << i)))
1363 {
1364 int8_t action = 0;
1365 if(enable){
1366 action = update_streampid_list(i, demux[demux_id].STREAMpids[num], idx);
1367 }
1368 if(!enable){
1369 action = remove_streampid_from_list(i, demux[demux_id].STREAMpids[num], idx);
1370 }
1371
1372 if(action != NO_STREAMPID_LISTED && action != FOUND_STREAMPID_INDEX && action != ADDED_STREAMPID_INDEX)
1373 {
1374 ca_pid_t ca_pid2;
1375 memset(&ca_pid2, 0, sizeof(ca_pid2));
1376 ca_pid2.pid = demux[demux_id].STREAMpids[num];
1377
1378 // removed last of this streampid on ca? -> disable this pid with -1 on this ca
1379 if((action == REMOVED_STREAMPID_LASTINDEX) && (is_ca_used(i, ca_pid2.pid) == CA_IS_CLEAR)) idx = -1;
1380
1381 // removed index of streampid that is used to decode on ca -> get a fresh one
1382 if(action == REMOVED_DECODING_STREAMPID_INDEX)
1383 {
1384 idx = is_ca_used(i, demux[demux_id].STREAMpids[num]); // get an active index for this pid and enable it on ca device
1385 enable = 1;
1386 }
1387
1388 ca_pid2.index = idx;
1389 cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x index=%d on ca%d", demux_id,
1390 (enable ? "enable" : "disable"), num + 1, ca_pid2.pid, ca_pid2.index, i);
1391
1392 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
1393 dvbapi_net_send(DVBAPI_CA_SET_PID, demux[demux_id].socket_fd, demux_id, -1 /*unused*/, (unsigned char *) &ca_pid2);
1394 else
1395 {
1396 currentfd = ca_fd[i];
1397 if(currentfd <= 0)
1398 {
1399 currentfd = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
1400 ca_fd[i] = currentfd; // save fd of this ca
1401 }
1402 if(currentfd > 0)
1403 {
1404 if(dvbapi_ioctl(currentfd, CA_SET_PID, &ca_pid2) == -1)
1405 cs_log_dbg(D_TRACE | D_DVBAPI,"CA_SET_PID ioctl error (errno=%d %s)", errno, strerror(errno));
1406 int8_t result = is_ca_used(i,0); // check if in use by any pid
1407 if(!enable && result == CA_IS_CLEAR){
1408 cs_log_dbg(D_DVBAPI, "Demuxer %d close now unused CA%d device", demux_id, i);
1409 int32_t ret = close(currentfd);
1410 if(ret < 0) { cs_log("ERROR: Could not close demuxer fd (errno=%d %s)", errno, strerror(errno)); }
1411 currentfd = ca_fd[i] = 0;
1412 }
1413 }
1414 }
1415 }
1416 }
1417 }
1418 break;
1419 }
1420 return;
1421}
1422
1423void dvbapi_stop_all_descrambling(void)
1424{
1425 int32_t j;
1426 for(j = 0; j < MAX_DEMUX; j++)
1427 {
1428 if(demux[j].program_number == 0) { continue; }
1429 dvbapi_stop_descrambling(j);
1430 }
1431}
1432
1433void dvbapi_stop_descrambling(int32_t demux_id)
1434{
1435 int32_t i;
1436 if(demux[demux_id].program_number == 0) { return; }
1437 char channame[32];
1438 i = demux[demux_id].pidindex;
1439 if(i < 0) { i = 0; }
1440 demux[demux_id].pidindex = -1; // no ecmpid is to be descrambling since we start stop descrambling!
1441 get_servicename(dvbapi_client, demux[demux_id].program_number, demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].CAID : 0, channame);
1442 cs_log_dbg(D_DVBAPI, "Demuxer %d stop descrambling program number %04X (%s)", demux_id, demux[demux_id].program_number, channame);
1443 dvbapi_stop_filter(demux_id, TYPE_EMM);
1444 if(demux[demux_id].ECMpidcount > 0)
1445 {
1446 dvbapi_stop_filter(demux_id, TYPE_ECM);
1447 }
1448
1449 memset(&demux[demux_id], 0 , sizeof(DEMUXTYPE));
1450 demux[demux_id].pidindex = -1;
1451 demux[demux_id].curindex = -1;
1452 if (!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
1453 unlink(ECMINFO_FILE);
1454 return;
1455}
1456
1457int32_t dvbapi_start_descrambling(int32_t demux_id, int32_t pid, int8_t checked)
1458{
1459 int32_t started = 0; // in case ecmfilter started = 1
1460 int32_t fake_ecm = 0;
1461 ECM_REQUEST *er;
1462 struct s_reader *rdr;
1463 if(!(er = get_ecmtask())) { return started; }
1464 demux[demux_id].ECMpids[pid].checked = checked + 1; // mark this pid as checked!
1465
1466 struct s_dvbapi_priority *p;
1467 for(p = dvbapi_priority; p != NULL ; p = p->next)
1468 {
1469 if((p->type != 'p')
1470 || (p->caid && p->caid != demux[demux_id].ECMpids[pid].CAID)
1471 || (p->provid && p->provid != demux[demux_id].ECMpids[pid].PROVID)
1472 || (p->ecmpid && p->ecmpid != demux[demux_id].ECMpids[pid].ECM_PID)
1473 || (p->srvid && p->srvid != demux[demux_id].program_number)
1474 || (p->pidx && p->pidx-1 != pid))
1475 { continue; }
1476 // if found chid and first run apply chid filter, on forced pids always apply!
1477 if(p->type == 'p' && p->chid < 0x10000 && (demux[demux_id].ECMpids[pid].checked == 1 || (p && p->force)))
1478 {
1479 if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
1480 {
1481 er->chid = demux[demux_id].ECMpids[pid].CHID;
1482 }
1483 else
1484 {
1485 er->chid = p->chid; // no channelcache or no chid in use, so use prio chid
1486 demux[demux_id].ECMpids[pid].CHID = p->chid;
1487 }
1488 //cs_log("********* CHID %04X **************", demux[demux_id].ECMpids[pid].CHID);
1489 break; // we only accept one!
1490 }
1491 else
1492 {
1493 if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
1494 {
1495 er->chid = demux[demux_id].ECMpids[pid].CHID;
1496 }
1497 else // no channelcache or no chid in use
1498 {
1499 er->chid = 0;
1500 demux[demux_id].ECMpids[pid].CHID = 0x10000;
1501 }
1502 }
1503 }
1504 er->srvid = demux[demux_id].program_number;
1505 er->caid = demux[demux_id].ECMpids[pid].CAID;
1506 er->pid = demux[demux_id].ECMpids[pid].ECM_PID;
1507 er->prid = demux[demux_id].ECMpids[pid].PROVID;
1508 er->vpid = demux[demux_id].ECMpids[pid].VPID;
1509 er->pmtpid = demux[demux_id].pmtpid;
1510
1511 struct timeb now;
1512 cs_ftime(&now);
1513 for(rdr = first_active_reader; rdr != NULL ; rdr = rdr->next)
1514 {
1515 int8_t match = matching_reader(er, rdr); // check for matching reader
1516 int64_t gone = comp_timeb(&now, &rdr->emm_last);
1517 if(gone > 3600*1000 && rdr->needsemmfirst && caid_is_irdeto(er->caid))
1518 {
1519 cs_log("Warning reader %s received no emms for the last %d seconds -> skip, this reader needs emms first!", rdr->label,
1520 (int)(gone/1000));
1521 continue; // skip this card needs to process emms first before it can be used for descramble
1522 }
1523 if(p && p->force) { match = 1; } // forced pid always started!
1524
1525 if(!match) // if this reader does not match, check betatunnel for it
1526 match = lb_check_auto_betatunnel(er, rdr);
1527
1528 if(!match && chk_is_betatunnel_caid(er->caid)) // these caids might be tunneled invisible by peers
1529 { match = 1; } // so make it a match to try it!
1530
1531 if(config_enabled(CS_CACHEEX) && (!match && (cacheex_is_match_alias(dvbapi_client, er)))) // check if cache-ex is matching
1532 {
1533 match = 1; // so make it a match to try it!
1534 }
1535
1536 // BISS or FAKE CAID
1537 // ecm stream pid is fake, so send out one fake ecm request
1538 // special treatment: if we asked the cw first without starting a filter the cw request will be killed due to no ecmfilter started
1539 if(caid_is_fake(demux[demux_id].ECMpids[pid].CAID) || caid_is_biss(demux[demux_id].ECMpids[pid].CAID))
1540 {
1541 int32_t j, n;
1542 er->ecmlen = 5;
1543 er->ecm[0] = 0x80; // to pass the cache check it must be 0x80 or 0x81
1544 er->ecm[1] = 0x00;
1545 er->ecm[2] = 0x02;
1546 i2b_buf(2, er->srvid, er->ecm + 3);
1547
1548 for(j = 0, n = 5; j < demux[demux_id].STREAMpidcount; j++, n += 2)
1549 {
1550 i2b_buf(2, demux[demux_id].STREAMpids[j], er->ecm + n);
1551 er->ecm[2] += 2;
1552 er->ecmlen += 2;
1553 }
1554
1555 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X", demux_id, pid,
1556 demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].PROVID, demux[demux_id].ECMpids[pid].ECM_PID,
1557 demux[demux_id].pmtpid, demux[demux_id].ECMpids[pid].VPID);
1558
1559 demux[demux_id].curindex = pid; // set current pid to the fresh started one
1560
1561 dvbapi_start_filter(demux_id, pid, demux[demux_id].ECMpids[pid].ECM_PID, demux[demux_id].ECMpids[pid].CAID,
1562 demux[demux_id].ECMpids[pid].PROVID, 0x80, 0xF0, 3000, TYPE_ECM);
1563 started = 1;
1564
1565 request_cw(dvbapi_client, er, demux_id, 0); // do not register ecm since this try!
1566 fake_ecm = 1;
1567 break; // we started an ecmfilter so stop looking for next matching reader!
1568 }
1569 if(match) // if matching reader found check for irdeto cas if local irdeto card check if it received emms in last 60 minutes
1570 {
1571
1572 if(caid_is_irdeto(er->caid)) // irdeto cas init irdeto_curindex to wait for first index (00)
1573 {
1574 if(demux[demux_id].ECMpids[pid].irdeto_curindex == 0xFE) { demux[demux_id].ECMpids[pid].irdeto_curindex = 0x00; }
1575 }
1576
1577 if(p && p->chid < 0x10000) // do we prio a certain chid?
1578 {
1579 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X PMTPID %04X VPID %04X", demux_id, pid,
1580 demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].PROVID, demux[demux_id].ECMpids[pid].ECM_PID,
1581 demux[demux_id].ECMpids[pid].CHID, demux[demux_id].pmtpid, demux[demux_id].ECMpids[pid].VPID);
1582 }
1583 else
1584 {
1585 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X", demux_id, pid,
1586 demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].PROVID, demux[demux_id].ECMpids[pid].ECM_PID,
1587 demux[demux_id].pmtpid, demux[demux_id].ECMpids[pid].VPID);
1588 }
1589
1590 demux[demux_id].curindex = pid; // set current pid to the fresh started one
1591
1592 dvbapi_start_filter(demux_id, pid, demux[demux_id].ECMpids[pid].ECM_PID, demux[demux_id].ECMpids[pid].CAID,
1593 demux[demux_id].ECMpids[pid].PROVID, 0x80, 0xF0, 3000, TYPE_ECM);
1594 started = 1;
1595 break; // we started an ecmfilter so stop looking for next matching reader!
1596 }
1597 }
1598 if(demux[demux_id].curindex != pid)
1599 {
1600 cs_log("Demuxer %d impossible to descramble PID %d CAID %04X PROVID %06X ECMPID %04X PMTPID %04X (NO MATCHING READER)", demux_id, pid,
1601 demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].PROVID, demux[demux_id].ECMpids[pid].ECM_PID, demux[demux_id].pmtpid);
1602 demux[demux_id].ECMpids[pid].checked = 4; // flag this pid as checked
1603 demux[demux_id].ECMpids[pid].status = -1; // flag this pid as unusable
1604 dvbapi_edit_channel_cache(demux_id, pid, 0); // remove this pid from channelcache
1605 }
1606 if(!fake_ecm) { NULLFREE(er); }
1607 return started;
1608}
1609
1610struct s_dvbapi_priority *dvbapi_check_prio_match_emmpid(int32_t demux_id, uint16_t caid, uint32_t provid, char type)
1611{
1612 struct s_dvbapi_priority *p;
1613 int32_t i;
1614
1615 uint16_t ecm_pid = 0;
1616 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
1617 {
1618 if((demux[demux_id].ECMpids[i].CAID == caid) && (demux[demux_id].ECMpids[i].PROVID == provid))
1619 {
1620 ecm_pid = demux[demux_id].ECMpids[i].ECM_PID;
1621 break;
1622 }
1623 }
1624
1625 if(!ecm_pid)
1626 { return NULL; }
1627
1628 for(p = dvbapi_priority; p != NULL; p = p->next)
1629 {
1630 if(p->type != type
1631 || (p->caid && p->caid != caid)
1632 || (p->provid && p->provid != provid)
1633 || (p->ecmpid && p->ecmpid != ecm_pid)
1634 || (p->srvid && p->srvid != demux[demux_id].program_number)
1635 || (p->pidx && p->pidx-1 !=i)
1636 || (p->type == 'i' && (p->chid < 0x10000)))
1637 { continue; }
1638 return p;
1639 }
1640 return NULL;
1641}
1642
1643struct s_dvbapi_priority *dvbapi_check_prio_match(int32_t demux_id, int32_t pidindex, char type)
1644{
1645 struct s_dvbapi_priority *p;
1646 struct s_ecmpids *ecmpid = &demux[demux_id].ECMpids[pidindex];
1647
1648 for(p = dvbapi_priority; p != NULL; p = p->next)
1649 {
1650 if(p->type != type
1651 || (p->caid && p->caid != ecmpid->CAID)
1652 || (p->provid && p->provid != ecmpid->PROVID)
1653 || (p->ecmpid && p->ecmpid != ecmpid->ECM_PID)
1654 || (p->srvid && p->srvid != demux[demux_id].program_number)
1655 //|| (p->type == 'i' && (p->chid > -1))) ///????
1656 || (p->pidx && p->pidx-1 != pidindex)
1657 || (p->chid < 0x10000 && p->chid != ecmpid->CHID))
1658 { continue; }
1659 return p;
1660 }
1661 return NULL;
1662}
1663
1664void dvbapi_process_emm(int32_t demux_index, int32_t filter_num, unsigned char *buffer, uint32_t len)
1665{
1666 EMM_PACKET epg;
1667
1668 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d fetched emm data", demux_index, filter_num + 1); // emm shown with -d64
1669
1670 struct s_emm_filter *filter = get_emmfilter_by_filternum(demux_index, filter_num + 1);
1671
1672 if(!filter)
1673 {
1674 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d no filter matches -> SKIP!", demux_index, filter_num +1);
1675 return;
1676 }
1677
1678 uint32_t provider = filter->provid;
1679 uint16_t caid = filter->caid;
1680
1681 struct s_dvbapi_priority *mapentry = dvbapi_check_prio_match_emmpid(filter->demux_id, filter->caid, filter->provid, 'm');
1682 if(mapentry)
1683 {
1684 cs_log_dbg(D_DVBAPI, "Demuxer %d mapping EMM from %04X:%06X to %04X:%06X", demux_index, caid, provider, mapentry->mapcaid,
1685 mapentry->mapprovid);
1686 caid = mapentry->mapcaid;
1687 provider = mapentry->mapprovid;
1688 }
1689
1690 memset(&epg, 0, sizeof(epg));
1691
1692 i2b_buf(2, caid, epg.caid);
1693 i2b_buf(4, provider, epg.provid);
1694
1695 epg.emmlen = len > sizeof(epg.emm) ? sizeof(epg.emm) : len;
1696 memcpy(epg.emm, buffer, epg.emmlen);
1697
1698 if(config_enabled(READER_IRDETO) && chk_is_betatunnel_caid(caid) == 2)
1699 {
1700 uint16_t ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_index].program_number);
1701 if(caid != ncaid)
1702 {
1703 irdeto_add_emm_header(&epg);
1704 i2b_buf(2, ncaid, epg.caid);
1705 }
1706 }
1707
1708 do_emm(dvbapi_client, &epg);
1709}
1710
1711void dvbapi_read_priority(void)
1712{
1713 FILE *fp;
1714 char token[128], str1[128];
1715 char type;
1716 int32_t i, ret, count = 0;
1717
1718 const char *cs_prio = "oscam.dvbapi";
1719
1720 fp = fopen(get_config_filename(token, sizeof(token), cs_prio), "r");
1721
1722 if(!fp)
1723 {
1724 cs_log_dbg(D_DVBAPI, "ERROR: Can't open priority file %s", token);
1725 return;
1726 }
1727
1728 if(dvbapi_priority)
1729 {
1730 cs_log_dbg(D_DVBAPI, "reread priority file %s", cs_prio);
1731 struct s_dvbapi_priority *o, *p;
1732 for(p = dvbapi_priority; p != NULL; p = o)
1733 {
1734 o = p->next;
1735 NULLFREE(p);
1736 }
1737 dvbapi_priority = NULL;
1738 }
1739
1740 while(fgets(token, sizeof(token), fp))
1741 {
1742 // Ignore comments and empty lines
1743 if(token[0] == '#' || token[0] == '/' || token[0] == '\n' || token[0] == '\r' || token[0] == '\0')
1744 { continue; }
1745 if(strlen(token) > 100) { continue; }
1746
1747 memset(str1, 0, 128);
1748
1749 for(i = 0; i < (int)strlen(token) && token[i] == ' '; i++) { ; }
1750 if(i == (int)strlen(token) - 1) //empty line or all spaces
1751 { continue; }
1752
1753 for(i = 0; i < (int)strlen(token); i++)
1754 {
1755 if((token[i] == ':' || token[i] == ' ') && token[i + 1] == ':') // if "::" or " :"
1756 {
1757 memmove(token + i + 2, token + i + 1, strlen(token) - i + 1); //insert extra position
1758 token[i + 1] = '0'; //and fill it with NULL
1759 }
1760 if(token[i] == '#' || token[i] == '/')
1761 {
1762 token[i] = '\0';
1763 break;
1764 }
1765 }
1766
1767 type = 0;
1768#ifdef WITH_STAPI
1769 uint32_t disablefilter = 0;
1770 ret = sscanf(trim(token), "%c: %63s %63s %d", &type, str1, str1 + 64, &disablefilter);
1771#else
1772 ret = sscanf(trim(token), "%c: %63s %63s", &type, str1, str1 + 64);
1773#endif
1774 type = tolower((uchar)type);
1775
1776 if(ret < 1 || (type != 'p' && type != 'i' && type != 'm' && type != 'd' && type != 's' && type != 'l'
1777 && type != 'j' && type != 'a' && type != 'x'))
1778 {
1779 //fprintf(stderr, "Warning: line containing %s in %s not recognized, ignoring line\n", token, cs_prio);
1780 //fprintf would issue the warning to the command line, which is more consistent with other config warnings
1781 //however it takes OSCam a long time (>4 seconds) to reach this part of the program, so the warnings are reaching tty rather late
1782 //which leads to confusion. So send the warnings to log file instead
1783 cs_log_dbg(D_DVBAPI, "WARN: line containing %s in %s not recognized, ignoring line\n", token, cs_prio);
1784 continue;
1785 }
1786
1787 struct s_dvbapi_priority *entry;
1788 if(!cs_malloc(&entry, sizeof(struct s_dvbapi_priority)))
1789 {
1790 ret = fclose(fp);
1791 if(ret < 0) { cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno)); }
1792 return;
1793 }
1794
1795 entry->type = type;
1796 entry->next = NULL;
1797
1798 count++;
1799
1800#ifdef WITH_STAPI
1801 if(type == 's')
1802 {
1803 strncpy(entry->devname, str1, 29);
1804 strncpy(entry->pmtfile, str1 + 64, 29);
1805
1806 entry->disablefilter = disablefilter;
1807
1808 cs_log_dbg(D_DVBAPI, "stapi prio: ret=%d | %c: %s %s | disable %d",
1809 ret, type, entry->devname, entry->pmtfile, disablefilter);
1810
1811 if(!dvbapi_priority)
1812 {
1813 dvbapi_priority = entry;
1814 }
1815 else
1816 {
1817 struct s_dvbapi_priority *p;
1818 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
1819 p->next = entry;
1820 }
1821 continue;
1822 }
1823#endif
1824
1825 char c_srvid[34];
1826 c_srvid[0] = '\0';
1827 uint32_t caid = 0, provid = 0, srvid = 0, ecmpid = 0;
1828 uint32_t chid = 0x10000; //chid=0 is a valid chid
1829 ret = sscanf(str1, "%4x:%6x:%33[^:]:%4x:%4x"SCNx16, &caid, &provid, c_srvid, &ecmpid, &chid);
1830 if(ret < 1)
1831 {
1832 cs_log("Error in oscam.dvbapi: ret=%d | %c: %04X %06X %s %04X %04X",
1833 ret, type, caid, provid, c_srvid, ecmpid, chid);
1834 continue; // skip this entry!
1835 }
1836 else
1837 {
1838 cs_log_dbg(D_DVBAPI, "Parsing rule: ret=%d | %c: %04X %06X %s %04X %04X",
1839 ret, type, caid, provid, c_srvid, ecmpid, chid);
1840 }
1841
1842 entry->caid = caid;
1843 entry->provid = provid;
1844 entry->ecmpid = ecmpid;
1845 entry->chid = chid;
1846
1847 uint32_t delay = 0, force = 0, mapcaid = 0, mapprovid = 0, mapecmpid = 0, pidx = 0;
1848 switch(type)
1849 {
1850 case 'i':
1851 ret = sscanf(str1 + 64, "%1d", &pidx);
1852 entry->pidx = pidx+1;
1853 if(ret < 1) entry->pidx = 0;
1854 break;
1855 case 'd':
1856 sscanf(str1 + 64, "%4d", &delay);
1857 entry->delay = delay;
1858 break;
1859 case 'l':
1860 entry->delay = dyn_word_atob(str1 + 64);
1861 if(entry->delay == -1) { entry->delay = 0; }
1862 break;
1863 case 'p':
1864 ret = sscanf(str1 + 64, "%1d:%1d", &force, &pidx);
1865 entry->force = force;
1866 entry->pidx = pidx+1;
1867 if(ret < 2) entry->pidx = 0;
1868 break;
1869 case 'm':
1870 sscanf(str1 + 64, "%4x:%6x", &mapcaid, &mapprovid);
1871 if(!mapcaid) { mapcaid = 0xFFFF; }
1872 entry->mapcaid = mapcaid;
1873 entry->mapprovid = mapprovid;
1874 break;
1875 case 'a':
1876 case 'j':
1877 sscanf(str1 + 64, "%4x:%6x:%4x", &mapcaid, &mapprovid, &mapecmpid);
1878 if(!mapcaid) { mapcaid = 0xFFFF; }
1879 entry->mapcaid = mapcaid;
1880 entry->mapprovid = mapprovid;
1881 entry->mapecmpid = mapecmpid;
1882 break;
1883 }
1884
1885 if(c_srvid[0] == '=')
1886 {
1887 struct s_srvid *this;
1888
1889 for(i = 0; i < 16; i++)
1890 for(this = cfg.srvid[i]; this != NULL; this = this->next)
1891 {
1892 if(strcmp(this->prov, c_srvid + 1) == 0)
1893 {
1894 struct s_dvbapi_priority *entry2;
1895 if(!cs_malloc(&entry2, sizeof(struct s_dvbapi_priority)))
1896 { continue; }
1897 memcpy(entry2, entry, sizeof(struct s_dvbapi_priority));
1898
1899 entry2->srvid = this->srvid;
1900
1901 cs_log_dbg(D_DVBAPI, "prio srvid: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
1902 ret, entry2->type, entry2->caid, entry2->provid, entry2->srvid, entry2->ecmpid, entry2->chid,
1903 entry2->mapcaid, entry2->mapprovid, entry2->mapecmpid, entry2->force, entry2->delay);
1904
1905 if(!dvbapi_priority)
1906 {
1907 dvbapi_priority = entry2;
1908 }
1909 else
1910 {
1911 struct s_dvbapi_priority *p;
1912 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
1913 p->next = entry2;
1914 }
1915 }
1916 }
1917 NULLFREE(entry);
1918 continue;
1919 }
1920 else
1921 {
1922 sscanf(c_srvid, "%4x", &srvid);
1923 entry->srvid = srvid;
1924 }
1925
1926 cs_log_dbg(D_DVBAPI, "prio: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
1927 ret, entry->type, entry->caid, entry->provid, entry->srvid, entry->ecmpid, entry->chid, entry->mapcaid,
1928 entry->mapprovid, entry->mapecmpid, entry->force, entry->delay);
1929
1930 if(!dvbapi_priority)
1931 {
1932 dvbapi_priority = entry;
1933 }
1934 else
1935 {
1936 struct s_dvbapi_priority *p;
1937 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
1938 p->next = entry;
1939 }
1940 }
1941
1942 cs_log_dbg(D_DVBAPI, "Read %d entries from %s", count, cs_prio);
1943
1944 ret = fclose(fp);
1945 if(ret < 0) { cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno)); }
1946 return;
1947}
1948
1949void dvbapi_resort_ecmpids(int32_t demux_index)
1950{
1951 int32_t n, cache = 0, prio = 1, highest_prio = 0, matching_done = 0, found = -1;
1952 uint16_t btun_caid = 0;
1953 struct timeb start,end;
1954 cs_ftime(&start);
1955 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
1956 {
1957 demux[demux_index].ECMpids[n].status = 0;
1958 demux[demux_index].ECMpids[n].checked = 0;
1959 }
1960
1961 demux[demux_index].max_status = 0;
1962 demux[demux_index].curindex = -1;
1963 demux[demux_index].pidindex = -1;
1964
1965 struct s_channel_cache *c = NULL;
1966
1967 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
1968 {
1969 c = dvbapi_find_channel_cache(demux_index, n, 0); // find exact channel match
1970 if(c != NULL)
1971 {
1972 found = n;
1973 cache = 2; //found cache entry with higher priority
1974 demux[demux_index].ECMpids[n].status = prio * 2; // prioritize CAIDs which already decoded same caid:provid:srvid
1975 if(c->chid < 0x10000) { demux[demux_index].ECMpids[n].CHID = c->chid; } // if chid registered in cache -> use it!
1976 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X (found caid/provid/srvid in cache - weight: %d)", demux_index, n,
1977 demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID, demux[demux_index].ECMpids[n].ECM_PID, demux[demux_index].ECMpids[n].status);
1978 break;
1979 }
1980 }
1981
1982 if(found == -1)
1983 {
1984 // prioritize CAIDs which already decoded same caid:provid
1985 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
1986 {
1987 c = dvbapi_find_channel_cache(demux_index, n, 1);
1988 if(c != NULL)
1989 {
1990 cache = 1; //found cache entry
1991 demux[demux_index].ECMpids[n].status = prio;
1992 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X (found caid/provid in cache - weight: %d)", demux_index, n,
1993 demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID, demux[demux_index].ECMpids[n].ECM_PID, demux[demux_index].ECMpids[n].status);
1994 }
1995 }
1996 }
1997
1998 // prioritize & ignore according to oscam.dvbapi and cfg.preferlocalcards
1999 if(!dvbapi_priority) { cs_log_dbg(D_DVBAPI, "Demuxer %d no oscam.dvbapi found or no valid rules are parsed!", demux_index); }
2000
2001 if(dvbapi_priority)
2002 {
2003 struct s_reader *rdr;
2004 ECM_REQUEST *er;
2005 if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
2006 { return; }
2007
2008 int32_t add_prio = 0; // make sure that p: values overrule cache
2009 if(cache == 1)
2010 { add_prio = prio; }
2011 else if(cache == 2)
2012 { add_prio = prio * 2; }
2013
2014 // reverse order! makes sure that user defined p: values are in the right order
2015 int32_t p_order = demux[demux_index].ECMpidcount;
2016
2017 highest_prio = (prio * demux[demux_index].ECMpidcount) + p_order;
2018
2019 struct s_dvbapi_priority *p;
2020 for(p = dvbapi_priority; p != NULL; p = p->next)
2021 {
2022 if(p->type != 'p' && p->type != 'i')
2023 { continue; }
2024 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2025 {
2026 if(!cache && demux[demux_index].ECMpids[n].status != 0)
2027 { continue; }
2028 else if(cache == 1 && (demux[demux_index].ECMpids[n].status < 0 || demux[demux_index].ECMpids[n].status > prio))
2029 { continue; }
2030 else if(cache == 2 && (demux[demux_index].ECMpids[n].status < 0 || demux[demux_index].ECMpids[n].status > prio * 2))
2031 { continue; }
2032
2033 er->caid = er->ocaid = demux[demux_index].ECMpids[n].CAID;
2034 er->prid = demux[demux_index].ECMpids[n].PROVID;
2035 er->pid = demux[demux_index].ECMpids[n].ECM_PID;
2036 er->srvid = demux[demux_index].program_number;
2037 er->client = cur_client();
2038
2039 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
2040 if(p->type == 'p' && btun_caid)
2041 { er->caid = btun_caid; }
2042
2043 if(p->caid && p->caid != er->caid)
2044 { continue; }
2045 if(p->provid && p->provid != er->prid)
2046 { continue; }
2047 if(p->ecmpid && p->ecmpid != er->pid)
2048 { continue; }
2049 if(p->srvid && p->srvid != er->srvid)
2050 { continue; }
2051 if (p->pidx && p->pidx-1 != n)
2052 { continue; }
2053
2054 if(p->type == 'i') // check if ignored by dvbapi
2055 {
2056 if(p->chid == 0x10000) // ignore all? disable pid
2057 {
2058 demux[demux_index].ECMpids[n].status = -1;
2059 }
2060 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X:%06X:%04X:%04X (file)", demux_index, n, demux[demux_index].ECMpids[n].CAID,
2061 demux[demux_index].ECMpids[n].PROVID, demux[demux_index].ECMpids[n].ECM_PID, (uint16_t) p->chid);
2062 continue;
2063 }
2064
2065 if(p->type == 'p')
2066 {
2067 if(demux[demux_index].ECMpids[n].status == -1) //skip ignores
2068 { continue; }
2069
2070 matching_done = 1;
2071 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
2072 {
2073 if(cfg.preferlocalcards && !is_network_reader(rdr)
2074 && rdr->card_status == CARD_INSERTED) // cfg.preferlocalcards = 1 local reader
2075 {
2076
2077 if(matching_reader(er, rdr))
2078 {
2079 if(cache == 2 && demux[demux_index].ECMpids[n].status == 1)
2080 { demux[demux_index].ECMpids[n].status++; }
2081 else if(cache && !demux[demux_index].ECMpids[n].status)
2082 { demux[demux_index].ECMpids[n].status += add_prio; }
2083 //priority*ECMpidcount should overrule network reader
2084 demux[demux_index].ECMpids[n].status += (prio * demux[demux_index].ECMpidcount) + (p_order--);
2085 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X:%04X (localrdr: %s weight: %d)", demux_index,
2086 n, demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID,
2087 demux[demux_index].ECMpids[n].ECM_PID, (uint16_t) p->chid, rdr->label,
2088 demux[demux_index].ECMpids[n].status);
2089 break;
2090 }
2091 }
2092 else // cfg.preferlocalcards = 0 or cfg.preferlocalcards = 1 and no local reader
2093 {
2094 if(matching_reader(er, rdr))
2095 {
2096 if(cache == 2 && demux[demux_index].ECMpids[n].status == 1)
2097 { demux[demux_index].ECMpids[n].status++; }
2098 else if(cache && !demux[demux_index].ECMpids[n].status)
2099 { demux[demux_index].ECMpids[n].status += add_prio; }
2100 demux[demux_index].ECMpids[n].status += prio + (p_order--);
2101 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X:%04X (rdr: %s weight: %d)", demux_index,
2102 n, demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID,
2103 demux[demux_index].ECMpids[n].ECM_PID, (uint16_t) p->chid, rdr->label,
2104 demux[demux_index].ECMpids[n].status);
2105 break;
2106 }
2107 }
2108 }
2109 }
2110 }
2111 }
2112 NULLFREE(er);
2113 }
2114
2115 if(!matching_done) //works if there is no oscam.dvbapi or if there is oscam.dvbapi but not p rules in it
2116 {
2117 if(dvbapi_priority && !matching_done)
2118 { cs_log_dbg(D_DVBAPI, "Demuxer %d no prio rules in oscam.dvbapi matches!", demux_index); }
2119
2120 struct s_reader *rdr;
2121 ECM_REQUEST *er;
2122 if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
2123 { return; }
2124
2125 highest_prio = prio * 2;
2126
2127 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2128 {
2129 if(demux[demux_index].ECMpids[n].status == -1) //skip ignores
2130 { continue; }
2131
2132 er->caid = er->ocaid = demux[demux_index].ECMpids[n].CAID;
2133 er->prid = demux[demux_index].ECMpids[n].PROVID;
2134 er->pid = demux[demux_index].ECMpids[n].ECM_PID;
2135 er->srvid = demux[demux_index].program_number;
2136 er->client = cur_client();
2137
2138 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
2139 if(btun_caid)
2140 { er->caid = btun_caid; }
2141
2142 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
2143 {
2144 if(cfg.preferlocalcards
2145 && !is_network_reader(rdr)
2146 && rdr->card_status == CARD_INSERTED) // cfg.preferlocalcards = 1 local reader
2147 {
2148 if(matching_reader(er, rdr))
2149 {
2150 demux[demux_index].ECMpids[n].status += prio * 2;
2151 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X (localrdr: %s weight: %d)", demux_index,
2152 n, demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID,
2153 demux[demux_index].ECMpids[n].ECM_PID, rdr->label,
2154 demux[demux_index].ECMpids[n].status);
2155 break;
2156 }
2157 }
2158 else // cfg.preferlocalcards = 0 or cfg.preferlocalcards = 1 and no local reader
2159 {
2160 if(matching_reader(er, rdr))
2161 {
2162 demux[demux_index].ECMpids[n].status += prio;
2163 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X (rdr: %s weight: %d)", demux_index,
2164 n, demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID,
2165 demux[demux_index].ECMpids[n].ECM_PID, rdr->label, demux[demux_index].ECMpids[n].status);
2166 break;
2167 }
2168 }
2169 }
2170 }
2171 NULLFREE(er);
2172 }
2173
2174 if(cache == 1)
2175 { highest_prio += prio; }
2176 else if(cache == 2)
2177 { highest_prio += prio * 2; };
2178
2179 highest_prio++;
2180
2181 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2182 {
2183 int32_t nr;
2184 SIDTAB *sidtab;
2185 ECM_REQUEST er;
2186 er.caid = demux[demux_index].ECMpids[n].CAID;
2187 er.prid = demux[demux_index].ECMpids[n].PROVID;
2188 er.srvid = demux[demux_index].program_number;
2189
2190 for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
2191 {
2192 if(sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid)
2193 {
2194 if((cfg.dvbapi_sidtabs.no & ((SIDTABBITS)1 << nr)) && (chk_srvid_match(&er, sidtab)))
2195 {
2196 demux[demux_index].ECMpids[n].status = -1; //ignore
2197 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X:%06X:%04X (service %s) pos %d", demux_index,
2198 n, demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID,
2199 demux[demux_index].ECMpids[n].ECM_PID, sidtab->label, nr);
2200 }
2201 if((cfg.dvbapi_sidtabs.ok & ((SIDTABBITS)1 << nr)) && (chk_srvid_match(&er, sidtab)))
2202 {
2203 demux[demux_index].ECMpids[n].status = highest_prio++; //priority
2204 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X:%06X:%04X (service: %s position: %d)", demux_index,
2205 n, demux[demux_index].ECMpids[n].CAID, demux[demux_index].ECMpids[n].PROVID,
2206 demux[demux_index].ECMpids[n].ECM_PID, sidtab->label,
2207 demux[demux_index].ECMpids[n].status);
2208 }
2209 }
2210 }
2211 }
2212
2213 struct s_reader *rdr;
2214 ECM_REQUEST *er;
2215 if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
2216 { return; }
2217
2218 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2219 {
2220 er->caid = er->ocaid = demux[demux_index].ECMpids[n].CAID;
2221 er->prid = demux[demux_index].ECMpids[n].PROVID;
2222 er->pid = demux[demux_index].ECMpids[n].ECM_PID;
2223 er->srvid = demux[demux_index].program_number;
2224 er->client = cur_client();
2225 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
2226 if(btun_caid)
2227 {
2228 er->caid = btun_caid;
2229 }
2230
2231 int32_t match = 0;
2232 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
2233 {
2234 if(matching_reader(er, rdr))
2235 {
2236 match++;
2237 }
2238 }
2239 if (match == 0)
2240 {
2241 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X:%06X:%04X:%04X (no matching reader)", demux_index, n, demux[demux_index].ECMpids[n].CAID,
2242 demux[demux_index].ECMpids[n].PROVID, demux[demux_index].ECMpids[n].ECM_PID, demux[demux_index].ECMpids[n].CHID);
2243 demux[demux_index].ECMpids[n].status = -1;
2244 }
2245 }
2246 NULLFREE(er);
2247
2248 highest_prio = 0;
2249 int32_t highest_priopid = -1;
2250 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2251 {
2252 if(demux[demux_index].ECMpids[n].status > highest_prio) // find highest prio pid
2253 {
2254 highest_prio = demux[demux_index].ECMpids[n].status;
2255 highest_priopid = n;
2256 }
2257 if(demux[demux_index].ECMpids[n].status == 0) { demux[demux_index].ECMpids[n].checked = 2; } // set pids with no status to no prio run
2258 }
2259
2260 struct s_dvbapi_priority *match;
2261 for(match = dvbapi_priority; match != NULL; match = match->next)
2262 {
2263 if(match->type != 'p')
2264 { continue; }
2265 if(!match || !match->force) // only evaluate forced prio's
2266 { continue; }
2267 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2268 {
2269 if(match->caid && match->caid != demux[demux_index].ECMpids[n].CAID) { continue; }
2270 if(match->provid && match->provid != demux[demux_index].ECMpids[n].PROVID) { continue; }
2271 if(match->srvid && match->srvid != demux[demux_index].program_number) { continue; }
2272 if(match->ecmpid && match->ecmpid != demux[demux_index].ECMpids[n].ECM_PID) { continue; }
2273 if(match->pidx && match->pidx-1 != n) { continue; }
2274 if(match->chid < 0x10000) { demux[demux_index].ECMpids[n].CHID = match->chid; }
2275 demux[demux_index].ECMpids[n].status = ++highest_prio;
2276 cs_log_dbg(D_DVBAPI, "Demuxer %d forced ecmpid %d %04X:%06X:%04X:%04X", demux_index, n, demux[demux_index].ECMpids[n].CAID,
2277 demux[demux_index].ECMpids[n].PROVID, demux[demux_index].ECMpids[n].ECM_PID, (uint16_t) match->chid);
2278 demux[demux_index].max_status = highest_prio; // register maxstatus
2279 demux[demux_index].ECMpids[n].checked = 0; // set forced pid to prio run
2280 return; // we only accept one forced pid!
2281 }
2282 }
2283 demux[demux_index].max_status = highest_prio; // register maxstatus
2284 if(highest_priopid != -1 && found == highest_priopid) // Found in cache
2285 {
2286 for(n = 0; n < demux[demux_index].ECMpidcount; n++)
2287 {
2288 if(n != found)
2289 {
2290 // disable non matching pid
2291 demux[demux_index].ECMpids[n].status = -1;
2292 }
2293 else
2294 {
2295 demux[demux_index].ECMpids[n].status = 1;
2296 }
2297 }
2298 demux[demux_index].max_emm_filter = maxfilter - 1;
2299 demux[demux_index].max_status = 1;
2300 cs_log("Demuxer %d found channel in cache and matching prio -> start descrambling ecmpid %d ", demux_index, found);
2301 }
2302 cs_ftime(&end);
2303 int64_t gone = comp_timeb(&end, &start);
2304 cs_log_dbg(D_DVBAPI, "Demuxer %d sorting the ecmpids took %"PRId64" ms", demux_index, gone);
2305 return;
2306}
2307
2308void dvbapi_parse_descriptor(int32_t demux_id, uint32_t info_length, unsigned char *buffer)
2309{
2310 // int32_t ca_pmt_cmd_id = buffer[i + 5];
2311 uint32_t descriptor_length = 0;
2312 uint32_t j, u;
2313
2314 if(info_length < 1)
2315 { return; }
2316
2317 if(buffer[0] == 0x01)
2318 {
2319 buffer = buffer + 1;
2320 info_length--;
2321 }
2322
2323 for(j = 0; j < info_length; j += descriptor_length + 2)
2324 {
2325 descriptor_length = buffer[j + 1];
2326
2327 if(buffer[j] == 0x81 && descriptor_length == 8) // private descriptor of length 8, assume enigma/tvh
2328 {
2329 demux[demux_id].enigma_namespace = b2i(4, buffer + j + 2);
2330 demux[demux_id].tsid = b2i(2, buffer + j + 6);
2331 demux[demux_id].onid = b2i(2, buffer + j + 8);
2332 cs_log_dbg(D_DVBAPI, "Demuxer %d found pmt type: %02x length: %d (assuming enigma private descriptor: namespace %04x tsid %02x onid %02x)", demux_id,
2333 buffer[j], descriptor_length, demux[demux_id].enigma_namespace, demux[demux_id].tsid, demux[demux_id].onid);
2334 }
2335 else if (descriptor_length !=0)
2336 {
2337 cs_log_dbg(D_TRACE, "Demuxer %d found pmt type: %02x length: %d", demux_id, buffer[j], descriptor_length);
2338 }
2339
2340 if(buffer[j] != 0x09) { continue; }
2341
2342 if(demux[demux_id].ECMpidcount >= ECM_PIDS) { break; }
2343
2344 int32_t descriptor_ca_system_id = b2i(2, buffer + j + 2);
2345 int32_t descriptor_ca_pid = b2i(2, buffer + j + 4)&0x1FFF;
2346 int32_t descriptor_ca_provider = 0;
2347
2348 if(descriptor_ca_system_id >> 8 == 0x01)
2349 {
2350 for(u = 2; u < descriptor_length; u += 15)
2351 {
2352 descriptor_ca_pid = b2i(2, buffer + j + u + 2)&0x1FFF;
2353 descriptor_ca_provider = b2i(2, buffer + j + u + 4);
2354 dvbapi_add_ecmpid(demux_id, descriptor_ca_system_id, descriptor_ca_pid, descriptor_ca_provider);
2355 }
2356 }
2357 else
2358 {
2359 if(caid_is_viaccess(descriptor_ca_system_id) && descriptor_length == 0x0F && buffer[j + 12] == 0x14)
2360 { descriptor_ca_provider = b2i(3, buffer + j + 14) &0xFFFFF0; }
2361
2362 if(caid_is_nagra(descriptor_ca_system_id) && descriptor_length == 0x07)
2363 { descriptor_ca_provider = b2i(2, buffer + j + 7); }
2364
2365 if(descriptor_ca_system_id >> 8 == 0x4A && descriptor_length == 0x05)
2366 { descriptor_ca_provider = buffer[j + 6]; }
2367
2368 dvbapi_add_ecmpid(demux_id, descriptor_ca_system_id, descriptor_ca_pid, descriptor_ca_provider);
2369
2370 }
2371 }
2372
2373 // Apply mapping:
2374 if(dvbapi_priority)
2375 {
2376 struct s_dvbapi_priority *mapentry;
2377 for(j = 0; (int32_t)j < demux[demux_id].ECMpidcount; j++)
2378 {
2379 mapentry = dvbapi_check_prio_match(demux_id, j, 'm');
2380 if(mapentry)
2381 {
2382 cs_log_dbg(D_DVBAPI, "Demuxer %d mapping ecmpid %d from %04X:%06X to %04X:%06X", demux_id, j,
2383 demux[demux_id].ECMpids[j].CAID, demux[demux_id].ECMpids[j].PROVID,
2384 mapentry->mapcaid, mapentry->mapprovid);
2385 demux[demux_id].ECMpids[j].CAID = mapentry->mapcaid;
2386 demux[demux_id].ECMpids[j].PROVID = mapentry->mapprovid;
2387 }
2388 }
2389 }
2390}
2391
2392void request_cw(struct s_client *client, ECM_REQUEST *er, int32_t demux_id, uint8_t delayed_ecm_check)
2393{
2394 int32_t filternum = dvbapi_set_section_filter(demux_id, er, -1); // set ecm filter to odd -> even and visaversa
2395
2396 if(!USE_OPENXCAS && filternum < 0)
2397 {
2398 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting cw -> ecm filter was killed!", demux_id);
2399 return;
2400 }
2401
2402 cs_log_dbg(D_DVBAPI, "Demuxer %d get controlword!", demux_id);
2403 get_cw(client, er);
2404
2405 if (!USE_OPENXCAS) {
2406 if(delayed_ecm_check) { memcpy(demux[demux_id].demux_fd[filternum].ecmd5, er->ecmd5, CS_ECMSTORESIZE); } // register this ecm as latest request for this filter
2407 else { memset(demux[demux_id].demux_fd[filternum].ecmd5, 0, CS_ECMSTORESIZE); } // zero out ecmcheck!
2408 }
2409
2410#ifdef WITH_DEBUG
2411 char buf[ECM_FMT_LEN];
2412 format_ecm(er, buf, ECM_FMT_LEN);
2413 cs_log_dbg(D_DVBAPI, "Demuxer %d request controlword for ecm %s", demux_id, buf);
2414#endif
2415}
2416
2417void dvbapi_try_next_caid(int32_t demux_id, int8_t checked)
2418{
2419
2420 int32_t n, j, found = -1, started = 0;
2421
2422 int32_t status = demux[demux_id].max_status;
2423
2424 for(j = status; j >= 0; j--) // largest status first!
2425 {
2426
2427 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
2428 {
2429 //cs_log_dbg(D_DVBAPI,"Demuxer %d PID %d checked = %d status = %d (searching for pid with status = %d)", demux_id, n,
2430 // demux[demux_id].ECMpids[n].checked, demux[demux_id].ECMpids[n].status, j);
2431 if(demux[demux_id].ECMpids[n].checked == checked && demux[demux_id].ECMpids[n].status == j)
2432 {
2433 found = n;
2434
2435 openxcas_set_provid(demux[demux_id].ECMpids[found].PROVID);
2436 openxcas_set_caid(demux[demux_id].ECMpids[found].CAID);
2437 openxcas_set_ecm_pid(demux[demux_id].ECMpids[found].ECM_PID);
2438
2439 // fixup for cas that need emm first!
2440 if(caid_is_irdeto(demux[demux_id].ECMpids[found].CAID)) { demux[demux_id].emmstart.time = 0; }
2441 started = dvbapi_start_descrambling(demux_id, found, checked);
2442 if(cfg.dvbapi_requestmode == 0 && started == 1) { return; } // in requestmode 0 we only start 1 ecm request at the time
2443 }
2444 }
2445 }
2446
2447 if(found == -1 && demux[demux_id].pidindex == -1)
2448 {
2449 cs_log("Demuxer %d no suitable readers found that can be used for decoding!", demux_id);
2450 return;
2451 }
2452}
2453
2454static void getDemuxOptions(int32_t demux_id, unsigned char *buffer, uint16_t *ca_mask, uint16_t *demux_index, uint16_t *adapter_index, uint16_t *pmtpid)
2455{
2456 *ca_mask = 0x01, *demux_index = 0x00, *adapter_index = 0x00, *pmtpid = 0x00;
2457
2458 if(buffer[17] == 0x82 && buffer[18] == 0x02)
2459 {
2460 // enigma2
2461 *ca_mask = buffer[19];
2462 uint32_t demuxid = buffer[20];
2463 if (demuxid == 0xff) demuxid = 0; // tryfix prismcube (0xff -> "demux-1" = error! )
2464 *demux_index = demuxid;
2465 if (buffer[21]==0x84 && buffer[22]==0x02) *pmtpid = b2i(2, buffer+23);
2466 if (buffer[25]==0x83 && buffer[26]==0x01) *adapter_index=buffer[27]; // from code cahandler.cpp 0x83 index of adapter
2467 }
2468
2469 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
2470 {
2471 *ca_mask = demux_id + 1;
2472 *demux_index = demux_id;
2473 }
2474
2475 if(cfg.dvbapi_boxtype == BOXTYPE_QBOXHD && buffer[17] == 0x82 && buffer[18] == 0x03)
2476 {
2477 // ca_mask = buffer[19]; // with STONE 1.0.4 always 0x01
2478 *demux_index = buffer[20]; // with STONE 1.0.4 always 0x00
2479 *adapter_index = buffer[21]; // with STONE 1.0.4 adapter index can be 0,1,2
2480 *ca_mask = (1 << *adapter_index); // use adapter_index as ca_mask (used as index for ca_fd[] array)
2481 }
2482
2483 if((cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX) && buffer[7] == 0x82 && buffer[8] == 0x02)
2484 {
2485 *demux_index = buffer[9]; // it is always 0 but you never know
2486 *adapter_index = buffer[10]; // adapter index can be 0,1,2
2487 *ca_mask = (1 << *adapter_index); // use adapter_index as ca_mask (used as index for ca_fd[] array)
2488 }
2489}
2490
2491static void dvbapi_capmt_notify(struct demux_s *dmx)
2492{
2493 struct s_client *cl;
2494 for(cl = first_client->next; cl ; cl = cl->next)
2495 {
2496 if((cl->typ == 'p' || cl->typ == 'r') && cl->reader && cl->reader->ph.c_capmt)
2497 {
2498 struct demux_s *curdemux;
2499 if(cs_malloc(&curdemux, sizeof(struct demux_s)))
2500 {
2501 memcpy(curdemux, dmx, sizeof(struct demux_s));
2502 add_job(cl, ACTION_READER_CAPMT_NOTIFY, curdemux, sizeof(struct demux_s));
2503 }
2504 }
2505 }
2506}
2507
2508int32_t dvbapi_parse_capmt(unsigned char *buffer, uint32_t length, int32_t connfd, char *pmtfile)
2509{
2510 uint32_t i = 0, start_descrambling = 0;
2511 int32_t j = 0;
2512 int32_t demux_id = -1;
2513 uint16_t ca_mask, demux_index, adapter_index, pmtpid;
2514
2515#define LIST_MORE 0x00 //*CA application should append a 'MORE' CAPMT object to the list and start receiving the next object
2516#define LIST_FIRST 0x01 //*CA application should clear the list when a 'FIRST' CAPMT object is received, and start receiving the next object
2517#define LIST_LAST 0x02 //*CA application should append a 'LAST' CAPMT object to the list and start working with the list
2518#define LIST_ONLY 0x03 //*CA application should clear the list when an 'ONLY' CAPMT object is received, and start working with the object
2519#define LIST_ADD 0x04 //*CA application should append an 'ADD' CAPMT object to the current list and start working with the updated list
2520#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
2521
2522#ifdef WITH_COOLAPI
2523 int32_t ca_pmt_list_management = LIST_ONLY;
2524#else
2525 int32_t ca_pmt_list_management = buffer[0];
2526#endif
2527 uint32_t program_number = b2i(2, buffer + 1);
2528 uint32_t program_info_length = b2i(2, buffer + 4) &0xFFF;
2529
2530 cs_log_dump_dbg(D_DVBAPI, buffer, length, "capmt:");
2531 cs_log_dbg(D_DVBAPI, "Receiver sends PMT command %d for channel %04X", ca_pmt_list_management, program_number);
2532 if((ca_pmt_list_management == LIST_FIRST || ca_pmt_list_management == LIST_ONLY))
2533 {
2534 for(i = 0; i < MAX_DEMUX; i++)
2535 {
2536 if(cfg.dvbapi_pmtmode == 6 && pmt_stopmarking == 1) { continue; } // already marked -> skip!
2537 if(demux[i].program_number == 0) { continue; } // skip empty demuxers
2538 if(demux[i].ECMpidcount != 0 && demux[i].pidindex != -1 ) { demux[i].running = 1; } // running channel changes from scrambled to fta
2539 if(demux[i].socket_fd != connfd) { continue; } // skip demuxers belonging to other ca pmt connection
2540 if(cfg.dvbapi_pmtmode == 6)
2541 {
2542 demux[i].stopdescramble = 1; // Mark for deletion if not used again by following pmt objects.
2543 cs_log_dbg(D_DVBAPI, "Marked demuxer %d/%d (srvid = %04X fd = %d) to stop decoding", i, MAX_DEMUX, demux[i].program_number, connfd);
2544 }
2545 }
2546 pmt_stopmarking = 1;
2547 }
2548 getDemuxOptions(i, buffer, &ca_mask, &demux_index, &adapter_index, &pmtpid);
2549 cs_log_dbg(D_DVBAPI,"Receiver wants to demux srvid %04X on adapter %04X camask %04X index %04X pmtpid %04X",
2550 program_number, adapter_index, ca_mask, demux_index, pmtpid);
2551
2552 for(i = 0; i < MAX_DEMUX; i++) // search current demuxers for running the same program as the one we received in this PMT object
2553 {
2554 if(demux[i].program_number == 0) { continue; }
2555 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT) demux_index = i; // fixup for ipbox
2556
2557 bool full_check = 1, matched = 0;
2558 if (config_enabled(WITH_COOLAPI) || is_samygo)
2559 full_check = 0;
2560
2561 if (full_check)
2562 matched = (connfd > 0 && demux[i].socket_fd == connfd) && demux[i].program_number == program_number;
2563 else
2564 matched = connfd > 0 && demux[i].program_number == program_number;
2565
2566 if(matched)
2567 {
2568 if (full_check) {
2569 if (demux[i].adapter_index != adapter_index) continue; // perhaps next demuxer matches?
2570 if (demux[i].ca_mask != ca_mask) continue; // perhaps next demuxer matches?
2571 if (demux[i].demux_index != demux_index) continue; // perhaps next demuxer matches?
2572 }
2573 if(ca_pmt_list_management == LIST_UPDATE){
2574 cs_log("Demuxer %d PMT update for decoding of SRVID %04X! ", i, program_number);
2575 }
2576
2577 demux_id = i;
2578
2579 cs_log("Demuxer %d continue decoding of SRVID %04X", i, demux[i].program_number);
2580
2581 openxcas_set_sid(program_number);
2582
2583 demux[i].stopdescramble = 0; // dont stop current demuxer!
2584 break; // no need to explore other demuxers since we have a found!
2585 }
2586 }
2587
2588 // stop descramble old demuxers from this ca pmt connection that arent used anymore
2589 if((ca_pmt_list_management == LIST_LAST) || (ca_pmt_list_management == LIST_ONLY))
2590 {
2591 for(j = 0; j < MAX_DEMUX; j++)
2592 {
2593 if(demux[j].program_number == 0) { continue; }
2594 if(demux[j].stopdescramble == 1) { dvbapi_stop_descrambling(j); } // Stop descrambling and remove all demuxer entries not in new PMT.
2595 }
2596 start_descrambling = 1; // flag that demuxer descrambling is to be executed!
2597 }
2598
2599 if(demux_id == -1)
2600 {
2601 for(demux_id = 0; demux_id < MAX_DEMUX && demux[demux_id].program_number > 0; demux_id++) { ; }
2602 }
2603
2604 if(demux_id >= MAX_DEMUX)
2605 {
2606 cs_log("ERROR: No free id (MAX_DEMUX)");
2607 return -1;
2608 }
2609
2610 demux[demux_id].program_number = program_number; // do this early since some prio items use them!
2611
2612 demux[demux_id].enigma_namespace = 0;
2613 demux[demux_id].tsid = 0;
2614 demux[demux_id].onid = 0;
2615 demux[demux_id].pmtpid = pmtpid;
2616
2617 if(pmtfile)
2618 {
2619 cs_strncpy(demux[demux_id].pmt_file, pmtfile, sizeof(demux[demux_id].pmt_file));
2620 }
2621
2622 demux[demux_id].ECMpidcount = 0; // reset number of ecmpids
2623
2624 if(program_info_length > 1 && program_info_length < length)
2625 {
2626 dvbapi_parse_descriptor(demux_id, program_info_length - 1, buffer + 7);
2627 }
2628
2629 uint32_t es_info_length = 0, vpid = 0;
2630 struct s_dvbapi_priority *addentry;
2631
2632 for(j = 0; j < demux[demux_id].ECMpidcount; j++)
2633 {
2634 demux[demux_id].ECMpids[j].streams = 0; // reset streams of each ecmpid!
2635 }
2636 demux[demux_id].STREAMpidcount = 0; // reset number of streams
2637
2638 const char *stream_in_text = NULL;
2639
2640 for(i = program_info_length + 6; i < length; i += es_info_length + 5)
2641 {
2642 uint32_t stream_type = buffer[i];
2643 uint16_t elementary_pid = b2i(2, buffer + i + 1)&0x1FFF;
2644 es_info_length = b2i(2, buffer + i +3)&0x0FFF;
2645 if(stream_type < (sizeof(streamtxt) / sizeof(const char *)))
2646 {
2647 stream_in_text = streamtxt[stream_type];
2648 }
2649 else
2650 {
2651 stream_in_text = "";
2652 }
2653 cs_log_dbg(D_DVBAPI, "Demuxer %d added new stream %s(type: %02x pid: %04x length: %d)", demux_id, stream_in_text, stream_type, elementary_pid, es_info_length);
2654
2655 if(demux[demux_id].STREAMpidcount >= ECM_PIDS)
2656 {
2657 break;
2658 }
2659
2660 demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount++] = elementary_pid;
2661 // find and register videopid
2662 if(!vpid && (stream_type == 01 || stream_type == 02 || stream_type == 0x10 || stream_type == 0x1B)) { vpid = elementary_pid; }
2663
2664 if(es_info_length != 0 && es_info_length < length)
2665 {
2666 dvbapi_parse_descriptor(demux_id, es_info_length, buffer + i + 5);
2667 }
2668 else
2669 {
2670 for(addentry = dvbapi_priority; addentry != NULL; addentry = addentry->next)
2671 {
2672 if(addentry->type != 'a'
2673 || (addentry->ecmpid && pmtpid && addentry->ecmpid != pmtpid) // ecmpid is misused to hold pmtpid in case of A: rule
2674 || (addentry->ecmpid && !pmtpid && addentry->ecmpid != vpid) // some receivers dont forward pmtpid, use vpid instead
2675 || (addentry->srvid != demux[demux_id].program_number))
2676 { continue; }
2677 cs_log_dbg(D_DVBAPI, "Demuxer %d added fake ecmpid %04X:%06x:%04x for unencrypted stream on srvid %04X", demux_id, addentry->mapcaid, addentry->mapprovid,
2678 addentry->mapecmpid, demux[demux_id].program_number);
2679 dvbapi_add_ecmpid(demux_id, addentry->mapcaid, addentry->mapecmpid, addentry->mapprovid);
2680 break;
2681 }
2682 }
2683 }
2684 for(j = 0; j < demux[demux_id].ECMpidcount; j++)
2685 {
2686 demux[demux_id].ECMpids[j].VPID = vpid; // register found vpid on all ecmpids of this demuxer
2687 }
2688 cs_log("Demuxer %d found %d ECMpids and %d STREAMpids in PMT", demux_id, demux[demux_id].ECMpidcount, demux[demux_id].STREAMpidcount);
2689
2690 getDemuxOptions(demux_id, buffer, &ca_mask, &demux_index, &adapter_index, &pmtpid);
2691 cs_log("Demuxer %d receiver wants to demux srvid %04X on adapter %04X camask %04X index %04X pmtpid %04X", demux_id,
2692 demux[demux_id].program_number, adapter_index, ca_mask, demux_index, pmtpid);
2693 demux[demux_id].adapter_index = adapter_index;
2694 demux[demux_id].ca_mask = ca_mask;
2695 demux[demux_id].rdr = NULL;
2696 demux[demux_id].demux_index = demux_index;
2697 demux[demux_id].socket_fd = connfd;
2698 demux[demux_id].stopdescramble = 0; // remove deletion mark!
2699
2700 // remove from unassoc_fd when necessary
2701 for (j = 0; j < MAX_DEMUX; j++)
2702 if (unassoc_fd[j] == connfd)
2703 unassoc_fd[j] = 0;
2704
2705 char channame[32];
2706 get_servicename(dvbapi_client, demux[demux_id].program_number, demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[0].CAID : NO_CAID_VALUE, channame);
2707 cs_log("Demuxer %d new program number: %04X (%s) [pmt_list_management %d]", demux_id, program_number, channame, ca_pmt_list_management);
2708
2709 dvbapi_capmt_notify(&demux[demux_id]);
2710
2711 cs_log_dbg(D_DVBAPI, "Demuxer %d demux_index: %2d ca_mask: %02x program_info_length: %3d ca_pmt_list_management %02x",
2712 demux_id, demux[demux_id].demux_index, demux[demux_id].ca_mask, program_info_length, ca_pmt_list_management);
2713
2714 struct s_dvbapi_priority *xtraentry;
2715 int32_t k, l, m, xtra_demux_id;
2716
2717 for(xtraentry = dvbapi_priority; xtraentry != NULL; xtraentry = xtraentry->next)
2718 {
2719 if(xtraentry->type != 'x') { continue; }
2720
2721 for(j = 0; j <= demux[demux_id].ECMpidcount; ++j)
2722 {
2723 if((xtraentry->caid && xtraentry->caid != demux[demux_id].ECMpids[j].CAID)
2724 || (xtraentry->provid && xtraentry->provid != demux[demux_id].ECMpids[j].PROVID)
2725 || (xtraentry->ecmpid && xtraentry->ecmpid != demux[demux_id].ECMpids[j].ECM_PID)
2726 || (xtraentry->srvid && xtraentry->srvid != demux[demux_id].program_number))
2727 { continue; }
2728
2729 cs_log("Mapping ecmpid %04X:%06X:%04X:%04X to xtra demuxer/ca-devices", xtraentry->caid, xtraentry->provid, xtraentry->ecmpid, xtraentry->srvid);
2730
2731 for(xtra_demux_id = 0; xtra_demux_id < MAX_DEMUX && demux[xtra_demux_id].program_number > 0; xtra_demux_id++)
2732 { ; }
2733
2734 if(xtra_demux_id >= MAX_DEMUX)
2735 {
2736 cs_log("Found no free demux device for xtra streams.");
2737 continue;
2738 }
2739 // copy to new demuxer
2740 getDemuxOptions(demux_id, buffer, &ca_mask, &demux_index, &adapter_index, &pmtpid);
2741 demux[xtra_demux_id].ECMpids[0] = demux[demux_id].ECMpids[j];
2742 demux[xtra_demux_id].ECMpidcount = 1;
2743 demux[xtra_demux_id].STREAMpidcount = 0;
2744 demux[xtra_demux_id].program_number = demux[demux_id].program_number;
2745 demux[xtra_demux_id].pmtpid = demux[demux_id].pmtpid;
2746 demux[xtra_demux_id].demux_index = demux_index;
2747 demux[xtra_demux_id].adapter_index = adapter_index;
2748 demux[xtra_demux_id].ca_mask = ca_mask;
2749 demux[xtra_demux_id].socket_fd = connfd;
2750 demux[xtra_demux_id].stopdescramble = 0; // remove deletion mark!
2751 demux[xtra_demux_id].rdr = NULL;
2752 demux[xtra_demux_id].curindex = -1;
2753
2754 // add streams to xtra demux
2755 for(k = 0; k < demux[demux_id].STREAMpidcount; ++k)
2756 {
2757 if(!demux[demux_id].ECMpids[j].streams || demux[demux_id].ECMpids[j].streams & (1 << k))
2758 {
2759 demux[xtra_demux_id].ECMpids[0].streams |= (1 << demux[xtra_demux_id].STREAMpidcount);
2760 demux[xtra_demux_id].STREAMpids[demux[xtra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpids[k];
2761 ++demux[xtra_demux_id].STREAMpidcount;
2762
2763 // shift stream associations in normal demux because we will remove the stream entirely
2764 for(l = 0; l < demux[demux_id].ECMpidcount; ++l)
2765 {
2766 for(m = k; m < demux[demux_id].STREAMpidcount - 1; ++m)
2767 {
2768 if(demux[demux_id].ECMpids[l].streams & (1 << (m + 1)))
2769 {
2770 demux[demux_id].ECMpids[l].streams |= (1 << m);
2771 }
2772 else
2773 {
2774 demux[demux_id].ECMpids[l].streams &= ~(1 << m);
2775 }
2776 }
2777 }
2778
2779 // remove stream association from normal demux device
2780 for(l = k; l < demux[demux_id].STREAMpidcount - 1; ++l)
2781 {
2782 demux[demux_id].STREAMpids[l] = demux[demux_id].STREAMpids[l + 1];
2783 }
2784 --demux[demux_id].STREAMpidcount;
2785 --k;
2786 }
2787 }
2788
2789 // remove ecmpid from normal demuxer
2790 for(k = j; k < demux[demux_id].ECMpidcount; ++k)
2791 {
2792 demux[demux_id].ECMpids[k] = demux[demux_id].ECMpids[k + 1];
2793 }
2794 --demux[demux_id].ECMpidcount;
2795 --j;
2796
2797 if(demux[xtra_demux_id].STREAMpidcount <= 0)
2798 {
2799 cs_log("Found no streams for xtra demuxer. Not starting additional decoding on it.");
2800 demux[xtra_demux_id].program_number = 0;
2801 demux[xtra_demux_id].stopdescramble = 1;
2802 }
2803
2804 if(demux[demux_id].STREAMpidcount < 1)
2805 {
2806 cs_log("Found no streams for normal demuxer. Not starting additional decoding on it.");
2807 }
2808 }
2809 }
2810
2811 if(demux[demux_id].running == 0) // only do emm setup on non-running channels!
2812 {
2813 demux[demux_id].emm_filter = -1; // to register first run emmfilter start
2814 if(cfg.dvbapi_au > 0 && demux[demux_id].emmstart.time == 1) // irdeto fetch emm cat direct!
2815 {
2816 cs_ftime(&demux[demux_id].emmstart); // trick to let emm fetching start after 30 seconds to speed up zapping
2817 dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x001, 0x001, 0x01, 0x01, 0xFF, 0, TYPE_EMM); //CAT
2818 }
2819 else { cs_ftime(&demux[demux_id].emmstart); } // for all other caids delayed start!
2820 }
2821
2822 if(start_descrambling)
2823 {
2824 for(j = 0; j < MAX_DEMUX; j++)
2825 {
2826 if(demux[j].program_number == 0) { continue; }
2827
2828 if(demux[j].running) disable_unused_streampids(j); // disable all streampids not in use anymore
2829
2830 if(demux[j].running == 0 && demux[j].ECMpidcount != 0 ) // only start demuxer if it wasnt running
2831 {
2832 openxcas_set_sid(demux[j].program_number);
2833 demux[j].decodingtries = -1;
2834 dvbapi_resort_ecmpids(j);
2835 dvbapi_try_next_caid(j, 0);
2836 cs_sleepms(1);
2837 }
2838 else if(demux[j].ECMpidcount == 0) //fta do logging and part of ecmhandler since there will be no ecms asked!
2839 {
2840 demux[j].running = 0; // reset running flag
2841 demux[demux_id].pidindex = -1; // reset ecmpid used for descrambling
2842 dvbapi_stop_filter(j, TYPE_ECM);
2843 if(cfg.usrfileflag) { cs_statistics(dvbapi_client);} // add to user log previous channel + time on channel
2844 dvbapi_client->last_srvid = demux[demux_id].program_number; // set new channel srvid
2845 dvbapi_client->last_caid = NO_CAID_VALUE; // FTA channels have no caid!
2846 dvbapi_client->lastswitch = dvbapi_client->last = time((time_t *)0); // reset idle-Time & last switch
2847 }
2848 }
2849 }
2850 return demux_id;
2851}
2852
2853
2854void dvbapi_handlesockmsg(unsigned char *buffer, uint32_t len, int32_t connfd)
2855{
2856 uint32_t val = 0, size = 0, i, k;
2857
2858 for(k = 0; k < len; k += 3 + size + val)
2859 {
2860 if(buffer[0 + k] != 0x9F || buffer[1 + k] != 0x80)
2861 {
2862 cs_log_dbg(D_DVBAPI, "Received unknown PMT command: %02x", buffer[0 + k]);
2863 break;
2864 }
2865
2866 if(k > 0)
2867 cs_log_dump_dbg(D_DVBAPI, buffer + k, len - k, "Parsing next PMT object(s):");
2868
2869 if(buffer[3 + k] & 0x80)
2870 {
2871 val = 0;
2872 size = buffer[3 + k] & 0x7F;
2873 for(i = 0; i < size; i++)
2874 { val = (val << 8) | buffer[i + 1 + 3 + k]; }
2875 size++;
2876 }
2877 else
2878 {
2879 val = buffer[3 + k] & 0x7F;
2880 size = 1;
2881 }
2882 switch(buffer[2 + k])
2883 {
2884 case 0x32:
2885 dvbapi_parse_capmt(buffer + size + 3 + k, val, connfd, NULL);
2886 break;
2887 case 0x3f:
2888 // 9F 80 3f 04 83 02 00 <demux index>
2889 cs_log_dump_dbg(D_DVBAPI, buffer, len, "capmt 3f:");
2890 // ipbox fix
2891 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_listenport)
2892 {
2893 int32_t demux_index = buffer[7 + k];
2894 for(i = 0; i < MAX_DEMUX; i++)
2895 {
2896 // 0xff demux_index is a wildcard => close all related demuxers
2897 if (demux_index == 0xff)
2898 {
2899 if (demux[i].socket_fd == connfd)
2900 dvbapi_stop_descrambling(i);
2901 }
2902 else if (demux[i].demux_index == demux_index)
2903 {
2904 dvbapi_stop_descrambling(i);
2905 break;
2906 }
2907 }
2908 if (cfg.dvbapi_boxtype == BOXTYPE_IPBOX)
2909 {
2910 // check do we have any demux running on this fd
2911 int16_t execlose = 1;
2912 for(i = 0; i < MAX_DEMUX; i++)
2913 {
2914 if(demux[i].socket_fd == connfd)
2915 {
2916 execlose = 0;
2917 break;
2918 }
2919 }
2920 if(execlose)
2921 {
2922 int32_t ret = close(connfd);
2923 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
2924 }
2925 }
2926 }
2927 else
2928 {
2929 if(cfg.dvbapi_pmtmode != 6)
2930 {
2931 int32_t ret = close(connfd);
2932 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
2933 }
2934 }
2935 break;
2936 default:
2937 cs_log_dbg(D_DVBAPI, "handlesockmsg() unknown command");
2938 cs_log_dump(buffer, len, "unknown command:");
2939 break;
2940 }
2941 }
2942}
2943
2944int32_t dvbapi_init_listenfd(void)
2945{
2946 int32_t clilen, listenfd;
2947 struct sockaddr_un servaddr;
2948
2949 memset(&servaddr, 0, sizeof(struct sockaddr_un));
2950 servaddr.sun_family = AF_UNIX;
2951 cs_strncpy(servaddr.sun_path, devices[selected_box].cam_socket_path, sizeof(servaddr.sun_path));
2952 clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
2953
2954 if((unlink(devices[selected_box].cam_socket_path) < 0) && (errno != ENOENT))
2955 { return 0; }
2956 if((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
2957 { return 0; }
2958 if(bind(listenfd, (struct sockaddr *)&servaddr, clilen) < 0)
2959 { return 0; }
2960 if(listen(listenfd, 5) < 0)
2961 { return 0; }
2962
2963 // change the access right on the camd.socket
2964 // this will allow oscam to run as root if needed
2965 // and still allow non root client to connect to the socket
2966 chmod(devices[selected_box].cam_socket_path, S_IRWXU | S_IRWXG | S_IRWXO);
2967
2968 return listenfd;
2969}
2970
2971int32_t dvbapi_net_init_listenfd(void)
2972{
2973 int32_t listenfd;
2974 struct SOCKADDR servaddr;
2975
2976 memset(&servaddr, 0, sizeof(servaddr));
2977 SIN_GET_FAMILY(servaddr) = DEFAULT_AF;
2978 SIN_GET_ADDR(servaddr) = ADDR_ANY;
2979 SIN_GET_PORT(servaddr) = htons((uint16_t)cfg.dvbapi_listenport);
2980
2981 if((listenfd = socket(DEFAULT_AF, SOCK_STREAM, 0)) < 0)
2982 { return 0; }
2983
2984 int32_t opt = 0;
2985#ifdef IPV6SUPPORT
2986 // set the server socket option to listen on IPv4 and IPv6 simultaneously
2987 setsockopt(listenfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&opt, sizeof(opt));
2988#endif
2989
2990 opt = 1;
2991 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
2992 set_so_reuseport(listenfd);
2993
2994 if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
2995 { return 0; }
2996 if(listen(listenfd, 5) < 0)
2997 { return 0; }
2998
2999 return listenfd;
3000}
3001
3002static pthread_mutex_t event_handler_lock;
3003
3004void event_handler(int32_t UNUSED(signal))
3005{
3006 struct stat pmt_info;
3007 char dest[1024];
3008 DIR *dirp;
3009 struct dirent entry, *dp = NULL;
3010 int32_t i, pmt_fd;
3011 uchar mbuf[2048]; // dirty fix: larger buffer needed for CA PMT mode 6 with many parallel channels to decode
3012 if(dvbapi_client != cur_client()) { return; }
3013
3014 pthread_mutex_lock(&event_handler_lock);
3015
3016 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
3017 { pausecam = 0; }
3018 else
3019 {
3020 int32_t standby_fd = open(STANDBY_FILE, O_RDONLY);
3021 pausecam = (standby_fd > 0) ? 1 : 0;
3022 if(standby_fd > 0)
3023 {
3024 int32_t ret = close(standby_fd);
3025 if(ret < 0) { cs_log("ERROR: Could not close standby fd (errno=%d %s)", errno, strerror(errno)); }
3026 }
3027 }
3028
3029 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_pmtmode == 1)
3030 {
3031 pthread_mutex_unlock(&event_handler_lock);
3032 return;
3033 }
3034
3035 for(i = 0; i < MAX_DEMUX; i++)
3036 {
3037 if(demux[i].pmt_file[0] != 0)
3038 {
3039 snprintf(dest, sizeof(dest), "%s%s", TMPDIR, demux[i].pmt_file);
3040 pmt_fd = open(dest, O_RDONLY);
3041 if(pmt_fd > 0)
3042 {
3043 if(fstat(pmt_fd, &pmt_info) != 0)
3044 {
3045 int32_t ret = close(pmt_fd);
3046 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
3047 continue;
3048 }
3049
3050 if((time_t)pmt_info.st_mtime != demux[i].pmt_time)
3051 {
3052 dvbapi_stop_descrambling(i);
3053 }
3054
3055 int32_t ret = close(pmt_fd);
3056 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
3057 continue;
3058 }
3059 else
3060 {
3061 cs_log("Demuxer %d Unable to open PMT file %s -> stop descrambling!", i, dest);
3062 dvbapi_stop_descrambling(i);
3063 }
3064 }
3065 }
3066
3067 if(disable_pmt_files)
3068 {
3069 pthread_mutex_unlock(&event_handler_lock);
3070 return;
3071 }
3072
3073 dirp = opendir(TMPDIR);
3074 if(!dirp)
3075 {
3076 cs_log_dbg(D_DVBAPI, "opendir failed (errno=%d %s)", errno, strerror(errno));
3077 pthread_mutex_unlock(&event_handler_lock);
3078 return;
3079 }
3080
3081 while(!cs_readdir_r(dirp, &entry, &dp))
3082 {
3083 if(!dp) { break; }
3084
3085 if(strlen(dp->d_name) < 7)
3086 { continue; }
3087 if(strncmp(dp->d_name, "pmt", 3) != 0 || strncmp(dp->d_name + strlen(dp->d_name) - 4, ".tmp", 4) != 0)
3088 { continue; }
3089#ifdef WITH_STAPI
3090 struct s_dvbapi_priority *p;
3091 for(p = dvbapi_priority; p != NULL; p = p->next) // stapi: check if there is a device connected to this pmt file!
3092 {
3093 if(p->type != 's') { continue; } // stapi rule?
3094 if(strcmp(dp->d_name, p->pmtfile) != 0) { continue; } // same file?
3095 break; // found match!
3096 }
3097 if(p == NULL)
3098 {
3099 cs_log_dbg(D_DVBAPI, "No matching S: line in oscam.dvbapi for pmtfile %s -> skip!", dp->d_name);
3100 continue;
3101 }
3102#endif
3103 snprintf(dest, sizeof(dest), "%s%s", TMPDIR, dp->d_name);
3104 pmt_fd = open(dest, O_RDONLY);
3105 if(pmt_fd < 0)
3106 { continue; }
3107
3108 if(fstat(pmt_fd, &pmt_info) != 0)
3109 {
3110 int32_t ret = close(pmt_fd);
3111 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
3112 continue;
3113 }
3114
3115 int32_t found = 0;
3116 for(i = 0; i < MAX_DEMUX; i++)
3117 {
3118 if(strcmp(demux[i].pmt_file, dp->d_name) == 0)
3119 {
3120 if((time_t)pmt_info.st_mtime == demux[i].pmt_time)
3121 {
3122 found = 1;
3123 continue;
3124 }
3125 dvbapi_stop_descrambling(i);
3126 }
3127 }
3128 if(found)
3129 {
3130 int32_t ret = close(pmt_fd);
3131 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
3132 continue;
3133 }
3134
3135 cs_log_dbg(D_DVBAPI, "found pmt file %s", dest);
3136 cs_sleepms(100);
3137
3138 uint32_t len = read(pmt_fd, mbuf, sizeof(mbuf));
3139 int32_t ret = close(pmt_fd);
3140 if(ret < 0) { cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno)); }
3141
3142 if(len < 1)
3143 {
3144 cs_log_dbg(D_DVBAPI, "pmt file %s have invalid len!", dest);
3145 continue;
3146 }
3147
3148 int32_t pmt_id;
3149
3150#ifdef QBOXHD
3151 uint32_t j1, j2;
3152 // QboxHD pmt.tmp is the full capmt written as a string of hex values
3153 // pmt.tmp must be longer than 3 bytes (6 hex chars) and even length
3154 if((len < 6) || ((len % 2) != 0) || ((len / 2) > sizeof(dest)))
3155 {
3156 cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, incorrect length");
3157 continue;
3158 }
3159
3160 for(j2 = 0, j1 = 0; j2 < len; j2 += 2, j1++)
3161 {
3162 unsigned int tmp;
3163 if(sscanf((char *)mbuf + j2, "%02X", &tmp) != 1)
3164 {
3165 cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, data not valid in position %d", j2);
3166 pthread_mutex_unlock(&event_handler_lock);
3167 return;
3168 }
3169 else
3170 {
3171 memcpy(dest + j1, &tmp, 4);
3172 }
3173 }
3174
3175 cs_log_dump_dbg(D_DVBAPI, (unsigned char *)dest, len / 2, "QboxHD pmt.tmp:");
3176 pmt_id = dvbapi_parse_capmt((unsigned char *)dest + 4, (len / 2) - 4, -1, dp->d_name);
3177#else
3178 if(len > sizeof(dest))
3179 {
3180 cs_log_dbg(D_DVBAPI, "event_handler() dest buffer is to small for pmt data!");
3181 continue;
3182 }
3183 if(len < 16)
3184 {
3185 cs_log_dbg(D_DVBAPI, "event_handler() received pmt is too small! (%d < 16 bytes!)", len);
3186 continue;
3187 }
3188 cs_log_dump_dbg(D_DVBAPI, mbuf, len, "pmt:");
3189
3190 dest[0] = 0x03;
3191 dest[1] = mbuf[3];
3192 dest[2] = mbuf[4];
3193 uint32_t pmt_program_length = b2i(2, mbuf + 10)&0xFFF;
3194 i2b_buf(2, pmt_program_length + 1, (uchar *) dest + 4);
3195 dest[6] = 0;
3196
3197 memcpy(dest + 7, mbuf + 12, len - 12 - 4);
3198
3199 pmt_id = dvbapi_parse_capmt((uchar *)dest, 7 + len - 12 - 4, -1, dp->d_name);
3200#endif
3201
3202 if(pmt_id >= 0)
3203 {
3204 cs_strncpy(demux[pmt_id].pmt_file, dp->d_name, sizeof(demux[pmt_id].pmt_file));
3205 demux[pmt_id].pmt_time = (time_t)pmt_info.st_mtime;
3206 }
3207
3208 if(cfg.dvbapi_pmtmode == 3)
3209 {
3210 disable_pmt_files = 1;
3211 break;
3212 }
3213 }
3214 closedir(dirp);
3215 pthread_mutex_unlock(&event_handler_lock);
3216}
3217
3218void *dvbapi_event_thread(void *cli)
3219{
3220 struct s_client *client = (struct s_client *) cli;
3221 pthread_setspecific(getclient, client);
3222 set_thread_name(__func__);
3223 while(1)
3224 {
3225 cs_sleepms(750);
3226 event_handler(0);
3227 }
3228
3229 return NULL;
3230}
3231
3232void dvbapi_process_input(int32_t demux_id, int32_t filter_num, uchar *buffer, int32_t len)
3233{
3234 int32_t pid = demux[demux_id].demux_fd[filter_num].pidindex;
3235 struct s_ecmpids *curpid = NULL;
3236 if(pid != -1)
3237 {
3238 curpid = &demux[demux_id].ECMpids[pid];
3239 }
3240 uint32_t chid = 0x10000;
3241 uint32_t ecmlen = (b2i(2, buffer + 1)&0xFFF)+3;
3242 ECM_REQUEST *er;
3243
3244 if(demux[demux_id].demux_fd[filter_num].type == TYPE_ECM)
3245 {
3246 if(len != 0) // len = 0 receiver encountered an internal bufferoverflow!
3247 {
3248 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d fetched ECM data (ecmlength = %03X)", demux_id, filter_num + 1, ecmlen);
3249 if((uint) len < ecmlen) // invalid CAT length
3250 {
3251 cs_log_dbg(D_DVBAPI, "Received data with total length %03X but ECM length is %03X -> invalid CAT length!", len, ecmlen);
3252 return;
3253 }
3254
3255 if(!(buffer[0] == 0x80 || buffer[0] == 0x81))
3256 {
3257 cs_log_dbg(D_DVBAPI, "Received an ECM with invalid ecmtable ID %02X -> ignoring!", buffer[0]);
3258 return;
3259 }
3260
3261 if(curpid->table == buffer[0] && !caid_is_irdeto(curpid->CAID)) // wait for odd / even ecm change (only not for irdeto!)
3262 {
3263
3264 if(!(er = get_ecmtask()))
3265 {
3266 return;
3267 }
3268
3269 er->srvid = demux[demux_id].program_number;
3270
3271 er->tsid = demux[demux_id].tsid;
3272 er->onid = demux[demux_id].onid;
3273 er->pmtpid = demux[demux_id].pmtpid;
3274 er->ens = demux[demux_id].enigma_namespace;
3275
3276 er->caid = curpid->CAID;
3277 er->pid = curpid->ECM_PID;
3278 er->prid = curpid->PROVID;
3279 er->vpid = curpid->VPID;
3280 er->ecmlen = ecmlen;
3281 memcpy(er->ecm, buffer, er->ecmlen);
3282 chid = get_subid(er); // fetch chid or fake chid
3283 er->chid = chid;
3284 dvbapi_set_section_filter(demux_id, er, filter_num);
3285 NULLFREE(er);
3286 return;
3287 }
3288
3289 if(caid_is_irdeto(curpid->CAID))
3290 {
3291 // 80 70 39 53 04 05 00 88
3292 // 81 70 41 41 01 06 00 13 00 06 80 38 1F 52 93 D2
3293 //if (buffer[5]>20) return;
3294 if(curpid->irdeto_maxindex != buffer[5]) //6, register max irdeto index
3295 {
3296 cs_log_dbg(D_DVBAPI, "Found %d IRDETO ECM CHIDs", buffer[5] + 1);
3297 curpid->irdeto_maxindex = buffer[5]; // numchids = 7 (0..6)
3298 }
3299 }
3300 }
3301
3302 if(!(er = get_ecmtask()))
3303 {
3304 return;
3305 }
3306
3307 er->srvid = demux[demux_id].program_number;
3308
3309 er->tsid = demux[demux_id].tsid;
3310 er->onid = demux[demux_id].onid;
3311 er->pmtpid = demux[demux_id].pmtpid;
3312 er->ens = demux[demux_id].enigma_namespace;
3313
3314 er->caid = curpid->CAID;
3315 er->pid = curpid->ECM_PID;
3316 er->prid = curpid->PROVID;
3317 er->vpid = curpid->VPID;
3318 er->ecmlen = ecmlen;
3319 memcpy(er->ecm, buffer, er->ecmlen);
3320
3321 chid = get_subid(er); // fetch chid or fake chid
3322 er->chid = chid;
3323
3324 if(len == 0) // only used on receiver internal bufferoverflow to get quickly fresh ecm filterdata otherwise freezing!
3325 {
3326 curpid->table = 0;
3327 dvbapi_set_section_filter(demux_id, er, filter_num);
3328 NULLFREE(er);
3329 return;
3330 }
3331
3332 if(caid_is_irdeto(curpid->CAID))
3333 {
3334
3335 if(curpid->irdeto_curindex != buffer[4]) // old style wrong irdeto index
3336 {
3337 if(curpid->irdeto_curindex == 0xFE) // check if this ecmfilter just started up
3338 {
3339 curpid->irdeto_curindex = buffer[4]; // on startup set the current index to the irdeto index of the ecm
3340 }
3341 else // we are already running and not interested in this ecm
3342 {
3343 if(curpid->table != buffer[0]) curpid->table = 0; // fix for receivers not supporting section filtering
3344 dvbapi_set_section_filter(demux_id, er, filter_num); // set ecm filter to odd + even since this ecm doesnt match with current irdeto index
3345 NULLFREE(er);
3346 return;
3347 }
3348 }
3349 else //fix for receivers not supporting section filtering
3350 {
3351 if(curpid->table == buffer[0]){
3352 NULLFREE(er);
3353 return;
3354 }
3355 }
3356 cs_log_dbg(D_DVBAPI, "Demuxer %d ECMTYPE %02X CAID %04X PROVID %06X ECMPID %04X IRDETO INDEX %02X MAX INDEX %02X CHID %04X CYCLE %02X VPID %04X", demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->ecm[4], er->ecm[5], er->chid, curpid->irdeto_cycle, er->vpid);
3357 }
3358 else
3359 {
3360 cs_log_dbg(D_DVBAPI, "Demuxer %d ECMTYPE %02X CAID %04X PROVID %06X ECMPID %04X FAKECHID %04X (unique part in ecm)",
3361 demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->chid);
3362 }
3363
3364 // check for matching chid (unique ecm part in case of non-irdeto cas) + added fix for seca2 monthly changing fakechid
3365 if((curpid->CHID < 0x10000) && !((chid == curpid->CHID) || ((curpid->CAID >> 8 == 0x01) && (chid&0xF0FF) == (curpid->CHID&0xF0FF)) ) )
3366 {
3367 if(caid_is_irdeto(curpid->CAID))
3368 {
3369
3370 if((curpid->irdeto_cycle < 0xFE) && (curpid->irdeto_cycle == curpid->irdeto_curindex)) // if same: we cycled all indexes but no luck!
3371 {
3372 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
3373 if(!forceentry || !forceentry->force) // forced pid? keep trying the forced ecmpid, no force kill ecm filter
3374 {
3375 if(curpid->checked == 2) { curpid->checked = 4; }
3376 if(curpid->checked == 1)
3377 {
3378 curpid->checked = 2;
3379 curpid->CHID = 0x10000;
3380 }
3381 dvbapi_stop_filternum(demux_id, filter_num); // stop this ecm filter!
3382 NULLFREE(er);
3383 return;
3384 }
3385 }
3386
3387 curpid->irdeto_curindex++; // set check on next index
3388 if(curpid->irdeto_cycle == 0xFE) curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
3389 if(curpid->irdeto_curindex > curpid->irdeto_maxindex) { curpid->irdeto_curindex = 0; } // check if we reached max irdeto index, if so reset to 0
3390
3391 curpid->table = 0;
3392 dvbapi_set_section_filter(demux_id, er, filter_num); // set ecm filter to odd + even since this ecm doesnt match with current irdeto index
3393 NULLFREE(er);
3394 return;
3395 }
3396 else // all nonirdeto cas systems
3397 {
3398 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
3399 curpid->table = 0;
3400 dvbapi_set_section_filter(demux_id, er, filter_num); // set ecm filter to odd + even since this ecm doesnt match with current irdeto index
3401 if(forceentry && forceentry->force)
3402 {
3403 NULLFREE(er);
3404 return; // forced pid? keep trying the forced ecmpid!
3405 }
3406 if(curpid->checked == 2) { curpid->checked = 4; }
3407 if(curpid->checked == 1)
3408 {
3409 curpid->checked = 2;
3410 curpid->CHID = 0x10000;
3411 }
3412 dvbapi_stop_filternum(demux_id, filter_num); // stop this ecm filter!
3413 NULLFREE(er);
3414 return;
3415 }
3416 }
3417
3418 struct s_dvbapi_priority *p;
3419
3420 for(p = dvbapi_priority; p != NULL; p = p->next)
3421 {
3422 if(p->type != 'l'
3423 || (p->caid && p->caid != curpid->CAID)
3424 || (p->provid && p->provid != curpid->PROVID)
3425 || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
3426 || (p->srvid && p->srvid != demux[demux_id].program_number))
3427 { continue; }
3428
3429 if((uint)p->delay == ecmlen && p->force < 6)
3430 {
3431 p->force++;
3432 NULLFREE(er);
3433 return;
3434 }
3435 if(p->force >= 6)
3436 { p->force = 0; }
3437 }
3438
3439 if(!curpid->PROVID)
3440 { curpid->PROVID = chk_provid(buffer, curpid->CAID); }
3441
3442 if(caid_is_irdeto(curpid->CAID)) // irdeto: wait for the correct index
3443 {
3444 if(buffer[4] != curpid->irdeto_curindex)
3445 {
3446 curpid->table = 0;
3447 dvbapi_set_section_filter(demux_id, er, filter_num); // set ecm filter to odd + even since this ecm doesnt match with current irdeto index
3448 NULLFREE(er);
3449 return;
3450 }
3451 }
3452 // we have an ecm with the correct irdeto index (or fakechid)
3453 for(p = dvbapi_priority; p != NULL ; p = p->next) // check for ignore!
3454 {
3455 if((p->type != 'i')
3456 || (p->caid && p->caid != curpid->CAID)
3457 || (p->provid && p->provid != curpid->PROVID)
3458 || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
3459 || (p->pidx && p->pidx-1 != pid)
3460 || (p->srvid && p->srvid != demux[demux_id].program_number))
3461 { continue; }
3462
3463 if(p->type == 'i' && (p->chid < 0x10000 && p->chid == chid)) // found a ignore chid match with current ecm -> ignoring this irdeto index
3464 {
3465 curpid->irdeto_curindex++;
3466 if(curpid->irdeto_cycle == 0xFE) curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
3467 if(curpid->irdeto_curindex > curpid->irdeto_maxindex) // check if curindex is over the max
3468 {
3469 curpid->irdeto_curindex = 0;
3470 }
3471 curpid->table = 0;
3472 if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_cycle != curpid->irdeto_curindex)) // irdeto: wait for the correct index + check if we cycled all
3473 {
3474 dvbapi_set_section_filter(demux_id, er, filter_num); // set ecm filter to odd + even since this chid has to be ignored!
3475 }
3476 else // this fakechid has to be ignored, kill this filter!
3477 {
3478 if(curpid->checked == 2) { curpid->checked = 4; }
3479 if(curpid->checked == 1)
3480 {
3481 curpid->checked = 2;
3482 curpid->CHID = 0x10000;
3483 }
3484 dvbapi_stop_filternum(demux_id, filter_num); // stop this ecm filter!
3485 }
3486 NULLFREE(er);
3487 return;
3488 }
3489 }
3490
3491 if(er)
3492 {
3493 curpid->table = er->ecm[0];
3494 }
3495
3496 request_cw(dvbapi_client, er, demux_id, 1); // register this ecm for delayed ecm response check
3497 return; // end of ecm filterhandling!
3498 }
3499
3500 if(demux[demux_id].demux_fd[filter_num].type == TYPE_EMM && len != 0) // len = 0 receiver encountered an internal bufferoverflow!
3501 {
3502 if(demux[demux_id].demux_fd[filter_num].pid == 0x01) // CAT
3503 {
3504 cs_log_dbg(D_DVBAPI, "receiving cat");
3505 dvbapi_parse_cat(demux_id, buffer, len);
3506
3507 dvbapi_stop_filternum(demux_id, filter_num);
3508 return;
3509 }
3510 dvbapi_process_emm(demux_id, filter_num, buffer, len);
3511 }
3512}
3513
3514static void *dvbapi_main_local(void *cli)
3515{
3516 int32_t i, j;
3517 struct s_client *client = (struct s_client *) cli;
3518 client->thread = pthread_self();
3519 pthread_setspecific(getclient, cli);
3520
3521 dvbapi_client = cli;
3522
3523 int32_t maxpfdsize = (MAX_DEMUX * maxfilter) + MAX_DEMUX + 2;
3524 struct pollfd pfd2[maxpfdsize];
3525 struct timeb start, end; // start time poll, end time poll
3526#define PMT_SERVER_SOCKET "/tmp/.listen.camd.socket"
3527 struct sockaddr_un saddr;
3528 saddr.sun_family = AF_UNIX;
3529 strncpy(saddr.sun_path, PMT_SERVER_SOCKET, 107);
3530 saddr.sun_path[107] = '\0';
3531
3532 int32_t rc, pfdcount, g, connfd, clilen;
3533 int32_t ids[maxpfdsize], fdn[maxpfdsize], type[maxpfdsize];
3534 struct SOCKADDR servaddr;
3535 ssize_t len = 0;
3536 uchar mbuf[1024];
3537
3538 struct s_auth *account;
3539 int32_t ok = 0;
3540 for(account = cfg.account; account != NULL; account = account->next)
3541 {
3542 if((ok = is_dvbapi_usr(account->usr)))
3543 { break; }
3544 }
3545 cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
3546
3547 memset(demux, 0, sizeof(struct demux_s) * MAX_DEMUX);
3548 memset(ca_fd, 0, sizeof(ca_fd));
3549 memset(unassoc_fd, 0, sizeof(unassoc_fd));
3550
3551 dvbapi_read_priority();
3552 dvbapi_load_channel_cache();
3553 dvbapi_detect_api();
3554
3555 if(selected_box == -1 || selected_api == -1)
3556 {
3557 cs_log("ERROR: Could not detect DVBAPI version.");
3558 return NULL;
3559 }
3560
3561 if(cfg.dvbapi_pmtmode == 1)
3562 { disable_pmt_files = 1; }
3563
3564 int32_t listenfd = -1;
3565 if(cfg.dvbapi_boxtype != BOXTYPE_IPBOX_PMT && cfg.dvbapi_pmtmode != 2 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
3566 {
3567 if (!cfg.dvbapi_listenport)
3568 listenfd = dvbapi_init_listenfd();
3569 else
3570 listenfd = dvbapi_net_init_listenfd();
3571 if(listenfd < 1)
3572 {
3573 cs_log("ERROR: Could not init socket: (errno=%d: %s)", errno, strerror(errno));
3574 return NULL;
3575 }
3576 }
3577
3578 pthread_mutex_init(&event_handler_lock, NULL);
3579
3580 for(i = 0; i < MAX_DEMUX; i++) // init all demuxers!
3581 {
3582 demux[i].pidindex = -1;
3583 demux[i].curindex = -1;
3584 }
3585
3586 if(cfg.dvbapi_pmtmode != 4 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
3587 {
3588 struct sigaction signal_action;
3589 signal_action.sa_handler = event_handler;
3590 sigemptyset(&signal_action.sa_mask);
3591 signal_action.sa_flags = SA_RESTART;
3592 sigaction(SIGRTMIN + 1, &signal_action, NULL);
3593
3594 dir_fd = open(TMPDIR, O_RDONLY);
3595 if(dir_fd >= 0)
3596 {
3597 fcntl(dir_fd, F_SETSIG, SIGRTMIN + 1);
3598 fcntl(dir_fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_DELETE | DN_MULTISHOT);
3599 event_handler(SIGRTMIN + 1);
3600 }
3601 }
3602 else
3603 {
3604 pthread_t event_thread;
3605 int32_t ret = pthread_create(&event_thread, NULL, dvbapi_event_thread, (void *) dvbapi_client);
3606 if(ret)
3607 {
3608 cs_log("ERROR: Can't create dvbapi event thread (errno=%d %s)", ret, strerror(ret));
3609 return NULL;
3610 }
3611 else
3612 { pthread_detach(event_thread); }
3613 }
3614
3615 if(listenfd != -1)
3616 {
3617 pfd2[0].fd = listenfd;
3618 pfd2[0].events = (POLLIN | POLLPRI);
3619 type[0] = 1;
3620 }
3621
3622#ifdef WITH_COOLAPI
3623 system("pzapit -rz");
3624#endif
3625 cs_ftime(&start); // register start time
3626 while(1)
3627 {
3628 if(pausecam) // for dbox2, STAPI or PC in standby mode dont parse any ecm/emm or try to start next filter
3629 { continue; }
3630
3631 if(cfg.dvbapi_pmtmode == 6)
3632 {
3633 if(listenfd < 0)
3634 {
3635 cs_log("PMT6: Trying connect to enigma CA PMT listen socket...");
3636 /* socket init */
3637 if((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
3638 {
3639
3640 cs_log("socket error (errno=%d %s)", errno, strerror(errno));
3641 listenfd = -1;
3642 }
3643 else if(connect(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
3644 {
3645 cs_log("socket connect error (errno=%d %s)", errno, strerror(errno));
3646 close(listenfd);
3647 listenfd = -1;
3648 }
3649 else
3650 {
3651 pfd2[0].fd = listenfd;
3652 pfd2[0].events = (POLLIN | POLLPRI);
3653 type[0] = 1;
3654 cs_log("PMT6 CA PMT Server connected on fd %d!", listenfd);
3655 }
3656 }
3657
3658 }
3659 pfdcount = (listenfd > -1) ? 1 : 0;
3660
3661 for(i = 0; i < MAX_DEMUX; i++)
3662 {
3663 // add client fd's which are not yet associated with the demux but needs to be polled for data
3664 if (unassoc_fd[i]) {
3665 pfd2[pfdcount].fd = unassoc_fd[i];
3666 pfd2[pfdcount].events = (POLLIN | POLLPRI);
3667 type[pfdcount++] = 1;
3668 }
3669
3670 if(demux[i].program_number == 0) { continue; } // only evalutate demuxers that have channels assigned
3671
3672 uint32_t ecmcounter = 0, emmcounter = 0;
3673 for(g = 0; g < maxfilter; g++)
3674 {
3675 if(demux[i].demux_fd[g].fd <= 0) continue; // deny obvious invalid fd!
3676
3677 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX && selected_api != STAPI && selected_api != COOLAPI)
3678 {
3679 pfd2[pfdcount].fd = demux[i].demux_fd[g].fd;
3680 pfd2[pfdcount].events = (POLLIN | POLLPRI);
3681 ids[pfdcount] = i;
3682 fdn[pfdcount] = g;
3683 type[pfdcount++] = 0;
3684 }
3685 if(demux[i].demux_fd[g].type == TYPE_ECM) { ecmcounter++; } // count ecm filters to see if demuxing is possible anyway
3686 if(demux[i].demux_fd[g].type == TYPE_EMM) { emmcounter++; } // count emm filters also
3687 }
3688 if(ecmcounter != demux[i].old_ecmfiltercount || emmcounter != demux[i].old_emmfiltercount) // only produce log if something changed
3689 {
3690 cs_log_dbg(D_DVBAPI, "Demuxer %d has %d ecmpids, %d streampids, %d ecmfilters and %d of max %d emmfilters", i, demux[i].ECMpidcount,
3691 demux[i].STREAMpidcount, ecmcounter, emmcounter, demux[i].max_emm_filter);
3692 demux[i].old_ecmfiltercount = ecmcounter; // save new amount of ecmfilters
3693 demux[i].old_emmfiltercount = emmcounter; // save new amount of emmfilters
3694 }
3695
3696 // delayed emm start for non irdeto caids, start emm cat if not already done for this demuxer!
3697
3698 struct timeb now;
3699 cs_ftime(&now);
3700
3701 if(cfg.dvbapi_au > 0 && demux[i].emm_filter == -1 && demux[i].EMMpidcount == 0 && emmcounter == 0)
3702 {
3703 int64_t gone = comp_timeb(&now, &demux[i].emmstart);
3704 if(gone > 30*1000){
3705 cs_ftime(&demux[i].emmstart); // trick to let emm fetching start after 30 seconds to speed up zapping
3706 dvbapi_start_filter(i, demux[i].pidindex, 0x001, 0x001, 0x01, 0x01, 0xFF, 0, TYPE_EMM); //CAT
3707 }
3708 }
3709
3710 //early start for irdeto since they need emm before ecm (pmt emmstart = 1 if detected caid 0x06)
3711 int32_t emmstarted = demux[i].emm_filter;
3712 if(cfg.dvbapi_au && demux[i].EMMpidcount > 0) // check every time since share readers might give us new filters due to hexserial change
3713 {
3714 if(!emmcounter && emmstarted == -1)
3715 {
3716 demux[i].emmstart = now;
3717 dvbapi_start_emm_filter(i); // start emmfiltering if emmpids are found
3718 }
3719 else
3720 {
3721 int64_t gone = comp_timeb(&now, &demux[i].emmstart);
3722 if(gone > 30*1000)
3723 {
3724 demux[i].emmstart = now;
3725 dvbapi_start_emm_filter(i); // start emmfiltering delayed if filters already were running
3726 rotate_emmfilter(i); // rotate active emmfilters
3727 }
3728 }
3729 }
3730
3731 if(ecmcounter == 0 && demux[i].ECMpidcount > 0) // Restart decoding all caids we have ecmpids but no ecm filters!
3732 {
3733
3734 int32_t started = 0;
3735
3736 for(g = 0; g < demux[i].ECMpidcount; g++) // avoid race: not all pids are asked and checked out yet!
3737 {
3738 if(demux[i].ECMpids[g].checked == 0 && demux[i].ECMpids[g].status >= 0) // check if prio run is done
3739 {
3740 dvbapi_try_next_caid(i, 0); // not done, so start next prio pid
3741 started = 1;
3742 break;
3743 }
3744 }
3745 if(started) { continue; } // if started a filter proceed with next demuxer
3746
3747 if(g == demux[i].ECMpidcount) // all usable pids (with prio) are tried, lets start over again without prio!
3748 {
3749 for(g = 0; g < demux[i].ECMpidcount; g++) // avoid race: not all pids are asked and checked out yet!
3750 {
3751 if(demux[i].ECMpids[g].checked == 2 && demux[i].ECMpids[g].status >= 0) // check if noprio run is done
3752 {
3753 demux[i].ECMpids[g].irdeto_curindex = 0xFE;
3754 demux[i].ECMpids[g].irdeto_maxindex = 0;
3755 demux[i].ECMpids[g].irdeto_cycle = 0xFE;
3756 demux[i].ECMpids[g].tries = 0xFE;
3757 demux[i].ECMpids[g].table = 0;
3758 demux[i].ECMpids[g].CHID = 0x10000; // remove chid prio
3759 dvbapi_try_next_caid(i, 2); // not done, so start next no prio pid
3760 started = 1;
3761 break;
3762 }
3763 }
3764 }
3765 if(started) { continue; } // if started a filter proceed with next demuxer
3766
3767 if(g == demux[i].ECMpidcount) // all usable pids are tried, lets start over again!
3768 {
3769 if(demux[i].decodingtries == -1) // first redecoding attempt?
3770 {
3771 cs_ftime(&demux[i].decstart);
3772 for(g = 0; g < demux[i].ECMpidcount; g++) // reinit some used things from second run (without prio)
3773 {
3774 demux[i].ECMpids[g].checked = 0;
3775 demux[i].ECMpids[g].irdeto_curindex = 0xFE;
3776 demux[i].ECMpids[g].irdeto_maxindex = 0;
3777 demux[i].ECMpids[g].irdeto_cycle = 0xFE;
3778 demux[i].ECMpids[g].table = 0;
3779 demux[i].decodingtries = 0;
3780 dvbapi_edit_channel_cache(i, g, 0); // remove this pid from channelcache since we had no founds on any ecmpid!
3781 }
3782 }
3783 uint8_t number_of_enabled_pids = 0;
3784 demux[i].decodingtries++;
3785 dvbapi_resort_ecmpids(i);
3786
3787 for(g = 0; g < demux[i].ECMpidcount; g++) // count number of enabled pids!
3788 {
3789 if(demux[i].ECMpids[g].status >= 0) number_of_enabled_pids++;
3790 }
3791 if(!number_of_enabled_pids)
3792 {
3793 if(demux[i].decodingtries == 10)
3794 {
3795 demux[i].decodingtries = 0;
3796 cs_log("Demuxer %d no enabled matching ecmpids -> decoding is waiting for matching readers!",i);
3797 }
3798 }
3799 else
3800 {
3801 cs_ftime(&demux[i].decend);
3802 demux[i].decodingtries = -1; // reset to first run again!
3803 int64_t gone = comp_timeb(&demux[i].decend, &demux[i].decstart);
3804 cs_log("Demuxer %d restarting decodingrequests after %"PRId64" ms with %d enabled and %d disabled ecmpids!", i, gone, number_of_enabled_pids,
3805 (demux[i].ECMpidcount-number_of_enabled_pids));
3806 dvbapi_try_next_caid(i, 0);
3807 }
3808 }
3809 }
3810
3811 if(demux[i].socket_fd > 0 && cfg.dvbapi_pmtmode != 6)
3812 {
3813 rc = 0;
3814 for(j = 0; j < pfdcount; j++)
3815 {
3816 if(pfd2[j].fd == demux[i].socket_fd)
3817 {
3818 rc = 1;
3819 break;
3820 }
3821 }
3822 if(rc == 1) { continue; }
3823
3824 pfd2[pfdcount].fd = demux[i].socket_fd;
3825 pfd2[pfdcount].events = (POLLIN | POLLPRI);
3826 ids[pfdcount] = i;
3827 type[pfdcount++] = 1;
3828 }
3829 }
3830
3831 while(1)
3832 {
3833 rc = poll(pfd2, pfdcount, 300);
3834 if(listenfd == -1 && cfg.dvbapi_pmtmode == 6) { break; }
3835 if(rc < 0)
3836 { continue; }
3837 break;
3838 }
3839
3840 if(rc > 0)
3841 {
3842 cs_ftime(&end); // register end time
3843 int64_t timeout = comp_timeb(&end, &start);
3844 if (timeout < 0) {
3845 cs_log("*** WARNING: BAD TIME AFFECTING WHOLE OSCAM ECM HANDLING ****");
3846 }
3847 cs_log_dbg(D_TRACE, "New events occurred on %d of %d handlers after %"PRId64" ms inactivity", rc, pfdcount, timeout);
3848 cs_ftime(&start); // register new start time for next poll
3849 }
3850
3851 for(i = 0; i < pfdcount && rc > 0; i++)
3852 {
3853 if(pfd2[i].revents == 0) { continue; } // skip sockets with no changes
3854 rc--; //event handled!
3855 cs_log_dbg(D_TRACE, "Now handling fd %d that reported event %d", pfd2[i].fd, pfd2[i].revents);
3856
3857 if(pfd2[i].revents & (POLLHUP | POLLNVAL | POLLERR))
3858 {
3859 if(type[i] == 1)
3860 {
3861 for(j = 0; j < MAX_DEMUX; j++)
3862 {
3863 if(demux[j].socket_fd == pfd2[i].fd) // if listenfd closes stop all assigned decoding!
3864 {
3865 dvbapi_stop_descrambling(j);
3866 }
3867 }
3868 int32_t ret = close(pfd2[i].fd);
3869 if(ret < 0 && errno != 9) { cs_log("ERROR: Could not close demuxer socket fd (errno=%d %s)", errno, strerror(errno)); }
3870 if(pfd2[i].fd == listenfd && cfg.dvbapi_pmtmode == 6)
3871 {
3872 listenfd = -1;
3873 }
3874 }
3875 else // type = 0
3876 {
3877 int32_t demux_index = ids[i];
3878 int32_t n = fdn[i];
3879 dvbapi_stop_filternum(demux_index, n); // stop filter since its giving errors and wont return anything good.
3880 }
3881 continue; // continue with other events
3882 }
3883
3884 if(pfd2[i].revents & (POLLIN | POLLPRI))
3885 {
3886 if(type[i] == 1 && pmthandling == 0)
3887 {
3888 pmthandling = 1; // pmthandling in progress!
3889 if(cfg.dvbapi_pmtmode == 6)
3890 {
3891 pmt_stopmarking = 0; // to stop_descrambling marking in PMT 6 mode
3892 }
3893
3894 connfd = -1; // initially no socket to read from
3895 int add_to_poll = 0; // we may need to additionally poll this socket when no PMT data comes in
3896
3897 if (pfd2[i].fd == listenfd)
3898 {
3899 if (cfg.dvbapi_pmtmode == 6) {
3900 connfd = listenfd;
3901 disable_pmt_files = 1;
3902 } else {
3903 clilen = sizeof(servaddr);
3904 connfd = accept(listenfd, (struct sockaddr *)&servaddr, (socklen_t *)&clilen);
3905 cs_log_dbg(D_DVBAPI, "new socket connection fd: %d", connfd);
3906 if (cfg.dvbapi_listenport)
3907 {
3908 //update webif data
3909 client->ip = SIN_GET_ADDR(servaddr);
3910 client->port = ntohs(SIN_GET_PORT(servaddr));
3911 }
3912 add_to_poll = 1;
3913
3914 if(cfg.dvbapi_pmtmode == 3 || cfg.dvbapi_pmtmode == 0) { disable_pmt_files = 1; }
3915
3916 if(connfd <= 0)
3917 cs_log_dbg(D_DVBAPI, "accept() returns error on fd event %d (errno=%d %s)", pfd2[i].revents, errno, strerror(errno));
3918 }
3919 }
3920 else
3921 {
3922 cs_log_dbg(D_DVBAPI, "PMT Update on socket %d.", pfd2[i].fd);
3923 connfd = pfd2[i].fd;
3924 }
3925
3926 //reading and completing data from socket
3927 if (connfd > 0) {
3928 uint32_t pmtlen = 0, chunks_processed = 0;
3929
3930 int tries = 100;
3931 do {
3932 len = recv(connfd, mbuf + pmtlen, sizeof(mbuf) - pmtlen, MSG_DONTWAIT);
3933 if (len > 0)
3934 pmtlen += len;
3935 if ((cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX) &&
3936 (len == 0 || (len == -1 && (errno != EINTR && errno != EAGAIN))))
3937 {
3938 //client disconnects, stop all assigned decoding
3939 cs_log_dbg(D_DVBAPI, "Socket %d reported connection close", connfd);
3940 int active_conn = 0; //other active connections counter
3941 for (j = 0; j < MAX_DEMUX; j++)
3942 {
3943 if (demux[j].socket_fd == connfd)
3944 dvbapi_stop_descrambling(j);
3945 else if (demux[j].socket_fd)
3946 active_conn++;
3947 // remove from unassoc_fd when necessary
3948 if (unassoc_fd[j] == connfd)
3949 unassoc_fd[j] = 0;
3950 }
3951 close(connfd);
3952 connfd = -1;
3953 add_to_poll = 0;
3954 if (!active_conn) //last connection closed
3955 {
3956 client_proto_version = 0;
3957 if (client_name)
3958 {
3959 free(client_name);
3960 client_name = NULL;
3961 }
3962 if (cfg.dvbapi_listenport)
3963 {
3964 //update webif data
3965 client->ip = get_null_ip();
3966 client->port = 0;
3967 }
3968 }
3969 break;
3970 }
3971 if (pmtlen >= 8) //if we received less then 8 bytes, than it's not complete for sure
3972 {
3973 // check and try to process complete PMT objects and filter data by chunks to avoid PMT buffer overflows
3974 uint32_t opcode_ptr;
3975 memcpy(&opcode_ptr, &mbuf[0], 4); //used only to silent compiler warning about dereferencing type-punned pointer
3976 uint32_t opcode = ntohl(opcode_ptr); //get the client opcode (4 bytes)
3977 uint32_t chunksize = 0; //size of complete chunk in the buffer (an opcode with the data)
3978 uint32_t data_len = 0; //variable for internal data length (eg. for the filter data size, PMT len)
3979
3980 //detect the opcode, its size (chunksize) and its internal data size (data_len)
3981 if ((opcode & 0xFFFFF000) == DVBAPI_AOT_CA)
3982 {
3983 // parse packet size (ASN.1)
3984 uint32_t size = 0;
3985 if (mbuf[3] & 0x80)
3986 {
3987 data_len = 0;
3988 size = mbuf[3] & 0x7F;
3989 if (pmtlen > 4 + size)
3990 {
3991 uint32_t k;
3992 for (k = 0; k < size; k++)
3993 data_len = (data_len << 8) | mbuf[3 + 1 + k];
3994 size++;
3995 }
3996 }
3997 else
3998 {
3999 data_len = mbuf[3] & 0x7F;
4000 size = 1;
4001 }
4002 chunksize = 3 + size + data_len;
4003 }
4004 else switch (opcode)
4005 {
4006 case DVBAPI_FILTER_DATA:
4007 {
4008 data_len = b2i(2, mbuf + 7) & 0x0FFF;
4009 chunksize = 6 + 3 + data_len;
4010 break;
4011 }
4012 case DVBAPI_CLIENT_INFO:
4013 {
4014 data_len = mbuf[6];
4015 chunksize = 6 + 1 + data_len;
4016 break;
4017 }
4018 default:
4019 cs_log("Unknown socket command received: 0x%08X", opcode);
4020 }
4021
4022 //processing the complete data according to type
4023 if (chunksize < sizeof(mbuf) && chunksize <= pmtlen) // only handle if we fetched a complete chunksize!
4024 {
4025 chunks_processed++;
4026 if ((opcode & 0xFFFFF000) == DVBAPI_AOT_CA)
4027 {
4028 cs_log_dump_dbg(D_DVBAPI, mbuf, chunksize, "Parsing %d PMT object(s):", chunks_processed);
4029 dvbapi_handlesockmsg(mbuf, chunksize, connfd);
4030 add_to_poll = 0;
4031 if (cfg.dvbapi_listenport && opcode == DVBAPI_AOT_CA_STOP)
4032 add_to_poll = 1;
4033 }
4034 else switch (opcode)
4035 {
4036 case DVBAPI_FILTER_DATA:
4037 {
4038 int32_t demux_index = mbuf[4];
4039 int32_t filter_num = mbuf[5];
4040 dvbapi_process_input(demux_index, filter_num, mbuf + 6, data_len + 3);
4041 break;
4042 }
4043 case DVBAPI_CLIENT_INFO:
4044 {
4045 uint16_t client_proto_ptr;
4046 memcpy(&client_proto_ptr, &mbuf[4], 2);
4047 uint16_t client_proto = ntohs(client_proto_ptr);
4048 if (client_name)
4049 free(client_name);
4050 if (cs_malloc(&client_name, data_len + 1))
4051 {
4052 memcpy(client_name, &mbuf[7], data_len);
4053 client_name[data_len] = 0;
4054 cs_log("Client connected: '%s' (protocol version = %d)", client_name, client_proto);
4055 }
4056 client_proto_version = client_proto; //setting the global var according to the client
4057
4058 // as a response we are sending our info to the client:
4059 dvbapi_net_send(DVBAPI_SERVER_INFO, connfd, -1, -1, NULL);
4060 break;
4061 }
4062 }
4063
4064 if (pmtlen == chunksize) // if we fetched and handled the exact chunksize reset buffer counter!
4065 pmtlen = 0;
4066
4067 // if we read more data then processed, move it to beginning
4068 if (pmtlen > chunksize)
4069 {
4070 memmove(mbuf, mbuf + chunksize, pmtlen - chunksize);
4071 pmtlen -= chunksize;
4072 }
4073 continue;
4074 }
4075 }
4076 if (len <= 0) {
4077 if (pmtlen > 0 || chunks_processed > 0) //all data read
4078 break;
4079 else { //wait for data become available and try again
4080
4081 // remove from unassoc_fd if the socket fd is invalid
4082 if (errno == EBADF)
4083 for (j = 0; j < MAX_DEMUX; j++)
4084 if (unassoc_fd[j] == connfd)
4085 unassoc_fd[j] = 0;
4086 cs_sleepms(20);
4087 continue;
4088 }
4089 }
4090 } while (pmtlen < sizeof(mbuf) && tries--);
4091
4092 // if the connection is new and we read no PMT data, then add it to the poll,
4093 // otherwise this socket will not be checked with poll when data arives
4094 // because fd it is not yet assigned with the demux
4095 if (add_to_poll) {
4096 for (j = 0; j < MAX_DEMUX; j++) {
4097 if (!unassoc_fd[j]) {
4098 unassoc_fd[j] = connfd;
4099 break;
4100 }
4101 }
4102 }
4103
4104 if (pmtlen > 0) {
4105 if (pmtlen < 3)
4106 cs_log_dbg(D_DVBAPI, "CA PMT server message too short!");
4107 else {
4108 if (pmtlen >= sizeof(mbuf))
4109 cs_log("***** WARNING: PMT BUFFER OVERFLOW, PLEASE REPORT! ****** ");
4110 cs_log_dump_dbg(D_DVBAPI, mbuf, pmtlen, "New PMT info from socket (total size: %d)", pmtlen);
4111 dvbapi_handlesockmsg(mbuf, pmtlen, connfd);
4112 }
4113 }
4114 }
4115 pmthandling = 0; // pmthandling done!
4116 continue; // continue with other events!
4117 }
4118 else // type==0
4119 {
4120 int32_t demux_index = ids[i];
4121 int32_t n = fdn[i];
4122
4123 if((int)demux[demux_index].demux_fd[n].fd != pfd2[i].fd) { continue; } // filter already killed, no need to process this data!
4124
4125 len = dvbapi_read_device(pfd2[i].fd, mbuf, sizeof(mbuf));
4126 if(len < 0) // serious filterdata read error
4127 {
4128 dvbapi_stop_filternum(demux_index, n); // stop filter since its giving errors and wont return anything good.
4129 maxfilter--; // lower maxfilters to avoid this with new filter setups!
4130 continue;
4131 }
4132 if(!len) // receiver internal filterbuffer overflow
4133 {
4134 memset(mbuf, 0, sizeof(mbuf));
4135 }
4136
4137 dvbapi_process_input(demux_index, n, mbuf, len);
4138 }
4139 continue; // continue with other events!
4140 }
4141 }
4142 }
4143 return NULL;
4144}
4145
4146void dvbapi_write_cw(int32_t demux_id, uchar *cw, int32_t pid)
4147{
4148 int32_t n;
4149 int8_t cwEmpty = 0;
4150 unsigned char nullcw[8];
4151 memset(nullcw, 0, 8);
4152 ca_descr_t ca_descr;
4153
4154 memset(&ca_descr, 0, sizeof(ca_descr));
4155
4156 if(memcmp(demux[demux_id].lastcw[0], nullcw, 8) == 0
4157 && memcmp(demux[demux_id].lastcw[1], nullcw, 8) == 0)
4158 { cwEmpty = 1; } // to make sure that both cws get written on constantcw
4159
4160
4161 for(n = 0; n < 2; n++)
4162 {
4163 char lastcw[9 * 3];
4164 char newcw[9 * 3];
4165 cs_hexdump(0, demux[demux_id].lastcw[n], 8, lastcw, sizeof(lastcw));
4166 cs_hexdump(0, cw + (n * 8), 8, newcw, sizeof(newcw));
4167
4168 if((memcmp(cw + (n * 8), demux[demux_id].lastcw[n], 8) != 0 || cwEmpty)
4169 && memcmp(cw + (n * 8), nullcw, 8) != 0) // check if already delivered and new cw part is valid!
4170 {
4171 int32_t idx = dvbapi_ca_setpid(demux_id, pid); // prepare ca
4172 if (idx == -1) return; // return on no index!
4173
4174#ifdef WITH_COOLAPI
4175 ca_descr.index = idx;
4176 ca_descr.parity = n;
4177 memcpy(demux[demux_id].lastcw[n], cw + (n * 8), 8);
4178 memcpy(ca_descr.cw, cw + (n * 8), 8);
4179 cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca_mask %d)", demux_id, n, ca_descr.index, demux[demux_id].ca_mask);
4180 coolapi_write_cw(demux[demux_id].ca_mask, demux[demux_id].STREAMpids, demux[demux_id].STREAMpidcount, &ca_descr);
4181#else
4182 int32_t i,j, write_cw = 0;
4183 for(i = 0; i < MAX_DEMUX; i++)
4184 {
4185 if(!(demux[demux_id].ca_mask & (1 << i))) continue; // ca not in use by this demuxer!
4186
4187 for(j = 0; j < demux[demux_id].STREAMpidcount; j++)
4188 {
4189 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << j)) == (uint) (1 << j)))
4190 {
4191 int32_t usedidx = is_ca_used(i, demux[demux_id].STREAMpids[j]);
4192 if(idx != usedidx)
4193 {
4194 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip!", demux_id, i, usedidx, demux[demux_id].STREAMpids[j]);
4195 continue; // if not used for descrambling -> skip!
4196 }
4197 else
4198 {
4199 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> write!", demux_id, i, usedidx, demux[demux_id].STREAMpids[j]);
4200 write_cw = 1;
4201 }
4202 }
4203 }
4204 if(!write_cw) { continue; } // no need to write the cw since this ca isnt using it!
4205
4206 ca_descr.index = idx;
4207 ca_descr.parity = n;
4208 memcpy(demux[demux_id].lastcw[n], cw + (n * 8), 8);
4209 memcpy(ca_descr.cw, cw + (n * 8), 8);
4210 cs_log_dbg(D_DVBAPI, "Demuxer %d writing %s part (%s) of controlword, replacing expired (%s)", demux_id, (n == 1 ? "even" : "odd"), newcw, lastcw);
4211 cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca%d)", demux_id, n, ca_descr.index, i);
4212
4213 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
4214 dvbapi_net_send(DVBAPI_CA_SET_DESCR, demux[demux_id].socket_fd, demux_id, -1 /*unused*/, (unsigned char *) &ca_descr);
4215 else
4216 {
4217 if(ca_fd[i] <= 0)
4218 {
4219 ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
4220 if(ca_fd[i] <= 0) { continue; }
4221 }
4222 if (dvbapi_ioctl(ca_fd[i], CA_SET_DESCR, &ca_descr) < 0)
4223 {
4224 cs_log("ERROR: ioctl(CA_SET_DESCR): %s", strerror(errno));
4225 }
4226 }
4227 }
4228#endif
4229 }
4230 }
4231}
4232
4233void delayer(ECM_REQUEST *er)
4234{
4235 if(cfg.dvbapi_delayer <= 0) { return; }
4236
4237 struct timeb tpe;
4238 cs_ftime(&tpe);
4239 int64_t gone = comp_timeb(&tpe, &er->tps);
4240 if( gone < cfg.dvbapi_delayer)
4241 {
4242 cs_log_dbg(D_DVBAPI, "delayer: gone=%"PRId64" ms, cfg=%d ms -> delay=%"PRId64" ms", gone, cfg.dvbapi_delayer, cfg.dvbapi_delayer - gone);
4243 cs_sleepms(cfg.dvbapi_delayer - gone);
4244 }
4245}
4246
4247void dvbapi_send_dcw(struct s_client *client, ECM_REQUEST *er)
4248{
4249 int32_t i, j, handled = 0;
4250
4251 for(i = 0; i < MAX_DEMUX; i++)
4252 {
4253 uint32_t nocw_write = 0; // 0 = write cw, 1 = dont write cw to hardware demuxer
4254 if(demux[i].program_number == 0) { continue; } // ignore empty demuxers
4255 if(demux[i].program_number != er->srvid) { continue; } // skip ecm response for other srvid
4256 demux[i].rdr = er->selected_reader;
4257 for(j = 0; j < demux[i].ECMpidcount; j++) // check for matching ecmpid
4258 {
4259 if((demux[i].ECMpids[j].CAID == er->caid || demux[i].ECMpids[j].CAID == er->ocaid)
4260 && demux[i].ECMpids[j].ECM_PID == er->pid
4261 && demux[i].ECMpids[j].PROVID == er->prid
4262 && demux[i].ECMpids[j].VPID == er->vpid)
4263 { break; }
4264 }
4265 if(j == demux[i].ECMpidcount) { continue; } // ecm response srvid ok but no matching ecmpid, perhaps this for other demuxer
4266
4267 cs_log_dbg(D_DVBAPI, "Demuxer %d %scontrolword received for PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X", i,
4268 (er->rc >= E_NOTFOUND ? "no " : ""), j, er->caid, er->prid, er->pid, er->chid, er->vpid);
4269
4270 uint32_t status = dvbapi_check_ecm_delayed_delivery(i, er);
4271
4272 uint32_t comparecw0 = 0, comparecw1 = 0;
4273 char ecmd5[17 * 3];
4274 cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
4275
4276 if(status == 1 && er->rc) // wrong ecmhash
4277 {
4278 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (requested different one)", i, ecmd5);
4279 continue;
4280 }
4281 if(status == 2) // no filter
4282 {
4283 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (filter already killed)", i, ecmd5);
4284 continue;
4285 }
4286 if(status == 5) // empty cw
4287 {
4288 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (delivered cw is empty!)", i, ecmd5);
4289 nocw_write = 1;
4290 if(er->rc < E_NOTFOUND) { er->rc = E_NOTFOUND; }
4291 }
4292
4293 if((status == 0 || status == 3 || status == 4) && er->rc < E_NOTFOUND) // 0=matching ecm hash, 2=no filter, 3=table reset, 4=cache-ex response
4294 {
4295 if(memcmp(er->cw, demux[i].lastcw[0], 8) == 0 && memcmp(er->cw + 8, demux[i].lastcw[1], 8) == 0) // check for matching controlword
4296 {
4297 comparecw0 = 1;
4298 }
4299 else if(memcmp(er->cw, demux[i].lastcw[1], 8) == 0 && memcmp(er->cw + 8, demux[i].lastcw[0], 8) == 0) // check for matching controlword
4300 {
4301 comparecw1 = 1;
4302 }
4303 if(comparecw0 == 1 || comparecw1 == 1)
4304 {
4305 cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate controlword ecm response hash %s (duplicate controlword!)", i, ecmd5);
4306 nocw_write = 1;
4307 }
4308 }
4309
4310 if(status == 3) // table reset
4311 {
4312 cs_log_dbg(D_DVBAPI, "Demuxer %d luckyshot new controlword ecm response hash %s (ecm table reset)", i, ecmd5);
4313 }
4314
4315 if(status == 4) // no check on cache-ex responses!
4316 {
4317 cs_log_dbg(D_DVBAPI, "Demuxer %d new controlword from cache-ex reader (no ecmhash check possible)", i);
4318 }
4319
4320 handled = 1; // mark this ecm response as handled
4321 if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 0 && (demux[i].pidindex == -1) && er->caid != 0)
4322 {
4323 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
4324 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
4325 demux[i].pidindex = j; // set current index as *the* pid to descramble
4326 demux[i].ECMpids[j].checked = 4;
4327 cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
4328 i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
4329 }
4330
4331 if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 1 && er->caid != 0) // FOUND
4332 {
4333 pthread_mutex_lock(&demux[i].answerlock); // only process one ecm answer
4334 if(demux[i].ECMpids[j].checked != 4)
4335 {
4336
4337 int32_t t, o, ecmcounter = 0;
4338 int32_t oldpidindex = demux[i].pidindex;
4339 demux[i].pidindex = j; // set current ecmpid as the new pid to descramble
4340 if(oldpidindex != -1) demux[i].ECMpids[j].index = demux[i].ECMpids[oldpidindex].index; // swap index with lower status pid that was descrambling
4341 for(t = 0; t < demux[i].ECMpidcount; t++) //check this pid with controlword FOUND for higher status:
4342 {
4343 if(t != j && demux[i].ECMpids[j].status >= demux[i].ECMpids[t].status)
4344 {
4345 for(o = 0; o < maxfilter; o++) // check if ecmfilter is in use & stop all ecmfilters of lower status pids
4346 {
4347 if(demux[i].demux_fd[o].fd > 0 && demux[i].demux_fd[o].type == TYPE_ECM && (demux[i].demux_fd[o].pidindex == t))
4348 {
4349 dvbapi_stop_filternum(i, o); // ecmfilter belongs to lower status pid -> kill!
4350 }
4351 }
4352 dvbapi_edit_channel_cache(i, t, 0); // remove lowerstatus pid from channelcache
4353 demux[i].ECMpids[t].checked = 4; // mark index t as low status
4354 }
4355 }
4356
4357
4358 for(o = 0; o < maxfilter; o++) if(demux[i].demux_fd[o].type == TYPE_ECM) { ecmcounter++; } // count all ecmfilters
4359
4360 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
4361 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
4362
4363 if(ecmcounter == 1) // if total found running ecmfilters is 1 -> we found the "best" pid
4364 {
4365 dvbapi_edit_channel_cache(i, j, 1);
4366 demux[i].ECMpids[j].checked = 4; // mark best pid last ;)
4367 }
4368
4369 cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
4370 i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
4371 }
4372 pthread_mutex_unlock(&demux[i].answerlock); // and release it!
4373 }
4374
4375 if(er->rc >= E_NOTFOUND) // not found on requestmode 0 + 1
4376 {
4377 if(er->rc == E_SLEEPING)
4378 {
4379 dvbapi_stop_descrambling(i);
4380 return;
4381 }
4382
4383 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(i, j, 'p');
4384
4385 if(forceentry && forceentry->force) // forced pid? keep trying the forced ecmpid!
4386 {
4387 if(!caid_is_irdeto(er->caid) || forceentry->chid < 0x10000) //all cas or irdeto cas with forced prio chid
4388 {
4389 demux[i].ECMpids[j].table = 0;
4390 dvbapi_set_section_filter(i, er, -1);
4391 continue;
4392 }
4393 else // irdeto cas without chid prio forced
4394 {
4395 if(demux[i].ECMpids[j].irdeto_curindex == 0xFE) { demux[i].ECMpids[j].irdeto_curindex = 0x00; } // init irdeto current index to first one
4396 if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex)) // check for last / max chid
4397 {
4398 cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of FORCED PID %d CAID %04X PROVID %06X ECMPID %04X", i,
4399 j, er->caid, er->prid, er->pid);
4400 demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
4401 demux[i].ECMpids[j].table = 0;
4402 dvbapi_set_section_filter(i, er, -1);
4403 continue;
4404 }
4405 }
4406 }
4407
4408 // in case of timeout or fatal LB event give this pid another try but no more than 1 try
4409 if((er->rc == E_TIMEOUT || (er->rcEx && er->rcEx <= E2_CCCAM_NOCARD)) && demux[i].ECMpids[j].tries == 0xFE)
4410 {
4411 demux[i].ECMpids[j].tries = 1;
4412 demux[i].ECMpids[j].table = 0;
4413 dvbapi_set_section_filter(i, er, -1);
4414 continue;
4415 }
4416 else // all not found responses exception: first timeout response and first fatal loadbalancer response
4417 {
4418 demux[i].ECMpids[j].CHID = 0x10000; // get rid of this prio chid since it failed!
4419 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry
4420 }
4421
4422 if(caid_is_irdeto(er->caid))
4423 {
4424 if(demux[i].ECMpids[j].irdeto_curindex == 0xFE) { demux[i].ECMpids[j].irdeto_curindex = 0x00; } // init irdeto current index to first one
4425 if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex)) // check for last / max chid
4426 {
4427 cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of PID %d CAID %04X PROVID %06X ECMPID %04X VPID %04X", i,
4428 j, er->caid, er->prid, er->pid, er->vpid);
4429 demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
4430 demux[i].ECMpids[j].table = 0;
4431 dvbapi_set_section_filter(i, er, -1);
4432 continue;
4433 }
4434 }
4435
4436 dvbapi_edit_channel_cache(i, j, 0); // remove this pid from channelcache
4437 if(demux[i].pidindex == j)
4438 {
4439 demux[i].pidindex = -1; // current pid delivered a notfound so this pid isnt being used to descramble any longer-> clear pidindex
4440 }
4441 demux[i].ECMpids[j].irdeto_maxindex = 0;
4442 demux[i].ECMpids[j].irdeto_curindex = 0xFE;
4443 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
4444 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
4445 demux[i].ECMpids[j].table = 0;
4446 demux[i].ECMpids[j].checked = 4; // flag ecmpid as checked
4447 demux[i].ECMpids[j].status = -1; // flag ecmpid as unusable
4448 int32_t found = 1; // setup for first run
4449 int32_t filternum = -1;
4450
4451 while(found > 0) // disable all ecm + emm filters for this notfound
4452 {
4453 found = 0;
4454 filternum = dvbapi_get_filternum(i, er, TYPE_ECM); // get ecm filternumber
4455 if(filternum > -1) // in case valid filter found
4456 {
4457 int32_t fd = demux[i].demux_fd[filternum].fd;
4458 if(fd > 0) // in case valid fd
4459 {
4460 dvbapi_stop_filternum(i, filternum); // stop ecmfilter
4461 found = 1;
4462 }
4463 }
4464 if(caid_is_irdeto(er->caid)) // in case irdeto cas stop old emm filters
4465 {
4466 filternum = dvbapi_get_filternum(i, er, TYPE_EMM); // get emm filternumber
4467 if(filternum > -1) // in case valid filter found
4468 {
4469 int32_t fd = demux[i].demux_fd[filternum].fd;
4470 if(fd > 0) // in case valid fd
4471 {
4472 dvbapi_stop_filternum(i, filternum); // stop emmfilter
4473 found = 1;
4474 }
4475 }
4476 }
4477 }
4478
4479 continue;
4480 }
4481
4482
4483 // below this should be only run in case of ecm answer is found
4484
4485 uint32_t chid = get_subid(er); // derive current chid in case of irdeto, or a unique part of ecm on other cas systems
4486 demux[i].ECMpids[j].CHID = (chid != 0 ? chid : 0x10000); // if not zero apply, otherwise use no chid value 0x10000
4487 dvbapi_edit_channel_cache(i, j, 1); // do it here to here after the right CHID is registered
4488
4489 //dvbapi_set_section_filter(i, er); is not needed anymore (unsure)
4490 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
4491 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
4492
4493 if(nocw_write || demux[i].pidindex != j) { continue; } // cw was already written by another filter or current pid isnt pid used to descramble so it ends here!
4494
4495 struct s_dvbapi_priority *delayentry = dvbapi_check_prio_match(i, demux[i].pidindex, 'd');
4496 if(delayentry)
4497 {
4498 if(delayentry->delay < 1000)
4499 {
4500 cs_log_dbg(D_DVBAPI, "wait %d ms", delayentry->delay);
4501 cs_sleepms(delayentry->delay);
4502 }
4503 }
4504
4505 delayer(er);
4506
4507 switch(selected_api)
4508 {
4509#ifdef WITH_STAPI
4510 case STAPI:
4511 stapi_write_cw(i, er->cw, demux[i].STREAMpids, demux[i].STREAMpidcount, demux[i].pmt_file);
4512 break;
4513#endif
4514 default:
4515 dvbapi_write_cw(i, er->cw, j);
4516 break;
4517 }
4518
4519 // reset idle-Time
4520 client->last = time((time_t *)0); // ********* TO BE FIXED LATER ON ******
4521
4522 FILE *ecmtxt = NULL;
4523 if (!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
4524 ecmtxt = fopen(ECMINFO_FILE, "w");
4525 if(ecmtxt != NULL && er->rc < E_NOTFOUND)
4526 {
4527 char tmp[25];
4528 fprintf(ecmtxt, "caid: 0x%04X\npid: 0x%04X\nprov: 0x%06X\n", er->caid, er->pid, (uint) er->prid);
4529 switch(er->rc)
4530 {
4531 case E_FOUND:
4532 if(er->selected_reader)
4533 {
4534 fprintf(ecmtxt, "reader: %s\n", er->selected_reader->label);
4535 if(is_network_reader(er->selected_reader))
4536 { fprintf(ecmtxt, "from: %s\n", er->selected_reader->device); }
4537 else
4538 { fprintf(ecmtxt, "from: local\n"); }
4539 fprintf(ecmtxt, "protocol: %s\n", reader_get_type_desc(er->selected_reader, 1));
4540 fprintf(ecmtxt, "hops: %d\n", er->selected_reader->currenthops);
4541 }
4542 break;
4543
4544 case E_CACHE1:
4545 fprintf(ecmtxt, "reader: Cache\n");
4546 fprintf(ecmtxt, "from: cache1\n");
4547 fprintf(ecmtxt, "protocol: none\n");
4548 break;
4549
4550 case E_CACHE2:
4551 fprintf(ecmtxt, "reader: Cache\n");
4552 fprintf(ecmtxt, "from: cache2\n");
4553 fprintf(ecmtxt, "protocol: none\n");
4554 break;
4555
4556 case E_CACHEEX:
4557 fprintf(ecmtxt, "reader: Cache\n");
4558 fprintf(ecmtxt, "from: cache3\n");
4559 fprintf(ecmtxt, "protocol: none\n");
4560 break;
4561 }
4562 fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime / 1000);
4563 fprintf(ecmtxt, "cw0: %s\n", cs_hexdump(1, demux[i].lastcw[0], 8, tmp, sizeof(tmp)));
4564 fprintf(ecmtxt, "cw1: %s\n", cs_hexdump(1, demux[i].lastcw[1], 8, tmp, sizeof(tmp)));
4565 }
4566 if(ecmtxt)
4567 {
4568 int32_t ret = fclose(ecmtxt);
4569 if(ret < 0) { cs_log("ERROR: Could not close ecmtxt fd (errno=%d %s)", errno, strerror(errno)); }
4570 ecmtxt = NULL;
4571 }
4572
4573 }
4574 if(handled == 0)
4575 {
4576 cs_log_dbg(D_DVBAPI, "Unhandled ECM response received for CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
4577 er->caid, er->prid, er->pid, er->chid, er->vpid);
4578 }
4579
4580}
4581
4582void *dvbapi_start_handler(struct s_client *cl, uchar *UNUSED(mbuf), int32_t module_idx, void * (*_main_func)(void *))
4583{
4584 // cs_log("dvbapi loaded fd=%d", idx);
4585 if(cfg.dvbapi_enabled == 1)
4586 {
4587 cl = create_client(get_null_ip());
4588 cl->module_idx = module_idx;
4589 cl->typ = 'c';
4590 int32_t ret = pthread_create(&cl->thread, NULL, _main_func, (void *) cl);
4591 if(ret)
4592 {
4593 cs_log("ERROR: Can't create dvbapi handler thread (errno=%d %s)", ret, strerror(ret));
4594 return NULL;
4595 }
4596 else
4597 { pthread_detach(cl->thread); }
4598 }
4599
4600 return NULL;
4601}
4602
4603void *dvbapi_handler(struct s_client *cl, uchar *mbuf, int32_t module_idx)
4604{
4605 return dvbapi_start_handler(cl, mbuf, module_idx, dvbapi_main_local);
4606}
4607
4608int32_t dvbapi_set_section_filter(int32_t demux_index, ECM_REQUEST *er, int32_t n)
4609{
4610 if(!er) { return -1; }
4611
4612 if(selected_api != DVBAPI_3 && selected_api != DVBAPI_1 && selected_api != STAPI) // only valid for dvbapi3, dvbapi1 and STAPI
4613 {
4614 return 0;
4615 }
4616
4617 if(n == -1)
4618 {
4619 n = dvbapi_get_filternum(demux_index, er, TYPE_ECM);
4620 }
4621
4622 if(n < 0) { return -1; } // in case no valid filter found;
4623
4624 int32_t fd = demux[demux_index].demux_fd[n].fd;
4625 if(fd < 1) { return -1 ; } // in case no valid fd
4626
4627 uchar filter[16];
4628 uchar mask[16];
4629 memset(filter, 0, 16);
4630 memset(mask, 0, 16);
4631
4632 struct s_ecmpids *curpid = NULL;
4633 int32_t pid = demux[demux_index].demux_fd[n].pidindex;
4634 if(pid != -1)
4635 {
4636 curpid = &demux[demux_index].ECMpids[pid];
4637 }
4638 if(curpid->table != er->ecm[0] && curpid->table != 0) { return -1; } // if current ecmtype differs from latest requested ecmtype do not apply section filtering!
4639 uint8_t ecmfilter = 0;
4640
4641 if(er->ecm[0] == 0x80) { ecmfilter = 0x81; } // current processed ecm is even, next will be filtered for odd
4642 else { ecmfilter = 0x80; } // current processed ecm is odd, next will be filtered for even
4643
4644 if(curpid->table != 0) // cycle ecmtype from odd to even or even to odd
4645 {
4646 filter[0] = ecmfilter; // only accept new ecms (if previous odd, filter for even and visaversa)
4647 mask[0] = 0xFF;
4648 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to %s (CAID %04X PROVID %06X FD %d)", demux_index, n + 1,
4649 (ecmfilter == 0x80 ? "EVEN" : "ODD"), curpid->CAID, curpid->PROVID, fd);
4650 }
4651 else // not decoding right now so we are interessted in all ecmtypes!
4652 {
4653 filter[0] = 0x80; // set filter to wait for any ecms
4654 mask[0] = 0xF0;
4655 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to ODD+EVEN (CAID %04X PROVID %06X FD %d)", demux_index, n + 1,
4656 curpid->CAID, curpid->PROVID, fd);
4657 }
4658 uint32_t offset = 0, extramask = 0xFF;
4659
4660 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_index, pid, 'p');
4661 //cs_log("**** curpid->CHID %04X, checked = %d, er->chid = %04X *****", curpid->CHID, curpid->checked, er->chid);
4662 // checked 4 to make sure we dont set chid filter and no such ecm in dvbstream except for forced pids!
4663 if(curpid->CHID < 0x10000 && (curpid->checked == 4 || (forceentry && forceentry->force)))
4664 {
4665
4666 switch(er->caid >> 8)
4667 {
4668 case 0x01:
4669 offset = 7;
4670 extramask = 0xF0;
4671 break; // seca
4672 case 0x05:
4673 offset = 8;
4674 break; // viaccess
4675 case 0x06:
4676 offset = 6;
4677 break; // irdeto
4678 case 0x09:
4679 offset = 11;
4680 break; // videoguard
4681 case 0x4A: // DRE-Crypt, Bulcrypt,Tongang and others?
4682 if(!caid_is_bulcrypt(er->caid))
4683 { offset = 6; }
4684 break;
4685 }
4686 }
4687
4688 int32_t irdetomatch = 1; // check if wanted irdeto index is the one the delivers current chid!
4689 if(caid_is_irdeto(curpid->CAID))
4690 {
4691 if(curpid->irdeto_curindex == er->ecm[4]) { irdetomatch = 1; } // ok apply chid filtering
4692 else { irdetomatch = 0; } // skip chid filtering but apply irdeto index filtering
4693 }
4694
4695 if(offset && irdetomatch) // we have a cas with chid or unique part in checked ecm
4696 {
4697 i2b_buf(2, curpid->CHID, filter + (offset - 2));
4698 mask[(offset - 2)] = 0xFF&extramask; // additional mask seca2 chid can be FC10 or FD10 varies each month so only apply F?10
4699 mask[(offset - 1)] = 0xFF;
4700 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to %04X on fd %d", demux_index, n + 1, curpid->CHID, fd);
4701 }
4702 else
4703 {
4704 if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_curindex < 0xFE)) // on irdeto we can always apply irdeto index filtering!
4705 {
4706 filter[2] = curpid->irdeto_curindex;
4707 mask[2] = 0xFF;
4708 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set irdetoindex to %d on fd %d", demux_index, n + 1, curpid->irdeto_curindex, fd);
4709 }
4710 else // all other cas systems also cas systems without chid or unique ecm part
4711 {
4712 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to ANY CHID on fd %d", demux_index, n + 1, fd);
4713 }
4714 }
4715
4716 int32_t ret = dvbapi_activate_section_filter(demux_index, n, fd, curpid->ECM_PID, filter, mask);
4717 if(ret < 0) // something went wrong setting filter!
4718 {
4719 cs_log("Demuxer %d Filter %d (fd %d) error setting section filtering -> stop filter!", demux_index, n + 1, fd);
4720 ret = dvbapi_stop_filternum(demux_index, n);
4721 if(ret == -1)
4722 {
4723 cs_log("Demuxer %d Filter %d (fd %d) stopping filter failed -> kill all filters of this demuxer!", demux_index, n + 1, fd);
4724 dvbapi_stop_filter(demux_index, TYPE_EMM);
4725 dvbapi_stop_filter(demux_index, TYPE_ECM);
4726 }
4727 return -1;
4728 }
4729 return n;
4730}
4731
4732int32_t dvbapi_activate_section_filter(int32_t demux_index, int32_t num, int32_t fd, int32_t pid, uchar *filter, uchar *mask)
4733{
4734
4735 int32_t ret = -1;
4736
4737 switch(selected_api)
4738 {
4739 case DVBAPI_3:
4740 {
4741 struct dmx_sct_filter_params sFP2;
4742 memset(&sFP2, 0, sizeof(sFP2));
4743 sFP2.pid = pid;
4744 sFP2.timeout = 0;
4745 sFP2.flags = DMX_IMMEDIATE_START;
4746 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
4747 {
4748 //DeepThought: on dgs/cubestation and neumo images, perhaps others
4749 //the following code is needed to descramble
4750 sFP2.filter.filter[0] = filter[0];
4751 sFP2.filter.mask[0] = mask[0];
4752 sFP2.filter.filter[1] = 0;
4753 sFP2.filter.mask[1] = 0;
4754 sFP2.filter.filter[2] = 0;
4755 sFP2.filter.mask[2] = 0;
4756 memcpy(sFP2.filter.filter + 3, filter + 1, 16 - 3);
4757 memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
4758 //DeepThought: in the drivers of the dgs/cubestation and neumo images,
4759 //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
4760 //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
4761 //due to a bug its sets the "positive mask" wrongly (they should be all 0).
4762 //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
4763 //structure, which is incorrect (it should be dmxSctFilterParams).
4764 //The only way to get it right is to call DMX_SET_FILTER1 with the argument
4765 //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
4766 ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP2);
4767 }
4768 else
4769 {
4770 memcpy(sFP2.filter.filter, filter, 16);
4771 memcpy(sFP2.filter.mask, mask, 16);
4772 if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
4773 ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER, demux[demux_index].socket_fd, demux_index, num, (unsigned char *) &sFP2);
4774 else
4775 ret = dvbapi_ioctl(fd, DMX_SET_FILTER, &sFP2);
4776 }
4777 break;
4778 }
4779
4780 case DVBAPI_1:
4781 {
4782 struct dmxSctFilterParams sFP1;
4783 memset(&sFP1, 0, sizeof(sFP1));
4784 sFP1.pid = pid;
4785 sFP1.timeout = 0;
4786 sFP1.flags = DMX_IMMEDIATE_START;
4787 memcpy(sFP1.filter.filter, filter, 16);
4788 memcpy(sFP1.filter.mask, mask, 16);
4789 ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP1);
4790 break;
4791 }
4792#ifdef WITH_STAPI
4793 case STAPI:
4794 {
4795 ret = stapi_activate_section_filter(fd, filter, mask);
4796 break;
4797 }
4798#endif
4799 /*#ifdef WITH_COOLAPI ******* NOT IMPLEMENTED YET ********
4800 case COOLAPI: {
4801 coolapi_set_filter(demux[demux_id].demux_fd[n].fd, n, pid, filter, mask, TYPE_ECM);
4802 break;
4803 }
4804 #endif
4805 */
4806 default:
4807 break;
4808 }
4809 return ret;
4810}
4811
4812
4813int32_t dvbapi_check_ecm_delayed_delivery(int32_t demux_index, ECM_REQUEST *er)
4814{
4815 char nullcw[CS_ECMSTORESIZE];
4816 memset(nullcw, 0, CS_ECMSTORESIZE);
4817 if(memcmp(er->cw, nullcw, 8) == 0 && memcmp(er->cw+8, nullcw, 8) == 0) {return 5;} // received a null cw -> not usable!
4818 int32_t filternum = dvbapi_get_filternum(demux_index, er, TYPE_ECM);
4819 if(filternum < 0) { return 2; } // if no matching filter act like ecm response is delayed
4820 struct s_ecmpids *curpid = NULL;
4821 int32_t pid = demux[demux_index].demux_fd[filternum].pidindex;
4822 if(pid !=-1)
4823 {
4824 curpid = &demux[demux_index].ECMpids[pid];
4825 if(curpid->table == 0) { return 3; } // on change table act like ecm response is found
4826 }
4827
4828 if(er->rc == E_CACHEEX) { return 4; } // on cache-ex response act like ecm response is found
4829
4830 if(memcmp(demux[demux_index].demux_fd[filternum].ecmd5, nullcw, CS_ECMSTORESIZE))
4831 {
4832 char ecmd5[17 * 3];
4833 cs_hexdump(0, demux[demux_index].demux_fd[filternum].ecmd5, 16, ecmd5, sizeof(ecmd5));
4834 cs_log_dbg(D_DVBAPI, "Demuxer %d requested controlword for ecm %s on fd %d", demux_index, ecmd5, demux[demux_index].demux_fd[filternum].fd);
4835 return memcmp(demux[demux_index].demux_fd[filternum].ecmd5, er->ecmd5, CS_ECMSTORESIZE); // 1 = no response on the ecm we request last for this fd!
4836 }
4837 else { return 0; }
4838}
4839
4840int32_t dvbapi_get_filternum(int32_t demux_index, ECM_REQUEST *er, int32_t type)
4841{
4842 if(!er) { return -1; }
4843
4844 int32_t n;
4845 int32_t fd = -1;
4846
4847 for(n = 0; n < maxfilter; n++) // determine fd
4848 {
4849 if(demux[demux_index].demux_fd[n].fd > 0 && demux[demux_index].demux_fd[n].type == type) // check for valid and right type (ecm or emm)
4850 {
4851 if(type == TYPE_ECM && er->srvid != demux[demux_index].program_number) continue;
4852 if((demux[demux_index].demux_fd[n].pid == er->pid) &&
4853 ((demux[demux_index].demux_fd[n].provid == er->prid) || demux[demux_index].demux_fd[n].provid == 0) &&
4854 ((demux[demux_index].demux_fd[n].caid == er->caid) || (demux[demux_index].demux_fd[n].caid == er->ocaid))) // current ecm pid?
4855 {
4856 fd = demux[demux_index].demux_fd[n].fd; // found!
4857 break;
4858 }
4859 }
4860 }
4861 if(fd > 0 && demux[demux_index].demux_fd[n].provid == 0) { demux[demux_index].demux_fd[n].provid = er->prid; } // hack to fill in provid into demuxer
4862
4863 return (fd > 0 ? n : fd); // return -1(fd) on not found, on found return filternumber(n)
4864}
4865
4866int32_t dvbapi_ca_setpid(int32_t demux_index, int32_t pid)
4867{
4868 int32_t idx = -1, n;
4869 if(pid == -1 || pid > demux[demux_index].ECMpidcount) return -1;
4870
4871 idx = demux[demux_index].ECMpids[pid].index;
4872
4873 if(!idx) // if no indexer for this pid get one!
4874 {
4875 idx = dvbapi_get_descindex(demux_index);
4876 demux[demux_index].ECMpids[pid].index = idx;
4877 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d", demux_index, pid,
4878 demux[demux_index].ECMpids[pid].CAID, demux[demux_index].ECMpids[pid].ECM_PID, idx - 1);
4879 }
4880
4881 for(n = 0; n < demux[demux_index].STREAMpidcount; n++)
4882 {
4883 if(!demux[demux_index].ECMpids[pid].streams || ((demux[demux_index].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n))){
4884 dvbapi_set_pid(demux_index, n, idx - 1, true); // enable streampid
4885 }
4886 else{
4887 dvbapi_set_pid(demux_index, n, idx - 1, false); // disable streampid
4888 }
4889 }
4890
4891 return idx - 1; // return caindexer
4892}
4893
4894int8_t update_streampid_list(uint8_t cadevice, uint16_t pid, int32_t idx)
4895{
4896 struct s_streampid *listitem, *newlistitem;
4897 if(!ll_activestreampids)
4898 { ll_activestreampids = ll_create("ll_activestreampids"); }
4899 LL_ITER itr;
4900 if(ll_count(ll_activestreampids) > 0)
4901 {
4902 itr = ll_iter_create(ll_activestreampids);
4903 while((listitem = ll_iter_next(&itr)))
4904 {
4905 if (cadevice == listitem->cadevice && pid == listitem->streampid){
4906 if((listitem->activeindexers & (1 << idx)) == (uint) (1 << idx)){
4907 return FOUND_STREAMPID_INDEX; // match found
4908 }else{
4909 listitem->activeindexers|=(1 << idx); // ca + pid found but not this index -> add this index
4910 cs_log_dbg(D_DVBAPI, "Added existing streampid %04X with new index %d to ca%d", pid, idx, cadevice);
4911 return ADDED_STREAMPID_INDEX;
4912 }
4913 }
4914 }
4915 }
4916 if(!cs_malloc(&newlistitem, sizeof(struct s_streampid)))
4917 { return FIRST_STREAMPID_INDEX; }
4918 newlistitem->cadevice = cadevice;
4919 newlistitem->streampid = pid;
4920 newlistitem->activeindexers = (1 << idx);
4921 newlistitem->caindex = idx; // set this index as used to decode on ca device
4922 ll_append(ll_activestreampids, newlistitem);
4923 cs_log_dbg(D_DVBAPI, "Added new streampid %04X with index %d to ca%d", pid, idx, cadevice);
4924 return FIRST_STREAMPID_INDEX;
4925}
4926
4927int8_t remove_streampid_from_list(uint8_t cadevice, uint16_t pid, int32_t idx)
4928{
4929 if(!ll_activestreampids) return NO_STREAMPID_LISTED;
4930
4931 struct s_streampid *listitem;
4932 int8_t removed = 0;
4933
4934 LL_ITER itr;
4935 if(ll_count(ll_activestreampids) > 0)
4936 {
4937 itr = ll_iter_create(ll_activestreampids);
4938 while((listitem = ll_iter_next(&itr)))
4939 {
4940 if (cadevice == listitem->cadevice && pid == listitem->streampid)
4941 {
4942 if(idx == -1){ // idx -1 means disable all!
4943 listitem->activeindexers = 0;
4944 removed = 1;
4945 }
4946 else if((listitem->activeindexers & (1 << idx)) == (uint) (1 << idx))
4947 {
4948 listitem->activeindexers &= ~(1 << idx); // flag it as disabled for this index
4949 removed = 1;
4950 }
4951
4952 if(removed)
4953 {
4954 cs_log_dbg(D_DVBAPI, "Remove streampid %04X using indexer %d from ca%d", pid, idx, cadevice);
4955 }
4956 if (listitem->activeindexers == 0 && removed == 1) // all indexers disabled? -> remove pid from list!
4957 {
4958 ll_iter_remove_data(&itr);
4959 cs_log_dbg(D_DVBAPI, "Removed last indexer of streampid %04X from ca%d", pid, cadevice);
4960 return REMOVED_STREAMPID_LASTINDEX;
4961 }
4962 else if(removed == 1)
4963 {
4964 if (idx > 0 && (uint) idx != listitem->caindex)
4965 {
4966 return REMOVED_STREAMPID_INDEX;
4967 }
4968 else
4969 {
4970 listitem->caindex = 0xFFFF;
4971 cs_log_dbg(D_DVBAPI, "Streampid %04X index %d was used for decoding on ca%d", pid, idx, cadevice);
4972 return REMOVED_DECODING_STREAMPID_INDEX;
4973 }
4974 }
4975 }
4976 }
4977 }
4978 return NO_STREAMPID_LISTED;
4979}
4980
4981void disable_unused_streampids(int16_t demux_id)
4982{
4983 if(!ll_activestreampids) return;
4984 if(ll_count(ll_activestreampids) == 0) return; // no items in list?
4985
4986 int32_t ecmpid = demux[demux_id].pidindex;
4987 if (ecmpid == -1) return; // no active ecmpid!
4988
4989 int32_t idx = demux[demux_id].ECMpids[ecmpid].index;
4990 int32_t i,n;
4991 struct s_streampid *listitem;
4992 // search for old enabled streampids on all ca devices that have to be disabled, index 0 is skipped as it belongs to fta!
4993 for(i = 0; i < MAX_DEMUX && idx; i++){
4994 if(!((demux[demux_id].ca_mask & (1 << i)) == (uint) (1 << i))) continue; // continue if ca is unused by this demuxer
4995
4996 LL_ITER itr;
4997 itr = ll_iter_create(ll_activestreampids);
4998 while((listitem = ll_iter_next(&itr)))
4999 {
5000 if (i != listitem->cadevice) continue; // ca doesnt match
5001 if (!((listitem->activeindexers & (1 << (idx-1))) == (uint) (1 << (idx-1)))) continue; // index doesnt match
5002 for(n = 0; n < demux[demux_id].STREAMpidcount; n++){
5003 if (listitem->streampid == demux[demux_id].STREAMpids[n]){ // check if pid matches with current streampid on demuxer
5004 break;
5005 }
5006 }
5007 if (n == demux[demux_id].STREAMpidcount){
5008 demux[demux_id].STREAMpids[n] = listitem->streampid; // put it temp here!
5009 dvbapi_set_pid(demux_id, n, idx - 1, false); // no match found so disable this now unused streampid
5010 demux[demux_id].STREAMpids[n] = 0; // remove temp!
5011 }
5012 }
5013 }
5014}
5015
5016
5017int8_t is_ca_used(uint8_t cadevice, int32_t pid)
5018{
5019 if(!ll_activestreampids) return CA_IS_CLEAR;
5020
5021 struct s_streampid *listitem;
5022
5023 LL_ITER itr;
5024 if(ll_count(ll_activestreampids) > 0)
5025 {
5026 itr = ll_iter_create(ll_activestreampids);
5027 while((listitem = ll_iter_next(&itr)))
5028 {
5029 if(listitem->cadevice != cadevice) continue;
5030 if(pid && listitem->streampid != pid) continue;
5031 uint32_t i = 0;
5032 int32_t newindex = -1;
5033 if(listitem->caindex != 0xFFFF)
5034 {
5035 newindex = listitem->caindex;
5036 }
5037 while(newindex == -1)
5038 {
5039 if((listitem->activeindexers&(1 << i)) == (uint)(1 << i))
5040 {
5041 newindex = i;
5042 }
5043 i++;
5044 }
5045 if(listitem->caindex == 0xFFFF) // check if this pid has active index for ca device (0xFFFF means no active index!)
5046 {
5047
5048 listitem->caindex = newindex; // set fresh one
5049 cs_log_dbg(D_DVBAPI, "Streampid %04X is now using index %d for decoding on ca%d", pid, newindex, cadevice);
5050 }
5051 return newindex;
5052 }
5053 }
5054 return CA_IS_CLEAR; // no indexer found for this pid!
5055}
5056
5057const char *dvbapi_get_client_name(void)
5058{
5059 return client_name;
5060}
5061
5062void check_add_emmpid(int32_t demux_index, uchar *filter, int32_t l, int32_t emmtype)
5063{
5064 if (l<0) return;
5065
5066 uint32_t typtext_idx = 0;
5067 int32_t ret = -1;
5068 const char *typtext[] = { "UNIQUE", "SHARED", "GLOBAL", "UNKNOWN" };
5069
5070 while(((emmtype >> typtext_idx) & 0x01) == 0 && typtext_idx < sizeof(typtext) / sizeof(const char *))
5071 {
5072 ++typtext_idx;
5073 }
5074
5075 //filter already in list?
5076 if(is_emmfilter_in_list(filter, demux[demux_index].EMMpids[l].PID, demux[demux_index].EMMpids[l].PROVID, demux[demux_index].EMMpids[l].CAID))
5077 {
5078 cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate emm filter type %s, emmpid: 0x%04X, emmcaid: %04X, emmprovid: %06X -> SKIPPED!", demux_index,
5079 typtext[typtext_idx], demux[demux_index].EMMpids[l].PID, demux[demux_index].EMMpids[l].CAID, demux[demux_index].EMMpids[l].PROVID);
5080 return;
5081 }
5082
5083 if(demux[demux_index].emm_filter < demux[demux_index].max_emm_filter) // can this filter be started?
5084 {
5085 // try to activate this emmfilter
5086 ret = dvbapi_set_filter(demux_index, selected_api, demux[demux_index].EMMpids[l].PID, demux[demux_index].EMMpids[l].CAID,
5087 demux[demux_index].EMMpids[l].PROVID, filter, filter + 16, 0, demux[demux_index].pidindex, TYPE_EMM, 1);
5088 }
5089
5090 if(ret != -1) // -1 if maxfilter reached or filter start error!
5091 {
5092 if(demux[demux_index].emm_filter == -1) // -1: first run of emm filtering on this demuxer
5093 {
5094 demux[demux_index].emm_filter = 0;
5095 }
5096 demux[demux_index].emm_filter++; // increase total active filters
5097 cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d started emm filter type %s, pid: 0x%04X", demux_index, typtext[typtext_idx], demux[demux_index].EMMpids[l].PID);
5098 return;
5099 }
5100 else // not set successful, so add it to the list for try again later on!
5101 {
5102 add_emmfilter_to_list(demux_index, filter, demux[demux_index].EMMpids[l].CAID, demux[demux_index].EMMpids[l].PROVID, demux[demux_index].EMMpids[l].PID, 0, false);
5103 cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d added inactive emm filter type %s, pid: 0x%04X", demux_index, typtext[typtext_idx], demux[demux_index].EMMpids[l].PID);
5104 }
5105 return;
5106}
5107
5108void rotate_emmfilter(int32_t demux_id)
5109{
5110 // emm filter iteration
5111 if(!ll_emm_active_filter)
5112 { ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
5113
5114 if(!ll_emm_inactive_filter)
5115 { ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
5116
5117 if(!ll_emm_pending_filter)
5118 { ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
5119
5120 uint32_t filter_count = ll_count(ll_emm_active_filter) + ll_count(ll_emm_inactive_filter);
5121
5122 if(demux[demux_id].max_emm_filter > 0
5123 && ll_count(ll_emm_inactive_filter) > 0
5124 && filter_count > demux[demux_id].max_emm_filter)
5125 {
5126
5127 int32_t filter_queue = ll_count(ll_emm_inactive_filter);
5128 int32_t stopped = 0, started = 0;
5129 struct timeb now;
5130 cs_ftime(&now);
5131
5132 struct s_emm_filter *filter_item;
5133 LL_ITER itr;
5134 itr = ll_iter_create(ll_emm_active_filter);
5135
5136 while((filter_item = ll_iter_next(&itr)) != NULL)
5137 {
5138 if(!ll_count(ll_emm_inactive_filter) || started == filter_queue)
5139 { break; }
5140 int64_t gone = comp_timeb(&now, &filter_item->time_started);
5141 if( gone > 45*1000)
5142 {
5143 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match_emmpid(filter_item->demux_id, filter_item->caid,
5144 filter_item->provid, 'p');
5145
5146 if(!forceentry || (forceentry && !forceentry->force))
5147 {
5148 // stop active filter and add to pending list
5149 dvbapi_stop_filternum(filter_item->demux_id, filter_item->num - 1);
5150 ll_iter_remove_data(&itr);
5151 add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid,
5152 filter_item->provid, filter_item->pid, -1, false);
5153 stopped++;
5154 }
5155 }
5156
5157 int32_t ret;
5158 if(stopped > started) // we have room for new filters, try to start an inactive emmfilter!
5159 {
5160 struct s_emm_filter *filter_item2;
5161 LL_ITER itr2 = ll_iter_create(ll_emm_inactive_filter);
5162
5163 while((filter_item2 = ll_iter_next(&itr2)))
5164 {
5165 ret = dvbapi_set_filter(filter_item2->demux_id, selected_api, filter_item2->pid, filter_item2->caid,
5166 filter_item2->provid, filter_item2->filter, filter_item2->filter + 16, 0,
5167 demux[filter_item2->demux_id].pidindex, TYPE_EMM, 1);
5168 if(ret != -1)
5169 {
5170 ll_iter_remove_data(&itr2);
5171 started++;
5172 break;
5173 }
5174 }
5175 }
5176 }
5177
5178 itr = ll_iter_create(ll_emm_pending_filter);
5179
5180 while((filter_item = ll_iter_next(&itr)) != NULL) // move pending filters to inactive
5181 {
5182 add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, 0, false);
5183 ll_iter_remove_data(&itr);
5184 }
5185 }
5186}
5187
5188uint16_t dvbapi_get_client_proto_version(void)
5189{
5190 return client_proto_version;
5191}
5192
5193/*
5194 * protocol structure
5195 */
5196
5197void module_dvbapi(struct s_module *ph)
5198{
5199 ph->desc = "dvbapi";
5200 ph->type = MOD_CONN_SERIAL;
5201 ph->listenertype = LIS_DVBAPI;
5202#if defined(WITH_AZBOX)
5203 ph->s_handler = azbox_handler;
5204 ph->send_dcw = azbox_send_dcw;
5205#elif defined(WITH_MCA)
5206 ph->s_handler = mca_handler;
5207 ph->send_dcw = mca_send_dcw;
5208 selected_box = selected_api = 0; // HACK: This fixes incorrect warning about out of bounds array access in functionas that are not even called when WITH_MCA is defined
5209#else
5210 ph->s_handler = dvbapi_handler;
5211 ph->send_dcw = dvbapi_send_dcw;
5212#endif
5213}
5214#endif // HAVE_DVBAPI
Note: See TracBrowser for help on using the repository browser.