From 64b08d1d1ebc4ed4aca7c201a82da6b4df225ea2 Mon Sep 17 00:00:00 2001 From: Martin Wilck Date: Wed, 24 Aug 2016 21:16:28 +0200 Subject: [PATCH 07/12] mdns: Skip lo and interfaces without IPv4 address Signed-off-by: Martin Wilck --- protocol/discovery/mdns.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/protocol/discovery/mdns.c b/protocol/discovery/mdns.c index cafbabf..92e153a 100644 --- a/protocol/discovery/mdns.c +++ b/protocol/discovery/mdns.c @@ -1,3 +1,4 @@ + /***************************************************************************** mdns.c - mDNS related calls @@ -39,6 +40,7 @@ #include #include "mdns.h" #include +#include /* Convert "www.google.com" to "3www6google3com". */ static int mdns_convert_name_to_dns(const char *name, int name_size, char *dns_name) @@ -86,6 +88,29 @@ struct mdns_socket { }; #define MDNS_SOCKET_INIT { .socket = -1, .idx = NULL, .good = NULL, } +static int get_ipv4_address(const char *iface, struct in_addr *addr) +{ + int s, r; + struct ifreq ifr; + + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, iface, IFNAMSIZ-1); + + s = socket(AF_INET, SOCK_DGRAM, 0); + r = ioctl(s, SIOCGIFADDR, &ifr); + close(s); + + if (r == -1) { + DBG("error in SIOCGIFADDR for %s: %m\n", iface); + return MDNS_STATUS_ERROR; + } + + if (addr != NULL) + memcpy(addr, &((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr, + sizeof(*addr)); + return MDNS_STATUS_OK; +} + static int mdns_open_socket(struct mdns_socket *mdns_sock) { int stat = MDNS_STATUS_ERROR; @@ -152,6 +177,13 @@ static int mdns_open_socket(struct mdns_socket *mdns_sock) for (idx = mdns_sock->idx, ifaces = 0; idx && (idx->if_index != 0 || idx->if_name != NULL); idx++) { + + /* Skip lo and interfaces without IPv4 address */ + if (!strcmp(idx->if_name, "lo")) + continue; + if (get_ipv4_address(idx->if_name, NULL) == MDNS_STATUS_ERROR) + continue; + /* Join the multicast group on each local interface */ MREQN_INIT(mreqn, idx->if_index); if (setsockopt(udp_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreqn, -- 2.9.2