summaryrefslogtreecommitdiff
path: root/server/connectionVTP.c
blob: 9642f220010222077daac316d2134926f751953f (plain)
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
/*
 *  $Id: connectionVTP.c,v 1.3 2005/02/08 15:34:38 lordjaxom Exp $
 */
 
#include "server/connectionVTP.h"
#include "server/livestreamer.h"
#include "server/suspend.h"
#include "setup.h"

#include <vdr/tools.h>
#include <tools/select.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>

/* VTP Response codes:
	220: Service ready
	221: Service closing connection
	500: Syntax error or Command unrecognized
	501: Wrong parameters or missing parameters
	550: Action not done
	551: Data connection not accepted
	560: Channel not available currently
  561: Capability not known
	562: Pid not available currently
	563: Recording not available (currently?)
*/

cConnectionVTP::cConnectionVTP(void): cServerConnection("VTP") {
	m_LiveStreamer = NULL;
	memset(m_DataSockets, 0, sizeof(cTBSocket*) * si_Count);
}

cConnectionVTP::~cConnectionVTP() {
	if (m_LiveStreamer != NULL) delete m_LiveStreamer;

	for (int idx = 0; idx < si_Count; ++idx)
		if (m_DataSockets[idx] != NULL) delete m_DataSockets[idx];
}

void cConnectionVTP::Welcome(void) {
	Respond(220, "Welcome to Video Disk Recorder (VTP)");
}

void cConnectionVTP::Reject(void) {
	Respond(221, "Too many clients or client not allowed to connect");
	cServerConnection::Reject();
}

void cConnectionVTP::Detach(void) {
	if (m_LiveStreamer != NULL) m_LiveStreamer->Detach();
}

void cConnectionVTP::Attach(void) {
	if (m_LiveStreamer != NULL) m_LiveStreamer->Attach();
}

bool cConnectionVTP::Command(char *Cmd) {
	char *ep;
	if ((ep = strchr(Cmd, ' ')) != NULL)
		*(ep++) = '\0';
	else 
		ep = Cmd + strlen(Cmd);

	if      (strcasecmp(Cmd, "CAPS") == 0) return CmdCAPS(ep);
	else if (strcasecmp(Cmd, "PROV") == 0) return CmdPROV(ep);
	else if (strcasecmp(Cmd, "PORT") == 0) return CmdPORT(ep);
	else if (strcasecmp(Cmd, "TUNE") == 0) return CmdTUNE(ep);
	else if (strcasecmp(Cmd, "ADDP") == 0) return CmdADDP(ep);
	else if (strcasecmp(Cmd, "DELP") == 0) return CmdDELP(ep);
	else if (strcasecmp(Cmd, "ADDF") == 0) return CmdADDF(ep);
	else if (strcasecmp(Cmd, "DELF") == 0) return CmdDELF(ep);
	else if (strcasecmp(Cmd, "ABRT") == 0) return CmdABRT(ep);
	else if (strcasecmp(Cmd, "QUIT") == 0) return CmdQUIT(ep);
	else if (strcasecmp(Cmd, "SUSP") == 0) return CmdSUSP(ep);
	// Commands adopted from SVDRP
	else if (strcasecmp(Cmd, "LSTE") == 0) return CmdLSTE(ep);
	else if (strcasecmp(Cmd, "LSTR") == 0) return CmdLSTR(ep);
	else if (strcasecmp(Cmd, "DELR") == 0) return CmdDELR(ep);
	else if (strcasecmp(Cmd, "LSTT") == 0) return CmdLSTT(ep);
	else if (strcasecmp(Cmd, "MODT") == 0) return CmdMODT(ep);
	else if (strcasecmp(Cmd, "NEWT") == 0) return CmdNEWT(ep);
	else if (strcasecmp(Cmd, "DELT") == 0) return CmdDELT(ep);
	else
		return Respond(500, (cTBString)"Unknown Command '" + Cmd + "'");
}

bool cConnectionVTP::CmdCAPS(char *Opts) {
	if (strcasecmp(Opts, "TSPIDS") == 0) 
		return Respond(220, (cTBString)"Capability \"" + Opts + "\" accepted");
	return Respond(561, (cTBString)"Capability \"" + Opts + "\" not known");
}

bool cConnectionVTP::CmdPROV(char *Opts) {
	cChannel *chan;
	int prio;
	char *ep;
	
	prio = strtol(Opts, &ep, 10);
	if (ep == Opts || !isspace(*ep))
		return Respond(501, "Use: PROV Priority Channel");

	Opts = skipspace(ep);
	if ((chan = ChannelFromString(Opts)) == NULL)
		return Respond(550, (cTBString)"Undefined channel \"" + Opts + "\"");

	return GetDevice(chan, prio) != NULL
			? Respond(220, "Channel available")
			: Respond(560, "Channel not available");
}

bool cConnectionVTP::CmdPORT(char *Opts) {
	uint id, dataport = 0;
	char dataip[20];
	char *ep, *ipoffs;
	int n;

	id = strtoul(Opts, &ep, 10);
	if (ep == Opts || !isspace(*ep))
		return Respond(500, "Use: PORT Id Destination");

	if (id >= si_Count)
		return Respond(501, "Wrong connection id " + cTBString::Number(id));
	
	Opts = skipspace(ep);
	n = 0;
	ipoffs = dataip;
	while ((ep = strchr(Opts, ',')) != NULL) {
		if (n < 4) {
			memcpy(ipoffs, Opts, ep - Opts);
			ipoffs += ep - Opts;
			if (n < 3) *(ipoffs++) = '.';
		} else if (n == 4) {
			*ep = 0;
			dataport = strtoul(Opts, NULL, 10) << 8;
		} else
			break;
		Opts = ep + 1;
		++n;
	}
	*ipoffs = '\0';

	if (n != 5)
		return Respond(501, "Argument count invalid (must be 6 values)");

	dataport |= strtoul(Opts, NULL, 10);

	isyslog("Streamdev: Setting data connection to %s:%d", dataip, dataport);

	m_DataSockets[id] = new cTBSocket(SOCK_STREAM);
	if (!m_DataSockets[id]->Connect(dataip, dataport)) {
		esyslog("ERROR: Streamdev: Couldn't open data connection to %s:%d: %s",
				dataip, dataport, strerror(errno));
		DELETENULL(m_DataSockets[id]);
		return Respond(551, "Couldn't open data connection");
	}

	if (id == siLive)
		m_LiveStreamer->Start(m_DataSockets[id]);

  return Respond(220, "Port command ok, data connection opened");
}

bool cConnectionVTP::CmdTUNE(char *Opts) {
	const cChannel *chan;
	cDevice *dev;
	
	if ((chan = ChannelFromString(Opts)) == NULL)
		return Respond(550, (cTBString)"Undefined channel \"" + Opts + "\"");

	if ((dev = GetDevice(chan, 0)) == NULL)
		return Respond(560, "Channel not available");

	if (!dev->SwitchChannel(chan, false))
		return Respond(560, "Channel not available");

	delete m_LiveStreamer;
	m_LiveStreamer = new cStreamdevLiveStreamer(1);
	m_LiveStreamer->SetChannel(chan, stTSPIDS);
	m_LiveStreamer->SetDevice(dev);
	
	return Respond(220, "Channel tuned");
}

bool cConnectionVTP::CmdADDP(char *Opts) {
	int pid;
	char *end;

	pid = strtoul(Opts, &end, 10);
	if (end == Opts || (*end != '\0' && *end != ' '))
		return Respond(500, "Use: ADDP Pid");

	return m_LiveStreamer && m_LiveStreamer->SetPid(pid, true)
			? Respond(220, "Pid " + cTBString::Number(pid) + " available")
			: Respond(560, "Pid " + cTBString::Number(pid) + " not available");
}

bool cConnectionVTP::CmdDELP(char *Opts) {
	int pid;
	char *end;

	pid = strtoul(Opts, &end, 10);
	if (end == Opts || (*end != '\0' && *end != ' '))
		return Respond(500, "Use: DELP Pid");

	return m_LiveStreamer && m_LiveStreamer->SetPid(pid, false)
			? Respond(220, "Pid " + cTBString::Number(pid) + " stopped")
			: Respond(560, "Pid " + cTBString::Number(pid) + " not transferring");
}

bool cConnectionVTP::CmdADDF(char *Opts) {
#if VDRVERSNUM >= 10300
	int pid, tid, mask;
	char *ep;

	if (m_LiveStreamer == NULL)
		return Respond(560, "Can't set filters without a stream");
	
	pid = strtol(Opts, &ep, 10);
	if (ep == Opts || (*ep != ' '))
		return Respond(500, "Use: ADDF Pid Tid Mask");
	Opts = skipspace(ep);
	tid = strtol(Opts, &ep, 10);
	if (ep == Opts || (*ep != ' '))
		return Respond(500, "Use: ADDF Pid Tid Mask");
	Opts = skipspace(ep);
	mask = strtol(Opts, &ep, 10);
	if (ep == Opts || (*ep != '\0' && *ep != ' '))
		return Respond(500, "Use: ADDF Pid Tid Mask");

	return m_LiveStreamer->SetFilter(pid, tid, mask, true)
			? Respond(220, "Filter " + cTBString::Number(pid) + " transferring")
			: Respond(560, "Filter " + cTBString::Number(pid) + " not available");
#else
	return Respond(500, "ADDF known but unimplemented with VDR < 1.3.0");
#endif
}

bool cConnectionVTP::CmdDELF(char *Opts) {
#if VDRVERSNUM >= 10307
	int pid, tid, mask;
	char *ep;
	
	if (m_LiveStreamer == NULL)
		return Respond(560, "Can't delete filters without a stream");
	
	pid = strtol(Opts, &ep, 10);
	if (ep == Opts || (*ep != ' '))
		return Respond(500, "Use: DELF Pid Tid Mask");
	Opts = skipspace(ep);
	tid = strtol(Opts, &ep, 10);
	if (ep == Opts || (*ep != ' '))
		return Respond(500, "Use: DELF Pid Tid Mask");
	Opts = skipspace(ep);
	mask = strtol(Opts, &ep, 10);
	if (ep == Opts || (*ep != '\0' && *ep != ' '))
		return Respond(500, "Use: DELF Pid Tid Mask");

	return m_LiveStreamer->SetFilter(pid, tid, mask, false)
			? Respond(220, "Filter " + cTBString::Number(pid) + " stopped")
			: Respond(560, "Filter " + cTBString::Number(pid) + " not transferring");
#else
	return Respond(500, "DELF known but unimplemented with VDR < 1.3.0");
#endif
}

bool cConnectionVTP::CmdABRT(char *Opts) {
	uint id;
	char *ep;

	id = strtoul(Opts, &ep, 10);
	if (ep == Opts || (*ep != '\0' && *ep != ' '))
		return Respond(500, "Use: ABRT Id");

	cTimeMs starttime;
	if (id == siLive)
		DELETENULL(m_LiveStreamer);

	Dprintf("ABRT took %ld ms\n", starttime.Elapsed());
	DELETENULL(m_DataSockets[id]);
	return Respond(220, "Data connection closed");
}

bool cConnectionVTP::CmdQUIT(char *Opts) {
	if (!Respond(221, "Video Disk Recorder closing connection"))
		return false;
	DeferClose();
	return true;
}

bool cConnectionVTP::CmdSUSP(char *Opts) {
	if (StreamdevServerSetup.SuspendMode == smAlways || cSuspendCtl::IsActive())
		return Respond(220, "Server is suspended");
	else if (StreamdevServerSetup.SuspendMode == smOffer 
			&& StreamdevServerSetup.AllowSuspend) {
		cControl::Launch(new cSuspendCtl);
		return Respond(220, "Server is suspended");
	} else
		return Respond(550, "Client may not suspend server");
}

// Functions adopted from SVDRP
#define INIT_WRAPPER() bool _res = true
#define Reply(x...) _res &= ReplyWrapper(x)
#define EXIT_WRAPPER() return _res

bool cConnectionVTP::ReplyWrapper(int Code, const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	char *buffer;
	vasprintf(&buffer, fmt, ap);
	int npos;
	if (buffer[npos = strlen(buffer)-1] == '\n')
		buffer[npos] = '\0';
	bool res = Respond(Code, buffer);
	free(buffer);
	return res;
}

bool cConnectionVTP::CmdLSTE(char *Option) {
#if VDRVERSNUM < 10300
	cMutexLock MutexLock;
#else
  cSchedulesLock MutexLock;
#endif
	INIT_WRAPPER();
	/* we need to create a blocking copy of the socket here */
	int dupfd = dup(*this);
	fcntl(dupfd, F_SETFL, fcntl(dupfd, F_GETFL) & ~O_NONBLOCK);
#if VDRVERSNUM < 10300
  const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
#else
  const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
#endif
  if (Schedules) {
     FILE *f = fdopen(dupfd, "w");
     if (f) {
        Schedules->Dump(f, "215-");
        fflush(f);
        Reply(215, "End of EPG data");
				fclose(f);
        }
     else
        Reply(451, "Can't open file connection");
     }
  else
     Reply(451, "Can't get EPG data");
	EXIT_WRAPPER();
}

bool cConnectionVTP::CmdLSTR(char *Option) {
	INIT_WRAPPER();
  bool recordings = Recordings.Load();
	Recordings.Sort();
  if (*Option) {
     if (isnumber(Option)) {
        cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
        if (recording) {
           if (recording->Summary()) {
              char *summary = strdup(recording->Summary());
              Reply(250, "%s", strreplace(summary,'\n','|'));
              free(summary);
              }
           else
              Reply(550, "No summary availabe");
           }
        else
           Reply(550, "Recording \"%s\" not found", Option);
        }
     else
        Reply(501, "Error in recording number \"%s\"", Option);
     }
  else if (recordings) {
     cRecording *recording = Recordings.First();
     while (recording) {
           Reply(recording == Recordings.Last() ? 250 : -250, "%d %s", recording->Index() + 1, recording->Title(' ', true));
           recording = Recordings.Next(recording);
           }
     }
  else
     Reply(550, "No recordings available");
	EXIT_WRAPPER();
}

bool cConnectionVTP::CmdDELR(char *Option) {
	INIT_WRAPPER();
  if (*Option) {
     if (isnumber(Option)) {
        cRecording *recording = Recordings.Get(strtol(Option, NULL, 10) - 1);
        if (recording) {
           if (recording->Delete())
              Reply(250, "Recording \"%s\" deleted", Option);
           else
              Reply(554, "Error while deleting recording!");
           }
        else
           Reply(550, "Recording \"%s\" not found%s", Option, Recordings.Count() ? "" : " (use LSTR before deleting)");
        }
     else
        Reply(501, "Error in recording number \"%s\"", Option);
     }
  else
     Reply(501, "Missing recording number");
	EXIT_WRAPPER();
}

bool cConnectionVTP::CmdLSTT(char *Option) {
	INIT_WRAPPER();
  if (*Option) {
     if (isnumber(Option)) {
        cTimer *timer = Timers.Get(strtol(Option, NULL, 10) - 1);
        if (timer)
           Reply(250, "%d %s", timer->Index() + 1, (const char*)timer->ToText(true));
        else
           Reply(501, "Timer \"%s\" not defined", Option);
        }
     else
        Reply(501, "Error in timer number \"%s\"", Option);
     }
  else if (Timers.Count()) {
     for (int i = 0; i < Timers.Count(); i++) {
         cTimer *timer = Timers.Get(i);
        if (timer)
           Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, (const char*)timer->ToText(true));
        else
           Reply(501, "Timer \"%d\" not found", i + 1);
         }
     }
  else
     Reply(550, "No timers defined");
	EXIT_WRAPPER();
}

bool cConnectionVTP::CmdMODT(char *Option) {
	INIT_WRAPPER();
  if (*Option) {
     char *tail;
     int n = strtol(Option, &tail, 10);
     if (tail && tail != Option) {
        tail = skipspace(tail);
        cTimer *timer = Timers.Get(n - 1);
        if (timer) {
           cTimer t = *timer;
           if (strcasecmp(tail, "ON") == 0)
#if VDRVERSNUM < 10300
              t.SetActive(taActive);
#else
              t.SetFlags(tfActive);
#endif
           else if (strcasecmp(tail, "OFF") == 0)
#if VDRVERSNUM < 10300
              t.SetActive(taInactive);
#else
              t.ClrFlags(tfActive);
#endif
           else if (!t.Parse(tail)) {
              Reply(501, "Error in timer settings");
              EXIT_WRAPPER();
              }
           *timer = t;
           Timers.Save();
#if VDRVERSNUM < 10300
           isyslog("timer %d modified (%s)", timer->Index() + 1, 
							 timer->Active() ? "active" : "inactive");
#else
           isyslog("timer %d modified (%s)", timer->Index() + 1, 
							 timer->HasFlags(tfActive) ? "active" : "inactive");
#endif
           Reply(250, "%d %s", timer->Index() + 1, (const char*)timer->ToText(true));
           }
        else
           Reply(501, "Timer \"%d\" not defined", n);
        }
     else
        Reply(501, "Error in timer number");
     }
  else
     Reply(501, "Missing timer settings");
	EXIT_WRAPPER();
}

bool cConnectionVTP::CmdNEWT(char *Option) {
	INIT_WRAPPER();
  if (*Option) {
     cTimer *timer = new cTimer;
     if (timer->Parse(Option)) {
        cTimer *t = Timers.GetTimer(timer);
        if (!t) {
           Timers.Add(timer);
           Timers.Save();
           isyslog("timer %d added", timer->Index() + 1);
           Reply(250, "%d %s", timer->Index() + 1, (const char*)timer->ToText(true));
           EXIT_WRAPPER();
           }
        else
           Reply(550, "Timer already defined: %d %s", t->Index() + 1, (const char*)t->ToText(true));
        }
     else
        Reply(501, "Error in timer settings");
     delete timer;
     }
  else
     Reply(501, "Missing timer settings");
	EXIT_WRAPPER();
}

bool cConnectionVTP::CmdDELT(char *Option) {
	INIT_WRAPPER();
  if (*Option) {
     if (isnumber(Option)) {
        cTimer *timer = Timers.Get(strtol(Option, NULL, 10) - 1);
        if (timer) {
           if (!timer->Recording()) {
              Timers.Del(timer);
              Timers.Save();
              isyslog("timer %s deleted", Option);
              Reply(250, "Timer \"%s\" deleted", Option);
              }
           else
              Reply(550, "Timer \"%s\" is recording", Option);
           }
        else
           Reply(501, "Timer \"%s\" not defined", Option);
        }
     else
        Reply(501, "Error in timer number \"%s\"", Option);
     }
  else
     Reply(501, "Missing timer number");
	EXIT_WRAPPER();
}

bool cConnectionVTP::Respond(int Code, const char *Message) {
	cTBString pkt;
	if (Code < 0)
		pkt.Format("%03d-%s", -Code, Message);
	else	
		pkt.Format("%03d %s", Code, Message);
	return cServerConnection::Respond((const char*)pkt);
}