ZeroconfGlue.cxx 2.52 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21 22
#include "ZeroconfGlue.hxx"
#include "ZeroconfAvahi.hxx"
#include "ZeroconfBonjour.hxx"
23
#include "config/Data.hxx"
24
#include "config/Option.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "Listen.hxx"
26 27
#include "util/Domain.hxx"
#include "Log.hxx"
28
#include "util/Compiler.h"
29

30 31
#include <climits>

32
#include <string.h>
33
#include <unistd.h>
34

35 36 37 38 39 40
#ifndef HOST_NAME_MAX
/* HOST_NAME_MAX is not a portable macro; it is undefined on some
   systems */
#define HOST_NAME_MAX 255
#endif

41
static constexpr Domain zeroconf_domain("zeroconf");
42

43
/* The default service name to publish
44 45
 * (overridden by 'zeroconf_name' config parameter)
 */
46
#define SERVICE_NAME		"Music Player @ %h"
47

48 49
#define DEFAULT_ZEROCONF_ENABLED 1

50
static int zeroconfEnabled;
51

52
void
Rosen Penev's avatar
Rosen Penev committed
53
ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop)
54
{
55
	const char *serviceName;
56

57 58
	zeroconfEnabled = config.GetBool(ConfigOption::ZEROCONF_ENABLED,
					 DEFAULT_ZEROCONF_ENABLED);
59
	if (!zeroconfEnabled)
60
		return;
61

62
	if (listen_port <= 0) {
63 64
		LogWarning(zeroconf_domain,
			   "No global port, disabling zeroconf");
65 66 67 68
		zeroconfEnabled = false;
		return;
	}

69 70
	serviceName = config.GetString(ConfigOption::ZEROCONF_NAME,
				       SERVICE_NAME);
71

72 73 74 75 76 77 78 79 80 81 82
	/* 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();
		}
	}
83

84
#ifdef HAVE_AVAHI
85
	AvahiInit(loop, serviceName);
86 87 88
#endif

#ifdef HAVE_BONJOUR
89
	BonjourInit(loop, serviceName);
90
#endif
91 92
}

93 94
void
ZeroconfDeinit()
95
{
96
	if (!zeroconfEnabled)
97 98
		return;

99
#ifdef HAVE_AVAHI
100
	AvahiDeinit();
101
#endif /* HAVE_AVAHI */
102 103

#ifdef HAVE_BONJOUR
104
	BonjourDeinit();
105
#endif
106
}