diff --git a/copyparty/mdns.py b/copyparty/mdns.py index 1bc330a3..ebd2d80c 100644 --- a/copyparty/mdns.py +++ b/copyparty/mdns.py @@ -25,7 +25,7 @@ from .stolen.dnslib import ( DNSQuestion, DNSRecord, ) -from .util import CachedSet, Daemon, Netdev, min_ex +from .util import CachedSet, Daemon, Netdev, list_ips, min_ex if TYPE_CHECKING: from .svchub import SvcHub @@ -55,6 +55,7 @@ class MDNS_Sck(MC_Sck): self.bp_bye = b"" self.last_tx = 0.0 + self.tx_ex = False class MDNS(MCast): @@ -374,6 +375,14 @@ class MDNS(MCast): # avahi broadcasting 127.0.0.1-only packets return + # check if we've been given additional IPs + for ip in list_ips(): + if ip in cips: + self.sips.add(ip) + + if not self.sips.isdisjoint(cips): + return + t = "mdns zeroconf: " if self.probing: t += "Cannot start; hostname '{}' is occupied" @@ -507,6 +516,15 @@ class MDNS(MCast): if now < srv.last_tx + cooldown: return False - srv.sck.sendto(msg, (srv.grp, 5353)) - srv.last_tx = now + try: + srv.sck.sendto(msg, (srv.grp, 5353)) + srv.last_tx = now + except Exception as ex: + if srv.tx_ex: + return True + + srv.tx_ex = True + t = "tx({},|{}|,{}): {}" + self.log(t.format(srv.ip, len(msg), cooldown, ex), 3) + return True diff --git a/copyparty/util.py b/copyparty/util.py index 0e9286ab..36fbb621 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -2006,6 +2006,20 @@ def read_socket_chunked( raise Pebkac(400, t.format(x)) +def list_ips() -> list[str]: + from .stolen.ifaddr import get_adapters + + ret: set[str] = set() + for nic in get_adapters(): + for ipo in nic.ips: + if len(ipo.ip) < 7: + ret.add(ipo.ip[0]) # ipv6 is (ip,0,0) + else: + ret.add(ipo.ip) + + return list(ret) + + def yieldfile(fn: str) -> Generator[bytes, None, None]: with open(fsenc(fn), "rb", 512 * 1024) as f: while True: