From 6cd59a124047d3deb0a55ffe2f260c7ab2bad7db Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Sun, 6 Mar 2011 12:37:58 +0100 Subject: [PATCH] Introduce mDNS browser flags for address type selection Allow each user to specify the address types that should be returned through the mDNS browse callback. --- src/mdns.h | 9 ++++++++- src/mdns_avahi.c | 33 ++++++++++++++++++++++++++++----- src/player.c | 2 +- src/remote_pairing.c | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/mdns.h b/src/mdns.h index 4c76defc..f054aca8 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -4,6 +4,13 @@ #include "misc.h" +#define MDNS_WANT_V4 (1 << 0) +#define MDNS_WANT_V4LL (1 << 1) +#define MDNS_WANT_V6 (1 << 2) +#define MDNS_WANT_V6LL (1 << 3) + +#define MDNS_WANT_DEFAULT (MDNS_WANT_V4 | MDNS_WANT_V6 | MDNS_WANT_V6LL) + typedef void (* mdns_browse_cb)(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, struct keyval *txt); /* mDNS interface functions */ @@ -18,6 +25,6 @@ int mdns_register(char *name, char *type, int port, char **txt); int -mdns_browse(char *type, mdns_browse_cb cb); +mdns_browse(char *type, int flags, mdns_browse_cb cb); #endif /* !__MDNS_H__ */ diff --git a/src/mdns_avahi.c b/src/mdns_avahi.c index 3372d94a..76d15b49 100644 --- a/src/mdns_avahi.c +++ b/src/mdns_avahi.c @@ -315,6 +315,8 @@ struct mdns_browser char *type; mdns_browse_cb cb; + int flags; + struct mdns_browser *next; }; @@ -367,6 +369,7 @@ browse_record_callback_v4(AvahiRecordBrowser *b, AvahiIfIndex intf, AvahiProtoco char address[INET_ADDRSTRLEN]; struct in_addr addr; struct mdns_record_browser *rb_data; + int ll; rb_data = (struct mdns_record_browser *)userdata; @@ -382,10 +385,15 @@ browse_record_callback_v4(AvahiRecordBrowser *b, AvahiIfIndex intf, AvahiProtoco memcpy(&addr.s_addr, rdata, sizeof(addr.s_addr)); - if (is_v4ll(&addr)) + ll = is_v4ll(&addr); + if (ll && !(rb_data->mb->flags & MDNS_WANT_V4LL)) { - DPRINTF(E_DBG, L_MDNS, "Discarding IPv4 LL address\n"); - + DPRINTF(E_DBG, L_MDNS, "Discarding IPv4 LL, not interested (service %s)\n", rb_data->name); + return; + } + else if (!ll && !(rb_data->mb->flags & MDNS_WANT_V4)) + { + DPRINTF(E_DBG, L_MDNS, "Discarding IPv4, not interested (service %s)\n", rb_data->name); return; } @@ -438,6 +446,7 @@ browse_record_callback_v6(AvahiRecordBrowser *b, AvahiIfIndex intf, AvahiProtoco char ifname[IF_NAMESIZE]; struct in6_addr addr; struct mdns_record_browser *rb_data; + int ll; int len; int ret; @@ -455,6 +464,18 @@ browse_record_callback_v6(AvahiRecordBrowser *b, AvahiIfIndex intf, AvahiProtoco memcpy(&addr.s6_addr, rdata, sizeof(addr.s6_addr)); + ll = is_v6ll(&addr); + if (ll && !(rb_data->mb->flags & MDNS_WANT_V6LL)) + { + DPRINTF(E_DBG, L_MDNS, "Discarding IPv6 LL, not interested (service %s)\n", rb_data->name); + return; + } + else if (!ll && !(rb_data->mb->flags & MDNS_WANT_V6)) + { + DPRINTF(E_DBG, L_MDNS, "Discarding IPv6, not interested (service %s)\n", rb_data->name); + return; + } + if (!inet_ntop(AF_INET6, &addr.s6_addr, address, sizeof(address))) { DPRINTF(E_LOG, L_MDNS, "Could not print IPv6 address: %s\n", strerror(errno)); @@ -462,7 +483,7 @@ browse_record_callback_v6(AvahiRecordBrowser *b, AvahiIfIndex intf, AvahiProtoco return; } - if (is_v6ll(&addr)) + if (ll) { if (!if_indextoname(intf, ifname)) { @@ -980,7 +1001,7 @@ mdns_register(char *name, char *type, int port, char **txt) } int -mdns_browse(char *type, mdns_browse_cb cb) +mdns_browse(char *type, int flags, mdns_browse_cb cb) { struct mdns_browser *mb; AvahiServiceBrowser *b; @@ -994,6 +1015,8 @@ mdns_browse(char *type, mdns_browse_cb cb) mb->type = strdup(type); mb->cb = cb; + mb->flags = (flags) ? flags : MDNS_WANT_DEFAULT; + mb->next = browser_list; browser_list = mb; diff --git a/src/player.c b/src/player.c index a6803fb3..a31d033e 100644 --- a/src/player.c +++ b/src/player.c @@ -3834,7 +3834,7 @@ player_init(void) goto raop_fail; } - ret = mdns_browse("_raop._tcp", raop_device_cb); + ret = mdns_browse("_raop._tcp", 0, raop_device_cb); if (ret < 0) { DPRINTF(E_FATAL, L_PLAYER, "Could not add mDNS browser for AirTunes devices\n"); diff --git a/src/remote_pairing.c b/src/remote_pairing.c index 560def11..156b25b4 100644 --- a/src/remote_pairing.c +++ b/src/remote_pairing.c @@ -899,7 +899,7 @@ remote_pairing_init(void) # endif #endif /* USE_EVENTFD */ - ret = mdns_browse("_touch-remote._tcp", touch_remote_cb); + ret = mdns_browse("_touch-remote._tcp", 0, touch_remote_cb); if (ret < 0) { DPRINTF(E_FATAL, L_REMOTE, "Could not browse for Remote services\n");