Skip to content

Commit eebff52

Browse files
rgetzpcercuei
authored andcommitted
mdns: update windows to properly timeout
The current mdns code returns when there has not been any mdns traffic for 2 seconds. However, on a noisy network there is sometimes mdns traffic we don't care about, and we never timeout. Refactor things, so it keeps track of when we last success (that matches "_iio._tcp"...). Signed-off-by: Robin Getz <[email protected]> Signed-off-by: Paul Cercueil <[email protected]>
1 parent 209163a commit eebff52

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

dns_sd_windows.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#define STRINGIFY(x) _STRINGIFY(x)
2525
#define MDNS_PORT_STR STRINGIFY(MDNS_PORT)
2626

27+
static uint64_t lasttime;
28+
#define TIMEOUT 2
29+
2730
#ifdef HAVE_IPV6
2831
static const unsigned char localhost[] = {
2932
0, 0, 0, 0, 0, 0, 0, 0,
@@ -347,6 +350,7 @@ static int query_callback(int sock, const struct sockaddr *from, size_t addrlen,
347350
}
348351
}
349352
#endif /* HAVE_IPV6 */
353+
lasttime = iio_read_counter_us();
350354
iio_mutex_unlock(dd->lock);
351355
quit:
352356
return 0;
@@ -370,6 +374,8 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
370374
int transaction_id[32];
371375
int nfds, res, ret = -ENOMEM;
372376
struct timeval timeout;
377+
uint64_t nowtime;
378+
int64_t diff;
373379

374380
if (WSAStartup(versionWanted, &wsaData)) {
375381
IIO_ERROR("Failed to initialize WinSock\n");
@@ -422,9 +428,11 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
422428
IIO_DEBUG("Reading mDNS query replies\n");
423429

424430
records = 0;
431+
lasttime = iio_read_counter_us();
432+
425433
do {
426434
nfds = 0;
427-
timeout.tv_sec = 2;
435+
timeout.tv_sec = TIMEOUT;
428436
timeout.tv_usec = 0;
429437

430438
fd_set readfs;
@@ -448,6 +456,16 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
448456
FD_SET(sockets[isock], &readfs);
449457
}
450458
}
459+
460+
/* res > 0 even if we didn't process anything :(
461+
* timeout from the last time we successfully added a proper mdns record
462+
*/
463+
nowtime = iio_read_counter_us();
464+
465+
/* convert to ms */
466+
diff = (nowtime - lasttime) / 1000ull;
467+
if (diff > (TIMEOUT * 1000))
468+
res = 0;
451469
} while (res > 0);
452470

453471
for (isock = 0; isock < num_sockets; ++isock)

0 commit comments

Comments
 (0)