ZeroconfAvahi.cxx 7.28 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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

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

32
#include <avahi-common/watch.h>
33 34 35 36 37
#include <avahi-common/alternative.h>
#include <avahi-common/domain.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>

38
#include <stddef.h>
39

40
static constexpr Domain avahi_domain("avahi");
41 42

static char *avahiName;
43
static bool avahi_running;
44
static MyAvahiPoll *avahi_poll;
45 46 47 48 49 50 51 52
static AvahiClient *avahiClient;
static AvahiEntryGroup *avahiGroup;

static void avahiRegisterService(AvahiClient * c);

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

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

	switch (state) {
	case AVAHI_ENTRY_GROUP_ESTABLISHED:
		/* The entry group has been established successfully */
64 65 66
		FormatDefault(avahi_domain,
			      "Service '%s' successfully established.",
			      avahiName);
67 68 69 70 71 72 73 74
		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;

75 76 77
		FormatDefault(avahi_domain,
			      "Service name collision, renaming service to '%s'",
			      avahiName);
78 79 80 81 82 83

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

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

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

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

106 107 108 109 110 111
	int ret;
	assert(c);

	/* If this is the first time we're called,
	 * let's create a new entry group */
	if (!avahiGroup) {
112
		avahiGroup = avahi_entry_group_new(c, avahiGroupCallback, nullptr);
113
		if (!avahiGroup) {
114 115 116
			FormatError(avahi_domain,
				    "Failed to create avahi EntryGroup: %s",
				    avahi_strerror(avahi_client_errno(c)));
117 118 119 120 121 122 123 124 125 126
			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,
127
					    AvahiPublishFlags(0),
128 129
					    avahiName, SERVICE_TYPE, nullptr,
					    nullptr, listen_port, nullptr);
130
	if (ret < 0) {
131 132
		FormatError(avahi_domain, "Failed to add service %s: %s",
			    SERVICE_TYPE, avahi_strerror(ret));
133 134 135 136 137 138
		goto fail;
	}

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

fail:
146
	avahi_running = false;
147 148 149 150
}

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

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

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

		/* 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) {
172 173
			LogDefault(avahi_domain,
				   "Client Disconnected, will reconnect shortly");
174 175
			if (avahiGroup) {
				avahi_entry_group_free(avahiGroup);
176
				avahiGroup = nullptr;
177 178 179 180
			}
			if (avahiClient)
				avahi_client_free(avahiClient);
			avahiClient =
181
			    avahi_client_new(avahi_poll,
182
					     AVAHI_CLIENT_NO_FAIL,
183
					     avahiClientCallback, nullptr,
184 185
					     &reason);
			if (!avahiClient) {
186 187 188
				FormatWarning(avahi_domain,
					      "Could not reconnect: %s",
					      avahi_strerror(reason));
189
				avahi_running = false;
190 191
			}
		} else {
192 193 194
			FormatWarning(avahi_domain,
				      "Client failure: %s (terminal)",
				      avahi_strerror(reason));
195
			avahi_running = false;
196 197 198 199
		}
		break;

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

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

210 211
		break;

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

215 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. */

		if (avahiGroup) {
221
			LogDebug(avahi_domain, "Resetting group");
222 223 224 225 226 227
			avahi_entry_group_reset(avahiGroup);
		}

		break;

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

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

	if (!avahi_is_valid_service_name(serviceName))
239
		FormatFatalError("Invalid zeroconf_name \"%s\"", serviceName);
240 241 242

	avahiName = avahi_strdup(serviceName);

243
	avahi_running = true;
244

245
	avahi_poll = new MyAvahiPoll(loop);
246

247
	int error;
248
	avahiClient = avahi_client_new(avahi_poll, AVAHI_CLIENT_NO_FAIL,
249
				       avahiClientCallback, nullptr, &error);
250 251

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

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

	if (avahiGroup) {
		avahi_entry_group_free(avahiGroup);
265
		avahiGroup = nullptr;
266 267 268 269
	}

	if (avahiClient) {
		avahi_client_free(avahiClient);
270
		avahiClient = nullptr;
271 272
	}

273 274
	delete avahi_poll;
	avahi_poll = nullptr;
275

276
	avahi_free(avahiName);
277
	avahiName = nullptr;
278
}