From 05b17e5a00bdfdeb12f04600766b9236b0ed9142 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Mon, 9 Nov 2009 17:09:53 +0000 Subject: [PATCH] * i2psnark: - Reject torrents with too many pieces - Reject torrents with a single file named *.torrent - Increase max piece size to 2MB (was 1MB), but reduce max number of connections to lessen ooms --- .../src/org/klomp/snark/PeerCoordinator.java | 19 +++++++++++++++---- .../src/org/klomp/snark/SnarkManager.java | 6 +++++- .../java/src/org/klomp/snark/Storage.java | 3 ++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java b/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java index ba5597e591..2b517da862 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java @@ -236,10 +236,21 @@ public class PeerCoordinator implements PeerListener { synchronized(peers) { - return !halted && peers.size() < _util.getMaxConnections(); + return !halted && peers.size() < getMaxConnections(); } } + /** reduce max if huge pieces to keep from ooming */ + private int getMaxConnections() { + int size = metainfo.getPieceLength(0); + int max = _util.getMaxConnections(); + if (size <= 1024*1024) + return max; + if (size <= 2*1024*1024) + return (max + 1) / 2; + return (max + 3) / 4; + } + public boolean halted() { return halted; } public void halt() @@ -294,7 +305,7 @@ public class PeerCoordinator implements PeerListener peer.disconnect(false); // Don't deregister this connection/peer. } // This is already checked in addPeer() but we could have gone over the limit since then - else if (peers.size() >= _util.getMaxConnections()) + else if (peers.size() >= getMaxConnections()) { if (_log.shouldLog(Log.WARN)) _log.warn("Already at MAX_CONNECTIONS in connected() with peer: " + peer); @@ -350,7 +361,7 @@ public class PeerCoordinator implements PeerListener peersize = peers.size(); // This isn't a strict limit, as we may have several pending connections; // thus there is an additional check in connected() - need_more = (!peer.isConnected()) && peersize < _util.getMaxConnections(); + need_more = (!peer.isConnected()) && peersize < getMaxConnections(); // Check if we already have this peer before we build the connection Peer old = peerIDInList(peer.getPeerID(), peers); need_more = need_more && ((old == null) || (old.getInactiveTime() > 8*60*1000)); @@ -378,7 +389,7 @@ public class PeerCoordinator implements PeerListener if (peer.isConnected()) _log.info("Add peer already connected: " + peer); else - _log.info("Connections: " + peersize + "/" + _util.getMaxConnections() + _log.info("Connections: " + peersize + "/" + getMaxConnections() + " not accepting extra peer: " + peer); } return false; diff --git a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java index f9470356b8..00f9d52be6 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java +++ b/apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java @@ -552,11 +552,15 @@ public class SnarkManager implements Snark.CompleteListener { List files = info.getFiles(); if ( (files != null) && (files.size() > MAX_FILES_PER_TORRENT) ) { return "Too many files in " + info.getName() + " (" + files.size() + "), deleting it!"; + } else if ( (files == null) && (info.getName().endsWith(".torrent")) ) { + return "Torrent file " + info.getName() + " cannot end in '.torrent', deleting it!"; } else if (info.getPieces() <= 0) { return "No pieces in " + info.getName() + "? deleting it!"; + } else if (info.getPieces() > Storage.MAX_PIECES) { + return "Too many pieces in " + info.getName() + ", limit is " + Storage.MAX_PIECES + ", deleting it!"; } else if (info.getPieceLength(0) > Storage.MAX_PIECE_SIZE) { return "Pieces are too large in " + info.getName() + " (" + DataHelper.formatSize(info.getPieceLength(0)) + - "B), deleting it."; + "B, limit is " + DataHelper.formatSize(Storage.MAX_PIECE_SIZE) + "B), deleting it."; } else if (info.getTotalLength() > Storage.MAX_TOTAL_SIZE) { System.out.println("torrent info: " + info.toString()); List lengths = info.getLengths(); diff --git a/apps/i2psnark/java/src/org/klomp/snark/Storage.java b/apps/i2psnark/java/src/org/klomp/snark/Storage.java index 85a470f441..cf3fcdc4b0 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/Storage.java +++ b/apps/i2psnark/java/src/org/klomp/snark/Storage.java @@ -57,7 +57,8 @@ public class Storage /** The default piece size. */ private static final int MIN_PIECE_SIZE = 256*1024; - public static final int MAX_PIECE_SIZE = 1024*1024; + /** note that we start reducing max number of peer connections above 1MB */ + public static final int MAX_PIECE_SIZE = 2*1024*1024; /** The maximum number of pieces in a torrent. */ public static final int MAX_PIECES = 10*1024; public static final long MAX_TOTAL_SIZE = MAX_PIECE_SIZE * (long) MAX_PIECES; -- GitLab