2014-11-16 23:23:20 +01:00
|
|
|
/*
|
|
|
|
* msearch.c: SAT>IP plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-03 18:57:23 +01:00
|
|
|
#include "config.h"
|
2014-11-16 23:23:20 +01:00
|
|
|
#include "common.h"
|
|
|
|
#include "discover.h"
|
2014-12-05 22:14:40 +01:00
|
|
|
#include "log.h"
|
2014-11-16 23:23:20 +01:00
|
|
|
#include "poller.h"
|
|
|
|
#include "msearch.h"
|
|
|
|
|
|
|
|
const char *cSatipMsearch::bcastAddressS = "239.255.255.250";
|
|
|
|
const char *cSatipMsearch::bcastMessageS = "M-SEARCH * HTTP/1.1\r\n" \
|
|
|
|
"HOST: 239.255.255.250:1900\r\n" \
|
|
|
|
"MAN: \"ssdp:discover\"\r\n" \
|
|
|
|
"ST: urn:ses-com:device:SatIPServer:1\r\n" \
|
|
|
|
"MX: 2\r\n\r\n";
|
|
|
|
|
2014-11-29 14:37:21 +01:00
|
|
|
cSatipMsearch::cSatipMsearch(cSatipDiscoverIf &discoverP)
|
|
|
|
: discoverM(discoverP),
|
|
|
|
bufferLenM(eProbeBufferSize),
|
2014-11-16 23:23:20 +01:00
|
|
|
bufferM(MALLOC(unsigned char, bufferLenM)),
|
|
|
|
registeredM(false)
|
|
|
|
{
|
|
|
|
if (bufferM)
|
|
|
|
memset(bufferM, 0, bufferLenM);
|
|
|
|
else
|
|
|
|
error("Cannot create Msearch buffer!");
|
|
|
|
if (!Open(eDiscoveryPort))
|
|
|
|
error("Cannot open Msearch port!");
|
|
|
|
}
|
|
|
|
|
|
|
|
cSatipMsearch::~cSatipMsearch()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipMsearch::Probe(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-11-16 23:23:20 +01:00
|
|
|
if (!registeredM) {
|
|
|
|
cSatipPoller::GetInstance()->Register(*this);
|
|
|
|
registeredM = true;
|
|
|
|
}
|
|
|
|
Write(bcastAddressS, reinterpret_cast<const unsigned char *>(bcastMessageS), strlen(bcastMessageS));
|
|
|
|
}
|
|
|
|
|
|
|
|
int cSatipMsearch::GetFd(void)
|
|
|
|
{
|
|
|
|
return Fd();
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:04:34 +01:00
|
|
|
void cSatipMsearch::Process(void)
|
2014-11-16 23:23:20 +01:00
|
|
|
{
|
2014-12-14 16:45:55 +01:00
|
|
|
debug16("%s", __PRETTY_FUNCTION__);
|
2014-11-16 23:23:20 +01:00
|
|
|
if (bufferM) {
|
2014-11-29 14:37:21 +01:00
|
|
|
int length;
|
|
|
|
while ((length = Read(bufferM, bufferLenM)) > 0) {
|
|
|
|
bufferM[min(length, int(bufferLenM - 1))] = 0;
|
2015-01-14 20:25:43 +01:00
|
|
|
debug12("%s len=%d buf=%s", __PRETTY_FUNCTION__, length, bufferM);
|
2014-11-29 14:37:21 +01:00
|
|
|
bool status = false, valid = false;
|
|
|
|
char *s, *p = reinterpret_cast<char *>(bufferM), *location = NULL;
|
|
|
|
char *r = strtok_r(p, "\r\n", &s);
|
|
|
|
while (r) {
|
2015-01-14 20:25:43 +01:00
|
|
|
debug12("%s r=%s", __PRETTY_FUNCTION__, r);
|
2014-11-29 14:37:21 +01:00
|
|
|
// Check the status code
|
|
|
|
// HTTP/1.1 200 OK
|
|
|
|
if (!status && startswith(r, "HTTP/1.1 200 OK"))
|
|
|
|
status = true;
|
|
|
|
if (status) {
|
|
|
|
// Check the location data
|
|
|
|
// LOCATION: http://192.168.0.115:8888/octonet.xml
|
|
|
|
if (startswith(r, "LOCATION:")) {
|
|
|
|
location = compactspace(r + 9);
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s location='%s'", __PRETTY_FUNCTION__, location);
|
2014-11-29 14:37:21 +01:00
|
|
|
}
|
|
|
|
// Check the source type
|
|
|
|
// ST: urn:ses-com:device:SatIPServer:1
|
|
|
|
else if (startswith(r, "ST:")) {
|
|
|
|
char *st = compactspace(r + 3);
|
|
|
|
if (strstr(st, "urn:ses-com:device:SatIPServer:1"))
|
|
|
|
valid = true;
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s st='%s'", __PRETTY_FUNCTION__, st);
|
2014-11-29 14:37:21 +01:00
|
|
|
}
|
|
|
|
// Check whether all the required data is found
|
|
|
|
if (valid && !isempty(location)) {
|
|
|
|
discoverM.SetUrl(location);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-16 23:23:20 +01:00
|
|
|
}
|
2014-11-29 14:37:21 +01:00
|
|
|
r = strtok_r(NULL, "\r\n", &s);
|
2014-11-16 23:23:20 +01:00
|
|
|
}
|
2014-11-29 14:37:21 +01:00
|
|
|
}
|
|
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK)
|
|
|
|
error("Error %d reading in %s", errno, *ToString());
|
2014-11-16 23:23:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 13:44:30 +01:00
|
|
|
|
|
|
|
cString cSatipMsearch::ToString(void) const
|
|
|
|
{
|
|
|
|
return "MSearch";
|
|
|
|
}
|