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"
|
---|
28 |
|
---|
29 | extern int32_t exit_oscam;
|
---|
30 |
|
---|
31 | #if defined (__CYGWIN__)
|
---|
32 | #define F_NOTIFY 0
|
---|
33 | #define F_SETSIG 0
|
---|
34 | #define DN_MODIFY 0
|
---|
35 | #define DN_CREATE 0
|
---|
36 | #define DN_DELETE 0
|
---|
37 | #define DN_MULTISHOT 0
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | static const char *get_stream_type_txt(uint8_t stream_type)
|
---|
41 | {
|
---|
42 | switch(stream_type)
|
---|
43 | {
|
---|
44 | case 0x00: return "invalid";
|
---|
45 | case 0x01: return "MPEG-1 video";
|
---|
46 | case 0x02: return "MPEG-2 video"; // MPEG-1 (constrained parameter) or MPEG-2 video
|
---|
47 | case 0x03: return "MPEG-1 audio"; // MP1, MP2, MP3
|
---|
48 | case 0x04: return "MPEG-2 audio"; // MP1, MP2, MP3
|
---|
49 | case 0x05: return "MPEG-2 private sections";
|
---|
50 | case 0x06: return "MPEG-2 PES private data"; // AC-3, Enhanced AC-3, AC-4, DTS(-HD) audio, subtitles, etc (DVB) (depends on descriptor)
|
---|
51 | case 0x07: return "MHEG data";
|
---|
52 | case 0x08: return "DSM-CC data";
|
---|
53 | case 0x09: return "MPEG-2 over ATM data";
|
---|
54 | case 0x0A: return "DSM-CC data";
|
---|
55 | case 0x0B: return "DSM-CC data";
|
---|
56 | case 0x0C: return "DSM-CC data";
|
---|
57 | case 0x0D: return "DSM-CC data";
|
---|
58 | case 0x0E: return "MPEG-2 auxiliary data";
|
---|
59 | case 0x0F: return "MPEG-2 audio"; // AAC
|
---|
60 | case 0x10: return "MPEG-4 video";
|
---|
61 | case 0x11: return "MPEG-4 audio"; // AAC, HE AAC and AAC v2
|
---|
62 | case 0x12: return "MPEG-4 PES data";
|
---|
63 | case 0x13: return "MPEG-4 data";
|
---|
64 | case 0x14: return "DSM-CC data";
|
---|
65 | case 0x15: return "MPEG-7 MetaData";
|
---|
66 | case 0x16: return "MPEG-7 MetaData";
|
---|
67 | case 0x17: return "MPEG-7 MetaData";
|
---|
68 | case 0x18: return "MPEG-7 MetaData";
|
---|
69 | case 0x19: return "MPEG-7 MetaData";
|
---|
70 | case 0x1A: return "MPEG-2 IPMP data";
|
---|
71 | case 0x1B: return "AVC video";
|
---|
72 | case 0x1C: return "MPEG-4 audio"; // DST, ALS, SLS
|
---|
73 | case 0x24: return "HEVC video";
|
---|
74 | case 0x25: return "HEVC subset video";
|
---|
75 | case 0x2D: return "MPEG-H 3D audio"; // main stream
|
---|
76 | case 0x2E: return "MPEG-H 3D audio"; // auxiliary stream
|
---|
77 | case 0x42: return "Chinese video";
|
---|
78 | case 0x7F: return "IPMP data";
|
---|
79 | case 0x81: return "AC-3 audio (ATSC)"; // with descriptor tag 0x81
|
---|
80 | case 0x86: return "SCTE 35 data";
|
---|
81 | case 0x87: return "enhanced AC-3 audio (ATSC)";
|
---|
82 | //case 0x88: return "DTS-HD audio (ATSC 2.0)"; // fixme: has ATSC 2.0 ever been used?
|
---|
83 | //case 0x??: return "AC-4 audio (ATSC 3.0)"; // fixme: add the actual value when it gets published
|
---|
84 | //case 0x??: return "MPEG-H 3D audio (ATSC 3.0)"; // fixme: add the actual value when it gets published
|
---|
85 | case 0xD1: return "BBC Dirac video";
|
---|
86 | case 0xEA: return "VC-1 video";
|
---|
87 | default: return "user private";
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | static const char *get_descriptor_tag_txt(uint8_t descriptor_tag)
|
---|
92 | {
|
---|
93 | switch(descriptor_tag)
|
---|
94 | {
|
---|
95 | // Valid in all MPEG contexts:
|
---|
96 | case 0x00: return "reserved";
|
---|
97 | case 0x01: return "forbidden";
|
---|
98 | case 0x02: return "video stream";
|
---|
99 | case 0x03: return "audio stream";
|
---|
100 | case 0x04: return "hierarchy";
|
---|
101 | case 0x05: return "registration";
|
---|
102 | case 0x06: return "data stream alignment";
|
---|
103 | case 0x07: return "target background grid";
|
---|
104 | case 0x08: return "video window";
|
---|
105 | case 0x09: return "CA";
|
---|
106 | case 0x0A: return "ISO 639 language";
|
---|
107 | case 0x0B: return "system clock";
|
---|
108 | case 0x0C: return "multiplex buffer utilization";
|
---|
109 | case 0x0D: return "copyright";
|
---|
110 | case 0x0E: return "maximum bitrate";
|
---|
111 | case 0x0F: return "private data indicator";
|
---|
112 | case 0x10: return "smoothing buffer";
|
---|
113 | case 0x11: return "STD";
|
---|
114 | case 0x12: return "IBP";
|
---|
115 | case 0x13: return "DSM-CC carousel identifier";
|
---|
116 | case 0x14: return "DSM-CC association tag";
|
---|
117 | case 0x15: return "DSM-CC deferred association tags";
|
---|
118 | case 0x17: return "DSM-CC NPT reference";
|
---|
119 | case 0x18: return "DSM-CC NPT endpoint";
|
---|
120 | case 0x19: return "DSM-CC stream mode";
|
---|
121 | case 0x1A: return "DSM-CC stream event";
|
---|
122 | case 0x1B: return "MPEG-4 video";
|
---|
123 | case 0x1C: return "MPEG-4 audio";
|
---|
124 | case 0x1D: return "IOD";
|
---|
125 | case 0x1E: return "SL";
|
---|
126 | case 0x1F: return "FMC";
|
---|
127 | case 0x20: return "External ES id";
|
---|
128 | case 0x21: return "MuxCode";
|
---|
129 | case 0x22: return "FmxBufferSize";
|
---|
130 | case 0x23: return "MultiplexBuffer";
|
---|
131 | case 0x24: return "Content labeling";
|
---|
132 | case 0x25: return "Metadata association";
|
---|
133 | case 0x26: return "Metadata";
|
---|
134 | case 0x27: return "Metadata STD";
|
---|
135 | case 0x28: return "AVC video";
|
---|
136 | case 0x29: return "MPEG-2 IPMP";
|
---|
137 | case 0x2A: return "AVC timing and HRD";
|
---|
138 | case 0x2B: return "MPEG-2 AAC Audio";
|
---|
139 | case 0x2C: return "FlexMuxTiming";
|
---|
140 | case 0x2D: return "MPEG-4 Text";
|
---|
141 | case 0x2E: return "MPEG-4 Audio Extension";
|
---|
142 | case 0x2F: return "Auxiliary Video Stream";
|
---|
143 | case 0x30: return "SVC Extension";
|
---|
144 | case 0x31: return "MVC Extension";
|
---|
145 | case 0x32: return "J2K Video";
|
---|
146 | case 0x33: return "MVC Operation Point";
|
---|
147 | case 0x34: return "MPEG-2 Stereoscopic Video Format";
|
---|
148 | case 0x35: return "Stereoscopic Program Info";
|
---|
149 | case 0x36: return "Stereoscopic Video Info";
|
---|
150 | case 0x37: return "Transport Profile";
|
---|
151 | case 0x38: return "HEVC Video";
|
---|
152 | case 0x3F: return "MPEG-2 Extension";
|
---|
153 | // Valid in DVB context:
|
---|
154 | case 0x45: return "VBI data";
|
---|
155 | case 0x46: return "VBI teletext";
|
---|
156 | case 0x51: return "mosaic";
|
---|
157 | case 0x52: return "stream identifier";
|
---|
158 | case 0x56: return "teletext";
|
---|
159 | case 0x59: return "subtitling"; // with stream type 0x06
|
---|
160 | case 0x5F: return "private data specifier";
|
---|
161 | case 0x60: return "service move";
|
---|
162 | case 0x65: return "scrambling";
|
---|
163 | case 0x66: return "data broadcast id";
|
---|
164 | case 0x6A: return "AC-3"; // with stream type 0x06
|
---|
165 | case 0x6B: return "ancillary data";
|
---|
166 | case 0x6F: return "application signalling";
|
---|
167 | case 0x70: return "adaptation field data";
|
---|
168 | case 0x74: return "related content";
|
---|
169 | case 0x78: return "ECM repetition rate";
|
---|
170 | case 0x7A: return "enhanced AC-3"; // with stream type 0x06
|
---|
171 | case 0x7B: return "DTS"; // with stream type 0x06
|
---|
172 | case 0x7C: return "AAC"; // with stream type 0x06
|
---|
173 | case 0x7D: return "XAIT location";
|
---|
174 | case 0x7F: return "DVB extension";
|
---|
175 | default: return "user private";
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | static const char *get_extension_descriptor_txt(uint8_t extension_tag)
|
---|
180 | {
|
---|
181 | switch(extension_tag)
|
---|
182 | {
|
---|
183 | case 0x02: return "CP";
|
---|
184 | case 0x06: return "supplementary audio";
|
---|
185 | case 0x0E: return "DTS-HD"; // with stream type 0x06
|
---|
186 | case 0x0F: return "DTS Neural"; // with stream type 0x06
|
---|
187 | case 0x11: return "T2MI";
|
---|
188 | case 0x13: return "URI linkage";
|
---|
189 | case 0x15: return "AC-4"; // with stream type 0x06
|
---|
190 | case 0x18: return "protection message";
|
---|
191 | case 0x19: return "audio preselection";
|
---|
192 | case 0x20: return "TTML subtitling"; // (could become 0x1A, value in A038 draft seems weird)
|
---|
193 | default: return "Undefined";
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | void flush_read_fd(int32_t demux_id, int32_t num, int fd)
|
---|
198 | {
|
---|
199 | if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
|
---|
200 | {
|
---|
201 | cs_log_dbg(D_DVBAPI,"Demuxer %d flushing stale input data of filter %d (fd:%d)", demux_id, num + 1, fd);
|
---|
202 |
|
---|
203 | fd_set rd;
|
---|
204 | struct timeval t;
|
---|
205 | char buff[100];
|
---|
206 |
|
---|
207 | t.tv_sec = 0;
|
---|
208 | t.tv_usec = 0;
|
---|
209 |
|
---|
210 | FD_ZERO(&rd);
|
---|
211 | FD_SET(fd, &rd);
|
---|
212 |
|
---|
213 | while(select(fd + 1, &rd, NULL, NULL, &t) > 0)
|
---|
214 | {
|
---|
215 | if(read(fd, buff, 100)) { ; }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | static int dvbapi_ioctl(int fd, uint32_t request, ...)
|
---|
221 | {
|
---|
222 | int ret = 0;
|
---|
223 | va_list args;
|
---|
224 | va_start(args, request);
|
---|
225 | if(!(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO))
|
---|
226 | {
|
---|
227 | void *param = va_arg(args, void *);
|
---|
228 | ret = ioctl(fd, request, param);
|
---|
229 | }
|
---|
230 | else
|
---|
231 | {
|
---|
232 | switch(request)
|
---|
233 | {
|
---|
234 | case DMX_SET_FILTER:
|
---|
235 | {
|
---|
236 | struct dmx_sct_filter_params *sFP = va_arg(args, struct dmx_sct_filter_params *);
|
---|
237 | //fix filter for samygo
|
---|
238 | //note: we only have 14 available filter bytes (instead of 16) on samygo
|
---|
239 | memmove(&sFP->filter.filter[3], &sFP->filter.filter[1], 13);
|
---|
240 | memset(&sFP->filter.filter[1], 0, 2);
|
---|
241 | memmove(&sFP->filter.mask[3], &sFP->filter.mask[1], 13);
|
---|
242 | memset(&sFP->filter.mask[1], 0, 2);
|
---|
243 | // prepare packet
|
---|
244 | uint8_t packet[sizeof(request) + sizeof(struct dmx_sct_filter_params)];
|
---|
245 | memcpy(&packet, &request, sizeof(request));
|
---|
246 | memcpy(&packet[sizeof(request)], sFP, sizeof(struct dmx_sct_filter_params));
|
---|
247 | ret = send(fd, packet, sizeof(packet), 0);
|
---|
248 | break;
|
---|
249 | }
|
---|
250 |
|
---|
251 | case DMX_SET_FILTER1:
|
---|
252 | {
|
---|
253 | cs_log("error: samygo does not support DMX_SET_FILTER1");
|
---|
254 | ret = -1;
|
---|
255 | break;
|
---|
256 | }
|
---|
257 |
|
---|
258 | case DMX_STOP:
|
---|
259 | {
|
---|
260 | ret = send(fd, &request, sizeof(request), 0);
|
---|
261 | ret = 1;
|
---|
262 | break;
|
---|
263 | }
|
---|
264 |
|
---|
265 | case CA_GET_DESCR_INFO:
|
---|
266 | {
|
---|
267 | ca_descr_info_t *ca_descr_info = va_arg(args, ca_descr_info_t *);
|
---|
268 | // preparing packet
|
---|
269 | uint8_t packet[sizeof(request) + sizeof(ca_descr_info_t)];
|
---|
270 | memcpy(&packet[0], &request, sizeof(request));
|
---|
271 | memcpy(&packet[sizeof(request)], ca_descr_info, sizeof(ca_descr_info_t));
|
---|
272 | // sending data to UDP
|
---|
273 | ret = send(fd, &packet[0], sizeof(packet), 0);
|
---|
274 | break;
|
---|
275 | }
|
---|
276 |
|
---|
277 | case CA_SET_DESCR:
|
---|
278 | {
|
---|
279 | ca_descr_t *ca_descr = va_arg(args, ca_descr_t *);
|
---|
280 | // preparing packet
|
---|
281 | uint8_t packet[sizeof(request) + sizeof(ca_descr_t)];
|
---|
282 | memcpy(&packet[0], &request, sizeof(request));
|
---|
283 | memcpy(&packet[sizeof(request)], ca_descr, sizeof(ca_descr_t));
|
---|
284 | // sending data to UDP
|
---|
285 | ret = send(fd, &packet[0], sizeof(packet), 0);
|
---|
286 | break;
|
---|
287 | }
|
---|
288 |
|
---|
289 | case CA_SET_PID:
|
---|
290 | {
|
---|
291 | ca_pid_t *ca_pid2 = va_arg(args, ca_pid_t *);
|
---|
292 | // preparing packet
|
---|
293 | uint8_t packet[sizeof(request) + sizeof(ca_pid_t)];
|
---|
294 | memcpy(&packet[0], &request, sizeof(request));
|
---|
295 | memcpy(&packet[sizeof(request)], ca_pid2, sizeof(ca_pid_t));
|
---|
296 | // sending data to UDP
|
---|
297 | ret = send(fd, &packet[0], sizeof(packet), 0);
|
---|
298 | break;
|
---|
299 | }
|
---|
300 |
|
---|
301 | case CA_SET_DESCR_MODE:
|
---|
302 | {
|
---|
303 | cs_log("error: samygo does not support CA_SET_DESCR_MODE");
|
---|
304 | ret = -1;
|
---|
305 | break;
|
---|
306 | }
|
---|
307 |
|
---|
308 | case CA_SET_DESCR_DATA:
|
---|
309 | {
|
---|
310 | cs_log("error: samygo does not support CA_SET_DESCR_DATA");
|
---|
311 | ret = -1;
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | if(ret > 0) // send() may return larger than 1
|
---|
317 | {
|
---|
318 | ret = 1;
|
---|
319 | }
|
---|
320 | }
|
---|
321 | #if defined(__powerpc__)
|
---|
322 | // Old dm500 boxes (ppc old) are using broken kernel, se we need some fixups
|
---|
323 | switch (request)
|
---|
324 | {
|
---|
325 | case DMX_STOP:
|
---|
326 | case CA_SET_DESCR:
|
---|
327 | case CA_SET_PID:
|
---|
328 | ret = 1;
|
---|
329 | }
|
---|
330 | #endif
|
---|
331 | // FIXME: Workaround for su980 bug
|
---|
332 | // See: http://www.streamboard.tv/wbb2/thread.php?postid=533940
|
---|
333 | if(boxtype_is("su980"))
|
---|
334 | {
|
---|
335 | ret = 1;
|
---|
336 | }
|
---|
337 | va_end(args);
|
---|
338 | return ret;
|
---|
339 | }
|
---|
340 |
|
---|
341 | // tunemm_caid_map
|
---|
342 | #define FROM_TO 0
|
---|
343 | #define TO_FROM 1
|
---|
344 |
|
---|
345 | #if defined(CARDREADER_STAPI) || defined(CARDREADER_STAPI5)
|
---|
346 | //fix from stapi5 patch
|
---|
347 | int32_t pausecam = 0, disable_pmt_files = 0, pmt_stopmarking = 1;
|
---|
348 | #else
|
---|
349 | int32_t pausecam = 0, disable_pmt_files = 0, pmt_stopmarking = 0;
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | DEMUXTYPE demux[MAX_DEMUX];
|
---|
353 | struct s_dvbapi_priority *dvbapi_priority;
|
---|
354 | struct s_client *dvbapi_client;
|
---|
355 |
|
---|
356 | const char *boxdesc[] = { "none", "dreambox", "duckbox", "ufs910", "dbox2", "ipbox", "ipbox-pmt",
|
---|
357 | "dm7000", "qboxhd", "coolstream", "neumo", "pc", "pc-nodmx", "samygo" };
|
---|
358 |
|
---|
359 | // when updating devices[BOX_COUNT] make sure to update these index defines
|
---|
360 | #define BOX_INDEX_QBOXHD 0
|
---|
361 | #define BOX_INDEX_DREAMBOX_DVBAPI3 1
|
---|
362 | #define BOX_INDEX_COOLSTREAM 6
|
---|
363 |
|
---|
364 | static const struct box_devices devices[BOX_COUNT] =
|
---|
365 | {
|
---|
366 | /* QboxHD (dvb-api-3)*/ { "/tmp/virtual_adapter/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
|
---|
367 | /* dreambox (dvb-api-3)*/ { "/dev/dvb/adapter%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
|
---|
368 | /* wetek (dvb-api-3)*/ { "/dev/dvb%d.", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
|
---|
369 | /* dreambox (dvb-api-1)*/ { "/dev/dvb/card%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_1 },
|
---|
370 | /* neumo (dvb-api-1)*/ { "/dev/", "demuxapi", "demuxapi", "/tmp/camd.socket", DVBAPI_1 },
|
---|
371 | #ifdef WITH_STAPI5
|
---|
372 | /* sh4 (stapi5)*/ { "/dev/stapi/", "stpti5_ioctl", "stpti5_ioctl", "/tmp/camd.socket", STAPI },
|
---|
373 | #else
|
---|
374 | /* sh4 (stapi)*/ { "/dev/stapi/", "stpti4_ioctl", "stpti4_ioctl", "/tmp/camd.socket", STAPI },
|
---|
375 | #endif
|
---|
376 | /* coolstream*/ { "/dev/cnxt/", "null", "null", "/tmp/camd.socket", COOLAPI },
|
---|
377 | };
|
---|
378 |
|
---|
379 | static int32_t selected_box = -1;
|
---|
380 | static int32_t selected_api = -1;
|
---|
381 | static int32_t maxfilter = MAX_FILTER;
|
---|
382 | static int32_t dir_fd = -1;
|
---|
383 | static uint16_t last_client_proto_version = 0;
|
---|
384 | static char *last_client_name = NULL;
|
---|
385 | static uint32_t ca_descramblers_total = 0; // total number of available descramblers in box
|
---|
386 | static uint32_t ca_descramblers_used = 0; // total number of used descramblers during decoding
|
---|
387 | static int32_t ca_fd[CA_MAX]; // holds fd handle of all ca devices (0 not in use)
|
---|
388 | static LLIST *ll_activestreampids; // list of all enabled streampids on ca devices
|
---|
389 | static int32_t unassoc_fd[MAX_DEMUX];
|
---|
390 |
|
---|
391 | bool is_dvbapi_usr(char *usr)
|
---|
392 | {
|
---|
393 | return streq(cfg.dvbapi_usr, usr);
|
---|
394 | }
|
---|
395 |
|
---|
396 | struct s_emm_filter
|
---|
397 | {
|
---|
398 | int32_t demux_id;
|
---|
399 | uint8_t filter[32];
|
---|
400 | uint16_t caid;
|
---|
401 | uint32_t provid;
|
---|
402 | uint16_t pid;
|
---|
403 | uint32_t num;
|
---|
404 | struct timeb time_started;
|
---|
405 | };
|
---|
406 |
|
---|
407 | static LLIST *ll_emm_active_filter;
|
---|
408 | static LLIST *ll_emm_inactive_filter;
|
---|
409 | static LLIST *ll_emm_pending_filter;
|
---|
410 |
|
---|
411 | int32_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)
|
---|
412 | {
|
---|
413 | if(!ll_emm_active_filter)
|
---|
414 | {
|
---|
415 | ll_emm_active_filter = ll_create("ll_emm_active_filter");
|
---|
416 | }
|
---|
417 |
|
---|
418 | if(!ll_emm_inactive_filter)
|
---|
419 | {
|
---|
420 | ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
|
---|
421 | }
|
---|
422 |
|
---|
423 | if(!ll_emm_pending_filter)
|
---|
424 | {
|
---|
425 | ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
|
---|
426 | }
|
---|
427 |
|
---|
428 | struct s_emm_filter *filter_item;
|
---|
429 | if(!cs_malloc(&filter_item, sizeof(struct s_emm_filter)))
|
---|
430 | {
|
---|
431 | return 0;
|
---|
432 | }
|
---|
433 |
|
---|
434 | filter_item->demux_id = demux_id;
|
---|
435 | memcpy(filter_item->filter, filter, 32);
|
---|
436 | filter_item->caid = caid;
|
---|
437 | filter_item->provid = provid;
|
---|
438 | filter_item->pid = emmpid;
|
---|
439 | filter_item->num = num;
|
---|
440 |
|
---|
441 | if(enable)
|
---|
442 | {
|
---|
443 | cs_ftime(&filter_item->time_started);
|
---|
444 | }
|
---|
445 | else
|
---|
446 | {
|
---|
447 | memset(&filter_item->time_started, 0, sizeof(filter_item->time_started));
|
---|
448 | }
|
---|
449 |
|
---|
450 | if(num > 0)
|
---|
451 | {
|
---|
452 | ll_append(ll_emm_active_filter, filter_item);
|
---|
453 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d added to active emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
|
---|
454 | filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
|
---|
455 | }
|
---|
456 | else if(num < 0)
|
---|
457 | {
|
---|
458 | ll_append(ll_emm_pending_filter, filter_item);
|
---|
459 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to pending emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
|
---|
460 | filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | ll_append(ll_emm_inactive_filter, filter_item);
|
---|
465 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to inactive emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
|
---|
466 | filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
|
---|
467 | }
|
---|
468 | return 1;
|
---|
469 | }
|
---|
470 |
|
---|
471 | int32_t is_emmfilter_in_list_internal(LLIST *ll, uint8_t *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
|
---|
472 | {
|
---|
473 | struct s_emm_filter *filter_item;
|
---|
474 | LL_ITER itr;
|
---|
475 | if(ll_count(ll) > 0)
|
---|
476 | {
|
---|
477 | itr = ll_iter_create(ll);
|
---|
478 | while((filter_item = ll_iter_next(&itr)) != NULL)
|
---|
479 | {
|
---|
480 | if(!memcmp(filter_item->filter, filter, 32) && (filter_item->pid == emmpid)
|
---|
481 | && (filter_item->provid == provid) && (filter_item->caid == caid))
|
---|
482 | {
|
---|
483 | return 1;
|
---|
484 | }
|
---|
485 | }
|
---|
486 | }
|
---|
487 | return 0;
|
---|
488 | }
|
---|
489 |
|
---|
490 | int32_t is_emmfilter_in_list(uint8_t *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
|
---|
491 | {
|
---|
492 | if(!ll_emm_active_filter)
|
---|
493 | {
|
---|
494 | ll_emm_active_filter = ll_create("ll_emm_active_filter");
|
---|
495 | }
|
---|
496 |
|
---|
497 | if(!ll_emm_inactive_filter)
|
---|
498 | {
|
---|
499 | ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
|
---|
500 | }
|
---|
501 |
|
---|
502 | if(!ll_emm_pending_filter)
|
---|
503 | {
|
---|
504 | ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
|
---|
505 | }
|
---|
506 |
|
---|
507 | if(is_emmfilter_in_list_internal(ll_emm_active_filter, filter, emmpid, provid, caid)
|
---|
508 | || is_emmfilter_in_list_internal(ll_emm_inactive_filter, filter, emmpid, provid, caid)
|
---|
509 | || is_emmfilter_in_list_internal(ll_emm_pending_filter, filter, emmpid, provid, caid))
|
---|
510 | {
|
---|
511 | return 1;
|
---|
512 | }
|
---|
513 | return 0;
|
---|
514 | }
|
---|
515 |
|
---|
516 | struct s_emm_filter *get_emmfilter_by_filternum_internal(LLIST *ll, int32_t demux_id, uint32_t num)
|
---|
517 | {
|
---|
518 | struct s_emm_filter *filter;
|
---|
519 | LL_ITER itr;
|
---|
520 | if(ll_count(ll) > 0)
|
---|
521 | {
|
---|
522 | itr = ll_iter_create(ll);
|
---|
523 | while((filter = ll_iter_next(&itr)))
|
---|
524 | {
|
---|
525 | if(filter->demux_id == demux_id && filter->num == num)
|
---|
526 | {
|
---|
527 | return filter;
|
---|
528 | }
|
---|
529 | }
|
---|
530 | }
|
---|
531 | return NULL;
|
---|
532 | }
|
---|
533 |
|
---|
534 | struct s_emm_filter *get_emmfilter_by_filternum(int32_t demux_id, uint32_t num)
|
---|
535 | {
|
---|
536 | if(!ll_emm_active_filter)
|
---|
537 | {
|
---|
538 | ll_emm_active_filter = ll_create("ll_emm_active_filter");
|
---|
539 | }
|
---|
540 |
|
---|
541 | if(!ll_emm_inactive_filter)
|
---|
542 | {
|
---|
543 | ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
|
---|
544 | }
|
---|
545 |
|
---|
546 | if(!ll_emm_pending_filter)
|
---|
547 | {
|
---|
548 | ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
|
---|
549 | }
|
---|
550 |
|
---|
551 | struct s_emm_filter *emm_filter = NULL;
|
---|
552 |
|
---|
553 | emm_filter = get_emmfilter_by_filternum_internal(ll_emm_active_filter, demux_id, num);
|
---|
554 | if(emm_filter)
|
---|
555 | {
|
---|
556 | return emm_filter;
|
---|
557 | }
|
---|
558 |
|
---|
559 | emm_filter = get_emmfilter_by_filternum_internal(ll_emm_inactive_filter, demux_id, num);
|
---|
560 | if(emm_filter)
|
---|
561 | {
|
---|
562 | return emm_filter;
|
---|
563 | }
|
---|
564 |
|
---|
565 | emm_filter = get_emmfilter_by_filternum_internal(ll_emm_pending_filter, demux_id, num);
|
---|
566 | if(emm_filter)
|
---|
567 | {
|
---|
568 | return emm_filter;
|
---|
569 | }
|
---|
570 |
|
---|
571 | return NULL;
|
---|
572 | }
|
---|
573 |
|
---|
574 | int8_t remove_emmfilter_from_list_internal(LLIST *ll, int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
|
---|
575 | {
|
---|
576 | struct s_emm_filter *filter;
|
---|
577 | LL_ITER itr;
|
---|
578 |
|
---|
579 | if(ll_count(ll) > 0)
|
---|
580 | {
|
---|
581 | itr = ll_iter_create(ll);
|
---|
582 | while((filter = ll_iter_next(&itr)))
|
---|
583 | {
|
---|
584 | if(filter->demux_id == demux_id && filter->caid == caid && filter->provid == provid && filter->pid == pid && filter->num == num)
|
---|
585 | {
|
---|
586 | ll_iter_remove_data(&itr);
|
---|
587 | return 1;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 | return 0;
|
---|
592 | }
|
---|
593 |
|
---|
594 | void remove_emmfilter_from_list(int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
|
---|
595 | {
|
---|
596 | if((ll_emm_active_filter && remove_emmfilter_from_list_internal(ll_emm_active_filter, demux_id, caid, provid, pid, num))
|
---|
597 | || (ll_emm_inactive_filter && remove_emmfilter_from_list_internal(ll_emm_inactive_filter, demux_id, caid, provid, pid, num))
|
---|
598 | || (ll_emm_pending_filter && remove_emmfilter_from_list_internal(ll_emm_pending_filter, demux_id, caid, provid, pid, num)))
|
---|
599 | {
|
---|
600 | return;
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | void dvbapi_net_add_str(uint8_t *packet, int *size, const char *str)
|
---|
605 | {
|
---|
606 | uint8_t *str_len = &packet[*size]; // string length
|
---|
607 | *size += 1;
|
---|
608 | *str_len = snprintf((char *) &packet[*size], DVBAPI_MAX_PACKET_SIZE - *size, "%s", str);
|
---|
609 | *size += *str_len;
|
---|
610 | }
|
---|
611 |
|
---|
612 | int32_t dvbapi_net_send(uint32_t request, int32_t socket_fd, uint32_t msgid, int32_t demux_id,
|
---|
613 | uint32_t filter_number, uint8_t *data, struct s_client *client, ECM_REQUEST *er, uint16_t client_proto_version)
|
---|
614 | {
|
---|
615 | uint8_t packet[DVBAPI_MAX_PACKET_SIZE]; // maximum possible packet size
|
---|
616 | int32_t size = 0;
|
---|
617 | uint32_t u32;
|
---|
618 |
|
---|
619 | // not connected?
|
---|
620 | if(socket_fd <= 0)
|
---|
621 | {
|
---|
622 | return 0;
|
---|
623 | }
|
---|
624 |
|
---|
625 | // preparing packet - header
|
---|
626 | // in old protocol client expect this first byte as adapter index,
|
---|
627 | // changed in the new protocol to be always after request type (opcode)
|
---|
628 | if(client_proto_version <= 0)
|
---|
629 | {
|
---|
630 | packet[size++] = demux[demux_id].adapter_index; // adapter index - 1 byte
|
---|
631 | }
|
---|
632 | else if(client_proto_version >= 3)
|
---|
633 | {
|
---|
634 | packet[size++] = 0xa5; // message start
|
---|
635 | u32 = htonl(msgid);
|
---|
636 | memcpy(&packet[size], &u32, 4);
|
---|
637 | size += 4;
|
---|
638 | }
|
---|
639 |
|
---|
640 | // type of request
|
---|
641 | u32 = request;
|
---|
642 | if(client_proto_version >= 1)
|
---|
643 | {
|
---|
644 | u32 = htonl(u32);
|
---|
645 | }
|
---|
646 | memcpy(&packet[size], &u32, 4);
|
---|
647 | size += 4;
|
---|
648 |
|
---|
649 | // preparing packet - adapter index for proto >= 1
|
---|
650 | if((request != DVBAPI_SERVER_INFO) && client_proto_version >= 1)
|
---|
651 | {
|
---|
652 | packet[size++] = demux[demux_id].adapter_index; // adapter index - 1 byte
|
---|
653 | }
|
---|
654 |
|
---|
655 | // struct with data
|
---|
656 | switch(request)
|
---|
657 | {
|
---|
658 | case DVBAPI_SERVER_INFO:
|
---|
659 | {
|
---|
660 | int16_t proto_version = htons(DVBAPI_PROTOCOL_VERSION); // our protocol version
|
---|
661 | char capabilities[128] = "\x00"; // two zero characters
|
---|
662 | memcpy(&packet[size], &proto_version, 2);
|
---|
663 | size += 2;
|
---|
664 | uint8_t *info_len = &packet[size]; // info string length
|
---|
665 | size += 1;
|
---|
666 |
|
---|
667 | if(cfg.dvbapi_extended_cw_api == 1)
|
---|
668 | {
|
---|
669 | strcat(capabilities, ",e1mk"); // extended cw, key follows mode - supports CSA, DES, AES128
|
---|
670 | }
|
---|
671 |
|
---|
672 | if(cfg.dvbapi_extended_cw_api == 2)
|
---|
673 | {
|
---|
674 | strcat(capabilities, ",e2"); // usage of DES algo signalled through PID index - CSA and DES only
|
---|
675 | }
|
---|
676 |
|
---|
677 | *info_len = snprintf((char *) &packet[size], sizeof(packet) - size, "OSCam v%s, build r%s (%s); %s",
|
---|
678 | CS_VERSION, CS_SVN_VERSION, CS_TARGET, capabilities + 1);
|
---|
679 |
|
---|
680 | size += *info_len;
|
---|
681 | break;
|
---|
682 | }
|
---|
683 |
|
---|
684 | case DVBAPI_ECM_INFO:
|
---|
685 | {
|
---|
686 | if(er->rc >= E_NOTFOUND)
|
---|
687 | {
|
---|
688 | return 0;
|
---|
689 | }
|
---|
690 |
|
---|
691 | int8_t hops = 0;
|
---|
692 |
|
---|
693 | uint16_t sid = htons(er->srvid); // service ID (program number)
|
---|
694 | memcpy(&packet[size], &sid, 2);
|
---|
695 | size += 2;
|
---|
696 |
|
---|
697 | uint16_t caid = htons(er->caid); // CAID
|
---|
698 | memcpy(&packet[size], &caid, 2);
|
---|
699 | size += 2;
|
---|
700 |
|
---|
701 | uint16_t pid = htons(er->pid); // PID
|
---|
702 | memcpy(&packet[size], &pid, 2);
|
---|
703 | size += 2;
|
---|
704 |
|
---|
705 | uint32_t prid = htonl(er->prid); // Provider ID
|
---|
706 | memcpy(&packet[size], &prid, 4);
|
---|
707 | size += 4;
|
---|
708 |
|
---|
709 | uint32_t ecmtime = htonl(client->cwlastresptime); // ECM time
|
---|
710 | memcpy(&packet[size], &ecmtime, 4);
|
---|
711 | size += 4;
|
---|
712 |
|
---|
713 | dvbapi_net_add_str(packet, &size, get_cardsystem_desc_by_caid(er->caid)); // cardsystem name
|
---|
714 |
|
---|
715 | switch (er->rc)
|
---|
716 | {
|
---|
717 | case E_FOUND:
|
---|
718 | if(er->selected_reader)
|
---|
719 | {
|
---|
720 | dvbapi_net_add_str(packet, &size, er->selected_reader->label); // reader
|
---|
721 | if(is_network_reader(er->selected_reader))
|
---|
722 | {
|
---|
723 | dvbapi_net_add_str(packet, &size, er->selected_reader->device); // from
|
---|
724 | }
|
---|
725 | else
|
---|
726 | {
|
---|
727 | dvbapi_net_add_str(packet, &size, "local"); // from
|
---|
728 | }
|
---|
729 | dvbapi_net_add_str(packet, &size, reader_get_type_desc(er->selected_reader, 1)); // protocol
|
---|
730 | hops = er->selected_reader->currenthops;
|
---|
731 | }
|
---|
732 | break;
|
---|
733 |
|
---|
734 | case E_CACHE1:
|
---|
735 | dvbapi_net_add_str(packet, &size, "Cache"); // reader
|
---|
736 | dvbapi_net_add_str(packet, &size, "cache1"); // from
|
---|
737 | dvbapi_net_add_str(packet, &size, "none"); // protocol
|
---|
738 | break;
|
---|
739 |
|
---|
740 | case E_CACHE2:
|
---|
741 | dvbapi_net_add_str(packet, &size, "Cache"); // reader
|
---|
742 | dvbapi_net_add_str(packet, &size, "cache2"); // from
|
---|
743 | dvbapi_net_add_str(packet, &size, "none"); // protocol
|
---|
744 | break;
|
---|
745 |
|
---|
746 | case E_CACHEEX:
|
---|
747 | dvbapi_net_add_str(packet, &size, "Cache"); // reader
|
---|
748 | dvbapi_net_add_str(packet, &size, "cache3"); // from
|
---|
749 | dvbapi_net_add_str(packet, &size, "none"); // protocol
|
---|
750 | break;
|
---|
751 | }
|
---|
752 | packet[size++] = hops; // hops
|
---|
753 | break;
|
---|
754 | }
|
---|
755 |
|
---|
756 | case DVBAPI_CA_GET_DESCR_INFO:
|
---|
757 | {
|
---|
758 | int sct_cadescr_info_size = sizeof(ca_descr_info_t);
|
---|
759 | if(client_proto_version >= 1)
|
---|
760 | {
|
---|
761 | ca_descr_info_t *cadescr_info = (ca_descr_info_t *) data;
|
---|
762 | cadescr_info->num = htonl(cadescr_info->num);
|
---|
763 | cadescr_info->type = htonl(cadescr_info->type);
|
---|
764 | }
|
---|
765 | memcpy(&packet[size], data, sct_cadescr_info_size);
|
---|
766 | size += sct_cadescr_info_size;
|
---|
767 | break;
|
---|
768 | }
|
---|
769 |
|
---|
770 | case DVBAPI_CA_SET_DESCR:
|
---|
771 | {
|
---|
772 | int sct_cadescr_size = sizeof(ca_descr_t);
|
---|
773 | if(client_proto_version >= 1)
|
---|
774 | {
|
---|
775 | ca_descr_t *cadesc = (ca_descr_t *) data;
|
---|
776 | cadesc->index = htonl(cadesc->index);
|
---|
777 | cadesc->parity = htonl(cadesc->parity);
|
---|
778 | }
|
---|
779 | memcpy(&packet[size], data, sct_cadescr_size);
|
---|
780 | size += sct_cadescr_size;
|
---|
781 | break;
|
---|
782 | }
|
---|
783 |
|
---|
784 | case DVBAPI_CA_SET_PID:
|
---|
785 | {
|
---|
786 | int sct_capid_size = sizeof(ca_pid_t);
|
---|
787 | if(client_proto_version >= 1)
|
---|
788 | {
|
---|
789 | ca_pid_t *capid = (ca_pid_t *) data;
|
---|
790 | capid->pid = htonl(capid->pid);
|
---|
791 | capid->index = htonl(capid->index);
|
---|
792 | }
|
---|
793 | memcpy(&packet[size], data, sct_capid_size);
|
---|
794 | size += sct_capid_size;
|
---|
795 | break;
|
---|
796 | }
|
---|
797 |
|
---|
798 | case DVBAPI_CA_SET_DESCR_MODE:
|
---|
799 | {
|
---|
800 | int sct_cadescr_mode_size = sizeof(ca_descr_mode_t);
|
---|
801 | if(client_proto_version >= 1)
|
---|
802 | {
|
---|
803 | ca_descr_mode_t *cadesc_mode = (ca_descr_mode_t *) data;
|
---|
804 | cadesc_mode->index = htonl(cadesc_mode->index);
|
---|
805 | cadesc_mode->algo = htonl(cadesc_mode->algo);
|
---|
806 | cadesc_mode->cipher_mode = htonl(cadesc_mode->cipher_mode);
|
---|
807 | }
|
---|
808 | memcpy(&packet[size], data, sct_cadescr_mode_size);
|
---|
809 | size += sct_cadescr_mode_size;
|
---|
810 | break;
|
---|
811 | }
|
---|
812 |
|
---|
813 | case DVBAPI_CA_SET_DESCR_DATA:
|
---|
814 | {
|
---|
815 | int sct_cadescr_data_size = sizeof(ca_descr_data_t);
|
---|
816 | if(client_proto_version >= 1)
|
---|
817 | {
|
---|
818 | ca_descr_data_t *cadesc_data = (ca_descr_data_t *) data;
|
---|
819 | cadesc_data->index = htonl(cadesc_data->index);
|
---|
820 | cadesc_data->parity = htonl(cadesc_data->parity);
|
---|
821 | cadesc_data->data_type = htonl(cadesc_data->data_type);
|
---|
822 | cadesc_data->length = htonl(cadesc_data->length);
|
---|
823 | }
|
---|
824 | memcpy(&packet[size], data, sct_cadescr_data_size);
|
---|
825 | size += sct_cadescr_data_size;
|
---|
826 | break;
|
---|
827 | }
|
---|
828 |
|
---|
829 | case DVBAPI_DMX_SET_FILTER:
|
---|
830 | case DVBAPI_DMX_STOP:
|
---|
831 | {
|
---|
832 | int32_t sct_filter_size = sizeof(struct dmx_sct_filter_params);
|
---|
833 | packet[size++] = demux_id; // demux id - 1 byte
|
---|
834 | packet[size++] = filter_number; // filter number - 1 byte
|
---|
835 | if(data) // filter data when starting
|
---|
836 | {
|
---|
837 | if(client_proto_version >= 1)
|
---|
838 | {
|
---|
839 | struct dmx_sct_filter_params *fp = (struct dmx_sct_filter_params *)data;
|
---|
840 | // adding all dmx_sct_filter_params structure fields
|
---|
841 | // one by one to avoid padding problems
|
---|
842 | uint16_t pid = htons(fp->pid);
|
---|
843 | memcpy(&packet[size], &pid, 2);
|
---|
844 | size += 2;
|
---|
845 | memcpy(&packet[size], fp->filter.filter, 16);
|
---|
846 | size += 16;
|
---|
847 | memcpy(&packet[size], fp->filter.mask, 16);
|
---|
848 | size += 16;
|
---|
849 | memcpy(&packet[size], fp->filter.mode, 16);
|
---|
850 | size += 16;
|
---|
851 | uint32_t timeout = htonl(fp->timeout);
|
---|
852 | memcpy(&packet[size], &timeout, 4);
|
---|
853 | size += 4;
|
---|
854 | uint32_t flags = htonl(fp->flags);
|
---|
855 | memcpy(&packet[size], &flags, 4);
|
---|
856 | size += 4;
|
---|
857 | }
|
---|
858 | else
|
---|
859 | {
|
---|
860 | memcpy(&packet[size], data, sct_filter_size); // dmx_sct_filter_params struct
|
---|
861 | size += sct_filter_size;
|
---|
862 | }
|
---|
863 | }
|
---|
864 | else // pid when stopping
|
---|
865 | {
|
---|
866 | if(client_proto_version >= 1)
|
---|
867 | {
|
---|
868 | uint16_t pid = htons(demux[demux_id].demux_fd[filter_number].pid);
|
---|
869 | memcpy(&packet[size], &pid, 2);
|
---|
870 | size += 2;
|
---|
871 | }
|
---|
872 | else
|
---|
873 | {
|
---|
874 | uint16_t pid = demux[demux_id].demux_fd[filter_number].pid;
|
---|
875 | packet[size++] = pid >> 8;
|
---|
876 | packet[size++] = pid & 0xff;
|
---|
877 | }
|
---|
878 | }
|
---|
879 | break;
|
---|
880 | }
|
---|
881 |
|
---|
882 | default: // unknown request
|
---|
883 | {
|
---|
884 | cs_log("ERROR: dvbapi_net_send: invalid request");
|
---|
885 | return 0;
|
---|
886 | }
|
---|
887 | }
|
---|
888 | // sending
|
---|
889 | cs_log_dump_dbg(D_DVBAPI, packet, size, "Sending packet to dvbapi client (fd=%d):", socket_fd);
|
---|
890 | send(socket_fd, &packet, size, MSG_DONTWAIT);
|
---|
891 | // always returning success as the client could close socket
|
---|
892 | return 0;
|
---|
893 | }
|
---|
894 |
|
---|
895 | int32_t dvbapi_set_filter(int32_t demux_id, int32_t api, uint16_t pid, uint16_t caid, uint32_t provid,
|
---|
896 | uint8_t *filt, uint8_t *mask, int32_t timeout, int32_t pidindex, int32_t type, int8_t add_to_emm_list)
|
---|
897 | {
|
---|
898 | int32_t ret = -1, n = -1, i, filterfd = -1;
|
---|
899 |
|
---|
900 | for(i = 0; i < maxfilter && demux[demux_id].demux_fd[i].fd > 0; i++) { ; }
|
---|
901 |
|
---|
902 | if(i >= maxfilter)
|
---|
903 | {
|
---|
904 | cs_log_dbg(D_DVBAPI, "no free filter");
|
---|
905 | return -1;
|
---|
906 | }
|
---|
907 | n = i;
|
---|
908 |
|
---|
909 | if(USE_OPENXCAS)
|
---|
910 | {
|
---|
911 | if(type == TYPE_ECM)
|
---|
912 | {
|
---|
913 | openxcas_set_caid(demux[demux_id].ECMpids[pidindex].CAID);
|
---|
914 | openxcas_set_ecm_pid(pid);
|
---|
915 | }
|
---|
916 |
|
---|
917 | demux[demux_id].demux_fd[n].fd = DUMMY_FD;
|
---|
918 | demux[demux_id].demux_fd[n].pidindex = pidindex;
|
---|
919 | demux[demux_id].demux_fd[n].pid = pid;
|
---|
920 | demux[demux_id].demux_fd[n].caid = caid;
|
---|
921 | demux[demux_id].demux_fd[n].provid = provid;
|
---|
922 | demux[demux_id].demux_fd[n].type = type;
|
---|
923 | memcpy(demux[demux_id].demux_fd[n].filter, filt, 16); // copy filter to check later on if receiver delivered accordingly
|
---|
924 | memcpy(demux[demux_id].demux_fd[n].mask, mask, 16); // copy mask to check later on if receiver delivered accordingly
|
---|
925 | return 1;
|
---|
926 | }
|
---|
927 |
|
---|
928 | switch(api)
|
---|
929 | {
|
---|
930 | case DVBAPI_3:
|
---|
931 | {
|
---|
932 | if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
933 | {
|
---|
934 | ret = filterfd = DUMMY_FD;
|
---|
935 | }
|
---|
936 | else
|
---|
937 | {
|
---|
938 | ret = filterfd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
|
---|
939 | }
|
---|
940 |
|
---|
941 | if(ret < 0)
|
---|
942 | {
|
---|
943 | return ret; // return if device cant be opened!
|
---|
944 | }
|
---|
945 |
|
---|
946 | struct dmx_sct_filter_params sFP2;
|
---|
947 | memset(&sFP2, 0, sizeof(sFP2));
|
---|
948 | sFP2.pid = pid;
|
---|
949 | sFP2.timeout = timeout;
|
---|
950 | sFP2.flags = DMX_IMMEDIATE_START;
|
---|
951 |
|
---|
952 | if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
|
---|
953 | {
|
---|
954 | //DeepThought: on dgs/cubestation and neumo images, perhaps others
|
---|
955 | //the following code is needed to descramble
|
---|
956 | sFP2.filter.filter[0] = filt[0];
|
---|
957 | sFP2.filter.mask[0] = mask[0];
|
---|
958 | sFP2.filter.filter[1] = 0;
|
---|
959 | sFP2.filter.mask[1] = 0;
|
---|
960 | sFP2.filter.filter[2] = 0;
|
---|
961 | sFP2.filter.mask[2] = 0;
|
---|
962 | memcpy(sFP2.filter.filter + 3, filt + 1, 16 - 3);
|
---|
963 | memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
|
---|
964 |
|
---|
965 | //DeepThought: in the drivers of the dgs/cubestation and neumo images,
|
---|
966 | //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
|
---|
967 | //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
|
---|
968 | //due to a bug its sets the "positive mask" wrongly (they should be all 0).
|
---|
969 | //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
|
---|
970 | //structure, which is incorrect (it should be dmxSctFilterParams).
|
---|
971 | //The only way to get it right is to call DMX_SET_FILTER1 with the argument
|
---|
972 | //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
|
---|
973 | ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER1, &sFP2);
|
---|
974 | }
|
---|
975 | else
|
---|
976 | {
|
---|
977 | memcpy(sFP2.filter.filter, filt, 16);
|
---|
978 | memcpy(sFP2.filter.mask, mask, 16);
|
---|
979 | if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
980 | {
|
---|
981 | ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER,
|
---|
982 | demux[demux_id].socket_fd,
|
---|
983 | 0,
|
---|
984 | demux_id,
|
---|
985 | n,
|
---|
986 | (uint8_t *) &sFP2,
|
---|
987 | NULL,
|
---|
988 | NULL,
|
---|
989 | demux[demux_id].client_proto_version);
|
---|
990 | }
|
---|
991 | else
|
---|
992 | {
|
---|
993 | ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER, &sFP2);
|
---|
994 | }
|
---|
995 | }
|
---|
996 | break;
|
---|
997 | }
|
---|
998 |
|
---|
999 | case DVBAPI_1:
|
---|
1000 | {
|
---|
1001 | ret = filterfd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
|
---|
1002 | if(ret < 0) { return ret; } // return if device cant be opened!
|
---|
1003 |
|
---|
1004 | struct dmxSctFilterParams sFP1;
|
---|
1005 | memset(&sFP1, 0, sizeof(sFP1));
|
---|
1006 | sFP1.pid = pid;
|
---|
1007 | sFP1.timeout = timeout;
|
---|
1008 | sFP1.flags = DMX_IMMEDIATE_START;
|
---|
1009 | memcpy(sFP1.filter.filter, filt, 16);
|
---|
1010 | memcpy(sFP1.filter.mask, mask, 16);
|
---|
1011 | ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER1, &sFP1);
|
---|
1012 | break;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
1016 | case STAPI:
|
---|
1017 | {
|
---|
1018 | ret = filterfd = stapi_set_filter(demux_id, pid, filt, mask, n, demux[demux_id].pmt_file);
|
---|
1019 | if(ret <= 0)
|
---|
1020 | {
|
---|
1021 | ret = -1; // error setting filter!
|
---|
1022 | }
|
---|
1023 | break;
|
---|
1024 | }
|
---|
1025 | #endif
|
---|
1026 |
|
---|
1027 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
1028 | case COOLAPI:
|
---|
1029 | {
|
---|
1030 | ret = filterfd = coolapi_open_device(demux[demux_id].demux_index, demux_id);
|
---|
1031 | if(ret > 0)
|
---|
1032 | {
|
---|
1033 | ret = coolapi_set_filter(filterfd, n, pid, filt, mask, type);
|
---|
1034 | }
|
---|
1035 | else
|
---|
1036 | {
|
---|
1037 | ret = -1; // fail
|
---|
1038 | }
|
---|
1039 | break;
|
---|
1040 | }
|
---|
1041 | #endif
|
---|
1042 |
|
---|
1043 | default:
|
---|
1044 | break;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | if(ret != -1) // filter set successful
|
---|
1048 | {
|
---|
1049 | // only register if filter was set successful
|
---|
1050 | demux[demux_id].demux_fd[n].fd = filterfd;
|
---|
1051 | demux[demux_id].demux_fd[n].pidindex = pidindex;
|
---|
1052 | demux[demux_id].demux_fd[n].pid = pid;
|
---|
1053 | demux[demux_id].demux_fd[n].caid = caid;
|
---|
1054 | demux[demux_id].demux_fd[n].provid = provid;
|
---|
1055 | demux[demux_id].demux_fd[n].type = type;
|
---|
1056 |
|
---|
1057 | // copy filter and mask to check later on if receiver delivered accordingly
|
---|
1058 | memcpy(demux[demux_id].demux_fd[n].filter, filt, 16);
|
---|
1059 | memcpy(demux[demux_id].demux_fd[n].mask, mask, 16);
|
---|
1060 |
|
---|
1061 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d started successfully (caid %04X provid %06X pid %04X)",
|
---|
1062 | demux_id, n + 1, caid, provid, pid);
|
---|
1063 |
|
---|
1064 | if(type == TYPE_EMM && add_to_emm_list)
|
---|
1065 | {
|
---|
1066 | add_emmfilter_to_list(demux_id, filt, caid, provid, pid, n + 1, true);
|
---|
1067 | }
|
---|
1068 | }
|
---|
1069 | else
|
---|
1070 | {
|
---|
1071 | cs_log("ERROR: Could not start demux filter (api: %d errno=%d %s)", selected_api, errno, strerror(errno));
|
---|
1072 | }
|
---|
1073 | return ret;
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /*
|
---|
1077 | * Get number of available descramblers from box. On success, it stores result on
|
---|
1078 | * global variable "ca_descramblers_total". On failure, it uses a safe default value.
|
---|
1079 | */
|
---|
1080 | static int32_t dvbapi_get_descrambler_info(void)
|
---|
1081 | {
|
---|
1082 | // In enigma2 all ca devices are listed under adapter0. In addition we only
|
---|
1083 | // need to ask one ca device to get the total number of descramblers. In
|
---|
1084 | // PC installations, there are no ca devices, so we use a predefined value.
|
---|
1085 |
|
---|
1086 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
1087 | {
|
---|
1088 | ca_descramblers_total = INDEX_MAX_NET;
|
---|
1089 | return 1; // nothing else to do for PCs
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | int32_t fd = 0, ca_offset = 0;
|
---|
1093 | char device_path[128], device_path2[128];
|
---|
1094 | struct ca_descr_info descr_info;
|
---|
1095 | memset(&descr_info, 0, sizeof(descr_info));
|
---|
1096 |
|
---|
1097 | // Use a safe default in case we fail to get the exact number
|
---|
1098 | ca_descramblers_total = INDEX_MAX_LOCAL;
|
---|
1099 |
|
---|
1100 | if(cfg.dvbapi_boxtype == BOXTYPE_DUCKBOX ||
|
---|
1101 | cfg.dvbapi_boxtype == BOXTYPE_DBOX2 ||
|
---|
1102 | cfg.dvbapi_boxtype == BOXTYPE_UFS910)
|
---|
1103 | {
|
---|
1104 | ca_offset = 1;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | // Ask device for exact number of ca descramblers
|
---|
1108 | snprintf(device_path2, sizeof(device_path2), devices[selected_box].ca_device, ca_offset);
|
---|
1109 | snprintf(device_path, sizeof(device_path), devices[selected_box].path, 0);
|
---|
1110 | strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path));
|
---|
1111 |
|
---|
1112 | if((fd = open(device_path, O_RDWR | O_NONBLOCK)) < 0)
|
---|
1113 | {
|
---|
1114 | cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
|
---|
1115 | return 0;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | if(dvbapi_ioctl(fd, CA_GET_DESCR_INFO, &descr_info) < 0)
|
---|
1119 | {
|
---|
1120 | cs_log("ERROR: ioctl(CA_GET_DESCR_INFO): %s", strerror(errno));
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | if(close(fd) < 0)
|
---|
1124 | {
|
---|
1125 | cs_log("ERROR: Can't close device %s (errno=%d %s)", device_path, errno, strerror(errno));
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | // We got a valid response from device (count the ECD type only)
|
---|
1129 | if(descr_info.num > 0 && (descr_info.type & 1))
|
---|
1130 | {
|
---|
1131 | ca_descramblers_total = descr_info.num;
|
---|
1132 | cs_log("Detected %s device, total available descramblers: %d", device_path, ca_descramblers_total);
|
---|
1133 | return 1;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | return 0;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | static int32_t dvbapi_detect_api(void)
|
---|
1140 | {
|
---|
1141 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
1142 | selected_api = COOLAPI;
|
---|
1143 | selected_box = BOX_INDEX_COOLSTREAM;
|
---|
1144 | disable_pmt_files = 1;
|
---|
1145 | cfg.dvbapi_listenport = 0;
|
---|
1146 | cs_log("Detected Coolstream API");
|
---|
1147 | return 1;
|
---|
1148 | #else
|
---|
1149 | if(cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_PC)
|
---|
1150 | {
|
---|
1151 | selected_api = DVBAPI_3;
|
---|
1152 | selected_box = BOX_INDEX_DREAMBOX_DVBAPI3;
|
---|
1153 | if(cfg.dvbapi_listenport)
|
---|
1154 | {
|
---|
1155 | cs_log("Using TCP listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d",
|
---|
1156 | selected_api, cfg.dvbapi_boxtype);
|
---|
1157 | }
|
---|
1158 | else
|
---|
1159 | {
|
---|
1160 | cs_log("Using %s listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d",
|
---|
1161 | devices[selected_box].cam_socket_path, selected_api, cfg.dvbapi_boxtype);
|
---|
1162 | }
|
---|
1163 | return 1;
|
---|
1164 | }
|
---|
1165 | else if(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
|
---|
1166 | {
|
---|
1167 | selected_api = DVBAPI_3;
|
---|
1168 | selected_box = BOX_INDEX_QBOXHD;
|
---|
1169 | cfg.dvbapi_listenport = 0;
|
---|
1170 | disable_pmt_files = 1;
|
---|
1171 | cs_log("Using SamyGO dvbapi v0.1");
|
---|
1172 | return 1;
|
---|
1173 | }
|
---|
1174 | else
|
---|
1175 | {
|
---|
1176 | cfg.dvbapi_listenport = 0;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | int32_t i = 0, n = 0, devnum = -1, dmx_fd = 0, filtercount = 0;
|
---|
1180 | char device_path[128], device_path2[128];
|
---|
1181 | static LLIST *ll_max_fd;
|
---|
1182 | ll_max_fd = ll_create("ll_max_fd");
|
---|
1183 | LL_ITER itr;
|
---|
1184 |
|
---|
1185 | struct s_open_fd
|
---|
1186 | {
|
---|
1187 | uint32_t fd;
|
---|
1188 | };
|
---|
1189 | struct s_open_fd *open_fd;
|
---|
1190 |
|
---|
1191 | while(i < BOX_COUNT)
|
---|
1192 | {
|
---|
1193 | do
|
---|
1194 | {
|
---|
1195 | snprintf(device_path2, sizeof(device_path2), devices[i].demux_device, 0);
|
---|
1196 | snprintf(device_path, sizeof(device_path), devices[i].path, n);
|
---|
1197 | strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path));
|
---|
1198 |
|
---|
1199 | filtercount = 0;
|
---|
1200 | while((dmx_fd = open(device_path, O_RDWR | O_NONBLOCK)) > 0 && filtercount < MAX_FILTER)
|
---|
1201 | {
|
---|
1202 | filtercount++;
|
---|
1203 | if(!cs_malloc(&open_fd, sizeof(struct s_open_fd)))
|
---|
1204 | {
|
---|
1205 | close(dmx_fd);
|
---|
1206 | break;
|
---|
1207 | }
|
---|
1208 | open_fd->fd = dmx_fd;
|
---|
1209 | ll_append(ll_max_fd, open_fd);
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | if(filtercount > 0)
|
---|
1213 | {
|
---|
1214 | itr = ll_iter_create(ll_max_fd);
|
---|
1215 | while((open_fd = ll_iter_next(&itr)))
|
---|
1216 | {
|
---|
1217 | dmx_fd = open_fd->fd;
|
---|
1218 | do
|
---|
1219 | {
|
---|
1220 | ;
|
---|
1221 | }
|
---|
1222 | while(close(dmx_fd) < 0);
|
---|
1223 | ll_iter_remove_data(&itr);
|
---|
1224 | }
|
---|
1225 | devnum = i;
|
---|
1226 | selected_api = devices[devnum].api;
|
---|
1227 | selected_box = devnum;
|
---|
1228 |
|
---|
1229 | if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
|
---|
1230 | {
|
---|
1231 | selected_api = DVBAPI_3; //DeepThought
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
1235 | if(selected_api == STAPI && stapi_open() == 0)
|
---|
1236 | {
|
---|
1237 | cs_log("ERROR: stapi: setting up stapi failed.");
|
---|
1238 | return 0;
|
---|
1239 | }
|
---|
1240 | #endif
|
---|
1241 | maxfilter = filtercount;
|
---|
1242 | cs_log("Detected %s Api: %d, userconfig boxtype: %d maximum number of filters is %d (oscam limit is %d)",
|
---|
1243 | device_path, selected_api, cfg.dvbapi_boxtype, filtercount, MAX_FILTER);
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | // try at least 8 adapters
|
---|
1247 | if((strchr(devices[i].path, '%') != NULL) && (n < 8))
|
---|
1248 | {
|
---|
1249 | n++;
|
---|
1250 | }
|
---|
1251 | else
|
---|
1252 | {
|
---|
1253 | n = 0; i++;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | } while(n != 0); // n is set to 0 again if 8 adapters are tried!
|
---|
1257 |
|
---|
1258 | if(devnum != -1)
|
---|
1259 | {
|
---|
1260 | break; // check if box detected
|
---|
1261 | }
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | ll_destroy(&ll_max_fd);
|
---|
1265 | if(devnum == -1)
|
---|
1266 | {
|
---|
1267 | return 0;
|
---|
1268 | }
|
---|
1269 | #endif
|
---|
1270 |
|
---|
1271 | return 1;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | static int32_t dvbapi_read_device(int32_t dmx_fd, uint8_t *buf, uint32_t length)
|
---|
1275 | {
|
---|
1276 | int32_t readed;
|
---|
1277 | uint32_t count = 0;
|
---|
1278 | struct pollfd pfd[1];
|
---|
1279 |
|
---|
1280 | pfd[0].fd = dmx_fd;
|
---|
1281 | pfd[0].events = (POLLIN | POLLPRI);
|
---|
1282 |
|
---|
1283 | while(count < length)
|
---|
1284 | {
|
---|
1285 | if(poll(pfd, 1, 0)) // fd ready for reading?
|
---|
1286 | {
|
---|
1287 | if(pfd[0].revents & (POLLIN | POLLPRI)) // is there data to read?
|
---|
1288 | {
|
---|
1289 | readed = read(dmx_fd, &buf[count], length-count);
|
---|
1290 | if(readed < 0) // error occured while reading
|
---|
1291 | {
|
---|
1292 | if(errno == EINTR || errno == EAGAIN) // try again in case of interrupt
|
---|
1293 | {
|
---|
1294 | continue;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | cs_log("ERROR: Read error on fd %d (errno=%d %s)", dmx_fd, errno, strerror(errno));
|
---|
1298 | return (errno == EOVERFLOW ? 0 : -1);
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | if(readed > 0) // succesfull read
|
---|
1302 | {
|
---|
1303 | count += readed;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | if(readed == 0 && count > 0) // nothing to read left
|
---|
1307 | {
|
---|
1308 | break;
|
---|
1309 | }
|
---|
1310 | }
|
---|
1311 | else
|
---|
1312 | {
|
---|
1313 | return -1; // other events than pollin/pri means bad news -> abort!
|
---|
1314 | }
|
---|
1315 | }
|
---|
1316 | else
|
---|
1317 | {
|
---|
1318 | break;
|
---|
1319 | }
|
---|
1320 | }
|
---|
1321 | cs_log_dump_dbg(D_TRACE, buf, count, "Received:");
|
---|
1322 | return count;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | int32_t dvbapi_open_device(int32_t type, int32_t num, int32_t adapter)
|
---|
1326 | {
|
---|
1327 | int32_t dmx_fd, ret;
|
---|
1328 | int32_t ca_offset = 0;
|
---|
1329 | char device_path[128], device_path2[128];
|
---|
1330 |
|
---|
1331 | if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
1332 | {
|
---|
1333 | return DUMMY_FD;
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | if(type == 0)
|
---|
1337 | {
|
---|
1338 | snprintf(device_path2, sizeof(device_path2), devices[selected_box].demux_device, num);
|
---|
1339 | snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
|
---|
1340 | strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path));
|
---|
1341 | }
|
---|
1342 | else
|
---|
1343 | {
|
---|
1344 | if(cfg.dvbapi_boxtype == BOXTYPE_DUCKBOX
|
---|
1345 | || cfg.dvbapi_boxtype == BOXTYPE_DBOX2
|
---|
1346 | || cfg.dvbapi_boxtype == BOXTYPE_UFS910)
|
---|
1347 | {
|
---|
1348 | ca_offset = 1;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | if(cfg.dvbapi_boxtype == BOXTYPE_QBOXHD
|
---|
1352 | || cfg.dvbapi_boxtype == BOXTYPE_PC
|
---|
1353 | || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
|
---|
1354 | {
|
---|
1355 | num = 0;
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | snprintf(device_path2, sizeof(device_path2), devices[selected_box].ca_device, num + ca_offset);
|
---|
1359 | snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
|
---|
1360 | strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path));
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | if(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
|
---|
1364 | {
|
---|
1365 | if(type == 0)
|
---|
1366 | {
|
---|
1367 | struct sockaddr_un saddr;
|
---|
1368 | memset(&saddr, 0, sizeof(saddr));
|
---|
1369 | saddr.sun_family = AF_UNIX;
|
---|
1370 | cs_strncpy(saddr.sun_path, device_path, sizeof(saddr.sun_path));
|
---|
1371 | dmx_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
1372 | ret = connect(dmx_fd, (struct sockaddr *)&saddr, sizeof(saddr));
|
---|
1373 | if(ret < 0)
|
---|
1374 | {
|
---|
1375 | close(dmx_fd);
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | else if(type == 1)
|
---|
1379 | {
|
---|
1380 | int32_t udp_port = 9000;
|
---|
1381 | struct sockaddr_in saddr;
|
---|
1382 | memset(&saddr, 0, sizeof(saddr));
|
---|
1383 | saddr.sin_family = AF_INET;
|
---|
1384 | saddr.sin_port = htons(udp_port + adapter);
|
---|
1385 | saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
---|
1386 | dmx_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
---|
1387 | set_nonblock(dmx_fd, true);
|
---|
1388 | ret = connect(dmx_fd, (struct sockaddr *) &saddr, sizeof(saddr));
|
---|
1389 | if(ret < 0)
|
---|
1390 | {
|
---|
1391 | close(dmx_fd);
|
---|
1392 | }
|
---|
1393 | cs_log_dbg(D_DVBAPI, "NET DEVICE open (port = %d) fd %d", udp_port + adapter, dmx_fd);
|
---|
1394 | }
|
---|
1395 | else
|
---|
1396 | {
|
---|
1397 | ret = -1;
|
---|
1398 | }
|
---|
1399 | }
|
---|
1400 | else
|
---|
1401 | {
|
---|
1402 | dmx_fd = ret = open(device_path, O_RDWR | O_NONBLOCK);
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | if(ret < 0)
|
---|
1406 | {
|
---|
1407 | cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
|
---|
1408 | return -1;
|
---|
1409 | }
|
---|
1410 | cs_log_dbg(D_DVBAPI, "Open device %s (fd %d)", device_path, dmx_fd);
|
---|
1411 | return dmx_fd;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | uint16_t tunemm_caid_map(uint8_t direct, uint16_t caid, uint16_t srvid)
|
---|
1415 | {
|
---|
1416 | int32_t i;
|
---|
1417 | struct s_client *cl = cur_client();
|
---|
1418 | TUNTAB *ttab = &cl->ttab;
|
---|
1419 |
|
---|
1420 | if(!ttab->ttnum)
|
---|
1421 | {
|
---|
1422 | return caid;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | if(direct)
|
---|
1426 | {
|
---|
1427 | for(i = 0; i < ttab->ttnum; i++)
|
---|
1428 | {
|
---|
1429 | if(caid == ttab->ttdata[i].bt_caidto &&
|
---|
1430 | (srvid == ttab->ttdata[i].bt_srvid || ttab->ttdata[i].bt_srvid == 0xFFFF || !ttab->ttdata[i].bt_srvid))
|
---|
1431 | {
|
---|
1432 | return ttab->ttdata[i].bt_caidfrom;
|
---|
1433 | }
|
---|
1434 | }
|
---|
1435 | }
|
---|
1436 | else
|
---|
1437 | {
|
---|
1438 | for(i = 0; i < ttab->ttnum; i++)
|
---|
1439 | {
|
---|
1440 | if(caid == ttab->ttdata[i].bt_caidfrom &&
|
---|
1441 | (srvid == ttab->ttdata[i].bt_srvid || ttab->ttdata[i].bt_srvid == 0xFFFF || !ttab->ttdata[i].bt_srvid))
|
---|
1442 | {
|
---|
1443 | return ttab->ttdata[i].bt_caidto;
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 | return caid;
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | int32_t dvbapi_stop_filter(int32_t demux_id, int32_t type, uint32_t msgid)
|
---|
1451 | {
|
---|
1452 | #if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
|
---|
1453 | // We prevented PAT and PMT from starting, so lets don't close them either.
|
---|
1454 | if(type != TYPE_ECM && type != TYPE_EMM && type != TYPE_SDT)
|
---|
1455 | {
|
---|
1456 | return 1;
|
---|
1457 | }
|
---|
1458 | #endif
|
---|
1459 |
|
---|
1460 | int32_t g, error = 0;
|
---|
1461 |
|
---|
1462 | // just stop them all, we don't want to risk leaving
|
---|
1463 | // any stale filter running due to lowering of maxfilters
|
---|
1464 | for(g = 0; g < MAX_FILTER; g++)
|
---|
1465 | {
|
---|
1466 | if(demux[demux_id].demux_fd[g].type == type)
|
---|
1467 | {
|
---|
1468 | if(dvbapi_stop_filternum(demux_id, g, msgid) == -1)
|
---|
1469 | {
|
---|
1470 | error = 1;
|
---|
1471 | }
|
---|
1472 | }
|
---|
1473 | }
|
---|
1474 | return !error; // on error return 0, all ok 1
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | int32_t dvbapi_stop_filternum(int32_t demux_id, int32_t num, uint32_t msgid)
|
---|
1478 | {
|
---|
1479 | int32_t retfilter = -1, retfd = -1, fd = demux[demux_id].demux_fd[num].fd, try = 0;
|
---|
1480 |
|
---|
1481 | if(USE_OPENXCAS)
|
---|
1482 | {
|
---|
1483 | demux[demux_id].demux_fd[num].type = 0;
|
---|
1484 | demux[demux_id].demux_fd[num].fd = 0;
|
---|
1485 | return 1; // all ok!
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | if(fd > 0)
|
---|
1489 | {
|
---|
1490 | do
|
---|
1491 | {
|
---|
1492 | errno = 0;
|
---|
1493 | if(try)
|
---|
1494 | {
|
---|
1495 | cs_sleepms(50);
|
---|
1496 | }
|
---|
1497 | try++;
|
---|
1498 |
|
---|
1499 | cs_log_dbg(D_DVBAPI, "Demuxer %d stop filter %d try %d (fd: %d api: %d, caid: %04X, provid: %06X, %spid: %04X)",
|
---|
1500 | demux_id,
|
---|
1501 | num + 1,
|
---|
1502 | try,
|
---|
1503 | fd,
|
---|
1504 | selected_api,
|
---|
1505 | demux[demux_id].demux_fd[num].caid,
|
---|
1506 | demux[demux_id].demux_fd[num].provid,
|
---|
1507 | demux[demux_id].demux_fd[num].type == TYPE_ECM ? "ecm" : "emm",
|
---|
1508 | demux[demux_id].demux_fd[num].pid);
|
---|
1509 |
|
---|
1510 | switch(selected_api)
|
---|
1511 | {
|
---|
1512 | case DVBAPI_3:
|
---|
1513 | {
|
---|
1514 | if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
1515 | {
|
---|
1516 | retfilter = dvbapi_net_send(DVBAPI_DMX_STOP,
|
---|
1517 | demux[demux_id].socket_fd,
|
---|
1518 | msgid,
|
---|
1519 | demux_id,
|
---|
1520 | num,
|
---|
1521 | NULL,
|
---|
1522 | NULL,
|
---|
1523 | NULL,
|
---|
1524 | demux[demux_id].client_proto_version);
|
---|
1525 | }
|
---|
1526 | else
|
---|
1527 | {
|
---|
1528 | retfilter = dvbapi_ioctl(fd, DMX_STOP, NULL);
|
---|
1529 | }
|
---|
1530 | break;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | case DVBAPI_1:
|
---|
1534 | {
|
---|
1535 | retfilter = dvbapi_ioctl(fd, DMX_STOP, NULL);
|
---|
1536 | break;
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
1540 | case STAPI:
|
---|
1541 | {
|
---|
1542 | retfilter = stapi_remove_filter(demux_id, num, demux[demux_id].pmt_file);
|
---|
1543 | if(retfilter != 1) // stapi returns 0 for error, 1 for all ok
|
---|
1544 | {
|
---|
1545 | retfilter = -1;
|
---|
1546 | }
|
---|
1547 | break;
|
---|
1548 | }
|
---|
1549 | #endif
|
---|
1550 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
1551 | case COOLAPI:
|
---|
1552 | {
|
---|
1553 | retfilter = coolapi_remove_filter(fd, num);
|
---|
1554 | if(retfilter >= 0)
|
---|
1555 | {
|
---|
1556 | retfd = coolapi_close_device(fd);
|
---|
1557 | }
|
---|
1558 | break;
|
---|
1559 | }
|
---|
1560 | #endif
|
---|
1561 | default:
|
---|
1562 | break;
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | if(errno == 9) // no error on bad file descriptor
|
---|
1566 | {
|
---|
1567 | retfilter = 0;
|
---|
1568 | }
|
---|
1569 | } while(retfilter < 0 && try < 10);
|
---|
1570 |
|
---|
1571 | #if !defined WITH_COOLAPI && !defined WITH_COOLAPI2 // no fd close for coolapi and stapi, all others do close fd!
|
---|
1572 | try = 0;
|
---|
1573 | do
|
---|
1574 | {
|
---|
1575 | if(try)
|
---|
1576 | {
|
---|
1577 | errno = 0;
|
---|
1578 | cs_sleepms(50);
|
---|
1579 | }
|
---|
1580 | try++;
|
---|
1581 |
|
---|
1582 | // on bad filterfd dont try to close!
|
---|
1583 | if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX && errno != 9)
|
---|
1584 | {
|
---|
1585 | if(selected_api == STAPI)
|
---|
1586 | {
|
---|
1587 | retfd = 0; // stapi closes its own filter fd!
|
---|
1588 | }
|
---|
1589 | else
|
---|
1590 | {
|
---|
1591 | // flush filter input buffer in attempt to avoid overflow receivers internal buffer
|
---|
1592 | flush_read_fd(demux_id, num, fd);
|
---|
1593 | retfd = close(fd);
|
---|
1594 | if(errno == 9)
|
---|
1595 | {
|
---|
1596 | retfd = 0; // no error on bad file descriptor
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 | }
|
---|
1600 | else
|
---|
1601 | {
|
---|
1602 | retfd = 0;
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 | } while(retfd < 0 && try < 10);
|
---|
1606 | #endif
|
---|
1607 | }
|
---|
1608 | else // fd <=0
|
---|
1609 | {
|
---|
1610 | return 1; // filter was already killed!
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | if(retfilter < 0) // error on remove filter
|
---|
1614 | {
|
---|
1615 | cs_log("ERROR: Demuxer %d could not stop Filter %d (fd:%d api:%d errno=%d %s)",
|
---|
1616 | demux_id, num + 1, fd, selected_api, errno, strerror(errno));
|
---|
1617 | return retfilter;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | if(retfd < 0) // error on close filter fd
|
---|
1621 | {
|
---|
1622 | cs_log("ERROR: Demuxer %d could not close fd of Filter %d (fd=%d api:%d errno=%d %s)",
|
---|
1623 | demux_id, num + 1, fd, selected_api, errno, strerror(errno));
|
---|
1624 | return retfd;
|
---|
1625 | }
|
---|
1626 |
|
---|
1627 | // code below runs only if nothing has gone wrong
|
---|
1628 | if(demux[demux_id].demux_fd[num].type == TYPE_ECM) // ecm filter stopped: reset index!
|
---|
1629 | {
|
---|
1630 | int32_t oldpid = demux[demux_id].demux_fd[num].pidindex;
|
---|
1631 | int32_t curpid = demux[demux_id].pidindex;
|
---|
1632 |
|
---|
1633 | // workaround: below dont run on stapi since it handles it own pids...
|
---|
1634 | // stapi need to be better integrated in oscam dvbapi.
|
---|
1635 | if(selected_api != STAPI)
|
---|
1636 | {
|
---|
1637 | int32_t z;
|
---|
1638 | for(z = 0; z < MAX_STREAM_INDICES; z++)
|
---|
1639 | {
|
---|
1640 | uint32_t idx = demux[demux_id].ECMpids[oldpid].index[z];
|
---|
1641 | demux[demux_id].ECMpids[oldpid].index[z] = INDEX_INVALID;
|
---|
1642 |
|
---|
1643 | if(idx != INDEX_INVALID) // if in use
|
---|
1644 | {
|
---|
1645 | int32_t i;
|
---|
1646 | for(i = 0; i < demux[demux_id].STREAMpidcount; i++)
|
---|
1647 | {
|
---|
1648 | int8_t match = 0;
|
---|
1649 |
|
---|
1650 | // check streams of old disabled ecmpid
|
---|
1651 | if(!demux[demux_id].ECMpids[oldpid].streams
|
---|
1652 | || ((demux[demux_id].ECMpids[oldpid].streams & (1 << i)) == (uint) (1 << i)))
|
---|
1653 | {
|
---|
1654 | // check if new ecmpid is using same streams
|
---|
1655 | if(curpid != -1 && (!demux[demux_id].ECMpids[curpid].streams
|
---|
1656 | || ((demux[demux_id].ECMpids[curpid].streams & (1 << i)) == (uint) (1 << i))))
|
---|
1657 | {
|
---|
1658 | continue; // found same stream on old and new ecmpid -> skip! (and leave it enabled!)
|
---|
1659 | }
|
---|
1660 | int32_t pidtobestopped = demux[demux_id].STREAMpids[i];
|
---|
1661 | int32_t j, k, otherdemuxpid;
|
---|
1662 | uint32_t otherdemuxidx;
|
---|
1663 |
|
---|
1664 | for(j = 0; j < MAX_DEMUX; j++) // check other demuxers for same streampid with same index
|
---|
1665 | {
|
---|
1666 | if(demux[j].program_number == 0 || demux_id == j || demux[j].ca_mask != demux[demux_id].ca_mask)
|
---|
1667 | {
|
---|
1668 | continue;
|
---|
1669 | // skip empty demuxers
|
---|
1670 | // skip same demuxer
|
---|
1671 | // skip streampid running on other ca device
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | otherdemuxpid = demux[j].pidindex;
|
---|
1675 | if(otherdemuxpid == -1)
|
---|
1676 | {
|
---|
1677 | continue; // Other demuxer not descrambling yet
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | int32_t y;
|
---|
1681 | for(y = 0; y < MAX_STREAM_INDICES; y++)
|
---|
1682 | {
|
---|
1683 | otherdemuxidx = demux[j].ECMpids[otherdemuxpid].index[y];
|
---|
1684 | if(otherdemuxidx == INDEX_INVALID || otherdemuxidx != idx)
|
---|
1685 | {
|
---|
1686 | continue; // Other demuxer has no index yet, or index is different
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | for(k = 0; k < demux[j].STREAMpidcount; k++)
|
---|
1690 | {
|
---|
1691 | if(!demux[j].ECMpids[otherdemuxpid].streams
|
---|
1692 | || ((demux[j].ECMpids[otherdemuxpid].streams & (1 << k)) == (uint) (1 << k)))
|
---|
1693 | {
|
---|
1694 | if(demux[j].STREAMpids[k] == pidtobestopped)
|
---|
1695 | {
|
---|
1696 | continue; // found same streampid enabled with same index
|
---|
1697 | // on one or more other demuxers -> skip! (and leave it enabled!)
|
---|
1698 | }
|
---|
1699 | }
|
---|
1700 | match = 1; // matching stream found
|
---|
1701 | }
|
---|
1702 | }
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | if(!match)
|
---|
1706 | {
|
---|
1707 | for(j = 0; j < CA_MAX; j++)
|
---|
1708 | {
|
---|
1709 | if(((demux[demux_id].ca_mask & (1 << j)) == (uint32_t) (1 << j)))
|
---|
1710 | {
|
---|
1711 | remove_streampid_from_list(j, pidtobestopped, idx);
|
---|
1712 | break;
|
---|
1713 | }
|
---|
1714 | }
|
---|
1715 | }
|
---|
1716 | }
|
---|
1717 | }
|
---|
1718 | }
|
---|
1719 | }
|
---|
1720 | }
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | if(demux[demux_id].demux_fd[num].type == TYPE_EMM) // If emm type remove from emm filterlist
|
---|
1724 | {
|
---|
1725 | remove_emmfilter_from_list(demux_id,
|
---|
1726 | demux[demux_id].demux_fd[num].caid,
|
---|
1727 | demux[demux_id].demux_fd[num].provid,
|
---|
1728 | demux[demux_id].demux_fd[num].pid,
|
---|
1729 | num + 1);
|
---|
1730 | }
|
---|
1731 | demux[demux_id].demux_fd[num].type = 0;
|
---|
1732 | demux[demux_id].demux_fd[num].fd = 0;
|
---|
1733 | return 1; // all ok!
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | void dvbapi_start_filter(int32_t demux_id, int32_t pidindex, uint16_t pid, uint16_t caid,
|
---|
1737 | uint32_t provid, uint8_t table, uint8_t mask, int32_t timeout, int32_t type)
|
---|
1738 | {
|
---|
1739 | int32_t i;
|
---|
1740 | for(i = 0; i < maxfilter; i++) // check if filter is present
|
---|
1741 | {
|
---|
1742 | if(demux[demux_id].demux_fd[i].fd > 0 &&
|
---|
1743 | demux[demux_id].demux_fd[i].pid == pid &&
|
---|
1744 | demux[demux_id].demux_fd[i].type == type &&
|
---|
1745 | demux[demux_id].demux_fd[i].filter[0] == table &&
|
---|
1746 | demux[demux_id].demux_fd[i].mask[0] == mask)
|
---|
1747 | {
|
---|
1748 | return;
|
---|
1749 | }
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | uint8_t filter[32];
|
---|
1753 | memset(filter, 0, 32);
|
---|
1754 | filter[0] = table;
|
---|
1755 | filter[16] = mask;
|
---|
1756 |
|
---|
1757 | cs_log_dbg(D_DVBAPI, "Demuxer %d try to start new filter for caid: %04X, provid: %06X, pid: %04X",
|
---|
1758 | demux_id, caid, provid, pid);
|
---|
1759 |
|
---|
1760 | dvbapi_set_filter(demux_id, selected_api, pid, caid, provid, filter, filter + 16, timeout, pidindex, type, 0);
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | void dvbapi_start_sdt_filter(int32_t demux_id)
|
---|
1764 | {
|
---|
1765 | dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x11, 0x001, 0x01, 0x42, 0xFF, 0, TYPE_SDT);
|
---|
1766 | demux[demux_id].sdt_filter = 0;
|
---|
1767 | }
|
---|
1768 |
|
---|
1769 | void dvbapi_start_pat_filter(int32_t demux_id)
|
---|
1770 | {
|
---|
1771 | #if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
|
---|
1772 | // PAT-Filter breaks API and OSCAM for Coolstream.
|
---|
1773 | // Don't use it
|
---|
1774 | return;
|
---|
1775 | #endif
|
---|
1776 | dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x00, 0x001, 0x01, 0x00, 0xFF, 0, TYPE_PAT);
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | void dvbapi_start_pmt_filter(int32_t demux_id, int32_t pmt_pid)
|
---|
1780 | {
|
---|
1781 | #if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
|
---|
1782 | // PMT-Filter breaks API and OSCAM for Coolstream.
|
---|
1783 | // Don't use it
|
---|
1784 | return;
|
---|
1785 | #endif
|
---|
1786 | uint8_t filter[16], mask[16];
|
---|
1787 | memset(filter, 0, 16);
|
---|
1788 | memset(mask, 0, 16);
|
---|
1789 | filter[0] = 0x02;
|
---|
1790 | i2b_buf(2, demux[demux_id].program_number, filter + 1); // add srvid to filter since the pid can deliver pmt for multiple srvid
|
---|
1791 | mask[0] = 0xFF;
|
---|
1792 | mask[1] = 0xFF;
|
---|
1793 | mask[2] = 0xFF;
|
---|
1794 | dvbapi_set_filter(demux_id, selected_api, pmt_pid, 0x001, 0x01, filter, mask, 0, 0, TYPE_PMT, 0);
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | void dvbapi_start_emm_filter(int32_t demux_id)
|
---|
1798 | {
|
---|
1799 | unsigned int j;
|
---|
1800 | if(!demux[demux_id].EMMpidcount)
|
---|
1801 | {
|
---|
1802 | return;
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | struct s_csystem_emm_filter *dmx_filter = NULL;
|
---|
1806 | unsigned int filter_count = 0;
|
---|
1807 | uint16_t caid, ncaid;
|
---|
1808 | uint32_t provid;
|
---|
1809 | struct s_reader *rdr = NULL;
|
---|
1810 | struct s_client *cl = cur_client();
|
---|
1811 |
|
---|
1812 | if(!cl || !cl->aureader_list)
|
---|
1813 | {
|
---|
1814 | return;
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | LL_ITER itr = ll_iter_create(cl->aureader_list);
|
---|
1818 | while((rdr = ll_iter_next(&itr)))
|
---|
1819 | {
|
---|
1820 | if(!(rdr->grp & cl->grp))
|
---|
1821 | {
|
---|
1822 | continue;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | if(rdr->audisabled || !rdr->enable || (!is_network_reader(rdr) && rdr->card_status != CARD_INSERTED))
|
---|
1826 | {
|
---|
1827 | continue;
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | const struct s_cardsystem *csystem;
|
---|
1831 | uint16_t c, match;
|
---|
1832 | cs_log_dbg(D_DVBAPI, "Demuxer %d matching reader %s against available emmpids -> START!", demux_id, rdr->label);
|
---|
1833 |
|
---|
1834 | for(c = 0; c < demux[demux_id].EMMpidcount; c++)
|
---|
1835 | {
|
---|
1836 | caid = ncaid = demux[demux_id].EMMpids[c].CAID;
|
---|
1837 | if(!caid) continue;
|
---|
1838 |
|
---|
1839 | if(chk_is_betatunnel_caid(caid) == 2)
|
---|
1840 | {
|
---|
1841 | ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_id].program_number);
|
---|
1842 | }
|
---|
1843 | provid = demux[demux_id].EMMpids[c].PROVID;
|
---|
1844 |
|
---|
1845 | if(caid == ncaid)
|
---|
1846 | {
|
---|
1847 | match = emm_reader_match(rdr, caid, provid);
|
---|
1848 | }
|
---|
1849 | else
|
---|
1850 | {
|
---|
1851 | match = emm_reader_match(rdr, ncaid, provid);
|
---|
1852 | }
|
---|
1853 |
|
---|
1854 | if(match)
|
---|
1855 | {
|
---|
1856 | if(rdr->typ == R_EMU)
|
---|
1857 | {
|
---|
1858 | csystem = rdr->csystem;
|
---|
1859 | }
|
---|
1860 | else
|
---|
1861 | {
|
---|
1862 | csystem = get_cardsystem_by_caid(caid);
|
---|
1863 | }
|
---|
1864 | if(csystem)
|
---|
1865 | {
|
---|
1866 | if(caid != ncaid)
|
---|
1867 | {
|
---|
1868 | csystem = get_cardsystem_by_caid(ncaid);
|
---|
1869 | if(csystem && csystem->get_tunemm_filter)
|
---|
1870 | {
|
---|
1871 | csystem->get_tunemm_filter(rdr, &dmx_filter, &filter_count);
|
---|
1872 | cs_log_dbg(D_DVBAPI, "Demuxer %d setting emm filter for betatunnel: %04X -> %04X",
|
---|
1873 | demux_id, ncaid, caid);
|
---|
1874 | }
|
---|
1875 | else
|
---|
1876 | {
|
---|
1877 | cs_log_dbg(D_DVBAPI, "Demuxer %d cardsystem for emm filter for caid %04X of reader %s not found",
|
---|
1878 | demux_id, ncaid, rdr->label);
|
---|
1879 | continue;
|
---|
1880 | }
|
---|
1881 | }
|
---|
1882 | else if(csystem->get_emm_filter)
|
---|
1883 | {
|
---|
1884 | if(rdr->typ == R_EMU)
|
---|
1885 | {
|
---|
1886 | csystem->get_emm_filter_adv(rdr, &dmx_filter, &filter_count, caid, provid, demux[demux_id].program_number,
|
---|
1887 | demux[demux_id].tsid, demux[demux_id].onid, demux[demux_id].ens);
|
---|
1888 | }
|
---|
1889 | else
|
---|
1890 | {
|
---|
1891 | csystem->get_emm_filter(rdr, &dmx_filter, &filter_count);
|
---|
1892 | }
|
---|
1893 | }
|
---|
1894 | }
|
---|
1895 | else
|
---|
1896 | {
|
---|
1897 | cs_log_dbg(D_DVBAPI, "Demuxer %d cardsystem for emm filter for caid %04X of reader %s not found",
|
---|
1898 | demux_id, caid, rdr->label);
|
---|
1899 | continue;
|
---|
1900 | }
|
---|
1901 |
|
---|
1902 | for(j = 0; j < filter_count ; j++)
|
---|
1903 | {
|
---|
1904 | if(dmx_filter[j].enabled == 0)
|
---|
1905 | {
|
---|
1906 | continue;
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | uint8_t filter[32];
|
---|
1910 | memset(filter, 0, sizeof(filter)); // reset filter
|
---|
1911 | uint32_t usefilterbytes = 16; // default use all filters
|
---|
1912 | memcpy(filter, dmx_filter[j].filter, usefilterbytes);
|
---|
1913 | memcpy(filter + 16, dmx_filter[j].mask, usefilterbytes);
|
---|
1914 | int32_t emmtype = dmx_filter[j].type;
|
---|
1915 |
|
---|
1916 | if(filter[0] && (((1 << (filter[0] % 0x80)) & rdr->b_nano) && !((1 << (filter[0] % 0x80)) & rdr->s_nano)))
|
---|
1917 | {
|
---|
1918 | cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d blocked by userconfig -> SKIP!",
|
---|
1919 | demux_id, rdr->label, j + 1, filter_count);
|
---|
1920 | continue;
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | if((rdr->blockemm & emmtype) && !(((1 << (filter[0] % 0x80)) & rdr->s_nano) || (rdr->saveemm & emmtype)))
|
---|
1924 | {
|
---|
1925 | cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d blocked by userconfig -> SKIP!",
|
---|
1926 | demux_id, rdr->label, j + 1, filter_count);
|
---|
1927 | continue;
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | if(demux[demux_id].EMMpids[c].type & emmtype)
|
---|
1931 | {
|
---|
1932 | cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d type match -> ENABLE!",
|
---|
1933 | demux_id, rdr->label, j + 1, filter_count);
|
---|
1934 | check_add_emmpid(demux_id, filter, c, emmtype);
|
---|
1935 | }
|
---|
1936 | else
|
---|
1937 | {
|
---|
1938 | cs_log_dbg(D_DVBAPI, "Demuxer %d reader %s emmfilter %d/%d type mismatch -> SKIP!",
|
---|
1939 | demux_id, rdr->label, j + 1, filter_count);
|
---|
1940 | }
|
---|
1941 | }
|
---|
1942 | // dmx_filter not use below this point;
|
---|
1943 | NULLFREE(dmx_filter);
|
---|
1944 | filter_count = 0;
|
---|
1945 | }
|
---|
1946 | }
|
---|
1947 | cs_log_dbg(D_DVBAPI, "Demuxer %d matching reader %s against available emmpids -> DONE!", demux_id, rdr->label);
|
---|
1948 | }
|
---|
1949 |
|
---|
1950 | if(demux[demux_id].emm_filter == -1) // first run -1
|
---|
1951 | {
|
---|
1952 | demux[demux_id].emm_filter = 0;
|
---|
1953 | }
|
---|
1954 | cs_log_dbg(D_DVBAPI, "Demuxer %d handles %i emm filters", demux_id, demux[demux_id].emm_filter);
|
---|
1955 | }
|
---|
1956 |
|
---|
1957 | void dvbapi_add_ecmpid_int(int32_t demux_id, uint16_t caid, uint16_t ecmpid, uint32_t provid, uint32_t cadata, char *txt)
|
---|
1958 | {
|
---|
1959 | int32_t n, added = 0;
|
---|
1960 | int32_t stream = demux[demux_id].STREAMpidcount - 1;
|
---|
1961 |
|
---|
1962 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
1963 | {
|
---|
1964 | if(demux[demux_id].ECMpids[n].CAID == caid && demux[demux_id].ECMpids[n].ECM_PID == ecmpid
|
---|
1965 | && (!provid || (provid && demux[demux_id].ECMpids[n].PROVID == provid)))
|
---|
1966 | {
|
---|
1967 | added = 1;
|
---|
1968 | if(stream > -1)
|
---|
1969 | {
|
---|
1970 | if(!demux[demux_id].ECMpids[n].streams)
|
---|
1971 | {
|
---|
1972 | // we already got this caid/ecmpid as global, no need to add the single stream
|
---|
1973 | cs_log_dbg(D_DVBAPI, "Demuxer %d skipped stream CAID: %04X ECM_PID: %04X PROVID: %06X (Same as ECMPID %d)",
|
---|
1974 | demux_id, caid, ecmpid, provid, n);
|
---|
1975 | continue;
|
---|
1976 | }
|
---|
1977 | demux[demux_id].ECMpids[n].streams |= (1 << stream);
|
---|
1978 | cs_log("Demuxer %d added stream to ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X",
|
---|
1979 | demux_id, n, caid, ecmpid, provid);
|
---|
1980 | }
|
---|
1981 | }
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | if(added == 1)
|
---|
1985 | {
|
---|
1986 | return;
|
---|
1987 | }
|
---|
1988 |
|
---|
1989 | if(demux[demux_id].ECMpidcount >= MAX_ECM_PIDS)
|
---|
1990 | {
|
---|
1991 | cs_log("We reached maximum ECMpids: unable to add to demuxer %d ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X %s",
|
---|
1992 | demux_id, demux[demux_id].ECMpidcount, caid, ecmpid, provid, txt);
|
---|
1993 | return;
|
---|
1994 | }
|
---|
1995 |
|
---|
1996 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].ECM_PID = ecmpid;
|
---|
1997 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CAID = caid;
|
---|
1998 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].PROVID = provid;
|
---|
1999 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CHID = 0x10000; // reset CHID
|
---|
2000 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].checked = 0;
|
---|
2001 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].status = 0;
|
---|
2002 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].tries = 0xFE;
|
---|
2003 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].streams = 0; // reset streams!
|
---|
2004 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_curindex = 0xFE; // reset
|
---|
2005 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_maxindex = 0; // reset
|
---|
2006 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].irdeto_cycle = 0xFE; // reset
|
---|
2007 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].table = 0;
|
---|
2008 | demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].cadata = cadata;
|
---|
2009 |
|
---|
2010 | cs_log("Demuxer %d ecmpid %d CAID: %04X ECM_PID: %04X PROVID: %06X %s",
|
---|
2011 | demux_id, demux[demux_id].ECMpidcount, caid, ecmpid, provid, txt);
|
---|
2012 |
|
---|
2013 | // marker to fetch emms early irdeto needs them!
|
---|
2014 | if(caid_is_irdeto(caid) || (caid_is_dre(caid) && (provid == 0x11 || provid == 0xFE)))
|
---|
2015 | {
|
---|
2016 | demux[demux_id].emmstart.time = 1;
|
---|
2017 | }
|
---|
2018 | demux[demux_id].ECMpidcount++;
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 | void dvbapi_add_ecmpid(int32_t demux_id, uint16_t caid, uint16_t ecmpid, uint32_t provid, uint32_t cadata, char *txt)
|
---|
2022 | {
|
---|
2023 | dvbapi_add_ecmpid_int(demux_id, caid, ecmpid, provid, cadata, txt);
|
---|
2024 | struct s_dvbapi_priority *joinentry;
|
---|
2025 |
|
---|
2026 | for(joinentry = dvbapi_priority; joinentry != NULL; joinentry = joinentry->next)
|
---|
2027 | {
|
---|
2028 | if((joinentry->type != 'j')
|
---|
2029 | || (joinentry->caid && joinentry->caid != caid)
|
---|
2030 | || (joinentry->provid && joinentry->provid != provid)
|
---|
2031 | || (joinentry->ecmpid && joinentry->ecmpid != ecmpid)
|
---|
2032 | || (joinentry->srvid && joinentry->srvid != demux[demux_id].program_number))
|
---|
2033 | {
|
---|
2034 | continue;
|
---|
2035 | }
|
---|
2036 |
|
---|
2037 | cs_log_dbg(D_DVBAPI, "Join ecmpid %04X@%06X:%04X to %04X@%06X:%04X",
|
---|
2038 | caid, provid, ecmpid, joinentry->mapcaid, joinentry->mapprovid, joinentry->mapecmpid);
|
---|
2039 | dvbapi_add_ecmpid_int(demux_id, joinentry->mapcaid, joinentry->mapecmpid, joinentry->mapprovid, 0, txt);
|
---|
2040 | }
|
---|
2041 | }
|
---|
2042 |
|
---|
2043 | void dvbapi_add_emmpid(int32_t demux_id, uint16_t caid, uint16_t emmpid, uint32_t provid, uint32_t cadata, uint8_t type)
|
---|
2044 | {
|
---|
2045 | char typetext[40];
|
---|
2046 | char cadatatext[40];
|
---|
2047 | cs_strncpy(typetext, ":", sizeof(typetext));
|
---|
2048 |
|
---|
2049 | if(type & 0x01) { strcat(typetext, "UNIQUE:"); }
|
---|
2050 | if(type & 0x02) { strcat(typetext, "SHARED:"); }
|
---|
2051 | if(type & 0x04) { strcat(typetext, "GLOBAL:"); }
|
---|
2052 | if(type & 0xF8) { strcat(typetext, "UNKNOWN:"); }
|
---|
2053 |
|
---|
2054 | if(cadata > 0)
|
---|
2055 | {
|
---|
2056 | snprintf(cadatatext, 40, " CA DATA %X ", cadata);
|
---|
2057 | }
|
---|
2058 | else
|
---|
2059 | {
|
---|
2060 | cadatatext[0] = '\t'; cadatatext[1] = '\0';
|
---|
2061 | }
|
---|
2062 |
|
---|
2063 | if(caid == 0x4AE1 && provid == 0x11 && cadata == 0)
|
---|
2064 | {
|
---|
2065 | return;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | uint16_t i;
|
---|
2069 | for(i = 0; i < demux[demux_id].EMMpidcount; i++)
|
---|
2070 | {
|
---|
2071 | if(demux[demux_id].EMMpids[i].PID == emmpid && demux[demux_id].EMMpids[i].CAID == caid &&
|
---|
2072 | demux[demux_id].EMMpids[i].PROVID == provid && demux[demux_id].EMMpids[i].cadata == cadata)
|
---|
2073 | {
|
---|
2074 | if(!(demux[demux_id].EMMpids[i].type & type))
|
---|
2075 | {
|
---|
2076 | demux[demux_id].EMMpids[i].type |= type; // register this emm kind to this emmpid
|
---|
2077 | cs_log_dbg(D_DVBAPI, "Added to existing emmpid %d additional emmtype %s",
|
---|
2078 | demux[demux_id].EMMpidcount - 1, typetext);
|
---|
2079 | }
|
---|
2080 | return;
|
---|
2081 | }
|
---|
2082 | }
|
---|
2083 |
|
---|
2084 | if(i < MAX_EMM_PIDS)
|
---|
2085 | {
|
---|
2086 | demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].PID = emmpid;
|
---|
2087 | demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].CAID = caid;
|
---|
2088 | demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].PROVID = provid;
|
---|
2089 | demux[demux_id].EMMpids[demux[demux_id].EMMpidcount].type = type;
|
---|
2090 | demux[demux_id].EMMpids[demux[demux_id].EMMpidcount++].cadata = cadata;
|
---|
2091 | cs_log_dbg(D_DVBAPI, "Added new emmpid %d CAID: %04X EMM_PID: %04X PROVID: %06X%sTYPE %s",
|
---|
2092 | demux[demux_id].EMMpidcount - 1, caid, emmpid, provid, cadatatext, typetext);
|
---|
2093 | }
|
---|
2094 | else
|
---|
2095 | {
|
---|
2096 | cs_log_dbg(D_DVBAPI, "We reached max emmpids: unable to add new emmpid %d CAID: %04X EMM_PID: %04X PROVID: %06X%sTYPE %s",
|
---|
2097 | demux[demux_id].EMMpidcount - 1, caid, emmpid, provid, cadatatext, typetext);
|
---|
2098 | }
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | void dvbapi_parse_cat(int32_t demux_id, uint8_t *buf, int32_t len)
|
---|
2102 | {
|
---|
2103 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
2104 | // driver sometimes reports error if too many emm filter
|
---|
2105 | // but adding more ecm filter is no problem
|
---|
2106 | // ... so ifdef here instead of limiting MAX_FILTER
|
---|
2107 | demux[demux_id].max_emm_filter = 14;
|
---|
2108 | #else
|
---|
2109 | if(cfg.dvbapi_requestmode == 1)
|
---|
2110 | {
|
---|
2111 | uint16_t ecm_filter_needed = 0, n;
|
---|
2112 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
2113 | {
|
---|
2114 | if(demux[demux_id].ECMpids[n].status > -1)
|
---|
2115 | {
|
---|
2116 | ecm_filter_needed++;
|
---|
2117 | }
|
---|
2118 | }
|
---|
2119 |
|
---|
2120 | if(maxfilter - ecm_filter_needed <= 0)
|
---|
2121 | {
|
---|
2122 | demux[demux_id].max_emm_filter = 0;
|
---|
2123 | }
|
---|
2124 | else
|
---|
2125 | {
|
---|
2126 | demux[demux_id].max_emm_filter = maxfilter - ecm_filter_needed;
|
---|
2127 | }
|
---|
2128 | }
|
---|
2129 | else
|
---|
2130 | {
|
---|
2131 | demux[demux_id].max_emm_filter = maxfilter - 1;
|
---|
2132 | }
|
---|
2133 | #endif
|
---|
2134 |
|
---|
2135 | uint16_t i, k;
|
---|
2136 | cs_log_dump_dbg(D_DVBAPI, buf, len, "cat:");
|
---|
2137 |
|
---|
2138 | for(i = 8; i < (b2i(2, buf + 1) & 0xFFF) - 1; i += buf[i + 1] + 2)
|
---|
2139 | {
|
---|
2140 | if(buf[i] != 0x09)
|
---|
2141 | {
|
---|
2142 | continue;
|
---|
2143 | }
|
---|
2144 |
|
---|
2145 | uint16_t caid = b2i(2, buf + i + 2);
|
---|
2146 | uint16_t emm_pid = b2i(2, buf + i +4) & 0x1FFF;
|
---|
2147 | uint32_t emm_provider = 0;
|
---|
2148 |
|
---|
2149 | switch(caid >> 8)
|
---|
2150 | {
|
---|
2151 | case 0x01:
|
---|
2152 | dvbapi_add_emmpid(demux_id, caid, emm_pid, 0, 0, EMM_UNIQUE | EMM_GLOBAL);
|
---|
2153 | for(k = i + 7; k < i + buf[i + 1] + 2; k += 4)
|
---|
2154 | {
|
---|
2155 | emm_provider = b2i(2, buf + k + 2);
|
---|
2156 | emm_pid = b2i(2, buf + k) & 0xFFF;
|
---|
2157 | dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, 0, EMM_SHARED);
|
---|
2158 | }
|
---|
2159 | break;
|
---|
2160 |
|
---|
2161 | case 0x05:
|
---|
2162 | for(k = i + 6; k < i + buf[i + 1] + 2; k += buf[k + 1] + 2)
|
---|
2163 | {
|
---|
2164 | if(buf[k] == 0x14)
|
---|
2165 | {
|
---|
2166 | emm_provider = (b2i(3, buf + k + 2) & 0xFFFFF0); // viaccess fixup last digit is a dont care!
|
---|
2167 | dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
|
---|
2168 | }
|
---|
2169 | }
|
---|
2170 | break;
|
---|
2171 |
|
---|
2172 | case 0x18:
|
---|
2173 | if(buf[i + 1] == 0x07 || buf[i + 1] == 0x0B)
|
---|
2174 | {
|
---|
2175 | for(k = i + 7; k < i + 7 + buf[i + 6]; k += 2)
|
---|
2176 | {
|
---|
2177 | emm_provider = b2i(2, buf + k);
|
---|
2178 | dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
|
---|
2179 | }
|
---|
2180 | }
|
---|
2181 | else
|
---|
2182 | {
|
---|
2183 | dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
|
---|
2184 | }
|
---|
2185 | break;
|
---|
2186 |
|
---|
2187 | case 0x27:
|
---|
2188 | case 0x4A:
|
---|
2189 | if(caid_is_bulcrypt(caid))
|
---|
2190 | {
|
---|
2191 | dvbapi_add_emmpid(demux_id, caid, emm_pid, 0, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
|
---|
2192 | break;
|
---|
2193 | }
|
---|
2194 | emm_provider = (uint32_t)buf[i + 6];
|
---|
2195 | if(buf[i + 6] == 0xFE)
|
---|
2196 | {
|
---|
2197 | dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, 0x102, EMM_GLOBAL);
|
---|
2198 | }
|
---|
2199 | else
|
---|
2200 | {
|
---|
2201 | uint32_t cadata = 0;
|
---|
2202 | if(buf[i + 1] == 0x0A)
|
---|
2203 | {
|
---|
2204 | cadata = (buf[i + 8] << 24) | (buf[i + 9] << 16) | (buf[i + 10] << 8) | buf[i + 11];
|
---|
2205 | }
|
---|
2206 | dvbapi_add_emmpid(demux_id, caid, emm_pid, emm_provider, cadata, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
|
---|
2207 | }
|
---|
2208 | break;
|
---|
2209 |
|
---|
2210 | default:
|
---|
2211 | dvbapi_add_emmpid(demux_id, caid, emm_pid, 0, 0, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL);
|
---|
2212 | break;
|
---|
2213 | }
|
---|
2214 | }
|
---|
2215 | return;
|
---|
2216 | }
|
---|
2217 |
|
---|
2218 | static pthread_mutex_t lockindex = PTHREAD_MUTEX_INITIALIZER;
|
---|
2219 |
|
---|
2220 | uint32_t dvbapi_get_desc_index(int32_t demux_id, int32_t pid, int32_t stream_id)
|
---|
2221 | {
|
---|
2222 | int32_t i, j, k, fail = 1;
|
---|
2223 | uint32_t idx = 0;
|
---|
2224 |
|
---|
2225 | if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
|
---|
2226 | {
|
---|
2227 | sscanf(demux[demux_id].pmt_file, "pmt%3d.tmp", &idx);
|
---|
2228 | return idx;
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | SAFE_MUTEX_LOCK(&lockindex); // to avoid race when readers become responsive!
|
---|
2232 | while(fail && idx <= INDEX_MAX)
|
---|
2233 | {
|
---|
2234 | fail = 0;
|
---|
2235 | for(i = 0; i < MAX_DEMUX && !fail && idx < INDEX_MAX; i++)
|
---|
2236 | {
|
---|
2237 | if(demux[i].program_number == 0)
|
---|
2238 | {
|
---|
2239 | continue; // skip empty demuxers
|
---|
2240 | }
|
---|
2241 |
|
---|
2242 | if(demux[i].ca_mask != demux[demux_id].ca_mask
|
---|
2243 | && (!(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)))
|
---|
2244 | {
|
---|
2245 | continue; // skip demuxer using other ca device
|
---|
2246 | }
|
---|
2247 |
|
---|
2248 | for(j = 0; j < demux[i].ECMpidcount && !fail; j++) // search for new unique index
|
---|
2249 | {
|
---|
2250 | for(k = 0; k < MAX_STREAM_INDICES; k++)
|
---|
2251 | {
|
---|
2252 | if(demux[i].ECMpids[j].index[k] == idx)
|
---|
2253 | {
|
---|
2254 | fail = 1;
|
---|
2255 | idx++;
|
---|
2256 | }
|
---|
2257 | }
|
---|
2258 | }
|
---|
2259 | }
|
---|
2260 | }
|
---|
2261 |
|
---|
2262 | if(selected_api == DVBAPI_3 || selected_api == DVBAPI_1)
|
---|
2263 | {
|
---|
2264 | // make sure we haven't run out of descramblers
|
---|
2265 | if(ca_descramblers_used < ca_descramblers_total)
|
---|
2266 | {
|
---|
2267 | ca_descramblers_used++; // increase number of descramblers used
|
---|
2268 | }
|
---|
2269 | else
|
---|
2270 | {
|
---|
2271 | idx = INDEX_INVALID; // we don't have any descramblers left
|
---|
2272 | }
|
---|
2273 | }
|
---|
2274 | else // old style check for STAPI, COOLAPI, etc
|
---|
2275 | {
|
---|
2276 | if(idx > ca_descramblers_total)
|
---|
2277 | {
|
---|
2278 | idx = INDEX_INVALID; // we don't have any descramblers left
|
---|
2279 | }
|
---|
2280 | }
|
---|
2281 |
|
---|
2282 | demux[demux_id].ECMpids[pid].index[stream_id] = idx;
|
---|
2283 | SAFE_MUTEX_UNLOCK(&lockindex); // and release it!
|
---|
2284 |
|
---|
2285 | return idx;
|
---|
2286 | }
|
---|
2287 |
|
---|
2288 | void dvbapi_set_pid(int32_t demux_id, int32_t num, uint32_t idx, bool enable, bool use_des, uint32_t msgid)
|
---|
2289 | {
|
---|
2290 | int32_t i, currentfd;
|
---|
2291 | uint16_t streampid = demux[demux_id].STREAMpids[num];
|
---|
2292 | uint32_t newidx = 0, curidx;
|
---|
2293 | ca_pid_t ca_pid2;
|
---|
2294 |
|
---|
2295 | if(demux[demux_id].pidindex == -1 && enable)
|
---|
2296 | {
|
---|
2297 | return; // no current pid on enable? --> exit
|
---|
2298 | }
|
---|
2299 |
|
---|
2300 | switch(selected_api)
|
---|
2301 | {
|
---|
2302 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
2303 | case STAPI:
|
---|
2304 | if(!enable) idx = INDEX_INVALID;
|
---|
2305 | stapi_set_pid(demux_id, num, idx, streampid, demux[demux_id].pmt_file); // only used to disable pids!!!
|
---|
2306 | break;
|
---|
2307 | #endif
|
---|
2308 |
|
---|
2309 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
2310 | case COOLAPI:
|
---|
2311 | break;
|
---|
2312 | #endif
|
---|
2313 |
|
---|
2314 | default:
|
---|
2315 | for(i = 0; i < CA_MAX; i++)
|
---|
2316 | {
|
---|
2317 | newidx = INDEX_INVALID;
|
---|
2318 | curidx = idx;
|
---|
2319 |
|
---|
2320 | if(((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
|
---|
2321 | {
|
---|
2322 | uint32_t action = 0;
|
---|
2323 | if(enable)
|
---|
2324 | {
|
---|
2325 | action = update_streampid_list(i, streampid, curidx, use_des);
|
---|
2326 | }
|
---|
2327 |
|
---|
2328 | if(!enable)
|
---|
2329 | {
|
---|
2330 | action = remove_streampid_from_list(i, streampid, curidx);
|
---|
2331 | }
|
---|
2332 |
|
---|
2333 | if(action != NO_STREAMPID_LISTED && action != INVALID_STREAMPID_INDEX &&
|
---|
2334 | action != FOUND_STREAMPID_INDEX && action != ADDED_STREAMPID_INDEX &&
|
---|
2335 | action != REMOVED_STREAMPID_INDEX)
|
---|
2336 | {
|
---|
2337 | // removed last index of this streampid on ca? -> disable this pid with -1 on this ca
|
---|
2338 | if((action == REMOVED_STREAMPID_LASTINDEX || action == FIRST_STREAMPID_INDEX)
|
---|
2339 | && (is_ca_used(i, streampid) == INDEX_INVALID))
|
---|
2340 | {
|
---|
2341 | curidx = DVBAPI_INDEX_DISABLE;
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 | // removed index of streampid that is used to decode on ca -> get a fresh one
|
---|
2345 | if(action == REMOVED_DECODING_STREAMPID_INDEX || action == FIRST_STREAMPID_INDEX)
|
---|
2346 | {
|
---|
2347 | newidx = is_ca_used(i, streampid); // get an active index for this pid and enable it on ca device
|
---|
2348 | curidx = DVBAPI_INDEX_DISABLE;
|
---|
2349 | }
|
---|
2350 |
|
---|
2351 | while(curidx != INDEX_INVALID || newidx != INDEX_INVALID)
|
---|
2352 | {
|
---|
2353 | memset(&ca_pid2, 0, sizeof(ca_pid2));
|
---|
2354 | ca_pid2.pid = streampid;
|
---|
2355 | if(curidx != INDEX_INVALID)
|
---|
2356 | {
|
---|
2357 | (curidx == DVBAPI_INDEX_DISABLE) ? (ca_pid2.index = -1) : (ca_pid2.index = curidx);
|
---|
2358 | cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x index=%d on ca%d",
|
---|
2359 | demux_id,
|
---|
2360 | ((enable && curidx != DVBAPI_INDEX_DISABLE) ? "enable" : "disable"),
|
---|
2361 | num + 1,
|
---|
2362 | ca_pid2.pid,
|
---|
2363 | ca_pid2.index,
|
---|
2364 | i);
|
---|
2365 | curidx = INDEX_INVALID; // flag this index as handled
|
---|
2366 | }
|
---|
2367 | else if(newidx != INDEX_INVALID)
|
---|
2368 | {
|
---|
2369 | (newidx == DVBAPI_INDEX_DISABLE) ? (ca_pid2.index = -1) : (ca_pid2.index = newidx);
|
---|
2370 | cs_log_dbg(D_DVBAPI, "Demuxer %d %s stream %d pid=0x%04x by index=%d on ca%d",
|
---|
2371 | demux_id,
|
---|
2372 | ((enable && action == FIRST_STREAMPID_INDEX) ? "enable" : "takeover"),
|
---|
2373 | num + 1,
|
---|
2374 | ca_pid2.pid,
|
---|
2375 | ca_pid2.index,
|
---|
2376 | i);
|
---|
2377 | newidx = INDEX_INVALID; // flag this takeover / new index as handled
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | if(use_des && cfg.dvbapi_extended_cw_api == 2 && ca_pid2.index != -1)
|
---|
2381 | {
|
---|
2382 | ca_pid2.index |= 0x100; // flag DES algo through pid index
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
2386 | {
|
---|
2387 | dvbapi_net_send(DVBAPI_CA_SET_PID, demux[demux_id].socket_fd,
|
---|
2388 | msgid,
|
---|
2389 | demux_id,
|
---|
2390 | -1 /*unused*/,
|
---|
2391 | (uint8_t *) &ca_pid2,
|
---|
2392 | NULL,
|
---|
2393 | NULL,
|
---|
2394 | demux[demux_id].client_proto_version);
|
---|
2395 | }
|
---|
2396 | else
|
---|
2397 | {
|
---|
2398 | currentfd = ca_fd[i];
|
---|
2399 | if(currentfd <= 0)
|
---|
2400 | {
|
---|
2401 | currentfd = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
|
---|
2402 | ca_fd[i] = currentfd; // save fd of this ca
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | if(currentfd > 0)
|
---|
2406 | {
|
---|
2407 | if(dvbapi_ioctl(currentfd, CA_SET_PID, &ca_pid2) == -1)
|
---|
2408 | {
|
---|
2409 | cs_log_dbg(D_TRACE | D_DVBAPI,"CA_SET_PID ioctl error (errno=%d %s)", errno, strerror(errno));
|
---|
2410 | remove_streampid_from_list(i, ca_pid2.pid, INDEX_DISABLE_ALL);
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 | uint32_t result = is_ca_used(i, 0); // check if in use by any pid
|
---|
2414 | if(result == INDEX_INVALID)
|
---|
2415 | {
|
---|
2416 | cs_log_dbg(D_DVBAPI, "Demuxer %d close now unused CA%d device", demux_id, i);
|
---|
2417 | int32_t ret = close(currentfd);
|
---|
2418 | if(ret < 0)
|
---|
2419 | {
|
---|
2420 | cs_log("ERROR: Could not close demuxer fd (errno=%d %s)", errno, strerror(errno));
|
---|
2421 | }
|
---|
2422 | currentfd = ca_fd[i] = 0;
|
---|
2423 | }
|
---|
2424 | }
|
---|
2425 | }
|
---|
2426 | }
|
---|
2427 | }
|
---|
2428 | }
|
---|
2429 | }
|
---|
2430 | break;
|
---|
2431 | }
|
---|
2432 | return;
|
---|
2433 | }
|
---|
2434 |
|
---|
2435 | void dvbapi_stop_all_descrambling(uint32_t msgid)
|
---|
2436 | {
|
---|
2437 | int32_t j;
|
---|
2438 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
2439 | {
|
---|
2440 | if(demux[j].program_number == 0) { continue; }
|
---|
2441 | dvbapi_stop_descrambling(j, msgid);
|
---|
2442 | }
|
---|
2443 | }
|
---|
2444 |
|
---|
2445 | void dvbapi_stop_all_emm_sdt_filtering(uint32_t msgid)
|
---|
2446 | {
|
---|
2447 | int32_t j;
|
---|
2448 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
2449 | {
|
---|
2450 | if(demux[j].program_number == 0) { continue; }
|
---|
2451 | dvbapi_stop_filter(j, TYPE_EMM, msgid);
|
---|
2452 | dvbapi_stop_filter(j, TYPE_SDT, msgid);
|
---|
2453 | demux[j].emm_filter = -1;
|
---|
2454 | }
|
---|
2455 | }
|
---|
2456 |
|
---|
2457 | void dvbapi_stop_descrambling(int32_t demux_id, uint32_t msgid)
|
---|
2458 | {
|
---|
2459 | int32_t i, j, z;
|
---|
2460 | if(demux[demux_id].program_number == 0) { return; }
|
---|
2461 | char channame[CS_SERVICENAME_SIZE];
|
---|
2462 |
|
---|
2463 | i = demux[demux_id].pidindex;
|
---|
2464 | if(i < 0)
|
---|
2465 | {
|
---|
2466 | i = 0;
|
---|
2467 | }
|
---|
2468 |
|
---|
2469 | demux[demux_id].pidindex = -1; // no ecmpid is to be descrambling since we start stop descrambling!
|
---|
2470 |
|
---|
2471 | get_servicename(dvbapi_client, demux[demux_id].program_number,
|
---|
2472 | demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].PROVID : NO_PROVID_VALUE,
|
---|
2473 | demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[i].CAID : NO_CAID_VALUE,
|
---|
2474 | channame, sizeof(channame));
|
---|
2475 |
|
---|
2476 | cs_log("Demuxer %d stop descrambling program number %04X (%s)",
|
---|
2477 | demux_id, demux[demux_id].program_number, channame);
|
---|
2478 |
|
---|
2479 | dvbapi_stop_filter(demux_id, TYPE_EMM, msgid);
|
---|
2480 | dvbapi_stop_filter(demux_id, TYPE_SDT, msgid);
|
---|
2481 | dvbapi_stop_filter(demux_id, TYPE_PAT, msgid);
|
---|
2482 | dvbapi_stop_filter(demux_id, TYPE_PMT, msgid);
|
---|
2483 |
|
---|
2484 | for(i = 0; i < demux[demux_id].ECMpidcount && demux[demux_id].ECMpidcount > 0; i++)
|
---|
2485 | {
|
---|
2486 | for(j = 0; j < MAX_STREAM_INDICES; j++)
|
---|
2487 | {
|
---|
2488 | if(demux[demux_id].ECMpids[i].index[j] == INDEX_INVALID)
|
---|
2489 | {
|
---|
2490 | continue;
|
---|
2491 | }
|
---|
2492 |
|
---|
2493 | // disable streams!
|
---|
2494 | for(z = 0; z < demux[demux_id].STREAMpidcount; z++)
|
---|
2495 | {
|
---|
2496 | dvbapi_set_pid(demux_id, z, demux[demux_id].ECMpids[i].index[j], false, false, msgid); // disable streampid
|
---|
2497 | }
|
---|
2498 | demux[demux_id].ECMpids[i].index[j] = INDEX_INVALID;
|
---|
2499 | }
|
---|
2500 | }
|
---|
2501 | dvbapi_stop_filter(demux_id, TYPE_ECM, msgid);
|
---|
2502 |
|
---|
2503 | pthread_mutex_destroy(&demux[demux_id].answerlock);
|
---|
2504 | memset(&demux[demux_id], 0 , sizeof(DEMUXTYPE));
|
---|
2505 |
|
---|
2506 | SAFE_MUTEX_INIT(&demux[demux_id].answerlock, NULL);
|
---|
2507 | for(i = 0; i < MAX_ECM_PIDS; i++)
|
---|
2508 | {
|
---|
2509 | for(j = 0; j < MAX_STREAM_INDICES; j++)
|
---|
2510 | {
|
---|
2511 | demux[demux_id].ECMpids[i].index[j] = INDEX_INVALID;
|
---|
2512 | }
|
---|
2513 | }
|
---|
2514 | demux[demux_id].pidindex = -1;
|
---|
2515 | demux[demux_id].curindex = -1;
|
---|
2516 |
|
---|
2517 | if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
|
---|
2518 | {
|
---|
2519 | unlink(ECMINFO_FILE);
|
---|
2520 | }
|
---|
2521 | return;
|
---|
2522 | }
|
---|
2523 |
|
---|
2524 | int32_t dvbapi_start_descrambling(int32_t demux_id, int32_t pid, int8_t checked, uint32_t msgid)
|
---|
2525 | {
|
---|
2526 | int32_t started = 0; // in case ecmfilter started = 1
|
---|
2527 | int32_t fake_ecm = 0;
|
---|
2528 | ECM_REQUEST *er;
|
---|
2529 | struct s_reader *rdr;
|
---|
2530 |
|
---|
2531 | if(!(er = get_ecmtask()))
|
---|
2532 | {
|
---|
2533 | return started;
|
---|
2534 | }
|
---|
2535 | demux[demux_id].ECMpids[pid].checked = checked + 1; // mark this pid as checked!
|
---|
2536 |
|
---|
2537 | struct s_dvbapi_priority *p;
|
---|
2538 | for(p = dvbapi_priority; p != NULL ; p = p->next)
|
---|
2539 | {
|
---|
2540 | if((p->type != 'p')
|
---|
2541 | || (p->caid && p->caid != demux[demux_id].ECMpids[pid].CAID)
|
---|
2542 | || (p->provid && p->provid != demux[demux_id].ECMpids[pid].PROVID)
|
---|
2543 | || (p->ecmpid && p->ecmpid != demux[demux_id].ECMpids[pid].ECM_PID)
|
---|
2544 | || (p->srvid && p->srvid != demux[demux_id].program_number)
|
---|
2545 | || (p->pidx && p->pidx-1 != pid)
|
---|
2546 | || (p->cadata && p->cadata != demux[demux_id].ECMpids[pid].cadata))
|
---|
2547 | {
|
---|
2548 | continue;
|
---|
2549 | }
|
---|
2550 |
|
---|
2551 | // if found chid and first run apply chid filter, on forced pids always apply!
|
---|
2552 | if(p->type == 'p' && p->chid < 0x10000 && (demux[demux_id].ECMpids[pid].checked == 1 || (p && p->force)))
|
---|
2553 | {
|
---|
2554 | if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
|
---|
2555 | {
|
---|
2556 | er->chid = demux[demux_id].ECMpids[pid].CHID;
|
---|
2557 | }
|
---|
2558 | else
|
---|
2559 | {
|
---|
2560 | er->chid = p->chid; // no channelcache or no chid in use, so use prio chid
|
---|
2561 | demux[demux_id].ECMpids[pid].CHID = p->chid;
|
---|
2562 | }
|
---|
2563 | //cs_log("********* CHID %04X **************", demux[demux_id].ECMpids[pid].CHID);
|
---|
2564 | break; // we only accept one!
|
---|
2565 | }
|
---|
2566 | else
|
---|
2567 | {
|
---|
2568 | if(demux[demux_id].ECMpids[pid].CHID < 0x10000) // channelcache delivered chid
|
---|
2569 | {
|
---|
2570 | er->chid = demux[demux_id].ECMpids[pid].CHID;
|
---|
2571 | }
|
---|
2572 | else // no channelcache or no chid in use
|
---|
2573 | {
|
---|
2574 | er->chid = 0;
|
---|
2575 | demux[demux_id].ECMpids[pid].CHID = 0x10000;
|
---|
2576 | }
|
---|
2577 | }
|
---|
2578 | }
|
---|
2579 |
|
---|
2580 | er->srvid = demux[demux_id].program_number;
|
---|
2581 | er->caid = demux[demux_id].ECMpids[pid].CAID;
|
---|
2582 | er->pid = demux[demux_id].ECMpids[pid].ECM_PID;
|
---|
2583 | er->prid = demux[demux_id].ECMpids[pid].PROVID;
|
---|
2584 | er->vpid = demux[demux_id].ECMpids[pid].VPID;
|
---|
2585 | er->pmtpid = demux[demux_id].pmtpid;
|
---|
2586 | er->onid = demux[demux_id].onid;
|
---|
2587 | er->msgid = msgid;
|
---|
2588 |
|
---|
2589 | #ifdef WITH_STAPI5
|
---|
2590 | cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
|
---|
2591 | #endif
|
---|
2592 | struct timeb now;
|
---|
2593 | cs_ftime(&now);
|
---|
2594 |
|
---|
2595 | for(rdr = first_active_reader; rdr != NULL ; rdr = rdr->next)
|
---|
2596 | {
|
---|
2597 | int8_t match = matching_reader(er, rdr); // check for matching reader
|
---|
2598 | int64_t gone = comp_timeb(&now, &rdr->emm_last);
|
---|
2599 | if(gone > 3600*1000 && rdr->needsemmfirst && caid_is_irdeto(er->caid))
|
---|
2600 | {
|
---|
2601 | cs_log("Warning reader %s received no emms for the last %d seconds -> skip, this reader needs emms first!",
|
---|
2602 | rdr->label, (int)(gone / 1000));
|
---|
2603 | continue; // skip this card needs to process emms first before it can be used for descramble
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 | if(p && p->force)
|
---|
2607 | {
|
---|
2608 | match = 1; // forced pid always started!
|
---|
2609 | }
|
---|
2610 |
|
---|
2611 | if(!match) // if this reader does not match, check betatunnel for it
|
---|
2612 | {
|
---|
2613 | match = lb_check_auto_betatunnel(er, rdr);
|
---|
2614 | }
|
---|
2615 |
|
---|
2616 | if(!match && chk_is_betatunnel_caid(er->caid)) // these caids might be tunneled invisible by peers
|
---|
2617 | {
|
---|
2618 | match = 1; // so make it a match to try it!
|
---|
2619 | }
|
---|
2620 |
|
---|
2621 | // check if cache-ex is matching
|
---|
2622 | if(config_enabled(CS_CACHEEX) && (!match && (cacheex_is_match_alias(dvbapi_client, er))))
|
---|
2623 | {
|
---|
2624 | match = 1; // so make it a match to try it!
|
---|
2625 | }
|
---|
2626 |
|
---|
2627 | // BISS1 and BISS2 mode 1/E or FAKE caid
|
---|
2628 | // ecm pid is fake, so send out one fake ecm request
|
---|
2629 | // special treatment: if we asked the cw first without starting a filter,
|
---|
2630 | // the cw request will be killed due to no ecmfilter started
|
---|
2631 | if(caid_is_fake(demux[demux_id].ECMpids[pid].CAID) || caid_is_biss_fixed(demux[demux_id].ECMpids[pid].CAID))
|
---|
2632 | {
|
---|
2633 | int32_t j, n;
|
---|
2634 | er->ecmlen = 5;
|
---|
2635 | er->ecm[0] = 0x80; // to pass the cache check it must be 0x80 or 0x81
|
---|
2636 | er->ecm[1] = 0x00;
|
---|
2637 | er->ecm[2] = 0x02;
|
---|
2638 | i2b_buf(2, er->srvid, er->ecm + 3);
|
---|
2639 |
|
---|
2640 | for(j = 0, n = 5; j < demux[demux_id].STREAMpidcount; j++, n += 2)
|
---|
2641 | {
|
---|
2642 | i2b_buf(2, demux[demux_id].STREAMpids[j], er->ecm + n);
|
---|
2643 | er->ecm[2] += 2;
|
---|
2644 | er->ecmlen += 2;
|
---|
2645 | }
|
---|
2646 |
|
---|
2647 | cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
|
---|
2648 | demux_id,
|
---|
2649 | pid,
|
---|
2650 | demux[demux_id].ECMpids[pid].CAID,
|
---|
2651 | demux[demux_id].ECMpids[pid].PROVID,
|
---|
2652 | demux[demux_id].ECMpids[pid].ECM_PID,
|
---|
2653 | demux[demux_id].pmtpid,
|
---|
2654 | demux[demux_id].ECMpids[pid].VPID);
|
---|
2655 |
|
---|
2656 | demux[demux_id].curindex = pid; // set current pid to the fresh started one
|
---|
2657 | dvbapi_start_filter(demux_id,
|
---|
2658 | pid,
|
---|
2659 | demux[demux_id].ECMpids[pid].ECM_PID,
|
---|
2660 | demux[demux_id].ECMpids[pid].CAID,
|
---|
2661 | demux[demux_id].ECMpids[pid].PROVID,
|
---|
2662 | 0x80,
|
---|
2663 | 0xF0,
|
---|
2664 | 3000,
|
---|
2665 | TYPE_ECM);
|
---|
2666 |
|
---|
2667 | started = 1;
|
---|
2668 | request_cw(dvbapi_client, er, demux_id, 0); // do not register ecm since this try!
|
---|
2669 | fake_ecm = 1;
|
---|
2670 | break; // we started an ecmfilter so stop looking for next matching reader!
|
---|
2671 | }
|
---|
2672 |
|
---|
2673 | if(match) // if matching reader found check for irdeto cas if local irdeto card check if it received emms in last 60 minutes
|
---|
2674 | {
|
---|
2675 | if(caid_is_irdeto(er->caid)) // irdeto cas init irdeto_curindex to wait for first index (00)
|
---|
2676 | {
|
---|
2677 | if(demux[demux_id].ECMpids[pid].irdeto_curindex == 0xFE)
|
---|
2678 | {
|
---|
2679 | demux[demux_id].ECMpids[pid].irdeto_curindex = 0x00;
|
---|
2680 | }
|
---|
2681 | }
|
---|
2682 |
|
---|
2683 | if(p && p->chid < 0x10000) // do we prio a certain chid?
|
---|
2684 | {
|
---|
2685 | cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X PMTPID %04X VPID %04X",
|
---|
2686 | demux_id, pid,
|
---|
2687 | demux[demux_id].ECMpids[pid].CAID,
|
---|
2688 | demux[demux_id].ECMpids[pid].PROVID,
|
---|
2689 | demux[demux_id].ECMpids[pid].ECM_PID,
|
---|
2690 | demux[demux_id].ECMpids[pid].CHID,
|
---|
2691 | demux[demux_id].pmtpid,
|
---|
2692 | demux[demux_id].ECMpids[pid].VPID);
|
---|
2693 | }
|
---|
2694 | else
|
---|
2695 | {
|
---|
2696 | cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
|
---|
2697 | demux_id, pid,
|
---|
2698 | demux[demux_id].ECMpids[pid].CAID,
|
---|
2699 | demux[demux_id].ECMpids[pid].PROVID,
|
---|
2700 | demux[demux_id].ECMpids[pid].ECM_PID,
|
---|
2701 | demux[demux_id].pmtpid,
|
---|
2702 | demux[demux_id].ECMpids[pid].VPID);
|
---|
2703 | }
|
---|
2704 |
|
---|
2705 | demux[demux_id].curindex = pid; // set current pid to the fresh started one
|
---|
2706 | dvbapi_start_filter(demux_id,
|
---|
2707 | pid,
|
---|
2708 | demux[demux_id].ECMpids[pid].ECM_PID,
|
---|
2709 | demux[demux_id].ECMpids[pid].CAID,
|
---|
2710 | demux[demux_id].ECMpids[pid].PROVID,
|
---|
2711 | 0x80,
|
---|
2712 | 0xF0,
|
---|
2713 | 3000,
|
---|
2714 | TYPE_ECM);
|
---|
2715 |
|
---|
2716 | started = 1;
|
---|
2717 | break; // we started an ecmfilter so stop looking for next matching reader!
|
---|
2718 | }
|
---|
2719 | }
|
---|
2720 |
|
---|
2721 | if(demux[demux_id].curindex != pid)
|
---|
2722 | {
|
---|
2723 | cs_log("Demuxer %d impossible to descramble PID %d CAID %04X PROVID %06X ECMPID %04X PMTPID %04X (NO MATCHING READER)",
|
---|
2724 | demux_id, pid,
|
---|
2725 | demux[demux_id].ECMpids[pid].CAID,
|
---|
2726 | demux[demux_id].ECMpids[pid].PROVID,
|
---|
2727 | demux[demux_id].ECMpids[pid].ECM_PID,
|
---|
2728 | demux[demux_id].pmtpid);
|
---|
2729 |
|
---|
2730 | demux[demux_id].ECMpids[pid].checked = 4; // flag this pid as checked
|
---|
2731 | demux[demux_id].ECMpids[pid].status = -1; // flag this pid as unusable
|
---|
2732 | dvbapi_edit_channel_cache(demux_id, pid, 0); // remove this pid from channelcache
|
---|
2733 | }
|
---|
2734 |
|
---|
2735 | if(!fake_ecm)
|
---|
2736 | {
|
---|
2737 | NULLFREE(er);
|
---|
2738 | }
|
---|
2739 | return started;
|
---|
2740 | }
|
---|
2741 |
|
---|
2742 | struct s_dvbapi_priority *dvbapi_check_prio_match_emmpid(int32_t demux_id, uint16_t caid, uint32_t provid, char type)
|
---|
2743 | {
|
---|
2744 | struct s_dvbapi_priority *p;
|
---|
2745 | int32_t i;
|
---|
2746 | uint16_t ecm_pid = 0;
|
---|
2747 |
|
---|
2748 | for(i = 0; i < demux[demux_id].ECMpidcount; i++)
|
---|
2749 | {
|
---|
2750 | if((demux[demux_id].ECMpids[i].CAID == caid) && (demux[demux_id].ECMpids[i].PROVID == provid))
|
---|
2751 | {
|
---|
2752 | ecm_pid = demux[demux_id].ECMpids[i].ECM_PID;
|
---|
2753 | break;
|
---|
2754 | }
|
---|
2755 | }
|
---|
2756 |
|
---|
2757 | if(!ecm_pid)
|
---|
2758 | {
|
---|
2759 | return NULL;
|
---|
2760 | }
|
---|
2761 |
|
---|
2762 | for(p = dvbapi_priority; p != NULL; p = p->next)
|
---|
2763 | {
|
---|
2764 | if(p->type != type
|
---|
2765 | || (p->caid && p->caid != caid)
|
---|
2766 | || (p->provid && p->provid != provid)
|
---|
2767 | || (p->ecmpid && p->ecmpid != ecm_pid)
|
---|
2768 | || (p->srvid && p->srvid != demux[demux_id].program_number)
|
---|
2769 | || (p->pidx && p->pidx-1 !=i)
|
---|
2770 | || (p->type == 'i' && (p->chid < 0x10000)))
|
---|
2771 | {
|
---|
2772 | continue;
|
---|
2773 | }
|
---|
2774 |
|
---|
2775 | return p;
|
---|
2776 | }
|
---|
2777 | return NULL;
|
---|
2778 | }
|
---|
2779 |
|
---|
2780 | struct s_dvbapi_priority *dvbapi_check_prio_match(int32_t demux_id, int32_t pidindex, char type)
|
---|
2781 | {
|
---|
2782 | if(!dvbapi_priority)
|
---|
2783 | {
|
---|
2784 | return NULL;
|
---|
2785 | }
|
---|
2786 |
|
---|
2787 | struct s_dvbapi_priority *p;
|
---|
2788 | struct s_ecmpid *ecmpid = &demux[demux_id].ECMpids[pidindex];
|
---|
2789 |
|
---|
2790 | for(p = dvbapi_priority; p != NULL; p = p->next)
|
---|
2791 | {
|
---|
2792 | if(p->type != type
|
---|
2793 | || (p->caid && p->caid != ecmpid->CAID)
|
---|
2794 | || (p->provid && p->provid != ecmpid->PROVID)
|
---|
2795 | || (p->ecmpid && p->ecmpid != ecmpid->ECM_PID)
|
---|
2796 | || (p->srvid && p->srvid != demux[demux_id].program_number)
|
---|
2797 | || (p->pidx && p->pidx-1 != pidindex)
|
---|
2798 | || (p->chid < 0x10000 && p->chid != ecmpid->CHID))
|
---|
2799 | {
|
---|
2800 | continue;
|
---|
2801 | }
|
---|
2802 |
|
---|
2803 | return p;
|
---|
2804 | }
|
---|
2805 | return NULL;
|
---|
2806 | }
|
---|
2807 |
|
---|
2808 | void dvbapi_process_emm(int32_t demux_id, int32_t filter_num, uint8_t *buffer, uint32_t len)
|
---|
2809 | {
|
---|
2810 | EMM_PACKET epg;
|
---|
2811 |
|
---|
2812 | struct s_emm_filter *filter = get_emmfilter_by_filternum(demux_id, filter_num + 1); // 0 is used for pending emmfilters, so everything increase 1
|
---|
2813 | if(!filter)
|
---|
2814 | {
|
---|
2815 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d no filter matches -> SKIP!", demux_id, filter_num + 1);
|
---|
2816 | return;
|
---|
2817 | }
|
---|
2818 |
|
---|
2819 | uint32_t provider = filter->provid;
|
---|
2820 | uint16_t caid = filter->caid;
|
---|
2821 | struct s_dvbapi_priority *mapentry = dvbapi_check_prio_match_emmpid(filter->demux_id, filter->caid, filter->provid, 'm');
|
---|
2822 | if(mapentry)
|
---|
2823 | {
|
---|
2824 | cs_log_dbg(D_DVBAPI, "Demuxer %d mapping EMM from %04X@%06X to %04X@%06X",
|
---|
2825 | demux_id, caid, provider, mapentry->mapcaid, mapentry->mapprovid);
|
---|
2826 |
|
---|
2827 | caid = mapentry->mapcaid;
|
---|
2828 | provider = mapentry->mapprovid;
|
---|
2829 | }
|
---|
2830 |
|
---|
2831 | memset(&epg, 0, sizeof(epg));
|
---|
2832 | i2b_buf(2, caid, epg.caid);
|
---|
2833 | i2b_buf(4, provider, epg.provid);
|
---|
2834 | epg.emmlen = len > sizeof(epg.emm) ? sizeof(epg.emm) : len;
|
---|
2835 | memcpy(epg.emm, buffer, epg.emmlen);
|
---|
2836 |
|
---|
2837 | if(config_enabled(READER_IRDETO) && chk_is_betatunnel_caid(caid) == 2)
|
---|
2838 | {
|
---|
2839 | uint16_t ncaid = tunemm_caid_map(FROM_TO, caid, demux[demux_id].program_number);
|
---|
2840 | if(caid != ncaid)
|
---|
2841 | {
|
---|
2842 | irdeto_add_emm_header(&epg);
|
---|
2843 | i2b_buf(2, ncaid, epg.caid);
|
---|
2844 | }
|
---|
2845 | }
|
---|
2846 | do_emm(dvbapi_client, &epg);
|
---|
2847 | }
|
---|
2848 |
|
---|
2849 | void dvbapi_read_priority(void)
|
---|
2850 | {
|
---|
2851 | FILE *fp;
|
---|
2852 | char token[128], str1[128];
|
---|
2853 | char type;
|
---|
2854 | int32_t i, ret, count = 0;
|
---|
2855 | const char *cs_prio = "oscam.dvbapi";
|
---|
2856 |
|
---|
2857 | fp = fopen(get_config_filename(token, sizeof(token), cs_prio), "r");
|
---|
2858 | if(!fp)
|
---|
2859 | {
|
---|
2860 | cs_log_dbg(D_DVBAPI, "ERROR: Can't open priority file %s", token);
|
---|
2861 | return;
|
---|
2862 | }
|
---|
2863 |
|
---|
2864 | if(dvbapi_priority)
|
---|
2865 | {
|
---|
2866 | cs_log_dbg(D_DVBAPI, "reread priority file %s", cs_prio);
|
---|
2867 | struct s_dvbapi_priority *o, *p;
|
---|
2868 | for(p = dvbapi_priority; p != NULL; p = o)
|
---|
2869 | {
|
---|
2870 | o = p->next;
|
---|
2871 | NULLFREE(p);
|
---|
2872 | }
|
---|
2873 | dvbapi_priority = NULL;
|
---|
2874 | }
|
---|
2875 |
|
---|
2876 | while(fgets(token, sizeof(token), fp))
|
---|
2877 | {
|
---|
2878 | // Ignore comments and empty lines
|
---|
2879 | if(token[0] == '#' || token[0] == '/' || token[0] == '\n' || token[0] == '\r' || token[0] == '\0' || strlen(token) > 100)
|
---|
2880 | {
|
---|
2881 | continue;
|
---|
2882 | }
|
---|
2883 | memset(str1, 0, 128);
|
---|
2884 |
|
---|
2885 | for(i = 0; i < (int)strlen(token) && token[i] == ' '; i++) { ; }
|
---|
2886 |
|
---|
2887 | if(i == (int)strlen(token) - 1) // empty line or all spaces
|
---|
2888 | {
|
---|
2889 | continue;
|
---|
2890 | }
|
---|
2891 |
|
---|
2892 | for(i = 0; i < (int)strlen(token); i++)
|
---|
2893 | {
|
---|
2894 | if(token[i] == '@')
|
---|
2895 | {
|
---|
2896 | token[i] = ':';
|
---|
2897 | }
|
---|
2898 | }
|
---|
2899 |
|
---|
2900 | for(i = 0; i < (int)strlen(token); i++)
|
---|
2901 | {
|
---|
2902 | if((token[i] == ':' || token[i] == ' ') && token[i + 1] == ':') // if "::" or " :"
|
---|
2903 | {
|
---|
2904 | memmove(token + i + 2, token + i + 1, strlen(token) - i + 1); // insert extra position
|
---|
2905 | token[i + 1] = '0'; // and fill it with NULL
|
---|
2906 | }
|
---|
2907 |
|
---|
2908 | if(token[i] == '#' || token[i] == '/')
|
---|
2909 | {
|
---|
2910 | token[i] = '\0';
|
---|
2911 | break;
|
---|
2912 | }
|
---|
2913 | }
|
---|
2914 |
|
---|
2915 | type = 0;
|
---|
2916 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
2917 | uint32_t disablefilter = 0;
|
---|
2918 | ret = sscanf(trim(token), "%c: %63s %63s %d", &type, str1, str1 + 64, &disablefilter);
|
---|
2919 | #else
|
---|
2920 | ret = sscanf(trim(token), "%c: %63s %63s", &type, str1, str1 + 64);
|
---|
2921 | #endif
|
---|
2922 | type = tolower((uint8_t)type);
|
---|
2923 |
|
---|
2924 | if(ret < 1 || (type != 'p' && type != 'i' && type != 'm' && type != 'd' &&
|
---|
2925 | type != 's' && type != 'l' && type != 'j' && type != 'a' && type != 'x'))
|
---|
2926 | {
|
---|
2927 | //fprintf(stderr, "Warning: line containing %s in %s not recognized, ignoring line\n", token, cs_prio);
|
---|
2928 | // fprintf would issue the warning to the command line, which is more consistent with other config warnings
|
---|
2929 | // however it takes OSCam a long time (>4 seconds) to reach this part of the program, so the warnings are
|
---|
2930 | // reaching tty rather late which leads to confusion. So send the warnings to log file instead
|
---|
2931 | cs_log_dbg(D_DVBAPI, "WARN: line containing %s in %s not recognized, ignoring...", token, cs_prio);
|
---|
2932 | continue;
|
---|
2933 | }
|
---|
2934 |
|
---|
2935 | struct s_dvbapi_priority *entry;
|
---|
2936 | if(!cs_malloc(&entry, sizeof(struct s_dvbapi_priority)))
|
---|
2937 | {
|
---|
2938 | ret = fclose(fp);
|
---|
2939 | if(ret < 0)
|
---|
2940 | {
|
---|
2941 | cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno));
|
---|
2942 | }
|
---|
2943 | return;
|
---|
2944 | }
|
---|
2945 |
|
---|
2946 | entry->type = type;
|
---|
2947 | entry->next = NULL;
|
---|
2948 | count++;
|
---|
2949 |
|
---|
2950 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
2951 | if(type == 's')
|
---|
2952 | {
|
---|
2953 | strncpy(entry->devname, str1, 29);
|
---|
2954 | strncpy(entry->pmtfile, str1 + 64, 29);
|
---|
2955 | entry->disablefilter = disablefilter;
|
---|
2956 | cs_log_dbg(D_DVBAPI, "stapi prio: ret=%d | %c: %s %s | disable %d",
|
---|
2957 | ret, type, entry->devname, entry->pmtfile, disablefilter);
|
---|
2958 |
|
---|
2959 | if(!dvbapi_priority)
|
---|
2960 | {
|
---|
2961 | dvbapi_priority = entry;
|
---|
2962 | }
|
---|
2963 | else
|
---|
2964 | {
|
---|
2965 | struct s_dvbapi_priority *p;
|
---|
2966 | for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
|
---|
2967 | p->next = entry;
|
---|
2968 | }
|
---|
2969 | continue;
|
---|
2970 | }
|
---|
2971 | #endif
|
---|
2972 | char c_srvid[34];
|
---|
2973 | c_srvid[0] = '\0';
|
---|
2974 | uint32_t caid = 0, provid = 0, srvid = 0, ecmpid = 0, cadata = 0;;
|
---|
2975 | uint32_t chid = 0x10000; //chid=0 is a valid chid
|
---|
2976 |
|
---|
2977 | ret = sscanf(str1, "%4x:%6x:%33[^:]:%4x:%4x:%8x"SCNx16, &caid, &provid, c_srvid, &ecmpid, &chid, &cadata);
|
---|
2978 | if(ret < 1)
|
---|
2979 | {
|
---|
2980 | cs_log("Error in oscam.dvbapi: ret=%d | %c: %04X %06X %s %04X %04X",
|
---|
2981 | ret, type, caid, provid, c_srvid, ecmpid, chid);
|
---|
2982 | continue; // skip this entry!
|
---|
2983 | }
|
---|
2984 | else
|
---|
2985 | {
|
---|
2986 | cs_log_dbg(D_DVBAPI, "Parsing rule: ret=%d | %c: %04X %06X %s %04X %04X %04X",
|
---|
2987 | ret, type, caid, provid, c_srvid, ecmpid, chid, cadata);
|
---|
2988 | }
|
---|
2989 |
|
---|
2990 | entry->caid = caid;
|
---|
2991 | entry->provid = provid;
|
---|
2992 | entry->ecmpid = ecmpid;
|
---|
2993 | entry->chid = chid;
|
---|
2994 | entry->cadata = cadata;
|
---|
2995 | uint32_t delay = 0, force = 0, mapcaid = 0, mapprovid = 0, mapecmpid = 0, pidx = 0;
|
---|
2996 |
|
---|
2997 | switch(type)
|
---|
2998 | {
|
---|
2999 | case 'i':
|
---|
3000 | ret = sscanf(str1 + 64, "%1d", &pidx);
|
---|
3001 | entry->pidx = pidx + 1;
|
---|
3002 | if(ret < 1) entry->pidx = 0;
|
---|
3003 | break;
|
---|
3004 |
|
---|
3005 | case 'd':
|
---|
3006 | sscanf(str1 + 64, "%4d", &delay);
|
---|
3007 | entry->delay = delay;
|
---|
3008 | break;
|
---|
3009 |
|
---|
3010 | case 'l':
|
---|
3011 | entry->delay = dyn_word_atob(str1 + 64);
|
---|
3012 | if(entry->delay == -1) { entry->delay = 0; }
|
---|
3013 | break;
|
---|
3014 |
|
---|
3015 | case 'p':
|
---|
3016 | ret = sscanf(str1 + 64, "%1d:%1d", &force, &pidx);
|
---|
3017 | entry->force = force;
|
---|
3018 | entry->pidx = pidx + 1;
|
---|
3019 | if(ret < 2) entry->pidx = 0;
|
---|
3020 | break;
|
---|
3021 |
|
---|
3022 | case 'm':
|
---|
3023 | sscanf(str1 + 64, "%4x:%6x", &mapcaid, &mapprovid);
|
---|
3024 | if(!mapcaid) { mapcaid = 0xFFFF; }
|
---|
3025 | entry->mapcaid = mapcaid;
|
---|
3026 | entry->mapprovid = mapprovid;
|
---|
3027 | break;
|
---|
3028 |
|
---|
3029 | case 'a':
|
---|
3030 | case 'j':
|
---|
3031 | sscanf(str1 + 64, "%4x:%6x:%4x", &mapcaid, &mapprovid, &mapecmpid);
|
---|
3032 | if(!mapcaid) { mapcaid = 0xFFFF; }
|
---|
3033 | entry->mapcaid = mapcaid;
|
---|
3034 | entry->mapprovid = mapprovid;
|
---|
3035 | entry->mapecmpid = mapecmpid;
|
---|
3036 | break;
|
---|
3037 | }
|
---|
3038 |
|
---|
3039 | if(c_srvid[0] == '=')
|
---|
3040 | {
|
---|
3041 | struct s_srvid *this;
|
---|
3042 | for(i = 0; i < 16; i++)
|
---|
3043 | {
|
---|
3044 | for(this = cfg.srvid[i]; this != NULL; this = this->next)
|
---|
3045 | {
|
---|
3046 | if(this->name && strcmp(this->name, c_srvid + 1) == 0)
|
---|
3047 | {
|
---|
3048 | struct s_dvbapi_priority *entry2;
|
---|
3049 | if(!cs_malloc(&entry2, sizeof(struct s_dvbapi_priority)))
|
---|
3050 | {
|
---|
3051 | continue;
|
---|
3052 | }
|
---|
3053 |
|
---|
3054 | memcpy(entry2, entry, sizeof(struct s_dvbapi_priority));
|
---|
3055 | entry2->srvid = this->srvid;
|
---|
3056 | cs_log_dbg(D_DVBAPI, "prio srvid: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
|
---|
3057 | ret,
|
---|
3058 | entry2->type,
|
---|
3059 | entry2->caid,
|
---|
3060 | entry2->provid,
|
---|
3061 | entry2->srvid,
|
---|
3062 | entry2->ecmpid,
|
---|
3063 | entry2->chid,
|
---|
3064 | entry2->mapcaid,
|
---|
3065 | entry2->mapprovid,
|
---|
3066 | entry2->mapecmpid,
|
---|
3067 | entry2->force,
|
---|
3068 | entry2->delay);
|
---|
3069 |
|
---|
3070 | if(!dvbapi_priority)
|
---|
3071 | {
|
---|
3072 | dvbapi_priority = entry2;
|
---|
3073 | }
|
---|
3074 | else
|
---|
3075 | {
|
---|
3076 | struct s_dvbapi_priority *p;
|
---|
3077 | for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
|
---|
3078 | p->next = entry2;
|
---|
3079 | }
|
---|
3080 | }
|
---|
3081 | }
|
---|
3082 | }
|
---|
3083 | NULLFREE(entry);
|
---|
3084 | continue;
|
---|
3085 | }
|
---|
3086 | else
|
---|
3087 | {
|
---|
3088 | sscanf(c_srvid, "%4x", &srvid);
|
---|
3089 | entry->srvid = srvid;
|
---|
3090 | }
|
---|
3091 | cs_log_dbg(D_DVBAPI, "prio: ret=%d | %c: %04X %06X %04X %04X %04X -> map %04X %06X %04X | prio %d | delay %d",
|
---|
3092 | ret,
|
---|
3093 | entry->type,
|
---|
3094 | entry->caid,
|
---|
3095 | entry->provid,
|
---|
3096 | entry->srvid,
|
---|
3097 | entry->ecmpid,
|
---|
3098 | entry->chid,
|
---|
3099 | entry->mapcaid,
|
---|
3100 | entry->mapprovid,
|
---|
3101 | entry->mapecmpid,
|
---|
3102 | entry->force,
|
---|
3103 | entry->delay);
|
---|
3104 |
|
---|
3105 | if(!dvbapi_priority)
|
---|
3106 | {
|
---|
3107 | dvbapi_priority = entry;
|
---|
3108 | }
|
---|
3109 | else
|
---|
3110 | {
|
---|
3111 | struct s_dvbapi_priority *p;
|
---|
3112 | for(p = dvbapi_priority; p->next != NULL; p = p->next) { ; }
|
---|
3113 | p->next = entry;
|
---|
3114 | }
|
---|
3115 | }
|
---|
3116 | cs_log_dbg(D_DVBAPI, "Read %d entries from %s", count, cs_prio);
|
---|
3117 |
|
---|
3118 | ret = fclose(fp);
|
---|
3119 | if(ret < 0)
|
---|
3120 | {
|
---|
3121 | cs_log("ERROR: Could not close oscam.dvbapi fd (errno=%d %s)", errno, strerror(errno));
|
---|
3122 | }
|
---|
3123 | return;
|
---|
3124 | }
|
---|
3125 |
|
---|
3126 | void dvbapi_resort_ecmpids(int32_t demux_id)
|
---|
3127 | {
|
---|
3128 | int32_t n, cache = 0, matching_done = 0, found = -1, match_reader_count = 0, total_reader = 0;
|
---|
3129 | uint16_t btun_caid = 0;
|
---|
3130 | struct timeb start,end;
|
---|
3131 | cs_ftime(&start);
|
---|
3132 |
|
---|
3133 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3134 | {
|
---|
3135 | demux[demux_id].ECMpids[n].status = 0;
|
---|
3136 | demux[demux_id].ECMpids[n].checked = 0;
|
---|
3137 | demux[demux_id].ECMpids[n].irdeto_curindex = 0xFE;
|
---|
3138 | demux[demux_id].ECMpids[n].irdeto_maxindex = 0;
|
---|
3139 | demux[demux_id].ECMpids[n].irdeto_cycle = 0xFE;
|
---|
3140 | demux[demux_id].ECMpids[n].tries = 0xFE;
|
---|
3141 | demux[demux_id].ECMpids[n].table = 0;
|
---|
3142 | }
|
---|
3143 |
|
---|
3144 | demux[demux_id].max_status = 0;
|
---|
3145 | demux[demux_id].curindex = -1;
|
---|
3146 | demux[demux_id].pidindex = -1;
|
---|
3147 |
|
---|
3148 | struct s_reader *rdr;
|
---|
3149 | int32_t p_order = demux[demux_id].ECMpidcount + 1;
|
---|
3150 | struct s_dvbapi_priority *prio;
|
---|
3151 |
|
---|
3152 | // handle prio order in oscam.dvbapi + ignore all chids
|
---|
3153 | for(rdr = first_active_reader; rdr ; rdr = rdr->next)
|
---|
3154 | {
|
---|
3155 | total_reader++; // only need to calculate once!
|
---|
3156 | }
|
---|
3157 |
|
---|
3158 | ECM_REQUEST *er;
|
---|
3159 | if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
|
---|
3160 | {
|
---|
3161 | return;
|
---|
3162 | }
|
---|
3163 |
|
---|
3164 | for(prio = dvbapi_priority; prio != NULL; prio = prio->next)
|
---|
3165 | {
|
---|
3166 | if(prio->type != 'p' && prio->type != 'i')
|
---|
3167 | {
|
---|
3168 | continue;
|
---|
3169 | }
|
---|
3170 |
|
---|
3171 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3172 | {
|
---|
3173 | if(demux[demux_id].ECMpids[n].status == -1) continue; // skip ignores!
|
---|
3174 |
|
---|
3175 | er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
|
---|
3176 | er->prid = demux[demux_id].ECMpids[n].PROVID;
|
---|
3177 | er->pid = demux[demux_id].ECMpids[n].ECM_PID;
|
---|
3178 | er->srvid = demux[demux_id].program_number;
|
---|
3179 | er->client = cur_client();
|
---|
3180 | btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
|
---|
3181 |
|
---|
3182 | if(prio->type == 'p' && btun_caid)
|
---|
3183 | {
|
---|
3184 | er->caid = btun_caid;
|
---|
3185 | }
|
---|
3186 |
|
---|
3187 | if((prio->caid && (prio->caid != er->caid && prio->caid != er->ocaid))
|
---|
3188 | || (prio->provid && prio->provid != er->prid)
|
---|
3189 | || (prio->srvid && prio->srvid != er->srvid)
|
---|
3190 | || (prio->ecmpid && prio->ecmpid != er->pid)
|
---|
3191 | || (prio->pidx && prio->pidx - 1 != n))
|
---|
3192 | {
|
---|
3193 | continue;
|
---|
3194 | }
|
---|
3195 |
|
---|
3196 | if(prio->type == 'p') // check for prio
|
---|
3197 | {
|
---|
3198 | if(prio->cadata != 0 && prio->cadata != demux[demux_id].ECMpids[n].cadata)
|
---|
3199 | {
|
---|
3200 | continue;
|
---|
3201 | }
|
---|
3202 |
|
---|
3203 | if(prio->chid < 0x10000)
|
---|
3204 | {
|
---|
3205 | demux[demux_id].ECMpids[n].CHID = prio->chid;
|
---|
3206 | }
|
---|
3207 |
|
---|
3208 | if(prio->force)
|
---|
3209 | {
|
---|
3210 | int32_t j;
|
---|
3211 | for(j = 0; j < demux[demux_id].ECMpidcount; j++)
|
---|
3212 | {
|
---|
3213 | demux[demux_id].ECMpids[j].status = -1;
|
---|
3214 | }
|
---|
3215 |
|
---|
3216 | demux[demux_id].ECMpids[n].status = 1;
|
---|
3217 | demux[demux_id].ECMpids[n].checked = 0;
|
---|
3218 | demux[demux_id].max_status = 1;
|
---|
3219 | demux[demux_id].max_emm_filter = maxfilter - 1;
|
---|
3220 |
|
---|
3221 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio forced%s ecmpid %d %04X@%06X:%04X:%04X (file)",
|
---|
3222 | demux_id,
|
---|
3223 | ((prio->caid == er->caid && prio->caid != er->ocaid) ? " betatunneled" : ""),
|
---|
3224 | n,
|
---|
3225 | demux[demux_id].ECMpids[n].CAID,
|
---|
3226 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3227 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3228 | (uint16_t) prio->chid);
|
---|
3229 | NULLFREE(er);
|
---|
3230 | return; // go start descrambling since its forced by user!
|
---|
3231 | }
|
---|
3232 | else
|
---|
3233 | {
|
---|
3234 | if(!demux[demux_id].ECMpids[n].status) // only accept first matching prio from oscam.dvbapi
|
---|
3235 | {
|
---|
3236 | demux[demux_id].ECMpids[n].status = total_reader + p_order--;
|
---|
3237 | matching_done = 1;
|
---|
3238 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio%s ecmpid %d %04X@%06X:%04X:%04X weight: %d (file)",
|
---|
3239 | demux_id,
|
---|
3240 | ((prio->caid == er->caid && prio->caid != er->ocaid) ? " betatunneled" : ""),
|
---|
3241 | n,
|
---|
3242 | demux[demux_id].ECMpids[n].CAID,
|
---|
3243 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3244 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3245 | (uint16_t) prio->chid,
|
---|
3246 | demux[demux_id].ECMpids[n].status);
|
---|
3247 | }
|
---|
3248 | continue; // evaluate next ecmpid
|
---|
3249 | }
|
---|
3250 | }
|
---|
3251 |
|
---|
3252 | // check for ignore all chids
|
---|
3253 | if(prio->type == 'i' && prio->chid == 0x10000 && demux[demux_id].ECMpids[n].status == 0)
|
---|
3254 | {
|
---|
3255 | cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X all chids (file)",
|
---|
3256 | demux_id,
|
---|
3257 | n,
|
---|
3258 | demux[demux_id].ECMpids[n].CAID,
|
---|
3259 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3260 | demux[demux_id].ECMpids[n].ECM_PID);
|
---|
3261 | demux[demux_id].ECMpids[n].status = -1;
|
---|
3262 | continue; // evaluate next ecmpid
|
---|
3263 | }
|
---|
3264 | }
|
---|
3265 | }
|
---|
3266 | p_order = demux[demux_id].ECMpidcount + 1;
|
---|
3267 |
|
---|
3268 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3269 | {
|
---|
3270 | if(demux[demux_id].ECMpids[n].status == -1)
|
---|
3271 | {
|
---|
3272 | continue; // skip ignores!
|
---|
3273 | }
|
---|
3274 |
|
---|
3275 | int32_t nr;
|
---|
3276 | SIDTAB *sidtab;
|
---|
3277 |
|
---|
3278 | er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
|
---|
3279 | er->prid = demux[demux_id].ECMpids[n].PROVID;
|
---|
3280 | er->pid = demux[demux_id].ECMpids[n].ECM_PID;
|
---|
3281 | er->srvid = demux[demux_id].program_number;
|
---|
3282 | er->client = cur_client();
|
---|
3283 |
|
---|
3284 | btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
|
---|
3285 | if(btun_caid)
|
---|
3286 | {
|
---|
3287 | er->caid = btun_caid;
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 | match_reader_count = 0;
|
---|
3291 |
|
---|
3292 | for(rdr = first_active_reader; rdr ; rdr = rdr->next)
|
---|
3293 | {
|
---|
3294 | if(matching_reader(er, rdr))
|
---|
3295 | {
|
---|
3296 | match_reader_count++;
|
---|
3297 | }
|
---|
3298 | }
|
---|
3299 |
|
---|
3300 | if(match_reader_count == 0)
|
---|
3301 | {
|
---|
3302 | cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X:%04X (no matching reader)",
|
---|
3303 | demux_id,
|
---|
3304 | n,
|
---|
3305 | demux[demux_id].ECMpids[n].CAID,
|
---|
3306 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3307 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3308 | demux[demux_id].ECMpids[n].CHID);
|
---|
3309 | demux[demux_id].ECMpids[n].status = -1;
|
---|
3310 | continue; // evaluate next ecmpid
|
---|
3311 | }
|
---|
3312 | else // ecmpids with no matching readers are disabled and matching sidtabbits have now highest status
|
---|
3313 | {
|
---|
3314 | for(nr = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, nr++)
|
---|
3315 | {
|
---|
3316 | if(sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid)
|
---|
3317 | {
|
---|
3318 | if((cfg.dvbapi_sidtabs.no & ((SIDTABBITS)1 << nr)) && (chk_srvid_match(er, sidtab)))
|
---|
3319 | {
|
---|
3320 | demux[demux_id].ECMpids[n].status = -1; //ignore
|
---|
3321 | cs_log_dbg(D_DVBAPI, "Demuxer %d ignore ecmpid %d %04X@%06X:%04X (service %s pos %d)",
|
---|
3322 | demux_id,
|
---|
3323 | n,
|
---|
3324 | demux[demux_id].ECMpids[n].CAID,
|
---|
3325 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3326 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3327 | sidtab->label,
|
---|
3328 | nr);
|
---|
3329 | continue; // evaluate next ecmpid
|
---|
3330 | }
|
---|
3331 | if((cfg.dvbapi_sidtabs.ok & ((SIDTABBITS) 1 << nr)) && (chk_srvid_match(er, sidtab)))
|
---|
3332 | {
|
---|
3333 | demux[demux_id].ECMpids[n].status++; // priority
|
---|
3334 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (service %s pos %d)",
|
---|
3335 | demux_id,
|
---|
3336 | n,
|
---|
3337 | demux[demux_id].ECMpids[n].CAID,
|
---|
3338 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3339 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3340 | demux[demux_id].ECMpids[n].status,
|
---|
3341 | sidtab->label,
|
---|
3342 | nr);
|
---|
3343 | }
|
---|
3344 | }
|
---|
3345 | }
|
---|
3346 | }
|
---|
3347 | }
|
---|
3348 |
|
---|
3349 | // ecmpid with highest prio from oscam.dvbapi has now highest status
|
---|
3350 | // check all ecmpids and get the highest amount cache-ex and local readers
|
---|
3351 | int32_t max_local_matching_reader = 0, max_cacheex_reader = 0;
|
---|
3352 |
|
---|
3353 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3354 | {
|
---|
3355 | int32_t count_matching_cacheex_reader = 0, count_matching_local_reader = 0;
|
---|
3356 | if(demux[demux_id].ECMpids[n].status == -1)
|
---|
3357 | {
|
---|
3358 | continue; // skip ignores!
|
---|
3359 | }
|
---|
3360 |
|
---|
3361 | er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
|
---|
3362 | er->prid = demux[demux_id].ECMpids[n].PROVID;
|
---|
3363 | er->pid = demux[demux_id].ECMpids[n].ECM_PID;
|
---|
3364 | er->srvid = demux[demux_id].program_number;
|
---|
3365 | er->client = cur_client();
|
---|
3366 |
|
---|
3367 | btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
|
---|
3368 | if(btun_caid)
|
---|
3369 | {
|
---|
3370 | er->caid = btun_caid;
|
---|
3371 | }
|
---|
3372 |
|
---|
3373 | for(rdr = first_active_reader; rdr ; rdr = rdr->next)
|
---|
3374 | {
|
---|
3375 | if(matching_reader(er, rdr))
|
---|
3376 | {
|
---|
3377 | if(cacheex_reader(rdr))
|
---|
3378 | {
|
---|
3379 | count_matching_cacheex_reader++;
|
---|
3380 | }
|
---|
3381 | else if(is_localreader(rdr, er))
|
---|
3382 | {
|
---|
3383 | count_matching_local_reader++;
|
---|
3384 | }
|
---|
3385 | }
|
---|
3386 | }
|
---|
3387 |
|
---|
3388 | if(max_local_matching_reader < count_matching_local_reader)
|
---|
3389 | {
|
---|
3390 | max_local_matching_reader = count_matching_local_reader;
|
---|
3391 | }
|
---|
3392 |
|
---|
3393 | if(max_cacheex_reader < count_matching_cacheex_reader)
|
---|
3394 | {
|
---|
3395 | max_cacheex_reader = count_matching_cacheex_reader;
|
---|
3396 | }
|
---|
3397 | }
|
---|
3398 |
|
---|
3399 | if(max_local_matching_reader != 0 || max_cacheex_reader != 0)
|
---|
3400 | {
|
---|
3401 | p_order = demux[demux_id].ECMpidcount * 2;
|
---|
3402 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3403 | {
|
---|
3404 | int32_t count_matching_cacheex_reader = 0, count_matching_local_reader = 0;
|
---|
3405 | int32_t localprio = 1, cacheexprio = 1;
|
---|
3406 |
|
---|
3407 | if(demux[demux_id].ECMpids[n].status == -1)
|
---|
3408 | {
|
---|
3409 | continue; // skip ignores!
|
---|
3410 | }
|
---|
3411 |
|
---|
3412 | if(cfg.preferlocalcards == 2) // ecmpids with local reader get highest prio
|
---|
3413 | {
|
---|
3414 | localprio = max_cacheex_reader + p_order + 1;
|
---|
3415 | }
|
---|
3416 | else if(cfg.preferlocalcards == 1) // ecmpids with cacheex reader get highest prio
|
---|
3417 | {
|
---|
3418 | cacheexprio = max_local_matching_reader + p_order + 1;
|
---|
3419 | }
|
---|
3420 |
|
---|
3421 | er->caid = er->ocaid = demux[demux_id].ECMpids[n].CAID;
|
---|
3422 | er->prid = demux[demux_id].ECMpids[n].PROVID;
|
---|
3423 | er->pid = demux[demux_id].ECMpids[n].ECM_PID;
|
---|
3424 | er->srvid = demux[demux_id].program_number;
|
---|
3425 | er->client = cur_client();
|
---|
3426 |
|
---|
3427 | btun_caid = chk_on_btun(SRVID_MASK, er->client, er);
|
---|
3428 | if(btun_caid)
|
---|
3429 | {
|
---|
3430 | er->caid = btun_caid;
|
---|
3431 | }
|
---|
3432 |
|
---|
3433 | int32_t oldstatus = demux[demux_id].ECMpids[n].status;
|
---|
3434 | int32_t anyreader = 0;
|
---|
3435 |
|
---|
3436 | for(rdr = first_active_reader; rdr; rdr = rdr->next)
|
---|
3437 | {
|
---|
3438 | if(matching_reader(er, rdr))
|
---|
3439 | {
|
---|
3440 | if(cfg.preferlocalcards == 0)
|
---|
3441 | {
|
---|
3442 | if(!matching_done)
|
---|
3443 | {
|
---|
3444 | demux[demux_id].ECMpids[n].status++;
|
---|
3445 | }
|
---|
3446 | anyreader++;
|
---|
3447 | continue;
|
---|
3448 | }
|
---|
3449 |
|
---|
3450 | if(cacheex_reader(rdr))
|
---|
3451 | {
|
---|
3452 | demux[demux_id].ECMpids[n].status += cacheexprio;
|
---|
3453 | count_matching_cacheex_reader++;
|
---|
3454 | cacheexprio = 1;
|
---|
3455 | }
|
---|
3456 |
|
---|
3457 | if(is_localreader(rdr, er))
|
---|
3458 | {
|
---|
3459 | demux[demux_id].ECMpids[n].status += localprio;
|
---|
3460 | count_matching_local_reader++;
|
---|
3461 | localprio = 1;
|
---|
3462 | }
|
---|
3463 | }
|
---|
3464 | }
|
---|
3465 |
|
---|
3466 | if(oldstatus != demux[demux_id].ECMpids[n].status)
|
---|
3467 | {
|
---|
3468 | if(anyreader)
|
---|
3469 | {
|
---|
3470 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X:%04X weight: %d (%d readers)",
|
---|
3471 | demux_id,
|
---|
3472 | n,
|
---|
3473 | demux[demux_id].ECMpids[n].CAID,
|
---|
3474 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3475 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3476 | demux[demux_id].ECMpids[n].CHID,
|
---|
3477 | demux[demux_id].ECMpids[n].status,
|
---|
3478 | anyreader);
|
---|
3479 | }
|
---|
3480 | else
|
---|
3481 | {
|
---|
3482 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X:%04X weight: %d (%d local and %d cacheex readers)",
|
---|
3483 | demux_id,
|
---|
3484 | n,
|
---|
3485 | demux[demux_id].ECMpids[n].CAID,
|
---|
3486 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3487 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3488 | demux[demux_id].ECMpids[n].CHID,
|
---|
3489 | demux[demux_id].ECMpids[n].status,
|
---|
3490 | count_matching_local_reader,
|
---|
3491 | count_matching_cacheex_reader);
|
---|
3492 | }
|
---|
3493 | }
|
---|
3494 | }
|
---|
3495 | }
|
---|
3496 |
|
---|
3497 | struct s_channel_cache *c = NULL;
|
---|
3498 |
|
---|
3499 | for(n = 0; n < demux[demux_id].ECMpidcount && matching_done == 0; n++)
|
---|
3500 | {
|
---|
3501 | if(demux[demux_id].ECMpids[n].status == -1)
|
---|
3502 | {
|
---|
3503 | continue; // skip ignores!
|
---|
3504 | }
|
---|
3505 |
|
---|
3506 | c = dvbapi_find_channel_cache(demux_id, n, 0); // find exact channel match
|
---|
3507 | if(c != NULL)
|
---|
3508 | {
|
---|
3509 | found = n;
|
---|
3510 | cache = 2; // found cache entry with higher priority
|
---|
3511 | demux[demux_id].ECMpids[n].status++; // prioritize CAIDs which already decoded same caid:provid:srvid
|
---|
3512 |
|
---|
3513 | if(c->chid < 0x10000)
|
---|
3514 | {
|
---|
3515 | demux[demux_id].ECMpids[n].CHID = c->chid; // if chid registered in cache -> use it!
|
---|
3516 | }
|
---|
3517 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (found caid/provid/srvid in cache)",
|
---|
3518 | demux_id,
|
---|
3519 | n,
|
---|
3520 | demux[demux_id].ECMpids[n].CAID,
|
---|
3521 | demux[demux_id].ECMpids[n].PROVID,
|
---|
3522 | demux[demux_id].ECMpids[n].ECM_PID,
|
---|
3523 | demux[demux_id].ECMpids[n].status);
|
---|
3524 | break;
|
---|
3525 | }
|
---|
3526 | }
|
---|
3527 |
|
---|
3528 | if(found == -1)
|
---|
3529 | {
|
---|
3530 | // prioritize CAIDs which already decoded same caid:provid
|
---|
3531 | for(n = 0; n < demux[demux_id].ECMpidcount && matching_done == 0; n++)
|
---|
3532 | {
|
---|
3533 | if(demux[demux_id].ECMpids[n].status == -1) continue; // skip ignores!
|
---|
3534 |
|
---|
3535 | c = dvbapi_find_channel_cache(demux_id, n, 1);
|
---|
3536 | if(c != NULL)
|
---|
3537 | {
|
---|
3538 | cache = 1; //found cache entry
|
---|
3539 | demux[demux_id].ECMpids[n].status++;
|
---|
3540 | cs_log_dbg(D_DVBAPI, "Demuxer %d prio ecmpid %d %04X@%06X:%04X weight: %d (found caid/provid in cache)",
|
---|
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].status);
|
---|
3547 | }
|
---|
3548 | }
|
---|
3549 | }
|
---|
3550 |
|
---|
3551 | int32_t max_status = 0;
|
---|
3552 | int32_t highest_priopid = -1;
|
---|
3553 |
|
---|
3554 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3555 | {
|
---|
3556 | if(demux[demux_id].ECMpids[n].status == -1)
|
---|
3557 | {
|
---|
3558 | continue; // skip ignores!
|
---|
3559 | }
|
---|
3560 |
|
---|
3561 | if(demux[demux_id].ECMpids[n].status > max_status) // find highest prio pid
|
---|
3562 | {
|
---|
3563 | max_status = demux[demux_id].ECMpids[n].status;
|
---|
3564 | highest_priopid = n;
|
---|
3565 | }
|
---|
3566 |
|
---|
3567 | if(!USE_OPENXCAS) // openxcas doesnt use prio and non-prio run: all are equal!
|
---|
3568 | {
|
---|
3569 | if(demux[demux_id].ECMpids[n].status == 0)
|
---|
3570 | {
|
---|
3571 | demux[demux_id].ECMpids[n].checked = 2; // set pids with no status to no prio run
|
---|
3572 | }
|
---|
3573 | }
|
---|
3574 | }
|
---|
3575 |
|
---|
3576 | demux[demux_id].max_status = max_status; // register maxstatus
|
---|
3577 |
|
---|
3578 | // Found entry in channelcache that is valid and has exact match on srvid
|
---|
3579 | if(highest_priopid != -1 && found == highest_priopid && cache == 2)
|
---|
3580 | {
|
---|
3581 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3582 | {
|
---|
3583 | if(demux[demux_id].ECMpids[n].status == -1)
|
---|
3584 | {
|
---|
3585 | continue; // skip ignores!
|
---|
3586 | }
|
---|
3587 |
|
---|
3588 | if(n != found)
|
---|
3589 | {
|
---|
3590 | // disable non matching pid
|
---|
3591 | demux[demux_id].ECMpids[n].status = -1;
|
---|
3592 | }
|
---|
3593 | else
|
---|
3594 | {
|
---|
3595 | demux[demux_id].ECMpids[n].status = 1;
|
---|
3596 | }
|
---|
3597 | }
|
---|
3598 | demux[demux_id].max_emm_filter = maxfilter - 1;
|
---|
3599 | demux[demux_id].max_status = 1;
|
---|
3600 | cs_log("Demuxer %d found channel in cache and matching prio -> start descrambling ecmpid %d ", demux_id, found);
|
---|
3601 | }
|
---|
3602 |
|
---|
3603 | NULLFREE(er);
|
---|
3604 | cs_ftime(&end);
|
---|
3605 | int64_t gone = comp_timeb(&end, &start);
|
---|
3606 | cs_log_dbg(D_DVBAPI, "Demuxer %d sorting the ecmpids took %"PRId64" ms", demux_id, gone);
|
---|
3607 | return;
|
---|
3608 | }
|
---|
3609 |
|
---|
3610 | void dvbapi_parse_descriptors(int32_t demux_id, uint32_t info_length, uint8_t *buffer, uint8_t *type)
|
---|
3611 | {
|
---|
3612 | // int32_t ca_pmt_cmd_id = buffer[i + 5];
|
---|
3613 | uint8_t descriptor_tag = buffer[0], descriptor_length = 0;
|
---|
3614 | uint8_t skip_border = cfg.dvbapi_boxtype == BOXTYPE_SAMYGO ? 0x05 : 0x02; // skip input values <0x05 on samygo
|
---|
3615 | uint32_t j, u, k;
|
---|
3616 |
|
---|
3617 | static const char format_identifiers_audio[10][5] =
|
---|
3618 | {
|
---|
3619 | // "HDMV" format identifier is removed
|
---|
3620 | // OSCam does not need to know about Blu-ray
|
---|
3621 | "AC-3", "BSSD", "dmat", "DRA1", "DTS1",
|
---|
3622 | "DTS2", "DTS3", "EAC3", "mlpa", "Opus",
|
---|
3623 | };
|
---|
3624 |
|
---|
3625 | if(info_length < 1)
|
---|
3626 | {
|
---|
3627 | return;
|
---|
3628 | }
|
---|
3629 |
|
---|
3630 | // skip descriptors with tag 0x00 and 0x01,
|
---|
3631 | // or even greater for samygo (not sure why we do this...)
|
---|
3632 | if((descriptor_tag < skip_border) && info_length > 0)
|
---|
3633 | {
|
---|
3634 | buffer++;
|
---|
3635 | info_length--;
|
---|
3636 | }
|
---|
3637 |
|
---|
3638 | for(j = 0; j + 1 < info_length; j += descriptor_length + 2)
|
---|
3639 | {
|
---|
3640 | descriptor_tag = buffer[j];
|
---|
3641 | descriptor_length = buffer[j + 1];
|
---|
3642 |
|
---|
3643 | cs_log_dbg(D_DVBAPI, "Demuxer %d found %s descriptor (tag: %02X length: %02X)",
|
---|
3644 | demux_id, get_descriptor_tag_txt(descriptor_tag), descriptor_tag, descriptor_length);
|
---|
3645 |
|
---|
3646 | switch(descriptor_tag)
|
---|
3647 | {
|
---|
3648 | case 0x05: // registration descriptor
|
---|
3649 | {
|
---|
3650 | for(k = 0; k < 10; k++)
|
---|
3651 | {
|
---|
3652 | if(memcmp(buffer + j + 2, format_identifiers_audio[k], 4) == 0)
|
---|
3653 | {
|
---|
3654 | *type = STREAM_AUDIO;
|
---|
3655 | break;
|
---|
3656 | }
|
---|
3657 | }
|
---|
3658 | break;
|
---|
3659 | }
|
---|
3660 |
|
---|
3661 | case 0x09: // CA descriptor
|
---|
3662 | {
|
---|
3663 | int32_t descriptor_ca_system_id = b2i(2, buffer + j + 2);
|
---|
3664 | int32_t descriptor_ca_pid = b2i(2, buffer + j + 4) & 0x1FFF;
|
---|
3665 | int32_t descriptor_ca_provider = 0;
|
---|
3666 | uint32_t descriptor_ca_data = 0;
|
---|
3667 | char txt[40]; // room for PBM: 8 byte pbm and DATE: date
|
---|
3668 | memset(txt, 0x00, sizeof(txt));
|
---|
3669 |
|
---|
3670 | if(descriptor_ca_system_id >> 8 == 0x01)
|
---|
3671 | {
|
---|
3672 | for(u = 2; u < descriptor_length; u += 15)
|
---|
3673 | {
|
---|
3674 | descriptor_ca_pid = b2i(2, buffer + j + u + 2) & 0x1FFF;
|
---|
3675 | descriptor_ca_provider = b2i(2, buffer + j + u + 4);
|
---|
3676 | int8_t year = buffer[j + u + 15] >> 1;
|
---|
3677 | int8_t month = (((buffer[j + u + 15] & 0x01) << 3) | (buffer[j + u + 16] >> 5));
|
---|
3678 | int8_t day = buffer[j + u + 16] & 0x1F;
|
---|
3679 | snprintf(txt, sizeof(txt), "PBM: ");
|
---|
3680 | cs_hexdump(0, buffer + j + u + 7, 8, txt + 5, (2 * 8) + 1); // hexdump 8 byte pbm
|
---|
3681 | snprintf(txt + 20, sizeof(txt) - 20, " DATE: %d-%d-%d", day, month, year + 1990);
|
---|
3682 | dvbapi_add_ecmpid(demux_id, descriptor_ca_system_id, descriptor_ca_pid, descriptor_ca_provider, 0, txt);
|
---|
3683 | }
|
---|
3684 | }
|
---|
3685 | else
|
---|
3686 | {
|
---|
3687 | if(caid_is_viaccess(descriptor_ca_system_id) && descriptor_length == 0x0F && buffer[j + 12] == 0x14)
|
---|
3688 | {
|
---|
3689 | descriptor_ca_provider = b2i(3, buffer + j + 14) & 0xFFFFF0;
|
---|
3690 | }
|
---|
3691 | else if(caid_is_nagra(descriptor_ca_system_id) && descriptor_length == 0x07)
|
---|
3692 | {
|
---|
3693 | descriptor_ca_provider = b2i(2, buffer + j + 7);
|
---|
3694 | }
|
---|
3695 | else if((descriptor_ca_system_id >> 8 == 0x4A || descriptor_ca_system_id == 0x2710) && descriptor_length > 0x04)
|
---|
3696 | {
|
---|
3697 | descriptor_ca_provider = buffer[j + 6];
|
---|
3698 | if(caid_is_dre(descriptor_ca_system_id) && descriptor_length == 0xA)
|
---|
3699 | {
|
---|
3700 | descriptor_ca_data = b2i(4, buffer + j + 8);
|
---|
3701 | snprintf(txt, 40, "CA DATA: %X", descriptor_ca_data);
|
---|
3702 | }
|
---|
3703 | }
|
---|
3704 | dvbapi_add_ecmpid(demux_id, descriptor_ca_system_id, descriptor_ca_pid, descriptor_ca_provider, descriptor_ca_data, txt);
|
---|
3705 | }
|
---|
3706 | break;
|
---|
3707 | }
|
---|
3708 |
|
---|
3709 | case 0x59: // subtitling descriptor (DVB)
|
---|
3710 | {
|
---|
3711 | *type = STREAM_SUBTITLE;
|
---|
3712 | break;
|
---|
3713 | }
|
---|
3714 |
|
---|
3715 | case 0x6A: // AC-3 descriptor (DVB)
|
---|
3716 | case 0x7A: // enhanced AC-3 descriptor (DVB)
|
---|
3717 | case 0x7B: // DTS descriptor (DVB)
|
---|
3718 | case 0x7C: // AAC descriptor (DVB)
|
---|
3719 | {
|
---|
3720 | *type = STREAM_AUDIO;
|
---|
3721 | break;
|
---|
3722 | }
|
---|
3723 |
|
---|
3724 | case 0x7F: // extension descriptor (DVB)
|
---|
3725 | {
|
---|
3726 | uint8_t extension_descriptor_tag = buffer[j + 2];
|
---|
3727 |
|
---|
3728 | cs_log_dbg(D_DVBAPI, "Demuxer %d found %s descriptor (extension tag: %02X)",
|
---|
3729 | demux_id, get_extension_descriptor_txt(extension_descriptor_tag), extension_descriptor_tag);
|
---|
3730 |
|
---|
3731 | switch(extension_descriptor_tag)
|
---|
3732 | {
|
---|
3733 | case 0x0E: // DTS-HD descriptor (DVB)
|
---|
3734 | case 0x0F: // DTS Neural descriptor (DVB)
|
---|
3735 | case 0x15: // AC-4 descriptor (DVB)
|
---|
3736 | *type = STREAM_AUDIO;
|
---|
3737 | break;
|
---|
3738 |
|
---|
3739 | default:
|
---|
3740 | *type = STREAM_UNDEFINED;
|
---|
3741 | break;
|
---|
3742 | }
|
---|
3743 | break;
|
---|
3744 | }
|
---|
3745 |
|
---|
3746 | case 0x81:
|
---|
3747 | {
|
---|
3748 | if(descriptor_length == 8) // private descriptor of length 8, assume enigma/tvh
|
---|
3749 | {
|
---|
3750 | demux[demux_id].ens = b2i(4, buffer + j + 2);
|
---|
3751 | demux[demux_id].tsid = b2i(2, buffer + j + 6);
|
---|
3752 | demux[demux_id].onid = b2i(2, buffer + j + 8);
|
---|
3753 | cs_log_dbg(D_DVBAPI, "Demuxer %d assuming enigma private descriptor (namespace: %08X tsid: %04X onid: %04X)",
|
---|
3754 | demux_id, demux[demux_id].ens, demux[demux_id].tsid, demux[demux_id].onid);
|
---|
3755 | }
|
---|
3756 | else if(descriptor_length != 0) // AC-3 descriptor (ATSC)
|
---|
3757 | {
|
---|
3758 | *type = STREAM_AUDIO;
|
---|
3759 | cs_log_dbg(D_DVBAPI, "Demuxer %d assuming AC-3 descriptor (ATSC)", demux_id);
|
---|
3760 | }
|
---|
3761 | break;
|
---|
3762 | }
|
---|
3763 |
|
---|
3764 | default:
|
---|
3765 | break;
|
---|
3766 | }
|
---|
3767 | }
|
---|
3768 |
|
---|
3769 | // Apply mapping:
|
---|
3770 | if(dvbapi_priority)
|
---|
3771 | {
|
---|
3772 | struct s_dvbapi_priority *mapentry;
|
---|
3773 | for(j = 0; (int32_t)j < demux[demux_id].ECMpidcount; j++)
|
---|
3774 | {
|
---|
3775 | mapentry = dvbapi_check_prio_match(demux_id, j, 'm');
|
---|
3776 | if(mapentry)
|
---|
3777 | {
|
---|
3778 | cs_log_dbg(D_DVBAPI, "Demuxer %d mapping ecmpid %d from %04X@%06X to %04X@%06X",
|
---|
3779 | demux_id,
|
---|
3780 | j,
|
---|
3781 | demux[demux_id].ECMpids[j].CAID,
|
---|
3782 | demux[demux_id].ECMpids[j].PROVID,
|
---|
3783 | mapentry->mapcaid, mapentry->mapprovid);
|
---|
3784 |
|
---|
3785 | demux[demux_id].ECMpids[j].CAID = mapentry->mapcaid;
|
---|
3786 | demux[demux_id].ECMpids[j].PROVID = mapentry->mapprovid;
|
---|
3787 | }
|
---|
3788 | }
|
---|
3789 | }
|
---|
3790 | }
|
---|
3791 |
|
---|
3792 | void request_cw(struct s_client *client, ECM_REQUEST *er, int32_t demux_id, uint8_t delayed_ecm_check)
|
---|
3793 | {
|
---|
3794 | if(!er) { return; }
|
---|
3795 |
|
---|
3796 | int32_t filternum = dvbapi_set_section_filter(demux_id, er, -1); // set ecm filter to odd -> even and visaversa
|
---|
3797 | if(filternum < 0)
|
---|
3798 | {
|
---|
3799 | cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting cw -> ecm filter was killed!", demux_id);
|
---|
3800 | NULLFREE(er);
|
---|
3801 | return;
|
---|
3802 | }
|
---|
3803 |
|
---|
3804 | if(!delayed_ecm_check) // no delayed ecm check for this filter
|
---|
3805 | {
|
---|
3806 | memset(demux[demux_id].demux_fd[filternum].lastecmd5, 0, CS_ECMSTORESIZE); // no ecm delay check: zero it!
|
---|
3807 | }
|
---|
3808 | else
|
---|
3809 | {
|
---|
3810 | uint8_t md5tmp[MD5_DIGEST_LENGTH];
|
---|
3811 | MD5(er->ecm, er->ecmlen, md5tmp);
|
---|
3812 |
|
---|
3813 | if(!memcmp(demux[demux_id].demux_fd[filternum].prevecmd5, md5tmp, CS_ECMSTORESIZE))
|
---|
3814 | {
|
---|
3815 | if(demux[demux_id].demux_fd[filternum].prevresult < E_NOTFOUND)
|
---|
3816 | {
|
---|
3817 | cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting same ecm again! -> SKIP!", demux_id);
|
---|
3818 | NULLFREE(er);
|
---|
3819 | return;
|
---|
3820 | }
|
---|
3821 | else
|
---|
3822 | {
|
---|
3823 | cs_log_dbg(D_DVBAPI, "Demuxer %d requesting same ecm again (previous result was not found!)", demux_id);
|
---|
3824 | }
|
---|
3825 | }
|
---|
3826 | else if(!memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE))
|
---|
3827 | {
|
---|
3828 | if(demux[demux_id].demux_fd[filternum].lastresult < E_NOTFOUND)
|
---|
3829 | {
|
---|
3830 | cs_log_dbg(D_DVBAPI, "Demuxer %d not requesting same ecm again! -> SKIP!", demux_id);
|
---|
3831 | NULLFREE(er);
|
---|
3832 | return;
|
---|
3833 | }
|
---|
3834 | else
|
---|
3835 | {
|
---|
3836 | cs_log_dbg(D_DVBAPI, "Demuxer %d requesting same ecm again (previous result was not found!)", demux_id);
|
---|
3837 | }
|
---|
3838 | }
|
---|
3839 |
|
---|
3840 | memcpy(demux[demux_id].demux_fd[filternum].prevecmd5, demux[demux_id].demux_fd[filternum].lastecmd5, CS_ECMSTORESIZE);
|
---|
3841 | demux[demux_id].demux_fd[filternum].prevresult = demux[demux_id].demux_fd[filternum].lastresult;
|
---|
3842 | memcpy(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE);
|
---|
3843 | demux[demux_id].demux_fd[filternum].lastresult = 0xFF;
|
---|
3844 | }
|
---|
3845 |
|
---|
3846 | er->adapter_index = demux[demux_id].adapter_index;
|
---|
3847 | cs_log_dbg(D_DVBAPI, "Demuxer %d get controlword!", demux_id);
|
---|
3848 | get_cw(client, er);
|
---|
3849 |
|
---|
3850 | #ifdef WITH_DEBUG
|
---|
3851 | char buf[ECM_FMT_LEN];
|
---|
3852 | format_ecm(er, buf, ECM_FMT_LEN);
|
---|
3853 | cs_log_dbg(D_DVBAPI, "Demuxer %d request controlword for ecm %s", demux_id, buf);
|
---|
3854 | #endif
|
---|
3855 | }
|
---|
3856 |
|
---|
3857 | void dvbapi_try_next_caid(int32_t demux_id, int8_t checked, uint32_t msgid)
|
---|
3858 | {
|
---|
3859 | int32_t n, j, found = -1, started = 0;
|
---|
3860 | int32_t status = demux[demux_id].max_status;
|
---|
3861 |
|
---|
3862 | for(j = status; j >= 0; j--) // largest status first!
|
---|
3863 | {
|
---|
3864 | for(n = 0; n < demux[demux_id].ECMpidcount; n++)
|
---|
3865 | {
|
---|
3866 | //cs_log_dbg(D_DVBAPI,"Demuxer %d PID %d checked = %d status = %d (searching for pid with status = %d)",
|
---|
3867 | // demux_id, n, demux[demux_id].ECMpids[n].checked, demux[demux_id].ECMpids[n].status, j);
|
---|
3868 |
|
---|
3869 | if(demux[demux_id].ECMpids[n].checked == checked && demux[demux_id].ECMpids[n].status == j)
|
---|
3870 | {
|
---|
3871 | found = n;
|
---|
3872 | openxcas_set_provid(demux[demux_id].ECMpids[found].PROVID);
|
---|
3873 | openxcas_set_caid(demux[demux_id].ECMpids[found].CAID);
|
---|
3874 | openxcas_set_ecm_pid(demux[demux_id].ECMpids[found].ECM_PID);
|
---|
3875 |
|
---|
3876 | // fixup for cas that need emm first!
|
---|
3877 | if(caid_is_irdeto(demux[demux_id].ECMpids[found].CAID) || (caid_is_dre(demux[demux_id].ECMpids[found].CAID)
|
---|
3878 | && ((demux[demux_id].ECMpids[found].PROVID == 0x11 || demux[demux_id].ECMpids[found].PROVID == 0xFE))))
|
---|
3879 | {
|
---|
3880 | demux[demux_id].emmstart.time = 0;
|
---|
3881 | }
|
---|
3882 |
|
---|
3883 | started = dvbapi_start_descrambling(demux_id, found, checked, msgid);
|
---|
3884 | if(cfg.dvbapi_requestmode == 0 && started == 1)
|
---|
3885 | {
|
---|
3886 | return; // in requestmode 0 we only start 1 ecm request at the time
|
---|
3887 | }
|
---|
3888 | }
|
---|
3889 | }
|
---|
3890 | }
|
---|
3891 |
|
---|
3892 | if(found == -1 && demux[demux_id].pidindex == -1)
|
---|
3893 | {
|
---|
3894 | cs_log("Demuxer %d no suitable readers found that can be used for decoding!", demux_id);
|
---|
3895 | return;
|
---|
3896 | }
|
---|
3897 | }
|
---|
3898 |
|
---|
3899 | static void get_demux_options(int32_t demux_id, uint8_t *buffer, uint32_t *ca_mask,
|
---|
3900 | uint16_t *demux_index, uint16_t *adapter_index, uint16_t *pmtpid)
|
---|
3901 | {
|
---|
3902 | *ca_mask = 0x01, *demux_index = 0x00, *adapter_index = 0x00, *pmtpid = 0x00;
|
---|
3903 |
|
---|
3904 | if(buffer[17] == 0x82 && buffer[18] == 0x02)
|
---|
3905 | {
|
---|
3906 | // enigma2
|
---|
3907 | *ca_mask = buffer[19];
|
---|
3908 | uint32_t demuxid = buffer[20];
|
---|
3909 |
|
---|
3910 | if(demuxid == 0xff)
|
---|
3911 | {
|
---|
3912 | demuxid = 0; // tryfix prismcube (0xff -> "demux-1" = error!)
|
---|
3913 | }
|
---|
3914 | *demux_index = demuxid;
|
---|
3915 |
|
---|
3916 | if(buffer[21] == 0x84 && buffer[22] == 0x02)
|
---|
3917 | {
|
---|
3918 | *pmtpid = b2i(2, buffer + 23);
|
---|
3919 | }
|
---|
3920 |
|
---|
3921 | if(buffer[25] == 0x83 && buffer[26] == 0x01)
|
---|
3922 | {
|
---|
3923 | *adapter_index = buffer[27]; // from code cahandler.cpp 0x83 index of adapter
|
---|
3924 | }
|
---|
3925 | }
|
---|
3926 |
|
---|
3927 | if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
|
---|
3928 | {
|
---|
3929 | *ca_mask = demux_id + 1;
|
---|
3930 | *demux_index = demux_id;
|
---|
3931 | }
|
---|
3932 |
|
---|
3933 | if(cfg.dvbapi_boxtype == BOXTYPE_QBOXHD && buffer[17] == 0x82 && buffer[18] == 0x03)
|
---|
3934 | {
|
---|
3935 | // ca_mask = buffer[19]; // with STONE 1.0.4 always 0x01
|
---|
3936 | *demux_index = buffer[20]; // with STONE 1.0.4 always 0x00
|
---|
3937 | *adapter_index = buffer[21]; // with STONE 1.0.4 adapter index can be 0,1,2
|
---|
3938 | *ca_mask = (1 << *adapter_index); // use adapter_index as ca_mask (used as index for ca_fd[] array)
|
---|
3939 |
|
---|
3940 | if(buffer[21] == 0x84 && buffer[22] == 0x02)
|
---|
3941 | {
|
---|
3942 | *pmtpid = b2i(2, buffer + 23);
|
---|
3943 | }
|
---|
3944 | }
|
---|
3945 |
|
---|
3946 | if((cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
|
---|
3947 | && buffer[7] == 0x82 && buffer[8] == 0x02)
|
---|
3948 | {
|
---|
3949 | *demux_index = buffer[9]; // it is always 0 but you never know
|
---|
3950 | *adapter_index = buffer[10]; // adapter index can be 0,1,2
|
---|
3951 | *ca_mask = (1 << *adapter_index); // use adapter_index as ca_mask (used as index for ca_fd[] array)
|
---|
3952 |
|
---|
3953 | if(buffer[21] == 0x84 && buffer[22] == 0x02)
|
---|
3954 | {
|
---|
3955 | *pmtpid = b2i(2, buffer + 23);
|
---|
3956 | }
|
---|
3957 | }
|
---|
3958 | }
|
---|
3959 |
|
---|
3960 | static void dvbapi_capmt_notify(struct demux_s *dmx)
|
---|
3961 | {
|
---|
3962 | struct s_client *cl;
|
---|
3963 | for(cl = first_client->next; cl ; cl = cl->next)
|
---|
3964 | {
|
---|
3965 | if((cl->typ == 'p' || cl->typ == 'r') && cl->reader && cl->reader->ph.c_capmt)
|
---|
3966 | {
|
---|
3967 | struct demux_s *curdemux;
|
---|
3968 | if(cs_malloc(&curdemux, sizeof(struct demux_s)))
|
---|
3969 | {
|
---|
3970 | memcpy(curdemux, dmx, sizeof(struct demux_s));
|
---|
3971 | add_job(cl, ACTION_READER_CAPMT_NOTIFY, curdemux, sizeof(struct demux_s));
|
---|
3972 | }
|
---|
3973 | }
|
---|
3974 | }
|
---|
3975 | }
|
---|
3976 |
|
---|
3977 | int32_t dvbapi_parse_capmt(uint8_t *buffer, uint32_t length, int32_t connfd, char *pmtfile, int8_t is_real_pmt,
|
---|
3978 | uint16_t existing_demux_id, uint16_t client_proto_version, uint32_t msgid)
|
---|
3979 | {
|
---|
3980 | uint32_t i = 0, start_descrambling = 0;
|
---|
3981 | int32_t j = 0;
|
---|
3982 | int32_t demux_id = -1;
|
---|
3983 | uint16_t demux_index, adapter_index, pmtpid;
|
---|
3984 | uint32_t ca_mask;
|
---|
3985 | uint32_t program_number, program_info_length;
|
---|
3986 | uint8_t program_info_start = is_real_pmt ? 12 : 6;
|
---|
3987 |
|
---|
3988 | if(!is_real_pmt)
|
---|
3989 | {
|
---|
3990 | #define LIST_MORE 0x00 // *CA application should append a 'MORE' CAPMT object to the list and start receiving the next object
|
---|
3991 | #define LIST_FIRST 0x01 // *CA application should clear the list when a 'FIRST' CAPMT object is received, and start receiving the next object
|
---|
3992 | #define LIST_LAST 0x02 // *CA application should append a 'LAST' CAPMT object to the list and start working with the list
|
---|
3993 | #define LIST_ONLY 0x03 // *CA application should clear the list when an 'ONLY' CAPMT object is received, and start working with the object
|
---|
3994 | #define LIST_ADD 0x04 // *CA application should append an 'ADD' CAPMT object to the current list and start working with the updated list
|
---|
3995 | #define LIST_UPDATE 0x05 // *CA application should replace an entry in the list with an 'UPDATE' CAPMT object, and start working with the updated list
|
---|
3996 |
|
---|
3997 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
3998 | int32_t ca_pmt_list_management = LIST_ONLY;
|
---|
3999 | #else
|
---|
4000 | int32_t ca_pmt_list_management = buffer[0];
|
---|
4001 | #endif
|
---|
4002 | program_number = b2i(2, buffer + 1);
|
---|
4003 | program_info_length = b2i(2, buffer + 4) &0xFFF;
|
---|
4004 | cs_log_dump_dbg(D_DVBAPI, buffer, length, "capmt:");
|
---|
4005 | cs_log_dbg(D_DVBAPI, "Receiver sends PMT command %d for channel %04X", ca_pmt_list_management, program_number);
|
---|
4006 |
|
---|
4007 | if(!pmt_stopmarking && (ca_pmt_list_management == LIST_FIRST || ca_pmt_list_management == LIST_ONLY))
|
---|
4008 | {
|
---|
4009 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
4010 | {
|
---|
4011 | // skip empty demuxers, skip demuxers belonging to other ca pmt connection, skip demuxers handled by other pmt files
|
---|
4012 | if((demux[i].program_number == 0) || (demux[i].socket_fd != connfd) || ((demux[i].socket_fd == -1)
|
---|
4013 | && (pmtfile && strcmp(demux[i].pmt_file, pmtfile) != 0)))
|
---|
4014 | {
|
---|
4015 | continue;
|
---|
4016 | }
|
---|
4017 |
|
---|
4018 | demux[i].stopdescramble = 1; // Mark for deletion if not used again by following pmt objects.
|
---|
4019 | cs_log_dbg(D_DVBAPI, "Marked demuxer %d/%d (srvid = %04X fd = %d) to stop decoding",
|
---|
4020 | i, MAX_DEMUX, demux[i].program_number, connfd);
|
---|
4021 | }
|
---|
4022 | pmt_stopmarking = 1; // only stop demuxing for first pmt record
|
---|
4023 | }
|
---|
4024 |
|
---|
4025 | get_demux_options(i, buffer, &ca_mask, &demux_index, &adapter_index, &pmtpid);
|
---|
4026 | cs_log_dbg(D_DVBAPI,"Receiver wants to demux srvid %04X on adapter %04X camask %04X index %04X pmtpid %04X",
|
---|
4027 | program_number, adapter_index, ca_mask, demux_index, pmtpid);
|
---|
4028 |
|
---|
4029 | for(i = 0; i < MAX_DEMUX; i++) // search current demuxers for running the same program as the one we received in this PMT object
|
---|
4030 | {
|
---|
4031 | if(demux[i].program_number == 0)
|
---|
4032 | {
|
---|
4033 | continue;
|
---|
4034 | }
|
---|
4035 |
|
---|
4036 | if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)
|
---|
4037 | {
|
---|
4038 | demux_index = i; // fixup for ipbox
|
---|
4039 | }
|
---|
4040 |
|
---|
4041 | bool full_check = 1, matched = 0;
|
---|
4042 | if(config_enabled(WITH_COOLAPI) || config_enabled(WITH_COOLAPI2) || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
|
---|
4043 | {
|
---|
4044 | full_check = 0;
|
---|
4045 | }
|
---|
4046 |
|
---|
4047 | if(full_check)
|
---|
4048 | {
|
---|
4049 | matched = (connfd > 0 && demux[i].socket_fd == connfd) && demux[i].program_number == program_number;
|
---|
4050 | }
|
---|
4051 | else
|
---|
4052 | {
|
---|
4053 | matched = connfd > 0 && demux[i].program_number == program_number;
|
---|
4054 | }
|
---|
4055 |
|
---|
4056 | if(matched)
|
---|
4057 | {
|
---|
4058 | if(full_check)
|
---|
4059 | {
|
---|
4060 | // perhaps next demuxer matches?
|
---|
4061 | if((demux[i].adapter_index != adapter_index) || (demux[i].ca_mask != ca_mask) || (demux[i].demux_index != demux_index))
|
---|
4062 | {
|
---|
4063 | continue;
|
---|
4064 | }
|
---|
4065 | }
|
---|
4066 |
|
---|
4067 | if(ca_pmt_list_management == LIST_UPDATE)
|
---|
4068 | {
|
---|
4069 | cs_log("Demuxer %d PMT update for decoding of SRVID %04X! ", i, program_number);
|
---|
4070 | }
|
---|
4071 |
|
---|
4072 | demux_id = i;
|
---|
4073 | cs_log("Demuxer %d continue decoding of SRVID %04X", i, demux[i].program_number);
|
---|
4074 | openxcas_set_sid(program_number);
|
---|
4075 | demux[i].stopdescramble = 0; // dont stop current demuxer!
|
---|
4076 | break; // no need to explore other demuxers since we have a found!
|
---|
4077 | }
|
---|
4078 | }
|
---|
4079 |
|
---|
4080 | // start using the new list
|
---|
4081 | if(ca_pmt_list_management != LIST_FIRST && ca_pmt_list_management != LIST_MORE)
|
---|
4082 | {
|
---|
4083 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
4084 | {
|
---|
4085 | if(demux[j].program_number == 0)
|
---|
4086 | {
|
---|
4087 | continue;
|
---|
4088 | }
|
---|
4089 |
|
---|
4090 | if(demux[j].stopdescramble == 1)
|
---|
4091 | {
|
---|
4092 | dvbapi_stop_descrambling(j, msgid); // Stop descrambling and remove all demuxer entries not in new PMT.
|
---|
4093 | }
|
---|
4094 | }
|
---|
4095 | start_descrambling = 1; // flag that demuxer descrambling is to be executed!
|
---|
4096 | pmt_stopmarking = 0; // flag that demuxers may be marked for stop decoding again
|
---|
4097 | }
|
---|
4098 |
|
---|
4099 | if(demux_id == -1)
|
---|
4100 | {
|
---|
4101 | for(demux_id = 0; demux_id < MAX_DEMUX; demux_id++)
|
---|
4102 | {
|
---|
4103 | if(demux[demux_id].program_number == 0)
|
---|
4104 | {
|
---|
4105 | demux[demux_id].program_number = program_number; // do this early since some prio items use them!
|
---|
4106 | demux[demux_id].ens = 0;
|
---|
4107 | demux[demux_id].tsid = 0;
|
---|
4108 | demux[demux_id].onid = 0;
|
---|
4109 | demux[demux_id].pmtpid = pmtpid;
|
---|
4110 | demux[demux_id].socket_fd = connfd;
|
---|
4111 | demux[demux_id].adapter_index = adapter_index;
|
---|
4112 | demux[demux_id].client_proto_version = client_proto_version;
|
---|
4113 | demux[demux_id].sdt_filter = -1;
|
---|
4114 |
|
---|
4115 | if(demux_id < 1)
|
---|
4116 | {
|
---|
4117 | demux[demux_id].demux_index = demux_id; // set demuxer stop point if PMT demux_index == 0
|
---|
4118 | }
|
---|
4119 |
|
---|
4120 | if(pmtfile)
|
---|
4121 | {
|
---|
4122 | cs_strncpy(demux[demux_id].pmt_file, pmtfile, sizeof(demux[demux_id].pmt_file));
|
---|
4123 | }
|
---|
4124 |
|
---|
4125 | // free demuxer found, start pat/pmt filter for this new demuxer
|
---|
4126 | if(pmtpid)
|
---|
4127 | {
|
---|
4128 | dvbapi_start_pmt_filter(demux_id, pmtpid);
|
---|
4129 | }
|
---|
4130 | else
|
---|
4131 | {
|
---|
4132 | dvbapi_start_pat_filter(demux_id);
|
---|
4133 | }
|
---|
4134 | break;
|
---|
4135 | }
|
---|
4136 | }
|
---|
4137 | }
|
---|
4138 |
|
---|
4139 | if(demux_id >= MAX_DEMUX)
|
---|
4140 | {
|
---|
4141 | cs_log("ERROR: No free id (MAX_DEMUX)");
|
---|
4142 | return -1;
|
---|
4143 | }
|
---|
4144 |
|
---|
4145 | if(demux[demux_id].running == 0)
|
---|
4146 | {
|
---|
4147 | demux[demux_id].ECMpidcount = 0; // reset number of ecmpids only if it was not running!
|
---|
4148 | }
|
---|
4149 | }
|
---|
4150 | else // is_real_pmt
|
---|
4151 | {
|
---|
4152 | demux_id = existing_demux_id;
|
---|
4153 | dvbapi_stop_filter(demux_id, TYPE_PMT, msgid);
|
---|
4154 | program_number = b2i(2, buffer + 3);
|
---|
4155 | program_info_length = b2i(2, buffer + 10) & 0xFFF;
|
---|
4156 | cs_log_dump_dbg(D_DVBAPI, buffer, length, "pmt:");
|
---|
4157 | pmtpid = demux[demux_id].pmtpid;
|
---|
4158 | }
|
---|
4159 |
|
---|
4160 | for(j = 0; j < demux[demux_id].ECMpidcount; j++) // cleanout demuxer from possible stale info
|
---|
4161 | {
|
---|
4162 | demux[demux_id].ECMpids[j].streams = 0; // reset streams of each ecmpid!
|
---|
4163 | }
|
---|
4164 | demux[demux_id].STREAMpidcount = 0; // reset number of streams
|
---|
4165 |
|
---|
4166 | if(program_info_length > 1 && program_info_length < length) // parse program descriptors
|
---|
4167 | {
|
---|
4168 | dvbapi_parse_descriptors(demux_id, program_info_length, buffer + program_info_start, NULL);
|
---|
4169 | }
|
---|
4170 | uint32_t es_info_length = 0, vpid = 0;
|
---|
4171 | struct s_dvbapi_priority *addentry;
|
---|
4172 |
|
---|
4173 | for(i = program_info_length + program_info_start; i + 4 < length; i += es_info_length + 5)
|
---|
4174 | {
|
---|
4175 | uint8_t stream_type = buffer[i], type = STREAM_AUDIO; // default to audio - quick fix for missing audio when recording
|
---|
4176 | uint16_t elementary_pid = b2i(2, buffer + i + 1) & 0x1FFF;
|
---|
4177 | es_info_length = b2i(2, buffer + i + 3) & 0x0FFF;
|
---|
4178 |
|
---|
4179 | if(demux[demux_id].STREAMpidcount < MAX_STREAM_PIDS)
|
---|
4180 | {
|
---|
4181 | cs_log_dbg(D_DVBAPI,"Demuxer %d found %s stream (type: %02X pid: %04X)",
|
---|
4182 | demux_id, get_stream_type_txt(stream_type), stream_type, elementary_pid);
|
---|
4183 |
|
---|
4184 | if(es_info_length != 0 && es_info_length < length) // parse program element descriptors
|
---|
4185 | {
|
---|
4186 | dvbapi_parse_descriptors(demux_id, es_info_length, buffer + i + 5, &type);
|
---|
4187 | }
|
---|
4188 | else
|
---|
4189 | {
|
---|
4190 | for(addentry = dvbapi_priority; addentry != NULL; addentry = addentry->next)
|
---|
4191 | {
|
---|
4192 | if(addentry->type != 'a'
|
---|
4193 | || (addentry->ecmpid && pmtpid && addentry->ecmpid != pmtpid) // ecmpid is misused to hold pmtpid in case of A: rule
|
---|
4194 | || (addentry->ecmpid && !pmtpid && addentry->ecmpid != vpid) // some receivers don't forward pmtpid, use vpid instead
|
---|
4195 | || (addentry->srvid != demux[demux_id].program_number))
|
---|
4196 | {
|
---|
4197 | continue;
|
---|
4198 | }
|
---|
4199 |
|
---|
4200 | cs_log_dbg(D_DVBAPI, "Demuxer %d fake ecmpid %04X@%06X:%04x for unencrypted stream on srvid %04X",
|
---|
4201 | demux_id,
|
---|
4202 | addentry->mapcaid,
|
---|
4203 | addentry->mapprovid,
|
---|
4204 | addentry->mapecmpid,
|
---|
4205 | demux[demux_id].program_number);
|
---|
4206 | dvbapi_add_ecmpid(demux_id, addentry->mapcaid, addentry->mapecmpid, addentry->mapprovid, 0, " (fake ecmpid)");
|
---|
4207 | break;
|
---|
4208 | }
|
---|
4209 | }
|
---|
4210 |
|
---|
4211 | switch(stream_type)
|
---|
4212 | {
|
---|
4213 | case 0x01:
|
---|
4214 | case 0x02:
|
---|
4215 | case 0x10:
|
---|
4216 | case 0x1B:
|
---|
4217 | case 0x20:
|
---|
4218 | case 0x24:
|
---|
4219 | case 0x25:
|
---|
4220 | case 0x42:
|
---|
4221 | case 0xD1:
|
---|
4222 | case 0xEA:
|
---|
4223 | if(!vpid)
|
---|
4224 | {
|
---|
4225 | vpid = elementary_pid; // register videopid
|
---|
4226 | }
|
---|
4227 | demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_VIDEO;
|
---|
4228 | break;
|
---|
4229 |
|
---|
4230 | case 0x03:
|
---|
4231 | case 0x04:
|
---|
4232 | case 0x0F:
|
---|
4233 | case 0x11:
|
---|
4234 | case 0x1C:
|
---|
4235 | case 0x2D:
|
---|
4236 | case 0x2E:
|
---|
4237 | demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_AUDIO;
|
---|
4238 | break;
|
---|
4239 |
|
---|
4240 | case 0x06:
|
---|
4241 | case 0x81:
|
---|
4242 | case 0x87:
|
---|
4243 | // Set the type based on the descriptors for these streams
|
---|
4244 | demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = type;
|
---|
4245 | break;
|
---|
4246 |
|
---|
4247 | default:
|
---|
4248 | demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = STREAM_UNDEFINED;
|
---|
4249 | break;
|
---|
4250 | }
|
---|
4251 | demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount] = elementary_pid;
|
---|
4252 | demux[demux_id].STREAMpidcount++;
|
---|
4253 | }
|
---|
4254 | }
|
---|
4255 |
|
---|
4256 | if(!is_real_pmt)
|
---|
4257 | {
|
---|
4258 | cs_log("Demuxer %d found %d ECMpids and %d STREAMpids in caPMT",
|
---|
4259 | demux_id, demux[demux_id].ECMpidcount, demux[demux_id].STREAMpidcount);
|
---|
4260 |
|
---|
4261 | get_demux_options(demux_id, buffer, &ca_mask, &demux_index, &adapter_index, &pmtpid);
|
---|
4262 |
|
---|
4263 | demux[demux_id].adapter_index = adapter_index;
|
---|
4264 | demux[demux_id].ca_mask = ca_mask;
|
---|
4265 | demux[demux_id].rdr = NULL;
|
---|
4266 | demux[demux_id].demux_index = demux_index;
|
---|
4267 | demux[demux_id].socket_fd = connfd;
|
---|
4268 | demux[demux_id].client_proto_version = client_proto_version;
|
---|
4269 |
|
---|
4270 | if(demux[demux_id].STREAMpidcount == 0) // encrypted PMT
|
---|
4271 | {
|
---|
4272 | demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount] = pmtpid;
|
---|
4273 | demux[demux_id].STREAMpidsType[demux[demux_id].STREAMpidcount] = 0x01;
|
---|
4274 | demux[demux_id].STREAMpidcount++;
|
---|
4275 | vpid = pmtpid;
|
---|
4276 | }
|
---|
4277 | }
|
---|
4278 | else
|
---|
4279 | {
|
---|
4280 | cs_log("Demuxer %d found %d ECMpids and %d STREAMpids in PMT",
|
---|
4281 | demux_id, demux[demux_id].ECMpidcount, demux[demux_id].STREAMpidcount);
|
---|
4282 |
|
---|
4283 | ca_mask = demux[demux_id].ca_mask;
|
---|
4284 | demux_index = demux[demux_id].demux_index;
|
---|
4285 | adapter_index = demux[demux_id].adapter_index;
|
---|
4286 | connfd = demux[demux_id].socket_fd;
|
---|
4287 | }
|
---|
4288 |
|
---|
4289 | for(j = 0; j < demux[demux_id].ECMpidcount; j++)
|
---|
4290 | {
|
---|
4291 | demux[demux_id].ECMpids[j].VPID = vpid; // register found vpid on all ecmpids of this demuxer
|
---|
4292 | }
|
---|
4293 |
|
---|
4294 | char channame[CS_SERVICENAME_SIZE];
|
---|
4295 | get_servicename(dvbapi_client,
|
---|
4296 | demux[demux_id].program_number,
|
---|
4297 | demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[0].PROVID : NO_PROVID_VALUE,
|
---|
4298 | demux[demux_id].ECMpidcount > 0 ? demux[demux_id].ECMpids[0].CAID : NO_CAID_VALUE,
|
---|
4299 | channame,
|
---|
4300 | sizeof(channame));
|
---|
4301 |
|
---|
4302 | cs_log_dbg(D_DVBAPI,"Demuxer %d serving srvid %04X (%s) on adapter %04X camask %04X index %04X pmtpid %04X",
|
---|
4303 | demux_id,
|
---|
4304 | demux[demux_id].program_number,
|
---|
4305 | channame,
|
---|
4306 | adapter_index,
|
---|
4307 | ca_mask,
|
---|
4308 | demux_index,
|
---|
4309 | pmtpid);
|
---|
4310 |
|
---|
4311 | demux[demux_id].stopdescramble = 0; // remove deletion mark!
|
---|
4312 |
|
---|
4313 | // remove from unassoc_fd when necessary
|
---|
4314 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
4315 | {
|
---|
4316 | if(unassoc_fd[j] == connfd)
|
---|
4317 | {
|
---|
4318 | unassoc_fd[j] = 0;
|
---|
4319 | }
|
---|
4320 | }
|
---|
4321 |
|
---|
4322 | dvbapi_capmt_notify(&demux[demux_id]);
|
---|
4323 | struct s_dvbapi_priority *xtraentry;
|
---|
4324 | int32_t k, l, m, xtra_demux_id;
|
---|
4325 |
|
---|
4326 | for(xtraentry = dvbapi_priority; xtraentry != NULL; xtraentry = xtraentry->next)
|
---|
4327 | {
|
---|
4328 | if(xtraentry->type != 'x')
|
---|
4329 | {
|
---|
4330 | continue;
|
---|
4331 | }
|
---|
4332 |
|
---|
4333 | for(j = 0; j <= demux[demux_id].ECMpidcount; ++j)
|
---|
4334 | {
|
---|
4335 | if((xtraentry->caid && xtraentry->caid != demux[demux_id].ECMpids[j].CAID)
|
---|
4336 | || (xtraentry->provid && xtraentry->provid != demux[demux_id].ECMpids[j].PROVID)
|
---|
4337 | || (xtraentry->ecmpid && xtraentry->ecmpid != demux[demux_id].ECMpids[j].ECM_PID)
|
---|
4338 | || (xtraentry->srvid && xtraentry->srvid != demux[demux_id].program_number))
|
---|
4339 | {
|
---|
4340 | continue;
|
---|
4341 | }
|
---|
4342 |
|
---|
4343 | cs_log("Mapping ecmpid %04X@%06X:%04X:%04X to xtra demuxer/ca-devices",
|
---|
4344 | xtraentry->caid, xtraentry->provid, xtraentry->ecmpid, xtraentry->srvid);
|
---|
4345 |
|
---|
4346 | for(xtra_demux_id = 0; xtra_demux_id < MAX_DEMUX && demux[xtra_demux_id].program_number > 0; xtra_demux_id++){ ; }
|
---|
4347 |
|
---|
4348 | if(xtra_demux_id >= MAX_DEMUX)
|
---|
4349 | {
|
---|
4350 | cs_log("Found no free demux device for extra streams");
|
---|
4351 | continue;
|
---|
4352 | }
|
---|
4353 |
|
---|
4354 | // copy to new demuxer
|
---|
4355 | if(!is_real_pmt)
|
---|
4356 | {
|
---|
4357 | get_demux_options(demux_id, buffer, &ca_mask, &demux_index, &adapter_index, &pmtpid);
|
---|
4358 | }
|
---|
4359 |
|
---|
4360 | demux[xtra_demux_id].ECMpids[0] = demux[demux_id].ECMpids[j];
|
---|
4361 | demux[xtra_demux_id].ECMpidcount = 1;
|
---|
4362 | demux[xtra_demux_id].STREAMpidcount = 0;
|
---|
4363 | demux[xtra_demux_id].program_number = demux[demux_id].program_number;
|
---|
4364 | demux[xtra_demux_id].pmtpid = demux[demux_id].pmtpid;
|
---|
4365 | demux[xtra_demux_id].demux_index = demux_index;
|
---|
4366 | demux[xtra_demux_id].adapter_index = adapter_index;
|
---|
4367 | demux[xtra_demux_id].ca_mask = ca_mask;
|
---|
4368 | demux[xtra_demux_id].socket_fd = connfd;
|
---|
4369 | demux[xtra_demux_id].stopdescramble = 0; // remove deletion mark!
|
---|
4370 | demux[xtra_demux_id].rdr = NULL;
|
---|
4371 | demux[xtra_demux_id].curindex = -1;
|
---|
4372 |
|
---|
4373 | // add streams to xtra demux
|
---|
4374 | for(k = 0; k < demux[demux_id].STREAMpidcount; ++k)
|
---|
4375 | {
|
---|
4376 | if(!demux[demux_id].ECMpids[j].streams || (demux[demux_id].ECMpids[j].streams & (1 << k)))
|
---|
4377 | {
|
---|
4378 | demux[xtra_demux_id].ECMpids[0].streams |= (1 << demux[xtra_demux_id].STREAMpidcount);
|
---|
4379 | demux[xtra_demux_id].STREAMpids[demux[xtra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpids[k];
|
---|
4380 | demux[xtra_demux_id].STREAMpidsType[demux[xtra_demux_id].STREAMpidcount] = demux[demux_id].STREAMpidsType[k];
|
---|
4381 | ++demux[xtra_demux_id].STREAMpidcount;
|
---|
4382 |
|
---|
4383 | // shift stream associations in normal demux because we will remove the stream entirely
|
---|
4384 | for(l = 0; l < demux[demux_id].ECMpidcount; ++l)
|
---|
4385 | {
|
---|
4386 | for(m = k; m < demux[demux_id].STREAMpidcount - 1; ++m)
|
---|
4387 | {
|
---|
4388 | if(demux[demux_id].ECMpids[l].streams & (1 << (m + 1)))
|
---|
4389 | {
|
---|
4390 | demux[demux_id].ECMpids[l].streams |= (1 << m);
|
---|
4391 | }
|
---|
4392 | else
|
---|
4393 | {
|
---|
4394 | demux[demux_id].ECMpids[l].streams &= ~(1 << m);
|
---|
4395 | }
|
---|
4396 | }
|
---|
4397 | }
|
---|
4398 |
|
---|
4399 | // remove stream association from normal demux device
|
---|
4400 | for(l = k; l < demux[demux_id].STREAMpidcount - 1; ++l)
|
---|
4401 | {
|
---|
4402 | demux[demux_id].STREAMpids[l] = demux[demux_id].STREAMpids[l + 1];
|
---|
4403 | demux[demux_id].STREAMpidsType[l] = demux[demux_id].STREAMpidsType[l + 1];
|
---|
4404 | }
|
---|
4405 | --demux[demux_id].STREAMpidcount;
|
---|
4406 | --k;
|
---|
4407 | }
|
---|
4408 | }
|
---|
4409 |
|
---|
4410 | // remove ecmpid from normal demuxer
|
---|
4411 | for(k = j; k < demux[demux_id].ECMpidcount; ++k)
|
---|
4412 | {
|
---|
4413 | demux[demux_id].ECMpids[k] = demux[demux_id].ECMpids[k + 1];
|
---|
4414 | }
|
---|
4415 | --demux[demux_id].ECMpidcount;
|
---|
4416 | --j;
|
---|
4417 |
|
---|
4418 | if(demux[xtra_demux_id].STREAMpidcount <= 0)
|
---|
4419 | {
|
---|
4420 | cs_log("Found no streams for extra demuxer. Not starting additional decoding on it.");
|
---|
4421 | demux[xtra_demux_id].program_number = 0;
|
---|
4422 | demux[xtra_demux_id].stopdescramble = 1;
|
---|
4423 | }
|
---|
4424 |
|
---|
4425 | if(demux[demux_id].STREAMpidcount < 1)
|
---|
4426 | {
|
---|
4427 | cs_log("Found no streams for normal demuxer. Not starting additional decoding on it.");
|
---|
4428 | }
|
---|
4429 | }
|
---|
4430 | }
|
---|
4431 |
|
---|
4432 | if(start_descrambling)
|
---|
4433 | {
|
---|
4434 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
4435 | {
|
---|
4436 | // skip demuxers belonging to other caPMT connection
|
---|
4437 | // skip demuxers handled by other PMT files
|
---|
4438 | if((demux[j].program_number == 0) || (demux[j].socket_fd != connfd) || ((demux[j].socket_fd == -1)
|
---|
4439 | && (pmtfile && strcmp(demux[j].pmt_file, pmtfile) != 0)))
|
---|
4440 | {
|
---|
4441 | continue;
|
---|
4442 | }
|
---|
4443 |
|
---|
4444 | if(demux[j].running && demux_id == j)
|
---|
4445 | {
|
---|
4446 | disable_unused_streampids(j); // disable all streampids not in use anymore
|
---|
4447 | }
|
---|
4448 |
|
---|
4449 | if(demux[j].running == 0 && demux[j].ECMpidcount != 0) // only start demuxer if it wasn't running
|
---|
4450 | {
|
---|
4451 | // remove all non important filtering
|
---|
4452 | // (there are images with limited amount of filters available!)
|
---|
4453 | dvbapi_stop_all_emm_sdt_filtering(msgid);
|
---|
4454 |
|
---|
4455 | cs_log_dbg(D_DVBAPI, "Demuxer %d/%d lets start descrambling (srvid = %04X fd = %d ecmpids = %d)",
|
---|
4456 | j, MAX_DEMUX, demux[j].program_number, connfd, demux[j].ECMpidcount);
|
---|
4457 |
|
---|
4458 | demux[j].running = 1; // mark channel as running
|
---|
4459 | openxcas_set_sid(demux[j].program_number);
|
---|
4460 | demux[j].decodingtries = -1;
|
---|
4461 | dvbapi_resort_ecmpids(j);
|
---|
4462 | dvbapi_try_next_caid(j, 0, msgid);
|
---|
4463 | cs_sleepms(1);
|
---|
4464 | }
|
---|
4465 | else if(demux[j].ECMpidcount == 0) // fta do logging and part of ecmhandler since there will be no ecms asked!
|
---|
4466 | {
|
---|
4467 | cs_log_dbg(D_DVBAPI, "Demuxer %d/%d no descrambling needed (srvid = %04X fd = %d ecmpids = %d)",
|
---|
4468 | j, MAX_DEMUX, demux[j].program_number, connfd, demux[j].ECMpidcount);
|
---|
4469 |
|
---|
4470 | demux[j].running = 0; // reset running flag
|
---|
4471 | demux[demux_id].pidindex = -1; // reset ecmpid used for descrambling
|
---|
4472 | dvbapi_stop_filter(j, TYPE_ECM, msgid);
|
---|
4473 |
|
---|
4474 | if(cfg.usrfileflag) // add to user log previous channel + time on channel
|
---|
4475 | {
|
---|
4476 | cs_statistics(dvbapi_client);
|
---|
4477 | }
|
---|
4478 |
|
---|
4479 | dvbapi_client->last_srvid = demux[demux_id].program_number; // set new channel srvid
|
---|
4480 | dvbapi_client->last_caid = NO_CAID_VALUE; // FTA channels have no caid!
|
---|
4481 | dvbapi_client->last_provid = NO_PROVID_VALUE; // FTA channels have no provid!
|
---|
4482 | dvbapi_client->lastswitch = dvbapi_client->last = time((time_t *)0); // reset idle-Time & last switch
|
---|
4483 | }
|
---|
4484 | }
|
---|
4485 | }
|
---|
4486 |
|
---|
4487 | int32_t DoNotStartEMM = 0;
|
---|
4488 |
|
---|
4489 | #if defined(WITH_COOLAPI) || defined(WITH_COOLAPI2)
|
---|
4490 | // Don't start and Stop EMM Filters over and over again if we are on FTA
|
---|
4491 | if(dvbapi_client->last_caid == NO_CAID_VALUE)
|
---|
4492 | {
|
---|
4493 | DoNotStartEMM = 1;
|
---|
4494 | }
|
---|
4495 | #endif
|
---|
4496 |
|
---|
4497 | // only do emm setup if au enabled and not running!
|
---|
4498 | if(cfg.dvbapi_au > 0 && demux[demux_id].EMMpidcount == 0 && !DoNotStartEMM)
|
---|
4499 | {
|
---|
4500 | demux[demux_id].emm_filter = -1; // to register first run emmfilter start
|
---|
4501 |
|
---|
4502 | if(demux[demux_id].emmstart.time == 1) // irdeto fetch emm cat direct!
|
---|
4503 | {
|
---|
4504 | // trick to let emm fetching start after 30 seconds to speed up zapping
|
---|
4505 | cs_ftime(&demux[demux_id].emmstart);
|
---|
4506 |
|
---|
4507 | dvbapi_start_filter(demux_id, demux[demux_id].pidindex, 0x001, 0x001, 0x01, 0x01, 0xFF, 0, TYPE_EMM); // CAT
|
---|
4508 | }
|
---|
4509 | else
|
---|
4510 | {
|
---|
4511 | cs_ftime(&demux[demux_id].emmstart); // for all other caids delayed start!
|
---|
4512 | }
|
---|
4513 | }
|
---|
4514 |
|
---|
4515 | return demux_id;
|
---|
4516 | }
|
---|
4517 |
|
---|
4518 | static uint32_t dvbapi_extract_sdt_string(char *buf, uint32_t buflen, uint8_t *source, uint32_t sourcelen)
|
---|
4519 | {
|
---|
4520 | uint32_t i, j, offset = 0;
|
---|
4521 | int8_t iso_mode = -1;
|
---|
4522 | char *tmpbuf;
|
---|
4523 | const uint8_t *ptr_in;
|
---|
4524 | uint8_t *ptr_out;
|
---|
4525 | size_t in_bytes, out_bytes;
|
---|
4526 |
|
---|
4527 | if(sourcelen == 0)
|
---|
4528 | {
|
---|
4529 | buf[0] = '\0';
|
---|
4530 | return 1;
|
---|
4531 | }
|
---|
4532 |
|
---|
4533 | if(!cs_malloc(&tmpbuf, buflen))
|
---|
4534 | {
|
---|
4535 | return 0;
|
---|
4536 | }
|
---|
4537 |
|
---|
4538 | if((sourcelen + 1) > buflen)
|
---|
4539 | {
|
---|
4540 | sourcelen = buflen - 1;
|
---|
4541 | }
|
---|
4542 |
|
---|
4543 | if(sourcelen > 0 && source[0] < 0x20)
|
---|
4544 | {
|
---|
4545 | //ISO-8859
|
---|
4546 | if(source[0] >= 0x01 && source[0] <= 0x0B && source[0] != 0x08)
|
---|
4547 | {
|
---|
4548 | offset = 1;
|
---|
4549 | iso_mode = 4 + source[0];
|
---|
4550 | }
|
---|
4551 | //ISO-8859
|
---|
4552 | else if(source[0] == 0x10 && source[1] == 0x00 && source[2] >= 0x01 && source[2] <= 0x0F && source[2] != 0x0C)
|
---|
4553 | {
|
---|
4554 | offset = 3; iso_mode = source[2];
|
---|
4555 | }
|
---|
4556 | //Unicode
|
---|
4557 | else if(source[0] == 0x11)
|
---|
4558 | {
|
---|
4559 | offset = 1;
|
---|
4560 | iso_mode = -2;
|
---|
4561 | }
|
---|
4562 | //missing: 0x12 Korean Character Set KSC5601-1987
|
---|
4563 | //missing: 0x13 Simplified Chinese Character Set GB-2312-1980
|
---|
4564 | //missing: 0x14 Big5 subset of ISO/IEC 10646-1 (Traditional Chinese)
|
---|
4565 | //Unicode as UTF-8
|
---|
4566 | else if(source[0] == 0x15)
|
---|
4567 | {
|
---|
4568 | offset = 1;
|
---|
4569 | iso_mode = -3;
|
---|
4570 | }
|
---|
4571 | //missing: 0x1F Described by encoding_type_id *standard not finished yet*
|
---|
4572 | //Reserved for future use
|
---|
4573 | else
|
---|
4574 | {
|
---|
4575 | NULLFREE(tmpbuf); return 0;
|
---|
4576 | }
|
---|
4577 | }
|
---|
4578 |
|
---|
4579 | if(offset >= sourcelen)
|
---|
4580 | {
|
---|
4581 | NULLFREE(tmpbuf); return 0;
|
---|
4582 | }
|
---|
4583 |
|
---|
4584 | if(iso_mode >= -1)
|
---|
4585 | {
|
---|
4586 | for(i = 0, j = 0; i < (sourcelen - offset); i++)
|
---|
4587 | {
|
---|
4588 | if(((uint8_t)source[offset + i]) >= 0x80 && ((uint8_t)source[offset + i]) <= 0x9F)
|
---|
4589 | {
|
---|
4590 | continue;
|
---|
4591 | }
|
---|
4592 | tmpbuf[j] = source[offset + i];
|
---|
4593 | j++;
|
---|
4594 | }
|
---|
4595 | tmpbuf[j] = '\0';
|
---|
4596 | }
|
---|
4597 |
|
---|
4598 | ptr_in = (const uint8_t *)tmpbuf;
|
---|
4599 | in_bytes = strlen(tmpbuf);
|
---|
4600 | ptr_out = (uint8_t *)buf;
|
---|
4601 | out_bytes = buflen;
|
---|
4602 |
|
---|
4603 | #ifdef READ_SDT_CHARSETS
|
---|
4604 | if(iso_mode >= -1)
|
---|
4605 | {
|
---|
4606 | memset(buf, 0, buflen);
|
---|
4607 | cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: %d offset: %u", iso_mode, offset);
|
---|
4608 | cs_log_dump_dbg(D_DVBAPI, (uint8_t *)tmpbuf, in_bytes, "sdt-info dbg: raw string: ");
|
---|
4609 |
|
---|
4610 | if(iso_mode == -1)
|
---|
4611 | {
|
---|
4612 | if(ISO6937toUTF8(&ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
|
---|
4613 | {
|
---|
4614 | cs_log_dbg(D_DVBAPI, "sdt-info error: ISO6937toUTF8 failed");
|
---|
4615 | NULLFREE(tmpbuf);
|
---|
4616 | return 0;
|
---|
4617 | }
|
---|
4618 | }
|
---|
4619 | else
|
---|
4620 | {
|
---|
4621 | if(ISO8859toUTF8(iso_mode, &ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
|
---|
4622 | {
|
---|
4623 | cs_log_dbg(D_DVBAPI, "sdt-info error: ISO8859toUTF8 failed");
|
---|
4624 | NULLFREE(tmpbuf);
|
---|
4625 | return 0;
|
---|
4626 | }
|
---|
4627 | }
|
---|
4628 | }
|
---|
4629 | #else
|
---|
4630 | if(iso_mode >= -1)
|
---|
4631 | {
|
---|
4632 | cs_strncpy(buf, tmpbuf, buflen);
|
---|
4633 | cs_log_dbg(D_DVBAPI, "sdt-info warning: your build of oscam does not support iso-to-utf8 conversion, special chars may be corrupted!");
|
---|
4634 | }
|
---|
4635 | #endif
|
---|
4636 | else if(iso_mode == -2)
|
---|
4637 | {
|
---|
4638 | memset(buf, 0, buflen);
|
---|
4639 | cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: %d offset: %u", iso_mode, offset);
|
---|
4640 |
|
---|
4641 | if(UnicodetoUTF8(&ptr_in, &in_bytes, &ptr_out, &out_bytes) == (size_t)(-1))
|
---|
4642 | {
|
---|
4643 | cs_log_dbg(D_DVBAPI, "sdt-info error: UnicodetoUTF8 failed");
|
---|
4644 | NULLFREE(tmpbuf);
|
---|
4645 | return 0;
|
---|
4646 | }
|
---|
4647 | }
|
---|
4648 | else if(iso_mode == -3)
|
---|
4649 | {
|
---|
4650 | memcpy(buf, source + offset, sourcelen - offset);
|
---|
4651 | buf[sourcelen - offset] = '\0';
|
---|
4652 | cs_log_dbg(D_DVBAPI, "sdt-info dbg: iso_mode: -3 offset: %u", offset);
|
---|
4653 | }
|
---|
4654 | cs_log_dump_dbg(D_DVBAPI, (uint8_t*)buf, strlen(buf), "sdt-info dbg: encoded string: ");
|
---|
4655 | NULLFREE(tmpbuf);
|
---|
4656 | return 1;
|
---|
4657 | }
|
---|
4658 |
|
---|
4659 | static void dvbapi_create_srvid_line(int32_t demux_id, char *buffer, uint32_t buflen)
|
---|
4660 | {
|
---|
4661 | int32_t i, j;
|
---|
4662 | uint16_t caid_done[32], cur_caid;
|
---|
4663 | uint8_t caid_done_count = 0, skip_caid;
|
---|
4664 | int32_t pos = 0;
|
---|
4665 |
|
---|
4666 | if(demux[demux_id].ECMpidcount == 0)
|
---|
4667 | {
|
---|
4668 | snprintf(buffer, buflen, "%04X@%06X", NO_CAID_VALUE, NO_PROVID_VALUE);
|
---|
4669 | return;
|
---|
4670 | }
|
---|
4671 |
|
---|
4672 | for(i = 0; i < demux[demux_id].ECMpidcount && i < 32; i++)
|
---|
4673 | {
|
---|
4674 | skip_caid = 0;
|
---|
4675 | for(j = 0; j < caid_done_count; j++)
|
---|
4676 | {
|
---|
4677 | if(caid_done[j] == demux[demux_id].ECMpids[i].CAID)
|
---|
4678 | {
|
---|
4679 | skip_caid = 1;
|
---|
4680 | break;
|
---|
4681 | }
|
---|
4682 | }
|
---|
4683 |
|
---|
4684 | if(skip_caid)
|
---|
4685 | {
|
---|
4686 | continue;
|
---|
4687 | }
|
---|
4688 |
|
---|
4689 | cur_caid = demux[demux_id].ECMpids[i].CAID;
|
---|
4690 | pos += snprintf(buffer + pos, buflen - pos, "%s%04X", caid_done_count > 0 ? "," : "", cur_caid == 0 ? NO_CAID_VALUE : cur_caid);
|
---|
4691 |
|
---|
4692 | for(j = i; j < demux[demux_id].ECMpidcount; j++)
|
---|
4693 | {
|
---|
4694 | if(demux[demux_id].ECMpids[j].PROVID == 0)
|
---|
4695 | {
|
---|
4696 | continue;
|
---|
4697 | }
|
---|
4698 |
|
---|
4699 | if(cur_caid == demux[demux_id].ECMpids[j].CAID)
|
---|
4700 | {
|
---|
4701 | pos += snprintf(buffer + pos, buflen - pos, "@%06X", demux[demux_id].ECMpids[j].PROVID);
|
---|
4702 | }
|
---|
4703 | }
|
---|
4704 | caid_done[caid_done_count] = demux[demux_id].ECMpids[i].CAID;
|
---|
4705 | caid_done_count++;
|
---|
4706 | }
|
---|
4707 | }
|
---|
4708 |
|
---|
4709 | static const char *dvbapi_get_service_type(uint8_t service_type_id)
|
---|
4710 | {
|
---|
4711 | switch(service_type_id)
|
---|
4712 | {
|
---|
4713 | case 0x01:
|
---|
4714 | case 0x11:
|
---|
4715 | return "TV";
|
---|
4716 |
|
---|
4717 | case 0x02:
|
---|
4718 | case 0x07:
|
---|
4719 | case 0x0A:
|
---|
4720 | return "Radio";
|
---|
4721 |
|
---|
4722 | case 0x03:
|
---|
4723 | return "Teletext";
|
---|
4724 |
|
---|
4725 | case 0x0C:
|
---|
4726 | return "Data";
|
---|
4727 |
|
---|
4728 | default:
|
---|
4729 | return "TV";
|
---|
4730 | }
|
---|
4731 | }
|
---|
4732 |
|
---|
4733 | static void dvbapi_parse_sdt(int32_t demux_id, uint8_t *buffer, uint32_t length, uint32_t msgid)
|
---|
4734 | {
|
---|
4735 | uint8_t tag, data_length = 0, provider_name_length, service_name_length, service_type;
|
---|
4736 | uint16_t service_id, descriptor_length, dpos;
|
---|
4737 | uint32_t section_length, pos;
|
---|
4738 | int8_t did_save_srvid = 0;
|
---|
4739 | int32_t provid, caid, pidindex;
|
---|
4740 | char provider_name[64], service_name[64], tmp[256], srvid_line[1024];
|
---|
4741 | const char *type;
|
---|
4742 | FILE *fpsave = NULL;
|
---|
4743 |
|
---|
4744 | cs_log_dump_dbg(D_DVBAPI, buffer, length, "sdt-info dbg: sdt data: ");
|
---|
4745 |
|
---|
4746 | if(length < 3)
|
---|
4747 | {
|
---|
4748 | return;
|
---|
4749 | }
|
---|
4750 |
|
---|
4751 | if(buffer[0] != 0x42)
|
---|
4752 | {
|
---|
4753 | return;
|
---|
4754 | }
|
---|
4755 |
|
---|
4756 | section_length = b2i(2, buffer + 1) & 0xFFF;
|
---|
4757 | if(section_length + 3 != length)
|
---|
4758 | {
|
---|
4759 | return;
|
---|
4760 | }
|
---|
4761 |
|
---|
4762 | for(pos = 11; pos + 5 < length; pos += descriptor_length)
|
---|
4763 | {
|
---|
4764 | service_id = b2i(2, buffer + pos);
|
---|
4765 | descriptor_length = b2i(2, buffer + pos + 3) & 0xFFF;
|
---|
4766 |
|
---|
4767 | if((pos + 5 + descriptor_length) >= length)
|
---|
4768 | {
|
---|
4769 | return;
|
---|
4770 | }
|
---|
4771 | pos += 5;
|
---|
4772 |
|
---|
4773 | if(service_id != demux[demux_id].program_number)
|
---|
4774 | {
|
---|
4775 | continue;
|
---|
4776 | }
|
---|
4777 |
|
---|
4778 | for(dpos = 0; dpos + 1 < descriptor_length; dpos += (2 + data_length))
|
---|
4779 | {
|
---|
4780 | tag = buffer[pos + dpos];
|
---|
4781 | data_length = buffer[pos + dpos + 1];
|
---|
4782 |
|
---|
4783 | if(dpos + 1 + data_length >= descriptor_length)
|
---|
4784 | {
|
---|
4785 | break;
|
---|
4786 | }
|
---|
4787 |
|
---|
4788 | if(tag != 0x48)
|
---|
4789 | {
|
---|
4790 | continue;
|
---|
4791 | }
|
---|
4792 |
|
---|
4793 | if(dpos + 3 >= descriptor_length)
|
---|
4794 | {
|
---|
4795 | break;
|
---|
4796 | }
|
---|
4797 |
|
---|
4798 | service_type = buffer[pos + dpos + 2];
|
---|
4799 | provider_name_length = buffer[pos + dpos + 3];
|
---|
4800 |
|
---|
4801 | if((dpos + 4 + provider_name_length + 1) > descriptor_length)
|
---|
4802 | {
|
---|
4803 | break;
|
---|
4804 | }
|
---|
4805 |
|
---|
4806 | service_name_length = buffer[pos + dpos + 4 + provider_name_length];
|
---|
4807 | if((dpos + 4 + provider_name_length + 1 + service_name_length) > descriptor_length)
|
---|
4808 | {
|
---|
4809 | break;
|
---|
4810 | }
|
---|
4811 |
|
---|
4812 | pidindex = demux[demux_id].pidindex;
|
---|
4813 | if(pidindex !=-1)
|
---|
4814 | {
|
---|
4815 | provid = demux[demux_id].ECMpids[pidindex].PROVID;
|
---|
4816 | caid = demux[demux_id].ECMpids[pidindex].CAID;
|
---|
4817 | }
|
---|
4818 | else
|
---|
4819 | {
|
---|
4820 | if(demux[demux_id].ECMpidcount == 0 || demux[demux_id].ECMpids[0].CAID == 0)
|
---|
4821 | {
|
---|
4822 | caid = NO_CAID_VALUE;
|
---|
4823 | provid = NO_PROVID_VALUE;
|
---|
4824 | }
|
---|
4825 | else
|
---|
4826 | {
|
---|
4827 | caid = demux[demux_id].ECMpids[0].CAID;
|
---|
4828 | provid = demux[demux_id].ECMpids[0].PROVID;
|
---|
4829 | }
|
---|
4830 | }
|
---|
4831 |
|
---|
4832 | if(!dvbapi_extract_sdt_string(provider_name, sizeof(provider_name), buffer + pos + dpos + 4, provider_name_length))
|
---|
4833 | {
|
---|
4834 | break;
|
---|
4835 | }
|
---|
4836 |
|
---|
4837 | if(!dvbapi_extract_sdt_string(service_name, sizeof(service_name), buffer + pos + dpos + 4 + provider_name_length + 1, service_name_length))
|
---|
4838 | {
|
---|
4839 | break;
|
---|
4840 | }
|
---|
4841 |
|
---|
4842 | cs_log_dbg(D_DVBAPI,"sdt-info (provider: %s - channel: %s)", provider_name, service_name);
|
---|
4843 | dvbapi_stop_filter(demux_id, TYPE_SDT, msgid);
|
---|
4844 |
|
---|
4845 | if(strlen(provider_name) && caid != NO_CAID_VALUE)
|
---|
4846 | {
|
---|
4847 | get_providername_or_null(provid, caid, tmp, sizeof(tmp));
|
---|
4848 | if(tmp[0] == '\0')
|
---|
4849 | {
|
---|
4850 | get_config_filename(tmp, sizeof(tmp), "oscam.provid");
|
---|
4851 | if((fpsave = fopen(tmp, "a")))
|
---|
4852 | {
|
---|
4853 | fprintf(fpsave, "\n%04X@%06X|%s|", caid, provid, provider_name);
|
---|
4854 | fclose(fpsave);
|
---|
4855 | init_provid();
|
---|
4856 | }
|
---|
4857 | }
|
---|
4858 | }
|
---|
4859 |
|
---|
4860 | if(strlen(service_name))
|
---|
4861 | {
|
---|
4862 | get_servicename_or_null(cur_client(), service_id, provid, caid, tmp, sizeof(tmp));
|
---|
4863 |
|
---|
4864 | if(tmp[0] == '\0')
|
---|
4865 | {
|
---|
4866 | type = dvbapi_get_service_type(service_type);
|
---|
4867 | get_config_filename(tmp, sizeof(tmp), "oscam.srvid2");
|
---|
4868 |
|
---|
4869 | if(!access(tmp, F_OK) && (fpsave = fopen(tmp, "a")))
|
---|
4870 | {
|
---|
4871 | if((caid != NO_CAID_VALUE) || (cfg.dvbapi_read_sdt > 1))
|
---|
4872 | {
|
---|
4873 | dvbapi_create_srvid_line(demux_id, srvid_line, sizeof(srvid_line));
|
---|
4874 |
|
---|
4875 | if(cfg.dvbapi_write_sdt_prov)
|
---|
4876 | {
|
---|
4877 | fprintf(fpsave, "\n%04X:%s|%s|%s||%s", service_id, srvid_line, service_name, type, provider_name);
|
---|
4878 | }
|
---|
4879 | else
|
---|
4880 | {
|
---|
4881 | fprintf(fpsave, "\n%04X:%s|%s|%s", service_id, srvid_line, service_name, type);
|
---|
4882 | }
|
---|
4883 | did_save_srvid = 1;
|
---|
4884 | }
|
---|
4885 | }
|
---|
4886 | else
|
---|
4887 | {
|
---|
4888 | get_config_filename(tmp, sizeof(tmp), "oscam.srvid");
|
---|
4889 |
|
---|
4890 | if((fpsave = fopen(tmp, "a")))
|
---|
4891 | {
|
---|
4892 | if((caid != NO_CAID_VALUE) || (cfg.dvbapi_read_sdt > 1))
|
---|
4893 | {
|
---|
4894 | dvbapi_create_srvid_line(demux_id, srvid_line, sizeof(srvid_line));
|
---|
4895 |
|
---|
4896 | if(cfg.dvbapi_write_sdt_prov)
|
---|
4897 | {
|
---|
4898 | fprintf(fpsave, "\n%s:%04X|%s|%s|%s", srvid_line, service_id, provider_name, service_name, type);
|
---|
4899 | }
|
---|
4900 | else
|
---|
4901 | {
|
---|
4902 | fprintf(fpsave, "\n%s:%04X||%s|%s", srvid_line, service_id, service_name, type);
|
---|
4903 | }
|
---|
4904 | did_save_srvid = 1;
|
---|
4905 | }
|
---|
4906 | }
|
---|
4907 | }
|
---|
4908 |
|
---|
4909 | if(fpsave)
|
---|
4910 | {
|
---|
4911 | fclose(fpsave);
|
---|
4912 | }
|
---|
4913 |
|
---|
4914 | if(did_save_srvid)
|
---|
4915 | {
|
---|
4916 | init_srvid();
|
---|
4917 | }
|
---|
4918 | }
|
---|
4919 | }
|
---|
4920 | return;
|
---|
4921 | }
|
---|
4922 | }
|
---|
4923 | }
|
---|
4924 |
|
---|
4925 | static void dvbapi_parse_pat(int32_t demux_id, uint8_t *buffer, uint32_t length, uint32_t msgid)
|
---|
4926 | {
|
---|
4927 | uint16_t srvid;
|
---|
4928 | uint32_t i;
|
---|
4929 | dvbapi_stop_filter(demux_id, TYPE_PAT, msgid);
|
---|
4930 |
|
---|
4931 | for(i = 8; i + 7 < length; i += 4)
|
---|
4932 | {
|
---|
4933 | srvid = b2i(2, buffer + i);
|
---|
4934 | if(srvid == 0)
|
---|
4935 | {
|
---|
4936 | continue;
|
---|
4937 | }
|
---|
4938 |
|
---|
4939 | if(demux[demux_id].program_number == srvid)
|
---|
4940 | {
|
---|
4941 | dvbapi_start_pmt_filter(demux_id, b2i(2, buffer + i + 2) & 0x1FFF);
|
---|
4942 | break;
|
---|
4943 | }
|
---|
4944 | }
|
---|
4945 | }
|
---|
4946 |
|
---|
4947 | int32_t dvbapi_init_listenfd(void)
|
---|
4948 | {
|
---|
4949 | int32_t clilen, listenfd;
|
---|
4950 | struct sockaddr_un servaddr;
|
---|
4951 |
|
---|
4952 | memset(&servaddr, 0, sizeof(struct sockaddr_un));
|
---|
4953 | servaddr.sun_family = AF_UNIX;
|
---|
4954 | cs_strncpy(servaddr.sun_path, devices[selected_box].cam_socket_path, sizeof(servaddr.sun_path));
|
---|
4955 | clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
|
---|
4956 |
|
---|
4957 | if(((unlink(devices[selected_box].cam_socket_path) < 0) && (errno != ENOENT))
|
---|
4958 | || ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
|
---|
4959 | || (bind(listenfd, (struct sockaddr *)&servaddr, clilen) < 0)
|
---|
4960 | || (listen(listenfd, 5) < 0))
|
---|
4961 | {
|
---|
4962 | return 0;
|
---|
4963 | }
|
---|
4964 |
|
---|
4965 | // change the access right on the camd.socket
|
---|
4966 | // this will allow oscam to run as root if needed
|
---|
4967 | // and still allow non root client to connect to the socket
|
---|
4968 | chmod(devices[selected_box].cam_socket_path, S_IRWXU | S_IRWXG | S_IRWXO);
|
---|
4969 | return listenfd;
|
---|
4970 | }
|
---|
4971 |
|
---|
4972 | int32_t dvbapi_net_init_listenfd(void)
|
---|
4973 | {
|
---|
4974 | int32_t listenfd;
|
---|
4975 | struct SOCKADDR servaddr;
|
---|
4976 |
|
---|
4977 | memset(&servaddr, 0, sizeof(servaddr));
|
---|
4978 | SIN_GET_FAMILY(servaddr) = DEFAULT_AF;
|
---|
4979 | SIN_GET_ADDR(servaddr) = ADDR_ANY;
|
---|
4980 | SIN_GET_PORT(servaddr) = htons((uint16_t)cfg.dvbapi_listenport);
|
---|
4981 |
|
---|
4982 | if((listenfd = socket(DEFAULT_AF, SOCK_STREAM, 0)) < 0)
|
---|
4983 | {
|
---|
4984 | return 0;
|
---|
4985 | }
|
---|
4986 | int32_t opt = 0;
|
---|
4987 |
|
---|
4988 | #ifdef IPV6SUPPORT
|
---|
4989 | // azbox toolchain do not have this define
|
---|
4990 | #ifndef IPV6_V6ONLY
|
---|
4991 | #define IPV6_V6ONLY 26
|
---|
4992 | #endif
|
---|
4993 | // set the server socket option to listen on IPv4 and IPv6 simultaneously
|
---|
4994 | setsockopt(listenfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&opt, sizeof(opt));
|
---|
4995 | #endif
|
---|
4996 |
|
---|
4997 | opt = 1;
|
---|
4998 | setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt));
|
---|
4999 | set_so_reuseport(listenfd);
|
---|
5000 |
|
---|
5001 | if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
|
---|
5002 | {
|
---|
5003 | return 0;
|
---|
5004 | }
|
---|
5005 |
|
---|
5006 | if(listen(listenfd, 5) < 0)
|
---|
5007 | {
|
---|
5008 | return 0;
|
---|
5009 | }
|
---|
5010 | return listenfd;
|
---|
5011 | }
|
---|
5012 |
|
---|
5013 | static pthread_mutex_t event_handler_lock = PTHREAD_MUTEX_INITIALIZER;
|
---|
5014 |
|
---|
5015 | void event_handler(int32_t UNUSED(signal))
|
---|
5016 | {
|
---|
5017 | struct stat pmt_info;
|
---|
5018 | char dest[1024];
|
---|
5019 | DIR *dirp;
|
---|
5020 | struct dirent entry, *dp = NULL;
|
---|
5021 | int32_t i, pmt_fd;
|
---|
5022 | uint8_t mbuf[2048]; // dirty fix: larger buffer needed for CA PMT mode 6 with many parallel channels to decode
|
---|
5023 |
|
---|
5024 | if(dvbapi_client != cur_client())
|
---|
5025 | {
|
---|
5026 | return;
|
---|
5027 | }
|
---|
5028 |
|
---|
5029 | SAFE_MUTEX_LOCK(&event_handler_lock);
|
---|
5030 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
|
---|
5031 | {
|
---|
5032 | pausecam = 0;
|
---|
5033 | }
|
---|
5034 | else
|
---|
5035 | {
|
---|
5036 | int32_t standby_fd = open(STANDBY_FILE, O_RDONLY);
|
---|
5037 | pausecam = (standby_fd > 0) ? 1 : 0;
|
---|
5038 | if(standby_fd > 0)
|
---|
5039 | {
|
---|
5040 | int32_t ret = close(standby_fd);
|
---|
5041 | if(ret < 0)
|
---|
5042 | {
|
---|
5043 | cs_log("ERROR: Could not close standby fd (errno=%d %s)", errno, strerror(errno));
|
---|
5044 | }
|
---|
5045 | }
|
---|
5046 | }
|
---|
5047 |
|
---|
5048 | if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_pmtmode == 1)
|
---|
5049 | {
|
---|
5050 | SAFE_MUTEX_UNLOCK(&event_handler_lock);
|
---|
5051 | return;
|
---|
5052 | }
|
---|
5053 |
|
---|
5054 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
5055 | {
|
---|
5056 | if(demux[i].pmt_file[0] != 0)
|
---|
5057 | {
|
---|
5058 | snprintf(dest, sizeof(dest), "%s%s", TMPDIR, demux[i].pmt_file);
|
---|
5059 | pmt_fd = open(dest, O_RDONLY);
|
---|
5060 | if(pmt_fd > 0)
|
---|
5061 | {
|
---|
5062 | if(fstat(pmt_fd, &pmt_info) != 0)
|
---|
5063 | {
|
---|
5064 | int32_t ret = close(pmt_fd);
|
---|
5065 | if(ret < 0)
|
---|
5066 | {
|
---|
5067 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
5068 | }
|
---|
5069 | continue;
|
---|
5070 | }
|
---|
5071 |
|
---|
5072 | if((time_t)pmt_info.st_mtime != demux[i].pmt_time)
|
---|
5073 | {
|
---|
5074 | dvbapi_stop_descrambling(i, 0);
|
---|
5075 | }
|
---|
5076 |
|
---|
5077 | int32_t ret = close(pmt_fd);
|
---|
5078 | if(ret < 0)
|
---|
5079 | {
|
---|
5080 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
5081 | }
|
---|
5082 | continue;
|
---|
5083 | }
|
---|
5084 | else
|
---|
5085 | {
|
---|
5086 | cs_log("Demuxer %d Unable to open PMT file %s -> stop descrambling!", i, dest);
|
---|
5087 | dvbapi_stop_descrambling(i, 0);
|
---|
5088 | }
|
---|
5089 | }
|
---|
5090 | }
|
---|
5091 |
|
---|
5092 | if(disable_pmt_files)
|
---|
5093 | {
|
---|
5094 | SAFE_MUTEX_UNLOCK(&event_handler_lock);
|
---|
5095 | return;
|
---|
5096 | }
|
---|
5097 |
|
---|
5098 | dirp = opendir(TMPDIR);
|
---|
5099 | if(!dirp)
|
---|
5100 | {
|
---|
5101 | cs_log_dbg(D_DVBAPI, "opendir failed (errno=%d %s)", errno, strerror(errno));
|
---|
5102 | SAFE_MUTEX_UNLOCK(&event_handler_lock);
|
---|
5103 | return;
|
---|
5104 | }
|
---|
5105 |
|
---|
5106 | while(!cs_readdir_r(dirp, &entry, &dp))
|
---|
5107 | {
|
---|
5108 | if(!dp)
|
---|
5109 | {
|
---|
5110 | break;
|
---|
5111 | }
|
---|
5112 |
|
---|
5113 | if(strlen(dp->d_name) < 7)
|
---|
5114 | {
|
---|
5115 | continue;
|
---|
5116 | }
|
---|
5117 |
|
---|
5118 | if(strncmp(dp->d_name, "pmt", 3) != 0 || strncmp(dp->d_name + strlen(dp->d_name) - 4, ".tmp", 4) != 0)
|
---|
5119 | {
|
---|
5120 | continue;
|
---|
5121 | }
|
---|
5122 |
|
---|
5123 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
5124 | struct s_dvbapi_priority *p;
|
---|
5125 | for(p = dvbapi_priority; p != NULL; p = p->next) // stapi: check if there is a device connected to this pmt file!
|
---|
5126 | {
|
---|
5127 | if(p->type != 's') { continue; } // stapi rule?
|
---|
5128 | if(strcmp(dp->d_name, p->pmtfile) != 0) { continue; } // same file?
|
---|
5129 | break; // found match!
|
---|
5130 | }
|
---|
5131 |
|
---|
5132 | if(p == NULL)
|
---|
5133 | {
|
---|
5134 | cs_log_dbg(D_DVBAPI, "No matching S: line in oscam.dvbapi for pmtfile %s -> skip!", dp->d_name);
|
---|
5135 | continue;
|
---|
5136 | }
|
---|
5137 | #endif
|
---|
5138 | snprintf(dest, sizeof(dest), "%s%s", TMPDIR, dp->d_name);
|
---|
5139 | pmt_fd = open(dest, O_RDONLY);
|
---|
5140 | if(pmt_fd < 0)
|
---|
5141 | {
|
---|
5142 | continue;
|
---|
5143 | }
|
---|
5144 |
|
---|
5145 | if(fstat(pmt_fd, &pmt_info) != 0)
|
---|
5146 | {
|
---|
5147 | int32_t ret = close(pmt_fd);
|
---|
5148 | if(ret < 0)
|
---|
5149 | {
|
---|
5150 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
5151 | }
|
---|
5152 | continue;
|
---|
5153 | }
|
---|
5154 |
|
---|
5155 | int32_t found = 0;
|
---|
5156 |
|
---|
5157 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
5158 | {
|
---|
5159 | if(strcmp(demux[i].pmt_file, dp->d_name) == 0)
|
---|
5160 | {
|
---|
5161 | if((time_t)pmt_info.st_mtime == demux[i].pmt_time)
|
---|
5162 | {
|
---|
5163 | found = 1;
|
---|
5164 | break;
|
---|
5165 | }
|
---|
5166 | }
|
---|
5167 | }
|
---|
5168 |
|
---|
5169 | if(found)
|
---|
5170 | {
|
---|
5171 | int32_t ret = close(pmt_fd);
|
---|
5172 | if(ret < 0)
|
---|
5173 | {
|
---|
5174 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
5175 | }
|
---|
5176 | continue;
|
---|
5177 | }
|
---|
5178 | cs_log_dbg(D_DVBAPI, "found pmt file %s", dest);
|
---|
5179 | cs_sleepms(100);
|
---|
5180 |
|
---|
5181 | uint32_t len = read(pmt_fd, mbuf, sizeof(mbuf));
|
---|
5182 | int32_t ret = close(pmt_fd);
|
---|
5183 |
|
---|
5184 | if(ret < 0)
|
---|
5185 | {
|
---|
5186 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
5187 | }
|
---|
5188 |
|
---|
5189 | if(len < 1)
|
---|
5190 | {
|
---|
5191 | cs_log_dbg(D_DVBAPI, "pmt file %s have invalid len!", dest);
|
---|
5192 | continue;
|
---|
5193 | }
|
---|
5194 |
|
---|
5195 | int32_t pmt_id;
|
---|
5196 | #ifdef QBOXHD
|
---|
5197 | uint32_t j1, j2;
|
---|
5198 |
|
---|
5199 | // QboxHD pmt.tmp is the full capmt written as a string of hex values
|
---|
5200 | // pmt.tmp must be longer than 3 bytes (6 hex chars) and even length
|
---|
5201 | if((len < 6) || ((len % 2) != 0) || ((len / 2) > sizeof(dest)))
|
---|
5202 | {
|
---|
5203 | cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, incorrect length");
|
---|
5204 | continue;
|
---|
5205 | }
|
---|
5206 |
|
---|
5207 | for(j2 = 0, j1 = 0; j2 < len; j2 += 2, j1++)
|
---|
5208 | {
|
---|
5209 | unsigned int tmp;
|
---|
5210 | if(sscanf((char *)mbuf + j2, "%02X", &tmp) != 1)
|
---|
5211 | {
|
---|
5212 | cs_log_dbg(D_DVBAPI, "error parsing QboxHD pmt.tmp, data not valid in position %d", j2);
|
---|
5213 | SAFE_MUTEX_UNLOCK(&event_handler_lock);
|
---|
5214 | return;
|
---|
5215 | }
|
---|
5216 | else
|
---|
5217 | {
|
---|
5218 | memcpy(dest + j1, &tmp, 4);
|
---|
5219 | }
|
---|
5220 | }
|
---|
5221 | cs_log_dump_dbg(D_DVBAPI, (uint8_t *)dest, len / 2, "QboxHD pmt.tmp:");
|
---|
5222 | pmt_id = dvbapi_parse_capmt((uint8_t *)dest + 4, (len / 2) - 4, -1, dp->d_name, 0, 0, 0, 0);
|
---|
5223 | #else
|
---|
5224 | if(len > sizeof(dest))
|
---|
5225 | {
|
---|
5226 | cs_log_dbg(D_DVBAPI, "event_handler() dest buffer is to small for pmt data!");
|
---|
5227 | continue;
|
---|
5228 | }
|
---|
5229 |
|
---|
5230 | if(len < 16)
|
---|
5231 | {
|
---|
5232 | cs_log_dbg(D_DVBAPI, "event_handler() received pmt is too small! (%d < 16 bytes!)", len);
|
---|
5233 | continue;
|
---|
5234 | }
|
---|
5235 | cs_log_dump_dbg(D_DVBAPI, mbuf, len, "pmt:");
|
---|
5236 |
|
---|
5237 | dest[0] = 0x03;
|
---|
5238 | dest[1] = mbuf[3];
|
---|
5239 | dest[2] = mbuf[4];
|
---|
5240 |
|
---|
5241 | uint32_t pmt_program_length = b2i(2, mbuf + 10) & 0xFFF;
|
---|
5242 | i2b_buf(2, pmt_program_length + 1, (uint8_t *) dest + 4);
|
---|
5243 | dest[6] = 0;
|
---|
5244 | memcpy(dest + 7, mbuf + 12, len - 12 - 4);
|
---|
5245 |
|
---|
5246 | pmt_id = dvbapi_parse_capmt((uint8_t *)dest, 7 + len - 12 - 4, -1, dp->d_name, 0, 0, 0, 0);
|
---|
5247 | #endif
|
---|
5248 | if(pmt_id >= 0)
|
---|
5249 | {
|
---|
5250 | cs_strncpy(demux[pmt_id].pmt_file, dp->d_name, sizeof(demux[pmt_id].pmt_file));
|
---|
5251 | demux[pmt_id].pmt_time = (time_t)pmt_info.st_mtime;
|
---|
5252 | }
|
---|
5253 |
|
---|
5254 | if(cfg.dvbapi_pmtmode == 3)
|
---|
5255 | {
|
---|
5256 | disable_pmt_files = 1;
|
---|
5257 | break;
|
---|
5258 | }
|
---|
5259 | }
|
---|
5260 | closedir(dirp);
|
---|
5261 | SAFE_MUTEX_UNLOCK(&event_handler_lock);
|
---|
5262 | }
|
---|
5263 |
|
---|
5264 | void *dvbapi_event_thread(void *cli)
|
---|
5265 | {
|
---|
5266 | struct s_client *client = (struct s_client *) cli;
|
---|
5267 | SAFE_SETSPECIFIC(getclient, client);
|
---|
5268 | set_thread_name(__func__);
|
---|
5269 |
|
---|
5270 | while(!exit_oscam)
|
---|
5271 | {
|
---|
5272 | cs_sleepms(750);
|
---|
5273 | event_handler(0);
|
---|
5274 | }
|
---|
5275 | return NULL;
|
---|
5276 | }
|
---|
5277 |
|
---|
5278 | void dvbapi_process_input(int32_t demux_id, int32_t filter_num, uint8_t *buffer, int32_t len, uint32_t msgid)
|
---|
5279 | {
|
---|
5280 | struct s_ecmpid *curpid = NULL;
|
---|
5281 | int32_t pid = demux[demux_id].demux_fd[filter_num].pidindex;
|
---|
5282 | uint16_t filtertype = demux[demux_id].demux_fd[filter_num].type;
|
---|
5283 | uint32_t sctlen = SCT_LEN(buffer);
|
---|
5284 |
|
---|
5285 | if((uint32_t) len < sctlen) // invalid CAT length
|
---|
5286 | {
|
---|
5287 | cs_log_dbg(D_DVBAPI, "Received filterdata with total length 0x%03X but section length is 0x%03X -> invalid length!", len, sctlen);
|
---|
5288 | return;
|
---|
5289 | }
|
---|
5290 |
|
---|
5291 | if(demux_id < 0 || demux_id >= MAX_DEMUX)
|
---|
5292 | {
|
---|
5293 | cs_log("dvbapi_process_input(): error - received invalid demux_id (%d)", demux_id);
|
---|
5294 | return;
|
---|
5295 | }
|
---|
5296 |
|
---|
5297 | if(filter_num < 0 || filter_num >= MAX_FILTER)
|
---|
5298 | {
|
---|
5299 | cs_log("dvbapi_process_input(): error - received invalid filter_num (%d)", filter_num);
|
---|
5300 | return;
|
---|
5301 | }
|
---|
5302 |
|
---|
5303 | if(pid != -1 && filtertype == TYPE_ECM)
|
---|
5304 | {
|
---|
5305 | curpid = &demux[demux_id].ECMpids[pid];
|
---|
5306 | }
|
---|
5307 |
|
---|
5308 | int32_t filt_match = filtermatch(buffer, filter_num, demux_id, sctlen); // acts on all filters (sdt/emm/ecm)
|
---|
5309 | if(!filt_match)
|
---|
5310 | {
|
---|
5311 | cs_log_dbg(D_DVBAPI,"Demuxer %d receiver returned data not matching the filter -> delivered filter data discarded!", demux_id);
|
---|
5312 | return;
|
---|
5313 | }
|
---|
5314 |
|
---|
5315 | if(curpid && curpid->tries <= 0xF0 && filtertype == TYPE_ECM)
|
---|
5316 | {
|
---|
5317 | curpid->irdeto_maxindex = 0;
|
---|
5318 | curpid->irdeto_curindex = 0xFE;
|
---|
5319 | curpid->tries = 0xFE; // reset timeout retry flag
|
---|
5320 | curpid->irdeto_cycle = 0xFE; // reset irdetocycle
|
---|
5321 | curpid->table = 0;
|
---|
5322 | curpid->checked = 4; // flag ecmpid as checked
|
---|
5323 | curpid->status = -1; // flag ecmpid as unusable
|
---|
5324 |
|
---|
5325 | if(pid == demux[demux_id].pidindex)
|
---|
5326 | {
|
---|
5327 | // current pid delivered problems so this pid isn't
|
---|
5328 | // being used to descramble any longer -> clear pidindex
|
---|
5329 | demux[demux_id].pidindex = -1;
|
---|
5330 |
|
---|
5331 | // remove this pid from channel cache since we had no founds on any ecmpid!
|
---|
5332 | dvbapi_edit_channel_cache(demux_id, pid, 0);
|
---|
5333 | }
|
---|
5334 |
|
---|
5335 | dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
|
---|
5336 | return;
|
---|
5337 | }
|
---|
5338 |
|
---|
5339 | if(filtertype == TYPE_ECM)
|
---|
5340 | {
|
---|
5341 | uint32_t chid = 0x10000;
|
---|
5342 | int8_t pvu_skip = 0;
|
---|
5343 | ECM_REQUEST *er;
|
---|
5344 |
|
---|
5345 | if(len != 0) // len = 0 receiver encountered an internal bufferoverflow!
|
---|
5346 | {
|
---|
5347 | cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d Filter %d fetched ECM data (ecmlength = 0x%03X):",
|
---|
5348 | demux_id, filter_num + 1, sctlen);
|
---|
5349 |
|
---|
5350 | if(sctlen > MAX_ECM_SIZE) // ecm too long to handle!
|
---|
5351 | {
|
---|
5352 | cs_log_dbg(D_DVBAPI, "Received data with total length 0x%03X but max supported ECM length is 0x%03X -> Please report!",
|
---|
5353 | sctlen, MAX_ECM_SIZE);
|
---|
5354 |
|
---|
5355 | if(curpid)
|
---|
5356 | {
|
---|
5357 | curpid->tries -= 0x0E;
|
---|
5358 | }
|
---|
5359 | return;
|
---|
5360 | }
|
---|
5361 |
|
---|
5362 | if(!(buffer[0] == 0x80 || buffer[0] == 0x81))
|
---|
5363 | {
|
---|
5364 | cs_log_dbg(D_DVBAPI, "Received an ECM with invalid ecmtable ID %02X -> ignoring!", buffer[0]);
|
---|
5365 | if(curpid)
|
---|
5366 | {
|
---|
5367 | curpid->tries--;
|
---|
5368 | }
|
---|
5369 | return;
|
---|
5370 | }
|
---|
5371 |
|
---|
5372 | #ifdef WITH_EMU
|
---|
5373 | if(caid_is_powervu(curpid->CAID)) // ecm counter for powervu
|
---|
5374 | {
|
---|
5375 | pvu_skip = 1;
|
---|
5376 |
|
---|
5377 | if(sctlen - 11 > buffer[9])
|
---|
5378 | {
|
---|
5379 | if(buffer[11 + buffer[9]] > curpid->pvu_counter
|
---|
5380 | || (curpid->pvu_counter == 255 && buffer[11 + buffer[9]] == 0)
|
---|
5381 | || ((curpid->pvu_counter - buffer[11 + buffer[9]]) > 5))
|
---|
5382 | {
|
---|
5383 | curpid->pvu_counter = buffer[11 + buffer[9]];
|
---|
5384 | pvu_skip = 0;
|
---|
5385 | }
|
---|
5386 | }
|
---|
5387 | }
|
---|
5388 | #endif
|
---|
5389 | // wait for odd / even ecm change (only not for irdeto!)
|
---|
5390 | if((curpid->table == buffer[0] && !caid_is_irdeto(curpid->CAID)) || pvu_skip)
|
---|
5391 | {
|
---|
5392 | if(!(er = get_ecmtask()))
|
---|
5393 | {
|
---|
5394 | return;
|
---|
5395 | }
|
---|
5396 | er->srvid = demux[demux_id].program_number;
|
---|
5397 |
|
---|
5398 | #ifdef WITH_STAPI5
|
---|
5399 | cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
|
---|
5400 | #endif
|
---|
5401 | er->tsid = demux[demux_id].tsid;
|
---|
5402 | er->onid = demux[demux_id].onid;
|
---|
5403 | er->pmtpid = demux[demux_id].pmtpid;
|
---|
5404 | er->ens = demux[demux_id].ens;
|
---|
5405 | er->caid = curpid->CAID;
|
---|
5406 | er->pid = curpid->ECM_PID;
|
---|
5407 | er->prid = curpid->PROVID;
|
---|
5408 | er->vpid = curpid->VPID;
|
---|
5409 | er->ecmlen = sctlen;
|
---|
5410 | memcpy(er->ecm, buffer, er->ecmlen);
|
---|
5411 | chid = get_subid(er); // fetch chid or fake chid
|
---|
5412 | er->chid = chid;
|
---|
5413 | er->msgid = msgid;
|
---|
5414 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5415 | NULLFREE(er);
|
---|
5416 | return;
|
---|
5417 | }
|
---|
5418 |
|
---|
5419 | if(caid_is_irdeto(curpid->CAID))
|
---|
5420 | {
|
---|
5421 | // 80 70 39 53 04 05 00 88
|
---|
5422 | // 81 70 41 41 01 06 00 13 00 06 80 38 1F 52 93 D2
|
---|
5423 | //if(buffer[5]>20) return;
|
---|
5424 | if(curpid->irdeto_maxindex != buffer[5]) // 6, register max irdeto index
|
---|
5425 | {
|
---|
5426 | cs_log_dbg(D_DVBAPI, "Found %d IRDETO ECM CHIDs", buffer[5] + 1);
|
---|
5427 | curpid->irdeto_maxindex = buffer[5]; // numchids = 7 (0..6)
|
---|
5428 | }
|
---|
5429 | }
|
---|
5430 | }
|
---|
5431 |
|
---|
5432 | if(!(er = get_ecmtask()))
|
---|
5433 | {
|
---|
5434 | return;
|
---|
5435 | }
|
---|
5436 | er->srvid = demux[demux_id].program_number;
|
---|
5437 |
|
---|
5438 | #ifdef WITH_STAPI5
|
---|
5439 | cs_strncpy(er->dev_name, dev_list[demux[demux_id].dev_index].name, sizeof(dev_list[demux[demux_id].dev_index].name));
|
---|
5440 | #endif
|
---|
5441 |
|
---|
5442 | er->tsid = demux[demux_id].tsid;
|
---|
5443 | er->onid = demux[demux_id].onid;
|
---|
5444 | er->pmtpid = demux[demux_id].pmtpid;
|
---|
5445 | er->ens = demux[demux_id].ens;
|
---|
5446 | er->caid = curpid->CAID;
|
---|
5447 | er->pid = curpid->ECM_PID;
|
---|
5448 | er->prid = curpid->PROVID;
|
---|
5449 | er->vpid = curpid->VPID;
|
---|
5450 | er->ecmlen = sctlen;
|
---|
5451 | memcpy(er->ecm, buffer, er->ecmlen);
|
---|
5452 | er->msgid = msgid;
|
---|
5453 | chid = get_subid(er); // fetch chid or fake chid
|
---|
5454 | uint32_t fixedprovid = chk_provid(er->ecm, er->caid);
|
---|
5455 |
|
---|
5456 | if(fixedprovid && fixedprovid != er->prid)
|
---|
5457 | {
|
---|
5458 | cs_log_dbg(D_DVBAPI, "Fixing provid ecmpid %d from %06X -> %06X", pid, curpid->PROVID, fixedprovid);
|
---|
5459 | curpid->PROVID = fixedprovid;
|
---|
5460 |
|
---|
5461 | if(!USE_OPENXCAS)
|
---|
5462 | {
|
---|
5463 | cs_log_dbg(D_DVBAPI, "Fixing provid filter %d from %06X -> %06X",
|
---|
5464 | filter_num + 1, demux[demux_id].demux_fd[filter_num].provid, fixedprovid);
|
---|
5465 |
|
---|
5466 | demux[demux_id].demux_fd[filter_num].provid = fixedprovid;
|
---|
5467 | }
|
---|
5468 | cs_log_dbg(D_DVBAPI, "Fixing provid ecmrequest from %06X -> %06X", er->prid, fixedprovid);
|
---|
5469 | er->prid = fixedprovid;
|
---|
5470 | }
|
---|
5471 | er->chid = chid;
|
---|
5472 |
|
---|
5473 | // only used on receiver internal buffer overflow
|
---|
5474 | // to get quickly fresh ecm filterdata otherwise freezing!
|
---|
5475 | if(len == 0)
|
---|
5476 | {
|
---|
5477 | curpid->table = 0;
|
---|
5478 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5479 | NULLFREE(er);
|
---|
5480 | return;
|
---|
5481 | }
|
---|
5482 |
|
---|
5483 | if(caid_is_irdeto(curpid->CAID))
|
---|
5484 | {
|
---|
5485 | if(curpid->irdeto_curindex != buffer[4]) // old style wrong irdeto index
|
---|
5486 | {
|
---|
5487 | if(curpid->irdeto_curindex == 0xFE) // check if this ecmfilter just started up
|
---|
5488 | {
|
---|
5489 | // on startup set the current index to the irdeto index of the ecm
|
---|
5490 | curpid->irdeto_curindex = buffer[4];
|
---|
5491 | }
|
---|
5492 | else // we are already running and not interested in this ecm
|
---|
5493 | {
|
---|
5494 | if(curpid->table != buffer[0]) // fix for receivers not supporting section filtering
|
---|
5495 | {
|
---|
5496 | curpid->table = 0;
|
---|
5497 | }
|
---|
5498 |
|
---|
5499 | // set ecm filter to odd + even since
|
---|
5500 | // this ecm doesn't match with current irdeto index
|
---|
5501 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5502 |
|
---|
5503 | NULLFREE(er);
|
---|
5504 | return;
|
---|
5505 | }
|
---|
5506 | }
|
---|
5507 | else // fix for receivers not supporting section filtering
|
---|
5508 | {
|
---|
5509 | if(curpid->table == buffer[0])
|
---|
5510 | {
|
---|
5511 | NULLFREE(er);
|
---|
5512 | return;
|
---|
5513 | }
|
---|
5514 | }
|
---|
5515 | 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",
|
---|
5516 | demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->ecm[4], er->ecm[5], er->chid, curpid->irdeto_cycle, er->vpid);
|
---|
5517 | }
|
---|
5518 | else
|
---|
5519 | {
|
---|
5520 | cs_log_dbg(D_DVBAPI, "Demuxer %d ECMTYPE %02X CAID %04X PROVID %06X ECMPID %04X FAKECHID %04X (unique part in ecm)",
|
---|
5521 | demux_id, er->ecm[0], er->caid, er->prid, er->pid, er->chid);
|
---|
5522 | }
|
---|
5523 |
|
---|
5524 | // check for matching chid (unique ecm part in case of non-irdeto cas)
|
---|
5525 | // plus added fix for seca2 monthly changing fakechid
|
---|
5526 | if((curpid->CHID < 0x10000) && !((chid == curpid->CHID) || ((curpid->CAID >> 8 == 0x01) && (chid & 0xF0FF) == (curpid->CHID & 0xF0FF))))
|
---|
5527 | {
|
---|
5528 | if(caid_is_irdeto(curpid->CAID))
|
---|
5529 | {
|
---|
5530 | // if same: we cycled all indexes but no luck!
|
---|
5531 | if((curpid->irdeto_cycle < 0xFE) && (curpid->irdeto_cycle == curpid->irdeto_curindex))
|
---|
5532 | {
|
---|
5533 | struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
|
---|
5534 |
|
---|
5535 | // forced pid? keep trying the forced ecmpid, no force kill ecm filter
|
---|
5536 | if(!forceentry || !forceentry->force)
|
---|
5537 | {
|
---|
5538 | if(curpid->checked == 2)
|
---|
5539 | {
|
---|
5540 | curpid->checked = 4;
|
---|
5541 | }
|
---|
5542 |
|
---|
5543 | if(curpid->checked == 1)
|
---|
5544 | {
|
---|
5545 | curpid->checked = 2;
|
---|
5546 | curpid->CHID = 0x10000;
|
---|
5547 | }
|
---|
5548 |
|
---|
5549 | dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
|
---|
5550 | NULLFREE(er);
|
---|
5551 | return;
|
---|
5552 | }
|
---|
5553 | }
|
---|
5554 | curpid->irdeto_curindex++; // set check on next index
|
---|
5555 |
|
---|
5556 | if(curpid->irdeto_cycle == 0xFE)
|
---|
5557 | {
|
---|
5558 | curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
|
---|
5559 | }
|
---|
5560 |
|
---|
5561 | if(curpid->irdeto_curindex > curpid->irdeto_maxindex)
|
---|
5562 | {
|
---|
5563 | curpid->irdeto_curindex = 0; // check if we reached max irdeto index, if so reset to 0
|
---|
5564 | }
|
---|
5565 | curpid->table = 0;
|
---|
5566 |
|
---|
5567 | // set ecm filter to odd + even since
|
---|
5568 | // this ecm doesn't match with current irdeto index
|
---|
5569 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5570 |
|
---|
5571 | NULLFREE(er);
|
---|
5572 | return;
|
---|
5573 | }
|
---|
5574 | else // all non irdeto cas systems
|
---|
5575 | {
|
---|
5576 | struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
|
---|
5577 | curpid->table = 0;
|
---|
5578 |
|
---|
5579 | // set ecm filter to odd + even since
|
---|
5580 | // this ecm doesn't match with current irdeto index
|
---|
5581 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5582 |
|
---|
5583 | if(forceentry && forceentry->force)
|
---|
5584 | {
|
---|
5585 | NULLFREE(er);
|
---|
5586 | return; // forced pid? keep trying the forced ecmpid!
|
---|
5587 | }
|
---|
5588 |
|
---|
5589 | if(curpid->checked == 2)
|
---|
5590 | {
|
---|
5591 | curpid->checked = 4;
|
---|
5592 | }
|
---|
5593 |
|
---|
5594 | if(curpid->checked == 1)
|
---|
5595 | {
|
---|
5596 | curpid->checked = 2;
|
---|
5597 | curpid->CHID = 0x10000;
|
---|
5598 | }
|
---|
5599 |
|
---|
5600 | dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
|
---|
5601 | NULLFREE(er);
|
---|
5602 | return;
|
---|
5603 | }
|
---|
5604 | }
|
---|
5605 |
|
---|
5606 | struct s_dvbapi_priority *p;
|
---|
5607 | for(p = dvbapi_priority; p != NULL; p = p->next)
|
---|
5608 | {
|
---|
5609 | if(p->type != 'l'
|
---|
5610 | || (p->caid && p->caid != curpid->CAID)
|
---|
5611 | || (p->provid && p->provid != curpid->PROVID)
|
---|
5612 | || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
|
---|
5613 | || (p->srvid && p->srvid != demux[demux_id].program_number))
|
---|
5614 | {
|
---|
5615 | continue;
|
---|
5616 | }
|
---|
5617 |
|
---|
5618 | if((uint)p->delay == sctlen && p->force < 6)
|
---|
5619 | {
|
---|
5620 | p->force++;
|
---|
5621 | NULLFREE(er);
|
---|
5622 | return;
|
---|
5623 | }
|
---|
5624 |
|
---|
5625 | if(p->force >= 6)
|
---|
5626 | {
|
---|
5627 | p->force = 0;
|
---|
5628 | }
|
---|
5629 | }
|
---|
5630 |
|
---|
5631 | if(!curpid->PROVID)
|
---|
5632 | {
|
---|
5633 | curpid->PROVID = chk_provid(buffer, curpid->CAID);
|
---|
5634 | }
|
---|
5635 |
|
---|
5636 | if(caid_is_irdeto(curpid->CAID)) // irdeto: wait for the correct index
|
---|
5637 | {
|
---|
5638 | if(buffer[4] != curpid->irdeto_curindex)
|
---|
5639 | {
|
---|
5640 | curpid->table = 0;
|
---|
5641 |
|
---|
5642 | // set ecm filter to odd + even since
|
---|
5643 | // this ecm doesn't match with current irdeto index
|
---|
5644 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5645 |
|
---|
5646 | NULLFREE(er);
|
---|
5647 | return;
|
---|
5648 | }
|
---|
5649 | }
|
---|
5650 |
|
---|
5651 | // we have an ecm with the correct irdeto index (or fakechid)
|
---|
5652 | for(p = dvbapi_priority; p != NULL ; p = p->next) // check for ignore!
|
---|
5653 | {
|
---|
5654 | if((p->type != 'i')
|
---|
5655 | || (p->caid && p->caid != curpid->CAID)
|
---|
5656 | || (p->provid && p->provid != curpid->PROVID)
|
---|
5657 | || (p->ecmpid && p->ecmpid != curpid->ECM_PID)
|
---|
5658 | || (p->pidx && p->pidx - 1 != pid)
|
---|
5659 | || (p->srvid && p->srvid != demux[demux_id].program_number))
|
---|
5660 | {
|
---|
5661 | continue;
|
---|
5662 | }
|
---|
5663 |
|
---|
5664 | // found an ignore chid match with current ecm -> ignoring this irdeto index
|
---|
5665 | if(p->type == 'i' && (p->chid < 0x10000 && p->chid == chid))
|
---|
5666 | {
|
---|
5667 | curpid->irdeto_curindex++;
|
---|
5668 | if(curpid->irdeto_cycle == 0xFE)
|
---|
5669 | {
|
---|
5670 | curpid->irdeto_cycle = buffer[4]; // on startup set to current irdeto index
|
---|
5671 | }
|
---|
5672 |
|
---|
5673 | if(curpid->irdeto_curindex > curpid->irdeto_maxindex) // check if curindex is over the max
|
---|
5674 | {
|
---|
5675 | curpid->irdeto_curindex = 0;
|
---|
5676 | }
|
---|
5677 | curpid->table = 0;
|
---|
5678 |
|
---|
5679 | // irdeto: wait for the correct index + check if we cycled all
|
---|
5680 | if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_cycle != curpid->irdeto_curindex))
|
---|
5681 | {
|
---|
5682 | // set ecm filter to odd + even since this chid has to be ignored!
|
---|
5683 | dvbapi_set_section_filter(demux_id, er, filter_num);
|
---|
5684 | }
|
---|
5685 | else // this fakechid has to be ignored, kill this filter!
|
---|
5686 | {
|
---|
5687 | if(curpid->checked == 2)
|
---|
5688 | {
|
---|
5689 | curpid->checked = 4;
|
---|
5690 | }
|
---|
5691 |
|
---|
5692 | if(curpid->checked == 1)
|
---|
5693 | {
|
---|
5694 | curpid->checked = 2;
|
---|
5695 | curpid->CHID = 0x10000;
|
---|
5696 | }
|
---|
5697 |
|
---|
5698 | dvbapi_stop_filternum(demux_id, filter_num, msgid); // stop this ecm filter!
|
---|
5699 | }
|
---|
5700 | NULLFREE(er);
|
---|
5701 | return;
|
---|
5702 | }
|
---|
5703 | }
|
---|
5704 |
|
---|
5705 | if(er)
|
---|
5706 | {
|
---|
5707 | curpid->table = er->ecm[0];
|
---|
5708 | }
|
---|
5709 |
|
---|
5710 | request_cw(dvbapi_client, er, demux_id, 1); // register this ecm for delayed ecm response check
|
---|
5711 | return; // end of ecm filterhandling!
|
---|
5712 | }
|
---|
5713 |
|
---|
5714 | if(filtertype == TYPE_EMM)
|
---|
5715 | {
|
---|
5716 | if(len != 0) // len = 0 receiver encountered an internal buffer overflow!
|
---|
5717 | {
|
---|
5718 | cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d Filter %d fetched EMM data (emmlength = 0x%03X):",
|
---|
5719 | demux_id, filter_num + 1, sctlen);
|
---|
5720 |
|
---|
5721 | if(sctlen > MAX_EMM_SIZE) // emm too long to handle!
|
---|
5722 | {
|
---|
5723 | cs_log_dbg(D_DVBAPI, "Received data with total length 0x%03X but max supported EMM length is 0x%03X -> Please report!",
|
---|
5724 | sctlen, MAX_EMM_SIZE);
|
---|
5725 |
|
---|
5726 | return;
|
---|
5727 | }
|
---|
5728 | }
|
---|
5729 | else
|
---|
5730 | {
|
---|
5731 | return; // just skip on internal buffer overflow
|
---|
5732 | }
|
---|
5733 |
|
---|
5734 | if(demux[demux_id].demux_fd[filter_num].pid == 0x01) // CAT
|
---|
5735 | {
|
---|
5736 | cs_log_dbg(D_DVBAPI, "receiving cat");
|
---|
5737 | dvbapi_parse_cat(demux_id, buffer, sctlen);
|
---|
5738 | dvbapi_stop_filternum(demux_id, filter_num, msgid);
|
---|
5739 | return;
|
---|
5740 | }
|
---|
5741 |
|
---|
5742 | #ifdef WITH_EMU
|
---|
5743 | if(caid_is_director(demux[demux_id].demux_fd[filter_num].caid))
|
---|
5744 | {
|
---|
5745 | uint32_t i;
|
---|
5746 | uint32_t emmhash;
|
---|
5747 |
|
---|
5748 | if(sctlen < 4)
|
---|
5749 | {
|
---|
5750 | return;
|
---|
5751 | }
|
---|
5752 |
|
---|
5753 | for(i = 0; i + 2 < sctlen; i++)
|
---|
5754 | {
|
---|
5755 | if(buffer[i] == 0xF0 && (buffer[i + 2] == 0xE1 || buffer[i + 2] == 0xE4))
|
---|
5756 | {
|
---|
5757 | emmhash = (buffer[3] << 8) | buffer[sctlen - 2];
|
---|
5758 | if(demux[demux_id].demux_fd[filter_num].cadata == emmhash)
|
---|
5759 | {
|
---|
5760 | return;
|
---|
5761 | }
|
---|
5762 |
|
---|
5763 | demux[demux_id].demux_fd[filter_num].cadata = emmhash;
|
---|
5764 | dvbapi_process_emm(demux_id, filter_num, buffer, sctlen);
|
---|
5765 | return;
|
---|
5766 | }
|
---|
5767 | }
|
---|
5768 | return;
|
---|
5769 | }
|
---|
5770 | #endif
|
---|
5771 | // fix to handle more than one irdeto emm packet
|
---|
5772 | uint8_t *pbuf = buffer;
|
---|
5773 | int32_t done = 0;
|
---|
5774 | int32_t unhandled = len;
|
---|
5775 |
|
---|
5776 | while(len > done)
|
---|
5777 | {
|
---|
5778 | pbuf += done;
|
---|
5779 | sctlen = SCT_LEN(pbuf);
|
---|
5780 |
|
---|
5781 | if(unhandled < 4 || (int32_t)sctlen > unhandled || sctlen > MAX_EMM_SIZE)
|
---|
5782 | {
|
---|
5783 | break;
|
---|
5784 | }
|
---|
5785 |
|
---|
5786 | dvbapi_process_emm(demux_id, filter_num, pbuf, sctlen);
|
---|
5787 | done += sctlen;
|
---|
5788 | unhandled -= sctlen;
|
---|
5789 | }
|
---|
5790 | }
|
---|
5791 |
|
---|
5792 | if(filtertype == TYPE_SDT)
|
---|
5793 | {
|
---|
5794 | cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d Filter %d fetched SDT data (length = 0x%03X):",
|
---|
5795 | demux_id, filter_num + 1, sctlen);
|
---|
5796 |
|
---|
5797 | dvbapi_parse_sdt(demux_id, buffer, sctlen, msgid);
|
---|
5798 | }
|
---|
5799 |
|
---|
5800 | if(filtertype == TYPE_PAT)
|
---|
5801 | {
|
---|
5802 | cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d Filter %d fetched PAT data (length = 0x%03X):",
|
---|
5803 | demux_id, filter_num + 1, sctlen);
|
---|
5804 |
|
---|
5805 | dvbapi_parse_pat(demux_id, buffer, sctlen, msgid);
|
---|
5806 | }
|
---|
5807 |
|
---|
5808 | if(filtertype == TYPE_PMT)
|
---|
5809 | {
|
---|
5810 | cs_log_dump_dbg(D_DVBAPI, buffer, sctlen, "Demuxer %d Filter %d fetched PMT data (length = 0x%03X):",
|
---|
5811 | demux_id, filter_num + 1, sctlen);
|
---|
5812 |
|
---|
5813 | dvbapi_parse_capmt(buffer, sctlen, demux[demux_id].socket_fd, demux[demux_id].pmt_file, 1, demux_id, demux[demux_id].client_proto_version, msgid);
|
---|
5814 | }
|
---|
5815 | }
|
---|
5816 |
|
---|
5817 | static int32_t dvbapi_recv(int32_t connfd, uint8_t *mbuf, size_t rlen)
|
---|
5818 | {
|
---|
5819 | ssize_t len = cs_recv(connfd, mbuf, rlen, MSG_DONTWAIT);
|
---|
5820 |
|
---|
5821 | if((len == -1 && (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) || (len == 0))
|
---|
5822 | {
|
---|
5823 | return -1;
|
---|
5824 | }
|
---|
5825 |
|
---|
5826 | if(len == -1)
|
---|
5827 | {
|
---|
5828 | return 0;
|
---|
5829 | }
|
---|
5830 |
|
---|
5831 | return len;
|
---|
5832 | }
|
---|
5833 |
|
---|
5834 | static uint16_t dvbapi_get_nbof_missing_header_bytes(uint8_t *mbuf, uint16_t mbuf_len)
|
---|
5835 | {
|
---|
5836 | if(mbuf_len < 4)
|
---|
5837 | {
|
---|
5838 | return 4 - mbuf_len;
|
---|
5839 | }
|
---|
5840 | uint32_t opcode = b2i(4, mbuf); //get the client opcode (4 bytes)
|
---|
5841 |
|
---|
5842 | // detect the opcode, its size (chunksize) and its internal data size (data_len)
|
---|
5843 | if((opcode & 0xFFFFF000) == DVBAPI_AOT_CA) // min 4+size bytes
|
---|
5844 | {
|
---|
5845 | if(mbuf[3] & 0x80)
|
---|
5846 | {
|
---|
5847 | uint32_t size = mbuf[3] & 0x7F;
|
---|
5848 | if(mbuf_len < (4 + size))
|
---|
5849 | {
|
---|
5850 | return (4 + size) - mbuf_len;
|
---|
5851 | }
|
---|
5852 | }
|
---|
5853 | return 0;
|
---|
5854 | }
|
---|
5855 | else
|
---|
5856 | {
|
---|
5857 | switch (opcode)
|
---|
5858 | {
|
---|
5859 | case DVBAPI_FILTER_DATA: // min 9 bytes
|
---|
5860 | if(mbuf_len < 9)
|
---|
5861 | {
|
---|
5862 | return 9 - mbuf_len;
|
---|
5863 | }
|
---|
5864 | return 0;
|
---|
5865 |
|
---|
5866 | case DVBAPI_CLIENT_INFO: // min 7 bytes
|
---|
5867 | if(mbuf_len < 7)
|
---|
5868 | {
|
---|
5869 | return 7 - mbuf_len;
|
---|
5870 | }
|
---|
5871 | return 0;
|
---|
5872 |
|
---|
5873 | default:
|
---|
5874 | return 0;
|
---|
5875 | }
|
---|
5876 | }
|
---|
5877 | }
|
---|
5878 |
|
---|
5879 | static void dvbapi_get_packet_size(uint8_t *mbuf, uint16_t mbuf_len, uint16_t *chunksize, uint16_t *data_len, uint16_t client_proto_version)
|
---|
5880 | {
|
---|
5881 | //chunksize: size of complete chunk in the buffer (an opcode with the data)
|
---|
5882 | //data_len: variable for internal data length (eg. for the filter data size, PMT len)
|
---|
5883 | uint32_t msgid_size = 0;
|
---|
5884 | (*chunksize) = 0;
|
---|
5885 | (*data_len) = 0;
|
---|
5886 |
|
---|
5887 | if(client_proto_version >= 3)
|
---|
5888 | {
|
---|
5889 | msgid_size = 5;
|
---|
5890 | }
|
---|
5891 |
|
---|
5892 | if(mbuf_len < 4 + msgid_size)
|
---|
5893 | {
|
---|
5894 | cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short", mbuf_len);
|
---|
5895 | (*chunksize) = 1;
|
---|
5896 | (*data_len) = 1;
|
---|
5897 | return;
|
---|
5898 | }
|
---|
5899 |
|
---|
5900 | mbuf += msgid_size;
|
---|
5901 | uint32_t opcode = b2i(4, mbuf); //get the client opcode (4 bytes)
|
---|
5902 |
|
---|
5903 | //detect the opcode, its size (chunksize) and its internal data size (data_len)
|
---|
5904 | if((opcode & 0xFFFFF000) == DVBAPI_AOT_CA) // min 4+size bytes
|
---|
5905 | {
|
---|
5906 | // parse packet size (ASN.1)
|
---|
5907 | uint32_t size = 0;
|
---|
5908 | if(mbuf[3] & 0x80)
|
---|
5909 | {
|
---|
5910 | uint32_t tmp_data_len = 0;
|
---|
5911 | size = mbuf[3] & 0x7F;
|
---|
5912 | if(3 + size < mbuf_len)
|
---|
5913 | {
|
---|
5914 | uint32_t k;
|
---|
5915 | for(k = 0; k < size; k++)
|
---|
5916 | {
|
---|
5917 | tmp_data_len = (tmp_data_len << 8) | mbuf[4 + k];
|
---|
5918 | }
|
---|
5919 | }
|
---|
5920 | else
|
---|
5921 | {
|
---|
5922 | cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short for opcode %08X", mbuf_len, opcode);
|
---|
5923 | (*chunksize) = 1;
|
---|
5924 | (*data_len) = 1;
|
---|
5925 | return;
|
---|
5926 | }
|
---|
5927 |
|
---|
5928 | if(tmp_data_len > 0xFFFF)
|
---|
5929 | {
|
---|
5930 | cs_log("Socket command too big: %d bytes", tmp_data_len);
|
---|
5931 | (*data_len) = 0xFFFF - 4 - size;
|
---|
5932 | }
|
---|
5933 | else
|
---|
5934 | {
|
---|
5935 | (*data_len) = tmp_data_len;
|
---|
5936 | }
|
---|
5937 | }
|
---|
5938 | else
|
---|
5939 | {
|
---|
5940 | (*data_len) = mbuf[3] & 0x7F;
|
---|
5941 | }
|
---|
5942 |
|
---|
5943 | (*chunksize) = 4 + size + (*data_len);
|
---|
5944 | cs_log_dbg(D_DVBAPI, "Got packet with opcode %08X and size %" PRIu16, opcode, (*chunksize));
|
---|
5945 | }
|
---|
5946 | else
|
---|
5947 | {
|
---|
5948 | switch (opcode)
|
---|
5949 | {
|
---|
5950 | case DVBAPI_FILTER_DATA: // min 9 bytes
|
---|
5951 | {
|
---|
5952 | if(mbuf_len < 9)
|
---|
5953 | {
|
---|
5954 | cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short for DVBAPI_FILTER_DATA", mbuf_len);
|
---|
5955 | (*chunksize) = 1;
|
---|
5956 | (*data_len) = 1;
|
---|
5957 | return;
|
---|
5958 | }
|
---|
5959 | (*data_len) = b2i(2, mbuf + 7) & 0x0FFF;
|
---|
5960 | (*chunksize) = 6 + 3 + (*data_len);
|
---|
5961 | cs_log_dbg(D_DVBAPI, "Got DVBAPI_FILTER_DATA packet with size %" PRIu16, (*chunksize));
|
---|
5962 | break;
|
---|
5963 | }
|
---|
5964 |
|
---|
5965 | case DVBAPI_CLIENT_INFO: // min 7 bytes
|
---|
5966 | {
|
---|
5967 | if(mbuf_len < 7)
|
---|
5968 | {
|
---|
5969 | cs_log("dvbapi_get_packet_size(): error - buffer length (%" PRIu16 ") too short for DVBAPI_CLIENT_INFO", mbuf_len);
|
---|
5970 | (*chunksize) = 1;
|
---|
5971 | (*data_len) = 1;
|
---|
5972 | return;
|
---|
5973 | }
|
---|
5974 | (*data_len) = mbuf[6];
|
---|
5975 | (*chunksize) = 6 + 1 + (*data_len);
|
---|
5976 | cs_log_dbg(D_DVBAPI, "Got DVBAPI_CLIENT_INFO packet with size %" PRIu16, (*chunksize));
|
---|
5977 | break;
|
---|
5978 | }
|
---|
5979 |
|
---|
5980 | default:
|
---|
5981 | {
|
---|
5982 | cs_log("Unknown socket command received: 0x%08X", opcode);
|
---|
5983 | (*chunksize) = 1;
|
---|
5984 | (*data_len) = 1;
|
---|
5985 | break;
|
---|
5986 | }
|
---|
5987 | }
|
---|
5988 | }
|
---|
5989 |
|
---|
5990 | if((*chunksize) < 1)
|
---|
5991 | {
|
---|
5992 | (*chunksize) = 1;
|
---|
5993 | (*data_len) = 1;
|
---|
5994 | }
|
---|
5995 |
|
---|
5996 | if((*chunksize) > 1)
|
---|
5997 | {
|
---|
5998 | (*chunksize) += msgid_size;
|
---|
5999 | }
|
---|
6000 | }
|
---|
6001 |
|
---|
6002 | static void dvbapi_handlesockmsg(uint8_t *mbuf, uint16_t chunksize, uint16_t data_len, uint8_t *add_to_poll, int32_t connfd, uint16_t *client_proto_version)
|
---|
6003 | {
|
---|
6004 | uint32_t msgid = 0;
|
---|
6005 | if(*client_proto_version >= 3)
|
---|
6006 | {
|
---|
6007 | if(mbuf[0] != 0xa5)
|
---|
6008 | {
|
---|
6009 | cs_log("Error: network packet malformed! (no start)");
|
---|
6010 | return;
|
---|
6011 | }
|
---|
6012 | msgid = b2i(4, mbuf + 1);
|
---|
6013 | mbuf += 5;
|
---|
6014 | }
|
---|
6015 |
|
---|
6016 | uint32_t opcode = b2i(4, mbuf); //get the client opcode (4 bytes)
|
---|
6017 | if((opcode & 0xFFFFF000) == DVBAPI_AOT_CA)
|
---|
6018 | {
|
---|
6019 | switch(opcode & 0xFFFFFF00)
|
---|
6020 | {
|
---|
6021 | case DVBAPI_AOT_CA_PMT:
|
---|
6022 | {
|
---|
6023 | if(data_len < 5)
|
---|
6024 | {
|
---|
6025 | cs_log("Error: packet DVBAPI_AOT_CA_PMT is too short!");
|
---|
6026 | break;
|
---|
6027 | }
|
---|
6028 | cs_log_dbg(D_DVBAPI, "PMT Update on socket %d.", connfd);
|
---|
6029 | cs_log_dump_dbg(D_DVBAPI, mbuf, chunksize, "Parsing PMT object:");
|
---|
6030 | dvbapi_parse_capmt(mbuf + (chunksize - data_len), data_len, connfd, NULL, 0, 0, *client_proto_version, msgid);
|
---|
6031 | break;
|
---|
6032 | }
|
---|
6033 |
|
---|
6034 | case (DVBAPI_AOT_CA_STOP & 0xFFFFFF00):
|
---|
6035 | {
|
---|
6036 | // 9F 80 3f 04 83 02 00 <demux index>
|
---|
6037 | if(opcode != DVBAPI_AOT_CA_STOP)
|
---|
6038 | {
|
---|
6039 | cs_log_dbg(D_DVBAPI, "dvbapi_handlesockmsg(): DVBAPI_AOT_CA opcode unknown: %08X", opcode);
|
---|
6040 | break;
|
---|
6041 | }
|
---|
6042 |
|
---|
6043 | int32_t i;
|
---|
6044 |
|
---|
6045 | if(data_len < 4)
|
---|
6046 | {
|
---|
6047 | cs_log("Error: packet DVBAPI_AOT_CA_STOP is too short!");
|
---|
6048 | break;
|
---|
6049 | }
|
---|
6050 |
|
---|
6051 | // ipbox fix
|
---|
6052 | if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_listenport)
|
---|
6053 | {
|
---|
6054 | int32_t demux_index = mbuf[7];
|
---|
6055 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
6056 | {
|
---|
6057 | // 0xff demux_index is a wildcard => close all related demuxers
|
---|
6058 | if(demux_index == 0xff)
|
---|
6059 | {
|
---|
6060 | if(demux[i].socket_fd == connfd)
|
---|
6061 | {
|
---|
6062 | dvbapi_stop_descrambling(i, msgid);
|
---|
6063 | }
|
---|
6064 | }
|
---|
6065 | else if(demux[i].demux_index == demux_index)
|
---|
6066 | {
|
---|
6067 | dvbapi_stop_descrambling(i, msgid);
|
---|
6068 | break;
|
---|
6069 | }
|
---|
6070 | }
|
---|
6071 |
|
---|
6072 | if(cfg.dvbapi_boxtype == BOXTYPE_IPBOX)
|
---|
6073 | {
|
---|
6074 | // check do we have any demux running on this fd
|
---|
6075 | int16_t execlose = 1;
|
---|
6076 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
6077 | {
|
---|
6078 | if(demux[i].socket_fd == connfd)
|
---|
6079 | {
|
---|
6080 | execlose = 0;
|
---|
6081 | break;
|
---|
6082 | }
|
---|
6083 | }
|
---|
6084 |
|
---|
6085 | if(execlose)
|
---|
6086 | {
|
---|
6087 | int32_t ret = close(connfd);
|
---|
6088 | if(ret < 0)
|
---|
6089 | {
|
---|
6090 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
6091 | }
|
---|
6092 | }
|
---|
6093 | }
|
---|
6094 | }
|
---|
6095 | else
|
---|
6096 | {
|
---|
6097 | if(cfg.dvbapi_pmtmode != 6)
|
---|
6098 | {
|
---|
6099 | int32_t ret = close(connfd);
|
---|
6100 | if(ret < 0)
|
---|
6101 | {
|
---|
6102 | cs_log("ERROR: Could not close PMT fd (errno=%d %s)", errno, strerror(errno));
|
---|
6103 | }
|
---|
6104 | }
|
---|
6105 | }
|
---|
6106 | break;
|
---|
6107 | }
|
---|
6108 |
|
---|
6109 | default:
|
---|
6110 | {
|
---|
6111 | cs_log_dbg(D_DVBAPI, "dvbapi_handlesockmsg(): DVBAPI_AOT_CA opcode unknown: %08X", opcode);
|
---|
6112 | break;
|
---|
6113 | }
|
---|
6114 | }
|
---|
6115 |
|
---|
6116 | if(cfg.dvbapi_listenport && opcode == DVBAPI_AOT_CA_STOP)
|
---|
6117 | {
|
---|
6118 | (*add_to_poll) = 1;
|
---|
6119 | }
|
---|
6120 | else
|
---|
6121 | {
|
---|
6122 | (*add_to_poll) = 0;
|
---|
6123 | }
|
---|
6124 | }
|
---|
6125 | else
|
---|
6126 | {
|
---|
6127 | switch(opcode)
|
---|
6128 | {
|
---|
6129 | case DVBAPI_FILTER_DATA:
|
---|
6130 | {
|
---|
6131 | if(data_len < 1)
|
---|
6132 | {
|
---|
6133 | cs_log("Error: packet DVBAPI_FILTER_DATA is too short!");
|
---|
6134 | break;
|
---|
6135 | }
|
---|
6136 |
|
---|
6137 | int32_t demux_id = mbuf[4];
|
---|
6138 | int32_t filter_num = mbuf[5];
|
---|
6139 |
|
---|
6140 | if(demux_id < 0 || demux_id >= MAX_DEMUX)
|
---|
6141 | {
|
---|
6142 | cs_log("dvbapi_handlesockmsg(): error - received invalid demux_id (%d)", demux_id);
|
---|
6143 | break;
|
---|
6144 | }
|
---|
6145 |
|
---|
6146 | if(filter_num < 0 || filter_num >= MAX_FILTER)
|
---|
6147 | {
|
---|
6148 | cs_log("dvbapi_handlesockmsg(): error - received invalid filter_num (%d)", filter_num);
|
---|
6149 | break;
|
---|
6150 | }
|
---|
6151 | dvbapi_process_input(demux_id, filter_num, mbuf + 6, data_len + 3, msgid);
|
---|
6152 | break;
|
---|
6153 | }
|
---|
6154 |
|
---|
6155 | case DVBAPI_CLIENT_INFO:
|
---|
6156 | {
|
---|
6157 | uint16_t client_proto = b2i(2, mbuf + 4);
|
---|
6158 | NULLFREE(last_client_name);
|
---|
6159 |
|
---|
6160 | if(cs_malloc(&last_client_name, data_len + 1))
|
---|
6161 | {
|
---|
6162 | memcpy(last_client_name, &mbuf[7], data_len);
|
---|
6163 | last_client_name[data_len] = 0;
|
---|
6164 | cs_log("Client connected: '%s' (protocol version = %" PRIu16 ")", last_client_name, client_proto);
|
---|
6165 | }
|
---|
6166 |
|
---|
6167 | (*client_proto_version) = client_proto; // setting the global var according to the client
|
---|
6168 | last_client_proto_version = client_proto;
|
---|
6169 |
|
---|
6170 | // as a response we are sending our info to the client:
|
---|
6171 | dvbapi_net_send(DVBAPI_SERVER_INFO, connfd, msgid, -1, -1, NULL, NULL, NULL, client_proto);
|
---|
6172 | break;
|
---|
6173 | }
|
---|
6174 |
|
---|
6175 | default:
|
---|
6176 | {
|
---|
6177 | cs_log_dbg(D_DVBAPI, "dvbapi_handlesockmsg(): opcode unknown: %08X", opcode);
|
---|
6178 | cs_log_dump(mbuf, chunksize, "Unknown command:");
|
---|
6179 | break;
|
---|
6180 | }
|
---|
6181 | }
|
---|
6182 | }
|
---|
6183 | }
|
---|
6184 |
|
---|
6185 | static bool dvbapi_handlesockdata(int32_t connfd, uint8_t *mbuf, uint16_t mbuf_size, uint16_t unhandled_len,
|
---|
6186 | uint8_t *add_to_poll, uint16_t *new_unhandled_len, uint16_t *client_proto_version)
|
---|
6187 | {
|
---|
6188 | int32_t recv_result;
|
---|
6189 | uint16_t chunksize = 1, data_len = 1;
|
---|
6190 | uint8_t packet_count = 0;
|
---|
6191 | uint16_t missing_header_bytes = dvbapi_get_nbof_missing_header_bytes(mbuf, unhandled_len);
|
---|
6192 |
|
---|
6193 | if(missing_header_bytes)
|
---|
6194 | {
|
---|
6195 | // read first few bytes so we know packet type and length
|
---|
6196 | cs_log_dbg(D_TRACE, "%s reading %" PRIu16 " bytes from connection fd %d",
|
---|
6197 | (unhandled_len == 0) ? "Try" : "Continue", missing_header_bytes, connfd);
|
---|
6198 |
|
---|
6199 | recv_result = dvbapi_recv(connfd, mbuf + unhandled_len, mbuf_size - unhandled_len);
|
---|
6200 | if(recv_result < 1)
|
---|
6201 | {
|
---|
6202 | (*new_unhandled_len) = unhandled_len;
|
---|
6203 | return (recv_result != -1);
|
---|
6204 | }
|
---|
6205 | else
|
---|
6206 | {
|
---|
6207 | unhandled_len += recv_result;
|
---|
6208 | if(unhandled_len < dvbapi_get_nbof_missing_header_bytes(mbuf, unhandled_len))
|
---|
6209 | {
|
---|
6210 | (*new_unhandled_len) = unhandled_len;
|
---|
6211 | return true;
|
---|
6212 | }
|
---|
6213 | }
|
---|
6214 | }
|
---|
6215 |
|
---|
6216 | do
|
---|
6217 | {
|
---|
6218 | // we got at least the first few bytes, detect packet type and length, then read the missing bytes
|
---|
6219 | dvbapi_get_packet_size(mbuf, unhandled_len, &chunksize, &data_len, *client_proto_version);
|
---|
6220 | if(chunksize > mbuf_size)
|
---|
6221 | {
|
---|
6222 | cs_log("***** WARNING: SOCKET DATA BUFFER OVERFLOW (%" PRIu16 " bytes), PLEASE REPORT! ****** ", chunksize);
|
---|
6223 | (*new_unhandled_len) = 0;
|
---|
6224 | return true;
|
---|
6225 | }
|
---|
6226 |
|
---|
6227 | if(unhandled_len < chunksize) // we are missing some bytes, try to read them
|
---|
6228 | {
|
---|
6229 | cs_log_dbg(D_TRACE, "Continue to read %d bytes from connection fd %d", chunksize - unhandled_len, connfd);
|
---|
6230 | recv_result = dvbapi_recv(connfd, mbuf + unhandled_len, mbuf_size - unhandled_len);
|
---|
6231 | if(recv_result < 1)
|
---|
6232 | {
|
---|
6233 | (*new_unhandled_len) = unhandled_len;
|
---|
6234 | return (recv_result != -1);
|
---|
6235 | }
|
---|
6236 | else
|
---|
6237 | {
|
---|
6238 | unhandled_len += recv_result;
|
---|
6239 | if(unhandled_len < chunksize)
|
---|
6240 | {
|
---|
6241 | (*new_unhandled_len) = unhandled_len;
|
---|
6242 | return true;
|
---|
6243 | }
|
---|
6244 | }
|
---|
6245 | }
|
---|
6246 |
|
---|
6247 | // we got at least one full packet, handle it, then return
|
---|
6248 | dvbapi_handlesockmsg(mbuf, chunksize, data_len, add_to_poll, connfd, client_proto_version);
|
---|
6249 |
|
---|
6250 | unhandled_len -= chunksize;
|
---|
6251 | if(unhandled_len > 0)
|
---|
6252 | {
|
---|
6253 | memmove(mbuf, mbuf + chunksize, unhandled_len);
|
---|
6254 | }
|
---|
6255 | packet_count++;
|
---|
6256 | } while(dvbapi_get_nbof_missing_header_bytes(mbuf, unhandled_len) == 0 && packet_count < 7);
|
---|
6257 |
|
---|
6258 | (*new_unhandled_len) = unhandled_len;
|
---|
6259 | return true;
|
---|
6260 | }
|
---|
6261 |
|
---|
6262 | static void *dvbapi_main_local(void *cli)
|
---|
6263 | {
|
---|
6264 | int32_t i, j, l;
|
---|
6265 | struct s_client *client = (struct s_client *)cli;
|
---|
6266 | client->thread = pthread_self();
|
---|
6267 | SAFE_SETSPECIFIC(getclient, cli);
|
---|
6268 | dvbapi_client = cli;
|
---|
6269 | int32_t maxpfdsize = (MAX_DEMUX * maxfilter) + MAX_DEMUX + 2;
|
---|
6270 | struct pollfd pfd2[maxpfdsize];
|
---|
6271 | struct timeb start, end; // start time poll, end time poll
|
---|
6272 | #define PMT_SERVER_SOCKET "/tmp/.listen.camd.socket"
|
---|
6273 | struct sockaddr_un saddr;
|
---|
6274 | saddr.sun_family = AF_UNIX;
|
---|
6275 | cs_strncpy(saddr.sun_path, PMT_SERVER_SOCKET, sizeof(saddr.sun_path));
|
---|
6276 | int32_t rc, pfdcount, g, connfd, clilen;
|
---|
6277 | int32_t ids[maxpfdsize], fdn[maxpfdsize], type[maxpfdsize];
|
---|
6278 | struct SOCKADDR servaddr;
|
---|
6279 | ssize_t len = 0;
|
---|
6280 | static const uint16_t mbuf_size = 2048;
|
---|
6281 | uint8_t *mbuf;
|
---|
6282 | uint16_t unhandled_buf_len[maxpfdsize], unhandled_buf_used[maxpfdsize];
|
---|
6283 | uint8_t *unhandled_buf[maxpfdsize];
|
---|
6284 | struct s_auth *account;
|
---|
6285 | int32_t ok = 0;
|
---|
6286 | uint16_t client_proto_version[maxpfdsize];
|
---|
6287 |
|
---|
6288 | if(!cs_malloc(&mbuf, sizeof(uint8_t) * mbuf_size))
|
---|
6289 | {
|
---|
6290 | return NULL;
|
---|
6291 | }
|
---|
6292 |
|
---|
6293 | for(i = 0; i < maxpfdsize; i++)
|
---|
6294 | {
|
---|
6295 | unhandled_buf[i] = NULL;
|
---|
6296 | unhandled_buf_len[i] = 0;
|
---|
6297 | unhandled_buf_used[i] = 0;
|
---|
6298 | client_proto_version[i] = 0;
|
---|
6299 | }
|
---|
6300 |
|
---|
6301 | for(account = cfg.account; account != NULL; account = account->next)
|
---|
6302 | {
|
---|
6303 | if((ok = is_dvbapi_usr(account->usr)))
|
---|
6304 | {
|
---|
6305 | break;
|
---|
6306 | }
|
---|
6307 | }
|
---|
6308 |
|
---|
6309 | cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
|
---|
6310 | memset(demux, 0, sizeof(demux));
|
---|
6311 |
|
---|
6312 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
6313 | {
|
---|
6314 | SAFE_MUTEX_INIT(&demux[i].answerlock, NULL);
|
---|
6315 | for(j = 0; j < MAX_ECM_PIDS; j++)
|
---|
6316 | {
|
---|
6317 | for(l = 0; l < MAX_STREAM_INDICES; l++)
|
---|
6318 | {
|
---|
6319 | demux[i].ECMpids[j].index[l] = INDEX_INVALID;
|
---|
6320 | }
|
---|
6321 | }
|
---|
6322 | demux[i].pidindex = -1;
|
---|
6323 | demux[i].curindex = -1;
|
---|
6324 | }
|
---|
6325 |
|
---|
6326 | memset(ca_fd, 0, sizeof(ca_fd));
|
---|
6327 | memset(unassoc_fd, 0, sizeof(unassoc_fd));
|
---|
6328 | dvbapi_read_priority();
|
---|
6329 | dvbapi_load_channel_cache();
|
---|
6330 | dvbapi_detect_api();
|
---|
6331 |
|
---|
6332 | if(selected_box == -1 || selected_api == -1)
|
---|
6333 | {
|
---|
6334 | cs_log("ERROR: Could not detect DVBAPI version.");
|
---|
6335 | free(mbuf);
|
---|
6336 | return NULL;
|
---|
6337 | }
|
---|
6338 |
|
---|
6339 | // detect box type first and then get descrambler info
|
---|
6340 | dvbapi_get_descrambler_info();
|
---|
6341 |
|
---|
6342 | if(cfg.dvbapi_pmtmode == 1)
|
---|
6343 | {
|
---|
6344 | disable_pmt_files = 1;
|
---|
6345 | }
|
---|
6346 |
|
---|
6347 | int32_t listenfd = -1;
|
---|
6348 | if(cfg.dvbapi_boxtype != BOXTYPE_IPBOX_PMT &&
|
---|
6349 | cfg.dvbapi_pmtmode != 2 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
|
---|
6350 | {
|
---|
6351 | if(!cfg.dvbapi_listenport)
|
---|
6352 | {
|
---|
6353 | listenfd = dvbapi_init_listenfd();
|
---|
6354 | }
|
---|
6355 | else
|
---|
6356 | {
|
---|
6357 | listenfd = dvbapi_net_init_listenfd();
|
---|
6358 | }
|
---|
6359 |
|
---|
6360 | if(listenfd < 1)
|
---|
6361 | {
|
---|
6362 | cs_log("ERROR: Could not init socket: (errno=%d: %s)", errno, strerror(errno));
|
---|
6363 | free(mbuf);
|
---|
6364 | return NULL;
|
---|
6365 | }
|
---|
6366 | }
|
---|
6367 |
|
---|
6368 | for(i = 0; i < MAX_DEMUX; i++) // init all demuxers!
|
---|
6369 | {
|
---|
6370 | demux[i].pidindex = -1;
|
---|
6371 | demux[i].curindex = -1;
|
---|
6372 | }
|
---|
6373 |
|
---|
6374 | if(cfg.dvbapi_pmtmode != 4 && cfg.dvbapi_pmtmode != 5 && cfg.dvbapi_pmtmode != 6)
|
---|
6375 | {
|
---|
6376 | struct sigaction signal_action;
|
---|
6377 | signal_action.sa_handler = event_handler;
|
---|
6378 | sigemptyset(&signal_action.sa_mask);
|
---|
6379 | signal_action.sa_flags = SA_RESTART;
|
---|
6380 | sigaction(SIGRTMIN + 1, &signal_action, NULL);
|
---|
6381 |
|
---|
6382 | dir_fd = open(TMPDIR, O_RDONLY);
|
---|
6383 | if(dir_fd >= 0)
|
---|
6384 | {
|
---|
6385 | fcntl(dir_fd, F_SETSIG, SIGRTMIN + 1);
|
---|
6386 | fcntl(dir_fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_DELETE | DN_MULTISHOT);
|
---|
6387 | event_handler(SIGRTMIN + 1);
|
---|
6388 | }
|
---|
6389 | }
|
---|
6390 | else
|
---|
6391 | {
|
---|
6392 | int32_t ret = start_thread("dvbapi event", dvbapi_event_thread, (void *) dvbapi_client, NULL, 1, 0);
|
---|
6393 | if(ret)
|
---|
6394 | {
|
---|
6395 | free(mbuf);
|
---|
6396 | return NULL;
|
---|
6397 | }
|
---|
6398 | }
|
---|
6399 |
|
---|
6400 | if(listenfd != -1)
|
---|
6401 | {
|
---|
6402 | pfd2[0].fd = listenfd;
|
---|
6403 | pfd2[0].events = (POLLIN | POLLPRI);
|
---|
6404 | type[0] = 1;
|
---|
6405 | }
|
---|
6406 |
|
---|
6407 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2 || defined WITH_NEUTRINO
|
---|
6408 | system("pzapit -rz");
|
---|
6409 | #endif
|
---|
6410 | cs_ftime(&start); // register start time
|
---|
6411 |
|
---|
6412 | while(!exit_oscam)
|
---|
6413 | {
|
---|
6414 | if(pausecam) // for dbox2, STAPI or PC in standby mode don't parse any ecm/emm or try to start next filter
|
---|
6415 | {
|
---|
6416 | continue;
|
---|
6417 | }
|
---|
6418 |
|
---|
6419 | if(cfg.dvbapi_pmtmode == 6)
|
---|
6420 | {
|
---|
6421 | if(listenfd < 0)
|
---|
6422 | {
|
---|
6423 | cs_log("PMT6: Trying connect to enigma CA PMT listen socket...");
|
---|
6424 |
|
---|
6425 | // socket init
|
---|
6426 | if((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
|
---|
6427 | {
|
---|
6428 | cs_log("socket error (errno=%d %s)", errno, strerror(errno));
|
---|
6429 | listenfd = -1;
|
---|
6430 | }
|
---|
6431 | else if(connect(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
|
---|
6432 | {
|
---|
6433 | cs_log("socket connect error (errno=%d %s)", errno, strerror(errno));
|
---|
6434 | close(listenfd);
|
---|
6435 | listenfd = -1;
|
---|
6436 | }
|
---|
6437 | else
|
---|
6438 | {
|
---|
6439 | pfd2[0].fd = listenfd;
|
---|
6440 | pfd2[0].events = (POLLIN | POLLPRI);
|
---|
6441 | type[0] = 1;
|
---|
6442 | cs_log("PMT6 CA PMT Server connected on fd %d!", listenfd);
|
---|
6443 | }
|
---|
6444 | }
|
---|
6445 |
|
---|
6446 | if(listenfd == -1) // not connected!
|
---|
6447 | {
|
---|
6448 | cs_sleepms(1000);
|
---|
6449 | continue; // start fresh connect attempt!
|
---|
6450 | }
|
---|
6451 | }
|
---|
6452 |
|
---|
6453 | pfdcount = (listenfd > -1) ? 1 : 0;
|
---|
6454 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
6455 | {
|
---|
6456 | // add client fd's which are not yet associated
|
---|
6457 | // with the demux but needs to be polled for data
|
---|
6458 | if(unassoc_fd[i])
|
---|
6459 | {
|
---|
6460 | pfd2[pfdcount].fd = unassoc_fd[i];
|
---|
6461 | pfd2[pfdcount].events = (POLLIN | POLLPRI);
|
---|
6462 | client_proto_version[pfdcount] = last_client_proto_version;
|
---|
6463 | type[pfdcount++] = 1;
|
---|
6464 | }
|
---|
6465 |
|
---|
6466 | if(demux[i].program_number == 0)
|
---|
6467 | {
|
---|
6468 | continue; // only evalutate demuxers that have channels assigned
|
---|
6469 | }
|
---|
6470 |
|
---|
6471 | uint32_t ecmcounter = 0, emmcounter = 0;
|
---|
6472 | for(g = 0; g < maxfilter; g++)
|
---|
6473 | {
|
---|
6474 | if(demux[i].demux_fd[g].fd <= 0)
|
---|
6475 | {
|
---|
6476 | continue; // deny obvious invalid fd!
|
---|
6477 | }
|
---|
6478 |
|
---|
6479 | if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX
|
---|
6480 | && selected_api != STAPI && selected_api != COOLAPI)
|
---|
6481 | {
|
---|
6482 | pfd2[pfdcount].fd = demux[i].demux_fd[g].fd;
|
---|
6483 | pfd2[pfdcount].events = (POLLIN | POLLPRI);
|
---|
6484 | ids[pfdcount] = i;
|
---|
6485 | fdn[pfdcount] = g;
|
---|
6486 | type[pfdcount++] = 0;
|
---|
6487 | }
|
---|
6488 |
|
---|
6489 | // count ecm filters to see if demuxing is possible anyway
|
---|
6490 | if(demux[i].demux_fd[g].type == TYPE_ECM)
|
---|
6491 | {
|
---|
6492 | ecmcounter++;
|
---|
6493 | }
|
---|
6494 |
|
---|
6495 | // count emm filters also
|
---|
6496 | if(demux[i].demux_fd[g].type == TYPE_EMM)
|
---|
6497 | {
|
---|
6498 | emmcounter++;
|
---|
6499 | }
|
---|
6500 | }
|
---|
6501 |
|
---|
6502 | // only produce log if something changed
|
---|
6503 | if(ecmcounter != demux[i].old_ecmfiltercount || emmcounter != demux[i].old_emmfiltercount)
|
---|
6504 | {
|
---|
6505 | cs_log_dbg(D_DVBAPI, "Demuxer %d has %d ecmpids, %d streampids, %d ecmfilters and %d of max %d emmfilters",
|
---|
6506 | i, demux[i].ECMpidcount, demux[i].STREAMpidcount, ecmcounter, emmcounter, demux[i].max_emm_filter);
|
---|
6507 |
|
---|
6508 | demux[i].old_ecmfiltercount = ecmcounter; // save new amount of ecm filters
|
---|
6509 | demux[i].old_emmfiltercount = emmcounter; // save new amount of emm filters
|
---|
6510 | }
|
---|
6511 |
|
---|
6512 | // delayed emm start for non irdeto caids,
|
---|
6513 | // start emm cat if not already done for this demuxer!
|
---|
6514 | struct timeb now;
|
---|
6515 | cs_ftime(&now);
|
---|
6516 | int64_t gone;
|
---|
6517 | int8_t do_emm_start = (cfg.dvbapi_au > 0 && demux[i].emm_filter == -1 && demux[i].EMMpidcount == 0 && emmcounter == 0);
|
---|
6518 | int8_t do_sdt_start = (cfg.dvbapi_read_sdt && demux[i].sdt_filter == -1 && cfg.dvbapi_boxtype != BOXTYPE_SAMYGO);
|
---|
6519 |
|
---|
6520 | if(do_emm_start || do_sdt_start)
|
---|
6521 | {
|
---|
6522 | gone = comp_timeb(&now, &demux[i].emmstart);
|
---|
6523 | if(gone > 20 * 1000)
|
---|
6524 | {
|
---|
6525 | if(do_emm_start)
|
---|
6526 | {
|
---|
6527 | cs_ftime(&demux[i].emmstart); // trick to let emm fetching start after 30 seconds to speed up zapping
|
---|
6528 | dvbapi_start_filter(i, demux[i].pidindex, 0x001, 0x001, 0x01, 0x01, 0xFF, 0, TYPE_EMM); //CAT
|
---|
6529 | }
|
---|
6530 | }
|
---|
6531 |
|
---|
6532 | if(gone > 5 * 1000)
|
---|
6533 | {
|
---|
6534 | if(do_sdt_start)
|
---|
6535 | {
|
---|
6536 | dvbapi_start_sdt_filter(i);
|
---|
6537 | }
|
---|
6538 | }
|
---|
6539 | }
|
---|
6540 |
|
---|
6541 | // early start for irdeto since they need emm before ecm
|
---|
6542 | // (pmt emmstart = 1 if detected caid 0x06)
|
---|
6543 | int32_t emmstarted = demux[i].emm_filter;
|
---|
6544 |
|
---|
6545 | // check every time since share readers might
|
---|
6546 | // give us new filters due to hexserial change
|
---|
6547 | if(cfg.dvbapi_au && demux[i].EMMpidcount > 0)
|
---|
6548 | {
|
---|
6549 | if(!emmcounter && emmstarted == -1)
|
---|
6550 | {
|
---|
6551 | demux[i].emmstart = now;
|
---|
6552 | dvbapi_start_emm_filter(i); // start emm filtering if emm pids are found
|
---|
6553 | }
|
---|
6554 | else
|
---|
6555 | {
|
---|
6556 | gone = comp_timeb(&now, &demux[i].emmstart);
|
---|
6557 | if(gone > 30 * 1000)
|
---|
6558 | {
|
---|
6559 | demux[i].emmstart = now;
|
---|
6560 | dvbapi_start_emm_filter(i); // start emm filtering delayed if filters already were running
|
---|
6561 | rotate_emmfilter(i); // rotate active emm filters
|
---|
6562 | }
|
---|
6563 | }
|
---|
6564 | }
|
---|
6565 |
|
---|
6566 | // Restart decoding all caids we have ecmpids but no ecm filters!
|
---|
6567 | if(ecmcounter == 0 && demux[i].ECMpidcount > 0)
|
---|
6568 | {
|
---|
6569 | int32_t started = 0;
|
---|
6570 |
|
---|
6571 | // avoid race: not all pids are asked and checked out yet!
|
---|
6572 | for(g = 0; g < demux[i].ECMpidcount; g++)
|
---|
6573 | {
|
---|
6574 | // check if prio run is done
|
---|
6575 | if(demux[i].ECMpids[g].checked == 0 && demux[i].ECMpids[g].status >= 0)
|
---|
6576 | {
|
---|
6577 | dvbapi_try_next_caid(i, 0, 0); // not done, so start next prio pid
|
---|
6578 | started = 1;
|
---|
6579 | break;
|
---|
6580 | }
|
---|
6581 | }
|
---|
6582 |
|
---|
6583 | if(started)
|
---|
6584 | {
|
---|
6585 | continue; // if started a filter proceed with next demuxer
|
---|
6586 | }
|
---|
6587 |
|
---|
6588 | // all usable pids (with prio) are tried, lets start over again without prio!
|
---|
6589 | if(g == demux[i].ECMpidcount)
|
---|
6590 | {
|
---|
6591 | // avoid race: not all pids are asked and checked out yet!
|
---|
6592 | for(g = 0; g < demux[i].ECMpidcount; g++)
|
---|
6593 | {
|
---|
6594 | // check if noprio run is done
|
---|
6595 | if(demux[i].ECMpids[g].checked == 2 && demux[i].ECMpids[g].status >= 0)
|
---|
6596 | {
|
---|
6597 | demux[i].ECMpids[g].irdeto_curindex = 0xFE;
|
---|
6598 | demux[i].ECMpids[g].irdeto_maxindex = 0;
|
---|
6599 | demux[i].ECMpids[g].irdeto_cycle = 0xFE;
|
---|
6600 | demux[i].ECMpids[g].tries = 0xFE;
|
---|
6601 | demux[i].ECMpids[g].table = 0;
|
---|
6602 | demux[i].ECMpids[g].CHID = 0x10000; // remove chid prio
|
---|
6603 |
|
---|
6604 | dvbapi_try_next_caid(i, 2, 0); // not done, so start next no prio pid
|
---|
6605 | started = 1;
|
---|
6606 | break;
|
---|
6607 | }
|
---|
6608 | }
|
---|
6609 | }
|
---|
6610 |
|
---|
6611 | if(started)
|
---|
6612 | {
|
---|
6613 | continue; // if started a filter proceed with next demuxer
|
---|
6614 | }
|
---|
6615 |
|
---|
6616 | if(g == demux[i].ECMpidcount) // all usable pids are tried, lets start over again!
|
---|
6617 | {
|
---|
6618 | if(demux[i].decodingtries == -1) // first redecoding attempt?
|
---|
6619 | {
|
---|
6620 | cs_ftime(&demux[i].decstart);
|
---|
6621 |
|
---|
6622 | // re-init some used things from second run (without prio)
|
---|
6623 | for(g = 0; g < demux[i].ECMpidcount; g++)
|
---|
6624 | {
|
---|
6625 | demux[i].ECMpids[g].checked = 0;
|
---|
6626 | demux[i].ECMpids[g].irdeto_curindex = 0xFE;
|
---|
6627 | demux[i].ECMpids[g].irdeto_maxindex = 0;
|
---|
6628 | demux[i].ECMpids[g].irdeto_cycle = 0xFE;
|
---|
6629 | demux[i].ECMpids[g].table = 0;
|
---|
6630 | demux[i].decodingtries = 0;
|
---|
6631 |
|
---|
6632 | // remove this pid from channel cache since we had no founds on any ecmpid!
|
---|
6633 | dvbapi_edit_channel_cache(i, g, 0);
|
---|
6634 | }
|
---|
6635 | }
|
---|
6636 |
|
---|
6637 | uint8_t number_of_enabled_pids = 0;
|
---|
6638 | demux[i].decodingtries++;
|
---|
6639 | dvbapi_resort_ecmpids(i);
|
---|
6640 |
|
---|
6641 | for(g = 0; g < demux[i].ECMpidcount; g++) // count number of enabled pids!
|
---|
6642 | {
|
---|
6643 | if(demux[i].ECMpids[g].status >= 0) number_of_enabled_pids++;
|
---|
6644 | }
|
---|
6645 |
|
---|
6646 | if(!number_of_enabled_pids)
|
---|
6647 | {
|
---|
6648 | if(demux[i].decodingtries == 10)
|
---|
6649 | {
|
---|
6650 | demux[i].decodingtries = 0;
|
---|
6651 | cs_log("Demuxer %d no enabled matching ecmpids -> decoding is waiting for matching readers!",i);
|
---|
6652 | }
|
---|
6653 | }
|
---|
6654 | else
|
---|
6655 | {
|
---|
6656 | cs_ftime(&demux[i].decend);
|
---|
6657 | demux[i].decodingtries = -1; // reset to first run again!
|
---|
6658 | gone = comp_timeb(&demux[i].decend, &demux[i].decstart);
|
---|
6659 |
|
---|
6660 | cs_log("Demuxer %d restarting decoding requests after %"PRId64" ms with %d enabled and %d disabled ecmpids!",
|
---|
6661 | i, gone, number_of_enabled_pids, (demux[i].ECMpidcount-number_of_enabled_pids));
|
---|
6662 |
|
---|
6663 | dvbapi_try_next_caid(i, 0, 0);
|
---|
6664 | }
|
---|
6665 | }
|
---|
6666 | }
|
---|
6667 |
|
---|
6668 | if(demux[i].socket_fd > 0 && cfg.dvbapi_pmtmode != 6)
|
---|
6669 | {
|
---|
6670 | rc = 0;
|
---|
6671 | for(j = 0; j < pfdcount; j++)
|
---|
6672 | {
|
---|
6673 | if(pfd2[j].fd == demux[i].socket_fd)
|
---|
6674 | {
|
---|
6675 | rc = 1;
|
---|
6676 | break;
|
---|
6677 | }
|
---|
6678 | }
|
---|
6679 |
|
---|
6680 | if(rc == 1)
|
---|
6681 | {
|
---|
6682 | continue;
|
---|
6683 | }
|
---|
6684 |
|
---|
6685 | pfd2[pfdcount].fd = demux[i].socket_fd;
|
---|
6686 | pfd2[pfdcount].events = (POLLIN | POLLPRI);
|
---|
6687 | ids[pfdcount] = i;
|
---|
6688 | type[pfdcount++] = 1;
|
---|
6689 | }
|
---|
6690 | }
|
---|
6691 |
|
---|
6692 | rc = 0;
|
---|
6693 | while(!(listenfd == -1 && cfg.dvbapi_pmtmode == 6))
|
---|
6694 | {
|
---|
6695 | rc = poll(pfd2, pfdcount, 500);
|
---|
6696 | if(rc < 0) // error occured while polling for fd's with fresh data
|
---|
6697 | {
|
---|
6698 | if(errno == EINTR || errno == EAGAIN) // try again in case of interrupt
|
---|
6699 | {
|
---|
6700 | continue;
|
---|
6701 | }
|
---|
6702 | cs_log("ERROR: error on poll of %d fd's (errno=%d %s)", pfdcount, errno, strerror(errno));
|
---|
6703 | break;
|
---|
6704 | }
|
---|
6705 | else
|
---|
6706 | {
|
---|
6707 | break;
|
---|
6708 | }
|
---|
6709 | }
|
---|
6710 |
|
---|
6711 | if(rc > 0)
|
---|
6712 | {
|
---|
6713 | cs_ftime(&end); // register end time
|
---|
6714 | int64_t timeout = comp_timeb(&end, &start);
|
---|
6715 | if(timeout < 0)
|
---|
6716 | {
|
---|
6717 | cs_log("*** WARNING: BAD TIME AFFECTING WHOLE OSCAM ECM HANDLING ****");
|
---|
6718 | }
|
---|
6719 | cs_log_dbg(D_TRACE, "New events occurred on %d of %d handlers after %"PRId64" ms inactivity", rc, pfdcount, timeout);
|
---|
6720 | cs_ftime(&start); // register new start time for next poll
|
---|
6721 | }
|
---|
6722 |
|
---|
6723 | for(i = 0; i < pfdcount && rc > 0; i++)
|
---|
6724 | {
|
---|
6725 | if(pfd2[i].revents == 0) { continue; } // skip sockets with no changes
|
---|
6726 | rc--; //event handled!
|
---|
6727 | cs_log_dbg(D_TRACE, "Now handling fd %d that reported event %d", pfd2[i].fd, pfd2[i].revents);
|
---|
6728 |
|
---|
6729 | if(pfd2[i].revents & (POLLHUP | POLLNVAL | POLLERR))
|
---|
6730 | {
|
---|
6731 | if(type[i] == 1)
|
---|
6732 | {
|
---|
6733 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
6734 | {
|
---|
6735 | // if listenfd closes stop all assigned decoding!
|
---|
6736 | if(demux[j].socket_fd == pfd2[i].fd)
|
---|
6737 | {
|
---|
6738 | dvbapi_stop_descrambling(j, 0);
|
---|
6739 | }
|
---|
6740 |
|
---|
6741 | // remove from unassoc_fd when necessary
|
---|
6742 | if(unassoc_fd[j] == pfd2[i].fd)
|
---|
6743 | {
|
---|
6744 | unassoc_fd[j] = 0;
|
---|
6745 | }
|
---|
6746 | }
|
---|
6747 |
|
---|
6748 | int32_t ret = close(pfd2[i].fd);
|
---|
6749 | if(ret < 0 && errno != 9)
|
---|
6750 | {
|
---|
6751 | cs_log("ERROR: Could not close demuxer socket fd (errno=%d %s)", errno, strerror(errno));
|
---|
6752 | }
|
---|
6753 |
|
---|
6754 | if(pfd2[i].fd == listenfd && cfg.dvbapi_pmtmode == 6)
|
---|
6755 | {
|
---|
6756 | listenfd = -1;
|
---|
6757 | }
|
---|
6758 | cs_log_dbg(D_DVBAPI, "Socket %d reported hard connection close", pfd2[i].fd);
|
---|
6759 | }
|
---|
6760 | else // type = 0
|
---|
6761 | {
|
---|
6762 | int32_t demux_id = ids[i];
|
---|
6763 | int32_t n = fdn[i];
|
---|
6764 |
|
---|
6765 | if(cfg.dvbapi_boxtype != BOXTYPE_SAMYGO)
|
---|
6766 | {
|
---|
6767 | // stop filter since its giving errors and wont return anything good
|
---|
6768 | dvbapi_stop_filternum(demux_id, n, 0);
|
---|
6769 | }
|
---|
6770 | else
|
---|
6771 | {
|
---|
6772 | int32_t ret, pid;
|
---|
6773 | uint8_t filter[32];
|
---|
6774 | struct dmx_sct_filter_params sFP;
|
---|
6775 | cs_log_dbg(D_DVBAPI, "re-opening connection to demux socket");
|
---|
6776 | close(demux[demux_id].demux_fd[n].fd);
|
---|
6777 | demux[demux_id].demux_fd[n].fd = -1;
|
---|
6778 |
|
---|
6779 | ret = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
|
---|
6780 | if(ret != -1)
|
---|
6781 | {
|
---|
6782 | demux[demux_id].demux_fd[n].fd = ret;
|
---|
6783 | pid = demux[demux_id].curindex;
|
---|
6784 | memset(filter, 0, 32);
|
---|
6785 | memset(&sFP, 0, sizeof(sFP));
|
---|
6786 | filter[0] = 0x80;
|
---|
6787 | filter[16] = 0xF0;
|
---|
6788 | sFP.pid = demux[demux_id].ECMpids[pid].ECM_PID;
|
---|
6789 | sFP.timeout = 3000;
|
---|
6790 | sFP.flags = DMX_IMMEDIATE_START;
|
---|
6791 | memcpy(sFP.filter.filter, filter, 16);
|
---|
6792 | memcpy(sFP.filter.mask, filter + 16, 16);
|
---|
6793 | ret = dvbapi_ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER, &sFP);
|
---|
6794 | }
|
---|
6795 |
|
---|
6796 | if(ret == -1)
|
---|
6797 | {
|
---|
6798 | // stop filter since it's giving errors and wont return anything good
|
---|
6799 | dvbapi_stop_filternum(demux_id, n, 0);
|
---|
6800 | }
|
---|
6801 | }
|
---|
6802 | }
|
---|
6803 | continue; // continue with other events
|
---|
6804 | }
|
---|
6805 |
|
---|
6806 | if(pfd2[i].revents & (POLLIN | POLLPRI))
|
---|
6807 | {
|
---|
6808 | if(type[i] == 1)
|
---|
6809 | {
|
---|
6810 | connfd = -1; // initially no socket to read from
|
---|
6811 | uint8_t add_to_poll = 0; // we may need to additionally poll this socket when no PMT data comes in
|
---|
6812 |
|
---|
6813 | if(pfd2[i].fd == listenfd)
|
---|
6814 | {
|
---|
6815 | if(cfg.dvbapi_pmtmode == 6)
|
---|
6816 | {
|
---|
6817 | connfd = listenfd;
|
---|
6818 | disable_pmt_files = 1;
|
---|
6819 | }
|
---|
6820 | else
|
---|
6821 | {
|
---|
6822 | clilen = sizeof(servaddr);
|
---|
6823 | connfd = accept(listenfd, (struct sockaddr *)&servaddr, (socklen_t *)&clilen);
|
---|
6824 | cs_log_dbg(D_DVBAPI, "new socket connection fd: %d", connfd);
|
---|
6825 |
|
---|
6826 | if(cfg.dvbapi_listenport)
|
---|
6827 | {
|
---|
6828 | // update webif data
|
---|
6829 | client->ip = SIN_GET_ADDR(servaddr);
|
---|
6830 | client->port = ntohs(SIN_GET_PORT(servaddr));
|
---|
6831 | }
|
---|
6832 | add_to_poll = 1;
|
---|
6833 |
|
---|
6834 | if(cfg.dvbapi_pmtmode == 3 || cfg.dvbapi_pmtmode == 0)
|
---|
6835 | {
|
---|
6836 | disable_pmt_files = 1;
|
---|
6837 | }
|
---|
6838 |
|
---|
6839 | if(connfd <= 0)
|
---|
6840 | {
|
---|
6841 | cs_log_dbg(D_DVBAPI, "accept() returns error on fd event %d (errno=%d %s)",
|
---|
6842 | pfd2[i].revents, errno, strerror(errno));
|
---|
6843 | }
|
---|
6844 | }
|
---|
6845 | }
|
---|
6846 | else
|
---|
6847 | {
|
---|
6848 | connfd = pfd2[i].fd;
|
---|
6849 | }
|
---|
6850 |
|
---|
6851 | //reading and completing data from socket
|
---|
6852 | if(connfd > 0)
|
---|
6853 | {
|
---|
6854 | if(unhandled_buf_used[i])
|
---|
6855 | {
|
---|
6856 | memcpy(mbuf, unhandled_buf[i], unhandled_buf_used[i]);
|
---|
6857 | }
|
---|
6858 |
|
---|
6859 | if(!dvbapi_handlesockdata(connfd, mbuf, mbuf_size, unhandled_buf_used[i], &add_to_poll, &unhandled_buf_used[i], &client_proto_version[i]))
|
---|
6860 | {
|
---|
6861 | unhandled_buf_used[i] = 0;
|
---|
6862 | // client disconnects, stop all assigned decoding
|
---|
6863 | cs_log_dbg(D_DVBAPI, "Socket %d reported connection close", connfd);
|
---|
6864 | int active_conn = 0; // other active connections counter
|
---|
6865 | add_to_poll = 0;
|
---|
6866 |
|
---|
6867 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
6868 | {
|
---|
6869 | if(demux[j].socket_fd == connfd)
|
---|
6870 | {
|
---|
6871 | dvbapi_stop_descrambling(j, 0);
|
---|
6872 | }
|
---|
6873 | else if(demux[j].socket_fd)
|
---|
6874 | {
|
---|
6875 | active_conn++;
|
---|
6876 | }
|
---|
6877 |
|
---|
6878 | // remove from unassoc_fd when necessary
|
---|
6879 | if(unassoc_fd[j] == connfd)
|
---|
6880 | {
|
---|
6881 | unassoc_fd[j] = 0;
|
---|
6882 | }
|
---|
6883 | }
|
---|
6884 | close(connfd);
|
---|
6885 | connfd = -1;
|
---|
6886 |
|
---|
6887 | // last connection closed
|
---|
6888 | if(!active_conn && (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX))
|
---|
6889 | {
|
---|
6890 | if(cfg.dvbapi_listenport)
|
---|
6891 | {
|
---|
6892 | // update webif data
|
---|
6893 | client->ip = get_null_ip();
|
---|
6894 | client->port = 0;
|
---|
6895 | }
|
---|
6896 | }
|
---|
6897 | continue;
|
---|
6898 | }
|
---|
6899 |
|
---|
6900 | if(unhandled_buf_used[i])
|
---|
6901 | {
|
---|
6902 | if(unhandled_buf_used[i] > unhandled_buf_len[i])
|
---|
6903 | {
|
---|
6904 | NULLFREE(unhandled_buf[i]);
|
---|
6905 | unhandled_buf_len[i] = unhandled_buf_used[i] < 128 ? 128 : unhandled_buf_used[i];
|
---|
6906 | if(!cs_malloc(&unhandled_buf[i], sizeof(uint8_t) * unhandled_buf_len[i]))
|
---|
6907 | {
|
---|
6908 | unhandled_buf_len[i] = 0;
|
---|
6909 | unhandled_buf_used[i] = 0;
|
---|
6910 | continue;
|
---|
6911 | }
|
---|
6912 | }
|
---|
6913 | memcpy(unhandled_buf[i], mbuf, unhandled_buf_used[i]);
|
---|
6914 | }
|
---|
6915 |
|
---|
6916 | // if the connection is new and we read no PMT data, then add it to the poll,
|
---|
6917 | // otherwise this socket will not be checked with poll when data arives
|
---|
6918 | // because fd it is not yet assigned with the demux
|
---|
6919 | if(add_to_poll)
|
---|
6920 | {
|
---|
6921 | for(j = 0; j < MAX_DEMUX; j++)
|
---|
6922 | {
|
---|
6923 | if(!unassoc_fd[j])
|
---|
6924 | {
|
---|
6925 | unassoc_fd[j] = connfd;
|
---|
6926 | break;
|
---|
6927 | }
|
---|
6928 | }
|
---|
6929 | }
|
---|
6930 | }
|
---|
6931 | }
|
---|
6932 | else // type == 0
|
---|
6933 | {
|
---|
6934 | int32_t demux_id = ids[i];
|
---|
6935 | int32_t n = fdn[i];
|
---|
6936 |
|
---|
6937 | if((int)demux[demux_id].demux_fd[n].fd != pfd2[i].fd)
|
---|
6938 | {
|
---|
6939 | continue; // filter already killed, no need to process this data!
|
---|
6940 | }
|
---|
6941 |
|
---|
6942 | len = dvbapi_read_device(pfd2[i].fd, mbuf, mbuf_size);
|
---|
6943 | if(len < 0) // serious filterdata read error
|
---|
6944 | {
|
---|
6945 | // stop filter since it's giving errors and won't return anything good
|
---|
6946 | dvbapi_stop_filternum(demux_id, n, 0);
|
---|
6947 |
|
---|
6948 | maxfilter--; // lower maxfilters to avoid this with new filter setups!
|
---|
6949 | continue;
|
---|
6950 | }
|
---|
6951 |
|
---|
6952 | if(!len) // receiver internal filter buffer overflow
|
---|
6953 | {
|
---|
6954 | memset(mbuf, 0, mbuf_size);
|
---|
6955 | }
|
---|
6956 | dvbapi_process_input(demux_id, n, mbuf, len, 0);
|
---|
6957 | }
|
---|
6958 | continue; // continue with other events!
|
---|
6959 | }
|
---|
6960 | }
|
---|
6961 | }
|
---|
6962 |
|
---|
6963 | for(j = 0; j < maxpfdsize; j++)
|
---|
6964 | {
|
---|
6965 | NULLFREE(unhandled_buf[j]);
|
---|
6966 | }
|
---|
6967 | free(mbuf);
|
---|
6968 |
|
---|
6969 | return NULL;
|
---|
6970 | }
|
---|
6971 |
|
---|
6972 | void dvbapi_write_cw(int32_t demux_id, int32_t pid, int32_t stream_id, uint8_t *cw, uint8_t cw_length, uint8_t *iv,
|
---|
6973 | uint8_t iv_length, enum ca_descr_algo algo, enum ca_descr_cipher_mode cipher_mode, uint32_t msgid)
|
---|
6974 | {
|
---|
6975 | int8_t n, cw_empty = 0;
|
---|
6976 | uint8_t null_cw[cw_length];
|
---|
6977 | ca_descr_t ca_descr;
|
---|
6978 | ca_descr_mode_t ca_descr_mode;
|
---|
6979 | ca_descr_data_t ca_descr_data;
|
---|
6980 |
|
---|
6981 | memset(null_cw, 0, cw_length);
|
---|
6982 | memset(&ca_descr, 0, sizeof(ca_descr));
|
---|
6983 | memset(&ca_descr_mode, 0, sizeof(ca_descr_mode));
|
---|
6984 | memset(&ca_descr_data, 0, sizeof(ca_descr_data));
|
---|
6985 |
|
---|
6986 | if(memcmp(demux[demux_id].last_cw[stream_id][0], null_cw, cw_length) == 0
|
---|
6987 | && memcmp(demux[demux_id].last_cw[stream_id][1], null_cw, cw_length) == 0)
|
---|
6988 | {
|
---|
6989 | cw_empty = 1; // to make sure that both cws get written on constantcw
|
---|
6990 | }
|
---|
6991 |
|
---|
6992 | for(n = 0; n < 2; n++)
|
---|
6993 | {
|
---|
6994 | // Check if cw has changed and if new cw is empty (all zeros)
|
---|
6995 | // Skip check for BISS1 - cw could be indeed zero
|
---|
6996 | // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
|
---|
6997 | if((memcmp(cw + (n * cw_length), demux[demux_id].last_cw[stream_id][n], cw_length) != 0 || cw_empty)
|
---|
6998 | && (memcmp(cw + (n * cw_length), null_cw, cw_length) != 0 || caid_is_biss(demux[demux_id].ECMpids[pid].CAID)))
|
---|
6999 | {
|
---|
7000 | // prepare ca device
|
---|
7001 | uint32_t idx = dvbapi_ca_set_pid(demux_id, pid, stream_id, (algo == CA_ALGO_DES), msgid);
|
---|
7002 | if(idx == INDEX_INVALID)
|
---|
7003 | {
|
---|
7004 | return; // return on no index!
|
---|
7005 | }
|
---|
7006 |
|
---|
7007 | #if defined WITH_COOLAPI || defined WITH_COOLAPI2
|
---|
7008 | ca_descr.index = idx;
|
---|
7009 | ca_descr.parity = n;
|
---|
7010 |
|
---|
7011 | // just to make the compiler happy (-Wunused-parameter)
|
---|
7012 | // (better move the coolapi code to a separate function)
|
---|
7013 | ca_descr_mode.cipher_mode = cipher_mode;
|
---|
7014 | ca_descr_data.data = iv;
|
---|
7015 | ca_descr_data.length = iv_length;
|
---|
7016 |
|
---|
7017 | memcpy(demux[demux_id].last_cw[stream_id][n], cw + (n * 8), 8);
|
---|
7018 | memcpy(ca_descr.cw, cw + (n * 8), 8);
|
---|
7019 |
|
---|
7020 | cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca_mask %d)",
|
---|
7021 | demux_id, n, ca_descr.index, demux[demux_id].ca_mask);
|
---|
7022 |
|
---|
7023 | coolapi_write_cw(demux[demux_id].ca_mask, demux[demux_id].STREAMpids, demux[demux_id].STREAMpidcount, &ca_descr);
|
---|
7024 | #else
|
---|
7025 | int32_t i, j, write_cw = 0;
|
---|
7026 | uint32_t usedidx, lastidx;
|
---|
7027 |
|
---|
7028 | char lastcw[2 * cw_length + 1];
|
---|
7029 | char newcw[2 * cw_length + 1];
|
---|
7030 | cs_hexdump(0, demux[demux_id].last_cw[stream_id][n], cw_length, lastcw, sizeof(lastcw));
|
---|
7031 | cs_hexdump(0, cw + (n * cw_length), cw_length, newcw, sizeof(newcw));
|
---|
7032 |
|
---|
7033 | for(i = 0; i < CA_MAX; i++)
|
---|
7034 | {
|
---|
7035 | if(!(demux[demux_id].ca_mask & (1 << i)))
|
---|
7036 | {
|
---|
7037 | continue; // ca not in use by this demuxer!
|
---|
7038 | }
|
---|
7039 | lastidx = INDEX_INVALID;
|
---|
7040 |
|
---|
7041 | for(j = 0; j < demux[demux_id].STREAMpidcount; j++)
|
---|
7042 | {
|
---|
7043 | write_cw = 0;
|
---|
7044 | if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << j)) == (uint) (1 << j)))
|
---|
7045 | {
|
---|
7046 | usedidx = is_ca_used(i, demux[demux_id].STREAMpids[j]);
|
---|
7047 | if(usedidx != INDEX_INVALID)
|
---|
7048 | {
|
---|
7049 | if(idx != usedidx)
|
---|
7050 | {
|
---|
7051 | cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip!",
|
---|
7052 | demux_id, i, usedidx, demux[demux_id].STREAMpids[j]);
|
---|
7053 | continue; // if not used for descrambling -> skip!
|
---|
7054 | }
|
---|
7055 | else
|
---|
7056 | {
|
---|
7057 | if(usedidx == lastidx)
|
---|
7058 | {
|
---|
7059 | cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> skip, %s part of cw already written!",
|
---|
7060 | demux_id, i, usedidx, demux[demux_id].STREAMpids[j], (n == 1 ? "even" : "odd"));
|
---|
7061 | continue;
|
---|
7062 | }
|
---|
7063 |
|
---|
7064 | cs_log_dbg(D_DVBAPI,"Demuxer %d ca%d is using index %d for streampid %04X -> write %s part of cw!",
|
---|
7065 | demux_id, i, usedidx, demux[demux_id].STREAMpids[j], (n == 1 ? "even" : "odd"));
|
---|
7066 |
|
---|
7067 | write_cw = 1;
|
---|
7068 | }
|
---|
7069 | }
|
---|
7070 | }
|
---|
7071 |
|
---|
7072 | if(!write_cw)
|
---|
7073 | {
|
---|
7074 | continue; // no need to write the cw since this ca isnt using it!
|
---|
7075 | }
|
---|
7076 |
|
---|
7077 | lastidx = usedidx;
|
---|
7078 | ca_descr.index = usedidx;
|
---|
7079 | ca_descr.parity = n;
|
---|
7080 |
|
---|
7081 | memcpy(demux[demux_id].last_cw[stream_id][n], cw + (n * cw_length), cw_length);
|
---|
7082 | memcpy(ca_descr.cw, cw + (n * 8), 8); // ca_descr is only used for 8 byte CWs
|
---|
7083 |
|
---|
7084 | cs_log_dbg(D_DVBAPI, "Demuxer %d writing %s part (%s) of controlword, replacing expired (%s)",
|
---|
7085 | demux_id, (n == 1 ? "even" : "odd"), newcw, lastcw);
|
---|
7086 |
|
---|
7087 | cs_log_dbg(D_DVBAPI, "Demuxer %d write cw%d index: %d (ca%d)", demux_id, n, ca_descr.index, i);
|
---|
7088 |
|
---|
7089 | if(cfg.dvbapi_extended_cw_api == 1) // Set descrambler algorithm and mode
|
---|
7090 | {
|
---|
7091 | ca_descr_mode.index = usedidx;
|
---|
7092 | ca_descr_mode.algo = algo;
|
---|
7093 | ca_descr_mode.cipher_mode = cipher_mode;
|
---|
7094 |
|
---|
7095 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
7096 | {
|
---|
7097 | dvbapi_net_send(DVBAPI_CA_SET_DESCR_MODE, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
|
---|
7098 | (uint8_t *) &ca_descr_mode, NULL, NULL, demux[demux_id].client_proto_version);
|
---|
7099 | }
|
---|
7100 | else
|
---|
7101 | {
|
---|
7102 | if(ca_fd[i] <= 0)
|
---|
7103 | {
|
---|
7104 | ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
|
---|
7105 | if(ca_fd[i] <= 0) { continue; }
|
---|
7106 | }
|
---|
7107 |
|
---|
7108 | if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_MODE, &ca_descr_mode) < 0)
|
---|
7109 | {
|
---|
7110 | cs_log("ERROR: ioctl(CA_SET_DESCR_MODE): %s", strerror(errno));
|
---|
7111 | }
|
---|
7112 | }
|
---|
7113 | }
|
---|
7114 |
|
---|
7115 | // Send 16 byte CW and IV for AES128, DVB-CISSA
|
---|
7116 | if(cfg.dvbapi_extended_cw_api == 1 && algo == CA_ALGO_AES128)
|
---|
7117 | {
|
---|
7118 | // First send IV
|
---|
7119 | ca_descr_data.index = usedidx;
|
---|
7120 | ca_descr_data.data_type = CA_DATA_IV;
|
---|
7121 | ca_descr_data.data = iv;
|
---|
7122 | ca_descr_data.length = iv_length;
|
---|
7123 |
|
---|
7124 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
7125 | {
|
---|
7126 | dvbapi_net_send(DVBAPI_CA_SET_DESCR_DATA, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
|
---|
7127 | (uint8_t *) &ca_descr_data, NULL, NULL, demux[demux_id].client_proto_version);
|
---|
7128 | }
|
---|
7129 | else
|
---|
7130 | {
|
---|
7131 | if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_DATA, &ca_descr_data) < 0)
|
---|
7132 | {
|
---|
7133 | cs_log("ERROR: ioctl(CA_SET_DESCR_DATA): %s", strerror(errno));
|
---|
7134 | }
|
---|
7135 | }
|
---|
7136 |
|
---|
7137 | // Then send CW
|
---|
7138 | ca_descr_data.index = usedidx;
|
---|
7139 | ca_descr_data.data_type = CA_DATA_KEY;
|
---|
7140 | ca_descr_data.data = cw + (n * cw_length);
|
---|
7141 | ca_descr_data.length = cw_length;
|
---|
7142 | ca_descr_data.parity = n;
|
---|
7143 |
|
---|
7144 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
7145 | {
|
---|
7146 | dvbapi_net_send(DVBAPI_CA_SET_DESCR_DATA, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
|
---|
7147 | (uint8_t *) &ca_descr_data, NULL, NULL, demux[demux_id].client_proto_version);
|
---|
7148 | }
|
---|
7149 | else
|
---|
7150 | {
|
---|
7151 | if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR_DATA, &ca_descr_data) < 0)
|
---|
7152 | {
|
---|
7153 | cs_log("ERROR: ioctl(CA_SET_DESCR_DATA): %s", strerror(errno));
|
---|
7154 | }
|
---|
7155 | }
|
---|
7156 | }
|
---|
7157 | else // Send 8 byte CW for DVB-CSA or DES
|
---|
7158 | {
|
---|
7159 | if(cfg.dvbapi_boxtype == BOXTYPE_PC || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
7160 | {
|
---|
7161 | dvbapi_net_send(DVBAPI_CA_SET_DESCR, demux[demux_id].socket_fd, msgid, demux_id, -1 /*unused*/,
|
---|
7162 | (uint8_t *) &ca_descr, NULL, NULL, demux[demux_id].client_proto_version);
|
---|
7163 | }
|
---|
7164 | else
|
---|
7165 | {
|
---|
7166 | if(ca_fd[i] <= 0)
|
---|
7167 | {
|
---|
7168 | ca_fd[i] = dvbapi_open_device(1, i, demux[demux_id].adapter_index);
|
---|
7169 | if(ca_fd[i] <= 0)
|
---|
7170 | {
|
---|
7171 | continue;
|
---|
7172 | }
|
---|
7173 | }
|
---|
7174 |
|
---|
7175 | if(dvbapi_ioctl(ca_fd[i], CA_SET_DESCR, &ca_descr) < 0)
|
---|
7176 | {
|
---|
7177 | cs_log("ERROR: ioctl(CA_SET_DESCR): %s", strerror(errno));
|
---|
7178 | }
|
---|
7179 | }
|
---|
7180 | }
|
---|
7181 | }
|
---|
7182 | }
|
---|
7183 | #endif
|
---|
7184 | }
|
---|
7185 | }
|
---|
7186 | cs_log_dbg(D_DVBAPI, "Using %d of %d total descramblers", ca_descramblers_used, ca_descramblers_total);
|
---|
7187 | }
|
---|
7188 |
|
---|
7189 | void delayer(ECM_REQUEST *er, uint32_t delay)
|
---|
7190 | {
|
---|
7191 | if(delay <= 0) { return; }
|
---|
7192 |
|
---|
7193 | struct timeb tpe;
|
---|
7194 | cs_ftime(&tpe);
|
---|
7195 | int64_t gone = comp_timeb(&tpe, &er->tps);
|
---|
7196 |
|
---|
7197 | if(gone < delay)
|
---|
7198 | {
|
---|
7199 | cs_log_dbg(D_DVBAPI, "delayer: gone=%"PRId64" ms, cfg=%d ms -> delay=%"PRId64" ms", gone, delay, delay - gone);
|
---|
7200 | cs_sleepms(delay - gone);
|
---|
7201 | }
|
---|
7202 | }
|
---|
7203 |
|
---|
7204 | void dvbapi_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
7205 | {
|
---|
7206 | int32_t i, j, k, handled = 0;
|
---|
7207 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
7208 | {
|
---|
7209 | uint32_t nocw_write = 0; // 0 = write cw, 1 = dont write cw to hardware demuxer
|
---|
7210 |
|
---|
7211 | // ignore empty demuxers, skip ecm response for other
|
---|
7212 | // srvid and ecm recponse for different adapter
|
---|
7213 | if(demux[i].program_number == 0
|
---|
7214 | || demux[i].program_number != er->srvid
|
---|
7215 | || demux[i].adapter_index != er->adapter_index)
|
---|
7216 | {
|
---|
7217 | continue;
|
---|
7218 | }
|
---|
7219 |
|
---|
7220 | #ifdef WITH_STAPI5
|
---|
7221 | if(strcmp(dev_list[demux[i].dev_index].name, er->dev_name) != 0)
|
---|
7222 | {
|
---|
7223 | continue; // skip request if PTI device doesn't match request
|
---|
7224 | }
|
---|
7225 | #endif
|
---|
7226 |
|
---|
7227 | demux[i].rdr = er->selected_reader;
|
---|
7228 |
|
---|
7229 | for(j = 0; j < demux[i].ECMpidcount; j++) // check for matching ecmpid
|
---|
7230 | {
|
---|
7231 | if((demux[i].ECMpids[j].CAID == er->caid || demux[i].ECMpids[j].CAID == er->ocaid)
|
---|
7232 | && demux[i].ECMpids[j].ECM_PID == er->pid && demux[i].ECMpids[j].PROVID == er->prid
|
---|
7233 | && demux[i].ECMpids[j].VPID == er->vpid)
|
---|
7234 | {
|
---|
7235 | break;
|
---|
7236 | }
|
---|
7237 | }
|
---|
7238 |
|
---|
7239 | if(j == demux[i].ECMpidcount)
|
---|
7240 | {
|
---|
7241 | continue; // ecm response srvid ok but no matching ecmpid, perhaps this for other demuxer
|
---|
7242 | }
|
---|
7243 |
|
---|
7244 | cs_log_dbg(D_DVBAPI, "Demuxer %d %scontrol word received for PID %d CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
|
---|
7245 | i, (er->rc >= E_NOTFOUND ? "no " : ""), j, er->caid, er->prid, er->pid, er->chid, er->vpid);
|
---|
7246 |
|
---|
7247 | uint32_t status = dvbapi_check_ecm_delayed_delivery(i, er);
|
---|
7248 | uint32_t comparecw0 = 0, comparecw1 = 0;
|
---|
7249 | char ecmd5[17 * 3];
|
---|
7250 | cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
|
---|
7251 |
|
---|
7252 | if(status == 1 && er->rc) // wrong ecmhash
|
---|
7253 | {
|
---|
7254 | cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (requested different one)", i, ecmd5);
|
---|
7255 | continue;
|
---|
7256 | }
|
---|
7257 |
|
---|
7258 | if(status == 2) // no filter
|
---|
7259 | {
|
---|
7260 | cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (filter already killed)", i, ecmd5);
|
---|
7261 | continue;
|
---|
7262 | }
|
---|
7263 |
|
---|
7264 | if(status == 5) // empty cw
|
---|
7265 | {
|
---|
7266 | cs_log_dbg(D_DVBAPI, "Demuxer %d not interested in response ecmhash %s (delivered cw is empty!)", i, ecmd5);
|
---|
7267 | nocw_write = 1;
|
---|
7268 |
|
---|
7269 | if(er->rc < E_NOTFOUND)
|
---|
7270 | {
|
---|
7271 | er->rc = E_NOTFOUND;
|
---|
7272 | }
|
---|
7273 | }
|
---|
7274 |
|
---|
7275 | // 0=matching ecm hash, 2=no filter, 3=table reset, 4=cache-ex response
|
---|
7276 | // Check only against last_cw[0] (index 0) - No need to check the rest
|
---|
7277 | // Skip check for BISS1 - cw could be indeed zero
|
---|
7278 | // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
|
---|
7279 | if((status == 0 || status == 3 || status == 4) && er->rc < E_NOTFOUND && !caid_is_biss(er->caid))
|
---|
7280 | {
|
---|
7281 | // check for matching control word
|
---|
7282 | if(memcmp(er->cw, demux[i].last_cw[0][0], 8) == 0 &&
|
---|
7283 | memcmp(er->cw + 8, demux[i].last_cw[0][1], 8) == 0)
|
---|
7284 | {
|
---|
7285 | comparecw0 = 1;
|
---|
7286 | }
|
---|
7287 | else if(memcmp(er->cw, demux[i].last_cw[0][1], 8) == 0 &&
|
---|
7288 | memcmp(er->cw + 8, demux[i].last_cw[0][0], 8) == 0)
|
---|
7289 | {
|
---|
7290 | comparecw1 = 1;
|
---|
7291 | }
|
---|
7292 |
|
---|
7293 | if(comparecw0 == 1 || comparecw1 == 1)
|
---|
7294 | {
|
---|
7295 | cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate controlword ecm response hash %s (duplicate controlword!)", i, ecmd5);
|
---|
7296 | nocw_write = 1;
|
---|
7297 | }
|
---|
7298 | }
|
---|
7299 |
|
---|
7300 | if(status == 3) // table reset
|
---|
7301 | {
|
---|
7302 | cs_log_dbg(D_DVBAPI, "Demuxer %d luckyshot new controlword ecm response hash %s (ecm table reset)", i, ecmd5);
|
---|
7303 | }
|
---|
7304 |
|
---|
7305 | if(status == 4) // no check on cache-ex responses!
|
---|
7306 | {
|
---|
7307 | cs_log_dbg(D_DVBAPI, "Demuxer %d new controlword from cache-ex reader (no ecmhash check possible)", i);
|
---|
7308 | }
|
---|
7309 |
|
---|
7310 | handled = 1; // mark this ecm response as handled
|
---|
7311 | if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 0 && (demux[i].pidindex == -1) && er->caid != 0)
|
---|
7312 | {
|
---|
7313 | demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
|
---|
7314 | demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
|
---|
7315 | demux[i].pidindex = j; // set current index as *the* pid to descramble
|
---|
7316 | demux[i].ECMpids[j].checked = 4;
|
---|
7317 |
|
---|
7318 | cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
|
---|
7319 | i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
|
---|
7320 | }
|
---|
7321 |
|
---|
7322 | if(er->rc < E_NOTFOUND && cfg.dvbapi_requestmode == 1 && er->caid != 0) // FOUND
|
---|
7323 | {
|
---|
7324 | SAFE_MUTEX_LOCK(&demux[i].answerlock); // only process one ecm answer
|
---|
7325 | if(demux[i].ECMpids[j].checked != 4)
|
---|
7326 | {
|
---|
7327 | int32_t t, o, ecmcounter = 0;
|
---|
7328 | int32_t oldpidindex = demux[i].pidindex;
|
---|
7329 | demux[i].pidindex = j; // set current ecmpid as the new pid to descramble
|
---|
7330 |
|
---|
7331 | if(oldpidindex != -1)
|
---|
7332 | {
|
---|
7333 | for(k = 0; k < MAX_STREAM_INDICES; k++)
|
---|
7334 | {
|
---|
7335 | demux[i].ECMpids[j].index[k] = demux[i].ECMpids[oldpidindex].index[k]; // swap index with lower status pid that was descrambling
|
---|
7336 | demux[i].ECMpids[j].useMultipleIndices = demux[i].ECMpids[oldpidindex].useMultipleIndices;
|
---|
7337 | }
|
---|
7338 | }
|
---|
7339 |
|
---|
7340 | // check this pid with control word FOUND for higher status
|
---|
7341 | for(t = 0; t < demux[i].ECMpidcount; t++)
|
---|
7342 | {
|
---|
7343 | if(t != j && demux[i].ECMpids[j].status >= demux[i].ECMpids[t].status)
|
---|
7344 | {
|
---|
7345 | // check if ecm filter is in use and
|
---|
7346 | // stop all ecm filters of lower status pids
|
---|
7347 | for(o = 0; o < maxfilter; o++)
|
---|
7348 | {
|
---|
7349 | if(demux[i].demux_fd[o].fd > 0 && demux[i].demux_fd[o].type == TYPE_ECM
|
---|
7350 | && demux[i].demux_fd[o].pidindex == t)
|
---|
7351 | {
|
---|
7352 | // ecm filter belongs to lower status pid -> kill!
|
---|
7353 | dvbapi_stop_filternum(i, o, er->msgid);
|
---|
7354 | }
|
---|
7355 | }
|
---|
7356 | dvbapi_edit_channel_cache(i, t, 0); // remove lower status pid from channel cache
|
---|
7357 | demux[i].ECMpids[t].checked = 4; // mark index t as low status
|
---|
7358 | }
|
---|
7359 | }
|
---|
7360 |
|
---|
7361 | for(o = 0; o < maxfilter; o++)
|
---|
7362 | {
|
---|
7363 | if(demux[i].demux_fd[o].type == TYPE_ECM)
|
---|
7364 | {
|
---|
7365 | ecmcounter++; // count all ecm filters
|
---|
7366 | }
|
---|
7367 | }
|
---|
7368 |
|
---|
7369 | demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
|
---|
7370 | demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
|
---|
7371 |
|
---|
7372 | if(ecmcounter == 1) // if total found running ecmfilters is 1 -> we found the "best" pid
|
---|
7373 | {
|
---|
7374 | dvbapi_edit_channel_cache(i, j, 1);
|
---|
7375 | demux[i].ECMpids[j].checked = 4; // mark best pid last ;)
|
---|
7376 | }
|
---|
7377 | cs_log_dbg(D_DVBAPI, "Demuxer %d descrambling PID %d CAID %04X PROVID %06X ECMPID %04X CHID %02X VPID %04X",
|
---|
7378 | i, demux[i].pidindex, er->caid, er->prid, er->pid, er->chid, er->vpid);
|
---|
7379 | }
|
---|
7380 | SAFE_MUTEX_UNLOCK(&demux[i].answerlock); // and release it!
|
---|
7381 | }
|
---|
7382 |
|
---|
7383 | if(er->rc >= E_NOTFOUND) // not found on requestmode 0 + 1
|
---|
7384 | {
|
---|
7385 | if(er->rc == E_SLEEPING)
|
---|
7386 | {
|
---|
7387 | dvbapi_stop_descrambling(i, er->msgid);
|
---|
7388 | return;
|
---|
7389 | }
|
---|
7390 |
|
---|
7391 | struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(i, j, 'p');
|
---|
7392 | if(forceentry && forceentry->force) // forced pid? keep trying the forced ecmpid!
|
---|
7393 | {
|
---|
7394 | // all cas or irdeto cas with forced prio chid
|
---|
7395 | if(!caid_is_irdeto(er->caid) || forceentry->chid < 0x10000)
|
---|
7396 | {
|
---|
7397 | demux[i].ECMpids[j].table = 0;
|
---|
7398 | dvbapi_set_section_filter(i, er, -1);
|
---|
7399 | continue;
|
---|
7400 | }
|
---|
7401 | else // irdeto cas without chid prio forced
|
---|
7402 | {
|
---|
7403 | // init irdeto current index to first one
|
---|
7404 | if(demux[i].ECMpids[j].irdeto_curindex == 0xFE)
|
---|
7405 | {
|
---|
7406 | demux[i].ECMpids[j].irdeto_curindex = 0x00;
|
---|
7407 | }
|
---|
7408 |
|
---|
7409 | // check for last / max chid
|
---|
7410 | if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex))
|
---|
7411 | {
|
---|
7412 | cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of FORCED PID %d CAID %04X PROVID %06X ECMPID %04X",
|
---|
7413 | i, j, er->caid, er->prid, er->pid);
|
---|
7414 |
|
---|
7415 | demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
|
---|
7416 | demux[i].ECMpids[j].table = 0;
|
---|
7417 | dvbapi_set_section_filter(i, er, -1);
|
---|
7418 | continue;
|
---|
7419 | }
|
---|
7420 | }
|
---|
7421 | }
|
---|
7422 |
|
---|
7423 | // in case of timeout or fatal LB event give
|
---|
7424 | // this pid another try but no more than 1 try
|
---|
7425 | if((er->rc == E_TIMEOUT || (er->rcEx && er->rcEx <= E2_CCCAM_NOCARD))
|
---|
7426 | && demux[i].ECMpids[j].tries == 0xFE)
|
---|
7427 | {
|
---|
7428 | demux[i].ECMpids[j].tries -= 0x07;
|
---|
7429 | demux[i].ECMpids[j].table = 0;
|
---|
7430 | dvbapi_set_section_filter(i, er, -1);
|
---|
7431 | continue;
|
---|
7432 | }
|
---|
7433 | else // all not found responses exception: first timeout response and first fatal loadbalancer response
|
---|
7434 | {
|
---|
7435 | demux[i].ECMpids[j].CHID = 0x10000; // get rid of this prio chid since it failed!
|
---|
7436 | demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry
|
---|
7437 | }
|
---|
7438 |
|
---|
7439 | if(caid_is_irdeto(er->caid))
|
---|
7440 | {
|
---|
7441 | // init irdeto current index to first one
|
---|
7442 | if(demux[i].ECMpids[j].irdeto_curindex == 0xFE)
|
---|
7443 | {
|
---|
7444 | demux[i].ECMpids[j].irdeto_curindex = 0x00;
|
---|
7445 | }
|
---|
7446 |
|
---|
7447 | // check for last / max chid
|
---|
7448 | if(!(demux[i].ECMpids[j].irdeto_curindex + 1 > demux[i].ECMpids[j].irdeto_maxindex))
|
---|
7449 | {
|
---|
7450 | cs_log_dbg(D_DVBAPI, "Demuxer %d trying next irdeto chid of PID %d CAID %04X PROVID %06X ECMPID %04X VPID %04X",
|
---|
7451 | i, j, er->caid, er->prid, er->pid, er->vpid);
|
---|
7452 |
|
---|
7453 | demux[i].ECMpids[j].irdeto_curindex++; // irdeto index one up
|
---|
7454 | demux[i].ECMpids[j].table = 0;
|
---|
7455 | dvbapi_set_section_filter(i, er, -1);
|
---|
7456 | continue;
|
---|
7457 | }
|
---|
7458 | }
|
---|
7459 |
|
---|
7460 | dvbapi_edit_channel_cache(i, j, 0); // remove this pid from channelcache
|
---|
7461 |
|
---|
7462 | if(demux[i].pidindex == j)
|
---|
7463 | {
|
---|
7464 | // current pid delivered a notfound so this pid isn't
|
---|
7465 | // being used to descramble any longer -> clear pidindex
|
---|
7466 | demux[i].pidindex = -1;
|
---|
7467 | }
|
---|
7468 |
|
---|
7469 | demux[i].ECMpids[j].irdeto_maxindex = 0;
|
---|
7470 | demux[i].ECMpids[j].irdeto_curindex = 0xFE;
|
---|
7471 | demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
|
---|
7472 | demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdetocycle
|
---|
7473 | demux[i].ECMpids[j].table = 0;
|
---|
7474 | demux[i].ECMpids[j].checked = 4; // flag ecmpid as checked
|
---|
7475 | demux[i].ECMpids[j].status = -1; // flag ecmpid as unusable
|
---|
7476 |
|
---|
7477 | int32_t found = 1; // setup for first run
|
---|
7478 | int32_t filternum = -1;
|
---|
7479 |
|
---|
7480 | while(found > 0) // disable all ecm + emm filters for this notfound
|
---|
7481 | {
|
---|
7482 | found = 0;
|
---|
7483 |
|
---|
7484 | filternum = dvbapi_get_filternum(i, er, TYPE_ECM); // get ecm filternumber
|
---|
7485 | if(filternum > -1) // in case valid filter found
|
---|
7486 | {
|
---|
7487 | int32_t fd = demux[i].demux_fd[filternum].fd;
|
---|
7488 | if(fd > 0) // in case valid fd
|
---|
7489 | {
|
---|
7490 | dvbapi_stop_filternum(i, filternum, er->msgid); // stop ecmfilter
|
---|
7491 | found = 1;
|
---|
7492 | }
|
---|
7493 | }
|
---|
7494 |
|
---|
7495 | if(caid_is_irdeto(er->caid)) // in case irdeto cas stop old emm filters
|
---|
7496 | {
|
---|
7497 | filternum = dvbapi_get_filternum(i, er, TYPE_EMM); // get emm filternumber
|
---|
7498 | if(filternum > -1) // in case valid filter found
|
---|
7499 | {
|
---|
7500 | int32_t fd = demux[i].demux_fd[filternum].fd;
|
---|
7501 | if(fd > 0) // in case valid fd
|
---|
7502 | {
|
---|
7503 | dvbapi_stop_filternum(i, filternum, er->msgid); // stop emmfilter
|
---|
7504 | found = 1;
|
---|
7505 | }
|
---|
7506 | }
|
---|
7507 | }
|
---|
7508 | }
|
---|
7509 | continue;
|
---|
7510 | }
|
---|
7511 |
|
---|
7512 | // below this should be only run in case of ecm answer is found
|
---|
7513 | uint32_t chid = get_subid(er); // derive current chid in case of irdeto, or a unique part of ecm on other cas systems
|
---|
7514 | demux[i].ECMpids[j].CHID = (chid != 0 ? chid : 0x10000); // if not zero apply, otherwise use no chid value 0x10000
|
---|
7515 | dvbapi_edit_channel_cache(i, j, 1); // do it here to here after the right CHID is registered
|
---|
7516 |
|
---|
7517 | //dvbapi_set_section_filter(i, er); is not needed anymore (unsure)
|
---|
7518 | demux[i].ECMpids[j].tries = 0xFE; // reset timeout retry flag
|
---|
7519 | demux[i].ECMpids[j].irdeto_cycle = 0xFE; // reset irdeto cycle
|
---|
7520 |
|
---|
7521 | // cw was already written by another filter or current pid
|
---|
7522 | // isn't pid used to descramble so it ends here!
|
---|
7523 | if(nocw_write || demux[i].pidindex != j)
|
---|
7524 | {
|
---|
7525 | continue;
|
---|
7526 | }
|
---|
7527 |
|
---|
7528 | struct s_dvbapi_priority *delayentry = dvbapi_check_prio_match(i, demux[i].pidindex, 'd');
|
---|
7529 | uint32_t delay = 0;
|
---|
7530 |
|
---|
7531 | if(delayentry)
|
---|
7532 | {
|
---|
7533 | if(delayentry->delay < 1000)
|
---|
7534 | {
|
---|
7535 | delay = delayentry->delay;
|
---|
7536 | cs_log_dbg(D_DVBAPI, "specific delay: write cw %d ms after ecmrequest", delay);
|
---|
7537 | }
|
---|
7538 | }
|
---|
7539 | else if(cfg.dvbapi_delayer > 0)
|
---|
7540 | {
|
---|
7541 | delay = cfg.dvbapi_delayer;
|
---|
7542 | cs_log_dbg(D_DVBAPI, "generic delay: write cw %d ms after ecmrequest", delay);
|
---|
7543 | }
|
---|
7544 |
|
---|
7545 | delayer(er, delay);
|
---|
7546 |
|
---|
7547 | #ifdef WITH_EMU
|
---|
7548 | if(!chk_ctab_ex(er->caid, &cfg.emu_stream_relay_ctab) || !cfg.emu_stream_relay_enabled)
|
---|
7549 | #endif
|
---|
7550 | switch(selected_api)
|
---|
7551 | {
|
---|
7552 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
7553 | case STAPI:
|
---|
7554 | stapi_write_cw(i, er->cw, demux[i].STREAMpids, demux[i].STREAMpidcount, demux[i].pmt_file);
|
---|
7555 | break;
|
---|
7556 | #endif
|
---|
7557 | default:
|
---|
7558 | {
|
---|
7559 | #ifdef WITH_EXTENDED_CW
|
---|
7560 | if(er->cw_ex.mode != demux[i].ECMpids[j].useMultipleIndices)
|
---|
7561 | {
|
---|
7562 | uint32_t idx;
|
---|
7563 |
|
---|
7564 | for(k = 0; k < demux[i].STREAMpidcount; k++)
|
---|
7565 | {
|
---|
7566 | if(demux[i].ECMpids[j].useMultipleIndices)
|
---|
7567 | {
|
---|
7568 | idx = demux[i].ECMpids[j].index[k];
|
---|
7569 | }
|
---|
7570 | else
|
---|
7571 | {
|
---|
7572 | idx = demux[i].ECMpids[j].index[0];
|
---|
7573 | }
|
---|
7574 | dvbapi_set_pid(i, k, idx, false, false, er->msgid); // disable streampid
|
---|
7575 | }
|
---|
7576 |
|
---|
7577 | for(k = 0; k < MAX_STREAM_INDICES; k++)
|
---|
7578 | {
|
---|
7579 | demux[i].ECMpids[j].index[k] = INDEX_INVALID;
|
---|
7580 | }
|
---|
7581 | }
|
---|
7582 |
|
---|
7583 | if(er->cw_ex.mode == CW_MODE_MULTIPLE_CW)
|
---|
7584 | {
|
---|
7585 | int32_t key_pos_a = 0;
|
---|
7586 | demux[i].ECMpids[j].useMultipleIndices = 1;
|
---|
7587 |
|
---|
7588 | for(k = 0; k < demux[i].STREAMpidcount; k++)
|
---|
7589 | {
|
---|
7590 | if(demux[i].STREAMpidsType[k] == STREAM_VIDEO)
|
---|
7591 | {
|
---|
7592 | dvbapi_write_cw(i, j, k, er->cw, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
|
---|
7593 | }
|
---|
7594 | else if(demux[i].STREAMpidsType[k] == STREAM_AUDIO)
|
---|
7595 | {
|
---|
7596 | if(key_pos_a < 4)
|
---|
7597 | {
|
---|
7598 | 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);
|
---|
7599 | key_pos_a++;
|
---|
7600 | }
|
---|
7601 | }
|
---|
7602 | // Every channel that uses the extended cw has unencrypted subtitle streams,
|
---|
7603 | // so disable CW writing to save indices for audio streams and recordings.
|
---|
7604 | //else // Data
|
---|
7605 | //{
|
---|
7606 | // dvbapi_write_cw(i, j, k, er->cw_ex.data, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
|
---|
7607 | //}
|
---|
7608 | }
|
---|
7609 | }
|
---|
7610 | else
|
---|
7611 | {
|
---|
7612 | demux[i].ECMpids[j].useMultipleIndices = 0;
|
---|
7613 |
|
---|
7614 | if(er->cw_ex.algo == CW_ALGO_AES128)
|
---|
7615 | {
|
---|
7616 | 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);
|
---|
7617 | }
|
---|
7618 | else
|
---|
7619 | {
|
---|
7620 | dvbapi_write_cw(i, j, 0, er->cw, 8, NULL, 0, er->cw_ex.algo, er->cw_ex.algo_mode, er->msgid);
|
---|
7621 | }
|
---|
7622 | }
|
---|
7623 | #else
|
---|
7624 | cfg.dvbapi_extended_cw_api = 0; // in CSA mode extended_cw_api should be always 0 regardless what user selected!
|
---|
7625 | dvbapi_write_cw(i, j, 0, er->cw, 8, NULL, 0, CA_ALGO_DVBCSA, CA_MODE_CBC, er->msgid);
|
---|
7626 | #endif
|
---|
7627 | break;
|
---|
7628 | }
|
---|
7629 | }
|
---|
7630 |
|
---|
7631 | // reset idle-Time
|
---|
7632 | client->last = time((time_t *)0); // ********* TO BE FIXED LATER ON ******
|
---|
7633 |
|
---|
7634 | if((cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX) && demux[i].client_proto_version >= 2)
|
---|
7635 | {
|
---|
7636 | dvbapi_net_send(DVBAPI_ECM_INFO, demux[i].socket_fd, 0, i, 0, NULL, client, er, demux[i].client_proto_version);
|
---|
7637 | }
|
---|
7638 | #ifndef __CYGWIN__
|
---|
7639 | else if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
|
---|
7640 | #endif
|
---|
7641 | if(cfg.dvbapi_boxtype != BOXTYPE_SAMYGO)
|
---|
7642 | {
|
---|
7643 | #ifdef WITH_EXTENDED_CW
|
---|
7644 | // Only print CWs for index 0 in ecm.info file
|
---|
7645 | if(er->cw_ex.algo == CA_ALGO_AES128)
|
---|
7646 | {
|
---|
7647 | dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 16);
|
---|
7648 | }
|
---|
7649 | else
|
---|
7650 | {
|
---|
7651 | dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 8);
|
---|
7652 | }
|
---|
7653 | #else
|
---|
7654 | dvbapi_write_ecminfo_file(client, er, demux[i].last_cw[0][0], demux[i].last_cw[0][1], 8);
|
---|
7655 | #endif
|
---|
7656 | }
|
---|
7657 | }
|
---|
7658 |
|
---|
7659 | if(handled == 0)
|
---|
7660 | {
|
---|
7661 | cs_log_dbg(D_DVBAPI, "Unhandled ECM response received for CAID %04X PROVID %06X ECMPID %04X CHID %04X VPID %04X",
|
---|
7662 | er->caid, er->prid, er->pid, er->chid, er->vpid);
|
---|
7663 | }
|
---|
7664 | }
|
---|
7665 |
|
---|
7666 | static int8_t isValidCW(uint8_t *cw)
|
---|
7667 | {
|
---|
7668 | uint8_t i;
|
---|
7669 | for(i = 0; i < 16; i += 4)
|
---|
7670 | {
|
---|
7671 | if(((cw[i] + cw[i + 1] + cw[i + 2]) & 0xff) != cw[i + 3])
|
---|
7672 | {
|
---|
7673 | return 0;
|
---|
7674 | }
|
---|
7675 | }
|
---|
7676 | return 1;
|
---|
7677 | }
|
---|
7678 |
|
---|
7679 | void dvbapi_write_ecminfo_file(struct s_client *client, ECM_REQUEST *er, uint8_t *lastcw0, uint8_t *lastcw1, uint8_t cw_length)
|
---|
7680 | {
|
---|
7681 | #define ECMINFO_TYPE_OSCAM 0
|
---|
7682 | #define ECMINFO_TYPE_OSCAM_MS 1
|
---|
7683 | #define ECMINFO_TYPE_WICARDD 2
|
---|
7684 | #define ECMINFO_TYPE_MGCAMD 3
|
---|
7685 | #define ECMINFO_TYPE_CCCAM 4
|
---|
7686 | #define ECMINFO_TYPE_CAMD3 5
|
---|
7687 |
|
---|
7688 | FILE *ecmtxt = fopen(ECMINFO_FILE, "w");
|
---|
7689 | if(ecmtxt != NULL && er->rc < E_NOTFOUND)
|
---|
7690 | {
|
---|
7691 | char tmp[49]; // holds 16 byte cw - (2 hex digits + 1 space) * 16 byte + string termination)
|
---|
7692 | const char *reader_name = NULL, *from_name = NULL, *proto_name = NULL;
|
---|
7693 | int8_t hops = 0;
|
---|
7694 | int32_t from_port = 0;
|
---|
7695 | char system_name[64];
|
---|
7696 | const char *const_system_name = get_cardsystem_desc_by_caid(er->caid);
|
---|
7697 |
|
---|
7698 | cs_strncpy(system_name, const_system_name, sizeof(system_name));
|
---|
7699 | system_name[0] = (char)toupper((int)system_name[0]);
|
---|
7700 |
|
---|
7701 | if(cfg.dvbapi_ecminfo_type <= ECMINFO_TYPE_WICARDD)
|
---|
7702 | {
|
---|
7703 | if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD)
|
---|
7704 | {
|
---|
7705 | fprintf(ecmtxt, "system: %s\n", system_name);
|
---|
7706 | }
|
---|
7707 |
|
---|
7708 | fprintf(ecmtxt, "caid: 0x%04X\npid: 0x%04X\n", er->caid, er->pid);
|
---|
7709 |
|
---|
7710 | if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD)
|
---|
7711 | {
|
---|
7712 | fprintf(ecmtxt, "prov: %06X\n", (uint) er->prid);
|
---|
7713 | }
|
---|
7714 | else
|
---|
7715 | {
|
---|
7716 | fprintf(ecmtxt, "prov: 0x%06X\n", (uint) er->prid);
|
---|
7717 | }
|
---|
7718 |
|
---|
7719 | fprintf(ecmtxt, "chid: 0x%04X\n", er->chid);
|
---|
7720 | }
|
---|
7721 | else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_MGCAMD)
|
---|
7722 | {
|
---|
7723 | fprintf(ecmtxt, "===== %s ECM on CaID 0x%04X, pid 0x%04X =====\nprov: %06X\n",
|
---|
7724 | system_name, er->caid, er->pid, (uint) er->prid);
|
---|
7725 | }
|
---|
7726 | else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CCCAM)
|
---|
7727 | {
|
---|
7728 | char provider_name[128];
|
---|
7729 | get_providername(er->prid, er->caid, provider_name, sizeof(provider_name));
|
---|
7730 |
|
---|
7731 | if(provider_name[0])
|
---|
7732 | {
|
---|
7733 | fprintf(ecmtxt, "system: %s\ncaid: 0x%04X\nprovider: %s\nprovid: 0x%06X\npid: 0x%04X\n",
|
---|
7734 | system_name, er->caid, provider_name, (uint) er->prid, er->pid);
|
---|
7735 | }
|
---|
7736 | else
|
---|
7737 | {
|
---|
7738 | fprintf(ecmtxt, "system: %s\ncaid: 0x%04X\nprovid: 0x%06X\npid: 0x%04X\n",
|
---|
7739 | system_name, er->caid, (uint) er->prid, er->pid);
|
---|
7740 | }
|
---|
7741 | }
|
---|
7742 | else if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CAMD3)
|
---|
7743 | {
|
---|
7744 | fprintf(ecmtxt, "CAID 0x%04X, PID 0x%04X, PROVIDER 0x%06X\n",
|
---|
7745 | er->caid, er->pid, (uint) er->prid);
|
---|
7746 | }
|
---|
7747 |
|
---|
7748 | switch(er->rc)
|
---|
7749 | {
|
---|
7750 | case E_FOUND:
|
---|
7751 | if(er->selected_reader)
|
---|
7752 | {
|
---|
7753 | reader_name = er->selected_reader->label;
|
---|
7754 | if(is_network_reader(er->selected_reader))
|
---|
7755 | {
|
---|
7756 | from_name = er->selected_reader->device;
|
---|
7757 | }
|
---|
7758 | else
|
---|
7759 | {
|
---|
7760 | from_name = "local";
|
---|
7761 | }
|
---|
7762 | from_port = er->selected_reader->r_port;
|
---|
7763 | proto_name = reader_get_type_desc(er->selected_reader, 1);
|
---|
7764 | hops = er->selected_reader->currenthops;
|
---|
7765 | }
|
---|
7766 | else
|
---|
7767 | {
|
---|
7768 | reader_name = "none";
|
---|
7769 | from_name = "local";
|
---|
7770 | proto_name = "none";
|
---|
7771 | }
|
---|
7772 | break;
|
---|
7773 |
|
---|
7774 | case E_CACHE1:
|
---|
7775 | reader_name = "Cache";
|
---|
7776 | from_name = "cache1";
|
---|
7777 | proto_name = "none";
|
---|
7778 | break;
|
---|
7779 |
|
---|
7780 | case E_CACHE2:
|
---|
7781 | reader_name = "Cache";
|
---|
7782 | from_name = "cache2";
|
---|
7783 | proto_name = "none";
|
---|
7784 | break;
|
---|
7785 |
|
---|
7786 | case E_CACHEEX:
|
---|
7787 | reader_name = "Cache";
|
---|
7788 | from_name = "cache3";
|
---|
7789 | proto_name = "none";
|
---|
7790 | break;
|
---|
7791 | }
|
---|
7792 |
|
---|
7793 | if(cfg.dvbapi_ecminfo_type <= ECMINFO_TYPE_OSCAM_MS)
|
---|
7794 | {
|
---|
7795 | switch(er->rc)
|
---|
7796 | {
|
---|
7797 | case E_FOUND:
|
---|
7798 | if(er->selected_reader)
|
---|
7799 | {
|
---|
7800 | fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\nhops: %d\n",
|
---|
7801 | reader_name, from_name, from_port, proto_name, hops);
|
---|
7802 | }
|
---|
7803 | break;
|
---|
7804 |
|
---|
7805 | case E_CACHE1:
|
---|
7806 | case E_CACHE2:
|
---|
7807 | case E_CACHEEX:
|
---|
7808 | fprintf(ecmtxt, "reader: %s\nfrom: %s:%d\nprotocol: %s\n",
|
---|
7809 | reader_name, from_name, from_port, proto_name);
|
---|
7810 | break;
|
---|
7811 | }
|
---|
7812 |
|
---|
7813 | if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_OSCAM)
|
---|
7814 | {
|
---|
7815 | fprintf(ecmtxt, "ecm time: %.3f\n", (float) client->cwlastresptime / 1000);
|
---|
7816 | }
|
---|
7817 | else
|
---|
7818 | {
|
---|
7819 | fprintf(ecmtxt, "ecm time: %d\n", client->cwlastresptime);
|
---|
7820 | }
|
---|
7821 | }
|
---|
7822 |
|
---|
7823 | if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CAMD3)
|
---|
7824 | {
|
---|
7825 | fprintf(ecmtxt, "FROM: %s\n", reader_name);
|
---|
7826 | fprintf(ecmtxt, "CW0: %s\n", cs_hexdump(1, lastcw0, cw_length, tmp, sizeof(tmp)));
|
---|
7827 | fprintf(ecmtxt, "CW1: %s\n", cs_hexdump(1, lastcw1, cw_length, tmp, sizeof(tmp)));
|
---|
7828 | }
|
---|
7829 | else
|
---|
7830 | {
|
---|
7831 | fprintf(ecmtxt, "cw0: %s\n", cs_hexdump(1, lastcw0, cw_length, tmp, sizeof(tmp)));
|
---|
7832 | fprintf(ecmtxt, "cw1: %s\n", cs_hexdump(1, lastcw1, cw_length, tmp, sizeof(tmp)));
|
---|
7833 | }
|
---|
7834 |
|
---|
7835 | if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_WICARDD || cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_MGCAMD)
|
---|
7836 | {
|
---|
7837 | time_t walltime;
|
---|
7838 | struct tm lt;
|
---|
7839 | char timebuf[32];
|
---|
7840 |
|
---|
7841 | if(cw_length == 8) // only check checksum for 8 byte CWs
|
---|
7842 | {
|
---|
7843 | fprintf(ecmtxt, "Signature %s\n", (isValidCW(lastcw0) || isValidCW(lastcw1)) ? "OK" : "NOK");
|
---|
7844 | }
|
---|
7845 | else
|
---|
7846 | {
|
---|
7847 | fprintf(ecmtxt, "Signature %s\n", "OK");
|
---|
7848 | }
|
---|
7849 |
|
---|
7850 | if(reader_name != NULL)
|
---|
7851 | {
|
---|
7852 | fprintf(ecmtxt, "source: %s (%s at %s:%d)\n", reader_name, proto_name, from_name, from_port);
|
---|
7853 | }
|
---|
7854 |
|
---|
7855 | walltime = cs_time();
|
---|
7856 | localtime_r(&walltime, <);
|
---|
7857 |
|
---|
7858 | if(strftime(timebuf, 32, "%a %b %d %H:%M:%S %Y", <) != 0)
|
---|
7859 | {
|
---|
7860 | fprintf(ecmtxt, "%d msec -- %s\n", client->cwlastresptime, timebuf);
|
---|
7861 | }
|
---|
7862 | }
|
---|
7863 |
|
---|
7864 | if(cfg.dvbapi_ecminfo_type == ECMINFO_TYPE_CCCAM)
|
---|
7865 | {
|
---|
7866 | if(reader_name != NULL)
|
---|
7867 | {
|
---|
7868 | fprintf(ecmtxt, "using: %s\naddress: %s:%d\nhops: %d\n",
|
---|
7869 | proto_name, from_name, from_port, hops);
|
---|
7870 | }
|
---|
7871 | fprintf(ecmtxt, "ecm time: %d\n", client->cwlastresptime);
|
---|
7872 | }
|
---|
7873 | }
|
---|
7874 |
|
---|
7875 | if(ecmtxt)
|
---|
7876 | {
|
---|
7877 | int32_t ret = fclose(ecmtxt);
|
---|
7878 | if(ret < 0)
|
---|
7879 | {
|
---|
7880 | cs_log("ERROR: Could not close ecmtxt fd (errno=%d %s)", errno, strerror(errno));
|
---|
7881 | }
|
---|
7882 | ecmtxt = NULL;
|
---|
7883 | }
|
---|
7884 | }
|
---|
7885 |
|
---|
7886 |
|
---|
7887 | void *dvbapi_start_handler(struct s_client *cl, uint8_t *UNUSED(mbuf), int32_t module_idx, void *(*_main_func)(void *))
|
---|
7888 | {
|
---|
7889 | // cs_log("dvbapi loaded fd=%d", idx);
|
---|
7890 | if(cfg.dvbapi_enabled == 1)
|
---|
7891 | {
|
---|
7892 | cl = create_client(get_null_ip());
|
---|
7893 | cl->module_idx = module_idx;
|
---|
7894 | cl->typ = 'c';
|
---|
7895 |
|
---|
7896 | int32_t ret = start_thread("dvbapi handler", _main_func, (void *)cl, &cl->thread, 1, 0);
|
---|
7897 | if(ret)
|
---|
7898 | {
|
---|
7899 | return NULL;
|
---|
7900 | }
|
---|
7901 | }
|
---|
7902 | return NULL;
|
---|
7903 | }
|
---|
7904 |
|
---|
7905 | void *dvbapi_handler(struct s_client *cl, uint8_t *mbuf, int32_t module_idx)
|
---|
7906 | {
|
---|
7907 | return dvbapi_start_handler(cl, mbuf, module_idx, dvbapi_main_local);
|
---|
7908 | }
|
---|
7909 |
|
---|
7910 | int32_t dvbapi_set_section_filter(int32_t demux_id, ECM_REQUEST *er, int32_t n)
|
---|
7911 | {
|
---|
7912 | if(!er) { return -1; }
|
---|
7913 |
|
---|
7914 | if(USE_OPENXCAS || (selected_api != DVBAPI_3 && selected_api != DVBAPI_1 && selected_api != STAPI) // only valid for dvbapi3, dvbapi1 and STAPI
|
---|
7915 | || (cfg.dvbapi_boxtype == BOXTYPE_IPBOX || cfg.dvbapi_boxtype == BOXTYPE_IPBOX_PMT)) // reported buggy using sectionfiltering after 1~4 hours -> for now disabled!
|
---|
7916 | {
|
---|
7917 | return 0;
|
---|
7918 | }
|
---|
7919 |
|
---|
7920 | if(n == -1)
|
---|
7921 | {
|
---|
7922 | n = dvbapi_get_filternum(demux_id, er, TYPE_ECM);
|
---|
7923 | }
|
---|
7924 |
|
---|
7925 | if(n < 0) // in case no valid filter found;
|
---|
7926 | {
|
---|
7927 | return -1;
|
---|
7928 | }
|
---|
7929 |
|
---|
7930 | int32_t fd = demux[demux_id].demux_fd[n].fd;
|
---|
7931 | if(fd < 1) // in case no valid fd
|
---|
7932 | {
|
---|
7933 | return -1;
|
---|
7934 | }
|
---|
7935 |
|
---|
7936 | uint8_t filter[16];
|
---|
7937 | uint8_t mask[16];
|
---|
7938 | memset(filter, 0, 16);
|
---|
7939 | memset(mask, 0, 16);
|
---|
7940 | struct s_ecmpid *curpid = NULL;
|
---|
7941 |
|
---|
7942 | int32_t pid = demux[demux_id].demux_fd[n].pidindex;
|
---|
7943 | if(pid != -1)
|
---|
7944 | {
|
---|
7945 | curpid = &demux[demux_id].ECMpids[pid];
|
---|
7946 | }
|
---|
7947 |
|
---|
7948 | if(curpid->table != er->ecm[0] && curpid->table != 0)
|
---|
7949 | {
|
---|
7950 | return -1; // if current ecmtype differs from latest requested ecmtype do not apply section filtering!
|
---|
7951 | }
|
---|
7952 |
|
---|
7953 | uint8_t ecmfilter = 0;
|
---|
7954 |
|
---|
7955 | if(er->ecm[0] == 0x80)
|
---|
7956 | {
|
---|
7957 | ecmfilter = 0x81; // current processed ecm is even, next will be filtered for odd
|
---|
7958 | }
|
---|
7959 | else
|
---|
7960 | {
|
---|
7961 | ecmfilter = 0x80; // current processed ecm is odd, next will be filtered for even
|
---|
7962 | }
|
---|
7963 |
|
---|
7964 | if(curpid->table != 0) // cycle ecmtype from odd to even or even to odd
|
---|
7965 | {
|
---|
7966 | filter[0] = ecmfilter; // only accept new ecms (if previous odd, filter for even and vice versa)
|
---|
7967 | mask[0] = 0xFF;
|
---|
7968 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to %s (CAID %04X PROVID %06X FD %d)",
|
---|
7969 | demux_id, n + 1, (ecmfilter == 0x80 ? "EVEN" : "ODD"), curpid->CAID, curpid->PROVID, fd);
|
---|
7970 | }
|
---|
7971 | else // not decoding right now so we are interessted in all ecm types!
|
---|
7972 | {
|
---|
7973 | filter[0] = 0x80; // set filter to wait for any ecms
|
---|
7974 | mask[0] = 0xF0;
|
---|
7975 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set ecmtable to ODD+EVEN (CAID %04X PROVID %06X FD %d)",
|
---|
7976 | demux_id, n + 1, curpid->CAID, curpid->PROVID, fd);
|
---|
7977 | }
|
---|
7978 |
|
---|
7979 | uint32_t offset = 0, extramask = 0xFF;
|
---|
7980 | struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match(demux_id, pid, 'p');
|
---|
7981 | //cs_log("**** curpid->CHID %04X, checked = %d, er->chid = %04X *****", curpid->CHID, curpid->checked, er->chid);
|
---|
7982 | // checked 4 to make sure we dont set chid filter and no such ecm in dvbstream except for forced pids!
|
---|
7983 |
|
---|
7984 | if(curpid->CHID < 0x10000 && (curpid->checked == 4 || (forceentry && forceentry->force)))
|
---|
7985 | {
|
---|
7986 | switch(er->caid >> 8)
|
---|
7987 | {
|
---|
7988 | case 0x01: // seca
|
---|
7989 | offset = 7;
|
---|
7990 | extramask = 0xF0;
|
---|
7991 | break;
|
---|
7992 |
|
---|
7993 | case 0x05: // viaccess
|
---|
7994 | offset = 8;
|
---|
7995 | break;
|
---|
7996 |
|
---|
7997 | case 0x06: // irdeto
|
---|
7998 | offset = 6;
|
---|
7999 | break;
|
---|
8000 |
|
---|
8001 | case 0x09: // videoguard
|
---|
8002 | offset = 11;
|
---|
8003 | break;
|
---|
8004 |
|
---|
8005 | case 0x4A: // DRE-Crypt, Bulcrypt, Tongang and others?
|
---|
8006 | if(!caid_is_bulcrypt(er->caid))
|
---|
8007 | {
|
---|
8008 | offset = 6;
|
---|
8009 | }
|
---|
8010 | break;
|
---|
8011 | }
|
---|
8012 | }
|
---|
8013 |
|
---|
8014 | int32_t irdetomatch = 1; // check if wanted irdeto index is the one the delivers current chid!
|
---|
8015 | if(caid_is_irdeto(curpid->CAID))
|
---|
8016 | {
|
---|
8017 | if(curpid->irdeto_curindex == er->ecm[4]) { irdetomatch = 1; } // ok apply chid filtering
|
---|
8018 | else { irdetomatch = 0; } // skip chid filtering but apply irdeto index filtering
|
---|
8019 | }
|
---|
8020 |
|
---|
8021 | if(offset && irdetomatch) // we have a cas with chid or unique part in checked ecm
|
---|
8022 | {
|
---|
8023 | i2b_buf(2, curpid->CHID, filter + (offset - 2));
|
---|
8024 | mask[(offset - 2)] = 0xFF & extramask; // additional mask seca2 chid can be FC10 or FD10 varies each month so only apply F?10
|
---|
8025 | mask[(offset - 1)] = 0xFF;
|
---|
8026 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to %04X on fd %d", demux_id, n + 1, curpid->CHID, fd);
|
---|
8027 | }
|
---|
8028 | else
|
---|
8029 | {
|
---|
8030 | // on irdeto we can always apply irdeto index filtering!
|
---|
8031 | if(caid_is_irdeto(curpid->CAID) && (curpid->irdeto_curindex < 0xFE))
|
---|
8032 | {
|
---|
8033 | filter[2] = curpid->irdeto_curindex;
|
---|
8034 | mask[2] = 0xFF;
|
---|
8035 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set irdetoindex to %d on fd %d",
|
---|
8036 | demux_id, n + 1, curpid->irdeto_curindex, fd);
|
---|
8037 | }
|
---|
8038 | else // all other cas systems also cas systems without chid or unique ecm part
|
---|
8039 | {
|
---|
8040 | cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d set chid to ANY CHID on fd %d", demux_id, n + 1, fd);
|
---|
8041 | }
|
---|
8042 | }
|
---|
8043 |
|
---|
8044 | int32_t ret = dvbapi_activate_section_filter(demux_id, n, fd, curpid->ECM_PID, filter, mask, er->msgid);
|
---|
8045 | if(ret < 0) // something went wrong setting filter!
|
---|
8046 | {
|
---|
8047 | cs_log("Demuxer %d Filter %d (fd %d) error setting section filtering -> stop filter!", demux_id, n + 1, fd);
|
---|
8048 |
|
---|
8049 | ret = dvbapi_stop_filternum(demux_id, n, er->msgid);
|
---|
8050 | if(ret == -1)
|
---|
8051 | {
|
---|
8052 | cs_log("Demuxer %d Filter %d (fd %d) stopping filter failed -> kill all filters of this demuxer!", demux_id, n + 1, fd);
|
---|
8053 | dvbapi_stop_filter(demux_id, TYPE_EMM, er->msgid);
|
---|
8054 | dvbapi_stop_filter(demux_id, TYPE_ECM, er->msgid);
|
---|
8055 | }
|
---|
8056 | return -1;
|
---|
8057 | }
|
---|
8058 | return n;
|
---|
8059 | }
|
---|
8060 |
|
---|
8061 | int32_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)
|
---|
8062 | {
|
---|
8063 | int32_t ret = -1;
|
---|
8064 |
|
---|
8065 | switch(selected_api)
|
---|
8066 | {
|
---|
8067 | case DVBAPI_3:
|
---|
8068 | {
|
---|
8069 | struct dmx_sct_filter_params sFP2;
|
---|
8070 | memset(&sFP2, 0, sizeof(sFP2));
|
---|
8071 | sFP2.pid = pid;
|
---|
8072 | sFP2.timeout = 0;
|
---|
8073 | sFP2.flags = DMX_IMMEDIATE_START;
|
---|
8074 |
|
---|
8075 | if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
|
---|
8076 | {
|
---|
8077 | //DeepThought: on dgs/cubestation and neumo images, perhaps others
|
---|
8078 | //the following code is needed to descramble
|
---|
8079 | sFP2.filter.filter[0] = filter[0];
|
---|
8080 | sFP2.filter.mask[0] = mask[0];
|
---|
8081 | sFP2.filter.filter[1] = 0;
|
---|
8082 | sFP2.filter.mask[1] = 0;
|
---|
8083 | sFP2.filter.filter[2] = 0;
|
---|
8084 | sFP2.filter.mask[2] = 0;
|
---|
8085 | memcpy(sFP2.filter.filter + 3, filter + 1, 16 - 3);
|
---|
8086 | memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
|
---|
8087 |
|
---|
8088 | //DeepThought: in the drivers of the dgs/cubestation and neumo images,
|
---|
8089 | //dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
|
---|
8090 | //ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
|
---|
8091 | //due to a bug its sets the "positive mask" wrongly (they should be all 0).
|
---|
8092 | //On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
|
---|
8093 | //structure, which is incorrect (it should be dmxSctFilterParams).
|
---|
8094 | //The only way to get it right is to call DMX_SET_FILTER1 with the argument
|
---|
8095 | //expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
|
---|
8096 |
|
---|
8097 | ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP2);
|
---|
8098 | }
|
---|
8099 | else
|
---|
8100 | {
|
---|
8101 | memcpy(sFP2.filter.filter, filter, 16);
|
---|
8102 | memcpy(sFP2.filter.mask, mask, 16);
|
---|
8103 |
|
---|
8104 | if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
|
---|
8105 | {
|
---|
8106 | ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER,
|
---|
8107 | demux[demux_id].socket_fd,
|
---|
8108 | msgid,
|
---|
8109 | demux_id,
|
---|
8110 | num,
|
---|
8111 | (uint8_t *) &sFP2,
|
---|
8112 | NULL,
|
---|
8113 | NULL,
|
---|
8114 | demux[demux_id].client_proto_version);
|
---|
8115 | }
|
---|
8116 | else
|
---|
8117 | {
|
---|
8118 | ret = dvbapi_ioctl(fd, DMX_SET_FILTER, &sFP2);
|
---|
8119 | }
|
---|
8120 | }
|
---|
8121 | break;
|
---|
8122 | }
|
---|
8123 |
|
---|
8124 | case DVBAPI_1:
|
---|
8125 | {
|
---|
8126 | struct dmxSctFilterParams sFP1;
|
---|
8127 | memset(&sFP1, 0, sizeof(sFP1));
|
---|
8128 | sFP1.pid = pid;
|
---|
8129 | sFP1.timeout = 0;
|
---|
8130 | sFP1.flags = DMX_IMMEDIATE_START;
|
---|
8131 | memcpy(sFP1.filter.filter, filter, 16);
|
---|
8132 | memcpy(sFP1.filter.mask, mask, 16);
|
---|
8133 | ret = dvbapi_ioctl(fd, DMX_SET_FILTER1, &sFP1);
|
---|
8134 | break;
|
---|
8135 | }
|
---|
8136 |
|
---|
8137 | #if defined(WITH_STAPI) || defined(WITH_STAPI5)
|
---|
8138 | case STAPI:
|
---|
8139 | {
|
---|
8140 | ret = stapi_activate_section_filter(fd, filter, mask);
|
---|
8141 | break;
|
---|
8142 | }
|
---|
8143 | #endif
|
---|
8144 | // Isn't implemented in COOLAPI-1 (legacy)
|
---|
8145 | #if defined WITH_COOLAPI2
|
---|
8146 | case COOLAPI:
|
---|
8147 | {
|
---|
8148 | int32_t n = coolapi_get_filter_num(fd);
|
---|
8149 | if(n < 0)
|
---|
8150 | {
|
---|
8151 | return n;
|
---|
8152 | }
|
---|
8153 | coolapi_set_filter(fd, n, pid, filter, mask, TYPE_ECM);
|
---|
8154 | break;
|
---|
8155 | }
|
---|
8156 | #endif
|
---|
8157 |
|
---|
8158 | default:
|
---|
8159 | break;
|
---|
8160 | }
|
---|
8161 |
|
---|
8162 | if(ret != -1) // only change filter/mask for comparing if box returned no errors!
|
---|
8163 | {
|
---|
8164 | // copy filter and mask to check later on if receiver delivered accordingly
|
---|
8165 | memcpy(demux[demux_id].demux_fd[num].filter, filter, 16);
|
---|
8166 | memcpy(demux[demux_id].demux_fd[num].mask, mask, 16);
|
---|
8167 | }
|
---|
8168 | return ret;
|
---|
8169 | }
|
---|
8170 |
|
---|
8171 | int32_t dvbapi_check_ecm_delayed_delivery(int32_t demux_id, ECM_REQUEST *er)
|
---|
8172 | {
|
---|
8173 | int32_t ret = 0;
|
---|
8174 | int32_t filternum = dvbapi_get_filternum(demux_id, er, TYPE_ECM);
|
---|
8175 | char nullcw[CS_ECMSTORESIZE];
|
---|
8176 | memset(nullcw, 0, CS_ECMSTORESIZE);
|
---|
8177 |
|
---|
8178 | if(filternum < 0) // if no matching filter act like ecm response is delayed
|
---|
8179 | {
|
---|
8180 | return 2;
|
---|
8181 | }
|
---|
8182 |
|
---|
8183 | if(memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, nullcw, CS_ECMSTORESIZE))
|
---|
8184 | {
|
---|
8185 | demux[demux_id].demux_fd[filternum].lastresult = er->rc; // save last result
|
---|
8186 | char ecmd5[17 * 3];
|
---|
8187 | cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
|
---|
8188 |
|
---|
8189 | cs_log_dbg(D_DVBAPI, "Demuxer %d requested controlword for ecm %s on fd %d",
|
---|
8190 | demux_id, ecmd5, demux[demux_id].demux_fd[filternum].fd);
|
---|
8191 |
|
---|
8192 | uint8_t md5tmp[MD5_DIGEST_LENGTH];
|
---|
8193 | MD5(er->ecm, er->ecmlen, md5tmp);
|
---|
8194 |
|
---|
8195 | // 1 = no response on the ecm we request last for this fd!
|
---|
8196 | ret = (memcmp(demux[demux_id].demux_fd[filternum].lastecmd5, md5tmp, CS_ECMSTORESIZE) != 0 ? 1 : 0);
|
---|
8197 | }
|
---|
8198 |
|
---|
8199 | // Check for null cw
|
---|
8200 | // Skip check for BISS1 - cw could be indeed zero
|
---|
8201 | // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
|
---|
8202 | if(memcmp(er->cw, nullcw, 8) == 0 && memcmp(er->cw + 8, nullcw, 8) == 0 && !caid_is_biss(er->caid))
|
---|
8203 | {
|
---|
8204 | return 5;
|
---|
8205 | }
|
---|
8206 |
|
---|
8207 | struct s_ecmpid *curpid = NULL;
|
---|
8208 | int32_t pid = demux[demux_id].demux_fd[filternum].pidindex;
|
---|
8209 |
|
---|
8210 | if(pid !=-1)
|
---|
8211 | {
|
---|
8212 | curpid = &demux[demux_id].ECMpids[pid];
|
---|
8213 | if(curpid->table == 0) // on change table act like ecm response is found
|
---|
8214 | {
|
---|
8215 | return 3;
|
---|
8216 | }
|
---|
8217 | }
|
---|
8218 |
|
---|
8219 | if(er->rc == E_CACHEEX) // on cache-ex response act like ecm response is found
|
---|
8220 | {
|
---|
8221 | return 4;
|
---|
8222 | }
|
---|
8223 |
|
---|
8224 | return ret;
|
---|
8225 | }
|
---|
8226 |
|
---|
8227 | int32_t dvbapi_get_filternum(int32_t demux_id, ECM_REQUEST *er, int32_t type)
|
---|
8228 | {
|
---|
8229 | if(!er) { return -1; }
|
---|
8230 |
|
---|
8231 | int32_t n;
|
---|
8232 | int32_t fd = -1;
|
---|
8233 |
|
---|
8234 | for(n = 0; n < maxfilter; n++) // determine fd
|
---|
8235 | {
|
---|
8236 | // check for valid and right type (ecm or emm)
|
---|
8237 | if(demux[demux_id].demux_fd[n].fd > 0 && demux[demux_id].demux_fd[n].type == type)
|
---|
8238 | {
|
---|
8239 | if(type == TYPE_ECM && er->srvid != demux[demux_id].program_number)
|
---|
8240 | {
|
---|
8241 | continue;
|
---|
8242 | }
|
---|
8243 |
|
---|
8244 | if((demux[demux_id].demux_fd[n].pid == er->pid) && ((demux[demux_id].demux_fd[n].provid == er->prid)
|
---|
8245 | || demux[demux_id].demux_fd[n].provid == 0 || er->prid == 0) && ((demux[demux_id].demux_fd[n].caid == er->caid)
|
---|
8246 | || (demux[demux_id].demux_fd[n].caid == er->ocaid))) // current ecm pid?
|
---|
8247 | {
|
---|
8248 | fd = demux[demux_id].demux_fd[n].fd; // found!
|
---|
8249 | if(demux[demux_id].demux_fd[n].caid == er->ocaid)
|
---|
8250 | {
|
---|
8251 | // clear ecmd5 hash since betatunneled ecms hash different!
|
---|
8252 | memset(demux[demux_id].demux_fd[n].lastecmd5, 0, CS_ECMSTORESIZE);
|
---|
8253 | }
|
---|
8254 | break;
|
---|
8255 | }
|
---|
8256 | }
|
---|
8257 | }
|
---|
8258 |
|
---|
8259 | if(fd > 0 && demux[demux_id].demux_fd[n].provid == 0)
|
---|
8260 | {
|
---|
8261 | demux[demux_id].demux_fd[n].provid = er->prid; // hack to fill in provid into demuxer
|
---|
8262 | }
|
---|
8263 | return (fd > 0 ? n : fd); // return -1(fd) on not found, on found return filternumber(n)
|
---|
8264 | }
|
---|
8265 |
|
---|
8266 | uint32_t dvbapi_ca_set_pid(int32_t demux_id, int32_t pid, int32_t stream_id, bool use_des, uint32_t msgid)
|
---|
8267 | {
|
---|
8268 | uint32_t idx;
|
---|
8269 | int32_t n;
|
---|
8270 |
|
---|
8271 | if(pid == -1 || pid > demux[demux_id].ECMpidcount)
|
---|
8272 | {
|
---|
8273 | return INDEX_INVALID;
|
---|
8274 | }
|
---|
8275 |
|
---|
8276 | if(demux[demux_id].ECMpids[pid].useMultipleIndices)
|
---|
8277 | {
|
---|
8278 | n = stream_id;
|
---|
8279 | idx = demux[demux_id].ECMpids[pid].index[n];
|
---|
8280 |
|
---|
8281 | if(idx == INDEX_INVALID) // if we have no index for this pid, get one!
|
---|
8282 | {
|
---|
8283 | idx = dvbapi_get_desc_index(demux_id, pid, n);
|
---|
8284 | if(idx == INDEX_INVALID)
|
---|
8285 | {
|
---|
8286 | cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X has no free index",
|
---|
8287 | demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID);
|
---|
8288 |
|
---|
8289 | return INDEX_INVALID;
|
---|
8290 | }
|
---|
8291 |
|
---|
8292 | cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d for stream %d",
|
---|
8293 | demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID, idx, n);
|
---|
8294 | }
|
---|
8295 |
|
---|
8296 | if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n)))
|
---|
8297 | {
|
---|
8298 | dvbapi_set_pid(demux_id, n, idx, true, use_des, msgid); // enable stream pid
|
---|
8299 | }
|
---|
8300 | else
|
---|
8301 | {
|
---|
8302 | dvbapi_set_pid(demux_id, n, idx, false, false, msgid); // disable stream pid
|
---|
8303 | }
|
---|
8304 | }
|
---|
8305 | else
|
---|
8306 | {
|
---|
8307 | idx = demux[demux_id].ECMpids[pid].index[0];
|
---|
8308 |
|
---|
8309 | if(idx == INDEX_INVALID) // if we have no index for this pid, get one!
|
---|
8310 | {
|
---|
8311 | idx = dvbapi_get_desc_index(demux_id, pid, 0);
|
---|
8312 | if(idx == INDEX_INVALID)
|
---|
8313 | {
|
---|
8314 | cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X has no free index",
|
---|
8315 | demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID);
|
---|
8316 |
|
---|
8317 | return INDEX_INVALID;
|
---|
8318 | }
|
---|
8319 |
|
---|
8320 | cs_log_dbg(D_DVBAPI, "Demuxer %d PID: %d CAID: %04X ECMPID: %04X is using index %d",
|
---|
8321 | demux_id, pid, demux[demux_id].ECMpids[pid].CAID, demux[demux_id].ECMpids[pid].ECM_PID, idx);
|
---|
8322 | }
|
---|
8323 |
|
---|
8324 | for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
|
---|
8325 | {
|
---|
8326 | if(!demux[demux_id].ECMpids[pid].streams || ((demux[demux_id].ECMpids[pid].streams & (1 << n)) == (uint) (1 << n)))
|
---|
8327 | {
|
---|
8328 | dvbapi_set_pid(demux_id, n, idx, true, use_des, 0); // enable stream pid
|
---|
8329 | }
|
---|
8330 | else
|
---|
8331 | {
|
---|
8332 | dvbapi_set_pid(demux_id, n, idx, false, false, 0); // disable stream pid
|
---|
8333 | }
|
---|
8334 | }
|
---|
8335 | }
|
---|
8336 | return idx; // return ca index
|
---|
8337 | }
|
---|
8338 |
|
---|
8339 | int8_t update_streampid_list(uint8_t cadevice, uint16_t pid, uint32_t idx, bool use_des)
|
---|
8340 | {
|
---|
8341 | struct s_streampid *listitem, *newlistitem;
|
---|
8342 | LL_ITER itr;
|
---|
8343 | if(!ll_activestreampids)
|
---|
8344 | {
|
---|
8345 | ll_activestreampids = ll_create("ll_activestreampids");
|
---|
8346 | }
|
---|
8347 |
|
---|
8348 | if(idx >= INDEX_MAX)
|
---|
8349 | {
|
---|
8350 | return INVALID_STREAMPID_INDEX;
|
---|
8351 | }
|
---|
8352 |
|
---|
8353 | if(ll_count(ll_activestreampids) > 0)
|
---|
8354 | {
|
---|
8355 | itr = ll_iter_create(ll_activestreampids);
|
---|
8356 | while((listitem = ll_iter_next(&itr)))
|
---|
8357 | {
|
---|
8358 | if(cadevice == listitem->cadevice && pid == listitem->streampid)
|
---|
8359 | {
|
---|
8360 | if((listitem->activeindexers & (1 << idx)) == (uint64_t) (1 << idx))
|
---|
8361 | {
|
---|
8362 | if(cfg.dvbapi_extended_cw_api == 2 && use_des != listitem->use_des)
|
---|
8363 | {
|
---|
8364 | listitem->use_des = use_des;
|
---|
8365 | return FIRST_STREAMPID_INDEX;
|
---|
8366 | }
|
---|
8367 | return FOUND_STREAMPID_INDEX; // match found
|
---|
8368 | }
|
---|
8369 | else
|
---|
8370 | {
|
---|
8371 | listitem->activeindexers |= (1 << idx); // ca + pid found but not this index -> add this index
|
---|
8372 | cs_log_dbg(D_DVBAPI, "Added existing streampid %04X with new index %d to ca%d", pid, idx, cadevice);
|
---|
8373 |
|
---|
8374 | if(cfg.dvbapi_extended_cw_api == 2 && use_des != listitem->use_des)
|
---|
8375 | {
|
---|
8376 | listitem->use_des = use_des;
|
---|
8377 | return FIRST_STREAMPID_INDEX;
|
---|
8378 | }
|
---|
8379 | return ADDED_STREAMPID_INDEX;
|
---|
8380 | }
|
---|
8381 | }
|
---|
8382 | }
|
---|
8383 | }
|
---|
8384 |
|
---|
8385 | if(!cs_malloc(&newlistitem, sizeof(struct s_streampid)))
|
---|
8386 | {
|
---|
8387 | return FIRST_STREAMPID_INDEX; // not sure if this is correct
|
---|
8388 | }
|
---|
8389 |
|
---|
8390 | newlistitem->cadevice = cadevice;
|
---|
8391 | newlistitem->streampid = pid;
|
---|
8392 | newlistitem->activeindexers = (1 << idx);
|
---|
8393 | newlistitem->caindex = idx; // set this index as used to decode on ca device
|
---|
8394 | newlistitem->use_des = use_des;
|
---|
8395 |
|
---|
8396 | ll_append(ll_activestreampids, newlistitem);
|
---|
8397 | cs_log_dbg(D_DVBAPI, "Added new streampid %04X with index %d to ca%d", pid, idx, cadevice);
|
---|
8398 |
|
---|
8399 | return FIRST_STREAMPID_INDEX;
|
---|
8400 | }
|
---|
8401 |
|
---|
8402 | int8_t remove_streampid_from_list(uint8_t cadevice, uint16_t pid, uint32_t idx)
|
---|
8403 | {
|
---|
8404 | struct s_streampid *listitem;
|
---|
8405 | int8_t removed = 0;
|
---|
8406 | LL_ITER itr;
|
---|
8407 |
|
---|
8408 | if(!ll_activestreampids)
|
---|
8409 | {
|
---|
8410 | return NO_STREAMPID_LISTED;
|
---|
8411 | }
|
---|
8412 |
|
---|
8413 | if(idx >= INDEX_MAX)
|
---|
8414 | {
|
---|
8415 | return INVALID_STREAMPID_INDEX;
|
---|
8416 | }
|
---|
8417 |
|
---|
8418 | if(ll_count(ll_activestreampids) > 0)
|
---|
8419 | {
|
---|
8420 | itr = ll_iter_create(ll_activestreampids);
|
---|
8421 | while((listitem = ll_iter_next(&itr)))
|
---|
8422 | {
|
---|
8423 | if(cadevice == listitem->cadevice && pid == listitem->streampid)
|
---|
8424 | {
|
---|
8425 | if(idx == INDEX_DISABLE_ALL)
|
---|
8426 | {
|
---|
8427 | listitem->activeindexers = 0;
|
---|
8428 | removed = 1;
|
---|
8429 | }
|
---|
8430 | else if((listitem->activeindexers & (1 << idx)) == (uint64_t) (1 << idx))
|
---|
8431 | {
|
---|
8432 | listitem->activeindexers &= ~(1 << idx); // flag it as disabled for this index
|
---|
8433 | removed = 1;
|
---|
8434 | }
|
---|
8435 |
|
---|
8436 | if(removed)
|
---|
8437 | {
|
---|
8438 | if(ca_descramblers_used > 0) // it should never go below 0, but you never know
|
---|
8439 | {
|
---|
8440 | ca_descramblers_used--; // decrease number of used descramblers
|
---|
8441 | }
|
---|
8442 | cs_log_dbg(D_DVBAPI, "Remove streampid %04X using indexer %d from ca%d", pid, idx, cadevice);
|
---|
8443 | }
|
---|
8444 |
|
---|
8445 | if(listitem->activeindexers == 0 && removed == 1) // all indexers disabled? -> remove pid from list!
|
---|
8446 | {
|
---|
8447 | ll_iter_remove_data(&itr);
|
---|
8448 | cs_log_dbg(D_DVBAPI, "Removed last indexer of streampid %04X from ca%d", pid, cadevice);
|
---|
8449 | return REMOVED_STREAMPID_LASTINDEX;
|
---|
8450 | }
|
---|
8451 | else if(removed == 1)
|
---|
8452 | {
|
---|
8453 | if(idx != INDEX_DISABLE_ALL && idx != listitem->caindex)
|
---|
8454 | {
|
---|
8455 | return REMOVED_STREAMPID_INDEX;
|
---|
8456 | }
|
---|
8457 | else
|
---|
8458 | {
|
---|
8459 | listitem->caindex = INDEX_INVALID;
|
---|
8460 | cs_log_dbg(D_DVBAPI, "Streampid %04X index %d was used for decoding on ca%d", pid, idx, cadevice);
|
---|
8461 | return REMOVED_DECODING_STREAMPID_INDEX;
|
---|
8462 | }
|
---|
8463 | }
|
---|
8464 | return INVALID_STREAMPID_INDEX;
|
---|
8465 | }
|
---|
8466 | }
|
---|
8467 | }
|
---|
8468 | return NO_STREAMPID_LISTED;
|
---|
8469 | }
|
---|
8470 |
|
---|
8471 | void disable_unused_streampids(int16_t demux_id)
|
---|
8472 | {
|
---|
8473 | int32_t ecmpid = demux[demux_id].pidindex;
|
---|
8474 |
|
---|
8475 | if(ecmpid == -1 // no active ecmpid!
|
---|
8476 | || !ll_activestreampids
|
---|
8477 | || selected_api == STAPI // stapi handles pids itself!
|
---|
8478 | || ll_count(ll_activestreampids) == 0) // no items in list?
|
---|
8479 | {
|
---|
8480 | return;
|
---|
8481 | }
|
---|
8482 |
|
---|
8483 | int32_t j;
|
---|
8484 | if(demux[demux_id].ECMpids[ecmpid].useMultipleIndices == 0)
|
---|
8485 | {
|
---|
8486 | uint32_t idx = demux[demux_id].ECMpids[ecmpid].index[0];
|
---|
8487 | int32_t i, n;
|
---|
8488 | struct s_streampid *listitem;
|
---|
8489 |
|
---|
8490 | // search for old enabled streampids on
|
---|
8491 | // all ca devices that have to be disabled
|
---|
8492 | for(i = 0; i < CA_MAX && idx != INDEX_INVALID; i++)
|
---|
8493 | {
|
---|
8494 | if(!((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
|
---|
8495 | {
|
---|
8496 | continue; // ca is not used by this demuxer
|
---|
8497 | }
|
---|
8498 |
|
---|
8499 | LL_ITER itr;
|
---|
8500 | itr = ll_iter_create(ll_activestreampids);
|
---|
8501 |
|
---|
8502 | while((listitem = ll_iter_next(&itr)))
|
---|
8503 | {
|
---|
8504 | if(i != listitem->cadevice)
|
---|
8505 | {
|
---|
8506 | continue; // ca doesn't match
|
---|
8507 | }
|
---|
8508 |
|
---|
8509 | if(!((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx))))
|
---|
8510 | {
|
---|
8511 | continue; // index doesn't match
|
---|
8512 | }
|
---|
8513 |
|
---|
8514 | for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
|
---|
8515 | {
|
---|
8516 | if(demux[demux_id].ECMpidcount == 0) // FTA? -> disable stream!
|
---|
8517 | {
|
---|
8518 | n = demux[demux_id].STREAMpidcount;
|
---|
8519 | break;
|
---|
8520 | }
|
---|
8521 |
|
---|
8522 | // check if pid matches with current stream pid on demuxer
|
---|
8523 | if(listitem->streampid == demux[demux_id].STREAMpids[n])
|
---|
8524 | {
|
---|
8525 | break;
|
---|
8526 | }
|
---|
8527 | }
|
---|
8528 |
|
---|
8529 | if(n == demux[demux_id].STREAMpidcount) // no match found
|
---|
8530 | {
|
---|
8531 | demux[demux_id].STREAMpids[n] = listitem->streampid; // put it here temporarily!
|
---|
8532 | dvbapi_set_pid(demux_id, n, idx, false, false, 0); // disable this unused streampid
|
---|
8533 | demux[demux_id].STREAMpids[n] = 0; // remove it from temp position!
|
---|
8534 | }
|
---|
8535 | }
|
---|
8536 |
|
---|
8537 | // ECMpidcount != 0 -> skip enabling on fta
|
---|
8538 | for(n = 0; n < demux[demux_id].STREAMpidcount && demux[demux_id].ECMpidcount != 0; n++)
|
---|
8539 | {
|
---|
8540 | ll_iter_reset(&itr);
|
---|
8541 | if(!demux[demux_id].ECMpids[ecmpid].streams || ((demux[demux_id].ECMpids[ecmpid].streams & (1 << n)) == (uint) (1 << n)))
|
---|
8542 | {
|
---|
8543 | while((listitem = ll_iter_next(&itr)))
|
---|
8544 | {
|
---|
8545 | if(i != listitem->cadevice) // ca doesn't match
|
---|
8546 | {
|
---|
8547 | continue;
|
---|
8548 | }
|
---|
8549 |
|
---|
8550 | if(!((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))) // index doesn't match
|
---|
8551 | {
|
---|
8552 | continue;
|
---|
8553 | }
|
---|
8554 |
|
---|
8555 | // check if pid matches with current streampid on demuxer
|
---|
8556 | if(listitem->streampid == demux[demux_id].STREAMpids[n])
|
---|
8557 | {
|
---|
8558 | break;
|
---|
8559 | }
|
---|
8560 | }
|
---|
8561 |
|
---|
8562 | if(!listitem) // if streampid not listed -> enable it!
|
---|
8563 | {
|
---|
8564 | dvbapi_set_pid(demux_id, n, idx, true, false, 0); // enable streampid
|
---|
8565 | }
|
---|
8566 | }
|
---|
8567 | }
|
---|
8568 | }
|
---|
8569 | }
|
---|
8570 | else
|
---|
8571 | {
|
---|
8572 | uint32_t idx = INDEX_INVALID;
|
---|
8573 | int32_t i, n;
|
---|
8574 | uint8_t skip;
|
---|
8575 | struct s_streampid *listitem;
|
---|
8576 |
|
---|
8577 | // search for old enabled streampids
|
---|
8578 | // on all ca devices that have to be disabled
|
---|
8579 | for(i = 0; i < CA_MAX && idx != INDEX_INVALID; i++)
|
---|
8580 | {
|
---|
8581 | if(!((demux[demux_id].ca_mask & (1 << i)) == (uint32_t) (1 << i)))
|
---|
8582 | {
|
---|
8583 | continue; // continue if ca is unused by this demuxer
|
---|
8584 | }
|
---|
8585 |
|
---|
8586 | LL_ITER itr;
|
---|
8587 | itr = ll_iter_create(ll_activestreampids);
|
---|
8588 |
|
---|
8589 | while((listitem = ll_iter_next(&itr)))
|
---|
8590 | {
|
---|
8591 | if(i != listitem->cadevice)
|
---|
8592 | {
|
---|
8593 | continue; // ca doesn't match
|
---|
8594 | }
|
---|
8595 |
|
---|
8596 | for(skip = 1, j = 0; j < MAX_STREAM_INDICES; j++)
|
---|
8597 | {
|
---|
8598 | idx = demux[demux_id].ECMpids[ecmpid].index[j];
|
---|
8599 | if(idx == INDEX_INVALID)
|
---|
8600 | {
|
---|
8601 | continue;
|
---|
8602 | }
|
---|
8603 |
|
---|
8604 | // index match
|
---|
8605 | if((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))
|
---|
8606 | {
|
---|
8607 | skip = 0;
|
---|
8608 | break;
|
---|
8609 | }
|
---|
8610 | }
|
---|
8611 |
|
---|
8612 | if(skip)
|
---|
8613 | {
|
---|
8614 | continue;
|
---|
8615 | }
|
---|
8616 |
|
---|
8617 | for(n = 0; n < demux[demux_id].STREAMpidcount; n++)
|
---|
8618 | {
|
---|
8619 | if(demux[demux_id].ECMpidcount == 0) // FTA? -> disable stream!
|
---|
8620 | {
|
---|
8621 | n = demux[demux_id].STREAMpidcount;
|
---|
8622 | break;
|
---|
8623 | }
|
---|
8624 |
|
---|
8625 | // check if pid matches with current streampid on demuxer
|
---|
8626 | if(listitem->streampid == demux[demux_id].STREAMpids[n])
|
---|
8627 | {
|
---|
8628 | break;
|
---|
8629 | }
|
---|
8630 | }
|
---|
8631 |
|
---|
8632 | if(n == demux[demux_id].STREAMpidcount)
|
---|
8633 | {
|
---|
8634 | demux[demux_id].STREAMpids[n] = listitem->streampid; // put it temp here!
|
---|
8635 | dvbapi_set_pid(demux_id, n, idx, false, false, 0); // no match found so disable this now unused streampid
|
---|
8636 | demux[demux_id].STREAMpids[n] = 0; // remove temp!
|
---|
8637 | }
|
---|
8638 | }
|
---|
8639 |
|
---|
8640 | // ECMpidcount != 0 -> skip enabling on fta
|
---|
8641 | for(n = 0; n < demux[demux_id].STREAMpidcount && demux[demux_id].ECMpidcount != 0; n++)
|
---|
8642 | {
|
---|
8643 | ll_iter_reset(&itr);
|
---|
8644 | if(!demux[demux_id].ECMpids[ecmpid].streams || ((demux[demux_id].ECMpids[ecmpid].streams & (1 << n)) == (uint) (1 << n)))
|
---|
8645 | {
|
---|
8646 | while((listitem = ll_iter_next(&itr)))
|
---|
8647 | {
|
---|
8648 | if(i != listitem->cadevice) // ca doesn't match
|
---|
8649 | {
|
---|
8650 | continue;
|
---|
8651 | }
|
---|
8652 |
|
---|
8653 | for(skip = 1, j = 0; j < MAX_STREAM_INDICES; j++)
|
---|
8654 | {
|
---|
8655 | idx = demux[demux_id].ECMpids[ecmpid].index[j];
|
---|
8656 | if(idx == INDEX_INVALID)
|
---|
8657 | {
|
---|
8658 | continue;
|
---|
8659 | }
|
---|
8660 |
|
---|
8661 | if((listitem->activeindexers & (1 << (idx))) == (uint64_t) (1 << (idx)))
|
---|
8662 | {
|
---|
8663 | skip = 0; // index match
|
---|
8664 | break;
|
---|
8665 | }
|
---|
8666 | }
|
---|
8667 |
|
---|
8668 | if(skip)
|
---|
8669 | {
|
---|
8670 | continue;
|
---|
8671 | }
|
---|
8672 |
|
---|
8673 | // check if pid matches with current streampid on demuxer
|
---|
8674 | if(listitem->streampid == demux[demux_id].STREAMpids[n])
|
---|
8675 | {
|
---|
8676 | break;
|
---|
8677 | }
|
---|
8678 | }
|
---|
8679 |
|
---|
8680 | if(!listitem) // if streampid not listed -> enable it!
|
---|
8681 | {
|
---|
8682 | dvbapi_set_pid(demux_id, n, idx, true, false, 0); // enable streampid
|
---|
8683 | }
|
---|
8684 | }
|
---|
8685 | }
|
---|
8686 | }
|
---|
8687 | }
|
---|
8688 | }
|
---|
8689 |
|
---|
8690 | uint32_t is_ca_used(uint8_t cadevice, int32_t pid)
|
---|
8691 | {
|
---|
8692 | struct s_streampid *listitem;
|
---|
8693 | LL_ITER itr;
|
---|
8694 | if(!ll_activestreampids)
|
---|
8695 | {
|
---|
8696 | return INDEX_INVALID;
|
---|
8697 | }
|
---|
8698 |
|
---|
8699 | if(ll_count(ll_activestreampids) > 0)
|
---|
8700 | {
|
---|
8701 | itr = ll_iter_create(ll_activestreampids);
|
---|
8702 | while((listitem = ll_iter_next(&itr)))
|
---|
8703 | {
|
---|
8704 | // if pid is 0, we match ca device only
|
---|
8705 | if(listitem->cadevice != cadevice || (pid && listitem->streampid != pid))
|
---|
8706 | {
|
---|
8707 | continue;
|
---|
8708 | }
|
---|
8709 |
|
---|
8710 | uint32_t i = 0;
|
---|
8711 | while(listitem->caindex == INDEX_INVALID && i < INDEX_MAX)
|
---|
8712 | {
|
---|
8713 | if((listitem->activeindexers & (1 << i)) == (uint64_t) (1 << i))
|
---|
8714 | {
|
---|
8715 | listitem->caindex = i; // set fresh one
|
---|
8716 | cs_log_dbg(D_DVBAPI, "Streampid %04X is now using index %d for decoding on ca%d", pid, i, cadevice);
|
---|
8717 | break;
|
---|
8718 | }
|
---|
8719 | i++;
|
---|
8720 | }
|
---|
8721 |
|
---|
8722 | if(listitem->caindex == INDEX_INVALID)
|
---|
8723 | {
|
---|
8724 | ll_iter_remove_data(&itr);
|
---|
8725 | return INDEX_INVALID;
|
---|
8726 | }
|
---|
8727 | return listitem->caindex;
|
---|
8728 | }
|
---|
8729 | }
|
---|
8730 | return INDEX_INVALID; // no indexer found for this pid!
|
---|
8731 | }
|
---|
8732 |
|
---|
8733 | uint16_t dvbapi_get_client_proto_version(void)
|
---|
8734 | {
|
---|
8735 | return last_client_proto_version;
|
---|
8736 | }
|
---|
8737 |
|
---|
8738 | const char *dvbapi_get_client_name(void)
|
---|
8739 | {
|
---|
8740 | return last_client_name ? last_client_name : "";
|
---|
8741 | }
|
---|
8742 |
|
---|
8743 | void check_add_emmpid(int32_t demux_id, uint8_t *filter, int32_t l, int32_t emmtype)
|
---|
8744 | {
|
---|
8745 | if(l < 0) { return; }
|
---|
8746 |
|
---|
8747 | uint32_t typtext_idx = 0;
|
---|
8748 | int32_t ret = -1;
|
---|
8749 | const char *typtext[] = { "UNIQUE", "SHARED", "GLOBAL", "UNKNOWN" };
|
---|
8750 |
|
---|
8751 | while(((emmtype >> typtext_idx) & 0x01) == 0 && typtext_idx < sizeof(typtext) / sizeof(const char *))
|
---|
8752 | {
|
---|
8753 | ++typtext_idx;
|
---|
8754 | }
|
---|
8755 |
|
---|
8756 | // filter already in list?
|
---|
8757 | if(is_emmfilter_in_list(filter, demux[demux_id].EMMpids[l].PID, demux[demux_id].EMMpids[l].PROVID, demux[demux_id].EMMpids[l].CAID))
|
---|
8758 | {
|
---|
8759 | cs_log_dbg(D_DVBAPI, "Demuxer %d duplicate emm filter type %s, emmpid: 0x%04X, emmcaid: %04X, emmprovid: %06X -> SKIPPED!",
|
---|
8760 | demux_id,
|
---|
8761 | typtext[typtext_idx],
|
---|
8762 | demux[demux_id].EMMpids[l].PID,
|
---|
8763 | demux[demux_id].EMMpids[l].CAID,
|
---|
8764 | demux[demux_id].EMMpids[l].PROVID);
|
---|
8765 | return;
|
---|
8766 | }
|
---|
8767 |
|
---|
8768 | if(demux[demux_id].emm_filter < demux[demux_id].max_emm_filter) // can this filter be started?
|
---|
8769 | {
|
---|
8770 | // try to activate this emmfilter
|
---|
8771 | ret = dvbapi_set_filter(demux_id,
|
---|
8772 | selected_api,
|
---|
8773 | demux[demux_id].EMMpids[l].PID,
|
---|
8774 | demux[demux_id].EMMpids[l].CAID,
|
---|
8775 | demux[demux_id].EMMpids[l].PROVID,
|
---|
8776 | filter,
|
---|
8777 | filter + 16,
|
---|
8778 | 0,
|
---|
8779 | demux[demux_id].pidindex,
|
---|
8780 | TYPE_EMM, 1);
|
---|
8781 | }
|
---|
8782 |
|
---|
8783 | if(ret != -1) // -1 if maxfilter reached or filter start error!
|
---|
8784 | {
|
---|
8785 | if(demux[demux_id].emm_filter == -1) // -1: first run of emm filtering on this demuxer
|
---|
8786 | {
|
---|
8787 | demux[demux_id].emm_filter = 0;
|
---|
8788 | }
|
---|
8789 | demux[demux_id].emm_filter++; // increase total active filters
|
---|
8790 |
|
---|
8791 | cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d started emm filter type %s, pid: 0x%04X",
|
---|
8792 | demux_id, typtext[typtext_idx], demux[demux_id].EMMpids[l].PID);
|
---|
8793 | return;
|
---|
8794 | }
|
---|
8795 | else // not set successful, so add it to the list for try again later on!
|
---|
8796 | {
|
---|
8797 | 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);
|
---|
8798 | cs_log_dump_dbg(D_DVBAPI, filter, 32, "Demuxer %d added inactive emm filter type %s, pid: 0x%04X",
|
---|
8799 | demux_id, typtext[typtext_idx], demux[demux_id].EMMpids[l].PID);
|
---|
8800 | }
|
---|
8801 | return;
|
---|
8802 | }
|
---|
8803 |
|
---|
8804 | void rotate_emmfilter(int32_t demux_id)
|
---|
8805 | {
|
---|
8806 | // emm filter iteration
|
---|
8807 | if(!ll_emm_active_filter)
|
---|
8808 | {
|
---|
8809 | ll_emm_active_filter = ll_create("ll_emm_active_filter");
|
---|
8810 | }
|
---|
8811 |
|
---|
8812 | if(!ll_emm_inactive_filter)
|
---|
8813 | {
|
---|
8814 | ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
|
---|
8815 | }
|
---|
8816 |
|
---|
8817 | if(!ll_emm_pending_filter)
|
---|
8818 | {
|
---|
8819 | ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
|
---|
8820 | }
|
---|
8821 |
|
---|
8822 | uint32_t filter_count = ll_count(ll_emm_active_filter) + ll_count(ll_emm_inactive_filter);
|
---|
8823 | if(demux[demux_id].max_emm_filter > 0 && ll_count(ll_emm_inactive_filter) > 0 && filter_count > demux[demux_id].max_emm_filter)
|
---|
8824 | {
|
---|
8825 | int32_t filter_queue = ll_count(ll_emm_inactive_filter);
|
---|
8826 | int32_t stopped = 0, started = 0;
|
---|
8827 | struct timeb now;
|
---|
8828 | cs_ftime(&now);
|
---|
8829 | struct s_emm_filter *filter_item;
|
---|
8830 | LL_ITER itr;
|
---|
8831 | itr = ll_iter_create(ll_emm_active_filter);
|
---|
8832 |
|
---|
8833 | while((filter_item = ll_iter_next(&itr)) != NULL)
|
---|
8834 | {
|
---|
8835 | if(!ll_count(ll_emm_inactive_filter) || started == filter_queue)
|
---|
8836 | {
|
---|
8837 | break;
|
---|
8838 | }
|
---|
8839 |
|
---|
8840 | int64_t gone = comp_timeb(&now, &filter_item->time_started);
|
---|
8841 | if(gone > 45 * 1000)
|
---|
8842 | {
|
---|
8843 | struct s_dvbapi_priority *forceentry = dvbapi_check_prio_match_emmpid(filter_item->demux_id, filter_item->caid, filter_item->provid, 'p');
|
---|
8844 | if(!forceentry || (forceentry && !forceentry->force))
|
---|
8845 | {
|
---|
8846 | // stop active filter and add to pending list
|
---|
8847 | dvbapi_stop_filternum(filter_item->demux_id, filter_item->num - 1, 0);
|
---|
8848 | ll_iter_remove_data(&itr);
|
---|
8849 | add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, -1, false);
|
---|
8850 | stopped++;
|
---|
8851 | }
|
---|
8852 | }
|
---|
8853 |
|
---|
8854 | int32_t ret;
|
---|
8855 | if(stopped > started) // we have room for new filters, try to start an inactive emmfilter!
|
---|
8856 | {
|
---|
8857 | struct s_emm_filter *filter_item2;
|
---|
8858 | LL_ITER itr2 = ll_iter_create(ll_emm_inactive_filter);
|
---|
8859 | while((filter_item2 = ll_iter_next(&itr2)))
|
---|
8860 | {
|
---|
8861 | ret = dvbapi_set_filter(filter_item2->demux_id,
|
---|
8862 | selected_api,
|
---|
8863 | filter_item2->pid,
|
---|
8864 | filter_item2->caid,
|
---|
8865 | filter_item2->provid,
|
---|
8866 | filter_item2->filter,
|
---|
8867 | filter_item2->filter + 16,
|
---|
8868 | 0,
|
---|
8869 | demux[filter_item2->demux_id].pidindex,
|
---|
8870 | TYPE_EMM, 1);
|
---|
8871 | if(ret != -1)
|
---|
8872 | {
|
---|
8873 | ll_iter_remove_data(&itr2);
|
---|
8874 | started++;
|
---|
8875 | break;
|
---|
8876 | }
|
---|
8877 | }
|
---|
8878 | }
|
---|
8879 | }
|
---|
8880 |
|
---|
8881 | itr = ll_iter_create(ll_emm_pending_filter);
|
---|
8882 | while((filter_item = ll_iter_next(&itr)) != NULL) // move pending filters to inactive
|
---|
8883 | {
|
---|
8884 | add_emmfilter_to_list(filter_item->demux_id, filter_item->filter, filter_item->caid, filter_item->provid, filter_item->pid, 0, false);
|
---|
8885 | ll_iter_remove_data(&itr);
|
---|
8886 | }
|
---|
8887 | }
|
---|
8888 | }
|
---|
8889 |
|
---|
8890 | int32_t filtermatch(uint8_t *buffer, int32_t filter_num, int32_t demux_id, int32_t len)
|
---|
8891 | {
|
---|
8892 | int32_t i, k, match;
|
---|
8893 | uint8_t flt, mask;
|
---|
8894 |
|
---|
8895 | match = 1;
|
---|
8896 | for(i = 0, k = 0; i < 16 && match; i++, k++)
|
---|
8897 | {
|
---|
8898 | mask = demux[demux_id].demux_fd[filter_num].mask[i];
|
---|
8899 | if(k == 1) // skip len bytes
|
---|
8900 | {
|
---|
8901 | k += 2;
|
---|
8902 | }
|
---|
8903 |
|
---|
8904 | if(!mask)
|
---|
8905 | {
|
---|
8906 | continue;
|
---|
8907 | }
|
---|
8908 |
|
---|
8909 | flt = (demux[demux_id].demux_fd[filter_num].filter[i]&mask);
|
---|
8910 | 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",
|
---|
8911 | 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);
|
---|
8912 |
|
---|
8913 | if(k <= len)
|
---|
8914 | {
|
---|
8915 | match = (flt == (buffer[k] & mask));
|
---|
8916 | }
|
---|
8917 | else
|
---|
8918 | {
|
---|
8919 | match = 0;
|
---|
8920 | }
|
---|
8921 | }
|
---|
8922 | return (match && i == 16); // 0 = delivered data does not match with filter, 1 = delivered data matches with filter
|
---|
8923 | }
|
---|
8924 |
|
---|
8925 | /*
|
---|
8926 | * protocol structure
|
---|
8927 | */
|
---|
8928 | void module_dvbapi(struct s_module *ph)
|
---|
8929 | {
|
---|
8930 | ph->desc = "dvbapi";
|
---|
8931 | ph->type = MOD_CONN_SERIAL;
|
---|
8932 | ph->listenertype = LIS_DVBAPI;
|
---|
8933 | #if defined(WITH_AZBOX)
|
---|
8934 | ph->s_handler = azbox_handler;
|
---|
8935 | ph->send_dcw = azbox_send_dcw;
|
---|
8936 | #elif defined(WITH_MCA)
|
---|
8937 | ph->s_handler = mca_handler;
|
---|
8938 | ph->send_dcw = mca_send_dcw;
|
---|
8939 | 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
|
---|
8940 | #else
|
---|
8941 | ph->s_handler = dvbapi_handler;
|
---|
8942 | ph->send_dcw = dvbapi_send_dcw;
|
---|
8943 | #endif
|
---|
8944 | }
|
---|
8945 | #endif // HAVE_DVBAPI
|
---|