[#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 397f81f253
commit 4f5f4cdc9d
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.net.InetSocketAddress;
import java.net.UnknownHostException;
abstract class AbstractEpollChannel extends AbstractChannel {
private static final ChannelMetadata DATA = new ChannelMetadata(false);
@ -128,6 +129,12 @@ abstract class AbstractEpollChannel extends AbstractChannel {
@Override
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 boolean readPending;

View File

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

View File

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