source: trunk/module-dvbapi.c@ 10652

Last change on this file since 10652 was 10652, checked in by manio, 7 years ago

dvbapi: add support for ECMINFO in network mode (bump proto to v2)

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