use of com.biglybt.core.networkmanager.impl.TransportHelperFilter in project BiglyBT by BiglySoftware.
the class UDPConnectionManager method accept.
protected void accept(final int local_port, final InetSocketAddress remote_address, final UDPConnection connection) {
final UDPTransportHelper helper = new UDPTransportHelper(this, remote_address, connection);
try {
connection.setTransport(helper);
TransportCryptoManager.getSingleton().manageCrypto(helper, null, true, null, new TransportCryptoManager.HandshakeListener() {
@Override
public void handshakeSuccess(ProtocolDecoder decoder, ByteBuffer remaining_initial_data) {
TransportHelperFilter filter = decoder.getFilter();
ConnectionEndpoint co_ep = new ConnectionEndpoint(remote_address);
ProtocolEndpointUDP pe_udp = (ProtocolEndpointUDP) ProtocolEndpointFactory.createEndpoint(ProtocolEndpoint.PROTOCOL_UDP, co_ep, remote_address);
UDPTransport transport = new UDPTransport(pe_udp, filter);
helper.setTransport(transport);
incoming_manager.addConnection(local_port, filter, transport);
}
@Override
public void handshakeFailure(Throwable failure_msg) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "incoming crypto handshake failure: " + Debug.getNestedExceptionMessage(failure_msg)));
}
connection.close("handshake failure: " + Debug.getNestedExceptionMessage(failure_msg));
}
@Override
public void gotSecret(byte[] session_secret) {
helper.getConnection().setSecret(session_secret);
}
@Override
public int getMaximumPlainHeaderLength() {
return (incoming_manager.getMaxMinMatchBufferSize());
}
@Override
public int matchPlainHeader(ByteBuffer buffer) {
Object[] match_data = incoming_manager.checkForMatch(helper, local_port, buffer, true);
if (match_data == null) {
return (TransportCryptoManager.HandshakeListener.MATCH_NONE);
} else {
return (TransportCryptoManager.HandshakeListener.MATCH_CRYPTO_NO_AUTO_FALLBACK);
}
}
});
} catch (Throwable e) {
Debug.printStackTrace(e);
helper.close(Debug.getNestedExceptionMessage(e));
}
}
use of com.biglybt.core.networkmanager.impl.TransportHelperFilter in project BiglyBT by BiglySoftware.
the class ConnectionManagerImpl method createTransportFilter.
@Override
public TransportFilter createTransportFilter(Connection connection, TransportCipher read_cipher, TransportCipher write_cipher) throws TransportException {
Transport transport = connection.getTransport();
if (transport == null) {
throw (new TransportException("no transport available"));
}
com.biglybt.core.networkmanager.Transport core_transport;
try {
core_transport = ((TransportImpl) transport).coreTransport();
} catch (IOException e) {
throw new TransportException(e);
}
TransportHelper helper;
if (core_transport instanceof TCPTransportImpl) {
TransportHelperFilter hfilter = ((TCPTransportImpl) core_transport).getFilter();
if (hfilter != null) {
helper = hfilter.getHelper();
} else {
helper = new TCPTransportHelper(((TCPTransportImpl) (core_transport)).getSocketChannel());
}
} else if (core_transport instanceof UDPTransport) {
TransportHelperFilter hfilter = ((UDPTransport) core_transport).getFilter();
if (hfilter != null) {
helper = hfilter.getHelper();
} else {
helper = ((UDPTransport) core_transport).getFilter().getHelper();
InetSocketAddress addr = core_transport.getTransportEndpoint().getProtocolEndpoint().getConnectionEndpoint().getNotionalAddress();
if (!connection.isIncoming()) {
try {
helper = new UDPTransportHelper(UDPNetworkManager.getSingleton().getConnectionManager(), addr, (UDPTransport) core_transport);
} catch (IOException ioe) {
throw new TransportException(ioe);
}
} else {
// helper = new UDPTransportHelper(UDPNetworkManager.getSingleton().getConnectionManager(), addr, (UDPTransport)core_transport);
throw new TransportException("udp incoming transport type not supported - " + core_transport);
}
}
} else {
throw new TransportException("transport type not supported - " + core_transport);
}
TransportHelperFilterStreamCipher core_filter = new TransportHelperFilterStreamCipher(helper, ((TransportCipherImpl) read_cipher).cipher, ((TransportCipherImpl) write_cipher).cipher);
return new TransportFilterImpl(core_filter);
}
use of com.biglybt.core.networkmanager.impl.TransportHelperFilter in project BiglyBT by BiglySoftware.
the class UDPConnectionManager method connectOutbound.
public void connectOutbound(final UDPTransport udp_transport, final InetSocketAddress address, byte[][] shared_secrets, ByteBuffer initial_data, final ConnectListener listener) {
UDPTransportHelper helper = null;
try {
if (address.isUnresolved()) {
listener.connectFailure(new UnknownHostException(address.getHostName()));
return;
}
int time = listener.connectAttemptStarted(-1);
if (time != -1) {
Debug.out("UDP connect time override not supported");
}
helper = new UDPTransportHelper(this, address, udp_transport);
final UDPTransportHelper f_helper = helper;
synchronized (this) {
outbound_connection_count++;
if (outbound_connection_count >= max_outbound_connections) {
if (!max_conn_exceeded_logged) {
max_conn_exceeded_logged = true;
Debug.out("UDPConnectionManager: max outbound connection limit reached (" + max_outbound_connections + ")");
}
}
}
try {
TransportCryptoManager.getSingleton().manageCrypto(helper, shared_secrets, false, initial_data, new TransportCryptoManager.HandshakeListener() {
@Override
public void handshakeSuccess(ProtocolDecoder decoder, ByteBuffer remaining_initial_data) {
synchronized (UDPConnectionManager.this) {
if (outbound_connection_count > 0) {
outbound_connection_count--;
}
}
TransportHelperFilter filter = decoder.getFilter();
try {
udp_transport.setFilter(filter);
if (udp_transport.isClosed()) {
udp_transport.close("Already closed");
listener.connectFailure(new Exception("Connection already closed"));
} else {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "Outgoing UDP stream to " + address + " established, type = " + filter.getName(false)));
}
udp_transport.connectedOutbound();
listener.connectSuccess(udp_transport, remaining_initial_data);
}
} catch (Throwable e) {
Debug.printStackTrace(e);
udp_transport.close(Debug.getNestedExceptionMessageAndStack(e));
listener.connectFailure(e);
}
}
@Override
public void handshakeFailure(Throwable failure_msg) {
synchronized (UDPConnectionManager.this) {
if (outbound_connection_count > 0) {
outbound_connection_count--;
}
}
f_helper.close(Debug.getNestedExceptionMessageAndStack(failure_msg));
listener.connectFailure(failure_msg);
}
@Override
public void gotSecret(byte[] session_secret) {
f_helper.getConnection().setSecret(session_secret);
}
@Override
public int getMaximumPlainHeaderLength() {
// this is outgoing
throw (new RuntimeException());
}
@Override
public int matchPlainHeader(ByteBuffer buffer) {
// this is outgoing
throw (new RuntimeException());
}
});
} catch (Throwable e) {
synchronized (this) {
if (outbound_connection_count > 0) {
outbound_connection_count--;
}
}
throw (e);
}
} catch (Throwable e) {
Debug.printStackTrace(e);
if (helper != null) {
helper.close(Debug.getNestedExceptionMessage(e));
}
listener.connectFailure(e);
}
}
use of com.biglybt.core.networkmanager.impl.TransportHelperFilter in project BiglyBT by BiglySoftware.
the class UDPTransport method close.
@Override
public void close(String reason) {
closed = true;
readyForRead(false);
readyForWrite(false);
TransportHelperFilter filter = getFilter();
if (filter != null) {
filter.getHelper().close(reason);
setFilter(null);
}
}
Aggregations