2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2021-02-24 12:36:44 +01:00
|
|
|
#include "Glue.hxx"
|
2021-02-24 06:35:52 +01:00
|
|
|
#include "Helper.hxx"
|
2024-05-06 13:47:04 +02:00
|
|
|
#include "avahi/Helper.hxx"
|
2018-07-17 22:44:16 +02:00
|
|
|
#include "config/Data.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "config/Option.hxx"
|
2013-01-03 11:05:44 +01:00
|
|
|
#include "Listen.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
2018-10-31 19:23:57 +01:00
|
|
|
|
2020-03-13 00:14:04 +01:00
|
|
|
#include <climits>
|
|
|
|
|
2018-10-31 19:23:57 +01:00
|
|
|
#include <string.h>
|
2018-10-31 17:54:45 +01:00
|
|
|
#include <unistd.h>
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2018-11-04 11:12:03 +01:00
|
|
|
#ifndef HOST_NAME_MAX
|
|
|
|
/* HOST_NAME_MAX is not a portable macro; it is undefined on some
|
|
|
|
systems */
|
|
|
|
#define HOST_NAME_MAX 255
|
|
|
|
#endif
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain zeroconf_domain("zeroconf");
|
2008-11-24 14:40:40 +01:00
|
|
|
|
2008-01-03 11:03:28 +01:00
|
|
|
/* The default service name to publish
|
2007-01-11 21:41:17 +01:00
|
|
|
* (overridden by 'zeroconf_name' config parameter)
|
|
|
|
*/
|
2018-10-31 17:54:45 +01:00
|
|
|
#define SERVICE_NAME "Music Player @ %h"
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2021-02-24 15:03:58 +01:00
|
|
|
/* The dns-sd service type qualifier to publish */
|
|
|
|
#define SERVICE_TYPE "_mpd._tcp"
|
|
|
|
|
2007-06-03 21:25:25 +02:00
|
|
|
#define DEFAULT_ZEROCONF_ENABLED 1
|
|
|
|
|
2021-02-24 06:35:52 +01:00
|
|
|
std::unique_ptr<ZeroconfHelper>
|
2020-03-12 20:56:11 +01:00
|
|
|
ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop)
|
2007-01-14 03:08:09 +01:00
|
|
|
{
|
2009-01-27 20:17:44 +01:00
|
|
|
const char *serviceName;
|
2007-06-03 20:08:51 +02:00
|
|
|
|
2021-02-24 06:35:52 +01:00
|
|
|
if (!config.GetBool(ConfigOption::ZEROCONF_ENABLED,
|
|
|
|
DEFAULT_ZEROCONF_ENABLED))
|
|
|
|
return nullptr;
|
2007-01-14 03:08:09 +01:00
|
|
|
|
2012-02-13 21:04:50 +01:00
|
|
|
if (listen_port <= 0) {
|
2013-11-04 22:20:11 +01:00
|
|
|
LogWarning(zeroconf_domain,
|
|
|
|
"No global port, disabling zeroconf");
|
2021-02-24 06:35:52 +01:00
|
|
|
return nullptr;
|
2012-02-13 21:04:50 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 22:44:16 +02:00
|
|
|
serviceName = config.GetString(ConfigOption::ZEROCONF_NAME,
|
|
|
|
SERVICE_NAME);
|
2007-06-02 19:06:08 +02:00
|
|
|
|
2018-10-31 19:23:57 +01:00
|
|
|
/* replace "%h" with the host name */
|
|
|
|
const char *h = strstr(serviceName, "%h");
|
|
|
|
std::string buffer;
|
|
|
|
if (h != nullptr) {
|
|
|
|
char hostname[HOST_NAME_MAX+1];
|
|
|
|
if (gethostname(hostname, HOST_NAME_MAX) == 0) {
|
|
|
|
buffer = serviceName;
|
|
|
|
buffer.replace(h - serviceName, 2, hostname);
|
|
|
|
serviceName = buffer.c_str();
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 17:54:45 +01:00
|
|
|
|
2021-02-24 15:03:58 +01:00
|
|
|
return std::make_unique<ZeroconfHelper>(loop, serviceName,
|
|
|
|
SERVICE_TYPE, listen_port);
|
2007-01-11 21:41:17 +01:00
|
|
|
}
|