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>
|
2012-09-30 17:42:40 +02:00
|
|
|
#include <net/if.h>
|
2007-10-21 15:31:21 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vdr/device.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "socket.h"
|
|
|
|
|
|
|
|
cIptvSocket::cIptvSocket()
|
2013-02-23 16:12:46 +01:00
|
|
|
: socketPortM(0),
|
|
|
|
socketDescM(-1),
|
|
|
|
isActiveM(false)
|
2007-10-21 15:31:21 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvSocket::%s()", __FUNCTION__);
|
|
|
|
memset(&sockAddrM, 0, sizeof(sockAddrM));
|
2007-10-21 15:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvSocket::~cIptvSocket()
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvSocket::%s()", __FUNCTION__);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvSocket::OpenSocket(const int portP, const bool isUdpP)
|
2007-10-21 15:31:21 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvSocket::%s()", __FUNCTION__);
|
2007-10-21 15:31:21 +02:00
|
|
|
// If socket is there already and it is bound to a different port, it must
|
|
|
|
// be closed first
|
2013-02-23 16:12:46 +01:00
|
|
|
if (portP != socketPortM) {
|
|
|
|
debug("cIptvSocket::%s(): socket tear-down", __FUNCTION__);
|
2007-10-21 15:31:21 +02:00
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
// Bind to the socket if it is not active already
|
2013-02-23 16:12:46 +01:00
|
|
|
if (socketDescM < 0) {
|
2009-02-26 15:04:12 +01:00
|
|
|
int yes = 1;
|
2007-10-21 15:31:21 +02:00
|
|
|
// Create socket
|
2013-02-23 16:12:46 +01:00
|
|
|
if (isUdpP)
|
|
|
|
socketDescM = socket(PF_INET, SOCK_DGRAM, 0);
|
2007-10-21 15:31:21 +02:00
|
|
|
else
|
2013-02-23 16:12:46 +01:00
|
|
|
socketDescM = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
ERROR_IF_RET(socketDescM < 0, "socket()", return false);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Make it use non-blocking I/O to avoid stuck read calls
|
2013-02-23 16:12:46 +01:00
|
|
|
ERROR_IF_FUNC(fcntl(socketDescM, F_SETFL, O_NONBLOCK), "fcntl(O_NONBLOCK)",
|
2009-02-27 15:05:19 +01:00
|
|
|
CloseSocket(), return false);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Allow multiple sockets to use the same PORT number
|
2013-03-01 11:22:41 +01:00
|
|
|
ERROR_IF_FUNC(setsockopt(socketDescM, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0,
|
|
|
|
"setsockopt(SO_REUSEADDR)", CloseSocket(), return false);
|
2012-07-10 21:12:29 +02:00
|
|
|
#ifndef __FreeBSD__
|
2010-09-12 11:54:14 +02:00
|
|
|
// Allow packet information to be fetched
|
2013-03-01 11:22:41 +01:00
|
|
|
ERROR_IF_FUNC(setsockopt(socketDescM, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0,
|
|
|
|
"setsockopt(IP_PKTINFO)", CloseSocket(), return false);
|
2012-07-10 21:12:29 +02:00
|
|
|
#endif // __FreeBSD__
|
2007-10-21 15:31:21 +02:00
|
|
|
// Bind socket
|
2013-02-23 16:12:46 +01:00
|
|
|
memset(&sockAddrM, 0, sizeof(sockAddrM));
|
|
|
|
sockAddrM.sin_family = AF_INET;
|
|
|
|
sockAddrM.sin_port = htons((uint16_t)(portP & 0xFFFF));
|
|
|
|
sockAddrM.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (isUdpP)
|
2013-03-01 11:22:41 +01:00
|
|
|
ERROR_IF_FUNC(bind(socketDescM, (struct sockaddr *)&sockAddrM, sizeof(sockAddrM)) < 0,
|
|
|
|
"bind()", CloseSocket(), return false);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Update socket port
|
2013-02-23 16:12:46 +01:00
|
|
|
socketPortM = portP;
|
2007-10-21 15:31:21 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvSocket::CloseSocket(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvSocket::%s()", __FUNCTION__);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Check if socket exists
|
2013-02-23 16:12:46 +01:00
|
|
|
if (socketDescM >= 0) {
|
|
|
|
close(socketDescM);
|
|
|
|
socketDescM = -1;
|
|
|
|
socketPortM = 0;
|
|
|
|
memset(&sockAddrM, 0, sizeof(sockAddrM));
|
2007-10-21 15:31:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvSocket::CheckAddress(const char *addrP, in_addr_t *inAddrP)
|
2012-09-30 17:42:40 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
if (inAddrP) {
|
2012-09-30 17:42:40 +02:00
|
|
|
// First try only the IP address
|
2013-03-17 00:18:58 +01:00
|
|
|
*inAddrP = inet_addr(addrP);
|
2013-02-23 16:12:46 +01:00
|
|
|
if (*inAddrP == htonl(INADDR_NONE)) {
|
|
|
|
debug("cIptvSocket::%s(%s): cannot convert to address", __FUNCTION__, addrP);
|
2012-09-30 17:42:40 +02:00
|
|
|
// It may be a host name, get the name
|
2013-02-23 16:12:46 +01:00
|
|
|
struct hostent *host = gethostbyname(addrP);
|
2012-09-30 17:42:40 +02:00
|
|
|
if (!host) {
|
|
|
|
char tmp[64];
|
2013-03-01 11:22:41 +01:00
|
|
|
error("gethostbyname() failed: %s is not valid address: %s", addrP,
|
|
|
|
strerror_r(h_errno, tmp, sizeof(tmp)));
|
2012-09-30 17:42:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-17 00:18:58 +01:00
|
|
|
*inAddrP = inet_addr(*host->h_addr_list);
|
2012-09-30 17:42:40 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-21 19:32:43 +02:00
|
|
|
// UDP socket class
|
|
|
|
cIptvUdpSocket::cIptvUdpSocket()
|
2013-02-23 16:12:46 +01:00
|
|
|
: streamAddrM(htonl(INADDR_ANY)),
|
|
|
|
sourceAddrM(htonl(INADDR_ANY)),
|
|
|
|
useIGMPv3M(false)
|
2007-10-21 15:31:21 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvUdpSocket::~cIptvUdpSocket()
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvUdpSocket::OpenSocket(const int portP)
|
2012-09-30 17:42:40 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
|
|
|
streamAddrM = htonl(INADDR_ANY);
|
|
|
|
sourceAddrM = htonl(INADDR_ANY);
|
|
|
|
useIGMPv3M = false;
|
|
|
|
return cIptvSocket::OpenSocket(portP, true);
|
2012-09-30 17:42:40 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvUdpSocket::OpenSocket(const int portP, const char *streamAddrP, const char *sourceAddrP, bool useIGMPv3P)
|
2007-10-21 21:32:14 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s(%d, %s, %s, %d)", __FUNCTION__, portP, streamAddrP, sourceAddrP, useIGMPv3P);
|
|
|
|
CheckAddress(streamAddrP, &streamAddrM);
|
|
|
|
CheckAddress(sourceAddrP, &sourceAddrM);
|
|
|
|
useIGMPv3M = useIGMPv3P;
|
|
|
|
return cIptvSocket::OpenSocket(portP, true);
|
2010-09-15 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvUdpSocket::CloseSocket(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
|
|
|
streamAddrM = htonl(INADDR_ANY);
|
|
|
|
sourceAddrM = htonl(INADDR_ANY);
|
|
|
|
useIGMPv3M = false;
|
2010-09-15 16:37:20 +02:00
|
|
|
cIptvSocket::CloseSocket();
|
2007-10-21 21:32:14 +02:00
|
|
|
}
|
|
|
|
|
2012-04-01 23:35:32 +02:00
|
|
|
bool cIptvUdpSocket::JoinMulticast(void)
|
2010-09-15 17:18:46 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
2010-09-15 17:18:46 +02:00
|
|
|
// Check if socket exists
|
2013-02-23 16:12:46 +01:00
|
|
|
if (!isActiveM && (socketDescM >= 0)) {
|
2010-09-15 17:18:46 +02:00
|
|
|
// Join a new multicast group
|
2013-02-23 16:12:46 +01:00
|
|
|
if (useIGMPv3M) {
|
2012-09-30 17:42:40 +02:00
|
|
|
// Source-specific multicast (SSM) is used
|
|
|
|
struct group_source_req gsr;
|
|
|
|
struct sockaddr_in *grp;
|
|
|
|
struct sockaddr_in *src;
|
|
|
|
gsr.gsr_interface = 0; // if_nametoindex("any") ?
|
|
|
|
grp = (struct sockaddr_in*)&gsr.gsr_group;
|
|
|
|
grp->sin_family = AF_INET;
|
2013-02-23 16:12:46 +01:00
|
|
|
grp->sin_addr.s_addr = streamAddrM;
|
2012-09-30 17:42:40 +02:00
|
|
|
grp->sin_port = 0;
|
|
|
|
src = (struct sockaddr_in*)&gsr.gsr_source;
|
|
|
|
src->sin_family = AF_INET;
|
2013-02-23 16:12:46 +01:00
|
|
|
src->sin_addr.s_addr = sourceAddrM;
|
2012-09-30 17:42:40 +02:00
|
|
|
src->sin_port = 0;
|
2013-02-23 16:12:46 +01:00
|
|
|
ERROR_IF_RET(setsockopt(socketDescM, SOL_IP, MCAST_JOIN_SOURCE_GROUP, &gsr, sizeof(gsr)) < 0, "setsockopt(MCAST_JOIN_SOURCE_GROUP)", return false);
|
2012-09-30 17:42:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct ip_mreq mreq;
|
2013-02-23 16:12:46 +01:00
|
|
|
mreq.imr_multiaddr.s_addr = streamAddrM;
|
2012-09-30 17:42:40 +02:00
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
2013-02-23 16:12:46 +01:00
|
|
|
ERROR_IF_RET(setsockopt(socketDescM, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0, "setsockopt(IP_ADD_MEMBERSHIP)", return false);
|
2012-09-30 17:42:40 +02:00
|
|
|
}
|
2010-09-15 17:18:46 +02:00
|
|
|
// Update multicasting flag
|
2013-02-23 16:12:46 +01:00
|
|
|
isActiveM = true;
|
2010-09-15 17:18:46 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-01 23:35:32 +02:00
|
|
|
bool cIptvUdpSocket::DropMulticast(void)
|
2010-09-15 17:18:46 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
2010-09-15 17:18:46 +02:00
|
|
|
// Check if socket exists
|
2013-02-23 16:12:46 +01:00
|
|
|
if (isActiveM && (socketDescM >= 0)) {
|
2010-09-15 17:18:46 +02:00
|
|
|
// Drop the existing multicast group
|
2013-02-23 16:12:46 +01:00
|
|
|
if (useIGMPv3M) {
|
2012-09-30 17:42:40 +02:00
|
|
|
// Source-specific multicast (SSM) is used
|
|
|
|
struct group_source_req gsr;
|
|
|
|
struct sockaddr_in *grp;
|
|
|
|
struct sockaddr_in *src;
|
|
|
|
gsr.gsr_interface = 0; // if_nametoindex("any") ?
|
|
|
|
grp = (struct sockaddr_in*)&gsr.gsr_group;
|
|
|
|
grp->sin_family = AF_INET;
|
2013-02-23 16:12:46 +01:00
|
|
|
grp->sin_addr.s_addr = streamAddrM;
|
2012-09-30 17:42:40 +02:00
|
|
|
grp->sin_port = 0;
|
|
|
|
src = (struct sockaddr_in*)&gsr.gsr_source;
|
|
|
|
src->sin_family = AF_INET;
|
2013-02-23 16:12:46 +01:00
|
|
|
src->sin_addr.s_addr = sourceAddrM;
|
2012-09-30 17:42:40 +02:00
|
|
|
src->sin_port = 0;
|
2013-02-23 16:12:46 +01:00
|
|
|
ERROR_IF_RET(setsockopt(socketDescM, SOL_IP, MCAST_LEAVE_SOURCE_GROUP, &gsr, sizeof(gsr)) < 0, "setsockopt(MCAST_LEAVE_SOURCE_GROUP)", return false);
|
2012-09-30 17:42:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct ip_mreq mreq;
|
2013-02-23 16:12:46 +01:00
|
|
|
mreq.imr_multiaddr.s_addr = streamAddrM;
|
2012-09-30 17:42:40 +02:00
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
2013-02-23 16:12:46 +01:00
|
|
|
ERROR_IF_RET(setsockopt(socketDescM, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0, "setsockopt(IP_DROP_MEMBERSHIP)", return false);
|
2012-09-30 17:42:40 +02:00
|
|
|
}
|
2010-09-15 17:18:46 +02:00
|
|
|
// Update multicasting flag
|
2013-02-23 16:12:46 +01:00
|
|
|
isActiveM = false;
|
2010-09-15 17:18:46 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
int cIptvUdpSocket::Read(unsigned char *bufferAddrP, unsigned int bufferLenP)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
//debug("cIptvUdpSocket::%s()", __FUNCTION__);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Error out if socket not initialized
|
2013-02-23 16:12:46 +01:00
|
|
|
if (socketDescM <= 0) {
|
2012-10-25 20:46:05 +02:00
|
|
|
error("Invalid socket in cIptvUdpSocket::%s()\n", __FUNCTION__);
|
2008-01-30 22:57:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-02-26 15:04:12 +01:00
|
|
|
int len = 0;
|
2012-06-02 13:24:29 +02:00
|
|
|
// Read data from socket in a loop
|
|
|
|
do {
|
2013-02-23 16:12:46 +01:00
|
|
|
socklen_t addrlen = sizeof(sockAddrM);
|
2012-06-02 13:24:29 +02:00
|
|
|
struct msghdr msgh;
|
|
|
|
struct iovec iov;
|
|
|
|
char cbuf[256];
|
|
|
|
len = 0;
|
|
|
|
// Initialize iov and msgh structures
|
|
|
|
memset(&msgh, 0, sizeof(struct msghdr));
|
2013-02-23 16:12:46 +01:00
|
|
|
iov.iov_base = bufferAddrP;
|
|
|
|
iov.iov_len = bufferLenP;
|
2012-06-02 13:24:29 +02:00
|
|
|
msgh.msg_control = cbuf;
|
|
|
|
msgh.msg_controllen = sizeof(cbuf);
|
2013-02-23 16:12:46 +01:00
|
|
|
msgh.msg_name = &sockAddrM;
|
2012-06-02 13:24:29 +02:00
|
|
|
msgh.msg_namelen = addrlen;
|
|
|
|
msgh.msg_iov = &iov;
|
|
|
|
msgh.msg_iovlen = 1;
|
|
|
|
msgh.msg_flags = 0;
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
if (isActiveM && socketDescM && bufferAddrP && (bufferLenP > 0))
|
|
|
|
len = (int)recvmsg(socketDescM, &msgh, MSG_DONTWAIT);
|
2012-06-02 13:24:29 +02:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (len > 0) {
|
2012-07-10 21:12:29 +02:00
|
|
|
#ifndef __FreeBSD__
|
2012-06-02 13:24:29 +02:00
|
|
|
// Process auxiliary received data and validate source address
|
2012-09-30 17:42:40 +02:00
|
|
|
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
|
2012-06-02 13:24:29 +02:00
|
|
|
if ((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
|
|
|
|
struct in_pktinfo *i = (struct in_pktinfo *)CMSG_DATA(cmsg);
|
2013-02-23 16:12:46 +01:00
|
|
|
if ((i->ipi_addr.s_addr == streamAddrM) || (htonl(INADDR_ANY) == streamAddrM)) {
|
2012-07-10 21:12:29 +02:00
|
|
|
#endif // __FreeBSD__
|
2013-02-23 16:12:46 +01:00
|
|
|
if (bufferAddrP[0] == TS_SYNC_BYTE)
|
2012-06-02 13:24:29 +02:00
|
|
|
return len;
|
|
|
|
else if (len > 3) {
|
2013-02-23 01:03:27 +01:00
|
|
|
// http://tools.ietf.org/html/rfc3550
|
|
|
|
// http://tools.ietf.org/html/rfc2250
|
2013-02-25 20:08:08 +01:00
|
|
|
// Version
|
2013-02-23 16:12:46 +01:00
|
|
|
unsigned int v = (bufferAddrP[0] >> 6) & 0x03;
|
2013-02-25 20:08:08 +01:00
|
|
|
// Extension bit
|
2013-02-23 16:12:46 +01:00
|
|
|
unsigned int x = (bufferAddrP[0] >> 4) & 0x01;
|
2013-02-25 20:08:08 +01:00
|
|
|
// CSCR count
|
2013-02-23 16:12:46 +01:00
|
|
|
unsigned int cc = bufferAddrP[0] & 0x0F;
|
2013-02-25 20:08:08 +01:00
|
|
|
// Payload type: MPEG2 TS = 33
|
2013-02-23 16:12:46 +01:00
|
|
|
//unsigned int pt = bufferAddrP[1] & 0x7F;
|
2013-02-25 20:08:08 +01:00
|
|
|
// Header lenght
|
2012-06-02 13:24:29 +02:00
|
|
|
unsigned int headerlen = (3 + cc) * (unsigned int)sizeof(uint32_t);
|
2013-02-25 20:08:08 +01:00
|
|
|
// Check if extension
|
2012-06-02 13:24:29 +02:00
|
|
|
if (x) {
|
2013-02-25 20:08:08 +01:00
|
|
|
// Extension header length
|
2013-02-23 16:12:46 +01:00
|
|
|
unsigned int ehl = (((bufferAddrP[headerlen + 2] & 0xFF) << 8) |
|
|
|
|
(bufferAddrP[headerlen + 3] & 0xFF));
|
2013-02-25 20:08:08 +01:00
|
|
|
// Update header length
|
2012-06-02 13:24:29 +02:00
|
|
|
headerlen += (ehl + 1) * (unsigned int)sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
// Check that rtp is version 2 and payload contains multiple of TS packet data
|
|
|
|
if ((v == 2) && (((len - headerlen) % TS_SIZE) == 0) &&
|
2013-02-23 16:12:46 +01:00
|
|
|
(bufferAddrP[headerlen] == TS_SYNC_BYTE)) {
|
2012-06-02 13:24:29 +02:00
|
|
|
// Set argument point to payload in read buffer
|
2013-02-23 16:12:46 +01:00
|
|
|
memmove(bufferAddrP, &bufferAddrP[headerlen], (len - headerlen));
|
2012-06-02 13:24:29 +02:00
|
|
|
return (len - headerlen);
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 21:12:29 +02:00
|
|
|
#ifndef __FreeBSD__
|
2012-06-02 13:24:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 21:12:29 +02:00
|
|
|
#endif // __FreeBSD__
|
2012-06-02 13:24:29 +02:00
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
ERROR_IF_RET(len < 0 && errno != EAGAIN, "recvmsg()", return -1);
|
|
|
|
|
2007-10-21 15:31:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-10-21 19:32:43 +02:00
|
|
|
|
|
|
|
// TCP socket class
|
|
|
|
cIptvTcpSocket::cIptvTcpSocket()
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvTcpSocket::~cIptvTcpSocket()
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvTcpSocket::OpenSocket(const int portP, const char *streamAddrP)
|
2007-10-21 21:32:14 +02:00
|
|
|
{
|
2013-02-23 16:19:22 +01:00
|
|
|
debug("cIptvTcpSocket::%s(%d, %s)", __FUNCTION__, portP, streamAddrP);
|
2012-04-26 20:32:20 +02:00
|
|
|
// Socket must be opened before setting the host address
|
2013-02-23 16:12:46 +01:00
|
|
|
return (cIptvSocket::OpenSocket(portP, false) && CheckAddress(streamAddrP, &sockAddrM.sin_addr.s_addr));
|
2007-10-21 21:32:14 +02:00
|
|
|
}
|
|
|
|
|
2010-09-15 22:41:25 +02:00
|
|
|
void cIptvTcpSocket::CloseSocket(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
2010-09-15 22:41:25 +02:00
|
|
|
cIptvSocket::CloseSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvTcpSocket::ConnectSocket(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
|
|
|
if (!isActiveM && (socketDescM >= 0)) {
|
|
|
|
int retval = connect(socketDescM, (struct sockaddr *)&sockAddrM, sizeof(sockAddrM));
|
2010-09-15 22:41:25 +02:00
|
|
|
// Non-blocking sockets always report in-progress error when connected
|
|
|
|
ERROR_IF_RET(retval < 0 && errno != EINPROGRESS, "connect()", return false);
|
|
|
|
// Select with 800ms timeout on the socket completion, check if it is writable
|
2013-02-23 16:12:46 +01:00
|
|
|
retval = select_single_desc(socketDescM, 800000, true);
|
2010-09-15 22:41:25 +02:00
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
// Select has returned. Get socket errors if there are any
|
|
|
|
retval = 0;
|
|
|
|
socklen_t len = sizeof(retval);
|
2013-02-23 16:12:46 +01:00
|
|
|
getsockopt(socketDescM, SOL_SOCKET, SO_ERROR, &retval, &len);
|
2010-09-15 22:41:25 +02:00
|
|
|
// If not any errors, then socket must be ready and connected
|
|
|
|
if (retval != 0) {
|
|
|
|
char tmp[64];
|
|
|
|
error("Connect() failed: %s", strerror_r(retval, tmp, sizeof(tmp)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
int cIptvTcpSocket::Read(unsigned char *bufferAddrP, unsigned int bufferLenP)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
//debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
2007-10-21 19:32:43 +02:00
|
|
|
// Error out if socket not initialized
|
2013-02-23 16:12:46 +01:00
|
|
|
if (socketDescM <= 0) {
|
2012-10-25 20:46:05 +02:00
|
|
|
error("Invalid socket in cIptvTcpSocket::%s()\n", __FUNCTION__);
|
2008-01-30 22:57:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-07-02 17:28:49 +02:00
|
|
|
int len = 0;
|
2013-02-23 16:12:46 +01:00
|
|
|
socklen_t addrlen = sizeof(sockAddrM);
|
2009-02-26 15:04:12 +01:00
|
|
|
// Read data from socket
|
2013-02-23 16:12:46 +01:00
|
|
|
if (isActiveM && socketDescM && bufferAddrP && (bufferLenP > 0))
|
|
|
|
len = (int)recvfrom(socketDescM, bufferAddrP, bufferLenP, MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *)&sockAddrM, &addrlen);
|
2010-07-02 17:28:49 +02:00
|
|
|
return len;
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
2010-09-15 22:41:25 +02:00
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvTcpSocket::ReadChar(char *bufferAddrP, unsigned int timeoutMsP)
|
2010-09-15 22:41:25 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
//debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
2010-09-15 22:41:25 +02:00
|
|
|
// Error out if socket not initialized
|
2013-02-23 16:12:46 +01:00
|
|
|
if (socketDescM <= 0) {
|
2012-10-25 20:46:05 +02:00
|
|
|
error("Invalid socket in cIptvTcpSocket::%s()\n", __FUNCTION__);
|
2010-09-15 22:41:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-23 16:12:46 +01:00
|
|
|
socklen_t addrlen = sizeof(sockAddrM);
|
2010-09-15 22:41:25 +02:00
|
|
|
// Wait 500ms for data
|
2013-02-23 16:12:46 +01:00
|
|
|
int retval = select_single_desc(socketDescM, 1000 * timeoutMsP, false);
|
2010-09-15 22:41:25 +02:00
|
|
|
// Check if error
|
|
|
|
if (retval < 0)
|
|
|
|
return false;
|
|
|
|
// Check if data available
|
|
|
|
else if (retval) {
|
2013-02-23 16:12:46 +01:00
|
|
|
retval = (int)recvfrom(socketDescM, bufferAddrP, 1, MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *)&sockAddrM, &addrlen);
|
2010-09-15 22:41:25 +02:00
|
|
|
if (retval <= 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-23 16:12:46 +01:00
|
|
|
bool cIptvTcpSocket::Write(const char *bufferAddrP, unsigned int bufferLenP)
|
2010-09-15 22:41:25 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
//debug("cIptvTcpSocket::%s()", __FUNCTION__);
|
2010-09-15 22:41:25 +02:00
|
|
|
// Error out if socket not initialized
|
2013-02-23 16:12:46 +01:00
|
|
|
if (socketDescM <= 0) {
|
2012-10-25 20:46:05 +02:00
|
|
|
error("Invalid socket in cIptvTcpSocket::%s()\n", __FUNCTION__);
|
2010-09-15 22:41:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-23 16:12:46 +01:00
|
|
|
ERROR_IF_RET(send(socketDescM, bufferAddrP, bufferLenP, 0) < 0, "send()", return false);
|
2010-09-15 22:41:25 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|