2013-11-01 19:26:01 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "Discovery.hxx"
|
|
|
|
#include "Device.hxx"
|
|
|
|
#include "Domain.hxx"
|
|
|
|
#include "ContentDirectoryService.hxx"
|
|
|
|
#include "WorkQueue.hxx"
|
|
|
|
#include "upnpplib.hxx"
|
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
|
|
|
|
#include <upnp/upnp.h>
|
|
|
|
#include <upnp/upnptools.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
// The service type string we are looking for.
|
2014-01-11 01:21:54 +01:00
|
|
|
static const char *const ContentDirectorySType = "urn:schemas-upnp-org:service:ContentDirectory:1";
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
// We don't include a version in comparisons, as we are satisfied with
|
|
|
|
// version 1
|
2014-01-11 01:21:54 +01:00
|
|
|
gcc_pure
|
2013-11-01 19:26:01 +01:00
|
|
|
static bool
|
2014-01-11 01:21:54 +01:00
|
|
|
isCDService(const char *st)
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-11 01:21:54 +01:00
|
|
|
const size_t sz = strlen(ContentDirectorySType) - 2;
|
|
|
|
return memcmp(ContentDirectorySType, st, sz) == 0;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The type of device we're asking for in search
|
2014-01-11 01:21:54 +01:00
|
|
|
static const char *const MediaServerDType = "urn:schemas-upnp-org:device:MediaServer:1";
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-11 01:21:54 +01:00
|
|
|
gcc_pure
|
2013-11-01 19:26:01 +01:00
|
|
|
static bool
|
2014-01-11 01:21:54 +01:00
|
|
|
isMSDevice(const char *st)
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-11 01:21:54 +01:00
|
|
|
const size_t sz = strlen(MediaServerDType) - 2;
|
|
|
|
return memcmp(MediaServerDType, st, sz) == 0;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each appropriate discovery event (executing in a libupnp thread
|
|
|
|
* context) queues the following task object for processing by the
|
|
|
|
* discovery thread.
|
|
|
|
*/
|
|
|
|
struct DiscoveredTask {
|
|
|
|
bool alive;
|
|
|
|
std::string url;
|
|
|
|
std::string deviceId;
|
|
|
|
int expires; // Seconds valid
|
|
|
|
|
|
|
|
DiscoveredTask(bool _alive, const Upnp_Discovery *disco)
|
|
|
|
: alive(_alive), url(disco->Location),
|
|
|
|
deviceId(disco->DeviceId),
|
|
|
|
expires(disco->Expires) {}
|
|
|
|
|
|
|
|
};
|
|
|
|
static WorkQueue<DiscoveredTask *> discoveredQueue("DiscoveredQueue");
|
|
|
|
|
|
|
|
// Descriptor for one device having a Content Directory service found
|
|
|
|
// on the network.
|
|
|
|
class ContentDirectoryDescriptor {
|
|
|
|
public:
|
|
|
|
ContentDirectoryDescriptor(const std::string &url,
|
|
|
|
const std::string &description,
|
|
|
|
time_t last, int exp)
|
|
|
|
:device(url, description), last_seen(last), expires(exp+20) {}
|
|
|
|
UPnPDevice device;
|
|
|
|
time_t last_seen;
|
|
|
|
int expires; // seconds valid
|
|
|
|
};
|
|
|
|
|
|
|
|
// A ContentDirectoryPool holds the characteristics of the servers
|
|
|
|
// currently on the network.
|
|
|
|
// The map is referenced by deviceId (==UDN)
|
|
|
|
// The class is instanciated as a static (unenforced) singleton.
|
|
|
|
class ContentDirectoryPool {
|
|
|
|
public:
|
|
|
|
Mutex m_mutex;
|
|
|
|
std::map<std::string, ContentDirectoryDescriptor> m_directories;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ContentDirectoryPool contentDirectories;
|
|
|
|
|
|
|
|
// Worker routine for the discovery queue. Get messages about devices
|
|
|
|
// appearing and disappearing, and update the directory pool
|
|
|
|
// accordingly.
|
|
|
|
static void *
|
|
|
|
discoExplorer(void *)
|
|
|
|
{
|
2014-01-17 00:17:20 +01:00
|
|
|
auto &mutex = contentDirectories.m_mutex;
|
|
|
|
auto &directories = contentDirectories.m_directories;
|
|
|
|
|
2013-11-01 19:26:01 +01:00
|
|
|
for (;;) {
|
|
|
|
DiscoveredTask *tsk = 0;
|
2014-01-14 11:02:04 +01:00
|
|
|
if (!discoveredQueue.take(tsk)) {
|
2013-11-01 19:26:01 +01:00
|
|
|
discoveredQueue.workerExit();
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:17:20 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (!tsk->alive) {
|
|
|
|
// Device signals it is going off.
|
2014-01-17 00:17:20 +01:00
|
|
|
auto it = directories.find(tsk->deviceId);
|
|
|
|
if (it != directories.end()) {
|
|
|
|
directories.erase(it);
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Device signals its existence and well-being. Perform the
|
|
|
|
// UPnP "description" phase by downloading and decoding the
|
|
|
|
// description document.
|
|
|
|
char *buf;
|
|
|
|
// LINE_SIZE is defined by libupnp's upnp.h...
|
|
|
|
char contentType[LINE_SIZE];
|
|
|
|
int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType);
|
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string sdesc(buf);
|
|
|
|
|
|
|
|
// Update or insert the device
|
|
|
|
ContentDirectoryDescriptor d(tsk->url, sdesc,
|
|
|
|
time(0), tsk->expires);
|
|
|
|
if (!d.device.ok) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-13 21:35:14 +01:00
|
|
|
#if defined(__clang__) || GCC_CHECK_VERSION(4,8)
|
2014-01-17 00:17:20 +01:00
|
|
|
auto e = directories.emplace(tsk->deviceId, d);
|
2014-01-13 21:35:14 +01:00
|
|
|
#else
|
2014-01-17 00:17:20 +01:00
|
|
|
auto e = directories.insert(std::make_pair(tsk->deviceId, d));
|
2014-01-13 21:35:14 +01:00
|
|
|
#endif
|
2013-11-01 19:26:01 +01:00
|
|
|
if (!e.second)
|
|
|
|
e.first->second = d;
|
|
|
|
}
|
|
|
|
delete tsk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 00:26:05 +01:00
|
|
|
static int
|
|
|
|
OnAlive(Upnp_Discovery *disco)
|
|
|
|
{
|
|
|
|
if (isMSDevice(disco->DeviceType) ||
|
|
|
|
isCDService(disco->ServiceType)) {
|
|
|
|
DiscoveredTask *tp = new DiscoveredTask(1, disco);
|
|
|
|
if (discoveredQueue.put(tp))
|
|
|
|
return UPNP_E_FINISH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UPNP_E_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
OnByeBye(Upnp_Discovery *disco)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (isMSDevice(disco->DeviceType) ||
|
|
|
|
isCDService(disco->ServiceType)) {
|
|
|
|
DiscoveredTask *tp = new DiscoveredTask(0, disco);
|
|
|
|
if (discoveredQueue.put(tp))
|
|
|
|
return UPNP_E_FINISH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UPNP_E_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:01 +01:00
|
|
|
// This gets called for all libupnp asynchronous events, in a libupnp
|
|
|
|
// thread context.
|
|
|
|
// Example: ContentDirectories appearing and disappearing from the network
|
|
|
|
// We queue a task for our worker thread(s)
|
|
|
|
static int
|
2014-01-13 22:05:45 +01:00
|
|
|
cluCallBack(Upnp_EventType et, void *evp)
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
|
|
|
switch (et) {
|
|
|
|
case UPNP_DISCOVERY_SEARCH_RESULT:
|
|
|
|
case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
|
|
|
|
{
|
|
|
|
Upnp_Discovery *disco = (Upnp_Discovery *)evp;
|
2014-01-17 00:26:05 +01:00
|
|
|
return OnAlive(disco);
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
|
|
|
|
{
|
|
|
|
Upnp_Discovery *disco = (Upnp_Discovery *)evp;
|
2014-01-17 00:26:05 +01:00
|
|
|
return OnByeBye(disco);
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Ignore other events for now
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UPNP_E_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UPnPDeviceDirectory::expireDevices()
|
|
|
|
{
|
|
|
|
const ScopeLock protect(contentDirectories.m_mutex);
|
2014-01-17 00:17:20 +01:00
|
|
|
auto &directories = contentDirectories.m_directories;
|
2013-11-01 19:26:01 +01:00
|
|
|
time_t now = time(0);
|
|
|
|
bool didsomething = false;
|
|
|
|
|
2014-01-17 00:17:20 +01:00
|
|
|
for (auto it = directories.begin();
|
|
|
|
it != directories.end();) {
|
2013-11-01 19:26:01 +01:00
|
|
|
if (now - it->second.last_seen > it->second.expires) {
|
2014-01-17 00:17:20 +01:00
|
|
|
it = directories.erase(it);
|
2013-11-01 19:26:01 +01:00
|
|
|
didsomething = true;
|
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (didsomething)
|
|
|
|
search();
|
|
|
|
}
|
|
|
|
|
2014-01-16 09:06:01 +01:00
|
|
|
UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib)
|
|
|
|
:lib(_lib), m_searchTimeout(2), m_lastSearch(0)
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
|
|
|
if (!discoveredQueue.start(1, discoExplorer, 0)) {
|
|
|
|
error.Set(upnp_domain, "Discover work queue start failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-13 22:05:45 +01:00
|
|
|
lib->SetHandler([](Upnp_EventType type, void *event){
|
|
|
|
cluCallBack(type, event);
|
|
|
|
});
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
search();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UPnPDeviceDirectory::search()
|
|
|
|
{
|
|
|
|
time_t now = time(0);
|
|
|
|
if (now - m_lastSearch < 10)
|
|
|
|
return true;
|
|
|
|
m_lastSearch = now;
|
|
|
|
|
|
|
|
// We search both for device and service just in case.
|
|
|
|
int code = UpnpSearchAsync(lib->getclh(), m_searchTimeout,
|
2014-01-11 01:21:54 +01:00
|
|
|
ContentDirectorySType, lib);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
error.Format(upnp_domain, code,
|
|
|
|
"UpnpSearchAsync() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
code = UpnpSearchAsync(lib->getclh(), m_searchTimeout,
|
2014-01-11 01:21:54 +01:00
|
|
|
MediaServerDType, lib);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
error.Format(upnp_domain, code,
|
|
|
|
"UpnpSearchAsync() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UPnPDeviceDirectory::getDirServices(std::vector<ContentDirectoryService> &out)
|
|
|
|
{
|
|
|
|
if (!ok())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Has locking, do it before our own lock
|
|
|
|
expireDevices();
|
|
|
|
|
|
|
|
const ScopeLock protect(contentDirectories.m_mutex);
|
2014-01-17 00:17:20 +01:00
|
|
|
auto &directories = contentDirectories.m_directories;
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-17 00:17:20 +01:00
|
|
|
for (auto dit = directories.begin();
|
|
|
|
dit != directories.end(); dit++) {
|
2013-11-01 19:26:01 +01:00
|
|
|
for (const auto &service : dit->second.device.services) {
|
2014-01-11 01:21:54 +01:00
|
|
|
if (isCDService(service.serviceType.c_str())) {
|
2013-11-01 19:26:01 +01:00
|
|
|
out.emplace_back(dit->second.device, service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UPnPDeviceDirectory::getServer(const char *friendlyName,
|
|
|
|
ContentDirectoryService &server)
|
|
|
|
{
|
|
|
|
std::vector<ContentDirectoryService> ds;
|
|
|
|
if (!getDirServices(ds)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &i : ds) {
|
|
|
|
if (strcmp(friendlyName, i.getFriendlyName()) == 0) {
|
|
|
|
server = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|