Skip to content

Commit

Permalink
Allow empty path for unix socket addresses to represent unnamed addre…
Browse files Browse the repository at this point in the history
…sses and work around the null domain socket address returned by Netty client channels.
  • Loading branch information
vietj committed Jun 1, 2023
1 parent 7ae199e commit 2b69cbe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public SocketAddress convert(io.vertx.core.net.SocketAddress address) {
@Override
public io.vertx.core.net.SocketAddress convert(SocketAddress address) {
if (address instanceof DomainSocketAddress) {
String path = ((DomainSocketAddress) address).path();
if (path.isEmpty()) {
return null;
}
return new SocketAddressImpl(path);
return new SocketAddressImpl(((DomainSocketAddress) address).path());
}
return Transport.super.convert(address);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/net/SocketAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static SocketAddress inetSocketAddress(InetSocketAddress address) {
int port();

/**
* @return the domain socket path or {@code null} for a inet socket address.
* @return the domain socket path or {@code null} for inet socket address, empty path represents unnamed domain socket addresses.
*/
@CacheReturn
String path();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/vertx/core/net/impl/ConnectionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ public SocketAddress remoteAddress(boolean real) {

private SocketAddress channelLocalAddress() {
java.net.SocketAddress addr = chctx.channel().localAddress();
if (addr == null && channel().getClass().getSimpleName().endsWith("DomainSocketChannel")) {
// Workaround bug https://github.com/netty/netty/issues/13417
return SocketAddress.domainSocketAddress("");
}
return addr != null ? vertx.transport().convert(addr) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public SocketAddressImpl(int port, String host) {

public SocketAddressImpl(String path) {
Objects.requireNonNull(path, "domain socket path must be non null");
Arguments.require(!path.isEmpty(), "domain socket must not be empty");
this.port = -1;
this.host = null;
this.ipAddress = null;
Expand Down
49 changes: 31 additions & 18 deletions src/test/java/io/vertx/core/net/NetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2206,24 +2206,33 @@ public void testFanout() throws Exception {
}

@Test
public void testRemoteAddress() {
public void testSocketAddress() {
server.connectHandler(socket -> {
SocketAddress addr = socket.remoteAddress();
assertEquals("127.0.0.1", addr.host());
assertEquals(null, addr.hostName());
assertEquals("127.0.0.1", addr.hostAddress());
SocketAddress addr = socket.localAddress();
if (addr.isInetSocket()) {
assertEquals("127.0.0.1", addr.host());
assertEquals(null, addr.hostName());
assertEquals("127.0.0.1", addr.hostAddress());
} else {
assertEquals(testAddress.path(), addr.path());
assertEquals("", socket.remoteAddress().path());
}
socket.close();
}).listen(1234, "localhost", ar -> {
assertTrue(ar.succeeded());
vertx.createNetClient(new NetClientOptions()).connect(1234, "localhost", onSuccess(socket -> {
}).listen(1234, "localhost").onComplete(onSuccess(v -> {
vertx.createNetClient(new NetClientOptions()).connect(1234, "localhost").onComplete(onSuccess(socket -> {
SocketAddress addr = socket.remoteAddress();
assertEquals("localhost", addr.host());
assertEquals("localhost", addr.hostName());
assertEquals("127.0.0.1", addr.hostAddress());
assertEquals(addr.port(), 1234);
socket.closeHandler(v -> testComplete());
if (addr.isInetSocket()) {
assertEquals("localhost", addr.host());
assertEquals("localhost", addr.hostName());
assertEquals("127.0.0.1", addr.hostAddress());
assertEquals(addr.port(), 1234);
} else {
assertEquals(testAddress.path(), addr.path());
assertEquals("", socket.localAddress().path());
}
socket.closeHandler(v2 -> testComplete());
}));
});
}));
await();
}

Expand Down Expand Up @@ -4324,10 +4333,14 @@ private void testHAProxyProtocolAccepted(Buffer header, SocketAddress remote, So
server = vertx.createNetServer(new NetServerOptions()
.setUseProxyProtocol(true))
.connectHandler(so -> {
SocketAddress ra = so.remoteAddress();
SocketAddress la = so.localAddress();
assertAddresses(remote == null && testAddress.isInetSocket() ? proxy.getConnectionLocalAddress() : remote, ra);
assertAddresses(local == null && testAddress.isInetSocket() ? proxy.getConnectionRemoteAddress() : local, la);
assertAddresses(remote == null && testAddress.isInetSocket() ?
proxy.getConnectionLocalAddress() :
remote,
so.remoteAddress());
assertAddresses(local == null && testAddress.isInetSocket() ?
proxy.getConnectionRemoteAddress() :
local,
so.localAddress());
complete();
});
startServer();
Expand Down

0 comments on commit 2b69cbe

Please sign in to comment.