2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2010-12-02 09:57:17 +01:00
|
|
|
* $Id: connection.c,v 1.16 2010/08/03 10:51:53 schmirl Exp $
|
2004-12-30 23:43:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "server/connection.h"
|
|
|
|
#include "server/setup.h"
|
|
|
|
#include "server/suspend.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <vdr/tools.h>
|
2011-03-07 21:37:08 +01:00
|
|
|
#include <vdr/thread.h>
|
2004-12-30 23:43:55 +01:00
|
|
|
#include <string.h>
|
2005-05-09 22:30:38 +02:00
|
|
|
#include <stdarg.h>
|
2004-12-30 23:43:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2009-02-13 11:39:20 +01:00
|
|
|
cServerConnection::cServerConnection(const char *Protocol, int Type):
|
|
|
|
cTBSocket(Type),
|
2005-05-09 22:22:29 +02:00
|
|
|
m_Protocol(Protocol),
|
|
|
|
m_DeferClose(false),
|
2011-10-24 08:44:31 +02:00
|
|
|
m_Pending(false),
|
2005-05-09 22:22:29 +02:00
|
|
|
m_ReadBytes(0),
|
|
|
|
m_WriteBytes(0),
|
2012-03-31 16:19:44 +02:00
|
|
|
m_WriteIndex(0),
|
2014-06-07 00:24:27 +02:00
|
|
|
m_Streamer(NULL)
|
2005-05-09 22:22:29 +02:00
|
|
|
{
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
cServerConnection::~cServerConnection()
|
|
|
|
{
|
2014-05-18 18:16:51 +02:00
|
|
|
delete(m_Streamer);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
const cChannel* cServerConnection::ChannelFromString(const char *String, int *Apid, int *Dpid) {
|
2009-09-18 12:41:41 +02:00
|
|
|
const cChannel *channel = NULL;
|
|
|
|
char *string = strdup(String);
|
|
|
|
char *ptr, *end;
|
|
|
|
int apididx = 0;
|
|
|
|
|
|
|
|
if ((ptr = strrchr(string, '+')) != NULL) {
|
|
|
|
*(ptr++) = '\0';
|
|
|
|
apididx = strtoul(ptr, &end, 10);
|
|
|
|
Dprintf("found apididx: %d\n", apididx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isnumber(string)) {
|
|
|
|
int temp = strtol(String, NULL, 10);
|
2012-04-21 22:47:36 +02:00
|
|
|
if (temp == 0)
|
|
|
|
temp = cDevice::CurrentChannel();
|
2009-09-18 12:41:41 +02:00
|
|
|
if (temp >= 1 && temp <= Channels.MaxNumber())
|
|
|
|
channel = Channels.GetByNumber(temp);
|
|
|
|
} else {
|
|
|
|
channel = Channels.GetByChannelID(tChannelID::FromString(string));
|
|
|
|
|
|
|
|
if (channel == NULL) {
|
|
|
|
int i = 1;
|
|
|
|
while ((channel = Channels.GetByNumber(i, 1)) != NULL) {
|
|
|
|
if (String == channel->Name())
|
|
|
|
break;
|
|
|
|
|
|
|
|
i = channel->Number() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel != NULL && apididx > 0) {
|
2010-12-02 09:57:17 +01:00
|
|
|
int apid = 0, dpid = 0;
|
|
|
|
int index = 1;
|
2009-09-18 12:41:41 +02:00
|
|
|
|
|
|
|
for (int i = 0; channel->Apid(i) != 0; ++i, ++index) {
|
|
|
|
if (index == apididx) {
|
|
|
|
apid = channel->Apid(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apid == 0) {
|
|
|
|
for (int i = 0; channel->Dpid(i) != 0; ++i, ++index) {
|
|
|
|
if (index == apididx) {
|
2010-12-02 09:57:17 +01:00
|
|
|
dpid = channel->Dpid(i);
|
2009-09-18 12:41:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Apid != NULL)
|
|
|
|
*Apid = apid;
|
2010-12-02 09:57:17 +01:00
|
|
|
if (Dpid != NULL)
|
|
|
|
*Dpid = dpid;
|
2009-09-18 12:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(string);
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
bool cServerConnection::Read(void)
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
if ((b = cTBSocket::Read(m_ReadBuffer + m_ReadBytes,
|
|
|
|
sizeof(m_ReadBuffer) - m_ReadBytes - 1)) < 0) {
|
|
|
|
esyslog("ERROR: read from client (%s) %s:%d failed: %m",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
if (b == 0) {
|
|
|
|
isyslog("client (%s) %s:%d has closed connection",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
m_ReadBytes += b;
|
|
|
|
m_ReadBuffer[m_ReadBytes] = '\0';
|
|
|
|
|
|
|
|
char *end;
|
|
|
|
bool result = true;
|
|
|
|
while ((end = strchr(m_ReadBuffer, '\012')) != NULL) {
|
|
|
|
*end = '\0';
|
|
|
|
if (end > m_ReadBuffer && *(end - 1) == '\015')
|
|
|
|
*(end - 1) = '\0';
|
|
|
|
|
|
|
|
if (!Command(m_ReadBuffer))
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
m_ReadBytes -= ++end - m_ReadBuffer;
|
|
|
|
if (m_ReadBytes > 0)
|
|
|
|
memmove(m_ReadBuffer, end, m_ReadBytes);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
if (m_ReadBytes == sizeof(m_ReadBuffer) - 1) {
|
|
|
|
esyslog("ERROR: streamdev: input buffer overflow (%s) for %s:%d",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2005-05-09 22:22:29 +02:00
|
|
|
|
|
|
|
return result;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
bool cServerConnection::Write(void)
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
if ((b = cTBSocket::Write(m_WriteBuffer + m_WriteIndex,
|
|
|
|
m_WriteBytes - m_WriteIndex)) < 0) {
|
|
|
|
esyslog("ERROR: streamdev: write to client (%s) %s:%d failed: %m",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_WriteIndex += b;
|
|
|
|
if (m_WriteIndex == m_WriteBytes) {
|
|
|
|
m_WriteIndex = 0;
|
|
|
|
m_WriteBytes = 0;
|
2011-10-24 08:44:31 +02:00
|
|
|
if (m_Pending)
|
|
|
|
Command(NULL);
|
2005-05-09 22:22:29 +02:00
|
|
|
if (m_DeferClose)
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
2005-05-09 22:22:29 +02:00
|
|
|
Flushed();
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-24 08:44:31 +02:00
|
|
|
bool cServerConnection::Respond(const char *Message, bool Last, ...)
|
2005-05-09 22:22:29 +02:00
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int length;
|
|
|
|
va_list ap;
|
2011-10-24 08:44:31 +02:00
|
|
|
va_start(ap, Last);
|
2005-05-09 22:22:29 +02:00
|
|
|
length = vasprintf(&buffer, Message, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2007-05-07 14:25:11 +02:00
|
|
|
if (length < 0) {
|
|
|
|
esyslog("ERROR: streamdev: buffer allocation failed (%s) for %s:%d",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
if (m_WriteBytes + length + 2 > sizeof(m_WriteBuffer)) {
|
|
|
|
esyslog("ERROR: streamdev: output buffer overflow (%s) for %s:%d",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
2007-05-07 13:43:23 +02:00
|
|
|
free(buffer);
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2005-05-09 22:22:29 +02:00
|
|
|
Dprintf("OUT: |%s|\n", buffer);
|
|
|
|
memcpy(m_WriteBuffer + m_WriteBytes, buffer, length);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
m_WriteBytes += length;
|
|
|
|
m_WriteBuffer[m_WriteBytes++] = '\015';
|
|
|
|
m_WriteBuffer[m_WriteBytes++] = '\012';
|
2011-10-24 08:44:31 +02:00
|
|
|
m_Pending = !Last;
|
2004-12-30 23:43:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-02 09:57:17 +01:00
|
|
|
|
2011-11-28 16:23:57 +01:00
|
|
|
bool cServerConnection::Close()
|
|
|
|
{
|
|
|
|
if (IsOpen())
|
|
|
|
isyslog("streamdev-server: closing %s connection to %s:%d", Protocol(), RemoteIp().c_str(), RemotePort());
|
|
|
|
return cTBSocket::Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
cString cServerConnection::ToText() const
|
|
|
|
{ return cString::sprintf("%s\t%s:%d", Protocol(), RemoteIp().c_str(), RemotePort()); }
|