use of com.biglybt.core.networkmanager.impl.TransportHelper 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.networkmanager.impl.TransportHelper 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.TransportHelper in project BiglyBT by BiglySoftware.
the class MessageManagerImpl method registerGenericMessageType.
@Override
public GenericMessageRegistration registerGenericMessageType(final String _type, final String description, final int stream_crypto, final GenericMessageHandler handler) throws MessageException {
final String type = "AEGEN:" + _type;
final byte[] type_bytes = type.getBytes();
final byte[][] shared_secrets = new byte[][] { new SHA1Simple().calculateHash(type_bytes) };
synchronized (message_handlers) {
message_handlers.put(type, handler);
}
final NetworkManager.ByteMatcher matcher = new NetworkManager.ByteMatcher() {
@Override
public int matchThisSizeOrBigger() {
return (maxSize());
}
@Override
public int maxSize() {
return type_bytes.length;
}
@Override
public int minSize() {
return maxSize();
}
@Override
public Object matches(TransportHelper transport, ByteBuffer to_compare, int port) {
int old_limit = to_compare.limit();
to_compare.limit(to_compare.position() + maxSize());
boolean matches = to_compare.equals(ByteBuffer.wrap(type_bytes));
// restore buffer structure
to_compare.limit(old_limit);
return matches ? "" : null;
}
@Override
public Object minMatches(TransportHelper transport, ByteBuffer to_compare, int port) {
return (matches(transport, to_compare, port));
}
@Override
public byte[][] getSharedSecrets() {
return (shared_secrets);
}
@Override
public int getSpecificPort() {
return (-1);
}
};
NetworkManager.getSingleton().requestIncomingConnectionRouting(matcher, new NetworkManager.RoutingListener() {
@Override
public void connectionRouted(final NetworkConnection connection, Object routing_data) {
try {
ByteBuffer[] skip_buffer = { ByteBuffer.allocate(type_bytes.length) };
connection.getTransport().read(skip_buffer, 0, 1);
if (skip_buffer[0].remaining() != 0) {
Debug.out("incomplete read");
}
GenericMessageEndpointImpl endpoint = new GenericMessageEndpointImpl(connection.getEndpoint());
GenericMessageConnectionDirect direct_connection = GenericMessageConnectionDirect.receive(endpoint, type, description, stream_crypto, shared_secrets);
GenericMessageConnectionImpl new_connection = new GenericMessageConnectionImpl(MessageManagerImpl.this, direct_connection);
direct_connection.connect(connection);
if (handler.accept(new_connection)) {
new_connection.accepted();
} else {
connection.close("connection not accepted");
}
} catch (Throwable e) {
Debug.printStackTrace(e);
connection.close(e == null ? null : Debug.getNestedExceptionMessage(e));
}
}
@Override
public boolean autoCryptoFallback() {
return (stream_crypto != MessageManager.STREAM_ENCRYPTION_RC4_REQUIRED);
}
}, new MessageStreamFactory() {
@Override
public MessageStreamEncoder createEncoder() {
return new GenericMessageEncoder();
}
@Override
public MessageStreamDecoder createDecoder() {
return new GenericMessageDecoder(type, description);
}
});
return (new GenericMessageRegistration() {
@Override
public GenericMessageEndpoint createEndpoint(InetSocketAddress notional_target) {
return (new GenericMessageEndpointImpl(notional_target));
}
@Override
public GenericMessageConnection createConnection(GenericMessageEndpoint endpoint) throws MessageException {
return (new GenericMessageConnectionImpl(MessageManagerImpl.this, type, description, (GenericMessageEndpointImpl) endpoint, stream_crypto, shared_secrets));
}
@Override
public void cancel() {
NetworkManager.getSingleton().cancelIncomingConnectionRouting(matcher);
synchronized (message_handlers) {
message_handlers.remove(type);
}
}
});
}
use of com.biglybt.core.networkmanager.impl.TransportHelper in project BiglyBT by BiglySoftware.
the class PeerManager method init.
protected void init() {
// ensure it gets initialized
MessageManager.getSingleton().initialize();
NetworkManager.ByteMatcher matcher = new NetworkManager.ByteMatcher() {
@Override
public int matchThisSizeOrBigger() {
return (48);
}
@Override
public int maxSize() {
return 48;
}
@Override
public int minSize() {
return 20;
}
@Override
public Object matches(TransportHelper transport, ByteBuffer to_compare, int port) {
InetSocketAddress address = transport.getAddress();
int old_limit = to_compare.limit();
int old_position = to_compare.position();
to_compare.limit(old_position + 20);
PeerManagerRegistrationImpl routing_data = null;
if (to_compare.equals(legacy_handshake_header)) {
// compare header
to_compare.limit(old_position + 48);
to_compare.position(old_position + 28);
byte[] hash = new byte[to_compare.remaining()];
to_compare.get(hash);
try {
managers_mon.enter();
List<PeerManagerRegistrationImpl> registrations = registered_legacy_managers.get(new HashWrapper(hash));
if (registrations != null) {
routing_data = registrations.get(0);
}
} finally {
managers_mon.exit();
}
}
// restore buffer structure
to_compare.limit(old_limit);
to_compare.position(old_position);
if (routing_data != null) {
if (!routing_data.isActive()) {
if (routing_data.isKnownSeed(address)) {
String reason = "Activation request from " + address + " denied as known seed";
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, reason));
}
transport.close(reason);
routing_data = null;
} else {
if (!routing_data.getAdapter().activateRequest(address)) {
String reason = "Activation request from " + address + " denied by rules";
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, reason));
}
transport.close(reason);
routing_data = null;
}
}
}
}
return routing_data;
}
@Override
public Object minMatches(TransportHelper transport, ByteBuffer to_compare, int port) {
boolean matches = false;
int old_limit = to_compare.limit();
int old_position = to_compare.position();
to_compare.limit(old_position + 20);
if (to_compare.equals(legacy_handshake_header)) {
matches = true;
}
// restore buffer structure
to_compare.limit(old_limit);
to_compare.position(old_position);
return matches ? "" : null;
}
@Override
public byte[][] getSharedSecrets() {
// registered manually above
return (null);
}
@Override
public int getSpecificPort() {
return (-1);
}
};
// register for incoming connection routing
NetworkManager.getSingleton().requestIncomingConnectionRouting(matcher, new NetworkManager.RoutingListener() {
@Override
public void connectionRouted(NetworkConnection connection, Object routing_data) {
PeerManagerRegistrationImpl registration = (PeerManagerRegistrationImpl) routing_data;
registration.route(connection, null);
}
@Override
public boolean autoCryptoFallback() {
return (false);
}
}, new MessageStreamFactory() {
@Override
public MessageStreamEncoder createEncoder() {
return new BTMessageEncoder();
}
@Override
public MessageStreamDecoder createDecoder() {
return new BTMessageDecoder();
}
});
}
Aggregations