1 From 61df54155a3cb1846e6bf15e4f007ec8d623de63 Mon Sep 17 00:00:00 2001
2 From: Jean-Francois Dockes <jf@dockes.org>
3 Date: Sun, 23 Aug 2020 14:22:21 +0200
4 Subject: [PATCH] Modification to use npupnp instead of pupnp when the upnp
8 meson_options.txt | 5 +-
9 .../plugins/upnp/ContentDirectoryService.cxx | 101 ++++++++++++++++++
10 src/lib/upnp/Action.hxx | 2 +
11 src/lib/upnp/ClientInit.cxx | 12 +--
12 src/lib/upnp/Compat.hxx | 4 +-
13 src/lib/upnp/ContentDirectoryService.cxx | 25 +++++
14 src/lib/upnp/Init.cxx | 4 +
15 src/lib/upnp/UniqueIxml.hxx | 2 +
16 src/lib/upnp/ixmlwrap.cxx | 4 +
17 src/lib/upnp/ixmlwrap.hxx | 2 +
18 src/lib/upnp/meson.build | 20 +++-
19 11 files changed, 170 insertions(+), 11 deletions(-)
21 --- a/meson_options.txt
22 +++ b/meson_options.txt
23 @@ -54,7 +54,10 @@ option('dsd', type: 'boolean', value: tr
26 option('database', type: 'boolean', value: true, description: 'enable support for the music database')
27 -option('upnp', type: 'feature', description: 'UPnP client support')
28 +option('upnp', type: 'combo',
29 + choices: ['auto', 'pupnp', 'npupnp', 'disabled'],
31 + description: 'UPnP client support')
32 option('libmpdclient', type: 'feature', description: 'libmpdclient support (for the proxy database plugin)')
35 --- a/src/db/plugins/upnp/ContentDirectoryService.cxx
36 +++ b/src/db/plugins/upnp/ContentDirectoryService.cxx
40 #include "lib/upnp/ContentDirectoryService.hxx"
43 #include "lib/upnp/ixmlwrap.hxx"
45 #include "lib/upnp/UniqueIxml.hxx"
46 #include "lib/upnp/Action.hxx"
47 #include "Directory.hxx"
49 #include "util/ScopeExit.hxx"
50 #include "util/StringFormat.hxx"
58 ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response)
60 @@ -39,6 +45,7 @@ ReadResultTag(UPnPDirContent &dirbuf, IX
67 ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
68 @@ -47,6 +54,7 @@ ContentDirectoryService::readDirSlice(Up
70 unsigned &totalp) const
73 // Some devices require an empty SortCriteria, else bad params
74 IXML_Document *request =
75 MakeActionHelper("Browse", m_serviceType.c_str(),
76 @@ -82,6 +90,37 @@ ContentDirectoryService::readDirSlice(Up
77 totalp = ParseUnsigned(value);
79 ReadResultTag(dirbuf, response);
81 + std::vector<std::pair<std::string, std::string> > actionParams{
82 + { "ObjectID", objectId },
83 + { "BrowseFlag", "BrowseDirectChildren" },
85 + { "SortCriteria", "" },
86 + { "StartingIndex", StringFormat<32>("%u", offset).c_str() },
87 + { "RequestedCount", StringFormat<32>("%u", count).c_str() }
89 + std::vector<std::pair<std::string, std::string> > responseData;
91 + std::string errdesc;
93 + UpnpSendAction(hdl, "", m_actionURL, m_serviceType, "Browse",
94 + actionParams, responseData, &errcode, errdesc);
95 + if (code != UPNP_E_SUCCESS)
96 + throw FormatRuntimeError("UpnpSendAction() failed: %s",
97 + UpnpGetErrorMessage(code));
100 + for (const auto &entry : responseData) {
101 + if (entry.first == "Result") {
102 + p = entry.second.c_str();
103 + } else if (entry.first == "TotalMatches") {
104 + totalp = ParseUnsigned(entry.second.c_str());
105 + } else if (entry.first == "NumberReturned") {
106 + didreadp = ParseUnsigned(entry.second.c_str());
114 @@ -110,6 +149,7 @@ ContentDirectoryService::search(UpnpClie
115 unsigned offset = 0, total = -1, count;
119 UniqueIxmlDocument request(MakeActionHelper("Search", m_serviceType.c_str(),
120 "ContainerID", objectId,
121 "SearchCriteria", ss,
122 @@ -147,6 +187,39 @@ ContentDirectoryService::search(UpnpClie
123 total = ParseUnsigned(value);
125 ReadResultTag(dirbuf, response.get());
127 + std::vector<std::pair<std::string, std::string> > actionParams{
128 + { "ContainerID", objectId },
129 + { "SearchCriteria", ss },
131 + { "SortCriteria", "" },
133 + StringFormat<32>("%u", offset).c_str() },
134 + { "RequestedCount", "0" }
136 + std::vector<std::pair<std::string, std::string> > responseData;
138 + std::string errdesc;
139 + int code = UpnpSendAction(hdl, "", m_actionURL, m_serviceType,
140 + "Search", actionParams, responseData,
141 + &errcode, errdesc);
142 + if (code != UPNP_E_SUCCESS)
143 + throw FormatRuntimeError("UpnpSendAction() failed: %s",
144 + UpnpGetErrorMessage(code));
145 + const char *p = "";
147 + for (const auto &entry : responseData) {
148 + if (entry.first == "Result") {
149 + p = entry.second.c_str();
150 + } else if (entry.first == "TotalMatches") {
151 + total = ParseUnsigned(entry.second.c_str());
152 + } else if (entry.first == "NumberReturned") {
153 + count = ParseUnsigned(entry.second.c_str());
159 } while (count > 0 && offset < total);
162 @@ -156,6 +229,7 @@ UPnPDirContent
163 ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
164 const char *objectId) const
168 UniqueIxmlDocument request(MakeActionHelper("Browse", m_serviceType.c_str(),
169 "ObjectID", objectId,
170 @@ -179,4 +253,31 @@ ContentDirectoryService::getMetadata(Upn
171 UPnPDirContent dirbuf;
172 ReadResultTag(dirbuf, response.get());
175 + std::vector<std::pair<std::string, std::string> > actionParams{
176 + { "ObjectID", objectId }, { "BrowseFlag", "BrowseMetadata" },
177 + { "Filter", "*" }, { "SortCriteria", "" },
178 + { "StartingIndex", "0" }, { "RequestedCount", "1" }
180 + std::vector<std::pair<std::string, std::string> > responseData;
182 + std::string errdesc;
184 + UpnpSendAction(hdl, "", m_actionURL, m_serviceType, "Browse",
185 + actionParams, responseData, &errcode, errdesc);
186 + if (code != UPNP_E_SUCCESS)
187 + throw FormatRuntimeError("UpnpSendAction() failed: %s",
188 + UpnpGetErrorMessage(code));
189 + const char *p = "";
190 + for (const auto &entry : responseData) {
191 + if (entry.first == "Result") {
192 + p = entry.second.c_str();
197 + UPnPDirContent dirbuf;
202 --- a/src/lib/upnp/Action.hxx
203 +++ b/src/lib/upnp/Action.hxx
204 @@ -38,6 +38,7 @@ CountNameValuePairs(gcc_unused const cha
205 return 1 + CountNameValuePairs(args...);
210 * A wrapper for UpnpMakeAction() that counts the number of name/value
211 * pairs and adds the nullptr sentinel.
212 @@ -52,5 +53,6 @@ MakeActionHelper(const char *action_name
219 --- a/src/lib/upnp/ClientInit.cxx
220 +++ b/src/lib/upnp/ClientInit.cxx
221 @@ -31,14 +31,12 @@ static Mutex upnp_client_init_mutex;
222 static unsigned upnp_client_ref;
223 static UpnpClient_Handle upnp_client_handle;
226 -UpnpClientCallback(Upnp_EventType et,
227 -#if UPNP_VERSION >= 10800
229 +static int UpnpClientCallback(Upnp_EventType et,
234 - void *cookie) noexcept
237 + void *cookie) noexcept {
238 if (cookie == nullptr)
239 /* this is the cookie passed to UpnpRegisterClient();
240 but can this ever happen? Will libupnp ever invoke
241 --- a/src/lib/upnp/Compat.hxx
242 +++ b/src/lib/upnp/Compat.hxx
247 -#if UPNP_VERSION < 10800
249 /* emulate the libupnp 1.8 API with older versions */
251 using UpnpDiscovery = Upnp_Discovery;
255 -#if UPNP_VERSION < 10624
257 #include "util/Compiler.h"
260 --- a/src/lib/upnp/ContentDirectoryService.cxx
261 +++ b/src/lib/upnp/ContentDirectoryService.cxx
263 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
268 #include "ContentDirectoryService.hxx"
269 #include "UniqueIxml.hxx"
270 #include "Device.hxx"
272 #include "ixmlwrap.hxx"
274 #include "Action.hxx"
275 #include "util/UriUtil.hxx"
276 #include "util/RuntimeError.hxx"
277 #include "util/SplitString.hxx"
279 +#include <algorithm>
281 ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device,
282 const UPnPService &service) noexcept
283 :m_actionURL(uri_apply_base(service.controlURL, device.URLBase)),
284 @@ -51,6 +57,7 @@ ContentDirectoryService::~ContentDirecto
285 std::forward_list<std::string>
286 ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl) const
289 UniqueIxmlDocument request(UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
292 @@ -69,6 +76,24 @@ ContentDirectoryService::getSearchCapabi
294 const char *s = ixmlwrap::getFirstElementValue(response.get(),
297 + std::vector<std::pair<std::string, std::string> > responseData;
299 + std::string errdesc;
300 + auto code = UpnpSendAction(hdl, "", m_actionURL, m_serviceType,
301 + "GetSearchCapabilities", {}, responseData,
302 + &errcode, errdesc);
303 + if (code != UPNP_E_SUCCESS)
304 + throw FormatRuntimeError("UpnpSendAction() failed: %s",
305 + UpnpGetErrorMessage(code));
306 + const char *s{ nullptr };
307 + for (auto &entry : responseData) {
308 + if (entry.first == "SearchCaps") {
309 + s = entry.second.c_str();
314 if (s == nullptr || *s == 0)
315 /* we could just "return {}" here, but GCC 5 doesn't
317 --- a/src/lib/upnp/Init.cxx
318 +++ b/src/lib/upnp/Init.cxx
322 #include <upnptools.h>
329 @@ -44,8 +46,10 @@ DoInit()
331 UpnpSetMaxContentLength(2000*1024);
334 // Servers sometimes make error (e.g.: minidlna returns bad utf-8)
340 --- a/src/lib/upnp/UniqueIxml.hxx
341 +++ b/src/lib/upnp/UniqueIxml.hxx
343 #ifndef MPD_UPNP_UNIQUE_XML_HXX
344 #define MPD_UPNP_UNIQUE_XML_HXX
350 @@ -37,4 +38,5 @@ struct UpnpIxmlDeleter {
351 typedef std::unique_ptr<IXML_Document, UpnpIxmlDeleter> UniqueIxmlDocument;
352 typedef std::unique_ptr<IXML_NodeList, UpnpIxmlDeleter> UniqueIxmlNodeList;
354 +#endif /* USING_PUPNP */
356 --- a/src/lib/upnp/ixmlwrap.cxx
357 +++ b/src/lib/upnp/ixmlwrap.cxx
359 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
365 #include "ixmlwrap.hxx"
366 #include "UniqueIxml.hxx"
368 @@ -39,3 +42,4 @@ getFirstElementValue(IXML_Document *doc,
373 --- a/src/lib/upnp/ixmlwrap.hxx
374 +++ b/src/lib/upnp/ixmlwrap.hxx
376 #ifndef _IXMLWRAP_H_INCLUDED_
377 #define _IXMLWRAP_H_INCLUDED_
383 @@ -32,4 +33,5 @@ namespace ixmlwrap {
387 +#endif /* USING_PUPNP */
388 #endif /* _IXMLWRAP_H_INCLUDED_ */
389 --- a/src/lib/upnp/meson.build
390 +++ b/src/lib/upnp/meson.build
392 -upnp_dep = dependency('libupnp', required: get_option('upnp'))
393 +upnp_option = get_option('upnp')
395 +if upnp_option == 'auto'
396 + upnp_dep = dependency('libupnp', version: '>= 1.8', required: false)
397 + conf.set('USING_PUPNP', upnp_dep.found())
398 + if not upnp_dep.found()
399 + upnp_dep = dependency('libnpupnp', version: '>= 1.8', required: false)
401 +elif upnp_option == 'pupnp'
402 + upnp_dep = dependency('libupnp', version: '>= 1.8', required: true)
403 + conf.set('USING_PUPNP', true)
404 +elif upnp_option == 'npupnp'
405 + upnp_dep = dependency('libnpupnp', required: true)
406 + conf.set('USING_PUPNP', false)
407 +elif upnp_option == 'disabled'
408 + upnp_dep = dependency('', required: false)
412 conf.set('ENABLE_UPNP', upnp_dep.found())
413 if not upnp_dep.found()