forked from I2P_Developers/i2p.i2p
prevent accept() hang on internal socket
This commit is contained in:
@@ -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,20 +135,24 @@ 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}
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user