2007-09-16 11:38:00 +02:00
|
|
|
/*
|
|
|
|
* protocolhttp.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>
|
2007-09-19 19:14:03 +02:00
|
|
|
#include <string.h>
|
2007-09-16 11:38:00 +02:00
|
|
|
|
|
|
|
#include <vdr/device.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2007-09-16 15:38:20 +02:00
|
|
|
#include "config.h"
|
2007-09-16 11:38:00 +02:00
|
|
|
#include "protocolhttp.h"
|
|
|
|
|
|
|
|
cIptvProtocolHttp::cIptvProtocolHttp()
|
|
|
|
{
|
2007-09-28 18:44:59 +02:00
|
|
|
debug("cIptvProtocolHttp::cIptvProtocolHttp()\n");
|
2007-09-16 11:38:00 +02:00
|
|
|
streamAddr = strdup("");
|
2007-09-19 19:14:03 +02:00
|
|
|
streamPath = strdup("/");
|
2007-09-16 11:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvProtocolHttp::~cIptvProtocolHttp()
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolHttp::~cIptvProtocolHttp()\n");
|
|
|
|
// Close the socket
|
2008-01-05 00:36:37 +01:00
|
|
|
cIptvProtocolHttp::Close();
|
2007-09-16 11:38:00 +02:00
|
|
|
// Free allocated memory
|
2007-09-19 19:14:03 +02:00
|
|
|
free(streamPath);
|
2007-09-16 11:38:00 +02:00
|
|
|
free(streamAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::Connect(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolHttp::Connect()\n");
|
|
|
|
// Check that stream address is valid
|
2007-09-26 21:49:35 +02:00
|
|
|
if (!isActive && !isempty(streamAddr) && !isempty(streamPath)) {
|
2007-09-16 11:38:00 +02:00
|
|
|
// Ensure that socket is valid
|
2007-10-21 21:32:14 +02:00
|
|
|
OpenSocket(socketPort);
|
2007-09-16 11:38:00 +02:00
|
|
|
|
2007-09-19 19:14:03 +02:00
|
|
|
// First try only the IP address
|
2007-09-16 11:38:00 +02:00
|
|
|
sockAddr.sin_addr.s_addr = inet_addr(streamAddr);
|
2009-02-26 15:04:12 +01:00
|
|
|
|
2007-09-16 11:38:00 +02:00
|
|
|
if (sockAddr.sin_addr.s_addr == INADDR_NONE) {
|
|
|
|
debug("Cannot convert %s directly to internet address\n", streamAddr);
|
|
|
|
|
|
|
|
// It may be a host name, get the name
|
|
|
|
struct hostent *host;
|
|
|
|
host = gethostbyname(streamAddr);
|
|
|
|
if (!host) {
|
|
|
|
error("%s is not valid address\n", streamAddr);
|
|
|
|
char tmp[64];
|
|
|
|
error("ERROR: %s", strerror_r(h_errno, tmp, sizeof(tmp)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockAddr.sin_addr.s_addr = inet_addr(*host->h_addr_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = connect(socketDesc, (struct sockaddr*)&sockAddr,
|
|
|
|
sizeof(sockAddr));
|
|
|
|
// Non-blocking sockets always report in-progress error when connected
|
2007-10-21 01:16:28 +02:00
|
|
|
ERROR_IF_FUNC(err < 0 && errno != EINPROGRESS, "connect()", CloseSocket(), return false);
|
2007-10-21 21:46:03 +02:00
|
|
|
// Select with 800ms timeout on the socket completion, check if it is writable
|
2007-10-20 22:43:22 +02:00
|
|
|
int retval = select_single_desc(socketDesc, 800000, true);
|
2007-10-20 22:27:58 +02:00
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
|
2007-09-16 11:38:00 +02:00
|
|
|
// Select has returned. Get socket errors if there are any
|
|
|
|
int socketStatus = 0;
|
|
|
|
socklen_t len = sizeof(socketStatus);
|
|
|
|
getsockopt(socketDesc, SOL_SOCKET, SO_ERROR, &socketStatus, &len);
|
|
|
|
|
|
|
|
// If not any errors, then socket must be ready and connected
|
|
|
|
if (socketStatus != 0) {
|
|
|
|
error("Cannot connect to %s\n", streamAddr);
|
|
|
|
char tmp[64];
|
|
|
|
error("ERROR: %s", strerror_r(socketStatus, tmp, sizeof(tmp)));
|
2007-09-29 13:17:57 +02:00
|
|
|
CloseSocket();
|
2007-09-16 11:38:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-09-19 19:14:03 +02:00
|
|
|
// Formulate and send HTTP request
|
2008-02-17 20:18:47 +01:00
|
|
|
cString buffer = cString::sprintf("GET %s HTTP/1.1\r\n"
|
|
|
|
"Host: %s\r\n"
|
|
|
|
"User-Agent: vdr-iptv\r\n"
|
|
|
|
"Range: bytes=0-\r\n"
|
|
|
|
"Connection: Close\r\n"
|
|
|
|
"\r\n", streamPath, streamAddr);
|
|
|
|
|
|
|
|
debug("Sending http request: %s\n", *buffer);
|
2007-09-16 11:38:00 +02:00
|
|
|
err = send(socketDesc, buffer, strlen(buffer), 0);
|
2007-10-21 01:16:28 +02:00
|
|
|
ERROR_IF_FUNC(err < 0, "send()", CloseSocket(), return false);
|
2007-09-16 11:38:00 +02:00
|
|
|
|
2007-09-29 12:55:14 +02:00
|
|
|
// Now process headers
|
2007-09-29 13:17:57 +02:00
|
|
|
if (!ProcessHeaders()) {
|
|
|
|
CloseSocket();
|
|
|
|
return false;
|
2007-09-29 18:21:04 +02:00
|
|
|
}
|
2007-09-16 11:38:00 +02:00
|
|
|
|
|
|
|
// Update active flag
|
2007-09-26 21:49:35 +02:00
|
|
|
isActive = true;
|
2007-09-16 11:38:00 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::Disconnect(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolHttp::Disconnect()\n");
|
|
|
|
// Check that stream address is valid
|
2007-09-26 21:49:35 +02:00
|
|
|
if (isActive) {
|
2008-01-30 22:57:33 +01:00
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
|
|
|
// Update active flag
|
|
|
|
isActive = false;
|
2007-09-16 11:38:00 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-29 12:55:14 +02:00
|
|
|
bool cIptvProtocolHttp::GetHeaderLine(char* dest, unsigned int destLen,
|
|
|
|
unsigned int &recvLen)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolHttp::GetHeaderLine()\n");
|
|
|
|
bool linefeed = false;
|
|
|
|
bool newline = false;
|
|
|
|
char buf[256];
|
|
|
|
char *bufptr = buf;
|
|
|
|
memset(buf, '\0', sizeof(buf));
|
|
|
|
recvLen = 0;
|
|
|
|
|
2007-09-29 18:21:04 +02:00
|
|
|
while (!newline || !linefeed) {
|
2007-09-29 12:55:14 +02:00
|
|
|
socklen_t addrlen = sizeof(sockAddr);
|
2007-10-21 21:46:03 +02:00
|
|
|
// Wait 500ms for data
|
2007-10-20 22:43:22 +02:00
|
|
|
int retval = select_single_desc(socketDesc, 500000, false);
|
2007-09-29 12:55:14 +02:00
|
|
|
// Check if error
|
2007-10-20 22:27:58 +02:00
|
|
|
if (retval < 0)
|
2007-09-29 12:55:14 +02:00
|
|
|
return false;
|
|
|
|
// Check if data available
|
|
|
|
else if (retval) {
|
|
|
|
int retval = recvfrom(socketDesc, bufptr, 1, MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *)&sockAddr, &addrlen);
|
|
|
|
if (retval <= 0)
|
|
|
|
return false;
|
|
|
|
// Parsing end conditions, if line ends with \r\n
|
2007-09-29 18:21:04 +02:00
|
|
|
if (linefeed && *bufptr == '\n')
|
2007-09-29 12:55:14 +02:00
|
|
|
newline = true;
|
|
|
|
// First occurrence of \r seen
|
2007-09-29 18:21:04 +02:00
|
|
|
else if (*bufptr == '\r')
|
2007-09-29 12:55:14 +02:00
|
|
|
linefeed = true;
|
|
|
|
// Saw just data or \r without \n
|
|
|
|
else {
|
|
|
|
linefeed = false;
|
|
|
|
++recvLen;
|
|
|
|
}
|
|
|
|
++bufptr;
|
|
|
|
// Check that buffers won't be exceeded
|
|
|
|
if (recvLen >= sizeof(buf) || recvLen >= destLen) {
|
|
|
|
error("Header wouldn't fit into buffer\n");
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-29 18:21:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2007-09-29 12:55:14 +02:00
|
|
|
error("No HTTP response received\n");
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-29 18:21:04 +02:00
|
|
|
}
|
2007-09-29 12:55:14 +02:00
|
|
|
memcpy(dest, buf, recvLen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::ProcessHeaders(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolHttp::ProcessHeaders()\n");
|
|
|
|
unsigned int lineLength = 0;
|
|
|
|
int response = 0;
|
|
|
|
bool responseFound = false;
|
|
|
|
char buf[256];
|
|
|
|
|
2007-09-29 18:21:04 +02:00
|
|
|
while (!responseFound || lineLength != 0) {
|
|
|
|
memset(buf, '\0', sizeof(buf));
|
|
|
|
if (!GetHeaderLine(buf, sizeof(buf), lineLength))
|
|
|
|
return false;
|
|
|
|
if (!responseFound && sscanf(buf, "HTTP/1.%*i %i ",&response) != 1) {
|
|
|
|
error("Expected HTTP header not found\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
responseFound = true;
|
|
|
|
if (response != 200) {
|
|
|
|
error("ERROR: %s\n", buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-09-29 12:55:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-16 11:38:00 +02:00
|
|
|
bool cIptvProtocolHttp::Open(void)
|
|
|
|
{
|
2007-10-19 23:36:27 +02:00
|
|
|
debug("cIptvProtocolHttp::Open()\n");
|
2007-09-16 11:38:00 +02:00
|
|
|
// Connect the socket
|
|
|
|
return Connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::Close(void)
|
|
|
|
{
|
2007-10-19 23:36:27 +02:00
|
|
|
debug("cIptvProtocolHttp::Close()\n");
|
2007-09-16 11:38:00 +02:00
|
|
|
// Disconnect the current stream
|
|
|
|
Disconnect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-26 15:04:12 +01:00
|
|
|
int cIptvProtocolHttp::Read(unsigned char* BufferAddr, unsigned int BufferLen)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
2009-02-26 15:04:12 +01:00
|
|
|
return cIptvTcpSocket::Read(BufferAddr, BufferLen);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2007-10-20 00:18:55 +02:00
|
|
|
bool cIptvProtocolHttp::Set(const char* Location, const int Parameter, const int Index)
|
2007-09-16 11:38:00 +02:00
|
|
|
{
|
2007-10-20 00:18:55 +02:00
|
|
|
debug("cIptvProtocolHttp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
|
2007-10-19 23:36:27 +02:00
|
|
|
if (!isempty(Location)) {
|
2008-01-30 22:57:33 +01:00
|
|
|
// Disconnect the current socket
|
|
|
|
Disconnect();
|
|
|
|
// Update stream address, path and port
|
|
|
|
streamAddr = strcpyrealloc(streamAddr, Location);
|
|
|
|
char *path = strstr(streamAddr, "/");
|
|
|
|
if (path) {
|
|
|
|
streamPath = strcpyrealloc(streamPath, path);
|
|
|
|
*path = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
streamPath = strcpyrealloc(streamPath, "/");
|
|
|
|
socketPort = Parameter;
|
2008-04-03 00:55:04 +02:00
|
|
|
//debug("http://%s:%d%s\n", streamAddr, socketPort, streamPath);
|
2008-01-30 22:57:33 +01:00
|
|
|
// Re-connect the socket
|
|
|
|
Connect();
|
|
|
|
}
|
2007-09-16 11:38:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-10-08 00:54:09 +02:00
|
|
|
|
|
|
|
cString cIptvProtocolHttp::GetInformation(void)
|
|
|
|
{
|
|
|
|
//debug("cIptvProtocolHttp::GetInformation()");
|
2007-10-21 15:31:21 +02:00
|
|
|
return cString::sprintf("http://%s:%d%s", streamAddr, socketPort, streamPath);
|
2007-10-08 00:54:09 +02:00
|
|
|
}
|