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;
}
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) {
}
}
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;
}
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) {
}
}
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) {
}
}
Aggregations