ZeroconfAvahi.cxx 7.23 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config.h"
21
#include "ZeroconfAvahi.hxx"
22
#include "AvahiPoll.hxx"
23
#include "ZeroconfInternal.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Listen.hxx"
25
#include "system/FatalError.hxx"
26 27
#include "util/Domain.hxx"
#include "Log.hxx"
28 29 30 31 32 33 34 35 36

#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>

37
static constexpr Domain avahi_domain("avahi");
38 39

static char *avahiName;
40
static bool avahi_running;
41
static MyAvahiPoll *avahi_poll;
42 43 44 45 46 47 48 49
static AvahiClient *avahiClient;
static AvahiEntryGroup *avahiGroup;

static void avahiRegisterService(AvahiClient * c);

/* Callback when the EntryGroup changes state */
static void avahiGroupCallback(AvahiEntryGroup * g,
			       AvahiEntryGroupState state,
50
			       gcc_unused void *userdata)
51 52 53 54
{
	char *n;
	assert(g);

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

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

	case AVAHI_ENTRY_GROUP_COLLISION:
		/* A service name collision happened. Let's pick a new name */
		n = avahi_alternative_service_name(avahiName);
		avahi_free(avahiName);
		avahiName = n;

72 73 74
		FormatDefault(avahi_domain,
			      "Service name collision, renaming service to '%s'",
			      avahiName);
75 76 77 78 79 80

		/* And recreate the services */
		avahiRegisterService(avahi_entry_group_get_client(g));
		break;

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

	case AVAHI_ENTRY_GROUP_UNCOMMITED:
90
		LogDebug(avahi_domain, "Service group is UNCOMMITED");
91 92
		break;
	case AVAHI_ENTRY_GROUP_REGISTERING:
93
		LogDebug(avahi_domain, "Service group is REGISTERING");
94 95 96 97 98 99
	}
}

/* Registers a new service with avahi */
static void avahiRegisterService(AvahiClient * c)
{
100 101 102
	FormatDebug(avahi_domain, "Registering service %s/%s",
		    SERVICE_TYPE, avahiName);

103 104 105 106 107 108
	int ret;
	assert(c);

	/* If this is the first time we're called,
	 * let's create a new entry group */
	if (!avahiGroup) {
109
		avahiGroup = avahi_entry_group_new(c, avahiGroupCallback, nullptr);
110
		if (!avahiGroup) {
111 112 113
			FormatError(avahi_domain,
				    "Failed to create avahi EntryGroup: %s",
				    avahi_strerror(avahi_client_errno(c)));
114 115 116 117 118 119 120 121 122 123
			goto fail;
		}
	}

	/* Add the service */
	/* TODO: This currently binds to ALL interfaces.
	 *       We could maybe add a service per actual bound interface,
	 *       if that's better. */
	ret = avahi_entry_group_add_service(avahiGroup,
					    AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
124
					    AvahiPublishFlags(0),
125 126
					    avahiName, SERVICE_TYPE, nullptr,
					    nullptr, listen_port, nullptr);
127
	if (ret < 0) {
128 129
		FormatError(avahi_domain, "Failed to add service %s: %s",
			    SERVICE_TYPE, avahi_strerror(ret));
130 131 132 133 134 135
		goto fail;
	}

	/* Tell the server to register the service group */
	ret = avahi_entry_group_commit(avahiGroup);
	if (ret < 0) {
136 137
		FormatError(avahi_domain, "Failed to commit service group: %s",
			    avahi_strerror(ret));
138 139 140 141 142
		goto fail;
	}
	return;

fail:
143
	avahi_running = false;
144 145 146 147
}

/* Callback when avahi changes state */
static void avahiClientCallback(AvahiClient * c, AvahiClientState state,
148
				gcc_unused void *userdata)
149 150 151 152 153
{
	int reason;
	assert(c);

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

	switch (state) {
	case AVAHI_CLIENT_S_RUNNING:
158
		LogDebug(avahi_domain, "Client is RUNNING");
159 160 161 162 163 164 165 166 167 168

		/* The server has startup successfully and registered its host
		 * name on the network, so it's time to create our services */
		if (!avahiGroup)
			avahiRegisterService(c);
		break;

	case AVAHI_CLIENT_FAILURE:
		reason = avahi_client_errno(c);
		if (reason == AVAHI_ERR_DISCONNECTED) {
169 170
			LogDefault(avahi_domain,
				   "Client Disconnected, will reconnect shortly");
171 172
			if (avahiGroup) {
				avahi_entry_group_free(avahiGroup);
173
				avahiGroup = nullptr;
174 175 176 177
			}
			if (avahiClient)
				avahi_client_free(avahiClient);
			avahiClient =
178
			    avahi_client_new(avahi_poll,
179
					     AVAHI_CLIENT_NO_FAIL,
180
					     avahiClientCallback, nullptr,
181 182
					     &reason);
			if (!avahiClient) {
183 184 185
				FormatWarning(avahi_domain,
					      "Could not reconnect: %s",
					      avahi_strerror(reason));
186
				avahi_running = false;
187 188
			}
		} else {
189 190 191
			FormatWarning(avahi_domain,
				      "Client failure: %s (terminal)",
				      avahi_strerror(reason));
192
			avahi_running = false;
193 194 195 196
		}
		break;

	case AVAHI_CLIENT_S_COLLISION:
197 198
		LogDebug(avahi_domain, "Client is COLLISION");

199 200 201 202
		/* 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 (avahiGroup) {
203
			LogDebug(avahi_domain, "Resetting group");
204 205 206
			avahi_entry_group_reset(avahiGroup);
		}

207 208
		break;

209
	case AVAHI_CLIENT_S_REGISTERING:
210 211
		LogDebug(avahi_domain, "Client is REGISTERING");

212 213 214 215 216 217
		/* 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. */

		if (avahiGroup) {
218
			LogDebug(avahi_domain, "Resetting group");
219 220 221 222 223 224
			avahi_entry_group_reset(avahiGroup);
		}

		break;

	case AVAHI_CLIENT_CONNECTING:
225 226
		LogDebug(avahi_domain, "Client is CONNECTING");
		break;
227 228 229
	}
}

230
void
231
AvahiInit(EventLoop &loop, const char *serviceName)
232
{
233
	LogDebug(avahi_domain, "Initializing interface");
234 235

	if (!avahi_is_valid_service_name(serviceName))
236
		FormatFatalError("Invalid zeroconf_name \"%s\"", serviceName);
237 238 239

	avahiName = avahi_strdup(serviceName);

240
	avahi_running = true;
241

242
	avahi_poll = new MyAvahiPoll(loop);
243

244
	int error;
245
	avahiClient = avahi_client_new(avahi_poll, AVAHI_CLIENT_NO_FAIL,
246
				       avahiClientCallback, nullptr, &error);
247 248

	if (!avahiClient) {
249 250
		FormatError(avahi_domain, "Failed to create client: %s",
			    avahi_strerror(error));
251
		AvahiDeinit();
252 253 254
	}
}

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

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

	if (avahiClient) {
		avahi_client_free(avahiClient);
267
		avahiClient = nullptr;
268 269
	}

270 271
	delete avahi_poll;
	avahi_poll = nullptr;
272

273
	avahi_free(avahiName);
274
	avahiName = nullptr;
275
}