Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #6410 - Use SocketAddress instead of InetSocketAddress. #6414

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,7 +13,7 @@

package org.eclipse.jetty.docs.programming.server.http2;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void accept()
@Override
public void onAccept(Session session)
{
InetSocketAddress remoteAddress = session.getRemoteAddress();
SocketAddress remoteAddress = session.getRemoteSocketAddress();
System.getLogger("http2").log(INFO, "Connection from {0}", remoteAddress);
}
};
Expand Down
1 change: 0 additions & 1 deletion jetty-client/src/main/java/module-info.java
Expand Up @@ -18,7 +18,6 @@
exports org.eclipse.jetty.client.dynamic;
exports org.eclipse.jetty.client.http;
exports org.eclipse.jetty.client.jmx to org.eclipse.jetty.jmx;
exports org.eclipse.jetty.client.proxy;
exports org.eclipse.jetty.client.util;

requires org.eclipse.jetty.alpn.client;
Expand Down
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jetty.client;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected void doStart() throws Exception
}

@Override
public void connect(InetSocketAddress address, Map<String, Object> context)
public void connect(SocketAddress address, Map<String, Object> context)
{
HttpDestination destination = (HttpDestination)context.get(HTTP_DESTINATION_CONTEXT_KEY);
context.put(ClientConnector.CLIENT_CONNECTION_FACTORY_CONTEXT_KEY, destination.getClientConnectionFactory());
Expand All @@ -71,4 +72,10 @@ public void connect(InetSocketAddress address, Map<String, Object> context)
context.put(ClientConnector.CONNECTION_PROMISE_CONTEXT_KEY, Promise.from(ioConnection -> {}, promise::failed));
connector.connect(address, context);
}

@Override
public void connect(InetSocketAddress address, Map<String, Object> context)
{
connect((SocketAddress)address, context);
}
}
Expand Up @@ -590,7 +590,7 @@ public void failed(Throwable x)
connect(socketAddresses, nextIndex, context);
}
});
transport.connect(socketAddresses.get(index), context);
transport.connect((SocketAddress)socketAddresses.get(index), context);
}
});
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jetty.client;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;

import org.eclipse.jetty.io.ClientConnectionFactory;
Expand Down Expand Up @@ -69,9 +70,25 @@ public interface HttpClientTransport extends ClientConnectionFactory
*
* @param address the address to connect to
* @param context the context information to establish the connection
* @deprecated use {@link #connect(SocketAddress, Map)} instead.
*/
@Deprecated
public void connect(InetSocketAddress address, Map<String, Object> context);

/**
* Establishes a physical connection to the given {@code address}.
*
* @param address the address to connect to
* @param context the context information to establish the connection
*/
public default void connect(SocketAddress address, Map<String, Object> context)
{
if (address instanceof InetSocketAddress)
connect((InetSocketAddress)address, context);
else
throw new UnsupportedOperationException("Unsupported SocketAddress " + address);
}

/**
* @return the factory for ConnectionPool instances
*/
Expand Down
Expand Up @@ -16,6 +16,7 @@
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand Down Expand Up @@ -60,10 +61,18 @@ protected ProxyProtocolConnection newProxyProtocolConnection(EndPoint endPoint,
Tag tag = (Tag)destination.getOrigin().getTag();
if (tag == null)
{
InetSocketAddress local = endPoint.getLocalAddress();
InetSocketAddress remote = endPoint.getRemoteAddress();
boolean ipv4 = local.getAddress() instanceof Inet4Address;
tag = new Tag(ipv4 ? "TCP4" : "TCP6", local.getAddress().getHostAddress(), local.getPort(), remote.getAddress().getHostAddress(), remote.getPort());
SocketAddress local = endPoint.getLocalSocketAddress();
InetSocketAddress inetLocal = local instanceof InetSocketAddress ? (InetSocketAddress)local : null;
InetAddress localAddress = inetLocal == null ? null : inetLocal.getAddress();
SocketAddress remote = endPoint.getRemoteSocketAddress();
InetSocketAddress inetRemote = remote instanceof InetSocketAddress ? (InetSocketAddress)remote : null;
InetAddress remoteAddress = inetRemote == null ? null : inetRemote.getAddress();
String family = local == null || inetLocal == null ? "UNKNOWN" : localAddress instanceof Inet4Address ? "TCP4" : "TCP6";
tag = new Tag(family,
localAddress == null ? null : localAddress.getHostAddress(),
inetLocal == null ? 0 : inetLocal.getPort(),
remoteAddress == null ? null : remoteAddress.getHostAddress(),
inetRemote == null ? 0 : inetRemote.getPort());
}
return new ProxyProtocolConnectionV1(endPoint, executor, getClientConnectionFactory(), context, tag);
}
Expand Down Expand Up @@ -198,10 +207,21 @@ protected ProxyProtocolConnection newProxyProtocolConnection(EndPoint endPoint,
Tag tag = (Tag)destination.getOrigin().getTag();
if (tag == null)
{
InetSocketAddress local = endPoint.getLocalAddress();
InetSocketAddress remote = endPoint.getRemoteAddress();
boolean ipv4 = local.getAddress() instanceof Inet4Address;
tag = new Tag(Tag.Command.PROXY, ipv4 ? Tag.Family.INET4 : Tag.Family.INET6, Tag.Protocol.STREAM, local.getAddress().getHostAddress(), local.getPort(), remote.getAddress().getHostAddress(), remote.getPort(), null);
SocketAddress local = endPoint.getLocalSocketAddress();
InetSocketAddress inetLocal = local instanceof InetSocketAddress ? (InetSocketAddress)local : null;
InetAddress localAddress = inetLocal == null ? null : inetLocal.getAddress();
SocketAddress remote = endPoint.getRemoteSocketAddress();
InetSocketAddress inetRemote = remote instanceof InetSocketAddress ? (InetSocketAddress)remote : null;
InetAddress remoteAddress = inetRemote == null ? null : inetRemote.getAddress();
Tag.Family family = local == null || inetLocal == null ? Tag.Family.UNSPEC : localAddress instanceof Inet4Address ? Tag.Family.INET4 : Tag.Family.INET6;
tag = new Tag(Tag.Command.PROXY,
family,
Tag.Protocol.STREAM,
localAddress == null ? null : localAddress.getHostAddress(),
inetLocal == null ? 0 : inetLocal.getPort(),
remoteAddress == null ? null : remoteAddress.getHostAddress(),
inetRemote == null ? 0 : inetRemote.getPort(),
null);
}
return new ProxyProtocolConnectionV2(endPoint, executor, getClientConnectionFactory(), context, tag);
}
Expand All @@ -220,14 +240,14 @@ public static class Tag implements ClientConnectionFactory.Decorator
*/
public static final Tag LOCAL = new Tag(Command.LOCAL, Family.UNSPEC, Protocol.UNSPEC, null, 0, null, 0, null);

private Command command;
private Family family;
private Protocol protocol;
private String srcIP;
private int srcPort;
private String dstIP;
private int dstPort;
private List<TLV> tlvs;
private final Command command;
private final Family family;
private final Protocol protocol;
private final String srcIP;
private final int srcPort;
private final String dstIP;
private final int dstPort;
private final List<TLV> tlvs;

/**
* <p>Creates a Tag whose metadata will be derived from the underlying EndPoint.</p>
Expand Down Expand Up @@ -514,32 +534,36 @@ protected void writePROXYBytes(EndPoint endPoint, Callback callback)
{
try
{
InetSocketAddress localAddress = endPoint.getLocalAddress();
InetSocketAddress remoteAddress = endPoint.getRemoteAddress();
SocketAddress local = endPoint.getLocalSocketAddress();
InetSocketAddress inetLocal = local instanceof InetSocketAddress ? (InetSocketAddress)local : null;
InetAddress localAddress = inetLocal == null ? null : inetLocal.getAddress();
SocketAddress remote = endPoint.getRemoteSocketAddress();
InetSocketAddress inetRemote = remote instanceof InetSocketAddress ? (InetSocketAddress)remote : null;
InetAddress remoteAddress = inetRemote == null ? null : inetRemote.getAddress();
String family = tag.getFamily();
String srcIP = tag.getSourceAddress();
int srcPort = tag.getSourcePort();
String dstIP = tag.getDestinationAddress();
int dstPort = tag.getDestinationPort();
if (family == null)
family = localAddress.getAddress() instanceof Inet4Address ? "TCP4" : "TCP6";
family = local == null || inetLocal == null ? "UNKNOWN" : localAddress instanceof Inet4Address ? "TCP4" : "TCP6";
family = family.toUpperCase(Locale.ENGLISH);
boolean unknown = family.equals("UNKNOWN");
StringBuilder builder = new StringBuilder(64);
builder.append("PROXY ").append(family);
if (!unknown)
{
if (srcIP == null)
srcIP = localAddress.getAddress().getHostAddress();
if (srcIP == null && localAddress != null)
srcIP = localAddress.getHostAddress();
builder.append(" ").append(srcIP);
if (dstIP == null)
dstIP = remoteAddress.getAddress().getHostAddress();
if (dstIP == null && remoteAddress != null)
dstIP = remoteAddress.getHostAddress();
builder.append(" ").append(dstIP);
if (srcPort <= 0)
srcPort = localAddress.getPort();
if (srcPort <= 0 && inetLocal != null)
srcPort = inetLocal.getPort();
builder.append(" ").append(srcPort);
if (dstPort <= 0)
dstPort = remoteAddress.getPort();
if (dstPort <= 0 && inetRemote != null)
dstPort = inetRemote.getPort();
builder.append(" ").append(dstPort);
}
builder.append("\r\n");
Expand Down Expand Up @@ -590,16 +614,19 @@ protected void writePROXYBytes(EndPoint endPoint, Callback callback)
buffer.put((byte)versionAndCommand);
V2.Tag.Family family = tag.getFamily();
String srcAddr = tag.getSourceAddress();
if (srcAddr == null)
srcAddr = endPoint.getLocalAddress().getAddress().getHostAddress();
SocketAddress local = endPoint.getLocalSocketAddress();
InetSocketAddress inetLocal = local instanceof InetSocketAddress ? (InetSocketAddress)local : null;
InetAddress localAddress = inetLocal == null ? null : inetLocal.getAddress();
if (srcAddr == null && localAddress != null)
srcAddr = localAddress.getHostAddress();
int srcPort = tag.getSourcePort();
if (srcPort <= 0)
srcPort = endPoint.getLocalAddress().getPort();
if (srcPort <= 0 && inetLocal != null)
srcPort = inetLocal.getPort();
if (family == null)
family = InetAddress.getByName(srcAddr) instanceof Inet4Address ? V2.Tag.Family.INET4 : V2.Tag.Family.INET6;
family = local == null || inetLocal == null ? V2.Tag.Family.UNSPEC : localAddress instanceof Inet4Address ? V2.Tag.Family.INET4 : V2.Tag.Family.INET6;
V2.Tag.Protocol protocol = tag.getProtocol();
if (protocol == null)
protocol = V2.Tag.Protocol.STREAM;
protocol = local == null ? V2.Tag.Protocol.UNSPEC : V2.Tag.Protocol.STREAM;
int familyAndProtocol = (family.ordinal() << 4) | protocol.ordinal();
buffer.put((byte)familyAndProtocol);
int length = 0;
Expand All @@ -622,11 +649,14 @@ protected void writePROXYBytes(EndPoint endPoint, Callback callback)
length += vectorsLength;
buffer.putShort((short)length);
String dstAddr = tag.getDestinationAddress();
if (dstAddr == null)
dstAddr = endPoint.getRemoteAddress().getAddress().getHostAddress();
SocketAddress remote = endPoint.getRemoteSocketAddress();
InetSocketAddress inetRemote = remote instanceof InetSocketAddress ? (InetSocketAddress)remote : null;
InetAddress remoteAddress = inetRemote == null ? null : inetRemote.getAddress();
if (dstAddr == null && remoteAddress != null)
dstAddr = remoteAddress.getHostAddress();
int dstPort = tag.getDestinationPort();
if (dstPort <= 0)
dstPort = endPoint.getRemoteAddress().getPort();
if (dstPort <= 0 && inetRemote != null)
dstPort = inetRemote.getPort();
switch (family)
{
case UNSPEC:
Expand All @@ -640,9 +670,14 @@ protected void writePROXYBytes(EndPoint endPoint, Callback callback)
break;
case UNIX:
int position = buffer.position();
buffer.put(srcAddr.getBytes(StandardCharsets.US_ASCII));
buffer.position(position + 108);
buffer.put(dstAddr.getBytes(StandardCharsets.US_ASCII));
if (srcAddr != null)
buffer.put(srcAddr.getBytes(StandardCharsets.US_ASCII));
position = position + 108;
buffer.position(position);
if (dstAddr != null)
buffer.put(dstAddr.getBytes(StandardCharsets.US_ASCII));
position = position + 108;
buffer.position(position);
break;
default:
throw new IllegalStateException();
Expand Down
Expand Up @@ -256,8 +256,8 @@ public String toConnectionString()
return String.format("%s@%x(l:%s <-> r:%s,closed=%b)=>%s",
getClass().getSimpleName(),
hashCode(),
getEndPoint().getLocalAddress(),
getEndPoint().getRemoteAddress(),
getEndPoint().getLocalSocketAddress(),
getEndPoint().getRemoteSocketAddress(),
closed.get(),
channel);
}
Expand Down