prevent accept() hang on internal socket

This commit is contained in:
zzz
2009-12-04 11:16:43 +00:00
parent 7262c014c0
commit bda4eb830e

View File

@@ -9,6 +9,7 @@ package net.i2p.router.client;
*/ */
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@@ -134,19 +135,23 @@ public class ClientListenerRunner implements Runnable {
/** give the i2cp client 5 seconds to show that they're really i2cp clients */ /** give the i2cp client 5 seconds to show that they're really i2cp clients */
private final static int CONNECT_TIMEOUT = 5*1000; private final static int CONNECT_TIMEOUT = 5*1000;
/**
* Verify the first byte.
* The InternalSocket doesn't support SoTimeout, so use available()
* instead to prevent hanging.
*/
protected boolean validate(Socket socket) { protected boolean validate(Socket socket) {
try { try {
socket.setSoTimeout(CONNECT_TIMEOUT); InputStream is = socket.getInputStream();
int read = socket.getInputStream().read(); for (int i = 0; i < 20; i++) {
if (read != I2PClient.PROTOCOL_BYTE) if (is.available() > 0)
return false; return is.read() == I2PClient.PROTOCOL_BYTE;
socket.setSoTimeout(0); try { Thread.sleep(250); } catch (InterruptedException ie) {}
return true; }
} catch (IOException ioe) { } catch (IOException ioe) {}
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Peer did not authenticate themselves as I2CP quickly enough, dropping"); _log.warn("Peer did not authenticate themselves as I2CP quickly enough, dropping");
return false; return false;
}
} }
/** /**
* Handle the connection by passing it off to a {@link ClientConnectionRunner ClientConnectionRunner} * Handle the connection by passing it off to a {@link ClientConnectionRunner ClientConnectionRunner}