2014-03-08 12:07:47 +01:00
|
|
|
/*
|
|
|
|
* socket.c: SAT>IP 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 <net/if.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vdr/device.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "config.h"
|
2014-12-05 22:14:40 +01:00
|
|
|
#include "log.h"
|
2014-03-08 12:07:47 +01:00
|
|
|
#include "socket.h"
|
|
|
|
|
|
|
|
cSatipSocket::cSatipSocket()
|
|
|
|
: socketPortM(0),
|
2014-11-16 09:31:34 +01:00
|
|
|
socketDescM(-1)
|
2014-03-08 12:07:47 +01:00
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-03-08 12:07:47 +01:00
|
|
|
memset(&sockAddrM, 0, sizeof(sockAddrM));
|
|
|
|
}
|
|
|
|
|
|
|
|
cSatipSocket::~cSatipSocket()
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-03-08 12:07:47 +01:00
|
|
|
// Close the socket
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:56:39 +01:00
|
|
|
bool cSatipSocket::Open(const int portP, const bool reuseAddrP)
|
2014-03-08 12:07:47 +01:00
|
|
|
{
|
|
|
|
// Bind to the socket if it is not active already
|
|
|
|
if (socketDescM < 0) {
|
|
|
|
socklen_t len = sizeof(sockAddrM);
|
|
|
|
// Create socket
|
|
|
|
socketDescM = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
ERROR_IF_RET(socketDescM < 0, "socket()", return false);
|
|
|
|
// Make it use non-blocking I/O to avoid stuck read calls
|
|
|
|
ERROR_IF_FUNC(fcntl(socketDescM, F_SETFL, O_NONBLOCK), "fcntl(O_NONBLOCK)",
|
|
|
|
Close(), return false);
|
|
|
|
// Allow multiple sockets to use the same PORT number
|
2016-01-07 20:56:39 +01:00
|
|
|
int yes = reuseAddrP;
|
2014-03-08 12:07:47 +01:00
|
|
|
ERROR_IF_FUNC(setsockopt(socketDescM, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0,
|
|
|
|
"setsockopt(SO_REUSEADDR)", Close(), return false);
|
|
|
|
// Bind socket
|
|
|
|
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);
|
|
|
|
ERROR_IF_FUNC(bind(socketDescM, (struct sockaddr *)&sockAddrM, sizeof(sockAddrM)) < 0,
|
|
|
|
"bind()", Close(), return false);
|
|
|
|
// Update socket port
|
|
|
|
ERROR_IF_FUNC(getsockname(socketDescM,(struct sockaddr*)&sockAddrM, &len) < 0,
|
|
|
|
"getsockname()", Close(), return false);
|
|
|
|
socketPortM = ntohs(sockAddrM.sin_port);
|
|
|
|
}
|
2014-12-07 16:27:53 +01:00
|
|
|
debug1("%s (%d) socketPort=%d", __PRETTY_FUNCTION__, portP, socketPortM);
|
2014-03-08 12:07:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipSocket::Close(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s sockerPort=%d", __PRETTY_FUNCTION__, socketPortM);
|
2014-03-08 12:07:47 +01:00
|
|
|
// Check if socket exists
|
|
|
|
if (socketDescM >= 0) {
|
|
|
|
close(socketDescM);
|
|
|
|
socketDescM = -1;
|
|
|
|
socketPortM = 0;
|
|
|
|
memset(&sockAddrM, 0, sizeof(sockAddrM));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipSocket::Flush(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-03-08 12:07:47 +01:00
|
|
|
if (socketDescM < 0) {
|
|
|
|
const unsigned int len = 65535;
|
|
|
|
unsigned char *buf = MALLOC(unsigned char, len);
|
|
|
|
if (buf) {
|
|
|
|
int i = 0;
|
|
|
|
do {
|
|
|
|
// Sanity check
|
|
|
|
if (++i > 10)
|
|
|
|
break;
|
|
|
|
} while (Read(buf, len));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cSatipSocket::Read(unsigned char *bufferAddrP, unsigned int bufferLenP)
|
|
|
|
{
|
2014-12-14 16:45:55 +01:00
|
|
|
debug16("%s (, %d)", __PRETTY_FUNCTION__, bufferLenP);
|
2014-03-08 12:07:47 +01:00
|
|
|
// Error out if socket not initialized
|
|
|
|
if (socketDescM <= 0) {
|
2014-12-13 22:15:58 +01:00
|
|
|
error("%s Invalid socket", __PRETTY_FUNCTION__);
|
2014-03-08 12:07:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int len = 0;
|
|
|
|
// Read data from socket in a loop
|
|
|
|
do {
|
|
|
|
socklen_t addrlen = sizeof(sockAddrM);
|
|
|
|
struct msghdr msgh;
|
|
|
|
struct iovec iov;
|
|
|
|
char cbuf[256];
|
|
|
|
len = 0;
|
|
|
|
// Initialize iov and msgh structures
|
|
|
|
memset(&msgh, 0, sizeof(struct msghdr));
|
|
|
|
iov.iov_base = bufferAddrP;
|
|
|
|
iov.iov_len = bufferLenP;
|
|
|
|
msgh.msg_control = cbuf;
|
|
|
|
msgh.msg_controllen = sizeof(cbuf);
|
|
|
|
msgh.msg_name = &sockAddrM;
|
|
|
|
msgh.msg_namelen = addrlen;
|
2014-12-13 22:15:58 +01:00
|
|
|
msgh.msg_iov = &iov;
|
2014-03-08 12:07:47 +01:00
|
|
|
msgh.msg_iovlen = 1;
|
|
|
|
msgh.msg_flags = 0;
|
|
|
|
|
|
|
|
if (socketDescM && bufferAddrP && (bufferLenP > 0))
|
|
|
|
len = (int)recvmsg(socketDescM, &msgh, MSG_DONTWAIT);
|
|
|
|
if (len > 0)
|
|
|
|
return len;
|
|
|
|
} while (len > 0);
|
2015-01-24 15:20:13 +01:00
|
|
|
ERROR_IF_RET(len < 0 && errno != EAGAIN && errno != EWOULDBLOCK, "recvmsg()", return -1);
|
2014-03-08 12:07:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:15:58 +01:00
|
|
|
int cSatipSocket::ReadMulti(unsigned char *bufferAddrP, unsigned int *elementRecvSizeP, unsigned int elementCountP, unsigned int elementBufferSizeP)
|
|
|
|
{
|
2014-12-14 16:45:55 +01:00
|
|
|
debug16("%s (, , %d, %d)", __PRETTY_FUNCTION__, elementCountP, elementBufferSizeP);
|
2015-01-22 20:37:33 +01:00
|
|
|
int count = -1;
|
2014-12-13 22:15:58 +01:00
|
|
|
// Error out if socket not initialized
|
|
|
|
if (socketDescM <= 0) {
|
|
|
|
error("%s Invalid socket", __PRETTY_FUNCTION__);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-22 20:37:33 +01:00
|
|
|
if (!bufferAddrP || !elementRecvSizeP || !elementCountP || !elementBufferSizeP) {
|
|
|
|
error("%s Invalid parameter(s)", __PRETTY_FUNCTION__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2,12)
|
2014-12-13 22:15:58 +01:00
|
|
|
// Initialize iov and msgh structures
|
|
|
|
struct mmsghdr mmsgh[elementCountP];
|
|
|
|
struct iovec iov[elementCountP];
|
|
|
|
memset(mmsgh, 0, sizeof(mmsgh[0]) * elementCountP);
|
|
|
|
for (unsigned int i = 0; i < elementCountP; ++i) {
|
|
|
|
iov[i].iov_base = bufferAddrP + i * elementBufferSizeP;
|
|
|
|
iov[i].iov_len = elementBufferSizeP;
|
|
|
|
mmsgh[i].msg_hdr.msg_iov = &iov[i];
|
|
|
|
mmsgh[i].msg_hdr.msg_iovlen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read data from socket as a set
|
2015-01-22 20:37:33 +01:00
|
|
|
count = (int)recvmmsg(socketDescM, mmsgh, elementCountP, MSG_DONTWAIT, NULL);
|
2014-12-13 22:15:58 +01:00
|
|
|
ERROR_IF_RET(count < 0 && errno != EAGAIN && errno != EWOULDBLOCK, "recvmmsg()", return -1);
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
elementRecvSizeP[i] = mmsgh[i].msg_len;
|
2015-01-05 19:24:45 +01:00
|
|
|
#else
|
2015-01-22 20:37:33 +01:00
|
|
|
count = 0;
|
2015-01-05 19:24:45 +01:00
|
|
|
while (count < (int)elementCountP) {
|
|
|
|
int len = Read(bufferAddrP + count * elementBufferSizeP, elementBufferSizeP);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
else if (len == 0)
|
|
|
|
break;
|
|
|
|
elementRecvSizeP[count++] = len;
|
|
|
|
}
|
|
|
|
#endif
|
2014-12-14 16:45:55 +01:00
|
|
|
debug16("%s Received %d packets size[0]=%d", __PRETTY_FUNCTION__, count, elementRecvSizeP[0]);
|
2014-12-13 22:15:58 +01:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
|
|
|
|
bool cSatipSocket::Write(const char *addrP, const unsigned char *bufferAddrP, unsigned int bufferLenP)
|
|
|
|
{
|
2014-12-07 16:27:53 +01:00
|
|
|
debug1("%s (%s, , %d)", __PRETTY_FUNCTION__, addrP, bufferLenP);
|
2014-03-08 12:07:47 +01:00
|
|
|
// Error out if socket not initialized
|
|
|
|
if (socketDescM <= 0) {
|
2014-12-05 22:02:57 +01:00
|
|
|
error("%s Invalid socket", __PRETTY_FUNCTION__);
|
2014-03-08 12:07:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct sockaddr_in sockAddr;
|
|
|
|
memset(&sockAddr, 0, sizeof(sockAddr));
|
|
|
|
sockAddr.sin_family = AF_INET;
|
|
|
|
sockAddr.sin_port = htons((uint16_t)(socketPortM & 0xFFFF));
|
|
|
|
sockAddr.sin_addr.s_addr = inet_addr(addrP);
|
|
|
|
ERROR_IF_RET(sendto(socketDescM, bufferAddrP, bufferLenP, 0, (struct sockaddr *)&sockAddr, sizeof(sockAddr)) < 0, "sendto()", return false);
|
|
|
|
return true;
|
|
|
|
}
|