source: trunk/module-dvbapi.c@ 11705

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

[dvbapi] Clear ECM pids on CA PMT updates as well

ECM pids where not cleared when a CA PMT update was received. This was actually a bug carried over from the old code.

  • Property svn:eol-style set to LF
File size: 255.2 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 switch(ca_system_id >> 8)
2136 {
2137 case 0x01:
2138 {
2139 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, 0, 0, EMM_UNIQUE | EMM_GLOBAL);
2140
2141 for(i = 5; i < descriptor_length; i += 4)
2142 {
2143 ca_pid = b2i(2, buffer + i) & 0x1FFF;
2144 ca_provider = b2i(2, buffer + i + 2);
2145
2146 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_SHARED);
2147 }
2148 break;
2149 }
2150
2151 case 0x05:
2152 {
2153 for(i = 4; i < descriptor_length; i += 2 + buffer[i + 1])
2154 {
2155 if(buffer[i] == 0x14)
2156 {
2157 ca_provider = (b2i(3, buffer + i + 2) & 0xFFFFF0); // viaccess fixup: don't care about last digit
2158 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2159 }
2160 }
2161 break;
2162 }
2163
2164 case 0x18:
2165 {
2166 if(descriptor_length == 0x07 || descriptor_length == 0x0B)
2167 {
2168 for(i = 5; i < 5 + buffer[4]; i += 2)
2169 {
2170 ca_provider = b2i(2, buffer + i);
2171 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2172 }
2173 }
2174 else
2175 {
2176 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2177 }
2178 break;
2179 }
2180
2181 case 0x27:
2182 case 0x4A:
2183 {
2184 if(caid_is_bulcrypt(ca_system_id))
2185 {
2186 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, 0, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2187 break;
2188 }
2189
2190 ca_provider = buffer[4];
2191
2192 if(buffer[4] == 0xFE)
2193 {
2194 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0x102, EMM_GLOBAL);
2195 }
2196 else
2197 {
2198 if(descriptor_length == 0x0A)
2199 {
2200 ca_data = b2i(4, buffer + 6);
2201 }
2202
2203 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, ca_provider, ca_data, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2204 }
2205 break;
2206 }
2207
2208 default:
2209 dvbapi_add_emmpid(demux_id, ca_system_id, ca_pid, 0, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
2210 break;
2211 }
2212}
2213
2214static void dvbapi_parse_cat(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
2215{
2216#if defined WITH_COOLAPI || defined WITH_COOLAPI2
2217 // driver sometimes reports error if too many emm filter
2218 // but adding more ecm filter is no problem
2219 // ... so ifdef here instead of limiting MAX_FILTER
2220 demux[demux_id].max_emm_filter = 14;
2221#else
2222 if(cfg.dvbapi_requestmode == 1)
2223 {
2224 uint16_t ecm_filter_needed = 0, n;
2225 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
2226 {
2227 if(demux[demux_id].ECMpids[n].status > -1)
2228 {
2229 ecm_filter_needed++;
2230 }
2231 }
2232
2233 if(maxfilter - ecm_filter_needed <= 0)
2234 {
2235 demux[demux_id].max_emm_filter = 0;
2236 }
2237 else
2238 {
2239 demux[demux_id].max_emm_filter = maxfilter - ecm_filter_needed;
2240 }
2241 }
2242 else
2243 {
2244 demux[demux_id].max_emm_filter = maxfilter - 1;
2245 }
2246#endif
2247
2248 uint16_t i;
2249 uint8_t descriptor_tag, descriptor_length;
2250
2251 dvbapi_stop_filter(demux_id, TYPE_CAT, msgid);
2252
2253 for(i = 8; i + 1 < length; i += 2 + descriptor_length)
2254 {
2255 descriptor_tag = buffer[i];
2256 descriptor_length = buffer[i + 1];
2257
2258 if(descriptor_tag == 0x09) // There should be only CA descriptors here
2259 {
2260 dvbapi_parse_cat_ca_descriptor(demux_id, buffer + i + 2, descriptor_length);
2261 }
2262 }
2263}
2264
2265static pthread_mutex_t lockindex = PTHREAD_MUTEX_INITIALIZER;
2266
2267uint32_t dvbapi_get_desc_index(int32_t demux_id, int32_t pid, int32_t stream_id)
2268{
2269 int32_t i, j, k, fail = 1;
2270 uint32_t idx = 0;
2271
2272 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
2273 {
2274 sscanf(demux[demux_id].pmt_file, "pmt%3d.tmp", &idx);
2275 return idx;
2276 }
2277
2278 SAFE_MUTEX_LOCK(&lockindex); // to avoid race when readers become responsive!
2279 while(fail && idx <= INDEX_MAX)
2280 {
2281 fail = 0;
2282 for(i = 0; i < MAX_DEMUX && !fail && idx < INDEX_MAX; i++)
2283 {
2284 if(demux[i].program_number == 0)
2285 {
2286 continue; // skip empty demuxers
2287 }
2288
2289 if(demux[i].ca_mask != demux[demux_id].ca_mask
2290 && (!(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)))
2291 {
2292 continue; // skip demuxer using other ca device
2293 }
2294
2295 for(j = 0; j < demux[i].ECMpidcount && !fail; j++) // search for new unique index
2296 {
2297 for(k = 0; k < MAX_STREAM_INDICES; k++)
2298 {
2299 if(demux[i].ECMpids[j].index[k] == idx)
2300 {
2301 fail = 1;
2302 idx++;
2303 }
2304 }
2305 }
2306 }
2307 }
2308
2309 if(selected_api == DVBAPI_3 || selected_api == DVBAPI_1)
2310 {
2311 // make sure we haven't run out of descramblers
2312 if(ca_descramblers_used < ca_descramblers_total)
2313 {
2314 ca_descramblers_used++; // increase number of descramblers used
2315 }
2316 else
2317 {
2318 idx = INDEX_INVALID; // we don't have any descramblers left
2319 }
2320 }
2321 else // old style check for STAPI, COOLAPI, etc
2322 {
2323 if(idx > ca_descramblers_total)
2324 {
2325 idx = INDEX_INVALID; // we don't have any descramblers left
2326 }
2327 }
2328
2329 demux[demux_id].ECMpids[pid].index[stream_id] = idx;
2330 SAFE_MUTEX_UNLOCK(&lockindex); // and release it!
2331
2332 return idx;
2333}
2334
2335void dvbapi_set_pid(int32_t demux_id, int32_t num, uint32_t idx, bool enable, bool use_des, uint32_t msgid)
2336{
2337 int32_t i, currentfd;
2338 uint16_t streampid = demux[demux_id].STREAMpids[num];
2339 uint32_t newidx = 0, curidx;
2340 ca_pid_t ca_pid2;
2341
2342 if(demux[demux_id].pidindex == -1 && enable)
2343 {
2344 return; // no current pid on enable? --> exit
2345 }
2346
2347 switch(selected_api)
2348 {
2349#if defined(WITH_STAPI) || defined(WITH_STAPI5)
2350 case STAPI:
2351 if(!enable) idx = INDEX_INVALID;
2352 stapi_set_pid(demux_id, num, idx, streampid, demux[demux_id].pmt_file); // only used to disable pids!!!
2353 break;
2354#endif
2355
2356#if defined WITH_COOLAPI || defined WITH_COOLAPI2
2357 case COOLAPI:
2358 break;
2359#endif
2360
2361 default:
2362 for(i = 0; i < CA_MAX; i++)
2363 {
2364 newidx = INDEX_INVALID;
2365 curidx = idx;
2366
2367 if(((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
2368 {
2369 uint32_t action = 0;
2370 if(enable)
2371 {
2372 action = update_streampid_list(i, streampid, curidx, use_des);
2373 }
2374
2375 if(!enable)
2376 {
2377 action = remove_streampid_from_list(i, streampid, curidx);
2378 }
2379
2380 if(action != NO_STREAMPID_LISTED && action != INVALID_STREAMPID_INDEX &&
2381 action != FOUND_STREAMPID_INDEX && action != ADDED_STREAMPID_INDEX &&
2382 action != REMOVED_STREAMPID_INDEX)
2383 {
2384 // removed last index of this streampid on ca? -> disable this pid with -1 on this ca
2385 if((action == REMOVED_STREAMPID_LASTINDEX || action == FIRST_STREAMPID_INDEX)
2386 && (is_ca_used(i, streampid) == INDEX_INVALID))
2387 {
2388 curidx = DVBAPI_INDEX_DISABLE;
2389 }
2390
2391 // removed index of streampid that is used to decode on ca -> get a fresh one
2392 if(action == REMOVED_DECODING_STREAMPID_INDEX || action == FIRST_STREAMPID_INDEX)
2393 {
2394 newidx = is_ca_used(i, streampid); // get an active index for this pid and enable it on ca device
2395 curidx = DVBAPI_INDEX_DISABLE;
2396 }
2397
2398 while(curidx != INDEX_INVALID || newidx != INDEX_INVALID)
2399 {
2400 memset(&ca_pid2, 0, sizeof(ca_pid2));
2401 ca_pid2.pid = streampid;
2402 if(curidx != INDEX_INVALID)
2403 {
2404 (curidx == DVBAPI_INDEX_DISABLE) ? (ca_pid2.index = -1) : (ca_pid2.index = curidx);
2405 cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x index=%d on ca%d",
2406 demux_id,
2407 ((enable && curidx != DVBAPI_INDEX_DISABLE) ? "enable" : "disable"),
2408 num + 1,
2409 ca_pid2.pid,
2410 ca_pid2.index,
2411 i);
2412 curidx = INDEX_INVALID; // flag this index as handled
2413 }
2414 else if(newidx != INDEX_INVALID)
2415 {
2416 (newidx == DVBAPI_INDEX_DISABLE) ? (ca_pid2.index = -1) : (ca_pid2.index = newidx);
2417 cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x by index=%d on ca%d",
2418 demux_id,
2419 ((enable && action == FIRST_STREAMPID_INDEX) ? "enable" : "takeover"),
2420 num + 1,
2421 ca_pid2.pid,
2422 ca_pid2.index,
2423 i);
2424 newidx = INDEX_INVALID; // flag this takeover / new index as handled
2425 }
2426
2427 if(use_des && cfg.dvbapi_extended_cw_api == 2 && ca_pid2.index != -1)
2428 {
2429 ca_pid2.index |= 0x100; // flag DES algo through pid index
2430 }
2431
2432 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
2433 {
2434 dvbapi_net_send(DVBAPI_CA_SET_PID, demux[demux_id].socket_fd,
2435 msgid,
2436 demux_id,
2437 -1 /*unused*/,
2438 (uint8_t *) &ca_pid2,
2439 NULL,
2440 NULL,
2441 demux[demux_id].client_proto_version);
2442 }
2443 else
2444 {
2445 currentfd = ca_fd[i];
2446 if(currentfd <= 0)
2447 {
2448 currentfd = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
2449 ca_fd[i] = currentfd; // save fd of this ca
2450 }
2451
2452 if(currentfd > 0)
2453 {
2454 if(dvbapi_ioctl(currentfd, CA_SET_PID, &ca_pid2) == -1)
2455 {
2456 cs_log_dbg(D_TRACE | D_DVBAPI,"CA_SET_PID ioctl error (errno=%d %s)", errno, strerror(errno));
2457 remove_streampid_from_list(i, ca_pid2.pid, INDEX_DISABLE_ALL);
2458 }
2459
2460 uint32_t result = is_ca_used(i, 0); // check if in use by any pid
2461 if(result == INDEX_INVALID)
2462 {
2463 cs_log_dbg(D_DVBAPI, "Demuxer %d close now unused CA%d device", demux_id, i);
2464 int32_t ret = close(currentfd);
2465 if(ret < 0)
2466 {
2467 cs_log("ERROR: Could not close demuxer fd (errno=%d %s)", errno, strerror(errno));
2468 }
2469 ca_fd[i] = 0;
2470 }
2471 }
2472 }
2473 }
2474 }
2475 }
2476 }
2477 break;
2478 }
2479 return;
2480}
2481
2482void dvbapi_stop_all_descrambling(uint32_t msgid)
2483{
2484 int32_t j;
2485
2486 for(j = 0; j < MAX_DEMUX; j++)
2487 {
2488 if(demux[j].program_number == 0)
2489 {
2490 continue;
2491 }
2492
2493 dvbapi_stop_descrambling(j, msgid);
2494 }
2495}
2496
2497void dvbapi_stop_all_cat_emm_sdt_filtering(uint32_t msgid)
2498{
2499 int32_t j;
2500
2501 for(j = 0; j < MAX_DEMUX; j++)
2502 {
2503 if(demux[j].program_number == 0)
2504 {
2505 continue;
2506 }
2507
2508 dvbapi_stop_filter(j, TYPE_EMM, msgid);
2509 dvbapi_stop_filter(j, TYPE_SDT, msgid);
2510 dvbapi_stop_filter(j, TYPE_CAT, msgid);
2511 demux[j].emm_filter = -1;
2512 }
2513}
2514
2515void dvbapi_stop_descrambling(int32_t demux_id, uint32_t msgid)
2516{
2517 int32_t i, j, z;
2518 if(demux[demux_id].program_number == 0) { return; }
2519 char channame[CS_SERVICENAME_SIZE];
2520
2521 i = demux[demux_id].pidindex;
2522 if(i < 0)
2523 {
2524 i = 0;
2525 }
2526
2527 demux[demux_id].pidindex = -1; // no ecmpid is to be descrambling since we start stop descrambling!
2528
2529 get_servicename(dvbapi_client, demux[demux_id].program_number,
2530 demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].PROVID : NO_PROVID_VALUE,
2531 demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].CAID : NO_CAID_VALUE,
2532 channame, sizeof(channame));
2533
2534 cs_log("Demuxer %d stopped descrambling for program %04X (%s)",
2535 demux_id, demux[demux_id].program_number, channame);
2536
2537 dvbapi_stop_filter(demux_id, TYPE_EMM, msgid);
2538 dvbapi_stop_filter(demux_id, TYPE_SDT, msgid);
2539 dvbapi_stop_filter(demux_id, TYPE_PAT, msgid);
2540 dvbapi_stop_filter(demux_id, TYPE_PMT, msgid);
2541 dvbapi_stop_filter(demux_id, TYPE_CAT, msgid);
2542
2543 for(i = 0; i < demux[demux_id].ECMpidcount && demux[demux_id].ECMpidcount > 0; i++)
2544 {
2545 for(j = 0; j < MAX_STREAM_INDICES; j++)
2546 {
2547 if(demux[demux_id].ECMpids[i].index[j] == INDEX_INVALID)
2548 {
2549 continue;
2550 }
2551
2552 // disable streams!
2553 for(z = 0; z < demux[demux_id].STREAMpidcount; z++)
2554 {
2555 dvbapi_set_pid(demux_id, z, demux[demux_id].ECMpids[i].index[j], false, false, msgid); // disable streampid
2556 }
2557 demux[demux_id].ECMpids[i].index[j] = INDEX_INVALID;
2558 }
2559 }
2560 dvbapi_stop_filter(demux_id, TYPE_ECM, msgid);
2561
2562 pthread_mutex_destroy(&demux[demux_id].answerlock);
2563 memset(&demux[demux_id], 0 , sizeof(DEMUXTYPE));
2564
2565 SAFE_MUTEX_INIT(&demux[demux_id].answerlock, NULL);
2566 for(i = 0; i < MAX_ECM_PIDS; i++)
2567 {
2568 for(j = 0; j < MAX_STREAM_INDICES; j++)
2569 {
2570 demux[demux_id].ECMpids[i].index[j] = INDEX_INVALID;
2571 }
2572 }
2573 demux[demux_id].pidindex = -1;
2574 demux[demux_id].curindex = -1;
2575
2576 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
2577 {
2578 unlink(ECMINFO_FILE);
2579 }
2580 return;
2581}
2582
2583int32_t dvbapi_start_descrambling(int32_t demux_id, int32_t pid, int8_t checked, uint32_t msgid)
2584{
2585 int32_t started = 0; // in case ecmfilter started = 1
2586 int32_t fake_ecm = 0;
2587 ECM_REQUEST *er;
2588 struct s_reader *rdr;
2589
2590 if(!(er = get_ecmtask()))
2591 {
2592 return started;
2593 }
2594 demux[demux_id].ECMpids[pid].checked = checked + 1; // mark this pid as checked!
2595
2596 struct s_dvbapi_priority *p;
2597 for(p = dvbapi_priority; p != NULL ; p = p->next)
2598 {
2599 if((p->type != 'p')
2600 || (p->caid && p->caid != demux[demux_id].ECMpids[pid].CAID)
2601 || (p->provid && p->provid != demux[demux_id].ECMpids[pid].PROVID)
2602 || (p->ecmpid && p->ecmpid != demux[demux_id].ECMpids[pid].ECM_PID)
2603 || (p->srvid && p->srvid != demux[demux_id].program_number)
2604 || (p->pidx && p->pidx-1 != pid)
2605 || (p->cadata && p->cadata != demux[demux_id].ECMpids[pid].cadata))
2606 {
2607 continue;
2608 }
2609
2610 // if found chid and first run apply chid filter, on forced pids always apply!
2611 if(p->type == 'p' && p->chid < 0x10000 && (demux[demux_id].ECMpids[pid].checked == 1 || (p && p->force)))
2612 {
2613 if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
2614 {
2615 er->chid = demux[demux_id].ECMpids[pid].CHID;
2616 }
2617 else
2618 {
2619 er->chid = p->chid; // no channelcache or no chid in use, so use prio chid
2620 demux[demux_id].ECMpids[pid].CHID = p->chid;
2621 }
2622 //cs_log("********* CHID %04X **************", demux[demux_id].ECMpids[pid].CHID);
2623 break; // we only accept one!
2624 }
2625 else
2626 {
2627 if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
2628 {
2629 er->chid = demux[demux_id].ECMpids[pid].CHID;
2630 }
2631 else // no channelcache or no chid in use
2632 {
2633 er->chid = 0;
2634 demux[demux_id].ECMpids[pid].CHID = 0x10000;
2635 }
2636 }
2637 }
2638
2639 er->srvid = demux[demux_id].program_number;
2640 er->caid = demux[demux_id].ECMpids[pid].CAID;
2641 er->pid = demux[demux_id].ECMpids[pid].ECM_PID;
2642 er->prid = demux[demux_id].ECMpids[pid].PROVID;
2643 er->vpid = demux[demux_id].ECMpids[pid].VPID;
2644 er->pmtpid = demux[demux_id].pmtpid;
2645 er->onid = demux[demux_id].onid;
2646 er->msgid = msgid;
2647
2648#ifdef WITH_STAPI5
2649 cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
2650#endif
2651 struct timeb now;
2652 cs_ftime(&now);
2653
2654 for(rdr = first_active_reader; rdr != NULL ; rdr = rdr->next)
2655 {
2656 int8_t match = matching_reader(er, rdr); // check for matching reader
2657 int64_t gone = comp_timeb(&now, &rdr->emm_last);
2658 if(gone > 3600*1000 && rdr->needsemmfirst && caid_is_irdeto(er->caid))
2659 {
2660 cs_log("Warning reader %s received no emms for the last %d seconds -> skip, this reader needs emms first!",
2661 rdr->label, (int)(gone / 1000));
2662 continue; // skip this card needs to process emms first before it can be used for descramble
2663 }
2664
2665 if(p && p->force)
2666 {
2667 match = 1; // forced pid always started!
2668 }
2669
2670 if(!match) // if this reader does not match, check betatunnel for it
2671 {
2672 match = lb_check_auto_betatunnel(er, rdr);
2673 }
2674
2675 if(!match && chk_is_betatunnel_caid(er->caid)) // these caids might be tunneled invisible by peers
2676 {
2677 match = 1; // so make it a match to try it!
2678 }
2679
2680 // check if cache-ex is matching
2681 if(config_enabled(CS_CACHEEX) && (!match && (cacheex_is_match_alias(dvbapi_client, er))))
2682 {
2683 match = 1; // so make it a match to try it!
2684 }
2685
2686 // BISS1 and BISS2 mode 1/E or FAKE caid
2687 // ecm pid is fake, so send out one fake ecm request
2688 // special treatment: if we asked the cw first without starting a filter,
2689 // the cw request will be killed due to no ecmfilter started
2690 if(caid_is_fake(demux[demux_id].ECMpids[pid].CAID) || caid_is_biss_fixed(demux[demux_id].ECMpids[pid].CAID))
2691 {
2692 int32_t j, n;
2693 er->ecmlen = 5;
2694 er->ecm[0] = 0x80; // to pass the cache check it must be 0x80 or 0x81
2695 er->ecm[1] = 0x00;
2696 er->ecm[2] = 0x02;
2697 i2b_buf(2, er->srvid, er->ecm + 3);
2698
2699 for(j = 0, n = 5; j < demux[demux_id].STREAMpidcount; j++, n += 2)
2700 {
2701 i2b_buf(2, demux[demux_id].STREAMpids[j], er->ecm + n);
2702 er->ecm[2] += 2;
2703 er->ecmlen += 2;
2704 }
2705
2706 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
2707 demux_id,
2708 pid,
2709 demux[demux_id].ECMpids[pid].CAID,
2710 demux[demux_id].ECMpids[pid].PROVID,
2711 demux[demux_id].ECMpids[pid].ECM_PID,
2712 demux[demux_id].pmtpid,
2713 demux[demux_id].ECMpids[pid].VPID);
2714
2715 demux[demux_id].curindex = pid; // set current pid to the fresh started one
2716 dvbapi_start_filter(demux_id,
2717 pid,
2718 demux[demux_id].ECMpids[pid].ECM_PID,
2719 demux[demux_id].ECMpids[pid].CAID,
2720 demux[demux_id].ECMpids[pid].PROVID,
2721 0x80,
2722 0xF0,
2723 3000,
2724 TYPE_ECM);
2725
2726 started = 1;
2727 request_cw(dvbapi_client, er, demux_id, 0); // do not register ecm since this try!
2728 fake_ecm = 1;
2729 break; // we started an ecmfilter so stop looking for next matching reader!
2730 }
2731
2732 if(match) // if matching reader found check for irdeto cas if local irdeto card check if it received emms in last 60 minutes
2733 {
2734 if(caid_is_irdeto(er->caid)) // irdeto cas init irdeto_curindex to wait for first index (00)
2735 {
2736 if(demux[demux_id].ECMpids[pid].irdeto_curindex == 0xFE)
2737 {
2738 demux[demux_id].ECMpids[pid].irdeto_curindex = 0x00;
2739 }
2740 }
2741
2742 if(p && p->chid < 0x10000) // do we prio a certain chid?
2743 {
2744 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X PMTPID %04X VPID %04X",
2745 demux_id, pid,
2746 demux[demux_id].ECMpids[pid].CAID,
2747 demux[demux_id].ECMpids[pid].PROVID,
2748 demux[demux_id].ECMpids[pid].ECM_PID,
2749 demux[demux_id].ECMpids[pid].CHID,
2750 demux[demux_id].pmtpid,
2751 demux[demux_id].ECMpids[pid].VPID);
2752 }
2753 else
2754 {
2755 cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
2756 demux_id, pid,
2757 demux[demux_id].ECMpids[pid].CAID,
2758 demux[demux_id].ECMpids[pid].PROVID,
2759 demux[demux_id].ECMpids[pid].ECM_PID,
2760 demux[demux_id].pmtpid,
2761 demux[demux_id].ECMpids[pid].VPID);
2762 }
2763
2764 demux[demux_id].curindex = pid; // set current pid to the fresh started one
2765 dvbapi_start_filter(demux_id,
2766 pid,
2767 demux[demux_id].ECMpids[pid].ECM_PID,
2768 demux[demux_id].ECMpids[pid].CAID,
2769 demux[demux_id].ECMpids[pid].PROVID,
2770 0x80,
2771 0xF0,
2772 3000,
2773 TYPE_ECM);
2774
2775 started = 1;
2776 break; // we started an ecmfilter so stop looking for next matching reader!
2777 }
2778 }
2779
2780 if(demux[demux_id].curindex != pid)
2781 {
2782 cs_log("Demuxer %d impossible to descramble PID %d CAID %04X PROVID %06X ECMPID %04X PMTPID %04X (NO MATCHING READER)",
2783 demux_id, pid,
2784 demux[demux_id].ECMpids[pid].CAID,
2785 demux[demux_id].ECMpids[pid].PROVID,
2786 demux[demux_id].ECMpids[pid].ECM_PID,
2787 demux[demux_id].pmtpid);
2788
2789 demux[demux_id].ECMpids[pid].checked = 4; // flag this pid as checked
2790 demux[demux_id].ECMpids[pid].status = -1; // flag this pid as unusable
2791 dvbapi_edit_channel_cache(demux_id, pid, 0); // remove this pid from channelcache
2792 }
2793
2794 if(!fake_ecm)
2795 {
2796 NULLFREE(er);
2797 }
2798 return started;
2799}
2800
2801struct s_dvbapi_priority *dvbapi_check_prio_match_emmpid(int32_t demux_id, uint16_t caid, uint32_t provid, char type)
2802{
2803 struct s_dvbapi_priority *p;
2804 int32_t i;
2805 uint16_t ecm_pid = 0;
2806
2807 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
2808 {
2809 if((demux[demux_id].ECMpids[i].CAID == caid) && (demux[demux_id].ECMpids[i].PROVID == provid))
2810 {
2811 ecm_pid = demux[demux_id].ECMpids[i].ECM_PID;
2812 break;
2813 }
2814 }
2815
2816 if(!ecm_pid)
2817 {
2818 return NULL;
2819 }
2820
2821 for(p = dvbapi_priority; p != NULL; p = p->next)
2822 {
2823 if(p->type != type
2824 || (p->caid && p->caid != caid)
2825 || (p->provid && p->provid != provid)
2826 || (p->ecmpid && p->ecmpid != ecm_pid)
2827 || (p->srvid && p->srvid != demux[demux_id].program_number)
2828 || (p->pidx && p->pidx-1 !=i)
2829 || (p->type == 'i' && (p->chid < 0x10000)))
2830 {
2831 continue;
2832 }
2833
2834 return p;
2835 }
2836 return NULL;
2837}
2838
2839struct s_dvbapi_priority *dvbapi_check_prio_match(int32_t demux_id, int32_t pidindex, char type)
2840{
2841 if(!dvbapi_priority)
2842 {
2843 return NULL;
2844 }
2845
2846 struct s_dvbapi_priority *p;
2847 struct s_ecmpid *ecmpid = &demux[demux_id].ECMpids[pidindex];
2848
2849 for(p = dvbapi_priority; p != NULL; p = p->next)
2850 {
2851 if(p->type != type
2852 || (p->caid && p->caid != ecmpid->CAID)
2853 || (p->provid && p->provid != ecmpid->PROVID)
2854 || (p->ecmpid && p->ecmpid != ecmpid->ECM_PID)
2855 || (p->srvid && p->srvid != demux[demux_id].program_number)
2856 || (p->pidx && p->pidx-1 != pidindex)
2857 || (p->chid < 0x10000 && p->chid != ecmpid->CHID))
2858 {
2859 continue;
2860 }
2861
2862 return p;
2863 }
2864 return NULL;
2865}
2866
2867void dvbapi_process_emm(int32_t demux_id, int32_t filter_num, uint8_t *buffer, uint32_t len)
2868{
2869 EMM_PACKET epg;
2870
2871 struct s_emm_filter *filter = get_emmfilter_by_filternum(demux_id, filter_num + 1); // 0 is used for pending emmfilters, so everything increase 1
2872 if(!filter)
2873 {
2874 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d no filter matches -> SKIP!", demux_id, filter_num + 1);
2875 return;
2876 }
2877
2878 uint32_t provider = filter->provid;
2879 uint16_t caid = filter->caid;
2880 struct s_dvbapi_priority *mapentry = dvbapi_check_prio_match_emmpid(filter->demux_id, filter->caid, filter->provid, 'm');
2881 if(mapentry)
2882 {
2883 cs_log_dbg(D_DVBAPI, "Demuxer %d mapping EMM from %04X@%06X to %04X@%06X",
2884 demux_id, caid, provider, mapentry->mapcaid, mapentry->mapprovid);
2885
2886 caid = mapentry->mapcaid;
2887 provider = mapentry->mapprovid;
2888 }
2889
2890 memset(&epg, 0, sizeof(epg));
2891 i2b_buf(2, caid, epg.caid);
2892 i2b_buf(4, provider, epg.provid);
2893 epg.emmlen = len > sizeof(epg.emm) ? sizeof(epg.emm) : len;
2894 memcpy(epg.emm, buffer, epg.emmlen);
2895
2896 if(config_enabled(READER_IRDETO) && chk_is_betatunnel_caid(caid) == 2)
2897 {
2898 uint16_t ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_id].program_number);
2899 if(caid != ncaid)
2900 {
2901 irdeto_add_emm_header(&epg);
2902 i2b_buf(2, ncaid, epg.caid);
2903 }
2904 }
2905 do_emm(dvbapi_client, &epg);
2906}
2907
2908void dvbapi_read_priority(void)
2909{
2910 FILE *fp;
2911 char token[128], str1[128];
2912 char type;
2913 int32_t i, ret, count = 0;
2914 const char *cs_prio = "oscam.dvbapi";
2915
2916 fp = fopen(get_config_filename(token, sizeof(token), cs_prio), "r");
2917 if(!fp)
2918 {
2919 cs_log_dbg(D_DVBAPI, "ERROR: Can't open priority file %s", token);
2920 return;
2921 }
2922
2923 if(dvbapi_priority)
2924 {
2925 cs_log_dbg(D_DVBAPI, "reread priority file %s", cs_prio);
2926 struct s_dvbapi_priority *o, *p;
2927 for(p = dvbapi_priority; p != NULL; p = o)
2928 {
2929 o = p->next;
2930 NULLFREE(p);
2931 }
2932 dvbapi_priority = NULL;
2933 }
2934
2935 while(fgets(token, sizeof(token), fp))
2936 {
2937 // Ignore comments and empty lines
2938 if(token[0] == '#' || token[0] == '/' || token[0] == '\n' || token[0] == '\r' || token[0] == '\0' || cs_strlen(token) > 100)
2939 {
2940 continue;
2941 }
2942 memset(str1, 0, 128);
2943
2944 for(i = 0; i < (int)cs_strlen(token) && token[i] == ' '; i++) { ; }
2945
2946 if(i == (int)cs_strlen(token) - 1) // empty line or all spaces
2947 {
2948 continue;
2949 }
2950
2951 for(i = 0; i < (int)cs_strlen(token); i++)
2952 {
2953 if(token[i] == '@')
2954 {
2955 token[i] = ':';
2956 }
2957 }
2958
2959 for(i = 0; i < (int)cs_strlen(token); i++)
2960 {
2961 if((token[i] == ':' || token[i] == ' ') && token[i + 1] == ':') // if "::" or " :"
2962 {
2963 memmove(token + i + 2, token + i + 1, cs_strlen(token) - i + 1); // insert extra position
2964 token[i + 1] = '0'; // and fill it with NULL
2965 }
2966
2967 if(token[i] == '#' || token[i] == '/')
2968 {
2969 token[i] = '\0';
2970 break;
2971 }
2972 }
2973
2974 type = 0;
2975#if defined(WITH_STAPI) || defined(WITH_STAPI5)
2976 uint32_t disablefilter = 0;
2977 ret = sscanf(trim(token), "%c: %63s %63s %d", &type, str1, str1 + 64, &disablefilter);
2978#else
2979 ret = sscanf(trim(token), "%c: %63s %63s", &type, str1, str1 + 64);
2980#endif
2981 type = tolower((uint8_t)type);
2982
2983 if(ret < 1 || (type != 'p' && type != 'i' && type != 'm' && type != 'd' &&
2984 type != 's' && type != 'l' && type != 'j' && type != 'a' && type != 'x'))
2985 {
2986 //fprintf(stderr, "Warning: line containing %s in %s not recognized, ignoring line\n", token, cs_prio);
2987 // fprintf would issue the warning to the command line, which is more consistent with other config warnings
2988 // however it takes OSCam a long time (>4 seconds) to reach this part of the program, so the warnings are
2989 // reaching tty rather late which leads to confusion. So send the warnings to log file instead
2990 cs_log_dbg(D_DVBAPI, "WARN: line containing %s in %s not recognized, ignoring...", token, cs_prio);
2991 continue;
2992 }
2993
2994 struct s_dvbapi_priority *entry;
2995 if(!cs_malloc(&entry, sizeof(struct s_dvbapi_priority)))
2996 {
2997 ret = fclose(fp);
2998 if(ret < 0)
2999 {
3000 cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno));
3001 }
3002 return;
3003 }
3004
3005 entry->type = type;
3006 entry->next = NULL;
3007 count++;
3008
3009#if defined(WITH_STAPI) || defined(WITH_STAPI5)
3010 if(type == 's')
3011 {
3012 cs_strncpy(entry->devname, str1, sizeof(entry->devname));
3013 cs_strncpy(entry->pmtfile, str1 + 64, sizeof(entry->pmtfile));
3014 entry->disablefilter = disablefilter;
3015 cs_log_dbg(D_DVBAPI, "stapi prio: ret=%d | %c: %s %s | disable %d",
3016 ret, type, entry->devname, entry->pmtfile, disablefilter);
3017
3018 if(!dvbapi_priority)
3019 {
3020 dvbapi_priority = entry;
3021 }
3022 else
3023 {
3024 struct s_dvbapi_priority *p;
3025 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
3026 p->next = entry;
3027 }
3028 continue;
3029 }
3030#endif
3031 char c_srvid[34];
3032 c_srvid[0] = '\0';
3033 uint32_t caid = 0, provid = 0, srvid = 0, ecmpid = 0, cadata = 0;;
3034 uint32_t chid = 0x10000; //chid=0 is a valid chid
3035
3036 ret = sscanf(str1, "%4x:%6x:%33[^:]:%4x:%4x:%8x"SCNx16, &caid, &provid, c_srvid, &ecmpid, &chid, &cadata);
3037 if(ret < 1)
3038 {
3039 cs_log("Error in oscam.dvbapi: ret=%d | %c: %04X %06X %s %04X %04X",
3040 ret, type, caid, provid, c_srvid, ecmpid, chid);
3041 continue; // skip this entry!
3042 }
3043 else
3044 {
3045 cs_log_dbg(D_DVBAPI, "Parsing rule: ret=%d | %c: %04X %06X %s %04X %04X %04X",
3046 ret, type, caid, provid, c_srvid, ecmpid, chid, cadata);
3047 }
3048
3049 entry->caid = caid;
3050 entry->provid = provid;
3051 entry->ecmpid = ecmpid;
3052 entry->chid = chid;
3053 entry->cadata = cadata;
3054 uint32_t delay = 0, force = 0, mapcaid = 0, mapprovid = 0, mapecmpid = 0, pidx = 0;
3055
3056 switch(type)
3057 {
3058 case 'i':
3059 ret = sscanf(str1 + 64, "%1d", &pidx);
3060 entry->pidx = pidx + 1;
3061 if(ret < 1) entry->pidx = 0;
3062 break;
3063
3064 case 'd':
3065 sscanf(str1 + 64, "%4d", &delay);
3066 entry->delay = delay;
3067 break;
3068
3069 case 'l':
3070 entry->delay = dyn_word_atob(str1 + 64);
3071 if(entry->delay == -1) { entry->delay = 0; }
3072 break;
3073
3074 case 'p':
3075 ret = sscanf(str1 + 64, "%1d:%1d", &force, &pidx);
3076 entry->force = force;
3077 entry->pidx = pidx + 1;
3078 if(ret < 2) entry->pidx = 0;
3079 break;
3080
3081 case 'm':
3082 sscanf(str1 + 64, "%4x:%6x", &mapcaid, &mapprovid);
3083 if(!mapcaid) { mapcaid = 0xFFFF; }
3084 entry->mapcaid = mapcaid;
3085 entry->mapprovid = mapprovid;
3086 break;
3087
3088 case 'a':
3089 case 'j':
3090 sscanf(str1 + 64, "%4x:%6x:%4x", &mapcaid, &mapprovid, &mapecmpid);
3091 if(!mapcaid) { mapcaid = 0xFFFF; }
3092 entry->mapcaid = mapcaid;
3093 entry->mapprovid = mapprovid;
3094 entry->mapecmpid = mapecmpid;
3095 break;
3096 }
3097
3098 if(c_srvid[0] == '=')
3099 {
3100 struct s_srvid *this;
3101 for(i = 0; i < 16; i++)
3102 {
3103 for(this = cfg.srvid[i]; this != NULL; this = this->next)
3104 {
3105 if(this->name && strcmp(this->name, c_srvid + 1) == 0)
3106 {
3107 struct s_dvbapi_priority *entry2;
3108 if(!cs_malloc(&entry2, sizeof(struct s_dvbapi_priority)))
3109 {
3110 continue;
3111 }
3112
3113 memcpy(entry2, entry, sizeof(struct s_dvbapi_priority));
3114 entry2->srvid = this->srvid;
3115 cs_log_dbg(D_DVBAPI, "prio srvid: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
3116 ret,
3117 entry2->type,
3118 entry2->caid,
3119 entry2->provid,
3120 entry2->srvid,
3121 entry2->ecmpid,
3122 entry2->chid,
3123 entry2->mapcaid,
3124 entry2->mapprovid,
3125 entry2->mapecmpid,
3126 entry2->force,
3127 entry2->delay);
3128
3129 if(!dvbapi_priority)
3130 {
3131 dvbapi_priority = entry2;
3132 }
3133 else
3134 {
3135 struct s_dvbapi_priority *p;
3136 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
3137 p->next = entry2;
3138 }
3139 }
3140 }
3141 }
3142 NULLFREE(entry);
3143 continue;
3144 }
3145 else
3146 {
3147 sscanf(c_srvid, "%4x", &srvid);
3148 entry->srvid = srvid;
3149 }
3150 cs_log_dbg(D_DVBAPI, "prio: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
3151 ret,
3152 entry->type,
3153 entry->caid,
3154 entry->provid,
3155 entry->srvid,
3156 entry->ecmpid,
3157 entry->chid,
3158 entry->mapcaid,
3159 entry->mapprovid,
3160 entry->mapecmpid,
3161 entry->force,
3162 entry->delay);
3163
3164 if(!dvbapi_priority)
3165 {
3166 dvbapi_priority = entry;
3167 }
3168 else
3169 {
3170 struct s_dvbapi_priority *p;
3171 for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
3172 p->next = entry;
3173 }
3174 }
3175 cs_log_dbg(D_DVBAPI, "Read %d entries from %s", count, cs_prio);
3176
3177 ret = fclose(fp);
3178 if(ret < 0)
3179 {
3180 cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno));
3181 }
3182 return;
3183}
3184
3185void dvbapi_resort_ecmpids(int32_t demux_id)
3186{
3187 int32_t n, cache = 0, matching_done = 0, found = -1, match_reader_count = 0, total_reader = 0;
3188 uint16_t btun_caid = 0;
3189 struct timeb start,end;
3190 cs_ftime(&start);
3191
3192 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3193 {
3194 demux[demux_id].ECMpids[n].status = 0;
3195 demux[demux_id].ECMpids[n].checked = 0;
3196 demux[demux_id].ECMpids[n].irdeto_curindex = 0xFE;
3197 demux[demux_id].ECMpids[n].irdeto_maxindex = 0;
3198 demux[demux_id].ECMpids[n].irdeto_cycle = 0xFE;
3199 demux[demux_id].ECMpids[n].tries = 0xFE;
3200 demux[demux_id].ECMpids[n].table = 0;
3201 }
3202
3203 demux[demux_id].max_status = 0;
3204 demux[demux_id].curindex = -1;
3205 demux[demux_id].pidindex = -1;
3206
3207 struct s_reader *rdr;
3208 int32_t p_order = demux[demux_id].ECMpidcount + 1;
3209 struct s_dvbapi_priority *prio;
3210
3211 // handle prio order in oscam.dvbapi + ignore all chids
3212 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
3213 {
3214 total_reader++; // only need to calculate once!
3215 }
3216
3217 ECM_REQUEST *er;
3218 if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
3219 {
3220 return;
3221 }
3222
3223 for(prio = dvbapi_priority; prio != NULL; prio = prio->next)
3224 {
3225 if(prio->type != 'p' && prio->type != 'i')
3226 {
3227 continue;
3228 }
3229
3230 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3231 {
3232 if(demux[demux_id].ECMpids[n].status == -1) continue; // skip ignores!
3233
3234 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3235 er->prid = demux[demux_id].ECMpids[n].PROVID;
3236 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3237 er->srvid = demux[demux_id].program_number;
3238 er->client = cur_client();
3239 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3240
3241 if(prio->type == 'p' && btun_caid)
3242 {
3243 er->caid = btun_caid;
3244 }
3245
3246 if((prio->caid && (prio->caid != er->caid && prio->caid != er->ocaid))
3247 || (prio->provid && prio->provid != er->prid)
3248 || (prio->srvid && prio->srvid != er->srvid)
3249 || (prio->ecmpid && prio->ecmpid != er->pid)
3250 || (prio->pidx && prio->pidx - 1 != n))
3251 {
3252 continue;
3253 }
3254
3255 if(prio->type == 'p') // check for prio
3256 {
3257 if(prio->cadata != 0 && prio->cadata != demux[demux_id].ECMpids[n].cadata)
3258 {
3259 continue;
3260 }
3261
3262 if(prio->chid < 0x10000)
3263 {
3264 demux[demux_id].ECMpids[n].CHID = prio->chid;
3265 }
3266
3267 if(prio->force)
3268 {
3269 int32_t j;
3270 for(j = 0; j < demux[demux_id].ECMpidcount; j++)
3271 {
3272 demux[demux_id].ECMpids[j].status = -1;
3273 }
3274
3275 demux[demux_id].ECMpids[n].status = 1;
3276 demux[demux_id].ECMpids[n].checked = 0;
3277 demux[demux_id].max_status = 1;
3278 demux[demux_id].max_emm_filter = maxfilter - 1;
3279
3280 cs_log_dbg(D_DVBAPI, "Demuxer %d prio forced%s ecmpid %d %04X@%06X:%04X:%04X (file)",
3281 demux_id,
3282 ((prio->caid == er->caid && prio->caid != er->ocaid) ? " betatunneled" : ""),
3283 n,
3284 demux[demux_id].ECMpids[n].CAID,
3285 demux[demux_id].ECMpids[n].PROVID,
3286 demux[demux_id].ECMpids[n].ECM_PID,
3287 (uint16_t) prio->chid);
3288 NULLFREE(er);
3289 return; // go start descrambling since its forced by user!
3290 }
3291 else
3292 {
3293 if(!demux[demux_id].ECMpids[n].status) // only accept first matching prio from oscam.dvbapi
3294 {
3295 demux[demux_id].ECMpids[n].status = total_reader + p_order--;
3296 matching_done = 1;
3297 cs_log_dbg(D_DVBAPI, "Demuxer %d prio%s ecmpid %d %04X@%06X:%04X:%04X weight: %d (file)",
3298 demux_id,
3299 ((prio->caid == er->caid && prio->caid != er->ocaid) ? " betatunneled" : ""),
3300 n,
3301 demux[demux_id].ECMpids[n].CAID,
3302 demux[demux_id].ECMpids[n].PROVID,
3303 demux[demux_id].ECMpids[n].ECM_PID,
3304 (uint16_t) prio->chid,
3305 demux[demux_id].ECMpids[n].status);
3306 }
3307 continue; // evaluate next ecmpid
3308 }
3309 }
3310
3311 // check for ignore all chids
3312 if(prio->type == 'i' && prio->chid == 0x10000 && demux[demux_id].ECMpids[n].status == 0)
3313 {
3314 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X all chids (file)",
3315 demux_id,
3316 n,
3317 demux[demux_id].ECMpids[n].CAID,
3318 demux[demux_id].ECMpids[n].PROVID,
3319 demux[demux_id].ECMpids[n].ECM_PID);
3320 demux[demux_id].ECMpids[n].status = -1;
3321 continue; // evaluate next ecmpid
3322 }
3323 }
3324 }
3325
3326 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3327 {
3328 if(demux[demux_id].ECMpids[n].status == -1)
3329 {
3330 continue; // skip ignores!
3331 }
3332
3333 int32_t nr;
3334 SIDTAB *sidtab;
3335
3336 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3337 er->prid = demux[demux_id].ECMpids[n].PROVID;
3338 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3339 er->srvid = demux[demux_id].program_number;
3340 er->client = cur_client();
3341
3342 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3343 if(btun_caid)
3344 {
3345 er->caid = btun_caid;
3346 }
3347
3348 match_reader_count = 0;
3349
3350 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
3351 {
3352 if(matching_reader(er, rdr))
3353 {
3354 match_reader_count++;
3355 }
3356 }
3357
3358 if(match_reader_count == 0)
3359 {
3360 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X:%04X (no matching reader)",
3361 demux_id,
3362 n,
3363 demux[demux_id].ECMpids[n].CAID,
3364 demux[demux_id].ECMpids[n].PROVID,
3365 demux[demux_id].ECMpids[n].ECM_PID,
3366 demux[demux_id].ECMpids[n].CHID);
3367 demux[demux_id].ECMpids[n].status = -1;
3368 continue; // evaluate next ecmpid
3369 }
3370 else // ecmpids with no matching readers are disabled and matching sidtabbits have now highest status
3371 {
3372 for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
3373 {
3374 if(sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid)
3375 {
3376 if((cfg.dvbapi_sidtabs.no & ((SIDTABBITS)1 << nr)) && (chk_srvid_match(er, sidtab)))
3377 {
3378 demux[demux_id].ECMpids[n].status = -1; //ignore
3379 cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X (service %s pos %d)",
3380 demux_id,
3381 n,
3382 demux[demux_id].ECMpids[n].CAID,
3383 demux[demux_id].ECMpids[n].PROVID,
3384 demux[demux_id].ECMpids[n].ECM_PID,
3385 sidtab->label,
3386 nr);
3387 continue; // evaluate next ecmpid
3388 }
3389 if((cfg.dvbapi_sidtabs.ok & ((SIDTABBITS) 1 << nr)) && (chk_srvid_match(er, sidtab)))
3390 {
3391 demux[demux_id].ECMpids[n].status++; // priority
3392 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (service %s pos %d)",
3393 demux_id,
3394 n,
3395 demux[demux_id].ECMpids[n].CAID,
3396 demux[demux_id].ECMpids[n].PROVID,
3397 demux[demux_id].ECMpids[n].ECM_PID,
3398 demux[demux_id].ECMpids[n].status,
3399 sidtab->label,
3400 nr);
3401 }
3402 }
3403 }
3404 }
3405 }
3406
3407 // ecmpid with highest prio from oscam.dvbapi has now highest status
3408 // check all ecmpids and get the highest amount cache-ex and local readers
3409 int32_t max_local_matching_reader = 0, max_cacheex_reader = 0;
3410
3411 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3412 {
3413 int32_t count_matching_cacheex_reader = 0, count_matching_local_reader = 0;
3414 if(demux[demux_id].ECMpids[n].status == -1)
3415 {
3416 continue; // skip ignores!
3417 }
3418
3419 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3420 er->prid = demux[demux_id].ECMpids[n].PROVID;
3421 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3422 er->srvid = demux[demux_id].program_number;
3423 er->client = cur_client();
3424
3425 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3426 if(btun_caid)
3427 {
3428 er->caid = btun_caid;
3429 }
3430
3431 for(rdr = first_active_reader; rdr ; rdr = rdr->next)
3432 {
3433 if(matching_reader(er, rdr))
3434 {
3435 if(cacheex_reader(rdr))
3436 {
3437 count_matching_cacheex_reader++;
3438 }
3439 else if(is_localreader(rdr, er))
3440 {
3441 count_matching_local_reader++;
3442 }
3443 }
3444 }
3445
3446 if(max_local_matching_reader < count_matching_local_reader)
3447 {
3448 max_local_matching_reader = count_matching_local_reader;
3449 }
3450
3451 if(max_cacheex_reader < count_matching_cacheex_reader)
3452 {
3453 max_cacheex_reader = count_matching_cacheex_reader;
3454 }
3455 }
3456
3457 if(max_local_matching_reader != 0 || max_cacheex_reader != 0)
3458 {
3459 p_order = demux[demux_id].ECMpidcount * 2;
3460 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3461 {
3462 int32_t count_matching_cacheex_reader = 0, count_matching_local_reader = 0;
3463 int32_t localprio = 1, cacheexprio = 1;
3464
3465 if(demux[demux_id].ECMpids[n].status == -1)
3466 {
3467 continue; // skip ignores!
3468 }
3469
3470 if(cfg.preferlocalcards == 2) // ecmpids with local reader get highest prio
3471 {
3472 localprio = max_cacheex_reader + p_order + 1;
3473 }
3474 else if(cfg.preferlocalcards == 1) // ecmpids with cacheex reader get highest prio
3475 {
3476 cacheexprio = max_local_matching_reader + p_order + 1;
3477 }
3478
3479 er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
3480 er->prid = demux[demux_id].ECMpids[n].PROVID;
3481 er->pid = demux[demux_id].ECMpids[n].ECM_PID;
3482 er->srvid = demux[demux_id].program_number;
3483 er->client = cur_client();
3484
3485 btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
3486 if(btun_caid)
3487 {
3488 er->caid = btun_caid;
3489 }
3490
3491 int32_t oldstatus = demux[demux_id].ECMpids[n].status;
3492 int32_t anyreader = 0;
3493
3494 for(rdr = first_active_reader; rdr; rdr = rdr->next)
3495 {
3496 if(matching_reader(er, rdr))
3497 {
3498 if(cfg.preferlocalcards == 0)
3499 {
3500 if(!matching_done)
3501 {
3502 demux[demux_id].ECMpids[n].status++;
3503 }
3504 anyreader++;
3505 continue;
3506 }
3507
3508 if(cacheex_reader(rdr))
3509 {
3510 demux[demux_id].ECMpids[n].status += cacheexprio;
3511 count_matching_cacheex_reader++;
3512 cacheexprio = 1;
3513 }
3514
3515 if(is_localreader(rdr, er))
3516 {
3517 demux[demux_id].ECMpids[n].status += localprio;
3518 count_matching_local_reader++;
3519 localprio = 1;
3520 }
3521 }
3522 }
3523
3524 if(oldstatus != demux[demux_id].ECMpids[n].status)
3525 {
3526 if(anyreader)
3527 {
3528 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X:%04X weight: %d (%d readers)",
3529 demux_id,
3530 n,
3531 demux[demux_id].ECMpids[n].CAID,
3532 demux[demux_id].ECMpids[n].PROVID,
3533 demux[demux_id].ECMpids[n].ECM_PID,
3534 demux[demux_id].ECMpids[n].CHID,
3535 demux[demux_id].ECMpids[n].status,
3536 anyreader);
3537 }
3538 else
3539 {
3540 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X:%04X weight: %d (%d local and %d cacheex readers)",
3541 demux_id,
3542 n,
3543 demux[demux_id].ECMpids[n].CAID,
3544 demux[demux_id].ECMpids[n].PROVID,
3545 demux[demux_id].ECMpids[n].ECM_PID,
3546 demux[demux_id].ECMpids[n].CHID,
3547 demux[demux_id].ECMpids[n].status,
3548 count_matching_local_reader,
3549 count_matching_cacheex_reader);
3550 }
3551 }
3552 }
3553 }
3554
3555 struct s_channel_cache *c = NULL;
3556
3557 for(n = 0; n < demux[demux_id].ECMpidcount && matching_done == 0; n++)
3558 {
3559 if(demux[demux_id].ECMpids[n].status == -1)
3560 {
3561 continue; // skip ignores!
3562 }
3563
3564 c = dvbapi_find_channel_cache(demux_id, n, 0); // find exact channel match
3565 if(c != NULL)
3566 {
3567 found = n;
3568 cache = 2; // found cache entry with higher priority
3569 demux[demux_id].ECMpids[n].status++; // prioritize CAIDs which already decoded same caid:provid:srvid
3570
3571 if(c->chid < 0x10000)
3572 {
3573 demux[demux_id].ECMpids[n].CHID = c->chid; // if chid registered in cache -> use it!
3574 }
3575 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (found caid/provid/srvid in cache)",
3576 demux_id,
3577 n,
3578 demux[demux_id].ECMpids[n].CAID,
3579 demux[demux_id].ECMpids[n].PROVID,
3580 demux[demux_id].ECMpids[n].ECM_PID,
3581 demux[demux_id].ECMpids[n].status);
3582 break;
3583 }
3584 }
3585
3586 if(found == -1)
3587 {
3588 // prioritize CAIDs which already decoded same caid:provid
3589 for(n = 0; n < demux[demux_id].ECMpidcount && matching_done == 0; n++)
3590 {
3591 if(demux[demux_id].ECMpids[n].status == -1) continue; // skip ignores!
3592
3593 c = dvbapi_find_channel_cache(demux_id, n, 1);
3594 if(c != NULL)
3595 {
3596 cache = 1; //found cache entry
3597 demux[demux_id].ECMpids[n].status++;
3598 cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (found caid/provid in cache)",
3599 demux_id,
3600 n,
3601 demux[demux_id].ECMpids[n].CAID,
3602 demux[demux_id].ECMpids[n].PROVID,
3603 demux[demux_id].ECMpids[n].ECM_PID,
3604 demux[demux_id].ECMpids[n].status);
3605 }
3606 }
3607 }
3608
3609 int32_t max_status = 0;
3610 int32_t highest_priopid = -1;
3611
3612 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3613 {
3614 if(demux[demux_id].ECMpids[n].status == -1)
3615 {
3616 continue; // skip ignores!
3617 }
3618
3619 if(demux[demux_id].ECMpids[n].status > max_status) // find highest prio pid
3620 {
3621 max_status = demux[demux_id].ECMpids[n].status;
3622 highest_priopid = n;
3623 }
3624
3625 if(!USE_OPENXCAS) // openxcas doesnt use prio and non-prio run: all are equal!
3626 {
3627 if(demux[demux_id].ECMpids[n].status == 0)
3628 {
3629 demux[demux_id].ECMpids[n].checked = 2; // set pids with no status to no prio run
3630 }
3631 }
3632 }
3633
3634 demux[demux_id].max_status = max_status; // register maxstatus
3635
3636 // Found entry in channelcache that is valid and has exact match on srvid
3637 if(highest_priopid != -1 && found == highest_priopid && cache == 2)
3638 {
3639 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
3640 {
3641 if(demux[demux_id].ECMpids[n].status == -1)
3642 {
3643 continue; // skip ignores!
3644 }
3645
3646 if(n != found)
3647 {
3648 // disable non matching pid
3649 demux[demux_id].ECMpids[n].status = -1;
3650 }
3651 else
3652 {
3653 demux[demux_id].ECMpids[n].status = 1;
3654 }
3655 }
3656 demux[demux_id].max_emm_filter = maxfilter - 1;
3657 demux[demux_id].max_status = 1;
3658 cs_log("Demuxer %d found channel in cache and matching prio -> start descrambling ecmpid %d ", demux_id, found);
3659 }
3660
3661 NULLFREE(er);
3662 cs_ftime(&end);
3663 int64_t gone = comp_timeb(&end, &start);
3664 cs_log_dbg(D_DVBAPI, "Demuxer %d sorting the ecmpids took %"PRId64" ms", demux_id, gone);
3665 return;
3666}
3667
3668static void dvbapi_priority_read_entry_add(int32_t demux_id, uint16_t video_pid)
3669{
3670 struct s_dvbapi_priority *add_entry;
3671
3672 for(add_entry = dvbapi_priority; add_entry != NULL; add_entry = add_entry->next)
3673 {
3674 // ECM pid is misused to hold PMT pid in case of 'A' rule.
3675 // Some receivers don't forward the PMT pid, so use the video pid instead
3676 if(add_entry->type != 'a' || add_entry->srvid != demux[demux_id].program_number
3677 || (add_entry->ecmpid && demux[demux_id].pmtpid && add_entry->ecmpid != demux[demux_id].pmtpid)
3678 || (add_entry->ecmpid && !demux[demux_id].pmtpid && add_entry->ecmpid != video_pid))
3679 {
3680 continue;
3681 }
3682
3683 dvbapi_add_ecmpid(demux_id, add_entry->mapcaid, add_entry->mapecmpid, add_entry->mapprovid, 0, "(fake ecm pid)");
3684
3685 cs_log_dbg(D_DVBAPI, "Demuxer %d added fake ecm pid %04X@%06X:%04X for program %04X", demux_id,
3686 add_entry->mapcaid, add_entry->mapprovid, add_entry->mapecmpid, demux[demux_id].program_number);
3687 break;
3688 }
3689}
3690
3691static void dvbapi_priority_read_entry_map(int32_t demux_id)
3692{
3693 int32_t j;
3694 struct s_dvbapi_priority *map_entry;
3695
3696 for(j = 0; j < demux[demux_id].ECMpidcount; j++)
3697 {
3698 map_entry = dvbapi_check_prio_match(demux_id, j, 'm');
3699 if(map_entry)
3700 {
3701 cs_log_dbg(D_DVBAPI, "Demuxer %d mapping ecm pid %d from %04X@%06X to %04X@%06X",
3702 demux_id, j, demux[demux_id].ECMpids[j].CAID, demux[demux_id].ECMpids[j].PROVID,
3703 map_entry->mapcaid, map_entry->mapprovid);
3704
3705 demux[demux_id].ECMpids[j].CAID = map_entry->mapcaid;
3706 demux[demux_id].ECMpids[j].PROVID = map_entry->mapprovid;
3707 }
3708 }
3709}
3710
3711static void dvbapi_priority_read_entry_extra(int32_t demux_id)
3712{
3713 struct s_dvbapi_priority *extra_entry;
3714 int32_t j, k, l, m, extra_demux_id;
3715
3716 for(extra_entry = dvbapi_priority; extra_entry != NULL; extra_entry = extra_entry->next)
3717 {
3718 if(extra_entry->type != 'x')
3719 {
3720 continue;
3721 }
3722
3723 for(j = 0; j <= demux[demux_id].ECMpidcount; ++j)
3724 {
3725 if((extra_entry->caid && extra_entry->caid != demux[demux_id].ECMpids[j].CAID)
3726 || (extra_entry->provid && extra_entry->provid != demux[demux_id].ECMpids[j].PROVID)
3727 || (extra_entry->ecmpid && extra_entry->ecmpid != demux[demux_id].ECMpids[j].ECM_PID)
3728 || (extra_entry->srvid && extra_entry->srvid != demux[demux_id].program_number))
3729 {
3730 continue;
3731 }
3732
3733 cs_log("Mapping ecm pid %04X@%06X:%04X:%04X to extra demuxer",
3734 extra_entry->caid, extra_entry->provid, extra_entry->ecmpid, extra_entry->srvid);
3735
3736 for(extra_demux_id = 0; extra_demux_id < MAX_DEMUX; extra_demux_id++)
3737 {
3738 if(demux[extra_demux_id].program_number != 0)
3739 {
3740 continue; // Skip occupied demuxers
3741 }
3742 }
3743
3744 if(extra_demux_id >= MAX_DEMUX)
3745 {
3746 cs_log("There is no free demuxer for extra streams");
3747 continue;
3748 }
3749
3750 demux[extra_demux_id].ECMpids[0] = demux[demux_id].ECMpids[j];
3751 demux[extra_demux_id].ECMpidcount = 1;
3752 demux[extra_demux_id].STREAMpidcount = 0;
3753 demux[extra_demux_id].program_number = demux[demux_id].program_number;
3754 demux[extra_demux_id].pmtpid = demux[demux_id].pmtpid;
3755 demux[extra_demux_id].demux_index = demux[demux_id].demux_index;
3756 demux[extra_demux_id].adapter_index = demux[demux_id].adapter_index;
3757 demux[extra_demux_id].ca_mask = demux[demux_id].ca_mask;
3758 demux[extra_demux_id].socket_fd = demux[demux_id].socket_fd;
3759 demux[extra_demux_id].stop_descrambling = false;
3760 demux[extra_demux_id].rdr = NULL;
3761 demux[extra_demux_id].curindex = -1;
3762
3763 // Add streams to extra demux
3764 for(k = 0; k < demux[demux_id].STREAMpidcount; ++k)
3765 {
3766 if(!demux[demux_id].ECMpids[j].streams || (demux[demux_id].ECMpids[j].streams & (1 << k)))
3767 {
3768 demux[extra_demux_id].ECMpids[0].streams |= (1 << demux[extra_demux_id].STREAMpidcount);
3769 demux[extra_demux_id].STREAMpids[demux[extra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpids[k];
3770 demux[extra_demux_id].STREAMpidsType[demux[extra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpidsType[k];
3771 ++demux[extra_demux_id].STREAMpidcount;
3772
3773 // Shift stream associations in normal demux because we will remove the stream entirely
3774 for(l = 0; l < demux[demux_id].ECMpidcount; ++l)
3775 {
3776 for(m = k; m < demux[demux_id].STREAMpidcount - 1; ++m)
3777 {
3778 if(demux[demux_id].ECMpids[l].streams & (1 << (m + 1)))
3779 {
3780 demux[demux_id].ECMpids[l].streams |= (1 << m);
3781 }
3782 else
3783 {
3784 demux[demux_id].ECMpids[l].streams &= ~(1 << m);
3785 }
3786 }
3787 }
3788
3789 // Remove stream association from normal demux device
3790 for(l = k; l < demux[demux_id].STREAMpidcount - 1; ++l)
3791 {
3792 demux[demux_id].STREAMpids[l] = demux[demux_id].STREAMpids[l + 1];
3793 demux[demux_id].STREAMpidsType[l] = demux[demux_id].STREAMpidsType[l + 1];
3794 }
3795
3796 --demux[demux_id].STREAMpidcount;
3797 --k;
3798 }
3799 }
3800
3801 // Remove ecm pid from normal demuxer
3802 for(k = j; k < demux[demux_id].ECMpidcount; ++k)
3803 {
3804 demux[demux_id].ECMpids[k] = demux[demux_id].ECMpids[k + 1];
3805 }
3806
3807 --demux[demux_id].ECMpidcount;
3808 --j;
3809
3810 if(demux[extra_demux_id].STREAMpidcount <= 0)
3811 {
3812 cs_log("Found no streams for extra demuxer. Not starting additional decoding on it.");
3813
3814 demux[extra_demux_id].program_number = 0;
3815 demux[extra_demux_id].stop_descrambling = true;
3816 }
3817
3818 if(demux[demux_id].STREAMpidcount < 1)
3819 {
3820 cs_log("Found no streams for normal demuxer. Not starting additional decoding on it.");
3821 }
3822 }
3823 }
3824}
3825
3826static void dvbapi_parse_pmt_ca_descriptor(int32_t demux_id, const uint8_t *buffer, uint8_t descriptor_length)
3827{
3828 uint16_t i, ca_system_id, ca_pid;
3829 uint32_t ca_provider = 0, ca_data = 0;
3830 char txt[40]; // room for PBM: 8 byte pbm and DATE: date
3831 memset(txt, 0x00, sizeof(txt));
3832
3833 if(descriptor_length < 4)
3834 {
3835 return; // CA descriptor has a minimum length of 4 bytes
3836 }
3837
3838 ca_system_id = b2i(2, buffer);
3839 ca_pid = b2i(2, buffer + 2) & 0x1FFF;
3840
3841 if(caid_is_seca(ca_system_id))
3842 {
3843 for(i = 2; i < descriptor_length; i += 15)
3844 {
3845 ca_pid = b2i(2, buffer + i) & 0x1FFF;
3846 ca_provider = b2i(2, buffer + i + 2);
3847
3848 int8_t year = buffer[i + 13] >> 1;
3849 int8_t month = (((buffer[i + 13] & 0x01) << 3) | (buffer[i + 14] >> 5));
3850 int8_t day = buffer[i + 14] & 0x1F;
3851
3852 snprintf(txt, sizeof(txt), "PBM: ");
3853 cs_hexdump(0, buffer + i + 5, 8, txt + 5, (2 * 8) + 1); // hexdump 8 byte pbm
3854 snprintf(txt + 20, sizeof(txt) - 20, " DATE: %d-%d-%d", day, month, year + 1990);
3855
3856 dvbapi_add_ecmpid(demux_id, ca_system_id, ca_pid, ca_provider, 0, txt);
3857 }
3858 }
3859 else
3860 {
3861 if(caid_is_viaccess(ca_system_id) && descriptor_length == 0x0F && buffer[10] == 0x14)
3862 {
3863 ca_provider = b2i(3, buffer + 12) & 0xFFFFF0;
3864 }
3865 else if(caid_is_nagra(ca_system_id) && descriptor_length == 0x07)
3866 {
3867 ca_provider = b2i(2, buffer + 5);
3868 }
3869 else if((ca_system_id >> 8 == 0x4A || ca_system_id == 0x2710) && descriptor_length > 0x04)
3870 {
3871 ca_provider = buffer[4];
3872
3873 if(caid_is_dre(ca_system_id) && descriptor_length == 0x0A)
3874 {
3875 ca_data = b2i(4, buffer + 6);
3876 snprintf(txt, 40, "CA DATA: %X", ca_data);
3877 }
3878 }
3879
3880 dvbapi_add_ecmpid(demux_id, ca_system_id, ca_pid, ca_provider, ca_data, txt);
3881 }
3882}
3883
3884static void dvbapi_parse_pmt_descriptors(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint8_t *type)
3885{
3886 uint16_t i, j;
3887 uint8_t descriptor_tag, descriptor_length;
3888
3889 for(i = 0; i + 1 < length; i += 2 + descriptor_length)
3890 {
3891 descriptor_tag = buffer[i];
3892 descriptor_length = buffer[i + 1];
3893
3894 cs_log_dbg(D_DVBAPI, "Demuxer %d found %s descriptor (tag: %02X length: %02X)",
3895 demux_id, get_descriptor_tag_txt(descriptor_tag), descriptor_tag, descriptor_length);
3896
3897 switch(descriptor_tag)
3898 {
3899 case 0x05: // Registration descriptor
3900 {
3901 // "HDMV" format identifier is removed
3902 // OSCam does not need to know about Blu-ray
3903 const char format_identifiers_audio[10][5] =
3904 {
3905 "AC-3", "BSSD", "dmat", "DRA1", "DTS1",
3906 "DTS2", "DTS3", "EAC3", "mlpa", "Opus",
3907 };
3908
3909 for(j = 0; j < 10; j++)
3910 {
3911 if(memcmp(buffer + i + 2, format_identifiers_audio[j], 4) == 0)
3912 {
3913 *type = STREAM_AUDIO;
3914 break;
3915 }
3916 }
3917 break;
3918 }
3919
3920 case 0x09: // CA descriptor
3921 {
3922 dvbapi_parse_pmt_ca_descriptor(demux_id, buffer + i + 2, descriptor_length);
3923 break;
3924 }
3925
3926 case 0x59: // Subtitling descriptor (DVB)
3927 {
3928 *type = STREAM_SUBTITLE;
3929 break;
3930 }
3931
3932 case 0x6A: // AC-3 descriptor (DVB)
3933 case 0x7A: // Enhanced AC-3 descriptor (DVB)
3934 case 0x7B: // DTS descriptor (DVB)
3935 case 0x7C: // AAC descriptor (DVB)
3936 case 0x81: // AC-3 descriptor (ATSC)
3937 case 0xCC: // Enhanced AC-3 descriptor (ATSC)
3938 {
3939 *type = STREAM_AUDIO;
3940 break;
3941 }
3942
3943 case 0x7F: // Extension descriptor (DVB)
3944 {
3945 uint8_t extension_descriptor_tag = buffer[i + 2];
3946
3947 cs_log_dbg(D_DVBAPI, "Demuxer %d found %s descriptor (extension tag: %02X)",
3948 demux_id, get_extension_descriptor_txt(extension_descriptor_tag), extension_descriptor_tag);
3949
3950 switch(extension_descriptor_tag)
3951 {
3952 case 0x0E: // DTS-HD descriptor (DVB)
3953 case 0x0F: // DTS Neural descriptor (DVB)
3954 case 0x15: // AC-4 descriptor (DVB)
3955 *type = STREAM_AUDIO;
3956 break;
3957
3958 case 0x20: // TTML subtitling descriptor (DVB)
3959 *type = STREAM_SUBTITLE;
3960 break;
3961
3962 default:
3963 *type = STREAM_UNDEFINED;
3964 break;
3965 }
3966 break;
3967 }
3968
3969 default:
3970 break;
3971 }
3972 }
3973}
3974
3975void request_cw(struct s_client *client, ECM_REQUEST *er, int32_t demux_id, uint8_t delayed_ecm_check)
3976{
3977 if(!er) { return; }
3978
3979 int32_t filternum = dvbapi_set_section_filter(demux_id, er, -1); // set ecm filter to odd -> even and visaversa
3980 if(filternum < 0)
3981 {
3982 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting cw -> ecm filter was killed!", demux_id);
3983 NULLFREE(er);
3984 return;
3985 }
3986
3987 if(!delayed_ecm_check) // no delayed ecm check for this filter
3988 {
3989 memset(demux[demux_id].demux_fd[filternum].lastecmd5, 0, CS_ECMSTORESIZE); // no ecm delay check: zero it!
3990 }
3991 else
3992 {
3993 uint8_t md5tmp[MD5_DIGEST_LENGTH];
3994 MD5(er->ecm, er->ecmlen, md5tmp);
3995
3996 if(!memcmp(demux[demux_id].demux_fd[filternum].prevecmd5, md5tmp, CS_ECMSTORESIZE))
3997 {
3998 if(demux[demux_id].demux_fd[filternum].prevresult < E_NOTFOUND)
3999 {
4000 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting same ecm again! -> SKIP!", demux_id);
4001 NULLFREE(er);
4002 return;
4003 }
4004 else
4005 {
4006 cs_log_dbg(D_DVBAPI, "Demuxer %d requesting same ecm again (previous result was not found!)", demux_id);
4007 }
4008 }
4009 else if(!memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE))
4010 {
4011 if(demux[demux_id].demux_fd[filternum].lastresult < E_NOTFOUND)
4012 {
4013 cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting same ecm again! -> SKIP!", demux_id);
4014 NULLFREE(er);
4015 return;
4016 }
4017 else
4018 {
4019 cs_log_dbg(D_DVBAPI, "Demuxer %d requesting same ecm again (previous result was not found!)", demux_id);
4020 }
4021 }
4022
4023 memcpy(demux[demux_id].demux_fd[filternum].prevecmd5, demux[demux_id].demux_fd[filternum].lastecmd5, CS_ECMSTORESIZE);
4024 demux[demux_id].demux_fd[filternum].prevresult = demux[demux_id].demux_fd[filternum].lastresult;
4025 memcpy(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE);
4026 demux[demux_id].demux_fd[filternum].lastresult = 0xFF;
4027 }
4028
4029 er->adapter_index = demux[demux_id].adapter_index;
4030 get_cw(client, er);
4031
4032#ifdef WITH_DEBUG
4033 char buf[ECM_FMT_LEN];
4034 format_ecm(er, buf, ECM_FMT_LEN);
4035 cs_log_dbg(D_DVBAPI, "Demuxer %d request controlword for ecm %s", demux_id, buf);
4036#endif
4037}
4038
4039void dvbapi_try_next_caid(int32_t demux_id, int8_t checked, uint32_t msgid)
4040{
4041 int32_t n, j, found = -1, started = 0;
4042 int32_t status = demux[demux_id].max_status;
4043
4044 for(j = status; j >= 0; j--) // largest status first!
4045 {
4046 for(n = 0; n < demux[demux_id].ECMpidcount; n++)
4047 {
4048 //cs_log_dbg(D_DVBAPI,"Demuxer %d PID %d checked = %d status = %d (searching for pid with status = %d)",
4049 // demux_id, n, demux[demux_id].ECMpids[n].checked, demux[demux_id].ECMpids[n].status, j);
4050
4051 if(demux[demux_id].ECMpids[n].checked == checked && demux[demux_id].ECMpids[n].status == j)
4052 {
4053 found = n;
4054 openxcas_set_provid(demux[demux_id].ECMpids[found].PROVID);
4055 openxcas_set_caid(demux[demux_id].ECMpids[found].CAID);
4056 openxcas_set_ecm_pid(demux[demux_id].ECMpids[found].ECM_PID);
4057
4058 // fixup for cas that need emm first!
4059 if(caid_is_irdeto(demux[demux_id].ECMpids[found].CAID) || (caid_is_dre(demux[demux_id].ECMpids[found].CAID)
4060 && ((demux[demux_id].ECMpids[found].PROVID == 0x11 || demux[demux_id].ECMpids[found].PROVID == 0xFE))))
4061 {
4062 demux[demux_id].emmstart.time = 0;
4063 }
4064
4065 started = dvbapi_start_descrambling(demux_id, found, checked, msgid);
4066 if(cfg.dvbapi_requestmode == 0 && started == 1)
4067 {
4068 return; // in requestmode 0 we only start 1 ecm request at the time
4069 }
4070 }
4071 }
4072 }
4073
4074 if(found == -1 && demux[demux_id].pidindex == -1)
4075 {
4076 cs_log("Demuxer %d no suitable readers found that can be used for decoding!", demux_id);
4077 return;
4078 }
4079}
4080
4081static void dvbapi_parse_pmt_program_info(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint8_t *ca_pmt_cmd_id)
4082{
4083 uint16_t i, offset = 0;
4084 uint8_t descriptor_tag, descriptor_length;
4085
4086 if(ca_pmt_cmd_id != NULL) // We are on CA PMT parsing
4087 {
4088 *ca_pmt_cmd_id = buffer[0];
4089 offset = 1;
4090 }
4091
4092 for(i = offset; i + 1 < length; i += 2 + descriptor_length)
4093 {
4094 descriptor_tag = buffer[i];
4095 descriptor_length = buffer[i + 1];
4096
4097 if(descriptor_tag == 0x09) // We only care about CA descriptors at program level
4098 {
4099 dvbapi_parse_pmt_ca_descriptor(demux_id, buffer + i + 2, descriptor_length);
4100 }
4101 }
4102}
4103
4104static 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)
4105{
4106 uint16_t i, elementary_pid, es_info_length, offset = 0;
4107 uint8_t stream_type, type;
4108
4109 for(i = 0; i + 4 < length; i += 5 + es_info_length)
4110 {
4111 if(demux[demux_id].STREAMpidcount >= MAX_STREAM_PIDS)
4112 {
4113 cs_log("Demuxer %d reached maximum number of elementary streams", demux_id);
4114 break;
4115 }
4116
4117 type = STREAM_UNDEFINED;
4118 stream_type = buffer[i];
4119 elementary_pid = b2i(2, buffer + i + 1) & 0x1FFF;
4120 es_info_length = b2i(2, buffer + i + 3) & 0x0FFF;
4121
4122 cs_log_dbg(D_DVBAPI,"Demuxer %d found %s stream (type: %02X pid: %04X)",
4123 demux_id, get_stream_type_txt(stream_type), stream_type, elementary_pid);
4124
4125 if(es_info_length != 0 && es_info_length < length)
4126 {
4127 if(ca_pmt_cmd_id != NULL) // We are on CA PMT parsing
4128 {
4129 // Only enigma2, Spark and VDR follow the CA PMT specification ("ca_pmt_cmd_id"
4130 // shall be present in the ES info loop). For the first two, checking for boxtype
4131 // "dreambox" is sufficient, but for VDR this is not enough, because it shares
4132 // the same boxtype with tvheadend. So, for every other box (including VDR and
4133 // tvheadend), we stick to the old style check based on the value (descriptors
4134 // with tag 0x00 or 0x01 are not allowed, so this works), while for enigma2 we
4135 // do a proper check, because the "ca_pmt_cmd_id" can also take greater values.
4136 if(cfg.dvbapi_boxtype == BOXTYPE_DREAMBOX)
4137 {
4138 *ca_pmt_cmd_id = buffer[i + 5]; // It should be identical for all ES and the same as in program info
4139 offset = 1;
4140 }
4141 else
4142 {
4143 offset = (buffer[i + 5] <= 0x01) ? 1 : 0;
4144 }
4145 }
4146
4147 // Parse descriptors at ES level
4148 dvbapi_parse_pmt_descriptors(demux_id, buffer + i + 5 + offset, es_info_length, &type);
4149 }
4150
4151 // Get basic stream type (video, audio, subtitle) for each ES pid
4152 switch(stream_type)
4153 {
4154 case 0x01:
4155 case 0x02:
4156 case 0x10:
4157 case 0x1B:
4158 case 0x20:
4159 case 0x24:
4160 case 0x25:
4161 case 0x42:
4162 case 0xD1:
4163 case 0xEA:
4164 if(*video_pid == 0)
4165 {
4166 *video_pid = elementary_pid;
4167 }
4168 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_VIDEO;
4169 break;
4170
4171 case 0x03:
4172 case 0x04:
4173 case 0x0F:
4174 case 0x11:
4175 case 0x1C:
4176 case 0x2D:
4177 case 0x2E:
4178 case 0x81:
4179 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_AUDIO;
4180 break;
4181
4182 case 0x06:
4183 //case 0x81: some ATSC AC-3 streams do not contain the AC-3 descriptor!
4184 case 0x87:
4185 // Set the type based on the descriptors for these stream types
4186 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = type;
4187 break;
4188
4189 default:
4190 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_UNDEFINED;
4191 break;
4192 }
4193
4194 demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount] = elementary_pid;
4195 demux[demux_id].STREAMpidcount++;
4196 }
4197}
4198
4199static 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)
4200{
4201 uint16_t i, program_info_length, video_pid = 0;
4202
4203 // Cleanout demuxer from possible stale info
4204 // (reset ECM pids and streams)
4205 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
4206 {
4207 demux[demux_id].ECMpids[i].streams = 0;
4208 }
4209
4210 demux[demux_id].ECMpidcount = 0;
4211 demux[demux_id].STREAMpidcount = 0;
4212
4213 // Parse program info
4214 // In case of CA PMT, read the ca_pmt_cmd_id as well
4215 program_info_length = b2i(2, buffer) & 0x0FFF;
4216 if(program_info_length != 0 && program_info_length < length)
4217 {
4218 dvbapi_parse_pmt_program_info(demux_id, buffer + 2, program_info_length, ca_pmt_cmd_id);
4219 }
4220
4221 // Parse elementary stream info
4222 // In case of CA PMT, read the ca_pmt_cmd_id for each stream as well
4223 dvbapi_parse_pmt_es_info(demux_id, buffer + 2 + program_info_length, length - 2 - program_info_length, ca_pmt_cmd_id, &video_pid);
4224
4225 cs_log("Demuxer %d found %d ECM pids and %d STREAM pids in %sPMT", demux_id,
4226 demux[demux_id].ECMpidcount, demux[demux_id].STREAMpidcount, ca_pmt_cmd_id != NULL ? "CA " : "");
4227
4228 // Various retarded boxes misuse the "ca_pmt_cmd_id" value,
4229 // usually by setting it to zero. If we are on CA PMT parsing,
4230 // make sure we pass a value we can work with later on.
4231 if(ca_pmt_cmd_id != NULL)
4232 {
4233 *ca_pmt_cmd_id = (*ca_pmt_cmd_id < CA_PMT_CMD_OK_DESCRAMBLING) ? CA_PMT_CMD_OK_DESCRAMBLING : *ca_pmt_cmd_id;
4234 }
4235
4236 // If no elementary streams are available, set the PMT pid as the
4237 // first stream (PMT cannot be encrypted, like it was mentioned
4238 // in the old comment, so not sure why this is needed...)
4239 if(demux[demux_id].STREAMpidcount == 0)
4240 {
4241 demux[demux_id].STREAMpids[0] = demux[demux_id].pmtpid;
4242 demux[demux_id].STREAMpidsType[0] = STREAM_VIDEO;
4243 demux[demux_id].STREAMpidcount++;
4244 video_pid = demux[demux_id].pmtpid;
4245 }
4246
4247 // Fix for channels not listing the video pid inside the PMT: use the pcr pid instead
4248 if(video_pid == 0 && pcr_pid != 0x1FFF)
4249 {
4250 demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount] = pcr_pid;
4251 demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_VIDEO;
4252 demux[demux_id].STREAMpidcount++;
4253 video_pid = pcr_pid;
4254
4255 cs_log("Demuxer %d found no video pid. Using the PCR pid %04X instead", demux_id, pcr_pid);
4256 }
4257
4258 // Register found video pid on all ECM pids of this demuxer
4259 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
4260 {
4261 demux[demux_id].ECMpids[i].VPID = video_pid;
4262 }
4263
4264 // Search for dvbapi priority entries for this program
4265 if(dvbapi_priority != NULL)
4266 {
4267 dvbapi_priority_read_entry_add(demux_id, video_pid);
4268 dvbapi_priority_read_entry_map(demux_id);
4269 dvbapi_priority_read_entry_extra(demux_id);
4270 }
4271}
4272
4273typedef struct demux_parameters
4274{
4275 uint8_t demux_index;
4276 uint8_t adapter_index;
4277 uint32_t ca_mask;
4278 uint16_t program_number;
4279 uint16_t pmtpid;
4280 uint16_t onid;
4281 uint16_t tsid;
4282 uint32_t ens;
4283} demux_parameters_t;
4284
4285static void get_demux_parameters(const uint8_t *buffer, demux_parameters_t *parameters)
4286{
4287 parameters->ca_mask = 1;
4288 parameters->demux_index = 0;
4289 parameters->adapter_index = 0;
4290 parameters->pmtpid = 0;
4291 parameters->program_number = b2i(2, buffer + 1);
4292
4293 uint16_t program_info_length = b2i(2, buffer + 4) & 0x0FFF;
4294 uint16_t pos = 7; // 4 + 2 (program_info_length) + 1 (ca_pmt_cmd_id)
4295
4296 while(pos + 1 < 5 + program_info_length)
4297 {
4298 uint8_t descriptor_tag = buffer[pos];
4299 uint8_t descriptor_length = buffer[pos + 1];
4300
4301 switch(descriptor_tag)
4302 {
4303 case CA:
4304 {
4305 break;
4306 }
4307
4308 case ENIGMA_NAMESPACE:
4309 {
4310 if(descriptor_length == 0x08)
4311 {
4312 parameters->ens = b2i(4, buffer + pos + 2);
4313 parameters->tsid = b2i(2, buffer + pos + 6);
4314 parameters->onid = b2i(2, buffer + pos + 8);
4315 }
4316 break;
4317 }
4318
4319 case DEMUX_CA_MASK_ADAPTER:
4320 {
4321 if(descriptor_length == 0x02 && (cfg.dvbapi_boxtype == BOXTYPE_PC ||
4322 cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO))
4323 {
4324 parameters->demux_index = buffer[pos + 2]; // Usually 0, but not always
4325 parameters->adapter_index = buffer[pos + 3]; // Can be 0, 1, 2, ...
4326 parameters->ca_mask = (1 << parameters->adapter_index); // use adapter_index as ca_mask
4327 }
4328 else if(descriptor_length == 0x03 && cfg.dvbapi_boxtype == BOXTYPE_QBOXHD)
4329 {
4330 // ca_mask = buffer[pos + 2]; // with STONE 1.0.4 always 0x01
4331 parameters->demux_index = buffer[pos + 3]; // with STONE 1.0.4 always 0x00
4332 parameters->adapter_index = buffer[pos + 4]; // with STONE 1.0.4 adapter index can be 0, 1, 2
4333 parameters->ca_mask = (1 << parameters->adapter_index); // use adapter_index as ca_mask
4334 }
4335 else if(descriptor_length == 0x02) // enigma2
4336 {
4337 parameters->ca_mask = buffer[pos + 2];
4338 uint8_t demux_tmp = buffer[pos + 3];
4339
4340 if(demux_tmp >= 8 && parameters->ca_mask == 0) // openpli based images
4341 {
4342 parameters->ca_mask = 1 << demux_tmp;
4343 }
4344
4345 if(demux_tmp == 0xFF) // tryfix prismcube (0xFF -> "demux-1" = error!)
4346 {
4347 demux_tmp = 0;
4348 }
4349 parameters->demux_index = demux_tmp;
4350 }
4351 break;
4352 }
4353
4354 case ADAPTER_DEVICE:
4355 {
4356 if(descriptor_length == 0x01)
4357 {
4358 parameters->adapter_index = buffer[pos + 2];
4359 }
4360 break;
4361 }
4362
4363 case PMT_PID:
4364 {
4365 if(descriptor_length == 0x02)
4366 {
4367 parameters->pmtpid = b2i(2, buffer + pos + 2);
4368 }
4369 break;
4370 }
4371
4372 case SERVICE_TYPE_MASK:
4373 break;
4374
4375 case DEMUX_DEVICE:
4376 {
4377 if(descriptor_length == 0x01)
4378 {
4379 parameters->demux_index = buffer[pos + 2];
4380 parameters->ca_mask = 1 << parameters->demux_index;
4381 }
4382 break;
4383 }
4384
4385 case CA_DEVICE:
4386 {
4387 if(descriptor_length == 0x01)
4388 {
4389 parameters->ca_mask = 1 << buffer[pos + 2];
4390 }
4391 break;
4392 }
4393
4394 default:
4395 {
4396 cs_log_dbg(D_DVBAPI, "Skipped unsupported or CA PMT irrelevant descriptor (tag: %02X length: %02X)", descriptor_tag, descriptor_length);
4397 break;
4398 }
4399 }
4400
4401 pos += 2 + descriptor_length;
4402 }
4403}
4404
4405static void dvbapi_capmt_notify(struct demux_s *dmx)
4406{
4407 struct s_client *cl;
4408 for(cl = first_client->next; cl; cl = cl->next)
4409 {
4410 if((cl->typ == 'p' || cl->typ == 'r') && cl->reader && cl->reader->ph.c_capmt)
4411 {
4412 struct demux_s *curdemux;
4413 if(cs_malloc(&curdemux, sizeof(struct demux_s)))
4414 {
4415 memcpy(curdemux, dmx, sizeof(struct demux_s));
4416 add_job(cl, ACTION_READER_CAPMT_NOTIFY, curdemux, sizeof(struct demux_s));
4417 }
4418 }
4419 }
4420}
4421
4422static void dvbapi_prepare_descrambling(int32_t demux_id, uint32_t msgid)
4423{
4424 bool is_powervu = false, start_emm = true;
4425 char service_name[CS_SERVICENAME_SIZE];
4426
4427 // The CA PMT should have given us enough info to determine if descrambling
4428 // is possible. Parsing the (real) PMT is not necessary, unless we have a
4429 // PowerVu encrypted channel or (for some weird reason) no stream pids at all.
4430 // Actually, when no streams are available, we set the PMT pid as the 1st
4431 // stream pid, so we have to check against that. Finally, if the PMT pid is
4432 // not included in the CA PMT, we start the PAT filter instead.
4433
4434#ifdef WITH_EXTENDED_CW
4435 uint8_t i;
4436 for(i = 0; i < demux[demux_id].ECMpidcount; i++)
4437 {
4438 if(caid_is_powervu(demux[demux_id].ECMpids[i].CAID))
4439 {
4440 is_powervu = true;
4441 break;
4442 }
4443 }
4444#endif
4445
4446 if(demux[demux_id].pmtpid == 0)
4447 {
4448 dvbapi_start_pat_filter(demux_id);
4449 }
4450 else if(demux[demux_id].STREAMpids[0] == demux[demux_id].pmtpid || is_powervu)
4451 {
4452 dvbapi_start_pmt_filter(demux_id);
4453 }
4454
4455 if(demux[demux_id].running)
4456 {
4457 disable_unused_streampids(demux_id); // disable all streampids not in use anymore
4458 }
4459
4460 if(!demux[demux_id].running && demux[demux_id].ECMpidcount != 0) // only start demuxer if it wasn't running
4461 {
4462 // remove all non important filtering
4463 // (there are images with limited amount of filters available!)
4464 dvbapi_stop_all_cat_emm_sdt_filtering(msgid);
4465
4466 get_servicename(dvbapi_client, demux[demux_id].program_number, demux[demux_id].ECMpids[0].PROVID,
4467 demux[demux_id].ECMpids[0].CAID, service_name, sizeof(service_name));
4468
4469 cs_log_dbg(D_DVBAPI, "Demuxer %d started descrambling for program %04X (%s) (fd: %d)",
4470 demux_id, demux[demux_id].program_number, service_name, demux[demux_id].socket_fd);
4471
4472 demux[demux_id].running = true; // mark channel as running
4473 openxcas_set_sid(demux[demux_id].program_number);
4474 demux[demux_id].decodingtries = -1;
4475 dvbapi_resort_ecmpids(demux_id);
4476 dvbapi_try_next_caid(demux_id, 0, msgid);
4477 cs_sleepms(1);
4478 }
4479 else if(demux[demux_id].ECMpidcount == 0) // FTA: do logging and part of ecm handler
4480 {
4481 get_servicename(dvbapi_client, demux[demux_id].program_number, NO_PROVID_VALUE, NO_CAID_VALUE,
4482 service_name, sizeof(service_name));
4483
4484 cs_log_dbg(D_DVBAPI, "Demuxer %d no descrambling needed for FTA program %04X (%s) (fd: %d)",
4485 demux_id, demux[demux_id].program_number, service_name, demux[demux_id].socket_fd);
4486
4487 demux[demux_id].running = false; // reset running flag
4488 demux[demux_id].pidindex = -1; // reset ecmpid used for descrambling
4489 dvbapi_stop_filter(demux_id, TYPE_ECM, msgid);
4490
4491 if(cfg.usrfileflag) // add to user log previous channel + time on channel
4492 {
4493 cs_statistics(dvbapi_client);
4494 }
4495
4496 dvbapi_client->last_srvid = demux[demux_id].program_number; // set new channel srvid
4497 dvbapi_client->last_caid = NO_CAID_VALUE; // FTA channels have no caid!
4498 dvbapi_client->last_provid = NO_PROVID_VALUE; // FTA channels have no provid!
4499 dvbapi_client->lastswitch = dvbapi_client->last = time((time_t *)0); // reset idle-Time & last switch
4500 }
4501
4502#if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
4503 // Don't start and Stop EMM Filters over and over again if we are on FTA
4504 if(dvbapi_client->last_caid == NO_CAID_VALUE)
4505 {
4506 start_emm = false;
4507 }
4508#endif
4509
4510 // only do emm setup if au enabled and not running!
4511 if(cfg.dvbapi_au > 0 && demux[demux_id].EMMpidcount == 0 && start_emm == true)
4512 {
4513 demux[demux_id].emm_filter = -1; // to register first run emmfilter start
4514
4515 if(demux[demux_id].emmstart.time == 1) // irdeto fetch emm cat direct!
4516 {
4517 // trick to let emm fetching start after 30 seconds to speed up zapping
4518 cs_ftime(&demux[demux_id].emmstart);
4519
4520 dvbapi_start_cat_filter(demux_id);
4521 }
4522 else
4523 {
4524 cs_ftime(&demux[demux_id].emmstart); // for all other caids delayed start!
4525 }
4526 }
4527}
4528
4529int32_t dvbapi_parse_capmt(const uint8_t *buffer, uint32_t length, int32_t connfd, char *pmtfile, uint16_t client_proto_version, uint32_t msgid)
4530{
4531 int32_t i, demux_id = -1;
4532 uint8_t ca_pmt_list_management, ca_pmt_cmd_id;
4533 bool is_update = false;
4534 demux_parameters_t parameters;
4535 memset(&parameters, 0, sizeof(parameters));
4536
4537#if defined WITH_COOLAPI || defined WITH_COOLAPI2
4538 ca_pmt_list_management = CA_PMT_LIST_ONLY;
4539#else
4540 ca_pmt_list_management = buffer[0];
4541#endif
4542
4543 // We received a new list of CA PMT objects.
4544 // Mark all demuxers to stop descrambling, but do not actually stop any of them,
4545 // until we verify the new list does not contain any previously selected program.
4546 if(ca_pmt_list_management == CA_PMT_LIST_FIRST || ca_pmt_list_management == CA_PMT_LIST_ONLY)
4547 {
4548 for(i = 0; i < MAX_DEMUX; i++)
4549 {
4550 // Skip empty demuxers, demuxers belonging to different
4551 // CA PMT connections or handled by different PMT files.
4552 if(demux[i].program_number == 0 || demux[i].socket_fd != connfd ||
4553 (demux[i].socket_fd == -1 && pmtfile && strcmp(demux[i].pmt_file, pmtfile) != 0))
4554 {
4555 continue;
4556 }
4557
4558 demux[i].stop_descrambling = true; // Mark for deletion if not used again by following CA PMT objects
4559
4560 cs_log_dbg(D_DVBAPI, "Demuxer %d marked to stop descrambling for program %04X (fd: %d)",
4561 i, demux[i].program_number, connfd);
4562 }
4563 }
4564
4565 // Read private descriptors inside the CA PMT message
4566 // in order to get adapter, demux, ca, pmt pid and more.
4567 get_demux_parameters(buffer, &parameters);
4568
4569 cs_log_dbg(D_DVBAPI, "Received CA PMT list management %d for program %04X (pmt pid: %04X adapter: %d demux: %d camask: %d)",
4570 ca_pmt_list_management, parameters.program_number, parameters.pmtpid, parameters.adapter_index,
4571 parameters.demux_index, parameters.ca_mask);
4572
4573 // Search current demuxers for having the same program
4574 // as the one we received in this CA PMT object.
4575 for(i = 0; i < MAX_DEMUX; i++)
4576 {
4577 if(demux[i].program_number == 0)
4578 {
4579 continue;
4580 }
4581
4582 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
4583 {
4584 parameters.demux_index = i; // fixup for ipbox
4585 }
4586
4587 bool full_check = true, matched = false;
4588
4589 if(config_enabled(WITH_COOLAPI) || config_enabled(WITH_COOLAPI2) || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
4590 {
4591 full_check = false;
4592 }
4593
4594 if(full_check)
4595 {
4596 matched = (connfd > 0 && demux[i].socket_fd == connfd) && demux[i].program_number == parameters.program_number;
4597 }
4598 else
4599 {
4600 matched = connfd > 0 && demux[i].program_number == parameters.program_number;
4601 }
4602
4603 if(matched)
4604 {
4605 if(full_check)
4606 {
4607 // In PMT mode 6, when zapping between channels with the same program number and PMT pid
4608 // (sometimes the case with satellite feeds), as all hardware parameters being the same
4609 // (adapter, demux, ca_mask, connfd), the new program is considered an existing one (matched).
4610 // The only reliable way to determine whether we actually have a new program is to compare
4611 // the enigma namespace, tsid and onid as well.
4612 if(demux[i].demux_index != parameters.demux_index || demux[i].ca_mask != parameters.ca_mask ||
4613 demux[i].adapter_index != parameters.adapter_index || demux[i].pmtpid != parameters.pmtpid ||
4614 demux[i].ens != parameters.ens || demux[i].tsid != parameters.tsid || demux[i].onid != parameters.onid)
4615 {
4616 continue;
4617 }
4618 }
4619
4620 // A program update is normally signaled by either a list management:
4621 // 1. UPDATE for an existing program
4622 // 2. ADD for an existing program (which according to the specifications should be treated as an UPDATE)
4623 // 3. ONLY for an existing program (with broken clients, in pmt modes other than 6)
4624 if(ca_pmt_list_management == CA_PMT_LIST_UPDATE || ca_pmt_list_management == CA_PMT_LIST_ADD
4625 || (cfg.dvbapi_pmtmode != 6 && ca_pmt_list_management == CA_PMT_LIST_ONLY))
4626 {
4627 is_update = true;
4628 cs_log("Demuxer %d received updated CA PMT for program %04X", i, parameters.program_number);
4629 }
4630
4631 cs_log("Demuxer %d continues descrambling for program %04X", i, demux[i].program_number);
4632 openxcas_set_sid(parameters.program_number);
4633 demux[i].stop_descrambling = false; // don't stop current demuxer!
4634 demux_id = i;
4635 break; // no need to explore other demuxers since we have a match!
4636 }
4637 }
4638
4639 // We are currently processing the last object of the CA PMT list.
4640 // We should now stop descrambling all programs not included in this list.
4641 if(ca_pmt_list_management != CA_PMT_LIST_FIRST && ca_pmt_list_management != CA_PMT_LIST_MORE)
4642 {
4643 for(i = 0; i < MAX_DEMUX; i++)
4644 {
4645 if(demux[i].program_number == 0)
4646 {
4647 continue;
4648 }
4649
4650 if(demux[i].stop_descrambling)
4651 {
4652 dvbapi_stop_descrambling(i, msgid);
4653 }
4654 }
4655 }
4656
4657 // We continue reading the CA PMT object only when we get
4658 // a new program (demux_id == -1) or an updated program.
4659 if(!(demux_id == -1 || is_update))
4660 {
4661 return demux_id;
4662 }
4663
4664 // We received a CA PMT object for a new program.
4665 // Let's find an empty demuxer for it.
4666 if(demux_id == -1)
4667 {
4668 if(ca_pmt_list_management == CA_PMT_LIST_UPDATE)
4669 {
4670 cs_log("ERROR: Received CA PMT list update for unknown program");
4671 return -1;
4672 }
4673
4674 for(demux_id = 0; demux_id < MAX_DEMUX; demux_id++)
4675 {
4676 if(demux[demux_id].program_number != 0)
4677 {
4678 continue; // Skip occupied demuxers
4679 }
4680
4681 // Probably this box doesn't send any private descriptor in the
4682 // CA PMT, so we have to improvise before saving to the demuxer.
4683 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
4684 {
4685 parameters.ca_mask = demux_id + 1;
4686 parameters.demux_index = demux_id;
4687 }
4688
4689 demux[demux_id].demux_index = parameters.demux_index;
4690 demux[demux_id].adapter_index = parameters.adapter_index;
4691 demux[demux_id].ca_mask = parameters.ca_mask;
4692 demux[demux_id].socket_fd = connfd;
4693 demux[demux_id].client_proto_version = client_proto_version;
4694 demux[demux_id].program_number = parameters.program_number;
4695 demux[demux_id].pmtpid = parameters.pmtpid;
4696 demux[demux_id].ens = parameters.ens;
4697 demux[demux_id].tsid = parameters.tsid;
4698 demux[demux_id].onid = parameters.onid;
4699 demux[demux_id].stop_descrambling = false;
4700 demux[demux_id].running = false;
4701 demux[demux_id].sdt_filter = -1;
4702 demux[demux_id].rdr = NULL;
4703
4704 if(pmtfile)
4705 {
4706 cs_strncpy(demux[demux_id].pmt_file, pmtfile, sizeof(demux[demux_id].pmt_file));
4707 }
4708
4709 break;
4710 }
4711
4712 if(demux_id >= MAX_DEMUX)
4713 {
4714 cs_log("There is no free demuxer for the new program! Aborting...");
4715 return -1;
4716 }
4717 }
4718
4719 // We continue parsing the CA PMT info for new or updated programs.
4720 // For updated programs, we just delete all previous stream pids and
4721 // ECM pids and start parsing the fresh data.
4722 dvbapi_parse_pmt_info(demux_id, buffer + 4, length - 4, 0x1FFF, &ca_pmt_cmd_id);
4723
4724 // Finally, evaluate what response the host requires from OSCam.
4725 // This allows multiple CA applications to run at the host simultaneously.
4726 // "OK query" will be implemented at a later stage, when support is first
4727 // added in enigma2.
4728 switch(ca_pmt_cmd_id)
4729 {
4730 case CA_PMT_CMD_OK_DESCRAMBLING:
4731 {
4732 dvbapi_capmt_notify(&demux[demux_id]);
4733 dvbapi_prepare_descrambling(demux_id, msgid);
4734 return demux_id;
4735 }
4736
4737 case CA_PMT_CMD_QUERY:
4738 {
4739 cs_log("Received unsupported CA PMT command ID 'query' for program %04X", demux[demux_id].program_number);
4740 dvbapi_stop_descrambling(demux_id, msgid); // Clear all data from this demuxer
4741 return -1;
4742 }
4743
4744 case CA_PMT_CMD_NOT_SELECTED:
4745 {
4746 cs_log("Program %04X is not selected for descrambling", demux[demux_id].program_number);
4747 dvbapi_stop_descrambling(demux_id, msgid); // Clear all data from this demuxer
4748 return -1;
4749 }
4750
4751 default:
4752 {
4753 cs_log("Received unknown or unsupported CA PMT command ID %02X from host", ca_pmt_cmd_id);
4754 dvbapi_stop_descrambling(demux_id, msgid);
4755 return -1;
4756 }
4757 }
4758}
4759
4760static void dvbapi_parse_pmt(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
4761{
4762 uint16_t program_number = b2i(2, buffer + 3);
4763 uint16_t pcr_pid = b2i(2, buffer + 8) & 0x1FFF;
4764
4765 if(program_number != demux[demux_id].program_number)
4766 {
4767 cs_log("Demuxer %d received PMT for undefined program %04X", demux_id, program_number);
4768 return;
4769 }
4770
4771 dvbapi_stop_filter(demux_id, TYPE_PMT, msgid);
4772 dvbapi_parse_pmt_info(demux_id, buffer + 10, length - 10 - 4, pcr_pid, NULL); // last 4 bytes are the CRC-32
4773}
4774
4775static void dvbapi_create_srvid_line(int32_t demux_id, char *buffer, uint32_t buflen)
4776{
4777 int32_t i, j, pos = 0;
4778 uint16_t caid_done[32], cur_caid;
4779 uint8_t caid_done_count = 0, skip_caid;
4780
4781 if(demux[demux_id].ECMpidcount == 0)
4782 {
4783 snprintf(buffer, buflen, "%04X@%06X", NO_CAID_VALUE, NO_PROVID_VALUE);
4784 return;
4785 }
4786
4787 for(i = 0; i < demux[demux_id].ECMpidcount && i < 32; i++)
4788 {
4789 skip_caid = 0;
4790 for(j = 0; j < caid_done_count; j++)
4791 {
4792 if(caid_done[j] == demux[demux_id].ECMpids[i].CAID)
4793 {
4794 skip_caid = 1;
4795 break;
4796 }
4797 }
4798
4799 if(skip_caid)
4800 {
4801 continue;
4802 }
4803
4804 cur_caid = demux[demux_id].ECMpids[i].CAID;
4805 pos += snprintf(buffer + pos, buflen - pos, "%s%04X", caid_done_count > 0 ? "," : "", cur_caid == 0 ? NO_CAID_VALUE : cur_caid);
4806
4807 for(j = i; j < demux[demux_id].ECMpidcount; j++)
4808 {
4809 if(demux[demux_id].ECMpids[j].PROVID == 0)
4810 {
4811 continue;
4812 }
4813
4814 if(cur_caid == demux[demux_id].ECMpids[j].CAID)
4815 {
4816 pos += snprintf(buffer + pos, buflen - pos, "@%06X", demux[demux_id].ECMpids[j].PROVID);
4817 }
4818 }
4819
4820 caid_done[caid_done_count] = demux[demux_id].ECMpids[i].CAID;
4821 caid_done_count++;
4822 }
4823}
4824
4825static void dvbapi_write_sdt_info(int32_t demux_id, const char *provider_name, const char* service_name, const char *service_type)
4826{
4827 int8_t did_save_srvid = 0;
4828 int32_t provid, caid, pidindex;
4829 char tmp[256], srvid_line[1024];
4830 FILE *fpsave = NULL;
4831
4832 pidindex = demux[demux_id].pidindex;
4833 if(pidindex != -1)
4834 {
4835 caid = demux[demux_id].ECMpids[pidindex].CAID;
4836 provid = demux[demux_id].ECMpids[pidindex].PROVID;
4837 }
4838 else
4839 {
4840 if(demux[demux_id].ECMpidcount == 0 || demux[demux_id].ECMpids[0].CAID == 0)
4841 {
4842 caid = NO_CAID_VALUE;
4843 provid = NO_PROVID_VALUE;
4844 }
4845 else
4846 {
4847 caid = demux[demux_id].ECMpids[0].CAID;
4848 provid = demux[demux_id].ECMpids[0].PROVID;
4849 }
4850 }
4851
4852 if(cs_strlen(provider_name) && caid != NO_CAID_VALUE)
4853 {
4854 get_providername_or_null(provid, caid, tmp, sizeof(tmp));
4855
4856 if(tmp[0] == '\0')
4857 {
4858 get_config_filename(tmp, sizeof(tmp), "oscam.provid");
4859
4860 if((fpsave = fopen(tmp, "a")))
4861 {
4862 fprintf(fpsave, "\n%04X@%06X|%s|", caid, provid, provider_name);
4863 fclose(fpsave);
4864 init_provid();
4865 }
4866 }
4867 }
4868
4869 if(cs_strlen(service_name))
4870 {
4871 get_servicename_or_null(cur_client(), demux[demux_id].program_number, provid, caid, tmp, sizeof(tmp));
4872
4873 if(tmp[0] == '\0')
4874 {
4875 get_config_filename(tmp, sizeof(tmp), "oscam.srvid2");
4876
4877 if(!access(tmp, F_OK) && (fpsave = fopen(tmp, "a")))
4878 {
4879 if((caid != NO_CAID_VALUE) || (cfg.dvbapi_read_sdt > 1))
4880 {
4881 dvbapi_create_srvid_line(demux_id, srvid_line, sizeof(srvid_line));
4882
4883 if(cfg.dvbapi_write_sdt_prov)
4884 {
4885 fprintf(fpsave, "\n%04X:%s|%s|%s||%s", demux[demux_id].program_number, srvid_line, service_name, service_type, provider_name);
4886 }
4887 else
4888 {
4889 fprintf(fpsave, "\n%04X:%s|%s|%s", demux[demux_id].program_number, srvid_line, service_name, service_type);
4890 }
4891
4892 did_save_srvid = 1;
4893 }
4894 }
4895 else
4896 {
4897 get_config_filename(tmp, sizeof(tmp), "oscam.srvid");
4898
4899 if((fpsave = fopen(tmp, "a")))
4900 {
4901 if((caid != NO_CAID_VALUE) || (cfg.dvbapi_read_sdt > 1))
4902 {
4903 dvbapi_create_srvid_line(demux_id, srvid_line, sizeof(srvid_line));
4904
4905 if(cfg.dvbapi_write_sdt_prov)
4906 {
4907 fprintf(fpsave, "\n%s:%04X|%s|%s|%s", srvid_line, demux[demux_id].program_number, provider_name, service_name, service_type);
4908 }
4909 else
4910 {
4911 fprintf(fpsave, "\n%s:%04X||%s|%s", srvid_line, demux[demux_id].program_number, service_name, service_type);
4912 }
4913
4914 did_save_srvid = 1;
4915 }
4916 }
4917 }
4918
4919 if(fpsave)
4920 {
4921 fclose(fpsave);
4922 }
4923
4924 if(did_save_srvid)
4925 {
4926 init_srvid();
4927 }
4928 }
4929 }
4930}
4931
4932static uint32_t dvbapi_extract_sdt_string(char *buf, uint32_t buflen, const uint8_t *source, uint8_t sourcelen)
4933{
4934 uint32_t i, j, offset = 0;
4935 int8_t iso_mode = -1;
4936 char *tmpbuf;
4937 const uint8_t *ptr_in;
4938 uint8_t *ptr_out;
4939 size_t in_bytes, out_bytes;
4940
4941 if(sourcelen == 0)
4942 {
4943 buf[0] = '\0';
4944 return 1;
4945 }
4946
4947 if(!cs_malloc(&tmpbuf, buflen))
4948 {
4949 return 0;
4950 }
4951
4952 if(sourcelen > buflen - 1)
4953 {
4954 sourcelen = buflen - 1;
4955 }
4956
4957 if(sourcelen > 0 && source[0] < 0x20)
4958 {
4959 if(source[0] >= 0x01 && source[0] <= 0x0B && source[0] != 0x08) // ISO/IEC 8859
4960 {
4961 offset = 1;
4962 iso_mode = 4 + source[0];
4963 }
4964 else if(source[0] == 0x10) // Dynamically selected part of ISO/IEC 8859
4965 {
4966 if(source[1] == 0x00 && source[2] >= 0x01 && source[2] <= 0x0F && source[2] != 0x0C)
4967 {
4968 offset = 3;
4969 iso_mode = source[2];
4970 }
4971 }
4972 else if(source[0] == 0x11) // ISO/IEC 10646
4973 {
4974 offset = 1;
4975 iso_mode = -2;
4976 }
4977 // missing: 0x12 KSX1001-2004 (Korean Character Set)
4978 // missing: 0x13 GB-2312-1980 (Simplified Chinese Character Set)
4979 // missing: 0x14 Big5 subset of ISO/IEC 10646 (Traditional Chinese)
4980 else if(source[0] == 0x15) // UTF-8 encoding of ISO/IEC 10646
4981 {
4982 offset = 1;
4983 iso_mode = -3;
4984 }
4985 // missing: 0x1F Described by encoding_type_id
4986 else
4987 {
4988 NULLFREE(tmpbuf);
4989 return 0;
4990 }
4991 }
4992
4993 if(offset >= sourcelen)
4994 {
4995 NULLFREE(tmpbuf);
4996 return 0;
4997 }
4998
4999 if(iso_mode >= -1)
5000 {
5001 for(i = 0, j = 0; i < sourcelen - offset; i++)
5002 {
5003 if(source[offset + i] >= 0x80 && source[offset + i] <= 0x9F)
5004 {
5005 continue;
5006 }
5007
5008 tmpbuf[j] = source[offset + i];
5009 j++;
5010 }
5011
5012 tmpbuf[j] = '\0';
5013 }
5014
5015 ptr_in = (const uint8_t *)tmpbuf;
5016 in_bytes = cs_strlen(tmpbuf);
5017 ptr_out = (uint8_t *)buf;
5018 out_bytes = buflen;
5019
5020#ifdef READ_SDT_CHARSETS
5021 if(iso_mode >= -1)
5022 {
5023 memset(buf, 0, buflen);
5024 cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: %d offset: %u", iso_mode, offset);
5025 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)tmpbuf, in_bytes, "sdt-info dbg: raw string:");
5026
5027 if(iso_mode == -1)
5028 {
5029 if(ISO6937toUTF8(&ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
5030 {
5031 cs_log_dbg(D_DVBAPI, "sdt-info error: ISO6937toUTF8 failed");
5032 NULLFREE(tmpbuf);
5033 return 0;
5034 }
5035 }
5036 else
5037 {
5038 if(ISO8859toUTF8(iso_mode, &ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
5039 {
5040 cs_log_dbg(D_DVBAPI, "sdt-info error: ISO8859toUTF8 failed");
5041 NULLFREE(tmpbuf);
5042 return 0;
5043 }
5044 }
5045 }
5046#else
5047 if(iso_mode >= -1)
5048 {
5049 cs_strncpy(buf, tmpbuf, buflen);
5050 cs_log_dbg(D_DVBAPI, "sdt-info warning: your build of oscam does not support iso-to-utf8 conversion, special chars may be corrupted!");
5051 }
5052#endif
5053 else if(iso_mode == -2)
5054 {
5055 memset(buf, 0, buflen);
5056 cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: %d offset: %u", iso_mode, offset);
5057
5058 if(UnicodetoUTF8(&ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
5059 {
5060 cs_log_dbg(D_DVBAPI, "sdt-info error: UnicodetoUTF8 failed");
5061 NULLFREE(tmpbuf);
5062 return 0;
5063 }
5064 }
5065 else if(iso_mode == -3) // No conversion, already in UTF-8
5066 {
5067 memcpy(buf, source + offset, sourcelen - offset);
5068 buf[sourcelen - offset] = '\0';
5069 cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: -3 offset: %u", offset);
5070 }
5071
5072 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)buf, cs_strlen(buf), "sdt-info dbg: encoded string:");
5073 NULLFREE(tmpbuf);
5074 return 1;
5075}
5076
5077static const char *dvbapi_get_service_type(uint8_t service_type)
5078{
5079 switch(service_type)
5080 {
5081 case 0x01:
5082 case 0x0B:
5083 case 0x11:
5084 case 0x16:
5085 case 0x17:
5086 case 0x18:
5087 case 0x19:
5088 case 0x1A:
5089 case 0x1B:
5090 case 0x1C:
5091 case 0x1D:
5092 case 0x1E:
5093 case 0x1F:
5094 case 0x20:
5095 return "TV";
5096
5097 case 0x02:
5098 case 0x07:
5099 case 0x0A:
5100 return "Radio";
5101
5102 case 0x03:
5103 return "Teletext";
5104
5105 case 0x0C:
5106 return "Data";
5107
5108 default:
5109 return "unknown";
5110 }
5111}
5112
5113static void dvbapi_parse_service_descriptor(int32_t demux_id, const uint8_t *buffer, uint8_t descriptor_length)
5114{
5115 uint8_t service_provider_name_length, service_name_length;
5116 char service_provider_name[64], service_name[64];
5117 const char *service_type;
5118
5119 if(descriptor_length < 3)
5120 {
5121 return; // Service descriptor has a minimum length of 3 bytes
5122 }
5123
5124 service_type = dvbapi_get_service_type(buffer[0]);
5125
5126 service_provider_name_length = buffer[1];
5127 if(2 + service_provider_name_length + 1 > descriptor_length)
5128 {
5129 return;
5130 }
5131
5132 service_name_length = buffer[2 + service_provider_name_length];
5133 if(2 + service_provider_name_length + 1 + service_name_length > descriptor_length)
5134 {
5135 return;
5136 }
5137
5138 if(!dvbapi_extract_sdt_string(service_provider_name, sizeof(service_provider_name), buffer + 2, service_provider_name_length) ||
5139 !dvbapi_extract_sdt_string(service_name, sizeof(service_name), buffer + 2 + service_provider_name_length + 1, service_name_length))
5140 {
5141 return;
5142 }
5143
5144 cs_log_dbg(D_DVBAPI, "Demuxer %d got service info (provider: %s - name: %s - type: %s)",
5145 demux_id, service_provider_name, service_name, service_type);
5146
5147 dvbapi_write_sdt_info(demux_id, service_provider_name, service_name, service_type);
5148}
5149
5150static void dvbapi_parse_sdt(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
5151{
5152 uint8_t descriptor_tag, descriptor_length;
5153 uint16_t service_id, descriptors_loop_length, i, j;
5154
5155 if(buffer[0] != 0x42) // SDT sections with table_id value 0x42 describe the actual TS
5156 {
5157 return;
5158 }
5159
5160 // Get the tsid and onid (in enigma2 STBs we have
5161 // already received them in the CA PMT message)
5162 demux[demux_id].tsid = b2i(2, buffer + 3);
5163 demux[demux_id].onid = b2i(2, buffer + 8);
5164
5165 for(i = 11; i + 5 < length; i += 5 + descriptors_loop_length)
5166 {
5167 service_id = b2i(2, buffer + i);
5168 descriptors_loop_length = b2i(2, buffer + i + 3) & 0x0FFF;
5169
5170 if(service_id != demux[demux_id].program_number)
5171 {
5172 continue;
5173 }
5174
5175 for(j = 0; j + 1 < descriptors_loop_length; j += 2 + descriptor_length)
5176 {
5177 descriptor_tag = buffer[i + 5 + j];
5178 descriptor_length = buffer[i + 5 + j + 1];
5179
5180 if(descriptor_tag == 0x48)
5181 {
5182 dvbapi_parse_service_descriptor(demux_id, buffer + i + 5 + j + 2, descriptor_length);
5183 }
5184 }
5185
5186 dvbapi_stop_filter(demux_id, TYPE_SDT, msgid);
5187 break;
5188 }
5189}
5190
5191static void dvbapi_parse_pat(int32_t demux_id, const uint8_t *buffer, uint16_t length, uint32_t msgid)
5192{
5193 uint16_t i, srvid;
5194
5195 dvbapi_stop_filter(demux_id, TYPE_PAT, msgid);
5196
5197 for(i = 8; i + 7 < length; i += 4)
5198 {
5199 srvid = b2i(2, buffer + i);
5200 if(srvid == 0)
5201 {
5202 continue;
5203 }
5204
5205 if(demux[demux_id].program_number == srvid)
5206 {
5207 demux[demux_id].pmtpid = b2i(2, buffer + i + 2) & 0x1FFF;
5208 dvbapi_start_pmt_filter(demux_id);
5209 break;
5210 }
5211 }
5212}
5213
5214int32_t dvbapi_init_listenfd(void)
5215{
5216 int32_t clilen, listenfd;
5217 struct sockaddr_un servaddr;
5218
5219 memset(&servaddr, 0, sizeof(struct sockaddr_un));
5220 servaddr.sun_family = AF_UNIX;
5221 cs_strncpy(servaddr.sun_path, devices[selected_box].cam_socket_path, sizeof(servaddr.sun_path));
5222 clilen = sizeof(servaddr.sun_family) + cs_strlen(servaddr.sun_path);
5223
5224 if(((unlink(devices[selected_box].cam_socket_path) < 0) && (errno != ENOENT))
5225 || ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
5226 || (bind(listenfd, (struct sockaddr *)&servaddr, clilen) < 0)
5227 || (listen(listenfd, 5) < 0))
5228 {
5229 return 0;
5230 }
5231
5232 // change the access right on the camd.socket
5233 // this will allow oscam to run as root if needed
5234 // and still allow non root client to connect to the socket
5235 chmod(devices[selected_box].cam_socket_path, S_IRWXU | S_IRWXG | S_IRWXO);
5236 return listenfd;
5237}
5238
5239int32_t dvbapi_net_init_listenfd(void)
5240{
5241 int32_t listenfd;
5242 struct SOCKADDR servaddr;
5243
5244 memset(&servaddr, 0, sizeof(servaddr));
5245 SIN_GET_FAMILY(servaddr) = DEFAULT_AF;
5246 SIN_GET_ADDR(servaddr) = ADDR_ANY;
5247 SIN_GET_PORT(servaddr) = htons((uint16_t)cfg.dvbapi_listenport);
5248
5249 if((listenfd = socket(DEFAULT_AF, SOCK_STREAM, 0)) < 0)
5250 {
5251 return 0;
5252 }
5253 int32_t opt = 0;
5254
5255#ifdef IPV6SUPPORT
5256// azbox toolchain do not have this define
5257#ifndef IPV6_V6ONLY
5258#define IPV6_V6ONLY 26
5259#endif
5260 // set the server socket option to listen on IPv4 and IPv6 simultaneously
5261 setsockopt(listenfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&opt, sizeof(opt));
5262#endif
5263
5264 opt = 1;
5265 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
5266 set_so_reuseport(listenfd);
5267
5268 if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
5269 {
5270 return 0;
5271 }
5272
5273 if(listen(listenfd, 5) < 0)
5274 {
5275 return 0;
5276 }
5277 return listenfd;
5278}
5279
5280static pthread_mutex_t event_handler_lock = PTHREAD_MUTEX_INITIALIZER;
5281
5282void event_handler(int32_t UNUSED(signal))
5283{
5284 struct stat pmt_info;
5285 char dest[1024];
5286 DIR *dirp;
5287 struct dirent entry, *dp = NULL;
5288 int32_t i;
5289 int32_t pmt_fd = -1;
5290 uint8_t mbuf[2048]; // dirty fix: larger buffer needed for CA PMT mode 6 with many parallel channels to decode
5291
5292 if(dvbapi_client != cur_client())
5293 {
5294 return;
5295 }
5296
5297 SAFE_MUTEX_LOCK(&event_handler_lock);
5298 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
5299 {
5300 pausecam = 0;
5301 }
5302 else
5303 {
5304 int32_t standby_fd = open(STANDBY_FILE, O_RDONLY);
5305 pausecam = (standby_fd > 0) ? 1 : 0;
5306 if(standby_fd > 0)
5307 {
5308 int32_t ret = close(standby_fd);
5309 if(ret < 0)
5310 {
5311 cs_log("ERROR: Could not close standby fd (errno=%d %s)", errno, strerror(errno));
5312 }
5313 }
5314 }
5315
5316 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_pmtmode == 1)
5317 {
5318 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5319 return;
5320 }
5321
5322 for(i = 0; i < MAX_DEMUX; i++)
5323 {
5324 if(demux[i].pmt_file[0] != 0)
5325 {
5326 snprintf(dest, sizeof(dest), "%s%s", TMPDIR, demux[i].pmt_file);
5327 pmt_fd = open(dest, O_RDONLY);
5328 if(pmt_fd > 0)
5329 {
5330 if(fstat(pmt_fd, &pmt_info) != 0)
5331 {
5332 int32_t ret = close(pmt_fd);
5333 if(ret < 0)
5334 {
5335 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5336 }
5337 continue;
5338 }
5339
5340 if((time_t)pmt_info.st_mtime != demux[i].pmt_time)
5341 {
5342 dvbapi_stop_descrambling(i, 0);
5343 }
5344
5345 int32_t ret = close(pmt_fd);
5346 if(ret < 0)
5347 {
5348 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5349 }
5350 continue;
5351 }
5352 else
5353 {
5354 cs_log("Demuxer %d Unable to open PMT file %s -> stop descrambling!", i, dest);
5355 dvbapi_stop_descrambling(i, 0);
5356 }
5357 }
5358 }
5359
5360 if(disable_pmt_files)
5361 {
5362 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5363 return;
5364 }
5365
5366 dirp = opendir(TMPDIR);
5367 if(!dirp)
5368 {
5369 cs_log_dbg(D_DVBAPI, "opendir failed (errno=%d %s)", errno, strerror(errno));
5370 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5371 return;
5372 }
5373
5374 while(!cs_readdir_r(dirp, &entry, &dp))
5375 {
5376 if(!dp)
5377 {
5378 break;
5379 }
5380
5381 if(cs_strlen(dp->d_name) < 7)
5382 {
5383 continue;
5384 }
5385
5386 if(strncmp(dp->d_name, "pmt", 3) != 0 || strncmp(dp->d_name + cs_strlen(dp->d_name) - 4, ".tmp", 4) != 0)
5387 {
5388 continue;
5389 }
5390
5391#if defined(WITH_STAPI) || defined(WITH_STAPI5)
5392 struct s_dvbapi_priority *p;
5393 for(p = dvbapi_priority; p != NULL; p = p->next) // stapi: check if there is a device connected to this pmt file!
5394 {
5395 if(p->type != 's') { continue; } // stapi rule?
5396 if(strcmp(dp->d_name, p->pmtfile) != 0) { continue; } // same file?
5397 break; // found match!
5398 }
5399
5400 if(p == NULL)
5401 {
5402 cs_log_dbg(D_DVBAPI, "No matching S: line in oscam.dvbapi for pmtfile %s -> skip!", dp->d_name);
5403 continue;
5404 }
5405#endif
5406
5407 if (!cs_strlen(TMPDIR))
5408 {
5409 cs_log_dbg(D_DVBAPI, "BUG! cs_strlen(TMPDIR)!!!\n");
5410 continue;
5411 }
5412
5413 if (!cs_strlen(dp->d_name))
5414 {
5415 cs_log_dbg(D_DVBAPI, "BUG! cs_strlen(dp->d_name)!!!\n");
5416 continue;
5417 }
5418
5419 if((cs_strlen(dp->d_name) + cs_strlen(TMPDIR) - 1) > sizeof(dest))
5420 {
5421 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));
5422 continue;
5423 }
5424 else
5425 {
5426 memcpy(dest, TMPDIR, cs_strlen(TMPDIR));
5427 memcpy(dest + cs_strlen(TMPDIR), dp->d_name, cs_strlen(dp->d_name));
5428 dest[cs_strlen(TMPDIR) + cs_strlen(dp->d_name)] = '\0';
5429 pmt_fd = open(dest, O_RDONLY);
5430 }
5431
5432 if(pmt_fd < 0)
5433 {
5434 continue;
5435 }
5436
5437 if(fstat(pmt_fd, &pmt_info) != 0)
5438 {
5439 int32_t ret = close(pmt_fd);
5440 if(ret < 0)
5441 {
5442 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5443 }
5444 continue;
5445 }
5446
5447 int32_t found = 0;
5448
5449 for(i = 0; i < MAX_DEMUX; i++)
5450 {
5451 if(strcmp(demux[i].pmt_file, dp->d_name) == 0)
5452 {
5453 if((time_t)pmt_info.st_mtime == demux[i].pmt_time)
5454 {
5455 found = 1;
5456 break;
5457 }
5458 }
5459 }
5460
5461 if(found)
5462 {
5463 int32_t ret = close(pmt_fd);
5464 if(ret < 0)
5465 {
5466 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5467 }
5468 continue;
5469 }
5470 cs_log_dbg(D_DVBAPI, "found pmt file %s", dest);
5471 cs_sleepms(100);
5472
5473 uint32_t len = read(pmt_fd, mbuf, sizeof(mbuf));
5474 int32_t ret = close(pmt_fd);
5475
5476 if(ret < 0)
5477 {
5478 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
5479 }
5480
5481 if(len < 1)
5482 {
5483 cs_log_dbg(D_DVBAPI, "pmt file %s have invalid len!", dest);
5484 continue;
5485 }
5486
5487 int32_t demux_id;
5488#ifdef QBOXHD
5489 uint32_t j1, j2;
5490
5491 // QboxHD pmt.tmp is the full capmt written as a string of hex values
5492 // pmt.tmp must be longer than 3 bytes (6 hex chars) and even length
5493 if((len < 6) || ((len % 2) != 0) || ((len / 2) > sizeof(dest)))
5494 {
5495 cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, incorrect length");
5496 continue;
5497 }
5498
5499 for(j2 = 0, j1 = 0; j2 < len; j2 += 2, j1++)
5500 {
5501 unsigned int tmp;
5502 if(sscanf((char *)mbuf + j2, "%02X", &tmp) != 1)
5503 {
5504 cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, data not valid in position %d", j2);
5505 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5506 return;
5507 }
5508 else
5509 {
5510 memcpy(dest + j1, &tmp, 4);
5511 }
5512 }
5513
5514 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)dest, len / 2, "QboxHD pmt.tmp:");
5515 demux_id = dvbapi_parse_capmt((uint8_t *)dest + 4, (len / 2) - 4, -1, dp->d_name, 0, 0);
5516#else
5517 if(len > sizeof(dest))
5518 {
5519 cs_log_dbg(D_DVBAPI, "event_handler() dest buffer is to small for pmt data!");
5520 continue;
5521 }
5522
5523 if(len < 16)
5524 {
5525 cs_log_dbg(D_DVBAPI, "event_handler() received pmt is too small! (%d < 16 bytes!)", len);
5526 continue;
5527 }
5528
5529 cs_log_dump_dbg(D_DVBAPI, mbuf, len, "PMT file:"); // Original PMT file
5530
5531 // Do some tidying on the PMT file to make it compatible with the CA PMT parser
5532 dest[0] = CA_PMT_LIST_ONLY;
5533 memcpy(dest + 1, mbuf + 3, 2); // program_number
5534 uint16_t pmt_program_info_length = b2i(2, mbuf + 10) & 0x0FFF;
5535 i2b_buf(2, pmt_program_info_length + 1, (uint8_t *)dest + 4);
5536 dest[6] = CA_PMT_CMD_OK_DESCRAMBLING;
5537 memcpy(dest + 7, mbuf + 12, len - 12 - 4);
5538
5539 cs_log_dump_dbg(D_DVBAPI, (uint8_t *)dest, 7 + len - 12 - 4, "CA PMT:"); // Actual CA PMT message
5540 demux_id = dvbapi_parse_capmt((uint8_t *)dest, 7 + len - 12 - 4, -1, dp->d_name, 0, 0);
5541#endif
5542
5543 if(demux_id >= 0)
5544 {
5545 cs_strncpy(demux[demux_id].pmt_file, dp->d_name, sizeof(demux[demux_id].pmt_file));
5546 demux[demux_id].pmt_time = (time_t)pmt_info.st_mtime;
5547 }
5548
5549 if(cfg.dvbapi_pmtmode == 3)
5550 {
5551 disable_pmt_files = 1;
5552 break;
5553 }
5554 }
5555 closedir(dirp);
5556 SAFE_MUTEX_UNLOCK(&event_handler_lock);
5557}
5558
5559void *dvbapi_event_thread(void *cli)
5560{
5561 struct s_client *client = (struct s_client *) cli;
5562 SAFE_SETSPECIFIC(getclient, client);
5563 set_thread_name(__func__);
5564
5565 while(!exit_oscam)
5566 {
5567 cs_sleepms(750);
5568 event_handler(0);
5569 }
5570 return NULL;
5571}
5572
5573void dvbapi_process_input(int32_t demux_id, int32_t filter_num, uint8_t *buffer, int32_t len, uint32_t msgid)
5574{
5575 struct s_ecmpid *curpid = NULL;
5576 int32_t pid = demux[demux_id].demux_fd[filter_num].pidindex;
5577 uint16_t filtertype = demux[demux_id].demux_fd[filter_num].type;
5578 uint16_t sctlen = SCT_LEN(buffer);
5579
5580 if(sctlen < 4)
5581 {
5582 cs_log_dbg(D_DVBAPI, "Received filter data with invalid section length!");
5583 return;
5584 }
5585
5586 if(len < sctlen)
5587 {
5588 cs_log_dbg(D_DVBAPI, "Received filter data with total length 0x%03X but section length is 0x%03X -> invalid length!", len, sctlen);
5589 return;
5590 }
5591
5592 if(demux_id < 0 || demux_id >= MAX_DEMUX)
5593 {
5594 cs_log("dvbapi_process_input(): error - received invalid demux_id (%d)", demux_id);
5595 return;
5596 }
5597
5598 if(filter_num < 0 || filter_num >= MAX_FILTER)
5599 {
5600 cs_log("dvbapi_process_input(): error - received invalid filter_num (%d)", filter_num);
5601 return;
5602 }
5603
5604 if(pid != -1 && filtertype == TYPE_ECM)
5605 {
5606 curpid = &demux[demux_id].ECMpids[pid];
5607 }
5608
5609 int32_t filt_match = filtermatch(buffer, filter_num, demux_id, sctlen); // acts on all filters (sdt/emm/ecm)
5610 if(!filt_match)
5611 {
5612 cs_log_dbg(D_DVBAPI,"Demuxer %d receiver returned data not matching the filter -> delivered filter data discarded!", demux_id);
5613 return;
5614 }
5615
5616 if(curpid && curpid->tries <= 0xF0 && filtertype == TYPE_ECM)
5617 {
5618 curpid->irdeto_maxindex = 0;
5619 curpid->irdeto_curindex = 0xFE;
5620 curpid->tries = 0xFE; // reset timeout retry flag
5621 curpid->irdeto_cycle = 0xFE; // reset irdetocycle
5622 curpid->table = 0;
5623 curpid->checked = 4; // flag ecmpid as checked
5624 curpid->status = -1; // flag ecmpid as unusable
5625
5626 if(pid == demux[demux_id].pidindex)
5627 {
5628 // current pid delivered problems so this pid isn't
5629 // being used to descramble any longer -> clear pidindex
5630 demux[demux_id].pidindex = -1;
5631
5632 // remove this pid from channel cache since we had no founds on any ecmpid!
5633 dvbapi_edit_channel_cache(demux_id, pid, 0);
5634 }
5635
5636 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
5637 return;
5638 }
5639
5640 if(filtertype == TYPE_ECM)
5641 {
5642 uint32_t chid = 0x10000;
5643 int8_t pvu_skip = 0;
5644 ECM_REQUEST *er;
5645
5646 if(len != 0) // len = 0 receiver encountered an internal bufferoverflow!
5647 {
5648 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched ECM data (length = 0x%03X):",
5649 demux_id, filter_num + 1, sctlen);
5650
5651 if(sctlen > MAX_ECM_SIZE) // ecm too long to handle!
5652 {
5653 cs_log_dbg(D_DVBAPI, "Received data with total length 0x%03X but max supported ECM length is 0x%03X -> Please report!",
5654 sctlen, MAX_ECM_SIZE);
5655
5656 if(curpid)
5657 {
5658 curpid->tries -= 0x0E;
5659 }
5660 return;
5661 }
5662
5663 if(!(buffer[0] == 0x80 || buffer[0] == 0x81))
5664 {
5665 cs_log_dbg(D_DVBAPI, "Received an ECM with invalid ecmtable ID %02X -> ignoring!", buffer[0]);
5666 if(curpid)
5667 {
5668 curpid->tries--;
5669 }
5670 return;
5671 }
5672
5673#ifdef WITH_EMU
5674 if(caid_is_powervu(curpid->CAID)) // ecm counter for powervu
5675 {
5676 pvu_skip = 1;
5677
5678 if(sctlen - 11 > buffer[9])
5679 {
5680 if(buffer[11 + buffer[9]] > curpid->pvu_counter
5681 || (curpid->pvu_counter == 255 && buffer[11 + buffer[9]] == 0)
5682 || ((curpid->pvu_counter - buffer[11 + buffer[9]]) > 5))
5683 {
5684 curpid->pvu_counter = buffer[11 + buffer[9]];
5685 pvu_skip = 0;
5686 }
5687 }
5688 }
5689#endif
5690 // wait for odd / even ecm change (only not for irdeto!)
5691 if((curpid->table == buffer[0] && !caid_is_irdeto(curpid->CAID)) || pvu_skip)
5692 {
5693 if(!(er = get_ecmtask()))
5694 {
5695 return;
5696 }
5697 er->srvid = demux[demux_id].program_number;
5698
5699#ifdef WITH_STAPI5
5700 cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
5701#endif
5702 er->tsid = demux[demux_id].tsid;
5703 er->onid = demux[demux_id].onid;
5704 er->pmtpid = demux[demux_id].pmtpid;
5705 er->ens = demux[demux_id].ens;
5706 er->caid = curpid->CAID;
5707 er->pid = curpid->ECM_PID;
5708 er->prid = curpid->PROVID;
5709 er->vpid = curpid->VPID;
5710 er->ecmlen = sctlen;
5711 memcpy(er->ecm, buffer, er->ecmlen);
5712 chid = get_subid(er); // fetch chid or fake chid
5713 er->chid = chid;
5714 er->msgid = msgid;
5715 dvbapi_set_section_filter(demux_id, er, filter_num);
5716 NULLFREE(er);
5717 return;
5718 }
5719
5720 if(caid_is_irdeto(curpid->CAID))
5721 {
5722 // 80 70 39 53 04 05 00 88
5723 // 81 70 41 41 01 06 00 13 00 06 80 38 1F 52 93 D2
5724 //if(buffer[5]>20) return;
5725 if(curpid->irdeto_maxindex != buffer[5]) // 6, register max irdeto index
5726 {
5727 cs_log_dbg(D_DVBAPI, "Found %d IRDETO ECM CHIDs", buffer[5] + 1);
5728 curpid->irdeto_maxindex = buffer[5]; // numchids = 7 (0..6)
5729 }
5730 }
5731 }
5732
5733 if(!(er = get_ecmtask()))
5734 {
5735 return;
5736 }
5737 er->srvid = demux[demux_id].program_number;
5738
5739#ifdef WITH_STAPI5
5740 cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
5741#endif
5742
5743 er->tsid = demux[demux_id].tsid;
5744 er->onid = demux[demux_id].onid;
5745 er->pmtpid = demux[demux_id].pmtpid;
5746 er->ens = demux[demux_id].ens;
5747 er->caid = curpid->CAID;
5748 er->pid = curpid->ECM_PID;
5749 er->prid = curpid->PROVID;
5750 er->vpid = curpid->VPID;
5751 er->ecmlen = sctlen;
5752 memcpy(er->ecm, buffer, er->ecmlen);
5753 er->msgid = msgid;
5754 chid = get_subid(er); // fetch chid or fake chid
5755 uint32_t fixedprovid = chk_provid(er->ecm, er->caid);
5756
5757 if(fixedprovid && fixedprovid != er->prid)
5758 {
5759 cs_log_dbg(D_DVBAPI, "Fixing provid ecmpid %d from %06X -> %06X", pid, curpid->PROVID, fixedprovid);
5760 curpid->PROVID = fixedprovid;
5761
5762 if(!USE_OPENXCAS)
5763 {
5764 cs_log_dbg(D_DVBAPI, "Fixing provid filter %d from %06X -> %06X",
5765 filter_num + 1, demux[demux_id].demux_fd[filter_num].provid, fixedprovid);
5766
5767 demux[demux_id].demux_fd[filter_num].provid = fixedprovid;
5768 }
5769 cs_log_dbg(D_DVBAPI, "Fixing provid ecmrequest from %06X -> %06X", er->prid, fixedprovid);
5770 er->prid = fixedprovid;
5771 }
5772 er->chid = chid;
5773
5774 // only used on receiver internal buffer overflow
5775 // to get quickly fresh ecm filterdata otherwise freezing!
5776 if(len == 0)
5777 {
5778 curpid->table = 0;
5779 dvbapi_set_section_filter(demux_id, er, filter_num);
5780 NULLFREE(er);
5781 return;
5782 }
5783
5784 if(caid_is_irdeto(curpid->CAID))
5785 {
5786 if(curpid->irdeto_curindex != buffer[4]) // old style wrong irdeto index
5787 {
5788 if(curpid->irdeto_curindex == 0xFE) // check if this ecmfilter just started up
5789 {
5790 // on startup set the current index to the irdeto index of the ecm
5791 curpid->irdeto_curindex = buffer[4];
5792 }
5793 else // we are already running and not interested in this ecm
5794 {
5795 if(curpid->table != buffer[0]) // fix for receivers not supporting section filtering
5796 {
5797 curpid->table = 0;
5798 }
5799
5800 // set ecm filter to odd + even since
5801 // this ecm doesn't match with current irdeto index
5802 dvbapi_set_section_filter(demux_id, er, filter_num);
5803
5804 NULLFREE(er);
5805 return;
5806 }
5807 }
5808 else // fix for receivers not supporting section filtering
5809 {
5810 if(curpid->table == buffer[0])
5811 {
5812 NULLFREE(er);
5813 return;
5814 }
5815 }
5816 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",
5817 demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->ecm[4], er->ecm[5], er->chid, curpid->irdeto_cycle, er->vpid);
5818 }
5819 else
5820 {
5821 cs_log_dbg(D_DVBAPI, "Demuxer %d ECMTYPE %02X CAID %04X PROVID %06X ECMPID %04X FAKECHID %04X (unique part in ecm)",
5822 demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->chid);
5823 }
5824
5825 // check for matching chid (unique ecm part in case of non-irdeto cas)
5826 // plus added fix for seca2 monthly changing fakechid
5827 if((curpid->CHID < 0x10000) && !((chid == curpid->CHID) || ((curpid->CAID >> 8 == 0x01) && (chid & 0xF0FF) == (curpid->CHID & 0xF0FF))))
5828 {
5829 if(caid_is_irdeto(curpid->CAID))
5830 {
5831 // if same: we cycled all indexes but no luck!
5832 if((curpid->irdeto_cycle < 0xFE) && (curpid->irdeto_cycle == curpid->irdeto_curindex))
5833 {
5834 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
5835
5836 // forced pid? keep trying the forced ecmpid, no force kill ecm filter
5837 if(!forceentry || !forceentry->force)
5838 {
5839 if(curpid->checked == 2)
5840 {
5841 curpid->checked = 4;
5842 }
5843
5844 if(curpid->checked == 1)
5845 {
5846 curpid->checked = 2;
5847 curpid->CHID = 0x10000;
5848 }
5849
5850 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
5851 NULLFREE(er);
5852 return;
5853 }
5854 }
5855 curpid->irdeto_curindex++; // set check on next index
5856
5857 if(curpid->irdeto_cycle == 0xFE)
5858 {
5859 curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
5860 }
5861
5862 if(curpid->irdeto_curindex > curpid->irdeto_maxindex)
5863 {
5864 curpid->irdeto_curindex = 0; // check if we reached max irdeto index, if so reset to 0
5865 }
5866 curpid->table = 0;
5867
5868 // set ecm filter to odd + even since
5869 // this ecm doesn't match with current irdeto index
5870 dvbapi_set_section_filter(demux_id, er, filter_num);
5871
5872 NULLFREE(er);
5873 return;
5874 }
5875 else // all non irdeto cas systems
5876 {
5877 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
5878 curpid->table = 0;
5879
5880 // set ecm filter to odd + even since
5881 // this ecm doesn't match with current irdeto index
5882 dvbapi_set_section_filter(demux_id, er, filter_num);
5883
5884 if(forceentry && forceentry->force)
5885 {
5886 NULLFREE(er);
5887 return; // forced pid? keep trying the forced ecmpid!
5888 }
5889
5890 if(curpid->checked == 2)
5891 {
5892 curpid->checked = 4;
5893 }
5894
5895 if(curpid->checked == 1)
5896 {
5897 curpid->checked = 2;
5898 curpid->CHID = 0x10000;
5899 }
5900
5901 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
5902 NULLFREE(er);
5903 return;
5904 }
5905 }
5906
5907 struct s_dvbapi_priority *p;
5908 for(p = dvbapi_priority; p != NULL; p = p->next)
5909 {
5910 if(p->type != 'l'
5911 || (p->caid && p->caid != curpid->CAID)
5912 || (p->provid && p->provid != curpid->PROVID)
5913 || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
5914 || (p->srvid && p->srvid != demux[demux_id].program_number))
5915 {
5916 continue;
5917 }
5918
5919 if((uint)p->delay == sctlen && p->force < 6)
5920 {
5921 p->force++;
5922 NULLFREE(er);
5923 return;
5924 }
5925
5926 if(p->force >= 6)
5927 {
5928 p->force = 0;
5929 }
5930 }
5931
5932 if(!curpid->PROVID)
5933 {
5934 curpid->PROVID = chk_provid(buffer, curpid->CAID);
5935 }
5936
5937 if(caid_is_irdeto(curpid->CAID)) // irdeto: wait for the correct index
5938 {
5939 if(buffer[4] != curpid->irdeto_curindex)
5940 {
5941 curpid->table = 0;
5942
5943 // set ecm filter to odd + even since
5944 // this ecm doesn't match with current irdeto index
5945 dvbapi_set_section_filter(demux_id, er, filter_num);
5946
5947 NULLFREE(er);
5948 return;
5949 }
5950 }
5951
5952 // we have an ecm with the correct irdeto index (or fakechid)
5953 for(p = dvbapi_priority; p != NULL ; p = p->next) // check for ignore!
5954 {
5955 if((p->type != 'i')
5956 || (p->caid && p->caid != curpid->CAID)
5957 || (p->provid && p->provid != curpid->PROVID)
5958 || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
5959 || (p->pidx && p->pidx - 1 != pid)
5960 || (p->srvid && p->srvid != demux[demux_id].program_number))
5961 {
5962 continue;
5963 }
5964
5965 // found an ignore chid match with current ecm -> ignoring this irdeto index
5966 if(p->type == 'i' && (p->chid < 0x10000 && p->chid == chid))
5967 {
5968 curpid->irdeto_curindex++;
5969 if(curpid->irdeto_cycle == 0xFE)
5970 {
5971 curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
5972 }
5973
5974 if(curpid->irdeto_curindex > curpid->irdeto_maxindex) // check if curindex is over the max
5975 {
5976 curpid->irdeto_curindex = 0;
5977 }
5978 curpid->table = 0;
5979
5980 // irdeto: wait for the correct index + check if we cycled all
5981 if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_cycle != curpid->irdeto_curindex))
5982 {
5983 // set ecm filter to odd + even since this chid has to be ignored!
5984 dvbapi_set_section_filter(demux_id, er, filter_num);
5985 }
5986 else // this fakechid has to be ignored, kill this filter!
5987 {
5988 if(curpid->checked == 2)
5989 {
5990 curpid->checked = 4;
5991 }
5992
5993 if(curpid->checked == 1)
5994 {
5995 curpid->checked = 2;
5996 curpid->CHID = 0x10000;
5997 }
5998
5999 dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
6000 }
6001 NULLFREE(er);
6002 return;
6003 }
6004 }
6005
6006 if(er)
6007 {
6008 curpid->table = er->ecm[0];
6009 }
6010
6011 request_cw(dvbapi_client, er, demux_id, 1); // register this ecm for delayed ecm response check
6012 return; // end of ecm filterhandling!
6013 }
6014
6015 if(filtertype == TYPE_EMM)
6016 {
6017 if(len != 0) // len = 0 receiver encountered an internal buffer overflow!
6018 {
6019 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched EMM data (length = 0x%03X):",
6020 demux_id, filter_num + 1, sctlen);
6021
6022 if(sctlen > MAX_EMM_SIZE) // emm too long to handle!
6023 {
6024 cs_log_dbg(D_DVBAPI, "Received data with total length 0x%03X but max supported EMM length is 0x%03X -> Please report!",
6025 sctlen, MAX_EMM_SIZE);
6026
6027 return;
6028 }
6029 }
6030 else
6031 {
6032 return; // just skip on internal buffer overflow
6033 }
6034
6035#ifdef WITH_EMU
6036 if(caid_is_director(demux[demux_id].demux_fd[filter_num].caid))
6037 {
6038 uint32_t i;
6039 uint32_t emmhash;
6040
6041 if(sctlen < 4)
6042 {
6043 return;
6044 }
6045
6046 for(i = 0; i + 2 < sctlen; i++)
6047 {
6048 if(buffer[i] == 0xF0 && (buffer[i + 2] == 0xE1 || buffer[i + 2] == 0xE4))
6049 {
6050 emmhash = (buffer[3] << 8) | buffer[sctlen - 2];
6051 if(demux[demux_id].demux_fd[filter_num].cadata == emmhash)
6052 {
6053 return;
6054 }
6055
6056 demux[demux_id].demux_fd[filter_num].cadata = emmhash;
6057 dvbapi_process_emm(demux_id, filter_num, buffer, sctlen);
6058 return;
6059 }
6060 }
6061 return;
6062 }
6063#endif
6064 // fix to handle more than one irdeto emm packet
6065 uint8_t *pbuf = buffer;
6066 int32_t done = 0;
6067 int32_t unhandled = len;
6068
6069 while(len > done)
6070 {
6071 pbuf += done;
6072 sctlen = SCT_LEN(pbuf);
6073
6074 if(unhandled < 4 || (int32_t)sctlen > unhandled || sctlen > MAX_EMM_SIZE || sctlen < 4)
6075 {
6076 break;
6077 }
6078
6079 dvbapi_process_emm(demux_id, filter_num, pbuf, sctlen);
6080 done += sctlen;
6081 unhandled -= sctlen;
6082 }
6083 }
6084
6085 if(filtertype == TYPE_SDT)
6086 {
6087 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched SDT data (length = 0x%03X):",
6088 demux_id, filter_num + 1, sctlen);
6089
6090 dvbapi_parse_sdt(demux_id, buffer, sctlen, msgid);
6091 }
6092
6093 if(filtertype == TYPE_PAT)
6094 {
6095 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched PAT data (length = 0x%03X):",
6096 demux_id, filter_num + 1, sctlen);
6097
6098 dvbapi_parse_pat(demux_id, buffer, sctlen, msgid);
6099 }
6100
6101 if(filtertype == TYPE_PMT)
6102 {
6103 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched PMT data (length = 0x%03X):",
6104 demux_id, filter_num + 1, sctlen);
6105
6106 dvbapi_parse_pmt(demux_id, buffer, sctlen, msgid);
6107 }
6108
6109 if(filtertype == TYPE_CAT)
6110 {
6111 cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d filter %d fetched CAT data (length = 0x%03X):",
6112 demux_id, filter_num + 1, sctlen);
6113
6114 dvbapi_parse_cat(demux_id, buffer, sctlen, msgid);
6115 }
6116}
6117
6118static int32_t dvbapi_recv(int32_t connfd, uint8_t *mbuf, size_t rlen)
6119{
6120 ssize_t len = cs_recv(connfd, mbuf, rlen, MSG_DONTWAIT);
6121
6122 if((len == -1 && (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) || (len == 0))
6123 {
6124 return -1;
6125 }
6126
6127 if(len == -1)
6128 {
6129 return 0;
6130 }
6131
6132 return len;
6133}
6134
6135static uint16_t dvbapi_get_nbof_missing_header_bytes(uint8_t *mbuf, uint16_t mbuf_len, uint32_t msgid_size)
6136{
6137 uint16_t commandsize = 4;
6138 commandsize += msgid_size;
6139 if(mbuf_len < commandsize)
6140 {
6141 return commandsize - mbuf_len;
6142 }
6143 else
6144 {
6145 mbuf += msgid_size;
6146 uint32_t opcode = b2i(4, mbuf);
6147
6148 if((opcode & 0xFFFFF000) == DVBAPI_AOT_CA)
6149 {
6150 if(mbuf[3] & 0x80)
6151 {
6152 uint32_t size = mbuf[3] & 0x7F;
6153 if(mbuf_len < (commandsize + size))
6154 {
6155 return (commandsize + size) - mbuf_len;
6156 }
6157 }
6158 return 0;
6159 }
6160 else
6161 {
6162 switch (opcode)
6163 {
6164 case DVBAPI_FILTER_DATA:
6165 commandsize = 9;
6166 commandsize += msgid_size;
6167 if(mbuf_len < commandsize)
6168 {
6169 return commandsize - mbuf_len;
6170 }
6171 return 0;
6172
6173 case DVBAPI_CLIENT_INFO:
6174 commandsize = 7;
6175 commandsize += msgid_size;
6176 if(mbuf_len < commandsize)
6177 {
6178 return commandsize - mbuf_len;
6179 }
6180 return 0;
6181
6182 default:
6183 return 0;
6184 }
6185 }
6186 }
6187}
6188
6189static void set_chunksize_data_len_to_invalid(uint16_t *chunksize, uint16_t *data_len)
6190{
6191 (*chunksize) = 1;
6192 (*data_len) = 1;
6193}
6194
6195static void log_packeterror(uint16_t mbuf_len, const char* command)
6196{
6197 cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short for %s", mbuf_len, command);
6198}
6199
6200static bool is_commandsize_valid(uint32_t commandsize, uint16_t mbuf_len, const char* command)
6201{
6202 bool isValid = mbuf_len >= commandsize;
6203 if(!isValid)
6204 {
6205 log_packeterror(mbuf_len, command);
6206 }
6207 return isValid;
6208}
6209
6210static uint8_t get_asn1packetsize(uint8_t *mbuf, uint16_t mbuf_len, const char *command, uint32_t *tmp_data_len)
6211{
6212 uint8_t sizebytes = 0;
6213 uint8_t commandsize = 4;
6214 *tmp_data_len = mbuf[3] & 0x7F;
6215 if(mbuf[3] & 0x80)
6216 {
6217 sizebytes = *tmp_data_len;
6218 if(is_commandsize_valid(3 + sizebytes, mbuf_len, command))
6219 {
6220 *tmp_data_len = b2i(sizebytes, mbuf + 4);
6221 }
6222 else
6223 {
6224 return 0;
6225 }
6226 }
6227 return commandsize + sizebytes;
6228}
6229
6230static void dvbapi_get_packet_size(uint8_t *mbuf, uint16_t mbuf_len, uint16_t *chunksize, uint16_t *data_len)
6231{
6232 //chunksize: size of complete chunk in the buffer (an opcode with the data)
6233 //data_len: variable for internal data length (eg. for the filter data size, PMT len)
6234 (*chunksize) = 0;
6235 (*data_len) = 0;
6236
6237 if(mbuf_len < 4)
6238 {
6239 cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short", mbuf_len);
6240 set_chunksize_data_len_to_invalid(chunksize, data_len);
6241 return;
6242 }
6243
6244 int32_t commandsize = 0;
6245 char* command = "DVBAPI_UNKNOWN_COMMAND";
6246 uint32_t tmp_data_len = 0;
6247 uint32_t opcode = b2i(4, mbuf);
6248
6249 switch (opcode)
6250 {
6251 case DVBAPI_AOT_CA_STOP:
6252 {
6253 command = "DVBAPI_AOT_CA_STOP";
6254 commandsize = get_asn1packetsize(mbuf, mbuf_len, command, &tmp_data_len);
6255 break;
6256 }
6257 case DVBAPI_FILTER_DATA:
6258 {
6259 command = "DVBAPI_FILTER_DATA";
6260 commandsize = 9;
6261 if(is_commandsize_valid(commandsize, mbuf_len, command))
6262 {
6263 tmp_data_len = b2i(2, mbuf + 7) & 0x0FFF;
6264 }
6265 break;
6266 }
6267
6268 case DVBAPI_CLIENT_INFO:
6269 {
6270 command = "DVBAPI_CLIENT_INFO";
6271 commandsize = 7;
6272 if(is_commandsize_valid(commandsize, mbuf_len, command))
6273 {
6274 tmp_data_len = mbuf[6];
6275 }
6276 break;
6277 }
6278
6279 default:
6280 {
6281 if((opcode & 0xFFFFFF00) == DVBAPI_AOT_CA_PMT)
6282 {
6283 command = "DVBAPI_AOT_CA_PMT";
6284 commandsize = get_asn1packetsize(mbuf, mbuf_len, command, &tmp_data_len);
6285 break;
6286 }
6287 else
6288 {
6289 cs_log("Unknown socket command received: 0x%08X", opcode);
6290 }
6291 break;
6292 }
6293 }
6294
6295 if(tmp_data_len == 0 || commandsize == 0)
6296 {
6297 set_chunksize_data_len_to_invalid(chunksize, data_len);
6298 return;
6299 }
6300
6301 if(tmp_data_len + commandsize > 0xFFFF)
6302 {
6303 cs_log("This packet is too big: %d bytes => truncated!", tmp_data_len);
6304 tmp_data_len = 0xFFFF - commandsize;
6305 }
6306
6307 (*data_len) = tmp_data_len;
6308 (*chunksize) += commandsize + tmp_data_len;
6309
6310 if(*chunksize > mbuf_len)
6311 {
6312 cs_log_dbg(D_DVBAPI, "This %s packet is incomplete => command length is (%" PRIu16 ")", command, *chunksize);
6313 }
6314 else
6315 {
6316 cs_log_dbg(D_DVBAPI, "This is a %s packet with size %d => lets process it!", command, (*chunksize));
6317 }
6318}
6319
6320static void dvbapi_handlesockmsg(uint8_t *mbuf, uint16_t chunksize, uint16_t data_len, int32_t connfd, uint16_t *client_proto_version)
6321{
6322 uint32_t msgid = 0;
6323 if(*client_proto_version >= 3)
6324 {
6325 if(mbuf[0] != 0xa5)
6326 {
6327 cs_log("Error: network packet malformed! (no start)");
6328 return;
6329 }
6330 msgid = b2i(4, mbuf + 1);
6331 mbuf += 5;
6332 }
6333
6334 uint32_t opcode = b2i(4, mbuf);
6335 switch(opcode)
6336 {
6337 case DVBAPI_FILTER_DATA:
6338 {
6339 int32_t demux_id = mbuf[4];
6340 int32_t filter_num = mbuf[5];
6341
6342 if(demux_id < 0 || demux_id >= MAX_DEMUX)
6343 {
6344 cs_log("dvbapi_handlesockmsg(): error - received invalid demux_id (%d)", demux_id);
6345 break;
6346 }
6347
6348 if(filter_num < 0 || filter_num >= MAX_FILTER)
6349 {
6350 cs_log("dvbapi_handlesockmsg(): error - received invalid filter_num (%d)", filter_num);
6351 break;
6352 }
6353 dvbapi_process_input(demux_id, filter_num, mbuf + 6, data_len + 3, msgid);
6354 break;
6355 }
6356
6357 case DVBAPI_CLIENT_INFO:
6358 {
6359 uint16_t client_proto = b2i(2, mbuf + 4);
6360 NULLFREE(last_client_name);
6361
6362 if(cs_malloc(&last_client_name, data_len + 1))
6363 {
6364 memcpy(last_client_name, &mbuf[7], data_len);
6365 last_client_name[data_len] = 0;
6366 cs_log("Client connected: '%s' (protocol version = %" PRIu16 ")", last_client_name, client_proto);
6367 }
6368 dvbapi_net_send(DVBAPI_SERVER_INFO, connfd, msgid, -1, -1, NULL, NULL, NULL, client_proto);
6369
6370 // now the protocol handshake is complete set correct version so all further packets are sent with correct message id.
6371 (*client_proto_version) = client_proto;
6372
6373 // setting the global var according to the client
6374 last_client_proto_version = client_proto;
6375 break;
6376 }
6377
6378 case DVBAPI_AOT_CA_PMT:
6379 {
6380 cs_log_dbg(D_DVBAPI,"Received DVBAPI_AOT_CA_PMT object on socket %d:", connfd);
6381 dvbapi_parse_capmt(mbuf + (chunksize - data_len), data_len, connfd, NULL, *client_proto_version, msgid);
6382 break;
6383 }
6384
6385 case DVBAPI_AOT_CA_STOP:
6386 {
6387 cs_log_dbg(D_DVBAPI, "Received DVBAPI_AOT_CA_STOP object on socket %d:", connfd);
6388 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_listenport)
6389 {
6390 int32_t i;
6391 int32_t demux_index = mbuf[7];
6392 for(i = 0; i < MAX_DEMUX; i++)
6393 {
6394 // 0xff demux_index is a wildcard => close all related demuxers
6395 if(demux_index == 0xff)
6396 {
6397 if(demux[i].socket_fd == connfd)
6398 {
6399 dvbapi_stop_descrambling(i, msgid);
6400 }
6401 }
6402 else if(demux[i].demux_index == demux_index)
6403 {
6404 dvbapi_stop_descrambling(i, msgid);
6405 break;
6406 }
6407 }
6408
6409 // ipbox fix
6410 if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX)
6411 {
6412 // check do we have any demux running on this fd
6413 int16_t execlose = 1;
6414 for(i = 0; i < MAX_DEMUX; i++)
6415 {
6416 if(demux[i].socket_fd == connfd)
6417 {
6418 execlose = 0;
6419 break;
6420 }
6421 }
6422 if(execlose)
6423 {
6424 int32_t ret = close(connfd);
6425 if(ret < 0)
6426 {
6427 cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
6428 }
6429 }
6430 }
6431 }
6432 else if(cfg.dvbapi_pmtmode != 6)
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 break;
6441 }
6442 default:
6443 {
6444 if((opcode & 0xFFFFFF00) == DVBAPI_AOT_CA_PMT)
6445 {
6446 cs_log_dbg(D_DVBAPI, "Received DVBAPI_AOT_CA_PMT object on socket %d:", connfd);
6447 dvbapi_parse_capmt(mbuf + (chunksize - data_len), data_len, connfd, NULL, *client_proto_version, msgid);
6448 }
6449 else
6450 {
6451 cs_log("Unknown socket command received: 0x%08X", opcode);
6452 }
6453 break;
6454 }
6455 }
6456}
6457
6458static 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)
6459{
6460 int32_t recv_result;
6461 uint16_t chunksize = 1, data_len = 1;
6462 uint8_t packet_count = 1;
6463 uint32_t msgid_size = 0;
6464 uint16_t missing_header_bytes = 0;
6465 if(*client_proto_version >= 3)
6466 {
6467 msgid_size = 5;
6468 }
6469
6470 do
6471 {
6472 missing_header_bytes = dvbapi_get_nbof_missing_header_bytes(mbuf, unhandled_len, msgid_size);
6473
6474 if(missing_header_bytes != 0)
6475 {
6476 // read first few bytes so we know packet type and length
6477 cs_log_dbg(D_TRACE, "%s reading %" PRIu16 " bytes from connection fd %d", (unhandled_len == 0) ? "Try" : "Continue", missing_header_bytes, connfd);
6478
6479 recv_result = dvbapi_recv(connfd, mbuf + unhandled_len, mbuf_size - unhandled_len);
6480 if(recv_result < 1)
6481 {
6482 (*new_unhandled_len) = unhandled_len;
6483 return (recv_result != -1);
6484 }
6485 else
6486 {
6487 unhandled_len += recv_result;
6488 if(unhandled_len < missing_header_bytes)
6489 {
6490 (*new_unhandled_len) = unhandled_len;
6491 return true;
6492 }
6493 }
6494 }
6495
6496 cs_log_dump_dbg(D_DVBAPI, mbuf, unhandled_len, "Got packetdata (msgid size: %d, clientprotocol: %d)", msgid_size, *client_proto_version);
6497 dvbapi_get_packet_size(mbuf+msgid_size, unhandled_len-msgid_size, &chunksize, &data_len);
6498
6499 chunksize+=msgid_size;
6500 if(chunksize > mbuf_size)
6501 {
6502 cs_log("***** WARNING: SOCKET DATA BUFFER OVERFLOW (%" PRIu16 " bytes), PLEASE REPORT! ****** ", chunksize);
6503 (*new_unhandled_len) = 0;
6504 return true;
6505 }
6506
6507 if(unhandled_len < chunksize) // we are missing some bytes, try to read them
6508 {
6509 cs_log_dbg(D_TRACE, "Continue to read the missing %d bytes from connection fd %d", chunksize - unhandled_len, connfd);
6510 recv_result = dvbapi_recv(connfd, mbuf + unhandled_len, mbuf_size - unhandled_len);
6511 if(recv_result < 1)
6512 {
6513 (*new_unhandled_len) = unhandled_len;
6514 return (recv_result != -1);
6515 }
6516 else
6517 {
6518 unhandled_len += recv_result;
6519 if(unhandled_len < chunksize)
6520 {
6521 (*new_unhandled_len) = unhandled_len;
6522 return true;
6523 }
6524 }
6525 }
6526
6527 dvbapi_handlesockmsg(mbuf, chunksize-msgid_size, data_len, connfd, client_proto_version);
6528
6529 unhandled_len -= chunksize;
6530 if(unhandled_len > 0)
6531 {
6532 memmove(mbuf, mbuf + chunksize, unhandled_len);
6533 }
6534 } while(unhandled_len != 0 && packet_count++ < 8);
6535
6536 cs_log_dbg(D_DVBAPI, "Processing socketdata completed after %d packets with %d bytes left unprocessed", packet_count, unhandled_len);
6537
6538 (*new_unhandled_len) = unhandled_len;
6539 return true;
6540}
6541
6542static void add_to_assoc_fd(int sock)
6543{
6544 uint i;
6545
6546 for(i = 0; i < MAX_ASSOC_FD; i++)
6547 {
6548 if(assoc_fd[i] == sock)
6549 {
6550 return; // do not add twice
6551 }
6552 }
6553
6554 for(i = 0; i < MAX_ASSOC_FD; i++)
6555 {
6556 if(!assoc_fd[i])
6557 {
6558 assoc_fd[i] = sock;
6559 return;
6560 }
6561 }
6562}
6563
6564static void del_from_assoc_fd(int sock)
6565{
6566 uint i;
6567
6568 for(i = 0; i < MAX_ASSOC_FD; i++)
6569 {
6570 if(assoc_fd[i] == sock)
6571 {
6572 assoc_fd[i] = 0;
6573 }
6574 }
6575}
6576
6577static void *dvbapi_main_local(void *cli)
6578{
6579 int32_t i, j, l;
6580 struct s_client *client = (struct s_client *)cli;
6581 client->thread = pthread_self();
6582 SAFE_SETSPECIFIC(getclient, cli);
6583 dvbapi_client = cli;
6584 int32_t maxpfdsize = (MAX_DEMUX * maxfilter) + MAX_DEMUX + 2;
6585 struct pollfd pfd2[maxpfdsize];
6586 struct timeb start, end; // start time poll, end time poll
6587#define PMT_SERVER_SOCKET "/tmp/.listen.camd.socket"
6588 struct sockaddr_un saddr;
6589 saddr.sun_family = AF_UNIX;
6590 cs_strncpy(saddr.sun_path, PMT_SERVER_SOCKET, sizeof(saddr.sun_path));
6591 int32_t rc, pfdcount, g, connfd, clilen;
6592 int32_t ids[maxpfdsize], fdn[maxpfdsize], type[maxpfdsize];
6593 struct SOCKADDR servaddr;
6594 ssize_t len = 0;
6595 static const uint16_t mbuf_size = 2048;
6596 uint8_t *mbuf;
6597 uint16_t unhandled_buf_len[maxpfdsize], unhandled_buf_used[maxpfdsize];
6598 uint8_t *unhandled_buf[maxpfdsize];
6599 struct s_auth *account;
6600 int32_t ok = 0;
6601 uint16_t client_proto_version[maxpfdsize];
6602
6603 if(!cs_malloc(&mbuf, sizeof(uint8_t) * mbuf_size))
6604 {
6605 return NULL;
6606 }
6607
6608 for(i = 0; i < maxpfdsize; i++)
6609 {
6610 unhandled_buf[i] = NULL;
6611 unhandled_buf_len[i] = 0;
6612 unhandled_buf_used[i] = 0;
6613 client_proto_version[i] = 0;
6614 }
6615
6616 for(account = cfg.account; account != NULL; account = account->next)
6617 {
6618 if((ok = is_dvbapi_usr(account->usr)))
6619 {
6620 break;
6621 }
6622 }
6623
6624 cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
6625 memset(demux, 0, sizeof(demux));
6626
6627 for(i = 0; i < MAX_DEMUX; i++)
6628 {
6629 SAFE_MUTEX_INIT(&demux[i].answerlock, NULL);
6630 for(j = 0; j < MAX_ECM_PIDS; j++)
6631 {
6632 for(l = 0; l < MAX_STREAM_INDICES; l++)
6633 {
6634 demux[i].ECMpids[j].index[l] = INDEX_INVALID;
6635 }
6636 }
6637 demux[i].pidindex = -1;
6638 demux[i].curindex = -1;
6639 }
6640
6641 memset(ca_fd, 0, sizeof(ca_fd));
6642 memset(assoc_fd, 0, sizeof(assoc_fd));
6643 dvbapi_read_priority();
6644 dvbapi_load_channel_cache();
6645 dvbapi_detect_api();
6646
6647 if(selected_box == -1 || selected_api == -1)
6648 {
6649 cs_log("ERROR: Could not detect DVBAPI version.");
6650 free(mbuf);
6651 return NULL;
6652 }
6653
6654 // detect box type first and then get descrambler info
6655 dvbapi_get_descrambler_info();
6656
6657 if(cfg.dvbapi_pmtmode == 1)
6658 {
6659 disable_pmt_files = 1;
6660 }
6661
6662 int32_t listenfd = -1;
6663 if(cfg.dvbapi_boxtype != BOXTYPE_IPBOX_PMT &&
6664 cfg.dvbapi_pmtmode != 2 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
6665 {
6666 if(!cfg.dvbapi_listenport)
6667 {
6668 listenfd = dvbapi_init_listenfd();
6669 }
6670 else
6671 {
6672 listenfd = dvbapi_net_init_listenfd();
6673 }
6674
6675 if(listenfd < 1)
6676 {
6677 cs_log("ERROR: Could not init socket: (errno=%d: %s)", errno, strerror(errno));
6678 free(mbuf);
6679 return NULL;
6680 }
6681 }
6682
6683 for(i = 0; i < MAX_DEMUX; i++) // init all demuxers!
6684 {
6685 demux[i].pidindex = -1;
6686 demux[i].curindex = -1;
6687 }
6688
6689 if(cfg.dvbapi_pmtmode != 4 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
6690 {
6691 struct sigaction signal_action;
6692 signal_action.sa_handler = event_handler;
6693 sigemptyset(&signal_action.sa_mask);
6694 signal_action.sa_flags = SA_RESTART;
6695 sigaction(SIGRTMIN + 1, &signal_action, NULL);
6696
6697 dir_fd = open(TMPDIR, O_RDONLY);
6698 if(dir_fd >= 0)
6699 {
6700 fcntl(dir_fd, F_SETSIG, SIGRTMIN + 1);
6701 fcntl(dir_fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_DELETE | DN_MULTISHOT);
6702 event_handler(SIGRTMIN + 1);
6703 }
6704 }
6705 else
6706 {
6707 int32_t ret = start_thread("dvbapi event", dvbapi_event_thread, (void *) dvbapi_client, NULL, 1, 0);
6708 if(ret)
6709 {
6710 free(mbuf);
6711 return NULL;
6712 }
6713 }
6714
6715 if(listenfd != -1)
6716 {
6717 pfd2[0].fd = listenfd;
6718 pfd2[0].events = (POLLIN | POLLPRI);
6719 type[0] = 1;
6720 }
6721
6722#if defined WITH_COOLAPI || defined WITH_COOLAPI2 || defined WITH_NEUTRINO
6723 system("pzapit -rz");
6724#endif
6725 cs_ftime(&start); // register start time
6726
6727 while(!exit_oscam)
6728 {
6729 if(pausecam) // for dbox2, STAPI or PC in standby mode don't parse any ecm/emm or try to start next filter
6730 {
6731 continue;
6732 }
6733
6734 if(cfg.dvbapi_pmtmode == 6)
6735 {
6736 if(listenfd < 0)
6737 {
6738 cs_log("PMT mode 6: Connecting to enigma CA PMT listen socket...");
6739
6740 // socket init
6741 if((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
6742 {
6743 cs_log("socket error (errno=%d %s)", errno, strerror(errno));
6744 listenfd = -1;
6745 }
6746 else if(connect(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
6747 {
6748 cs_log("socket connect error (errno=%d %s)", errno, strerror(errno));
6749 close(listenfd);
6750 listenfd = -1;
6751 }
6752 else
6753 {
6754 pfd2[0].fd = listenfd;
6755 pfd2[0].events = (POLLIN | POLLPRI);
6756 type[0] = 1;
6757 cs_log("PMT mode 6: Successfully connected to CA PMT server (fd %d)", listenfd);
6758 }
6759 }
6760
6761 if(listenfd == -1) // not connected!
6762 {
6763 cs_sleepms(1000);
6764 continue; // start fresh connect attempt!
6765 }
6766 }
6767
6768 pfdcount = (listenfd > -1) ? 1 : 0;
6769
6770 for(i = 0; i < MAX_ASSOC_FD; i++) // add all associated fds (this should include also demux[X].socket_fd)
6771 {
6772 if(assoc_fd[i])
6773 {
6774 pfd2[pfdcount].fd = assoc_fd[i];
6775 pfd2[pfdcount].events = (POLLIN | POLLPRI);
6776 client_proto_version[pfdcount] = last_client_proto_version;
6777 type[pfdcount++] = 1;
6778 }
6779 }
6780
6781 for(i = 0; i < MAX_DEMUX; i++)
6782 {
6783 if(demux[i].program_number == 0)
6784 {
6785 continue; // only evalutate demuxers that have channels assigned
6786 }
6787
6788 uint32_t ecmcounter = 0, emmcounter = 0;
6789 for(g = 0; g < maxfilter; g++)
6790 {
6791 if(demux[i].demux_fd[g].fd <= 0)
6792 {
6793 continue; // deny obvious invalid fd!
6794 }
6795
6796 if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX
6797 && selected_api != STAPI && selected_api != COOLAPI)
6798 {
6799 pfd2[pfdcount].fd = demux[i].demux_fd[g].fd;
6800 pfd2[pfdcount].events = (POLLIN | POLLPRI);
6801 ids[pfdcount] = i;
6802 fdn[pfdcount] = g;
6803 type[pfdcount++] = 0;
6804 }
6805
6806 // count ecm filters to see if demuxing is possible anyway
6807 if(demux[i].demux_fd[g].type == TYPE_ECM)
6808 {
6809 ecmcounter++;
6810 }
6811
6812 // count emm filters also
6813 if(demux[i].demux_fd[g].type == TYPE_EMM)
6814 {
6815 emmcounter++;
6816 }
6817 }
6818
6819 // only produce log if something changed
6820 if(ecmcounter != demux[i].old_ecmfiltercount || emmcounter != demux[i].old_emmfiltercount)
6821 {
6822 cs_log_dbg(D_DVBAPI, "Demuxer %d has %d ecmpids, %d streampids, %d ecmfilters and %d of max %d emmfilters",
6823 i, demux[i].ECMpidcount, demux[i].STREAMpidcount, ecmcounter, emmcounter, demux[i].max_emm_filter);
6824
6825 demux[i].old_ecmfiltercount = ecmcounter; // save new amount of ecm filters
6826 demux[i].old_emmfiltercount = emmcounter; // save new amount of emm filters
6827 }
6828
6829 // delayed emm start for non irdeto caids,
6830 // start emm cat if not already done for this demuxer!
6831 struct timeb now;
6832 cs_ftime(&now);
6833 int64_t gone;
6834 int8_t do_emm_start = (cfg.dvbapi_au > 0 && demux[i].emm_filter == -1 && demux[i].EMMpidcount == 0 && emmcounter == 0);
6835 int8_t do_sdt_start = (cfg.dvbapi_read_sdt && demux[i].sdt_filter == -1 && cfg.dvbapi_boxtype != BOXTYPE_SAMYGO);
6836
6837 if(do_emm_start || do_sdt_start)
6838 {
6839 gone = comp_timeb(&now, &demux[i].emmstart);
6840 if(gone > 20 * 1000)
6841 {
6842 if(do_emm_start)
6843 {
6844 cs_ftime(&demux[i].emmstart); // trick to let emm fetching start after 30 seconds to speed up zapping
6845 dvbapi_start_cat_filter(i);
6846 }
6847 }
6848
6849 if(gone > 5 * 1000)
6850 {
6851 if(do_sdt_start)
6852 {
6853 dvbapi_start_sdt_filter(i);
6854 }
6855 }
6856 }
6857
6858 // early start for irdeto since they need emm before ecm
6859 // (pmt emmstart = 1 if detected caid 0x06)
6860 int32_t emmstarted = demux[i].emm_filter;
6861
6862 // check every time since share readers might
6863 // give us new filters due to hexserial change
6864 if(cfg.dvbapi_au && demux[i].EMMpidcount > 0)
6865 {
6866 if(!emmcounter && emmstarted == -1)
6867 {
6868 demux[i].emmstart = now;
6869 dvbapi_start_emm_filter(i); // start emm filtering if emm pids are found
6870 }
6871 else
6872 {
6873 gone = comp_timeb(&now, &demux[i].emmstart);
6874 if(gone > 30 * 1000)
6875 {
6876 demux[i].emmstart = now;
6877 dvbapi_start_emm_filter(i); // start emm filtering delayed if filters already were running
6878 rotate_emmfilter(i); // rotate active emm filters
6879 }
6880 }
6881 }
6882
6883 // Restart decoding all caids we have ecmpids but no ecm filters!
6884 if(ecmcounter == 0 && demux[i].ECMpidcount > 0)
6885 {
6886 int32_t started = 0;
6887
6888 // avoid race: not all pids are asked and checked out yet!
6889 for(g = 0; g < demux[i].ECMpidcount; g++)
6890 {
6891 // check if prio run is done
6892 if(demux[i].ECMpids[g].checked == 0 && demux[i].ECMpids[g].status >= 0)
6893 {
6894 dvbapi_try_next_caid(i, 0, 0); // not done, so start next prio pid
6895 started = 1;
6896 break;
6897 }
6898 }
6899
6900 if(started)
6901 {
6902 continue; // if started a filter proceed with next demuxer
6903 }
6904
6905 // all usable pids (with prio) are tried, lets start over again without prio!
6906 if(g == demux[i].ECMpidcount)
6907 {
6908 // avoid race: not all pids are asked and checked out yet!
6909 for(g = 0; g < demux[i].ECMpidcount; g++)
6910 {
6911 // check if noprio run is done
6912 if(demux[i].ECMpids[g].checked == 2 && demux[i].ECMpids[g].status >= 0)
6913 {
6914 demux[i].ECMpids[g].irdeto_curindex = 0xFE;
6915 demux[i].ECMpids[g].irdeto_maxindex = 0;
6916 demux[i].ECMpids[g].irdeto_cycle = 0xFE;
6917 demux[i].ECMpids[g].tries = 0xFE;
6918 demux[i].ECMpids[g].table = 0;
6919 demux[i].ECMpids[g].CHID = 0x10000; // remove chid prio
6920
6921 dvbapi_try_next_caid(i, 2, 0); // not done, so start next no prio pid
6922 started = 1;
6923 break;
6924 }
6925 }
6926 }
6927
6928 if(started)
6929 {
6930 continue; // if started a filter proceed with next demuxer
6931 }
6932
6933 if(g == demux[i].ECMpidcount) // all usable pids are tried, lets start over again!
6934 {
6935 if(demux[i].decodingtries == -1) // first redecoding attempt?
6936 {
6937 cs_ftime(&demux[i].decstart);
6938
6939 // re-init some used things from second run (without prio)
6940 for(g = 0; g < demux[i].ECMpidcount; g++)
6941 {
6942 demux[i].ECMpids[g].checked = 0;
6943 demux[i].ECMpids[g].irdeto_curindex = 0xFE;
6944 demux[i].ECMpids[g].irdeto_maxindex = 0;
6945 demux[i].ECMpids[g].irdeto_cycle = 0xFE;
6946 demux[i].ECMpids[g].table = 0;
6947 demux[i].decodingtries = 0;
6948
6949 // remove this pid from channel cache since we had no founds on any ecmpid!
6950 dvbapi_edit_channel_cache(i, g, 0);
6951 }
6952 }
6953
6954 uint8_t number_of_enabled_pids = 0;
6955 demux[i].decodingtries++;
6956 dvbapi_resort_ecmpids(i);
6957
6958 for(g = 0; g < demux[i].ECMpidcount; g++) // count number of enabled pids!
6959 {
6960 if(demux[i].ECMpids[g].status >= 0) number_of_enabled_pids++;
6961 }
6962
6963 if(!number_of_enabled_pids)
6964 {
6965 if(demux[i].decodingtries == 10)
6966 {
6967 demux[i].decodingtries = 0;
6968 cs_log("Demuxer %d no enabled matching ecmpids -> decoding is waiting for matching readers!",i);
6969 }
6970 }
6971 else
6972 {
6973 cs_ftime(&demux[i].decend);
6974 demux[i].decodingtries = -1; // reset to first run again!
6975 gone = comp_timeb(&demux[i].decend, &demux[i].decstart);
6976
6977 cs_log("Demuxer %d restarting decoding requests after %"PRId64" ms with %d enabled and %d disabled ecmpids!",
6978 i, gone, number_of_enabled_pids, (demux[i].ECMpidcount-number_of_enabled_pids));
6979
6980 dvbapi_try_next_caid(i, 0, 0);
6981 }
6982 }
6983 }
6984 }
6985
6986 rc = 0;
6987 while(!(listenfd == -1 && cfg.dvbapi_pmtmode == 6))
6988 {
6989 rc = poll(pfd2, pfdcount, 500);
6990 if(rc < 0) // error occured while polling for fd's with fresh data
6991 {
6992 if(errno == EINTR || errno == EAGAIN) // try again in case of interrupt
6993 {
6994 continue;
6995 }
6996 cs_log("ERROR: error on poll of %d fd's (errno=%d %s)", pfdcount, errno, strerror(errno));
6997 break;
6998 }
6999 else
7000 {
7001 break;
7002 }
7003 }
7004
7005 if(rc > 0)
7006 {
7007 cs_ftime(&end); // register end time
7008 int64_t timeout = comp_timeb(&end, &start);
7009 if(timeout < 0)
7010 {
7011 cs_log("*** WARNING: BAD TIME AFFECTING WHOLE OSCAM ECM HANDLING ****");
7012 }
7013 cs_log_dbg(D_TRACE, "New events occurred on %d of %d handlers after %"PRId64" ms inactivity", rc, pfdcount, timeout);
7014 cs_ftime(&start); // register new start time for next poll
7015 }
7016
7017 for(i = 0; i < pfdcount && rc > 0; i++)
7018 {
7019 if(pfd2[i].revents == 0) { continue; } // skip sockets with no changes
7020 rc--; //event handled!
7021 cs_log_dbg(D_TRACE, "Now handling fd %d that reported event %d", pfd2[i].fd, pfd2[i].revents);
7022
7023 if(pfd2[i].revents & (POLLHUP | POLLNVAL | POLLERR))
7024 {
7025 if(type[i] == 1)
7026 {
7027 del_from_assoc_fd(pfd2[i].fd);
7028
7029 for(j = 0; j < MAX_DEMUX; j++)
7030 {
7031 // if listenfd closes stop all assigned decoding!
7032 if(demux[j].socket_fd == pfd2[i].fd)
7033 {
7034 dvbapi_stop_descrambling(j, 0);
7035 }
7036 }
7037
7038 int32_t ret = close(pfd2[i].fd);
7039 if(ret < 0 && errno != 9)
7040 {
7041 cs_log("ERROR: Could not close demuxer socket fd (errno=%d %s)", errno, strerror(errno));
7042 }
7043
7044 if(pfd2[i].fd == listenfd && cfg.dvbapi_pmtmode == 6)
7045 {
7046 listenfd = -1;
7047 }
7048 cs_log_dbg(D_DVBAPI, "Socket %d reported hard connection close", pfd2[i].fd);
7049 }
7050 else // type = 0
7051 {
7052 int32_t demux_id = ids[i];
7053 int32_t n = fdn[i];
7054
7055 if(cfg.dvbapi_boxtype != BOXTYPE_SAMYGO)
7056 {
7057 // stop filter since its giving errors and wont return anything good
7058 dvbapi_stop_filternum(demux_id, n, 0);
7059 }
7060 else
7061 {
7062 int32_t ret, pid;
7063 uint8_t filter[32];
7064 struct dmx_sct_filter_params sFP;
7065 cs_log_dbg(D_DVBAPI, "re-opening connection to demux socket");
7066 close(demux[demux_id].demux_fd[n].fd);
7067 demux[demux_id].demux_fd[n].fd = -1;
7068
7069 ret = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
7070 if(ret != -1)
7071 {
7072 demux[demux_id].demux_fd[n].fd = ret;
7073 pid = demux[demux_id].curindex;
7074 memset(filter, 0, 32);
7075 memset(&sFP, 0, sizeof(sFP));
7076 filter[0] = 0x80;
7077 filter[16] = 0xF0;
7078 sFP.pid = demux[demux_id].ECMpids[pid].ECM_PID;
7079 sFP.timeout = 3000;
7080 sFP.flags = DMX_IMMEDIATE_START;
7081 memcpy(sFP.filter.filter, filter, 16);
7082 memcpy(sFP.filter.mask, filter + 16, 16);
7083 ret = dvbapi_ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER, &sFP);
7084 }
7085
7086 if(ret == -1)
7087 {
7088 // stop filter since it's giving errors and wont return anything good
7089 dvbapi_stop_filternum(demux_id, n, 0);
7090 }
7091 }
7092 }
7093 continue; // continue with other events
7094 }
7095
7096 if(pfd2[i].revents & (POLLIN | POLLPRI))
7097 {
7098 if(type[i] == 1)
7099 {
7100 if(pfd2[i].fd == listenfd)
7101 {
7102 if(cfg.dvbapi_pmtmode == 6)
7103 {
7104 connfd = listenfd;
7105 disable_pmt_files = 1;
7106 }
7107 else
7108 {
7109 clilen = sizeof(servaddr);
7110 connfd = accept(listenfd, (struct sockaddr *)&servaddr, (socklen_t *)&clilen);
7111 cs_log_dbg(D_DVBAPI, "new socket connection fd: %d", connfd);
7112
7113 if(cfg.dvbapi_listenport)
7114 {
7115 // update webif data
7116 client->ip = SIN_GET_ADDR(servaddr);
7117 client->port = ntohs(SIN_GET_PORT(servaddr));
7118 }
7119
7120 add_to_assoc_fd(connfd);
7121
7122 if(cfg.dvbapi_pmtmode == 3 || cfg.dvbapi_pmtmode == 0)
7123 {
7124 disable_pmt_files = 1;
7125 }
7126
7127 if(connfd <= 0)
7128 {
7129 cs_log_dbg(D_DVBAPI, "accept() returns error on fd event %d (errno=%d %s)",
7130 pfd2[i].revents, errno, strerror(errno));
7131 }
7132 }
7133 }
7134 else
7135 {
7136 connfd = pfd2[i].fd;
7137 }
7138
7139 //reading and completing data from socket
7140 if(connfd > 0)
7141 {
7142 if(unhandled_buf_used[i])
7143 {
7144 memcpy(mbuf, unhandled_buf[i], unhandled_buf_used[i]);
7145 }
7146
7147 if(!dvbapi_handlesockdata(connfd, mbuf, mbuf_size, unhandled_buf_used[i], &unhandled_buf_used[i], &client_proto_version[i]))
7148 {
7149 unhandled_buf_used[i] = 0;
7150 client_proto_version[i] = 0; // reset protocol, next client could old protocol.
7151 last_client_proto_version = 0;
7152 // client disconnects, stop all assigned decoding
7153 cs_log_dbg(D_DVBAPI, "Socket %d reported connection close", connfd);
7154 int active_conn = 0; // other active connections counter
7155
7156 for(j = 0; j < MAX_DEMUX; j++)
7157 {
7158 if(demux[j].socket_fd == connfd)
7159 {
7160 dvbapi_stop_descrambling(j, 0);
7161 }
7162 else if(demux[j].socket_fd)
7163 {
7164 active_conn++;
7165 }
7166 }
7167
7168 // stop polling on this socket
7169 del_from_assoc_fd(connfd);
7170 close(connfd);
7171
7172 // last connection closed
7173 if(!active_conn && (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX))
7174 {
7175 if(cfg.dvbapi_listenport)
7176 {
7177 // update webif data
7178 client->ip = get_null_ip();
7179 client->port = 0;
7180 }
7181 }
7182 continue;
7183 }
7184
7185 if(unhandled_buf_used[i])
7186 {
7187 if(unhandled_buf_used[i] > unhandled_buf_len[i])
7188 {
7189 NULLFREE(unhandled_buf[i]);
7190 unhandled_buf_len[i] = unhandled_buf_used[i] < 128 ? 128 : unhandled_buf_used[i];
7191 if(!cs_malloc(&unhandled_buf[i], sizeof(uint8_t) * unhandled_buf_len[i]))
7192 {
7193 unhandled_buf_len[i] = 0;
7194 unhandled_buf_used[i] = 0;
7195 continue;
7196 }
7197 }
7198 memcpy(unhandled_buf[i], mbuf, unhandled_buf_used[i]);
7199 }
7200 }
7201 }
7202 else // type == 0
7203 {
7204 int32_t demux_id = ids[i];
7205 int32_t n = fdn[i];
7206
7207 if((int)demux[demux_id].demux_fd[n].fd != pfd2[i].fd)
7208 {
7209 continue; // filter already killed, no need to process this data!
7210 }
7211
7212 len = dvbapi_read_device(pfd2[i].fd, mbuf, mbuf_size);
7213 if(len < 0) // serious filterdata read error
7214 {
7215 // stop filter since it's giving errors and won't return anything good
7216 dvbapi_stop_filternum(demux_id, n, 0);
7217
7218 maxfilter--; // lower maxfilters to avoid this with new filter setups!
7219 continue;
7220 }
7221
7222 if(!len) // receiver internal filter buffer overflow
7223 {
7224 memset(mbuf, 0, mbuf_size);
7225 }
7226 dvbapi_process_input(demux_id, n, mbuf, len, 0);
7227 }
7228 continue; // continue with other events!
7229 }
7230 }
7231 }
7232
7233 for(j = 0; j < maxpfdsize; j++)
7234 {
7235 NULLFREE(unhandled_buf[j]);
7236 }
7237 free(mbuf);
7238
7239 return NULL;
7240}
7241
7242void dvbapi_write_cw(int32_t demux_id, int32_t pid, int32_t stream_id, uint8_t *cw, uint8_t cw_length, uint8_t *iv,
7243 uint8_t iv_length, enum ca_descr_algo algo, enum ca_descr_cipher_mode cipher_mode, uint32_t msgid)
7244{
7245 int8_t n, cw_empty = 0;
7246 uint8_t null_cw[cw_length];
7247 ca_descr_t ca_descr;
7248 ca_descr_mode_t ca_descr_mode;
7249 ca_descr_data_t ca_descr_data;
7250
7251 memset(null_cw, 0, cw_length);
7252 memset(&ca_descr, 0, sizeof(ca_descr));
7253 memset(&ca_descr_mode, 0, sizeof(ca_descr_mode));
7254 memset(&ca_descr_data, 0, sizeof(ca_descr_data));
7255
7256 if(memcmp(demux[demux_id].last_cw[stream_id][0], null_cw, cw_length) == 0
7257 && memcmp(demux[demux_id].last_cw[stream_id][1], null_cw, cw_length) == 0)
7258 {
7259 cw_empty = 1; // to make sure that both cws get written on constantcw
7260 }
7261
7262 for(n = 0; n < 2; n++)
7263 {
7264 // Check if cw has changed and if new cw is empty (all zeros)
7265 // Skip check for BISS1 - cw could be indeed zero
7266 // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
7267 if((memcmp(cw + (n * cw_length), demux[demux_id].last_cw[stream_id][n], cw_length) != 0 || cw_empty)
7268 && (memcmp(cw + (n * cw_length), null_cw, cw_length) != 0 || caid_is_biss(demux[demux_id].ECMpids[pid].CAID)))
7269 {
7270 // prepare ca device
7271 uint32_t idx = dvbapi_ca_set_pid(demux_id, pid, stream_id, (algo == CA_ALGO_DES), msgid);
7272 if(idx == INDEX_INVALID)
7273 {
7274 return; // return on no index!
7275 }
7276
7277#if defined WITH_COOLAPI || defined WITH_COOLAPI2
7278 ca_descr.index = idx;
7279 ca_descr.parity = n;
7280
7281 // just to make the compiler happy (-Wunused-parameter)
7282 // (better move the coolapi code to a separate function)
7283 ca_descr_mode.cipher_mode = cipher_mode;
7284 ca_descr_data.data = iv;
7285 ca_descr_data.length = iv_length;
7286
7287 memcpy(demux[demux_id].last_cw[stream_id][n], cw + (n * 8), 8);
7288 memcpy(ca_descr.cw, cw + (n * 8), 8);
7289
7290 cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca_mask %d)",
7291 demux_id, n, ca_descr.index, demux[demux_id].ca_mask);
7292
7293 coolapi_write_cw(demux[demux_id].ca_mask, demux[demux_id].STREAMpids, demux[demux_id].STREAMpidcount, &ca_descr);
7294#else
7295 int32_t i, j, write_cw = 0;
7296 uint32_t usedidx, lastidx;
7297
7298 char lastcw[2 * cw_length + 1];
7299 char newcw[2 * cw_length + 1];
7300#ifdef WITH_DEBUG
7301 if(cs_dblevel & D_DVBAPI)
7302 {
7303 cs_hexdump(0, demux[demux_id].last_cw[stream_id][n], cw_length, lastcw, sizeof(lastcw));
7304 cs_hexdump(0, cw + (n * cw_length), cw_length, newcw, sizeof(newcw));
7305 }
7306#endif
7307
7308 for(i = 0; i < CA_MAX; i++)
7309 {
7310 if(!(demux[demux_id].ca_mask & (1 << i)))
7311 {
7312 continue; // ca not in use by this demuxer!
7313 }
7314 lastidx = INDEX_INVALID;
7315
7316 for(j = 0; j < demux[demux_id].STREAMpidcount; j++)
7317 {
7318 write_cw = 0;
7319 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << j)) == (uint) (1 << j)))
7320 {
7321 usedidx = is_ca_used(i, demux[demux_id].STREAMpids[j]);
7322 if(usedidx != INDEX_INVALID)
7323 {
7324 if(idx != usedidx)
7325 {
7326 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip!",
7327 demux_id, i, usedidx, demux[demux_id].STREAMpids[j]);
7328 continue; // if not used for descrambling -> skip!
7329 }
7330 else
7331 {
7332 if(usedidx == lastidx)
7333 {
7334 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip, %s part of cw already written!",
7335 demux_id, i, usedidx, demux[demux_id].STREAMpids[j], (n == 1 ? "even" : "odd"));
7336 continue;
7337 }
7338
7339 cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> write %s part of cw!",
7340 demux_id, i, usedidx, demux[demux_id].STREAMpids[j], (n == 1 ? "even" : "odd"));
7341
7342 write_cw = 1;
7343 }
7344 }
7345 }
7346
7347 if(!write_cw)
7348 {
7349 continue; // no need to write the cw since this ca isnt using it!
7350 }
7351
7352 lastidx = usedidx;
7353 ca_descr.index = usedidx;
7354 ca_descr.parity = n;
7355
7356 memcpy(demux[demux_id].last_cw[stream_id][n], cw + (n * cw_length), cw_length);
7357 memcpy(ca_descr.cw, cw + (n * 8), 8); // ca_descr is only used for 8 byte CWs
7358
7359 cs_log_dbg(D_DVBAPI, "Demuxer %d writing %s part (%s) of controlword, replacing expired (%s)",
7360 demux_id, (n == 1 ? "even" : "odd"), newcw, lastcw);
7361
7362 cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca%d)", demux_id, n, ca_descr.index, i);
7363
7364 if(cfg.dvbapi_extended_cw_api == 1) // Set descrambler algorithm and mode
7365 {
7366 ca_descr_mode.index = usedidx;
7367 ca_descr_mode.algo = algo;
7368 ca_descr_mode.cipher_mode = cipher_mode;
7369
7370 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7371 {
7372 dvbapi_net_send(DVBAPI_CA_SET_DESCR_MODE, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7373 (uint8_t *) &ca_descr_mode, NULL, NULL, demux[demux_id].client_proto_version);
7374 }
7375 else
7376 {
7377 if(ca_fd[i] <= 0)
7378 {
7379 ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
7380 if(ca_fd[i] <= 0) { continue; }
7381 }
7382
7383 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_MODE, &ca_descr_mode) < 0)
7384 {
7385 cs_log("ERROR: ioctl(CA_SET_DESCR_MODE): %s", strerror(errno));
7386 }
7387 }
7388 }
7389
7390 // Send 16 byte CW and IV for AES128, DVB-CISSA
7391 if(cfg.dvbapi_extended_cw_api == 1 && algo == CA_ALGO_AES128)
7392 {
7393 // First send IV
7394 ca_descr_data.index = usedidx;
7395 ca_descr_data.data_type = CA_DATA_IV;
7396 ca_descr_data.data = iv;
7397 ca_descr_data.length = iv_length;
7398
7399 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7400 {
7401 dvbapi_net_send(DVBAPI_CA_SET_DESCR_DATA, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7402 (uint8_t *) &ca_descr_data, NULL, NULL, demux[demux_id].client_proto_version);
7403 }
7404 else
7405 {
7406 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_DATA, &ca_descr_data) < 0)
7407 {
7408 cs_log("ERROR: ioctl(CA_SET_DESCR_DATA): %s", strerror(errno));
7409 }
7410 }
7411
7412 // Then send CW
7413 ca_descr_data.index = usedidx;
7414 ca_descr_data.data_type = CA_DATA_KEY;
7415 ca_descr_data.data = cw + (n * cw_length);
7416 ca_descr_data.length = cw_length;
7417 ca_descr_data.parity = n;
7418
7419 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7420 {
7421 dvbapi_net_send(DVBAPI_CA_SET_DESCR_DATA, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7422 (uint8_t *) &ca_descr_data, NULL, NULL, demux[demux_id].client_proto_version);
7423 }
7424 else
7425 {
7426 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_DATA, &ca_descr_data) < 0)
7427 {
7428 cs_log("ERROR: ioctl(CA_SET_DESCR_DATA): %s", strerror(errno));
7429 }
7430 }
7431 }
7432 else // Send 8 byte CW for DVB-CSA or DES
7433 {
7434 if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
7435 {
7436 dvbapi_net_send(DVBAPI_CA_SET_DESCR, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
7437 (uint8_t *) &ca_descr, NULL, NULL, demux[demux_id].client_proto_version);
7438 }
7439 else
7440 {
7441 if(ca_fd[i] <= 0)
7442 {
7443 ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
7444 if(ca_fd[i] <= 0)
7445 {
7446 continue;
7447 }
7448 }
7449
7450 if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR, &ca_descr) < 0)
7451 {
7452 cs_log("ERROR: ioctl(CA_SET_DESCR): %s", strerror(errno));
7453 }
7454 }
7455 }
7456 }
7457 }
7458#endif
7459 }
7460 }
7461 cs_log_dbg(D_DVBAPI, "Using %d of %d total descramblers", ca_descramblers_used, ca_descramblers_total);
7462}
7463
7464void delayer(ECM_REQUEST *er, uint32_t delay)
7465{
7466 if(delay <= 0) { return; }
7467
7468 struct timeb tpe;
7469 cs_ftime(&tpe);
7470 int64_t gone = comp_timeb(&tpe, &er->tps);
7471
7472 if(gone < delay)
7473 {
7474 cs_log_dbg(D_DVBAPI, "delayer: gone=%"PRId64" ms, cfg=%d ms -> delay=%"PRId64" ms", gone, delay, delay - gone);
7475 cs_sleepms(delay - gone);
7476 }
7477}
7478
7479void dvbapi_send_dcw(struct s_client *client, ECM_REQUEST *er)
7480{
7481 int32_t i, j, k, handled = 0;
7482 for(i = 0; i < MAX_DEMUX; i++)
7483 {
7484 uint32_t nocw_write = 0; // 0 = write cw, 1 = dont write cw to hardware demuxer
7485
7486 // ignore empty demuxers, skip ecm response for other
7487 // srvid and ecm recponse for different adapter
7488 if(demux[i].program_number == 0
7489 || demux[i].program_number != er->srvid
7490 || demux[i].adapter_index != er->adapter_index)
7491 {
7492 continue;
7493 }
7494
7495#ifdef WITH_STAPI5
7496 if(strcmp(dev_list[demux[i].dev_index].name, er->dev_name) != 0)
7497 {
7498 continue; // skip request if PTI device doesn't match request
7499 }
7500#endif
7501
7502 demux[i].rdr = er->selected_reader;
7503
7504 for(j = 0; j < demux[i].ECMpidcount; j++) // check for matching ecmpid
7505 {
7506 if((demux[i].ECMpids[j].CAID == er->caid || demux[i].ECMpids[j].CAID == er->ocaid)
7507 && demux[i].ECMpids[j].ECM_PID == er->pid && demux[i].ECMpids[j].PROVID == er->prid
7508 && demux[i].ECMpids[j].VPID == er->vpid)
7509 {
7510 break;
7511 }
7512 }
7513
7514 if(j == demux[i].ECMpidcount)
7515 {
7516 continue; // ecm response srvid ok but no matching ecmpid, perhaps this for other demuxer
7517 }
7518
7519 cs_log_dbg(D_DVBAPI, "Demuxer %d %scontrol word received for PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
7520 i, (er->rc >= E_NOTFOUND ? "no " : ""), j, er->caid, er->prid, er->pid, er->chid, er->vpid);
7521
7522 uint32_t status = dvbapi_check_ecm_delayed_delivery(i, er);
7523 uint32_t comparecw0 = 0, comparecw1 = 0;
7524 char ecmd5[17 * 3];
7525#ifdef WITH_DEBUG
7526 if(cs_dblevel & D_DVBAPI)
7527 {
7528 cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
7529 }
7530#endif
7531 if(status == 1 && er->rc) // wrong ecmhash
7532 {
7533 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (requested different one)", i, ecmd5);
7534 continue;
7535 }
7536
7537 if(status == 2) // no filter
7538 {
7539 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (filter already killed)", i, ecmd5);
7540 continue;
7541 }
7542
7543 if(status == 5) // empty cw
7544 {
7545 cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (delivered cw is empty!)", i, ecmd5);
7546 nocw_write = 1;
7547
7548 if(er->rc < E_NOTFOUND)
7549 {
7550 er->rc = E_NOTFOUND;
7551 }
7552 }
7553
7554 // 0=matching ecm hash, 2=no filter, 3=table reset, 4=cache-ex response
7555 // Check only against last_cw[0] (index 0) - No need to check the rest
7556 // Skip check for BISS1 - cw could be indeed zero
7557 // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
7558 if((status == 0 || status == 3 || status == 4) && er->rc < E_NOTFOUND && !caid_is_biss(er->caid))
7559 {
7560 // check for matching control word
7561 if(memcmp(er->cw, demux[i].last_cw[0][0], 8) == 0 &&
7562 memcmp(er->cw + 8, demux[i].last_cw[0][1], 8) == 0)
7563 {
7564 comparecw0 = 1;
7565 }
7566 else if(memcmp(er->cw, demux[i].last_cw[0][1], 8) == 0 &&
7567 memcmp(er->cw + 8, demux[i].last_cw[0][0], 8) == 0)
7568 {
7569 comparecw1 = 1;
7570 }
7571
7572 if(comparecw0 == 1 || comparecw1 == 1)
7573 {
7574 cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate controlword ecm response hash %s (duplicate controlword!)", i, ecmd5);
7575 nocw_write = 1;
7576 }
7577 }
7578
7579 if(status == 3) // table reset
7580 {
7581 cs_log_dbg(D_DVBAPI, "Demuxer %d luckyshot new controlword ecm response hash %s (ecm table reset)", i, ecmd5);
7582 }
7583
7584 if(status == 4) // no check on cache-ex responses!
7585 {
7586 cs_log_dbg(D_DVBAPI, "Demuxer %d new controlword from cache-ex reader (no ecmhash check possible)", i);
7587 }
7588
7589 handled = 1; // mark this ecm response as handled
7590 if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 0 && (demux[i].pidindex == -1) && er->caid != 0)
7591 {
7592 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7593 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
7594 demux[i].pidindex = j; // set current index as *the* pid to descramble
7595 demux[i].ECMpids[j].checked = 4;
7596
7597 cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
7598 i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
7599 }
7600
7601 if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 1 && er->caid != 0) // FOUND
7602 {
7603 SAFE_MUTEX_LOCK(&demux[i].answerlock); // only process one ecm answer
7604 if(demux[i].ECMpids[j].checked != 4)
7605 {
7606 int32_t t, o, ecmcounter = 0;
7607 int32_t oldpidindex = demux[i].pidindex;
7608 demux[i].pidindex = j; // set current ecmpid as the new pid to descramble
7609
7610 if(oldpidindex != -1)
7611 {
7612 for(k = 0; k < MAX_STREAM_INDICES; k++)
7613 {
7614 demux[i].ECMpids[j].index[k] = demux[i].ECMpids[oldpidindex].index[k]; // swap index with lower status pid that was descrambling
7615 demux[i].ECMpids[j].useMultipleIndices = demux[i].ECMpids[oldpidindex].useMultipleIndices;
7616 }
7617 }
7618
7619 // check this pid with control word FOUND for higher status
7620 for(t = 0; t < demux[i].ECMpidcount; t++)
7621 {
7622 if(t != j && demux[i].ECMpids[j].status >= demux[i].ECMpids[t].status)
7623 {
7624 // check if ecm filter is in use and
7625 // stop all ecm filters of lower status pids
7626 for(o = 0; o < maxfilter; o++)
7627 {
7628 if(demux[i].demux_fd[o].fd > 0 && demux[i].demux_fd[o].type == TYPE_ECM
7629 && demux[i].demux_fd[o].pidindex == t)
7630 {
7631 // ecm filter belongs to lower status pid -> kill!
7632 dvbapi_stop_filternum(i, o, er->msgid);
7633 }
7634 }
7635 dvbapi_edit_channel_cache(i, t, 0); // remove lower status pid from channel cache
7636 demux[i].ECMpids[t].checked = 4; // mark index t as low status
7637 }
7638 }
7639
7640 for(o = 0; o < maxfilter; o++)
7641 {
7642 if(demux[i].demux_fd[o].type == TYPE_ECM)
7643 {
7644 ecmcounter++; // count all ecm filters
7645 }
7646 }
7647
7648 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7649 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
7650
7651 if(ecmcounter == 1) // if total found running ecmfilters is 1 -> we found the "best" pid
7652 {
7653 dvbapi_edit_channel_cache(i, j, 1);
7654 demux[i].ECMpids[j].checked = 4; // mark best pid last ;)
7655 }
7656 cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
7657 i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
7658 }
7659 SAFE_MUTEX_UNLOCK(&demux[i].answerlock); // and release it!
7660 }
7661
7662 if(er->rc >= E_NOTFOUND) // not found on requestmode 0 + 1
7663 {
7664 if(er->rc == E_SLEEPING)
7665 {
7666 dvbapi_stop_descrambling(i, er->msgid);
7667 return;
7668 }
7669
7670 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(i, j, 'p');
7671 if(forceentry && forceentry->force) // forced pid? keep trying the forced ecmpid!
7672 {
7673 // all cas or irdeto cas with forced prio chid
7674 if(!caid_is_irdeto(er->caid) || forceentry->chid < 0x10000)
7675 {
7676 demux[i].ECMpids[j].table = 0;
7677 dvbapi_set_section_filter(i, er, -1);
7678 continue;
7679 }
7680 else // irdeto cas without chid prio forced
7681 {
7682 // init irdeto current index to first one
7683 if(demux[i].ECMpids[j].irdeto_curindex == 0xFE)
7684 {
7685 demux[i].ECMpids[j].irdeto_curindex = 0x00;
7686 }
7687
7688 // check for last / max chid
7689 if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex))
7690 {
7691 cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of FORCED PID %d CAID %04X PROVID %06X ECMPID %04X",
7692 i, j, er->caid, er->prid, er->pid);
7693
7694 demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
7695 demux[i].ECMpids[j].table = 0;
7696 dvbapi_set_section_filter(i, er, -1);
7697 continue;
7698 }
7699 }
7700 }
7701
7702 // in case of timeout or fatal LB event give
7703 // this pid another try but no more than 1 try
7704 if((er->rc == E_TIMEOUT || (er->rcEx && er->rcEx <= E2_CCCAM_NOCARD))
7705 && demux[i].ECMpids[j].tries == 0xFE)
7706 {
7707 demux[i].ECMpids[j].tries -= 0x07;
7708 demux[i].ECMpids[j].table = 0;
7709 dvbapi_set_section_filter(i, er, -1);
7710 continue;
7711 }
7712 else // all not found responses exception: first timeout response and first fatal loadbalancer response
7713 {
7714 demux[i].ECMpids[j].CHID = 0x10000; // get rid of this prio chid since it failed!
7715 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry
7716 }
7717
7718 if(caid_is_irdeto(er->caid))
7719 {
7720 // init irdeto current index to first one
7721 if(demux[i].ECMpids[j].irdeto_curindex == 0xFE)
7722 {
7723 demux[i].ECMpids[j].irdeto_curindex = 0x00;
7724 }
7725
7726 // check for last / max chid
7727 if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex))
7728 {
7729 cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of PID %d CAID %04X PROVID %06X ECMPID %04X VPID %04X",
7730 i, j, er->caid, er->prid, er->pid, er->vpid);
7731
7732 demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
7733 demux[i].ECMpids[j].table = 0;
7734 dvbapi_set_section_filter(i, er, -1);
7735 continue;
7736 }
7737 }
7738
7739 dvbapi_edit_channel_cache(i, j, 0); // remove this pid from channelcache
7740
7741 if(demux[i].pidindex == j)
7742 {
7743 // current pid delivered a notfound so this pid isn't
7744 // being used to descramble any longer -> clear pidindex
7745 demux[i].pidindex = -1;
7746 }
7747
7748 demux[i].ECMpids[j].irdeto_maxindex = 0;
7749 demux[i].ECMpids[j].irdeto_curindex = 0xFE;
7750 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7751 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
7752 demux[i].ECMpids[j].table = 0;
7753 demux[i].ECMpids[j].checked = 4; // flag ecmpid as checked
7754 demux[i].ECMpids[j].status = -1; // flag ecmpid as unusable
7755
7756 int32_t found = 1; // setup for first run
7757 int32_t filternum = -1;
7758
7759 while(found > 0) // disable all ecm + emm filters for this notfound
7760 {
7761 found = 0;
7762
7763 filternum = dvbapi_get_filternum(i, er, TYPE_ECM); // get ecm filternumber
7764 if(filternum > -1) // in case valid filter found
7765 {
7766 int32_t fd = demux[i].demux_fd[filternum].fd;
7767 if(fd > 0) // in case valid fd
7768 {
7769 dvbapi_stop_filternum(i, filternum, er->msgid); // stop ecmfilter
7770 found = 1;
7771 }
7772 }
7773
7774 if(caid_is_irdeto(er->caid)) // in case irdeto cas stop old emm filters
7775 {
7776 filternum = dvbapi_get_filternum(i, er, TYPE_EMM); // get emm filternumber
7777 if(filternum > -1) // in case valid filter found
7778 {
7779 int32_t fd = demux[i].demux_fd[filternum].fd;
7780 if(fd > 0) // in case valid fd
7781 {
7782 dvbapi_stop_filternum(i, filternum, er->msgid); // stop emmfilter
7783 found = 1;
7784 }
7785 }
7786 }
7787 }
7788 continue;
7789 }
7790
7791 // below this should be only run in case of ecm answer is found
7792 uint32_t chid = get_subid(er); // derive current chid in case of irdeto, or a unique part of ecm on other cas systems
7793 demux[i].ECMpids[j].CHID = (chid != 0 ? chid : 0x10000); // if not zero apply, otherwise use no chid value 0x10000
7794 dvbapi_edit_channel_cache(i, j, 1); // do it here to here after the right CHID is registered
7795
7796 //dvbapi_set_section_filter(i, er); is not needed anymore (unsure)
7797 demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
7798 demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
7799
7800 // cw was already written by another filter or current pid
7801 // isn't pid used to descramble so it ends here!
7802 if(nocw_write || demux[i].pidindex != j)
7803 {
7804 continue;
7805 }
7806
7807 struct s_dvbapi_priority *delayentry = dvbapi_check_prio_match(i, demux[i].pidindex, 'd');
7808 uint32_t delay = 0;
7809
7810 if(delayentry)
7811 {
7812 if(delayentry->delay < 1000)
7813 {
7814 delay = delayentry->delay;
7815 cs_log_dbg(D_DVBAPI, "specific delay: write cw %d ms after ecmrequest", delay);
7816 }
7817 }
7818 else if(cfg.dvbapi_delayer > 0)
7819 {
7820 delay = cfg.dvbapi_delayer;
7821 cs_log_dbg(D_DVBAPI, "generic delay: write cw %d ms after ecmrequest", delay);
7822 }
7823
7824 delayer(er, delay);
7825
7826#ifdef WITH_EMU
7827 if(!chk_ctab_ex(er->caid, &cfg.emu_stream_relay_ctab) || !cfg.emu_stream_relay_enabled)
7828#endif
7829 switch(selected_api)
7830 {
7831#if defined(WITH_STAPI) || defined(WITH_STAPI5)
7832 case STAPI:
7833 stapi_write_cw(i, er->cw, demux[i].STREAMpids, demux[i].STREAMpidcount, demux[i].pmt_file);
7834 break;
7835#endif
7836 default:
7837 {
7838#ifdef WITH_EXTENDED_CW
7839 if(er->cw_ex.mode != demux[i].ECMpids[j].useMultipleIndices)
7840 {
7841 uint32_t idx;
7842
7843 for(k = 0; k < demux[i].STREAMpidcount; k++)
7844 {
7845 if(demux[i].ECMpids[j].useMultipleIndices)
7846 {
7847 idx = demux[i].ECMpids[j].index[k];
7848 }
7849 else
7850 {
7851 idx = demux[i].ECMpids[j].index[0];
7852 }
7853 dvbapi_set_pid(i, k, idx, false, false, er->msgid); // disable streampid
7854 }
7855
7856 for(k = 0; k < MAX_STREAM_INDICES; k++)
7857 {
7858 demux[i].ECMpids[j].index[k] = INDEX_INVALID;
7859 }
7860 }
7861
7862 if(er->cw_ex.mode == CW_MODE_MULTIPLE_CW)
7863 {
7864 int32_t key_pos_a = 0;
7865 demux[i].ECMpids[j].useMultipleIndices = 1;
7866
7867 for(k = 0; k < demux[i].STREAMpidcount; k++)
7868 {
7869 if(demux[i].STREAMpidsType[k] == STREAM_VIDEO)
7870 {
7871 dvbapi_write_cw(i, j, k, er->cw, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7872 }
7873 else if(demux[i].STREAMpidsType[k] == STREAM_AUDIO)
7874 {
7875 if(key_pos_a < 4)
7876 {
7877 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);
7878 key_pos_a++;
7879 }
7880 }
7881 // Every channel that uses the extended cw has unencrypted subtitle streams,
7882 // so disable CW writing to save indices for audio streams and recordings.
7883 //else // Data
7884 //{
7885 // dvbapi_write_cw(i, j, k, er->cw_ex.data, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7886 //}
7887 }
7888 }
7889 else
7890 {
7891 demux[i].ECMpids[j].useMultipleIndices = 0;
7892
7893 if(er->cw_ex.algo == CW_ALGO_AES128)
7894 {
7895 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);
7896 }
7897 else
7898 {
7899 dvbapi_write_cw(i, j, 0, er->cw, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
7900 }
7901 }
7902#else
7903 cfg.dvbapi_extended_cw_api = 0; // in CSA mode extended_cw_api should be always 0 regardless what user selected!
7904 dvbapi_write_cw(i, j, 0, er->cw, 8, NULL, 0, CA_ALGO_DVBCSA, CA_MODE_CBC, er->msgid);
7905#endif
7906 break;
7907 }
7908 }
7909
7910 // reset idle-Time
7911 client->last = time((time_t *)0); // ********* TO BE FIXED LATER ON ******
7912
7913 if((cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX) && demux[i].client_proto_version >= 2)
7914 {
7915 dvbapi_net_send(DVBAPI_ECM_INFO, demux[i].socket_fd, 0, i, 0, NULL, client, er, demux[i].client_proto_version);
7916 }
7917#ifndef __CYGWIN__
7918 else if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
7919#endif
7920 if(cfg.dvbapi_ecminfo_file != 0 && cfg.dvbapi_boxtype != BOXTYPE_SAMYGO)
7921 {
7922#ifdef WITH_EXTENDED_CW
7923 // Only print CWs for index 0 in ecm.info file
7924 if(er->cw_ex.algo == CA_ALGO_AES128)
7925 {
7926 dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 16);
7927 }
7928 else
7929 {
7930 dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 8);
7931 }
7932#else
7933 dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 8);
7934#endif
7935 }
7936 }
7937
7938 if(handled == 0)
7939 {
7940 cs_log_dbg(D_DVBAPI, "Unhandled ECM response received for CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
7941 er->caid, er->prid, er->pid, er->chid, er->vpid);
7942 }
7943}
7944
7945static int8_t isValidCW(uint8_t *cw)
7946{
7947 uint8_t i;
7948 for(i = 0; i < 16; i += 4)
7949 {
7950 if(((cw[i] + cw[i + 1] + cw[i + 2]) & 0xff) != cw[i + 3])
7951 {
7952 return 0;
7953 }
7954 }
7955 return 1;
7956}
7957
7958void dvbapi_write_ecminfo_file(struct s_client *client, ECM_REQUEST *er, uint8_t *lastcw0, uint8_t *lastcw1, uint8_t cw_length)
7959{
7960#define ECMINFO_TYPE_OSCAM 0
7961#define ECMINFO_TYPE_OSCAM_MS 1
7962#define ECMINFO_TYPE_WICARDD 2
7963#define ECMINFO_TYPE_MGCAMD 3
7964#define ECMINFO_TYPE_CCCAM 4
7965#define ECMINFO_TYPE_CAMD3 5
7966#define ECMINFO_TYPE_GBOX 6
7967
7968 FILE *ecmtxt = fopen(ECMINFO_FILE, "w");
7969 if(ecmtxt != NULL && er->rc < E_NOTFOUND)
7970 {
7971 char tmp[49]; // holds 16 byte cw - (2 hex digits + 1 space) * 16 byte + string termination)
7972 const char *reader_name = NULL, *from_name = NULL, *proto_name = NULL, *from_device= NULL ;
7973 int8_t hops = 0;
7974 int32_t from_port = 0;
7975 char system_name[64];
7976 const char *const_system_name = get_cardsystem_desc_by_caid(er->caid);
7977
7978 cs_strncpy(system_name, const_system_name, sizeof(system_name));
7979 system_name[0] = (char)toupper((int)system_name[0]);
7980
7981 if(cfg.dvbapi_ecminfo_type <= ECMINFO_TYPE_WICARDD)
7982 {
7983 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD)
7984 {
7985 fprintf(ecmtxt, "system: %s\n", system_name);
7986 }
7987
7988 fprintf(ecmtxt, "caid: 0x%04X\npid: 0x%04X\n", er->caid, er->pid);
7989
7990 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD)
7991 {
7992 fprintf(ecmtxt, "prov: %06X\n", (uint) er->prid);
7993 }
7994 else
7995 {
7996 fprintf(ecmtxt, "prov: 0x%06X\n", (uint) er->prid);
7997 }
7998
7999 fprintf(ecmtxt, "chid: 0x%04X\n", er->chid);
8000 }
8001 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_MGCAMD)
8002 {
8003 fprintf(ecmtxt, "===== %s ECM on CaID 0x%04X, pid 0x%04X =====\nprov: %06X\n",
8004 system_name, er->caid, er->pid, (uint) er->prid);
8005 }
8006 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CCCAM)
8007 {
8008 char provider_name[128];
8009 get_providername(er->prid, er->caid, provider_name, sizeof(provider_name));
8010
8011 if(provider_name[0])
8012 {
8013 fprintf(ecmtxt, "system: %s\ncaid: 0x%04X\nprovider: %s\nprovid: 0x%06X\npid: 0x%04X\n",
8014 system_name, er->caid, provider_name, (uint) er->prid, er->pid);
8015 }
8016 else
8017 {
8018 fprintf(ecmtxt, "system: %s\ncaid: 0x%04X\nprovid: 0x%06X\npid: 0x%04X\n",
8019 system_name, er->caid, (uint) er->prid, er->pid);
8020 }
8021 }
8022 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CAMD3)
8023 {
8024 fprintf(ecmtxt, "CAID 0x%04X, PID 0x%04X, PROVIDER 0x%06X\n",
8025 er->caid, er->pid, (uint) er->prid);
8026 }
8027#ifdef MODULE_GBOX
8028 else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_GBOX)
8029 {
8030 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",
8031 system_name, er->caid, er->pid, er->srvid, er->selected_reader->gbox_cw_src_peer, er->selected_reader->gbox_crd_slot_lev >> 4,
8032 er->selected_reader->gbox_crd_slot_lev & 0xf, er->selected_reader->currenthops, (uint) er->prid);
8033 }
8034#endif
8035
8036 switch(er->rc)
8037 {
8038 case E_FOUND:
8039 if(er->selected_reader)
8040 {
8041 reader_name = er->selected_reader->label;
8042 if(is_network_reader(er->selected_reader))
8043 {
8044 from_name = er->selected_reader->device;
8045 from_port = er->selected_reader->r_port;
8046 }
8047 else
8048 {
8049 from_name = "local";
8050 from_device = er->selected_reader->device;
8051 }
8052 proto_name = reader_get_type_desc(er->selected_reader, 1);
8053 hops = er->selected_reader->currenthops;
8054 }
8055 else
8056 {
8057 reader_name = "none";
8058 from_name = "local";
8059 proto_name = "none";
8060 }
8061 break;
8062
8063 case E_CACHE1:
8064 reader_name = "Cache";
8065 from_name = "cache1";
8066 proto_name = "none";
8067 break;
8068
8069 case E_CACHE2:
8070 reader_name = "Cache";
8071 from_name = "cache2";
8072 proto_name = "none";
8073 break;
8074
8075 case E_CACHEEX:
8076 reader_name = "Cache";
8077 from_name = "cache3";
8078 proto_name = "none";
8079 break;
8080 }
8081
8082 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_GBOX)
8083 {
8084 switch(er->rc)
8085 {
8086 case E_FOUND:
8087 if(er->selected_reader)
8088 {
8089 if(is_network_reader(er->selected_reader))
8090 {
8091 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
8092 reader_name, from_name, from_port, proto_name);
8093 }
8094 else
8095 {
8096 fprintf(ecmtxt, "reader: %s\nfrom: %s - %s\nprotocol: %s\n",
8097 reader_name, from_name, from_device, proto_name);
8098 }
8099 }
8100 break;
8101
8102 case E_CACHE1:
8103 case E_CACHE2:
8104 case E_CACHEEX:
8105 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
8106 reader_name, from_name, from_port, proto_name);
8107 break;
8108 }
8109 fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime / 1000);
8110 }
8111
8112 if(cfg.dvbapi_ecminfo_type <= ECMINFO_TYPE_OSCAM_MS)
8113 {
8114 switch(er->rc)
8115 {
8116 case E_FOUND:
8117 if(er->selected_reader)
8118 {
8119 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\nhops: %d\n",
8120 reader_name, from_name, from_port, proto_name, hops);
8121 }
8122 break;
8123
8124 case E_CACHE1:
8125 case E_CACHE2:
8126 case E_CACHEEX:
8127 fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
8128 reader_name, from_name, from_port, proto_name);
8129 break;
8130 }
8131
8132 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_OSCAM)
8133 {
8134 fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime / 1000);
8135 }
8136 else
8137 {
8138 fprintf(ecmtxt, "ecm time: %d\n", client->cwlastresptime);
8139 }
8140 }
8141
8142 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CAMD3)
8143 {
8144 fprintf(ecmtxt, "FROM: %s\n", reader_name);
8145 fprintf(ecmtxt, "CW0: %s\n", cs_hexdump(1, lastcw0, cw_length, tmp, sizeof(tmp)));
8146 fprintf(ecmtxt, "CW1: %s\n", cs_hexdump(1, lastcw1, cw_length, tmp, sizeof(tmp)));
8147 }
8148 else
8149 {
8150 fprintf(ecmtxt, "cw0: %s\n", cs_hexdump(1, lastcw0, cw_length, tmp, sizeof(tmp)));
8151 fprintf(ecmtxt, "cw1: %s\n", cs_hexdump(1, lastcw1, cw_length, tmp, sizeof(tmp)));
8152 }
8153
8154 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD || cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_MGCAMD)
8155 {
8156 time_t walltime;
8157 struct tm lt;
8158 char timebuf[32];
8159
8160 if(cw_length == 8) // only check checksum for 8 byte CWs
8161 {
8162 fprintf(ecmtxt, "Signature %s\n", (isValidCW(lastcw0) || isValidCW(lastcw1)) ? "OK" : "NOK");
8163 }
8164 else
8165 {
8166 fprintf(ecmtxt, "Signature %s\n", "OK");
8167 }
8168
8169 if(reader_name != NULL)
8170 {
8171 fprintf(ecmtxt, "source: %s (%s at %s:%d)\n", reader_name, proto_name, from_name, from_port);
8172 }
8173
8174 walltime = cs_time();
8175 localtime_r(&walltime, &lt);
8176
8177 if(strftime(timebuf, 32, "%a %b %d %H:%M:%S %Y", &lt) != 0)
8178 {
8179 fprintf(ecmtxt, "%d msec -- %s\n", client->cwlastresptime, timebuf);
8180 }
8181 }
8182
8183 if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CCCAM)
8184 {
8185 if(reader_name != NULL)
8186 {
8187 fprintf(ecmtxt, "using: %s\naddress: %s:%d\nhops: %d\n",
8188 proto_name, from_name, from_port, hops);
8189 }
8190 fprintf(ecmtxt, "ecm time: %d\n", client->cwlastresptime);
8191 }
8192 }
8193
8194 if(ecmtxt)
8195 {
8196 int32_t ret = fclose(ecmtxt);
8197 if(ret < 0)
8198 {
8199 cs_log("ERROR: Could not close ecmtxt fd (errno=%d %s)", errno, strerror(errno));
8200 }
8201 ecmtxt = NULL;
8202 }
8203}
8204
8205
8206void *dvbapi_start_handler(struct s_client *cl, uint8_t *UNUSED(mbuf), int32_t module_idx, void *(*_main_func)(void *))
8207{
8208 // cs_log("dvbapi loaded fd=%d", idx);
8209 if(cfg.dvbapi_enabled == 1)
8210 {
8211 cl = create_client(get_null_ip());
8212 cl->module_idx = module_idx;
8213 cl->typ = 'c';
8214
8215 int32_t ret = start_thread("dvbapi handler", _main_func, (void *)cl, &cl->thread, 1, 0);
8216 if(ret)
8217 {
8218 return NULL;
8219 }
8220 }
8221 return NULL;
8222}
8223
8224void *dvbapi_handler(struct s_client *cl, uint8_t *mbuf, int32_t module_idx)
8225{
8226 return dvbapi_start_handler(cl, mbuf, module_idx, dvbapi_main_local);
8227}
8228
8229int32_t dvbapi_set_section_filter(int32_t demux_id, ECM_REQUEST *er, int32_t n)
8230{
8231 if(!er) { return -1; }
8232
8233 if(USE_OPENXCAS || (selected_api != DVBAPI_3 && selected_api != DVBAPI_1 && selected_api != STAPI) // only valid for dvbapi3, dvbapi1 and STAPI
8234 || (cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)) // reported buggy using sectionfiltering after 1~4 hours -> for now disabled!
8235 {
8236 return 0;
8237 }
8238
8239 if(n == -1)
8240 {
8241 n = dvbapi_get_filternum(demux_id, er, TYPE_ECM);
8242 }
8243
8244 if(n < 0) // in case no valid filter found;
8245 {
8246 return -1;
8247 }
8248
8249 int32_t fd = demux[demux_id].demux_fd[n].fd;
8250 if(fd < 1) // in case no valid fd
8251 {
8252 return -1;
8253 }
8254
8255 uint8_t filter[16];
8256 uint8_t mask[16];
8257 memset(filter, 0, 16);
8258 memset(mask, 0, 16);
8259 struct s_ecmpid *curpid = NULL;
8260
8261 int32_t pid = demux[demux_id].demux_fd[n].pidindex;
8262 if(pid != -1)
8263 {
8264 curpid = &demux[demux_id].ECMpids[pid];
8265 }
8266
8267 if(curpid->table != er->ecm[0] && curpid->table != 0)
8268 {
8269 return -1; // if current ecmtype differs from latest requested ecmtype do not apply section filtering!
8270 }
8271
8272 uint8_t ecmfilter = 0;
8273
8274 if(er->ecm[0] == 0x80)
8275 {
8276 ecmfilter = 0x81; // current processed ecm is even, next will be filtered for odd
8277 }
8278 else
8279 {
8280 ecmfilter = 0x80; // current processed ecm is odd, next will be filtered for even
8281 }
8282
8283 if(curpid->table != 0) // cycle ecmtype from odd to even or even to odd
8284 {
8285 filter[0] = ecmfilter; // only accept new ecms (if previous odd, filter for even and vice versa)
8286 mask[0] = 0xFF;
8287 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to %s (CAID %04X PROVID %06X FD %d)",
8288 demux_id, n + 1, (ecmfilter == 0x80 ? "EVEN" : "ODD"), curpid->CAID, curpid->PROVID, fd);
8289 }
8290 else // not decoding right now so we are interessted in all ecm types!
8291 {
8292 filter[0] = 0x80; // set filter to wait for any ecms
8293 mask[0] = 0xF0;
8294 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to ODD+EVEN (CAID %04X PROVID %06X FD %d)",
8295 demux_id, n + 1, curpid->CAID, curpid->PROVID, fd);
8296 }
8297
8298 uint32_t offset = 0, extramask = 0xFF;
8299 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
8300 //cs_log("**** curpid->CHID %04X, checked = %d, er->chid = %04X *****", curpid->CHID, curpid->checked, er->chid);
8301 // checked 4 to make sure we dont set chid filter and no such ecm in dvbstream except for forced pids!
8302
8303 if(curpid->CHID < 0x10000 && (curpid->checked == 4 || (forceentry && forceentry->force)))
8304 {
8305 switch(er->caid >> 8)
8306 {
8307 case 0x01: // seca
8308 offset = 7;
8309 extramask = 0xF0;
8310 break;
8311
8312 case 0x05: // viaccess
8313 offset = 8;
8314 break;
8315
8316 case 0x06: // irdeto
8317 offset = 6;
8318 break;
8319
8320 case 0x09: // videoguard
8321 offset = 11;
8322 break;
8323
8324 case 0x4A: // DRE-Crypt, Bulcrypt, Tongang and others?
8325 if(!caid_is_bulcrypt(er->caid))
8326 {
8327 offset = 6;
8328 }
8329 break;
8330 }
8331 }
8332
8333 int32_t irdetomatch = 1; // check if wanted irdeto index is the one the delivers current chid!
8334 if(caid_is_irdeto(curpid->CAID))
8335 {
8336 if(curpid->irdeto_curindex == er->ecm[4]) { irdetomatch = 1; } // ok apply chid filtering
8337 else { irdetomatch = 0; } // skip chid filtering but apply irdeto index filtering
8338 }
8339
8340 if(offset && irdetomatch) // we have a cas with chid or unique part in checked ecm
8341 {
8342 i2b_buf(2, curpid->CHID, filter + (offset - 2));
8343 mask[(offset - 2)] = 0xFF & extramask; // additional mask seca2 chid can be FC10 or FD10 varies each month so only apply F?10
8344 mask[(offset - 1)] = 0xFF;
8345 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to %04X on fd %d", demux_id, n + 1, curpid->CHID, fd);
8346 }
8347 else
8348 {
8349 // on irdeto we can always apply irdeto index filtering!
8350 if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_curindex < 0xFE))
8351 {
8352 filter[2] = curpid->irdeto_curindex;
8353 mask[2] = 0xFF;
8354 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set irdetoindex to %d on fd %d",
8355 demux_id, n + 1, curpid->irdeto_curindex, fd);
8356 }
8357 else // all other cas systems also cas systems without chid or unique ecm part
8358 {
8359 cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to ANY CHID on fd %d", demux_id, n + 1, fd);
8360 }
8361 }
8362
8363 int32_t ret = dvbapi_activate_section_filter(demux_id, n, fd, curpid->ECM_PID, filter, mask, er->msgid);
8364 if(ret < 0) // something went wrong setting filter!
8365 {
8366 cs_log("Demuxer %d Filter %d (fd %d) error setting section filtering -> stop filter!", demux_id, n + 1, fd);
8367
8368 ret = dvbapi_stop_filternum(demux_id, n, er->msgid);
8369 if(ret == -1)
8370 {
8371 cs_log("Demuxer %d Filter %d (fd %d) stopping filter failed -> kill all filters of this demuxer!", demux_id, n + 1, fd);
8372 dvbapi_stop_filter(demux_id, TYPE_EMM, er->msgid);
8373 dvbapi_stop_filter(demux_id, TYPE_ECM, er->msgid);
8374 }
8375 return -1;
8376 }
8377 return n;
8378}
8379
8380int32_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)
8381{
8382 int32_t ret = -1;
8383
8384 switch(selected_api)
8385 {
8386 case DVBAPI_3:
8387 {
8388 struct dmx_sct_filter_params sFP2;
8389 memset(&sFP2, 0, sizeof(sFP2));
8390 sFP2.pid = pid;
8391 sFP2.timeout = 0;
8392 sFP2.flags = DMX_IMMEDIATE_START;
8393
8394 if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
8395 {
8396 //DeepThought: on dgs/cubestation and neumo images, perhaps others
8397 //the following code is needed to descramble
8398 sFP2.filter.filter[0] = filter[0];
8399 sFP2.filter.mask[0] = mask[0];
8400 sFP2.filter.filter[1] = 0;
8401 sFP2.filter.mask[1] = 0;
8402 sFP2.filter.filter[2] = 0;
8403 sFP2.filter.mask[2] = 0;
8404 memcpy(sFP2.filter.filter + 3, filter + 1, 16 - 3);
8405 memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
8406
8407 //DeepThought: in the drivers of the dgs/cubestation and neumo images,
8408 //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
8409 //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
8410 //due to a bug its sets the "positive mask" wrongly (they should be all 0).
8411 //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
8412 //structure, which is incorrect (it should be dmxSctFilterParams).
8413 //The only way to get it right is to call DMX_SET_FILTER1 with the argument
8414 //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
8415
8416 ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP2);
8417 }
8418 else
8419 {
8420 memcpy(sFP2.filter.filter, filter, 16);
8421 memcpy(sFP2.filter.mask, mask, 16);
8422
8423 if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
8424 {
8425 ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER,
8426 demux[demux_id].socket_fd,
8427 msgid,
8428 demux_id,
8429 num,
8430 (uint8_t *) &sFP2,
8431 NULL,
8432 NULL,
8433 demux[demux_id].client_proto_version);
8434 }
8435 else
8436 {
8437 ret = dvbapi_ioctl(fd, DMX_SET_FILTER, &sFP2);
8438 }
8439 }
8440 break;
8441 }
8442
8443 case DVBAPI_1:
8444 {
8445 struct dmxSctFilterParams sFP1;
8446 memset(&sFP1, 0, sizeof(sFP1));
8447 sFP1.pid = pid;
8448 sFP1.timeout = 0;
8449 sFP1.flags = DMX_IMMEDIATE_START;
8450 memcpy(sFP1.filter.filter, filter, 16);
8451 memcpy(sFP1.filter.mask, mask, 16);
8452 ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP1);
8453 break;
8454 }
8455
8456#if defined(WITH_STAPI) || defined(WITH_STAPI5)
8457 case STAPI:
8458 {
8459 ret = stapi_activate_section_filter(fd, filter, mask);
8460 break;
8461 }
8462#endif
8463 // Isn't implemented in COOLAPI-1 (legacy)
8464#if defined WITH_COOLAPI2
8465 case COOLAPI:
8466 {
8467 int32_t n = coolapi_get_filter_num(fd);
8468 if(n < 0)
8469 {
8470 return n;
8471 }
8472 coolapi_set_filter(fd, n, pid, filter, mask, TYPE_ECM);
8473 break;
8474 }
8475#endif
8476
8477 default:
8478 break;
8479 }
8480
8481 if(ret != -1) // only change filter/mask for comparing if box returned no errors!
8482 {
8483 // copy filter and mask to check later on if receiver delivered accordingly
8484 memcpy(demux[demux_id].demux_fd[num].filter, filter, 16);
8485 memcpy(demux[demux_id].demux_fd[num].mask, mask, 16);
8486 }
8487 return ret;
8488}
8489
8490int32_t dvbapi_check_ecm_delayed_delivery(int32_t demux_id, ECM_REQUEST *er)
8491{
8492 int32_t ret = 0;
8493 int32_t filternum = dvbapi_get_filternum(demux_id, er, TYPE_ECM);
8494 char nullcw[CS_ECMSTORESIZE];
8495 memset(nullcw, 0, CS_ECMSTORESIZE);
8496
8497 if(filternum < 0) // if no matching filter act like ecm response is delayed
8498 {
8499 return 2;
8500 }
8501
8502 if(memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, nullcw, CS_ECMSTORESIZE))
8503 {
8504 demux[demux_id].demux_fd[filternum].lastresult = er->rc; // save last result
8505 char ecmd5[17 * 3];
8506#ifdef WITH_DEBUG
8507 if(cs_dblevel & D_DVBAPI)
8508 {
8509 cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
8510 }
8511#endif
8512 cs_log_dbg(D_DVBAPI, "Demuxer %d requested controlword for ecm %s on fd %d",
8513 demux_id, ecmd5, demux[demux_id].demux_fd[filternum].fd);
8514
8515 uint8_t md5tmp[MD5_DIGEST_LENGTH];
8516 MD5(er->ecm, er->ecmlen, md5tmp);
8517
8518 // 1 = no response on the ecm we request last for this fd!
8519 ret = (memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE) != 0 ? 1 : 0);
8520 }
8521
8522 // Check for null cw
8523 // Skip check for BISS1 - cw could be indeed zero
8524 // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
8525 if(memcmp(er->cw, nullcw, 8) == 0 && memcmp(er->cw + 8, nullcw, 8) == 0 && !caid_is_biss(er->caid))
8526 {
8527 return 5;
8528 }
8529
8530 struct s_ecmpid *curpid = NULL;
8531 int32_t pid = demux[demux_id].demux_fd[filternum].pidindex;
8532
8533 if(pid !=-1)
8534 {
8535 curpid = &demux[demux_id].ECMpids[pid];
8536 if(curpid->table == 0) // on change table act like ecm response is found
8537 {
8538 return 3;
8539 }
8540 }
8541
8542 if(er->rc == E_CACHEEX) // on cache-ex response act like ecm response is found
8543 {
8544 return 4;
8545 }
8546
8547 return ret;
8548}
8549
8550int32_t dvbapi_get_filternum(int32_t demux_id, ECM_REQUEST *er, int32_t type)
8551{
8552 if(!er) { return -1; }
8553
8554 int32_t n;
8555 int32_t fd = -1;
8556
8557 for(n = 0; n < maxfilter; n++) // determine fd
8558 {
8559 // check for valid and right type (ecm or emm)
8560 if(demux[demux_id].demux_fd[n].fd > 0 && demux[demux_id].demux_fd[n].type == type)
8561 {
8562 if(type == TYPE_ECM && er->srvid != demux[demux_id].program_number)
8563 {
8564 continue;
8565 }
8566
8567 if((demux[demux_id].demux_fd[n].pid == er->pid) && ((demux[demux_id].demux_fd[n].provid == er->prid)
8568 || demux[demux_id].demux_fd[n].provid == 0 || er->prid == 0) && ((demux[demux_id].demux_fd[n].caid == er->caid)
8569 || (demux[demux_id].demux_fd[n].caid == er->ocaid))) // current ecm pid?
8570 {
8571 fd = demux[demux_id].demux_fd[n].fd; // found!
8572 if(demux[demux_id].demux_fd[n].caid == er->ocaid)
8573 {
8574 // clear ecmd5 hash since betatunneled ecms hash different!
8575 memset(demux[demux_id].demux_fd[n].lastecmd5, 0, CS_ECMSTORESIZE);
8576 }
8577 break;
8578 }
8579 }
8580 }
8581
8582 if(fd > 0 && demux[demux_id].demux_fd[n].provid == 0)
8583 {
8584 demux[demux_id].demux_fd[n].provid = er->prid; // hack to fill in provid into demuxer
8585 }
8586 return (fd > 0 ? n : fd); // return -1(fd) on not found, on found return filternumber(n)
8587}
8588
8589uint32_t dvbapi_ca_set_pid(int32_t demux_id, int32_t pid, int32_t stream_id, bool use_des, uint32_t msgid)
8590{
8591 uint32_t idx;
8592 int32_t n;
8593
8594 if(pid == -1 || pid > demux[demux_id].ECMpidcount)
8595 {
8596 return INDEX_INVALID;
8597 }
8598
8599 if(demux[demux_id].ECMpids[pid].useMultipleIndices)
8600 {
8601 n = stream_id;
8602 idx = demux[demux_id].ECMpids[pid].index[n];
8603
8604 if(idx == INDEX_INVALID) // if we have no index for this pid, get one!
8605 {
8606 idx = dvbapi_get_desc_index(demux_id, pid, n);
8607 if(idx == INDEX_INVALID)
8608 {
8609 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X has no free index",
8610 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID);
8611
8612 return INDEX_INVALID;
8613 }
8614
8615 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d for stream %d",
8616 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID, idx, n);
8617 }
8618
8619 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n)))
8620 {
8621 dvbapi_set_pid(demux_id, n, idx, true, use_des, msgid); // enable stream pid
8622 }
8623 else
8624 {
8625 dvbapi_set_pid(demux_id, n, idx, false, false, msgid); // disable stream pid
8626 }
8627 }
8628 else
8629 {
8630 idx = demux[demux_id].ECMpids[pid].index[0];
8631
8632 if(idx == INDEX_INVALID) // if we have no index for this pid, get one!
8633 {
8634 idx = dvbapi_get_desc_index(demux_id, pid, 0);
8635 if(idx == INDEX_INVALID)
8636 {
8637 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X has no free index",
8638 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID);
8639
8640 return INDEX_INVALID;
8641 }
8642
8643 cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d",
8644 demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID, idx);
8645 }
8646
8647 for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
8648 {
8649 if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n)))
8650 {
8651 dvbapi_set_pid(demux_id, n, idx, true, use_des, 0); // enable stream pid
8652 }
8653 else
8654 {
8655 dvbapi_set_pid(demux_id, n, idx, false, false, 0); // disable stream pid
8656 }
8657 }
8658 }
8659 return idx; // return ca index
8660}
8661
8662int8_t update_streampid_list(uint8_t cadevice, uint16_t pid, uint32_t idx, bool use_des)
8663{
8664 struct s_streampid *listitem, *newlistitem;
8665 LL_ITER itr;
8666 if(!ll_activestreampids)
8667 {
8668 ll_activestreampids = ll_create("ll_activestreampids");
8669 }
8670
8671 if(idx >= INDEX_MAX)
8672 {
8673 return INVALID_STREAMPID_INDEX;
8674 }
8675
8676 if(ll_count(ll_activestreampids) > 0)
8677 {
8678 itr = ll_iter_create(ll_activestreampids);
8679 while((listitem = ll_iter_next(&itr)))
8680 {
8681 if(cadevice == listitem->cadevice && pid == listitem->streampid)
8682 {
8683 if((listitem->activeindexers & (1 << idx)) == (uint64_t) (1 << idx))
8684 {
8685 if(cfg.dvbapi_extended_cw_api == 2 && use_des != listitem->use_des)
8686 {
8687 listitem->use_des = use_des;
8688 return FIRST_STREAMPID_INDEX;
8689 }
8690 return FOUND_STREAMPID_INDEX; // match found
8691 }
8692 else
8693 {
8694 listitem->activeindexers |= (1 << idx); // ca + pid found but not this index -> add this index
8695 cs_log_dbg(D_DVBAPI, "Added existing streampid %04X with new index %d to ca%d", pid, idx, cadevice);
8696
8697 if(cfg.dvbapi_extended_cw_api == 2 && use_des != listitem->use_des)
8698 {
8699 listitem->use_des = use_des;
8700 return FIRST_STREAMPID_INDEX;
8701 }
8702 return ADDED_STREAMPID_INDEX;
8703 }
8704 }
8705 }
8706 }
8707
8708 if(!cs_malloc(&newlistitem, sizeof(struct s_streampid)))
8709 {
8710 return FIRST_STREAMPID_INDEX; // not sure if this is correct
8711 }
8712
8713 newlistitem->cadevice = cadevice;
8714 newlistitem->streampid = pid;
8715 newlistitem->activeindexers = (1 << idx);
8716 newlistitem->caindex = idx; // set this index as used to decode on ca device
8717 newlistitem->use_des = use_des;
8718
8719 ll_append(ll_activestreampids, newlistitem);
8720 cs_log_dbg(D_DVBAPI, "Added new streampid %04X with index %d to ca%d", pid, idx, cadevice);
8721
8722 return FIRST_STREAMPID_INDEX;
8723}
8724
8725int8_t remove_streampid_from_list(uint8_t cadevice, uint16_t pid, uint32_t idx)
8726{
8727 struct s_streampid *listitem;
8728 int8_t removed = 0;
8729 LL_ITER itr;
8730
8731 if(!ll_activestreampids)
8732 {
8733 return NO_STREAMPID_LISTED;
8734 }
8735
8736 if(idx >= INDEX_MAX)
8737 {
8738 return INVALID_STREAMPID_INDEX;
8739 }
8740
8741 if(ll_count(ll_activestreampids) > 0)
8742 {
8743 itr = ll_iter_create(ll_activestreampids);
8744 while((listitem = ll_iter_next(&itr)))
8745 {
8746 if(cadevice == listitem->cadevice && pid == listitem->streampid)
8747 {
8748 if(idx == INDEX_DISABLE_ALL)
8749 {
8750 listitem->activeindexers = 0;
8751 removed = 1;
8752 }
8753 else if((listitem->activeindexers & (1 << idx)) == (uint64_t) (1 << idx))
8754 {
8755 listitem->activeindexers &= ~(1 << idx); // flag it as disabled for this index
8756 removed = 1;
8757 }
8758
8759 if(removed)
8760 {
8761 cs_log_dbg(D_DVBAPI, "Remove streampid %04X using indexer %d from ca%d", pid, idx, cadevice);
8762 }
8763
8764 if(listitem->activeindexers == 0 && removed == 1) // all indexers disabled? -> remove pid from list!
8765 {
8766 ll_iter_remove_data(&itr);
8767 cs_log_dbg(D_DVBAPI, "Removed last indexer of streampid %04X from ca%d", pid, cadevice);
8768 ca_descramblers_used = count_active_indexers();
8769 return REMOVED_STREAMPID_LASTINDEX;
8770 }
8771 else if(removed == 1)
8772 {
8773 if(idx != INDEX_DISABLE_ALL && idx != listitem->caindex)
8774 {
8775 return REMOVED_STREAMPID_INDEX;
8776 }
8777 else
8778 {
8779 listitem->caindex = INDEX_INVALID;
8780 cs_log_dbg(D_DVBAPI, "Streampid %04X index %d was used for decoding on ca%d", pid, idx, cadevice);
8781 return REMOVED_DECODING_STREAMPID_INDEX;
8782 }
8783 }
8784 return INVALID_STREAMPID_INDEX;
8785 }
8786 }
8787 }
8788 return NO_STREAMPID_LISTED;
8789}
8790
8791void disable_unused_streampids(int16_t demux_id)
8792{
8793 int32_t ecmpid = demux[demux_id].pidindex;
8794
8795 if(ecmpid == -1 // no active ecmpid!
8796 || !ll_activestreampids
8797 || selected_api == STAPI // stapi handles pids itself!
8798 || ll_count(ll_activestreampids) == 0) // no items in list?
8799 {
8800 return;
8801 }
8802
8803 int32_t j;
8804 if(demux[demux_id].ECMpids[ecmpid].useMultipleIndices == 0)
8805 {
8806 uint32_t idx = demux[demux_id].ECMpids[ecmpid].index[0];
8807 int32_t i, n;
8808 struct s_streampid *listitem;
8809
8810 // search for old enabled streampids on
8811 // all ca devices that have to be disabled
8812 for(i = 0; i < CA_MAX && idx != INDEX_INVALID; i++)
8813 {
8814 if(!((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
8815 {
8816 continue; // ca is not used by this demuxer
8817 }
8818
8819 LL_ITER itr;
8820 itr = ll_iter_create(ll_activestreampids);
8821
8822 while((listitem = ll_iter_next(&itr)))
8823 {
8824 if(i != listitem->cadevice)
8825 {
8826 continue; // ca doesn't match
8827 }
8828
8829 if(!((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx))))
8830 {
8831 continue; // index doesn't match
8832 }
8833
8834 for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
8835 {
8836 if(demux[demux_id].ECMpidcount == 0) // FTA? -> disable stream!
8837 {
8838 n = demux[demux_id].STREAMpidcount;
8839 break;
8840 }
8841
8842 // check if pid matches with current stream pid on demuxer
8843 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8844 {
8845 break;
8846 }
8847 }
8848
8849 if(n == demux[demux_id].STREAMpidcount) // no match found
8850 {
8851 demux[demux_id].STREAMpids[n] = listitem->streampid; // put it here temporarily!
8852 dvbapi_set_pid(demux_id, n, idx, false, false, 0); // disable this unused streampid
8853 demux[demux_id].STREAMpids[n] = 0; // remove it from temp position!
8854 }
8855 }
8856
8857 // ECMpidcount != 0 -> skip enabling on fta
8858 for(n = 0; n < demux[demux_id].STREAMpidcount && demux[demux_id].ECMpidcount != 0; n++)
8859 {
8860 ll_iter_reset(&itr);
8861 if(!demux[demux_id].ECMpids[ecmpid].streams || ((demux[demux_id].ECMpids[ecmpid].streams & (1 << n)) == (uint) (1 << n)))
8862 {
8863 while((listitem = ll_iter_next(&itr)))
8864 {
8865 if(i != listitem->cadevice) // ca doesn't match
8866 {
8867 continue;
8868 }
8869
8870 if(!((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))) // index doesn't match
8871 {
8872 continue;
8873 }
8874
8875 // check if pid matches with current streampid on demuxer
8876 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8877 {
8878 break;
8879 }
8880 }
8881
8882 if(!listitem) // if streampid not listed -> enable it!
8883 {
8884 dvbapi_set_pid(demux_id, n, idx, true, false, 0); // enable streampid
8885 }
8886 }
8887 }
8888 }
8889 }
8890 else
8891 {
8892 uint32_t idx = INDEX_INVALID;
8893 int32_t i, n;
8894 uint8_t skip;
8895 struct s_streampid *listitem;
8896
8897 // search for old enabled streampids
8898 // on all ca devices that have to be disabled
8899 for(i = 0; i < CA_MAX && idx != INDEX_INVALID; i++)
8900 {
8901 if(!((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
8902 {
8903 continue; // continue if ca is unused by this demuxer
8904 }
8905
8906 LL_ITER itr;
8907 itr = ll_iter_create(ll_activestreampids);
8908
8909 while((listitem = ll_iter_next(&itr)))
8910 {
8911 if(i != listitem->cadevice)
8912 {
8913 continue; // ca doesn't match
8914 }
8915
8916 for(skip = 1, j = 0; j < MAX_STREAM_INDICES; j++)
8917 {
8918 idx = demux[demux_id].ECMpids[ecmpid].index[j];
8919 if(idx == INDEX_INVALID)
8920 {
8921 continue;
8922 }
8923
8924 // index match
8925 if((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))
8926 {
8927 skip = 0;
8928 break;
8929 }
8930 }
8931
8932 if(skip)
8933 {
8934 continue;
8935 }
8936
8937 for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
8938 {
8939 if(demux[demux_id].ECMpidcount == 0) // FTA? -> disable stream!
8940 {
8941 n = demux[demux_id].STREAMpidcount;
8942 break;
8943 }
8944
8945 // check if pid matches with current streampid on demuxer
8946 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8947 {
8948 break;
8949 }
8950 }
8951
8952 if(n == demux[demux_id].STREAMpidcount)
8953 {
8954 demux[demux_id].STREAMpids[n] = listitem->streampid; // put it temp here!
8955 dvbapi_set_pid(demux_id, n, idx, false, false, 0); // no match found so disable this now unused streampid
8956 demux[demux_id].STREAMpids[n] = 0; // remove temp!
8957 }
8958 }
8959
8960 // ECMpidcount != 0 -> skip enabling on fta
8961 for(n = 0; n < demux[demux_id].STREAMpidcount && demux[demux_id].ECMpidcount != 0; n++)
8962 {
8963 ll_iter_reset(&itr);
8964 if(!demux[demux_id].ECMpids[ecmpid].streams || ((demux[demux_id].ECMpids[ecmpid].streams & (1 << n)) == (uint) (1 << n)))
8965 {
8966 while((listitem = ll_iter_next(&itr)))
8967 {
8968 if(i != listitem->cadevice) // ca doesn't match
8969 {
8970 continue;
8971 }
8972
8973 for(skip = 1, j = 0; j < MAX_STREAM_INDICES; j++)
8974 {
8975 idx = demux[demux_id].ECMpids[ecmpid].index[j];
8976 if(idx == INDEX_INVALID)
8977 {
8978 continue;
8979 }
8980
8981 if((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))
8982 {
8983 skip = 0; // index match
8984 break;
8985 }
8986 }
8987
8988 if(skip)
8989 {
8990 continue;
8991 }
8992
8993 // check if pid matches with current streampid on demuxer
8994 if(listitem->streampid == demux[demux_id].STREAMpids[n])
8995 {
8996 break;
8997 }
8998 }
8999
9000 if(!listitem) // if streampid not listed -> enable it!
9001 {
9002 dvbapi_set_pid(demux_id, n, idx, true, false, 0); // enable streampid
9003 }
9004 }
9005 }
9006 }
9007 }
9008}
9009
9010uint32_t is_ca_used(uint8_t cadevice, int32_t pid)
9011{
9012 struct s_streampid *listitem;
9013 LL_ITER itr;
9014 if(!ll_activestreampids)
9015 {
9016 return INDEX_INVALID;
9017 }
9018
9019 if(ll_count(ll_activestreampids) > 0)
9020 {
9021 itr = ll_iter_create(ll_activestreampids);
9022 while((listitem = ll_iter_next(&itr)))
9023 {
9024 // if pid is 0, we match ca device only
9025 if(listitem->cadevice != cadevice || (pid && listitem->streampid != pid))
9026 {
9027 continue;
9028 }
9029
9030 uint32_t i = 0;
9031 while(listitem->caindex == INDEX_INVALID && i < INDEX_MAX)
9032 {
9033 if((listitem->activeindexers & (1 << i)) == (uint64_t) (1 << i))
9034 {
9035 listitem->caindex = i; // set fresh one
9036 cs_log_dbg(D_DVBAPI, "Streampid %04X is now using index %d for decoding on ca%d", pid, i, cadevice);
9037 break;
9038 }
9039 i++;
9040 }
9041
9042 if(listitem->caindex == INDEX_INVALID)
9043 {
9044 ll_iter_remove_data(&itr);
9045 return INDEX_INVALID;
9046 }
9047 return listitem->caindex;
9048 }
9049 }
9050 return INDEX_INVALID; // no indexer found for this pid!
9051}
9052
9053uint32_t count_active_indexers(void)
9054{
9055 uint i, usecounter = 0;
9056 struct s_streampid *listitem;
9057 LL_ITER itr;
9058
9059 if(!ll_activestreampids)
9060 {
9061 return 0;
9062 }
9063
9064 bool indexer_in_use[ca_descramblers_total];
9065 memset(&indexer_in_use, 0, sizeof(indexer_in_use));
9066
9067 if(ll_count(ll_activestreampids) > 0)
9068 {
9069 itr = ll_iter_create(ll_activestreampids);
9070 while((listitem = ll_iter_next(&itr)))
9071 {
9072 if(listitem->caindex != INDEX_INVALID && listitem->caindex < INDEX_MAX)
9073 {
9074 indexer_in_use[listitem->caindex] = true;
9075 }
9076 }
9077
9078 for(i = 0; i < ca_descramblers_total; i++)
9079 {
9080 if(indexer_in_use[i] == true)
9081 {
9082 usecounter++;
9083 }
9084 }
9085 }
9086
9087 return usecounter;
9088}
9089
9090uint16_t dvbapi_get_client_proto_version(void)
9091{
9092 return last_client_proto_version;
9093}
9094
9095const char *dvbapi_get_client_name(void)
9096{
9097 return last_client_name ? last_client_name : "";
9098}
9099
9100void check_add_emmpid(int32_t demux_id, uint8_t *filter, int32_t l, int32_t emmtype)
9101{
9102 if(l < 0) { return; }
9103
9104 uint32_t typtext_idx = 0;
9105 int32_t ret = -1;
9106 const char *typtext[] = { "UNIQUE", "SHARED", "GLOBAL", "UNKNOWN" };
9107
9108 while(((emmtype >> typtext_idx) & 0x01) == 0 && typtext_idx < sizeof(typtext) / sizeof(const char *))
9109 {
9110 ++typtext_idx;
9111 }
9112
9113 // filter already in list?
9114 if(is_emmfilter_in_list(filter, demux[demux_id].EMMpids[l].PID, demux[demux_id].EMMpids[l].PROVID, demux[demux_id].EMMpids[l].CAID))
9115 {
9116 cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate emm filter type %s, emmpid: 0x%04X, emmcaid: %04X, emmprovid: %06X -> SKIPPED!",
9117 demux_id,
9118 typtext[typtext_idx],
9119 demux[demux_id].EMMpids[l].PID,
9120 demux[demux_id].EMMpids[l].CAID,
9121 demux[demux_id].EMMpids[l].PROVID);
9122 return;
9123 }
9124
9125 if(demux[demux_id].emm_filter < demux[demux_id].max_emm_filter) // can this filter be started?
9126 {
9127 // try to activate this emmfilter
9128 ret = dvbapi_set_filter(demux_id,
9129 selected_api,
9130 demux[demux_id].EMMpids[l].PID,
9131 demux[demux_id].EMMpids[l].CAID,
9132 demux[demux_id].EMMpids[l].PROVID,
9133 filter,
9134 filter + 16,
9135 0,
9136 demux[demux_id].pidindex,
9137 TYPE_EMM, 1);
9138 }
9139
9140 if(ret != -1) // -1 if maxfilter reached or filter start error!
9141 {
9142 if(demux[demux_id].emm_filter == -1) // -1: first run of emm filtering on this demuxer
9143 {
9144 demux[demux_id].emm_filter = 0;
9145 }
9146 demux[demux_id].emm_filter++; // increase total active filters
9147
9148 cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d started emm filter type %s, pid: 0x%04X",
9149 demux_id, typtext[typtext_idx], demux[demux_id].EMMpids[l].PID);
9150 return;
9151 }
9152 else // not set successful, so add it to the list for try again later on!
9153 {
9154 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);
9155 cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d added inactive emm filter type %s, pid: 0x%04X",
9156 demux_id, typtext[typtext_idx], demux[demux_id].EMMpids[l].PID);
9157 }
9158 return;
9159}
9160
9161void rotate_emmfilter(int32_t demux_id)
9162{
9163 // emm filter iteration
9164 if(!ll_emm_active_filter)
9165 {
9166 ll_emm_active_filter = ll_create("ll_emm_active_filter");
9167 }
9168
9169 if(!ll_emm_inactive_filter)
9170 {
9171 ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
9172 }
9173
9174 if(!ll_emm_pending_filter)
9175 {
9176 ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
9177 }
9178
9179 uint32_t filter_count = ll_count(ll_emm_active_filter) + ll_count(ll_emm_inactive_filter);
9180 if(demux[demux_id].max_emm_filter > 0 && ll_count(ll_emm_inactive_filter) > 0 && filter_count > demux[demux_id].max_emm_filter)
9181 {
9182 int32_t filter_queue = ll_count(ll_emm_inactive_filter);
9183 int32_t stopped = 0, started = 0;
9184 struct timeb now;
9185 cs_ftime(&now);
9186 struct s_emm_filter *filter_item;
9187 LL_ITER itr;
9188 itr = ll_iter_create(ll_emm_active_filter);
9189
9190 while((filter_item = ll_iter_next(&itr)) != NULL)
9191 {
9192 if(!ll_count(ll_emm_inactive_filter) || started == filter_queue)
9193 {
9194 break;
9195 }
9196
9197 int64_t gone = comp_timeb(&now, &filter_item->time_started);
9198 if(gone > 45 * 1000)
9199 {
9200 struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match_emmpid(filter_item->demux_id, filter_item->caid, filter_item->provid, 'p');
9201 if(!forceentry || (forceentry && !forceentry->force))
9202 {
9203 // stop active filter and add to pending list
9204 dvbapi_stop_filternum(filter_item->demux_id, filter_item->num - 1, 0);
9205 ll_iter_remove_data(&itr);
9206 add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, -1, false);
9207 stopped++;
9208 }
9209 }
9210
9211 int32_t ret;
9212 if(stopped > started) // we have room for new filters, try to start an inactive emmfilter!
9213 {
9214 struct s_emm_filter *filter_item2;
9215 LL_ITER itr2 = ll_iter_create(ll_emm_inactive_filter);
9216 while((filter_item2 = ll_iter_next(&itr2)))
9217 {
9218 ret = dvbapi_set_filter(filter_item2->demux_id,
9219 selected_api,
9220 filter_item2->pid,
9221 filter_item2->caid,
9222 filter_item2->provid,
9223 filter_item2->filter,
9224 filter_item2->filter + 16,
9225 0,
9226 demux[filter_item2->demux_id].pidindex,
9227 TYPE_EMM, 1);
9228 if(ret != -1)
9229 {
9230 ll_iter_remove_data(&itr2);
9231 started++;
9232 break;
9233 }
9234 }
9235 }
9236 }
9237
9238 itr = ll_iter_create(ll_emm_pending_filter);
9239 while((filter_item = ll_iter_next(&itr)) != NULL) // move pending filters to inactive
9240 {
9241 add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, 0, false);
9242 ll_iter_remove_data(&itr);
9243 }
9244 }
9245}
9246
9247int32_t filtermatch(uint8_t *buffer, int32_t filter_num, int32_t demux_id, int32_t len)
9248{
9249 int32_t i, k, match;
9250 uint8_t flt, mask;
9251
9252 match = 1;
9253 for(i = 0, k = 0; i < 16 && match; i++, k++)
9254 {
9255 mask = demux[demux_id].demux_fd[filter_num].mask[i];
9256 if(k == 1) // skip len bytes
9257 {
9258 k += 2;
9259 }
9260
9261 if(!mask)
9262 {
9263 continue;
9264 }
9265
9266 flt = (demux[demux_id].demux_fd[filter_num].filter[i]&mask);
9267 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",
9268 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);
9269
9270 if(k <= len)
9271 {
9272 match = (flt == (buffer[k] & mask));
9273 }
9274 else
9275 {
9276 match = 0;
9277 }
9278 }
9279 return (match && i == 16); // 0 = delivered data does not match with filter, 1 = delivered data matches with filter
9280}
9281
9282/*
9283 * protocol structure
9284 */
9285void module_dvbapi(struct s_module *ph)
9286{
9287 ph->desc = "dvbapi";
9288 ph->type = MOD_CONN_SERIAL;
9289 ph->listenertype = LIS_DVBAPI;
9290#if defined(WITH_AZBOX)
9291 ph->s_handler = azbox_handler;
9292 ph->send_dcw = azbox_send_dcw;
9293#elif defined(WITH_MCA)
9294 ph->s_handler = mca_handler;
9295 ph->send_dcw = mca_send_dcw;
9296 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
9297#else
9298 ph->s_handler = dvbapi_handler;
9299 ph->send_dcw = dvbapi_send_dcw;
9300#endif
9301}
9302#endif // HAVE_DVBAPI
Note: See TracBrowser for help on using the repository browser.