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:
@@ -39,7 +39,8 @@ struct ioOps {
|
||||
* Return the total number of fds left in all sets (Ie, return fdCount
|
||||
* 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 */
|
||||
|
120
src/zeroconf.c
120
src/zeroconf.c
@@ -16,8 +16,12 @@
|
||||
* 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 "os_compat.h"
|
||||
#include "conf.h"
|
||||
#include "log.h"
|
||||
#include "listen.h"
|
||||
@@ -35,11 +39,7 @@
|
||||
#define DEFAULT_ZEROCONF_ENABLED 1
|
||||
|
||||
static int zeroconfEnabled;
|
||||
|
||||
#ifdef HAVE_ZEROCONF
|
||||
static struct ioOps zeroConfIo = {
|
||||
};
|
||||
#endif
|
||||
static struct ioOps zeroConfIo;
|
||||
|
||||
#ifdef HAVE_BONJOUR
|
||||
#include <dns_sd.h>
|
||||
@@ -66,7 +66,8 @@ static AvahiPoll avahiPoll;
|
||||
static int avahiRunning;
|
||||
|
||||
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 */
|
||||
static void avahiRegisterService(AvahiClient * c);
|
||||
@@ -93,7 +94,9 @@ struct AvahiTimeout {
|
||||
static AvahiWatch *avahiWatchList;
|
||||
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));
|
||||
|
||||
@@ -174,7 +177,10 @@ static void avahiTimeoutFree( AvahiTimeout *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));
|
||||
|
||||
@@ -194,11 +200,10 @@ static AvahiTimeout* avahiTimeoutNew( const AvahiPoll *api, const struct timeval
|
||||
}
|
||||
|
||||
/* Callback when the EntryGroup changes state */
|
||||
static void avahiGroupCallback(
|
||||
AvahiEntryGroup *g,
|
||||
AvahiEntryGroupState state,
|
||||
void *userdata)
|
||||
static void avahiGroupCallback(AvahiEntryGroup * g,
|
||||
AvahiEntryGroupState state, void *userdata)
|
||||
{
|
||||
char *n;
|
||||
assert(g);
|
||||
|
||||
DEBUG("Avahi: Service group changed to state %d\n", state);
|
||||
@@ -206,27 +211,27 @@ static void avahiGroupCallback(
|
||||
switch (state) {
|
||||
case AVAHI_ENTRY_GROUP_ESTABLISHED:
|
||||
/* The entry group has been established successfully */
|
||||
LOG( "Avahi: Service '%s' successfully established.\n", avahiName );
|
||||
LOG("Avahi: Service '%s' successfully established.\n",
|
||||
avahiName);
|
||||
break;
|
||||
|
||||
case AVAHI_ENTRY_GROUP_COLLISION : {
|
||||
char *n;
|
||||
|
||||
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;
|
||||
|
||||
LOG( "Avahi: Service name collision, renaming service to '%s'\n", avahiName );
|
||||
LOG("Avahi: Service name collision, renaming service to '%s'\n",
|
||||
avahiName);
|
||||
|
||||
/* And recreate the services */
|
||||
avahiRegisterService(avahi_entry_group_get_client(g));
|
||||
break;
|
||||
}
|
||||
|
||||
case AVAHI_ENTRY_GROUP_FAILURE:
|
||||
ERROR("Avahi: Entry group failure: %s\n",
|
||||
avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))) );
|
||||
avahi_strerror(avahi_client_errno
|
||||
(avahi_entry_group_get_client(g))));
|
||||
/* Some kind of failure happened while we were registering our services */
|
||||
avahiRunning = 0;
|
||||
break;
|
||||
@@ -236,7 +241,6 @@ static void avahiGroupCallback(
|
||||
break;
|
||||
case AVAHI_ENTRY_GROUP_REGISTERING:
|
||||
DEBUG("Avahi: Service group is REGISTERING\n");
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,33 +251,36 @@ static void avahiRegisterService(AvahiClient *c)
|
||||
assert(c);
|
||||
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) {
|
||||
avahiGroup = avahi_entry_group_new(c, avahiGroupCallback, NULL);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add the service */
|
||||
/* 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,
|
||||
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
|
||||
avahiName, SERVICE_TYPE,
|
||||
NULL, NULL,
|
||||
boundPort,
|
||||
NULL);
|
||||
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
|
||||
0, avahiName, SERVICE_TYPE, NULL,
|
||||
NULL, boundPort, NULL);
|
||||
if (ret < 0) {
|
||||
ERROR( "Avahi: Failed to add service %s: %s\n", SERVICE_TYPE, avahi_strerror(ret) );
|
||||
ERROR("Avahi: Failed to add service %s: %s\n", SERVICE_TYPE,
|
||||
avahi_strerror(ret));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Tell the server to register the service group */
|
||||
ret = avahi_entry_group_commit(avahiGroup);
|
||||
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;
|
||||
}
|
||||
return;
|
||||
@@ -283,8 +290,10 @@ fail:
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
/* Called whenever the client or server state changes */
|
||||
@@ -301,27 +310,31 @@ static void avahiClientCallback(AvahiClient *c, AvahiClientState state, void *us
|
||||
break;
|
||||
|
||||
case AVAHI_CLIENT_FAILURE:
|
||||
{
|
||||
int reason = avahi_client_errno(c);
|
||||
reason = avahi_client_errno(c);
|
||||
if (reason == AVAHI_ERR_DISCONNECTED) {
|
||||
LOG( "Avahi: Client Disconnected, will reconnect shortly\n");
|
||||
LOG("Avahi: Client Disconnected, "
|
||||
"will reconnect shortly\n");
|
||||
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 );
|
||||
avahiClient =
|
||||
avahi_client_new(&avahiPoll,
|
||||
AVAHI_CLIENT_NO_FAIL,
|
||||
avahiClientCallback, NULL,
|
||||
&reason);
|
||||
if (!avahiClient) {
|
||||
ERROR( "Avahi: Could not reconnect: %s\n", avahi_strerror(reason) );
|
||||
ERROR("Avahi: Could not reconnect: %s\n",
|
||||
avahi_strerror(reason));
|
||||
avahiRunning = 0;
|
||||
}
|
||||
} else {
|
||||
ERROR( "Avahi: Client failure: %s (terminal)\n", avahi_strerror(reason));
|
||||
ERROR("Avahi: Client failure: %s (terminal)\n",
|
||||
avahi_strerror(reason));
|
||||
avahiRunning = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case AVAHI_CLIENT_S_COLLISION:
|
||||
@@ -350,7 +363,6 @@ static void avahiClientCallback(AvahiClient *c, AvahiClientState state, void *us
|
||||
|
||||
case AVAHI_CLIENT_CONNECTING:
|
||||
DEBUG("Avahi: Client is CONNECTING\n");
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -380,7 +392,8 @@ static int avahiFdset( fd_set* rfds, fd_set* wfds, fd_set* efds )
|
||||
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;
|
||||
AvahiTimeout *t;
|
||||
@@ -412,7 +425,8 @@ static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds
|
||||
|
||||
if (current->observedEvent && avahiRunning) {
|
||||
current->callback(current, current->fd,
|
||||
current->observedEvent, current->userdata );
|
||||
current->observedEvent,
|
||||
current->userdata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -438,7 +452,9 @@ static void init_avahi(const char *serviceName)
|
||||
if (avahi_is_valid_service_name(serviceName)) {
|
||||
avahiName = avahi_strdup(serviceName);
|
||||
} else {
|
||||
ERROR( "Invalid zeroconf_name \"%s\", defaulting to \"%s\" instead.\n", serviceName, SERVICE_NAME );
|
||||
ERROR("Invalid zeroconf_name \"%s\", defaulting to "
|
||||
"\"%s\" instead.\n",
|
||||
serviceName, SERVICE_NAME);
|
||||
avahiName = avahi_strdup(SERVICE_NAME);
|
||||
}
|
||||
|
||||
@@ -457,7 +473,8 @@ static void init_avahi(const char *serviceName)
|
||||
avahiClientCallback, NULL, &error);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -514,7 +531,8 @@ static int dnsRegisterFdconsume(int fdCount, fd_set* rfds, fd_set* wfds,
|
||||
|
||||
static void dnsRegisterCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
|
||||
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) {
|
||||
ERROR("Failed to register zeroconf service.\n");
|
||||
@@ -530,8 +548,12 @@ static void dnsRegisterCallback (DNSServiceRef sdRef, DNSServiceFlags flags,
|
||||
static void init_zeroconf_osx(const char *serviceName)
|
||||
{
|
||||
DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
|
||||
0, 0, serviceName, SERVICE_TYPE, NULL, NULL, htons(boundPort), 0,
|
||||
NULL, dnsRegisterCallback, NULL);
|
||||
0, 0, serviceName,
|
||||
SERVICE_TYPE, NULL, NULL,
|
||||
htons(boundPort), 0,
|
||||
NULL,
|
||||
dnsRegisterCallback,
|
||||
NULL);
|
||||
|
||||
if (error != kDNSServiceErr_NoError) {
|
||||
ERROR("Failed to register zeroconf service.\n");
|
||||
@@ -607,3 +629,5 @@ void finishZeroconf(void)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* HAVE_ZEROCONF */
|
||||
|
@@ -21,7 +21,16 @@
|
||||
|
||||
#include "../config.h"
|
||||
|
||||
#ifdef HAVE_ZEROCONF
|
||||
|
||||
void initZeroconf(void);
|
||||
void finishZeroconf(void);
|
||||
|
||||
#else /* ! HAVE_ZEROCONF */
|
||||
|
||||
static void initZeroconf(void) { }
|
||||
static void finishZeroconf(void) { }
|
||||
|
||||
#endif /* ! HAVE_ZEROCONF */
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user