[#2262] Fix NPE triggered by unresolveable InetSocketAddress in epoll transport

Motivation:
At the moment when an unresolvable InetSocketAddress is passed into the epoll transport a NPE is thrown

Modifications:
Add check in place which will throw an UnknownHostException if an InetSocketAddress could not been resolved.

Result:
Proper handling of unresolvable InetSocketAddresses
This commit is contained in:
Norman Maurer 2014-03-16 06:25:52 -07:00
parent 55bafbd9c2
commit b22d92057b
3 changed files with 11 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import io.netty.channel.EventLoop;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.UnknownHostException;
abstract class AbstractEpollChannel extends AbstractChannel { abstract class AbstractEpollChannel extends AbstractChannel {
private static final ChannelMetadata DATA = new ChannelMetadata(false); private static final ChannelMetadata DATA = new ChannelMetadata(false);
@ -128,6 +129,12 @@ abstract class AbstractEpollChannel extends AbstractChannel {
@Override @Override
protected abstract AbstractEpollUnsafe newUnsafe(); protected abstract AbstractEpollUnsafe newUnsafe();
protected static void checkResolvable(InetSocketAddress addr) throws UnknownHostException {
if (addr.isUnresolved()) {
throw new UnknownHostException("Unable to resolve addr " + addr);
}
}
protected abstract class AbstractEpollUnsafe extends AbstractUnsafe { protected abstract class AbstractEpollUnsafe extends AbstractUnsafe {
protected boolean readPending; protected boolean readPending;

View File

@ -47,6 +47,7 @@ public final class EpollServerSocketChannel extends AbstractEpollChannel impleme
@Override @Override
protected void doBind(SocketAddress localAddress) throws Exception { protected void doBind(SocketAddress localAddress) throws Exception {
InetSocketAddress addr = (InetSocketAddress) localAddress; InetSocketAddress addr = (InetSocketAddress) localAddress;
checkResolvable(addr);
Native.bind(fd, addr.getAddress(), addr.getPort()); Native.bind(fd, addr.getAddress(), addr.getPort());
local = addr; local = addr;
Native.listen(fd, config.getBacklog()); Native.listen(fd, config.getBacklog());

View File

@ -38,6 +38,7 @@ import java.io.IOException;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -488,11 +489,13 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So
*/ */
private boolean doConnect(InetSocketAddress remoteAddress, InetSocketAddress localAddress) throws Exception { private boolean doConnect(InetSocketAddress remoteAddress, InetSocketAddress localAddress) throws Exception {
if (localAddress != null) { if (localAddress != null) {
checkResolvable(localAddress);
Native.bind(fd, localAddress.getAddress(), localAddress.getPort()); Native.bind(fd, localAddress.getAddress(), localAddress.getPort());
} }
boolean success = false; boolean success = false;
try { try {
checkResolvable(remoteAddress);
boolean connected = Native.connect(fd, remoteAddress.getAddress(), boolean connected = Native.connect(fd, remoteAddress.getAddress(),
remoteAddress.getPort()); remoteAddress.getPort());
if (!connected) { if (!connected) {