vdr-plugin-streamdev/client/socket.c

329 lines
8.0 KiB
C
Raw Normal View History

2004-12-30 23:43:55 +01:00
/*
* $Id: socket.c,v 1.13 2010/06/08 05:55:17 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 "common.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;
}
const char *Filters = "";
if(Command("CAPS FILTERS", 220))
Filters = ",FILTERS";
isyslog("Streamdev: Connected to server %s:%d using capabilities TSPIDS%s",
RemoteIp().c_str(), RemotePort(), Filters);
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::CloseDataConnection(eSocketId Id) {
//if (!CheckConnection()) return false;
CMD_LOCK;
if(Id == siLive || Id == siLiveFilter)
if (m_DataSockets[Id] != NULL) {
std::string command = (std::string)"ABRT " + (const char*)itoa(Id);
if (!Command(command, 220)) {
if (errno == 0)
esyslog("ERROR: Streamdev: Couldn't cleanly close data connection");
//return false;
}
DELETENULL(m_DataSockets[Id]);
}
return true;
2004-12-30 23:43:55 +01:00
}
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;
}
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;
}
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::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::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;
}