source: trunk/module-dvbapi.c@ 11708

Last change on this file since 11708 was 11708, checked in by nautilus7, 5 months ago

[dvbapi] Do not block ECM / EMM pid 0x1FFF

ECM pid 0x1FFF is used in the fake ecm request for BISS. We cannot block it!

  • Property svn:eol-style set to LF
File size: 255.3 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#include "cscrypt/md5.h"
28extern int32_t exit_oscam;
29
30#if defined (__CYGWIN__)
31#define F_NOTIFY 0
32#define F_SETSIG 0
33#define DN_MODIFY 0
34#define DN_CREATE 0
35#define DN_DELETE 0
36#define DN_MULTISHOT 0
37#endif
38
39static const char *get_stream_type_txt(uint8_t stream_type)
40{
41 switch(stream_type)
42 {
43 case 0x00: return "invalid";
44 case 0x01: return "MPEG-1 video";
45 case 0x02: return "MPEG-2 video"; // MPEG-1 (constrained parameter) or MPEG-2 video
46 case 0x03: return "MPEG-1 audio"; // MP1, MP2, MP3
47 case 0x04: return "MPEG-2 audio"; // MP1, MP2, MP3
48 case 0x05: return "MPEG-2 private sections";
49 case 0x06: return "MPEG-2 PES private data"; // AC-3, Enhanced AC-3, AC-4, DTS(-HD) audio, subtitles, etc (DVB) (depends on descriptor)
50 case 0x07: return "MHEG data";
51 case 0x08: return "DSM-CC data";
52 case 0x09: return "MPEG-2 over ATM data";
53 case 0x0A: return "DSM-CC data";
54 case 0x0B: return "DSM-CC data";
55 case 0x0C: return "DSM-CC data";
56 case 0x0D: return "DSM-CC data";
57 case 0x0E: return "MPEG-2 auxiliary data";
58 case 0x0F: return "MPEG-2 audio"; // AAC
59 case 0x10: return "MPEG-4 video";
60 case 0x11: return "MPEG-4 audio"; // AAC, HE AAC and AAC v2
61 case 0x12: return "MPEG-4 PES data";
62 case 0x13: return "MPEG-4 data";
63 case 0x14: return "DSM-CC data";
64 case 0x15: return "MPEG-7 MetaData";
65 case 0x16: return "MPEG-7 MetaData";
66 case 0x17: return "MPEG-7 MetaData";
67 case 0x18: return "MPEG-7 MetaData";
68 case 0x19: return "MPEG-7 MetaData";
69 case 0x1A: return "MPEG-2 IPMP data";
70 case 0x1B: return "AVC video";
71 case 0x1C: return "MPEG-4 audio"; // DST, ALS, SLS
72 case 0x24: return "HEVC video";
73 case 0x25: return "HEVC subset video";
74 case 0x2D: return "MPEG-H 3D audio"; // main stream
75 case 0x2E: return "MPEG-H 3D audio"; // auxiliary stream
76 case 0x42: return "Chinese video";
77 case 0x7F: return "IPMP data";
78 case 0x81: return "AC-3 audio (ATSC)"; // with descriptor tag 0x81
79 case 0x86: return "SCTE 35 data";
80 case 0x87: return "enhanced AC-3 audio (ATSC)";
81 //case 0x88: return "DTS-HD audio (ATSC 2.0)"; // fixme: has ATSC 2.0 ever been used?
82 //case 0x??: return "AC-4 audio (ATSC 3.0)"; // fixme: add the actual value when it gets published
83 //case 0x??: return "MPEG-H 3D audio (ATSC 3.0)"; // fixme: add the actual value when it gets published
84 case 0xD1: return "BBC Dirac video";
85 case 0xEA: return "VC-1 video";
86 default: return "user private";
87 }
88}
89
90static const char *get_descriptor_tag_txt(uint8_t descriptor_tag)
91{
92 switch(descriptor_tag)
93 {
94 // Valid in all MPEG contexts:
95 case 0x00: return "reserved";
96 case 0x01: return "forbidden";
97 case 0x02: return "video stream";
98 case 0x03: return "audio stream";
99 case 0x04: return "hierarchy";
100 case 0x05: return "registration";
101 case 0x06: return "data stream alignment";
102 case 0x07: return "target background grid";
103 case 0x08: return "video window";
104 case 0x09: return "CA";
105 case 0x0A: return "ISO 639 language";
106 case 0x0B: return "system clock";
107 case 0x0C: return "multiplex buffer utilization";
108 case 0x0D: return "copyright";
109 case 0x0E: return "maximum bitrate";
110 case 0x0F: return "private data indicator";
111 case 0x10: return "smoothing buffer";
112 case 0x11: return "STD";
113 case 0x12: return "IBP";
114 case 0x13: return "DSM-CC carousel identifier";
115 case 0x14: return "DSM-CC association tag";
116 case 0x15: return "DSM-CC deferred association tags";
117 case 0x17: return "DSM-CC NPT reference";
118 case 0x18: return "DSM-CC NPT endpoint";
119 case 0x19: return "DSM-CC stream mode";
120 case 0x1A: return "DSM-CC stream event";
121 case 0x1B: return "MPEG-4 video";
122 case 0x1C: return "MPEG-4 audio";
123 case 0x1D: return "IOD";
124 case 0x1E: return "SL";
125 case 0x1F: return "FMC";
126 case 0x20: return "External ES id";
127 case 0x21: return "MuxCode";
128 case 0x22: return "FmxBufferSize";
129 case 0x23: return "MultiplexBuffer";
130 case 0x24: return "Content labeling";
131 case 0x25: return "Metadata association";
132 case 0x26: return "Metadata";
133 case 0x27: return "Metadata STD";
134 case 0x28: return "AVC video";
135 case 0x29: return "MPEG-2 IPMP";
136 case 0x2A: return "AVC timing and HRD";
137 case 0x2B: return "MPEG-2 AAC Audio";
138 case 0x2C: return "FlexMuxTiming";
139 case 0x2D: return "MPEG-4 Text";
140 case 0x2E: return "MPEG-4 Audio Extension";
141 case 0x2F: return "Auxiliary Video Stream";
142 case 0x30: return "SVC Extension";
143 case 0x31: return "MVC Extension";
144 case 0x32: return "J2K Video";
145 case 0x33: return "MVC Operation Point";
146 case 0x34: return "MPEG-2 Stereoscopic Video Format";
147 case 0x35: return "Stereoscopic Program Info";
148 case 0x36: return "Stereoscopic Video Info";
149 case 0x37: return "Transport Profile";
150 case 0x38: return "HEVC Video";
151 case 0x3F: return "MPEG-2 Extension";
152 // Valid in DVB context:
153 case 0x45: return "VBI data";
154 case 0x46: return "VBI teletext";
155 case 0x51: return "mosaic";
156 case 0x52: return "stream identifier";
157 case 0x56: return "teletext";
158 case 0x59: return "subtitling"; // with stream type 0x06
159 case 0x5F: return "private data specifier";
160 case 0x60: return "service move";
161 case 0x65: return "scrambling";
162 case 0x66: return "data broadcast id";
163 case 0x6A: return "AC-3"; // with stream type 0x06
164 case 0x6B: return "ancillary data";
165 case 0x6F: return "application signalling";
166 case 0x70: return "adaptation field data";
167 case 0x74: return "related content";
168 case 0x78: return "ECM repetition rate";
169 case 0x7A: return "enhanced AC-3"; // with stream type 0x06
170 case 0x7B: return "DTS"; // with stream type 0x06
171 case 0x7C: return "AAC"; // with stream type 0x06
172 case 0x7D: return "XAIT location";
173 case 0x7F: return "DVB extension";
174 // Valid in ATSC context:
175 case 0x81: return "AC-3"; // with stream type 0x81
176 case 0xCC: return "enhanced AC-3"; // with stream type 0x87
177 default: return "user private";
178 }
179}
180
181static const char *get_extension_descriptor_txt(uint8_t extension_tag)
182{
183 switch(extension_tag)
184 {
185 case 0x02: return "CP";
186 case 0x06: return "supplementary audio";
187 case 0x0E: return "DTS-HD"; // with stream type 0x06
188 case 0x0F: return "DTS Neural"; // with stream type 0x06
189 case 0x11: return "T2MI";
190 case 0x13: return "URI linkage";
191 case 0x15: return "AC-4"; // with stream type 0x06
192 case 0x18: return "protection message";
193 case 0x19: return "audio preselection";
194 case 0x20: return "TTML subtitling"; // with stream type 0x06
195 default: return "Undefined";
196 }
197}
198
199void flush_read_fd(int32_t demux_id, int32_t num, int fd)
200{
201 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
202 {
203 cs_log_dbg(D_DVBAPI,"Demuxer %d flushing stale input data of filter %d (fd:%d)", demux_id, num + 1, fd);
204
205 fd_set rd;
206 struct timeval t;
207 char buff[100];
208
209 t.tv_sec = 0;
210 t.tv_usec = 0;
211
212 FD_ZERO(&rd);
213 FD_SET(fd, &rd);
214
215 while(select(fd + 1, &rd, NULL, NULL, &t) > 0)
216 {
217 if(read(fd, buff, 100)) { ; }
218 }
219 }
220}
221
222static int dvbapi_ioctl(int fd, uint32_t request, ...)
223{
224 int ret = 0;
225 va_list args;
226 va_start(args, request);
227 if(!(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO))
228 {
229 void *param = va_arg(args, void *);
230 ret = ioctl(fd, request, param);
231 }
232 else
233 {
234 switch(request)
235 {
236 case DMX_SET_FILTER:
237 {
238 struct dmx_sct_filter_params *sFP = va_arg(args, struct dmx_sct_filter_params *);
239 //fix filter for samygo
240 //note: we only have 14 available filter bytes (instead of 16) on samygo
241 memmove(&sFP->filter.filter[3], &sFP->filter.filter[1], 13);
242 memset(&sFP->filter.filter[1], 0, 2);
243 memmove(&sFP->filter.mask[3], &sFP->filter.mask[1], 13);
244 memset(&sFP->filter.mask[1], 0, 2);
245 // prepare packet
246 uint8_t packet[sizeof(request) + sizeof(struct dmx_sct_filter_params)];
247 memcpy(&packet, &request, sizeof(request));
248 memcpy(&packet[sizeof(request)], sFP, sizeof(struct dmx_sct_filter_params));
249 ret = send(fd, packet, sizeof(packet), 0);
250 break;
251 }
252
253 case DMX_SET_FILTER1:
254 {
255 cs_log("error: samygo does not support DMX_SET_FILTER1");
256 ret = -1;
257 break;
258 }
259
260 case DMX_STOP:
261 {
262 ret = send(fd, &request, sizeof(request), 0);
263 if(!(ret == 1)){ ret = 1; }
264 break;
265 }
266
267 case CA_GET_DESCR_INFO:
268 {
269 ca_descr_info_t *ca_descr_info = va_arg(args, ca_descr_info_t *);
270 // preparing packet
271 uint8_t packet[sizeof(request) + sizeof(ca_descr_info_t)];
272 memcpy(&packet[0], &request, sizeof(request));
273 memcpy(&packet[sizeof(request)], ca_descr_info, sizeof(ca_descr_info_t));
274 // sending data to UDP
275 ret = send(fd, &packet[0], sizeof(packet), 0);
276 break;
277 }
278
279 case CA_SET_DESCR:
280 {
281 ca_descr_t *ca_descr = va_arg(args, ca_descr_t *);
282 // preparing packet
283 uint8_t packet[sizeof(request) + sizeof(ca_descr_t)];
284 memcpy(&packet[0], &request, sizeof(request));
285 memcpy(&packet[sizeof(request)], ca_descr, sizeof(ca_descr_t));
286 // sending data to UDP
287 ret = send(fd, &packet[0], sizeof(packet), 0);
288 break;
289 }
290
291 case CA_SET_PID:
292 {
293 ca_pid_t *ca_pid2 = va_arg(args, ca_pid_t *);
294 // preparing packet
295 uint8_t packet[sizeof(request) + sizeof(ca_pid_t)];
296 memcpy(&packet[0], &request, sizeof(request));
297 memcpy(&packet[sizeof(request)], ca_pid2, sizeof(ca_pid_t));
298 // sending data to UDP
299 ret = send(fd, &packet[0], sizeof(packet), 0);
300 break;
301 }
302
303 case CA_SET_DESCR_MODE:
304 {
305 cs_log("error: samygo does not support CA_SET_DESCR_MODE");
306 ret = -1;
307 break;
308 }
309
310 case CA_SET_DESCR_DATA:
311 {
312 cs_log("error: samygo does not support CA_SET_DESCR_DATA");
313 ret = -1;
314 break;
315 }
316 }
317
318 if(ret > 0) // send() may return larger than 1
319 {
320 ret = 1;
321 }
322 }
323#if defined(__powerpc__)
324 // Old dm500 boxes (ppc old) are using broken kernel, se we need some fixups
325 switch (request)
326 {
327 case DMX_STOP:
328 case CA_SET_DESCR:
329 case CA_SET_PID:
330 ret = 1;
331 }
332#endif
333 // FIXME: Workaround for su980 bug
334 // See: https://board.streamboard.tv/?postid=533940
335 if(boxtype_is("su980"))
336 {
337 ret = 1;
338 }
339 va_end(args);
340 return ret;
341}
342
343// tunemm_caid_map
344#define FROM_TO 0
345#define TO_FROM 1
346
347int32_t pausecam = 0, disable_pmt_files = 0;
348
349DEMUXTYPE demux[MAX_DEMUX];
350struct s_dvbapi_priority *dvbapi_priority;
351struct s_client *dvbapi_client;
352
353const char *boxdesc[] = { "none", "dreambox", "duckbox", "ufs910", "dbox2", "ipbox", "ipbox-pmt",
354 "dm7000", "qboxhd", "coolstream", "neumo", "pc", "pc-nodmx", "samygo" };
355
356// when updating devices[BOX_COUNT] make sure to update these index defines
357#define BOX_INDEX_QBOXHD 0
358#define BOX_INDEX_DREAMBOX_DVBAPI3 1
359#define BOX_INDEX_COOLSTREAM 6
360
361static const struct box_devices devices[BOX_COUNT] =
362{
363 /* QboxHD (dvb-api-3)*/ { "/tmp/virtual_adapter/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
364 /* dreambox (dvb-api-3)*/ { "/dev/dvb/adapter%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
365 /* wetek (dvb-api-3)*/ { "/dev/dvb%d.", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
366 /* dreambox (dvb-api-1)*/ { "/dev/dvb/card%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_1 },
367 /* neumo (dvb-api-1)*/ { "/dev/", "demuxapi", "demuxapi", "/tmp/camd.socket", DVBAPI_1 },
368#ifdef WITH_STAPI5
369 /* sh4 (stapi5)*/ { "/dev/stapi/", "stpti5_ioctl", "stpti5_ioctl", "/tmp/camd.socket", STAPI },
370#else
371 /* sh4 (stapi)*/ { "/dev/stapi/", "stpti4_ioctl", "stpti4_ioctl", "/tmp/camd.socket", STAPI },
372#endif
373 /* coolstream*/ { "/dev/cnxt/", "null", "null", "/tmp/camd.socket", COOLAPI },
374};
375
376static int32_t selected_box = -1;
377static int32_t selected_api = -1;
378static int32_t maxfilter = MAX_FILTER;
379static int32_t dir_fd = -1;
380static uint16_t last_client_proto_version = 0;
381static char *last_client_name = NULL;
382static uint32_t ca_descramblers_total = 0; // total number of available descramblers in box
383static uint32_t ca_descramblers_used = 0; // total number of used descramblers during decoding
384static int32_t ca_fd[CA_MAX]; // holds fd handle of all ca devices (0 not in use)
385static LLIST *ll_activestreampids; // list of all enabled streampids on ca devices
386static int32_t assoc_fd[MAX_ASSOC_FD]; // list of all client sockets
387
388bool is_dvbapi_usr(char *usr)
389{
390 return streq(cfg.dvbapi_usr, usr);
391}
392
393struct s_emm_filter
394{
395 int32_t demux_id;
396 uint8_t filter[32];
397 uint16_t caid;
398 uint32_t provid;
399 uint16_t pid;
400 uint32_t num;
401 struct timeb time_started;
402};
403
404static LLIST *ll_emm_active_filter;
405static LLIST *ll_emm_inactive_filter;
406static LLIST *ll_emm_pending_filter;
407
408int32_t add_emmfilter_to_list(int32_t demux_id, uint8_t *filter, uint16_t caid, uint32_t provid, uint16_t emmpid, int32_t num, bool enable)
409{
410 if(!ll_emm_active_filter)
411 {
412 ll_emm_active_filter = ll_create("ll_emm_active_filter");
413 }
414
415 if(!ll_emm_inactive_filter)
416 {
417 ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
418 }
419
420 if(!ll_emm_pending_filter)
421 {
422 ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
423 }
424
425 struct s_emm_filter *filter_item;
426 if(!cs_malloc(&filter_item, sizeof(struct s_emm_filter)))
427 {
428 return 0;
429 }
430
431 filter_item->demux_id = demux_id;
432 memcpy(filter_item->filter, filter, 32);
433 filter_item->caid = caid;
434 filter_item->provid = provid;
435 filter_item->pid = emmpid;
436 filter_item->num = num;
437
438 if(enable)
439 {
440 cs_ftime(&filter_item->time_started);
441 }
442 else
443 {
444 memset(&filter_item->time_started, 0, sizeof(filter_item->time_started));
445 }
446
447 if(num > 0)
448 {
449 ll_append(ll_emm_active_filter, filter_item);
450 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d added to active emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
451 filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
452 }
453 else if(num < 0)
454 {
455 ll_append(ll_emm_pending_filter, filter_item);
456 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to pending emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
457 filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
458 }
459 else
460 {
461 ll_append(ll_emm_inactive_filter, filter_item);
462 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to inactive emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
463 filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
464 }
465 return 1;
466}
467
468int32_t is_emmfilter_in_list_internal(LLIST *ll, uint8_t *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
469{
470 struct s_emm_filter *filter_item;
471 LL_ITER itr;
472 if(ll_count(ll) > 0)
473 {
474 itr = ll_iter_create(ll);
475 while((filter_item = ll_iter_next(&itr)) != NULL)
476 {
477 if(!memcmp(filter_item->filter, filter, 32) && (filter_item->pid == emmpid)
478 && (filter_item->provid == provid) && (filter_item->caid == caid))
479 {
480 return 1;
481 }
482 }
483 }
484 return 0;
485}
486
487int32_t is_emmfilter_in_list(uint8_t *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
488{
489 if(!ll_emm_active_filter)
490 {
491 ll_emm_active_filter = ll_create("ll_emm_active_filter");
492 }
493
494 if(!ll_emm_inactive_filter)
495 {
496 ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
497 }
498
499 if(!ll_emm_pending_filter)
500 {
501 ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
502 }
503
504 if(is_emmfilter_in_list_internal(ll_emm_active_filter, filter, emmpid, provid, caid)
505 || is_emmfilter_in_list_internal(ll_emm_inactive_filter, filter, emmpid, provid, caid)
506 || is_emmfilter_in_list_internal(ll_emm_pending_filter, filter, emmpid, provid, caid))
507 {
508 return 1;
509 }
510 return 0;
511}
512
513struct s_emm_filter *get_emmfilter_by_filternum_internal(LLIST *ll, int32_t demux_id, uint32_t num)
514{
515 struct s_emm_filter *filter;
516 LL_ITER itr;
517 if(ll_count(ll) > 0)
518 {
519 itr = ll_iter_create(ll);
520 while((filter = ll_iter_next(&itr)))
521 {
522 if(filter->demux_id == demux_id && filter->num == num)
523 {
524 return filter;
525 }
526 }
527 }
528 return NULL;
529}
530
531struct s_emm_filter *get_emmfilter_by_filternum(int32_t demux_id, uint32_t num)
532{
533 if(!ll_emm_active_filter)
534 {
535 ll_emm_active_filter = ll_create("ll_emm_active_filter");
536 }
537
538 if(!ll_emm_inactive_filter)
539 {
540 ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
541 }
542
543 if(!ll_emm_pending_filter)
544 {
545 ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
546 }
547
548 struct s_emm_filter *emm_filter = NULL;
549
550 emm_filter = get_emmfilter_by_filternum_internal(ll_emm_active_filter, demux_id, num);
551 if(emm_filter)
552 {
553 return emm_filter;
554 }
555
556 emm_filter = get_emmfilter_by_filternum_internal(ll_emm_inactive_filter, demux_id, num);
557 if(emm_filter)
558 {
559 return emm_filter;
560 }
561
562 emm_filter = get_emmfilter_by_filternum_internal(ll_emm_pending_filter, demux_id, num);
563 if(emm_filter)
564 {
565 return emm_filter;
566 }
567
568 return NULL;
569}
570
571int8_t remove_emmfilter_from_list_internal(LLIST *ll, int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
572{
573 struct s_emm_filter *filter;
574 LL_ITER itr;
575
576 if(ll_count(ll) > 0)
577 {
578 itr = ll_iter_create(ll);
579 while((filter = ll_iter_next(&itr)))
580 {
581 if(filter->demux_id == demux_id && filter->caid == caid && filter->provid == provid && filter->pid == pid && filter->num == num)
582 {
583 ll_iter_remove_data(&itr);
584 return 1;
585 }
586 }
587 }
588 return 0;
589}
590
591void remove_emmfilter_from_list(int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
592{
593 if((ll_emm_active_filter && remove_emmfilter_from_list_internal(ll_emm_active_filter, demux_id, caid, provid, pid, num))
594 || (ll_emm_inactive_filter && remove_emmfilter_from_list_internal(ll_emm_inactive_filter, demux_id, caid, provid, pid, num))
595 || (ll_emm_pending_filter && remove_emmfilter_from_list_internal(ll_emm_pending_filter, demux_id, caid, provid, pid, num)))
596 {
597 return;
598 }
599}
600
601void dvbapi_net_add_str(uint8_t *packet, int *size, const char *str)
602{
603 uint8_t *str_len = &packet[*size]; // string length
604 *size += 1;
605 *str_len = snprintf((char *) &packet[*size], DVBAPI_MAX_PACKET_SIZE - *size, "%s", str);
606 *size += *str_len;
607}
608
609int32_t dvbapi_net_send(uint32_t request, int32_t socket_fd, uint32_t msgid, int32_t demux_id,
610 uint32_t filter_number, uint8_t *data, struct s_client *client, ECM_REQUEST *er, uint16_t client_proto_version)
611{
612 uint8_t packet[DVBAPI_MAX_PACKET_SIZE]; // maximum possible packet size
613 int32_t size = 0;
614 uint32_t u32;
615
616 // not connected?
617 if(socket_fd <= 0)
618 {
619 return 0;
620 }
621
622 // preparing packet - header
623 // in old protocol client expect this first byte as adapter index,
624 // changed in the new protocol to be always after request type (opcode)
625 if(client_proto_version <= 0)
626 {
627 packet[size++] = demux[demux_id].adapter_index; // adapter index - 1 byte
628 }
629 else if(client_proto_version >= 3)
630 {
631 packet[size++] = 0xa5; // message start
632 u32 = htonl(msgid);
633 memcpy(&packet[size], &u32, 4);
634 size += 4;
635 }
636
637 // type of request
638 u32 = request;
639 if(client_proto_version >= 1)
640 {
641 u32 = htonl(u32);
642 }
643 memcpy(&packet[size], &u32, 4);
644 size += 4;
645
646 // preparing packet - adapter index for proto >= 1
647 if((request != DVBAPI_SERVER_INFO) && client_proto_version >= 1)
648 {
649 packet[size++] = demux[demux_id].adapter_index; // adapter index - 1 byte
650 }
651
652 // struct with data
653 switch(request)
654 {
655 case DVBAPI_SERVER_INFO:
656 {
657 int16_t proto_version = htons(DVBAPI_PROTOCOL_VERSION); // our protocol version
658 char capabilities[128];
659 memset(capabilities, 0, sizeof(capabilities));
660 memcpy(&packet[size], &proto_version, 2);
661 size += 2;
662 uint8_t *info_len = &packet[size]; // info string length
663 size += 1;
664
665 if(cfg.dvbapi_extended_cw_api == 1)
666 {
667 cs_strncat(capabilities, ",e1mk", sizeof(capabilities)); // extended cw, key follows mode - supports CSA, DES, AES128
668 }
669
670 if(cfg.dvbapi_extended_cw_api == 2)
671 {
672 cs_strncat(capabilities, ",e2", sizeof(capabilities)); // usage of DES algo signalled through PID index - CSA and DES only
673 }
674
675 *info_len = snprintf((char *) &packet[size], sizeof(packet) - size, "OSCam v%s, build r%s (%s); %s",
676 CS_VERSION, CS_SVN_VERSION, CS_TARGET, capabilities + 1);
677
678 size += *info_len;
679 break;
680 }
681
682 case DVBAPI_ECM_INFO:
683 {
684 if(er->rc >= E_NOTFOUND)
685 {
686 return 0;
687 }
688
689 int8_t hops = 0;
690
691 uint16_t sid = htons(er->srvid); // service ID (program number)
692 memcpy(&packet[size], &sid, 2);
693 size += 2;
694
695 uint16_t caid = htons(er->caid); // CAID
696 memcpy(&packet[size], &caid, 2);
697 size += 2;
698
699 uint16_t pid = htons(er->pid); // PID
700 memcpy(&packet[size], &pid, 2);
701 size += 2;
702
703 uint32_t prid = htonl(er->prid); // Provider ID
704 memcpy(&packet[size], &prid, 4);
705 size += 4;
706
707 uint32_t ecmtime = htonl(client->cwlastresptime); // ECM time
708 memcpy(&packet[size], &ecmtime, 4);
709 size += 4;
710
711 dvbapi_net_add_str(packet, &size, get_cardsystem_desc_by_caid(er->caid)); // cardsystem name
712
713 switch (er->rc)
714 {
715 case E_FOUND:
716 if(er->selected_reader)
717 {
718 dvbapi_net_add_str(packet, &size, er->selected_reader->label); // reader
719 if(is_network_reader(er->selected_reader))
720 {
721 dvbapi_net_add_str(packet, &size, er->selected_reader->device); // from
722 }
723 else
724 {
725 dvbapi_net_add_str(packet, &size, "local"); // from
726 }
727 dvbapi_net_add_str(packet, &size, reader_get_type_desc(er->selected_reader, 1)); // protocol
728 hops = er->selected_reader->currenthops;
729 }
730 break;
731
732 case E_CACHE1:
733 dvbapi_net_add_str(packet, &size, "Cache"); // reader
734 dvbapi_net_add_str(packet, &size, "cache1"); // from
735 dvbapi_net_add_str(packet, &size, "none"); // protocol
736 break;
737
738 case E_CACHE2:
739 dvbapi_net_add_str(packet, &size, "Cache"); // reader
740 dvbapi_net_add_str(packet, &size, "cache2"); // from
741 dvbapi_net_add_str(packet, &size, "none"); // protocol
742 break;
743
744 case E_CACHEEX:
745 dvbapi_net_add_str(packet, &size, "Cache"); // reader
746 dvbapi_net_add_str(packet, &size, "cache3"); // from
747 dvbapi_net_add_str(packet, &size, "none"); // protocol
748 break;
749 }
750 packet[size++] = hops; // hops
751 break;
752 }
753
754 case DVBAPI_CA_GET_DESCR_INFO:
755 {
756 int sct_cadescr_info_size = sizeof(ca_descr_info_t);
757 if(client_proto_version >= 1)
758 {
759 ca_descr_info_t *cadescr_info = (ca_descr_info_t *) data;
760 cadescr_info->num = htonl(cadescr_info->num);
761 cadescr_info->type = htonl(cadescr_info->type);
762 }
763 memcpy(&packet[size], data, sct_cadescr_info_size);
764 size += sct_cadescr_info_size;
765 break;
766 }
767
768 case DVBAPI_CA_SET_DESCR:
769 {
770 int sct_cadescr_size = sizeof(ca_descr_t);
771 if(client_proto_version >= 1)
772 {
773 ca_descr_t *cadesc = (ca_descr_t *) data;
774 cadesc->index = htonl(cadesc->index);
775 cadesc->parity = htonl(cadesc->parity);
776 }
777 memcpy(&packet[size], data, sct_cadescr_size);
778 size += sct_cadescr_size;
779 break;
780 }
781
782 case DVBAPI_CA_SET_PID:
783 {
784 int sct_capid_size = sizeof(ca_pid_t);
785 if(client_proto_version >= 1)
786 {
787 ca_pid_t *capid = (ca_pid_t *) data;
788 capid->pid = htonl(capid->pid);
789 capid->index = htonl(capid->index);
790 }
791 memcpy(&packet[size], data, sct_capid_size);
792 size += sct_capid_size;
793 break;
794 }
795
796 case DVBAPI_CA_SET_DESCR_MODE:
797 {
798 int sct_cadescr_mode_size = sizeof(ca_descr_mode_t);
799 if(client_proto_version >= 1)
800 {
801 ca_descr_mode_t *cadesc_mode = (ca_descr_mode_t *) data;
802 cadesc_mode->index = htonl(cadesc_mode->index);
803 cadesc_mode->algo = htonl(cadesc_mode->algo);
804 cadesc_mode->cipher_mode = htonl(cadesc_mode->cipher_mode);
805 }
806 memcpy(&packet[size], data, sct_cadescr_mode_size);
807 size += sct_cadescr_mode_size;
808 break;
809 }
810
811 case DVBAPI_CA_SET_DESCR_DATA:
812 {
813 ca_descr_data_t *cadesc_data = (ca_descr_data_t *) data;
814 uint32_t length = cadesc_data->length;
815 if(client_proto_version >= 1)
816 {
817 cadesc_data->index = htonl(cadesc_data->index);
818 cadesc_data->parity = htonl(cadesc_data->parity);
819 cadesc_data->data_type = htonl(cadesc_data->data_type);
820 cadesc_data->length = htonl(cadesc_data->length);
821 }
822 memcpy(&packet[size], &cadesc_data->index, 4);
823 memcpy(&packet[size + 4], &cadesc_data->parity, 4);
824 memcpy(&packet[size + 8], &cadesc_data->data_type, 4);
825 memcpy(&packet[size + 12], &cadesc_data->length, 4);
826 memcpy(&packet[size + 16], cadesc_data->data, length);
827 size += 16 + length;
828 break;
829 }
830
831 case DVBAPI_DMX_SET_FILTER:
832 case DVBAPI_DMX_STOP:
833 {
834 int32_t sct_filter_size = sizeof(struct dmx_sct_filter_params);
835 packet[size++] = demux_id; // demux id - 1 byte
836 packet[size++] = filter_number; // filter number - 1 byte
837 if(data) // filter data when starting
838 {
839 if(client_proto_version >= 1)
840 {
841 struct dmx_sct_filter_params *fp = (struct dmx_sct_filter_params *)data;
842 // adding all dmx_sct_filter_params structure fields
843 // one by one to avoid padding problems
844 uint16_t pid = htons(fp->pid);
845 memcpy(&packet[size], &pid, 2);
846 size += 2;
847 memcpy(&packet[size], fp->filter.filter, 16);
848 size += 16;
849 memcpy(&packet[size], fp->filter.mask, 16);
850 size += 16;
851 memcpy(&packet[size], fp->filter.mode, 16);
852 size += 16;
853 uint32_t timeout = htonl(fp->timeout);
854 memcpy(&packet[size], &timeout, 4);
855 size += 4;
856 uint32_t flags = htonl(fp->flags);
857 memcpy(&packet[size], &flags, 4);
858 size += 4;
859 }
860 else
861 {
862 memcpy(&packet[size], data, sct_filter_size); // dmx_sct_filter_params struct
863 size += sct_filter_size;
864 }
865 }
866 else // pid when stopping
867 {
868 if(client_proto_version >= 1)
869 {
870 uint16_t pid = htons(demux[demux_id].demux_fd[filter_number].pid);
871 memcpy(&packet[size], &pid, 2);
872 size += 2;
873 }
874 else
875 {
876 uint16_t pid = demux[demux_id].demux_fd[filter_number].pid;
877 packet[size++] = pid >> 8;
878 packet[size++] = pid & 0xff;
879 }
880 }
881 break;
882 }
883
884 default: // unknown request
885 {
886 cs_log("ERROR: dvbapi_net_send: invalid request");
887 return 0;
888 }
889 }
890 // sending
891 cs_log_dump_dbg(D_DVBAPI, packet, size, "Sending packet to dvbapi client (fd=%d):", socket_fd);
892 send(socket_fd, &packet, size, MSG_DONTWAIT);
893 // always returning success as the client could close socket
894 return 0;
895}
896
897int32_t dvbapi_set_filter(int32_t demux_id, int32_t api, uint16_t pid, uint16_t caid, uint32_t provid,
898 uint8_t *filt, uint8_t *mask, int32_t timeout, int32_t pidindex, int32_t type, int8_t add_to_emm_list)
899{
900 int32_t ret = -1, n = -1, i, filterfd = -1;
901
902 for(i = 0; i < maxfilter && demux[demux_id].demux_fd[i].fd > 0; i++) { ; }
903
904 if(i >= maxfilter)
905 {
906 cs_log_dbg(D_DVBAPI, "no free filter");
907 return -1;
908 }
909 n = i;
910
911 if(USE_OPENXCAS)
912 {
913 if(type == TYPE_ECM)
914 {
915 openxcas_set_caid(demux[demux_id].ECMpids[pidindex].CAID);
916 openxcas_set_ecm_pid(pid);
917 }
918
919 demux[demux_id].demux_fd[n].fd = DUMMY_FD;
920 demux[demux_id].demux_fd[n].pidindex = pidindex;
921 demux[demux_id].demux_fd[n].pid = pid;
922 demux[demux_id].demux_fd[n].caid = caid;
923 demux[demux_id].demux_fd[n].provid = provid;
924 demux[demux_id].demux_fd[n].type = type;
925 memcpy(demux[demux_id].demux_fd[n].filter, filt, 16); // copy filter to check later on if receiver delivered accordingly
926 memcpy(demux[demux_id].demux_fd[n].mask, mask, 16); // copy mask to check later on if receiver delivered accordingly
927 return 1;
928 }
929
930 switch(api)
931 {
932 case DVBAPI_3:
933 {
934 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
935 {
936 ret = filterfd = DUMMY_FD;
937 }
938 else
939 {
940 ret = filterfd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
941 }
942
943 if(ret < 0)
944 {
945 return ret; // return if device cant be opened!
946 }
947
948 struct dmx_sct_filter_params sFP2;
949 memset(&sFP2, 0, sizeof(sFP2));
950 sFP2.pid = pid;
951 sFP2.timeout = timeout;
952 sFP2.flags = DMX_IMMEDIATE_START;
953
954 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
955 {
956 //DeepThought: on dgs/cubestation and neumo images, perhaps others
957 //the following code is needed to descramble
958 sFP2.filter.filter[0] = filt[0];
959 sFP2.filter.mask[0] = mask[0];
960 sFP2.filter.filter[1] = 0;
961 sFP2.filter.mask[1] = 0;
962 sFP2.filter.filter[2] = 0;
963 sFP2.filter.mask[2] = 0;
964 memcpy(sFP2.filter.filter + 3, filt + 1, 16 - 3);
965 memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
966
967 //DeepThought: in the drivers of the dgs/cubestation and neumo images,
968 //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
969 //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
970 //due to a bug its sets the "positive mask" wrongly (they should be all 0).
971 //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
972 //structure, which is incorrect (it should be dmxSctFilterParams).
973 //The only way to get it right is to call DMX_SET_FILTER1 with the argument
974 //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
975 ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER1, &sFP2);
976 }
977 else
978 {
979 memcpy(sFP2.filter.filter, filt, 16);
980 memcpy(sFP2.filter.mask, mask, 16);
981 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
982 {
983 ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER,
984 demux[demux_id].socket_fd,
985 0,
986 demux_id,
987 n,
988 (uint8_t *) &sFP2,
989 NULL,
990 NULL,
991 demux[demux_id].client_proto_version);
992 }
993 else
994 {
995 ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER, &sFP2);
996 }
997 }
998 break;
999 }
1000
1001 case DVBAPI_1:
1002 {
1003 ret = filterfd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
1004 if(ret < 0) { return ret; } // return if device cant be opened!
1005
1006 struct dmxSctFilterParams sFP1;
1007 memset(&sFP1, 0, sizeof(sFP1));
1008 sFP1.pid = pid;
1009 sFP1.timeout = timeout;
1010 sFP1.flags = DMX_IMMEDIATE_START;
1011 memcpy(sFP1.filter.filter, filt, 16);
1012 memcpy(sFP1.filter.mask, mask, 16);
1013 ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER1, &sFP1);
1014 break;
1015 }
1016
1017#if defined(WITH_STAPI) || defined(WITH_STAPI5)
1018 case STAPI:
1019 {
1020 ret = filterfd = stapi_set_filter(demux_id, pid, filt, mask, n, demux[demux_id].pmt_file);
1021 if(ret <= 0)
1022 {
1023 ret = -1; // error setting filter!
1024 }
1025 break;
1026 }
1027#endif
1028
1029#if defined WITH_COOLAPI || defined WITH_COOLAPI2
1030 case COOLAPI:
1031 {
1032 ret = filterfd = coolapi_open_device(demux[demux_id].demux_index, demux_id);
1033 if(ret > 0)
1034 {
1035 ret = coolapi_set_filter(filterfd, n, pid, filt, mask, type);
1036 }
1037 else
1038 {
1039 ret = -1; // fail
1040 }
1041 break;
1042 }
1043#endif
1044
1045 default:
1046 break;
1047 }
1048
1049 if(ret != -1) // filter set successful
1050 {
1051 // only register if filter was set successful
1052 demux[demux_id].demux_fd[n].fd = filterfd;
1053 demux[demux_id].demux_fd[n].pidindex = pidindex;
1054 demux[demux_id].demux_fd[n].pid = pid;
1055 demux[demux_id].demux_fd[n].caid = caid;
1056 demux[demux_id].demux_fd[n].provid = provid;
1057 demux[demux_id].demux_fd[n].type = type;
1058
1059 // copy filter and mask to check later on if receiver delivered accordingly
1060 memcpy(demux[demux_id].demux_fd[n].filter, filt, 16);
1061 memcpy(demux[demux_id].demux_fd[n].mask, mask, 16);
1062
1063 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d started successfully (caid %04X provid %06X pid %04X)",
1064 demux_id, n + 1, caid, provid, pid);
1065
1066 if(type == TYPE_EMM && add_to_emm_list)
1067 {
1068 add_emmfilter_to_list(demux_id, filt, caid, provid, pid, n + 1, true);
1069 }
1070 }
1071 else
1072 {
1073 cs_log("ERROR: Could not start demux filter (api: %d errno=%d %s)", selected_api, errno, strerror(errno));
1074 }
1075 return ret;
1076}
1077
1078/*
1079 * Get number of available descramblers from box. On success, it stores result on
1080 * global variable "ca_descramblers_total". On failure, it uses a safe default value.
1081 */
1082static int32_t dvbapi_get_descrambler_info(void)
1083{
1084 // In enigma2 all ca devices are listed under adapter0. In addition we only
1085 // need to ask one ca device to get the total number of descramblers. In
1086 // PC installations, there are no ca devices, so we use a predefined value.
1087
1088 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
1089 {
1090 ca_descramblers_total = INDEX_MAX_NET;
1091 return 1; // nothing else to do for PCs
1092 }
1093
1094 int32_t fd = 0, ca_offset = 0;
1095 char device_path[128], device_path2[128];
1096 struct ca_descr_info descr_info;
1097 memset(&descr_info, 0, sizeof(descr_info));
1098
1099 // Use a safe default in case we fail to get the exact number
1100 ca_descramblers_total = INDEX_MAX_LOCAL;
1101
1102 if(cfg.dvbapi_boxtype == BOXTYPE_DUCKBOX ||
1103 cfg.dvbapi_boxtype == BOXTYPE_DBOX2 ||
1104 cfg.dvbapi_boxtype == BOXTYPE_UFS910)
1105 {
1106 ca_offset = 1;
1107 }
1108
1109 // Ask device for exact number of ca descramblers
1110 snprintf(device_path2, sizeof(device_path2), devices[selected_box].ca_device, ca_offset);
1111 snprintf(device_path, sizeof(device_path), devices[selected_box].path, 0);
1112
1113 if (!cs_strncat(device_path, device_path2, sizeof(device_path)))
1114 return 0;
1115
1116 if((fd = open(device_path, O_RDWR | O_NONBLOCK)) < 0)
1117 {
1118 cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
1119 return 0;
1120 }
1121
1122 if(dvbapi_ioctl(fd, CA_GET_DESCR_INFO, &descr_info) < 0)
1123 {
1124 cs_log_dbg(D_DVBAPI,"ERROR: ioctl(CA_GET_DESCR_INFO): %s", strerror(errno));
1125 }
1126
1127 if(close(fd) < 0)
1128 {
1129 cs_log("ERROR: Can't close device %s (errno=%d %s)", device_path, errno, strerror(errno));
1130 }
1131
1132 // We got a valid response from device (count the ECD type only)
1133 if(descr_info.num > 0 && (descr_info.type & 1))
1134 {
1135 ca_descramblers_total = descr_info.num;
1136 cs_log("Detected %s device, total available descramblers: %d", device_path, ca_descramblers_total);
1137 return 1;
1138 }
1139
1140 return 0;
1141}
1142
1143static int32_t dvbapi_detect_api(void)
1144{
1145#if defined WITH_COOLAPI || defined WITH_COOLAPI2
1146 selected_api = COOLAPI;
1147 selected_box = BOX_INDEX_COOLSTREAM;
1148 disable_pmt_files = 1;
1149 cfg.dvbapi_listenport = 0;
1150 cs_log("Detected Coolstream API");
1151 return 1;
1152#else
1153 if(cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_PC)
1154 {
1155 selected_api = DVBAPI_3;
1156 selected_box = BOX_INDEX_DREAMBOX_DVBAPI3;
1157 if(cfg.dvbapi_listenport)
1158 {
1159 cs_log("Using TCP listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d",
1160 selected_api, cfg.dvbapi_boxtype);
1161 }
1162 else
1163 {
1164 cs_log("Using %s listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d",
1165 devices[selected_box].cam_socket_path, selected_api, cfg.dvbapi_boxtype);
1166 }
1167 return 1;
1168 }
1169 else if(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
1170 {
1171 selected_api = DVBAPI_3;
1172 selected_box = BOX_INDEX_QBOXHD;
1173 cfg.dvbapi_listenport = 0;
1174 disable_pmt_files = 1;
1175 cs_log("Using SamyGO dvbapi v0.1");
1176 return 1;
1177 }
1178 else
1179 {
1180 cfg.dvbapi_listenport = 0;
1181 }
1182
1183 int32_t i = 0, n = 0, devnum = -1, dmx_fd = 0, filtercount = 0;
1184 char device_path[128], device_path2[128];
1185 static LLIST *ll_max_fd;
1186 ll_max_fd = ll_create("ll_max_fd");
1187 LL_ITER itr;
1188
1189 struct s_open_fd
1190 {
1191 uint32_t fd;
1192 };
1193 struct s_open_fd *open_fd;
1194
1195 while(i < BOX_COUNT)
1196 {
1197 do
1198 {
1199 snprintf(device_path2, sizeof(device_path2), devices[i].demux_device, 0);
1200 snprintf(device_path, sizeof(device_path), devices[i].path, n);
1201
1202 filtercount = 0;
1203
1204 if (cs_strncat(device_path, device_path2, sizeof(device_path)))
1205 {
1206 while((dmx_fd = open(device_path, O_RDWR | O_NONBLOCK)) > 0 && filtercount < MAX_FILTER)
1207 {
1208 filtercount++;
1209 if(!cs_malloc(&open_fd, sizeof(struct s_open_fd)))
1210 {
1211 close(dmx_fd);
1212 break;
1213 }
1214 open_fd->fd = dmx_fd;
1215 ll_append(ll_max_fd, open_fd);
1216 }
1217 }
1218
1219 if(filtercount > 0)
1220 {
1221 itr = ll_iter_create(ll_max_fd);
1222 while((open_fd = ll_iter_next(&itr)))
1223 {
1224 dmx_fd = open_fd->fd;
1225 do
1226 {
1227 ;
1228 }
1229 while(close(dmx_fd) < 0);
1230 ll_iter_remove_data(&itr);
1231 }
1232 devnum = i;
1233 selected_api = devices[devnum].api;
1234 selected_box = devnum;
1235
1236 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
1237 {
1238 selected_api = DVBAPI_3; //DeepThought
1239 }
1240
1241#if defined(WITH_STAPI) || defined(WITH_STAPI5)
1242 if(selected_api == STAPI && stapi_open() == 0)
1243 {
1244 cs_log("ERROR: stapi: setting up stapi failed.");
1245 return 0;
1246 }
1247#endif
1248 maxfilter = filtercount;
1249 cs_log("Detected %s Api: %d, userconfig boxtype: %d maximum number of filters is %d (oscam limit is %d)",
1250 device_path, selected_api, cfg.dvbapi_boxtype, filtercount, MAX_FILTER);
1251 }
1252
1253 // try at least 8 adapters
1254 if((strchr(devices[i].path, '%') != NULL) && (n < 8))
1255 {
1256 n++;
1257 }
1258 else
1259 {
1260 n = 0; i++;
1261 }
1262
1263 } while(n != 0); // n is set to 0 again if 8 adapters are tried!
1264
1265 if(devnum != -1)
1266 {
1267 break; // check if box detected
1268 }
1269 }
1270
1271 ll_destroy(&ll_max_fd);
1272 if(devnum == -1)
1273 {
1274 return 0;
1275 }
1276#endif
1277
1278 return 1;
1279}
1280
1281static int32_t dvbapi_read_device(int32_t dmx_fd, uint8_t *buf, uint32_t length)
1282{
1283 int32_t readed;
1284 uint32_t count = 0;
1285 struct pollfd pfd[1];
1286
1287 pfd[0].fd = dmx_fd;
1288 pfd[0].events = (POLLIN | POLLPRI);
1289
1290 while(count < length)
1291 {
1292 if(poll(pfd, 1, 0)) // fd ready for reading?
1293 {
1294 if(pfd[0].revents & (POLLIN | POLLPRI)) // is there data to read?
1295 {
1296 readed = read(dmx_fd, &buf[count], length-count);
1297 if(readed < 0) // error occured while reading
1298 {
1299 if(errno == EINTR || errno == EAGAIN) // try again in case of interrupt
1300 {
1301 continue;
1302 }
1303
1304 cs_log("ERROR: Read error on fd %d (errno=%d %s)", dmx_fd, errno, strerror(errno));
1305 return (errno == EOVERFLOW ? 0 : -1);
1306 }
1307
1308 if(readed > 0) // successful read
1309 {
1310 count += readed;
1311 }
1312
1313 if(readed == 0 && count > 0) // nothing to read left
1314 {
1315 break;
1316 }
1317 }
1318 else
1319 {
1320 return -1; // other events than pollin/pri means bad news -> abort!
1321 }
1322 }
1323 else
1324 {
1325 break;
1326 }
1327 }
1328 cs_log_dump_dbg(D_TRACE, buf, count, "Received:");
1329 return count;
1330}
1331
1332int32_t dvbapi_open_device(int32_t type, int32_t num, int32_t adapter)
1333{
1334 int32_t dmx_fd, ret;
1335 int32_t ca_offset = 0;
1336 char device_path[128], device_path2[128];
1337
1338 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
1339 {
1340 return DUMMY_FD;
1341 }
1342
1343 if(type == 0)
1344 {
1345 snprintf(device_path2, sizeof(device_path2), devices[selected_box].demux_device, num);
1346 snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
1347
1348 if (!cs_strncat(device_path, device_path2, sizeof(device_path)))
1349 return -1;
1350
1351 }
1352 else
1353 {
1354 if(cfg.dvbapi_boxtype == BOXTYPE_DUCKBOX
1355 || cfg.dvbapi_boxtype == BOXTYPE_DBOX2
1356 || cfg.dvbapi_boxtype == BOXTYPE_UFS910)
1357 {
1358 ca_offset = 1;
1359 }
1360
1361 if(cfg.dvbapi_boxtype == BOXTYPE_QBOXHD
1362 || cfg.dvbapi_boxtype == BOXTYPE_PC
1363 || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
1364 {
1365 num = 0;
1366 }
1367
1368 snprintf(device_path2, sizeof(device_path2), devices[selected_box].ca_device, num + ca_offset);
1369 snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
1370
1371 if (!cs_strncat(device_path, device_path2, sizeof(device_path)))
1372 return -1;
1373
1374 }
1375
1376 if(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
1377 {
1378 if(type == 0)
1379 {
1380 struct sockaddr_un saddr;
1381 memset(&saddr, 0, sizeof(saddr));
1382 saddr.sun_family = AF_UNIX;
1383 cs_strncpy(saddr.sun_path, device_path, sizeof(saddr.sun_path));
1384 dmx_fd = socket(AF_UNIX, SOCK_STREAM, 0);
1385 ret = connect(dmx_fd, (struct sockaddr *)&saddr, sizeof(saddr));
1386 if(ret < 0)
1387 {
1388 close(dmx_fd);
1389 }
1390 }
1391 else if(type == 1)
1392 {
1393 int32_t udp_port = 9000;
1394 struct sockaddr_in saddr;
1395 memset(&saddr, 0, sizeof(saddr));
1396 saddr.sin_family = AF_INET;
1397 saddr.sin_port = htons(udp_port + adapter);
1398 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1399 dmx_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
1400 set_nonblock(dmx_fd, true);
1401 ret = connect(dmx_fd, (struct sockaddr *) &saddr, sizeof(saddr));
1402 if(ret < 0)
1403 {
1404 close(dmx_fd);
1405 }
1406 cs_log_dbg(D_DVBAPI, "NET DEVICE open (port = %d) fd %d", udp_port + adapter, dmx_fd);
1407 }
1408 else
1409 {
1410 ret = -1;
1411 }
1412 }
1413 else
1414 {
1415 dmx_fd = ret = open(device_path, O_RDWR | O_NONBLOCK);
1416 }
1417
1418 if(ret < 0)
1419 {
1420 cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
1421 return -1;
1422 }
1423 cs_log_dbg(D_DVBAPI, "Open device %s (fd %d)", device_path, dmx_fd);
1424 return dmx_fd;
1425}
1426
1427uint16_t tunemm_caid_map(uint8_t direct, uint16_t caid, uint16_t srvid)
1428{
1429 int32_t i;
1430 struct s_client *cl = cur_client();
1431 TUNTAB *ttab = &cl->ttab;
1432
1433 if(!ttab->ttnum)
1434 {
1435 return caid;
1436 }
1437
1438 if(direct)
1439 {
1440 for(i = 0; i < ttab->ttnum; i++)
1441 {
1442 if(caid == ttab->ttdata[i].bt_caidto &&
1443 (srvid == ttab->ttdata[i].bt_srvid || ttab->ttdata[i].bt_srvid == 0xFFFF || !ttab->ttdata[i].bt_srvid))
1444 {
1445 return ttab->ttdata[i].bt_caidfrom;
1446 }
1447 }
1448 }
1449 else
1450 {
1451 for(i = 0; i < ttab->ttnum; i++)
1452 {
1453 if(caid == ttab->ttdata[i].bt_caidfrom &&
1454 (srvid == ttab->ttdata[i].bt_srvid || ttab->ttdata[i].bt_srvid == 0xFFFF || !ttab->ttdata[i].bt_srvid))
1455 {
1456 return ttab->ttdata[i].bt_caidto;
1457 }
1458 }
1459 }
1460 return caid;
1461}
1462
1463int32_t dvbapi_stop_filter(int32_t demux_id, int32_t type, uint32_t msgid)
1464{
1465#if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
1466 // We prevented PAT and PMT from starting, so lets don't close them either.
1467 if(type == TYPE_PAT || type == TYPE_PMT)
1468 {
1469 return 1;
1470 }
1471#endif
1472
1473 int32_t g, error = 0;
1474
1475 // just stop them all, we don't want to risk leaving
1476 // any stale filter running due to lowering of maxfilters
1477 for(g = 0; g < MAX_FILTER; g++)
1478 {
1479 if(demux[demux_id].demux_fd[g].type == type)
1480 {
1481 if(dvbapi_stop_filternum(demux_id, g, msgid) == -1)
1482 {
1483 error = 1;
1484 }
1485 }
1486 }
1487 return !error; // on error return 0, all ok 1
1488}
1489
1490int32_t dvbapi_stop_filternum(int32_t demux_id, int32_t num, uint32_t msgid)
1491{
1492 int32_t retfilter = -1, retfd = -1, fd = demux[demux_id].demux_fd[num].fd, try = 0;
1493
1494 if(USE_OPENXCAS)
1495 {
1496 demux[demux_id].demux_fd[num].type = 0;
1497 demux[demux_id].demux_fd[num].fd = 0;
1498 return 1; // all ok!
1499 }
1500
1501 if(fd > 0)
1502 {
1503 do
1504 {
1505 errno = 0;
1506 if(try)
1507 {
1508 cs_sleepms(50);
1509 }
1510 try++;
1511
1512 cs_log_dbg(D_DVBAPI, "Demuxer %d stop filter %d try %d (fd: %d api: %d, caid: %04X, provid: %06X, %spid: %04X)",
1513 demux_id,
1514 num + 1,
1515 try,
1516 fd,
1517 selected_api,
1518 demux[demux_id].demux_fd[num].caid,
1519 demux[demux_id].demux_fd[num].provid,
1520 demux[demux_id].demux_fd[num].type == TYPE_ECM ? "ecm" : "emm",
1521 demux[demux_id].demux_fd[num].pid);
1522
1523 switch(selected_api)
1524 {
1525 case DVBAPI_3:
1526 {
1527 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
1528 {
1529 retfilter = dvbapi_net_send(DVBAPI_DMX_STOP,
1530 demux[demux_id].socket_fd,
1531 msgid,
1532 demux_id,
1533 num,
1534 NULL,
1535 NULL,
1536 NULL,
1537 demux[demux_id].client_proto_version);
1538 }
1539 else
1540 {
1541 retfilter = dvbapi_ioctl(fd, DMX_STOP, NULL);
1542 }
1543 break;
1544 }
1545
1546 case DVBAPI_1:
1547 {
1548 retfilter = dvbapi_ioctl(fd, DMX_STOP, NULL);
1549 break;
1550 }
1551
1552#if defined(WITH_STAPI) || defined(WITH_STAPI5)
1553 case STAPI:
1554 {
1555 retfilter = stapi_remove_filter(demux_id, num, demux[demux_id].pmt_file);
1556 if(retfilter != 1) // stapi returns 0 for error, 1 for all ok
1557 {
1558 retfilter = -1;
1559 }
1560 break;
1561 }
1562#endif
1563#if defined WITH_COOLAPI || defined WITH_COOLAPI2
1564 case COOLAPI:
1565 {
1566 retfilter = coolapi_remove_filter(fd, num);
1567 if(retfilter >= 0)
1568 {
1569 retfd = coolapi_close_device(fd);
1570 }
1571 break;
1572 }
1573#endif
1574 default:
1575 break;
1576 }
1577
1578 if(errno == 9) // no error on bad file descriptor
1579 {
1580 retfilter = 0;
1581 }
1582 } while(retfilter < 0 && try < 10);
1583
1584#if !defined WITH_COOLAPI && !defined WITH_COOLAPI2 // no fd close for coolapi and stapi, all others do close fd!
1585 try = 0;
1586 do
1587 {
1588 if(try)
1589 {
1590 errno = 0;
1591 cs_sleepms(50);
1592 }
1593 try++;
1594
1595 // on bad filterfd dont try to close!
1596 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX && errno != 9)
1597 {
1598 if(selected_api == STAPI)
1599 {
1600 retfd = 0; // stapi closes its own filter fd!
1601 }
1602 else
1603 {
1604 // flush filter input buffer in attempt to avoid overflow receivers internal buffer
1605 flush_read_fd(demux_id, num, fd);
1606 retfd = close(fd);
1607 if(errno == 9)
1608 {
1609 retfd = 0; // no error on bad file descriptor
1610 }
1611 }
1612 }
1613 else
1614 {
1615 retfd = 0;
1616 }
1617
1618 } while(retfd < 0 && try < 10);
1619#endif
1620 }
1621 else // fd <=0
1622 {
1623 return 1; // filter was already killed!
1624 }
1625
1626 if(retfilter < 0) // error on remove filter
1627 {
1628 cs_log("ERROR: Demuxer %d could not stop Filter %d (fd:%d api:%d errno=%d %s)",
1629 demux_id, num + 1, fd, selected_api, errno, strerror(errno));
1630 return retfilter;
1631 }
1632
1633 if(retfd < 0) // error on close filter fd
1634 {
1635 cs_log("ERROR: Demuxer %d could not close fd of Filter %d (fd=%d api:%d errno=%d %s)",
1636 demux_id, num + 1, fd, selected_api, errno, strerror(errno));
1637 return retfd;
1638 }
1639
1640 // code below runs only if nothing has gone wrong
1641 if(demux[demux_id].demux_fd[num].type == TYPE_ECM) // ecm filter stopped: reset index!
1642 {
1643 int32_t oldpid = demux[demux_id].demux_fd[num].pidindex;
1644 int32_t curpid = demux[demux_id].pidindex;
1645
1646 // workaround: below dont run on stapi since it handles it own pids...
1647 // stapi need to be better integrated in oscam dvbapi.
1648 if(selected_api != STAPI)
1649 {
1650 int32_t z;
1651 for(z = 0; z < MAX_STREAM_INDICES; z++)
1652 {
1653 uint32_t idx = demux[demux_id].ECMpids[oldpid].index[z];
1654 demux[demux_id].ECMpids[oldpid].index[z] = INDEX_INVALID;
1655
1656 if(idx != INDEX_INVALID) // if in use
1657 {
1658 int32_t i;
1659 for(i = 0; i < demux[demux_id].STREAMpidcount; i++)
1660 {
1661 int8_t match = 0;
1662
1663 // check streams of old disabled ecmpid
1664 if(!demux[demux_id].ECMpids[oldpid].streams
1665 || ((demux[demux_id].ECMpids[oldpid].streams & (1 << i)) == (uint) (1 << i)))
1666 {
1667 // check if new ecmpid is using same streams
1668 if(curpid != -1 && (!demux[demux_id].ECMpids[curpid].streams
1669 || ((demux[demux_id].ECMpids[curpid].streams & (1 << i)) == (uint) (1 << i))))
1670 {
1671 continue; // found same stream on old and new ecmpid -> skip! (and leave it enabled!)
1672 }
1673 int32_t pidtobestopped = demux[demux_id].STREAMpids[i];
1674 int32_t j, k, otherdemuxpid;
1675 uint32_t otherdemuxidx;
1676
1677 for(j = 0; j < MAX_DEMUX; j++) // check other demuxers for same streampid with same index
1678 {
1679 if(demux[j].program_number == 0 || demux_id == j || demux[j].ca_mask != demux[demux_id].ca_mask)
1680 {
1681 continue;
1682 // skip empty demuxers
1683 // skip same demuxer
1684 // skip streampid running on other ca device
1685 }
1686
1687 otherdemuxpid = demux[j].pidindex;
1688 if(otherdemuxpid == -1)
1689 {
1690 continue; // Other demuxer not descrambling yet
1691 }
1692
1693 int32_t y;
1694 for(y = 0; y < MAX_STREAM_INDICES; y++)
1695 {
1696 otherdemuxidx = demux[j].ECMpids[otherdemuxpid].index[y];
1697 if(otherdemuxidx == INDEX_INVALID || otherdemuxidx != idx)
1698 {
1699 continue; // Other demuxer has no index yet, or index is different
1700 }
1701
1702 for(k = 0; k < demux[j].STREAMpidcount; k++)
1703 {
1704 if(!demux[j].ECMpids[otherdemuxpid].streams
1705 || ((demux[j].ECMpids[otherdemuxpid].streams & (1 << k)) == (uint) (1 << k)))
1706 {
1707 if(demux[j].STREAMpids[k] == pidtobestopped)
1708 {
1709 continue; // found same streampid enabled with same index
1710 // on one or more other demuxers -> skip! (and leave it enabled!)
1711 }
1712 }
1713 match = 1; // matching stream found
1714 }
1715 }
1716 }
1717
1718 if(!match)
1719 {
1720 for(j = 0; j < CA_MAX; j++)
1721 {
1722 if(((demux[demux_id].ca_mask & (1 << j)) == (uint32_t) (1 << j)))
1723 {
1724 remove_streampid_from_list(j, pidtobestopped, idx);
1725 break;
1726 }
1727 }
1728 }
1729 }
1730 }
1731 }
1732 }
1733 }
1734 }
1735
1736 if(demux[demux_id].demux_fd[num].type == TYPE_EMM) // If emm type remove from emm filterlist
1737 {
1738 remove_emmfilter_from_list(demux_id,
1739 demux[demux_id].demux_fd[num].caid,
1740 demux[demux_id].demux_fd[num].provid,
1741 demux[demux_id].demux_fd[num].pid,
1742 num + 1);
1743 }
1744 demux[demux_id].demux_fd[num].type = 0;
1745 demux[demux_id].demux_fd[num].fd = 0;
1746 return 1; // all ok!
1747}
1748
1749void dvbapi_start_filter(int32_t demux_id, int32_t pidindex, uint16_t pid, uint16_t caid,
1750 uint32_t provid, uint8_t table, uint8_t mask, int32_t timeout, int32_t type)
1751{
1752 int32_t i;
1753 for(i = 0; i < maxfilter; i++) // check if filter is present
1754 {
1755 if(demux[demux_id].demux_fd[i].fd > 0 &&
1756 demux[demux_id].demux_fd[i].pid == pid &&
1757 demux[demux_id].demux_fd[i].type == type &&
1758 demux[demux_id].demux_fd[i].filter[0] == table &&
1759 demux[demux_id].demux_fd[i].mask[0] == mask)
1760 {
1761 return;
1762 }
1763 }
1764
1765 uint8_t filter[32];
1766 memset(filter, 0, 32);
1767 filter[0] = table;
1768 filter[16] = mask;
1769
1770 cs_log_dbg(D_DVBAPI, "Demuxer %d try to start new filter for caid: %04X, provid: %06X, pid: %04X",
1771 demux_id, caid, provid, pid);
1772
1773 dvbapi_set_filter(demux_id, selected_api, pid, caid, provid, filter, filter + 16, timeout, pidindex, type, 0);
1774}
1775
1776void dvbapi_start_sdt_filter(int32_t demux_id)
1777{
1778 dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x0011, 0x001, 0x01, 0x42, 0xFF, 0, TYPE_SDT);
1779 demux[demux_id].sdt_filter = 0;
1780}
1781
1782void dvbapi_start_pat_filter(int32_t demux_id)
1783{
1784#if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
1785 // PAT-Filter breaks API and OSCAM for Coolstream.
1786 // Don't use it
1787 return;
1788#endif
1789 dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x0000, 0x001, 0x01, 0x00, 0xFF, 0, TYPE_PAT);
1790}
1791
1792void dvbapi_start_pmt_filter(int32_t demux_id)
1793{
1794#if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
1795 // PMT-Filter breaks API and OSCAM for Coolstream.
1796 // Don't use it
1797 return;
1798#endif
1799 uint8_t filter[16], mask[16];
1800 memset(filter, 0, 16);
1801 memset(mask, 0, 16);
1802
1803 filter[0] = 0x02;
1804 i2b_buf(2, demux[demux_id].program_number, filter + 1); // add srvid to filter since the pid can deliver pmt for multiple srvid
1805 mask[0] = 0xFF;
1806 mask[1] = 0xFF;
1807 mask[2] = 0xFF;
1808
1809 dvbapi_set_filter(demux_id, selected_api, demux[demux_id].pmtpid, 0x001, 0x01, filter, mask, 0, 0, TYPE_PMT, 0);
1810}
1811
1812void dvbapi_start_cat_filter(int32_t demux_id)
1813{
1814 dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x0001, 0x001, 0x01, 0x01, 0xFF, 0, TYPE_CAT);
1815 demux[demux_id].emm_filter = 0;
1816}
1817
1818void dvbapi_start_emm_filter(int32_t demux_id)
1819{
1820 unsigned int j;
1821 if(!demux[demux_id].EMMpidcount)
1822 {
1823 return;
1824 }
1825
1826 struct s_csystem_emm_filter *dmx_filter = NULL;
1827 unsigned int filter_count = 0;
1828 uint16_t caid, ncaid;
1829 uint32_t provid;
1830 struct s_reader *rdr = NULL;
1831 struct s_client *cl = cur_client();
1832
1833 if(!cl || !cl->aureader_list)
1834 {
1835 return;
1836 }
1837
1838 LL_ITER itr = ll_iter_create(cl->aureader_list);
1839 while((rdr = ll_iter_next(&itr)))
1840 {
1841 if(!(rdr->grp & cl->grp))
1842 {
1843 continue;
1844 }
1845
1846 if(rdr->audisabled || !rdr->enable || (!is_network_reader(rdr) && rdr->card_status != CARD_INSERTED))
1847 {
1848 continue;
1849 }
1850
1851 const struct s_cardsystem *csystem;
1852 uint16_t c, match;
1853 cs_log_dbg(D_DVBAPI, "Demuxer %d matching reader %s against available emmpids -> START!", demux_id, rdr->label);
1854
1855 for(c = 0; c < demux[demux_id].EMMpidcount; c++)
1856 {
1857 caid = ncaid = demux[demux_id].EMMpids[c].CAID;
1858 if(!caid) continue;
1859
1860 if(chk_is_betatunnel_caid(caid) == 2)
1861 {
1862 ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_id].program_number);
1863 }
1864 provid = demux[demux_id].EMMpids[c].PROVID;
1865
1866 if(caid == ncaid)
1867 {
1868 match = emm_reader_match(rdr, caid, provid);
1869 }
1870 else
1871 {
1872 match = emm_reader_match(rdr, ncaid, provid);
1873 }
1874
1875 if(match)
1876 {
1877 if(rdr->typ == R_EMU)
1878 {
1879 csystem = rdr->csystem;
1880 }
1881 else
1882 {
1883 csystem = get_cardsystem_by_caid(caid);
1884 }
1885 if(csystem)
1886 {
1887 if(caid != ncaid)
1888 {
1889 csystem = get_cardsystem_by_caid(ncaid);
1890 if(csystem && csystem->get_tunemm_filter)
1891 {
1892 csystem->get_tunemm_filter(rdr, &dmx_filter, &filter_count);
1893 cs_log_dbg(D_DVBAPI, "Demuxer %d setting emm filter for betatunnel: %04X -> %04X",
1894 demux_id, ncaid, caid);
1895 }
1896 else
1897 {
1898 cs_log_dbg(D_DVBAPI, "Demuxer %d cardsystem for emm filter for caid %04X of reader %s not found",
1899 demux_id, ncaid, rdr->label);
1900 continue;
1901 }
1902 }
1903 else if(csystem->get_emm_filter)
1904 {
1905 if(rdr->typ == R_EMU)
1906 {
1907 csystem->get_emm_filter_adv(rdr, &dmx_filter, &filter_count, caid, provid, demux[demux_id].program_number,
1908 demux[demux_id].tsid, demux[demux_id].onid, demux[demux_id].ens);
1909 }
1910 else
1911 {
1912 csystem->get_emm_filter(rdr, &dmx_filter, &filter_count);
1913 }
1914 }
1915 }
1916 else
1917 {
1918 cs_log_dbg(D_DVBAPI, "Demuxer %d cardsystem for emm filter for caid %04X of reader %s not found",
1919 demux_id, caid, rdr->label);
1920 continue;
1921 }
1922
1923 for(j = 0; j < filter_count ; j++)
1924 {
1925 if(dmx_filter[j].enabled == 0)
1926 {
1927 continue;
1928 }
1929
1930 uint8_t filter[32];
1931 memset(filter, 0, sizeof(filter)); // reset filter
1932 uint32_t usefilterbytes = 16; // default use all filters
1933 memcpy(filter, dmx_filter[j].filter, usefilterbytes);
1934 memcpy(filter + 16, dmx_filter[j].mask, usefilterbytes);
1935 int32_t emmtype = dmx_filter[j].type;
1936
1937 if(filter[0] && (((1 << (filter[0] % 0x80)) & rdr->b_nano) && !((1 << (filter[0] % 0x80)) & rdr->s_nano)))
1938 {
1939 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d blocked by userconfig -> SKIP!",
1940 demux_id, rdr->label, j + 1, filter_count);
1941 continue;
1942 }
1943
1944 if((rdr->blockemm & emmtype) && !(((1 << (filter[0] % 0x80)) & rdr->s_nano) || (rdr->saveemm & emmtype)))
1945 {
1946 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d blocked by userconfig -> SKIP!",
1947 demux_id, rdr->label, j + 1, filter_count);
1948 continue;
1949 }
1950
1951 if(demux[demux_id].EMMpids[c].type & emmtype)
1952 {
1953 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d type match -> ENABLE!",
1954 demux_id, rdr->label, j + 1, filter_count);
1955 check_add_emmpid(demux_id, filter, c, emmtype);
1956 }
1957 else
1958 {
1959 cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d type mismatch -> SKIP!",
1960 demux_id, rdr->label, j + 1, filter_count);
1961 }
1962 }
1963 // dmx_filter not use below this point;
1964 NULLFREE(dmx_filter);
1965 filter_count = 0;
1966 }
1967 }
1968 cs_log_dbg(D_DVBAPI, "Demuxer %d matching reader %s against available emmpids -> DONE!", demux_id, rdr->label);
1969 }
1970
1971 if(demux[demux_id].emm_filter == -1) // first run -1
1972 {
1973 demux[demux_id].emm_filter = 0;
1974 }
1975 cs_log_dbg(D_DVBAPI, "Demuxer %d handles %i emm filters", demux_id, demux[demux_id].emm_filter);
1976}
1977
1978void dvbapi_add_ecmpid_int(int32_t demux_id, uint16_t caid, uint16_t ecmpid, uint32_t provid, uint32_t cadata, char *txt)
1979{
1980 int32_t n, added = 0;
1981 int32_t stream = demux[demux_id].STREAMpidcount - 1;
1982
1983 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
1984 {
1985 if(demux[demux_id].ECMpids[n].CAID == caid && demux[demux_id].ECMpids[n].ECM_PID == ecmpid
1986 && (!provid || (provid && demux[demux_id].ECMpids[n].PROVID == provid)))
1987 {
1988 added = 1;
1989 if(stream > -1)
1990 {
1991 if(!demux[demux_id].ECMpids[n].streams)
1992 {
1993 // we already got this caid/ecmpid as global, no need to add the single stream
1994 cs_log_dbg(D_DVBAPI, "Demuxer %d skipped stream CAID: %04X ECM_PID: %04X PROVID: %06X (Same as ECMPID %d)",
1995 demux_id, caid, ecmpid, provid, n);
1996 continue;
1997 }
1998 demux[demux_id].ECMpids[n].streams |= (1 << stream);
1999 cs_log("Demuxer %d added stream to ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X",
2000 demux_id, n, caid, ecmpid, provid);
2001 }
2002 }
2003 }
2004
2005 if(added == 1)
2006 {
2007 return;
2008 }
2009
2010 if(demux[demux_id].ECMpidcount >= MAX_ECM_PIDS)
2011 {
2012 cs_log("We reached maximum ECMpids: unable to add to demuxer %d ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X %s",
2013 demux_id, demux[demux_id].ECMpidcount, caid, ecmpid, provid, txt);
2014 return;
2015 }
2016
2017 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].ECM_PID = ecmpid;
2018 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CAID = caid;
2019 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].PROVID = provid;
2020 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CHID = 0x10000; // reset CHID
2021 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].checked = 0;
2022 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].status = 0;
2023 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].tries = 0xFE;
2024 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].streams = 0; // reset streams!
2025 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_curindex = 0xFE; // reset
2026 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_maxindex = 0; // reset
2027 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_cycle = 0xFE; // reset
2028 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].table = 0;
2029 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].cadata = cadata;
2030
2031 cs_log("Demuxer %d ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X %s",
2032 demux_id, demux[demux_id].ECMpidcount, caid, ecmpid, provid, txt);
2033
2034 // marker to fetch emms early irdeto needs them!
2035 if(caid_is_irdeto(caid) || (caid_is_dre(caid) && (provid == 0x11 || provid == 0xFE)))
2036 {
2037 demux[demux_id].emmstart.time = 1;
2038 }
2039 demux[demux_id].ECMpidcount++;
2040}
2041
2042void dvbapi_add_ecmpid(int32_t demux_id, uint16_t caid, uint16_t ecmpid, uint32_t provid, uint32_t cadata, char *txt)
2043{
2044 dvbapi_add_ecmpid_int(demux_id, caid, ecmpid, provid, cadata, txt);
2045 struct s_dvbapi_priority *joinentry;
2046
2047 for(joinentry = dvbapi_priority; joinentry != NULL; joinentry = joinentry->next)
2048 {
2049 if((joinentry->type != 'j')
2050 || (joinentry->caid && joinentry->caid != caid)
2051 || (joinentry->provid && joinentry->provid != provid)
2052 || (joinentry->ecmpid && joinentry->ecmpid != ecmpid)
2053 || (joinentry->srvid && joinentry->srvid != demux[demux_id].program_number))
2054 {
2055 continue;
2056 }
2057
2058 cs_log_dbg(D_DVBAPI, "Join ecmpid %04X@%06X:%04X to %04X@%06X:%04X",
2059 caid, provid, ecmpid, joinentry->mapcaid, joinentry->mapprovid, joinentry->mapecmpid);
2060 dvbapi_add_ecmpid_int(demux_id, joinentry->mapcaid, joinentry->mapecmpid, joinentry->mapprovid, 0, txt);
2061 }
2062}
2063
2064void dvbapi_add_emmpid(int32_t demux_id, uint16_t caid, uint16_t emmpid, uint32_t provid, uint32_t cadata, uint8_t type)
2065{
2066 char typetext[40];
2067 char cadatatext[40];
2068 cs_strncpy(typetext, ":", sizeof(typetext));
2069
2070 if(type & 0x01) { cs_strncat(typetext, "UNIQUE:", sizeof(typetext)); }
2071 if(type & 0x02) { cs_strncat(typetext, "SHARED:", sizeof(typetext)); }
2072 if(type & 0x04) { cs_strncat(typetext, "GLOBAL:", sizeof(typetext)); }
2073 if(type & 0xF8) { cs_strncat(typetext, "UNKNOWN:", sizeof(typetext)); }
2074
2075 if(cadata > 0)
2076 {
2077 snprintf(cadatatext, 40, " CA DATA %X ", cadata);
2078 }
2079 else
2080 {
2081 cadatatext[0] = '\t'; cadatatext[1] = '\0';
2082 }
2083
2084 if(caid == 0x4AE1 && provid == 0x11 && cadata == 0)
2085 {
2086 return;
2087 }
2088
2089 uint16_t i;
2090 for(i = 0; i < demux[demux_id].EMMpidcount; i++)
2091 {
2092 if(demux[demux_id].EMMpids[i].PID == emmpid && demux[demux_id].EMMpids[i].CAID == caid &&
2093 demux[demux_id].EMMpids[i].PROVID == provid && demux[demux_id].EMMpids[i].cadata == cadata)
2094 {
2095 if(!(demux[demux_id].EMMpids[i].type & type))
2096 {
2097 demux[demux_id].EMMpids[i].type |= type; // register this emm kind to this emmpid
2098 cs_log_dbg(D_DVBAPI, "Added to existing emmpid %d additional emmtype %s",
2099 demux[demux_id].EMMpidcount - 1, typetext);
2100 }
2101 return;
2102 }
2103 }
2104
2105 if(i < MAX_EMM_PIDS)
2106 {
2107 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].PID = emmpid;
2108 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].CAID = caid;
2109 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].PROVID = provid;
2110 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].type = type;
2111 demux[demux_id].EMMpids[demux[demux_id].EMMpidcount++].cadata = cadata;
2112 cs_log_dbg(D_DVBAPI, "Added new emmpid %d CAID: %04X EMM_PID: %04X PROVID: %06X%sTYPE %s",
2113 demux[demux_id].EMMpidcount - 1, caid, emmpid, provid, cadatatext, typetext);
2114 }
2115 else
2116 {
2117 cs_log_dbg(D_DVBAPI, "We reached max emmpids: unable to add new emmpid %d CAID: %04X EMM_PID: %04X PROVID: %06X%sTYPE %s",
2118 demux[demux_id].EMMpidcount - 1, caid, emmpid, provid, cadatatext, typetext);
2119 }
2120}
2121
2122static void dvbapi_parse_cat_ca_descriptor(int32_t demux_id, const uint8_t *buffer, uint8_t descriptor_length)
2123{
2124 uint16_t i, ca_system_id, ca_pid;
2125 uint32_t ca_provider = 0, ca_data = 0;
2126
2127 if(descriptor_length < 4)
2128 {
2129 return; // CA descriptor has a minimum length of 4 bytes
2130 }
2131
2132 ca_system_id = b2i(2, buffer);
2133 ca_pid = b2i(2, buffer + 2) & 0x1FFF;
2134
2135 if(ca_system_id == 0x0000)
2136 {
2137 return; // This is not a valid CAID
2138 }
2139
2140 switch(ca_system_id >> 8)
2141 {
2142 case 0x01:
2143 {
2144 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, 0, 0, EMM_UNIQUE | EMM_GLOBAL);
2145
2146 for(i = 5; i < descriptor_length; i += 4)
2147 {
2148 ca_pid = b2i(2, buffer + i) & 0x1FFF;
2149 ca_provider = b2i(2, buffer + i + 2);
2150
2151 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_SHARED);
2152 }
2153 break;
2154 }
2155
2156 case 0x05:
2157 {
2158 for(i = 4; i < descriptor_length; i += 2 + buffer[i + 1])
2159 {
2160 if(buffer[i] == 0x14)
2161 {
2162 ca_provider = (b2i(3, buffer + i + 2) & 0xFFFFF0); // viaccess fixup: don't care about last digit
2163 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2164 }
2165 }
2166 break;
2167 }
2168
2169 case 0x18:
2170 {
2171 if(descriptor_length == 0x07 || descriptor_length == 0x0B)
2172 {
2173 for(i = 5; i < 5 + buffer[4]; i += 2)
2174 {
2175 ca_provider = b2i(2, buffer + i);
2176 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2177 }
2178 }
2179 else
2180 {
2181 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2182 }
2183 break;
2184 }
2185
2186 case 0x27:
2187 case 0x4A:
2188 {
2189 if(caid_is_bulcrypt(ca_system_id))
2190 {
2191 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, 0, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2192 break;
2193 }
2194
2195 ca_provider = buffer[4];
2196
2197 if(buffer[4] == 0xFE)
2198 {
2199 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0x102, EMM_GLOBAL);
2200 }
2201 else
2202 {
2203 if(descriptor_length == 0x0A)
2204 {
2205 ca_data = b2i(4, buffer + 6);
2206 }
2207
2208 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, ca_data, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2209 }
2210 break;
2211 }
2212
2213 default:
2214 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, 0, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2215 break;
2216 }
2217}
2218
2219static void dvbapi_parse_cat(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
2220{
2221#if defined WITH_COOLAPI || defined WITH_COOLAPI2
2222 // driver sometimes reports error if too many emm filter
2223 // but adding more ecm filter is no problem
2224 // ... so ifdef here instead of limiting MAX_FILTER
2225 demux[demux_id].max_emm_filter = 14;
2226#else
2227 if(cfg.dvbapi_requestmode == 1)
2228 {
2229 uint16_t ecm_filter_needed = 0, n;
2230 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
2231 {
2232 if(demux[demux_id].ECMpids[n].status > -1)
2233 {
2234 ecm_filter_needed++;
2235 }
2236 }
2237
2238 if(maxfilter - ecm_filter_needed <= 0)
2239 {
2240 demux[demux_id].max_emm_filter = 0;
2241 }
2242 else
2243 {
2244 demux[demux_id].max_emm_filter = maxfilter - ecm_filter_needed;
2245 }
2246 }
2247 else
2248 {
2249 demux[demux_id].max_emm_filter = maxfilter - 1;
2250 }
2251#endif
2252
2253 uint16_t i;
2254 uint8_t descriptor_tag, descriptor_length;
2255
2256 dvbapi_stop_filter(demux_id, TYPE_CAT, msgid);
2257
2258 for(i = 8; i + 1 < length; i += 2 + descriptor_length)
2259 {
2260 descriptor_tag = buffer[i];
2261 descriptor_length = buffer[i + 1];
2262
2263 if(descriptor_tag == 0x09) // There should be only CA descriptors here
2264 {
2265 dvbapi_parse_cat_ca_descriptor(demux_id, buffer + i + 2, descriptor_length);
2266 }
2267 }
2268}
2269
2270static pthread_mutex_t lockindex = PTHREAD_MUTEX_INITIALIZER;
2271
2272uint32_t dvbapi_get_desc_index(int32_t demux_id, int32_t pid, int32_t stream_id)
2273{
2274 int32_t i, j, k, fail = 1;
2275 uint32_t idx = 0;
2276
2277 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
2278 {
2279 sscanf(demux[demux_id].pmt_file, "pmt%3d.tmp", &idx);
2280 return idx;
2281 }
2282
2283 SAFE_MUTEX_LOCK(&lockindex); // to avoid race when readers become responsive!
2284 while(fail && idx <= INDEX_MAX)
2285 {
2286 fail = 0;
2287 for(i = 0; i < MAX_DEMUX && !fail && idx < INDEX_MAX; i++)
2288 {
2289 if(demux[i].program_number == 0)
2290 {
2291 continue; // skip empty demuxers
2292 }
2293
2294 if(demux[i].ca_mask != demux[demux_id].ca_mask
2295 && (!(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)))
2296 {
2297 continue; // skip demuxer using other ca device
2298 }
2299
2300 for(j = 0; j < demux[i].ECMpidcount && !fail; j++) // search for new unique index
2301 {
2302 for(k = 0; k < MAX_STREAM_INDICES; k++)
2303 {
2304 if(demux[i].ECMpids[j].index[k] == idx)
2305 {
2306 fail = 1;
2307 idx++;
2308 }
2309 }
2310 }
2311 }
2312 }
2313
2314 if(selected_api == DVBAPI_3 || selected_api == DVBAPI_1)
2315 {
2316 // make sure we haven't run out of descramblers
2317 if(ca_descramblers_used < ca_descramblers_total)
2318 {
2319 ca_descramblers_used++; // increase number of descramblers used
2320 }
2321 else
2322 {
2323 idx = INDEX_INVALID; // we don't have any descramblers left
2324 }
2325 }
2326 else // old style check for STAPI, COOLAPI, etc
2327 {
2328 if(idx > ca_descramblers_total)
2329 {
2330 idx = INDEX_INVALID; // we don't have any descramblers left
2331 }
2332 }
2333
2334 demux[demux_id].ECMpids[pid].index[stream_id] = idx;
2335 SAFE_MUTEX_UNLOCK(&lockindex); // and release it!
2336
2337 return idx;
2338}
2339
2340void dvbapi_set_pid(int32_t demux_id, int32_t num, uint32_t idx, bool enable, bool use_des, uint32_t msgid)
2341{
2342 int32_t i, currentfd;
2343 uint16_t streampid = demux[demux_id].STREAMpids[num];
2344 uint32_t newidx = 0, curidx;
2345 ca_pid_t ca_pid2;
2346
2347 if(demux[demux_id].pidindex == -1 && enable)
2348 {
2349 return; // no current pid on enable? --> exit
2350 }
2351
2352 switch(selected_api)
2353 {
2354#if defined(WITH_STAPI) || defined(WITH_STAPI5)
2355 case STAPI:
2356 if(!enable) idx = INDEX_INVALID;
2357 stapi_set_pid(demux_id, num, idx, streampid, demux[demux_id].pmt_file); // only used to disable pids!!!
2358 break;
2359#endif
2360
2361#if defined WITH_COOLAPI || defined WITH_COOLAPI2
2362 case COOLAPI:
2363 break;
2364#endif
2365
2366 default:
2367 for(i = 0; i < CA_MAX; i++)
2368 {
2369 newidx = INDEX_INVALID;
2370 curidx = idx;
2371
2372 if(((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
2373 {
2374 uint32_t action = 0;
2375 if(enable)
2376 {
2377 action = update_streampid_list(i, streampid, curidx, use_des);
2378 }
2379
2380 if(!enable)
2381 {
2382 action = remove_streampid_from_list(i, streampid, curidx);
2383 }
2384
2385 if(action != NO_STREAMPID_LISTED && action != INVALID_STREAMPID_INDEX &&
2386 action != FOUND_STREAMPID_INDEX && action != ADDED_STREAMPID_INDEX &&
2387 action != REMOVED_STREAMPID_INDEX)
2388 {
2389 // removed last index of this streampid on ca? -> disable this pid with -1 on this ca
2390 if((action == REMOVED_STREAMPID_LASTINDEX || action == FIRST_STREAMPID_INDEX)
2391 && (is_ca_used(i, streampid) == INDEX_INVALID))
2392 {
2393 curidx = DVBAPI_INDEX_DISABLE;
2394 }
2395
2396 // removed index of streampid that is used to decode on ca -> get a fresh one
2397 if(action == REMOVED_DECODING_STREAMPID_INDEX || action == FIRST_STREAMPID_INDEX)
2398 {
2399 newidx = is_ca_used(i, streampid); // get an active index for this pid and enable it on ca device
2400 curidx = DVBAPI_INDEX_DISABLE;
2401 }
2402
2403 while(curidx != INDEX_INVALID || newidx != INDEX_INVALID)
2404 {
2405 memset(&ca_pid2, 0, sizeof(ca_pid2));
2406 ca_pid2.pid = streampid;
2407 if(curidx != INDEX_INVALID)
2408 {
2409 (curidx == DVBAPI_INDEX_DISABLE) ? (ca_pid2.index = -1) : (ca_pid2.index = curidx);
2410 cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x index=%d on ca%d",
2411 demux_id,
2412 ((enable && curidx != DVBAPI_INDEX_DISABLE) ? "enable" : "disable"),
2413 num + 1,
2414 ca_pid2.pid,
2415 ca_pid2.index,
2416 i);
2417 curidx = INDEX_INVALID; // flag this index as handled
2418 }
2419 else if(newidx != INDEX_INVALID)
2420 {
2421 (newidx == DVBAPI_INDEX_DISABLE) ? (ca_pid2.index = -1) : (ca_pid2.index = newidx);
2422 cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x by index=%d on ca%d",
2423 demux_id,
2424 ((enable && action == FIRST_STREAMPID_INDEX) ? "enable" : "takeover"),
2425 num + 1,
2426 ca_pid2.pid,
2427 ca_pid2.index,
2428 i);
2429 newidx = INDEX_INVALID; // flag this takeover / new index as handled
2430 }
2431
2432 if(use_des && cfg.dvbapi_extended_cw_api == 2 && ca_pid2.index != -1)
2433 {
2434 ca_pid2.index |= 0x100; // flag DES algo through pid index
2435 }
2436
2437 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
2438 {
2439 dvbapi_net_send(DVBAPI_CA_SET_PID, demux[demux_id].socket_fd,
2440 msgid,
2441 demux_id,
2442 -1 /*unused*/,
2443 (uint8_t *) &ca_pid2,
2444 NULL,
2445 NULL,
2446 demux[demux_id].client_proto_version);
2447 }
2448 else
2449 {
2450 currentfd = ca_fd[i];
2451 if(currentfd <= 0)
2452 {
2453 currentfd = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
2454 ca_fd[i] = currentfd; // save fd of this ca
2455 }
2456
2457 if(currentfd > 0)
2458 {
2459 if(dvbapi_ioctl(currentfd, CA_SET_PID, &ca_pid2) == -1)
2460 {
2461 cs_log_dbg(D_TRACE | D_DVBAPI,"CA_SET_PID ioctl error (errno=%d %s)", errno, strerror(errno));
2462 remove_streampid_from_list(i, ca_pid2.pid, INDEX_DISABLE_ALL);
2463 }
2464
2465 uint32_t result = is_ca_used(i, 0); // check if in use by any pid
2466 if(result == INDEX_INVALID)
2467 {
2468 cs_log_dbg(D_DVBAPI, "Demuxer %d close now unused CA%d device", demux_id, i);
2469 int32_t ret = close(currentfd);
2470 if(ret < 0)
2471 {
2472 cs_log("ERROR: Could not close demuxer fd (errno=%d %s)", errno, strerror(errno));
2473 }
2474 ca_fd[i] = 0;
2475 }
2476 }
2477 }
2478 }
2479 }
2480 }
2481 }
2482 break;
2483 }
2484 return;
2485}
2486
2487void dvbapi_stop_all_descrambling(uint32_t msgid)
2488{
2489 int32_t j;
2490
2491 for(j = 0; j < MAX_DEMUX; j++)
2492 {
2493 if(demux[j].program_number == 0)
2494 {
2495 continue;
2496 }
2497
2498 dvbapi_stop_descrambling(j, msgid);
2499 }
2500}
2501
2502void dvbapi_stop_all_cat_emm_sdt_filtering(uint32_t msgid)
2503{
2504 int32_t j;
2505
2506 for(j = 0; j < MAX_DEMUX; j++)
2507 {
2508 if(demux[j].program_number == 0)
2509 {
2510 continue;
2511 }
2512
2513 dvbapi_stop_filter(j, TYPE_EMM, msgid);
2514 dvbapi_stop_filter(j, TYPE_SDT, msgid);
2515 dvbapi_stop_filter(j, TYPE_CAT, msgid);
2516 demux[j].emm_filter = -1;
2517 }
2518}
2519
2520void dvbapi_stop_descrambling(int32_t demux_id, uint32_t msgid)
2521{
2522 int32_t i, j, z;
2523 if(demux[demux_id].program_number == 0) { return; }
2524 char channame[CS_SERVICENAME_SIZE];
2525
2526 i = demux[demux_id].pidindex;
2527 if(i < 0)
2528 {
2529 i = 0;
2530 }
2531
2532 demux[demux_id].pidindex = -1; // no ecmpid is to be descrambling since we start stop descrambling!
2533
2534 get_servicename(dvbapi_client, demux[demux_id].program_number,
2535 demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].PROVID : NO_PROVID_VALUE,
2536 demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].CAID : NO_CAID_VALUE,
2537 channame, sizeof(channame));
2538
2539 cs_log("Demuxer %d stopped descrambling for program %04X (%s)",
2540 demux_id, demux[demux_id].program_number, channame);
2541
2542 dvbapi_stop_filter(demux_id, TYPE_EMM, msgid);
2543 dvbapi_stop_filter(demux_id, TYPE_SDT, msgid);
2544 dvbapi_stop_filter(demux_id, TYPE_PAT, msgid);
2545 dvbapi_stop_filter(demux_id, TYPE_PMT, msgid);
2546 dvbapi_stop_filter(demux_id, TYPE_CAT, msgid);
2547
2548 for(i = 0; i < demux[demux_id].ECMpidcount && demux[demux_id].ECMpidcount > 0; i++)
2549 {
2550 for(j = 0; j < MAX_STREAM_INDICES; j++)
2551 {
2552 if(demux[demux_id].ECMpids[i].index[j] == INDEX_INVALID)
2553 {
2554 continue;
2555 }
2556
2557 // disable streams!
2558 for(z = 0; z < demux[demux_id].STREAMpidcount; z++)
2559 {
2560 dvbapi_set_pid(demux_id, z, demux[demux_id].ECMpids[i].index[j], false, false, msgid); // disable streampid
2561 }
2562 demux[demux_id].ECMpids[i].index[j] = INDEX_INVALID;
2563 }
2564 }
2565 dvbapi_stop_filter(demux_id, TYPE_ECM, msgid);
2566
2567 pthread_mutex_destroy(&demux[demux_id].answerlock);
2568 memset(&demux[demux_id], 0 , sizeof(DEMUXTYPE));
2569
2570 SAFE_MUTEX_INIT(&demux[demux_id].answerlock, NULL);
2571 for(i = 0; i < MAX_ECM_PIDS; i++)
2572 {
2573 for(j = 0; j < MAX_STREAM_INDICES; j++)
2574 {
2575 demux[demux_id].ECMpids[i].index[j] = INDEX_INVALID;
2576 }
2577 }
2578 demux[demux_id].pidindex = -1;
2579 demux[demux_id].curindex = -1;
2580
2581 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
2582 {
2583 unlink(ECMINFO_FILE);
2584 }
2585 return;
2586}
2587
2588int32_t dvbapi_start_descrambling(int32_t demux_id, int32_t pid, int8_t checked, uint32_t msgid)
2589{
2590 int32_t started = 0; // in case ecmfilter started = 1
2591 int32_t fake_ecm = 0;
2592 ECM_REQUEST *er;
2593 struct s_reader *rdr;
2594
2595 if(!(er = get_ecmtask()))
2596 {
2597 return started;
2598 }
2599 demux[demux_id].ECMpids[pid].checked = checked + 1; // mark this pid as checked!
2600
2601 struct s_dvbapi_priority *p;
2602 for(p = dvbapi_priority; p != NULL ; p = p->next)
2603 {
2604 if((p->type != 'p')
2605 || (p->caid && p->caid != demux[demux_id].ECMpids[pid].CAID)
2606 || (p->provid && p->provid != demux[demux_id].ECMpids[pid].PROVID)
2607 || (p->ecmpid && p->ecmpid != demux[demux_id].ECMpids[pid].ECM_PID)
2608 || (p->srvid && p->srvid != demux[demux_id].program_number)
2609 || (p->pidx && p->pidx-1 != pid)
2610 || (p->cadata && p->cadata != demux[demux_id].ECMpids[pid].cadata))
2611 {
2612 continue;
2613 }
2614
2615 // if found chid and first run apply chid filter, on forced pids always apply!
2616 if(p->type == 'p' && p->chid < 0x10000 && (demux[demux_id].ECMpids[pid].checked == 1 || (p && p->force)))
2617 {
2618 if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
2619 {
2620 er->chid = demux[demux_id].ECMpids[pid].CHID;
2621 }
2622 else
2623 {
2624 er->chid = p->chid; // no channelcache or no chid in use, so use prio chid
2625 demux[demux_id].ECMpids[pid].CHID = p->chid;
2626 }
2627 //cs_log("********* CHID %04X **************", demux[demux_id].ECMpids[pid].CHID);
2628 break; // we only accept one!
2629 }
2630 else
2631 {
2632 if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
2633 {
2634 er->chid = demux[demux_id].ECMpids[pid].CHID;
2635 }
2636 else // no channelcache or no chid in use
2637 {
2638 er->chid = 0;
2639 demux[demux_id].ECMpids[pid].CHID = 0x10000;
2640 }
2641 }
2642 }
2643
2644 er->srvid = demux[demux_id].program_number;
2645 er->caid = demux[demux_id].ECMpids[pid].CAID;
2646 er->pid = demux[demux_id].ECMpids[pid].ECM_PID;
2647 er->prid = demux[demux_id].ECMpids[pid].PROVID;
2648 er->vpid = demux[demux_id].ECMpids[pid].VPID;
2649 er->pmtpid = demux[demux_id].pmtpid;
2650 er->onid = demux[demux_id].onid;
2651 er->msgid = msgid;
2652
2653#ifdef WITH_STAPI5
2654 cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
2655#endif
2656 struct timeb now;
2657 cs_ftime(&now);
2658
2659 for(rdr = first_active_reader; rdr != NULL ; rdr = rdr->next)
2660 {
2661 int8_t match = matching_reader(er, rdr); // check for matching reader
2662 int64_t gone = comp_timeb(&now, &rdr->emm_last);
2663 if(gone > 3600*1000 && rdr->needsemmfirst && caid_is_irdeto(er->caid))
2664 {
2665 cs_log("Warning reader %s received no emms for the last %d seconds -> skip, this reader needs emms first!",
2666 rdr->label, (int)(gone / 1000));
2667 continue; // skip this card needs to process emms first before it can be used for descramble
2668 }
2669
2670 if(p && p->force)
2671 {
2672 match = 1; // forced pid always started!
2673 }
2674
2675 if(!match) // if this reader does not match, check betatunnel for it
2676 {
2677 match = lb_check_auto_betatunnel(er, rdr);
2678 }
2679
2680 if(!match && chk_is_betatunnel_caid(er->caid)) // these caids might be tunneled invisible by peers
2681 {
2682 match = 1; // so make it a match to try it!
2683 }
2684
2685 // check if cache-ex is matching
2686 if(config_enabled(CS_CACHEEX) && (!match && (cacheex_is_match_alias(dvbapi_client, er))))
2687 {
2688 match = 1; // so make it a match to try it!
2689 }
2690
2691 // BISS1 and BISS2 mode 1/E or FAKE caid
2692 // ecm pid is fake, so send out one fake ecm request
2693 // special treatment: if we asked the cw first without starting a filter,
2694 // the cw request will be killed due to no ecmfilter started
2695 if(caid_is_fake(demux[demux_id].ECMpids[pid].CAID) || caid_is_biss_fixed(demux[demux_id].ECMpids[pid].CAID))
2696 {
2697 int32_t j, n;
2698 er->ecmlen = 5;
2699 er->ecm[0] = 0x80; // to pass the cache check it must be 0x80 or 0x81
2700 er->ecm[1] = 0x00;
2701 er->ecm[2] = 0x02;
2702 i2b_buf(2, er->srvid, er->ecm + 3);
2703
2704 for(j = 0, n = 5; j < demux[demux_id].STREAMpidcount; j++, n += 2)
2705 {
2706 i2b_buf(2, demux[demux_id].STREAMpids[j], er->ecm + n);
2707 er->ecm[2] += 2;
2708 er->ecmlen += 2;
2709 }
2710
2711 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
2712 demux_id,
2713 pid,
2714 demux[demux_id].ECMpids[pid].CAID,
2715 demux[demux_id].ECMpids[pid].PROVID,
2716 demux[demux_id].ECMpids[pid].ECM_PID,
2717 demux[demux_id].pmtpid,
2718 demux[demux_id].ECMpids[pid].VPID);
2719
2720 demux[demux_id].curindex = pid; // set current pid to the fresh started one
2721 dvbapi_start_filter(demux_id,
2722 pid,
2723 demux[demux_id].ECMpids[pid].ECM_PID,
2724 demux[demux_id].ECMpids[pid].CAID,
2725 demux[demux_id].ECMpids[pid].PROVID,
2726 0x80,
2727 0xF0,
2728 3000,
2729 TYPE_ECM);
2730
2731 started = 1;
2732 request_cw(dvbapi_client, er, demux_id, 0); // do not register ecm since this try!
2733 fake_ecm = 1;
2734 break; // we started an ecmfilter so stop looking for next matching reader!
2735 }
2736
2737 if(match) // if matching reader found check for irdeto cas if local irdeto card check if it received emms in last 60 minutes
2738 {
2739 if(caid_is_irdeto(er->caid)) // irdeto cas init irdeto_curindex to wait for first index (00)
2740 {
2741 if(demux[demux_id].ECMpids[pid].irdeto_curindex == 0xFE)
2742 {
2743 demux[demux_id].ECMpids[pid].irdeto_curindex = 0x00;
2744 }
2745 }
2746
2747 if(p && p->chid < 0x10000) // do we prio a certain chid?
2748 {
2749 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X PMTPID %04X VPID %04X",
2750 demux_id, pid,
2751 demux[demux_id].ECMpids[pid].CAID,
2752 demux[demux_id].ECMpids[pid].PROVID,
2753 demux[demux_id].ECMpids[pid].ECM_PID,
2754 demux[demux_id].ECMpids[pid].CHID,
2755 demux[demux_id].pmtpid,
2756 demux[demux_id].ECMpids[pid].VPID);
2757 }
2758 else
2759 {
2760 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
2761 demux_id, pid,
2762 demux[demux_id].ECMpids[pid].CAID,
2763 demux[demux_id].ECMpids[pid].PROVID,
2764 demux[demux_id].ECMpids[pid].ECM_PID,
2765 demux[demux_id].pmtpid,
2766 demux[demux_id].ECMpids[pid].VPID);
2767 }
2768
2769 demux[demux_id].curindex = pid; // set current pid to the fresh started one
2770 dvbapi_start_filter(demux_id,
2771 pid,
2772 demux[demux_id].ECMpids[pid].ECM_PID,
2773 demux[demux_id].ECMpids[pid].CAID,
2774 demux[demux_id].ECMpids[pid].PROVID,
2775 0x80,
2776 0xF0,
2777 3000,
2778 TYPE_ECM);
2779
2780 started = 1;
2781 break; // we started an ecmfilter so stop looking for next matching reader!
2782 }
2783 }
2784
2785 if(demux[demux_id].curindex != pid)
2786 {
2787 cs_log("Demuxer %d impossible to descramble PID %d CAID %04X PROVID %06X ECMPID %04X PMTPID %04X (NO MATCHING READER)",
2788 demux_id, pid,
2789 demux[demux_id].ECMpids[pid].CAID,
2790 demux[demux_id].ECMpids[pid].PROVID,
2791 demux[demux_id].ECMpids[pid].ECM_PID,
2792 demux[demux_id].pmtpid);
2793
2794 demux[demux_id].ECMpids[pid].checked = 4; // flag this pid as checked
2795 demux[demux_id].ECMpids[pid].status = -1; // flag this pid as unusable
2796 dvbapi_edit_channel_cache(demux_id, pid, 0); // remove this pid from channelcache
2797 }
2798
2799 if(!fake_ecm)
2800 {
2801 NULLFREE(er);
2802 }
2803 return started;
2804}
2805
2806struct s_dvbapi_priority *dvbapi_check_prio_match_emmpid(int32_t demux_id, uint16_t caid, uint32_t provid, char type)
2807{
2808 struct s_dvbapi_priority *p;
2809 int32_t i;
2810 uint16_t ecm_pid = 0;
2811
2812 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
2813 {
2814 if((demux[demux_id].ECMpids[i].CAID == caid) && (demux[demux_id].ECMpids[i].PROVID == provid))
2815 {
2816 ecm_pid = demux[demux_id].ECMpids[i].ECM_PID;
2817 break;
2818 }
2819 }
2820
2821 if(!ecm_pid)
2822 {
2823 return NULL;
2824 }
2825
2826 for(p = dvbapi_priority; p != NULL; p = p->next)
2827 {
2828 if(p->type != type
2829 || (p->caid && p->caid != caid)
2830 || (p->provid && p->provid != provid)
2831 || (p->ecmpid && p->ecmpid != ecm_pid)
2832 || (p->srvid && p->srvid != demux[demux_id].program_number)
2833 || (p->pidx && p->pidx-1 !=i)
2834 || (p->type == 'i' && (p->chid < 0x10000)))
2835 {
2836 continue;
2837 }
2838
2839 return p;
2840 }
2841 return NULL;
2842}
2843
2844struct s_dvbapi_priority *dvbapi_check_prio_match(int32_t demux_id, int32_t pidindex, char type)
2845{
2846 if(!dvbapi_priority)
2847 {
2848 return NULL;
2849 }
2850
2851 struct s_dvbapi_priority *p;
2852 struct s_ecmpid *ecmpid = &demux[demux_id].ECMpids[pidindex];
2853
2854 for(p = dvbapi_priority; p != NULL; p = p->next)
2855 {
2856 if(p->type != type
2857 || (p->caid && p->caid != ecmpid->CAID)
2858 || (p->provid && p->provid != ecmpid->PROVID)
2859 || (p->ecmpid && p->ecmpid != ecmpid->ECM_PID)
2860 || (p->srvid && p->srvid != demux[demux_id].program_number)
2861 || (p->pidx && p->pidx-1 != pidindex)
2862 || (p->chid < 0x10000 && p->chid != ecmpid->CHID))
2863 {
2864 continue;
2865 }
2866
2867 return p;
2868 }
2869 return NULL;
2870}
2871
2872void dvbapi_process_emm(int32_t demux_id, int32_t filter_num, uint8_t *buffer, uint32_t len)
2873{
2874 EMM_PACKET epg;
2875
2876 struct s_emm_filter *filter = get_emmfilter_by_filternum(demux_id, filter_num + 1); // 0 is used for pending emmfilters, so everything increase 1
2877 if(!filter)
2878 {
2879 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d no filter matches -> SKIP!", demux_id, filter_num + 1);
2880 return;
2881 }
2882
2883 uint32_t provider = filter->provid;
2884 uint16_t caid = filter->caid;
2885 struct s_dvbapi_priority *mapentry = dvbapi_check_prio_match_emmpid(filter->demux_id, filter->caid, filter->provid, 'm');
2886 if(mapentry)
2887 {
2888 cs_log_dbg(D_DVBAPI, "Demuxer %d mapping EMM from %04X@%06X to %04X@%06X",
2889 demux_id, caid, provider, mapentry->mapcaid, mapentry->mapprovid);
2890
2891 caid = mapentry->mapcaid;
2892 provider = mapentry->mapprovid;
2893 }
2894
2895 memset(&epg, 0, sizeof(epg));
2896 i2b_buf(2, caid, epg.caid);
2897 i2b_buf(4, provider, epg.provid);
2898 epg.emmlen = len > sizeof(epg.emm) ? sizeof(epg.emm) : len;
2899 memcpy(epg.emm, buffer, epg.emmlen);
2900
2901 if(config_enabled(READER_IRDETO) && chk_is_betatunnel_caid(caid) == 2)
2902 {
2903 uint16_t ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_id].program_number);
2904 if(caid != ncaid)
2905 {
2906 irdeto_add_emm_header(&epg);
2907 i2b_buf(2, ncaid, epg.caid);
2908 }
2909 }
2910 do_emm(dvbapi_client, &epg);
2911}
2912
2913void dvbapi_read_priority(void)
2914{
2915 FILE *fp;
2916 char token[128], str1[128];
2917 char type;
2918 int32_t i, ret, count = 0;
2919 const char *cs_prio = "oscam.dvbapi";
2920
2921 fp = fopen(get_config_filename(token, sizeof(token), cs_prio), "r");
2922 if(!fp)
2923 {
2924 cs_log_dbg(D_DVBAPI, "ERROR: Can't open priority file %s", token);
2925 return;
2926 }
2927
2928 if(dvbapi_priority)
2929 {
2930 cs_log_dbg(D_DVBAPI, "reread priority file %s", cs_prio);
2931 struct s_dvbapi_priority *o, *p;
2932 for(p = dvbapi_priority; p != NULL; p = o)
2933 {
2934 o = p->next;
2935 NULLFREE(p);
2936 }
2937 dvbapi_priority = NULL;
2938 }
2939
2940 while(fgets(token, sizeof(token), fp))
2941 {
2942 // Ignore comments and empty lines
2943 if(token[0] == '#' || token[0] == '/' || token[0] == '\n' || token[0] == '\r' || token[0] == '\0' || cs_strlen(token) > 100)
2944 {
2945 continue;
2946 }
2947 memset(str1, 0, 128);
2948
2949 for(i = 0; i < (int)cs_strlen(token) && token[i] == ' '; i++) { ; }
2950
2951 if(i == (int)cs_strlen(token) - 1) // empty line or all spaces
2952 {
2953 continue;
2954 }
2955
2956 for(i = 0; i < (int)cs_strlen(token); i++)
2957 {
2958 if(token[i] == '@')
2959 {
2960 token[i] = ':';
2961 }
2962 }
2963
2964 for(i = 0; i < (int)cs_strlen(token); i++)
2965 {
2966 if((token[i] == ':' || token[i] == ' ') && token[i + 1] == ':') // if "::" or " :"
2967 {
2968 memmove(token + i + 2, token + i + 1, cs_strlen(token) - i + 1); // insert extra position
2969 token[i + 1] = '0'; // and fill it with NULL
2970 }
2971
2972 if(token[i] == '#' || token[i] == '/')
2973 {
2974 token[i] = '\0';
2975 break;
2976 }
2977 }
2978
2979 type = 0;
2980#if defined(WITH_STAPI) || defined(WITH_STAPI5)
2981 uint32_t disablefilter = 0;
2982 ret = sscanf(trim(token), "%c: %63s %63s %d", &type, str1, str1 + 64, &disablefilter);
2983#else
2984 ret = sscanf(trim(token), "%c: %63s %63s", &type, str1, str1 + 64);
2985#endif
2986 type = tolower((uint8_t)type);
2987
2988 if(ret < 1 || (type != 'p' && type != 'i' && type != 'm' && type != 'd' &&
2989 type != 's' && type != 'l' && type != 'j' && type != 'a' && type != 'x'))
2990 {
2991 //fprintf(stderr, "Warning: line containing %s in %s not recognized, ignoring line\n", token, cs_prio);
2992 // fprintf would issue the warning to the command line, which is more consistent with other config warnings
2993 // however it takes OSCam a long time (>4 seconds) to reach this part of the program, so the warnings are
2994 // reaching tty rather late which leads to confusion. So send the warnings to log file instead
2995 cs_log_dbg(D_DVBAPI, "WARN: line containing %s in %s not recognized, ignoring...", token, cs_prio);
2996 continue;
2997 }
2998
2999 struct s_dvbapi_priority *entry;
3000 if(!cs_malloc(&entry, sizeof(struct s_dvbapi_priority)))
3001 {
3002 ret = fclose(fp);
3003 if(ret < 0)
3004 {
3005 cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno));
3006 }
3007 return;
3008 }
3009
3010 entry->type = type;
3011 entry->next = NULL;
3012 count++;
3013
3014#if defined(WITH_STAPI) || defined(WITH_STAPI5)
3015 if(type == 's')
3016 {
3017 cs_strncpy(entry->devname, str1, sizeof(entry->devname));
3018 cs_strncpy(entry->pmtfile, str1 + 64, sizeof(entry->pmtfile));
3019 entry->disablefilter = disablefilter;
3020 cs_log_dbg(D_DVBAPI, "stapi prio: ret=%d | %c: %s %s | disable %d",
3021 ret, type, entry->devname, entry->pmtfile, disablefilter);
3022
3023 if(!dvbapi_priority)
3024 {
3025 dvbapi_priority = entry;
3026 }
3027 else
3028 {
3029 struct s_dvbapi_priority *p;
3030 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
3031 p->next = entry;
3032 }
3033 continue;
3034 }
3035#endif
3036 char c_srvid[34];
3037 c_srvid[0] = '\0';
3038 uint32_t caid = 0, provid = 0, srvid = 0, ecmpid = 0, cadata = 0;;
3039 uint32_t chid = 0x10000; //chid=0 is a valid chid
3040
3041 ret = sscanf(str1, "%4x:%6x:%33[^:]:%4x:%4x:%8x"SCNx16, &caid, &provid, c_srvid, &ecmpid, &chid, &cadata);
3042 if(ret < 1)
3043 {
3044 cs_log("Error in oscam.dvbapi: ret=%d | %c: %04X %06X %s %04X %04X",
3045 ret, type, caid, provid, c_srvid, ecmpid, chid);
3046 continue; // skip this entry!
3047 }
3048 else
3049 {
3050 cs_log_dbg(D_DVBAPI, "Parsing rule: ret=%d | %c: %04X %06X %s %04X %04X %04X",
3051 ret, type, caid, provid, c_srvid, ecmpid, chid, cadata);
3052 }
3053
3054 entry->caid = caid;
3055 entry->provid = provid;
3056 entry->ecmpid = ecmpid;
3057 entry->chid = chid;
3058 entry->cadata = cadata;
3059 uint32_t delay = 0, force = 0, mapcaid = 0, mapprovid = 0, mapecmpid = 0, pidx = 0;
3060
3061 switch(type)
3062 {
3063 case 'i':
3064 ret = sscanf(str1 + 64, "%1d", &pidx);
3065 entry->pidx = pidx + 1;
3066 if(ret < 1) entry->pidx = 0;
3067 break;
3068
3069 case 'd':
3070 sscanf(str1 + 64, "%4d", &delay);
3071 entry->delay = delay;
3072 break;
3073
3074 case 'l':
3075 entry->delay = dyn_word_atob(str1 + 64);
3076 if(entry->delay == -1) { entry->delay = 0; }
3077 break;
3078
3079 case 'p':
3080 ret = sscanf(str1 + 64, "%1d:%1d", &force, &pidx);
3081 entry->force = force;
3082 entry->pidx = pidx + 1;
3083 if(ret < 2) entry->pidx = 0;
3084 break;
3085
3086 case 'm':
3087 sscanf(str1 + 64, "%4x:%6x", &mapcaid, &mapprovid);
3088 if(!mapcaid) { mapcaid = 0xFFFF; }
3089 entry->mapcaid = mapcaid;
3090 entry->mapprovid = mapprovid;
3091 break;
3092
3093 case 'a':
3094 case 'j':
3095 sscanf(str1 + 64, "%4x:%6x:%4x", &mapcaid, &mapprovid, &mapecmpid);
3096 if(!mapcaid) { mapcaid = 0xFFFF; }
3097 entry->mapcaid = mapcaid;
3098 entry->mapprovid = mapprovid;
3099 entry->mapecmpid = mapecmpid;
3100 break;
3101 }
3102
3103 if(c_srvid[0] == '=')
3104 {
3105 struct s_srvid *this;
3106 for(i = 0; i < 16; i++)
3107 {
3108 for(this = cfg.srvid[i]; this != NULL; this = this->next)
3109 {
3110 if(this->name && strcmp(this->name, c_srvid + 1) == 0)
3111 {
3112 struct s_dvbapi_priority *entry2;
3113 if(!cs_malloc(&entry2, sizeof(struct s_dvbapi_priority)))
3114 {
3115 continue;
3116 }
3117
3118 memcpy(entry2, entry, sizeof(struct s_dvbapi_priority));
3119 entry2->srvid = this->srvid;
3120 cs_log_dbg(D_DVBAPI, "prio srvid: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
3121 ret,
3122 entry2->type,
3123 entry2->caid,
3124 entry2->provid,
3125 entry2->srvid,
3126 entry2->ecmpid,
3127 entry2->chid,
3128 entry2->mapcaid,
3129 entry2->mapprovid,
3130 entry2->mapecmpid,
3131 entry2->force,
3132 entry2->delay);
3133
3134 if(!dvbapi_priority)
3135 {
3136 dvbapi_priority = entry2;
3137 }
3138 else
3139 {
3140 struct s_dvbapi_priority *p;
3141 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
3142 p->next = entry2;
3143 }
3144 }
3145 }
3146 }
3147 NULLFREE(entry);
3148 continue;
3149 }
3150 else
3151 {
3152 sscanf(c_srvid, "%4x", &srvid);
3153 entry->srvid = srvid;
3154 }
3155 cs_log_dbg(D_DVBAPI, "prio: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
3156 ret,
3157 entry->type,
3158 entry->caid,
3159 entry->provid,
3160 entry->srvid,
3161 entry->ecmpid,
3162 entry->chid,
3163 entry->mapcaid,
3164 entry->mapprovid,
3165 entry->mapecmpid,
3166 entry->force,
3167 entry->delay);
3168
3169 if(!dvbapi_priority)
3170 {
3171 dvbapi_priority = entry;
3172 }
3173 else
3174 {
3175 struct s_dvbapi_priority *p;
3176 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
3177 p->next = entry;
3178 }
3179 }
3180 cs_log_dbg(D_DVBAPI, "Read %d entries from %s", count, cs_prio);
3181
3182 ret = fclose(fp);
3183 if(ret < 0)
3184 {
3185 cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno));
3186 }
3187 return;
3188}
3189
3190void dvbapi_resort_ecmpids(int32_t demux_id)
3191{
3192 int32_t n, cache = 0, matching_done = 0, found = -1, match_reader_count = 0, total_reader = 0;
3193 uint16_t btun_caid = 0;
3194 struct timeb start,end;
3195 cs_ftime(&start);
3196
3197 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3198 {
3199 demux[demux_id].ECMpids[n].status = 0;
3200 demux[demux_id].ECMpids[n].checked = 0;
3201 demux[demux_id].ECMpids[n].irdeto_curindex = 0xFE;
3202 demux[demux_id].ECMpids[n].irdeto_maxindex = 0;
3203 demux[demux_id].ECMpids[n].irdeto_cycle = 0xFE;
3204 demux[demux_id].ECMpids[n].tries = 0xFE;
3205 demux[demux_id].ECMpids[n].table = 0;
3206 }
3207
3208 demux[demux_id].max_status = 0;
3209 demux[demux_id].curindex = -1;
3210 demux[demux_id].pidindex = -1;
3211
3212 struct s_reader *rdr;
3213 int32_t p_order = demux[demux_id].ECMpidcount + 1;
3214 struct s_dvbapi_priority *prio;
3215
3216 // handle prio order in oscam.dvbapi + ignore all chids
3217 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
3218 {
3219 total_reader++; // only need to calculate once!
3220 }
3221
3222 ECM_REQUEST *er;
3223 if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
3224 {
3225 return;
3226 }
3227
3228 for(prio = dvbapi_priority; prio != NULL; prio = prio->next)
3229 {
3230 if(prio->type != 'p' && prio->type != 'i')
3231 {
3232 continue;
3233 }
3234
3235 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3236 {
3237 if(demux[demux_id].ECMpids[n].status == -1) continue; // skip ignores!
3238
3239 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3240 er->prid = demux[demux_id].ECMpids[n].PROVID;
3241 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3242 er->srvid = demux[demux_id].program_number;
3243 er->client = cur_client();
3244 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3245
3246 if(prio->type == 'p' && btun_caid)
3247 {
3248 er->caid = btun_caid;
3249 }
3250
3251 if((prio->caid && (prio->caid != er->caid && prio->caid != er->ocaid))
3252 || (prio->provid && prio->provid != er->prid)
3253 || (prio->srvid && prio->srvid != er->srvid)
3254 || (prio->ecmpid && prio->ecmpid != er->pid)
3255 || (prio->pidx && prio->pidx - 1 != n))
3256 {
3257 continue;
3258 }
3259
3260 if(prio->type == 'p') // check for prio
3261 {
3262 if(prio->cadata != 0 && prio->cadata != demux[demux_id].ECMpids[n].cadata)
3263 {
3264 continue;
3265 }
3266
3267 if(prio->chid < 0x10000)
3268 {
3269 demux[demux_id].ECMpids[n].CHID = prio->chid;
3270 }
3271
3272 if(prio->force)
3273 {
3274 int32_t j;
3275 for(j = 0; j < demux[demux_id].ECMpidcount; j++)
3276 {
3277 demux[demux_id].ECMpids[j].status = -1;
3278 }
3279
3280 demux[demux_id].ECMpids[n].status = 1;
3281 demux[demux_id].ECMpids[n].checked = 0;
3282 demux[demux_id].max_status = 1;
3283 demux[demux_id].max_emm_filter = maxfilter - 1;
3284
3285 cs_log_dbg(D_DVBAPI, "Demuxer %d prio forced%s ecmpid %d %04X@%06X:%04X:%04X (file)",
3286 demux_id,
3287 ((prio->caid == er->caid && prio->caid != er->ocaid) ? " betatunneled" : ""),
3288 n,
3289 demux[demux_id].ECMpids[n].CAID,
3290 demux[demux_id].ECMpids[n].PROVID,
3291 demux[demux_id].ECMpids[n].ECM_PID,
3292 (uint16_t) prio->chid);
3293 NULLFREE(er);
3294 return; // go start descrambling since its forced by user!
3295 }
3296 else
3297 {
3298 if(!demux[demux_id].ECMpids[n].status) // only accept first matching prio from oscam.dvbapi
3299 {
3300 demux[demux_id].ECMpids[n].status = total_reader + p_order--;
3301 matching_done = 1;
3302 cs_log_dbg(D_DVBAPI, "Demuxer %d prio%s ecmpid %d %04X@%06X:%04X:%04X weight: %d (file)",
3303 demux_id,
3304 ((prio->caid == er->caid && prio->caid != er->ocaid) ? " betatunneled" : ""),
3305 n,
3306 demux[demux_id].ECMpids[n].CAID,
3307 demux[demux_id].ECMpids[n].PROVID,
3308 demux[demux_id].ECMpids[n].ECM_PID,
3309 (uint16_t) prio->chid,
3310 demux[demux_id].ECMpids[n].status);
3311 }
3312 continue; // evaluate next ecmpid
3313 }
3314 }
3315
3316 // check for ignore all chids
3317 if(prio->type == 'i' && prio->chid == 0x10000 && demux[demux_id].ECMpids[n].status == 0)
3318 {
3319 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X all chids (file)",
3320 demux_id,
3321 n,
3322 demux[demux_id].ECMpids[n].CAID,
3323 demux[demux_id].ECMpids[n].PROVID,
3324 demux[demux_id].ECMpids[n].ECM_PID);
3325 demux[demux_id].ECMpids[n].status = -1;
3326 continue; // evaluate next ecmpid
3327 }
3328 }
3329 }
3330
3331 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3332 {
3333 if(demux[demux_id].ECMpids[n].status == -1)
3334 {
3335 continue; // skip ignores!
3336 }
3337
3338 int32_t nr;
3339 SIDTAB *sidtab;
3340
3341 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3342 er->prid = demux[demux_id].ECMpids[n].PROVID;
3343 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3344 er->srvid = demux[demux_id].program_number;
3345 er->client = cur_client();
3346
3347 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3348 if(btun_caid)
3349 {
3350 er->caid = btun_caid;
3351 }
3352
3353 match_reader_count = 0;
3354
3355 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
3356 {
3357 if(matching_reader(er, rdr))
3358 {
3359 match_reader_count++;
3360 }
3361 }
3362
3363 if(match_reader_count == 0)
3364 {
3365 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X:%04X (no matching reader)",
3366 demux_id,
3367 n,
3368 demux[demux_id].ECMpids[n].CAID,
3369 demux[demux_id].ECMpids[n].PROVID,
3370 demux[demux_id].ECMpids[n].ECM_PID,
3371 demux[demux_id].ECMpids[n].CHID);
3372 demux[demux_id].ECMpids[n].status = -1;
3373 continue; // evaluate next ecmpid
3374 }
3375 else // ecmpids with no matching readers are disabled and matching sidtabbits have now highest status
3376 {
3377 for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
3378 {
3379 if(sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid)
3380 {
3381 if((cfg.dvbapi_sidtabs.no & ((SIDTABBITS)1 << nr)) && (chk_srvid_match(er, sidtab)))
3382 {
3383 demux[demux_id].ECMpids[n].status = -1; //ignore
3384 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X (service %s pos %d)",
3385 demux_id,
3386 n,
3387 demux[demux_id].ECMpids[n].CAID,
3388 demux[demux_id].ECMpids[n].PROVID,
3389 demux[demux_id].ECMpids[n].ECM_PID,
3390 sidtab->label,
3391 nr);
3392 continue; // evaluate next ecmpid
3393 }
3394 if((cfg.dvbapi_sidtabs.ok & ((SIDTABBITS) 1 << nr)) && (chk_srvid_match(er, sidtab)))
3395 {
3396 demux[demux_id].ECMpids[n].status++; // priority
3397 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (service %s pos %d)",
3398 demux_id,
3399 n,
3400 demux[demux_id].ECMpids[n].CAID,
3401 demux[demux_id].ECMpids[n].PROVID,
3402 demux[demux_id].ECMpids[n].ECM_PID,
3403 demux[demux_id].ECMpids[n].status,
3404 sidtab->label,
3405 nr);
3406 }
3407 }
3408 }
3409 }
3410 }
3411
3412 // ecmpid with highest prio from oscam.dvbapi has now highest status
3413 // check all ecmpids and get the highest amount cache-ex and local readers
3414 int32_t max_local_matching_reader = 0, max_cacheex_reader = 0;
3415
3416 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3417 {
3418 int32_t count_matching_cacheex_reader = 0, count_matching_local_reader = 0;
3419 if(demux[demux_id].ECMpids[n].status == -1)
3420 {
3421 continue; // skip ignores!
3422 }
3423
3424 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3425 er->prid = demux[demux_id].ECMpids[n].PROVID;
3426 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3427 er->srvid = demux[demux_id].program_number;
3428 er->client = cur_client();
3429
3430 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3431 if(btun_caid)
3432 {
3433 er->caid = btun_caid;
3434 }
3435
3436 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
3437 {
3438 if(matching_reader(er, rdr))
3439 {
3440 if(cacheex_reader(rdr))
3441 {
3442 count_matching_cacheex_reader++;
3443 }
3444 else if(is_localreader(rdr, er))
3445 {
3446 count_matching_local_reader++;
3447 }
3448 }
3449 }
3450
3451 if(max_local_matching_reader < count_matching_local_reader)
3452 {
3453 max_local_matching_reader = count_matching_local_reader;
3454 }
3455
3456 if(max_cacheex_reader < count_matching_cacheex_reader)
3457 {
3458 max_cacheex_reader = count_matching_cacheex_reader;
3459 }
3460 }
3461
3462 if(max_local_matching_reader != 0 || max_cacheex_reader != 0)
3463 {
3464 p_order = demux[demux_id].ECMpidcount * 2;
3465 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3466 {
3467 int32_t count_matching_cacheex_reader = 0, count_matching_local_reader = 0;
3468 int32_t localprio = 1, cacheexprio = 1;
3469
3470 if(demux[demux_id].ECMpids[n].status == -1)
3471 {
3472 continue; // skip ignores!
3473 }
3474
3475 if(cfg.preferlocalcards == 2) // ecmpids with local reader get highest prio
3476 {
3477 localprio = max_cacheex_reader + p_order + 1;
3478 }
3479 else if(cfg.preferlocalcards == 1) // ecmpids with cacheex reader get highest prio
3480 {
3481 cacheexprio = max_local_matching_reader + p_order + 1;
3482 }
3483
3484 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3485 er->prid = demux[demux_id].ECMpids[n].PROVID;
3486 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3487 er->srvid = demux[demux_id].program_number;
3488 er->client = cur_client();
3489
3490 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3491 if(btun_caid)
3492 {
3493 er->caid = btun_caid;
3494 }
3495
3496 int32_t oldstatus = demux[demux_id].ECMpids[n].status;
3497 int32_t anyreader = 0;
3498
3499 for(rdr = first_active_reader; rdr; rdr = rdr->next)
3500 {
3501 if(matching_reader(er, rdr))
3502 {
3503 if(cfg.preferlocalcards == 0)
3504 {
3505 if(!matching_done)
3506 {
3507 demux[demux_id].ECMpids[n].status++;
3508 }
3509 anyreader++;
3510 continue;
3511 }
3512
3513 if(cacheex_reader(rdr))
3514 {
3515 demux[demux_id].ECMpids[n].status += cacheexprio;
3516 count_matching_cacheex_reader++;
3517 cacheexprio = 1;
3518 }
3519
3520 if(is_localreader(rdr, er))
3521 {
3522 demux[demux_id].ECMpids[n].status += localprio;
3523 count_matching_local_reader++;
3524 localprio = 1;
3525 }
3526 }
3527 }
3528
3529 if(oldstatus != demux[demux_id].ECMpids[n].status)
3530 {
3531 if(anyreader)
3532 {
3533 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X:%04X weight: %d (%d readers)",
3534 demux_id,
3535 n,
3536 demux[demux_id].ECMpids[n].CAID,
3537 demux[demux_id].ECMpids[n].PROVID,
3538 demux[demux_id].ECMpids[n].ECM_PID,
3539 demux[demux_id].ECMpids[n].CHID,
3540 demux[demux_id].ECMpids[n].status,
3541 anyreader);
3542 }
3543 else
3544 {
3545 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X:%04X weight: %d (%d local and %d cacheex readers)",
3546 demux_id,
3547 n,
3548 demux[demux_id].ECMpids[n].CAID,
3549 demux[demux_id].ECMpids[n].PROVID,
3550 demux[demux_id].ECMpids[n].ECM_PID,
3551 demux[demux_id].ECMpids[n].CHID,
3552 demux[demux_id].ECMpids[n].status,
3553 count_matching_local_reader,
3554 count_matching_cacheex_reader);
3555 }
3556 }
3557 }
3558 }
3559
3560 struct s_channel_cache *c = NULL;
3561
3562 for(n = 0; n < demux[demux_id].ECMpidcount && matching_done == 0; n++)
3563 {
3564 if(demux[demux_id].ECMpids[n].status == -1)
3565 {
3566 continue; // skip ignores!
3567 }
3568
3569 c = dvbapi_find_channel_cache(demux_id, n, 0); // find exact channel match
3570 if(c != NULL)
3571 {
3572 found = n;
3573 cache = 2; // found cache entry with higher priority
3574 demux[demux_id].ECMpids[n].status++; // prioritize CAIDs which already decoded same caid:provid:srvid
3575
3576 if(c->chid < 0x10000)
3577 {
3578 demux[demux_id].ECMpids[n].CHID = c->chid; // if chid registered in cache -> use it!
3579 }
3580 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (found caid/provid/srvid in cache)",
3581 demux_id,
3582 n,
3583 demux[demux_id].ECMpids[n].CAID,
3584 demux[demux_id].ECMpids[n].PROVID,
3585 demux[demux_id].ECMpids[n].ECM_PID,
3586 demux[demux_id].ECMpids[n].status);
3587 break;
3588 }
3589 }
3590
3591 if(found == -1)
3592 {
3593 // prioritize CAIDs which already decoded same caid:provid
3594 for(n = 0; n < demux[demux_id].ECMpidcount && matching_done == 0; n++)
3595 {
3596 if(demux[demux_id].ECMpids[n].status == -1) continue; // skip ignores!
3597
3598 c = dvbapi_find_channel_cache(demux_id, n, 1);
3599 if(c != NULL)
3600 {
3601 cache = 1; //found cache entry
3602 demux[demux_id].ECMpids[n].status++;
3603 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (found caid/provid in cache)",
3604 demux_id,
3605 n,
3606 demux[demux_id].ECMpids[n].CAID,
3607 demux[demux_id].ECMpids[n].PROVID,
3608 demux[demux_id].ECMpids[n].ECM_PID,
3609 demux[demux_id].ECMpids[n].status);
3610 }
3611 }
3612 }
3613
3614 int32_t max_status = 0;
3615 int32_t highest_priopid = -1;
3616
3617 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3618 {
3619 if(demux[demux_id].ECMpids[n].status == -1)
3620 {
3621 continue; // skip ignores!
3622 }
3623
3624 if(demux[demux_id].ECMpids[n].status > max_status) // find highest prio pid
3625 {
3626 max_status = demux[demux_id].ECMpids[n].status;
3627 highest_priopid = n;
3628 }
3629
3630 if(!USE_OPENXCAS) // openxcas doesnt use prio and non-prio run: all are equal!
3631 {
3632 if(demux[demux_id].ECMpids[n].status == 0)
3633 {
3634 demux[demux_id].ECMpids[n].checked = 2; // set pids with no status to no prio run
3635 }
3636 }
3637 }
3638
3639 demux[demux_id].max_status = max_status; // register maxstatus
3640
3641 // Found entry in channelcache that is valid and has exact match on srvid
3642 if(highest_priopid != -1 && found == highest_priopid && cache == 2)
3643 {
3644 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3645 {
3646 if(demux[demux_id].ECMpids[n].status == -1)
3647 {
3648 continue; // skip ignores!
3649 }
3650
3651 if(n != found)
3652 {
3653 // disable non matching pid
3654 demux[demux_id].ECMpids[n].status = -1;
3655 }
3656 else
3657 {
3658 demux[demux_id].ECMpids[n].status = 1;
3659 }
3660 }
3661 demux[demux_id].max_emm_filter = maxfilter - 1;
3662 demux[demux_id].max_status = 1;
3663 cs_log("Demuxer %d found channel in cache and matching prio -> start descrambling ecmpid %d ", demux_id, found);
3664 }
3665
3666 NULLFREE(er);
3667 cs_ftime(&end);
3668 int64_t gone = comp_timeb(&end, &start);
3669 cs_log_dbg(D_DVBAPI, "Demuxer %d sorting the ecmpids took %"PRId64" ms", demux_id, gone);
3670 return;
3671}
3672
3673static void dvbapi_priority_read_entry_add(int32_t demux_id, uint16_t video_pid)
3674{
3675 struct s_dvbapi_priority *add_entry;
3676
3677 for(add_entry = dvbapi_priority; add_entry != NULL; add_entry = add_entry->next)
3678 {
3679 // ECM pid is misused to hold PMT pid in case of 'A' rule.
3680 // Some receivers don't forward the PMT pid, so use the video pid instead
3681 if(add_entry->type != 'a' || add_entry->srvid != demux[demux_id].program_number
3682 || (add_entry->ecmpid && demux[demux_id].pmtpid && add_entry->ecmpid != demux[demux_id].pmtpid)
3683 || (add_entry->ecmpid && !demux[demux_id].pmtpid && add_entry->ecmpid != video_pid))
3684 {
3685 continue;
3686 }
3687
3688 dvbapi_add_ecmpid(demux_id, add_entry->mapcaid, add_entry->mapecmpid, add_entry->mapprovid, 0, "(fake ecm pid)");
3689
3690 cs_log_dbg(D_DVBAPI, "Demuxer %d added fake ecm pid %04X@%06X:%04X for program %04X", demux_id,
3691 add_entry->mapcaid, add_entry->mapprovid, add_entry->mapecmpid, demux[demux_id].program_number);
3692 break;
3693 }
3694}
3695
3696static void dvbapi_priority_read_entry_map(int32_t demux_id)
3697{
3698 int32_t j;
3699 struct s_dvbapi_priority *map_entry;
3700
3701 for(j = 0; j < demux[demux_id].ECMpidcount; j++)
3702 {
3703 map_entry = dvbapi_check_prio_match(demux_id, j, 'm');
3704 if(map_entry)
3705 {
3706 cs_log_dbg(D_DVBAPI, "Demuxer %d mapping ecm pid %d from %04X@%06X to %04X@%06X",
3707 demux_id, j, demux[demux_id].ECMpids[j].CAID, demux[demux_id].ECMpids[j].PROVID,
3708 map_entry->mapcaid, map_entry->mapprovid);
3709
3710 demux[demux_id].ECMpids[j].CAID = map_entry->mapcaid;
3711 demux[demux_id].ECMpids[j].PROVID = map_entry->mapprovid;
3712 }
3713 }
3714}
3715
3716static void dvbapi_priority_read_entry_extra(int32_t demux_id)
3717{
3718 struct s_dvbapi_priority *extra_entry;
3719 int32_t j, k, l, m, extra_demux_id;
3720
3721 for(extra_entry = dvbapi_priority; extra_entry != NULL; extra_entry = extra_entry->next)
3722 {
3723 if(extra_entry->type != 'x')
3724 {
3725 continue;
3726 }
3727
3728 for(j = 0; j <= demux[demux_id].ECMpidcount; ++j)
3729 {
3730 if((extra_entry->caid && extra_entry->caid != demux[demux_id].ECMpids[j].CAID)
3731 || (extra_entry->provid && extra_entry->provid != demux[demux_id].ECMpids[j].PROVID)
3732 || (extra_entry->ecmpid && extra_entry->ecmpid != demux[demux_id].ECMpids[j].ECM_PID)
3733 || (extra_entry->srvid && extra_entry->srvid != demux[demux_id].program_number))
3734 {
3735 continue;
3736 }
3737
3738 cs_log("Mapping ecm pid %04X@%06X:%04X:%04X to extra demuxer",
3739 extra_entry->caid, extra_entry->provid, extra_entry->ecmpid, extra_entry->srvid);
3740
3741 for(extra_demux_id = 0; extra_demux_id < MAX_DEMUX; extra_demux_id++)
3742 {
3743 if(demux[extra_demux_id].program_number != 0)
3744 {
3745 continue; // Skip occupied demuxers
3746 }
3747 }
3748
3749 if(extra_demux_id >= MAX_DEMUX)
3750 {
3751 cs_log("There is no free demuxer for extra streams");
3752 continue;
3753 }
3754
3755 demux[extra_demux_id].ECMpids[0] = demux[demux_id].ECMpids[j];
3756 demux[extra_demux_id].ECMpidcount = 1;
3757 demux[extra_demux_id].STREAMpidcount = 0;
3758 demux[extra_demux_id].program_number = demux[demux_id].program_number;
3759 demux[extra_demux_id].pmtpid = demux[demux_id].pmtpid;
3760 demux[extra_demux_id].demux_index = demux[demux_id].demux_index;
3761 demux[extra_demux_id].adapter_index = demux[demux_id].adapter_index;
3762 demux[extra_demux_id].ca_mask = demux[demux_id].ca_mask;
3763 demux[extra_demux_id].socket_fd = demux[demux_id].socket_fd;
3764 demux[extra_demux_id].stop_descrambling = false;
3765 demux[extra_demux_id].rdr = NULL;
3766 demux[extra_demux_id].curindex = -1;
3767
3768 // Add streams to extra demux
3769 for(k = 0; k < demux[demux_id].STREAMpidcount; ++k)
3770 {
3771 if(!demux[demux_id].ECMpids[j].streams || (demux[demux_id].ECMpids[j].streams & (1 << k)))
3772 {
3773 demux[extra_demux_id].ECMpids[0].streams |= (1 << demux[extra_demux_id].STREAMpidcount);
3774 demux[extra_demux_id].STREAMpids[demux[extra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpids[k];
3775 demux[extra_demux_id].STREAMpidsType[demux[extra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpidsType[k];
3776 ++demux[extra_demux_id].STREAMpidcount;
3777
3778 // Shift stream associations in normal demux because we will remove the stream entirely
3779 for(l = 0; l < demux[demux_id].ECMpidcount; ++l)
3780 {
3781 for(m = k; m < demux[demux_id].STREAMpidcount - 1; ++m)
3782 {
3783 if(demux[demux_id].ECMpids[l].streams & (1 << (m + 1)))
3784 {
3785 demux[demux_id].ECMpids[l].streams |= (1 << m);
3786 }
3787 else
3788 {
3789 demux[demux_id].ECMpids[l].streams &= ~(1 << m);
3790 }
3791 }
3792 }
3793
3794 // Remove stream association from normal demux device
3795 for(l = k; l < demux[demux_id].STREAMpidcount - 1; ++l)
3796 {
3797 demux[demux_id].STREAMpids[l] = demux[demux_id].STREAMpids[l + 1];
3798 demux[demux_id].STREAMpidsType[l] = demux[demux_id].STREAMpidsType[l + 1];
3799 }
3800
3801 --demux[demux_id].STREAMpidcount;
3802 --k;
3803 }
3804 }
3805
3806 // Remove ecm pid from normal demuxer
3807 for(k = j; k < demux[demux_id].ECMpidcount; ++k)
3808 {
3809 demux[demux_id].ECMpids[k] = demux[demux_id].ECMpids[k + 1];
3810 }
3811
3812 --demux[demux_id].ECMpidcount;
3813 --j;
3814
3815 if(demux[extra_demux_id].STREAMpidcount <= 0)
3816 {
3817 cs_log("Found no streams for extra demuxer. Not starting additional decoding on it.");
3818
3819 demux[extra_demux_id].program_number = 0;
3820 demux[extra_demux_id].stop_descrambling = true;
3821 }
3822
3823 if(demux[demux_id].STREAMpidcount < 1)
3824 {
3825 cs_log("Found no streams for normal demuxer. Not starting additional decoding on it.");
3826 }
3827 }
3828 }
3829}
3830
3831static void dvbapi_parse_pmt_ca_descriptor(int32_t demux_id, const uint8_t *buffer, uint8_t descriptor_length)
3832{
3833 uint16_t i, ca_system_id, ca_pid;
3834 uint32_t ca_provider = 0, ca_data = 0;
3835 char txt[40]; // room for PBM: 8 byte pbm and DATE: date
3836 memset(txt, 0x00, sizeof(txt));
3837
3838 if(descriptor_length < 4)
3839 {
3840 return; // CA descriptor has a minimum length of 4 bytes
3841 }
3842
3843 ca_system_id = b2i(2, buffer);
3844 ca_pid = b2i(2, buffer + 2) & 0x1FFF;
3845
3846 if(ca_system_id == 0x0000)
3847 {
3848 return; // This is not a valid CAID
3849 }
3850
3851 if(caid_is_seca(ca_system_id))
3852 {
3853 for(i = 2; i < descriptor_length; i += 15)
3854 {
3855 ca_pid = b2i(2, buffer + i) & 0x1FFF;
3856 ca_provider = b2i(2, buffer + i + 2);
3857
3858 int8_t year = buffer[i + 13] >> 1;
3859 int8_t month = (((buffer[i + 13] & 0x01) << 3) | (buffer[i + 14] >> 5));
3860 int8_t day = buffer[i + 14] & 0x1F;
3861
3862 snprintf(txt, sizeof(txt), "PBM: ");
3863 cs_hexdump(0, buffer + i + 5, 8, txt + 5, (2 * 8) + 1); // hexdump 8 byte pbm
3864 snprintf(txt + 20, sizeof(txt) - 20, " DATE: %d-%d-%d", day, month, year + 1990);
3865
3866 dvbapi_add_ecmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, txt);
3867 }
3868 }
3869 else
3870 {
3871 if(caid_is_viaccess(ca_system_id) && descriptor_length == 0x0F && buffer[10] == 0x14)
3872 {
3873 ca_provider = b2i(3, buffer + 12) & 0xFFFFF0;
3874 }
3875 else if(caid_is_nagra(ca_system_id) && descriptor_length == 0x07)
3876 {
3877 ca_provider = b2i(2, buffer + 5);
3878 }
3879 else if((ca_system_id >> 8 == 0x4A || ca_system_id == 0x2710) && descriptor_length > 0x04)
3880 {
3881 ca_provider = buffer[4];
3882
3883 if(caid_is_dre(ca_system_id) && descriptor_length == 0x0A)
3884 {
3885 ca_data = b2i(4, buffer + 6);
3886 snprintf(txt, 40, "CA DATA: %X", ca_data);
3887 }
3888 }
3889
3890 dvbapi_add_ecmpid(demux_id, ca_system_id, ca_pid, ca_provider, ca_data, txt);
3891 }
3892}
3893
3894static void dvbapi_parse_pmt_descriptors(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint8_t *type)
3895{
3896 uint16_t i, j;
3897 uint8_t descriptor_tag, descriptor_length;
3898
3899 for(i = 0; i + 1 < length; i += 2 + descriptor_length)
3900 {
3901 descriptor_tag = buffer[i];
3902 descriptor_length = buffer[i + 1];
3903
3904 cs_log_dbg(D_DVBAPI, "Demuxer %d found %s descriptor (tag: %02X length: %02X)",
3905 demux_id, get_descriptor_tag_txt(descriptor_tag), descriptor_tag, descriptor_length);
3906
3907 switch(descriptor_tag)
3908 {
3909 case 0x05: // Registration descriptor
3910 {
3911 // "HDMV" format identifier is removed
3912 // OSCam does not need to know about Blu-ray
3913 const char format_identifiers_audio[10][5] =
3914 {
3915 "AC-3", "BSSD", "dmat", "DRA1", "DTS1",
3916 "DTS2", "DTS3", "EAC3", "mlpa", "Opus",
3917 };
3918
3919 for(j = 0; j < 10; j++)
3920 {
3921 if(memcmp(buffer + i + 2, format_identifiers_audio[j], 4) == 0)
3922 {
3923 *type = STREAM_AUDIO;
3924 break;
3925 }
3926 }
3927 break;
3928 }
3929
3930 case 0x09: // CA descriptor
3931 {
3932 dvbapi_parse_pmt_ca_descriptor(demux_id, buffer + i + 2, descriptor_length);
3933 break;
3934 }
3935
3936 case 0x59: // Subtitling descriptor (DVB)
3937 {
3938 *type = STREAM_SUBTITLE;
3939 break;
3940 }
3941
3942 case 0x6A: // AC-3 descriptor (DVB)
3943 case 0x7A: // Enhanced AC-3 descriptor (DVB)
3944 case 0x7B: // DTS descriptor (DVB)
3945 case 0x7C: // AAC descriptor (DVB)
3946 case 0x81: // AC-3 descriptor (ATSC)
3947 case 0xCC: // Enhanced AC-3 descriptor (ATSC)
3948 {
3949 *type = STREAM_AUDIO;
3950 break;
3951 }
3952
3953 case 0x7F: // Extension descriptor (DVB)
3954 {
3955 uint8_t extension_descriptor_tag = buffer[i + 2];
3956
3957 cs_log_dbg(D_DVBAPI, "Demuxer %d found %s descriptor (extension tag: %02X)",
3958 demux_id, get_extension_descriptor_txt(extension_descriptor_tag), extension_descriptor_tag);
3959
3960 switch(extension_descriptor_tag)
3961 {
3962 case 0x0E: // DTS-HD descriptor (DVB)
3963 case 0x0F: // DTS Neural descriptor (DVB)
3964 case 0x15: // AC-4 descriptor (DVB)
3965 *type = STREAM_AUDIO;
3966 break;
3967
3968 case 0x20: // TTML subtitling descriptor (DVB)
3969 *type = STREAM_SUBTITLE;
3970 break;
3971
3972 default:
3973 *type = STREAM_UNDEFINED;
3974 break;
3975 }
3976 break;
3977 }
3978
3979 default:
3980 break;
3981 }
3982 }
3983}
3984
3985void request_cw(struct s_client *client, ECM_REQUEST *er, int32_t demux_id, uint8_t delayed_ecm_check)
3986{
3987 if(!er) { return; }
3988
3989 int32_t filternum = dvbapi_set_section_filter(demux_id, er, -1); // set ecm filter to odd -> even and visaversa
3990 if(filternum < 0)
3991 {
3992 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting cw -> ecm filter was killed!", demux_id);
3993 NULLFREE(er);
3994 return;
3995 }
3996
3997 if(!delayed_ecm_check) // no delayed ecm check for this filter
3998 {
3999 memset(demux[demux_id].demux_fd[filternum].lastecmd5, 0, CS_ECMSTORESIZE); // no ecm delay check: zero it!
4000 }
4001 else
4002 {
4003 uint8_t md5tmp[MD5_DIGEST_LENGTH];
4004 MD5(er->ecm, er->ecmlen, md5tmp);
4005
4006 if(!memcmp(demux[demux_id].demux_fd[filternum].prevecmd5, md5tmp, CS_ECMSTORESIZE))
4007 {
4008 if(demux[demux_id].demux_fd[filternum].prevresult < E_NOTFOUND)
4009 {
4010 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting same ecm again! -> SKIP!", demux_id);
4011 NULLFREE(er);
4012 return;
4013 }
4014 else
4015 {
4016 cs_log_dbg(D_DVBAPI, "Demuxer %d requesting same ecm again (previous result was not found!)", demux_id);
4017 }
4018 }
4019 else if(!memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE))
4020 {
4021 if(demux[demux_id].demux_fd[filternum].lastresult < E_NOTFOUND)
4022 {
4023 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting same ecm again! -> SKIP!", demux_id);
4024 NULLFREE(er);
4025 return;
4026 }
4027 else
4028 {
4029 cs_log_dbg(D_DVBAPI, "Demuxer %d requesting same ecm again (previous result was not found!)", demux_id);
4030 }
4031 }
4032
4033 memcpy(demux[demux_id].demux_fd[filternum].prevecmd5, demux[demux_id].demux_fd[filternum].lastecmd5, CS_ECMSTORESIZE);
4034 demux[demux_id].demux_fd[filternum].prevresult = demux[demux_id].demux_fd[filternum].lastresult;
4035 memcpy(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE);
4036 demux[demux_id].demux_fd[filternum].lastresult = 0xFF;
4037 }
4038
4039 er->adapter_index = demux[demux_id].adapter_index;
4040 get_cw(client, er);
4041
4042#ifdef WITH_DEBUG
4043 char buf[ECM_FMT_LEN];
4044 format_ecm(er, buf, ECM_FMT_LEN);
4045 cs_log_dbg(D_DVBAPI, "Demuxer %d request controlword for ecm %s", demux_id, buf);
4046#endif
4047}
4048
4049void dvbapi_try_next_caid(int32_t demux_id, int8_t checked, uint32_t msgid)
4050{
4051 int32_t n, j, found = -1, started = 0;
4052 int32_t status = demux[demux_id].max_status;
4053
4054 for(j = status; j >= 0; j--) // largest status first!
4055 {
4056 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
4057 {
4058 //cs_log_dbg(D_DVBAPI,"Demuxer %d PID %d checked = %d status = %d (searching for pid with status = %d)",
4059 // demux_id, n, demux[demux_id].ECMpids[n].checked, demux[demux_id].ECMpids[n].status, j);
4060
4061 if(demux[demux_id].ECMpids[n].checked == checked && demux[demux_id].ECMpids[n].status == j)
4062 {
4063 found = n;
4064 openxcas_set_provid(demux[demux_id].ECMpids[found].PROVID);
4065 openxcas_set_caid(demux[demux_id].ECMpids[found].CAID);
4066 openxcas_set_ecm_pid(demux[demux_id].ECMpids[found].ECM_PID);
4067
4068 // fixup for cas that need emm first!
4069 if(caid_is_irdeto(demux[demux_id].ECMpids[found].CAID) || (caid_is_dre(demux[demux_id].ECMpids[found].CAID)
4070 && ((demux[demux_id].ECMpids[found].PROVID == 0x11 || demux[demux_id].ECMpids[found].PROVID == 0xFE))))
4071 {
4072 demux[demux_id].emmstart.time = 0;
4073 }
4074
4075 started = dvbapi_start_descrambling(demux_id, found, checked, msgid);
4076 if(cfg.dvbapi_requestmode == 0 && started == 1)
4077 {
4078 return; // in requestmode 0 we only start 1 ecm request at the time
4079 }
4080 }
4081 }
4082 }
4083
4084 if(found == -1 && demux[demux_id].pidindex == -1)
4085 {
4086 cs_log("Demuxer %d no suitable readers found that can be used for decoding!", demux_id);
4087 return;
4088 }
4089}
4090
4091static void dvbapi_parse_pmt_program_info(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint8_t *ca_pmt_cmd_id)
4092{
4093 uint16_t i, offset = 0;
4094 uint8_t descriptor_tag, descriptor_length;
4095
4096 if(ca_pmt_cmd_id != NULL) // We are on CA PMT parsing
4097 {
4098 *ca_pmt_cmd_id = buffer[0];
4099 offset = 1;
4100 }
4101
4102 for(i = offset; i + 1 < length; i += 2 + descriptor_length)
4103 {
4104 descriptor_tag = buffer[i];
4105 descriptor_length = buffer[i + 1];
4106
4107 if(descriptor_tag == 0x09) // We only care about CA descriptors at program level
4108 {
4109 dvbapi_parse_pmt_ca_descriptor(demux_id, buffer + i + 2, descriptor_length);
4110 }
4111 }
4112}
4113
4114static void dvbapi_parse_pmt_es_info(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint8_t *ca_pmt_cmd_id, uint16_t *video_pid)
4115{
4116 uint16_t i, elementary_pid, es_info_length, offset = 0;
4117 uint8_t stream_type, type;
4118
4119 for(i = 0; i + 4 < length; i += 5 + es_info_length)
4120 {
4121 if(demux[demux_id].STREAMpidcount >= MAX_STREAM_PIDS)
4122 {
4123 cs_log("Demuxer %d reached maximum number of elementary streams", demux_id);
4124 break;
4125 }
4126
4127 type = STREAM_UNDEFINED;
4128 stream_type = buffer[i];
4129 elementary_pid = b2i(2, buffer + i + 1) & 0x1FFF;
4130 es_info_length = b2i(2, buffer + i + 3) & 0x0FFF;
4131
4132 cs_log_dbg(D_DVBAPI,"Demuxer %d found %s stream (type: %02X pid: %04X)",
4133 demux_id, get_stream_type_txt(stream_type), stream_type, elementary_pid);
4134
4135 if(es_info_length != 0 && es_info_length < length)
4136 {
4137 if(ca_pmt_cmd_id != NULL) // We are on CA PMT parsing
4138 {
4139 // Only enigma2, Spark and VDR follow the CA PMT specification ("ca_pmt_cmd_id"
4140 // shall be present in the ES info loop). For the first two, checking for boxtype
4141 // "dreambox" is sufficient, but for VDR this is not enough, because it shares
4142 // the same boxtype with tvheadend. So, for every other box (including VDR and
4143 // tvheadend), we stick to the old style check based on the value (descriptors
4144 // with tag 0x00 or 0x01 are not allowed, so this works), while for enigma2 we
4145 // do a proper check, because the "ca_pmt_cmd_id" can also take greater values.
4146 if(cfg.dvbapi_boxtype == BOXTYPE_DREAMBOX)
4147 {
4148 *ca_pmt_cmd_id = buffer[i + 5]; // It should be identical for all ES and the same as in program info
4149 offset = 1;
4150 }
4151 else
4152 {
4153 offset = (buffer[i + 5] <= 0x01) ? 1 : 0;
4154 }
4155 }
4156
4157 // Parse descriptors at ES level
4158 dvbapi_parse_pmt_descriptors(demux_id, buffer + i + 5 + offset, es_info_length, &type);
4159 }
4160
4161 // Get basic stream type (video, audio, subtitle) for each ES pid
4162 switch(stream_type)
4163 {
4164 case 0x01:
4165 case 0x02:
4166 case 0x10:
4167 case 0x1B:
4168 case 0x20:
4169 case 0x24:
4170 case 0x25:
4171 case 0x42:
4172 case 0xD1:
4173 case 0xEA:
4174 if(*video_pid == 0)
4175 {
4176 *video_pid = elementary_pid;
4177 }
4178 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_VIDEO;
4179 break;
4180
4181 case 0x03:
4182 case 0x04:
4183 case 0x0F:
4184 case 0x11:
4185 case 0x1C:
4186 case 0x2D:
4187 case 0x2E:
4188 case 0x81:
4189 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_AUDIO;
4190 break;
4191
4192 case 0x06:
4193 //case 0x81: some ATSC AC-3 streams do not contain the AC-3 descriptor!
4194 case 0x87:
4195 // Set the type based on the descriptors for these stream types
4196 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = type;
4197 break;
4198
4199 default:
4200 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_UNDEFINED;
4201 break;
4202 }
4203
4204 demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount] = elementary_pid;
4205 demux[demux_id].STREAMpidcount++;
4206 }
4207}
4208
4209static void dvbapi_parse_pmt_info(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint16_t pcr_pid, uint8_t *ca_pmt_cmd_id)
4210{
4211 uint16_t i, program_info_length, video_pid = 0;
4212
4213 // Cleanout demuxer from possible stale info
4214 // (reset ECM pids and streams)
4215 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
4216 {
4217 demux[demux_id].ECMpids[i].streams = 0;
4218 }
4219
4220 demux[demux_id].ECMpidcount = 0;
4221 demux[demux_id].STREAMpidcount = 0;
4222
4223 // Parse program info
4224 // In case of CA PMT, read the ca_pmt_cmd_id as well
4225 program_info_length = b2i(2, buffer) & 0x0FFF;
4226 if(program_info_length != 0 && program_info_length < length)
4227 {
4228 dvbapi_parse_pmt_program_info(demux_id, buffer + 2, program_info_length, ca_pmt_cmd_id);
4229 }
4230
4231 // Parse elementary stream info
4232 // In case of CA PMT, read the ca_pmt_cmd_id for each stream as well
4233 dvbapi_parse_pmt_es_info(demux_id, buffer + 2 + program_info_length, length - 2 - program_info_length, ca_pmt_cmd_id, &video_pid);
4234
4235 cs_log("Demuxer %d found %d ECM pids and %d STREAM pids in %sPMT", demux_id,
4236 demux[demux_id].ECMpidcount, demux[demux_id].STREAMpidcount, ca_pmt_cmd_id != NULL ? "CA " : "");
4237
4238 // Various retarded boxes misuse the "ca_pmt_cmd_id" value,
4239 // usually by setting it to zero. If we are on CA PMT parsing,
4240 // make sure we pass a value we can work with later on.
4241 if(ca_pmt_cmd_id != NULL)
4242 {
4243 *ca_pmt_cmd_id = (*ca_pmt_cmd_id < CA_PMT_CMD_OK_DESCRAMBLING) ? CA_PMT_CMD_OK_DESCRAMBLING : *ca_pmt_cmd_id;
4244 }
4245
4246 // If no elementary streams are available, set the PMT pid as the
4247 // first stream (PMT cannot be encrypted, like it was mentioned
4248 // in the old comment, so not sure why this is needed...)
4249 if(demux[demux_id].STREAMpidcount == 0)
4250 {
4251 demux[demux_id].STREAMpids[0] = demux[demux_id].pmtpid;
4252 demux[demux_id].STREAMpidsType[0] = STREAM_VIDEO;
4253 demux[demux_id].STREAMpidcount++;
4254 video_pid = demux[demux_id].pmtpid;
4255 }
4256
4257 // Fix for channels not listing the video pid inside the PMT: use the pcr pid instead
4258 if(video_pid == 0 && pcr_pid != 0x1FFF)
4259 {
4260 demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount] = pcr_pid;
4261 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_VIDEO;
4262 demux[demux_id].STREAMpidcount++;
4263 video_pid = pcr_pid;
4264
4265 cs_log("Demuxer %d found no video pid. Using the PCR pid %04X instead", demux_id, pcr_pid);
4266 }
4267
4268 // Register found video pid on all ECM pids of this demuxer
4269 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
4270 {
4271 demux[demux_id].ECMpids[i].VPID = video_pid;
4272 }
4273
4274 // Search for dvbapi priority entries for this program
4275 if(dvbapi_priority != NULL)
4276 {
4277 dvbapi_priority_read_entry_add(demux_id, video_pid);
4278 dvbapi_priority_read_entry_map(demux_id);
4279 dvbapi_priority_read_entry_extra(demux_id);
4280 }
4281}
4282
4283typedef struct demux_parameters
4284{
4285 uint8_t demux_index;
4286 uint8_t adapter_index;
4287 uint32_t ca_mask;
4288 uint16_t program_number;
4289 uint16_t pmtpid;
4290 uint16_t onid;
4291 uint16_t tsid;
4292 uint32_t ens;
4293} demux_parameters_t;
4294
4295static void get_demux_parameters(const uint8_t *buffer, demux_parameters_t *parameters)
4296{
4297 parameters->ca_mask = 1;
4298 parameters->demux_index = 0;
4299 parameters->adapter_index = 0;
4300 parameters->pmtpid = 0;
4301 parameters->program_number = b2i(2, buffer + 1);
4302
4303 uint16_t program_info_length = b2i(2, buffer + 4) & 0x0FFF;
4304 uint16_t pos = 7; // 4 + 2 (program_info_length) + 1 (ca_pmt_cmd_id)
4305
4306 while(pos + 1 < 5 + program_info_length)
4307 {
4308 uint8_t descriptor_tag = buffer[pos];
4309 uint8_t descriptor_length = buffer[pos + 1];
4310
4311 switch(descriptor_tag)
4312 {
4313 case CA:
4314 {
4315 break;
4316 }
4317
4318 case ENIGMA_NAMESPACE:
4319 {
4320 if(descriptor_length == 0x08)
4321 {
4322 parameters->ens = b2i(4, buffer + pos + 2);
4323 parameters->tsid = b2i(2, buffer + pos + 6);
4324 parameters->onid = b2i(2, buffer + pos + 8);
4325 }
4326 break;
4327 }
4328
4329 case DEMUX_CA_MASK_ADAPTER:
4330 {
4331 if(descriptor_length == 0x02 && (cfg.dvbapi_boxtype == BOXTYPE_PC ||
4332 cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO))
4333 {
4334 parameters->demux_index = buffer[pos + 2]; // Usually 0, but not always
4335 parameters->adapter_index = buffer[pos + 3]; // Can be 0, 1, 2, ...
4336 parameters->ca_mask = (1 << parameters->adapter_index); // use adapter_index as ca_mask
4337 }
4338 else if(descriptor_length == 0x03 && cfg.dvbapi_boxtype == BOXTYPE_QBOXHD)
4339 {
4340 // ca_mask = buffer[pos + 2]; // with STONE 1.0.4 always 0x01
4341 parameters->demux_index = buffer[pos + 3]; // with STONE 1.0.4 always 0x00
4342 parameters->adapter_index = buffer[pos + 4]; // with STONE 1.0.4 adapter index can be 0, 1, 2
4343 parameters->ca_mask = (1 << parameters->adapter_index); // use adapter_index as ca_mask
4344 }
4345 else if(descriptor_length == 0x02) // enigma2
4346 {
4347 parameters->ca_mask = buffer[pos + 2];
4348 uint8_t demux_tmp = buffer[pos + 3];
4349
4350 if(demux_tmp >= 8 && parameters->ca_mask == 0) // openpli based images
4351 {
4352 parameters->ca_mask = 1 << demux_tmp;
4353 }
4354
4355 if(demux_tmp == 0xFF) // tryfix prismcube (0xFF -> "demux-1" = error!)
4356 {
4357 demux_tmp = 0;
4358 }
4359 parameters->demux_index = demux_tmp;
4360 }
4361 break;
4362 }
4363
4364 case ADAPTER_DEVICE:
4365 {
4366 if(descriptor_length == 0x01)
4367 {
4368 parameters->adapter_index = buffer[pos + 2];
4369 }
4370 break;
4371 }
4372
4373 case PMT_PID:
4374 {
4375 if(descriptor_length == 0x02)
4376 {
4377 parameters->pmtpid = b2i(2, buffer + pos + 2);
4378 }
4379 break;
4380 }
4381
4382 case SERVICE_TYPE_MASK:
4383 break;
4384
4385 case DEMUX_DEVICE:
4386 {
4387 if(descriptor_length == 0x01)
4388 {
4389 parameters->demux_index = buffer[pos + 2];
4390 parameters->ca_mask = 1 << parameters->demux_index;
4391 }
4392 break;
4393 }
4394
4395 case CA_DEVICE:
4396 {
4397 if(descriptor_length == 0x01)
4398 {
4399 parameters->ca_mask = 1 << buffer[pos + 2];
4400 }
4401 break;
4402 }
4403
4404 default:
4405 {
4406 cs_log_dbg(D_DVBAPI, "Skipped unsupported or CA PMT irrelevant descriptor (tag: %02X length: %02X)", descriptor_tag, descriptor_length);
4407 break;
4408 }
4409 }
4410
4411 pos += 2 + descriptor_length;
4412 }
4413}
4414
4415static void dvbapi_capmt_notify(struct demux_s *dmx)
4416{
4417 struct s_client *cl;
4418 for(cl = first_client->next; cl; cl = cl->next)
4419 {
4420 if((cl->typ == 'p' || cl->typ == 'r') && cl->reader && cl->reader->ph.c_capmt)
4421 {
4422 struct demux_s *curdemux;
4423 if(cs_malloc(&curdemux, sizeof(struct demux_s)))
4424 {
4425 memcpy(curdemux, dmx, sizeof(struct demux_s));
4426 add_job(cl, ACTION_READER_CAPMT_NOTIFY, curdemux, sizeof(struct demux_s));
4427 }
4428 }
4429 }
4430}
4431
4432static void dvbapi_prepare_descrambling(int32_t demux_id, uint32_t msgid)
4433{
4434 bool is_powervu = false, start_emm = true;
4435 char service_name[CS_SERVICENAME_SIZE];
4436
4437 // The CA PMT should have given us enough info to determine if descrambling
4438 // is possible. Parsing the (real) PMT is not necessary, unless we have a
4439 // PowerVu encrypted channel or (for some weird reason) no stream pids at all.
4440 // Actually, when no streams are available, we set the PMT pid as the 1st
4441 // stream pid, so we have to check against that. Finally, if the PMT pid is
4442 // not included in the CA PMT, we start the PAT filter instead.
4443
4444#ifdef WITH_EXTENDED_CW
4445 uint8_t i;
4446 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
4447 {
4448 if(caid_is_powervu(demux[demux_id].ECMpids[i].CAID))
4449 {
4450 is_powervu = true;
4451 break;
4452 }
4453 }
4454#endif
4455
4456 if(demux[demux_id].pmtpid == 0)
4457 {
4458 dvbapi_start_pat_filter(demux_id);
4459 }
4460 else if(demux[demux_id].STREAMpids[0] == demux[demux_id].pmtpid || is_powervu)
4461 {
4462 dvbapi_start_pmt_filter(demux_id);
4463 }
4464
4465 if(demux[demux_id].running)
4466 {
4467 disable_unused_streampids(demux_id); // disable all streampids not in use anymore
4468 }
4469
4470 if(!demux[demux_id].running && demux[demux_id].ECMpidcount != 0) // only start demuxer if it wasn't running
4471 {
4472 // remove all non important filtering
4473 // (there are images with limited amount of filters available!)
4474 dvbapi_stop_all_cat_emm_sdt_filtering(msgid);
4475
4476 get_servicename(dvbapi_client, demux[demux_id].program_number, demux[demux_id].ECMpids[0].PROVID,
4477 demux[demux_id].ECMpids[0].CAID, service_name, sizeof(service_name));
4478
4479 cs_log_dbg(D_DVBAPI, "Demuxer %d started descrambling for program %04X (%s) (fd: %d)",
4480 demux_id, demux[demux_id].program_number, service_name, demux[demux_id].socket_fd);
4481
4482 demux[demux_id].running = true; // mark channel as running
4483 openxcas_set_sid(demux[demux_id].program_number);
4484 demux[demux_id].decodingtries = -1;
4485 dvbapi_resort_ecmpids(demux_id);
4486 dvbapi_try_next_caid(demux_id, 0, msgid);
4487 cs_sleepms(1);
4488 }
4489 else if(demux[demux_id].ECMpidcount == 0) // FTA: do logging and part of ecm handler
4490 {
4491 get_servicename(dvbapi_client, demux[demux_id].program_number, NO_PROVID_VALUE, NO_CAID_VALUE,
4492 service_name, sizeof(service_name));
4493
4494 cs_log_dbg(D_DVBAPI, "Demuxer %d no descrambling needed for FTA program %04X (%s) (fd: %d)",
4495 demux_id, demux[demux_id].program_number, service_name, demux[demux_id].socket_fd);
4496
4497 demux[demux_id].running = false; // reset running flag
4498 demux[demux_id].pidindex = -1; // reset ecmpid used for descrambling
4499 dvbapi_stop_filter(demux_id, TYPE_ECM, msgid);
4500
4501 if(cfg.usrfileflag) // add to user log previous channel + time on channel
4502 {
4503 cs_statistics(dvbapi_client);
4504 }
4505
4506 dvbapi_client->last_srvid = demux[demux_id].program_number; // set new channel srvid
4507 dvbapi_client->last_caid = NO_CAID_VALUE; // FTA channels have no caid!
4508 dvbapi_client->last_provid = NO_PROVID_VALUE; // FTA channels have no provid!
4509 dvbapi_client->lastswitch = dvbapi_client->last = time((time_t *)0); // reset idle-Time & last switch
4510 }
4511
4512#if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
4513 // Don't start and Stop EMM Filters over and over again if we are on FTA
4514 if(dvbapi_client->last_caid == NO_CAID_VALUE)
4515 {
4516 start_emm = false;
4517 }
4518#endif
4519
4520 // only do emm setup if au enabled and not running!
4521 if(cfg.dvbapi_au > 0 && demux[demux_id].EMMpidcount == 0 && start_emm == true)
4522 {
4523 demux[demux_id].emm_filter = -1; // to register first run emmfilter start
4524
4525 if(demux[demux_id].emmstart.time == 1) // irdeto fetch emm cat direct!
4526 {
4527 // trick to let emm fetching start after 30 seconds to speed up zapping
4528 cs_ftime(&demux[demux_id].emmstart);
4529
4530 dvbapi_start_cat_filter(demux_id);
4531 }
4532 else
4533 {
4534 cs_ftime(&demux[demux_id].emmstart); // for all other caids delayed start!
4535 }
4536 }
4537}
4538
4539int32_t dvbapi_parse_capmt(const uint8_t *buffer, uint32_t length, int32_t connfd, char *pmtfile, uint16_t client_proto_version, uint32_t msgid)
4540{
4541 int32_t i, demux_id = -1;
4542 uint8_t ca_pmt_list_management, ca_pmt_cmd_id;
4543 bool is_update = false;
4544 demux_parameters_t parameters;
4545 memset(&parameters, 0, sizeof(parameters));
4546
4547#if defined WITH_COOLAPI || defined WITH_COOLAPI2
4548 ca_pmt_list_management = CA_PMT_LIST_ONLY;
4549#else
4550 ca_pmt_list_management = buffer[0];
4551#endif
4552
4553 // We received a new list of CA PMT objects.
4554 // Mark all demuxers to stop descrambling, but do not actually stop any of them,
4555 // until we verify the new list does not contain any previously selected program.
4556 if(ca_pmt_list_management == CA_PMT_LIST_FIRST || ca_pmt_list_management == CA_PMT_LIST_ONLY)
4557 {
4558 for(i = 0; i < MAX_DEMUX; i++)
4559 {
4560 // Skip empty demuxers, demuxers belonging to different
4561 // CA PMT connections or handled by different PMT files.
4562 if(demux[i].program_number == 0 || demux[i].socket_fd != connfd ||
4563 (demux[i].socket_fd == -1 && pmtfile && strcmp(demux[i].pmt_file, pmtfile) != 0))
4564 {
4565 continue;
4566 }
4567
4568 demux[i].stop_descrambling = true; // Mark for deletion if not used again by following CA PMT objects
4569
4570 cs_log_dbg(D_DVBAPI, "Demuxer %d marked to stop descrambling for program %04X (fd: %d)",
4571 i, demux[i].program_number, connfd);
4572 }
4573 }
4574
4575 // Read private descriptors inside the CA PMT message
4576 // in order to get adapter, demux, ca, pmt pid and more.
4577 get_demux_parameters(buffer, &parameters);
4578
4579 cs_log_dbg(D_DVBAPI, "Received CA PMT list management %d for program %04X (pmt pid: %04X adapter: %d demux: %d camask: %d)",
4580 ca_pmt_list_management, parameters.program_number, parameters.pmtpid, parameters.adapter_index,
4581 parameters.demux_index, parameters.ca_mask);
4582
4583 // Search current demuxers for having the same program
4584 // as the one we received in this CA PMT object.
4585 for(i = 0; i < MAX_DEMUX; i++)
4586 {
4587 if(demux[i].program_number == 0)
4588 {
4589 continue;
4590 }
4591
4592 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
4593 {
4594 parameters.demux_index = i; // fixup for ipbox
4595 }
4596
4597 bool full_check = true, matched = false;
4598
4599 if(config_enabled(WITH_COOLAPI) || config_enabled(WITH_COOLAPI2) || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
4600 {
4601 full_check = false;
4602 }
4603
4604 if(full_check)
4605 {
4606 matched = (connfd > 0 && demux[i].socket_fd == connfd) && demux[i].program_number == parameters.program_number;
4607 }
4608 else
4609 {
4610 matched = connfd > 0 && demux[i].program_number == parameters.program_number;
4611 }
4612
4613 if(matched)
4614 {
4615 if(full_check)
4616 {
4617 // In PMT mode 6, when zapping between channels with the same program number and PMT pid
4618 // (sometimes the case with satellite feeds), as all hardware parameters being the same
4619 // (adapter, demux, ca_mask, connfd), the new program is considered an existing one (matched).
4620 // The only reliable way to determine whether we actually have a new program is to compare
4621 // the enigma namespace, tsid and onid as well.
4622 if(demux[i].demux_index != parameters.demux_index || demux[i].ca_mask != parameters.ca_mask ||
4623 demux[i].adapter_index != parameters.adapter_index || demux[i].pmtpid != parameters.pmtpid ||
4624 demux[i].ens != parameters.ens || demux[i].tsid != parameters.tsid || demux[i].onid != parameters.onid)
4625 {
4626 continue;
4627 }
4628 }
4629
4630 // A program update is normally signaled by either a list management:
4631 // 1. UPDATE for an existing program
4632 // 2. ADD for an existing program (which according to the specifications should be treated as an UPDATE)
4633 // 3. ONLY for an existing program (with broken clients, in pmt modes other than 6)
4634 if(ca_pmt_list_management == CA_PMT_LIST_UPDATE || ca_pmt_list_management == CA_PMT_LIST_ADD
4635 || (cfg.dvbapi_pmtmode != 6 && ca_pmt_list_management == CA_PMT_LIST_ONLY))
4636 {
4637 is_update = true;
4638 cs_log("Demuxer %d received CA PMT update for program %04X", i, parameters.program_number);
4639 }
4640
4641 cs_log("Demuxer %d continues processing for program %04X", i, demux[i].program_number);
4642 openxcas_set_sid(parameters.program_number);
4643 demux[i].stop_descrambling = false; // don't stop current demuxer!
4644 demux_id = i;
4645 break; // no need to explore other demuxers since we have a match!
4646 }
4647 }
4648
4649 // We are currently processing the last object of the CA PMT list.
4650 // We should now stop descrambling all programs not included in this list.
4651 if(ca_pmt_list_management != CA_PMT_LIST_FIRST && ca_pmt_list_management != CA_PMT_LIST_MORE)
4652 {
4653 for(i = 0; i < MAX_DEMUX; i++)
4654 {
4655 if(demux[i].program_number == 0)
4656 {
4657 continue;
4658 }
4659
4660 if(demux[i].stop_descrambling)
4661 {
4662 dvbapi_stop_descrambling(i, msgid);
4663 }
4664 }
4665 }
4666
4667 // We continue reading the CA PMT object only when we get
4668 // a new program (demux_id == -1) or an updated program.
4669 if(!(demux_id == -1 || is_update))
4670 {
4671 return demux_id;
4672 }
4673
4674 // We received a CA PMT object for a new program.
4675 // Let's find an empty demuxer for it.
4676 if(demux_id == -1)
4677 {
4678 if(ca_pmt_list_management == CA_PMT_LIST_UPDATE)
4679 {
4680 cs_log("ERROR: Received CA PMT list update for unknown program");
4681 return -1;
4682 }
4683
4684 for(demux_id = 0; demux_id < MAX_DEMUX; demux_id++)
4685 {
4686 if(demux[demux_id].program_number != 0)
4687 {
4688 continue; // Skip occupied demuxers
4689 }
4690
4691 // Probably this box doesn't send any private descriptor in the
4692 // CA PMT, so we have to improvise before saving to the demuxer.
4693 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
4694 {
4695 parameters.ca_mask = demux_id + 1;
4696 parameters.demux_index = demux_id;
4697 }
4698
4699 demux[demux_id].demux_index = parameters.demux_index;
4700 demux[demux_id].adapter_index = parameters.adapter_index;
4701 demux[demux_id].ca_mask = parameters.ca_mask;
4702 demux[demux_id].socket_fd = connfd;
4703 demux[demux_id].client_proto_version = client_proto_version;
4704 demux[demux_id].program_number = parameters.program_number;
4705 demux[demux_id].pmtpid = parameters.pmtpid;
4706 demux[demux_id].ens = parameters.ens;
4707 demux[demux_id].tsid = parameters.tsid;
4708 demux[demux_id].onid = parameters.onid;
4709 demux[demux_id].stop_descrambling = false;
4710 demux[demux_id].running = false;
4711 demux[demux_id].sdt_filter = -1;
4712 demux[demux_id].rdr = NULL;
4713
4714 if(pmtfile)
4715 {
4716 cs_strncpy(demux[demux_id].pmt_file, pmtfile, sizeof(demux[demux_id].pmt_file));
4717 }
4718
4719 break;
4720 }
4721
4722 if(demux_id >= MAX_DEMUX)
4723 {
4724 cs_log("There is no free demuxer for the new program! Aborting...");
4725 return -1;
4726 }
4727 }
4728
4729 // We continue parsing the CA PMT info for new or updated programs.
4730 // For updated programs, we just delete all previous stream pids and
4731 // ECM pids and start parsing the fresh data.
4732 dvbapi_parse_pmt_info(demux_id, buffer + 4, length - 4, 0x1FFF, &ca_pmt_cmd_id);
4733
4734 // Finally, evaluate what response the host requires from OSCam.
4735 // This allows multiple CA applications to run at the host simultaneously.
4736 // "OK query" will be implemented at a later stage, when support is first
4737 // added in enigma2.
4738 switch(ca_pmt_cmd_id)
4739 {
4740 case CA_PMT_CMD_OK_DESCRAMBLING:
4741 {
4742 dvbapi_capmt_notify(&demux[demux_id]);
4743 dvbapi_prepare_descrambling(demux_id, msgid);
4744 return demux_id;
4745 }
4746
4747 case CA_PMT_CMD_QUERY:
4748 {
4749 cs_log("Received unsupported CA PMT command ID 'query' for program %04X", demux[demux_id].program_number);
4750 dvbapi_stop_descrambling(demux_id, msgid); // Clear all data from this demuxer
4751 return -1;
4752 }
4753
4754 case CA_PMT_CMD_NOT_SELECTED:
4755 {
4756 cs_log("Program %04X is not selected for descrambling", demux[demux_id].program_number);
4757 dvbapi_stop_descrambling(demux_id, msgid); // Clear all data from this demuxer
4758 return -1;
4759 }
4760
4761 default:
4762 {
4763 cs_log("Received unknown or unsupported CA PMT command ID %02X from host", ca_pmt_cmd_id);
4764 dvbapi_stop_descrambling(demux_id, msgid);
4765 return -1;
4766 }
4767 }
4768}
4769
4770static void dvbapi_parse_pmt(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
4771{
4772 uint16_t program_number = b2i(2, buffer + 3);
4773 uint16_t pcr_pid = b2i(2, buffer + 8) & 0x1FFF;
4774
4775 if(program_number != demux[demux_id].program_number)
4776 {
4777 cs_log("Demuxer %d received PMT for undefined program %04X", demux_id, program_number);
4778 return;
4779 }
4780
4781 dvbapi_stop_filter(demux_id, TYPE_PMT, msgid);
4782 dvbapi_parse_pmt_info(demux_id, buffer + 10, length - 10 - 4, pcr_pid, NULL); // last 4 bytes are the CRC-32
4783}
4784
4785static void dvbapi_create_srvid_line(int32_t demux_id, char *buffer, uint32_t buflen)
4786{
4787 int32_t i, j, pos = 0;
4788 uint16_t caid_done[32], cur_caid;
4789 uint8_t caid_done_count = 0, skip_caid;
4790
4791 if(demux[demux_id].ECMpidcount == 0)
4792 {
4793 snprintf(buffer, buflen, "%04X@%06X", NO_CAID_VALUE, NO_PROVID_VALUE);
4794 return;
4795 }
4796
4797 for(i = 0; i < demux[demux_id].ECMpidcount && i < 32; i++)
4798 {
4799 skip_caid = 0;
4800 for(j = 0; j < caid_done_count; j++)
4801 {
4802 if(caid_done[j] == demux[demux_id].ECMpids[i].CAID)
4803 {
4804 skip_caid = 1;
4805 break;
4806 }
4807 }
4808
4809 if(skip_caid)
4810 {
4811 continue;
4812 }
4813
4814 cur_caid = demux[demux_id].ECMpids[i].CAID;
4815 pos += snprintf(buffer + pos, buflen - pos, "%s%04X", caid_done_count > 0 ? "," : "", cur_caid == 0 ? NO_CAID_VALUE : cur_caid);
4816
4817 for(j = i; j < demux[demux_id].ECMpidcount; j++)
4818 {
4819 if(demux[demux_id].ECMpids[j].PROVID == 0)
4820 {
4821 continue;
4822 }
4823
4824 if(cur_caid == demux[demux_id].ECMpids[j].CAID)
4825 {
4826 pos += snprintf(buffer + pos, buflen - pos, "@%06X", demux[demux_id].ECMpids[j].PROVID);
4827 }
4828 }
4829
4830 caid_done[caid_done_count] = demux[demux_id].ECMpids[i].CAID;
4831 caid_done_count++;
4832 }
4833}
4834
4835static void dvbapi_write_sdt_info(int32_t demux_id, const char *provider_name, const char* service_name, const char *service_type)
4836{
4837 int8_t did_save_srvid = 0;
4838 int32_t provid, caid, pidindex;
4839 char tmp[256], srvid_line[1024];
4840 FILE *fpsave = NULL;
4841
4842 pidindex = demux[demux_id].pidindex;
4843 if(pidindex != -1)
4844 {
4845 caid = demux[demux_id].ECMpids[pidindex].CAID;
4846 provid = demux[demux_id].ECMpids[pidindex].PROVID;
4847 }
4848 else
4849 {
4850 if(demux[demux_id].ECMpidcount == 0 || demux[demux_id].ECMpids[0].CAID == 0)
4851 {
4852 caid = NO_CAID_VALUE;
4853 provid = NO_PROVID_VALUE;
4854 }
4855 else
4856 {
4857 caid = demux[demux_id].ECMpids[0].CAID;
4858 provid = demux[demux_id].ECMpids[0].PROVID;
4859 }
4860 }
4861
4862 if(cs_strlen(provider_name) && caid != NO_CAID_VALUE)
4863 {
4864 get_providername_or_null(provid, caid, tmp, sizeof(tmp));
4865
4866 if(tmp[0] == '\0')
4867 {
4868 get_config_filename(tmp, sizeof(tmp), "oscam.provid");
4869
4870 if((fpsave = fopen(tmp, "a")))
4871 {
4872 fprintf(fpsave, "\n%04X@%06X|%s|", caid, provid, provider_name);
4873 fclose(fpsave);
4874 init_provid();
4875 }
4876 }
4877 }
4878
4879 if(cs_strlen(service_name))
4880 {
4881 get_servicename_or_null(cur_client(), demux[demux_id].program_number, provid, caid, tmp, sizeof(tmp));
4882
4883 if(tmp[0] == '\0')
4884 {
4885 get_config_filename(tmp, sizeof(tmp), "oscam.srvid2");
4886
4887 if(!access(tmp, F_OK) && (fpsave = fopen(tmp, "a")))
4888 {
4889 if((caid != NO_CAID_VALUE) || (cfg.dvbapi_read_sdt > 1))
4890 {
4891 dvbapi_create_srvid_line(demux_id, srvid_line, sizeof(srvid_line));
4892
4893 if(cfg.dvbapi_write_sdt_prov)
4894 {
4895 fprintf(fpsave, "\n%04X:%s|%s|%s||%s", demux[demux_id].program_number, srvid_line, service_name, service_type, provider_name);
4896 }
4897 else
4898 {
4899 fprintf(fpsave, "\n%04X:%s|%s|%s", demux[demux_id].program_number, srvid_line, service_name, service_type);
4900 }
4901
4902 did_save_srvid = 1;
4903 }
4904 }
4905 else
4906 {
4907 get_config_filename(tmp, sizeof(tmp), "oscam.srvid");
4908
4909 if((fpsave = fopen(tmp, "a")))
4910 {
4911 if((caid != NO_CAID_VALUE) || (cfg.dvbapi_read_sdt > 1))
4912 {
4913 dvbapi_create_srvid_line(demux_id, srvid_line, sizeof(srvid_line));
4914
4915 if(cfg.dvbapi_write_sdt_prov)
4916 {
4917 fprintf(fpsave, "\n%s:%04X|%s|%s|%s", srvid_line, demux[demux_id].program_number, provider_name, service_name, service_type);
4918 }
4919 else
4920 {
4921 fprintf(fpsave, "\n%s:%04X||%s|%s", srvid_line, demux[demux_id].program_number, service_name, service_type);
4922 }
4923
4924 did_save_srvid = 1;
4925 }
4926 }
4927 }
4928
4929 if(fpsave)
4930 {
4931 fclose(fpsave);
4932 }
4933
4934 if(did_save_srvid)
4935 {
4936 init_srvid();
4937 }
4938 }
4939 }
4940}
4941
4942static uint32_t dvbapi_extract_sdt_string(char *buf, uint32_t buflen, const uint8_t *source, uint8_t sourcelen)
4943{
4944 uint32_t i, j, offset = 0;
4945 int8_t iso_mode = -1;
4946 char *tmpbuf;
4947 const uint8_t *ptr_in;
4948 uint8_t *ptr_out;
4949 size_t in_bytes, out_bytes;
4950
4951 if(sourcelen == 0)
4952 {
4953 buf[0] = '\0';
4954 return 1;
4955 }
4956
4957 if(!cs_malloc(&tmpbuf, buflen))
4958 {
4959 return 0;
4960 }
4961
4962 if(sourcelen > buflen - 1)
4963 {
4964 sourcelen = buflen - 1;
4965 }
4966
4967 if(sourcelen > 0 && source[0] < 0x20)
4968 {
4969 if(source[0] >= 0x01 && source[0] <= 0x0B && source[0] != 0x08) // ISO/IEC 8859
4970 {
4971 offset = 1;
4972 iso_mode = 4 + source[0];
4973 }
4974 else if(source[0] == 0x10) // Dynamically selected part of ISO/IEC 8859
4975 {
4976 if(source[1] == 0x00 && source[2] >= 0x01 && source[2] <= 0x0F && source[2] != 0x0C)
4977 {
4978 offset = 3;
4979 iso_mode = source[2];
4980 }
4981 }
4982 else if(source[0] == 0x11) // ISO/IEC 10646
4983 {
4984 offset = 1;
4985 iso_mode = -2;
4986 }
4987 // missing: 0x12 KSX1001-2004 (Korean Character Set)
4988 // missing: 0x13 GB-2312-1980 (Simplified Chinese Character Set)
4989 // missing: 0x14 Big5 subset of ISO/IEC 10646 (Traditional Chinese)
4990 else if(source[0] == 0x15) // UTF-8 encoding of ISO/IEC 10646
4991 {
4992 offset = 1;
4993 iso_mode = -3;
4994 }
4995 // missing: 0x1F Described by encoding_type_id
4996 else
4997 {
4998 NULLFREE(tmpbuf);
4999 return 0;
5000 }
5001 }
5002
5003 if(offset >= sourcelen)
5004 {
5005 NULLFREE(tmpbuf);
5006 return 0;
5007 }
5008
5009 if(iso_mode >= -1)
5010 {
5011 for(i = 0, j = 0; i < sourcelen - offset; i++)
5012 {
5013 if(source[offset + i] >= 0x80 && source[offset + i] <= 0x9F)
5014 {
5015 continue;
5016 }
5017
5018 tmpbuf[j] = source[offset + i];
5019 j++;
5020 }
5021
5022 tmpbuf[j] = '\0';
5023 }
5024
5025 ptr_in = (const uint8_t *)tmpbuf;
5026 in_bytes = cs_strlen(tmpbuf);
5027 ptr_out = (uint8_t *)buf;
5028 out_bytes = buflen;
5029
5030#ifdef READ_SDT_CHARSETS
5031 if(iso_mode >= -1)
5032 {
5033 memset(buf, 0, buflen);
5034 cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: %d offset: %u", iso_mode, offset);
5035 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)tmpbuf, in_bytes, "sdt-info dbg: raw string:");
5036
5037 if(iso_mode == -1)
5038 {
5039 if(ISO6937toUTF8(&ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
5040 {
5041 cs_log_dbg(D_DVBAPI, "sdt-info error: ISO6937toUTF8 failed");
5042 NULLFREE(tmpbuf);
5043 return 0;
5044 }
5045 }
5046 else
5047 {
5048 if(ISO8859toUTF8(iso_mode, &ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
5049 {
5050 cs_log_dbg(D_DVBAPI, "sdt-info error: ISO8859toUTF8 failed");
5051 NULLFREE(tmpbuf);
5052 return 0;
5053 }
5054 }
5055 }
5056#else
5057 if(iso_mode >= -1)
5058 {
5059 cs_strncpy(buf, tmpbuf, buflen);
5060 cs_log_dbg(D_DVBAPI, "sdt-info warning: your build of oscam does not support iso-to-utf8 conversion, special chars may be corrupted!");
5061 }
5062#endif
5063 else if(iso_mode == -2)
5064 {
5065 memset(buf, 0, buflen);
5066 cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: %d offset: %u", iso_mode, offset);
5067
5068 if(UnicodetoUTF8(&ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
5069 {
5070 cs_log_dbg(D_DVBAPI, "sdt-info error: UnicodetoUTF8 failed");
5071 NULLFREE(tmpbuf);
5072 return 0;
5073 }
5074 }
5075 else if(iso_mode == -3) // No conversion, already in UTF-8
5076 {
5077 memcpy(buf, source + offset, sourcelen - offset);
5078 buf[sourcelen - offset] = '\0';
5079 cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: -3 offset: %u", offset);
5080 }
5081
5082 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)buf, cs_strlen(buf), "sdt-info dbg: encoded string:");
5083 NULLFREE(tmpbuf);
5084 return 1;
5085}
5086
5087static const char *dvbapi_get_service_type(uint8_t service_type)
5088{
5089 switch(service_type)
5090 {
5091 case 0x01:
5092 case 0x0B:
5093 case 0x11:
5094 case 0x16:
5095 case 0x17:
5096 case 0x18:
5097 case 0x19:
5098 case 0x1A:
5099 case 0x1B:
5100 case 0x1C:
5101 case 0x1D:
5102 case 0x1E:
5103 case 0x1F:
5104 case 0x20:
5105 return "TV";
5106
5107 case 0x02:
5108 case 0x07:
5109 case 0x0A:
5110 return "Radio";
5111
5112 case 0x03:
5113 return "Teletext";
5114
5115 case 0x0C:
5116 return "Data";
5117
5118 default:
5119 return "unknown";
5120 }
5121}
5122
5123static void dvbapi_parse_service_descriptor(int32_t demux_id, const uint8_t *buffer, uint8_t descriptor_length)
5124{
5125 uint8_t service_provider_name_length, service_name_length;
5126 char service_provider_name[64], service_name[64];
5127 const char *service_type;
5128
5129 if(descriptor_length < 3)
5130 {
5131 return; // Service descriptor has a minimum length of 3 bytes
5132 }
5133
5134 service_type = dvbapi_get_service_type(buffer[0]);
5135
5136 service_provider_name_length = buffer[1];
5137 if(2 + service_provider_name_length + 1 > descriptor_length)
5138 {
5139 return;
5140 }
5141
5142 service_name_length = buffer[2 + service_provider_name_length];
5143 if(2 + service_provider_name_length + 1 + service_name_length > descriptor_length)
5144 {
5145 return;
5146 }
5147
5148 if(!dvbapi_extract_sdt_string(service_provider_name, sizeof(service_provider_name), buffer + 2, service_provider_name_length) ||
5149 !dvbapi_extract_sdt_string(service_name, sizeof(service_name), buffer + 2 + service_provider_name_length + 1, service_name_length))
5150 {
5151 return;
5152 }
5153
5154 cs_log_dbg(D_DVBAPI, "Demuxer %d got service info (provider: %s - name: %s - type: %s)",
5155 demux_id, service_provider_name, service_name, service_type);
5156
5157 dvbapi_write_sdt_info(demux_id, service_provider_name, service_name, service_type);
5158}
5159
5160static void dvbapi_parse_sdt(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
5161{
5162 uint8_t descriptor_tag, descriptor_length;
5163 uint16_t service_id, descriptors_loop_length, i, j;
5164
5165 if(buffer[0] != 0x42) // SDT sections with table_id value 0x42 describe the actual TS
5166 {
5167 return;
5168 }
5169
5170 // Get the tsid and onid (in enigma2 STBs we have
5171 // already received them in the CA PMT message)
5172 demux[demux_id].tsid = b2i(2, buffer + 3);
5173 demux[demux_id].onid = b2i(2, buffer + 8);
5174
5175 for(i = 11; i + 5 < length; i += 5 + descriptors_loop_length)
5176 {
5177 service_id = b2i(2, buffer + i);
5178 descriptors_loop_length = b2i(2, buffer + i + 3) & 0x0FFF;
5179
5180 if(service_id != demux[demux_id].program_number)
5181 {
5182 continue;
5183 }
5184
5185 for(j = 0; j + 1 < descriptors_loop_length; j += 2 + descriptor_length)
5186 {
5187 descriptor_tag = buffer[i + 5 + j];
5188 descriptor_length = buffer[i + 5 + j + 1];
5189
5190 if(descriptor_tag == 0x48)
5191 {
5192 dvbapi_parse_service_descriptor(demux_id, buffer + i + 5 + j + 2, descriptor_length);
5193 }
5194 }
5195
5196 dvbapi_stop_filter(demux_id, TYPE_SDT, msgid);
5197 break;
5198 }
5199}
5200
5201static void dvbapi_parse_pat(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
5202{
5203 uint16_t i, srvid;
5204
5205 dvbapi_stop_filter(demux_id, TYPE_PAT, msgid);
5206
5207 for(i = 8; i + 7 < length; i += 4)
5208 {
5209 srvid = b2i(2, buffer + i);
5210 if(srvid == 0)
5211 {
5212 continue;
5213 }
5214
5215 if(demux[demux_id].program_number == srvid)
5216 {
5217 demux[demux_id].pmtpid = b2i(2, buffer + i + 2) & 0x1FFF;
5218 dvbapi_start_pmt_filter(demux_id);
5219 break;
5220 }
5221 }
5222}
5223
5224int32_t dvbapi_init_listenfd(void)
5225{
5226 int32_t clilen, listenfd;
5227 struct sockaddr_un servaddr;
5228
5229 memset(&servaddr, 0, sizeof(struct sockaddr_un));
5230 servaddr.sun_family = AF_UNIX;
5231 cs_strncpy(servaddr.sun_path, devices[selected_box].cam_socket_path, sizeof(servaddr.sun_path));
5232 clilen = sizeof(servaddr.sun_family) + cs_strlen(servaddr.sun_path);
5233
5234 if(((unlink(devices[selected_box].cam_socket_path) < 0) && (errno != ENOENT))
5235 || ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
5236 || (bind(listenfd, (struct sockaddr *)&servaddr, clilen) < 0)
5237 || (listen(listenfd, 5) < 0))
5238 {
5239 return 0;
5240 }
5241
5242 // change the access right on the camd.socket
5243 // this will allow oscam to run as root if needed
5244 // and still allow non root client to connect to the socket
5245 chmod(devices[selected_box].cam_socket_path, S_IRWXU | S_IRWXG | S_IRWXO);
5246 return listenfd;
5247}
5248
5249int32_t dvbapi_net_init_listenfd(void)
5250{
5251 int32_t listenfd;
5252 struct SOCKADDR servaddr;
5253
5254 memset(&servaddr, 0, sizeof(servaddr));
5255 SIN_GET_FAMILY(servaddr) = DEFAULT_AF;
5256 SIN_GET_ADDR(servaddr) = ADDR_ANY;
5257 SIN_GET_PORT(servaddr) = htons((uint16_t)cfg.dvbapi_listenport);
5258
5259 if((listenfd = socket(DEFAULT_AF, SOCK_STREAM, 0)) < 0)
5260 {
5261 return 0;
5262 }
5263 int32_t opt = 0;
5264
5265#ifdef IPV6SUPPORT
5266// azbox toolchain do not have this define
5267#ifndef IPV6_V6ONLY
5268#define IPV6_V6ONLY 26
5269#endif
5270 // set the server socket option to listen on IPv4 and IPv6 simultaneously
5271 setsockopt(listenfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&opt, sizeof(opt));
5272#endif
5273
5274 opt = 1;
5275 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
5276 set_so_reuseport(listenfd);
5277
5278 if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
5279 {
5280 return 0;
5281 }
5282
5283 if(listen(listenfd, 5) < 0)
5284 {
5285 return 0;
5286 }
5287 return listenfd;
5288}
5289
5290static pthread_mutex_t event_handler_lock = PTHREAD_MUTEX_INITIALIZER;
5291
5292void event_handler(int32_t UNUSED(signal))
5293{
5294 struct stat pmt_info;
5295 char dest[1024];
5296 DIR *dirp;
5297 struct dirent entry, *dp = NULL;
5298 int32_t i;
5299 int32_t pmt_fd = -1;
5300 uint8_t mbuf[2048]; // dirty fix: larger buffer needed for CA PMT mode 6 with many parallel channels to decode
5301
5302 if(dvbapi_client != cur_client())
5303 {
5304 return;
5305 }
5306
5307 SAFE_MUTEX_LOCK(&event_handler_lock);
5308 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
5309 {
5310 pausecam = 0;
5311 }
5312 else
5313 {
5314 int32_t standby_fd = open(STANDBY_FILE, O_RDONLY);
5315 pausecam = (standby_fd > 0) ? 1 : 0;
5316 if(standby_fd > 0)
5317 {
5318 int32_t ret = close(standby_fd);
5319 if(ret < 0)
5320 {
5321 cs_log("ERROR: Could not close standby fd (errno=%d %s)", errno, strerror(errno));
5322 }
5323 }
5324 }
5325
5326 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_pmtmode == 1)
5327 {
5328 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5329 return;
5330 }
5331
5332 for(i = 0; i < MAX_DEMUX; i++)
5333 {
5334 if(demux[i].pmt_file[0] != 0)
5335 {
5336 snprintf(dest, sizeof(dest), "%s%s", TMPDIR, demux[i].pmt_file);
5337 pmt_fd = open(dest, O_RDONLY);
5338 if(pmt_fd > 0)
5339 {
5340 if(fstat(pmt_fd, &pmt_info) != 0)
5341 {
5342 int32_t ret = close(pmt_fd);
5343 if(ret < 0)
5344 {
5345 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5346 }
5347 continue;
5348 }
5349
5350 if((time_t)pmt_info.st_mtime != demux[i].pmt_time)
5351 {
5352 dvbapi_stop_descrambling(i, 0);
5353 }
5354
5355 int32_t ret = close(pmt_fd);
5356 if(ret < 0)
5357 {
5358 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5359 }
5360 continue;
5361 }
5362 else
5363 {
5364 cs_log("Demuxer %d Unable to open PMT file %s -> stop descrambling!", i, dest);
5365 dvbapi_stop_descrambling(i, 0);
5366 }
5367 }
5368 }
5369
5370 if(disable_pmt_files)
5371 {
5372 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5373 return;
5374 }
5375
5376 dirp = opendir(TMPDIR);
5377 if(!dirp)
5378 {
5379 cs_log_dbg(D_DVBAPI, "opendir failed (errno=%d %s)", errno, strerror(errno));
5380 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5381 return;
5382 }
5383
5384 while(!cs_readdir_r(dirp, &entry, &dp))
5385 {
5386 if(!dp)
5387 {
5388 break;
5389 }
5390
5391 if(cs_strlen(dp->d_name) < 7)
5392 {
5393 continue;
5394 }
5395
5396 if(strncmp(dp->d_name, "pmt", 3) != 0 || strncmp(dp->d_name + cs_strlen(dp->d_name) - 4, ".tmp", 4) != 0)
5397 {
5398 continue;
5399 }
5400
5401#if defined(WITH_STAPI) || defined(WITH_STAPI5)
5402 struct s_dvbapi_priority *p;
5403 for(p = dvbapi_priority; p != NULL; p = p->next) // stapi: check if there is a device connected to this pmt file!
5404 {
5405 if(p->type != 's') { continue; } // stapi rule?
5406 if(strcmp(dp->d_name, p->pmtfile) != 0) { continue; } // same file?
5407 break; // found match!
5408 }
5409
5410 if(p == NULL)
5411 {
5412 cs_log_dbg(D_DVBAPI, "No matching S: line in oscam.dvbapi for pmtfile %s -> skip!", dp->d_name);
5413 continue;
5414 }
5415#endif
5416
5417 if (!cs_strlen(TMPDIR))
5418 {
5419 cs_log_dbg(D_DVBAPI, "BUG! cs_strlen(TMPDIR)!!!\n");
5420 continue;
5421 }
5422
5423 if (!cs_strlen(dp->d_name))
5424 {
5425 cs_log_dbg(D_DVBAPI, "BUG! cs_strlen(dp->d_name)!!!\n");
5426 continue;
5427 }
5428
5429 if((cs_strlen(dp->d_name) + cs_strlen(TMPDIR) - 1) > sizeof(dest))
5430 {
5431 cs_log_dbg(D_DVBAPI, "BUG! Sum of the (d_name + TMPDIR) = %u > sizeof(dest) !!!\n", (unsigned int)(cs_strlen(dp->d_name) + cs_strlen(TMPDIR) - 1));
5432 continue;
5433 }
5434 else
5435 {
5436 memcpy(dest, TMPDIR, cs_strlen(TMPDIR));
5437 memcpy(dest + cs_strlen(TMPDIR), dp->d_name, cs_strlen(dp->d_name));
5438 dest[cs_strlen(TMPDIR) + cs_strlen(dp->d_name)] = '\0';
5439 pmt_fd = open(dest, O_RDONLY);
5440 }
5441
5442 if(pmt_fd < 0)
5443 {
5444 continue;
5445 }
5446
5447 if(fstat(pmt_fd, &pmt_info) != 0)
5448 {
5449 int32_t ret = close(pmt_fd);
5450 if(ret < 0)
5451 {
5452 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5453 }
5454 continue;
5455 }
5456
5457 int32_t found = 0;
5458
5459 for(i = 0; i < MAX_DEMUX; i++)
5460 {
5461 if(strcmp(demux[i].pmt_file, dp->d_name) == 0)
5462 {
5463 if((time_t)pmt_info.st_mtime == demux[i].pmt_time)
5464 {
5465 found = 1;
5466 break;
5467 }
5468 }
5469 }
5470
5471 if(found)
5472 {
5473 int32_t ret = close(pmt_fd);
5474 if(ret < 0)
5475 {
5476 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5477 }
5478 continue;
5479 }
5480 cs_log_dbg(D_DVBAPI, "found pmt file %s", dest);
5481 cs_sleepms(100);
5482
5483 uint32_t len = read(pmt_fd, mbuf, sizeof(mbuf));
5484 int32_t ret = close(pmt_fd);
5485
5486 if(ret < 0)
5487 {
5488 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5489 }
5490
5491 if(len < 1)
5492 {
5493 cs_log_dbg(D_DVBAPI, "pmt file %s have invalid len!", dest);
5494 continue;
5495 }
5496
5497 int32_t demux_id;
5498#ifdef QBOXHD
5499 uint32_t j1, j2;
5500
5501 // QboxHD pmt.tmp is the full capmt written as a string of hex values
5502 // pmt.tmp must be longer than 3 bytes (6 hex chars) and even length
5503 if((len < 6) || ((len % 2) != 0) || ((len / 2) > sizeof(dest)))
5504 {
5505 cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, incorrect length");
5506 continue;
5507 }
5508
5509 for(j2 = 0, j1 = 0; j2 < len; j2 += 2, j1++)
5510 {
5511 unsigned int tmp;
5512 if(sscanf((char *)mbuf + j2, "%02X", &tmp) != 1)
5513 {
5514 cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, data not valid in position %d", j2);
5515 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5516 return;
5517 }
5518 else
5519 {
5520 memcpy(dest + j1, &tmp, 4);
5521 }
5522 }
5523
5524 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)dest, len / 2, "QboxHD pmt.tmp:");
5525 demux_id = dvbapi_parse_capmt((uint8_t *)dest + 4, (len / 2) - 4, -1, dp->d_name, 0, 0);
5526#else
5527 if(len > sizeof(dest))
5528 {
5529 cs_log_dbg(D_DVBAPI, "event_handler() dest buffer is to small for pmt data!");
5530 continue;
5531 }
5532
5533 if(len < 16)
5534 {
5535 cs_log_dbg(D_DVBAPI, "event_handler() received pmt is too small! (%d < 16 bytes!)", len);
5536 continue;
5537 }
5538
5539 cs_log_dump_dbg(D_DVBAPI, mbuf, len, "PMT file:"); // Original PMT file
5540
5541 // Do some tidying on the PMT file to make it compatible with the CA PMT parser
5542 dest[0] = CA_PMT_LIST_ONLY;
5543 memcpy(dest + 1, mbuf + 3, 2); // program_number
5544 uint16_t pmt_program_info_length = b2i(2, mbuf + 10) & 0x0FFF;
5545 i2b_buf(2, pmt_program_info_length + 1, (uint8_t *)dest + 4);
5546 dest[6] = CA_PMT_CMD_OK_DESCRAMBLING;
5547 memcpy(dest + 7, mbuf + 12, len - 12 - 4);
5548
5549 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)dest, 7 + len - 12 - 4, "CA PMT:"); // Actual CA PMT message
5550 demux_id = dvbapi_parse_capmt((uint8_t *)dest, 7 + len - 12 - 4, -1, dp->d_name, 0, 0);
5551#endif
5552
5553 if(demux_id >= 0)
5554 {
5555 cs_strncpy(demux[demux_id].pmt_file, dp->d_name, sizeof(demux[demux_id].pmt_file));
5556 demux[demux_id].pmt_time = (time_t)pmt_info.st_mtime;
5557 }
5558
5559 if(cfg.dvbapi_pmtmode == 3)
5560 {
5561 disable_pmt_files = 1;
5562 break;
5563 }
5564 }
5565 closedir(dirp);
5566 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5567}
5568
5569void *dvbapi_event_thread(void *cli)
5570{
5571 struct s_client *client = (struct s_client *) cli;
5572 SAFE_SETSPECIFIC(getclient, client);
5573 set_thread_name(__func__);
5574
5575 while(!exit_oscam)
5576 {
5577 cs_sleepms(750);
5578 event_handler(0);
5579 }
5580 return NULL;
5581}
5582
5583void dvbapi_process_input(int32_t demux_id, int32_t filter_num, uint8_t *buffer, int32_t len, uint32_t msgid)
5584{
5585 struct s_ecmpid *curpid = NULL;
5586 int32_t pid = demux[demux_id].demux_fd[filter_num].pidindex;
5587 uint16_t filtertype = demux[demux_id].demux_fd[filter_num].type;
5588 uint16_t sctlen = SCT_LEN(buffer);
5589
5590 if(sctlen < 4)
5591 {
5592 cs_log_dbg(D_DVBAPI, "Received filter data with invalid section length!");
5593 return;
5594 }
5595
5596 if(len < sctlen)
5597 {
5598 cs_log_dbg(D_DVBAPI, "Received filter data with total length 0x%03X but section length is 0x%03X -> invalid length!", len, sctlen);
5599 return;
5600 }
5601
5602 if(demux_id < 0 || demux_id >= MAX_DEMUX)
5603 {
5604 cs_log("dvbapi_process_input(): error - received invalid demux_id (%d)", demux_id);
5605 return;
5606 }
5607
5608 if(filter_num < 0 || filter_num >= MAX_FILTER)
5609 {
5610 cs_log("dvbapi_process_input(): error - received invalid filter_num (%d)", filter_num);
5611 return;
5612 }
5613
5614 if(pid != -1 && filtertype == TYPE_ECM)
5615 {
5616 curpid = &demux[demux_id].ECMpids[pid];
5617 }
5618
5619 int32_t filt_match = filtermatch(buffer, filter_num, demux_id, sctlen); // acts on all filters (sdt/emm/ecm)
5620 if(!filt_match)
5621 {
5622 cs_log_dbg(D_DVBAPI,"Demuxer %d receiver returned data not matching the filter -> delivered filter data discarded!", demux_id);
5623 return;
5624 }
5625
5626 if(curpid && curpid->tries <= 0xF0 && filtertype == TYPE_ECM)
5627 {
5628 curpid->irdeto_maxindex = 0;
5629 curpid->irdeto_curindex = 0xFE;
5630 curpid->tries = 0xFE; // reset timeout retry flag
5631 curpid->irdeto_cycle = 0xFE; // reset irdetocycle
5632 curpid->table = 0;
5633 curpid->checked = 4; // flag ecmpid as checked
5634 curpid->status = -1; // flag ecmpid as unusable
5635
5636 if(pid == demux[demux_id].pidindex)
5637 {
5638 // current pid delivered problems so this pid isn't
5639 // being used to descramble any longer -> clear pidindex
5640 demux[demux_id].pidindex = -1;
5641
5642 // remove this pid from channel cache since we had no founds on any ecmpid!
5643 dvbapi_edit_channel_cache(demux_id, pid, 0);
5644 }
5645
5646 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
5647 return;
5648 }
5649
5650 if(filtertype == TYPE_ECM)
5651 {
5652 uint32_t chid = 0x10000;
5653 int8_t pvu_skip = 0;
5654 ECM_REQUEST *er;
5655
5656 if(len != 0) // len = 0 receiver encountered an internal bufferoverflow!
5657 {
5658 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched ECM data (length = 0x%03X):",
5659 demux_id, filter_num + 1, sctlen);
5660
5661 if(sctlen > MAX_ECM_SIZE) // ecm too long to handle!
5662 {
5663 cs_log_dbg(D_DVBAPI, "Received data with total length 0x%03X but max supported ECM length is 0x%03X -> Please report!",
5664 sctlen, MAX_ECM_SIZE);
5665
5666 if(curpid)
5667 {
5668 curpid->tries -= 0x0E;
5669 }
5670 return;
5671 }
5672
5673 if(!(buffer[0] == 0x80 || buffer[0] == 0x81))
5674 {
5675 cs_log_dbg(D_DVBAPI, "Received an ECM with invalid ecmtable ID %02X -> ignoring!", buffer[0]);
5676 if(curpid)
5677 {
5678 curpid->tries--;
5679 }
5680 return;
5681 }
5682
5683#ifdef WITH_EMU
5684 if(caid_is_powervu(curpid->CAID)) // ecm counter for powervu
5685 {
5686 pvu_skip = 1;
5687
5688 if(sctlen - 11 > buffer[9])
5689 {
5690 if(buffer[11 + buffer[9]] > curpid->pvu_counter
5691 || (curpid->pvu_counter == 255 && buffer[11 + buffer[9]] == 0)
5692 || ((curpid->pvu_counter - buffer[11 + buffer[9]]) > 5))
5693 {
5694 curpid->pvu_counter = buffer[11 + buffer[9]];
5695 pvu_skip = 0;
5696 }
5697 }
5698 }
5699#endif
5700 // wait for odd / even ecm change (only not for irdeto!)
5701 if((curpid->table == buffer[0] && !caid_is_irdeto(curpid->CAID)) || pvu_skip)
5702 {
5703 if(!(er = get_ecmtask()))
5704 {
5705 return;
5706 }
5707 er->srvid = demux[demux_id].program_number;
5708
5709#ifdef WITH_STAPI5
5710 cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
5711#endif
5712 er->tsid = demux[demux_id].tsid;
5713 er->onid = demux[demux_id].onid;
5714 er->pmtpid = demux[demux_id].pmtpid;
5715 er->ens = demux[demux_id].ens;
5716 er->caid = curpid->CAID;
5717 er->pid = curpid->ECM_PID;
5718 er->prid = curpid->PROVID;
5719 er->vpid = curpid->VPID;
5720 er->ecmlen = sctlen;
5721 memcpy(er->ecm, buffer, er->ecmlen);
5722 chid = get_subid(er); // fetch chid or fake chid
5723 er->chid = chid;
5724 er->msgid = msgid;
5725 dvbapi_set_section_filter(demux_id, er, filter_num);
5726 NULLFREE(er);
5727 return;
5728 }
5729
5730 if(caid_is_irdeto(curpid->CAID))
5731 {
5732 // 80 70 39 53 04 05 00 88
5733 // 81 70 41 41 01 06 00 13 00 06 80 38 1F 52 93 D2
5734 //if(buffer[5]>20) return;
5735 if(curpid->irdeto_maxindex != buffer[5]) // 6, register max irdeto index
5736 {
5737 cs_log_dbg(D_DVBAPI, "Found %d IRDETO ECM CHIDs", buffer[5] + 1);
5738 curpid->irdeto_maxindex = buffer[5]; // numchids = 7 (0..6)
5739 }
5740 }
5741 }
5742
5743 if(!(er = get_ecmtask()))
5744 {
5745 return;
5746 }
5747 er->srvid = demux[demux_id].program_number;
5748
5749#ifdef WITH_STAPI5
5750 cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
5751#endif
5752
5753 er->tsid = demux[demux_id].tsid;
5754 er->onid = demux[demux_id].onid;
5755 er->pmtpid = demux[demux_id].pmtpid;
5756 er->ens = demux[demux_id].ens;
5757 er->caid = curpid->CAID;
5758 er->pid = curpid->ECM_PID;
5759 er->prid = curpid->PROVID;
5760 er->vpid = curpid->VPID;
5761 er->ecmlen = sctlen;
5762 memcpy(er->ecm, buffer, er->ecmlen);
5763 er->msgid = msgid;
5764 chid = get_subid(er); // fetch chid or fake chid
5765 uint32_t fixedprovid = chk_provid(er->ecm, er->caid);
5766
5767 if(fixedprovid && fixedprovid != er->prid)
5768 {
5769 cs_log_dbg(D_DVBAPI, "Fixing provid ecmpid %d from %06X -> %06X", pid, curpid->PROVID, fixedprovid);
5770 curpid->PROVID = fixedprovid;
5771
5772 if(!USE_OPENXCAS)
5773 {
5774 cs_log_dbg(D_DVBAPI, "Fixing provid filter %d from %06X -> %06X",
5775 filter_num + 1, demux[demux_id].demux_fd[filter_num].provid, fixedprovid);
5776
5777 demux[demux_id].demux_fd[filter_num].provid = fixedprovid;
5778 }
5779 cs_log_dbg(D_DVBAPI, "Fixing provid ecmrequest from %06X -> %06X", er->prid, fixedprovid);
5780 er->prid = fixedprovid;
5781 }
5782 er->chid = chid;
5783
5784 // only used on receiver internal buffer overflow
5785 // to get quickly fresh ecm filterdata otherwise freezing!
5786 if(len == 0)
5787 {
5788 curpid->table = 0;
5789 dvbapi_set_section_filter(demux_id, er, filter_num);
5790 NULLFREE(er);
5791 return;
5792 }
5793
5794 if(caid_is_irdeto(curpid->CAID))
5795 {
5796 if(curpid->irdeto_curindex != buffer[4]) // old style wrong irdeto index
5797 {
5798 if(curpid->irdeto_curindex == 0xFE) // check if this ecmfilter just started up
5799 {
5800 // on startup set the current index to the irdeto index of the ecm
5801 curpid->irdeto_curindex = buffer[4];
5802 }
5803 else // we are already running and not interested in this ecm
5804 {
5805 if(curpid->table != buffer[0]) // fix for receivers not supporting section filtering
5806 {
5807 curpid->table = 0;
5808 }
5809
5810 // set ecm filter to odd + even since
5811 // this ecm doesn't match with current irdeto index
5812 dvbapi_set_section_filter(demux_id, er, filter_num);
5813
5814 NULLFREE(er);
5815 return;
5816 }
5817 }
5818 else // fix for receivers not supporting section filtering
5819 {
5820 if(curpid->table == buffer[0])
5821 {
5822 NULLFREE(er);
5823 return;
5824 }
5825 }
5826 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",
5827 demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->ecm[4], er->ecm[5], er->chid, curpid->irdeto_cycle, er->vpid);
5828 }
5829 else
5830 {
5831 cs_log_dbg(D_DVBAPI, "Demuxer %d ECMTYPE %02X CAID %04X PROVID %06X ECMPID %04X FAKECHID %04X (unique part in ecm)",
5832 demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->chid);
5833 }
5834
5835 // check for matching chid (unique ecm part in case of non-irdeto cas)
5836 // plus added fix for seca2 monthly changing fakechid
5837 if((curpid->CHID < 0x10000) && !((chid == curpid->CHID) || ((curpid->CAID >> 8 == 0x01) && (chid & 0xF0FF) == (curpid->CHID & 0xF0FF))))
5838 {
5839 if(caid_is_irdeto(curpid->CAID))
5840 {
5841 // if same: we cycled all indexes but no luck!
5842 if((curpid->irdeto_cycle < 0xFE) && (curpid->irdeto_cycle == curpid->irdeto_curindex))
5843 {
5844 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
5845
5846 // forced pid? keep trying the forced ecmpid, no force kill ecm filter
5847 if(!forceentry || !forceentry->force)
5848 {
5849 if(curpid->checked == 2)
5850 {
5851 curpid->checked = 4;
5852 }
5853
5854 if(curpid->checked == 1)
5855 {
5856 curpid->checked = 2;
5857 curpid->CHID = 0x10000;
5858 }
5859
5860 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
5861 NULLFREE(er);
5862 return;
5863 }
5864 }
5865 curpid->irdeto_curindex++; // set check on next index
5866
5867 if(curpid->irdeto_cycle == 0xFE)
5868 {
5869 curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
5870 }
5871
5872 if(curpid->irdeto_curindex > curpid->irdeto_maxindex)
5873 {
5874 curpid->irdeto_curindex = 0; // check if we reached max irdeto index, if so reset to 0
5875 }
5876 curpid->table = 0;
5877
5878 // set ecm filter to odd + even since
5879 // this ecm doesn't match with current irdeto index
5880 dvbapi_set_section_filter(demux_id, er, filter_num);
5881
5882 NULLFREE(er);
5883 return;
5884 }
5885 else // all non irdeto cas systems
5886 {
5887 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
5888 curpid->table = 0;
5889
5890 // set ecm filter to odd + even since
5891 // this ecm doesn't match with current irdeto index
5892 dvbapi_set_section_filter(demux_id, er, filter_num);
5893
5894 if(forceentry && forceentry->force)
5895 {
5896 NULLFREE(er);
5897 return; // forced pid? keep trying the forced ecmpid!
5898 }
5899
5900 if(curpid->checked == 2)
5901 {
5902 curpid->checked = 4;
5903 }
5904
5905 if(curpid->checked == 1)
5906 {
5907 curpid->checked = 2;
5908 curpid->CHID = 0x10000;
5909 }
5910
5911 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
5912 NULLFREE(er);
5913 return;
5914 }
5915 }
5916
5917 struct s_dvbapi_priority *p;
5918 for(p = dvbapi_priority; p != NULL; p = p->next)
5919 {
5920 if(p->type != 'l'
5921 || (p->caid && p->caid != curpid->CAID)
5922 || (p->provid && p->provid != curpid->PROVID)
5923 || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
5924 || (p->srvid && p->srvid != demux[demux_id].program_number))
5925 {
5926 continue;
5927 }
5928
5929 if((uint)p->delay == sctlen && p->force < 6)
5930 {
5931 p->force++;
5932 NULLFREE(er);
5933 return;
5934 }
5935
5936 if(p->force >= 6)
5937 {
5938 p->force = 0;
5939 }
5940 }
5941
5942 if(!curpid->PROVID)
5943 {
5944 curpid->PROVID = chk_provid(buffer, curpid->CAID);
5945 }
5946
5947 if(caid_is_irdeto(curpid->CAID)) // irdeto: wait for the correct index
5948 {
5949 if(buffer[4] != curpid->irdeto_curindex)
5950 {
5951 curpid->table = 0;
5952
5953 // set ecm filter to odd + even since
5954 // this ecm doesn't match with current irdeto index
5955 dvbapi_set_section_filter(demux_id, er, filter_num);
5956
5957 NULLFREE(er);
5958 return;
5959 }
5960 }
5961
5962 // we have an ecm with the correct irdeto index (or fakechid)
5963 for(p = dvbapi_priority; p != NULL ; p = p->next) // check for ignore!
5964 {
5965 if((p->type != 'i')
5966 || (p->caid && p->caid != curpid->CAID)
5967 || (p->provid && p->provid != curpid->PROVID)
5968 || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
5969 || (p->pidx && p->pidx - 1 != pid)
5970 || (p->srvid && p->srvid != demux[demux_id].program_number))
5971 {
5972 continue;
5973 }
5974
5975 // found an ignore chid match with current ecm -> ignoring this irdeto index
5976 if(p->type == 'i' && (p->chid < 0x10000 && p->chid == chid))
5977 {
5978 curpid->irdeto_curindex++;
5979 if(curpid->irdeto_cycle == 0xFE)
5980 {
5981 curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
5982 }
5983
5984 if(curpid->irdeto_curindex > curpid->irdeto_maxindex) // check if curindex is over the max
5985 {
5986 curpid->irdeto_curindex = 0;
5987 }
5988 curpid->table = 0;
5989
5990 // irdeto: wait for the correct index + check if we cycled all
5991 if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_cycle != curpid->irdeto_curindex))
5992 {
5993 // set ecm filter to odd + even since this chid has to be ignored!
5994 dvbapi_set_section_filter(demux_id, er, filter_num);
5995 }
5996 else // this fakechid has to be ignored, kill this filter!
5997 {
5998 if(curpid->checked == 2)
5999 {
6000 curpid->checked = 4;
6001 }
6002
6003 if(curpid->checked == 1)
6004 {
6005 curpid->checked = 2;
6006 curpid->CHID = 0x10000;
6007 }
6008
6009 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
6010 }
6011 NULLFREE(er);
6012 return;
6013 }
6014 }
6015
6016 if(er)
6017 {
6018 curpid->table = er->ecm[0];
6019 }
6020
6021 request_cw(dvbapi_client, er, demux_id, 1); // register this ecm for delayed ecm response check
6022 return; // end of ecm filterhandling!
6023 }
6024
6025 if(filtertype == TYPE_EMM)
6026 {
6027 if(len != 0) // len = 0 receiver encountered an internal buffer overflow!
6028 {
6029 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched EMM data (length = 0x%03X):",
6030 demux_id, filter_num + 1, sctlen);
6031
6032 if(sctlen > MAX_EMM_SIZE) // emm too long to handle!
6033 {
6034 cs_log_dbg(D_DVBAPI, "Received data with total length 0x%03X but max supported EMM length is 0x%03X -> Please report!",
6035 sctlen, MAX_EMM_SIZE);
6036
6037 return;
6038 }
6039 }
6040 else
6041 {
6042 return; // just skip on internal buffer overflow
6043 }
6044
6045#ifdef WITH_EMU
6046 if(caid_is_director(demux[demux_id].demux_fd[filter_num].caid))
6047 {
6048 uint32_t i;
6049 uint32_t emmhash;
6050
6051 if(sctlen < 4)
6052 {
6053 return;
6054 }
6055
6056 for(i = 0; i + 2 < sctlen; i++)
6057 {
6058 if(buffer[i] == 0xF0 && (buffer[i + 2] == 0xE1 || buffer[i + 2] == 0xE4))
6059 {
6060 emmhash = (buffer[3] << 8) | buffer[sctlen - 2];
6061 if(demux[demux_id].demux_fd[filter_num].cadata == emmhash)
6062 {
6063 return;
6064 }
6065
6066 demux[demux_id].demux_fd[filter_num].cadata = emmhash;
6067 dvbapi_process_emm(demux_id, filter_num, buffer, sctlen);
6068 return;
6069 }
6070 }
6071 return;
6072 }
6073#endif
6074 // fix to handle more than one irdeto emm packet
6075 uint8_t *pbuf = buffer;
6076 int32_t done = 0;
6077 int32_t unhandled = len;
6078
6079 while(len > done)
6080 {
6081 pbuf += done;
6082 sctlen = SCT_LEN(pbuf);
6083
6084 if(unhandled < 4 || (int32_t)sctlen > unhandled || sctlen > MAX_EMM_SIZE || sctlen < 4)
6085 {
6086 break;
6087 }
6088
6089 dvbapi_process_emm(demux_id, filter_num, pbuf, sctlen);
6090 done += sctlen;
6091 unhandled -= sctlen;
6092 }
6093 }
6094
6095 if(filtertype == TYPE_SDT)
6096 {
6097 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched SDT data (length = 0x%03X):",
6098 demux_id, filter_num + 1, sctlen);
6099
6100 dvbapi_parse_sdt(demux_id, buffer, sctlen, msgid);
6101 }
6102
6103 if(filtertype == TYPE_PAT)
6104 {
6105 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched PAT data (length = 0x%03X):",
6106 demux_id, filter_num + 1, sctlen);
6107
6108 dvbapi_parse_pat(demux_id, buffer, sctlen, msgid);
6109 }
6110
6111 if(filtertype == TYPE_PMT)
6112 {
6113 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched PMT data (length = 0x%03X):",
6114 demux_id, filter_num + 1, sctlen);
6115
6116 dvbapi_parse_pmt(demux_id, buffer, sctlen, msgid);
6117 }
6118
6119 if(filtertype == TYPE_CAT)
6120 {
6121 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched CAT data (length = 0x%03X):",
6122 demux_id, filter_num + 1, sctlen);
6123
6124 dvbapi_parse_cat(demux_id, buffer, sctlen, msgid);
6125 }
6126}
6127
6128static int32_t dvbapi_recv(int32_t connfd, uint8_t *mbuf, size_t rlen)
6129{
6130 ssize_t len = cs_recv(connfd, mbuf, rlen, MSG_DONTWAIT);
6131
6132 if((len == -1 && (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) || (len == 0))
6133 {
6134 return -1;
6135 }
6136
6137 if(len == -1)
6138 {
6139 return 0;
6140 }
6141
6142 return len;
6143}
6144
6145static uint16_t dvbapi_get_nbof_missing_header_bytes(uint8_t *mbuf, uint16_t mbuf_len, uint32_t msgid_size)
6146{
6147 uint16_t commandsize = 4;
6148 commandsize += msgid_size;
6149 if(mbuf_len < commandsize)
6150 {
6151 return commandsize - mbuf_len;
6152 }
6153 else
6154 {
6155 mbuf += msgid_size;
6156 uint32_t opcode = b2i(4, mbuf);
6157
6158 if((opcode & 0xFFFFF000) == DVBAPI_AOT_CA)
6159 {
6160 if(mbuf[3] & 0x80)
6161 {
6162 uint32_t size = mbuf[3] & 0x7F;
6163 if(mbuf_len < (commandsize + size))
6164 {
6165 return (commandsize + size) - mbuf_len;
6166 }
6167 }
6168 return 0;
6169 }
6170 else
6171 {
6172 switch (opcode)
6173 {
6174 case DVBAPI_FILTER_DATA:
6175 commandsize = 9;
6176 commandsize += msgid_size;
6177 if(mbuf_len < commandsize)
6178 {
6179 return commandsize - mbuf_len;
6180 }
6181 return 0;
6182
6183 case DVBAPI_CLIENT_INFO:
6184 commandsize = 7;
6185 commandsize += msgid_size;
6186 if(mbuf_len < commandsize)
6187 {
6188 return commandsize - mbuf_len;
6189 }
6190 return 0;
6191
6192 default:
6193 return 0;
6194 }
6195 }
6196 }
6197}
6198
6199static void set_chunksize_data_len_to_invalid(uint16_t *chunksize, uint16_t *data_len)
6200{
6201 (*chunksize) = 1;
6202 (*data_len) = 1;
6203}
6204
6205static void log_packeterror(uint16_t mbuf_len, const char* command)
6206{
6207 cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short for %s", mbuf_len, command);
6208}
6209
6210static bool is_commandsize_valid(uint32_t commandsize, uint16_t mbuf_len, const char* command)
6211{
6212 bool isValid = mbuf_len >= commandsize;
6213 if(!isValid)
6214 {
6215 log_packeterror(mbuf_len, command);
6216 }
6217 return isValid;
6218}
6219
6220static uint8_t get_asn1packetsize(uint8_t *mbuf, uint16_t mbuf_len, const char *command, uint32_t *tmp_data_len)
6221{
6222 uint8_t sizebytes = 0;
6223 uint8_t commandsize = 4;
6224 *tmp_data_len = mbuf[3] & 0x7F;
6225 if(mbuf[3] & 0x80)
6226 {
6227 sizebytes = *tmp_data_len;
6228 if(is_commandsize_valid(3 + sizebytes, mbuf_len, command))
6229 {
6230 *tmp_data_len = b2i(sizebytes, mbuf + 4);
6231 }
6232 else
6233 {
6234 return 0;
6235 }
6236 }
6237 return commandsize + sizebytes;
6238}
6239
6240static void dvbapi_get_packet_size(uint8_t *mbuf, uint16_t mbuf_len, uint16_t *chunksize, uint16_t *data_len)
6241{
6242 //chunksize: size of complete chunk in the buffer (an opcode with the data)
6243 //data_len: variable for internal data length (eg. for the filter data size, PMT len)
6244 (*chunksize) = 0;
6245 (*data_len) = 0;
6246
6247 if(mbuf_len < 4)
6248 {
6249 cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short", mbuf_len);
6250 set_chunksize_data_len_to_invalid(chunksize, data_len);
6251 return;
6252 }
6253
6254 int32_t commandsize = 0;
6255 char* command = "DVBAPI_UNKNOWN_COMMAND";
6256 uint32_t tmp_data_len = 0;
6257 uint32_t opcode = b2i(4, mbuf);
6258
6259 switch (opcode)
6260 {
6261 case DVBAPI_AOT_CA_STOP:
6262 {
6263 command = "DVBAPI_AOT_CA_STOP";
6264 commandsize = get_asn1packetsize(mbuf, mbuf_len, command, &tmp_data_len);
6265 break;
6266 }
6267 case DVBAPI_FILTER_DATA:
6268 {
6269 command = "DVBAPI_FILTER_DATA";
6270 commandsize = 9;
6271 if(is_commandsize_valid(commandsize, mbuf_len, command))
6272 {
6273 tmp_data_len = b2i(2, mbuf + 7) & 0x0FFF;
6274 }
6275 break;
6276 }
6277
6278 case DVBAPI_CLIENT_INFO:
6279 {
6280 command = "DVBAPI_CLIENT_INFO";
6281 commandsize = 7;
6282 if(is_commandsize_valid(commandsize, mbuf_len, command))
6283 {
6284 tmp_data_len = mbuf[6];
6285 }
6286 break;
6287 }
6288
6289 default:
6290 {
6291 if((opcode & 0xFFFFFF00) == DVBAPI_AOT_CA_PMT)
6292 {
6293 command = "DVBAPI_AOT_CA_PMT";
6294 commandsize = get_asn1packetsize(mbuf, mbuf_len, command, &tmp_data_len);
6295 break;
6296 }
6297 else
6298 {
6299 cs_log("Unknown socket command received: 0x%08X", opcode);
6300 }
6301 break;
6302 }
6303 }
6304
6305 if(tmp_data_len == 0 || commandsize == 0)
6306 {
6307 set_chunksize_data_len_to_invalid(chunksize, data_len);
6308 return;
6309 }
6310
6311 if(tmp_data_len + commandsize > 0xFFFF)
6312 {
6313 cs_log("This packet is too big: %d bytes => truncated!", tmp_data_len);
6314 tmp_data_len = 0xFFFF - commandsize;
6315 }
6316
6317 (*data_len) = tmp_data_len;
6318 (*chunksize) += commandsize + tmp_data_len;
6319
6320 if(*chunksize > mbuf_len)
6321 {
6322 cs_log_dbg(D_DVBAPI, "This %s packet is incomplete => command length is (%" PRIu16 ")", command, *chunksize);
6323 }
6324 else
6325 {
6326 cs_log_dbg(D_DVBAPI, "This is a %s packet with size %d => lets process it!", command, (*chunksize));
6327 }
6328}
6329
6330static void dvbapi_handlesockmsg(uint8_t *mbuf, uint16_t chunksize, uint16_t data_len, int32_t connfd, uint16_t *client_proto_version)
6331{
6332 uint32_t msgid = 0;
6333 if(*client_proto_version >= 3)
6334 {
6335 if(mbuf[0] != 0xa5)
6336 {
6337 cs_log("Error: network packet malformed! (no start)");
6338 return;
6339 }
6340 msgid = b2i(4, mbuf + 1);
6341 mbuf += 5;
6342 }
6343
6344 uint32_t opcode = b2i(4, mbuf);
6345 switch(opcode)
6346 {
6347 case DVBAPI_FILTER_DATA:
6348 {
6349 int32_t demux_id = mbuf[4];
6350 int32_t filter_num = mbuf[5];
6351
6352 if(demux_id < 0 || demux_id >= MAX_DEMUX)
6353 {
6354 cs_log("dvbapi_handlesockmsg(): error - received invalid demux_id (%d)", demux_id);
6355 break;
6356 }
6357
6358 if(filter_num < 0 || filter_num >= MAX_FILTER)
6359 {
6360 cs_log("dvbapi_handlesockmsg(): error - received invalid filter_num (%d)", filter_num);
6361 break;
6362 }
6363 dvbapi_process_input(demux_id, filter_num, mbuf + 6, data_len + 3, msgid);
6364 break;
6365 }
6366
6367 case DVBAPI_CLIENT_INFO:
6368 {
6369 uint16_t client_proto = b2i(2, mbuf + 4);
6370 NULLFREE(last_client_name);
6371
6372 if(cs_malloc(&last_client_name, data_len + 1))
6373 {
6374 memcpy(last_client_name, &mbuf[7], data_len);
6375 last_client_name[data_len] = 0;
6376 cs_log("Client connected: '%s' (protocol version = %" PRIu16 ")", last_client_name, client_proto);
6377 }
6378 dvbapi_net_send(DVBAPI_SERVER_INFO, connfd, msgid, -1, -1, NULL, NULL, NULL, client_proto);
6379
6380 // now the protocol handshake is complete set correct version so all further packets are sent with correct message id.
6381 (*client_proto_version) = client_proto;
6382
6383 // setting the global var according to the client
6384 last_client_proto_version = client_proto;
6385 break;
6386 }
6387
6388 case DVBAPI_AOT_CA_PMT:
6389 {
6390 cs_log_dbg(D_DVBAPI,"Received DVBAPI_AOT_CA_PMT object on socket %d:", connfd);
6391 dvbapi_parse_capmt(mbuf + (chunksize - data_len), data_len, connfd, NULL, *client_proto_version, msgid);
6392 break;
6393 }
6394
6395 case DVBAPI_AOT_CA_STOP:
6396 {
6397 cs_log_dbg(D_DVBAPI, "Received DVBAPI_AOT_CA_STOP object on socket %d:", connfd);
6398 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_listenport)
6399 {
6400 int32_t i;
6401 int32_t demux_index = mbuf[7];
6402 for(i = 0; i < MAX_DEMUX; i++)
6403 {
6404 // 0xff demux_index is a wildcard => close all related demuxers
6405 if(demux_index == 0xff)
6406 {
6407 if(demux[i].socket_fd == connfd)
6408 {
6409 dvbapi_stop_descrambling(i, msgid);
6410 }
6411 }
6412 else if(demux[i].demux_index == demux_index)
6413 {
6414 dvbapi_stop_descrambling(i, msgid);
6415 break;
6416 }
6417 }
6418
6419 // ipbox fix
6420 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX)
6421 {
6422 // check do we have any demux running on this fd
6423 int16_t execlose = 1;
6424 for(i = 0; i < MAX_DEMUX; i++)
6425 {
6426 if(demux[i].socket_fd == connfd)
6427 {
6428 execlose = 0;
6429 break;
6430 }
6431 }
6432 if(execlose)
6433 {
6434 int32_t ret = close(connfd);
6435 if(ret < 0)
6436 {
6437 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
6438 }
6439 }
6440 }
6441 }
6442 else if(cfg.dvbapi_pmtmode != 6)
6443 {
6444 int32_t ret = close(connfd);
6445 if(ret < 0)
6446 {
6447 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
6448 }
6449 }
6450 break;
6451 }
6452 default:
6453 {
6454 if((opcode & 0xFFFFFF00) == DVBAPI_AOT_CA_PMT)
6455 {
6456 cs_log_dbg(D_DVBAPI, "Received DVBAPI_AOT_CA_PMT object on socket %d:", connfd);
6457 dvbapi_parse_capmt(mbuf + (chunksize - data_len), data_len, connfd, NULL, *client_proto_version, msgid);
6458 }
6459 else
6460 {
6461 cs_log("Unknown socket command received: 0x%08X", opcode);
6462 }
6463 break;
6464 }
6465 }
6466}
6467
6468static bool dvbapi_handlesockdata(int32_t connfd, uint8_t *mbuf, uint16_t mbuf_size, uint16_t unhandled_len, uint16_t *new_unhandled_len, uint16_t *client_proto_version)
6469{
6470 int32_t recv_result;
6471 uint16_t chunksize = 1, data_len = 1;
6472 uint8_t packet_count = 1;
6473 uint32_t msgid_size = 0;
6474 uint16_t missing_header_bytes = 0;
6475 if(*client_proto_version >= 3)
6476 {
6477 msgid_size = 5;
6478 }
6479
6480 do
6481 {
6482 missing_header_bytes = dvbapi_get_nbof_missing_header_bytes(mbuf, unhandled_len, msgid_size);
6483
6484 if(missing_header_bytes != 0)
6485 {
6486 // read first few bytes so we know packet type and length
6487 cs_log_dbg(D_TRACE, "%s reading %" PRIu16 " bytes from connection fd %d", (unhandled_len == 0) ? "Try" : "Continue", missing_header_bytes, connfd);
6488
6489 recv_result = dvbapi_recv(connfd, mbuf + unhandled_len, mbuf_size - unhandled_len);
6490 if(recv_result < 1)
6491 {
6492 (*new_unhandled_len) = unhandled_len;
6493 return (recv_result != -1);
6494 }
6495 else
6496 {
6497 unhandled_len += recv_result;
6498 if(unhandled_len < missing_header_bytes)
6499 {
6500 (*new_unhandled_len) = unhandled_len;
6501 return true;
6502 }
6503 }
6504 }
6505
6506 cs_log_dump_dbg(D_DVBAPI, mbuf, unhandled_len, "Got packetdata (msgid size: %d, clientprotocol: %d)", msgid_size, *client_proto_version);
6507 dvbapi_get_packet_size(mbuf+msgid_size, unhandled_len-msgid_size, &chunksize, &data_len);
6508
6509 chunksize+=msgid_size;
6510 if(chunksize > mbuf_size)
6511 {
6512 cs_log("***** WARNING: SOCKET DATA BUFFER OVERFLOW (%" PRIu16 " bytes), PLEASE REPORT! ****** ", chunksize);
6513 (*new_unhandled_len) = 0;
6514 return true;
6515 }
6516
6517 if(unhandled_len < chunksize) // we are missing some bytes, try to read them
6518 {
6519 cs_log_dbg(D_TRACE, "Continue to read the missing %d bytes from connection fd %d", chunksize - unhandled_len, connfd);
6520 recv_result = dvbapi_recv(connfd, mbuf + unhandled_len, mbuf_size - unhandled_len);
6521 if(recv_result < 1)
6522 {
6523 (*new_unhandled_len) = unhandled_len;
6524 return (recv_result != -1);
6525 }
6526 else
6527 {
6528 unhandled_len += recv_result;
6529 if(unhandled_len < chunksize)
6530 {
6531 (*new_unhandled_len) = unhandled_len;
6532 return true;
6533 }
6534 }
6535 }
6536
6537 dvbapi_handlesockmsg(mbuf, chunksize-msgid_size, data_len, connfd, client_proto_version);
6538
6539 unhandled_len -= chunksize;
6540 if(unhandled_len > 0)
6541 {
6542 memmove(mbuf, mbuf + chunksize, unhandled_len);
6543 }
6544 } while(unhandled_len != 0 && packet_count++ < 8);
6545
6546 cs_log_dbg(D_DVBAPI, "Processing socketdata completed after %d packets with %d bytes left unprocessed", packet_count, unhandled_len);
6547
6548 (*new_unhandled_len) = unhandled_len;
6549 return true;
6550}
6551
6552static void add_to_assoc_fd(int sock)
6553{
6554 uint i;
6555
6556 for(i = 0; i < MAX_ASSOC_FD; i++)
6557 {
6558 if(assoc_fd[i] == sock)
6559 {
6560 return; // do not add twice
6561 }
6562 }
6563
6564 for(i = 0; i < MAX_ASSOC_FD; i++)
6565 {
6566 if(!assoc_fd[i])
6567 {
6568 assoc_fd[i] = sock;
6569 return;
6570 }
6571 }
6572}
6573
6574static void del_from_assoc_fd(int sock)
6575{
6576 uint i;
6577
6578 for(i = 0; i < MAX_ASSOC_FD; i++)
6579 {
6580 if(assoc_fd[i] == sock)
6581 {
6582 assoc_fd[i] = 0;
6583 }
6584 }
6585}
6586
6587static void *dvbapi_main_local(void *cli)
6588{
6589 int32_t i, j, l;
6590 struct s_client *client = (struct s_client *)cli;
6591 client->thread = pthread_self();
6592 SAFE_SETSPECIFIC(getclient, cli);
6593 dvbapi_client = cli;
6594 int32_t maxpfdsize = (MAX_DEMUX * maxfilter) + MAX_DEMUX + 2;
6595 struct pollfd pfd2[maxpfdsize];
6596 struct timeb start, end; // start time poll, end time poll
6597#define PMT_SERVER_SOCKET "/tmp/.listen.camd.socket"
6598 struct sockaddr_un saddr;
6599 saddr.sun_family = AF_UNIX;
6600 cs_strncpy(saddr.sun_path, PMT_SERVER_SOCKET, sizeof(saddr.sun_path));
6601 int32_t rc, pfdcount, g, connfd, clilen;
6602 int32_t ids[maxpfdsize], fdn[maxpfdsize], type[maxpfdsize];
6603 struct SOCKADDR servaddr;
6604 ssize_t len = 0;
6605 static const uint16_t mbuf_size = 2048;
6606 uint8_t *mbuf;
6607 uint16_t unhandled_buf_len[maxpfdsize], unhandled_buf_used[maxpfdsize];
6608 uint8_t *unhandled_buf[maxpfdsize];
6609 struct s_auth *account;
6610 int32_t ok = 0;
6611 uint16_t client_proto_version[maxpfdsize];
6612
6613 if(!cs_malloc(&mbuf, sizeof(uint8_t) * mbuf_size))
6614 {
6615 return NULL;
6616 }
6617
6618 for(i = 0; i < maxpfdsize; i++)
6619 {
6620 unhandled_buf[i] = NULL;
6621 unhandled_buf_len[i] = 0;
6622 unhandled_buf_used[i] = 0;
6623 client_proto_version[i] = 0;
6624 }
6625
6626 for(account = cfg.account; account != NULL; account = account->next)
6627 {
6628 if((ok = is_dvbapi_usr(account->usr)))
6629 {
6630 break;
6631 }
6632 }
6633
6634 cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
6635 memset(demux, 0, sizeof(demux));
6636
6637 for(i = 0; i < MAX_DEMUX; i++)
6638 {
6639 SAFE_MUTEX_INIT(&demux[i].answerlock, NULL);
6640 for(j = 0; j < MAX_ECM_PIDS; j++)
6641 {
6642 for(l = 0; l < MAX_STREAM_INDICES; l++)
6643 {
6644 demux[i].ECMpids[j].index[l] = INDEX_INVALID;
6645 }
6646 }
6647 demux[i].pidindex = -1;
6648 demux[i].curindex = -1;
6649 }
6650
6651 memset(ca_fd, 0, sizeof(ca_fd));
6652 memset(assoc_fd, 0, sizeof(assoc_fd));
6653 dvbapi_read_priority();
6654 dvbapi_load_channel_cache();
6655 dvbapi_detect_api();
6656
6657 if(selected_box == -1 || selected_api == -1)
6658 {
6659 cs_log("ERROR: Could not detect DVBAPI version.");
6660 free(mbuf);
6661 return NULL;
6662 }
6663
6664 // detect box type first and then get descrambler info
6665 dvbapi_get_descrambler_info();
6666
6667 if(cfg.dvbapi_pmtmode == 1)
6668 {
6669 disable_pmt_files = 1;
6670 }
6671
6672 int32_t listenfd = -1;
6673 if(cfg.dvbapi_boxtype != BOXTYPE_IPBOX_PMT &&
6674 cfg.dvbapi_pmtmode != 2 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
6675 {
6676 if(!cfg.dvbapi_listenport)
6677 {
6678 listenfd = dvbapi_init_listenfd();
6679 }
6680 else
6681 {
6682 listenfd = dvbapi_net_init_listenfd();
6683 }
6684
6685 if(listenfd < 1)
6686 {
6687 cs_log("ERROR: Could not init socket: (errno=%d: %s)", errno, strerror(errno));
6688 free(mbuf);
6689 return NULL;
6690 }
6691 }
6692
6693 for(i = 0; i < MAX_DEMUX; i++) // init all demuxers!
6694 {
6695 demux[i].pidindex = -1;
6696 demux[i].curindex = -1;
6697 }
6698
6699 if(cfg.dvbapi_pmtmode != 4 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
6700 {
6701 struct sigaction signal_action;
6702 signal_action.sa_handler = event_handler;
6703 sigemptyset(&signal_action.sa_mask);
6704 signal_action.sa_flags = SA_RESTART;
6705 sigaction(SIGRTMIN + 1, &signal_action, NULL);
6706
6707 dir_fd = open(TMPDIR, O_RDONLY);
6708 if(dir_fd >= 0)
6709 {
6710 fcntl(dir_fd, F_SETSIG, SIGRTMIN + 1);
6711 fcntl(dir_fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_DELETE | DN_MULTISHOT);
6712 event_handler(SIGRTMIN + 1);
6713 }
6714 }
6715 else
6716 {
6717 int32_t ret = start_thread("dvbapi event", dvbapi_event_thread, (void *) dvbapi_client, NULL, 1, 0);
6718 if(ret)
6719 {
6720 free(mbuf);
6721 return NULL;
6722 }
6723 }
6724
6725 if(listenfd != -1)
6726 {
6727 pfd2[0].fd = listenfd;
6728 pfd2[0].events = (POLLIN | POLLPRI);
6729 type[0] = 1;
6730 }
6731
6732#if defined WITH_COOLAPI || defined WITH_COOLAPI2 || defined WITH_NEUTRINO
6733 system("pzapit -rz");
6734#endif
6735 cs_ftime(&start); // register start time
6736
6737 while(!exit_oscam)
6738 {
6739 if(pausecam) // for dbox2, STAPI or PC in standby mode don't parse any ecm/emm or try to start next filter
6740 {
6741 continue;
6742 }
6743
6744 if(cfg.dvbapi_pmtmode == 6)
6745 {
6746 if(listenfd < 0)
6747 {
6748 cs_log("PMT mode 6: Connecting to enigma CA PMT listen socket...");
6749
6750 // socket init
6751 if((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
6752 {
6753 cs_log("socket error (errno=%d %s)", errno, strerror(errno));
6754 listenfd = -1;
6755 }
6756 else if(connect(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
6757 {
6758 cs_log("socket connect error (errno=%d %s)", errno, strerror(errno));
6759 close(listenfd);
6760 listenfd = -1;
6761 }
6762 else
6763 {
6764 pfd2[0].fd = listenfd;
6765 pfd2[0].events = (POLLIN | POLLPRI);
6766 type[0] = 1;
6767 cs_log("PMT mode 6: Successfully connected to CA PMT server (fd %d)", listenfd);
6768 }
6769 }
6770
6771 if(listenfd == -1) // not connected!
6772 {
6773 cs_sleepms(1000);
6774 continue; // start fresh connect attempt!
6775 }
6776 }
6777
6778 pfdcount = (listenfd > -1) ? 1 : 0;
6779
6780 for(i = 0; i < MAX_ASSOC_FD; i++) // add all associated fds (this should include also demux[X].socket_fd)
6781 {
6782 if(assoc_fd[i])
6783 {
6784 pfd2[pfdcount].fd = assoc_fd[i];
6785 pfd2[pfdcount].events = (POLLIN | POLLPRI);
6786 client_proto_version[pfdcount] = last_client_proto_version;
6787 type[pfdcount++] = 1;
6788 }
6789 }
6790
6791 for(i = 0; i < MAX_DEMUX; i++)
6792 {
6793 if(demux[i].program_number == 0)
6794 {
6795 continue; // only evalutate demuxers that have channels assigned
6796 }
6797
6798 uint32_t ecmcounter = 0, emmcounter = 0;
6799 for(g = 0; g < maxfilter; g++)
6800 {
6801 if(demux[i].demux_fd[g].fd <= 0)
6802 {
6803 continue; // deny obvious invalid fd!
6804 }
6805
6806 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX
6807 && selected_api != STAPI && selected_api != COOLAPI)
6808 {
6809 pfd2[pfdcount].fd = demux[i].demux_fd[g].fd;
6810 pfd2[pfdcount].events = (POLLIN | POLLPRI);
6811 ids[pfdcount] = i;
6812 fdn[pfdcount] = g;
6813 type[pfdcount++] = 0;
6814 }
6815
6816 // count ecm filters to see if demuxing is possible anyway
6817 if(demux[i].demux_fd[g].type == TYPE_ECM)
6818 {
6819 ecmcounter++;
6820 }
6821
6822 // count emm filters also
6823 if(demux[i].demux_fd[g].type == TYPE_EMM)
6824 {
6825 emmcounter++;
6826 }
6827 }
6828
6829 // only produce log if something changed
6830 if(ecmcounter != demux[i].old_ecmfiltercount || emmcounter != demux[i].old_emmfiltercount)
6831 {
6832 cs_log_dbg(D_DVBAPI, "Demuxer %d has %d ecmpids, %d streampids, %d ecmfilters and %d of max %d emmfilters",
6833 i, demux[i].ECMpidcount, demux[i].STREAMpidcount, ecmcounter, emmcounter, demux[i].max_emm_filter);
6834
6835 demux[i].old_ecmfiltercount = ecmcounter; // save new amount of ecm filters
6836 demux[i].old_emmfiltercount = emmcounter; // save new amount of emm filters
6837 }
6838
6839 // delayed emm start for non irdeto caids,
6840 // start emm cat if not already done for this demuxer!
6841 struct timeb now;
6842 cs_ftime(&now);
6843 int64_t gone;
6844 int8_t do_emm_start = (cfg.dvbapi_au > 0 && demux[i].emm_filter == -1 && demux[i].EMMpidcount == 0 && emmcounter == 0);
6845 int8_t do_sdt_start = (cfg.dvbapi_read_sdt && demux[i].sdt_filter == -1 && cfg.dvbapi_boxtype != BOXTYPE_SAMYGO);
6846
6847 if(do_emm_start || do_sdt_start)
6848 {
6849 gone = comp_timeb(&now, &demux[i].emmstart);
6850 if(gone > 20 * 1000)
6851 {
6852 if(do_emm_start)
6853 {
6854 cs_ftime(&demux[i].emmstart); // trick to let emm fetching start after 30 seconds to speed up zapping
6855 dvbapi_start_cat_filter(i);
6856 }
6857 }
6858
6859 if(gone > 5 * 1000)
6860 {
6861 if(do_sdt_start)
6862 {
6863 dvbapi_start_sdt_filter(i);
6864 }
6865 }
6866 }
6867
6868 // early start for irdeto since they need emm before ecm
6869 // (pmt emmstart = 1 if detected caid 0x06)
6870 int32_t emmstarted = demux[i].emm_filter;
6871
6872 // check every time since share readers might
6873 // give us new filters due to hexserial change
6874 if(cfg.dvbapi_au && demux[i].EMMpidcount > 0)
6875 {
6876 if(!emmcounter && emmstarted == -1)
6877 {
6878 demux[i].emmstart = now;
6879 dvbapi_start_emm_filter(i); // start emm filtering if emm pids are found
6880 }
6881 else
6882 {
6883 gone = comp_timeb(&now, &demux[i].emmstart);
6884 if(gone > 30 * 1000)
6885 {
6886 demux[i].emmstart = now;
6887 dvbapi_start_emm_filter(i); // start emm filtering delayed if filters already were running
6888 rotate_emmfilter(i); // rotate active emm filters
6889 }
6890 }
6891 }
6892
6893 // Restart decoding all caids we have ecmpids but no ecm filters!
6894 if(ecmcounter == 0 && demux[i].ECMpidcount > 0)
6895 {
6896 int32_t started = 0;
6897
6898 // avoid race: not all pids are asked and checked out yet!
6899 for(g = 0; g < demux[i].ECMpidcount; g++)
6900 {
6901 // check if prio run is done
6902 if(demux[i].ECMpids[g].checked == 0 && demux[i].ECMpids[g].status >= 0)
6903 {
6904 dvbapi_try_next_caid(i, 0, 0); // not done, so start next prio pid
6905 started = 1;
6906 break;
6907 }
6908 }
6909
6910 if(started)
6911 {
6912 continue; // if started a filter proceed with next demuxer
6913 }
6914
6915 // all usable pids (with prio) are tried, lets start over again without prio!
6916 if(g == demux[i].ECMpidcount)
6917 {
6918 // avoid race: not all pids are asked and checked out yet!
6919 for(g = 0; g < demux[i].ECMpidcount; g++)
6920 {
6921 // check if noprio run is done
6922 if(demux[i].ECMpids[g].checked == 2 && demux[i].ECMpids[g].status >= 0)
6923 {
6924 demux[i].ECMpids[g].irdeto_curindex = 0xFE;
6925 demux[i].ECMpids[g].irdeto_maxindex = 0;
6926 demux[i].ECMpids[g].irdeto_cycle = 0xFE;
6927 demux[i].ECMpids[g].tries = 0xFE;
6928 demux[i].ECMpids[g].table = 0;
6929 demux[i].ECMpids[g].CHID = 0x10000; // remove chid prio
6930
6931 dvbapi_try_next_caid(i, 2, 0); // not done, so start next no prio pid
6932 started = 1;
6933 break;
6934 }
6935 }
6936 }
6937
6938 if(started)
6939 {
6940 continue; // if started a filter proceed with next demuxer
6941 }
6942
6943 if(g == demux[i].ECMpidcount) // all usable pids are tried, lets start over again!
6944 {
6945 if(demux[i].decodingtries == -1) // first redecoding attempt?
6946 {
6947 cs_ftime(&demux[i].decstart);
6948
6949 // re-init some used things from second run (without prio)
6950 for(g = 0; g < demux[i].ECMpidcount; g++)
6951 {
6952 demux[i].ECMpids[g].checked = 0;
6953 demux[i].ECMpids[g].irdeto_curindex = 0xFE;
6954 demux[i].ECMpids[g].irdeto_maxindex = 0;
6955 demux[i].ECMpids[g].irdeto_cycle = 0xFE;
6956 demux[i].ECMpids[g].table = 0;
6957 demux[i].decodingtries = 0;
6958
6959 // remove this pid from channel cache since we had no founds on any ecmpid!
6960 dvbapi_edit_channel_cache(i, g, 0);
6961 }
6962 }
6963
6964 uint8_t number_of_enabled_pids = 0;
6965 demux[i].decodingtries++;
6966 dvbapi_resort_ecmpids(i);
6967
6968 for(g = 0; g < demux[i].ECMpidcount; g++) // count number of enabled pids!
6969 {
6970 if(demux[i].ECMpids[g].status >= 0) number_of_enabled_pids++;
6971 }
6972
6973 if(!number_of_enabled_pids)
6974 {
6975 if(demux[i].decodingtries == 10)
6976 {
6977 demux[i].decodingtries = 0;
6978 cs_log("Demuxer %d no enabled matching ecmpids -> decoding is waiting for matching readers!",i);
6979 }
6980 }
6981 else
6982 {
6983 cs_ftime(&demux[i].decend);
6984 demux[i].decodingtries = -1; // reset to first run again!
6985 gone = comp_timeb(&demux[i].decend, &demux[i].decstart);
6986
6987 cs_log("Demuxer %d restarting decoding requests after %"PRId64" ms with %d enabled and %d disabled ecmpids!",
6988 i, gone, number_of_enabled_pids, (demux[i].ECMpidcount-number_of_enabled_pids));
6989
6990 dvbapi_try_next_caid(i, 0, 0);
6991 }
6992 }
6993 }
6994 }
6995
6996 rc = 0;
6997 while(!(listenfd == -1 && cfg.dvbapi_pmtmode == 6))
6998 {
6999 rc = poll(pfd2, pfdcount, 500);
7000 if(rc < 0) // error occured while polling for fd's with fresh data
7001 {
7002 if(errno == EINTR || errno == EAGAIN) // try again in case of interrupt
7003 {
7004 continue;
7005 }
7006 cs_log("ERROR: error on poll of %d fd's (errno=%d %s)", pfdcount, errno, strerror(errno));
7007 break;
7008 }
7009 else
7010 {
7011 break;
7012 }
7013 }
7014
7015 if(rc > 0)
7016 {
7017 cs_ftime(&end); // register end time
7018 int64_t timeout = comp_timeb(&end, &start);
7019 if(timeout < 0)
7020 {
7021 cs_log("*** WARNING: BAD TIME AFFECTING WHOLE OSCAM ECM HANDLING ****");
7022 }
7023 cs_log_dbg(D_TRACE, "New events occurred on %d of %d handlers after %"PRId64" ms inactivity", rc, pfdcount, timeout);
7024 cs_ftime(&start); // register new start time for next poll
7025 }
7026
7027 for(i = 0; i < pfdcount && rc > 0; i++)
7028 {
7029 if(pfd2[i].revents == 0) { continue; } // skip sockets with no changes
7030 rc--; //event handled!
7031 cs_log_dbg(D_TRACE, "Now handling fd %d that reported event %d", pfd2[i].fd, pfd2[i].revents);
7032
7033 if(pfd2[i].revents & (POLLHUP | POLLNVAL | POLLERR))
7034 {
7035 if(type[i] == 1)
7036 {
7037 del_from_assoc_fd(pfd2[i].fd);
7038
7039 for(j = 0; j < MAX_DEMUX; j++)
7040 {
7041 // if listenfd closes stop all assigned decoding!
7042 if(demux[j].socket_fd == pfd2[i].fd)
7043 {
7044 dvbapi_stop_descrambling(j, 0);
7045 }
7046 }
7047
7048 int32_t ret = close(pfd2[i].fd);
7049 if(ret < 0 && errno != 9)
7050 {
7051 cs_log("ERROR: Could not close demuxer socket fd (errno=%d %s)", errno, strerror(errno));
7052 }
7053
7054 if(pfd2[i].fd == listenfd && cfg.dvbapi_pmtmode == 6)
7055 {
7056 listenfd = -1;
7057 }
7058 cs_log_dbg(D_DVBAPI, "Socket %d reported hard connection close", pfd2[i].fd);
7059 }
7060 else // type = 0
7061 {
7062 int32_t demux_id = ids[i];
7063 int32_t n = fdn[i];
7064
7065 if(cfg.dvbapi_boxtype != BOXTYPE_SAMYGO)
7066 {
7067 // stop filter since its giving errors and wont return anything good
7068 dvbapi_stop_filternum(demux_id, n, 0);
7069 }
7070 else
7071 {
7072 int32_t ret, pid;
7073 uint8_t filter[32];
7074 struct dmx_sct_filter_params sFP;
7075 cs_log_dbg(D_DVBAPI, "re-opening connection to demux socket");
7076 close(demux[demux_id].demux_fd[n].fd);
7077 demux[demux_id].demux_fd[n].fd = -1;
7078
7079 ret = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
7080 if(ret != -1)
7081 {
7082 demux[demux_id].demux_fd[n].fd = ret;
7083 pid = demux[demux_id].curindex;
7084 memset(filter, 0, 32);
7085 memset(&sFP, 0, sizeof(sFP));
7086 filter[0] = 0x80;
7087 filter[16] = 0xF0;
7088 sFP.pid = demux[demux_id].ECMpids[pid].ECM_PID;
7089 sFP.timeout = 3000;
7090 sFP.flags = DMX_IMMEDIATE_START;
7091 memcpy(sFP.filter.filter, filter, 16);
7092 memcpy(sFP.filter.mask, filter + 16, 16);
7093 ret = dvbapi_ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER, &sFP);
7094 }
7095
7096 if(ret == -1)
7097 {
7098 // stop filter since it's giving errors and wont return anything good
7099 dvbapi_stop_filternum(demux_id, n, 0);
7100 }
7101 }
7102 }
7103 continue; // continue with other events
7104 }
7105
7106 if(pfd2[i].revents & (POLLIN | POLLPRI))
7107 {
7108 if(type[i] == 1)
7109 {
7110 if(pfd2[i].fd == listenfd)
7111 {
7112 if(cfg.dvbapi_pmtmode == 6)
7113 {
7114 connfd = listenfd;
7115 disable_pmt_files = 1;
7116 }
7117 else
7118 {
7119 clilen = sizeof(servaddr);
7120 connfd = accept(listenfd, (struct sockaddr *)&servaddr, (socklen_t *)&clilen);
7121 cs_log_dbg(D_DVBAPI, "new socket connection fd: %d", connfd);
7122
7123 if(cfg.dvbapi_listenport)
7124 {
7125 // update webif data
7126 client->ip = SIN_GET_ADDR(servaddr);
7127 client->port = ntohs(SIN_GET_PORT(servaddr));
7128 }
7129
7130 add_to_assoc_fd(connfd);
7131
7132 if(cfg.dvbapi_pmtmode == 3 || cfg.dvbapi_pmtmode == 0)
7133 {
7134 disable_pmt_files = 1;
7135 }
7136
7137 if(connfd <= 0)
7138 {
7139 cs_log_dbg(D_DVBAPI, "accept() returns error on fd event %d (errno=%d %s)",
7140 pfd2[i].revents, errno, strerror(errno));
7141 }
7142 }
7143 }
7144 else
7145 {
7146 connfd = pfd2[i].fd;
7147 }
7148
7149 //reading and completing data from socket
7150 if(connfd > 0)
7151 {
7152 if(unhandled_buf_used[i])
7153 {
7154 memcpy(mbuf, unhandled_buf[i], unhandled_buf_used[i]);
7155 }
7156
7157 if(!dvbapi_handlesockdata(connfd, mbuf, mbuf_size, unhandled_buf_used[i], &unhandled_buf_used[i], &client_proto_version[i]))
7158 {
7159 unhandled_buf_used[i] = 0;
7160 client_proto_version[i] = 0; // reset protocol, next client could old protocol.
7161 last_client_proto_version = 0;
7162 // client disconnects, stop all assigned decoding
7163 cs_log_dbg(D_DVBAPI, "Socket %d reported connection close", connfd);
7164 int active_conn = 0; // other active connections counter
7165
7166 for(j = 0; j < MAX_DEMUX; j++)
7167 {
7168 if(demux[j].socket_fd == connfd)
7169 {
7170 dvbapi_stop_descrambling(j, 0);
7171 }
7172 else if(demux[j].socket_fd)
7173 {
7174 active_conn++;
7175 }
7176 }
7177
7178 // stop polling on this socket
7179 del_from_assoc_fd(connfd);
7180 close(connfd);
7181
7182 // last connection closed
7183 if(!active_conn && (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX))
7184 {
7185 if(cfg.dvbapi_listenport)
7186 {
7187 // update webif data
7188 client->ip = get_null_ip();
7189 client->port = 0;
7190 }
7191 }
7192 continue;
7193 }
7194
7195 if(unhandled_buf_used[i])
7196 {
7197 if(unhandled_buf_used[i] > unhandled_buf_len[i])
7198 {
7199 NULLFREE(unhandled_buf[i]);
7200 unhandled_buf_len[i] = unhandled_buf_used[i] < 128 ? 128 : unhandled_buf_used[i];
7201 if(!cs_malloc(&unhandled_buf[i], sizeof(uint8_t) * unhandled_buf_len[i]))
7202 {
7203 unhandled_buf_len[i] = 0;
7204 unhandled_buf_used[i] = 0;
7205 continue;
7206 }
7207 }
7208 memcpy(unhandled_buf[i], mbuf, unhandled_buf_used[i]);
7209 }
7210 }
7211 }
7212 else // type == 0
7213 {
7214 int32_t demux_id = ids[i];
7215 int32_t n = fdn[i];
7216
7217 if((int)demux[demux_id].demux_fd[n].fd != pfd2[i].fd)
7218 {
7219 continue; // filter already killed, no need to process this data!
7220 }
7221
7222 len = dvbapi_read_device(pfd2[i].fd, mbuf, mbuf_size);
7223 if(len < 0) // serious filterdata read error
7224 {
7225 // stop filter since it's giving errors and won't return anything good
7226 dvbapi_stop_filternum(demux_id, n, 0);
7227
7228 maxfilter--; // lower maxfilters to avoid this with new filter setups!
7229 continue;
7230 }
7231
7232 if(!len) // receiver internal filter buffer overflow
7233 {
7234 memset(mbuf, 0, mbuf_size);
7235 }
7236 dvbapi_process_input(demux_id, n, mbuf, len, 0);
7237 }
7238 continue; // continue with other events!
7239 }
7240 }
7241 }
7242
7243 for(j = 0; j < maxpfdsize; j++)
7244 {
7245 NULLFREE(unhandled_buf[j]);
7246 }
7247 free(mbuf);
7248
7249 return NULL;
7250}
7251
7252void dvbapi_write_cw(int32_t demux_id, int32_t pid, int32_t stream_id, uint8_t *cw, uint8_t cw_length, uint8_t *iv,
7253 uint8_t iv_length, enum ca_descr_algo algo, enum ca_descr_cipher_mode cipher_mode, uint32_t msgid)
7254{
7255 int8_t n, cw_empty = 0;
7256 uint8_t null_cw[cw_length];
7257 ca_descr_t ca_descr;
7258 ca_descr_mode_t ca_descr_mode;
7259 ca_descr_data_t ca_descr_data;
7260
7261 memset(null_cw, 0, cw_length);
7262 memset(&ca_descr, 0, sizeof(ca_descr));
7263 memset(&ca_descr_mode, 0, sizeof(ca_descr_mode));
7264 memset(&ca_descr_data, 0, sizeof(ca_descr_data));
7265
7266 if(memcmp(demux[demux_id].last_cw[stream_id][0], null_cw, cw_length) == 0
7267 && memcmp(demux[demux_id].last_cw[stream_id][1], null_cw, cw_length) == 0)
7268 {
7269 cw_empty = 1; // to make sure that both cws get written on constantcw
7270 }
7271
7272 for(n = 0; n < 2; n++)
7273 {
7274 // Check if cw has changed and if new cw is empty (all zeros)
7275 // Skip check for BISS1 - cw could be indeed zero
7276 // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
7277 if((memcmp(cw + (n * cw_length), demux[demux_id].last_cw[stream_id][n], cw_length) != 0 || cw_empty)
7278 && (memcmp(cw + (n * cw_length), null_cw, cw_length) != 0 || caid_is_biss(demux[demux_id].ECMpids[pid].CAID)))
7279 {
7280 // prepare ca device
7281 uint32_t idx = dvbapi_ca_set_pid(demux_id, pid, stream_id, (algo == CA_ALGO_DES), msgid);
7282 if(idx == INDEX_INVALID)
7283 {
7284 return; // return on no index!
7285 }
7286
7287#if defined WITH_COOLAPI || defined WITH_COOLAPI2
7288 ca_descr.index = idx;
7289 ca_descr.parity = n;
7290
7291 // just to make the compiler happy (-Wunused-parameter)
7292 // (better move the coolapi code to a separate function)
7293 ca_descr_mode.cipher_mode = cipher_mode;
7294 ca_descr_data.data = iv;
7295 ca_descr_data.length = iv_length;
7296
7297 memcpy(demux[demux_id].last_cw[stream_id][n], cw + (n * 8), 8);
7298 memcpy(ca_descr.cw, cw + (n * 8), 8);
7299
7300 cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca_mask %d)",
7301 demux_id, n, ca_descr.index, demux[demux_id].ca_mask);
7302
7303 coolapi_write_cw(demux[demux_id].ca_mask, demux[demux_id].STREAMpids, demux[demux_id].STREAMpidcount, &ca_descr);
7304#else
7305 int32_t i, j, write_cw = 0;
7306 uint32_t usedidx, lastidx;
7307
7308 char lastcw[2 * cw_length + 1];
7309 char newcw[2 * cw_length + 1];
7310#ifdef WITH_DEBUG
7311 if(cs_dblevel & D_DVBAPI)
7312 {
7313 cs_hexdump(0, demux[demux_id].last_cw[stream_id][n], cw_length, lastcw, sizeof(lastcw));
7314 cs_hexdump(0, cw + (n * cw_length), cw_length, newcw, sizeof(newcw));
7315 }
7316#endif
7317
7318 for(i = 0; i < CA_MAX; i++)
7319 {
7320 if(!(demux[demux_id].ca_mask & (1 << i)))
7321 {
7322 continue; // ca not in use by this demuxer!
7323 }
7324 lastidx = INDEX_INVALID;
7325
7326 for(j = 0; j < demux[demux_id].STREAMpidcount; j++)
7327 {
7328 write_cw = 0;
7329 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << j)) == (uint) (1 << j)))
7330 {
7331 usedidx = is_ca_used(i, demux[demux_id].STREAMpids[j]);
7332 if(usedidx != INDEX_INVALID)
7333 {
7334 if(idx != usedidx)
7335 {
7336 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip!",
7337 demux_id, i, usedidx, demux[demux_id].STREAMpids[j]);
7338 continue; // if not used for descrambling -> skip!
7339 }
7340 else
7341 {
7342 if(usedidx == lastidx)
7343 {
7344 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip, %s part of cw already written!",
7345 demux_id, i, usedidx, demux[demux_id].STREAMpids[j], (n == 1 ? "even" : "odd"));
7346 continue;
7347 }
7348
7349 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> write %s part of cw!",
7350 demux_id, i, usedidx, demux[demux_id].STREAMpids[j], (n == 1 ? "even" : "odd"));
7351
7352 write_cw = 1;
7353 }
7354 }
7355 }
7356
7357 if(!write_cw)
7358 {
7359 continue; // no need to write the cw since this ca isnt using it!
7360 }
7361
7362 lastidx = usedidx;
7363 ca_descr.index = usedidx;
7364 ca_descr.parity = n;
7365
7366 memcpy(demux[demux_id].last_cw[stream_id][n], cw + (n * cw_length), cw_length);
7367 memcpy(ca_descr.cw, cw + (n * 8), 8); // ca_descr is only used for 8 byte CWs
7368
7369 cs_log_dbg(D_DVBAPI, "Demuxer %d writing %s part (%s) of controlword, replacing expired (%s)",
7370 demux_id, (n == 1 ? "even" : "odd"), newcw, lastcw);
7371
7372 cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca%d)", demux_id, n, ca_descr.index, i);
7373
7374 if(cfg.dvbapi_extended_cw_api == 1) // Set descrambler algorithm and mode
7375 {
7376 ca_descr_mode.index = usedidx;
7377 ca_descr_mode.algo = algo;
7378 ca_descr_mode.cipher_mode = cipher_mode;
7379
7380 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7381 {
7382 dvbapi_net_send(DVBAPI_CA_SET_DESCR_MODE, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7383 (uint8_t *) &ca_descr_mode, NULL, NULL, demux[demux_id].client_proto_version);
7384 }
7385 else
7386 {
7387 if(ca_fd[i] <= 0)
7388 {
7389 ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
7390 if(ca_fd[i] <= 0) { continue; }
7391 }
7392
7393 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_MODE, &ca_descr_mode) < 0)
7394 {
7395 cs_log("ERROR: ioctl(CA_SET_DESCR_MODE): %s", strerror(errno));
7396 }
7397 }
7398 }
7399
7400 // Send 16 byte CW and IV for AES128, DVB-CISSA
7401 if(cfg.dvbapi_extended_cw_api == 1 && algo == CA_ALGO_AES128)
7402 {
7403 // First send IV
7404 ca_descr_data.index = usedidx;
7405 ca_descr_data.data_type = CA_DATA_IV;
7406 ca_descr_data.data = iv;
7407 ca_descr_data.length = iv_length;
7408
7409 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7410 {
7411 dvbapi_net_send(DVBAPI_CA_SET_DESCR_DATA, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7412 (uint8_t *) &ca_descr_data, NULL, NULL, demux[demux_id].client_proto_version);
7413 }
7414 else
7415 {
7416 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_DATA, &ca_descr_data) < 0)
7417 {
7418 cs_log("ERROR: ioctl(CA_SET_DESCR_DATA): %s", strerror(errno));
7419 }
7420 }
7421
7422 // Then send CW
7423 ca_descr_data.index = usedidx;
7424 ca_descr_data.data_type = CA_DATA_KEY;
7425 ca_descr_data.data = cw + (n * cw_length);
7426 ca_descr_data.length = cw_length;
7427 ca_descr_data.parity = n;
7428
7429 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7430 {
7431 dvbapi_net_send(DVBAPI_CA_SET_DESCR_DATA, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7432 (uint8_t *) &ca_descr_data, NULL, NULL, demux[demux_id].client_proto_version);
7433 }
7434 else
7435 {
7436 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_DATA, &ca_descr_data) < 0)
7437 {
7438 cs_log("ERROR: ioctl(CA_SET_DESCR_DATA): %s", strerror(errno));
7439 }
7440 }
7441 }
7442 else // Send 8 byte CW for DVB-CSA or DES
7443 {
7444 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7445 {
7446 dvbapi_net_send(DVBAPI_CA_SET_DESCR, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7447 (uint8_t *) &ca_descr, NULL, NULL, demux[demux_id].client_proto_version);
7448 }
7449 else
7450 {
7451 if(ca_fd[i] <= 0)
7452 {
7453 ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
7454 if(ca_fd[i] <= 0)
7455 {
7456 continue;
7457 }
7458 }
7459
7460 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR, &ca_descr) < 0)
7461 {
7462 cs_log("ERROR: ioctl(CA_SET_DESCR): %s", strerror(errno));
7463 }
7464 }
7465 }
7466 }
7467 }
7468#endif
7469 }
7470 }
7471 cs_log_dbg(D_DVBAPI, "Using %d of %d total descramblers", ca_descramblers_used, ca_descramblers_total);
7472}
7473
7474void delayer(ECM_REQUEST *er, uint32_t delay)
7475{
7476 if(delay <= 0) { return; }
7477
7478 struct timeb tpe;
7479 cs_ftime(&tpe);
7480 int64_t gone = comp_timeb(&tpe, &er->tps);
7481
7482 if(gone < delay)
7483 {
7484 cs_log_dbg(D_DVBAPI, "delayer: gone=%"PRId64" ms, cfg=%d ms -> delay=%"PRId64" ms", gone, delay, delay - gone);
7485 cs_sleepms(delay - gone);
7486 }
7487}
7488
7489void dvbapi_send_dcw(struct s_client *client, ECM_REQUEST *er)
7490{
7491 int32_t i, j, k, handled = 0;
7492 for(i = 0; i < MAX_DEMUX; i++)
7493 {
7494 uint32_t nocw_write = 0; // 0 = write cw, 1 = dont write cw to hardware demuxer
7495
7496 // ignore empty demuxers, skip ecm response for other
7497 // srvid and ecm recponse for different adapter
7498 if(demux[i].program_number == 0
7499 || demux[i].program_number != er->srvid
7500 || demux[i].adapter_index != er->adapter_index)
7501 {
7502 continue;
7503 }
7504
7505#ifdef WITH_STAPI5
7506 if(strcmp(dev_list[demux[i].dev_index].name, er->dev_name) != 0)
7507 {
7508 continue; // skip request if PTI device doesn't match request
7509 }
7510#endif
7511
7512 demux[i].rdr = er->selected_reader;
7513
7514 for(j = 0; j < demux[i].ECMpidcount; j++) // check for matching ecmpid
7515 {
7516 if((demux[i].ECMpids[j].CAID == er->caid || demux[i].ECMpids[j].CAID == er->ocaid)
7517 && demux[i].ECMpids[j].ECM_PID == er->pid && demux[i].ECMpids[j].PROVID == er->prid
7518 && demux[i].ECMpids[j].VPID == er->vpid)
7519 {
7520 break;
7521 }
7522 }
7523
7524 if(j == demux[i].ECMpidcount)
7525 {
7526 continue; // ecm response srvid ok but no matching ecmpid, perhaps this for other demuxer
7527 }
7528
7529 cs_log_dbg(D_DVBAPI, "Demuxer %d %scontrol word received for PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
7530 i, (er->rc >= E_NOTFOUND ? "no " : ""), j, er->caid, er->prid, er->pid, er->chid, er->vpid);
7531
7532 uint32_t status = dvbapi_check_ecm_delayed_delivery(i, er);
7533 uint32_t comparecw0 = 0, comparecw1 = 0;
7534 char ecmd5[17 * 3];
7535#ifdef WITH_DEBUG
7536 if(cs_dblevel & D_DVBAPI)
7537 {
7538 cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
7539 }
7540#endif
7541 if(status == 1 && er->rc) // wrong ecmhash
7542 {
7543 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (requested different one)", i, ecmd5);
7544 continue;
7545 }
7546
7547 if(status == 2) // no filter
7548 {
7549 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (filter already killed)", i, ecmd5);
7550 continue;
7551 }
7552
7553 if(status == 5) // empty cw
7554 {
7555 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (delivered cw is empty!)", i, ecmd5);
7556 nocw_write = 1;
7557
7558 if(er->rc < E_NOTFOUND)
7559 {
7560 er->rc = E_NOTFOUND;
7561 }
7562 }
7563
7564 // 0=matching ecm hash, 2=no filter, 3=table reset, 4=cache-ex response
7565 // Check only against last_cw[0] (index 0) - No need to check the rest
7566 // Skip check for BISS1 - cw could be indeed zero
7567 // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
7568 if((status == 0 || status == 3 || status == 4) && er->rc < E_NOTFOUND && !caid_is_biss(er->caid))
7569 {
7570 // check for matching control word
7571 if(memcmp(er->cw, demux[i].last_cw[0][0], 8) == 0 &&
7572 memcmp(er->cw + 8, demux[i].last_cw[0][1], 8) == 0)
7573 {
7574 comparecw0 = 1;
7575 }
7576 else if(memcmp(er->cw, demux[i].last_cw[0][1], 8) == 0 &&
7577 memcmp(er->cw + 8, demux[i].last_cw[0][0], 8) == 0)
7578 {
7579 comparecw1 = 1;
7580 }
7581
7582 if(comparecw0 == 1 || comparecw1 == 1)
7583 {
7584 cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate controlword ecm response hash %s (duplicate controlword!)", i, ecmd5);
7585 nocw_write = 1;
7586 }
7587 }
7588
7589 if(status == 3) // table reset
7590 {
7591 cs_log_dbg(D_DVBAPI, "Demuxer %d luckyshot new controlword ecm response hash %s (ecm table reset)", i, ecmd5);
7592 }
7593
7594 if(status == 4) // no check on cache-ex responses!
7595 {
7596 cs_log_dbg(D_DVBAPI, "Demuxer %d new controlword from cache-ex reader (no ecmhash check possible)", i);
7597 }
7598
7599 handled = 1; // mark this ecm response as handled
7600 if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 0 && (demux[i].pidindex == -1) && er->caid != 0)
7601 {
7602 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7603 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
7604 demux[i].pidindex = j; // set current index as *the* pid to descramble
7605 demux[i].ECMpids[j].checked = 4;
7606
7607 cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
7608 i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
7609 }
7610
7611 if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 1 && er->caid != 0) // FOUND
7612 {
7613 SAFE_MUTEX_LOCK(&demux[i].answerlock); // only process one ecm answer
7614 if(demux[i].ECMpids[j].checked != 4)
7615 {
7616 int32_t t, o, ecmcounter = 0;
7617 int32_t oldpidindex = demux[i].pidindex;
7618 demux[i].pidindex = j; // set current ecmpid as the new pid to descramble
7619
7620 if(oldpidindex != -1)
7621 {
7622 for(k = 0; k < MAX_STREAM_INDICES; k++)
7623 {
7624 demux[i].ECMpids[j].index[k] = demux[i].ECMpids[oldpidindex].index[k]; // swap index with lower status pid that was descrambling
7625 demux[i].ECMpids[j].useMultipleIndices = demux[i].ECMpids[oldpidindex].useMultipleIndices;
7626 }
7627 }
7628
7629 // check this pid with control word FOUND for higher status
7630 for(t = 0; t < demux[i].ECMpidcount; t++)
7631 {
7632 if(t != j && demux[i].ECMpids[j].status >= demux[i].ECMpids[t].status)
7633 {
7634 // check if ecm filter is in use and
7635 // stop all ecm filters of lower status pids
7636 for(o = 0; o < maxfilter; o++)
7637 {
7638 if(demux[i].demux_fd[o].fd > 0 && demux[i].demux_fd[o].type == TYPE_ECM
7639 && demux[i].demux_fd[o].pidindex == t)
7640 {
7641 // ecm filter belongs to lower status pid -> kill!
7642 dvbapi_stop_filternum(i, o, er->msgid);
7643 }
7644 }
7645 dvbapi_edit_channel_cache(i, t, 0); // remove lower status pid from channel cache
7646 demux[i].ECMpids[t].checked = 4; // mark index t as low status
7647 }
7648 }
7649
7650 for(o = 0; o < maxfilter; o++)
7651 {
7652 if(demux[i].demux_fd[o].type == TYPE_ECM)
7653 {
7654 ecmcounter++; // count all ecm filters
7655 }
7656 }
7657
7658 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7659 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
7660
7661 if(ecmcounter == 1) // if total found running ecmfilters is 1 -> we found the "best" pid
7662 {
7663 dvbapi_edit_channel_cache(i, j, 1);
7664 demux[i].ECMpids[j].checked = 4; // mark best pid last ;)
7665 }
7666 cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
7667 i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
7668 }
7669 SAFE_MUTEX_UNLOCK(&demux[i].answerlock); // and release it!
7670 }
7671
7672 if(er->rc >= E_NOTFOUND) // not found on requestmode 0 + 1
7673 {
7674 if(er->rc == E_SLEEPING)
7675 {
7676 dvbapi_stop_descrambling(i, er->msgid);
7677 return;
7678 }
7679
7680 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(i, j, 'p');
7681 if(forceentry && forceentry->force) // forced pid? keep trying the forced ecmpid!
7682 {
7683 // all cas or irdeto cas with forced prio chid
7684 if(!caid_is_irdeto(er->caid) || forceentry->chid < 0x10000)
7685 {
7686 demux[i].ECMpids[j].table = 0;
7687 dvbapi_set_section_filter(i, er, -1);
7688 continue;
7689 }
7690 else // irdeto cas without chid prio forced
7691 {
7692 // init irdeto current index to first one
7693 if(demux[i].ECMpids[j].irdeto_curindex == 0xFE)
7694 {
7695 demux[i].ECMpids[j].irdeto_curindex = 0x00;
7696 }
7697
7698 // check for last / max chid
7699 if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex))
7700 {
7701 cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of FORCED PID %d CAID %04X PROVID %06X ECMPID %04X",
7702 i, j, er->caid, er->prid, er->pid);
7703
7704 demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
7705 demux[i].ECMpids[j].table = 0;
7706 dvbapi_set_section_filter(i, er, -1);
7707 continue;
7708 }
7709 }
7710 }
7711
7712 // in case of timeout or fatal LB event give
7713 // this pid another try but no more than 1 try
7714 if((er->rc == E_TIMEOUT || (er->rcEx && er->rcEx <= E2_CCCAM_NOCARD))
7715 && demux[i].ECMpids[j].tries == 0xFE)
7716 {
7717 demux[i].ECMpids[j].tries -= 0x07;
7718 demux[i].ECMpids[j].table = 0;
7719 dvbapi_set_section_filter(i, er, -1);
7720 continue;
7721 }
7722 else // all not found responses exception: first timeout response and first fatal loadbalancer response
7723 {
7724 demux[i].ECMpids[j].CHID = 0x10000; // get rid of this prio chid since it failed!
7725 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry
7726 }
7727
7728 if(caid_is_irdeto(er->caid))
7729 {
7730 // init irdeto current index to first one
7731 if(demux[i].ECMpids[j].irdeto_curindex == 0xFE)
7732 {
7733 demux[i].ECMpids[j].irdeto_curindex = 0x00;
7734 }
7735
7736 // check for last / max chid
7737 if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex))
7738 {
7739 cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of PID %d CAID %04X PROVID %06X ECMPID %04X VPID %04X",
7740 i, j, er->caid, er->prid, er->pid, er->vpid);
7741
7742 demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
7743 demux[i].ECMpids[j].table = 0;
7744 dvbapi_set_section_filter(i, er, -1);
7745 continue;
7746 }
7747 }
7748
7749 dvbapi_edit_channel_cache(i, j, 0); // remove this pid from channelcache
7750
7751 if(demux[i].pidindex == j)
7752 {
7753 // current pid delivered a notfound so this pid isn't
7754 // being used to descramble any longer -> clear pidindex
7755 demux[i].pidindex = -1;
7756 }
7757
7758 demux[i].ECMpids[j].irdeto_maxindex = 0;
7759 demux[i].ECMpids[j].irdeto_curindex = 0xFE;
7760 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7761 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
7762 demux[i].ECMpids[j].table = 0;
7763 demux[i].ECMpids[j].checked = 4; // flag ecmpid as checked
7764 demux[i].ECMpids[j].status = -1; // flag ecmpid as unusable
7765
7766 int32_t found = 1; // setup for first run
7767 int32_t filternum = -1;
7768
7769 while(found > 0) // disable all ecm + emm filters for this notfound
7770 {
7771 found = 0;
7772
7773 filternum = dvbapi_get_filternum(i, er, TYPE_ECM); // get ecm filternumber
7774 if(filternum > -1) // in case valid filter found
7775 {
7776 int32_t fd = demux[i].demux_fd[filternum].fd;
7777 if(fd > 0) // in case valid fd
7778 {
7779 dvbapi_stop_filternum(i, filternum, er->msgid); // stop ecmfilter
7780 found = 1;
7781 }
7782 }
7783
7784 if(caid_is_irdeto(er->caid)) // in case irdeto cas stop old emm filters
7785 {
7786 filternum = dvbapi_get_filternum(i, er, TYPE_EMM); // get emm filternumber
7787 if(filternum > -1) // in case valid filter found
7788 {
7789 int32_t fd = demux[i].demux_fd[filternum].fd;
7790 if(fd > 0) // in case valid fd
7791 {
7792 dvbapi_stop_filternum(i, filternum, er->msgid); // stop emmfilter
7793 found = 1;
7794 }
7795 }
7796 }
7797 }
7798 continue;
7799 }
7800
7801 // below this should be only run in case of ecm answer is found
7802 uint32_t chid = get_subid(er); // derive current chid in case of irdeto, or a unique part of ecm on other cas systems
7803 demux[i].ECMpids[j].CHID = (chid != 0 ? chid : 0x10000); // if not zero apply, otherwise use no chid value 0x10000
7804 dvbapi_edit_channel_cache(i, j, 1); // do it here to here after the right CHID is registered
7805
7806 //dvbapi_set_section_filter(i, er); is not needed anymore (unsure)
7807 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7808 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
7809
7810 // cw was already written by another filter or current pid
7811 // isn't pid used to descramble so it ends here!
7812 if(nocw_write || demux[i].pidindex != j)
7813 {
7814 continue;
7815 }
7816
7817 struct s_dvbapi_priority *delayentry = dvbapi_check_prio_match(i, demux[i].pidindex, 'd');
7818 uint32_t delay = 0;
7819
7820 if(delayentry)
7821 {
7822 if(delayentry->delay < 1000)
7823 {
7824 delay = delayentry->delay;
7825 cs_log_dbg(D_DVBAPI, "specific delay: write cw %d ms after ecmrequest", delay);
7826 }
7827 }
7828 else if(cfg.dvbapi_delayer > 0)
7829 {
7830 delay = cfg.dvbapi_delayer;
7831 cs_log_dbg(D_DVBAPI, "generic delay: write cw %d ms after ecmrequest", delay);
7832 }
7833
7834 delayer(er, delay);
7835
7836#ifdef WITH_EMU
7837 if(!chk_ctab_ex(er->caid, &cfg.emu_stream_relay_ctab) || !cfg.emu_stream_relay_enabled)
7838#endif
7839 switch(selected_api)
7840 {
7841#if defined(WITH_STAPI) || defined(WITH_STAPI5)
7842 case STAPI:
7843 stapi_write_cw(i, er->cw, demux[i].STREAMpids, demux[i].STREAMpidcount, demux[i].pmt_file);
7844 break;
7845#endif
7846 default:
7847 {
7848#ifdef WITH_EXTENDED_CW
7849 if(er->cw_ex.mode != demux[i].ECMpids[j].useMultipleIndices)
7850 {
7851 uint32_t idx;
7852
7853 for(k = 0; k < demux[i].STREAMpidcount; k++)
7854 {
7855 if(demux[i].ECMpids[j].useMultipleIndices)
7856 {
7857 idx = demux[i].ECMpids[j].index[k];
7858 }
7859 else
7860 {
7861 idx = demux[i].ECMpids[j].index[0];
7862 }
7863 dvbapi_set_pid(i, k, idx, false, false, er->msgid); // disable streampid
7864 }
7865
7866 for(k = 0; k < MAX_STREAM_INDICES; k++)
7867 {
7868 demux[i].ECMpids[j].index[k] = INDEX_INVALID;
7869 }
7870 }
7871
7872 if(er->cw_ex.mode == CW_MODE_MULTIPLE_CW)
7873 {
7874 int32_t key_pos_a = 0;
7875 demux[i].ECMpids[j].useMultipleIndices = 1;
7876
7877 for(k = 0; k < demux[i].STREAMpidcount; k++)
7878 {
7879 if(demux[i].STREAMpidsType[k] == STREAM_VIDEO)
7880 {
7881 dvbapi_write_cw(i, j, k, er->cw, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7882 }
7883 else if(demux[i].STREAMpidsType[k] == STREAM_AUDIO)
7884 {
7885 if(key_pos_a < 4)
7886 {
7887 dvbapi_write_cw(i, j, k, er->cw_ex.audio[key_pos_a], 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7888 key_pos_a++;
7889 }
7890 }
7891 // Every channel that uses the extended cw has unencrypted subtitle streams,
7892 // so disable CW writing to save indices for audio streams and recordings.
7893 //else // Data
7894 //{
7895 // dvbapi_write_cw(i, j, k, er->cw_ex.data, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7896 //}
7897 }
7898 }
7899 else
7900 {
7901 demux[i].ECMpids[j].useMultipleIndices = 0;
7902
7903 if(er->cw_ex.algo == CW_ALGO_AES128)
7904 {
7905 dvbapi_write_cw(i, j, 0, er->cw_ex.session_word, 16, er->cw_ex.data, 16, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7906 }
7907 else
7908 {
7909 dvbapi_write_cw(i, j, 0, er->cw, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7910 }
7911 }
7912#else
7913 cfg.dvbapi_extended_cw_api = 0; // in CSA mode extended_cw_api should be always 0 regardless what user selected!
7914 dvbapi_write_cw(i, j, 0, er->cw, 8, NULL, 0, CA_ALGO_DVBCSA, CA_MODE_CBC, er->msgid);
7915#endif
7916 break;
7917 }
7918 }
7919
7920 // reset idle-Time
7921 client->last = time((time_t *)0); // ********* TO BE FIXED LATER ON ******
7922
7923 if((cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX) && demux[i].client_proto_version >= 2)
7924 {
7925 dvbapi_net_send(DVBAPI_ECM_INFO, demux[i].socket_fd, 0, i, 0, NULL, client, er, demux[i].client_proto_version);
7926 }
7927#ifndef __CYGWIN__
7928 else if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
7929#endif
7930 if(cfg.dvbapi_ecminfo_file != 0 && cfg.dvbapi_boxtype != BOXTYPE_SAMYGO)
7931 {
7932#ifdef WITH_EXTENDED_CW
7933 // Only print CWs for index 0 in ecm.info file
7934 if(er->cw_ex.algo == CA_ALGO_AES128)
7935 {
7936 dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 16);
7937 }
7938 else
7939 {
7940 dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 8);
7941 }
7942#else
7943 dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 8);
7944#endif
7945 }
7946 }
7947
7948 if(handled == 0)
7949 {
7950 cs_log_dbg(D_DVBAPI, "Unhandled ECM response received for CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
7951 er->caid, er->prid, er->pid, er->chid, er->vpid);
7952 }
7953}
7954
7955static int8_t isValidCW(uint8_t *cw)
7956{
7957 uint8_t i;
7958 for(i = 0; i < 16; i += 4)
7959 {
7960 if(((cw[i] + cw[i + 1] + cw[i + 2]) & 0xff) != cw[i + 3])
7961 {
7962 return 0;
7963 }
7964 }
7965 return 1;
7966}
7967
7968void dvbapi_write_ecminfo_file(struct s_client *client, ECM_REQUEST *er, uint8_t *lastcw0, uint8_t *lastcw1, uint8_t cw_length)
7969{
7970#define ECMINFO_TYPE_OSCAM 0
7971#define ECMINFO_TYPE_OSCAM_MS 1
7972#define ECMINFO_TYPE_WICARDD 2
7973#define ECMINFO_TYPE_MGCAMD 3
7974#define ECMINFO_TYPE_CCCAM 4
7975#define ECMINFO_TYPE_CAMD3 5
7976#define ECMINFO_TYPE_GBOX 6
7977
7978 FILE *ecmtxt = fopen(ECMINFO_FILE, "w");
7979 if(ecmtxt != NULL && er->rc < E_NOTFOUND)
7980 {
7981 char tmp[49]; // holds 16 byte cw - (2 hex digits + 1 space) * 16 byte + string termination)
7982 const char *reader_name = NULL, *from_name = NULL, *proto_name = NULL, *from_device= NULL ;
7983 int8_t hops = 0;
7984 int32_t from_port = 0;
7985 char system_name[64];
7986 const char *const_system_name = get_cardsystem_desc_by_caid(er->caid);
7987
7988 cs_strncpy(system_name, const_system_name, sizeof(system_name));
7989 system_name[0] = (char)toupper((int)system_name[0]);
7990
7991 if(cfg.dvbapi_ecminfo_type <= ECMINFO_TYPE_WICARDD)
7992 {
7993 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD)
7994 {
7995 fprintf(ecmtxt, "system: %s\n", system_name);
7996 }
7997
7998 fprintf(ecmtxt, "caid: 0x%04X\npid: 0x%04X\n", er->caid, er->pid);
7999
8000 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD)
8001 {
8002 fprintf(ecmtxt, "prov: %06X\n", (uint) er->prid);
8003 }
8004 else
8005 {
8006 fprintf(ecmtxt, "prov: 0x%06X\n", (uint) er->prid);
8007 }
8008
8009 fprintf(ecmtxt, "chid: 0x%04X\n", er->chid);
8010 }
8011 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_MGCAMD)
8012 {
8013 fprintf(ecmtxt, "===== %s ECM on CaID 0x%04X, pid 0x%04X =====\nprov: %06X\n",
8014 system_name, er->caid, er->pid, (uint) er->prid);
8015 }
8016 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CCCAM)
8017 {
8018 char provider_name[128];
8019 get_providername(er->prid, er->caid, provider_name, sizeof(provider_name));
8020
8021 if(provider_name[0])
8022 {
8023 fprintf(ecmtxt, "system: %s\ncaid: 0x%04X\nprovider: %s\nprovid: 0x%06X\npid: 0x%04X\n",
8024 system_name, er->caid, provider_name, (uint) er->prid, er->pid);
8025 }
8026 else
8027 {
8028 fprintf(ecmtxt, "system: %s\ncaid: 0x%04X\nprovid: 0x%06X\npid: 0x%04X\n",
8029 system_name, er->caid, (uint) er->prid, er->pid);
8030 }
8031 }
8032 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CAMD3)
8033 {
8034 fprintf(ecmtxt, "CAID 0x%04X, PID 0x%04X, PROVIDER 0x%06X\n",
8035 er->caid, er->pid, (uint) er->prid);
8036 }
8037#ifdef MODULE_GBOX
8038 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_GBOX)
8039 {
8040 fprintf(ecmtxt, "===== %s ECM on CaID 0x%04X, pid 0x%04X, sid 0x%04X =====\nprov: %04X, slot: %d, level: %d, dist: %d\nprovider: %06X\n",
8041 system_name, er->caid, er->pid, er->srvid, er->selected_reader->gbox_cw_src_peer, er->selected_reader->gbox_crd_slot_lev >> 4,
8042 er->selected_reader->gbox_crd_slot_lev & 0xf, er->selected_reader->currenthops, (uint) er->prid);
8043 }
8044#endif
8045
8046 switch(er->rc)
8047 {
8048 case E_FOUND:
8049 if(er->selected_reader)
8050 {
8051 reader_name = er->selected_reader->label;
8052 if(is_network_reader(er->selected_reader))
8053 {
8054 from_name = er->selected_reader->device;
8055 from_port = er->selected_reader->r_port;
8056 }
8057 else
8058 {
8059 from_name = "local";
8060 from_device = er->selected_reader->device;
8061 }
8062 proto_name = reader_get_type_desc(er->selected_reader, 1);
8063 hops = er->selected_reader->currenthops;
8064 }
8065 else
8066 {
8067 reader_name = "none";
8068 from_name = "local";
8069 proto_name = "none";
8070 }
8071 break;
8072
8073 case E_CACHE1:
8074 reader_name = "Cache";
8075 from_name = "cache1";
8076 proto_name = "none";
8077 break;
8078
8079 case E_CACHE2:
8080 reader_name = "Cache";
8081 from_name = "cache2";
8082 proto_name = "none";
8083 break;
8084
8085 case E_CACHEEX:
8086 reader_name = "Cache";
8087 from_name = "cache3";
8088 proto_name = "none";
8089 break;
8090 }
8091
8092 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_GBOX)
8093 {
8094 switch(er->rc)
8095 {
8096 case E_FOUND:
8097 if(er->selected_reader)
8098 {
8099 if(is_network_reader(er->selected_reader))
8100 {
8101 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
8102 reader_name, from_name, from_port, proto_name);
8103 }
8104 else
8105 {
8106 fprintf(ecmtxt, "reader: %s\nfrom: %s - %s\nprotocol: %s\n",
8107 reader_name, from_name, from_device, proto_name);
8108 }
8109 }
8110 break;
8111
8112 case E_CACHE1:
8113 case E_CACHE2:
8114 case E_CACHEEX:
8115 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
8116 reader_name, from_name, from_port, proto_name);
8117 break;
8118 }
8119 fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime / 1000);
8120 }
8121
8122 if(cfg.dvbapi_ecminfo_type <= ECMINFO_TYPE_OSCAM_MS)
8123 {
8124 switch(er->rc)
8125 {
8126 case E_FOUND:
8127 if(er->selected_reader)
8128 {
8129 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\nhops: %d\n",
8130 reader_name, from_name, from_port, proto_name, hops);
8131 }
8132 break;
8133
8134 case E_CACHE1:
8135 case E_CACHE2:
8136 case E_CACHEEX:
8137 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
8138 reader_name, from_name, from_port, proto_name);
8139 break;
8140 }
8141
8142 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_OSCAM)
8143 {
8144 fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime / 1000);
8145 }
8146 else
8147 {
8148 fprintf(ecmtxt, "ecm time: %d\n", client->cwlastresptime);
8149 }
8150 }
8151
8152 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CAMD3)
8153 {
8154 fprintf(ecmtxt, "FROM: %s\n", reader_name);
8155 fprintf(ecmtxt, "CW0: %s\n", cs_hexdump(1, lastcw0, cw_length, tmp, sizeof(tmp)));
8156 fprintf(ecmtxt, "CW1: %s\n", cs_hexdump(1, lastcw1, cw_length, tmp, sizeof(tmp)));
8157 }
8158 else
8159 {
8160 fprintf(ecmtxt, "cw0: %s\n", cs_hexdump(1, lastcw0, cw_length, tmp, sizeof(tmp)));
8161 fprintf(ecmtxt, "cw1: %s\n", cs_hexdump(1, lastcw1, cw_length, tmp, sizeof(tmp)));
8162 }
8163
8164 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD || cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_MGCAMD)
8165 {
8166 time_t walltime;
8167 struct tm lt;
8168 char timebuf[32];
8169
8170 if(cw_length == 8) // only check checksum for 8 byte CWs
8171 {
8172 fprintf(ecmtxt, "Signature %s\n", (isValidCW(lastcw0) || isValidCW(lastcw1)) ? "OK" : "NOK");
8173 }
8174 else
8175 {
8176 fprintf(ecmtxt, "Signature %s\n", "OK");
8177 }
8178
8179 if(reader_name != NULL)
8180 {
8181 fprintf(ecmtxt, "source: %s (%s at %s:%d)\n", reader_name, proto_name, from_name, from_port);
8182 }
8183
8184 walltime = cs_time();
8185 localtime_r(&walltime, &lt);
8186
8187 if(strftime(timebuf, 32, "%a %b %d %H:%M:%S %Y", &lt) != 0)
8188 {
8189 fprintf(ecmtxt, "%d msec -- %s\n", client->cwlastresptime, timebuf);
8190 }
8191 }
8192
8193 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CCCAM)
8194 {
8195 if(reader_name != NULL)
8196 {
8197 fprintf(ecmtxt, "using: %s\naddress: %s:%d\nhops: %d\n",
8198 proto_name, from_name, from_port, hops);
8199 }
8200 fprintf(ecmtxt, "ecm time: %d\n", client->cwlastresptime);
8201 }
8202 }
8203
8204 if(ecmtxt)
8205 {
8206 int32_t ret = fclose(ecmtxt);
8207 if(ret < 0)
8208 {
8209 cs_log("ERROR: Could not close ecmtxt fd (errno=%d %s)", errno, strerror(errno));
8210 }
8211 ecmtxt = NULL;
8212 }
8213}
8214
8215
8216void *dvbapi_start_handler(struct s_client *cl, uint8_t *UNUSED(mbuf), int32_t module_idx, void *(*_main_func)(void *))
8217{
8218 // cs_log("dvbapi loaded fd=%d", idx);
8219 if(cfg.dvbapi_enabled == 1)
8220 {
8221 cl = create_client(get_null_ip());
8222 cl->module_idx = module_idx;
8223 cl->typ = 'c';
8224
8225 int32_t ret = start_thread("dvbapi handler", _main_func, (void *)cl, &cl->thread, 1, 0);
8226 if(ret)
8227 {
8228 return NULL;
8229 }
8230 }
8231 return NULL;
8232}
8233
8234void *dvbapi_handler(struct s_client *cl, uint8_t *mbuf, int32_t module_idx)
8235{
8236 return dvbapi_start_handler(cl, mbuf, module_idx, dvbapi_main_local);
8237}
8238
8239int32_t dvbapi_set_section_filter(int32_t demux_id, ECM_REQUEST *er, int32_t n)
8240{
8241 if(!er) { return -1; }
8242
8243 if(USE_OPENXCAS || (selected_api != DVBAPI_3 && selected_api != DVBAPI_1 && selected_api != STAPI) // only valid for dvbapi3, dvbapi1 and STAPI
8244 || (cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)) // reported buggy using sectionfiltering after 1~4 hours -> for now disabled!
8245 {
8246 return 0;
8247 }
8248
8249 if(n == -1)
8250 {
8251 n = dvbapi_get_filternum(demux_id, er, TYPE_ECM);
8252 }
8253
8254 if(n < 0) // in case no valid filter found;
8255 {
8256 return -1;
8257 }
8258
8259 int32_t fd = demux[demux_id].demux_fd[n].fd;
8260 if(fd < 1) // in case no valid fd
8261 {
8262 return -1;
8263 }
8264
8265 uint8_t filter[16];
8266 uint8_t mask[16];
8267 memset(filter, 0, 16);
8268 memset(mask, 0, 16);
8269 struct s_ecmpid *curpid = NULL;
8270
8271 int32_t pid = demux[demux_id].demux_fd[n].pidindex;
8272 if(pid != -1)
8273 {
8274 curpid = &demux[demux_id].ECMpids[pid];
8275 }
8276
8277 if(curpid->table != er->ecm[0] && curpid->table != 0)
8278 {
8279 return -1; // if current ecmtype differs from latest requested ecmtype do not apply section filtering!
8280 }
8281
8282 uint8_t ecmfilter = 0;
8283
8284 if(er->ecm[0] == 0x80)
8285 {
8286 ecmfilter = 0x81; // current processed ecm is even, next will be filtered for odd
8287 }
8288 else
8289 {
8290 ecmfilter = 0x80; // current processed ecm is odd, next will be filtered for even
8291 }
8292
8293 if(curpid->table != 0) // cycle ecmtype from odd to even or even to odd
8294 {
8295 filter[0] = ecmfilter; // only accept new ecms (if previous odd, filter for even and vice versa)
8296 mask[0] = 0xFF;
8297 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to %s (CAID %04X PROVID %06X FD %d)",
8298 demux_id, n + 1, (ecmfilter == 0x80 ? "EVEN" : "ODD"), curpid->CAID, curpid->PROVID, fd);
8299 }
8300 else // not decoding right now so we are interessted in all ecm types!
8301 {
8302 filter[0] = 0x80; // set filter to wait for any ecms
8303 mask[0] = 0xF0;
8304 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to ODD+EVEN (CAID %04X PROVID %06X FD %d)",
8305 demux_id, n + 1, curpid->CAID, curpid->PROVID, fd);
8306 }
8307
8308 uint32_t offset = 0, extramask = 0xFF;
8309 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
8310 //cs_log("**** curpid->CHID %04X, checked = %d, er->chid = %04X *****", curpid->CHID, curpid->checked, er->chid);
8311 // checked 4 to make sure we dont set chid filter and no such ecm in dvbstream except for forced pids!
8312
8313 if(curpid->CHID < 0x10000 && (curpid->checked == 4 || (forceentry && forceentry->force)))
8314 {
8315 switch(er->caid >> 8)
8316 {
8317 case 0x01: // seca
8318 offset = 7;
8319 extramask = 0xF0;
8320 break;
8321
8322 case 0x05: // viaccess
8323 offset = 8;
8324 break;
8325
8326 case 0x06: // irdeto
8327 offset = 6;
8328 break;
8329
8330 case 0x09: // videoguard
8331 offset = 11;
8332 break;
8333
8334 case 0x4A: // DRE-Crypt, Bulcrypt, Tongang and others?
8335 if(!caid_is_bulcrypt(er->caid))
8336 {
8337 offset = 6;
8338 }
8339 break;
8340 }
8341 }
8342
8343 int32_t irdetomatch = 1; // check if wanted irdeto index is the one the delivers current chid!
8344 if(caid_is_irdeto(curpid->CAID))
8345 {
8346 if(curpid->irdeto_curindex == er->ecm[4]) { irdetomatch = 1; } // ok apply chid filtering
8347 else { irdetomatch = 0; } // skip chid filtering but apply irdeto index filtering
8348 }
8349
8350 if(offset && irdetomatch) // we have a cas with chid or unique part in checked ecm
8351 {
8352 i2b_buf(2, curpid->CHID, filter + (offset - 2));
8353 mask[(offset - 2)] = 0xFF & extramask; // additional mask seca2 chid can be FC10 or FD10 varies each month so only apply F?10
8354 mask[(offset - 1)] = 0xFF;
8355 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to %04X on fd %d", demux_id, n + 1, curpid->CHID, fd);
8356 }
8357 else
8358 {
8359 // on irdeto we can always apply irdeto index filtering!
8360 if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_curindex < 0xFE))
8361 {
8362 filter[2] = curpid->irdeto_curindex;
8363 mask[2] = 0xFF;
8364 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set irdetoindex to %d on fd %d",
8365 demux_id, n + 1, curpid->irdeto_curindex, fd);
8366 }
8367 else // all other cas systems also cas systems without chid or unique ecm part
8368 {
8369 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to ANY CHID on fd %d", demux_id, n + 1, fd);
8370 }
8371 }
8372
8373 int32_t ret = dvbapi_activate_section_filter(demux_id, n, fd, curpid->ECM_PID, filter, mask, er->msgid);
8374 if(ret < 0) // something went wrong setting filter!
8375 {
8376 cs_log("Demuxer %d Filter %d (fd %d) error setting section filtering -> stop filter!", demux_id, n + 1, fd);
8377
8378 ret = dvbapi_stop_filternum(demux_id, n, er->msgid);
8379 if(ret == -1)
8380 {
8381 cs_log("Demuxer %d Filter %d (fd %d) stopping filter failed -> kill all filters of this demuxer!", demux_id, n + 1, fd);
8382 dvbapi_stop_filter(demux_id, TYPE_EMM, er->msgid);
8383 dvbapi_stop_filter(demux_id, TYPE_ECM, er->msgid);
8384 }
8385 return -1;
8386 }
8387 return n;
8388}
8389
8390int32_t dvbapi_activate_section_filter(int32_t demux_id, int32_t num, int32_t fd, int32_t pid, uint8_t *filter, uint8_t *mask, uint32_t msgid)
8391{
8392 int32_t ret = -1;
8393
8394 switch(selected_api)
8395 {
8396 case DVBAPI_3:
8397 {
8398 struct dmx_sct_filter_params sFP2;
8399 memset(&sFP2, 0, sizeof(sFP2));
8400 sFP2.pid = pid;
8401 sFP2.timeout = 0;
8402 sFP2.flags = DMX_IMMEDIATE_START;
8403
8404 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
8405 {
8406 //DeepThought: on dgs/cubestation and neumo images, perhaps others
8407 //the following code is needed to descramble
8408 sFP2.filter.filter[0] = filter[0];
8409 sFP2.filter.mask[0] = mask[0];
8410 sFP2.filter.filter[1] = 0;
8411 sFP2.filter.mask[1] = 0;
8412 sFP2.filter.filter[2] = 0;
8413 sFP2.filter.mask[2] = 0;
8414 memcpy(sFP2.filter.filter + 3, filter + 1, 16 - 3);
8415 memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
8416
8417 //DeepThought: in the drivers of the dgs/cubestation and neumo images,
8418 //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
8419 //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
8420 //due to a bug its sets the "positive mask" wrongly (they should be all 0).
8421 //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
8422 //structure, which is incorrect (it should be dmxSctFilterParams).
8423 //The only way to get it right is to call DMX_SET_FILTER1 with the argument
8424 //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
8425
8426 ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP2);
8427 }
8428 else
8429 {
8430 memcpy(sFP2.filter.filter, filter, 16);
8431 memcpy(sFP2.filter.mask, mask, 16);
8432
8433 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
8434 {
8435 ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER,
8436 demux[demux_id].socket_fd,
8437 msgid,
8438 demux_id,
8439 num,
8440 (uint8_t *) &sFP2,
8441 NULL,
8442 NULL,
8443 demux[demux_id].client_proto_version);
8444 }
8445 else
8446 {
8447 ret = dvbapi_ioctl(fd, DMX_SET_FILTER, &sFP2);
8448 }
8449 }
8450 break;
8451 }
8452
8453 case DVBAPI_1:
8454 {
8455 struct dmxSctFilterParams sFP1;
8456 memset(&sFP1, 0, sizeof(sFP1));
8457 sFP1.pid = pid;
8458 sFP1.timeout = 0;
8459 sFP1.flags = DMX_IMMEDIATE_START;
8460 memcpy(sFP1.filter.filter, filter, 16);
8461 memcpy(sFP1.filter.mask, mask, 16);
8462 ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP1);
8463 break;
8464 }
8465
8466#if defined(WITH_STAPI) || defined(WITH_STAPI5)
8467 case STAPI:
8468 {
8469 ret = stapi_activate_section_filter(fd, filter, mask);
8470 break;
8471 }
8472#endif
8473 // Isn't implemented in COOLAPI-1 (legacy)
8474#if defined WITH_COOLAPI2
8475 case COOLAPI:
8476 {
8477 int32_t n = coolapi_get_filter_num(fd);
8478 if(n < 0)
8479 {
8480 return n;
8481 }
8482 coolapi_set_filter(fd, n, pid, filter, mask, TYPE_ECM);
8483 break;
8484 }
8485#endif
8486
8487 default:
8488 break;
8489 }
8490
8491 if(ret != -1) // only change filter/mask for comparing if box returned no errors!
8492 {
8493 // copy filter and mask to check later on if receiver delivered accordingly
8494 memcpy(demux[demux_id].demux_fd[num].filter, filter, 16);
8495 memcpy(demux[demux_id].demux_fd[num].mask, mask, 16);
8496 }
8497 return ret;
8498}
8499
8500int32_t dvbapi_check_ecm_delayed_delivery(int32_t demux_id, ECM_REQUEST *er)
8501{
8502 int32_t ret = 0;
8503 int32_t filternum = dvbapi_get_filternum(demux_id, er, TYPE_ECM);
8504 char nullcw[CS_ECMSTORESIZE];
8505 memset(nullcw, 0, CS_ECMSTORESIZE);
8506
8507 if(filternum < 0) // if no matching filter act like ecm response is delayed
8508 {
8509 return 2;
8510 }
8511
8512 if(memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, nullcw, CS_ECMSTORESIZE))
8513 {
8514 demux[demux_id].demux_fd[filternum].lastresult = er->rc; // save last result
8515 char ecmd5[17 * 3];
8516#ifdef WITH_DEBUG
8517 if(cs_dblevel & D_DVBAPI)
8518 {
8519 cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
8520 }
8521#endif
8522 cs_log_dbg(D_DVBAPI, "Demuxer %d requested controlword for ecm %s on fd %d",
8523 demux_id, ecmd5, demux[demux_id].demux_fd[filternum].fd);
8524
8525 uint8_t md5tmp[MD5_DIGEST_LENGTH];
8526 MD5(er->ecm, er->ecmlen, md5tmp);
8527
8528 // 1 = no response on the ecm we request last for this fd!
8529 ret = (memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE) != 0 ? 1 : 0);
8530 }
8531
8532 // Check for null cw
8533 // Skip check for BISS1 - cw could be indeed zero
8534 // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
8535 if(memcmp(er->cw, nullcw, 8) == 0 && memcmp(er->cw + 8, nullcw, 8) == 0 && !caid_is_biss(er->caid))
8536 {
8537 return 5;
8538 }
8539
8540 struct s_ecmpid *curpid = NULL;
8541 int32_t pid = demux[demux_id].demux_fd[filternum].pidindex;
8542
8543 if(pid !=-1)
8544 {
8545 curpid = &demux[demux_id].ECMpids[pid];
8546 if(curpid->table == 0) // on change table act like ecm response is found
8547 {
8548 return 3;
8549 }
8550 }
8551
8552 if(er->rc == E_CACHEEX) // on cache-ex response act like ecm response is found
8553 {
8554 return 4;
8555 }
8556
8557 return ret;
8558}
8559
8560int32_t dvbapi_get_filternum(int32_t demux_id, ECM_REQUEST *er, int32_t type)
8561{
8562 if(!er) { return -1; }
8563
8564 int32_t n;
8565 int32_t fd = -1;
8566
8567 for(n = 0; n < maxfilter; n++) // determine fd
8568 {
8569 // check for valid and right type (ecm or emm)
8570 if(demux[demux_id].demux_fd[n].fd > 0 && demux[demux_id].demux_fd[n].type == type)
8571 {
8572 if(type == TYPE_ECM && er->srvid != demux[demux_id].program_number)
8573 {
8574 continue;
8575 }
8576
8577 if((demux[demux_id].demux_fd[n].pid == er->pid) && ((demux[demux_id].demux_fd[n].provid == er->prid)
8578 || demux[demux_id].demux_fd[n].provid == 0 || er->prid == 0) && ((demux[demux_id].demux_fd[n].caid == er->caid)
8579 || (demux[demux_id].demux_fd[n].caid == er->ocaid))) // current ecm pid?
8580 {
8581 fd = demux[demux_id].demux_fd[n].fd; // found!
8582 if(demux[demux_id].demux_fd[n].caid == er->ocaid)
8583 {
8584 // clear ecmd5 hash since betatunneled ecms hash different!
8585 memset(demux[demux_id].demux_fd[n].lastecmd5, 0, CS_ECMSTORESIZE);
8586 }
8587 break;
8588 }
8589 }
8590 }
8591
8592 if(fd > 0 && demux[demux_id].demux_fd[n].provid == 0)
8593 {
8594 demux[demux_id].demux_fd[n].provid = er->prid; // hack to fill in provid into demuxer
8595 }
8596 return (fd > 0 ? n : fd); // return -1(fd) on not found, on found return filternumber(n)
8597}
8598
8599uint32_t dvbapi_ca_set_pid(int32_t demux_id, int32_t pid, int32_t stream_id, bool use_des, uint32_t msgid)
8600{
8601 uint32_t idx;
8602 int32_t n;
8603
8604 if(pid == -1 || pid > demux[demux_id].ECMpidcount)
8605 {
8606 return INDEX_INVALID;
8607 }
8608
8609 if(demux[demux_id].ECMpids[pid].useMultipleIndices)
8610 {
8611 n = stream_id;
8612 idx = demux[demux_id].ECMpids[pid].index[n];
8613
8614 if(idx == INDEX_INVALID) // if we have no index for this pid, get one!
8615 {
8616 idx = dvbapi_get_desc_index(demux_id, pid, n);
8617 if(idx == INDEX_INVALID)
8618 {
8619 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X has no free index",
8620 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID);
8621
8622 return INDEX_INVALID;
8623 }
8624
8625 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d for stream %d",
8626 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID, idx, n);
8627 }
8628
8629 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n)))
8630 {
8631 dvbapi_set_pid(demux_id, n, idx, true, use_des, msgid); // enable stream pid
8632 }
8633 else
8634 {
8635 dvbapi_set_pid(demux_id, n, idx, false, false, msgid); // disable stream pid
8636 }
8637 }
8638 else
8639 {
8640 idx = demux[demux_id].ECMpids[pid].index[0];
8641
8642 if(idx == INDEX_INVALID) // if we have no index for this pid, get one!
8643 {
8644 idx = dvbapi_get_desc_index(demux_id, pid, 0);
8645 if(idx == INDEX_INVALID)
8646 {
8647 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X has no free index",
8648 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID);
8649
8650 return INDEX_INVALID;
8651 }
8652
8653 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d",
8654 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID, idx);
8655 }
8656
8657 for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
8658 {
8659 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n)))
8660 {
8661 dvbapi_set_pid(demux_id, n, idx, true, use_des, 0); // enable stream pid
8662 }
8663 else
8664 {
8665 dvbapi_set_pid(demux_id, n, idx, false, false, 0); // disable stream pid
8666 }
8667 }
8668 }
8669 return idx; // return ca index
8670}
8671
8672int8_t update_streampid_list(uint8_t cadevice, uint16_t pid, uint32_t idx, bool use_des)
8673{
8674 struct s_streampid *listitem, *newlistitem;
8675 LL_ITER itr;
8676 if(!ll_activestreampids)
8677 {
8678 ll_activestreampids = ll_create("ll_activestreampids");
8679 }
8680
8681 if(idx >= INDEX_MAX)
8682 {
8683 return INVALID_STREAMPID_INDEX;
8684 }
8685
8686 if(ll_count(ll_activestreampids) > 0)
8687 {
8688 itr = ll_iter_create(ll_activestreampids);
8689 while((listitem = ll_iter_next(&itr)))
8690 {
8691 if(cadevice == listitem->cadevice && pid == listitem->streampid)
8692 {
8693 if((listitem->activeindexers & (1 << idx)) == (uint64_t) (1 << idx))
8694 {
8695 if(cfg.dvbapi_extended_cw_api == 2 && use_des != listitem->use_des)
8696 {
8697 listitem->use_des = use_des;
8698 return FIRST_STREAMPID_INDEX;
8699 }
8700 return FOUND_STREAMPID_INDEX; // match found
8701 }
8702 else
8703 {
8704 listitem->activeindexers |= (1 << idx); // ca + pid found but not this index -> add this index
8705 cs_log_dbg(D_DVBAPI, "Added existing streampid %04X with new index %d to ca%d", pid, idx, cadevice);
8706
8707 if(cfg.dvbapi_extended_cw_api == 2 && use_des != listitem->use_des)
8708 {
8709 listitem->use_des = use_des;
8710 return FIRST_STREAMPID_INDEX;
8711 }
8712 return ADDED_STREAMPID_INDEX;
8713 }
8714 }
8715 }
8716 }
8717
8718 if(!cs_malloc(&newlistitem, sizeof(struct s_streampid)))
8719 {
8720 return FIRST_STREAMPID_INDEX; // not sure if this is correct
8721 }
8722
8723 newlistitem->cadevice = cadevice;
8724 newlistitem->streampid = pid;
8725 newlistitem->activeindexers = (1 << idx);
8726 newlistitem->caindex = idx; // set this index as used to decode on ca device
8727 newlistitem->use_des = use_des;
8728
8729 ll_append(ll_activestreampids, newlistitem);
8730 cs_log_dbg(D_DVBAPI, "Added new streampid %04X with index %d to ca%d", pid, idx, cadevice);
8731
8732 return FIRST_STREAMPID_INDEX;
8733}
8734
8735int8_t remove_streampid_from_list(uint8_t cadevice, uint16_t pid, uint32_t idx)
8736{
8737 struct s_streampid *listitem;
8738 int8_t removed = 0;
8739 LL_ITER itr;
8740
8741 if(!ll_activestreampids)
8742 {
8743 return NO_STREAMPID_LISTED;
8744 }
8745
8746 if(idx >= INDEX_MAX)
8747 {
8748 return INVALID_STREAMPID_INDEX;
8749 }
8750
8751 if(ll_count(ll_activestreampids) > 0)
8752 {
8753 itr = ll_iter_create(ll_activestreampids);
8754 while((listitem = ll_iter_next(&itr)))
8755 {
8756 if(cadevice == listitem->cadevice && pid == listitem->streampid)
8757 {
8758 if(idx == INDEX_DISABLE_ALL)
8759 {
8760 listitem->activeindexers = 0;
8761 removed = 1;
8762 }
8763 else if((listitem->activeindexers & (1 << idx)) == (uint64_t) (1 << idx))
8764 {
8765 listitem->activeindexers &= ~(1 << idx); // flag it as disabled for this index
8766 removed = 1;
8767 }
8768
8769 if(removed)
8770 {
8771 cs_log_dbg(D_DVBAPI, "Remove streampid %04X using indexer %d from ca%d", pid, idx, cadevice);
8772 }
8773
8774 if(listitem->activeindexers == 0 && removed == 1) // all indexers disabled? -> remove pid from list!
8775 {
8776 ll_iter_remove_data(&itr);
8777 cs_log_dbg(D_DVBAPI, "Removed last indexer of streampid %04X from ca%d", pid, cadevice);
8778 ca_descramblers_used = count_active_indexers();
8779 return REMOVED_STREAMPID_LASTINDEX;
8780 }
8781 else if(removed == 1)
8782 {
8783 if(idx != INDEX_DISABLE_ALL && idx != listitem->caindex)
8784 {
8785 return REMOVED_STREAMPID_INDEX;
8786 }
8787 else
8788 {
8789 listitem->caindex = INDEX_INVALID;
8790 cs_log_dbg(D_DVBAPI, "Streampid %04X index %d was used for decoding on ca%d", pid, idx, cadevice);
8791 return REMOVED_DECODING_STREAMPID_INDEX;
8792 }
8793 }
8794 return INVALID_STREAMPID_INDEX;
8795 }
8796 }
8797 }
8798 return NO_STREAMPID_LISTED;
8799}
8800
8801void disable_unused_streampids(int16_t demux_id)
8802{
8803 int32_t ecmpid = demux[demux_id].pidindex;
8804
8805 if(ecmpid == -1 // no active ecmpid!
8806 || !ll_activestreampids
8807 || selected_api == STAPI // stapi handles pids itself!
8808 || ll_count(ll_activestreampids) == 0) // no items in list?
8809 {
8810 return;
8811 }
8812
8813 int32_t j;
8814 if(demux[demux_id].ECMpids[ecmpid].useMultipleIndices == 0)
8815 {
8816 uint32_t idx = demux[demux_id].ECMpids[ecmpid].index[0];
8817 int32_t i, n;
8818 struct s_streampid *listitem;
8819
8820 // search for old enabled streampids on
8821 // all ca devices that have to be disabled
8822 for(i = 0; i < CA_MAX && idx != INDEX_INVALID; i++)
8823 {
8824 if(!((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
8825 {
8826 continue; // ca is not used by this demuxer
8827 }
8828
8829 LL_ITER itr;
8830 itr = ll_iter_create(ll_activestreampids);
8831
8832 while((listitem = ll_iter_next(&itr)))
8833 {
8834 if(i != listitem->cadevice)
8835 {
8836 continue; // ca doesn't match
8837 }
8838
8839 if(!((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx))))
8840 {
8841 continue; // index doesn't match
8842 }
8843
8844 for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
8845 {
8846 if(demux[demux_id].ECMpidcount == 0) // FTA? -> disable stream!
8847 {
8848 n = demux[demux_id].STREAMpidcount;
8849 break;
8850 }
8851
8852 // check if pid matches with current stream pid on demuxer
8853 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8854 {
8855 break;
8856 }
8857 }
8858
8859 if(n == demux[demux_id].STREAMpidcount) // no match found
8860 {
8861 demux[demux_id].STREAMpids[n] = listitem->streampid; // put it here temporarily!
8862 dvbapi_set_pid(demux_id, n, idx, false, false, 0); // disable this unused streampid
8863 demux[demux_id].STREAMpids[n] = 0; // remove it from temp position!
8864 }
8865 }
8866
8867 // ECMpidcount != 0 -> skip enabling on fta
8868 for(n = 0; n < demux[demux_id].STREAMpidcount && demux[demux_id].ECMpidcount != 0; n++)
8869 {
8870 ll_iter_reset(&itr);
8871 if(!demux[demux_id].ECMpids[ecmpid].streams || ((demux[demux_id].ECMpids[ecmpid].streams & (1 << n)) == (uint) (1 << n)))
8872 {
8873 while((listitem = ll_iter_next(&itr)))
8874 {
8875 if(i != listitem->cadevice) // ca doesn't match
8876 {
8877 continue;
8878 }
8879
8880 if(!((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))) // index doesn't match
8881 {
8882 continue;
8883 }
8884
8885 // check if pid matches with current streampid on demuxer
8886 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8887 {
8888 break;
8889 }
8890 }
8891
8892 if(!listitem) // if streampid not listed -> enable it!
8893 {
8894 dvbapi_set_pid(demux_id, n, idx, true, false, 0); // enable streampid
8895 }
8896 }
8897 }
8898 }
8899 }
8900 else
8901 {
8902 uint32_t idx = INDEX_INVALID;
8903 int32_t i, n;
8904 uint8_t skip;
8905 struct s_streampid *listitem;
8906
8907 // search for old enabled streampids
8908 // on all ca devices that have to be disabled
8909 for(i = 0; i < CA_MAX && idx != INDEX_INVALID; i++)
8910 {
8911 if(!((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
8912 {
8913 continue; // continue if ca is unused by this demuxer
8914 }
8915
8916 LL_ITER itr;
8917 itr = ll_iter_create(ll_activestreampids);
8918
8919 while((listitem = ll_iter_next(&itr)))
8920 {
8921 if(i != listitem->cadevice)
8922 {
8923 continue; // ca doesn't match
8924 }
8925
8926 for(skip = 1, j = 0; j < MAX_STREAM_INDICES; j++)
8927 {
8928 idx = demux[demux_id].ECMpids[ecmpid].index[j];
8929 if(idx == INDEX_INVALID)
8930 {
8931 continue;
8932 }
8933
8934 // index match
8935 if((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))
8936 {
8937 skip = 0;
8938 break;
8939 }
8940 }
8941
8942 if(skip)
8943 {
8944 continue;
8945 }
8946
8947 for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
8948 {
8949 if(demux[demux_id].ECMpidcount == 0) // FTA? -> disable stream!
8950 {
8951 n = demux[demux_id].STREAMpidcount;
8952 break;
8953 }
8954
8955 // check if pid matches with current streampid on demuxer
8956 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8957 {
8958 break;
8959 }
8960 }
8961
8962 if(n == demux[demux_id].STREAMpidcount)
8963 {
8964 demux[demux_id].STREAMpids[n] = listitem->streampid; // put it temp here!
8965 dvbapi_set_pid(demux_id, n, idx, false, false, 0); // no match found so disable this now unused streampid
8966 demux[demux_id].STREAMpids[n] = 0; // remove temp!
8967 }
8968 }
8969
8970 // ECMpidcount != 0 -> skip enabling on fta
8971 for(n = 0; n < demux[demux_id].STREAMpidcount && demux[demux_id].ECMpidcount != 0; n++)
8972 {
8973 ll_iter_reset(&itr);
8974 if(!demux[demux_id].ECMpids[ecmpid].streams || ((demux[demux_id].ECMpids[ecmpid].streams & (1 << n)) == (uint) (1 << n)))
8975 {
8976 while((listitem = ll_iter_next(&itr)))
8977 {
8978 if(i != listitem->cadevice) // ca doesn't match
8979 {
8980 continue;
8981 }
8982
8983 for(skip = 1, j = 0; j < MAX_STREAM_INDICES; j++)
8984 {
8985 idx = demux[demux_id].ECMpids[ecmpid].index[j];
8986 if(idx == INDEX_INVALID)
8987 {
8988 continue;
8989 }
8990
8991 if((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))
8992 {
8993 skip = 0; // index match
8994 break;
8995 }
8996 }
8997
8998 if(skip)
8999 {
9000 continue;
9001 }
9002
9003 // check if pid matches with current streampid on demuxer
9004 if(listitem->streampid == demux[demux_id].STREAMpids[n])
9005 {
9006 break;
9007 }
9008 }
9009
9010 if(!listitem) // if streampid not listed -> enable it!
9011 {
9012 dvbapi_set_pid(demux_id, n, idx, true, false, 0); // enable streampid
9013 }
9014 }
9015 }
9016 }
9017 }
9018}
9019
9020uint32_t is_ca_used(uint8_t cadevice, int32_t pid)
9021{
9022 struct s_streampid *listitem;
9023 LL_ITER itr;
9024 if(!ll_activestreampids)
9025 {
9026 return INDEX_INVALID;
9027 }
9028
9029 if(ll_count(ll_activestreampids) > 0)
9030 {
9031 itr = ll_iter_create(ll_activestreampids);
9032 while((listitem = ll_iter_next(&itr)))
9033 {
9034 // if pid is 0, we match ca device only
9035 if(listitem->cadevice != cadevice || (pid && listitem->streampid != pid))
9036 {
9037 continue;
9038 }
9039
9040 uint32_t i = 0;
9041 while(listitem->caindex == INDEX_INVALID && i < INDEX_MAX)
9042 {
9043 if((listitem->activeindexers & (1 << i)) == (uint64_t) (1 << i))
9044 {
9045 listitem->caindex = i; // set fresh one
9046 cs_log_dbg(D_DVBAPI, "Streampid %04X is now using index %d for decoding on ca%d", pid, i, cadevice);
9047 break;
9048 }
9049 i++;
9050 }
9051
9052 if(listitem->caindex == INDEX_INVALID)
9053 {
9054 ll_iter_remove_data(&itr);
9055 return INDEX_INVALID;
9056 }
9057 return listitem->caindex;
9058 }
9059 }
9060 return INDEX_INVALID; // no indexer found for this pid!
9061}
9062
9063uint32_t count_active_indexers(void)
9064{
9065 uint i, usecounter = 0;
9066 struct s_streampid *listitem;
9067 LL_ITER itr;
9068
9069 if(!ll_activestreampids)
9070 {
9071 return 0;
9072 }
9073
9074 bool indexer_in_use[ca_descramblers_total];
9075 memset(&indexer_in_use, 0, sizeof(indexer_in_use));
9076
9077 if(ll_count(ll_activestreampids) > 0)
9078 {
9079 itr = ll_iter_create(ll_activestreampids);
9080 while((listitem = ll_iter_next(&itr)))
9081 {
9082 if(listitem->caindex != INDEX_INVALID && listitem->caindex < INDEX_MAX)
9083 {
9084 indexer_in_use[listitem->caindex] = true;
9085 }
9086 }
9087
9088 for(i = 0; i < ca_descramblers_total; i++)
9089 {
9090 if(indexer_in_use[i] == true)
9091 {
9092 usecounter++;
9093 }
9094 }
9095 }
9096
9097 return usecounter;
9098}
9099
9100uint16_t dvbapi_get_client_proto_version(void)
9101{
9102 return last_client_proto_version;
9103}
9104
9105const char *dvbapi_get_client_name(void)
9106{
9107 return last_client_name ? last_client_name : "";
9108}
9109
9110void check_add_emmpid(int32_t demux_id, uint8_t *filter, int32_t l, int32_t emmtype)
9111{
9112 if(l < 0) { return; }
9113
9114 uint32_t typtext_idx = 0;
9115 int32_t ret = -1;
9116 const char *typtext[] = { "UNIQUE", "SHARED", "GLOBAL", "UNKNOWN" };
9117
9118 while(((emmtype >> typtext_idx) & 0x01) == 0 && typtext_idx < sizeof(typtext) / sizeof(const char *))
9119 {
9120 ++typtext_idx;
9121 }
9122
9123 // filter already in list?
9124 if(is_emmfilter_in_list(filter, demux[demux_id].EMMpids[l].PID, demux[demux_id].EMMpids[l].PROVID, demux[demux_id].EMMpids[l].CAID))
9125 {
9126 cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate emm filter type %s, emmpid: 0x%04X, emmcaid: %04X, emmprovid: %06X -> SKIPPED!",
9127 demux_id,
9128 typtext[typtext_idx],
9129 demux[demux_id].EMMpids[l].PID,
9130 demux[demux_id].EMMpids[l].CAID,
9131 demux[demux_id].EMMpids[l].PROVID);
9132 return;
9133 }
9134
9135 if(demux[demux_id].emm_filter < demux[demux_id].max_emm_filter) // can this filter be started?
9136 {
9137 // try to activate this emmfilter
9138 ret = dvbapi_set_filter(demux_id,
9139 selected_api,
9140 demux[demux_id].EMMpids[l].PID,
9141 demux[demux_id].EMMpids[l].CAID,
9142 demux[demux_id].EMMpids[l].PROVID,
9143 filter,
9144 filter + 16,
9145 0,
9146 demux[demux_id].pidindex,
9147 TYPE_EMM, 1);
9148 }
9149
9150 if(ret != -1) // -1 if maxfilter reached or filter start error!
9151 {
9152 if(demux[demux_id].emm_filter == -1) // -1: first run of emm filtering on this demuxer
9153 {
9154 demux[demux_id].emm_filter = 0;
9155 }
9156 demux[demux_id].emm_filter++; // increase total active filters
9157
9158 cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d started emm filter type %s, pid: 0x%04X",
9159 demux_id, typtext[typtext_idx], demux[demux_id].EMMpids[l].PID);
9160 return;
9161 }
9162 else // not set successful, so add it to the list for try again later on!
9163 {
9164 add_emmfilter_to_list(demux_id, filter, demux[demux_id].EMMpids[l].CAID, demux[demux_id].EMMpids[l].PROVID, demux[demux_id].EMMpids[l].PID, 0, false);
9165 cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d added inactive emm filter type %s, pid: 0x%04X",
9166 demux_id, typtext[typtext_idx], demux[demux_id].EMMpids[l].PID);
9167 }
9168 return;
9169}
9170
9171void rotate_emmfilter(int32_t demux_id)
9172{
9173 // emm filter iteration
9174 if(!ll_emm_active_filter)
9175 {
9176 ll_emm_active_filter = ll_create("ll_emm_active_filter");
9177 }
9178
9179 if(!ll_emm_inactive_filter)
9180 {
9181 ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
9182 }
9183
9184 if(!ll_emm_pending_filter)
9185 {
9186 ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
9187 }
9188
9189 uint32_t filter_count = ll_count(ll_emm_active_filter) + ll_count(ll_emm_inactive_filter);
9190 if(demux[demux_id].max_emm_filter > 0 && ll_count(ll_emm_inactive_filter) > 0 && filter_count > demux[demux_id].max_emm_filter)
9191 {
9192 int32_t filter_queue = ll_count(ll_emm_inactive_filter);
9193 int32_t stopped = 0, started = 0;
9194 struct timeb now;
9195 cs_ftime(&now);
9196 struct s_emm_filter *filter_item;
9197 LL_ITER itr;
9198 itr = ll_iter_create(ll_emm_active_filter);
9199
9200 while((filter_item = ll_iter_next(&itr)) != NULL)
9201 {
9202 if(!ll_count(ll_emm_inactive_filter) || started == filter_queue)
9203 {
9204 break;
9205 }
9206
9207 int64_t gone = comp_timeb(&now, &filter_item->time_started);
9208 if(gone > 45 * 1000)
9209 {
9210 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match_emmpid(filter_item->demux_id, filter_item->caid, filter_item->provid, 'p');
9211 if(!forceentry || (forceentry && !forceentry->force))
9212 {
9213 // stop active filter and add to pending list
9214 dvbapi_stop_filternum(filter_item->demux_id, filter_item->num - 1, 0);
9215 ll_iter_remove_data(&itr);
9216 add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, -1, false);
9217 stopped++;
9218 }
9219 }
9220
9221 int32_t ret;
9222 if(stopped > started) // we have room for new filters, try to start an inactive emmfilter!
9223 {
9224 struct s_emm_filter *filter_item2;
9225 LL_ITER itr2 = ll_iter_create(ll_emm_inactive_filter);
9226 while((filter_item2 = ll_iter_next(&itr2)))
9227 {
9228 ret = dvbapi_set_filter(filter_item2->demux_id,
9229 selected_api,
9230 filter_item2->pid,
9231 filter_item2->caid,
9232 filter_item2->provid,
9233 filter_item2->filter,
9234 filter_item2->filter + 16,
9235 0,
9236 demux[filter_item2->demux_id].pidindex,
9237 TYPE_EMM, 1);
9238 if(ret != -1)
9239 {
9240 ll_iter_remove_data(&itr2);
9241 started++;
9242 break;
9243 }
9244 }
9245 }
9246 }
9247
9248 itr = ll_iter_create(ll_emm_pending_filter);
9249 while((filter_item = ll_iter_next(&itr)) != NULL) // move pending filters to inactive
9250 {
9251 add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, 0, false);
9252 ll_iter_remove_data(&itr);
9253 }
9254 }
9255}
9256
9257int32_t filtermatch(uint8_t *buffer, int32_t filter_num, int32_t demux_id, int32_t len)
9258{
9259 int32_t i, k, match;
9260 uint8_t flt, mask;
9261
9262 match = 1;
9263 for(i = 0, k = 0; i < 16 && match; i++, k++)
9264 {
9265 mask = demux[demux_id].demux_fd[filter_num].mask[i];
9266 if(k == 1) // skip len bytes
9267 {
9268 k += 2;
9269 }
9270
9271 if(!mask)
9272 {
9273 continue;
9274 }
9275
9276 flt = (demux[demux_id].demux_fd[filter_num].filter[i]&mask);
9277 cs_log_dbg(D_DVBAPI,"Demuxer %d filter%d[%d] = %02X, filter mask[%d] = %02X, flt&mask = %02X , buffer[%d] = %02X, buffer[%d] & mask = %02X",
9278 demux_id, filter_num + 1, i, demux[demux_id].demux_fd[filter_num].filter[i], i, mask, flt&mask, k, buffer[k], k, buffer[k] & mask);
9279
9280 if(k <= len)
9281 {
9282 match = (flt == (buffer[k] & mask));
9283 }
9284 else
9285 {
9286 match = 0;
9287 }
9288 }
9289 return (match && i == 16); // 0 = delivered data does not match with filter, 1 = delivered data matches with filter
9290}
9291
9292/*
9293 * protocol structure
9294 */
9295void module_dvbapi(struct s_module *ph)
9296{
9297 ph->desc = "dvbapi";
9298 ph->type = MOD_CONN_SERIAL;
9299 ph->listenertype = LIS_DVBAPI;
9300#if defined(WITH_AZBOX)
9301 ph->s_handler = azbox_handler;
9302 ph->send_dcw = azbox_send_dcw;
9303#elif defined(WITH_MCA)
9304 ph->s_handler = mca_handler;
9305 ph->send_dcw = mca_send_dcw;
9306 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
9307#else
9308 ph->s_handler = dvbapi_handler;
9309 ph->send_dcw = dvbapi_send_dcw;
9310#endif
9311}
9312#endif // HAVE_DVBAPI
Note: See TracBrowser for help on using the repository browser.