1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00
vdr-plugin-iptv/protocoludp.c

128 lines
3.4 KiB
C
Raw Normal View History

2007-09-14 17:44:25 +02:00
/*
* protocoludp.c: IPTV plugin for the Video Disk Recorder
2007-09-14 17:44:25 +02:00
*
* 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>
2007-09-14 17:44:25 +02:00
#include "common.h"
2007-09-16 15:38:20 +02:00
#include "config.h"
2007-09-14 17:44:25 +02:00
#include "protocoludp.h"
#include "socket.h"
2007-09-14 17:44:25 +02:00
cIptvProtocolUdp::cIptvProtocolUdp()
: streamAddr(strdup("")),
sourceAddr(strdup(""))
2007-09-14 17:44:25 +02:00
{
debug("cIptvProtocolUdp::cIptvProtocolUdp()\n");
2007-09-14 17:44:25 +02:00
}
cIptvProtocolUdp::~cIptvProtocolUdp()
{
debug("cIptvProtocolUdp::~cIptvProtocolUdp()\n");
// Drop the multicast group and close the socket
2008-01-05 00:36:37 +01:00
cIptvProtocolUdp::Close();
2007-09-14 17:44:25 +02:00
// Free allocated memory
free(streamAddr);
free(sourceAddr);
2007-09-14 17:44:25 +02:00
}
bool cIptvProtocolUdp::JoinMulticast(void)
{
debug("cIptvProtocolUdp::JoinMulticast()\n");
// Check that stream address is valid
if (!isActive && !isempty(streamAddr)) {
2007-09-14 17:44:25 +02:00
// Ensure that socket is valid
OpenSocket(socketPort, isempty(sourceAddr) ? INADDR_ANY : inet_addr(sourceAddr));
2007-09-14 17:44:25 +02:00
// Join a new multicast group
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(streamAddr);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq));
ERROR_IF_RET(err < 0, "setsockopt()", return false);
2007-09-14 17:44:25 +02:00
// Update multicasting flag
isActive = true;
2007-09-14 17:44:25 +02:00
}
return true;
}
bool cIptvProtocolUdp::DropMulticast(void)
{
debug("cIptvProtocolUdp::DropMulticast()\n");
// Check that stream address is valid
if (isActive && !isempty(streamAddr)) {
2007-09-14 17:44:25 +02:00
// Ensure that socket is valid
OpenSocket(socketPort, isempty(sourceAddr) ? INADDR_ANY : inet_addr(sourceAddr));
2007-09-14 17:44:25 +02:00
// Drop the multicast group
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(streamAddr);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
sizeof(mreq));
ERROR_IF_RET(err < 0, "setsockopt()", return false);
2007-09-14 17:44:25 +02:00
// Update multicasting flag
isActive = false;
2007-09-14 17:44:25 +02:00
}
return true;
}
bool cIptvProtocolUdp::Open(void)
{
2007-10-19 23:36:27 +02:00
debug("cIptvProtocolUdp::Open()\n");
2007-09-14 17:44:25 +02:00
// Join a new multicast group
JoinMulticast();
return true;
}
bool cIptvProtocolUdp::Close(void)
{
2007-10-19 23:36:27 +02:00
debug("cIptvProtocolUdp::Close()\n");
2007-09-14 17:44:25 +02:00
// Drop the multicast group
DropMulticast();
// Close the socket
CloseSocket();
return true;
}
2009-02-26 15:04:12 +01:00
int cIptvProtocolUdp::Read(unsigned char* BufferAddr, unsigned int BufferLen)
{
2009-02-26 15:04:12 +01:00
return cIptvUdpSocket::Read(BufferAddr, BufferLen);
}
bool cIptvProtocolUdp::Set(const char* Location, const int Parameter, const int Index)
2007-09-14 17:44:25 +02:00
{
debug("cIptvProtocolUdp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
2007-10-19 23:36:27 +02:00
if (!isempty(Location)) {
// Drop the multicast group
DropMulticast();
// Update stream address and port
streamAddr = strcpyrealloc(streamAddr, Location);
char *p = strstr(streamAddr, ";");
if (p) {
sourceAddr = strcpyrealloc(sourceAddr, p + 1);
*p = 0;
}
else
sourceAddr = strcpyrealloc(sourceAddr, "");
socketPort = Parameter;
// Join a new multicast group
JoinMulticast();
}
2007-09-14 17:44:25 +02:00
return true;
}
2007-10-08 00:54:09 +02:00
cString cIptvProtocolUdp::GetInformation(void)
{
//debug("cIptvProtocolUdp::GetInformation()");
return cString::sprintf("udp://%s:%d", streamAddr, socketPort);
2007-10-08 00:54:09 +02:00
}