vdr-plugin-streamdev/client/socket.c

588 lines
14 KiB
C
Raw Normal View History

2004-12-30 23:43:55 +01:00
/*
2007-01-15 12:45:48 +01:00
* $Id: socket.c,v 1.7 2007/01/15 11:45:48 schmirl Exp $
2004-12-30 23:43:55 +01:00
*/
#include <tools/select.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
2007-01-15 12:36:37 +01:00
#include <stdint.h>
#include <time.h>
#define MINLOGREPEAT 10 //don't log connect failures too often (seconds)
2004-12-30 23:43:55 +01:00
#include "client/socket.h"
#include "client/setup.h"
#include "client/remote.h"
#include "common.h"
#include "i18n.h"
cClientSocket ClientSocket;
2005-02-08 16:34:38 +01:00
cClientSocket::cClientSocket(void)
{
2004-12-30 23:43:55 +01:00
memset(m_DataSockets, 0, sizeof(cTBSocket*) * si_Count);
Reset();
}
2005-02-08 16:34:38 +01:00
cClientSocket::~cClientSocket()
{
2004-12-30 23:43:55 +01:00
Reset();
if (IsOpen()) Quit();
}
2005-02-08 16:34:38 +01:00
void cClientSocket::Reset(void)
{
for (int it = 0; it < si_Count; ++it) {
2004-12-30 23:43:55 +01:00
if (m_DataSockets[it] != NULL)
DELETENULL(m_DataSockets[it]);
2005-02-08 16:34:38 +01:00
}
2004-12-30 23:43:55 +01:00
}
cTBSocket *cClientSocket::DataSocket(eSocketId Id) const {
return m_DataSockets[Id];
}
2005-02-08 18:22:35 +01:00
bool cClientSocket::Command(const std::string &Command, uint Expected, uint TimeoutMs)
{
2004-12-30 23:43:55 +01:00
errno = 0;
2005-02-08 18:22:35 +01:00
std::string pkt = Command + "\015\012";
Dprintf("OUT: |%s|\n", Command.c_str());
2004-12-30 23:43:55 +01:00
2005-02-08 14:59:16 +01:00
cTimeMs starttime;
2005-02-08 18:22:35 +01:00
if (!TimedWrite(pkt.c_str(), pkt.size(), TimeoutMs)) {
esyslog("Streamdev: Lost connection to %s:%d: %s", RemoteIp().c_str(), RemotePort(),
strerror(errno));
2004-12-30 23:43:55 +01:00
Close();
return false;
}
2007-01-15 12:36:37 +01:00
uint64_t elapsed = starttime.Elapsed();
2005-02-08 14:59:16 +01:00
if (Expected != 0) { // XXX+ What if elapsed > TimeoutMs?
TimeoutMs -= elapsed;
2004-12-30 23:43:55 +01:00
return Expect(Expected, NULL, TimeoutMs);
}
return true;
}
2005-02-08 18:22:35 +01:00
bool cClientSocket::Expect(uint Expected, std::string *Result, uint TimeoutMs) {
2004-12-30 23:43:55 +01:00
char *endptr;
int bufcount;
bool res;
errno = 0;
2005-02-08 18:22:35 +01:00
if ((bufcount = ReadUntil(m_Buffer, sizeof(m_Buffer) - 1, "\012", TimeoutMs)) == -1) {
esyslog("Streamdev: Lost connection to %s:%d: %s", RemoteIp().c_str(), RemotePort(),
strerror(errno));
2004-12-30 23:43:55 +01:00
Close();
return false;
}
2005-02-08 18:22:35 +01:00
if (m_Buffer[bufcount - 1] == '\015')
2004-12-30 23:43:55 +01:00
--bufcount;
2005-02-08 18:22:35 +01:00
m_Buffer[bufcount] = '\0';
Dprintf("IN: |%s|\n", m_Buffer);
2004-12-30 23:43:55 +01:00
if (Result != NULL)
2005-02-08 18:22:35 +01:00
*Result = m_Buffer;
2004-12-30 23:43:55 +01:00
2005-02-08 18:22:35 +01:00
res = strtoul(m_Buffer, &endptr, 10) == Expected;
2004-12-30 23:43:55 +01:00
return res;
}
bool cClientSocket::CheckConnection(void) {
CMD_LOCK;
if (IsOpen()) {
cTBSelect select;
Dprintf("connection open\n");
// XXX+ check if connection is still alive (is there a better way?)
// There REALLY shouldn't be anything readable according to PROTOCOL here
// If there is, assume it's an eof signal (subseq. read would return 0)
select.Add(*this, false);
int res;
if ((res = select.Select(0)) == 0) {
Dprintf("select said nothing happened\n");
return true;
}
Dprintf("closing connection (res was %d)", res);
Close();
}
if (!Connect(StreamdevClientSetup.RemoteIp, StreamdevClientSetup.RemotePort)){
2007-01-15 12:45:48 +01:00
static time_t lastTime = 0;
if (time(NULL) - lastTime > MINLOGREPEAT) {
2007-01-15 12:45:48 +01:00
esyslog("ERROR: Streamdev: Couldn't connect to %s:%d: %s",
2004-12-30 23:43:55 +01:00
(const char*)StreamdevClientSetup.RemoteIp,
StreamdevClientSetup.RemotePort, strerror(errno));
2007-01-15 12:45:48 +01:00
lastTime = time(NULL);
}
2004-12-30 23:43:55 +01:00
return false;
}
if (!Expect(220)) {
if (errno == 0)
esyslog("ERROR: Streamdev: Didn't receive greeting from %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
Close();
return false;
}
2005-02-08 18:22:35 +01:00
if (!Command("CAPS TSPIDS", 220)) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't negotiate capabilities on %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
Close();
return false;
}
2005-02-08 16:34:38 +01:00
isyslog("Streamdev: Connected to server %s:%d using capabilities TSPIDS",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
return true;
}
bool cClientSocket::ProvidesChannel(const cChannel *Channel, int Priority) {
if (!CheckConnection()) return false;
CMD_LOCK;
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"PROV " + (const char*)itoa(Priority) + " "
+ (const char*)Channel->GetChannelID().ToString();
if (!Command(command))
2004-12-30 23:43:55 +01:00
return false;
2005-02-08 18:22:35 +01:00
std::string buffer;
2004-12-30 23:43:55 +01:00
if (!Expect(220, &buffer)) {
2005-02-08 18:22:35 +01:00
if (buffer.substr(0, 3) != "560" && errno == 0)
2004-12-30 23:43:55 +01:00
esyslog("ERROR: Streamdev: Couldn't check if %s:%d provides channel %s",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort(), Channel->Name());
2004-12-30 23:43:55 +01:00
return false;
}
return true;
}
bool cClientSocket::CreateDataConnection(eSocketId Id) {
cTBSocket listen(SOCK_STREAM);
if (!CheckConnection()) return false;
if (m_DataSockets[Id] != NULL)
DELETENULL(m_DataSockets[Id]);
2005-02-08 18:22:35 +01:00
if (!listen.Listen(LocalIp(), 0, 1)) {
2004-12-30 23:43:55 +01:00
esyslog("ERROR: Streamdev: Couldn't create data connection: %s",
strerror(errno));
return false;
}
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"PORT " + (const char*)itoa(Id) + " "
+ LocalIp().c_str() + ","
+ (const char*)itoa((listen.LocalPort() >> 8) & 0xff) + ","
+ (const char*)itoa(listen.LocalPort() & 0xff);
size_t idx = 4;
while ((idx = command.find('.', idx + 1)) != (size_t)-1)
command[idx] = ',';
2004-12-30 23:43:55 +01:00
CMD_LOCK;
2005-02-08 18:22:35 +01:00
if (!Command(command, 220)) {
2004-12-30 23:43:55 +01:00
Dprintf("error: %m\n");
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't establish data connection to %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
return false;
}
/* The server SHOULD do the following:
* - get PORT command
* - connect to socket
* - return 220
*/
m_DataSockets[Id] = new cTBSocket;
if (!m_DataSockets[Id]->Accept(listen)) {
esyslog("ERROR: Streamdev: Couldn't establish data connection to %s:%d%s%s",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort(), errno == 0 ? "" : ": ",
2004-12-30 23:43:55 +01:00
errno == 0 ? "" : strerror(errno));
DELETENULL(m_DataSockets[Id]);
return false;
}
return true;
}
bool cClientSocket::SetChannelDevice(const cChannel *Channel) {
if (!CheckConnection()) return false;
CMD_LOCK;
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"TUNE "
+ (const char*)Channel->GetChannelID().ToString();
if (!Command(command, 220)) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't tune %s:%d to channel %s",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort(), Channel->Name());
2004-12-30 23:43:55 +01:00
return false;
}
return true;
}
bool cClientSocket::SetPid(int Pid, bool On) {
if (!CheckConnection()) return false;
2005-02-08 16:34:38 +01:00
CMD_LOCK;
2004-12-30 23:43:55 +01:00
2005-02-08 18:22:35 +01:00
std::string command = (std::string)(On ? "ADDP " : "DELP ") + (const char*)itoa(Pid);
if (!Command(command, 220)) {
2005-02-08 16:34:38 +01:00
if (errno == 0)
2005-02-08 18:22:35 +01:00
esyslog("Streamdev: Pid %d not available from %s:%d", Pid, LocalIp().c_str(),
LocalPort());
2005-02-08 16:34:38 +01:00
return false;
2004-12-30 23:43:55 +01:00
}
return true;
}
#if VDRVERSNUM >= 10300
bool cClientSocket::SetFilter(ushort Pid, uchar Tid, uchar Mask, bool On) {
if (!CheckConnection()) return false;
CMD_LOCK;
2005-02-08 18:22:35 +01:00
std::string command = (std::string)(On ? "ADDF " : "DELF ") + (const char*)itoa(Pid)
+ " " + (const char*)itoa(Tid) + " " + (const char*)itoa(Mask);
if (!Command(command, 220)) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("Streamdev: Filter %hu, %hhu, %hhu not available from %s:%d",
2005-02-08 18:22:35 +01:00
Pid, Tid, Mask, LocalIp().c_str(), LocalPort());
2004-12-30 23:43:55 +01:00
return false;
}
return true;
}
#endif
bool cClientSocket::CloseDvr(void) {
if (!CheckConnection()) return false;
CMD_LOCK;
if (m_DataSockets[siLive] != NULL) {
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"ABRT " + (const char*)itoa(siLive);
if (!Command(command, 220)) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't cleanly close data connection");
return false;
}
DELETENULL(m_DataSockets[siLive]);
}
return true;
}
bool cClientSocket::SynchronizeEPG(void) {
2005-02-08 18:22:35 +01:00
std::string buffer;
bool result;
2004-12-30 23:43:55 +01:00
FILE *epgfd;
if (!CheckConnection()) return false;
isyslog("Streamdev: Synchronizing EPG from server\n");
CMD_LOCK;
if (!Command("LSTE"))
return false;
if ((epgfd = tmpfile()) == NULL) {
esyslog("ERROR: Streamdev: Error while processing EPG data: %s",
strerror(errno));
return false;
}
2005-02-08 18:22:35 +01:00
while ((result = Expect(215, &buffer))) {
2004-12-30 23:43:55 +01:00
if (buffer[3] == ' ') break;
2005-02-08 18:22:35 +01:00
fputs(buffer.c_str() + 4, epgfd);
2004-12-30 23:43:55 +01:00
fputc('\n', epgfd);
}
2005-02-08 18:22:35 +01:00
if (!result) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't fetch EPG data from %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
fclose(epgfd);
return false;
}
rewind(epgfd);
if (cSchedules::Read(epgfd))
#if VDRVERSNUM < 10300
cSIProcessor::TriggerDump();
#else
cSchedules::Cleanup(true);
#endif
else {
esyslog("ERROR: Streamdev: Parsing EPG data failed");
fclose(epgfd);
return false;
}
fclose(epgfd);
return true;
}
bool cClientSocket::Quit(void) {
bool res;
if (!CheckConnection()) return false;
if (!(res = Command("QUIT", 221))) {
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't quit command connection to %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
}
Close();
return res;
}
bool cClientSocket::LoadRecordings(cRemoteRecordings &Recordings) {
bool res;
if (!CheckConnection()) return false;
CMD_LOCK;
if (!Command("LSTR"))
return false;
2005-02-08 18:22:35 +01:00
std::string buffer;
2004-12-30 23:43:55 +01:00
while ((res = Expect(250, &buffer))) {
2005-02-08 18:22:35 +01:00
cRemoteRecording *rec = new cRemoteRecording(buffer.c_str() + 4);
2004-12-30 23:43:55 +01:00
Dprintf("recording valid: %d\n", rec->IsValid());
if (rec->IsValid())
Recordings.Add(rec);
else
delete rec;
if (buffer[3] == ' ') break;
}
2005-02-08 18:22:35 +01:00
if (!res && buffer.substr(0, 3) != "550") {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't fetch recordings from %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
return false;
}
for (cRemoteRecording *r = Recordings.First(); r; r = Recordings.Next(r)) {
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"LSTR " + (const char*)itoa(r->Index());
if (!Command(command))
2004-12-30 23:43:55 +01:00
return false;
if (Expect(250, &buffer))
2005-02-08 18:22:35 +01:00
r->ParseInfo(buffer.c_str() + 4);
else if (buffer.substr(0, 3) != "550") {
2004-12-30 23:43:55 +01:00
if (errno == 0)
2005-02-08 18:22:35 +01:00
esyslog("ERROR: Streamdev: Couldn't fetch details for recording from %s:%d",
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
return false;
}
Dprintf("recording complete: %d\n", r->Index());
}
return res;
}
bool cClientSocket::StartReplay(const char *Filename) {
if (!CheckConnection()) return false;
CMD_LOCK;
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"PLAY " + Filename;
if (!Command(command, 220)) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't replay \"%s\" from %s:%d",
2005-02-08 18:22:35 +01:00
Filename, RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
return false;
}
return true;
}
bool cClientSocket::AbortReplay(void) {
if (!CheckConnection()) return false;
CMD_LOCK;
if (m_DataSockets[siReplay] != NULL) {
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"ABRT " + (const char*)itoa(siReplay);
if (!Command(command, 220)) {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't cleanly close data connection");
return false;
}
DELETENULL(m_DataSockets[siReplay]);
}
return true;
}
bool cClientSocket::DeleteRecording(cRemoteRecording *Recording) {
bool res;
cRemoteRecording *rec = NULL;
if (!CheckConnection())
return false;
CMD_LOCK;
if (!Command("LSTR"))
return false;
2005-02-08 18:22:35 +01:00
std::string buffer;
2004-12-30 23:43:55 +01:00
while ((res = Expect(250, &buffer))) {
if (rec == NULL) {
2005-02-08 18:22:35 +01:00
rec = new cRemoteRecording(buffer.c_str() + 4);
2004-12-30 23:43:55 +01:00
if (!rec->IsValid() || rec->Index() != Recording->Index())
DELETENULL(rec);
}
if (buffer[3] == ' ') break;
}
2005-02-08 18:22:35 +01:00
if (!res && buffer.substr(0, 3) != "550") {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't fetch recordings from %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
if (rec != NULL) delete rec;
return false;
}
if (rec == NULL || *rec != *Recording) {
ERROR(tr("Recordings not in sync! Try again..."));
return false;
}
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"DELR " + (const char*)itoa(Recording->Index());
if (!Command(command, 250)) {
2004-12-30 23:43:55 +01:00
ERROR(tr("Couldn't delete recording! Try again..."));
return false;
}
return true;
}
bool cClientSocket::SuspendServer(void) {
if (!CheckConnection()) return false;
CMD_LOCK;
if (!Command("SUSP", 220)) {
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't suspend server");
return false;
}
return true;
}
bool cClientSocket::LoadTimers(cRemoteTimers &Timers) {
if (!CheckConnection()) return false;
CMD_LOCK;
if (!Command("LSTT"))
return false;
2005-02-08 18:22:35 +01:00
bool res;
std::string buffer;
2004-12-30 23:43:55 +01:00
while ((res = Expect(250, &buffer))) {
2005-02-08 18:22:35 +01:00
cRemoteTimer *timer = new cRemoteTimer(buffer.c_str() + 4);
2004-12-30 23:43:55 +01:00
Dprintf("timer valid: %d\n", timer->IsValid());
if (timer->IsValid())
Timers.Add(timer);
if (buffer[3] == ' ') break;
}
2005-02-08 18:22:35 +01:00
if (!res && buffer.substr(0, 3) != "550") {
2004-12-30 23:43:55 +01:00
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't fetch recordings from %s:%d",
2005-02-08 18:22:35 +01:00
RemoteIp().c_str(), RemotePort());
2004-12-30 23:43:55 +01:00
return false;
}
return res;
}
bool cClientSocket::SaveTimer(cRemoteTimer *Old, cRemoteTimer &New) {
if (!CheckConnection()) return false;
CMD_LOCK;
if (New.Index() == -1) { // New timer
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"NEWT " + (const char*)New.ToText();
if (!Command(command, 250)) {
2004-12-30 23:43:55 +01:00
ERROR(tr("Couldn't save timer! Try again..."));
return false;
}
} else { // Modified timer
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"LSTT " + (const char*)itoa(New.Index());
if (!Command(command))
2004-12-30 23:43:55 +01:00
return false;
2005-02-08 18:22:35 +01:00
std::string buffer;
2004-12-30 23:43:55 +01:00
if (!Expect(250, &buffer)) {
if (errno == 0)
ERROR(tr("Timers not in sync! Try again..."));
else
ERROR(tr("Server error! Try again..."));
return false;
}
2005-02-08 18:22:35 +01:00
cRemoteTimer oldstate(buffer.c_str() + 4);
2004-12-30 23:43:55 +01:00
if (oldstate != *Old) {
/*Dprintf("old timer: %d,%d,%d,%d,%d,%d,%s,%d,%s,%d\n", oldstate.m_Index,
oldstate.m_Active,oldstate.m_Day,oldstate.m_Start,oldstate.m_StartTime,oldstate.m_Priority,oldstate.m_File,oldstate.m_FirstDay,(const char*)oldstate.m_Summary,oldstate.m_Channel->Number());
Dprintf("new timer: %d,%d,%d,%d,%d,%d,%s,%d,%s,%d\n", Old->m_Index,
Old->m_Active,Old->m_Day,Old->m_Start,Old->m_StartTime,Old->m_Priority,Old->m_File,Old->m_FirstDay,(const char*)Old->m_Summary,Old->m_Channel->Number());*/
ERROR(tr("Timers not in sync! Try again..."));
return false;
}
2005-02-08 18:22:35 +01:00
command = (std::string)"MODT " + (const char*)itoa(New.Index()) + " "
+ (const char*)New.ToText();
if (!Command(command, 250)) {
2004-12-30 23:43:55 +01:00
ERROR(tr("Couldn't save timer! Try again..."));
return false;
}
}
return true;
}
bool cClientSocket::DeleteTimer(cRemoteTimer *Timer) {
2005-02-08 18:22:35 +01:00
if (!CheckConnection()) return false;
2004-12-30 23:43:55 +01:00
CMD_LOCK;
2005-02-08 18:22:35 +01:00
std::string command = (std::string)"LSTT " + (const char*)itoa(Timer->Index());
if (!Command(command))
2004-12-30 23:43:55 +01:00
return false;
2005-02-08 18:22:35 +01:00
std::string buffer;
2004-12-30 23:43:55 +01:00
if (!Expect(250, &buffer)) {
if (errno == 0)
ERROR(tr("Timers not in sync! Try again..."));
else
ERROR(tr("Server error! Try again..."));
return false;
}
2005-02-08 18:22:35 +01:00
cRemoteTimer oldstate(buffer.c_str() + 4);
2004-12-30 23:43:55 +01:00
if (oldstate != *Timer) {
ERROR(tr("Timers not in sync! Try again..."));
return false;
}
2005-02-08 18:22:35 +01:00
command = (std::string)"DELT " + (const char*)itoa(Timer->Index());
if (!Command(command, 250)) {
2004-12-30 23:43:55 +01:00
ERROR(tr("Couldn't delete timer! Try again..."));
return false;
}
return true;
}