2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2010-12-02 09:57:17 +01:00
|
|
|
* $Id: socket.c,v 1.15 2010/08/18 10:26:55 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>
|
2007-01-15 12:41:46 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define MINLOGREPEAT 10 //don't log connect failures too often (seconds)
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
// timeout for writing to command socket
|
|
|
|
#define WRITE_TIMEOUT_MS 200
|
|
|
|
#define QUIT_TIMEOUT_MS 500
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
#include "client/socket.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
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);
|
2012-04-21 22:28:58 +02:00
|
|
|
m_ServerVersion = 0;
|
2013-01-29 00:02:17 +01:00
|
|
|
m_Priority = -100;
|
2010-12-02 09:57:17 +01:00
|
|
|
m_Prio = false;
|
2012-03-03 23:24:30 +01:00
|
|
|
m_Abort = false;
|
2011-07-06 09:13:50 +02:00
|
|
|
m_LastSignalUpdate = 0;
|
|
|
|
m_LastSignalStrength = -1;
|
|
|
|
m_LastSignalQuality = -1;
|
2012-06-07 19:23:14 +02:00
|
|
|
m_LastDev = -1;
|
2004-12-30 23:43:55 +01:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
bool cClientSocket::Command(const std::string &Command, uint Expected)
|
2005-02-08 18:22:35 +01:00
|
|
|
{
|
2012-03-03 23:24:30 +01:00
|
|
|
uint code = 0;
|
|
|
|
std::string buffer;
|
|
|
|
if (Send(Command) && Receive(Command, &code, &buffer)) {
|
|
|
|
if (code == Expected)
|
|
|
|
return true;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
dsyslog("streamdev-client: Command '%s' rejected by %s:%d: %s",
|
|
|
|
Command.c_str(), RemoteIp().c_str(), RemotePort(), buffer.c_str());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool cClientSocket::Send(const std::string &Command)
|
|
|
|
{
|
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
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
errno = 0;
|
|
|
|
if (!TimedWrite(pkt.c_str(), pkt.size(), WRITE_TIMEOUT_MS)) {
|
|
|
|
esyslog("ERROR: streamdev-client: Failed sending command '%s' to %s:%d: %s",
|
|
|
|
Command.c_str(), RemoteIp().c_str(), RemotePort(), strerror(errno));
|
2004-12-30 23:43:55 +01:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
#define TIMEOUT_MS 1000
|
|
|
|
bool cClientSocket::Receive(const std::string &Command, uint *Code, std::string *Result, uint TimeoutMs) {
|
2004-12-30 23:43:55 +01:00
|
|
|
int bufcount;
|
2012-03-03 23:24:30 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
bufcount = ReadUntil(m_Buffer, sizeof(m_Buffer) - 1, "\012", TimeoutMs < TIMEOUT_MS ? TimeoutMs : TIMEOUT_MS);
|
|
|
|
if (bufcount == -1) {
|
|
|
|
if (m_Abort)
|
|
|
|
return false;
|
|
|
|
if (errno != ETIMEDOUT || TimeoutMs <= TIMEOUT_MS) {
|
|
|
|
esyslog("ERROR: streamdev-client: Failed reading reply to '%s' from %s:%d: %s",
|
|
|
|
Command.c_str(), RemoteIp().c_str(), RemotePort(), strerror(errno));
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TimeoutMs -= TIMEOUT_MS;
|
|
|
|
}
|
|
|
|
} while (bufcount == -1);
|
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;
|
2012-03-03 23:24:30 +01:00
|
|
|
if (Code != NULL)
|
|
|
|
*Code = strtoul(m_Buffer, NULL, 10);
|
|
|
|
return true;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
if (!Connect(StreamdevClientSetup.RemoteIp, StreamdevClientSetup.RemotePort, StreamdevClientSetup.Timeout * 1000)){
|
2007-01-15 12:45:48 +01:00
|
|
|
static time_t lastTime = 0;
|
2007-01-15 12:41:46 +01:00
|
|
|
if (time(NULL) - lastTime > MINLOGREPEAT) {
|
2012-03-03 23:24:30 +01:00
|
|
|
esyslog("ERROR: streamdev-client: 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);
|
2007-01-15 12:41:46 +01:00
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
uint code = 0;
|
|
|
|
std::string buffer;
|
|
|
|
if (!Receive("<connect>", &code, &buffer)) {
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (code != 220) {
|
|
|
|
esyslog("ERROR: streamdev-client: Didn't receive greeting from %s:%d: %s",
|
|
|
|
RemoteIp().c_str(), RemotePort(), buffer.c_str());
|
2004-12-30 23:43:55 +01:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-21 22:28:58 +02:00
|
|
|
unsigned int major, minor;
|
|
|
|
if (sscanf(buffer.c_str(), "%*u VTP/%u.%u", &major, &minor) == 2)
|
|
|
|
m_ServerVersion = major * 100 + minor;
|
|
|
|
|
|
|
|
if (m_ServerVersion == 0) {
|
|
|
|
if (!Command("CAPS TSPIDS", 220)) {
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2012-04-21 22:28:58 +02:00
|
|
|
const char *Filters = "";
|
|
|
|
if(Command("CAPS FILTERS", 220))
|
|
|
|
Filters = ",FILTERS";
|
2008-03-13 17:01:17 +01:00
|
|
|
|
2012-04-21 22:28:58 +02:00
|
|
|
const char *Prio = "";
|
|
|
|
if(Command("CAPS PRIO", 220)) {
|
|
|
|
Prio = ",PRIO";
|
|
|
|
m_Prio = true;
|
|
|
|
}
|
|
|
|
isyslog("streamdev-client: Connected to server %s:%d using capabilities TSPIDS%s%s",
|
|
|
|
RemoteIp().c_str(), RemotePort(), Filters, Prio);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(!Command("VERS 1.0", 220)) {
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-02 09:57:17 +01:00
|
|
|
m_Prio = true;
|
2012-04-21 22:28:58 +02:00
|
|
|
isyslog("streamdev-client: Connected to server %s:%d using protocol version %u.%u",
|
|
|
|
RemoteIp().c_str(), RemotePort(), major, minor);
|
2010-12-02 09:57:17 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2012-03-03 23:24:30 +01:00
|
|
|
if (!Send(command))
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
uint code;
|
2005-02-08 18:22:35 +01:00
|
|
|
std::string buffer;
|
2012-03-03 23:24:30 +01:00
|
|
|
if (!Receive(command, &code, &buffer))
|
|
|
|
return false;
|
|
|
|
if (code != 220 && code != 560) {
|
|
|
|
esyslog("streamdev-client: Unexpected reply to '%s' from %s:%d: %s",
|
|
|
|
command.c_str(), RemoteIp().c_str(), RemotePort(), buffer.c_str());
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-10 23:28:53 +01:00
|
|
|
return code == 220;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2012-03-03 23:24:30 +01:00
|
|
|
esyslog("ERROR: streamdev-client: Couldn't create data connection: %s",
|
2004-12-30 23:43:55 +01:00
|
|
|
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;
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
if (!Command(command, 220))
|
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)) {
|
2012-03-03 23:24:30 +01:00
|
|
|
esyslog("ERROR: streamdev-client: 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;
|
2007-04-24 12:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cClientSocket::CloseDataConnection(eSocketId Id) {
|
|
|
|
CMD_LOCK;
|
|
|
|
|
|
|
|
if(Id == siLive || Id == siLiveFilter)
|
|
|
|
if (m_DataSockets[Id] != NULL) {
|
|
|
|
std::string command = (std::string)"ABRT " + (const char*)itoa(Id);
|
2012-03-03 23:24:30 +01:00
|
|
|
Command(command, 220);
|
2007-04-24 12:57:34 +02:00
|
|
|
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 "
|
2010-12-02 09:57:17 +01:00
|
|
|
+ (const char*)Channel->GetChannelID().ToString();
|
2012-03-03 23:24:30 +01:00
|
|
|
if (!Command(command, 220))
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
2011-07-06 09:13:50 +02:00
|
|
|
|
|
|
|
m_LastSignalUpdate = 0;
|
2004-12-30 23:43:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
bool cClientSocket::SetPriority(int Priority) {
|
2013-01-29 00:02:17 +01:00
|
|
|
if (Priority == m_Priority)
|
|
|
|
return true;
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
if (!CheckConnection()) return false;
|
|
|
|
|
|
|
|
CMD_LOCK;
|
|
|
|
|
|
|
|
std::string command = (std::string)"PRIO " + (const char*)itoa(Priority);
|
2013-01-29 00:02:17 +01:00
|
|
|
if (!Command(command, 220))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Priority = Priority;
|
|
|
|
return true;
|
2010-12-02 09:57:17 +01:00
|
|
|
}
|
|
|
|
|
2012-06-07 19:23:14 +02:00
|
|
|
bool cClientSocket::GetSignal(int *SignalStrength, int *SignalQuality, int *Dev) {
|
2011-07-06 09:13:50 +02:00
|
|
|
if (!CheckConnection()) return -1;
|
|
|
|
|
|
|
|
CMD_LOCK;
|
|
|
|
|
|
|
|
if (m_LastSignalUpdate != time(NULL)) {
|
2012-03-03 23:24:30 +01:00
|
|
|
uint code = 0;
|
2011-07-06 09:13:50 +02:00
|
|
|
std::string buffer;
|
2012-03-03 23:24:30 +01:00
|
|
|
std::string command("SGNL");
|
|
|
|
if (!Send(command) || !Receive(command, &code, &buffer) || code != 220
|
2012-06-07 19:23:14 +02:00
|
|
|
|| sscanf(buffer.c_str(), "%*d %d %d:%d", &m_LastDev, &m_LastSignalStrength, &m_LastSignalQuality) != 3) {
|
|
|
|
m_LastDev = -1;
|
2011-07-06 09:13:50 +02:00
|
|
|
m_LastSignalStrength = -1;
|
|
|
|
m_LastSignalQuality = -1;
|
|
|
|
}
|
|
|
|
m_LastSignalUpdate = time(NULL);
|
|
|
|
}
|
|
|
|
if (SignalStrength)
|
|
|
|
*SignalStrength = m_LastSignalStrength;
|
|
|
|
if (SignalQuality)
|
|
|
|
*SignalQuality = m_LastSignalQuality;
|
2012-06-07 19:23:14 +02:00
|
|
|
if (Dev)
|
|
|
|
*Dev = m_LastDev;
|
2011-07-06 09:13:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
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);
|
2012-03-03 23:24:30 +01:00
|
|
|
return Command(command, 220);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-03-03 23:24:30 +01:00
|
|
|
return Command(command, 220);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-03-03 23:24:30 +01:00
|
|
|
if (!Command(command, 220))
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
DELETENULL(m_DataSockets[siLive]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cClientSocket::Quit(void) {
|
2012-03-03 23:24:30 +01:00
|
|
|
m_Abort = true;
|
|
|
|
if (!IsOpen()) return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
CMD_LOCK;
|
|
|
|
std::string command("QUIT");
|
|
|
|
bool res = Send(command) && Receive(command, NULL, NULL, QUIT_TIMEOUT_MS);
|
2004-12-30 23:43:55 +01:00
|
|
|
Close();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cClientSocket::SuspendServer(void) {
|
|
|
|
if (!CheckConnection()) return false;
|
|
|
|
|
|
|
|
CMD_LOCK;
|
|
|
|
|
2012-03-03 23:24:30 +01:00
|
|
|
return Command("SUSP", 220);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|