wsdd/c4a5f346bbb22777a8f66b0d6c6ef899f6d6cc74.patch

88 lines
3.5 KiB
Diff
Raw Normal View History

From c4a5f346bbb22777a8f66b0d6c6ef899f6d6cc74 Mon Sep 17 00:00:00 2001
From: Steffen Christgau <mail@s14u.de>
Date: Sun, 14 May 2023 21:47:16 +0200
Subject: [PATCH] fix(src): Handle multiple xaddrs in ResolveMatch/ProbeMatch
Currently, the `ProbeMatch`/`ResolveMatch` messages with multiple
transport addresses cause the `http.client.InvalidURL: URL can't contain
control characters.` exceptions. Those messages seem to be valid and can
be received e.g. from HP printers. Let's use the same approach as it was
implemented for the `Hello` message over the
https://github.com/christgau/wsdd/commit/918a1ae8a3e600366ccdf936bb247a43143f298f
commit to fix this issue.
Related: https://github.com/christgau/wsdd/issues/89
Fixes: https://github.com/christgau/wsdd/issues/149
Co-authored-by: Ondrej Holy <ondrejholy@users.noreply.github.com>
Signed-off-by: Steffen Christgau <mail@s14u.de>
---
src/wsdd.py | 33 ++++++++++++++++++++-------------
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git a/src/wsdd.py b/src/wsdd.py
index 862c6eb..39f54c9 100755
--- a/src/wsdd.py
+++ b/src/wsdd.py
@@ -738,6 +738,17 @@ def teardown(self) -> None:
def handle_packet(self, msg: str, src: Optional[UdpAddress] = None) -> None:
self.handle_message(msg, src)
+ def __extract_xaddr(self, xaddrs: str) -> Optional[str]:
+ for addr in xaddrs.strip().split():
+ if (self.mch.address.family == socket.AF_INET6) and ('//[fe80::' in addr):
+ # use first link-local address for IPv6
+ return addr
+ elif self.mch.address.family == socket.AF_INET:
+ # use first (and very likely the only) IPv4 address
+ return addr
+
+ return None
+
def handle_hello(self, header: ElementTree.Element, body: ElementTree.Element) -> Optional[WSDMessage]:
pm_path = 'wsd:Hello'
endpoint, xaddrs = self.extract_endpoint_metadata(body, pm_path)
@@ -747,17 +758,7 @@ def handle_hello(self, header: ElementTree.Element, body: ElementTree.Element) -
self.enqueue_datagram(msg, self.mch.multicast_address)
return None
- xaddr = None
- for addr in xaddrs.strip().split():
- if (self.mch.address.family == socket.AF_INET6) and ('//[fe80::' in addr):
- # use first link-local address for IPv6
- xaddr = addr
- break
- elif self.mch.address.family == socket.AF_INET:
- # use first (and very likely the only) IPv4 address
- xaddr = addr
- break
-
+ xaddr = self.__extract_xaddr(xaddrs)
if xaddr is None:
return None
@@ -790,7 +791,10 @@ def handle_probe_match(self, header: ElementTree.Element, body: ElementTree.Elem
self.enqueue_datagram(msg, self.mch.multicast_address)
return None
- xaddr = xaddrs.strip()
+ xaddr = self.__extract_xaddr(xaddrs)
+ if xaddr is None:
+ return None
+
logger.debug('probe match for {} on {}'.format(endpoint, xaddr))
self.perform_metadata_exchange(endpoint, xaddr)
@@ -809,7 +813,10 @@ def handle_resolve_match(self, header: ElementTree.Element, body: ElementTree.El
logger.debug('resolve match without endpoint/xaddr')
return None
- xaddr = xaddrs.strip()
+ xaddr = self.__extract_xaddr(xaddrs)
+ if xaddr is None:
+ return None
+
logger.debug('resolve match for {} on {}'.format(endpoint, xaddr))
self.perform_metadata_exchange(endpoint, xaddr)