From c93f774c93b80ddc710e62265868d5e57da03163 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Mon, 5 Feb 2024 15:46:55 +0100 Subject: [PATCH] GH-1580: ipv6 zone id in URI should be prefixed by %25. --- .../leshan/core/endpoint/EndpointUriUtil.java | 33 +++++++++++++++++-- .../endpoint/CaliforniumServerEndpoint.java | 3 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java b/leshan-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java index 9fc3dc9eae..791ce50561 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java @@ -15,6 +15,8 @@ *******************************************************************************/ package org.eclipse.leshan.core.endpoint; +import java.net.Inet6Address; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; @@ -33,7 +35,7 @@ public static URI createUri(String scheme, String host, int port) { public static URI createUri(String scheme, InetSocketAddress addr) { try { - return new URI(scheme, null, addr.getHostString(), addr.getPort(), null, null, null); + return new URI(scheme, null, toUriHostName(addr), addr.getPort(), null, null, null); } catch (URISyntaxException e) { throw new IllegalStateException(e); } @@ -49,7 +51,7 @@ public static URI createUri(String uri) { public static URI replaceAddress(URI originalUri, InetSocketAddress newAddress) { try { - return new URI(originalUri.getScheme(), null, newAddress.getHostString(), newAddress.getPort(), null, null, + return new URI(originalUri.getScheme(), null, toUriHostName(newAddress), newAddress.getPort(), null, null, null); } catch (URISyntaxException e) { throw new IllegalStateException(e); @@ -75,4 +77,31 @@ public static void validateURI(URI uri) throws IllegalArgumentException { throw new IllegalArgumentException(String.format("Invalid URI[%s]: Post MUST NOT be undefined", uri)); } } + + /** + * This convert socket address in URI hostname. + *

+ * Following https://www.rfc-editor.org/rfc/rfc6874#section-2, zone id (also called scope id) in URI should be + * prefixed by %25 + */ + private static String toUriHostName(InetSocketAddress socketAddr) { + if (socketAddr == null) { + Validate.notNull(socketAddr); + } + InetAddress addr = socketAddr.getAddress(); + String host = addr.getHostAddress(); + if (addr instanceof Inet6Address) { + Inet6Address address6 = (Inet6Address) addr; + if (address6.getScopedInterface() != null || address6.getScopeId() > 0) { + int pos = host.indexOf('%'); + if (pos > 0 && pos + 1 < host.length()) { + String separator = "%25"; + String scope = host.substring(pos + 1); + String hostAddress = host.substring(0, pos); + host = hostAddress + separator + scope; + } + } + } + return host; + } } diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpoint.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpoint.java index 1c56d79224..fd8bed79ed 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpoint.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpoint.java @@ -32,7 +32,6 @@ import org.eclipse.leshan.core.californium.ExceptionTranslator; import org.eclipse.leshan.core.californium.SyncRequestObserver; import org.eclipse.leshan.core.californium.identity.IdentityHandler; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.request.DownlinkRequest; @@ -86,7 +85,7 @@ public Protocol getProtocol() { @Override public URI getURI() { - return EndpointUriUtil.createUri(protocol.getUriScheme(), endpoint.getAddress()); + return endpoint.getUri(); } @Override