use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class ProtocolDecoderPHE method generateDHKeyPair.
protected static KeyPair generateDHKeyPair(TransportHelper transport, boolean outbound) throws IOException {
if (dh_key_generator == null) {
throw (new IOException("Crypto not setup"));
}
synchronized (dh_key_generator) {
if (!outbound) {
InetSocketAddress is_address = transport.getAddress();
byte[] address = AddressUtils.getAddressBytes(is_address);
int hit_count = generate_bloom.add(address);
long now = SystemTime.getCurrentTime();
if (generate_bloom.getSize() / generate_bloom.getEntryCount() < 10) {
generate_bloom = BloomFilterFactory.createAddRemove4Bit(generate_bloom.getSize() + BLOOM_INCREASE);
generate_bloom_create_time = now;
Logger.log(new LogEvent(LOGID, "PHE bloom: size increased to " + generate_bloom.getSize()));
} else if (now < generate_bloom_create_time || now - generate_bloom_create_time > BLOOM_RECREATE) {
generate_bloom = BloomFilterFactory.createAddRemove4Bit(generate_bloom.getSize());
generate_bloom_create_time = now;
}
if (hit_count >= 15) {
Logger.log(new LogEvent(LOGID, "PHE bloom: too many recent connection attempts from " + transport.getAddress()));
throw (new IOException("Too many recent connection attempts (phe)"));
}
long since_last = now - last_dh_incoming_key_generate;
long delay = 100 - since_last;
if (delay > 0 && delay < 100) {
try {
Thread.sleep(delay);
} catch (Throwable e) {
}
}
last_dh_incoming_key_generate = now;
}
KeyPair res = dh_key_generator.generateKeyPair();
return (res);
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class IncomingConnectionManager method doTimeoutChecks.
protected void doTimeoutChecks() {
try {
connections_mon.enter();
ArrayList to_close = null;
long now = SystemTime.getCurrentTime();
for (int i = 0; i < connections.size(); i++) {
IncomingConnection ic = (IncomingConnection) connections.get(i);
TransportHelper transport_helper = ic.filter.getHelper();
if (ic.last_read_time > 0) {
// at least one read op has occured
if (now < ic.last_read_time) {
// time went backwards!
ic.last_read_time = now;
} else if (now - ic.last_read_time > transport_helper.getReadTimeout()) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Incoming connection [" + transport_helper.getAddress() + "] forcibly timed out due to socket read inactivity [" + ic.buffer.position() + " bytes read: " + new String(ic.buffer.array()) + "]"));
if (to_close == null)
to_close = new ArrayList();
to_close.add(ic);
}
} else {
// no bytes have been read yet
if (now < ic.initial_connect_time) {
// time went backwards!
ic.initial_connect_time = now;
} else if (now - ic.initial_connect_time > transport_helper.getConnectTimeout()) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Incoming connection [" + transport_helper.getAddress() + "] forcibly timed out after " + "60sec due to socket inactivity"));
if (to_close == null)
to_close = new ArrayList();
to_close.add(ic);
}
}
}
if (to_close != null) {
for (int i = 0; i < to_close.size(); i++) {
IncomingConnection ic = (IncomingConnection) to_close.get(i);
removeConnection(ic, true, "incoming connection routing timeout");
}
}
} finally {
connections_mon.exit();
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class IncomingConnectionManager method addConnection.
public void addConnection(int local_port, TransportHelperFilter filter, Transport new_transport) {
TransportHelper transport_helper = filter.getHelper();
if (isEmpty()) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "Incoming connection from [" + transport_helper.getAddress() + "] dropped because zero routing handlers registered"));
}
transport_helper.close("No routing handler");
return;
}
// note that the filter may have some data internally queued in it after the crypto handshake decode
// (in particular the BT header). However, there should be some data right behind it that will trigger
// a read-select below, thus giving prompt access to the queued data
final IncomingConnection ic = new IncomingConnection(filter, getMaxMatchBufferSize());
TransportHelper.selectListener sel_listener = new SelectorListener(local_port, new_transport);
try {
connections_mon.enter();
connections.add(ic);
transport_helper.registerForReadSelects(sel_listener, ic);
} finally {
connections_mon.exit();
}
// might be stuff queued up in the filter - force one process cycle (NAT check in particular )
sel_listener.selectSuccess(transport_helper, ic);
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class HTTPNetworkManager method writeReply.
protected void writeReply(final NetworkConnection connection, final TransportHelper transport, final String data) {
byte[] bytes;
try {
bytes = data.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
bytes = data.getBytes();
}
final ByteBuffer bb = ByteBuffer.wrap(bytes);
try {
transport.write(bb, false);
if (bb.remaining() > 0) {
transport.registerForWriteSelects(new TransportHelper.selectListener() {
@Override
public boolean selectSuccess(TransportHelper helper, Object attachment) {
try {
int written = helper.write(bb, false);
if (bb.remaining() > 0) {
helper.registerForWriteSelects(this, null);
} else {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "HTTP connection from " + connection.getEndpoint().getNotionalAddress() + " closed"));
}
connection.close(null);
}
return (written > 0);
} catch (Throwable e) {
helper.cancelWriteSelects();
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "HTTP connection from " + connection.getEndpoint().getNotionalAddress() + " failed to write error '" + data + "'"));
}
connection.close(e == null ? null : Debug.getNestedExceptionMessage(e));
return (false);
}
}
@Override
public void selectFailure(TransportHelper helper, Object attachment, Throwable msg) {
helper.cancelWriteSelects();
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "HTTP connection from " + connection.getEndpoint().getNotionalAddress() + " failed to write error '" + data + "'"));
}
connection.close(msg == null ? null : Debug.getNestedExceptionMessage(msg));
}
}, null);
} else {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "HTTP connection from " + connection.getEndpoint().getNotionalAddress() + " closed"));
}
connection.close(null);
}
} catch (Throwable e) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "HTTP connection from " + connection.getEndpoint().getNotionalAddress() + " failed to write error '" + data + "'"));
}
connection.close(e == null ? null : Debug.getNestedExceptionMessage(e));
}
}
use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.
the class VirtualBlockingServerChannelSelector method start.
/**
* Start the server and begin accepting incoming connections.
*/
@Override
public void start() {
try {
this_mon.enter();
if (!isRunning()) {
try {
server_channel = ServerSocketChannel.open();
server_channel.socket().setReuseAddress(true);
if (receive_buffer_size > 0)
server_channel.socket().setReceiveBufferSize(receive_buffer_size);
server_channel.socket().bind(bind_address, 1024);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "TCP incoming server socket " + bind_address));
AEThread accept_thread = new AEThread("VServerSelector:port" + bind_address.getPort()) {
@Override
public void runSupport() {
accept_loop();
}
};
accept_thread.setDaemon(true);
accept_thread.start();
} catch (Throwable t) {
Debug.out(t);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "ERROR, unable to bind TCP incoming server socket to " + bind_address.getPort(), t));
}
// init to now
last_accept_time = SystemTime.getCurrentTime();
}
} finally {
this_mon.exit();
}
}
Aggregations