Search in sources :

Example 6 with BEValue

use of org.klomp.snark.bencode.BEValue in project i2p.i2p by i2p.

the class MetaInfo method calculateInfoHash.

private byte[] calculateInfoHash() {
    Map<String, BEValue> info = createInfoMap();
    if (_log.shouldLog(Log.DEBUG)) {
        StringBuilder buf = new StringBuilder(128);
        buf.append("info: ");
        for (Map.Entry<String, BEValue> entry : info.entrySet()) {
            String key = entry.getKey();
            Object val = entry.getValue();
            buf.append(key).append('=');
            buf.append(val.toString());
        }
        _log.debug(buf.toString());
    }
    byte[] infoBytes = BEncoder.bencode(info);
    // _log.debug("info bencoded: [" + Base64.encode(infoBytes, true) + "]");
    MessageDigest digest = SHA1.getInstance();
    byte[] hash = digest.digest(infoBytes);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("info hash: " + I2PSnarkUtil.toHex(hash));
    return hash;
}
Also used : MessageDigest(java.security.MessageDigest) BEValue(org.klomp.snark.bencode.BEValue) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with BEValue

use of org.klomp.snark.bencode.BEValue in project i2p.i2p by i2p.

the class PeerCoordinator method sendPeers.

/**
 *  Send a PEX message to the peer, if he supports PEX.
 *  This just sends everybody we are connected to, we don't
 *  track new vs. old peers yet.
 *  @since 0.8.4
 */
void sendPeers(Peer peer) {
    if (metainfo != null && metainfo.isPrivate())
        return;
    Map<String, BEValue> handshake = peer.getHandshakeMap();
    if (handshake == null)
        return;
    BEValue bev = handshake.get("m");
    if (bev == null)
        return;
    try {
        if (bev.getMap().get(ExtensionHandler.TYPE_PEX) != null) {
            List<Peer> pList = peerList();
            pList.remove(peer);
            if (!pList.isEmpty())
                ExtensionHandler.sendPEX(peer, pList);
        }
    } catch (InvalidBEncodingException ibee) {
    }
}
Also used : InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) BEValue(org.klomp.snark.bencode.BEValue)

Example 8 with BEValue

use of org.klomp.snark.bencode.BEValue in project i2p.i2p by i2p.

the class TrackerInfo method getPeers.

/**
 ****
 *  public static Set<Peer> getPeers(InputStream in, byte[] my_id, MetaInfo metainfo)
 *    throws IOException
 *  {
 *    return getPeers(new BDecoder(in), my_id, metainfo);
 *  }
 *
 *  public static Set<Peer> getPeers(BDecoder be, byte[] my_id, MetaInfo metainfo)
 *    throws IOException
 *  {
 *    return getPeers(be.bdecodeList().getList(), my_id, metainfo);
 *  }
 *****
 */
/**
 * List of Dictionaries or List of Strings
 */
private static Set<Peer> getPeers(List<BEValue> l, byte[] my_id, byte[] infohash, MetaInfo metainfo, I2PSnarkUtil util) throws IOException {
    Set<Peer> peers = new HashSet<Peer>(l.size());
    for (BEValue bev : l) {
        PeerID peerID;
        try {
            // Case 1 - non-compact - A list of dictionaries (maps)
            peerID = new PeerID(bev.getMap());
        } catch (InvalidBEncodingException ibe) {
            try {
                // Case 2 - compact - A list of 32-byte binary strings (hashes)
                // This was just for testing and is not the official format
                peerID = new PeerID(bev.getBytes(), util);
            } catch (InvalidBEncodingException ibe2) {
                // Snark.debug("Discarding peer from list: " + ibe, Snark.ERROR);
                continue;
            }
        }
        peers.add(new Peer(peerID, my_id, infohash, metainfo));
    }
    return peers;
}
Also used : InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) BEValue(org.klomp.snark.bencode.BEValue) HashSet(java.util.HashSet)

Example 9 with BEValue

use of org.klomp.snark.bencode.BEValue in project i2p.i2p by i2p.

the class PeerCoordinator method sendDHT.

/**
 *  Send a DHT message to the peer, if we both support DHT.
 *  @since DHT
 */
void sendDHT(Peer peer) {
    DHT dht = _util.getDHT();
    if (dht == null)
        return;
    Map<String, BEValue> handshake = peer.getHandshakeMap();
    if (handshake == null)
        return;
    BEValue bev = handshake.get("m");
    if (bev == null)
        return;
    try {
        if (bev.getMap().get(ExtensionHandler.TYPE_DHT) != null)
            ExtensionHandler.sendDHT(peer, dht.getPort(), dht.getRPort());
    } catch (InvalidBEncodingException ibee) {
    }
}
Also used : DHT(org.klomp.snark.dht.DHT) InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) BEValue(org.klomp.snark.bencode.BEValue)

Example 10 with BEValue

use of org.klomp.snark.bencode.BEValue in project i2p.i2p by i2p.

the class PeerCoordinator method sendCommentReq.

/**
 *  Send a commment request message to the peer, if he supports it.
 *  @since 0.9.31
 */
void sendCommentReq(Peer peer) {
    Map<String, BEValue> handshake = peer.getHandshakeMap();
    if (handshake == null)
        return;
    BEValue bev = handshake.get("m");
    if (bev == null)
        return;
    // unless forced at handshake time (see above)
    try {
        if (bev.getMap().get(ExtensionHandler.TYPE_COMMENT) != null) {
            int sz = 0;
            CommentSet comments = snark.getComments();
            if (comments != null) {
                synchronized (comments) {
                    sz = comments.size();
                }
            }
            if (sz >= CommentSet.MAX_SIZE)
                return;
            ExtensionHandler.sendCommentReq(peer, CommentSet.MAX_SIZE - sz);
        }
    } catch (InvalidBEncodingException ibee) {
    }
}
Also used : InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) CommentSet(org.klomp.snark.comments.CommentSet) BEValue(org.klomp.snark.bencode.BEValue)

Aggregations

BEValue (org.klomp.snark.bencode.BEValue)18 BDecoder (org.klomp.snark.bencode.BDecoder)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 InvalidBEncodingException (org.klomp.snark.bencode.InvalidBEncodingException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 MessageDigest (java.security.MessageDigest)1 HashSet (java.util.HashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 I2PSessionException (net.i2p.client.I2PSessionException)1 I2PInvalidDatagramException (net.i2p.client.datagram.I2PInvalidDatagramException)1 DataFormatException (net.i2p.data.DataFormatException)1 Hash (net.i2p.data.Hash)1 Comment (org.klomp.snark.comments.Comment)1 CommentSet (org.klomp.snark.comments.CommentSet)1