ESCL auto-discovery uses mDNS reflector's source IP instead of the resolved device IP, causing 404 on GetCapabilities
Environment
- OS: Linux Mint (Cinnamon), X11
- NAPS2 version: 8.2.1.0 (installed via .deb / apt repo)
- Scanner: Canon MF451dw (ESCL/AirPrint capable)
- Network: client and scanner are on different subnets/VLANs, connected via a router that performs mDNS reflection/repeating between them (this is why auto-discovery is able to see the device across subnets at all)
Describe the bug
When the scanner is on a different subnet from the client, and mDNS discovery
packets are relayed across that subnet boundary by a reflector, NAPS2's ESCL
auto-discovery resolves the correct device IP from the mDNS A record, but then
issues the actual GetCapabilities HTTP request against the reflector's
address instead of the device's address. This results in a 404 (or whatever the
reflector's own web server returns) instead of a successful scan.
Manually entering the scanner's real IP address in a profile (bypassing auto-discovery) works correctly and returns valid ESCL capabilities. Only the auto-discovered device entry is affected.
To Reproduce
- Set up a network where the scanning client and the ESCL scanner are on different subnets, with a router/gateway performing mDNS reflection between them (necessary for the scanner to be discoverable across subnets at all).
- In NAPS2, open Profiles → Choose Device → ESCL Driver, and let it auto-discover the scanner via mDNS.
- Create a profile using the discovered device and attempt to scan.
- Observe an
HttpRequestException: Response status code does not indicate success: 404 (Not Found)originating fromEsclClient.GetCapabilities(). - As a comparison, create a second profile using "Manual IP" pointing directly at the scanner's real address — this succeeds.
Expected behavior
NAPS2 should issue the ESCL request against the device's resolved IP address (from the mDNS A record), not the address the discovery packet was received from.
Debug log excerpt
With debug logging enabled (About → Enable debug logging), the log shows the correct IP being resolved, immediately followed by a request to the wrong one:
Discovered ESCL Service: Canon MF450 Series, instance Canon\032MF451dw\032(23:3e:7d)._uscan._tcp.local, endpoint 192.168.200.1:5353, ipv4 192.168.100.50, ipv6 (null), host CanonMF451dw.local, port 80, tlsPort -1, uuid 00000000-0000-0000-0000-000000000000
ESCL GET http://192.168.200.1:80/eSCL/ScannerCapabilities
Response status code does not indicate success: 404 (Not Found). System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
at HttpResponseMessage System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at async Task<XDocument> NAPS2.Escl.Client.EsclClient.DoRequest(string endpoint)
at async Task<EsclCapabilities> NAPS2.Escl.Client.EsclClient.GetCapabilities()
at async Task<ValueTuple<EsclClient, EsclCapabilities>> NAPS2.Scan.Internal.Escl.EsclScanDriver.GetEsclClientWithCaps(ScanOptions options, CancellationToken cancelToken, IScanEvents scanEvents)
at async Task NAPS2.Scan.Internal.Escl.EsclScanDriver.Scan(ScanOptions options, CancellationToken cancelToken, IScanEvents scanEvents, Action<IMemoryImage> callback)
at async Task NAPS2.Scan.Internal.RemoteScanController.Scan(ScanOptions options, CancellationToken cancelToken, IScanEvents scanEvents, Action<ProcessedImage, PostProcessingContext> callback)
at async Task NAPS2.Scan.ScanController+<>c__DisplayClass15_1.<Scan>g__DoScan|7(?)+DoScan(?)
at async IAsyncEnumerable<ProcessedImage> NAPS2.Scan.ScanController.Scan(ScanOptions options, CancellationToken cancelToken)+(?) => { }
Note the Discovered ESCL Service line correctly shows ipv4 192.168.100.50
(the scanner's real address), but the very next line, ESCL GET, uses
192.168.200.1 (the endpoint address the mDNS reply was received from — in
this topology, the router/reflector, not the scanner) rather than the resolved
ipv4 value.
Additional context
This only manifests when discovery packets are relayed by something in between (e.g. an mDNS reflector bridging VLANs) so that the UDP packet's source address differs from the address embedded in the mDNS A record. On a flat single-subnet network without a reflector, the "received from" address and the device's real address are the same, so this bug wouldn't be visible — which may be why it hasn't been reported before.
I had a look at the source code. The fix seems to be straightforward. Here is a proposed diff:
When mDNS discovery packets are relayed across subnets by an mDNS
reflector/repeater, the UDP packet's source address (RemoteEndpoint) is
the reflector's address, not the scanner's. EsclClient.GetHostAndPort()
built every request URL from RemoteEndpoint, so all ESCL requests were
sent to the reflector, which returns 404 for /eSCL/* paths.
The device's real address is already parsed from the mDNS A/AAAA record
into EsclService.IpV4/IpV6; prefer that, falling back to RemoteEndpoint
only if neither was resolved. On a flat single-subnet network the two
are identical, so behavior there is unchanged.
Note: for IPv6 link-local addresses (fe80::/10) the AAAA record does not
carry a zone/scope ID while the socket-derived RemoteEndpoint does, so
link-local IPv6 continues to use RemoteEndpoint.
---
NAPS2.Escl/Client/EsclClient.cs | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/NAPS2.Escl/Client/EsclClient.cs b/NAPS2.Escl/Client/EsclClient.cs
--- a/NAPS2.Escl/Client/EsclClient.cs
+++ b/NAPS2.Escl/Client/EsclClient.cs
@@ private string GetHostAndPort(bool tls)
private string GetHostAndPort(bool tls)
{
var port = tls ? _service!.TlsPort : _service!.Port;
- var host = new IPEndPoint(_service.RemoteEndpoint, port).ToString();
+ // Prefer the IP resolved from the mDNS A/AAAA record. The address the
+ // discovery packet was received from (RemoteEndpoint) is normally the
+ // same, but when an mDNS reflector relays discovery across subnets,
+ // RemoteEndpoint is the reflector's address rather than the device's.
+ // Exception: IPv6 link-local AAAA records lack a zone/scope ID, so for
+ // those we keep using the socket-derived RemoteEndpoint.
+ var ip = _service!.IpV4
+ ?? (_service.IpV6 is { IsIPv6LinkLocal: false } ipv6 ? ipv6 : null)
+ ?? _service.RemoteEndpoint;
+ var host = new IPEndPoint(ip, port).ToString();
#if NET6_0_OR_GREATER
if (OperatingSystem.IsMacOS())
{
// Using the mDNS hostname is more reliable on Mac (but doesn't work at all on Windows)
host = $"{_service.Host}:{port}";
}
#endif
return host;
}
Ok i think I found exactly the same bug on windows: https://github.com/cyanfish/naps2/issues/874 I am on NAPS2 8.3.2.0 Can you please push an update for it? Thank you
I think you are right. Your issue does seem to be the same as this one. The same code is probably responsible for this behaviour. I would suggest you mark your issue as a duplicate to this one, and add your details here.
I proposed a fix above. It's up to the owner of the repository to review it, and possibly incorporate it into the next release. Not being the ower and have no visibility to other potential issues, I am reluctant to just create a pull request.
There is a workaround. You can manually create a profile, with a hardcoded IP address for the scanner. This works on my system.
Note for self, this is the history of behavior changes for this code: https://github.com/cyanfish/naps2/commit/7bd7df7452668d79565b13e74f71176c07f0b764 https://github.com/cyanfish/naps2/commit/8dc3bf05cdb5ccad2e03737a294bdfaa1425b837 https://github.com/cyanfish/naps2/commit/0bae0b49eff5ed541abfdd1350e377b1b6e971d7 https://github.com/cyanfish/naps2/commit/9f9399f2cf7470cead82a0b9f4dbb58a69976434
I'll have to do some testing. But maybe reasonable behavior is to use RemoteEndpoint to determine the preference between ipv4 and ipv6, then use the provided ip (on non-Mac).
Hi, it is not yet fixed in 8.4.0 for windows
Should be fixed in 8.4.1.
Adding this here because I think it's the same or a very similar issue.
NAPS2 8.4.1 on macOS AirScap eSCL service discovered successfully SRV target: airscap.local:8080 A record: 192.168.1.130 UUID matches ScannerCapabilities GET http://192.168.1.130:8080/eSCL/ScannerCapabilities returns HTTP 200 GET http://airscap.local:8080/eSCL/ScannerCapabilities times out from NAPS2 AirScap receives no request during the NAPS2 failure ping airscap.local succeeds curl using the hostname fails to resolve curl with --resolve airscap.local:8080:192.168.1.130 succeeds