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
|
* 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 */
|
||||||
|
120
src/zeroconf.c
120
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"
|
||||||
@@ -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>
|
||||||
@@ -66,7 +66,8 @@ 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);
|
||||||
@@ -93,7 +94,9 @@ struct AvahiTimeout {
|
|||||||
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));
|
||||||
|
|
||||||
@@ -174,7 +177,10 @@ static void avahiTimeoutFree( AvahiTimeout *t )
|
|||||||
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));
|
||||||
|
|
||||||
@@ -194,11 +200,10 @@ static AvahiTimeout* avahiTimeoutNew( const AvahiPoll *api, const struct timeval
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
@@ -206,27 +211,27 @@ static void avahiGroupCallback(
|
|||||||
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",
|
||||||
|
avahiName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AVAHI_ENTRY_GROUP_COLLISION : {
|
case AVAHI_ENTRY_GROUP_COLLISION:
|
||||||
char *n;
|
|
||||||
|
|
||||||
/* A service name collision happened. Let's pick a new name */
|
/* A service name collision happened. Let's pick a new name */
|
||||||
n = avahi_alternative_service_name(avahiName);
|
n = avahi_alternative_service_name(avahiName);
|
||||||
avahi_free(avahiName);
|
avahi_free(avahiName);
|
||||||
avahiName = n;
|
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 */
|
/* And recreate the services */
|
||||||
avahiRegisterService(avahi_entry_group_get_client(g));
|
avahiRegisterService(avahi_entry_group_get_client(g));
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case AVAHI_ENTRY_GROUP_FAILURE:
|
case AVAHI_ENTRY_GROUP_FAILURE:
|
||||||
ERROR("Avahi: Entry group failure: %s\n",
|
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 */
|
/* Some kind of failure happened while we were registering our services */
|
||||||
avahiRunning = 0;
|
avahiRunning = 0;
|
||||||
break;
|
break;
|
||||||
@@ -236,7 +241,6 @@ static void avahiGroupCallback(
|
|||||||
break;
|
break;
|
||||||
case AVAHI_ENTRY_GROUP_REGISTERING:
|
case AVAHI_ENTRY_GROUP_REGISTERING:
|
||||||
DEBUG("Avahi: Service group is REGISTERING\n");
|
DEBUG("Avahi: Service group is REGISTERING\n");
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,33 +251,36 @@ static void avahiRegisterService(AvahiClient *c)
|
|||||||
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,
|
|
||||||
NULL);
|
|
||||||
if (ret < 0) {
|
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;
|
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,8 +290,10 @@ 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 */
|
||||||
@@ -301,27 +310,31 @@ static void avahiClientCallback(AvahiClient *c, AvahiClientState state, void *us
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case AVAHI_CLIENT_FAILURE:
|
case AVAHI_CLIENT_FAILURE:
|
||||||
{
|
reason = avahi_client_errno(c);
|
||||||
int reason = avahi_client_errno(c);
|
|
||||||
if (reason == AVAHI_ERR_DISCONNECTED) {
|
if (reason == AVAHI_ERR_DISCONNECTED) {
|
||||||
LOG( "Avahi: Client Disconnected, will reconnect shortly\n");
|
LOG("Avahi: Client Disconnected, "
|
||||||
|
"will reconnect shortly\n");
|
||||||
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 = avahi_client_new( &avahiPoll, AVAHI_CLIENT_NO_FAIL,
|
avahiClient =
|
||||||
avahiClientCallback, NULL, &reason );
|
avahi_client_new(&avahiPoll,
|
||||||
|
AVAHI_CLIENT_NO_FAIL,
|
||||||
|
avahiClientCallback, NULL,
|
||||||
|
&reason);
|
||||||
if (!avahiClient) {
|
if (!avahiClient) {
|
||||||
ERROR( "Avahi: Could not reconnect: %s\n", avahi_strerror(reason) );
|
ERROR("Avahi: Could not reconnect: %s\n",
|
||||||
|
avahi_strerror(reason));
|
||||||
avahiRunning = 0;
|
avahiRunning = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ERROR( "Avahi: Client failure: %s (terminal)\n", avahi_strerror(reason));
|
ERROR("Avahi: Client failure: %s (terminal)\n",
|
||||||
|
avahi_strerror(reason));
|
||||||
avahiRunning = 0;
|
avahiRunning = 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AVAHI_CLIENT_S_COLLISION:
|
case AVAHI_CLIENT_S_COLLISION:
|
||||||
@@ -350,7 +363,6 @@ static void avahiClientCallback(AvahiClient *c, AvahiClientState state, void *us
|
|||||||
|
|
||||||
case AVAHI_CLIENT_CONNECTING:
|
case AVAHI_CLIENT_CONNECTING:
|
||||||
DEBUG("Avahi: Client is CONNECTING\n");
|
DEBUG("Avahi: Client is CONNECTING\n");
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,7 +392,8 @@ static int avahiFdset( fd_set* rfds, fd_set* wfds, fd_set* efds )
|
|||||||
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;
|
||||||
@@ -412,7 +425,8 @@ static int avahiFdconsume( int fdCount, fd_set* rfds, fd_set* wfds, fd_set* efds
|
|||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -438,7 +452,9 @@ static void init_avahi(const char *serviceName)
|
|||||||
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 "
|
||||||
|
"\"%s\" instead.\n",
|
||||||
|
serviceName, SERVICE_NAME);
|
||||||
avahiName = avahi_strdup(SERVICE_NAME);
|
avahiName = avahi_strdup(SERVICE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,7 +473,8 @@ static void init_avahi(const char *serviceName)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -514,7 +531,8 @@ static int dnsRegisterFdconsume(int fdCount, fd_set* rfds, fd_set* wfds,
|
|||||||
|
|
||||||
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");
|
||||||
@@ -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");
|
||||||
@@ -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