Search in sources :

Example 1 with InvalidBEncodingException

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

the class KRPC method receiveMessage.

// /// Reception.....
/**
 *  @param from dest or null if it didn't come in on signed port
 */
private void receiveMessage(Destination from, int fromPort, byte[] payload) {
    try {
        InputStream is = new ByteArrayInputStream(payload);
        BDecoder dec = new BDecoder(is);
        BEValue bev = dec.bdecodeMap();
        Map<String, BEValue> map = bev.getMap();
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Got KRPC message " + bev.toString());
        // Lazy here, just let missing Map entries throw NPEs, caught below
        byte[] msgIDBytes = map.get("t").getBytes();
        MsgID mID = new MsgID(msgIDBytes);
        String type = map.get("y").getString();
        if (type.equals("q")) {
            // queries must be repliable
            String method = map.get("q").getString();
            Map<String, BEValue> args = map.get("a").getMap();
            receiveQuery(mID, from, fromPort, method, args);
        } else if (type.equals("r") || type.equals("e")) {
            // get dest from id->dest map
            ReplyWaiter waiter = _sentQueries.remove(mID);
            if (waiter != null) {
                // TODO verify waiter NID and port?
                if (type.equals("r")) {
                    Map<String, BEValue> response = map.get("r").getMap();
                    receiveResponse(waiter, response);
                } else {
                    List<BEValue> error = map.get("e").getList();
                    receiveError(waiter, error);
                }
            } else {
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Rcvd msg with no one waiting: " + bev.toString());
            }
        } else {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Unknown msg type rcvd: " + bev.toString());
            throw new InvalidBEncodingException("Unknown type: " + type);
        }
    // success
    /**
     *        } catch (InvalidBEncodingException e) {
     *        } catch (IOException e) {
     *        } catch (ArrayIndexOutOfBoundsException e) {
     *        } catch (IllegalArgumentException e) {
     *        } catch (ClassCastException e) {
     *        } catch (NullPointerException e) {
     **
     */
    } catch (Exception e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Receive error for message", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BEValue(org.klomp.snark.bencode.BEValue) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException) NoSuchElementException(java.util.NoSuchElementException) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) I2PSessionException(net.i2p.client.I2PSessionException) InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) ByteArrayInputStream(java.io.ByteArrayInputStream) BDecoder(org.klomp.snark.bencode.BDecoder) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with InvalidBEncodingException

use of org.klomp.snark.bencode.InvalidBEncodingException 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 3 with InvalidBEncodingException

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

the class TrackerClient method getPeersFromDHT.

/**
 *  @return max peers seen
 */
private int getPeersFromDHT() {
    // Get peers from DHT
    // FIXME this needs to be in its own thread
    int rv = 0;
    DHT dht = _util.getDHT();
    if (dht != null && (meta == null || !meta.isPrivate()) && (!stop) && (meta == null || _util.getContext().clock().now() > lastDHTAnnounce + MIN_DHT_ANNOUNCE_INTERVAL)) {
        int numwant;
        if (!coordinator.needOutboundPeers())
            numwant = 1;
        else
            numwant = _util.getMaxConnections();
        Collection<Hash> hashes = dht.getPeersAndAnnounce(snark.getInfoHash(), numwant, 5 * 60 * 1000, DHT_ANNOUNCE_PEERS, 3 * 60 * 1000, coordinator.completed(), numwant <= 1);
        if (!hashes.isEmpty()) {
            runStarted = true;
            lastDHTAnnounce = _util.getContext().clock().now();
            rv = hashes.size();
        } else {
            lastDHTAnnounce = 0;
        }
        if (_log.shouldLog(Log.INFO))
            _log.info("Got " + hashes + " from DHT");
        // now try these peers
        if ((!stop) && !hashes.isEmpty()) {
            List<Peer> peers = new ArrayList<Peer>(hashes.size());
            for (Hash h : hashes) {
                try {
                    PeerID pID = new PeerID(h.getData(), _util);
                    peers.add(new Peer(pID, snark.getID(), snark.getInfoHash(), snark.getMetaInfo()));
                } catch (InvalidBEncodingException ibe) {
                }
            }
            Random r = _util.getContext().random();
            Collections.shuffle(peers, r);
            Iterator<Peer> it = peers.iterator();
            while ((!stop) && it.hasNext() && coordinator.needOutboundPeers()) {
                Peer cur = it.next();
                if (coordinator.addPeer(cur) && it.hasNext()) {
                    int delay = r.nextInt(DELAY_RAND) + DELAY_MIN;
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException ie) {
                    }
                }
            }
        }
    } else {
        if (_log.shouldLog(Log.INFO))
            _log.info("Not getting DHT peers");
    }
    return rv;
}
Also used : DHT(org.klomp.snark.dht.DHT) InvalidBEncodingException(org.klomp.snark.bencode.InvalidBEncodingException) Random(java.util.Random) ArrayList(java.util.ArrayList) Hash(net.i2p.data.Hash) ConvertToHash(net.i2p.util.ConvertToHash)

Example 4 with InvalidBEncodingException

use of org.klomp.snark.bencode.InvalidBEncodingException 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 5 with InvalidBEncodingException

use of org.klomp.snark.bencode.InvalidBEncodingException 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)

Aggregations

InvalidBEncodingException (org.klomp.snark.bencode.InvalidBEncodingException)7 BEValue (org.klomp.snark.bencode.BEValue)6 ArrayList (java.util.ArrayList)2 BDecoder (org.klomp.snark.bencode.BDecoder)2 DHT (org.klomp.snark.dht.DHT)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)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 ConvertToHash (net.i2p.util.ConvertToHash)1