Skip to content

Commit d88bac2

Browse files
rgetzSRaus
authored andcommitted
iiod: Make sure network is alive before poking avahi
In the past, we were only checking hostname, as a way to see if the network was ready, but unfortunately - that doesn't work. The kernel really only needs to have a hostname set for uname() to work, which is what glibc and uclibc implement in gethostname(). So - now we check there is a working, configured ethernet card, that supports mdns (ie. multicast) that is up before moving on to making sure there is a hostname. We also don't allow "none" or "(none)" to be the hostname until after things time out (3 min). This fixes #1072 and turns: 192.168.2.1:5353 : answer _iio._tcp.local. PTR "iiod on (none)._iio._tcp.local." rclass 0x1 ttl 10 length 17 into: 192.168.2.1:5353 : answer _iio._tcp.local. PTR "iiod on pluto._iio._tcp.local." rclass 0x1 ttl 10 length 16 and when you have multiple on network (with same name): 192.168.1.110:5353 : answer _iio._tcp.local. PTR "iiod on pluto._iio._tcp.local." rclass 0x1 ttl 10 length 16 192.168.1.110:5353 : answer pluto.local. A 192.168.1.110 192.168.1.115:5353 : answer _iio._tcp.local. PTR "iiod on pluto #2._iio._tcp.local." rclass 0x1 ttl 10 length 19 192.168.1.115:5353 : answer pluto-2.local. A 192.168.1.115 Signed-off-by: Robin Getz <[email protected]> [pcercuei: change %d to %ld in print format string] Signed-off-by: Paul Cercueil <[email protected]> (cherry picked from commit 38483f3)
1 parent 8fb78e0 commit d88bac2

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

iiod/dns-sd.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <avahi-client/publish.h>
2222
#include <avahi-common/domain.h>
2323

24+
#include <ifaddrs.h>
25+
#include <net/if.h>
2426
#include <stddef.h>
2527
#include <time.h>
2628

@@ -242,21 +244,62 @@ static void create_services(AvahiClient *c)
242244
}
243245

244246
#define IIOD_ON "iiod on "
247+
#define TIMEOUT 20
245248

246249
static void start_avahi_thd(struct thread_pool *pool, void *d)
247250
{
248-
249251
char label[AVAHI_LABEL_MAX];
250252
char host[AVAHI_LABEL_MAX - sizeof(IIOD_ON)];
251253
struct timespec ts;
252-
int ret;
254+
int ret, net = 0;
253255

254256
ts.tv_nsec = 0;
255257
ts.tv_sec = 1;
256258

259+
/*
260+
* Try to make sure the network is up before letting avahi
261+
* know we are here, and advertising on the network.
262+
* However, if we are on the last try before we timeout,
263+
* ignore some prerequisites, and just assume it will be
264+
* OK later (if someone boots, and later plugs in USB <-> ethernet).
265+
*/
257266
while(true) {
267+
struct ifaddrs *ifaddr = 0;
268+
struct ifaddrs *ifa = 0;
269+
270+
if (!net && ts.tv_sec < TIMEOUT) {
271+
/* Ensure networking is alive */
272+
ret = getifaddrs(&ifaddr);
273+
if (ret)
274+
goto again;
275+
276+
/* Make sure at least one practical interface is up and ready */
277+
for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
278+
/* no address */
279+
if (!ifa->ifa_addr)
280+
continue;
281+
/* Interface is running, think ifup */
282+
if (!(ifa->ifa_flags & IFF_UP))
283+
continue;
284+
/* supports multicast (i.e. MDNS) */
285+
if (!(ifa->ifa_flags & IFF_MULTICAST))
286+
continue;
287+
/* Interface is not a loopback interface */
288+
if ((ifa->ifa_flags & IFF_LOOPBACK))
289+
continue;
290+
IIO_INFO("found applicable network for mdns on %s\n", ifa->ifa_name);
291+
net++;
292+
}
293+
freeifaddrs(ifaddr);
294+
if (!net)
295+
goto again;
296+
}
297+
298+
/* Get the hostname, which on uClibc, can return (none)
299+
* rather than fail/zero length string, like on glibc */
258300
ret = gethostname(host, sizeof(host));
259-
if (ret || !strcmp(host, "none"))
301+
if (ret || !host[0] || (ts.tv_sec < TIMEOUT && (!strcmp(host, "none") ||
302+
!strcmp(host, "(none)"))))
260303
goto again;
261304

262305
snprintf(label, sizeof(label), "%s%s", IIOD_ON, host);
@@ -277,13 +320,13 @@ static void start_avahi_thd(struct thread_pool *pool, void *d)
277320
if (avahi.client)
278321
break;
279322
again:
280-
IIO_INFO("Avahi didn't start, try again later\n");
323+
IIO_INFO("Avahi didn't start, try again in %ld seconds later\n", ts.tv_sec);
281324
nanosleep(&ts, NULL);
282325
ts.tv_sec++;
283-
/* If it hasn't started in 10 times over 60 seconds,
326+
/* If it hasn't started in 20 times over 210 seconds (3.5 min),
284327
* it is not going to, so stop
285328
*/
286-
if (ts.tv_sec >= 11)
329+
if (ts.tv_sec > TIMEOUT)
287330
break;
288331
}
289332

0 commit comments

Comments
 (0)