forked from VDR4Arch/vdr-mcli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.c
463 lines (413 loc) · 10.4 KB
/
filter.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
/*
* (c) BayCom GmbH, http://www.baycom.de, info@baycom.de
*
* See the COPYING file for copyright information and
* how to reach the author.
*
*/
#include "filter.h"
#include "device.h"
#include <vdr/device.h>
#define PID_MASK_HI 0x1F
class cMcliPid:public cListObject
{
private:
int m_Pid;
int m_Tid;
public:
cMcliPid (int Pid, int Tid)
{
m_Pid = Pid;
m_Tid = Tid;
}
~cMcliPid ()
{
}
int Tid (void)
{
return m_Tid;
}
int Pid (void)
{
return m_Pid;
}
void SetTid (int Tid)
{
m_Tid = Tid;
}
};
// --- cMcliFilter ------------------------------------------------------
class cMcliFilter:public cListObject
{
private:
uchar m_Buffer[65536];
int m_Used;
bool m_closed;
int m_Pipe[2];
u_short m_Pid;
u_char m_Tid;
u_char m_Mask;
public:
cMcliFilter (u_short Pid, u_char Tid, u_char Mask);
virtual ~ cMcliFilter ();
bool Matches (u_short Pid, u_char Tid);
bool PutSection (const uchar * Data, int Length, bool Pusi);
int ReadPipe (void) const
{
return m_Pipe[0];
}
bool IsClosed (void);
void Close (void);
void Reset (void);
u_short Pid (void) const
{
return m_Pid;
}
u_char Tid (void) const
{
return m_Tid;
}
u_char Mask (void) const
{
return m_Mask;
}
};
inline bool cMcliFilter::Matches (u_short Pid, u_char Tid)
{
// printf("Match: %d == %d m_Tid %d == %d %02x\n", m_Pid, Pid, m_Tid, Tid & m_Mask, m_Mask);
return m_Pid == Pid && m_Tid == (Tid & m_Mask);
}
cMcliFilter::cMcliFilter (u_short Pid, u_char Tid, u_char Mask)
{
m_Used = 0;
m_Pid = Pid;
m_Tid = Tid;
m_Mask = Mask;
m_Pipe[0] = m_Pipe[1] = -1;
m_closed = false;
#ifdef SOCK_SEQPACKET
// SOCK_SEQPACKET (since kernel 2.6.4)
if (socketpair (AF_UNIX, SOCK_SEQPACKET, 0, m_Pipe) != 0) {
esyslog ("mcli: socketpair(SOCK_SEQPACKET) failed: %m, trying SOCK_DGRAM");
}
#endif
if (m_Pipe[0] < 0 && socketpair (AF_UNIX, SOCK_DGRAM, 0, m_Pipe) != 0) {
esyslog ("mcli: couldn't open section filter socket: %m");
}
else if (fcntl (m_Pipe[0], F_SETFL, O_NONBLOCK) != 0 || fcntl (m_Pipe[1], F_SETFL, O_NONBLOCK) != 0) {
esyslog ("mcli: couldn't set section filter socket to non-blocking mode: %m");
}
}
cMcliFilter::~cMcliFilter ()
{
// printf ("~cMcliFilter %p\n", this);
Close();
}
bool cMcliFilter::PutSection (const uchar * Data, int Length, bool Pusi)
{
if (!m_Used && !Pusi) { /* wait for payload unit start indicator */
// printf("Mittendrin pid %d tid %d mask %02x \n", Pid(), Tid(), Mask());
return true;
}
if (m_Used && Pusi) { /* reset at payload unit start */
// int length = (((m_Buffer[1] & 0x0F) << 8) | m_Buffer[2]) + 3;
// printf("RESET expect %d got %d for pid %d tid %d mask %02x \n",length, m_Used, Pid(), Tid(), Mask());
Reset ();
}
if (m_Used + Length >= (int) sizeof (m_Buffer)) {
esyslog ("ERROR: Mcli: Section handler buffer overflow (%d bytes lost)", Length);
Reset ();
return true;
}
memcpy (m_Buffer + m_Used, Data, Length);
m_Used += Length;
if (m_Used > 3) {
int length = (((m_Buffer[1] & 0x0F) << 8) | m_Buffer[2]) + 3;
// printf("-> pid %d Tid %d Mask %02x expect %d got %d\n",Pid(), Tid(), Mask(), length, m_Used);
if (m_Used >= length) {
// printf("Section complete\n");
m_Used = 0;
if (write (m_Pipe[1], m_Buffer, length) < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
//dsyslog ("cMcliFilter::PutSection socket overflow, " "Pid %4d Tid %3d", m_Pid, m_Tid);
;
else {
m_closed=true;
return false;
}
}
}
if (m_Used > length) {
dsyslog ("cMcliFilter::PutSection: m_Used > length ! Pid %2d, Tid%2d " "(len %3d, got %d/%d)", m_Pid, m_Tid, Length, m_Used, length);
if (Length < TS_SIZE - 5) {
// TS packet not full -> this must be last TS packet of section data -> safe to reset now
Reset ();
}
}
}
return true;
}
void cMcliFilter::Reset (void)
{
#ifdef DEBUG_FILTER
if (m_Used)
dsyslog ("cMcliFilter::Reset skipping %d bytes", m_Used);
#endif
m_Used = 0;
}
bool cMcliFilter::IsClosed (void)
{
char m_Buffer[3] = { 0, 0, 0 }; /* tid 0, 0 bytes */
if(m_closed) {
return m_closed;
}
// Test if pipe/socket has been closed by writing empty section
if ((write (m_Pipe[1], m_Buffer, 3) < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
if (errno != ECONNREFUSED && errno != ECONNRESET && errno != EPIPE)
esyslog ("cMcliFilter::IsClosed failed: %m");
m_closed = true;
return true;
}
return false;
}
void cMcliFilter::Close (void)
{
if(m_Pipe[0]>=0) {
close (m_Pipe[0]);
m_Pipe[0]=-1;
}
if(m_Pipe[1]>=0) {
close (m_Pipe[1]);
m_Pipe[1]=-1;
}
m_closed=true;
}
// --- cMcliFilters -----------------------------------------------------
cMcliFilters::cMcliFilters (void):
cThread ("mcli: sections assembler")
{
m_PB = NULL;
}
cMcliFilters::~cMcliFilters ()
{
}
int cMcliFilters::PutTS (const uchar * data, int len)
{
u_short pid = (((u_short) data[1] & PID_MASK_HI) << 8) | data[2];
if (m_PB && WantPid (pid)) {
int i;
for (i = 0; i < len; i += TS_SIZE) {
unsigned char *ptr = m_PB->PutStart (TS_SIZE);
if (ptr) {
memcpy (ptr, data + i, TS_SIZE);
m_PB->PutEnd (TS_SIZE, 0, 0);
}
}
}
return 0;
}
int cMcliFilters::CloseFilter (int Handle)
{
// printf("cMcliFilters::CloseFilter: %d\n", Handle);
GarbageCollect ();
int pid = GetPid (Handle);
if (pid != -1) {
m_pl.SetPid (pid, -1);
}
cMcliFilter *f=GetFilter(Handle);
if(f) {
LOCK_THREAD;
f->Close();
Del(f);
}
return pid;
}
int cMcliFilters::OpenFilter (u_short Pid, u_char Tid, u_char Mask)
{
// printf("cMcliFilters::OpenFilter: %d %d %02x\n", Pid, Tid, Mask);
GarbageCollect ();
if (!WantPid (Pid)) {
m_pl.SetPid (Pid, 0xffff);
}
if (!m_PB) {
m_PB = new cMyPacketBuffer (10000 * TS_SIZE, 10000);
m_PB->SetTimeouts (0, 20);
}
Start ();
cMcliFilter *f = new cMcliFilter (Pid, Tid, Mask);
int fh = f->ReadPipe ();
Lock ();
Add (f);
Unlock ();
return fh;
}
int cMcliPidList::GetTidFromPid (int pid)
{
for (cMcliPid * p = First (); p; p = Next (p)) {
if (p->Pid () == pid) {
// printf("Found pid %d -> tid %d\n",pid, p->Tid());
return p->Tid ();
}
}
return -1;
}
void cMcliPidList::SetPid (int Pid, int Tid)
{
if (Tid >= 0) {
for (cMcliPid * p = First (); p; p = Next (p)) {
if (p->Pid () == Pid) {
// printf("Change pid %d -> tid %d\n", Pid, Tid);
if (Tid != 0xffff) {
p->SetTid (Tid);
}
return;
}
}
cMcliPid *pid = new cMcliPid (Pid, Tid);
Add (pid);
// printf("Add pid %d -> tid %d\n", Pid, Tid);
} else {
for (cMcliPid * p = First (); p; p = Next (p)) {
if (p->Pid () == Pid) {
// printf("Del pid %d\n", Pid);
Del (p);
return;
}
}
}
}
int cMcliFilters::GetPid (int Handle)
{
int used = 0;
int pid = -1;
LOCK_THREAD;
for (cMcliFilter * fi = First (); fi; fi = Next (fi)) {
if (fi->ReadPipe () == Handle) {
pid = fi->Pid ();
break;
}
}
if(pid != -1) {
for (cMcliFilter * fi = First (); fi; fi = Next (fi)) {
if (pid == fi->Pid ()) {
used++;
}
}
}
// printf("Pid %d used %dx\n", pid, used);
if (used==1) {
return pid;
}
return -1;
}
cMcliFilter *cMcliFilters::GetFilter (int Handle)
{
LOCK_THREAD;
for (cMcliFilter * fi = First (); fi; fi = Next (fi)) {
if (fi->ReadPipe () == Handle) {
return fi;
}
}
return NULL;
}
bool cMcliFilters::WantPid (int pid)
{
LOCK_THREAD;
for (cMcliFilter * fi = First (); fi; fi = Next (fi)) {
if (pid == fi->Pid ()) {
return true;
}
}
return false;
}
void cMcliFilters::GarbageCollect (void)
{
LOCK_THREAD;
for (cMcliFilter * fi = First (); fi;) {
if (fi->IsClosed ()) {
if (errno == ECONNREFUSED || errno == ECONNRESET || errno == EPIPE) {
// printf ("cMcliFilters::GarbageCollector: filter closed: Pid %4d, Tid %3d, Mask %2x (%d filters left)\n", (int) fi->Pid (), (int) fi->Tid (), fi->Mask (), Count () - 1);
cMcliFilter *next = Prev (fi);
Del (fi);
fi = next ? Next (next) : First ();
} else {
esyslog ("cMcliFilters::GarbageCollector() error: " "Pid %4d, Tid %3d, Mask %2x (%d filters left) failed", (int) fi->Pid (), (int) fi->Tid (), fi->Mask (), Count () - 1);
LOG_ERROR;
fi = Next (fi);
}
} else {
fi = Next (fi);
}
}
}
void cMcliFilters::Action (void)
{
while (Running ()) {
m_PB->GetEnd ();
int size;
const uchar *block = m_PB->GetStart (&size, 0, 0);
if (block) {
u_short pid = (((u_short) block[1] & PID_MASK_HI) << 8) | block[2];
int offset = ((block[3] & 0x20) >> 5) * (block[4] + 1); // offset is the length of the adaption field (see vdr/remux.c).
bool Pusi = (block[1] & 0x40) >> 6;
int len=188-4-offset; //payload len
block=block+4+offset; //point to payload
if ( Pusi ) {
if ( len>(block[0]) ) { //process last section chunk
ProcessChunk(pid,block+1,block[0],0);
len -= block[0]+1;
block += block[0]+1;
} else {
len=0; //error!?!
}
} else {
if ( len>0 ) { //process section chunk
ProcessChunk(pid,block,len,0);
block +=len;
len = 0;
}
}
while ( len>0 ) { //process complete section or initial chunk
int chunklen= ( ( (block[1]<<8) | block[2] ) & 0xFFF ) + 3 ;
if ( chunklen>len ) {
chunklen=len;
}
ProcessChunk(pid,block,chunklen,1);
block +=chunklen;
len -= chunklen;
}
}
}
DELETENULL (m_PB);
dsyslog ("McliFilters::Action() ended");
}
void cMcliFilters::ProcessChunk(u_short pid, const uchar *block, int len, bool Pusi) {
int tid = -1;
if (Pusi) {
tid = (int) block[0];
m_pl.SetPid (pid, tid);
} else {
tid = m_pl.GetTidFromPid (pid);
if (tid == -1) {
//printf("Failed to get tid for pid %d\n", pid);
}
}
//printf("pid:%d tid:%d Pusi: %d len: %d\n", pid, tid, Pusi, len);
LOCK_THREAD;
cMcliFilter *f = First ();
while (f) {
cMcliFilter *next = Next (f);
if (tid != -1 && f->Matches (pid, tid)) {
//printf("Match!!!!");
if (!f->PutSection (block, len, Pusi)) {
if (errno != ECONNREFUSED && errno != ECONNRESET && errno != EPIPE) {
esyslog ("mcli: couldn't send section packet");
}
Del (f);
// Filter was closed.
// - need to check remaining filters for another match
} // if
}
f = next;
}
}