2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2007-01-11 21:41:17 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2007-01-11 21:41:17 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-27 22:18:45 +01:00
|
|
|
#include "ZeroconfGlue.hxx"
|
|
|
|
#include "ZeroconfAvahi.hxx"
|
|
|
|
#include "ZeroconfBonjour.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-08-20 16:19:17 +02:00
|
|
|
#include "util/Compiler.h"
|
2018-10-31 19:23:57 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
2018-10-31 17:54:45 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.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
|
|
|
|
2007-06-03 21:25:25 +02:00
|
|
|
#define DEFAULT_ZEROCONF_ENABLED 1
|
|
|
|
|
2007-06-03 21:30:37 +02:00
|
|
|
static int zeroconfEnabled;
|
2007-06-02 19:06:08 +02:00
|
|
|
|
2013-01-27 22:18:45 +01:00
|
|
|
void
|
2018-07-17 22:44:16 +02:00
|
|
|
ZeroconfInit(const ConfigData &config, gcc_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
|
|
|
|
2018-07-17 22:44:16 +02:00
|
|
|
zeroconfEnabled = config.GetBool(ConfigOption::ZEROCONF_ENABLED,
|
|
|
|
DEFAULT_ZEROCONF_ENABLED);
|
2007-06-03 21:25:25 +02:00
|
|
|
if (!zeroconfEnabled)
|
2007-06-03 20:08:51 +02:00
|
|
|
return;
|
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");
|
2012-02-13 21:04:50 +01:00
|
|
|
zeroconfEnabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-06-02 19:06:08 +02:00
|
|
|
#ifdef HAVE_AVAHI
|
2013-01-27 22:53:21 +01:00
|
|
|
AvahiInit(loop, serviceName);
|
2007-06-02 19:06:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_BONJOUR
|
2013-01-27 22:32:10 +01:00
|
|
|
BonjourInit(loop, serviceName);
|
2007-06-02 19:06:08 +02:00
|
|
|
#endif
|
2007-01-11 21:41:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:18:45 +01:00
|
|
|
void
|
|
|
|
ZeroconfDeinit()
|
2007-01-11 21:41:17 +01:00
|
|
|
{
|
2007-06-03 21:25:25 +02:00
|
|
|
if (!zeroconfEnabled)
|
2007-06-03 20:08:51 +02:00
|
|
|
return;
|
|
|
|
|
2007-01-14 05:25:27 +01:00
|
|
|
#ifdef HAVE_AVAHI
|
2013-01-27 22:18:45 +01:00
|
|
|
AvahiDeinit();
|
2007-05-16 01:23:09 +02:00
|
|
|
#endif /* HAVE_AVAHI */
|
2007-06-02 19:06:08 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_BONJOUR
|
2013-01-27 22:18:45 +01:00
|
|
|
BonjourDeinit();
|
2007-06-02 19:06:08 +02:00
|
|
|
#endif
|
2007-01-11 21:41:17 +01:00
|
|
|
}
|