forked from oscam-emu/oscam-patched-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-dvbapi-azbox.c
354 lines (282 loc) · 9.76 KB
/
module-dvbapi-azbox.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
#define MODULE_LOG_PREFIX "dvbazbox"
#include "globals.h"
#if defined(HAVE_DVBAPI) && defined(WITH_AZBOX)
#include "extapi/openxcas/openxcas_api.h"
#include "extapi/openxcas/openxcas_message.h"
#include "module-dvbapi.h"
#include "module-dvbapi-azbox.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-reader.h"
#include "oscam-string.h"
#include "oscam-time.h"
// These variables are declared in module-dvbapi.c
extern void *dvbapi_client;
extern DEMUXTYPE demux[MAX_DEMUX];
// These are used in module-dvbapi.c
int32_t openxcas_provid;
uint16_t openxcas_sid, openxcas_caid, openxcas_ecm_pid;
static uint8_t openxcas_cw[16];
static int32_t openxcas_seq, openxcas_filter_idx, openxcas_stream_id, openxcas_cipher_idx, openxcas_busy = 0;
static uint16_t openxcas_video_pid, openxcas_audio_pid, openxcas_data_pid;
static void azbox_openxcas_ecm_callback(int32_t stream_id, uint32_t UNUSED(seq), int32_t cipher_index, uint32_t UNUSED(caid), uint8_t *ecm_data, int32_t l, uint16_t pid)
{
cs_log_dbg(D_DVBAPI, "ecm callback received");
openxcas_stream_id = stream_id;
//openxcas_seq = seq;
//openxcas_caid = caid;
openxcas_ecm_pid = pid;
openxcas_busy = 1;
if(l < 0 || l > MAX_ECM_SIZE)
{ return; }
ECM_REQUEST *er;
if(!(er = get_ecmtask()))
{ return; }
er->srvid = openxcas_sid;
er->caid = openxcas_caid;
er->pid = openxcas_ecm_pid;
er->prid = openxcas_provid;
er->ecmlen = l;
memcpy(er->ecm, ecm_data, er->ecmlen);
request_cw(dvbapi_client, er, 0, 0);
//openxcas_stop_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
//openxcas_remove_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
openxcas_cipher_idx = cipher_index;
struct timeb tp;
cs_ftime(&tp);
tp.time += 500;
}
static void azbox_openxcas_ex_callback(int32_t stream_id, uint32_t seq, int32_t idx, uint32_t pid, uint8_t *ecm_data, int32_t l)
{
cs_log_dbg(D_DVBAPI, "ex callback received");
openxcas_stream_id = stream_id;
openxcas_ecm_pid = pid;
openxcas_cipher_idx = idx; // is this really cipher_idx?
if(l < 0 || l > MAX_ECM_SIZE)
{ return; }
ECM_REQUEST *er;
if(!(er = get_ecmtask()))
{ return; }
er->srvid = openxcas_sid;
er->caid = openxcas_caid;
er->pid = openxcas_ecm_pid;
er->prid = openxcas_provid;
er->ecmlen = l;
memcpy(er->ecm, ecm_data, er->ecmlen);
request_cw(dvbapi_client, er, 0, 0);
if(openxcas_stop_filter_ex(stream_id, seq, openxcas_filter_idx) < 0)
{ cs_log("unable to stop ex filter"); }
else
{ cs_log_dbg(D_DVBAPI, "ex filter stopped"); }
uint8_t mask[12];
uint8_t comp[12];
memset(&mask, 0x00, sizeof(mask));
memset(&comp, 0x00, sizeof(comp));
mask[0] = 0xff;
comp[0] = ecm_data[0] ^ 1;
if((openxcas_filter_idx = openxcas_start_filter_ex(stream_id, seq, openxcas_ecm_pid, mask, comp, (void *)azbox_openxcas_ex_callback)) < 0)
{ cs_log("unable to start ex filter"); }
else
{ cs_log_dbg(D_DVBAPI, "ex filter started, pid = %x", openxcas_ecm_pid); }
}
static void *azbox_main_thread(void *cli)
{
struct s_client *client = (struct s_client *) cli;
client->thread = pthread_self();
SAFE_SETSPECIFIC(getclient, cli);
dvbapi_client = cli;
struct s_auth *account;
int32_t ok = 0;
for(account = cfg.account; account; account = account->next)
{
if((ok = is_dvbapi_usr(account->usr)))
{ break; }
}
cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
dvbapi_read_priority();
openxcas_msg_t msg;
int32_t ret;
while((ret = openxcas_get_message(&msg, 0)) >= 0)
{
cs_sleepms(10);
if(ret)
{
openxcas_stream_id = msg.stream_id;
openxcas_seq = msg.sequence;
struct stOpenXCAS_Data data;
switch(msg.cmd)
{
case OPENXCAS_SELECT_CHANNEL:
cs_log_dbg(D_DVBAPI, "OPENXCAS_SELECT_CHANNEL");
// parse channel info
struct stOpenXCASChannel chan;
memcpy(&chan, msg.buf, msg.buf_len);
cs_log("channel change: sid = %x, vpid = %x. apid = %x", chan.service_id, chan.v_pid, chan.a_pid);
openxcas_video_pid = chan.v_pid;
openxcas_audio_pid = chan.a_pid;
openxcas_data_pid = chan.d_pid;
break;
case OPENXCAS_START_PMT_ECM:
cs_log_dbg(D_DVBAPI, "OPENXCAS_START_PMT_ECM");
// parse pmt
uint8_t *dest;
if(!cs_malloc(&dest, msg.buf_len + 7 - 12 - 4))
{ break; }
memcpy(dest, "\x03\xFF\xFF\x00\x00\x13\x00", 7);
dest[1] = msg.buf[3];
dest[2] = msg.buf[4];
dest[5] = msg.buf[11] + 1;
memcpy(dest + 7, msg.buf + 12, msg.buf_len - 12 - 4);
dvbapi_parse_capmt(dest, 7 + msg.buf_len - 12 - 4, -1, NULL, 0, 0);
NULLFREE(dest);
uint8_t mask[12];
uint8_t comp[12];
memset(&mask, 0x00, sizeof(mask));
memset(&comp, 0x00, sizeof(comp));
mask[0] = 0xfe;
comp[0] = 0x80;
if((ret = openxcas_add_filter(msg.stream_id, OPENXCAS_FILTER_ECM, 0, 0xffff, openxcas_ecm_pid, mask, comp, (void *)azbox_openxcas_ecm_callback)) < 0)
{ cs_log("unable to add ecm filter"); }
else
{ cs_log_dbg(D_DVBAPI, "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, 0); }
if(openxcas_start_filter(msg.stream_id, msg.sequence, OPENXCAS_FILTER_ECM) < 0)
{ cs_log("unable to start ecm filter"); }
else
{ cs_log_dbg(D_DVBAPI, "ecm filter started"); }
if(!openxcas_create_cipher_ex(msg.stream_id, openxcas_seq, 0, openxcas_ecm_pid, openxcas_video_pid, 0xffff, openxcas_audio_pid, 0xffff, 0xffff, 0xffff))
{ cs_log("failed to create cipher ex"); }
else
{ cs_log_dbg(D_DVBAPI, "cipher created"); }
break;
case OPENXCAS_STOP_PMT_ECM:
cs_log_dbg(D_DVBAPI, "OPENXCAS_STOP_PMT_ECM");
openxcas_stop_filter(msg.stream_id, OPENXCAS_FILTER_ECM);
openxcas_remove_filter(msg.stream_id, OPENXCAS_FILTER_ECM);
openxcas_stop_filter_ex(msg.stream_id, msg.sequence, openxcas_filter_idx);
openxcas_destory_cipher_ex(msg.stream_id, msg.sequence);
memset(&demux, 0, sizeof(demux));
break;
case OPENXCAS_ECM_CALLBACK:
cs_log_dbg(D_DVBAPI, "OPENXCAS_ECM_CALLBACK");
memcpy(&data, msg.buf, msg.buf_len);
if(!openxcas_busy)
{ openxcas_filter_callback(msg.stream_id, msg.sequence, OPENXCAS_FILTER_ECM, &data); }
break;
case OPENXCAS_PID_FILTER_CALLBACK:
cs_log_dbg(D_DVBAPI, "OPENXCAS_PID_FILTER_CALLBACK");
memcpy(&data, msg.buf, msg.buf_len);
openxcas_filter_callback_ex(msg.stream_id, msg.sequence, &data);
break;
case OPENXCAS_QUIT:
cs_log_dbg(D_DVBAPI, "OPENXCAS_QUIT");
openxcas_close();
cs_log("exited");
return NULL;
break;
case OPENXCAS_UKNOWN_MSG:
default:
cs_log_dbg(D_DVBAPI, "OPENXCAS_UKNOWN_MSG (%d)", msg.cmd);
//cs_log_dump_dbg(D_DVBAPI, &msg, sizeof(msg), "msg dump:");
break;
}
}
}
cs_log("invalid message");
return NULL;
}
void azbox_send_dcw(struct s_client *client, ECM_REQUEST *er)
{
struct s_dvbapi_priority *delayentry = dvbapi_check_prio_match(0, demux[0].pidindex, 'd');
uint32_t delay = 0;
cs_log_dbg(D_DVBAPI, "send_dcw");
if(delayentry)
{
if(delayentry->delay < 1000)
{
delay = delayentry->delay;
cs_log_dbg(D_DVBAPI, "specific delay: write cw %d ms after ecmrequest", delay);
}
}
else if (cfg.dvbapi_delayer > 0)
{
delay = cfg.dvbapi_delayer;
cs_log_dbg(D_DVBAPI, "generic delay: write cw %d ms after ecmrequest", delay);
}
delayer(er, delay);
if(cfg.dvbapi_ecminfo_file != 0)
{
dvbapi_write_ecminfo_file(client, er, demux[0].last_cw[0][0], demux[0].last_cw[0][1], 8);
}
openxcas_busy = 0;
int32_t i;
for(i = 0; i < MAX_DEMUX; i++)
{
if(er->rc >= E_NOTFOUND)
{
cs_log_dbg(D_DVBAPI, "cw not found");
if(demux[i].pidindex == -1)
{ dvbapi_try_next_caid(i, 0, 0); }
openxcas_stop_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
openxcas_remove_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
uint8_t mask[12];
uint8_t comp[12];
memset(&mask, 0x00, sizeof(mask));
memset(&comp, 0x00, sizeof(comp));
mask[0] = 0xfe;
comp[0] = 0x80;
if(openxcas_add_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM, 0, 0xffff, openxcas_ecm_pid, mask, comp, (void *)azbox_openxcas_ecm_callback) < 0)
{
cs_log("unable to add ecm filter (0)");
if(openxcas_add_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM, openxcas_caid, 0xffff, openxcas_ecm_pid, mask, comp, (void *)azbox_openxcas_ecm_callback) < 0)
{ cs_log("unable to add ecm filter (%04x)", openxcas_caid); }
else
{ cs_log_dbg(D_DVBAPI, "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, openxcas_caid); }
}
else
{ cs_log_dbg(D_DVBAPI, "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, 0); }
if(openxcas_start_filter(openxcas_stream_id, openxcas_seq, OPENXCAS_FILTER_ECM) < 0)
{ cs_log("unable to start ecm filter"); }
else
{ cs_log_dbg(D_DVBAPI, "ecm filter started"); }
return;
}
}
uint8_t nullcw[8];
memset(nullcw, 0, 8);
int32_t n;
for(n = 0; n < 2; n++)
{
// Skip check for BISS1 - cw could be indeed zero
// Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
if(memcmp(er->cw + (n * 8), demux[0].last_cw[0][n], 8) && (memcmp(er->cw + (n * 8), nullcw, 8) != 0 || caid_is_biss(er->caid)))
{
memcpy(demux[0].last_cw[0][n], er->cw + (n * 8), 8);
memcpy(openxcas_cw + (n * 8), er->cw + (n * 8), 8);
}
}
if(openxcas_set_key(openxcas_stream_id, openxcas_seq, 0, openxcas_cipher_idx, openxcas_cw, openxcas_cw + 8) != 1)
{ cs_log("set cw failed"); }
else
{ cs_log_dump_dbg(D_DVBAPI, openxcas_cw, 16, "write cws to descrambler"); }
}
#ifdef WITH_CARDREADER
#define __openxcas_open openxcas_open_with_smartcard
#else
#define __openxcas_open openxcas_open
#endif
void azbox_init(void)
{
openxcas_debug_message_onoff(1); // debug
if(__openxcas_open("oscamCAS") < 0)
{ cs_log("could not init"); }
}
void azbox_close(void)
{
if(openxcas_close() < 0)
{ cs_log("could not close"); }
}
void *azbox_handler(struct s_client *cl, uint8_t *mbuf, int32_t module_idx)
{
return dvbapi_start_handler(cl, mbuf, module_idx, azbox_main_thread);
}
#endif