2009-04-07 13:31:46 -04:00
|
|
|
/*
|
|
|
|
* Avahi mDNS backend, with libevent polling
|
|
|
|
*
|
2011-03-05 16:44:12 -05:00
|
|
|
* Copyright (C) 2009-2011 Julien BLACHE <jb@jblache.org>
|
2009-04-07 13:31:46 -04:00
|
|
|
*
|
|
|
|
* Pieces coming from mt-daapd:
|
2015-10-31 04:01:38 -04:00
|
|
|
* Copyright (C) 2005 Sebastian Dröge <slomo@ubuntu.com>
|
2009-04-07 13:31:46 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2010-02-02 15:00:47 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-07 13:31:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-03-05 16:44:12 -05:00
|
|
|
#include <errno.h>
|
2010-05-08 02:36:28 -04:00
|
|
|
#include <sys/socket.h>
|
2011-03-21 13:46:28 -04:00
|
|
|
#include <netinet/in.h>
|
2010-07-10 06:28:43 -04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
#include <event2/event.h>
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
#include <avahi-common/watch.h>
|
|
|
|
#include <avahi-common/malloc.h>
|
|
|
|
#include <avahi-common/error.h>
|
|
|
|
#include <avahi-client/client.h>
|
|
|
|
#include <avahi-client/publish.h>
|
2010-01-15 13:40:17 -05:00
|
|
|
#include <avahi-client/lookup.h>
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2009-05-08 11:46:32 -04:00
|
|
|
#include "logger.h"
|
2010-09-18 10:29:06 -04:00
|
|
|
#include "mdns.h"
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
#define MDNSERR avahi_strerror(avahi_client_errno(mdns_client))
|
2009-05-05 10:08:49 -04:00
|
|
|
|
|
|
|
/* Main event base, from main.c */
|
|
|
|
extern struct event_base *evbase_main;
|
|
|
|
|
2009-04-07 13:31:46 -04:00
|
|
|
static AvahiClient *mdns_client = NULL;
|
|
|
|
static AvahiEntryGroup *mdns_group = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
struct AvahiWatch
|
|
|
|
{
|
2014-03-16 18:11:21 -04:00
|
|
|
struct event *ev;
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
AvahiWatchCallback cb;
|
|
|
|
void *userdata;
|
|
|
|
|
|
|
|
AvahiWatch *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AvahiTimeout
|
|
|
|
{
|
2014-03-16 18:11:21 -04:00
|
|
|
struct event *ev;
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
AvahiTimeoutCallback cb;
|
|
|
|
void *userdata;
|
|
|
|
|
|
|
|
AvahiTimeout *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static AvahiWatch *all_w;
|
|
|
|
static AvahiTimeout *all_t;
|
|
|
|
|
|
|
|
/* libevent callbacks */
|
|
|
|
|
|
|
|
static void
|
|
|
|
evcb_watch(int fd, short ev_events, void *arg)
|
|
|
|
{
|
|
|
|
AvahiWatch *w;
|
|
|
|
AvahiWatchEvent a_events;
|
|
|
|
|
|
|
|
w = (AvahiWatch *)arg;
|
|
|
|
|
|
|
|
a_events = 0;
|
|
|
|
if (ev_events & EV_READ)
|
|
|
|
a_events |= AVAHI_WATCH_IN;
|
|
|
|
if (ev_events & EV_WRITE)
|
|
|
|
a_events |= AVAHI_WATCH_OUT;
|
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
event_add(w->ev, NULL);
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
w->cb(w, fd, a_events, w->userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
evcb_timeout(int fd, short ev_events, void *arg)
|
|
|
|
{
|
|
|
|
AvahiTimeout *t;
|
|
|
|
|
|
|
|
t = (AvahiTimeout *)arg;
|
|
|
|
|
|
|
|
t->cb(t, t->userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AvahiPoll implementation for libevent */
|
|
|
|
|
|
|
|
static int
|
|
|
|
_ev_watch_add(AvahiWatch *w, int fd, AvahiWatchEvent a_events)
|
|
|
|
{
|
|
|
|
short ev_events;
|
|
|
|
|
|
|
|
ev_events = 0;
|
|
|
|
if (a_events & AVAHI_WATCH_IN)
|
|
|
|
ev_events |= EV_READ;
|
|
|
|
if (a_events & AVAHI_WATCH_OUT)
|
2010-06-21 13:18:52 -04:00
|
|
|
ev_events |= EV_WRITE;
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
if (w->ev)
|
|
|
|
event_free(w->ev);
|
|
|
|
|
|
|
|
w->ev = event_new(evbase_main, fd, ev_events, evcb_watch, w);
|
|
|
|
if (!w->ev)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MDNS, "Could not make new event in _ev_watch_add\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
return event_add(w->ev, NULL);
|
2009-04-07 13:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static AvahiWatch *
|
|
|
|
ev_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent a_events, AvahiWatchCallback cb, void *userdata)
|
|
|
|
{
|
|
|
|
AvahiWatch *w;
|
|
|
|
int ret;
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
w = calloc(1, sizeof(AvahiWatch));
|
2009-04-07 13:31:46 -04:00
|
|
|
if (!w)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
w->cb = cb;
|
|
|
|
w->userdata = userdata;
|
|
|
|
|
|
|
|
ret = _ev_watch_add(w, fd, a_events);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
free(w);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
w->next = all_w;
|
|
|
|
all_w = w;
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ev_watch_update(AvahiWatch *w, AvahiWatchEvent a_events)
|
|
|
|
{
|
2014-03-16 18:11:21 -04:00
|
|
|
if (w->ev)
|
|
|
|
event_del(w->ev);
|
|
|
|
|
|
|
|
_ev_watch_add(w, (int)event_get_fd(w->ev), a_events);
|
2009-04-07 13:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static AvahiWatchEvent
|
|
|
|
ev_watch_get_events(AvahiWatch *w)
|
|
|
|
{
|
|
|
|
AvahiWatchEvent a_events;
|
|
|
|
|
|
|
|
a_events = 0;
|
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
if (event_pending(w->ev, EV_READ, NULL))
|
2009-04-07 13:31:46 -04:00
|
|
|
a_events |= AVAHI_WATCH_IN;
|
2014-03-16 18:11:21 -04:00
|
|
|
if (event_pending(w->ev, EV_WRITE, NULL))
|
2009-04-07 13:31:46 -04:00
|
|
|
a_events |= AVAHI_WATCH_OUT;
|
|
|
|
|
|
|
|
return a_events;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ev_watch_free(AvahiWatch *w)
|
|
|
|
{
|
|
|
|
AvahiWatch *prev;
|
|
|
|
AvahiWatch *cur;
|
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
if (w->ev)
|
|
|
|
{
|
|
|
|
event_free(w->ev);
|
|
|
|
w->ev = NULL;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
for (cur = all_w; cur; prev = cur, cur = cur->next)
|
|
|
|
{
|
|
|
|
if (cur != w)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
all_w = w->next;
|
|
|
|
else
|
|
|
|
prev->next = w->next;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
_ev_timeout_add(AvahiTimeout *t, const struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct timeval e_tv;
|
|
|
|
struct timeval now;
|
|
|
|
int ret;
|
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
if (t->ev)
|
|
|
|
event_free(t->ev);
|
|
|
|
|
|
|
|
t->ev = evtimer_new(evbase_main, evcb_timeout, t);
|
|
|
|
if (!t->ev)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MDNS, "Could not make event in _ev_timeout_add - out of memory?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
if ((tv->tv_sec == 0) && (tv->tv_usec == 0))
|
|
|
|
{
|
|
|
|
evutil_timerclear(&e_tv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = gettimeofday(&now, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
evutil_timersub(tv, &now, &e_tv);
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
return evtimer_add(t->ev, &e_tv);
|
2009-04-07 13:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static AvahiTimeout *
|
|
|
|
ev_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback cb, void *userdata)
|
|
|
|
{
|
|
|
|
AvahiTimeout *t;
|
|
|
|
int ret;
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
t = calloc(1, sizeof(AvahiTimeout));
|
2009-04-07 13:31:46 -04:00
|
|
|
if (!t)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
t->cb = cb;
|
|
|
|
t->userdata = userdata;
|
|
|
|
|
|
|
|
if (tv != NULL)
|
|
|
|
{
|
|
|
|
ret = _ev_timeout_add(t, tv);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
free(t);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t->next = all_t;
|
|
|
|
all_t = t;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ev_timeout_update(AvahiTimeout *t, const struct timeval *tv)
|
|
|
|
{
|
2014-03-16 18:11:21 -04:00
|
|
|
if (t->ev)
|
|
|
|
event_del(t->ev);
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
if (tv)
|
|
|
|
_ev_timeout_add(t, tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ev_timeout_free(AvahiTimeout *t)
|
|
|
|
{
|
|
|
|
AvahiTimeout *prev;
|
|
|
|
AvahiTimeout *cur;
|
|
|
|
|
2014-03-16 18:11:21 -04:00
|
|
|
if (t->ev)
|
|
|
|
{
|
|
|
|
event_free(t->ev);
|
|
|
|
t->ev = NULL;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
for (cur = all_t; cur; prev = cur, cur = cur->next)
|
|
|
|
{
|
|
|
|
if (cur != t)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
all_t = t->next;
|
|
|
|
else
|
|
|
|
prev->next = t->next;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct AvahiPoll ev_poll_api =
|
|
|
|
{
|
|
|
|
.userdata = NULL,
|
|
|
|
.watch_new = ev_watch_new,
|
|
|
|
.watch_update = ev_watch_update,
|
|
|
|
.watch_get_events = ev_watch_get_events,
|
|
|
|
.watch_free = ev_watch_free,
|
|
|
|
.timeout_new = ev_timeout_new,
|
|
|
|
.timeout_update = ev_timeout_update,
|
|
|
|
.timeout_free = ev_timeout_free
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-05 16:44:12 -05:00
|
|
|
/* Avahi client callbacks & helpers */
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2010-01-15 13:40:17 -05:00
|
|
|
struct mdns_browser
|
|
|
|
{
|
|
|
|
char *type;
|
2016-10-12 16:24:58 -04:00
|
|
|
AvahiProtocol protocol;
|
2010-01-15 13:40:17 -05:00
|
|
|
mdns_browse_cb cb;
|
|
|
|
|
|
|
|
struct mdns_browser *next;
|
|
|
|
};
|
|
|
|
|
2009-04-07 13:31:46 -04:00
|
|
|
struct mdns_group_entry
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *type;
|
|
|
|
int port;
|
2009-04-08 10:07:05 -04:00
|
|
|
AvahiStringList *txt;
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
struct mdns_group_entry *next;
|
|
|
|
};
|
|
|
|
|
2010-01-15 13:40:17 -05:00
|
|
|
static struct mdns_browser *browser_list;
|
2009-04-07 13:31:46 -04:00
|
|
|
static struct mdns_group_entry *group_entries;
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
// Note: This will only return the first DNS record. Should be enough, but
|
|
|
|
// should more be needed, then a record browser needs to be added.
|
2011-03-05 16:44:12 -05:00
|
|
|
static void
|
2016-10-12 16:24:58 -04:00
|
|
|
browse_resolve_callback(AvahiServiceResolver *r, AvahiIfIndex intf, AvahiProtocol proto, AvahiResolverEvent event,
|
|
|
|
const char *name, const char *type, const char *domain, const char *hostname, const AvahiAddress *addr,
|
|
|
|
uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void *userdata)
|
2010-01-15 13:40:17 -05:00
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
struct mdns_browser *mb;
|
|
|
|
struct keyval txt_kv;
|
|
|
|
char address[AVAHI_ADDRESS_STR_MAX];
|
2010-09-18 11:16:29 -04:00
|
|
|
char *key;
|
|
|
|
char *value;
|
2016-10-12 16:24:58 -04:00
|
|
|
int family;
|
2010-07-10 06:28:43 -04:00
|
|
|
int ret;
|
2010-01-15 13:40:17 -05:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
if (event == AVAHI_RESOLVER_FAILURE)
|
2011-03-15 13:35:10 -04:00
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi Resolver failure: service '%s' type '%s': %s\n", name, type, MDNSERR);
|
|
|
|
goto out_free_resolver;
|
2011-03-15 13:35:10 -04:00
|
|
|
}
|
2016-10-12 16:24:58 -04:00
|
|
|
else if (event != AVAHI_RESOLVER_FOUND)
|
2010-01-15 13:40:17 -05:00
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi Resolver empty callback\n");
|
|
|
|
goto out_free_resolver;
|
2011-03-15 13:35:10 -04:00
|
|
|
}
|
2010-01-15 13:40:17 -05:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Avahi Resolver: resolved service '%s' type '%s' proto %d\n", name, type, proto);
|
2011-03-15 12:53:45 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
memset(&txt_kv, 0, sizeof(struct keyval));
|
2011-03-15 12:53:45 -04:00
|
|
|
|
2011-03-15 13:35:10 -04:00
|
|
|
while (txt)
|
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
ret = avahi_string_list_get_pair(txt, &key, &value, NULL);
|
|
|
|
txt = avahi_string_list_get_next(txt);
|
2010-05-08 02:36:28 -04:00
|
|
|
|
2011-03-15 13:35:10 -04:00
|
|
|
if (ret < 0)
|
2016-10-12 16:24:58 -04:00
|
|
|
continue;
|
2010-05-08 02:36:28 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
keyval_add(&txt_kv, key, value);
|
|
|
|
avahi_free(value);
|
2011-03-15 13:35:10 -04:00
|
|
|
}
|
2010-05-08 02:36:28 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
avahi_free(key);
|
2011-03-15 13:35:10 -04:00
|
|
|
}
|
2010-09-18 11:16:29 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
avahi_address_snprint(address, sizeof(address), addr);
|
2010-09-18 11:16:29 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
// For some reason, the proto returned by the callback will not always be the
|
|
|
|
// protocol of the address - so we use addr->proto instead
|
|
|
|
family = avahi_proto_to_af(addr->proto);
|
2011-03-15 13:35:10 -04:00
|
|
|
|
|
|
|
mb = (struct mdns_browser *)userdata;
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
if (family != AF_UNSPEC)
|
|
|
|
mb->cb(name, type, domain, hostname, family, address, port, &txt_kv);
|
2011-03-15 13:35:10 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
keyval_clear(&txt_kv);
|
2011-03-15 13:35:10 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
out_free_resolver:
|
2010-01-15 13:40:17 -05:00
|
|
|
avahi_service_resolver_free(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
browse_callback(AvahiServiceBrowser *b, AvahiIfIndex intf, AvahiProtocol proto, AvahiBrowserEvent event,
|
|
|
|
const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata)
|
|
|
|
{
|
|
|
|
struct mdns_browser *mb;
|
|
|
|
AvahiServiceResolver *res;
|
2010-05-08 02:36:28 -04:00
|
|
|
int family;
|
2010-01-15 13:40:17 -05:00
|
|
|
|
|
|
|
mb = (struct mdns_browser *)userdata;
|
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case AVAHI_BROWSER_FAILURE:
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi Browser failure: %s\n", MDNSERR);
|
2010-01-15 13:40:17 -05:00
|
|
|
|
|
|
|
avahi_service_browser_free(b);
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, mb->protocol, mb->type, NULL, 0, browse_callback, mb);
|
2010-01-15 13:40:17 -05:00
|
|
|
if (!b)
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Failed to recreate service browser (service type %s): %s\n", mb->type, MDNSERR);
|
|
|
|
|
2010-01-15 13:40:17 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
case AVAHI_BROWSER_NEW:
|
2010-05-08 02:36:28 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Avahi Browser: NEW service '%s' type '%s' proto %d\n", name, type, proto);
|
2010-01-15 13:40:17 -05:00
|
|
|
|
2010-05-08 02:36:28 -04:00
|
|
|
res = avahi_service_resolver_new(mdns_client, intf, proto, name, type, domain, proto, 0, browse_resolve_callback, mb);
|
2010-01-15 13:40:17 -05:00
|
|
|
if (!res)
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Failed to create service resolver: %s\n", MDNSERR);
|
|
|
|
|
2010-01-15 13:40:17 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_BROWSER_REMOVE:
|
2010-05-08 02:36:28 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Avahi Browser: REMOVE service '%s' type '%s' proto %d\n", name, type, proto);
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
family = avahi_proto_to_af(proto);
|
2010-09-26 08:52:41 -04:00
|
|
|
if (family != AF_UNSPEC)
|
2010-05-08 02:36:28 -04:00
|
|
|
mb->cb(name, type, domain, NULL, family, NULL, -1, NULL);
|
2010-01-15 13:40:17 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_BROWSER_ALL_FOR_NOW:
|
|
|
|
case AVAHI_BROWSER_CACHE_EXHAUSTED:
|
2010-01-20 12:17:04 -05:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Avahi Browser (%s): no more results (%s)\n", mb->type,
|
2010-01-15 13:40:17 -05:00
|
|
|
(event == AVAHI_BROWSER_CACHE_EXHAUSTED) ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-07 13:31:46 -04:00
|
|
|
static void
|
|
|
|
entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata)
|
|
|
|
{
|
|
|
|
if (!g || (g != mdns_group))
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case AVAHI_ENTRY_GROUP_ESTABLISHED:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Successfully added mDNS services\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_ENTRY_GROUP_COLLISION:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Group collision\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_ENTRY_GROUP_FAILURE:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Group failure\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_ENTRY_GROUP_UNCOMMITED:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Group uncommitted\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_ENTRY_GROUP_REGISTERING:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Group registering\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_create_services(void)
|
|
|
|
{
|
|
|
|
struct mdns_group_entry *pentry;
|
|
|
|
int ret;
|
|
|
|
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Creating service group\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
if (!group_entries)
|
|
|
|
{
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "No entries yet... skipping service create\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mdns_group == NULL)
|
|
|
|
{
|
|
|
|
mdns_group = avahi_entry_group_new(mdns_client, entry_group_callback, NULL);
|
|
|
|
if (!mdns_group)
|
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_WARN, L_MDNS, "Could not create Avahi EntryGroup: %s\n", MDNSERR);
|
2009-04-07 13:31:46 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pentry = group_entries;
|
|
|
|
while (pentry)
|
|
|
|
{
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Re-registering %s/%s\n", pentry->name, pentry->type);
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
ret = avahi_entry_group_add_service_strlst(mdns_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
|
2011-07-09 05:48:31 -04:00
|
|
|
pentry->name, pentry->type,
|
2009-04-08 10:07:05 -04:00
|
|
|
NULL, NULL, pentry->port, pentry->txt);
|
2009-04-07 13:31:46 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_WARN, L_MDNS, "Could not add mDNS services: %s\n", avahi_strerror(ret));
|
2009-04-07 13:31:46 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pentry = pentry->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avahi_entry_group_commit(mdns_group);
|
|
|
|
if (ret < 0)
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_WARN, L_MDNS, "Could not commit mDNS services: %s\n", MDNSERR);
|
2009-04-07 13:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata)
|
|
|
|
{
|
2010-01-15 13:40:17 -05:00
|
|
|
struct mdns_browser *mb;
|
|
|
|
AvahiServiceBrowser *b;
|
2009-04-07 13:31:46 -04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case AVAHI_CLIENT_S_RUNNING:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client running\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
if (!mdns_group)
|
|
|
|
_create_services();
|
2010-01-15 13:40:17 -05:00
|
|
|
|
|
|
|
for (mb = browser_list; mb; mb = mb->next)
|
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, mb->protocol, mb->type, NULL, 0, browse_callback, mb);
|
2010-01-15 13:40:17 -05:00
|
|
|
if (!b)
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Failed to recreate service browser (service type %s): %s\n", mb->type, MDNSERR);
|
2010-01-15 13:40:17 -05:00
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_CLIENT_S_COLLISION:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client collision\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
if(mdns_group)
|
|
|
|
avahi_entry_group_reset(mdns_group);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_CLIENT_FAILURE:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client failure\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
error = avahi_client_errno(c);
|
|
|
|
if (error == AVAHI_ERR_DISCONNECTED)
|
|
|
|
{
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi Server disconnected, reconnecting\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
avahi_client_free(mdns_client);
|
|
|
|
mdns_group = NULL;
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
mdns_client = avahi_client_new(&ev_poll_api, AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error);
|
2009-04-07 13:31:46 -04:00
|
|
|
if (!mdns_client)
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Failed to create new Avahi client: %s\n", avahi_strerror(error));
|
2009-04-07 13:31:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi client failure: %s\n", avahi_strerror(error));
|
2009-04-07 13:31:46 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_CLIENT_S_REGISTERING:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client registering\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
if (mdns_group)
|
|
|
|
avahi_entry_group_reset(mdns_group);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVAHI_CLIENT_CONNECTING:
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client connecting\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* mDNS interface - to be called only from the main thread */
|
|
|
|
|
|
|
|
int
|
|
|
|
mdns_init(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Initializing Avahi mDNS\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
all_w = NULL;
|
|
|
|
all_t = NULL;
|
|
|
|
group_entries = NULL;
|
2010-01-15 13:40:17 -05:00
|
|
|
browser_list = NULL;
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
mdns_client = avahi_client_new(&ev_poll_api, AVAHI_CLIENT_NO_FAIL,
|
|
|
|
client_callback, NULL, &error);
|
|
|
|
if (!mdns_client)
|
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_WARN, L_MDNS, "mdns_init: Could not create Avahi client: %s\n", MDNSERR);
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mdns_deinit(void)
|
|
|
|
{
|
2009-04-08 10:10:15 -04:00
|
|
|
struct mdns_group_entry *ge;
|
2010-01-15 13:40:17 -05:00
|
|
|
struct mdns_browser *mb;
|
2009-04-07 13:31:46 -04:00
|
|
|
AvahiWatch *w;
|
|
|
|
AvahiTimeout *t;
|
|
|
|
|
|
|
|
for (t = all_t; t; t = t->next)
|
2014-03-16 18:11:21 -04:00
|
|
|
if (t->ev)
|
|
|
|
{
|
|
|
|
event_free(t->ev);
|
|
|
|
t->ev = NULL;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
for (w = all_w; w; w = w->next)
|
2014-03-16 18:11:21 -04:00
|
|
|
if (w->ev)
|
|
|
|
{
|
|
|
|
event_free(w->ev);
|
|
|
|
w->ev = NULL;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2010-01-17 04:51:05 -05:00
|
|
|
for (ge = group_entries; group_entries; ge = group_entries)
|
2009-04-08 10:10:15 -04:00
|
|
|
{
|
|
|
|
group_entries = ge->next;
|
|
|
|
|
|
|
|
free(ge->name);
|
|
|
|
free(ge->type);
|
|
|
|
avahi_string_list_free(ge->txt);
|
|
|
|
|
|
|
|
free(ge);
|
|
|
|
}
|
|
|
|
|
2010-01-15 13:40:17 -05:00
|
|
|
for (mb = browser_list; browser_list; mb = browser_list)
|
|
|
|
{
|
|
|
|
browser_list = mb->next;
|
|
|
|
|
|
|
|
free(mb->type);
|
|
|
|
free(mb);
|
|
|
|
}
|
|
|
|
|
2010-07-16 12:36:09 -04:00
|
|
|
if (mdns_client)
|
2009-04-07 13:31:46 -04:00
|
|
|
avahi_client_free(mdns_client);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-04-08 10:35:59 -04:00
|
|
|
mdns_register(char *name, char *type, int port, char **txt)
|
2009-04-07 13:31:46 -04:00
|
|
|
{
|
|
|
|
struct mdns_group_entry *ge;
|
2009-04-08 10:07:05 -04:00
|
|
|
AvahiStringList *txt_sl;
|
2009-04-08 10:35:59 -04:00
|
|
|
int i;
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Adding mDNS service %s/%s\n", name, type);
|
2009-04-07 13:31:46 -04:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
ge = calloc(1, sizeof(struct mdns_group_entry));
|
2009-04-07 13:31:46 -04:00
|
|
|
if (!ge)
|
2016-10-12 16:24:58 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MDNS, "Out of memory for mdns register\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-04-07 13:31:46 -04:00
|
|
|
|
|
|
|
ge->name = strdup(name);
|
|
|
|
ge->type = strdup(type);
|
|
|
|
ge->port = port;
|
|
|
|
|
2009-04-08 10:07:05 -04:00
|
|
|
txt_sl = NULL;
|
2015-10-31 04:01:38 -04:00
|
|
|
if (txt)
|
2009-04-08 10:07:05 -04:00
|
|
|
{
|
2015-10-31 04:01:38 -04:00
|
|
|
for (i = 0; txt[i]; i++)
|
|
|
|
{
|
|
|
|
txt_sl = avahi_string_list_add(txt_sl, txt[i]);
|
2009-04-08 10:07:05 -04:00
|
|
|
|
2015-10-31 04:01:38 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Added key %s\n", txt[i]);
|
|
|
|
}
|
2009-04-08 10:07:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ge->txt = txt_sl;
|
|
|
|
|
2009-04-07 13:31:46 -04:00
|
|
|
ge->next = group_entries;
|
|
|
|
group_entries = ge;
|
|
|
|
|
|
|
|
if (mdns_group)
|
|
|
|
{
|
2010-01-09 05:14:23 -05:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Resetting mDNS group\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
avahi_entry_group_reset(mdns_group);
|
|
|
|
}
|
|
|
|
|
2009-05-08 11:53:39 -04:00
|
|
|
DPRINTF(E_DBG, L_MDNS, "Creating service group\n");
|
2009-04-07 13:31:46 -04:00
|
|
|
_create_services();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-15 13:40:17 -05:00
|
|
|
|
|
|
|
int
|
2016-10-12 16:24:58 -04:00
|
|
|
mdns_browse(char *type, int family, mdns_browse_cb cb)
|
2010-01-15 13:40:17 -05:00
|
|
|
{
|
|
|
|
struct mdns_browser *mb;
|
|
|
|
AvahiServiceBrowser *b;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_MDNS, "Adding service browser for type %s\n", type);
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
mb = calloc(1, sizeof(struct mdns_browser));
|
2010-01-15 13:40:17 -05:00
|
|
|
if (!mb)
|
2016-10-12 16:24:58 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MDNS, "Out of memory for new mdns browser\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-15 13:40:17 -05:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
mb->protocol = avahi_af_to_proto(family);
|
2010-01-15 13:40:17 -05:00
|
|
|
mb->type = strdup(type);
|
|
|
|
mb->cb = cb;
|
|
|
|
|
|
|
|
mb->next = browser_list;
|
|
|
|
browser_list = mb;
|
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, mb->protocol, mb->type, NULL, 0, browse_callback, mb);
|
2010-01-15 13:40:17 -05:00
|
|
|
if (!b)
|
|
|
|
{
|
2016-10-12 16:24:58 -04:00
|
|
|
DPRINTF(E_LOG, L_MDNS, "Failed to create service browser: %s\n", MDNSERR);
|
2010-07-16 12:36:16 -04:00
|
|
|
|
|
|
|
browser_list = mb->next;
|
|
|
|
free(mb->type);
|
|
|
|
free(mb);
|
|
|
|
|
2010-01-15 13:40:17 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|