Skip to content

Commit

Permalink
DNSSD: Increase DNS Service Discovery robustness
Browse files Browse the repository at this point in the history
This changes the implementation of avahi and Bonjour to be
more robust, and share more code.

It also now finds all DNS SD hosts advertised on the network, and
builds up a linked list to keep track of them. When you add "ip:" as the
uri, it will connect to the first (random) one it finds. The previous
implementation connected to the last (random) one if found, so I don't
think the overall functionality will be changed in any way.

Signed-off-by: Robin Getz <[email protected]>
Signed-off-by: Matej Kenda <[email protected]>
  • Loading branch information
rgetz committed Mar 30, 2020
1 parent 027919e commit 30209db
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 174 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ if(WITH_NETWORK_BACKEND)
message(STATUS "Building with CFNetServices, an Apple DNS SD implementation")
set(HAVE_DNS_SD ON)

list(APPEND LIBIIO_CFILES dns_sd_bonjour.c)
list(APPEND LIBIIO_CFILES dns_sd_bonjour.c dns_sd.c)
list(APPEND LIBS_TO_LINK ${CORE_SERVICES} )

elseif(AVAHI_CLIENT_LIBRARIES AND AVAHI_COMMON_LIBRARIES)
message(STATUS "Building with Avahi, a DNS SD implementation")
set(HAVE_DNS_SD ON)
set(HAVE_AVAHI ON)

list(APPEND LIBIIO_CFILES dns_sd_avahi.c)
list(APPEND LIBIIO_CFILES dns_sd_avahi.c dns_sd.c)
set(AVAHI_LIBRARIES ${AVAHI_CLIENT_LIBRARIES} ${AVAHI_COMMON_LIBRARIES})
list(APPEND LIBS_TO_LINK ${AVAHI_LIBRARIES})
else()
Expand Down
3 changes: 1 addition & 2 deletions Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,7 @@ RECURSIVE = NO
# Note that relative paths are relative to the directory from which doxygen is
# run.

EXCLUDE = @CMAKE_SOURCE_DIR@/iio-private.h @CMAKE_SOURCE_DIR@/debug.h @CMAKE_SOURCE_DIR@/iio-lock.h @CMAKE_SOURCE_DIR@/iiod-client.h @CMAKE_SOURCE_DIR@/sort.h

EXCLUDE = @CMAKE_SOURCE_DIR@/iio-private.h @CMAKE_SOURCE_DIR@/debug.h @CMAKE_SOURCE_DIR@/iio-lock.h @CMAKE_SOURCE_DIR@/iiod-client.h @CMAKE_SOURCE_DIR@/sort.h @CMAKE_SOURCE_DIR@/network.h

# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
Expand Down
175 changes: 175 additions & 0 deletions dns_sd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014-2020 Analog Devices, Inc.
* Author: Robin Getz <[email protected]>
* Matej Kenda <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* Some of this is insipred from libavahi's example:
* https://avahi.org/doxygen/html/client-browse-services_8c-example.html
* which is also LGPL 2.1 or later.
*
* */

#include "iio-lock.h"
#include "iio-private.h"
#include "network.h"

#include "debug.h"

/* Some functions for handling common linked list operations */
static void dnssd_remove_node(struct dns_sd_discovery_data **ddata, int n)
{

struct dns_sd_discovery_data *d, *ndata, *ldata, *tdata;
int i;

d = *ddata;

if (n == 0) {
tdata = d->next;
dnssd_free_discovery_data(d);
d = tdata;
} else {
for (i = 0, ndata = d; ndata->next != NULL; ndata = ndata->next) {
if (i == n) {
/* Could be NULL or node, both are OK */
tdata = ndata->next;
/* free the node to be removed */
dnssd_free_discovery_data(ndata);
/* snip it out */
ldata->next = tdata;
break;
}
ldata = ndata;
i++;
}
}

*ddata = d;
}

/*
* remove the ones in the list that you can't connect to
* This is sort of silly, but we have seen non-iio devices advertised
* and discovered on the network. Oh well....
*/
void port_knock_discovery_data(struct dns_sd_discovery_data **ddata)
{
struct dns_sd_discovery_data *d, *ndata;
int i, ret;

d = *ddata;
iio_mutex_lock(d->lock);
for (i = 0, ndata = d; ndata->next != NULL; ndata = ndata->next) {
char port_str[6];
struct addrinfo hints, *res, *rp;
int fd;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

iio_snprintf(port_str, sizeof(port_str), "%hu", ndata->port);
ret = getaddrinfo(ndata->addr_str, port_str, &hints, &res);

/* getaddrinfo() returns a list of address structures */
if (ret) {
DEBUG("Unable to find host ('%s'): %s\n",
ndata->hostname,
gai_strerror(ret));
dnssd_remove_node(&d, i);
} else {
for (rp = res; rp != NULL; rp = rp->ai_next) {
fd = create_socket(rp, DEFAULT_TIMEOUT_MS);
if (fd < 0) {
DEBUG("Unable to open %s%s socket ('%s:%d' %s)\n",
rp->ai_family == AF_INET ? "ipv4" : "",
rp->ai_family == AF_INET6? "ipv6" : "",
ndata->hostname, ndata->port, ndata->addr_str);
dnssd_remove_node(&d, i);
} else {
close(fd);
DEBUG("Something %s%s at '%s:%d' %s)\n",
rp->ai_family == AF_INET ? "ipv4" : "",
rp->ai_family == AF_INET6? "ipv6" : "",
ndata->hostname, ndata->port, ndata->addr_str);
i++;
}
}
}
freeaddrinfo(res);
}
iio_mutex_unlock(d->lock);
*ddata = d;

return;
}

void remove_dup_discovery_data(struct dns_sd_discovery_data **ddata)
{
struct dns_sd_discovery_data *d, *ndata, *mdata;
int i, j;

d = *ddata;

if (!d)
return;

if (!d->next)
return;

iio_mutex_lock(d->lock);
for (i = 0, ndata = d; ndata->next != NULL; ndata = ndata->next) {
for (j = i + 1, mdata = ndata->next; mdata->next != NULL; mdata = mdata->next) {
if (!strcmp(mdata->hostname, ndata->hostname) &&
!strcmp(mdata->addr_str, ndata->addr_str)){
DEBUG("Removing duplicate in list: '%s'\n",
ndata->hostname);
dnssd_remove_node(&d, j);
}
j++;
}
i++;
}
iio_mutex_unlock(d->lock);

*ddata = d;
}

int dnssd_discover_host(char *addr_str, size_t addr_len, uint16_t *port)
{
struct dns_sd_discovery_data *ddata;
int ret = 0;

ret = dnssd_find_hosts(&ddata);

if (ret < 0)
return ret;

if (ddata) {
*port = ddata->port;
strncpy(addr_str, ddata->addr_str, addr_len);
}

dnssd_free_all_discovery_data(ddata);

/* negative error codes, 0 for no data */
return ret;
}

void dnssd_free_all_discovery_data(struct dns_sd_discovery_data *d)
{
while (d)
dnssd_remove_node(&d, 0);
}
Loading

0 comments on commit 30209db

Please sign in to comment.