zeroconf: reformat (with mpd-indent.sh + manual tweaks)
Also, lower the impact of compiling this w/o zeroconf by making the init/teardown functions static no-ops. Eventually, we should separate the Bonjour and Avahi code into separate files and have callbacks registered for each one, avoiding the #ifdef mess we have now... git-svn-id: https://svn.musicpd.org/mpd/trunk@7132 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
@@ -811,23 +811,23 @@ static void printInterfaceOutBuffer(Interface * interface)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* From ioops.h: */
|
/* From ioops.h: */
|
||||||
void registerIO( struct ioOps *ops )
|
void registerIO(struct ioOps *ops)
|
||||||
{
|
{
|
||||||
assert( ops != NULL );
|
assert(ops != NULL);
|
||||||
|
|
||||||
ops->next = ioList;
|
ops->next = ioList;
|
||||||
ioList = ops;
|
ioList = ops;
|
||||||
ops->prev = NULL;
|
ops->prev = NULL;
|
||||||
if( ops->next )
|
if (ops->next)
|
||||||
ops->next->prev = ops;
|
ops->next->prev = ops;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deregisterIO( struct ioOps *ops )
|
void deregisterIO(struct ioOps *ops)
|
||||||
{
|
{
|
||||||
assert( ops != NULL );
|
assert(ops != NULL);
|
||||||
|
|
||||||
if( ioList == ops )
|
if (ioList == ops)
|
||||||
ioList = ops->next;
|
ioList = ops->next;
|
||||||
else if( ops->prev != NULL )
|
else if (ops->prev != NULL)
|
||||||
ops->prev->next = ops->next;
|
ops->prev->next = ops->next;
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +29,7 @@ struct ioOps {
|
|||||||
* To register for IO, call FD_SET for each required queue
|
* To register for IO, call FD_SET for each required queue
|
||||||
* Return the highest fd number you registered
|
* Return the highest fd number you registered
|
||||||
*/
|
*/
|
||||||
int (*fdset) ( fd_set *rfds, fd_set *wfds, fd_set *efds );
|
int (*fdset) (fd_set * rfds, fd_set * wfds, fd_set * efds);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Called after each 'select' statement.
|
* Called after each 'select' statement.
|
||||||
@@ -39,13 +39,14 @@ struct ioOps {
|
|||||||
* Return the total number of fds left in all sets (Ie, return fdCount
|
* Return the total number of fds left in all sets (Ie, return fdCount
|
||||||
* minus the number of times you called FD_CLR).
|
* minus the number of times you called FD_CLR).
|
||||||
*/
|
*/
|
||||||
int (*consume) ( int fdCount, fd_set *rfds, fd_set *wfds, fd_set *efds );
|
int (*consume) (int fdCount, fd_set * rfds, fd_set * wfds,
|
||||||
|
fd_set * efds);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Call this to register your io operation handler struct */
|
/* Call this to register your io operation handler struct */
|
||||||
void registerIO( struct ioOps *ops );
|
void registerIO(struct ioOps *ops);
|
||||||
|
|
||||||
/* Call this to deregister your io operation handler struct */
|
/* Call this to deregister your io operation handler struct */
|
||||||
void deregisterIO( struct ioOps *ops );
|
void deregisterIO(struct ioOps *ops);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
454
src/zeroconf.c
454
src/zeroconf.c
@@ -16,8 +16,12 @@
|
|||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "os_compat.h"
|
#include "../config.h"
|
||||||
|
|
||||||
|
#ifdef HAVE_ZEROCONF
|
||||||
|
|
||||||
#include "zeroconf.h"
|
#include "zeroconf.h"
|
||||||
|
#include "os_compat.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "listen.h"
|
#include "listen.h"
|
||||||
@@ -27,7 +31,7 @@
|
|||||||
/* The dns-sd service type qualifier to publish */
|
/* The dns-sd service type qualifier to publish */
|
||||||
#define SERVICE_TYPE "_mpd._tcp"
|
#define SERVICE_TYPE "_mpd._tcp"
|
||||||
|
|
||||||
/* The default service name to publish
|
/* The default service name to publish
|
||||||
* (overridden by 'zeroconf_name' config parameter)
|
* (overridden by 'zeroconf_name' config parameter)
|
||||||
*/
|
*/
|
||||||
#define SERVICE_NAME "Music Player"
|
#define SERVICE_NAME "Music Player"
|
||||||
@@ -35,11 +39,7 @@
|
|||||||
#define DEFAULT_ZEROCONF_ENABLED 1
|
#define DEFAULT_ZEROCONF_ENABLED 1
|
||||||
|
|
||||||
static int zeroconfEnabled;
|
static int zeroconfEnabled;
|
||||||
|
static struct ioOps zeroConfIo;
|
||||||
#ifdef HAVE_ZEROCONF
|
|
||||||
static struct ioOps zeroConfIo = {
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HAVE_BONJOUR
|
#ifdef HAVE_BONJOUR
|
||||||
#include <dns_sd.h>
|
#include <dns_sd.h>
|
||||||
@@ -61,41 +61,44 @@ static DNSServiceRef dnsReference;
|
|||||||
/* Static avahi data */
|
/* Static avahi data */
|
||||||
static AvahiEntryGroup *avahiGroup;
|
static AvahiEntryGroup *avahiGroup;
|
||||||
static char *avahiName;
|
static char *avahiName;
|
||||||
static AvahiClient* avahiClient;
|
static AvahiClient *avahiClient;
|
||||||
static AvahiPoll avahiPoll;
|
static AvahiPoll avahiPoll;
|
||||||
static int avahiRunning;
|
static int avahiRunning;
|
||||||
|
|
||||||
static int avahiFdset( fd_set* rfds, fd_set* wfds, fd_set* efds );
|
static int avahiFdset(fd_set * rfds, fd_set * wfds, fd_set * efds);
|
||||||
static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds );
|
static int avahiFdconsume(int fdCount, fd_set * rfds, fd_set * wfds,
|
||||||
|
fd_set * efds);
|
||||||
|
|
||||||
/* Forward Declaration */
|
/* Forward Declaration */
|
||||||
static void avahiRegisterService(AvahiClient *c);
|
static void avahiRegisterService(AvahiClient * c);
|
||||||
|
|
||||||
struct AvahiWatch {
|
struct AvahiWatch {
|
||||||
struct AvahiWatch* prev;
|
struct AvahiWatch *prev;
|
||||||
struct AvahiWatch* next;
|
struct AvahiWatch *next;
|
||||||
int fd;
|
int fd;
|
||||||
AvahiWatchEvent requestedEvent;
|
AvahiWatchEvent requestedEvent;
|
||||||
AvahiWatchEvent observedEvent;
|
AvahiWatchEvent observedEvent;
|
||||||
AvahiWatchCallback callback;
|
AvahiWatchCallback callback;
|
||||||
void* userdata;
|
void *userdata;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AvahiTimeout {
|
struct AvahiTimeout {
|
||||||
struct AvahiTimeout* prev;
|
struct AvahiTimeout *prev;
|
||||||
struct AvahiTimeout* next;
|
struct AvahiTimeout *next;
|
||||||
struct timeval expiry;
|
struct timeval expiry;
|
||||||
int enabled;
|
int enabled;
|
||||||
AvahiTimeoutCallback callback;
|
AvahiTimeoutCallback callback;
|
||||||
void* userdata;
|
void *userdata;
|
||||||
};
|
};
|
||||||
|
|
||||||
static AvahiWatch* avahiWatchList;
|
static AvahiWatch *avahiWatchList;
|
||||||
static AvahiTimeout* avahiTimeoutList;
|
static AvahiTimeout *avahiTimeoutList;
|
||||||
|
|
||||||
static AvahiWatch* avahiWatchNew( const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata )
|
static AvahiWatch *avahiWatchNew(const AvahiPoll * api, int fd,
|
||||||
|
AvahiWatchEvent event,
|
||||||
|
AvahiWatchCallback callback, void *userdata)
|
||||||
{
|
{
|
||||||
struct AvahiWatch* newWatch = xmalloc( sizeof(struct AvahiWatch) );
|
struct AvahiWatch *newWatch = xmalloc(sizeof(struct AvahiWatch));
|
||||||
|
|
||||||
newWatch->fd = fd;
|
newWatch->fd = fd;
|
||||||
newWatch->requestedEvent = event;
|
newWatch->requestedEvent = event;
|
||||||
@@ -107,53 +110,53 @@ static AvahiWatch* avahiWatchNew( const AvahiPoll *api, int fd, AvahiWatchEvent
|
|||||||
newWatch->next = avahiWatchList;
|
newWatch->next = avahiWatchList;
|
||||||
avahiWatchList = newWatch;
|
avahiWatchList = newWatch;
|
||||||
newWatch->prev = NULL;
|
newWatch->prev = NULL;
|
||||||
if( newWatch->next )
|
if (newWatch->next)
|
||||||
newWatch->next->prev = newWatch;
|
newWatch->next->prev = newWatch;
|
||||||
|
|
||||||
return newWatch;
|
return newWatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void avahiWatchUpdate( AvahiWatch *w, AvahiWatchEvent event )
|
static void avahiWatchUpdate(AvahiWatch * w, AvahiWatchEvent event)
|
||||||
{
|
{
|
||||||
assert( w != NULL );
|
assert(w != NULL);
|
||||||
w->requestedEvent = event;
|
w->requestedEvent = event;
|
||||||
}
|
}
|
||||||
|
|
||||||
static AvahiWatchEvent avahiWatchGetEvents( AvahiWatch *w )
|
static AvahiWatchEvent avahiWatchGetEvents(AvahiWatch * w)
|
||||||
{
|
{
|
||||||
assert( w != NULL );
|
assert(w != NULL);
|
||||||
return w->observedEvent;
|
return w->observedEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void avahiWatchFree( AvahiWatch *w )
|
static void avahiWatchFree(AvahiWatch * w)
|
||||||
{
|
{
|
||||||
assert( w != NULL );
|
assert(w != NULL);
|
||||||
|
|
||||||
if( avahiWatchList == w )
|
if (avahiWatchList == w)
|
||||||
avahiWatchList = w->next;
|
avahiWatchList = w->next;
|
||||||
else if( w->prev != NULL )
|
else if (w->prev != NULL)
|
||||||
w->prev->next = w->next;
|
w->prev->next = w->next;
|
||||||
|
|
||||||
free( w );
|
free(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void avahiCheckExpiry( AvahiTimeout *t )
|
static void avahiCheckExpiry(AvahiTimeout * t)
|
||||||
{
|
{
|
||||||
assert( t != NULL );
|
assert(t != NULL);
|
||||||
if( t->enabled ) {
|
if (t->enabled) {
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
gettimeofday( &now, NULL );
|
gettimeofday(&now, NULL);
|
||||||
if( timercmp( &now, &(t->expiry), > ) ) {
|
if (timercmp(&now, &(t->expiry), >)) {
|
||||||
t->enabled = 0;
|
t->enabled = 0;
|
||||||
t->callback( t, t->userdata );
|
t->callback(t, t->userdata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void avahiTimeoutUpdate( AvahiTimeout *t, const struct timeval *tv )
|
static void avahiTimeoutUpdate(AvahiTimeout * t, const struct timeval *tv)
|
||||||
{
|
{
|
||||||
assert( t != NULL );
|
assert(t != NULL);
|
||||||
if( tv ) {
|
if (tv) {
|
||||||
t->enabled = 1;
|
t->enabled = 1;
|
||||||
t->expiry.tv_sec = tv->tv_sec;
|
t->expiry.tv_sec = tv->tv_sec;
|
||||||
t->expiry.tv_usec = tv->tv_usec;
|
t->expiry.tv_usec = tv->tv_usec;
|
||||||
@@ -162,118 +165,122 @@ static void avahiTimeoutUpdate( AvahiTimeout *t, const struct timeval *tv )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void avahiTimeoutFree( AvahiTimeout *t )
|
static void avahiTimeoutFree(AvahiTimeout * t)
|
||||||
{
|
{
|
||||||
assert( t != NULL );
|
assert(t != NULL);
|
||||||
|
|
||||||
if( avahiTimeoutList == t )
|
if (avahiTimeoutList == t)
|
||||||
avahiTimeoutList = t->next;
|
avahiTimeoutList = t->next;
|
||||||
else if( t->prev != NULL )
|
else if (t->prev != NULL)
|
||||||
t->prev->next = t->next;
|
t->prev->next = t->next;
|
||||||
|
|
||||||
free( t );
|
free(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static AvahiTimeout* avahiTimeoutNew( const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata )
|
static AvahiTimeout *avahiTimeoutNew(const AvahiPoll * api,
|
||||||
|
const struct timeval *tv,
|
||||||
|
AvahiTimeoutCallback callback,
|
||||||
|
void *userdata)
|
||||||
{
|
{
|
||||||
struct AvahiTimeout* newTimeout = xmalloc( sizeof(struct AvahiTimeout) );
|
struct AvahiTimeout *newTimeout = xmalloc(sizeof(struct AvahiTimeout));
|
||||||
|
|
||||||
newTimeout->callback = callback;
|
newTimeout->callback = callback;
|
||||||
newTimeout->userdata = userdata;
|
newTimeout->userdata = userdata;
|
||||||
|
|
||||||
avahiTimeoutUpdate( newTimeout, tv );
|
avahiTimeoutUpdate(newTimeout, tv);
|
||||||
|
|
||||||
/* Insert at front of list */
|
/* Insert at front of list */
|
||||||
newTimeout->next = avahiTimeoutList;
|
newTimeout->next = avahiTimeoutList;
|
||||||
avahiTimeoutList = newTimeout;
|
avahiTimeoutList = newTimeout;
|
||||||
newTimeout->prev = NULL;
|
newTimeout->prev = NULL;
|
||||||
if( newTimeout->next )
|
if (newTimeout->next)
|
||||||
newTimeout->next->prev = newTimeout;
|
newTimeout->next->prev = newTimeout;
|
||||||
|
|
||||||
return newTimeout;
|
return newTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Callback when the EntryGroup changes state */
|
/* Callback when the EntryGroup changes state */
|
||||||
static void avahiGroupCallback(
|
static void avahiGroupCallback(AvahiEntryGroup * g,
|
||||||
AvahiEntryGroup *g,
|
AvahiEntryGroupState state, void *userdata)
|
||||||
AvahiEntryGroupState state,
|
|
||||||
void *userdata)
|
|
||||||
{
|
{
|
||||||
|
char *n;
|
||||||
assert(g);
|
assert(g);
|
||||||
|
|
||||||
DEBUG( "Avahi: Service group changed to state %d\n", state );
|
DEBUG("Avahi: Service group changed to state %d\n", state);
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case AVAHI_ENTRY_GROUP_ESTABLISHED :
|
case AVAHI_ENTRY_GROUP_ESTABLISHED:
|
||||||
/* The entry group has been established successfully */
|
/* The entry group has been established successfully */
|
||||||
LOG( "Avahi: Service '%s' successfully established.\n", avahiName );
|
LOG("Avahi: Service '%s' successfully established.\n",
|
||||||
break;
|
avahiName);
|
||||||
|
break;
|
||||||
|
|
||||||
case AVAHI_ENTRY_GROUP_COLLISION : {
|
case AVAHI_ENTRY_GROUP_COLLISION:
|
||||||
char *n;
|
/* A service name collision happened. Let's pick a new name */
|
||||||
|
n = avahi_alternative_service_name(avahiName);
|
||||||
/* A service name collision happened. Let's pick a new name */
|
avahi_free(avahiName);
|
||||||
n = avahi_alternative_service_name(avahiName);
|
avahiName = n;
|
||||||
avahi_free(avahiName);
|
|
||||||
avahiName = n;
|
|
||||||
|
|
||||||
LOG( "Avahi: Service name collision, renaming service to '%s'\n", avahiName );
|
|
||||||
|
|
||||||
/* And recreate the services */
|
LOG("Avahi: Service name collision, renaming service to '%s'\n",
|
||||||
avahiRegisterService(avahi_entry_group_get_client(g));
|
avahiName);
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case AVAHI_ENTRY_GROUP_FAILURE :
|
/* And recreate the services */
|
||||||
ERROR( "Avahi: Entry group failure: %s\n",
|
avahiRegisterService(avahi_entry_group_get_client(g));
|
||||||
avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))) );
|
break;
|
||||||
/* Some kind of failure happened while we were registering our services */
|
|
||||||
avahiRunning = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AVAHI_ENTRY_GROUP_UNCOMMITED:
|
case AVAHI_ENTRY_GROUP_FAILURE:
|
||||||
DEBUG( "Avahi: Service group is UNCOMMITED\n" );
|
ERROR("Avahi: Entry group failure: %s\n",
|
||||||
break;
|
avahi_strerror(avahi_client_errno
|
||||||
case AVAHI_ENTRY_GROUP_REGISTERING:
|
(avahi_entry_group_get_client(g))));
|
||||||
DEBUG( "Avahi: Service group is REGISTERING\n" );
|
/* Some kind of failure happened while we were registering our services */
|
||||||
;
|
avahiRunning = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AVAHI_ENTRY_GROUP_UNCOMMITED:
|
||||||
|
DEBUG("Avahi: Service group is UNCOMMITED\n");
|
||||||
|
break;
|
||||||
|
case AVAHI_ENTRY_GROUP_REGISTERING:
|
||||||
|
DEBUG("Avahi: Service group is REGISTERING\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Registers a new service with avahi */
|
/* Registers a new service with avahi */
|
||||||
static void avahiRegisterService(AvahiClient *c)
|
static void avahiRegisterService(AvahiClient * c)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
assert(c);
|
assert(c);
|
||||||
DEBUG( "Avahi: Registering service %s/%s\n", SERVICE_TYPE, avahiName );
|
DEBUG("Avahi: Registering service %s/%s\n", SERVICE_TYPE, avahiName);
|
||||||
|
|
||||||
/* If this is the first time we're called, let's create a new entry group */
|
/* If this is the first time we're called,
|
||||||
|
* let's create a new entry group */
|
||||||
if (!avahiGroup) {
|
if (!avahiGroup) {
|
||||||
avahiGroup = avahi_entry_group_new(c, avahiGroupCallback, NULL);
|
avahiGroup = avahi_entry_group_new(c, avahiGroupCallback, NULL);
|
||||||
if( !avahiGroup ) {
|
if (!avahiGroup) {
|
||||||
ERROR( "Avahi: Failed to create avahi EntryGroup: %s\n", avahi_strerror(avahi_client_errno(c)) );
|
ERROR("Avahi: Failed to create avahi EntryGroup: %s\n",
|
||||||
|
avahi_strerror(avahi_client_errno(c)));
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add the service */
|
/* Add the service */
|
||||||
/* TODO: This currently binds to ALL interfaces.
|
/* TODO: This currently binds to ALL interfaces.
|
||||||
* We could maybe add a service per actual bound interface, if that's better. */
|
* We could maybe add a service per actual bound interface,
|
||||||
|
* if that's better. */
|
||||||
ret = avahi_entry_group_add_service(avahiGroup,
|
ret = avahi_entry_group_add_service(avahiGroup,
|
||||||
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
|
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
|
||||||
avahiName, SERVICE_TYPE,
|
0, avahiName, SERVICE_TYPE, NULL,
|
||||||
NULL, NULL,
|
NULL, boundPort, NULL);
|
||||||
boundPort,
|
if (ret < 0) {
|
||||||
NULL);
|
ERROR("Avahi: Failed to add service %s: %s\n", SERVICE_TYPE,
|
||||||
if( ret < 0 ) {
|
avahi_strerror(ret));
|
||||||
ERROR( "Avahi: Failed to add service %s: %s\n", SERVICE_TYPE, avahi_strerror(ret) );
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Tell the server to register the service group */
|
/* Tell the server to register the service group */
|
||||||
ret = avahi_entry_group_commit(avahiGroup);
|
ret = avahi_entry_group_commit(avahiGroup);
|
||||||
if( ret < 0 ) {
|
if (ret < 0) {
|
||||||
ERROR( "Avahi: Failed to commit service group: %s\n", avahi_strerror(ret) );
|
ERROR("Avahi: Failed to commit service group: %s\n",
|
||||||
|
avahi_strerror(ret));
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -283,125 +290,131 @@ fail:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Callback when avahi changes state */
|
/* Callback when avahi changes state */
|
||||||
static void avahiClientCallback(AvahiClient *c, AvahiClientState state, void *userdata)
|
static void avahiClientCallback(AvahiClient * c, AvahiClientState state,
|
||||||
|
void *userdata)
|
||||||
{
|
{
|
||||||
|
int reason;
|
||||||
assert(c);
|
assert(c);
|
||||||
|
|
||||||
/* Called whenever the client or server state changes */
|
/* Called whenever the client or server state changes */
|
||||||
DEBUG( "Avahi: Client changed to state %d\n", state );
|
DEBUG("Avahi: Client changed to state %d\n", state);
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case AVAHI_CLIENT_S_RUNNING:
|
case AVAHI_CLIENT_S_RUNNING:
|
||||||
DEBUG( "Avahi: Client is RUNNING\n" );
|
DEBUG("Avahi: Client is RUNNING\n");
|
||||||
|
|
||||||
/* 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:
|
/* The server has startup successfully and registered its host
|
||||||
{
|
* name on the network, so it's time to create our services */
|
||||||
int reason = avahi_client_errno(c);
|
if (!avahiGroup)
|
||||||
if( reason == AVAHI_ERR_DISCONNECTED ) {
|
avahiRegisterService(c);
|
||||||
LOG( "Avahi: Client Disconnected, will reconnect shortly\n");
|
break;
|
||||||
if (avahiGroup) {
|
|
||||||
avahi_entry_group_free(avahiGroup);
|
|
||||||
avahiGroup = NULL;
|
|
||||||
}
|
|
||||||
if (avahiClient)
|
|
||||||
avahi_client_free(avahiClient);
|
|
||||||
avahiClient = avahi_client_new( &avahiPoll, AVAHI_CLIENT_NO_FAIL,
|
|
||||||
avahiClientCallback, NULL, &reason );
|
|
||||||
if( !avahiClient ) {
|
|
||||||
ERROR( "Avahi: Could not reconnect: %s\n", avahi_strerror(reason) );
|
|
||||||
avahiRunning = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ERROR( "Avahi: Client failure: %s (terminal)\n", avahi_strerror(reason));
|
|
||||||
avahiRunning = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AVAHI_CLIENT_S_COLLISION:
|
case AVAHI_CLIENT_FAILURE:
|
||||||
DEBUG( "Avahi: Client is COLLISION\n" );
|
reason = avahi_client_errno(c);
|
||||||
/* Let's drop our registered services. When the server is back
|
if (reason == AVAHI_ERR_DISCONNECTED) {
|
||||||
* in AVAHI_SERVER_RUNNING state we will register them
|
LOG("Avahi: Client Disconnected, "
|
||||||
* again with the new host name. */
|
"will reconnect shortly\n");
|
||||||
if (avahiGroup) {
|
if (avahiGroup) {
|
||||||
DEBUG( "Avahi: Resetting group\n" );
|
avahi_entry_group_free(avahiGroup);
|
||||||
avahi_entry_group_reset(avahiGroup);
|
avahiGroup = NULL;
|
||||||
}
|
}
|
||||||
|
if (avahiClient)
|
||||||
case AVAHI_CLIENT_S_REGISTERING:
|
avahi_client_free(avahiClient);
|
||||||
DEBUG( "Avahi: Client is REGISTERING\n" );
|
avahiClient =
|
||||||
/* The server records are now being established. This
|
avahi_client_new(&avahiPoll,
|
||||||
* might be caused by a host name change. We need to wait
|
AVAHI_CLIENT_NO_FAIL,
|
||||||
* for our own records to register until the host name is
|
avahiClientCallback, NULL,
|
||||||
* properly esatblished. */
|
&reason);
|
||||||
|
if (!avahiClient) {
|
||||||
if (avahiGroup) {
|
ERROR("Avahi: Could not reconnect: %s\n",
|
||||||
DEBUG( "Avahi: Resetting group\n" );
|
avahi_strerror(reason));
|
||||||
avahi_entry_group_reset(avahiGroup);
|
avahiRunning = 0;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
break;
|
ERROR("Avahi: Client failure: %s (terminal)\n",
|
||||||
|
avahi_strerror(reason));
|
||||||
|
avahiRunning = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case AVAHI_CLIENT_CONNECTING:
|
case AVAHI_CLIENT_S_COLLISION:
|
||||||
DEBUG( "Avahi: Client is CONNECTING\n" );
|
DEBUG("Avahi: Client is COLLISION\n");
|
||||||
;
|
/* 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) {
|
||||||
|
DEBUG("Avahi: Resetting group\n");
|
||||||
|
avahi_entry_group_reset(avahiGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
case AVAHI_CLIENT_S_REGISTERING:
|
||||||
|
DEBUG("Avahi: Client is REGISTERING\n");
|
||||||
|
/* 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) {
|
||||||
|
DEBUG("Avahi: Resetting group\n");
|
||||||
|
avahi_entry_group_reset(avahiGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AVAHI_CLIENT_CONNECTING:
|
||||||
|
DEBUG("Avahi: Client is CONNECTING\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int avahiFdset( fd_set* rfds, fd_set* wfds, fd_set* efds )
|
static int avahiFdset(fd_set * rfds, fd_set * wfds, fd_set * efds)
|
||||||
{
|
{
|
||||||
AvahiWatch* w;
|
AvahiWatch *w;
|
||||||
int maxfd = -1;
|
int maxfd = -1;
|
||||||
if( !avahiRunning )
|
if (!avahiRunning)
|
||||||
return maxfd;
|
return maxfd;
|
||||||
for( w = avahiWatchList; w != NULL; w = w->next ) {
|
for (w = avahiWatchList; w != NULL; w = w->next) {
|
||||||
if( w->requestedEvent & AVAHI_WATCH_IN ) {
|
if (w->requestedEvent & AVAHI_WATCH_IN) {
|
||||||
FD_SET( w->fd, rfds );
|
FD_SET(w->fd, rfds);
|
||||||
}
|
|
||||||
if( w->requestedEvent & AVAHI_WATCH_OUT ) {
|
|
||||||
FD_SET( w->fd, wfds );
|
|
||||||
}
|
}
|
||||||
if( w->requestedEvent & AVAHI_WATCH_ERR ) {
|
if (w->requestedEvent & AVAHI_WATCH_OUT) {
|
||||||
FD_SET( w->fd, efds );
|
FD_SET(w->fd, wfds);
|
||||||
}
|
}
|
||||||
if( w->requestedEvent & AVAHI_WATCH_HUP ) {
|
if (w->requestedEvent & AVAHI_WATCH_ERR) {
|
||||||
ERROR( "Avahi: No support for HUP events! (ignoring)\n" );
|
FD_SET(w->fd, efds);
|
||||||
|
}
|
||||||
|
if (w->requestedEvent & AVAHI_WATCH_HUP) {
|
||||||
|
ERROR("Avahi: No support for HUP events! (ignoring)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if( w->fd > maxfd )
|
if (w->fd > maxfd)
|
||||||
maxfd = w->fd;
|
maxfd = w->fd;
|
||||||
}
|
}
|
||||||
return maxfd;
|
return maxfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds )
|
static int avahiFdconsume(int fdCount, fd_set * rfds, fd_set * wfds,
|
||||||
|
fd_set * efds)
|
||||||
{
|
{
|
||||||
int retval = fdCount;
|
int retval = fdCount;
|
||||||
AvahiTimeout* t;
|
AvahiTimeout *t;
|
||||||
AvahiWatch* w = avahiWatchList;
|
AvahiWatch *w = avahiWatchList;
|
||||||
|
|
||||||
while( w != NULL && retval > 0 ) {
|
while (w != NULL && retval > 0) {
|
||||||
AvahiWatch* current = w;
|
AvahiWatch *current = w;
|
||||||
current->observedEvent = 0;
|
current->observedEvent = 0;
|
||||||
if( FD_ISSET( current->fd, rfds ) ) {
|
if (FD_ISSET(current->fd, rfds)) {
|
||||||
current->observedEvent |= AVAHI_WATCH_IN;
|
current->observedEvent |= AVAHI_WATCH_IN;
|
||||||
FD_CLR( current->fd, rfds );
|
FD_CLR(current->fd, rfds);
|
||||||
retval--;
|
retval--;
|
||||||
}
|
}
|
||||||
if( FD_ISSET( current->fd, wfds ) ) {
|
if (FD_ISSET(current->fd, wfds)) {
|
||||||
current->observedEvent |= AVAHI_WATCH_OUT;
|
current->observedEvent |= AVAHI_WATCH_OUT;
|
||||||
FD_CLR( current->fd, wfds );
|
FD_CLR(current->fd, wfds);
|
||||||
retval--;
|
retval--;
|
||||||
}
|
}
|
||||||
if( FD_ISSET( current->fd, efds ) ) {
|
if (FD_ISSET(current->fd, efds)) {
|
||||||
current->observedEvent |= AVAHI_WATCH_ERR;
|
current->observedEvent |= AVAHI_WATCH_ERR;
|
||||||
FD_CLR( current->fd, efds );
|
FD_CLR(current->fd, efds);
|
||||||
retval--;
|
retval--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -410,21 +423,22 @@ static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds
|
|||||||
*/
|
*/
|
||||||
w = w->next;
|
w = w->next;
|
||||||
|
|
||||||
if( current->observedEvent && avahiRunning ) {
|
if (current->observedEvent && avahiRunning) {
|
||||||
current->callback( current, current->fd,
|
current->callback(current, current->fd,
|
||||||
current->observedEvent, current->userdata );
|
current->observedEvent,
|
||||||
|
current->userdata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t = avahiTimeoutList;
|
t = avahiTimeoutList;
|
||||||
while( t != NULL && avahiRunning ) {
|
while (t != NULL && avahiRunning) {
|
||||||
AvahiTimeout* current = t;
|
AvahiTimeout *current = t;
|
||||||
|
|
||||||
/* Advance to the next one right now, in case the callback
|
/* Advance to the next one right now, in case the callback
|
||||||
* removes itself
|
* removes itself
|
||||||
*/
|
*/
|
||||||
t = t->next;
|
t = t->next;
|
||||||
avahiCheckExpiry( current );
|
avahiCheckExpiry(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
@@ -433,13 +447,15 @@ static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds
|
|||||||
static void init_avahi(const char *serviceName)
|
static void init_avahi(const char *serviceName)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
DEBUG( "Avahi: Initializing interface\n" );
|
DEBUG("Avahi: Initializing interface\n");
|
||||||
|
|
||||||
if( avahi_is_valid_service_name( serviceName ) ) {
|
if (avahi_is_valid_service_name(serviceName)) {
|
||||||
avahiName = avahi_strdup( serviceName );
|
avahiName = avahi_strdup(serviceName);
|
||||||
} else {
|
} else {
|
||||||
ERROR( "Invalid zeroconf_name \"%s\", defaulting to \"%s\" instead.\n", serviceName, SERVICE_NAME );
|
ERROR("Invalid zeroconf_name \"%s\", defaulting to "
|
||||||
avahiName = avahi_strdup( SERVICE_NAME );
|
"\"%s\" instead.\n",
|
||||||
|
serviceName, SERVICE_NAME);
|
||||||
|
avahiName = avahi_strdup(SERVICE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
avahiRunning = 1;
|
avahiRunning = 1;
|
||||||
@@ -453,17 +469,18 @@ static void init_avahi(const char *serviceName)
|
|||||||
avahiPoll.timeout_update = avahiTimeoutUpdate;
|
avahiPoll.timeout_update = avahiTimeoutUpdate;
|
||||||
avahiPoll.timeout_free = avahiTimeoutFree;
|
avahiPoll.timeout_free = avahiTimeoutFree;
|
||||||
|
|
||||||
avahiClient = avahi_client_new( &avahiPoll, AVAHI_CLIENT_NO_FAIL,
|
avahiClient = avahi_client_new(&avahiPoll, AVAHI_CLIENT_NO_FAIL,
|
||||||
avahiClientCallback, NULL, &error );
|
avahiClientCallback, NULL, &error);
|
||||||
|
|
||||||
if( !avahiClient ) {
|
if (!avahiClient) {
|
||||||
ERROR( "Avahi: Failed to create client: %s\n", avahi_strerror(error) );
|
ERROR("Avahi: Failed to create client: %s\n",
|
||||||
|
avahi_strerror(error));
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
zeroConfIo.fdset = avahiFdset;
|
zeroConfIo.fdset = avahiFdset;
|
||||||
zeroConfIo.consume = avahiFdconsume;
|
zeroConfIo.consume = avahiFdconsume;
|
||||||
registerIO( &zeroConfIo );
|
registerIO(&zeroConfIo);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -473,7 +490,7 @@ fail:
|
|||||||
#endif /* HAVE_AVAHI */
|
#endif /* HAVE_AVAHI */
|
||||||
|
|
||||||
#ifdef HAVE_BONJOUR
|
#ifdef HAVE_BONJOUR
|
||||||
static int dnsRegisterFdset(fd_set* rfds, fd_set* wfds, fd_set* efds)
|
static int dnsRegisterFdset(fd_set * rfds, fd_set * wfds, fd_set * efds)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
@@ -489,8 +506,8 @@ static int dnsRegisterFdset(fd_set* rfds, fd_set* wfds, fd_set* efds)
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dnsRegisterFdconsume(int fdCount, fd_set* rfds, fd_set* wfds,
|
static int dnsRegisterFdconsume(int fdCount, fd_set * rfds, fd_set * wfds,
|
||||||
fd_set* efds)
|
fd_set * efds)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
@@ -512,16 +529,17 @@ static int dnsRegisterFdconsume(int fdCount, fd_set* rfds, fd_set* wfds,
|
|||||||
return fdCount;
|
return fdCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dnsRegisterCallback (DNSServiceRef sdRef, DNSServiceFlags flags,
|
static void dnsRegisterCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
|
||||||
DNSServiceErrorType errorCode, const char *name,
|
DNSServiceErrorType errorCode, const char *name,
|
||||||
const char *regtype, const char *domain, void *context)
|
const char *regtype, const char *domain,
|
||||||
|
void *context)
|
||||||
{
|
{
|
||||||
if (errorCode != kDNSServiceErr_NoError) {
|
if (errorCode != kDNSServiceErr_NoError) {
|
||||||
ERROR("Failed to register zeroconf service.\n");
|
ERROR("Failed to register zeroconf service.\n");
|
||||||
|
|
||||||
DNSServiceRefDeallocate(dnsReference);
|
DNSServiceRefDeallocate(dnsReference);
|
||||||
dnsReference = NULL;
|
dnsReference = NULL;
|
||||||
deregisterIO( &zeroConfIo );
|
deregisterIO(&zeroConfIo);
|
||||||
} else {
|
} else {
|
||||||
DEBUG("Registered zeroconf service with name '%s'\n", name);
|
DEBUG("Registered zeroconf service with name '%s'\n", name);
|
||||||
}
|
}
|
||||||
@@ -530,8 +548,12 @@ static void dnsRegisterCallback (DNSServiceRef sdRef, DNSServiceFlags flags,
|
|||||||
static void init_zeroconf_osx(const char *serviceName)
|
static void init_zeroconf_osx(const char *serviceName)
|
||||||
{
|
{
|
||||||
DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
|
DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
|
||||||
0, 0, serviceName, SERVICE_TYPE, NULL, NULL, htons(boundPort), 0,
|
0, 0, serviceName,
|
||||||
NULL, dnsRegisterCallback, NULL);
|
SERVICE_TYPE, NULL, NULL,
|
||||||
|
htons(boundPort), 0,
|
||||||
|
NULL,
|
||||||
|
dnsRegisterCallback,
|
||||||
|
NULL);
|
||||||
|
|
||||||
if (error != kDNSServiceErr_NoError) {
|
if (error != kDNSServiceErr_NoError) {
|
||||||
ERROR("Failed to register zeroconf service.\n");
|
ERROR("Failed to register zeroconf service.\n");
|
||||||
@@ -545,7 +567,7 @@ static void init_zeroconf_osx(const char *serviceName)
|
|||||||
|
|
||||||
zeroConfIo.fdset = dnsRegisterFdset;
|
zeroConfIo.fdset = dnsRegisterFdset;
|
||||||
zeroConfIo.consume = dnsRegisterFdconsume;
|
zeroConfIo.consume = dnsRegisterFdconsume;
|
||||||
registerIO( &zeroConfIo );
|
registerIO(&zeroConfIo);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -581,25 +603,25 @@ void finishZeroconf(void)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
#ifdef HAVE_AVAHI
|
#ifdef HAVE_AVAHI
|
||||||
DEBUG( "Avahi: Shutting down interface\n" );
|
DEBUG("Avahi: Shutting down interface\n");
|
||||||
deregisterIO( &zeroConfIo );
|
deregisterIO(&zeroConfIo);
|
||||||
|
|
||||||
if( avahiGroup ) {
|
if (avahiGroup) {
|
||||||
avahi_entry_group_free( avahiGroup );
|
avahi_entry_group_free(avahiGroup);
|
||||||
avahiGroup = NULL;
|
avahiGroup = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( avahiClient ) {
|
if (avahiClient) {
|
||||||
avahi_client_free( avahiClient );
|
avahi_client_free(avahiClient);
|
||||||
avahiClient = NULL;
|
avahiClient = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
avahi_free( avahiName );
|
avahi_free(avahiName);
|
||||||
avahiName = NULL;
|
avahiName = NULL;
|
||||||
#endif /* HAVE_AVAHI */
|
#endif /* HAVE_AVAHI */
|
||||||
|
|
||||||
#ifdef HAVE_BONJOUR
|
#ifdef HAVE_BONJOUR
|
||||||
deregisterIO( &zeroConfIo );
|
deregisterIO(&zeroConfIo);
|
||||||
if (dnsReference != NULL) {
|
if (dnsReference != NULL) {
|
||||||
DNSServiceRefDeallocate(dnsReference);
|
DNSServiceRefDeallocate(dnsReference);
|
||||||
dnsReference = NULL;
|
dnsReference = NULL;
|
||||||
@@ -607,3 +629,5 @@ void finishZeroconf(void)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* HAVE_ZEROCONF */
|
||||||
|
@@ -21,7 +21,16 @@
|
|||||||
|
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
|
|
||||||
|
#ifdef HAVE_ZEROCONF
|
||||||
|
|
||||||
void initZeroconf(void);
|
void initZeroconf(void);
|
||||||
void finishZeroconf(void);
|
void finishZeroconf(void);
|
||||||
|
|
||||||
|
#else /* ! HAVE_ZEROCONF */
|
||||||
|
|
||||||
|
static void initZeroconf(void) { }
|
||||||
|
static void finishZeroconf(void) { }
|
||||||
|
|
||||||
|
#endif /* ! HAVE_ZEROCONF */
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user