2013-11-01 19:26:01 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-11-01 19:26:01 +01:00
|
|
|
* 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 "ContentDirectoryService.hxx"
|
2014-01-18 13:47:12 +01:00
|
|
|
#include "Log.hxx"
|
2016-02-06 22:06:43 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2016-02-07 00:29:06 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
#include <upnp/upnptools.h>
|
|
|
|
|
2014-11-02 11:04:13 +01:00
|
|
|
#include <stdlib.h>
|
2013-11-01 19:26:01 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// The service type string we are looking for.
|
2014-01-22 21:47:34 +01:00
|
|
|
static constexpr char 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
|
2017-05-08 14:44:49 +02:00
|
|
|
isCDService(const char *st) noexcept
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-22 21:47:34 +01:00
|
|
|
constexpr size_t sz = sizeof(ContentDirectorySType) - 3;
|
2014-01-11 01:21:54 +01:00
|
|
|
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-22 21:47:34 +01:00
|
|
|
static constexpr char 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
|
2017-05-08 14:44:49 +02:00
|
|
|
isMSDevice(const char *st) noexcept
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-22 21:47:34 +01:00
|
|
|
constexpr size_t sz = sizeof(MediaServerDType) - 3;
|
2014-01-11 01:21:54 +01:00
|
|
|
return memcmp(MediaServerDType, st, sz) == 0;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 13:14:15 +01:00
|
|
|
static void
|
|
|
|
AnnounceFoundUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device)
|
|
|
|
{
|
|
|
|
for (const auto &service : device.services)
|
|
|
|
if (isCDService(service.serviceType.c_str()))
|
|
|
|
listener.FoundUPnP(ContentDirectoryService(device,
|
|
|
|
service));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AnnounceLostUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device)
|
|
|
|
{
|
|
|
|
for (const auto &service : device.services)
|
|
|
|
if (isCDService(service.serviceType.c_str()))
|
|
|
|
listener.LostUPnP(ContentDirectoryService(device,
|
|
|
|
service));
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:21:45 +01:00
|
|
|
inline void
|
2014-01-23 22:18:40 +01:00
|
|
|
UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d)
|
2014-01-23 22:21:45 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2014-01-23 22:18:40 +01:00
|
|
|
|
|
|
|
for (auto &i : directories) {
|
|
|
|
if (i.id == d.id) {
|
|
|
|
i = std::move(d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
directories.emplace_back(std::move(d));
|
2014-01-26 13:14:15 +01:00
|
|
|
|
|
|
|
if (listener != nullptr)
|
|
|
|
AnnounceFoundUPnP(*listener, directories.back().device);
|
2014-01-23 22:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
UPnPDeviceDirectory::LockRemove(const std::string &id)
|
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2014-01-23 22:18:40 +01:00
|
|
|
|
|
|
|
for (auto i = directories.begin(), end = directories.end();
|
|
|
|
i != end; ++i) {
|
|
|
|
if (i->id == id) {
|
2014-01-26 13:14:15 +01:00
|
|
|
if (listener != nullptr)
|
|
|
|
AnnounceLostUPnP(*listener, i->device);
|
|
|
|
|
2014-01-23 22:18:40 +01:00
|
|
|
directories.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 22:21:45 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 12:50:59 +01:00
|
|
|
inline void
|
2015-02-02 22:12:19 +01:00
|
|
|
UPnPDeviceDirectory::Explore()
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
|
|
|
for (;;) {
|
2016-02-06 22:26:14 +01:00
|
|
|
std::unique_ptr<DiscoveredTask> tsk;
|
2015-02-02 22:12:19 +01:00
|
|
|
if (!queue.take(tsk)) {
|
|
|
|
queue.workerExit();
|
2014-01-16 12:50:59 +01:00
|
|
|
return;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 12:56:35 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-02-06 22:06:43 +01:00
|
|
|
AtScopeExit(buf){ free(buf); };
|
|
|
|
|
2014-01-18 12:56:35 +01:00
|
|
|
// Update or insert the device
|
2015-02-02 22:12:19 +01:00
|
|
|
ContentDirectoryDescriptor d(std::move(tsk->device_id),
|
2016-12-28 22:03:19 +01:00
|
|
|
std::chrono::steady_clock::now(),
|
|
|
|
tsk->expires);
|
2014-01-18 13:47:12 +01:00
|
|
|
|
2016-02-06 08:45:19 +01:00
|
|
|
try {
|
|
|
|
d.Parse(tsk->url, buf);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LogError(e);
|
2014-01-18 12:56:35 +01:00
|
|
|
}
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-23 22:18:40 +01:00
|
|
|
LockAdd(std::move(d));
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 12:50:59 +01:00
|
|
|
void *
|
2015-02-02 22:12:19 +01:00
|
|
|
UPnPDeviceDirectory::Explore(void *ctx)
|
2014-01-16 12:50:59 +01:00
|
|
|
{
|
|
|
|
UPnPDeviceDirectory &directory = *(UPnPDeviceDirectory *)ctx;
|
2015-02-02 22:12:19 +01:00
|
|
|
directory.Explore();
|
2014-01-16 12:50:59 +01:00
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int
|
|
|
|
UPnPDeviceDirectory::OnAlive(Upnp_Discovery *disco)
|
2014-01-17 00:26:05 +01:00
|
|
|
{
|
|
|
|
if (isMSDevice(disco->DeviceType) ||
|
|
|
|
isCDService(disco->ServiceType)) {
|
2014-01-18 12:56:35 +01:00
|
|
|
DiscoveredTask *tp = new DiscoveredTask(disco);
|
2015-02-02 22:12:19 +01:00
|
|
|
if (queue.put(tp))
|
2014-01-17 00:26:05 +01:00
|
|
|
return UPNP_E_FINISH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UPNP_E_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-01-16 12:50:59 +01:00
|
|
|
inline int
|
|
|
|
UPnPDeviceDirectory::OnByeBye(Upnp_Discovery *disco)
|
2014-01-17 00:26:05 +01:00
|
|
|
{
|
|
|
|
if (isMSDevice(disco->DeviceType) ||
|
|
|
|
isCDService(disco->ServiceType)) {
|
2014-01-18 12:56:35 +01:00
|
|
|
// Device signals it is going off.
|
2014-01-23 22:21:45 +01:00
|
|
|
LockRemove(disco->DeviceId);
|
2014-01-17 00:26:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-01-26 15:04:56 +01:00
|
|
|
int
|
|
|
|
UPnPDeviceDirectory::Invoke(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;
|
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
void
|
|
|
|
UPnPDeviceDirectory::ExpireDevices()
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2016-12-28 22:03:19 +01:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2013-11-01 19:26:01 +01:00
|
|
|
bool didsomething = false;
|
|
|
|
|
2014-01-17 00:17:20 +01:00
|
|
|
for (auto it = directories.begin();
|
|
|
|
it != directories.end();) {
|
2014-01-23 22:18:40 +01:00
|
|
|
if (now > it->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)
|
2016-02-07 00:29:06 +01:00
|
|
|
Search();
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 15:22:33 +01:00
|
|
|
UPnPDeviceDirectory::UPnPDeviceDirectory(UpnpClient_Handle _handle,
|
2014-01-26 13:14:15 +01:00
|
|
|
UPnPDiscoveryListener *_listener)
|
2014-01-26 15:22:33 +01:00
|
|
|
:handle(_handle),
|
2014-01-26 13:14:15 +01:00
|
|
|
listener(_listener),
|
2016-12-28 22:05:31 +01:00
|
|
|
queue("DiscoveredQueue")
|
2014-01-18 16:08:30 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:33:18 +01:00
|
|
|
UPnPDeviceDirectory::~UPnPDeviceDirectory()
|
|
|
|
{
|
|
|
|
/* this destructor exists here just so it won't get inlined */
|
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
void
|
|
|
|
UPnPDeviceDirectory::Start()
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2016-02-07 00:29:06 +01:00
|
|
|
if (!queue.start(1, Explore, this))
|
|
|
|
throw std::runtime_error("Discover work queue start failed");
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
Search();
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
void
|
|
|
|
UPnPDeviceDirectory::Search()
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2016-12-28 22:03:19 +01:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
if (now - last_search < std::chrono::seconds(10))
|
2016-02-07 00:29:06 +01:00
|
|
|
return;
|
2015-02-02 22:12:19 +01:00
|
|
|
last_search = now;
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
// We search both for device and service just in case.
|
2015-02-02 22:12:19 +01:00
|
|
|
int code = UpnpSearchAsync(handle, search_timeout,
|
2014-01-26 15:04:56 +01:00
|
|
|
ContentDirectorySType, GetUpnpCookie());
|
2016-02-07 00:29:06 +01:00
|
|
|
if (code != UPNP_E_SUCCESS)
|
|
|
|
throw FormatRuntimeError("UpnpSearchAsync() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2015-02-02 22:12:19 +01:00
|
|
|
code = UpnpSearchAsync(handle, search_timeout,
|
2014-01-26 15:04:56 +01:00
|
|
|
MediaServerDType, GetUpnpCookie());
|
2016-02-07 00:29:06 +01:00
|
|
|
if (code != UPNP_E_SUCCESS)
|
|
|
|
throw FormatRuntimeError("UpnpSearchAsync() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
std::vector<ContentDirectoryService>
|
|
|
|
UPnPDeviceDirectory::GetDirectories()
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2015-02-03 23:48:11 +01:00
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
ExpireDevices();
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
std::vector<ContentDirectoryService> out;
|
2014-01-17 00:17:20 +01:00
|
|
|
for (auto dit = directories.begin();
|
|
|
|
dit != directories.end(); dit++) {
|
2014-01-23 22:18:40 +01:00
|
|
|
for (const auto &service : dit->device.services) {
|
2014-01-11 01:21:54 +01:00
|
|
|
if (isCDService(service.serviceType.c_str())) {
|
2014-01-23 22:18:40 +01:00
|
|
|
out.emplace_back(dit->device, service);
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
return out;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
ContentDirectoryService
|
|
|
|
UPnPDeviceDirectory::GetServer(const char *friendly_name)
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2015-02-03 23:48:11 +01:00
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
ExpireDevices();
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-18 16:18:51 +01:00
|
|
|
for (const auto &i : directories) {
|
2014-01-23 22:18:40 +01:00
|
|
|
const auto &device = i.device;
|
2014-01-18 16:18:51 +01:00
|
|
|
|
2015-02-02 22:12:19 +01:00
|
|
|
if (device.friendlyName != friendly_name)
|
2014-01-18 16:18:51 +01:00
|
|
|
continue;
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
for (const auto &service : device.services)
|
|
|
|
if (isCDService(service.serviceType.c_str()))
|
|
|
|
return ContentDirectoryService(device,
|
|
|
|
service);
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 00:29:06 +01:00
|
|
|
throw std::runtime_error("Server not found");
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|