This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git


The following commit(s) were added to refs/heads/master by this push:
     new 2b140948 Parse IPv6 socket addresses in SocketFamily.fromString
2b140948 is described below

commit 2b140948b836fd2dde9e0f3417eb8835767dc048
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Nov 6 22:22:38 2026 +0100

    Parse IPv6 socket addresses in SocketFamily.fromString
---
 .../org/mvndaemon/mvnd/common/SocketFamily.java    | 46 ++++++++++++++--------
 .../mvndaemon/mvnd/common/SocketFamilyTest.java    | 44 +++++++++++++++++++++
 2 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/SocketFamily.java 
b/common/src/main/java/org/mvndaemon/mvnd/common/SocketFamily.java
index 243244da..7c0ba29c 100644
--- a/common/src/main/java/org/mvndaemon/mvnd/common/SocketFamily.java
+++ b/common/src/main/java/org/mvndaemon/mvnd/common/SocketFamily.java
@@ -71,33 +71,45 @@ public enum SocketFamily {
         if (str.startsWith("inet:")) {
             String s = str.substring("inet:".length());
             int ic = s.lastIndexOf(':');
-            String ia = s.substring(0, ic);
-            int is = ia.indexOf('/');
-            String h = ia.substring(0, is);
-            String a = ia.substring(is + 1);
+            int is = ic > 0 ? s.indexOf('/') : -1;
+            if (is < 0 || is > ic) {
+                throw new IllegalArgumentException("Unsupported socket 
address: '" + str + "'");
+            }
+            String h = s.substring(0, is);
+            String a = s.substring(is + 1, ic);
             String p = s.substring(ic + 1);
-            InetAddress addr;
-            if ("<unresolved>".equals(a)) {
-                return InetSocketAddress.createUnresolved(h, 
Integer.parseInt(p));
-            } else {
-                if (a.indexOf('.') > 0) {
+            try {
+                int port = Integer.parseInt(p);
+                if ("<unresolved>".equals(a)) {
+                    return InetSocketAddress.createUnresolved(h, port);
+                }
+                byte[] ab;
+                if (a.length() > 2 && a.charAt(0) == '[' && 
a.charAt(a.length() - 1) == ']') {
+                    // bracketed IPv6 literal, optionally followed by a scope 
id
+                    String lit = a.substring(1, a.length() - 1);
+                    int pc = lit.indexOf('%');
+                    if (pc >= 0) {
+                        lit = lit.substring(0, pc);
+                    }
+                    if (lit.indexOf(':') < 0) {
+                        throw new IllegalArgumentException("Unsupported socket 
address: '" + str + "'");
+                    }
+                    ab = InetAddress.getByName(lit).getAddress();
+                } else if (a.indexOf('.') > 0) {
                     String[] as = a.split("\\.");
                     if (as.length != 4) {
                         throw new IllegalArgumentException("Unsupported socket 
address: '" + str + "'");
                     }
-                    byte[] ab = new byte[4];
+                    ab = new byte[4];
                     for (int i = 0; i < 4; i++) {
                         ab[i] = (byte) Integer.parseInt(as[i]);
                     }
-                    try {
-                        addr = InetAddress.getByAddress(h.isEmpty() ? null : 
h, ab);
-                    } catch (UnknownHostException e) {
-                        throw new IllegalArgumentException("Unsupported 
address: " + str, e);
-                    }
                 } else {
-                    throw new IllegalArgumentException("Unsupported address: " 
+ str);
+                    throw new IllegalArgumentException("Unsupported socket 
address: '" + str + "'");
                 }
-                return new InetSocketAddress(addr, Integer.parseInt(p));
+                return new 
InetSocketAddress(InetAddress.getByAddress(h.isEmpty() ? null : h, ab), port);
+            } catch (UnknownHostException | NumberFormatException e) {
+                throw new IllegalArgumentException("Unsupported socket 
address: '" + str + "'", e);
             }
         } else if (str.startsWith("unix:")) {
             return 
SocketHelper.unixSocketAddressOf(str.substring("unix:".length()));
diff --git 
a/common/src/test/java/org/mvndaemon/mvnd/common/SocketFamilyTest.java 
b/common/src/test/java/org/mvndaemon/mvnd/common/SocketFamilyTest.java
index d651658b..2f920153 100644
--- a/common/src/test/java/org/mvndaemon/mvnd/common/SocketFamilyTest.java
+++ b/common/src/test/java/org/mvndaemon/mvnd/common/SocketFamilyTest.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.condition.EnabledForJreRange;
 import org.junit.jupiter.api.condition.JRE;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class SocketFamilyTest {
 
@@ -66,6 +67,49 @@ public class SocketFamilyTest {
         assertEquals(i4a, 
SocketFamily.fromString("inet:google.com/<unresolved>:8080"));
     }
 
+    @Test
+    void testInetIpv6Loopback() throws UnknownHostException {
+        InetSocketAddress i6a = new InetSocketAddress(
+                InetAddress.getByAddress("localhost", new byte[] {0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}),
+                4000);
+
+        assertEquals("inet:localhost/[0:0:0:0:0:0:0:1]:4000", 
SocketFamily.toString(i6a));
+        assertEquals(i6a, 
SocketFamily.fromString("inet:localhost/[0:0:0:0:0:0:0:1]:4000"));
+        assertEquals(i6a, SocketFamily.fromString(SocketFamily.toString(i6a)));
+    }
+
+    @Test
+    void testInetIpv6NullHost() throws UnknownHostException {
+        InetSocketAddress i6a = new InetSocketAddress(
+                InetAddress.getByAddress(
+                        null, new byte[] {0x20, 0x01, 0x0d, (byte) 0xb8, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1}),
+                8080);
+
+        assertEquals(i6a, SocketFamily.fromString(SocketFamily.toString(i6a)));
+    }
+
+    @Test
+    void testInetMalformed() {
+        for (String s : new String[] {
+            "inet:localhost:4000",
+            "inet:",
+            "inet:/",
+            "inet:/1.2.3:4000",
+            "inet:/1.2.3.4",
+            "inet:/1.2.3.4:abc",
+            "inet:/1.2.3.x:4000",
+            "inet:/[::1:4000",
+            "inet:/[nonsense]:4000",
+            "inet:/[]:4000",
+            "inet:host/name:4000",
+            "bogus:/1.2.3.4:4000"
+        }) {
+            IllegalArgumentException e =
+                    assertThrows(IllegalArgumentException.class, () -> 
SocketFamily.fromString(s), s);
+            assertEquals("Unsupported socket address: '" + s + "'", 
e.getMessage());
+        }
+    }
+
     @Test
     @EnabledForJreRange(min = JRE.JAVA_16)
     void testUnixFromTo() {

Reply via email to