Changeset 5c3e4087 for apps


Ignore:
Timestamp:
Jul 31, 2018 2:13:33 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
75d8672
Parents:
a51d260
Message:

i2psnark: Don't disconnect seeds immediately if comments enabled (ticket #2288)
Implement variable timeout
Hardcode handshake bytes
Log tweaks

Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/Peer.java

    ra51d260 r5c3e4087  
    8080  private long downloaded_old[] = {-1,-1,-1};
    8181
     82  private static final byte[] HANDSHAKE = DataHelper.getASCII("BitTorrent protocol");
    8283  //  bytes per bt spec:                         0011223344556677
    8384  private static final long OPTION_EXTENSION = 0x0000000000100000l;
     
    344345   
    345346    // Handshake write - header
    346     dout.write(19);
    347     dout.write("BitTorrent protocol".getBytes("UTF-8"));
     347    dout.write(HANDSHAKE.length);
     348    dout.write(HANDSHAKE);
    348349    // Handshake write - options
    349350    long myOptions = OPTION_EXTENSION;
     
    366367    // Handshake read - header
    367368    byte b = din.readByte();
    368     if (b != 19)
     369    if (b != HANDSHAKE.length)
    369370      throw new IOException("Handshake failure, expected 19, got "
    370371                            + (b & 0xff) + " on " + sock);
    371372   
    372     byte[] bs = new byte[19];
     373    byte[] bs = new byte[HANDSHAKE.length];
    373374    din.readFully(bs);
    374     String bittorrentProtocol = new String(bs, "UTF-8");
    375     if (!"BitTorrent protocol".equals(bittorrentProtocol))
     375    if (!Arrays.equals(HANDSHAKE, bs))
    376376      throw new IOException("Handshake failure, expected "
    377                             + "'BitTorrent protocol', got '"
    378                             + bittorrentProtocol + "'");
     377                            + "'BitTorrent protocol'");
    379378   
    380379    // Handshake read - options
     
    685684      }
    686685  }
     686 
     687  /** @since 0.9.36 */
     688  public long getMaxInactiveTime() {
     689      return isCompleted() && !isInteresting() ?
     690             PeerCoordinator.MAX_SEED_INACTIVE :
     691             PeerCoordinator.MAX_INACTIVE;
     692  }
    687693
    688694  /**
  • apps/i2psnark/java/src/org/klomp/snark/PeerCheckerTask.java

    ra51d260 r5c3e4087  
    9797              }
    9898
    99             if (peer.getInactiveTime() > PeerCoordinator.MAX_INACTIVE) {
     99            if (peer.getInactiveTime() > peer.getMaxInactiveTime()) {
    100100                if (_log.shouldLog(Log.WARN))
    101101                    _log.warn("Disconnecting peer idle " +
  • apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java

    ra51d260 r5c3e4087  
    7676  final static int MAX_UPLOADERS = 8;
    7777  public static final long MAX_INACTIVE = 8*60*1000;
     78  public static final long MAX_SEED_INACTIVE = 2*60*1000;
    7879
    7980  /**
     
    497498      {
    498499        Peer old = peerIDInList(peer.getPeerID(), peers);
    499         if ( (old != null) && (old.getInactiveTime() > MAX_INACTIVE) ) {
     500        if (old != null && old.getInactiveTime() > old.getMaxInactiveTime()) {
    500501            // idle for 8 minutes, kill the old con (32KB/8min = 68B/sec minimum for one block)
    501502            if (_log.shouldLog(Log.WARN))
     
    593594        need_more = (!peer.isConnected()) && peersize < getMaxConnections();
    594595        // Check if we already have this peer before we build the connection
    595         Peer old = peerIDInList(peer.getPeerID(), peers);
    596         need_more = need_more && ((old == null) || (old.getInactiveTime() > MAX_INACTIVE));
     596        if (need_more) {
     597            Peer old = peerIDInList(peer.getPeerID(), peers);
     598            need_more = old == null || old.getInactiveTime() > old.getMaxInactiveTime();
     599        }
    597600      }
    598601
     
    630633    if (_log.shouldLog(Log.DEBUG)) {
    631634      if (peer.isConnected())
    632         _log.info("Add peer already connected: " + peer);
     635        _log.debug("Add peer already connected: " + peer);
    633636      else
    634         _log.info("Connections: " + peersize + "/" + getMaxConnections()
     637        _log.debug("Connections: " + peersize + "/" + getMaxConnections()
    635638                  + " not accepting extra peer: " + peer);
    636639    }
  • apps/i2psnark/java/src/org/klomp/snark/PeerState.java

    ra51d260 r5c3e4087  
    2626import java.util.Iterator;
    2727import java.util.List;
     28import java.util.Map;
    2829import java.util.Set;
    2930
     
    3132import net.i2p.data.ByteArray;
    3233import net.i2p.util.Log;
     34
     35import org.klomp.snark.bencode.BEValue;
     36import org.klomp.snark.bencode.InvalidBEncodingException;
    3337
    3438class PeerState implements DataLoader
     
    243247
    244248    boolean interest = listener.gotBitField(peer, bitfield);
    245     setInteresting(interest);
    246249    if (bitfield.complete() && !interest) {
    247250        // They are seeding and we are seeding,
    248251        // why did they contact us? (robert)
    249252        // Dump them quick before we send our whole bitmap
     253
     254        // If we both support comments, allow it
     255        if (listener.getUtil().utCommentsEnabled()) {
     256            Map<String, BEValue> handshake = peer.getHandshakeMap();
     257            if (handshake != null) {
     258                BEValue bev = handshake.get("m");
     259                if (bev != null) {
     260                    try {
     261                        if (bev.getMap().get(ExtensionHandler.TYPE_COMMENT) != null) {
     262                            if (_log.shouldLog(Log.WARN))
     263                                _log.warn("Allowing seed that connects to seeds for comments: " + peer);
     264                            setInteresting(interest);
     265                            return;
     266                        }
     267                    } catch (InvalidBEncodingException ibee) {}
     268                }
     269            }
     270        }
    250271        if (_log.shouldLog(Log.WARN))
    251272            _log.warn("Disconnecting seed that connects to seeds: " + peer);
    252273        peer.disconnect(true);
     274    } else {
     275        setInteresting(interest);
    253276    }
    254277  }
Note: See TracChangeset for help on using the changeset viewer.