diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java index bfaf5da550..1925887676 100644 --- a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java @@ -16,11 +16,11 @@ package io.netty.handler.codec.socks; import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; import org.junit.Test; -import java.net.InetAddress; import java.net.UnknownHostException; import static org.junit.Assert.*; @@ -65,7 +65,7 @@ public class SocksCmdRequestDecoderTest { @Test public void testCmdRequestDecoderIPv6() throws UnknownHostException { - String[] hosts = {SocksCommonUtils.ipv6toStr(InetAddress.getByName("::1").getAddress())}; + String[] hosts = {SocksCommonUtils.ipv6toStr(SocketUtils.addressByName("::1").getAddress())}; int[] ports = {1, 32769, 65535}; for (SocksCmdType cmdType : SocksCmdType.values()) { for (String host : hosts) { diff --git a/common/src/main/java/io/netty/util/NetUtil.java b/common/src/main/java/io/netty/util/NetUtil.java index 4144f16352..d4f2354f10 100644 --- a/common/src/main/java/io/netty/util/NetUtil.java +++ b/common/src/main/java/io/netty/util/NetUtil.java @@ -16,6 +16,7 @@ package io.netty.util; import io.netty.util.internal.PlatformDependent; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -156,7 +157,7 @@ public final class NetUtil { for (Enumeration i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) { NetworkInterface iface = i.nextElement(); // Use the interface with proper INET addresses only. - if (iface.getInetAddresses().hasMoreElements()) { + if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) { ifaces.add(iface); } } @@ -170,7 +171,7 @@ public final class NetUtil { NetworkInterface loopbackIface = null; InetAddress loopbackAddr = null; loop: for (NetworkInterface iface: ifaces) { - for (Enumeration i = iface.getInetAddresses(); i.hasMoreElements();) { + for (Enumeration i = SocketUtils.addressesFromNetworkInterface(iface); i.hasMoreElements();) { InetAddress addr = i.nextElement(); if (addr.isLoopbackAddress()) { // Found @@ -186,7 +187,7 @@ public final class NetUtil { try { for (NetworkInterface iface: ifaces) { if (iface.isLoopback()) { - Enumeration i = iface.getInetAddresses(); + Enumeration i = SocketUtils.addressesFromNetworkInterface(iface); if (i.hasMoreElements()) { // Found the one with INET address. loopbackIface = iface; diff --git a/common/src/main/java/io/netty/util/internal/SocketUtils.java b/common/src/main/java/io/netty/util/internal/SocketUtils.java new file mode 100644 index 0000000000..6d566d7d5c --- /dev/null +++ b/common/src/main/java/io/netty/util/internal/SocketUtils.java @@ -0,0 +1,197 @@ +/* + * Copyright 2016 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.util.internal; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.net.SocketPermission; +import java.net.UnknownHostException; +import java.nio.channels.DatagramChannel; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Enumeration; + +/** + * Provides socket operations with privileges enabled. This is necessary for applications that use the + * {@link SecurityManager} to restrict {@link SocketPermission} to their application. By asserting that these + * operations are privileged, the operations can proceed even if some code in the calling chain lacks the appropriate + * {@link SocketPermission}. + */ +public final class SocketUtils { + + private SocketUtils() { + } + + public static void connect(final Socket socket, final SocketAddress remoteAddress, final int timeout) + throws IOException { + try { + AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public Void run() throws IOException { + socket.connect(remoteAddress, timeout); + return null; + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + public static void bind(final Socket socket, final SocketAddress bindpoint) throws IOException { + try { + AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public Void run() throws IOException { + socket.bind(bindpoint); + return null; + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress) + throws IOException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public Boolean run() throws IOException { + return socketChannel.connect(remoteAddress); + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException { + try { + AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public Void run() throws IOException { + socketChannel.bind(address); + return null; + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public SocketChannel run() throws IOException { + return serverSocketChannel.accept(); + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException { + try { + AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public Void run() throws IOException { + networkChannel.bind(address); + return null; + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + public static SocketAddress localSocketAddress(final ServerSocket socket) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public SocketAddress run() { + return socket.getLocalSocketAddress(); + } + }); + } + + public static InetAddress addressByName(final String hostname) throws UnknownHostException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public InetAddress run() throws UnknownHostException { + return InetAddress.getByName(hostname); + } + }); + } catch (PrivilegedActionException e) { + throw (UnknownHostException) e.getCause(); + } + } + + public static InetAddress[] allAddressesByName(final String hostname) throws UnknownHostException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public InetAddress[] run() throws UnknownHostException { + return InetAddress.getAllByName(hostname); + } + }); + } catch (PrivilegedActionException e) { + throw (UnknownHostException) e.getCause(); + } + } + + public static InetSocketAddress socketAddress(final String hostname, final int port) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public InetSocketAddress run() { + return new InetSocketAddress(hostname, port); + } + }); + } + + public static Enumeration addressesFromNetworkInterface(final NetworkInterface intf) { + return AccessController.doPrivileged(new PrivilegedAction>() { + @Override + public Enumeration run() { + return intf.getInetAddresses(); + } + }); + } + + public static byte[] hardwareAddressFromNetworkInterface(final NetworkInterface intf) throws SocketException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public byte[] run() throws SocketException { + return intf.getHardwareAddress(); + } + }); + } catch (PrivilegedActionException e) { + throw (SocketException) e.getCause(); + } + } +} diff --git a/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java b/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java index b5d9786d38..38366a4391 100644 --- a/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java +++ b/example/src/main/java/io/netty/example/http/upload/HttpUploadClient.java @@ -38,10 +38,10 @@ import io.netty.handler.codec.http.multipart.InterfaceHttpData; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; +import io.netty.util.internal.SocketUtils; import java.io.File; import java.io.FileNotFoundException; -import java.net.InetSocketAddress; import java.net.URI; import java.util.List; import java.util.Map.Entry; @@ -205,7 +205,7 @@ public final class HttpUploadClient { List> headers) throws Exception { // XXX /formpost // Start the connection attempt. - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.sync().channel(); @@ -265,7 +265,7 @@ public final class HttpUploadClient { List> headers, List bodylist) throws Exception { // XXX /formpostmultipart // Start the connection attempt. - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); + ChannelFuture future = bootstrap.connect(SocketUtils.socketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.sync().channel(); diff --git a/example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentClient.java b/example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentClient.java index 20e3ca4f3c..b0a2231cfb 100644 --- a/example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentClient.java +++ b/example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentClient.java @@ -24,8 +24,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.util.CharsetUtil; - -import java.net.InetSocketAddress; +import io.netty.util.internal.SocketUtils; /** * A UDP broadcast client that asks for a quote of the moment (QOTM) to {@link QuoteOfTheMomentServer}. @@ -52,7 +51,7 @@ public final class QuoteOfTheMomentClient { // Broadcast the QOTM request to port 8080. ch.writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), - new InetSocketAddress("255.255.255.255", PORT))).sync(); + SocketUtils.socketAddress("255.255.255.255", PORT))).sync(); // QuoteOfTheMomentClientHandler will close the DatagramChannel when a // response is received. If the channel is not closed within 5 seconds, diff --git a/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoClient.java b/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoClient.java index c3d681b7e9..c526da503b 100644 --- a/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoClient.java +++ b/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoClient.java @@ -24,6 +24,7 @@ import io.netty.channel.sctp.SctpChannel; import io.netty.channel.sctp.SctpChannelOption; import io.netty.channel.sctp.nio.NioSctpChannel; import io.netty.example.sctp.SctpEchoClientHandler; +import io.netty.util.internal.SocketUtils; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -58,10 +59,10 @@ public final class SctpMultiHomingEchoClient { } }); - InetSocketAddress localAddress = new InetSocketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT); - InetAddress localSecondaryAddress = InetAddress.getByName(CLIENT_SECONDARY_HOST); + InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT); + InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST); - InetSocketAddress remoteAddress = new InetSocketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT); + InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT); // Bind the client channel. ChannelFuture bindFuture = b.bind(localAddress).sync(); diff --git a/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoServer.java b/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoServer.java index 99faea25b4..ca8954bed7 100644 --- a/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoServer.java +++ b/example/src/main/java/io/netty/example/sctp/multihoming/SctpMultiHomingEchoServer.java @@ -27,6 +27,7 @@ import io.netty.channel.sctp.nio.NioSctpServerChannel; import io.netty.example.sctp.SctpEchoServerHandler; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; +import io.netty.util.internal.SocketUtils; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -60,8 +61,8 @@ public final class SctpMultiHomingEchoServer { } }); - InetSocketAddress localAddress = new InetSocketAddress(SERVER_PRIMARY_HOST, SERVER_PORT); - InetAddress localSecondaryAddress = InetAddress.getByName(SERVER_SECONDARY_HOST); + InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT); + InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST); // Bind the server to primary address. ChannelFuture bindFuture = b.bind(localAddress).sync(); diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java index 846f9e128c..047b793983 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java @@ -15,6 +15,8 @@ */ package io.netty.example.udt.echo.rendezvous; +import io.netty.util.internal.SocketUtils; + import java.net.InetSocketAddress; /** @@ -31,8 +33,8 @@ public class MsgEchoPeerOne extends MsgEchoPeerBase { public static void main(final String[] args) throws Exception { final int messageSize = 64 * 1024; - final InetSocketAddress self = new InetSocketAddress(Config.hostOne, Config.portOne); - final InetSocketAddress peer = new InetSocketAddress(Config.hostTwo, Config.portTwo); + final InetSocketAddress self = SocketUtils.socketAddress(Config.hostOne, Config.portOne); + final InetSocketAddress peer = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo); new MsgEchoPeerOne(self, peer, messageSize).run(); } } diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java index 2dc9b89577..84012e6f35 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java @@ -15,6 +15,8 @@ */ package io.netty.example.udt.echo.rendezvous; +import io.netty.util.internal.SocketUtils; + import java.net.InetSocketAddress; /** @@ -31,8 +33,8 @@ public class MsgEchoPeerTwo extends MsgEchoPeerBase { public static void main(final String[] args) throws Exception { final int messageSize = 64 * 1024; - final InetSocketAddress self = new InetSocketAddress(Config.hostTwo, Config.portTwo); - final InetSocketAddress peer = new InetSocketAddress(Config.hostOne, Config.portOne); + final InetSocketAddress self = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo); + final InetSocketAddress peer = SocketUtils.socketAddress(Config.hostOne, Config.portOne); new MsgEchoPeerTwo(self, peer, messageSize).run(); } } diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java index 71bda74190..1de5c653d5 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java @@ -16,6 +16,7 @@ package io.netty.example.udt.echo.rendezvousBytes; import io.netty.example.udt.echo.rendezvous.Config; +import io.netty.util.internal.SocketUtils; import java.net.InetSocketAddress; import java.net.SocketAddress; @@ -37,8 +38,8 @@ public class ByteEchoPeerOne extends ByteEchoPeerBase { public static void main(String[] args) throws Exception { final int messageSize = 64 * 1024; - final InetSocketAddress myAddress = new InetSocketAddress(Config.hostOne, Config.portOne); - final InetSocketAddress peerAddress = new InetSocketAddress(Config.hostTwo, Config.portTwo); + final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne); + final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo); new ByteEchoPeerOne(messageSize, myAddress, peerAddress).run(); } } diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java index a28c5829b2..dafbfe2ad0 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java @@ -16,6 +16,7 @@ package io.netty.example.udt.echo.rendezvousBytes; import io.netty.example.udt.echo.rendezvous.Config; +import io.netty.util.internal.SocketUtils; import java.net.InetSocketAddress; import java.net.SocketAddress; @@ -37,8 +38,8 @@ public class ByteEchoPeerTwo extends ByteEchoPeerBase { public static void main(String[] args) throws Exception { final int messageSize = 64 * 1024; - final InetSocketAddress myAddress = new InetSocketAddress(Config.hostTwo, Config.portTwo); - final InetSocketAddress peerAddress = new InetSocketAddress(Config.hostOne, Config.portOne); + final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo); + final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne); new ByteEchoPeerTwo(messageSize, myAddress, peerAddress).run(); } } diff --git a/handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java b/handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java index 6b5c8f4b38..abcb150687 100644 --- a/handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java +++ b/handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java @@ -15,6 +15,8 @@ */ package io.netty.handler.ipfilter; +import io.netty.util.internal.SocketUtils; + import java.math.BigInteger; import java.net.Inet4Address; import java.net.Inet6Address; @@ -32,7 +34,7 @@ public final class IpSubnetFilterRule implements IpFilterRule { public IpSubnetFilterRule(String ipAddress, int cidrPrefix, IpFilterRuleType ruleType) { try { - filterRule = selectFilterRule(InetAddress.getByName(ipAddress), cidrPrefix, ruleType); + filterRule = selectFilterRule(SocketUtils.addressByName(ipAddress), cidrPrefix, ruleType); } catch (UnknownHostException e) { throw new IllegalArgumentException("ipAddress", e); } diff --git a/handler/src/test/java/io/netty/handler/ipfilter/IpSubnetFilterTest.java b/handler/src/test/java/io/netty/handler/ipfilter/IpSubnetFilterTest.java index 843f493179..7fc5520e18 100644 --- a/handler/src/test/java/io/netty/handler/ipfilter/IpSubnetFilterTest.java +++ b/handler/src/test/java/io/netty/handler/ipfilter/IpSubnetFilterTest.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.util.internal.SocketUtils; import org.junit.Assert; import org.junit.Test; @@ -132,12 +133,12 @@ public class IpSubnetFilterTest { return new EmbeddedChannel(handlers) { @Override protected SocketAddress remoteAddress0() { - return isActive()? new InetSocketAddress(ipAddress, 5421) : null; + return isActive()? SocketUtils.socketAddress(ipAddress, 5421) : null; } }; } private static InetSocketAddress newSockAddress(String ipAddress) { - return new InetSocketAddress(ipAddress, 1234); + return SocketUtils.socketAddress(ipAddress, 1234); } } diff --git a/microbench/src/test/java/io/netty/microbench/internal/PrivilegedSocketOperationsBenchmark.java b/microbench/src/test/java/io/netty/microbench/internal/PrivilegedSocketOperationsBenchmark.java new file mode 100644 index 0000000000..377ea1ee9a --- /dev/null +++ b/microbench/src/test/java/io/netty/microbench/internal/PrivilegedSocketOperationsBenchmark.java @@ -0,0 +1,193 @@ +/* + * Copyright 2016 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.microbench.internal; + +import io.netty.microbench.util.AbstractMicrobenchmark; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.channels.ServerSocketChannel; +import java.security.AccessController; +import java.security.NoSuchAlgorithmException; +import java.security.Policy; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.security.URIParameter; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.SECONDS) +public class PrivilegedSocketOperationsBenchmark extends AbstractMicrobenchmark { + + @State(Scope.Benchmark) + public static class SecurityManagerInstalled { + + @Setup + public void setup() throws IOException, NoSuchAlgorithmException, URISyntaxException { + final URI policyFile = PrivilegedSocketOperationsBenchmark.class.getResource("/jmh-security.policy") + .toURI(); + Policy.setPolicy(Policy.getInstance("JavaPolicy", new URIParameter(policyFile))); + System.setSecurityManager(new SecurityManager()); + } + + @TearDown + public void tearDown() throws IOException { + System.setSecurityManager(null); + } + } + + @State(Scope.Benchmark) + public static class SecurityManagerEmpty { + + @Setup + public void setup() throws IOException, NoSuchAlgorithmException, URISyntaxException { + System.setSecurityManager(null); + } + } + + @Benchmark + public ServerSocketChannel testWithSMNoPrivileged(final SecurityManagerInstalled sm) throws IOException { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + ssc.close(); + return ssc; + } + + @Benchmark + public ServerSocketChannel testWithSM(final SecurityManagerInstalled sm) throws IOException { + try { + final ServerSocketChannel ssc = AccessController.doPrivileged( + new PrivilegedExceptionAction() { + @Override + public ServerSocketChannel run() throws Exception { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + return ssc; + } + }); + ssc.close(); + return ssc; + } catch (final PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + @Benchmark + public ServerSocketChannel testWithSMWithNullCheck(final SecurityManagerInstalled sm) throws IOException { + if (System.getSecurityManager() != null) { + try { + final ServerSocketChannel ssc = AccessController.doPrivileged( + new PrivilegedExceptionAction() { + @Override + public ServerSocketChannel run() throws Exception { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + return ssc; + } + }); + ssc.close(); + return ssc; + } catch (final PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } else { + // this should never happen during benchmarking, but we write the correct code here + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + ssc.close(); + return ssc; + } + } + + @Benchmark + public ServerSocketChannel testWithoutSMNoPrivileged(final SecurityManagerEmpty sm) throws IOException { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + ssc.close(); + return ssc; + } + + @Benchmark + public ServerSocketChannel testWithoutSM(final SecurityManagerEmpty sm) throws IOException { + try { + final ServerSocketChannel ssc = AccessController.doPrivileged( + new PrivilegedExceptionAction() { + @Override + public ServerSocketChannel run() throws Exception { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + return ssc; + } + }); + ssc.close(); + return ssc; + } catch (final PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + + @Benchmark + public ServerSocketChannel testWithoutSMWithNullCheck(final SecurityManagerEmpty sm) throws IOException { + if (System.getSecurityManager() != null) { + // this should never happen during benchmarking, but we write the correct code here + try { + final ServerSocketChannel ssc = AccessController.doPrivileged( + new PrivilegedExceptionAction() { + @Override + public ServerSocketChannel run() throws Exception { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + return ssc; + } + }); + ssc.close(); + return ssc; + } catch (final PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } else { + final ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.socket().bind(null); + ssc.configureBlocking(false); + ssc.accept(); + ssc.close(); + return ssc; + } + } +} diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramMulticastTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramMulticastTest.java index aab1447727..80ea96eee3 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramMulticastTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramMulticastTest.java @@ -25,6 +25,7 @@ import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.oio.OioDatagramChannel; import io.netty.util.NetUtil; +import io.netty.util.internal.SocketUtils; import org.junit.Test; import java.net.InetSocketAddress; @@ -69,7 +70,7 @@ public class DatagramMulticastTest extends AbstractDatagramTest { DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel(); String group = "230.0.0.1"; - InetSocketAddress groupAddress = new InetSocketAddress(group, addr.getPort()); + InetSocketAddress groupAddress = SocketUtils.socketAddress(group, addr.getPort()); cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync(); diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/ServerSocketSuspendTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/ServerSocketSuspendTest.java index af3fe50a72..931070c2fa 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/ServerSocketSuspendTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/ServerSocketSuspendTest.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelOption; +import io.netty.util.internal.SocketUtils; import org.junit.Ignore; import org.junit.Test; @@ -57,7 +58,7 @@ public class ServerSocketSuspendTest extends AbstractServerSocketTest { long startTime = System.nanoTime(); for (int i = 0; i < NUM_CHANNELS; i ++) { Socket s = new Socket(); - s.connect(addr, 10000); + SocketUtils.connect(s, addr, 10000); sockets.add(s); } diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java index 1890d0d96a..44f0fec065 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelOption; +import io.netty.util.internal.SocketUtils; import io.netty.testsuite.util.TestUtils; import io.netty.util.NetUtil; import io.netty.util.concurrent.GlobalEventExecutor; @@ -33,7 +34,6 @@ import org.junit.Test; import java.io.IOException; import java.net.ConnectException; -import java.net.InetSocketAddress; import java.net.Socket; import static org.hamcrest.CoreMatchers.*; @@ -108,7 +108,7 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest { boolean badHostTimedOut = true; Socket socket = new Socket(); try { - socket.connect(new InetSocketAddress(BAD_HOST, BAD_PORT), 10); + SocketUtils.connect(socket, SocketUtils.socketAddress(BAD_HOST, BAD_PORT), 10); } catch (ConnectException e) { badHostTimedOut = false; // is thrown for no route to host when using Socket connect diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketShutdownOutputByPeerTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketShutdownOutputByPeerTest.java index 3ad0d95a30..589afe8410 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketShutdownOutputByPeerTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketShutdownOutputByPeerTest.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOption; import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.util.internal.SocketUtils; import io.netty.channel.socket.ChannelInputShutdownEvent; import io.netty.channel.socket.SocketChannel; import org.junit.Test; @@ -45,7 +46,7 @@ public class SocketShutdownOutputByPeerTest extends AbstractServerSocketTest { try { sb.childHandler(h).childOption(ChannelOption.ALLOW_HALF_CLOSURE, true).bind().sync(); - s.connect(addr, 10000); + SocketUtils.connect(s, addr, 10000); s.getOutputStream().write(1); assertEquals(1, (int) h.queue.take()); @@ -82,7 +83,7 @@ public class SocketShutdownOutputByPeerTest extends AbstractServerSocketTest { try { sb.childHandler(h).bind().sync(); - s.connect(addr, 10000); + SocketUtils.connect(s, addr, 10000); s.getOutputStream().write(1); assertEquals(1, (int) h.queue.take()); diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java index 6b8471be8c..9c7f130bcb 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java @@ -21,6 +21,7 @@ import com.barchart.udt.nio.SocketChannelUDT; import io.netty.channel.ChannelException; import io.netty.channel.ChannelMetadata; import io.netty.channel.ChannelOutboundBuffer; +import io.netty.util.internal.SocketUtils; import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.udt.DefaultUdtServerChannelConfig; import io.netty.channel.udt.UdtChannel; @@ -121,8 +122,9 @@ public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel im @Override protected SocketAddress localAddress0() { - return javaChannel().socket().getLocalSocketAddress(); + return SocketUtils.localSocketAddress(javaChannel().socket()); } + @Override public InetSocketAddress localAddress() { return (InetSocketAddress) super.localAddress(); @@ -145,7 +147,7 @@ public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel im @Override protected int doReadMessages(List buf) throws Exception { - final SocketChannelUDT channelUDT = javaChannel().accept(); + final SocketChannelUDT channelUDT = (SocketChannelUDT) SocketUtils.accept(javaChannel()); if (channelUDT == null) { return 0; } else { diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java index de47a8b94b..9c81d30912 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java @@ -26,11 +26,16 @@ import io.netty.channel.nio.AbstractNioByteChannel; import io.netty.channel.udt.DefaultUdtChannelConfig; import io.netty.channel.udt.UdtChannel; import io.netty.channel.udt.UdtChannelConfig; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; +import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; import static java.nio.channels.SelectionKey.*; @@ -90,7 +95,7 @@ public class NioUdtByteConnectorChannel extends AbstractNioByteChannel implement @Override protected void doBind(final SocketAddress localAddress) throws Exception { - javaChannel().bind(localAddress); + privilegedBind(javaChannel(), localAddress); } @Override @@ -100,11 +105,11 @@ public class NioUdtByteConnectorChannel extends AbstractNioByteChannel implement @Override protected boolean doConnect(final SocketAddress remoteAddress, - final SocketAddress localAddress) throws Exception { + final SocketAddress localAddress) throws Exception { doBind(localAddress != null? localAddress : new InetSocketAddress(0)); boolean success = false; try { - final boolean connected = javaChannel().connect(remoteAddress); + final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress); if (!connected) { selectionKey().interestOps( selectionKey().interestOps() | OP_CONNECT); @@ -185,4 +190,20 @@ public class NioUdtByteConnectorChannel extends AbstractNioByteChannel implement public InetSocketAddress remoteAddress() { return (InetSocketAddress) super.remoteAddress(); } + + private static void privilegedBind(final SocketChannelUDT socketChannel, final SocketAddress localAddress) + throws IOException { + try { + AccessController.doPrivileged(new PrivilegedExceptionAction() { + @Override + public Void run() throws IOException { + socketChannel.bind(localAddress); + return null; + } + }); + } catch (PrivilegedActionException e) { + throw (IOException) e.getCause(); + } + } + } diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java index d98481a303..28411c9f94 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java @@ -22,6 +22,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelMetadata; import io.netty.channel.ChannelOutboundBuffer; +import io.netty.util.internal.SocketUtils; import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.udt.DefaultUdtChannelConfig; import io.netty.channel.udt.UdtChannel; @@ -96,7 +97,7 @@ public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel imp @Override protected void doBind(final SocketAddress localAddress) throws Exception { - javaChannel().bind(localAddress); + SocketUtils.bind(javaChannel(), localAddress); } @Override @@ -110,7 +111,7 @@ public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel imp doBind(localAddress != null? localAddress : new InetSocketAddress(0)); boolean success = false; try { - final boolean connected = javaChannel().connect(remoteAddress); + final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress); if (!connected) { selectionKey().interestOps( selectionKey().interestOps() | OP_CONNECT); diff --git a/transport-udt/src/test/java/io/netty/test/udt/util/UnitHelp.java b/transport-udt/src/test/java/io/netty/test/udt/util/UnitHelp.java index 62878f5d4d..24a411b40b 100644 --- a/transport-udt/src/test/java/io/netty/test/udt/util/UnitHelp.java +++ b/transport-udt/src/test/java/io/netty/test/udt/util/UnitHelp.java @@ -18,6 +18,7 @@ package io.netty.test.udt.util; import com.barchart.udt.SocketUDT; import com.barchart.udt.StatusUDT; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.ThreadLocalRandom; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -106,7 +107,7 @@ public final class UnitHelp { final String host) { ServerSocket socket = null; try { - final InetAddress address = InetAddress.getByName(host); + final InetAddress address = SocketUtils.addressByName(host); socket = new ServerSocket(0, 3, address); return (InetSocketAddress) socket.getLocalSocketAddress(); } catch (final Exception e) { diff --git a/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java b/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java index 88a4041507..84d0148198 100644 --- a/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java @@ -26,6 +26,7 @@ import io.netty.channel.ChannelPromise; import io.netty.channel.DefaultChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; +import io.netty.util.internal.SocketUtils; import io.netty.util.AttributeKey; import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.GlobalEventExecutor; @@ -140,7 +141,7 @@ public abstract class AbstractBootstrap, C ext * @see {@link #localAddress(SocketAddress)} */ public B localAddress(String inetHost, int inetPort) { - return localAddress(new InetSocketAddress(inetHost, inetPort)); + return localAddress(SocketUtils.socketAddress(inetHost, inetPort)); } /** @@ -247,7 +248,7 @@ public abstract class AbstractBootstrap, C ext * Create a new {@link Channel} and bind it. */ public ChannelFuture bind(String inetHost, int inetPort) { - return bind(new InetSocketAddress(inetHost, inetPort)); + return bind(SocketUtils.socketAddress(inetHost, inetPort)); } /** diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index dc43230a67..a34aaaecab 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -30,6 +30,7 @@ import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.socket.DatagramChannelConfig; import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.InternetProtocolFamily; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.StringUtil; @@ -194,7 +195,7 @@ public final class NioDatagramChannel private void doBind0(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { - javaChannel().bind(localAddress); + SocketUtils.bind(javaChannel(), localAddress); } else { javaChannel().socket().bind(localAddress); } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannelConfig.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannelConfig.java index 548f7cf432..122a4a5553 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannelConfig.java @@ -19,6 +19,7 @@ import io.netty.channel.ChannelException; import io.netty.channel.socket.DatagramChannelConfig; import io.netty.channel.socket.DefaultDatagramChannelConfig; import io.netty.util.internal.PlatformDependent; +import io.netty.util.internal.SocketUtils; import java.lang.reflect.Method; import java.net.InetAddress; @@ -132,7 +133,7 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig { if (inf == null) { return null; } else { - Enumeration addresses = inf.getInetAddresses(); + Enumeration addresses = SocketUtils.addressesFromNetworkInterface(inf); if (addresses.hasMoreElements()) { return addresses.nextElement(); } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java index 5c9ab98c65..f30ff9848a 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java @@ -18,6 +18,7 @@ package io.netty.channel.socket.nio; import io.netty.channel.ChannelException; import io.netty.channel.ChannelMetadata; import io.netty.channel.ChannelOutboundBuffer; +import io.netty.util.internal.SocketUtils; import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.socket.DefaultServerSocketChannelConfig; import io.netty.channel.socket.ServerSocketChannelConfig; @@ -118,7 +119,7 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel @Override protected SocketAddress localAddress0() { - return javaChannel().socket().getLocalSocketAddress(); + return SocketUtils.localSocketAddress(javaChannel().socket()); } @Override @@ -137,7 +138,7 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel @Override protected int doReadMessages(List buf) throws Exception { - SocketChannel ch = javaChannel().accept(); + SocketChannel ch = SocketUtils.accept(javaChannel()); try { if (ch != null) { @@ -189,7 +190,7 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel throw new UnsupportedOperationException(); } - private final class NioServerSocketChannelConfig extends DefaultServerSocketChannelConfig { + private final class NioServerSocketChannelConfig extends DefaultServerSocketChannelConfig { private NioServerSocketChannelConfig(NioServerSocketChannel channel, ServerSocket javaSocket) { super(channel, javaSocket); } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java index fb0892ab84..4a66a481f4 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java @@ -24,6 +24,7 @@ import io.netty.channel.ChannelOutboundBuffer; import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.FileRegion; +import io.netty.util.internal.SocketUtils; import io.netty.channel.nio.AbstractNioByteChannel; import io.netty.channel.socket.DefaultSocketChannelConfig; import io.netty.channel.socket.ServerSocketChannel; @@ -225,9 +226,9 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty private void doBind0(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { - javaChannel().bind(localAddress); + SocketUtils.bind(javaChannel(), localAddress); } else { - javaChannel().socket().bind(localAddress); + SocketUtils.bind(javaChannel().socket(), localAddress); } } @@ -239,7 +240,7 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty boolean success = false; try { - boolean connected = javaChannel().connect(remoteAddress); + boolean connected = SocketUtils.connect(javaChannel(), remoteAddress); if (!connected) { selectionKey().interestOps(SelectionKey.OP_CONNECT); } diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioServerSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioServerSocketChannel.java index 9574459d33..e766351ff8 100644 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioServerSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioServerSocketChannel.java @@ -20,6 +20,7 @@ import io.netty.channel.ChannelMetadata; import io.netty.channel.ChannelOutboundBuffer; import io.netty.channel.oio.AbstractOioMessageChannel; import io.netty.channel.socket.ServerSocketChannel; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -131,7 +132,7 @@ public class OioServerSocketChannel extends AbstractOioMessageChannel @Override protected SocketAddress localAddress0() { - return socket.getLocalSocketAddress(); + return SocketUtils.localSocketAddress(socket); } @Override diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java index 4ad6624546..e7b89a491f 100644 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java @@ -25,6 +25,7 @@ import io.netty.channel.EventLoop; import io.netty.channel.oio.OioByteStreamChannel; import io.netty.channel.socket.ServerSocketChannel; import io.netty.channel.socket.SocketChannel; +import io.netty.util.internal.SocketUtils; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -184,19 +185,19 @@ public class OioSocketChannel extends OioByteStreamChannel @Override protected void doBind(SocketAddress localAddress) throws Exception { - socket.bind(localAddress); + SocketUtils.bind(socket, localAddress); } @Override protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { if (localAddress != null) { - socket.bind(localAddress); + SocketUtils.bind(socket, localAddress); } boolean success = false; try { - socket.connect(remoteAddress, config().getConnectTimeoutMillis()); + SocketUtils.connect(socket, remoteAddress, config().getConnectTimeoutMillis()); activate(socket.getInputStream(), socket.getOutputStream()); success = true; } catch (SocketTimeoutException e) {