1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <arpa/inet.h>
31 /* The dns-sd service type qualifier to publish */
32 #define SERVICE_TYPE "_mpd._tcp"
34 /* The default service name to publish
35 * (overridden by 'zeroconf_name' config parameter)
37 #define SERVICE_NAME "Music Player"
39 #define DEFAULT_ZEROCONF_ENABLED 1
41 static int zeroconfEnabled;
44 static struct ioOps zeroConfIo = {
51 static DNSServiceRef dnsReference;
54 /* Here is the implementation for Avahi (http://avahi.org) Zeroconf support */
57 #include <avahi-client/client.h>
58 #include <avahi-client/publish.h>
60 #include <avahi-common/alternative.h>
61 #include <avahi-common/domain.h>
62 #include <avahi-common/malloc.h>
63 #include <avahi-common/error.h>
65 /* Static avahi data */
66 static AvahiEntryGroup *avahiGroup;
67 static char *avahiName;
68 static AvahiClient* avahiClient;
69 static AvahiPoll avahiPoll;
70 static int avahiRunning;
72 static int avahiFdset( fd_set* rfds, fd_set* wfds, fd_set* efds );
73 static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds );
75 /* Forward Declaration */
76 static void avahiRegisterService(AvahiClient *c);
79 struct AvahiWatch* prev;
80 struct AvahiWatch* next;
82 AvahiWatchEvent requestedEvent;
83 AvahiWatchEvent observedEvent;
84 AvahiWatchCallback callback;
89 struct AvahiTimeout* prev;
90 struct AvahiTimeout* next;
91 struct timeval expiry;
93 AvahiTimeoutCallback callback;
97 static AvahiWatch* avahiWatchList;
98 static AvahiTimeout* avahiTimeoutList;
100 static AvahiWatch* avahiWatchNew( const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata )
102 struct AvahiWatch* newWatch = xmalloc( sizeof(struct AvahiWatch) );
105 newWatch->requestedEvent = event;
106 newWatch->observedEvent = 0;
107 newWatch->callback = callback;
108 newWatch->userdata = userdata;
110 /* Insert at front of list */
111 newWatch->next = avahiWatchList;
112 avahiWatchList = newWatch;
113 newWatch->prev = NULL;
115 newWatch->next->prev = newWatch;
120 static void avahiWatchUpdate( AvahiWatch *w, AvahiWatchEvent event )
123 w->requestedEvent = event;
126 static AvahiWatchEvent avahiWatchGetEvents( AvahiWatch *w )
129 return w->observedEvent;
132 static void avahiWatchFree( AvahiWatch *w )
136 if( avahiWatchList == w )
137 avahiWatchList = w->next;
138 else if( w->prev != NULL )
139 w->prev->next = w->next;
144 static void avahiCheckExpiry( AvahiTimeout *t )
149 gettimeofday( &now, NULL );
150 if( timercmp( &now, &(t->expiry), > ) ) {
152 t->callback( t, t->userdata );
157 static void avahiTimeoutUpdate( AvahiTimeout *t, const struct timeval *tv )
162 t->expiry.tv_sec = tv->tv_sec;
163 t->expiry.tv_usec = tv->tv_usec;
169 static void avahiTimeoutFree( AvahiTimeout *t )
173 if( avahiTimeoutList == t )
174 avahiTimeoutList = t->next;
175 else if( t->prev != NULL )
176 t->prev->next = t->next;
181 static AvahiTimeout* avahiTimeoutNew( const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata )
183 struct AvahiTimeout* newTimeout = xmalloc( sizeof(struct AvahiTimeout) );
185 newTimeout->callback = callback;
186 newTimeout->userdata = userdata;
188 avahiTimeoutUpdate( newTimeout, tv );
190 /* Insert at front of list */
191 newTimeout->next = avahiTimeoutList;
192 avahiTimeoutList = newTimeout;
193 newTimeout->prev = NULL;
194 if( newTimeout->next )
195 newTimeout->next->prev = newTimeout;
200 /* Callback when the EntryGroup changes state */
201 static void avahiGroupCallback(
203 AvahiEntryGroupState state,
208 DEBUG( "Avahi: Service group changed to state %d\n", state );
211 case AVAHI_ENTRY_GROUP_ESTABLISHED :
212 /* The entry group has been established successfully */
213 LOG( "Avahi: Service '%s' successfully established.\n", avahiName );
216 case AVAHI_ENTRY_GROUP_COLLISION : {
219 /* A service name collision happened. Let's pick a new name */
220 n = avahi_alternative_service_name(avahiName);
221 avahi_free(avahiName);
224 LOG( "Avahi: Service name collision, renaming service to '%s'\n", avahiName );
226 /* And recreate the services */
227 avahiRegisterService(avahi_entry_group_get_client(g));
231 case AVAHI_ENTRY_GROUP_FAILURE :
232 ERROR( "Avahi: Entry group failure: %s\n",
233 avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))) );
234 /* Some kind of failure happened while we were registering our services */
238 case AVAHI_ENTRY_GROUP_UNCOMMITED:
239 DEBUG( "Avahi: Service group is UNCOMMITED\n" );
241 case AVAHI_ENTRY_GROUP_REGISTERING:
242 DEBUG( "Avahi: Service group is REGISTERING\n" );
247 /* Registers a new service with avahi */
248 static void avahiRegisterService(AvahiClient *c)
252 DEBUG( "Avahi: Registering service %s/%s\n", SERVICE_TYPE, avahiName );
254 /* If this is the first time we're called, let's create a new entry group */
256 avahiGroup = avahi_entry_group_new(c, avahiGroupCallback, NULL);
258 ERROR( "Avahi: Failed to create avahi EntryGroup: %s\n", avahi_strerror(avahi_client_errno(c)) );
263 /* Add the service */
264 /* TODO: This currently binds to ALL interfaces.
265 * We could maybe add a service per actual bound interface, if that's better. */
266 ret = avahi_entry_group_add_service(avahiGroup,
267 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
268 avahiName, SERVICE_TYPE,
273 ERROR( "Avahi: Failed to add service %s: %s\n", SERVICE_TYPE, avahi_strerror(ret) );
277 /* Tell the server to register the service group */
278 ret = avahi_entry_group_commit(avahiGroup);
280 ERROR( "Avahi: Failed to commit service group: %s\n", avahi_strerror(ret) );
289 /* Callback when avahi changes state */
290 static void avahiClientCallback(AvahiClient *c, AvahiClientState state, void *userdata)
294 /* Called whenever the client or server state changes */
295 DEBUG( "Avahi: Client changed to state %d\n", state );
298 case AVAHI_CLIENT_S_RUNNING:
299 DEBUG( "Avahi: Client is RUNNING\n" );
301 /* The server has startup successfully and registered its host
302 * name on the network, so it's time to create our services */
304 avahiRegisterService(c);
307 case AVAHI_CLIENT_FAILURE:
309 int reason = avahi_client_errno(c);
310 if( reason == AVAHI_ERR_DISCONNECTED ) {
311 LOG( "Avahi: Client Disconnected, will reconnect shortly\n");
312 avahi_entry_group_free( avahiGroup );
314 avahi_client_free( avahiClient );
316 avahiClient = avahi_client_new( &avahiPoll, AVAHI_CLIENT_NO_FAIL,
317 avahiClientCallback, NULL, &reason );
319 ERROR( "Avahi: Could not reconnect: %s\n", avahi_strerror(reason) );
323 ERROR( "Avahi: Client failure: %s (terminal)\n", avahi_strerror(reason));
329 case AVAHI_CLIENT_S_COLLISION:
330 DEBUG( "Avahi: Client is COLLISION\n" );
331 /* Let's drop our registered services. When the server is back
332 * in AVAHI_SERVER_RUNNING state we will register them
333 * again with the new host name. */
335 DEBUG( "Avahi: Resetting group\n" );
336 avahi_entry_group_reset(avahiGroup);
339 case AVAHI_CLIENT_S_REGISTERING:
340 DEBUG( "Avahi: Client is REGISTERING\n" );
341 /* The server records are now being established. This
342 * might be caused by a host name change. We need to wait
343 * for our own records to register until the host name is
344 * properly esatblished. */
347 DEBUG( "Avahi: Resetting group\n" );
348 avahi_entry_group_reset(avahiGroup);
353 case AVAHI_CLIENT_CONNECTING:
354 DEBUG( "Avahi: Client is CONNECTING\n" );
359 static int avahiFdset( fd_set* rfds, fd_set* wfds, fd_set* efds )
365 for( w = avahiWatchList; w != NULL; w = w->next ) {
366 if( w->requestedEvent & AVAHI_WATCH_IN ) {
367 FD_SET( w->fd, rfds );
369 if( w->requestedEvent & AVAHI_WATCH_OUT ) {
370 FD_SET( w->fd, wfds );
372 if( w->requestedEvent & AVAHI_WATCH_ERR ) {
373 FD_SET( w->fd, efds );
375 if( w->requestedEvent & AVAHI_WATCH_HUP ) {
376 ERROR( "Avahi: No support for HUP events! (ignoring)\n" );
385 static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds )
387 int retval = fdCount;
389 AvahiWatch* w = avahiWatchList;
391 while( w != NULL && retval > 0 ) {
392 AvahiWatch* current = w;
393 current->observedEvent = 0;
394 if( FD_ISSET( current->fd, rfds ) ) {
395 current->observedEvent |= AVAHI_WATCH_IN;
396 FD_CLR( current->fd, rfds );
399 if( FD_ISSET( current->fd, wfds ) ) {
400 current->observedEvent |= AVAHI_WATCH_OUT;
401 FD_CLR( current->fd, wfds );
404 if( FD_ISSET( current->fd, efds ) ) {
405 current->observedEvent |= AVAHI_WATCH_ERR;
406 FD_CLR( current->fd, efds );
410 /* Advance to the next one right now, in case the callback
415 if( current->observedEvent && avahiRunning ) {
416 current->callback( current, current->fd,
417 current->observedEvent, current->userdata );
421 t = avahiTimeoutList;
422 while( t != NULL && avahiRunning ) {
423 AvahiTimeout* current = t;
425 /* Advance to the next one right now, in case the callback
429 avahiCheckExpiry( current );
435 static void init_avahi(const char *serviceName)
438 DEBUG( "Avahi: Initializing interface\n" );
440 if( avahi_is_valid_service_name( serviceName ) ) {
441 avahiName = avahi_strdup( serviceName );
443 ERROR( "Invalid zeroconf_name \"%s\", defaulting to \"%s\" instead.\n", serviceName, SERVICE_NAME );
444 avahiName = avahi_strdup( SERVICE_NAME );
449 avahiPoll.userdata = NULL;
450 avahiPoll.watch_new = avahiWatchNew;
451 avahiPoll.watch_update = avahiWatchUpdate;
452 avahiPoll.watch_get_events = avahiWatchGetEvents;
453 avahiPoll.watch_free = avahiWatchFree;
454 avahiPoll.timeout_new = avahiTimeoutNew;
455 avahiPoll.timeout_update = avahiTimeoutUpdate;
456 avahiPoll.timeout_free = avahiTimeoutFree;
458 avahiClient = avahi_client_new( &avahiPoll, AVAHI_CLIENT_NO_FAIL,
459 avahiClientCallback, NULL, &error );
462 ERROR( "Avahi: Failed to create client: %s\n", avahi_strerror(error) );
466 zeroConfIo.fdset = avahiFdset;
467 zeroConfIo.consume = avahiFdconsume;
468 registerIO( &zeroConfIo );
475 #endif /* HAVE_AVAHI */
478 static int dnsRegisterFdset(fd_set* rfds, fd_set* wfds, fd_set* efds)
482 if (dnsReference == NULL)
485 fd = DNSServiceRefSockFD(dnsReference);
494 static int dnsRegisterFdconsume(int fdCount, fd_set* rfds, fd_set* wfds,
499 if (dnsReference == NULL)
502 fd = DNSServiceRefSockFD(dnsReference);
506 if (FD_ISSET(fd, rfds)) {
509 DNSServiceProcessResult(dnsReference);
517 static void dnsRegisterCallback (DNSServiceRef sdRef, DNSServiceFlags flags,
518 DNSServiceErrorType errorCode, const char *name,
519 const char *regtype, const char *domain, void *context)
521 if (errorCode != kDNSServiceErr_NoError) {
522 ERROR("Failed to register zeroconf service.\n");
524 DNSServiceRefDeallocate(dnsReference);
526 deregisterIO( &zeroConfIo );
528 DEBUG("Registered zeroconf service with name '%s'\n", name);
532 static void init_zeroconf_osx(const char *serviceName)
534 DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
535 0, 0, serviceName, SERVICE_TYPE, NULL, NULL, htons(boundPort), 0,
536 NULL, dnsRegisterCallback, NULL);
538 if (error != kDNSServiceErr_NoError) {
539 ERROR("Failed to register zeroconf service.\n");
542 DNSServiceRefDeallocate(dnsReference);
548 zeroConfIo.fdset = dnsRegisterFdset;
549 zeroConfIo.consume = dnsRegisterFdconsume;
550 registerIO( &zeroConfIo );
554 void initZeroconf(void)
556 const char *serviceName = SERVICE_NAME;
559 zeroconfEnabled = getBoolConfigParam(CONF_ZEROCONF_ENABLED);
560 if (zeroconfEnabled == -1)
561 zeroconfEnabled = DEFAULT_ZEROCONF_ENABLED;
562 else if (zeroconfEnabled < 0)
565 if (!zeroconfEnabled)
568 param = getConfigParam(CONF_ZEROCONF_NAME);
570 if (param && strlen(param->value) > 0)
571 serviceName = param->value;
574 init_avahi(serviceName);
578 init_zeroconf_osx(serviceName);
582 void finishZeroconf(void)
584 if (!zeroconfEnabled)
588 DEBUG( "Avahi: Shutting down interface\n" );
589 deregisterIO( &zeroConfIo );
592 avahi_entry_group_free( avahiGroup );
597 avahi_client_free( avahiClient );
601 avahi_free( avahiName );
603 #endif /* HAVE_AVAHI */
606 deregisterIO( &zeroConfIo );
607 if (dnsReference != NULL) {
608 DNSServiceRefDeallocate(dnsReference);
610 DEBUG("Deregistered Zeroconf service.\n");