forked from nx111/oscam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-dvbapi.c
7652 lines (6675 loc) · 237 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"
#ifdef WITH_TIARTOP
#include "module-xcam-a8plus.h"
#else
#include "module-dvbapi-azbox.h"
#endif
#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
int32_t priority_is_changed=0;
extern char cs_confdir[];
const char *streamtxt_00_to_1B[] = {
"Reserved", // 00
"Videostream (MPEG-1)", // 01
"Videostream (MPEG-2)", // 02
"Audiostream (MPEG-1)", // 03
"Audiostream (MPEG-2)", // 04
"Datastream (MPEG-2 tabled data)", // 05
"Data-/Audiostream (Subtitles/VBI and AC-3)", // 06
"Datastream (MHEG)", // 07
"Datastream (DSM CC)", // 08
"Conditional Access ", // 09
"Datastream (DSM CC)", // 0A
"Datastream (DSM CC)", // 0B
"Datastream (DSM CC)", // 0C
"Datastream (DSM CC)", // 0D
"Datastream (Auxiliary)", // 0E
"Audiostream (MPEG-2)", // 0F
"Videostream (MPEG-4 H.263)", // 10
"Audiostream (MPEG-4)", // 11
"Datastream (MPEG-4 FlexMux)", // 12
"Datastream (MPEG-4 FlexMux)", // 13
"Datastream (DSM CC)", // 14
"Datastream (Metadata)", // 15
"Datastream (Metadata)", // 16
"Datastream (DSM CC)", // 17
"Datastream (DSM CC)", // 18
"Datastream (Metadata)", // 19
"Datastream (IPMP)", // 1A
"Videostream (MPEG-4)", // 1B
};
const char *streamtxt_80_to_87[] = {
"Video-/Audiostream (H.262/PCM)", // 80
"Audiostream (Dolby Digital)", // 81
"Data-/Audiostream (Subtitles/DTS6)", // 82
"Audiostream (Dolby TrueHD)", // 83
"Audiostream (Dolby Digital Plus)", // 84
"Audiostream (DTS 8)", // 85
"Audiostream (DTS 8 losless)", // 86
"Audiostream (Dolby Digital Plus)", // 87
};
const char *get_streamtxt(uint8_t id)
{
if(id <= 0x1B)
{
return streamtxt_00_to_1B[id];
}
else if(id == 0x24)
{
return "Videostream (H.265 Ultra HD video)";
}
else if(id == 0x42)
{
return "Videostream (Chinese Video Standard)";
}
else if(id >= 0x80 && id <= 0x87)
{
return streamtxt_80_to_87[id - 0x80];
}
else if(id == 0x90)
{
return "Datastream (Blu-ray subtitling)";
}
else if(id == 0x95)
{
return "Datastream (DSM CC)";
}
else if(id == 0xC0)
{
return "Datastream (DigiCipher II text)";
}
else if(id == 0xC2)
{
return "Datastream (DSM CC)";
}
else if(id == 0xD1)
{
return "Videostream (BBC Dirac Ultra HD video)";
}
else if(id == 0xEA)
{
return "Videostream (WMV9 lower bit-rate)";
}
else
{
return "Reserved";
}
}
void flush_read_fd(int32_t demux_index, 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_index, 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
unsigned char 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_SET_PID:
{
ca_pid_t *ca_pid2 = va_arg(args, ca_pid_t *);
// preparing packet
unsigned char 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:
{
ca_descr_t *ca_descr = va_arg(args, ca_descr_t *);
// preparing packet
unsigned char 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_DESCR_MODE:
{
cs_log("error: samygo does not support CA_SET_DESCR_MODE");
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: http://www.streamboard.tv/wbb2/thread.php?postid=533940
if(boxtype_is("su980"))
ret = 1;
va_end(args);
return ret;
}
// tunemm_caid_map
#define FROM_TO 0
#define TO_FROM 1
#if defined(CARDREADER_STAPI) || defined(CARDREADER_STAPI5)
//fix from stapi5 patch
int32_t pausecam = 0, disable_pmt_files = 0, pmt_stopmarking = 1;
#else
int32_t pausecam = 0, disable_pmt_files = 0, pmt_stopmarking = 0;
#endif
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 int32_t ca_fd[MAX_DEMUX]; // holds fd handle of each ca device 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;
uchar 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, uchar *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, uchar *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(uchar *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))
{ return 1; }
if(is_emmfilter_in_list_internal(ll_emm_inactive_filter, filter, emmpid, provid, caid))
{ return 1; }
if(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))
{ return; }
if(ll_emm_inactive_filter && remove_emmfilter_from_list_internal(ll_emm_inactive_filter, demux_id, caid, provid, pid, num))
{ return; }
if(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(unsigned char *packet, int *size, const char *str)
{
unsigned char *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_index, uint32_t filter_number, unsigned char *data, struct s_client *client, ECM_REQUEST *er, uint16_t client_proto_version)
{
unsigned char 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_index].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_index].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] = "\x00"; // two zero characters
memcpy(&packet[size], &proto_version, 2);
size += 2;
unsigned char *info_len = &packet[size]; //info string length
size += 1;
if (cfg.dvbapi_extended_cw_api == 1)
strcat(capabilities, ",e1km"); //extended cw, mode follows key
if (cfg.dvbapi_extended_cw_api == 2)
strcat(capabilities, ",e2");
*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_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:
{
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_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_DMX_SET_FILTER:
case DVBAPI_DMX_STOP:
{
int32_t sct_filter_size = sizeof(struct dmx_sct_filter_params);
packet[size++] = demux_index; //demux index - 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_index].demux_fd[filter_number].pid);
memcpy(&packet[size], &pid, 2);
size += 2;
}
else
{
uint16_t pid = demux[demux_index].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, uchar *filt, uchar *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, (unsigned char *) &sFP2, NULL, NULL, demux[demux_id].client_proto_version);
else
ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER, &sFP2);
}
break;
case DVBAPI_1:
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 dmxSctFilterParams sFP1;
memset(&sFP1, 0, sizeof(sFP1));
sFP1.pid = pid;
sFP1.timeout = timeout;
sFP1.flags = DMX_IMMEDIATE_START;
memcpy(sFP1.filter.filter, filt, 16);
memcpy(sFP1.filter.mask, mask, 16);
ret = dvbapi_ioctl(filterfd, DMX_SET_FILTER1, &sFP1);
break;
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
case STAPI:
ret = filterfd = stapi_set_filter(demux_id, pid, filt, mask, n, demux[demux_id].pmt_file);
if(ret <= 0)
{
ret = -1; // error setting filter!
}
break;
#endif
#if defined WITH_COOLAPI || defined WITH_COOLAPI2
case COOLAPI:
ret = filterfd = coolapi_open_device(demux[demux_id].demux_index, demux_id);
if(ret > 0)
{
ret = coolapi_set_filter(filterfd, n, pid, filt, mask, type);
}
else
{
ret = -1; // fail
}
break;
#endif
default:
break;
}
if(ret != -1) // filter set successful
{
// only register if filter was set successful
demux[demux_id].demux_fd[n].fd = filterfd;
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
cs_log_dbg(D_DVBAPI, "Demuxer %d Filter %d started successfully (caid %04X provid %06X pid %04X)", demux_id, n + 1, caid, provid, pid);
if(type == TYPE_EMM && add_to_emm_list){
add_emmfilter_to_list(demux_id, filt, caid, provid, pid, n + 1, true);
}
}
else
{
cs_log("ERROR: Could not start demux filter (api: %d errno=%d %s)", selected_api, errno, strerror(errno));
}
return ret;
}
static int32_t dvbapi_detect_api(void)
{
#if defined WITH_COOLAPI || defined WITH_COOLAPI2
selected_api = COOLAPI;
selected_box = BOX_INDEX_COOLSTREAM;
disable_pmt_files = 1;
cfg.dvbapi_listenport = 0;
cs_log("Detected Coolstream API");
return 1;
#else
if (cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX || cfg.dvbapi_boxtype == BOXTYPE_PC ) {
selected_api = DVBAPI_3;
selected_box = BOX_INDEX_DREAMBOX_DVBAPI3;
if (cfg.dvbapi_listenport)
{
cs_log("Using TCP listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d", selected_api, cfg.dvbapi_boxtype);
}
else
{
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);
}
return 1;
}
else if(cfg.dvbapi_boxtype == BOXTYPE_SAMYGO)
{
selected_api = DVBAPI_3;
selected_box = BOX_INDEX_QBOXHD;
cfg.dvbapi_listenport = 0;
disable_pmt_files = 1;
cs_log("Using SamyGO dvbapi v0.1");
return 1;
}
else
{
cfg.dvbapi_listenport = 0;
}
int32_t i = 0, n = 0, devnum = -1, dmx_fd = 0, filtercount = 0;
char device_path[128], device_path2[128];
static LLIST *ll_max_fd;
ll_max_fd = ll_create("ll_max_fd");
LL_ITER itr;
struct s_open_fd
{
uint32_t fd;
};
struct s_open_fd *open_fd;
while (i < BOX_COUNT)
{
do
{
snprintf(device_path2, sizeof(device_path2), devices[i].demux_device, 0);
snprintf(device_path, sizeof(device_path), devices[i].path, n);
strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
filtercount = 0;
while((dmx_fd = open(device_path, O_RDWR | O_NONBLOCK)) > 0 && filtercount < MAX_FILTER)
{
filtercount++;
if(!cs_malloc(&open_fd, sizeof(struct s_open_fd)))
{
close(dmx_fd);
break;
}
open_fd->fd = dmx_fd;
ll_append(ll_max_fd, open_fd);
}
if(filtercount > 0)
{
itr = ll_iter_create(ll_max_fd);
while((open_fd = ll_iter_next(&itr)))
{
dmx_fd = open_fd->fd;
do
{
;
}
while(close(dmx_fd)<0);
ll_iter_remove_data(&itr);
}
devnum = i;
selected_api = devices[devnum].api;
selected_box = devnum;
if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
{
selected_api = DVBAPI_3; //DeepThought
}
#if defined(WITH_STAPI) || defined(WITH_STAPI5)
if(selected_api == STAPI && stapi_open() == 0)
{
cs_log("ERROR: stapi: setting up stapi failed.");
return 0;
}
#endif
maxfilter = filtercount;
cs_log("Detected %s Api: %d, userconfig boxtype: %d maximum amount of possible filters is %d (oscam limit is %d)",
device_path, selected_api, cfg.dvbapi_boxtype, filtercount, MAX_FILTER);
}
/* try at least 8 adapters */
if ((strchr(devices[i].path, '%') != NULL) && (n < 8)) n++; else { n = 0; i++; }
}while(n != 0); // n is set to 0 again if 8 adapters are tried!
if(devnum != -1) break; // check if box detected
}
ll_destroy(&ll_max_fd);
if(devnum == -1) { return 0; }
#endif
return 1;
}
static int32_t dvbapi_read_device(int32_t dmx_fd, unsigned char *buf, uint32_t length)
{
int32_t readed;
uint32_t count = 0;
struct pollfd pfd[1];
pfd[0].fd = dmx_fd;
pfd[0].events = (POLLIN | POLLPRI);