2007-10-21 15:31:21 +02:00
|
|
|
/*
|
|
|
|
* socket.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vdr/device.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "socket.h"
|
|
|
|
|
|
|
|
cIptvSocket::cIptvSocket()
|
2007-10-21 17:49:54 +02:00
|
|
|
: socketPort(0),
|
2007-10-21 15:31:21 +02:00
|
|
|
socketDesc(-1),
|
|
|
|
isActive(false)
|
|
|
|
{
|
|
|
|
debug("cIptvSocket::cIptvSocket()\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvSocket::~cIptvSocket()
|
|
|
|
{
|
|
|
|
debug("cIptvSocket::~cIptvSocket()\n");
|
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvSocket::OpenSocket(const int Port, const bool isUdp)
|
|
|
|
{
|
|
|
|
debug("cIptvSocket::OpenSocket()\n");
|
|
|
|
// If socket is there already and it is bound to a different port, it must
|
|
|
|
// be closed first
|
|
|
|
if (Port != socketPort) {
|
|
|
|
debug("cIptvSocket::OpenSocket(): Socket tear-down\n");
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
// Bind to the socket if it is not active already
|
|
|
|
if (socketDesc < 0) {
|
2009-02-26 15:04:12 +01:00
|
|
|
int yes = 1;
|
2007-10-21 15:31:21 +02:00
|
|
|
// Create socket
|
|
|
|
if (isUdp)
|
|
|
|
socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
else
|
|
|
|
socketDesc = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
ERROR_IF_RET(socketDesc < 0, "socket()", return false);
|
|
|
|
// Make it use non-blocking I/O to avoid stuck read calls
|
2009-02-27 15:05:19 +01:00
|
|
|
ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()",
|
|
|
|
CloseSocket(), return false);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Allow multiple sockets to use the same PORT number
|
2009-02-27 15:05:19 +01:00
|
|
|
ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0, "setsockopt()",
|
|
|
|
CloseSocket(), return false);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Bind socket
|
|
|
|
memset(&sockAddr, '\0', sizeof(sockAddr));
|
|
|
|
sockAddr.sin_family = AF_INET;
|
|
|
|
sockAddr.sin_port = htons(Port);
|
|
|
|
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (isUdp) {
|
|
|
|
int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
|
|
|
|
ERROR_IF_FUNC(err < 0, "bind()", CloseSocket(), return false);
|
2008-01-30 22:57:33 +01:00
|
|
|
}
|
2007-10-21 15:31:21 +02:00
|
|
|
// Update socket port
|
|
|
|
socketPort = Port;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvSocket::CloseSocket(void)
|
|
|
|
{
|
|
|
|
debug("cIptvSocket::CloseSocket()\n");
|
|
|
|
// Check if socket exists
|
|
|
|
if (socketDesc >= 0) {
|
|
|
|
close(socketDesc);
|
|
|
|
socketDesc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-21 19:32:43 +02:00
|
|
|
// UDP socket class
|
|
|
|
cIptvUdpSocket::cIptvUdpSocket()
|
2007-10-21 15:31:21 +02:00
|
|
|
{
|
2007-10-21 19:32:43 +02:00
|
|
|
debug("cIptvUdpSocket::cIptvUdpSocket()\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvUdpSocket::~cIptvUdpSocket()
|
|
|
|
{
|
|
|
|
debug("cIptvUdpSocket::~cIptvUdpSocket()\n");
|
|
|
|
}
|
|
|
|
|
2007-10-21 21:32:14 +02:00
|
|
|
bool cIptvUdpSocket::OpenSocket(const int Port)
|
|
|
|
{
|
|
|
|
debug("cIptvUdpSocket::OpenSocket()\n");
|
|
|
|
return cIptvSocket::OpenSocket(Port, true);
|
|
|
|
}
|
|
|
|
|
2009-02-26 15:04:12 +01:00
|
|
|
int cIptvUdpSocket::Read(unsigned char* BufferAddr, unsigned int BufferLen)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
|
|
|
//debug("cIptvUdpSocket::Read()\n");
|
2007-10-21 15:31:21 +02:00
|
|
|
// Error out if socket not initialized
|
|
|
|
if (socketDesc <= 0) {
|
2008-01-30 22:57:33 +01:00
|
|
|
error("ERROR: Invalid socket in %s\n", __FUNCTION__);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-10-21 15:31:21 +02:00
|
|
|
socklen_t addrlen = sizeof(sockAddr);
|
2009-02-26 15:04:12 +01:00
|
|
|
int len = 0;
|
|
|
|
// Read data from socket
|
|
|
|
if (isActive)
|
|
|
|
len = recvfrom(socketDesc, BufferAddr, BufferLen, MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *)&sockAddr, &addrlen);
|
2009-02-27 15:05:19 +01:00
|
|
|
if ((len > 0) && (BufferAddr[0] == TS_SYNC_BYTE)) {
|
2009-02-26 15:04:12 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
else if (len > 3) {
|
|
|
|
// http://www.networksorcery.com/enp/rfc/rfc2250.txt
|
|
|
|
// version
|
|
|
|
unsigned int v = (BufferAddr[0] >> 6) & 0x03;
|
|
|
|
// extension bit
|
|
|
|
unsigned int x = (BufferAddr[0] >> 4) & 0x01;
|
|
|
|
// cscr count
|
|
|
|
unsigned int cc = BufferAddr[0] & 0x0F;
|
|
|
|
// payload type: MPEG2 TS = 33
|
|
|
|
//unsigned int pt = readBuffer[1] & 0x7F;
|
|
|
|
// header lenght
|
|
|
|
unsigned int headerlen = (3 + cc) * sizeof(uint32_t);
|
|
|
|
// check if extension
|
|
|
|
if (x) {
|
|
|
|
// extension header length
|
2009-02-27 15:05:19 +01:00
|
|
|
unsigned int ehl = (((BufferAddr[headerlen + 2] & 0xFF) << 8) |
|
|
|
|
(BufferAddr[headerlen + 3] & 0xFF));
|
2009-02-26 15:04:12 +01:00
|
|
|
// update header length
|
|
|
|
headerlen += (ehl + 1) * sizeof(uint32_t);
|
2007-10-21 15:31:21 +02:00
|
|
|
}
|
2009-02-26 15:04:12 +01:00
|
|
|
// Check that rtp is version 2 and payload contains multiple of TS packet data
|
2009-02-27 15:05:19 +01:00
|
|
|
if ((v == 2) && (((len - headerlen) % TS_SIZE) == 0) &&
|
|
|
|
(BufferAddr[headerlen] == TS_SYNC_BYTE)) {
|
2009-02-26 15:04:12 +01:00
|
|
|
// Set argument point to payload in read buffer
|
|
|
|
memmove(BufferAddr, &BufferAddr[headerlen], (len - headerlen));
|
|
|
|
return (len - headerlen);
|
2007-10-21 15:31:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-21 19:32:43 +02:00
|
|
|
|
|
|
|
// TCP socket class
|
|
|
|
cIptvTcpSocket::cIptvTcpSocket()
|
|
|
|
{
|
|
|
|
debug("cIptvTcpSocket::cIptvTcpSocket()\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvTcpSocket::~cIptvTcpSocket()
|
|
|
|
{
|
|
|
|
debug("cIptvTcpSocket::~cIptvTcpSocket()\n");
|
|
|
|
}
|
|
|
|
|
2007-10-21 21:32:14 +02:00
|
|
|
bool cIptvTcpSocket::OpenSocket(const int Port)
|
|
|
|
{
|
|
|
|
debug("cIptvTcpSocket::OpenSocket()\n");
|
|
|
|
return cIptvSocket::OpenSocket(Port, false);
|
|
|
|
}
|
|
|
|
|
2009-02-26 15:04:12 +01:00
|
|
|
int cIptvTcpSocket::Read(unsigned char* BufferAddr, unsigned int BufferLen)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
|
|
|
//debug("cIptvTcpSocket::Read()\n");
|
|
|
|
// Error out if socket not initialized
|
|
|
|
if (socketDesc <= 0) {
|
2008-01-30 22:57:33 +01:00
|
|
|
error("ERROR: Invalid socket in %s\n", __FUNCTION__);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-10-21 19:32:43 +02:00
|
|
|
socklen_t addrlen = sizeof(sockAddr);
|
2009-02-26 15:04:12 +01:00
|
|
|
// Read data from socket
|
|
|
|
if (isActive)
|
|
|
|
return recvfrom(socketDesc, BufferAddr, BufferLen, MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *)&sockAddr, &addrlen);
|
2007-10-21 19:32:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|