ZeroconfAvahi.cxx 7.23 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
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
#include "ZeroconfAvahi.hxx"
21
#include "AvahiPoll.hxx"
22
#include "ZeroconfInternal.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "Listen.hxx"
24
#include "util/Domain.hxx"
25
#include "util/RuntimeError.hxx"
26
#include "Log.hxx"
27 28 29 30 31 32 33 34 35

#include <avahi-client/client.h>
#include <avahi-client/publish.h>

#include <avahi-common/alternative.h>
#include <avahi-common/domain.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>

36
static constexpr Domain avahi_domain("avahi");
37

38
static char *avahi_name;
39
static MyAvahiPoll *avahi_poll;
40 41
static AvahiClient *avahi_client;
static AvahiEntryGroup *avahi_group;
42

43 44
static void
AvahiRegisterService(AvahiClient *c);
45

46 47 48 49 50 51 52
/**
 * Callback when the EntryGroup changes state.
 */
static void
AvahiGroupCallback(AvahiEntryGroup *g,
		   AvahiEntryGroupState state,
		   gcc_unused void *userdata)
53
{
54
	assert(g != nullptr);
55

56 57
	FormatDebug(avahi_domain,
		    "Service group changed to state %d", state);
58 59 60 61

	switch (state) {
	case AVAHI_ENTRY_GROUP_ESTABLISHED:
		/* The entry group has been established successfully */
62 63
		FormatDefault(avahi_domain,
			      "Service '%s' successfully established.",
64
			      avahi_name);
65 66 67 68
		break;

	case AVAHI_ENTRY_GROUP_COLLISION:
		/* A service name collision happened. Let's pick a new name */
69 70 71 72 73
		{
			char *n = avahi_alternative_service_name(avahi_name);
			avahi_free(avahi_name);
			avahi_name = n;
		}
74

75 76
		FormatDefault(avahi_domain,
			      "Service name collision, renaming service to '%s'",
77
			      avahi_name);
78 79

		/* And recreate the services */
80
		AvahiRegisterService(avahi_entry_group_get_client(g));
81 82 83
		break;

	case AVAHI_ENTRY_GROUP_FAILURE:
84 85
		FormatError(avahi_domain,
			    "Entry group failure: %s",
86 87 88
			    avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
		/* Some kind of failure happened while we were
		   registering our services */
89 90 91
		break;

	case AVAHI_ENTRY_GROUP_UNCOMMITED:
92
		LogDebug(avahi_domain, "Service group is UNCOMMITED");
93
		break;
94

95
	case AVAHI_ENTRY_GROUP_REGISTERING:
96
		LogDebug(avahi_domain, "Service group is REGISTERING");
97 98 99
	}
}

100 101 102 103 104
/**
 * Registers a new service with avahi.
 */
static void
AvahiRegisterService(AvahiClient *c)
105
{
106
	assert(c != nullptr);
107

108 109
	FormatDebug(avahi_domain, "Registering service %s/%s",
		    SERVICE_TYPE, avahi_name);
110 111 112

	/* If this is the first time we're called,
	 * let's create a new entry group */
113 114 115
	if (!avahi_group) {
		avahi_group = avahi_entry_group_new(c, AvahiGroupCallback, nullptr);
		if (!avahi_group) {
116 117 118
			FormatError(avahi_domain,
				    "Failed to create avahi EntryGroup: %s",
				    avahi_strerror(avahi_client_errno(c)));
119
			return;
120 121 122 123 124 125 126
		}
	}

	/* Add the service */
	/* TODO: This currently binds to ALL interfaces.
	 *       We could maybe add a service per actual bound interface,
	 *       if that's better. */
127 128 129 130 131 132 133 134
	int result = avahi_entry_group_add_service(avahi_group,
						   AVAHI_IF_UNSPEC,
						   AVAHI_PROTO_UNSPEC,
						   AvahiPublishFlags(0),
						   avahi_name, SERVICE_TYPE,
						   nullptr, nullptr,
						   listen_port, nullptr);
	if (result < 0) {
135
		FormatError(avahi_domain, "Failed to add service %s: %s",
136
			    SERVICE_TYPE, avahi_strerror(result));
137
		return;
138 139 140
	}

	/* Tell the server to register the service group */
141 142
	result = avahi_entry_group_commit(avahi_group);
	if (result < 0) {
143
		FormatError(avahi_domain, "Failed to commit service group: %s",
144
			    avahi_strerror(result));
145
		return;
146 147 148 149
	}
}

/* Callback when avahi changes state */
150 151 152
static void
MyAvahiClientCallback(AvahiClient *c, AvahiClientState state,
		      gcc_unused void *userdata)
153
{
154
	assert(c != nullptr);
155 156

	/* Called whenever the client or server state changes */
157
	FormatDebug(avahi_domain, "Client changed to state %d", state);
158 159

	switch (state) {
160 161
		int reason;

162
	case AVAHI_CLIENT_S_RUNNING:
163
		LogDebug(avahi_domain, "Client is RUNNING");
164 165 166

		/* The server has startup successfully and registered its host
		 * name on the network, so it's time to create our services */
167 168
		if (avahi_group == nullptr)
			AvahiRegisterService(c);
169 170 171 172 173
		break;

	case AVAHI_CLIENT_FAILURE:
		reason = avahi_client_errno(c);
		if (reason == AVAHI_ERR_DISCONNECTED) {
174 175
			LogDefault(avahi_domain,
				   "Client Disconnected, will reconnect shortly");
176 177 178
			if (avahi_group != nullptr) {
				avahi_entry_group_free(avahi_group);
				avahi_group = nullptr;
179
			}
180 181 182 183

			if (avahi_client != nullptr)
				avahi_client_free(avahi_client);
			avahi_client =
184
			    avahi_client_new(avahi_poll,
185
					     AVAHI_CLIENT_NO_FAIL,
186
					     MyAvahiClientCallback, nullptr,
187
					     &reason);
188
			if (avahi_client == nullptr)
189 190 191
				FormatWarning(avahi_domain,
					      "Could not reconnect: %s",
					      avahi_strerror(reason));
192
		} else {
193 194 195
			FormatWarning(avahi_domain,
				      "Client failure: %s (terminal)",
				      avahi_strerror(reason));
196
		}
197

198 199 200
		break;

	case AVAHI_CLIENT_S_COLLISION:
201 202
		LogDebug(avahi_domain, "Client is COLLISION");

203 204 205 206
		/* Let's drop our registered services. When the server
		   is back in AVAHI_SERVER_RUNNING state we will
		   register them again with the new host name. */
		if (avahi_group != nullptr) {
207
			LogDebug(avahi_domain, "Resetting group");
208
			avahi_entry_group_reset(avahi_group);
209 210
		}

211 212
		break;

213
	case AVAHI_CLIENT_S_REGISTERING:
214 215
		LogDebug(avahi_domain, "Client is REGISTERING");

216 217 218 219 220
		/* The server records are now being established. This
		 * might be caused by a host name change. We need to wait
		 * for our own records to register until the host name is
		 * properly esatblished. */

221
		if (avahi_group != nullptr) {
222
			LogDebug(avahi_domain, "Resetting group");
223
			avahi_entry_group_reset(avahi_group);
224 225 226 227 228
		}

		break;

	case AVAHI_CLIENT_CONNECTING:
229 230
		LogDebug(avahi_domain, "Client is CONNECTING");
		break;
231 232 233
	}
}

234
void
235
AvahiInit(EventLoop &loop, const char *serviceName)
236
{
237
	LogDebug(avahi_domain, "Initializing interface");
238 239

	if (!avahi_is_valid_service_name(serviceName))
240
		throw FormatRuntimeError("Invalid zeroconf_name \"%s\"", serviceName);
241

242
	avahi_name = avahi_strdup(serviceName);
243

244
	avahi_poll = new MyAvahiPoll(loop);
245

246
	int error;
247 248 249 250
	avahi_client = avahi_client_new(avahi_poll, AVAHI_CLIENT_NO_FAIL,
					MyAvahiClientCallback, nullptr,
					&error);
	if (avahi_client == nullptr) {
251 252
		FormatError(avahi_domain, "Failed to create client: %s",
			    avahi_strerror(error));
253
		AvahiDeinit();
254 255 256
	}
}

257
void
258
AvahiDeinit()
259
{
260
	LogDebug(avahi_domain, "Shutting down interface");
261

262 263 264
	if (avahi_group != nullptr) {
		avahi_entry_group_free(avahi_group);
		avahi_group = nullptr;
265 266
	}

267 268 269
	if (avahi_client != nullptr) {
		avahi_client_free(avahi_client);
		avahi_client = nullptr;
270 271
	}

272 273
	delete avahi_poll;
	avahi_poll = nullptr;
274

275 276
	avahi_free(avahi_name);
	avahi_name = nullptr;
277
}