Wrap operations requiring SocketPermission with doPrivileged blocks
Motivation: Currently Netty does not wrap socket connect, bind, or accept operations in doPrivileged blocks. Nor does it wrap cases where a dns lookup might happen. This prevents an application utilizing the SecurityManager from isolating SocketPermissions to Netty. Modifications: I have introduced a class (SocketUtils) that wraps operations requiring SocketPermissions in doPrivileged blocks. Result: A user of Netty can grant SocketPermissions explicitly to the Netty jar, without granting it to the rest of their application.
This commit is contained in:
parent
a4d9d44bca
commit
095be39826
@ -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) {
|
||||
|
@ -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<NetworkInterface> 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<InetAddress> i = iface.getInetAddresses(); i.hasMoreElements();) {
|
||||
for (Enumeration<InetAddress> 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<InetAddress> i = iface.getInetAddresses();
|
||||
Enumeration<InetAddress> i = SocketUtils.addressesFromNetworkInterface(iface);
|
||||
if (i.hasMoreElements()) {
|
||||
// Found the one with INET address.
|
||||
loopbackIface = iface;
|
||||
|
197
common/src/main/java/io/netty/util/internal/SocketUtils.java
Normal file
197
common/src/main/java/io/netty/util/internal/SocketUtils.java
Normal file
@ -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<Void>() {
|
||||
@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<Void>() {
|
||||
@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<Boolean>() {
|
||||
@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<Void>() {
|
||||
@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<SocketChannel>() {
|
||||
@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<Void>() {
|
||||
@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<SocketAddress>() {
|
||||
@Override
|
||||
public SocketAddress run() {
|
||||
return socket.getLocalSocketAddress();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static InetAddress addressByName(final String hostname) throws UnknownHostException {
|
||||
try {
|
||||
return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress>() {
|
||||
@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<InetAddress[]>() {
|
||||
@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<InetSocketAddress>() {
|
||||
@Override
|
||||
public InetSocketAddress run() {
|
||||
return new InetSocketAddress(hostname, port);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Enumeration<InetAddress> addressesFromNetworkInterface(final NetworkInterface intf) {
|
||||
return AccessController.doPrivileged(new PrivilegedAction<Enumeration<InetAddress>>() {
|
||||
@Override
|
||||
public Enumeration<InetAddress> run() {
|
||||
return intf.getInetAddresses();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static byte[] hardwareAddressFromNetworkInterface(final NetworkInterface intf) throws SocketException {
|
||||
try {
|
||||
return AccessController.doPrivileged(new PrivilegedExceptionAction<byte[]>() {
|
||||
@Override
|
||||
public byte[] run() throws SocketException {
|
||||
return intf.getHardwareAddress();
|
||||
}
|
||||
});
|
||||
} catch (PrivilegedActionException e) {
|
||||
throw (SocketException) e.getCause();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Entry<String, String>> 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<Entry<String, String>> headers, List<InterfaceHttpData> 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();
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<ServerSocketChannel>() {
|
||||
@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<ServerSocketChannel>() {
|
||||
@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<ServerSocketChannel>() {
|
||||
@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<ServerSocketChannel>() {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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());
|
||||
|
@ -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<Object> buf) throws Exception {
|
||||
final SocketChannelUDT channelUDT = javaChannel().accept();
|
||||
final SocketChannelUDT channelUDT = (SocketChannelUDT) SocketUtils.accept(javaChannel());
|
||||
if (channelUDT == null) {
|
||||
return 0;
|
||||
} else {
|
||||
|
@ -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<Void>() {
|
||||
@Override
|
||||
public Void run() throws IOException {
|
||||
socketChannel.bind(localAddress);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
} catch (PrivilegedActionException e) {
|
||||
throw (IOException) e.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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<B extends AbstractBootstrap<B, C>, 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<B extends AbstractBootstrap<B, C>, 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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<InetAddress> addresses = inf.getInetAddresses();
|
||||
Enumeration<InetAddress> addresses = SocketUtils.addressesFromNetworkInterface(inf);
|
||||
if (addresses.hasMoreElements()) {
|
||||
return addresses.nextElement();
|
||||
}
|
||||
|
@ -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<Object> 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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user