/
dvbdescramble.c
300 lines (244 loc) · 8.03 KB
/
dvbdescramble.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
/*
* (C) 2008-2009 Patrik Fimml.
*
* This file is part of dvbdescramble.
*
* dvbdescramble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/dvb/dmx.h>
#include "ca-t-c.h"
#include "ca-resource-manager.h"
#define MAXARGS 256
#define DEFAULT_CA "/dev/dvb/adapter0/ca0"
#define DEFAULT_DMX "/dev/dvb/adapter0/demux0"
#define DEFAULT_SLOT 0
int pmtv[MAXARGS];
int programv[MAXARGS];
int pmtc, programc;
gchar *cadev = DEFAULT_CA;
gchar *dmxdev = DEFAULT_DMX;
CATC *catc = NULL;
CAResourceManager *camgr = NULL;
static void
poll_in(int fd, GIOFunc callback, gpointer data)
{
GIOChannel *ioc = g_io_channel_unix_new(fd);
g_io_add_watch(ioc, G_IO_IN, callback, data);
g_io_channel_unref(ioc);
}
static int
get_demux()
{
int fd = open(dmxdev, O_RDWR);
if (fd == -1)
g_error("could not open %s: %s", dmxdev, g_strerror(errno));
return fd;
}
static gboolean
read_pmt(GIOChannel *unused1, GIOCondition unused2, gpointer data)
{
int dmx = GPOINTER_TO_INT(data);
guint8 buf[512];
int r = read(dmx, buf, 512);
g_assert(r > 0);
guint16 program_number = (buf[3] << 8) | buf[4];
guint8 section_number = buf[6];
guint8 last_section = buf[7];
g_assert(section_number == last_section);
if (camgr == NULL || !ca_resource_manager_has_cainfo(camgr, catc))
return TRUE;
ca_resource_manager_descramble_pmt(camgr, catc, buf);
g_debug("programme %d should be ready", program_number);
/* FIXME: maybe replace one-shot behaviour later with continuous
* version_number checks */
close(dmx);
return FALSE;
}
static void
listen_pmt(int pid)
{
struct dmx_sct_filter_params param;
param.pid = pid;
param.timeout = 0;
param.flags = DMX_CHECK_CRC | DMX_IMMEDIATE_START;
memset(¶m.filter, 0, sizeof(struct dmx_filter));
/* table_id */
param.filter.filter[0] = 0x02;
param.filter.mask[0] = 0xFF;
/* current_next_indicator */
param.filter.filter[3] = 0x01;
param.filter.mask[3] = 0x01;
/* section_number */
param.filter.filter[4] = 0x00;
param.filter.mask[4] = 0xFF;
#if 0
/* program_number */
param.filter.filter[1] = use_program_number >> 8;
param.filter.filter[2] = use_program_number & 0xFF;
param.filter.mask[1] = 0xFF;
param.filter.mask[2] = 0xFF;
#endif
int dmx = get_demux();
if (ioctl(dmx, DMX_SET_FILTER, ¶m) == -1)
g_error("ioctl failed: %s", g_strerror(errno));
poll_in(dmx, (GIOFunc)read_pmt, GINT_TO_POINTER(dmx));
}
int pat_dmx = -1;
static gboolean
read_pat()
{
guint8 buf[512];
int r = read(pat_dmx, buf, 512);
g_assert(r > 0);
g_debug("received PAT");
guint8 section_number = buf[6];
guint8 last_section = buf[7];
g_assert(section_number == last_section);
guint16 length = ((buf[1] & 0x0F) << 8) | buf[2];
guint8 *end = buf+3+length-4; /* includes CRC */
guint8 *cur;
for (cur = buf+8; cur < end; cur += 4)
{
guint16 program_number = (cur[0] << 8) | cur[1];
guint16 pid = ((cur[2] & 0x1F) << 8) | cur[3];
for (int i = 0; i < programc; i++)
{
if (programv[i] == program_number)
{
g_debug("programme %d has PMT on %d", program_number, pid);
programv[i] = -1;
listen_pmt(pid);
}
}
}
g_assert(cur == end);
close(pat_dmx);
return FALSE;
}
static void
listen_pat()
{
struct dmx_sct_filter_params param;
param.pid = 0x0000;
param.timeout = 0;
param.flags = DMX_CHECK_CRC | DMX_IMMEDIATE_START;
memset(¶m.filter, 0, sizeof(struct dmx_filter));
/* table_id */
param.filter.filter[0] = 0x00;
param.filter.mask[0] = 0xFF;
/* current_next_indicator */
param.filter.filter[3] = 0x01;
param.filter.mask[3] = 0x01;
/* section_number */
param.filter.filter[4] = 0x00;
param.filter.mask[4] = 0xFF;
pat_dmx = get_demux();
if (ioctl(pat_dmx, DMX_SET_FILTER, ¶m) == -1)
g_error("ioctl failed: %s", g_strerror(errno));
g_debug("listening for PAT");
poll_in(pat_dmx, (GIOFunc)read_pat, NULL);
}
int main(int argc, char **argv)
{
gboolean error = FALSE;
gboolean help = FALSE;
guint ca_slot = DEFAULT_SLOT;
for (int i = 1; i < argc; i++)
{
if (!strcmp("--ca", argv[i]))
cadev = argv[++i];
else if (!strcmp("--demux", argv[i]))
dmxdev = argv[++i];
else if (!strcmp("--slot", argv[i]))
ca_slot = atoi(argv[++i]);
else if (!strcmp("-p", argv[i]) || !strcmp("--program", argv[i]))
programv[programc++] = atoi(argv[++i]);
else if (!strcmp("--pmt", argv[i]))
pmtv[pmtc++] = atoi(argv[++i]);
else if (!strcmp("--help", argv[i]))
{
help = TRUE;
break;
}
else
{
gchar *after;
gulong pid = strtoul(argv[i], &after, 10);
if (!after || *after)
{
fprintf(stderr, "invalid argument (expected PID): %s\n", argv[i]);
error = TRUE;
break;
}
}
}
if (error || argc <= 1)
{
fprintf(stderr, "Usage: dvbdescramble [options]\n");
fprintf(stderr, "see \"dvbdescramble --help\" for more information\n");
return 1;
}
if (help)
{
fprintf(stderr, "Usage: dvbdescramble [options]\n");
fprintf(stderr, "Set up a CA Module for descrambling.\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " --ca DEVICE CA device (default: %s)\n", DEFAULT_CA);
fprintf(stderr, " --demux DEVICE demux device (default: %s)\n", DEFAULT_DMX);
fprintf(stderr, " --slot N slot in CA device (default: %d)\n", DEFAULT_SLOT);
fprintf(stderr, " -p|--program N DVB program_number\n");
fprintf(stderr, " --pmt N PID of PMT\n\n");
fprintf(stderr, "dvbdescramble will tell the CA Module to descramble the PIDs\n"
"given at the command line. For this to work, the CA Module has\n"
"to be fed the corresponding Programme Map Tables (PMTs). You can\n"
"either give the PMTs' PIDs directly using --pmt, or let\n"
"dvbdescramble look them up in the Program Allocation Table with\n"
"--program.\n");
return 0;
}
g_type_init();
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
/* set up filters */
if (programc > 0)
listen_pat();
for (int i = 0; i < pmtc; i++)
listen_pmt(pmtv[i]);
CADevice *ca = ca_open_device(cadev);
if (ca == NULL)
g_error("failed to open CA device %s", cadev);
ca_device_reset(ca);
CAModule *cam = ca_device_open_module(ca, ca_slot);
if (cam == NULL)
g_error("failed to open module in slot %d", ca_slot);
g_debug("waiting for module");
ca_module_wait_ready(cam);
g_assert(ca_module_ready(cam));
catc = ca_module_create_t_c_block(cam, 1);
if (catc == NULL)
g_error("failed to create transport connection");
camgr = ca_resource_manager_new();
if (camgr == NULL)
g_error("failed to create CA Resource Manager");
ca_resource_manager_manage_t_c(camgr, catc);
g_main_loop_run(loop);
return 0;
}