Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import sun.net.ResourceManager;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.IPAddressUtil;
import sun.net.util.SocketExceptions;
import jdk.internal.util.Exceptions;

/**
* Default Socket Implementation. This implementation does
Expand Down Expand Up @@ -535,7 +535,7 @@ synchronized void doConnect(InetAddress address, int port, int timeout) throws I
}
} catch (IOException e) {
close();
throw SocketExceptions.of(e, new InetSocketAddress(address, port));
throw Exceptions.ioException(e, new InetSocketAddress(address, port));
}
}

Expand Down
42 changes: 24 additions & 18 deletions src/java.base/share/classes/java/net/HostPortrange.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,6 +30,9 @@
import java.util.Locale;
import sun.net.util.IPAddressUtil;

import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* Parses a string containing a host/domain name and port range
*/
Expand Down Expand Up @@ -57,7 +60,7 @@ public int hashCode() {
return hostname.hashCode() + portrange[0] + portrange[1];
}

HostPortrange(String scheme, String str) {
HostPortrange(String scheme, String host) {
// Parse the host name. A name has up to three components, the
// hostname, a port number, or two numbers representing a port
// range. "www.example.com:8080-9090" is a valid host name.
Expand All @@ -68,21 +71,23 @@ public int hashCode() {
// Refer to RFC 2732 for more information.

// first separate string into two fields: hoststr, portstr
String hoststr, portstr = null;
String hoststr = null, portstr = null;
this.scheme = scheme;

// check for IPv6 address
if (str.charAt(0) == '[') {
if (host.charAt(0) == '[') {
ipv6 = literal = true;
int rb = str.indexOf(']');
int rb = host.indexOf(']');
if (rb != -1) {
hoststr = str.substring(1, rb);
hoststr = host.substring(1, rb);
} else {
throw new IllegalArgumentException("invalid IPv6 address: " + str);
throw new IllegalArgumentException(
formatMsg("invalid IPv6 address%s",
filterNonSocketInfo(host).prefixWith(": ")));
}
int sep = str.indexOf(':', rb + 1);
if (sep != -1 && str.length() > sep) {
portstr = str.substring(sep + 1);
int sep = host.indexOf(':', rb + 1);
if (sep != -1 && host.length() > sep) {
portstr = host.substring(sep + 1);
}
// need to normalize hoststr now
byte[] ip = IPAddressUtil.textToNumericFormatV6(hoststr);
Expand All @@ -95,16 +100,16 @@ public int hashCode() {
+ "%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7], ip[8],
ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15]);
hostname = sb.toString();
this.hostname = sb.toString();
} else {
// not IPv6 therefore ':' is the port separator

int sep = str.indexOf(':');
if (sep != -1 && str.length() > sep) {
hoststr = str.substring(0, sep);
portstr = str.substring(sep + 1);
int sep = host.indexOf(':');
if (sep != -1 && host.length() > sep) {
hoststr = host.substring(0, sep);
portstr = host.substring(sep + 1);
} else {
hoststr = sep == -1 ? str : str.substring(0, sep);
hoststr = sep == -1 ? host : host.substring(0, sep);
}
// is this a domain wildcard specification?
if (hoststr.lastIndexOf('*') > 0) {
Expand Down Expand Up @@ -151,13 +156,14 @@ public int hashCode() {
}
}
}
hostname = hoststr;
this.hostname = hoststr;
}

try {
portrange = parsePort(portstr);
} catch (Exception e) {
throw new IllegalArgumentException("invalid port range: " + portstr);
throw new IllegalArgumentException(
formatMsg("invalid port range%s", filterNonSocketInfo(portstr).prefixWith(": ")));
}
}

Expand Down
46 changes: 30 additions & 16 deletions src/java.base/share/classes/java/net/InetAddress.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -46,13 +46,17 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.Arrays;

import jdk.internal.util.Exceptions;
import jdk.internal.access.JavaNetInetAddressAccess;
import jdk.internal.access.SharedSecrets;
import sun.security.action.*;
import sun.net.InetAddressCachePolicy;
import sun.net.util.IPAddressUtil;
import sun.nio.cs.UTF_8;

import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents an Internet Protocol (IP) address.
*
Expand Down Expand Up @@ -340,6 +344,7 @@ public byte[] addressBytes(Inet6Address inet6Address) {
}
}
);
Exceptions.setup(); // needed for native exceptions
init();
}

Expand Down Expand Up @@ -798,7 +803,7 @@ private static final class CachedAddresses implements Addresses, Comparable<Cac
@Override
public InetAddress[] get() throws UnknownHostException {
if (inetAddresses == null) {
throw new UnknownHostException(host);
throw new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)));
}
return inetAddresses;
}
Expand Down Expand Up @@ -876,7 +881,9 @@ public InetAddress[] get() throws UnknownHostException {
}
}
if (inetAddresses == null) {
throw ex == null ? new UnknownHostException(host) : ex;
throw ex == null
? new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)))
: ex;
}
return inetAddresses;
}
Expand Down Expand Up @@ -999,16 +1006,19 @@ public String getHostByAddr(byte[] addr) throws UnknownHostException {
}
}
} catch (IOException e) {
throw new UnknownHostException("Unable to resolve address "
+ Arrays.toString(addr) + " as hosts file " + hostsFile
+ " not found ");
throw new UnknownHostException(
formatMsg("Unable to resolve address %s as hosts file %s not found",
filterNonSocketInfo(Arrays.toString(addr)),
filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));
}

if ((host == null) || (host.isEmpty()) || (host.equals(" "))) {
throw new UnknownHostException("Requested address "
+ Arrays.toString(addr)
+ " resolves to an invalid entry in hosts file "
+ hostsFile);
throw new UnknownHostException(
formatMsg("Requested address %s resolves to an invalid entry in hosts file %s",
filterNonSocketInfo(Arrays.toString(addr)),
filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));
}
return host;
}
Expand Down Expand Up @@ -1060,8 +1070,11 @@ public InetAddress[] lookupAllHostAddr(String host)
}
}
} catch (IOException e) {
throw new UnknownHostException("Unable to resolve host " + host
+ " as hosts file " + hostsFile + " not found ");
throw new UnknownHostException(
formatMsg("Unable to resolve host %s as hosts file %s not found",
filterNonSocketInfo(host), filterNonSocketInfo(hostsFile)
.replaceWith("from ${jdk.net.hosts.file} system property")));

}

List<InetAddress> res;
Expand Down Expand Up @@ -1340,7 +1353,7 @@ private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
try {
addr = IPAddressUtil.validateNumericFormatV4(host);
} catch (IllegalArgumentException iae) {
var uhe = new UnknownHostException(host);
var uhe = new UnknownHostException(formatMsg("%s", filterNonSocketInfo(host)));
uhe.initCause(iae);
throw uhe;
}
Expand Down Expand Up @@ -1387,7 +1400,8 @@ private static InetAddress[] getAllByName(String host, InetAddress reqAddr)

private static UnknownHostException invalidIPv6LiteralException(String host, boolean wrapInBrackets) {
String hostString = wrapInBrackets ? "[" + host + "]" : host;
return new UnknownHostException(hostString + ": invalid IPv6 address literal");
return new UnknownHostException(formatMsg("%sinvalid IPv6 address literal",
filterNonSocketInfo(hostString).suffixWith(": ")));
}

/**
Expand Down Expand Up @@ -1667,8 +1681,8 @@ public static InetAddress getLocalHost() throws UnknownHostException {
} catch (UnknownHostException uhe) {
// Rethrow with a more informative error message.
UnknownHostException uhe2 =
new UnknownHostException(local + ": " +
uhe.getMessage());
new UnknownHostException(formatMsg(filterNonSocketInfo(local)
.suffixWith(": ") + uhe.getMessage()));
uhe2.initCause(uhe);
throw uhe2;
}
Expand Down
6 changes: 5 additions & 1 deletion src/java.base/share/classes/java/net/NetworkInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents a Network Interface made up of a name,
Expand Down Expand Up @@ -328,7 +330,9 @@ public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketE
+ addr.holder.family);
}
} else {
throw new IllegalArgumentException("invalid address type: " + addr);
throw new IllegalArgumentException(
formatMsg("invalid address type%s",
filterNonSocketInfo(addr.toString()).prefixWith(": ")));
}
return getByInetAddress0(addr);
}
Expand Down
12 changes: 9 additions & 3 deletions src/java.base/share/classes/java/net/Proxy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,9 @@

package java.net;

import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents a proxy setting, typically a type (http, socks) and
* a socket address.
Expand Down Expand Up @@ -91,8 +94,11 @@ private Proxy() {
* incompatible
*/
public Proxy(Type type, SocketAddress sa) {
if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))
throw new IllegalArgumentException("type " + type + " is not compatible with address " + sa);
if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress)) {
throw new IllegalArgumentException(
formatMsg("type " + type + " is not compatible with address %s",
filterNonSocketInfo(String.valueOf(sa))));
}
this.type = type;
this.sa = sa;
}
Expand Down
14 changes: 8 additions & 6 deletions src/java.base/share/classes/java/net/SocketPermission.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -49,6 +49,8 @@
import sun.security.util.SecurityConstants;
import sun.security.util.Debug;

import static jdk.internal.util.Exceptions.filterNonSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* This class represents access to a network via sockets.
Expand Down Expand Up @@ -424,8 +426,8 @@ private void init(String host, int mask) {
if (rb != -1) {
host = host.substring(start, rb);
} else {
throw new
IllegalArgumentException("invalid host/port: "+host);
throw new IllegalArgumentException(
formatMsg("invalid host/port%s", filterNonSocketInfo(host).prefixWith(": ")));
}
sep = hostport.indexOf(':', rb+1);
} else {
Expand All @@ -442,8 +444,8 @@ private void init(String host, int mask) {
try {
portrange = parsePort(port);
} catch (Exception e) {
throw new
IllegalArgumentException("invalid port range: "+port);
throw new IllegalArgumentException(
formatMsg("invalid port range%s", filterNonSocketInfo(port).prefixWith(": ")));
}
} else {
portrange = new int[] { PORT_MIN, PORT_MAX };
Expand Down Expand Up @@ -819,7 +821,7 @@ void getIP()
throw uhe;
} catch (IndexOutOfBoundsException iobe) {
invalid = true;
throw new UnknownHostException(getName());
throw new UnknownHostException(formatMsg("%s", filterNonSocketInfo(getName())));
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/java.base/share/classes/java/net/SocksSocketImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,6 +37,9 @@
import sun.net.spi.DefaultProxySelector;
import sun.net.www.ParseUtil;

import static jdk.internal.util.Exceptions.filterSocketInfo;
import static jdk.internal.util.Exceptions.formatMsg;

/**
* SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
*/
Expand Down Expand Up @@ -375,7 +378,7 @@ public ProxySelector run() {
// SOCKS Protocol version 4 doesn't know how to deal with
// DOMAIN type of addresses (unresolved addresses here)
if (epoint.isUnresolved())
throw new UnknownHostException(epoint.toString());
throw new UnknownHostException(formatMsg("%s", filterSocketInfo(epoint.toString())));
connectV4(in, out, epoint, deadlineMillis);
return;
}
Expand All @@ -394,7 +397,7 @@ public ProxySelector run() {
// SOCKS Protocol version 4 doesn't know how to deal with
// DOMAIN type of addresses (unresolved addresses here)
if (epoint.isUnresolved())
throw new UnknownHostException(epoint.toString());
throw new UnknownHostException(formatMsg("%s", filterSocketInfo(epoint.toString())));
connectV4(in, out, epoint, deadlineMillis);
return;
}
Expand Down
Loading