-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-dvbapi.c
9323 lines (8217 loc) · 256 KB
/
module-dvbapi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define MODULE_LOG_PREFIX "dvbapi"
#include "globals.h"
#ifdef HAVE_DVBAPI
#include "module-dvbapi.h"
#include "module-cacheex.h"
#include "module-dvbapi-azbox.h"
#include "module-dvbapi-mca.h"
#include "module-dvbapi-coolapi.h"
#include "module-dvbapi-stapi.h"
#include "module-dvbapi-chancache.h"
#include "module-stat.h"
#include "oscam-chk.h"
#include "oscam-client.h"
#include "oscam-config.h"
#include "oscam-ecm.h"
#include "oscam-emm.h"
#include "oscam-files.h"
#include "oscam-net.h"
#include "oscam-reader.h"
#include "oscam-string.h"
#include "oscam-time.h"
#include "oscam-work.h"
#include "reader-irdeto.h"
#include "cscrypt/md5.h"
extern int32_t exit_oscam;
#if defined (__CYGWIN__)
#define F_NOTIFY 0
#define F_SETSIG 0
#define DN_MODIFY 0
#define DN_CREATE 0
#define DN_DELETE 0
#define DN_MULTISHOT 0
#endif
static const char *get_stream_type_txt(uint8_t stream_type)
{
switch(stream_type)
{
case 0x00: return "invalid";
case 0x01: return "MPEG-1 video";
case 0x02: return "MPEG-2 video"; // MPEG-1 (constrained parameter) or MPEG-2 video
case 0x03: return "MPEG-1 audio"; // MP1, MP2, MP3
case 0x04: return "MPEG-2 audio"; // MP1, MP2, MP3
case 0x05: return "MPEG-2 private sections";
case 0x06: return "MPEG-2 PES private data"; // AC-3, Enhanced AC-3, AC-4, DTS(-HD) audio, subtitles, etc (DVB) (depends on descriptor)
case 0x07: return "MHEG data";
case 0x08: return "DSM-CC data";
case 0x09: return "MPEG-2 over ATM data";
case 0x0A: return "DSM-CC data";
case 0x0B: return "DSM-CC data";
case 0x0C: return "DSM-CC data";
case 0x0D: return "DSM-CC data";
case 0x0E: return "MPEG-2 auxiliary data";
case 0x0F: return "MPEG-2 audio"; // AAC
case 0x10: return "MPEG-4 video";
case 0x11: return "MPEG-4 audio"; // AAC, HE AAC and AAC v2
case 0x12: return "MPEG-4 PES data";
case 0x13: return "MPEG-4 data";
case 0x14: return "DSM-CC data";
case 0x15: return "MPEG-7 MetaData";
case 0x16: return "MPEG-7 MetaData";
case 0x17: return "MPEG-7 MetaData";
case 0x18: return "MPEG-7 MetaData";
case 0x19: return "MPEG-7 MetaData";
case 0x1A: return "MPEG-2 IPMP data";
case 0x1B: return "AVC video";
case 0x1C: return "MPEG-4 audio"; // DST, ALS, SLS
case 0x24: return "HEVC video";
case 0x25: return "HEVC subset video";
case 0x2D: return "MPEG-H 3D audio"; // main stream
case 0x2E: return "MPEG-H 3D audio"; // auxiliary stream
case 0x42: return "Chinese video";
case 0x7F: return "IPMP data";
case 0x81: return "AC-3 audio (ATSC)"; // with descriptor tag 0x81
case 0x86: return "SCTE 35 data";
case 0x87: return "enhanced AC-3 audio (ATSC)";
//case 0x88: return "DTS-HD audio (ATSC 2.0)"; // fixme: has ATSC 2.0 ever been used?
//case 0x??: return "AC-4 audio (ATSC 3.0)"; // fixme: add the actual value when it gets published
//case 0x??: return "MPEG-H 3D audio (ATSC 3.0)"; // fixme: add the actual value when it gets published
case 0xD1: return "BBC Dirac video";
case 0xEA: return "VC-1 video";
default: return "user private";
}
}
static const char *get_descriptor_tag_txt(uint8_t descriptor_tag)
{
switch(descriptor_tag)
{
// Valid in all MPEG contexts:
case 0x00: return "reserved";
case 0x01: return "forbidden";
case 0x02: return "video stream";
case 0x03: return "audio stream";
case 0x04: return "hierarchy";
case 0x05: return "registration";
case 0x06: return "data stream alignment";
case 0x07: return "target background grid";
case 0x08: return "video window";
case 0x09: return "CA";
case 0x0A: return "ISO 639 language";
case 0x0B: return "system clock";
case 0x0C: return "multiplex buffer utilization";
case 0x0D: return "copyright";
case 0x0E: return "maximum bitrate";
case 0x0F: return "private data indicator";
case 0x10: return "smoothing buffer";
case 0x11: return "STD";
case 0x12: return "IBP";
case 0x13: return "DSM-CC carousel identifier";
case 0x14: return "DSM-CC association tag";
case 0x15: return "DSM-CC deferred association tags";
case 0x17: return "DSM-CC NPT reference";
case 0x18: return "DSM-CC NPT endpoint";
case 0x19: return "DSM-CC stream mode";
case 0x1A: return "DSM-CC stream event";
case 0x1B: return "MPEG-4 video";
case 0x1C: return "MPEG-4 audio";
case 0x1D: return "IOD";
case 0x1E: return "SL";
case 0x1F: return "FMC";
case 0x20: return "External ES id";
case 0x21: return "MuxCode";
case 0x22: return "FmxBufferSize";
case 0x23: return "MultiplexBuffer";
case 0x24: return "Content labeling";
case 0x25: return "Metadata association";
case 0x26: return "Metadata";
case 0x27: return "Metadata STD";
case 0x28: return "AVC video";
case 0x29: return "MPEG-2 IPMP";
case 0x2A: return "AVC timing and HRD";
case 0x2B: return "MPEG-2 AAC Audio";
case 0x2C: return "FlexMuxTiming";
case 0x2D: return "MPEG-4 Text";
case 0x2E: return "MPEG-4 Audio Extension";
case 0x2F: return "Auxiliary Video Stream";
case 0x30: return "SVC Extension";
case 0x31: return "MVC Extension";
case 0x32: return "J2K Video";
case 0x33: return "MVC Operation Point";
case 0x34: return "MPEG-2 Stereoscopic Video Format";
case 0x35: return "Stereoscopic Program Info";
case 0x36: return "Stereoscopic Video Info";
case 0x37: return "Transport Profile";
case 0x38: return "HEVC Video";
case 0x3F: return "MPEG-2 Extension";
// Valid in DVB context:
case 0x45: return "VBI data";
case 0x46: return "VBI teletext";
case 0x51: return "mosaic";
case 0x52: return "stream identifier";
case 0x56: return "teletext";
case 0x59: return "subtitling"; // with stream type 0x06
case 0x5F: return "private data specifier";
case 0x60: return "service move";
case 0x65: return "scrambling";
case 0x66: return "data broadcast id";
case 0x6A: return "AC-3"; // with stream type 0x06
case 0x6B: return "ancillary data";
case 0x6F: return "application signalling";
case 0x70: return "adaptation field data";
case 0x74: return "related content";
case 0x78: return "ECM repetition rate";
case 0x7A: return "enhanced AC-3"; // with stream type 0x06
case 0x7B: return "DTS"; // with stream type 0x06
case 0x7C: return "AAC"; // with stream type 0x06
case 0x7D: return "XAIT location";
case 0x7F: return "DVB extension";
// Valid in ATSC context:
case 0x81: return "AC-3"; // with stream type 0x81
case 0xCC: return "enhanced AC-3"; // with stream type 0x87
default: return "user private";
}
}
static const char *get_extension_descriptor_txt(uint8_t extension_tag)
{
switch(extension_tag)
{
case 0x02: return "CP";
case 0x06: return "supplementary audio";
case 0x0E: return "DTS-HD"; // with stream type 0x06
case 0x0F: return "DTS Neural"; // with stream type 0x06
case 0x11: return "T2MI";
case 0x13: return "URI linkage";
case 0x15: return "AC-4"; // with stream type 0x06
case 0x18: return "protection message";
case 0x19: return "audio preselection";
case 0x20: return "TTML subtitling"; // (could become 0x1A, value in A038 draft seems weird)
case 0x21: return "DTS-UHD"; // with stream type 0x06 (could become 0x1B, value in A038 draft seems weird)
default: return "Undefined";
}
}
void flush_read_fd(int32_t demux_id, int32_t num, int fd)
{
if(!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
{
cs_log_dbg(D_DVBAPI,"Demuxer %d flushing stale input data of filter %d (fd:%d)", demux_id, num + 1, fd);
fd_set rd;
struct timeval t;
char buff[100];
t.tv_sec = 0;
t.tv_usec = 0;
FD_ZERO(&rd);
FD_SET(fd, &rd);
while(select(fd + 1, &rd, NULL, NULL, &t) > 0)
{
if(read(fd, buff, 100)) { ; }
}
}
}
static int dvbapi_ioctl(int fd, uint32_t request, ...)
{
int ret = 0;
va_list args;
va_start(args, request);
if(!(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO))
{
void *param = va_arg(args, void *);
ret = ioctl(fd, request, param);
}
else
{
switch(request)
{
case DMX_SET_FILTER:
{
struct dmx_sct_filter_params *sFP = va_arg(args, struct dmx_sct_filter_params *);
//fix filter for samygo
//note: we only have 14 available filter bytes (instead of 16) on samygo
memmove(&sFP->filter.filter[3], &sFP->filter.filter[1], 13);
memset(&sFP->filter.filter[1], 0, 2);
memmove(&sFP->filter.mask[3], &sFP->filter.mask[1], 13);
memset(&sFP->filter.mask[1], 0, 2);
// prepare packet
uint8_t packet[sizeof(request) + sizeof(struct dmx_sct_filter_params)];
memcpy(&packet, &request, sizeof(request));
memcpy(&packet[sizeof(request)], sFP, sizeof(struct dmx_sct_filter_params));
ret = send(fd, packet, sizeof(packet), 0);
break;
}
case DMX_SET_FILTER1:
{
cs_log("error: samygo does not support DMX_SET_FILTER1");
ret = -1;
break;
}
case DMX_STOP:
{
ret = send(fd, &request, sizeof(request), 0);
ret = 1;
break;
}
case CA_GET_DESCR_INFO:
{
ca_descr_info_t *ca_descr_info = va_arg(args, ca_descr_info_t *);
// preparing packet
uint8_t packet[sizeof(request) + sizeof(ca_descr_info_t)];
memcpy(&packet[0], &request, sizeof(request));
memcpy(&packet[sizeof(request)], ca_descr_info, sizeof(ca_descr_info_t));
// sending data to UDP
ret = send(fd, &packet[0], sizeof(packet), 0);
break;
}
case CA_SET_DESCR:
{
ca_descr_t *ca_descr = va_arg(args, ca_descr_t *);
// preparing packet
uint8_t packet[sizeof(request) + sizeof(ca_descr_t)];
memcpy(&packet[0], &request, sizeof(request));
memcpy(&packet[sizeof(request)], ca_descr, sizeof(ca_descr_t));
// sending data to UDP
ret = send(fd, &packet[0], sizeof(packet), 0);
break;
}
case CA_SET_PID:
{
ca_pid_t *ca_pid2 = va_arg(args, ca_pid_t *);
// preparing packet
uint8_t packet[sizeof(request) + sizeof(ca_pid_t)];
memcpy(&packet[0], &request, sizeof(request));
memcpy(&packet[sizeof(request)], ca_pid2, sizeof(ca_pid_t));
// sending data to UDP
ret = send(fd, &packet[0], sizeof(packet), 0);
break;
}
case CA_SET_DESCR_MODE:
{
cs_log("error: samygo does not support CA_SET_DESCR_MODE");
ret = -1;
break;
}
case CA_SET_DESCR_DATA:
{
cs_log("error: samygo does not support CA_SET_DESCR_DATA");
ret = -1;
break;
}
}
if(ret > 0) // send() may return larger than 1
{
ret = 1;
}
}
#if defined(__powerpc__)
// Old dm500 boxes (ppc old) are using broken kernel, se we need some fixups
switch (request)
{
case DMX_STOP:
case CA_SET_DESCR:
case CA_SET_PID:
ret = 1;
}
#endif
// FIXME: Workaround for su980 bug
// See: https://board.streamboard.tv/?postid=533940
if(boxtype_is("su980"))
{
ret = 1;
}
va_end(args);
return ret;
}
// tunemm_caid_map
#define FROM_TO 0
#define TO_FROM 1
int32_t pausecam = 0, disable_pmt_files = 0;
DEMUXTYPE demux[MAX_DEMUX];
struct s_dvbapi_priority *dvbapi_priority;
struct s_client *dvbapi_client;
const char *boxdesc[] = { "none", "dreambox", "duckbox", "ufs910", "dbox2", "ipbox", "ipbox-pmt",
"dm7000", "qboxhd", "coolstream", "neumo", "pc", "pc-nodmx", "samygo" };
// when updating devices[BOX_COUNT] make sure to update these index defines
#define BOX_INDEX_QBOXHD 0
#define BOX_INDEX_DREAMBOX_DVBAPI3 1
#define BOX_INDEX_COOLSTREAM 6
static const struct box_devices devices[BOX_COUNT] =
{
/* QboxHD (dvb-api-3)*/ { "/tmp/virtual_adapter/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
/* dreambox (dvb-api-3)*/ { "/dev/dvb/adapter%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
/* wetek (dvb-api-3)*/ { "/dev/dvb%d.", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
/* dreambox (dvb-api-1)*/ { "/dev/dvb/card%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_1 },
/* neumo (dvb-api-1)*/ { "/dev/", "demuxapi", "demuxapi", "/tmp/camd.socket", DVBAPI_1 },
#ifdef WITH_STAPI5
/* sh4 (stapi5)*/ { "/dev/stapi/", "stpti5_ioctl", "stpti5_ioctl", "/tmp/camd.socket", STAPI },
#else
/* sh4 (stapi)*/ { "/dev/stapi/", "stpti4_ioctl", "stpti4_ioctl", "/tmp/camd.socket", STAPI },
#endif
/* coolstream*/ { "/dev/cnxt/", "null", "null", "/tmp/camd.socket", COOLAPI },
};
static int32_t selected_box = -1;
static int32_t selected_api = -1;
static int32_t maxfilter = MAX_FILTER;
static int32_t dir_fd = -1;
static uint16_t last_client_proto_version = 0;
static char *last_client_name = NULL;
static uint32_t ca_descramblers_total = 0; // total number of available descramblers in box
static uint32_t ca_descramblers_used = 0; // total number of used descramblers during decoding
static int32_t ca_fd[CA_MAX]; // holds fd handle of all ca devices (0 not in use)
static LLIST *ll_activestreampids; // list of all enabled streampids on ca devices
static int32_t unassoc_fd[MAX_DEMUX];
bool is_dvbapi_usr(char *usr)
{
return streq(cfg.dvbapi_usr, usr);
}
struct s_emm_filter
{
int32_t demux_id;
uint8_t filter[32];
uint16_t caid;
uint32_t provid;
uint16_t pid;
uint32_t num;
struct timeb time_started;
};
static LLIST *ll_emm_active_filter;
static LLIST *ll_emm_inactive_filter;
static LLIST *ll_emm_pending_filter;
int32_t add_emmfilter_to_list(int32_t demux_id, uint8_t *filter, uint16_t caid, uint32_t provid, uint16_t emmpid, int32_t num, bool enable)
{
if(!ll_emm_active_filter)
{
ll_emm_active_filter = ll_create("ll_emm_active_filter");
}
if(!ll_emm_inactive_filter)
{
ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
}
if(!ll_emm_pending_filter)
{
ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
}
struct s_emm_filter *filter_item;
if(!cs_malloc(&filter_item, sizeof(struct s_emm_filter)))
{
return 0;
}
filter_item->demux_id = demux_id;
memcpy(filter_item->filter, filter, 32);
filter_item->caid = caid;
filter_item->provid = provid;
filter_item->pid = emmpid;
filter_item->num = num;
if(enable)
{
cs_ftime(&filter_item->time_started);
}
else
{
memset(&filter_item->time_started, 0, sizeof(filter_item->time_started));
}
if(num > 0)
{
ll_append(ll_emm_active_filter, filter_item);
cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d added to active emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
}
else if(num < 0)
{
ll_append(ll_emm_pending_filter, filter_item);
cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to pending emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
}
else
{
ll_append(ll_emm_inactive_filter, filter_item);
cs_log_dbg(D_DVBAPI, "Demuxer %d Filter added to inactive emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
filter_item->demux_id, filter_item->caid, filter_item->provid, filter_item->pid);
}
return 1;
}
int32_t is_emmfilter_in_list_internal(LLIST *ll, uint8_t *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
{
struct s_emm_filter *filter_item;
LL_ITER itr;
if(ll_count(ll) > 0)
{
itr = ll_iter_create(ll);
while((filter_item = ll_iter_next(&itr)) != NULL)
{
if(!memcmp(filter_item->filter, filter, 32) && (filter_item->pid == emmpid)
&& (filter_item->provid == provid) && (filter_item->caid == caid))
{
return 1;
}
}
}
return 0;
}
int32_t is_emmfilter_in_list(uint8_t *filter, uint16_t emmpid, uint32_t provid, uint16_t caid)
{
if(!ll_emm_active_filter)
{
ll_emm_active_filter = ll_create("ll_emm_active_filter");
}
if(!ll_emm_inactive_filter)
{
ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
}
if(!ll_emm_pending_filter)
{
ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
}
if(is_emmfilter_in_list_internal(ll_emm_active_filter, filter, emmpid, provid, caid)
|| is_emmfilter_in_list_internal(ll_emm_inactive_filter, filter, emmpid, provid, caid)
|| is_emmfilter_in_list_internal(ll_emm_pending_filter, filter, emmpid, provid, caid))
{
return 1;
}
return 0;
}
struct s_emm_filter *get_emmfilter_by_filternum_internal(LLIST *ll, int32_t demux_id, uint32_t num)
{
struct s_emm_filter *filter;
LL_ITER itr;
if(ll_count(ll) > 0)
{
itr = ll_iter_create(ll);
while((filter = ll_iter_next(&itr)))
{
if(filter->demux_id == demux_id && filter->num == num)
{
return filter;
}
}
}
return NULL;
}
struct s_emm_filter *get_emmfilter_by_filternum(int32_t demux_id, uint32_t num)
{
if(!ll_emm_active_filter)
{
ll_emm_active_filter = ll_create("ll_emm_active_filter");
}
if(!ll_emm_inactive_filter)
{
ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter");
}
if(!ll_emm_pending_filter)
{
ll_emm_pending_filter = ll_create("ll_emm_pending_filter");
}
struct s_emm_filter *emm_filter = NULL;
emm_filter = get_emmfilter_by_filternum_internal(ll_emm_active_filter, demux_id, num);
if(emm_filter)
{
return emm_filter;
}
emm_filter = get_emmfilter_by_filternum_internal(ll_emm_inactive_filter, demux_id, num);
if(emm_filter)
{
return emm_filter;
}
emm_filter = get_emmfilter_by_filternum_internal(ll_emm_pending_filter, demux_id, num);
if(emm_filter)
{
return emm_filter;
}
return NULL;
}
int8_t remove_emmfilter_from_list_internal(LLIST *ll, int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
{
struct s_emm_filter *filter;
LL_ITER itr;
if(ll_count(ll) > 0)
{
itr = ll_iter_create(ll);
while((filter = ll_iter_next(&itr)))
{
if(filter->demux_id == demux_id && filter->caid == caid && filter->provid == provid && filter->pid == pid && filter->num == num)
{
ll_iter_remove_data(&itr);
return 1;
}
}
}
return 0;
}
void remove_emmfilter_from_list(int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
{
if((ll_emm_active_filter && remove_emmfilter_from_list_internal(ll_emm_active_filter, demux_id, caid, provid, pid, num))
|| (ll_emm_inactive_filter && remove_emmfilter_from_list_internal(ll_emm_inactive_filter, demux_id, caid, provid, pid, num))
|| (ll_emm_pending_filter && remove_emmfilter_from_list_internal(ll_emm_pending_filter, demux_id, caid, provid, pid, num)))
{
return;
}
}
void dvbapi_net_add_str(uint8_t *packet, int *size, const char *str)
{
uint8_t *str_len = &packet[*size]; // string length
*size += 1;
*str_len = snprintf((char *) &packet[*size], DVBAPI_MAX_PACKET_SIZE - *size, "%s", str);
*size += *str_len;
}
int32_t dvbapi_net_send(uint32_t request, int32_t socket_fd, uint32_t msgid, int32_t demux_id,
uint32_t filter_number, uint8_t *data, struct s_client *client, ECM_REQUEST *er, uint16_t client_proto_version)
{
uint8_t packet[DVBAPI_MAX_PACKET_SIZE]; // maximum possible packet size
int32_t size = 0;
uint32_t u32;
// not connected?
if(socket_fd <= 0)
{
return 0;
}
// preparing packet - header
// in old protocol client expect this first byte as adapter index,
// changed in the new protocol to be always after request type (opcode)
if(client_proto_version <= 0)
{
packet[size++] = demux[demux_id].adapter_index; // adapter index - 1 byte
}
else if(client_proto_version >= 3)
{
packet[size++] = 0xa5; // message start
u32 = htonl(msgid);
memcpy(&packet[size], &u32, 4);
size += 4;
}
// type of request
u32 = request;
if(client_proto_version >= 1)
{
u32 = htonl(u32);
}
memcpy(&packet[size], &u32, 4);
size += 4;
// preparing packet - adapter index for proto >= 1
if((request != DVBAPI_SERVER_INFO) && client_proto_version >= 1)
{
packet[size++] = demux[demux_id].adapter_index; // adapter index - 1 byte
}
// struct with data
switch(request)
{
case DVBAPI_SERVER_INFO:
{
int16_t proto_version = htons(DVBAPI_PROTOCOL_VERSION); // our protocol version
char capabilities[128];
memset(capabilities, 0, sizeof(capabilities));
memcpy(&packet[size], &proto_version, 2);
size += 2;
uint8_t *info_len = &packet[size]; // info string length
size += 1;
if(cfg.dvbapi_extended_cw_api == 1)
{
cs_strncat(capabilities, ",e1mk", sizeof(capabilities)); // extended cw, key follows mode - supports CSA, DES, AES128
}
if(cfg.dvbapi_extended_cw_api == 2)
{
cs_strncat(capabilities, ",e2", sizeof(capabilities)); // usage of DES algo signalled through PID index - CSA and DES only
}
*info_len = snprintf((char *) &packet[size], sizeof(packet) - size, "OSCam v%s, build r%s (%s); %s",
CS_VERSION, CS_SVN_VERSION, CS_TARGET, capabilities + 1);
size += *info_len;
break;
}
case DVBAPI_ECM_INFO:
{
if(er->rc >= E_NOTFOUND)
{
return 0;
}
int8_t hops = 0;
uint16_t sid = htons(er->srvid); // service ID (program number)
memcpy(&packet[size], &sid, 2);
size += 2;
uint16_t caid = htons(er->caid); // CAID
memcpy(&packet[size], &caid, 2);
size += 2;
uint16_t pid = htons(er->pid); // PID
memcpy(&packet[size], &pid, 2);
size += 2;
uint32_t prid = htonl(er->prid); // Provider ID
memcpy(&packet[size], &prid, 4);
size += 4;
uint32_t ecmtime = htonl(client->cwlastresptime); // ECM time
memcpy(&packet[size], &ecmtime, 4);
size += 4;
dvbapi_net_add_str(packet, &size, get_cardsystem_desc_by_caid(er->caid)); // cardsystem name
switch (er->rc)
{
case E_FOUND:
if(er->selected_reader)
{
dvbapi_net_add_str(packet, &size, er->selected_reader->label); // reader
if(is_network_reader(er->selected_reader))
{
dvbapi_net_add_str(packet, &size, er->selected_reader->device); // from
}
else
{
dvbapi_net_add_str(packet, &size, "local"); // from
}
dvbapi_net_add_str(packet, &size, reader_get_type_desc(er->selected_reader, 1)); // protocol
hops = er->selected_reader->currenthops;
}
break;
case E_CACHE1:
dvbapi_net_add_str(packet, &size, "Cache"); // reader
dvbapi_net_add_str(packet, &size, "cache1"); // from
dvbapi_net_add_str(packet, &size, "none"); // protocol
break;
case E_CACHE2:
dvbapi_net_add_str(packet, &size, "Cache"); // reader
dvbapi_net_add_str(packet, &size, "cache2"); // from
dvbapi_net_add_str(packet, &size, "none"); // protocol
break;
case E_CACHEEX:
dvbapi_net_add_str(packet, &size, "Cache"); // reader
dvbapi_net_add_str(packet, &size, "cache3"); // from
dvbapi_net_add_str(packet, &size, "none"); // protocol
break;
}
packet[size++] = hops; // hops
break;
}
case DVBAPI_CA_GET_DESCR_INFO:
{
int sct_cadescr_info_size = sizeof(ca_descr_info_t);
if(client_proto_version >= 1)
{
ca_descr_info_t *cadescr_info = (ca_descr_info_t *) data;
cadescr_info->num = htonl(cadescr_info->num);
cadescr_info->type = htonl(cadescr_info->type);
}
memcpy(&packet[size], data, sct_cadescr_info_size);
size += sct_cadescr_info_size;
break;
}
case DVBAPI_CA_SET_DESCR:
{
int sct_cadescr_size = sizeof(ca_descr_t);
if(client_proto_version >= 1)
{
ca_descr_t *cadesc = (ca_descr_t *) data;
cadesc->index = htonl(cadesc->index);
cadesc->parity = htonl(cadesc->parity);
}
memcpy(&packet[size], data, sct_cadescr_size);
size += sct_cadescr_size;
break;
}
case DVBAPI_CA_SET_PID:
{
int sct_capid_size = sizeof(ca_pid_t);
if(client_proto_version >= 1)
{
ca_pid_t *capid = (ca_pid_t *) data;
capid->pid = htonl(capid->pid);
capid->index = htonl(capid->index);
}
memcpy(&packet[size], data, sct_capid_size);
size += sct_capid_size;
break;
}
case DVBAPI_CA_SET_DESCR_MODE:
{
int sct_cadescr_mode_size = sizeof(ca_descr_mode_t);
if(client_proto_version >= 1)
{
ca_descr_mode_t *cadesc_mode = (ca_descr_mode_t *) data;
cadesc_mode->index = htonl(cadesc_mode->index);
cadesc_mode->algo = htonl(cadesc_mode->algo);
cadesc_mode->cipher_mode = htonl(cadesc_mode->cipher_mode);
}
memcpy(&packet[size], data, sct_cadescr_mode_size);
size += sct_cadescr_mode_size;
break;
}
case DVBAPI_CA_SET_DESCR_DATA:
{
ca_descr_data_t *cadesc_data = (ca_descr_data_t *) data;
uint32_t length = cadesc_data->length;
if(client_proto_version >= 1)
{
cadesc_data->index = htonl(cadesc_data->index);
cadesc_data->parity = htonl(cadesc_data->parity);
cadesc_data->data_type = htonl(cadesc_data->data_type);
cadesc_data->length = htonl(cadesc_data->length);
}
memcpy(&packet[size], &cadesc_data->index, 4);
memcpy(&packet[size + 4], &cadesc_data->parity, 4);
memcpy(&packet[size + 8], &cadesc_data->data_type, 4);
memcpy(&packet[size + 12], &cadesc_data->length, 4);
memcpy(&packet[size + 16], cadesc_data->data, length);
size += 16 + length;
break;
}
case DVBAPI_DMX_SET_FILTER:
case DVBAPI_DMX_STOP:
{
int32_t sct_filter_size = sizeof(struct dmx_sct_filter_params);
packet[size++] = demux_id; // demux id - 1 byte
packet[size++] = filter_number; // filter number - 1 byte
if(data) // filter data when starting
{
if(client_proto_version >= 1)
{
struct dmx_sct_filter_params *fp = (struct dmx_sct_filter_params *)data;
// adding all dmx_sct_filter_params structure fields
// one by one to avoid padding problems
uint16_t pid = htons(fp->pid);
memcpy(&packet[size], &pid, 2);
size += 2;
memcpy(&packet[size], fp->filter.filter, 16);
size += 16;
memcpy(&packet[size], fp->filter.mask, 16);
size += 16;
memcpy(&packet[size], fp->filter.mode, 16);
size += 16;
uint32_t timeout = htonl(fp->timeout);
memcpy(&packet[size], &timeout, 4);
size += 4;
uint32_t flags = htonl(fp->flags);
memcpy(&packet[size], &flags, 4);
size += 4;
}
else
{
memcpy(&packet[size], data, sct_filter_size); // dmx_sct_filter_params struct
size += sct_filter_size;
}
}
else // pid when stopping
{
if(client_proto_version >= 1)
{
uint16_t pid = htons(demux[demux_id].demux_fd[filter_number].pid);
memcpy(&packet[size], &pid, 2);
size += 2;
}
else
{
uint16_t pid = demux[demux_id].demux_fd[filter_number].pid;
packet[size++] = pid >> 8;
packet[size++] = pid & 0xff;
}
}
break;
}
default: // unknown request
{
cs_log("ERROR: dvbapi_net_send: invalid request");
return 0;
}
}
// sending
cs_log_dump_dbg(D_DVBAPI, packet, size, "Sending packet to dvbapi client (fd=%d):", socket_fd);
send(socket_fd, &packet, size, MSG_DONTWAIT);
// always returning success as the client could close socket
return 0;
}
int32_t dvbapi_set_filter(int32_t demux_id, int32_t api, uint16_t pid, uint16_t caid, uint32_t provid,
uint8_t *filt, uint8_t *mask, int32_t timeout, int32_t pidindex, int32_t type, int8_t add_to_emm_list)
{
int32_t ret = -1, n = -1, i, filterfd = -1;
for(i = 0; i < maxfilter && demux[demux_id].demux_fd[i].fd > 0; i++) { ; }
if(i >= maxfilter)
{
cs_log_dbg(D_DVBAPI, "no free filter");
return -1;
}
n = i;
if(USE_OPENXCAS)
{
if(type == TYPE_ECM)
{
openxcas_set_caid(demux[demux_id].ECMpids[pidindex].CAID);
openxcas_set_ecm_pid(pid);
}
demux[demux_id].demux_fd[n].fd = DUMMY_FD;
demux[demux_id].demux_fd[n].pidindex = pidindex;
demux[demux_id].demux_fd[n].pid = pid;
demux[demux_id].demux_fd[n].caid = caid;
demux[demux_id].demux_fd[n].provid = provid;
demux[demux_id].demux_fd[n].type = type;
memcpy(demux[demux_id].demux_fd[n].filter, filt, 16); // copy filter to check later on if receiver delivered accordingly
memcpy(demux[demux_id].demux_fd[n].mask, mask, 16); // copy mask to check later on if receiver delivered accordingly
return 1;
}
switch(api)
{
case DVBAPI_3:
{
if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
{
ret = filterfd = DUMMY_FD;
}
else
{
ret = filterfd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
}
if(ret < 0)
{
return ret; // return if device cant be opened!
}
struct dmx_sct_filter_params sFP2;
memset(&sFP2, 0, sizeof(sFP2));
sFP2.pid = pid;
sFP2.timeout = timeout;
sFP2.flags = DMX_IMMEDIATE_START;
if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
{
//DeepThought: on dgs/cubestation and neumo images, perhaps others
//the following code is needed to descramble
sFP2.filter.filter[0] = filt[0];
sFP2.filter.mask[0] = mask[0];
sFP2.filter.filter[1] = 0;
sFP2.filter.mask[1] = 0;
sFP2.filter.filter[2] = 0;
sFP2.filter.mask[2] = 0;
memcpy(sFP2.filter.filter + 3, filt + 1, 16 - 3);
memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
//DeepThought: in the drivers of the dgs/cubestation and neumo images,
//dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
//ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
//due to a bug its sets the "positive mask" wrongly (they should be all 0).
//On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
//structure, which is incorrect (it should be dmxSctFilterParams).
//The only way to get it right is to call DMX_SET_FILTER1 with the argument
//expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER1, &sFP2);
}
else
{
memcpy(sFP2.filter.filter, filt, 16);
memcpy(sFP2.filter.mask, mask, 16);
if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
{
ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER,
demux[demux_id].socket_fd,
0,
demux_id,
n,
(uint8_t *) &sFP2,
NULL,
NULL,
demux[demux_id].client_proto_version);
}
else
{
ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER, &sFP2);
}
}
break;
}