Search in sources :

Example 1 with PeerItem

use of com.biglybt.core.peermanager.peerdb.PeerItem in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method updatePeerExchange.

@Override
public void updatePeerExchange() {
    if (current_peer_state != TRANSFERING)
        return;
    if (!peer_exchange_supported)
        return;
    PeerExchangerItem pex_item = peer_exchange_item;
    if (pex_item != null && manager.isPeerExchangeEnabled()) {
        if (peer_item_identity.getNetwork() == AENetworkClassifier.AT_PUBLIC) {
            final PeerItem[] adds = pex_item.getNewlyAddedPeerConnections(AENetworkClassifier.AT_PUBLIC);
            final PeerItem[] drops = pex_item.getNewlyDroppedPeerConnections(AENetworkClassifier.AT_PUBLIC);
            if ((adds != null && adds.length > 0) || (drops != null && drops.length > 0)) {
                if (ut_pex_enabled) {
                    connection.getOutgoingMessageQueue().addMessage(new UTPeerExchange(adds, drops, null, (byte) 0), false);
                } else {
                    connection.getOutgoingMessageQueue().addMessage(new AZPeerExchange(manager.getHash(), adds, drops, other_peer_pex_version), false);
                }
            }
        } else {
            MessageStreamEncoder encoder = connection.getOutgoingMessageQueue().getEncoder();
            if (encoder instanceof LTMessageEncoder) {
                ((LTMessageEncoder) encoder).handleCustomExtension(LTMessageEncoder.CET_PEX, new Object[] { pex_item });
            }
        }
    }
}
Also used : PeerItem(com.biglybt.core.peermanager.peerdb.PeerItem) PeerExchangerItem(com.biglybt.core.peermanager.peerdb.PeerExchangerItem) MessageStreamEncoder(com.biglybt.core.peermanager.messaging.MessageStreamEncoder)

Example 2 with PeerItem

use of com.biglybt.core.peermanager.peerdb.PeerItem in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method decodePeerExchange.

protected void decodePeerExchange(AZStylePeerExchange exchange) {
    // if we're seeding ignore µT-PEXed seeds, Az won't send them in the first place
    PeerItem[] added = exchange instanceof UTPeerExchange ? ((UTPeerExchange) exchange).getAddedPeers(!(manager.isSeeding() || Constants.DOWNLOAD_SOURCES_PRETEND_COMPLETE)) : exchange.getAddedPeers();
    PeerItem[] dropped = exchange.getDroppedPeers();
    int max_added = exchange.getMaxAllowedPeersPerVolley(!has_received_initial_pex, true);
    int max_dropped = exchange.getMaxAllowedPeersPerVolley(!has_received_initial_pex, false);
    exchange.destroy();
    if (!message_limiter.countIncomingMessage(exchange.getID(), 7, 120 * 1000)) {
        // allow max 7 PEX per 2min  //TODO reduce max after 2308 release?
        System.out.println(manager.getDisplayName() + ": Incoming PEX message flood detected, dropping spamming peer connection." + PEPeerTransportProtocol.this);
        closeConnectionInternally("Incoming PEX message flood detected, dropping spamming peer connection.");
        return;
    }
    if ((added != null && added.length > max_added) || (dropped != null && dropped.length > max_dropped)) {
        if (Logger.isEnabled()) {
            Logger.log(new LogEvent(this, LOGID, "Invalid PEX message received: too large, ignoring this exchange. (added=" + (added == null ? 0 : added.length) + ",dropped=" + (dropped == null ? 0 : dropped.length) + ")"));
        }
        added = null;
        dropped = null;
    }
    has_received_initial_pex = true;
    PeerExchangerItem pex_item = peer_exchange_item;
    if (peer_exchange_supported && pex_item != null && manager.isPeerExchangeEnabled()) {
        if (added != null) {
            for (int i = 0; i < added.length; i++) {
                PeerItem pi = added[i];
                manager.peerDiscovered(this, pi);
                pex_item.addConnectedPeer(pi);
            }
        }
        if (dropped != null) {
            for (int i = 0; i < dropped.length; i++) {
                pex_item.dropConnectedPeer(dropped[i]);
            }
        }
    } else {
        if (Logger.isEnabled())
            Logger.log(new LogEvent(this, LOGID, "Peer Exchange disabled for this download, " + "dropping received exchange message"));
    }
}
Also used : PeerItem(com.biglybt.core.peermanager.peerdb.PeerItem) PeerExchangerItem(com.biglybt.core.peermanager.peerdb.PeerExchangerItem)

Example 3 with PeerItem

use of com.biglybt.core.peermanager.peerdb.PeerItem in project BiglyBT by BiglySoftware.

the class AZPeerExchange method deserialize.

@Override
public Message deserialize(DirectByteBuffer data, byte version) throws MessageException {
    if (data.remaining(bss) > 2000)
        System.out.println("Received PEX msg byte size = " + data.remaining(bss));
    Map root = MessagingUtil.convertBencodedByteStreamToPayload(data, 10, getID());
    byte[] hash = (byte[]) root.get("infohash");
    if (hash == null)
        throw new MessageException("hash == null");
    if (hash.length != 20)
        throw new MessageException("hash.length != 20: " + hash.length);
    PeerItem[] added = extractPeers("added", root);
    PeerItem[] dropped = extractPeers("dropped", root);
    if (added == null && dropped == null)
        throw new MessageException("[" + getID() + "] received exchange message without any adds or drops");
    return new AZPeerExchange(hash, added, dropped, version);
}
Also used : PeerItem(com.biglybt.core.peermanager.peerdb.PeerItem) MessageException(com.biglybt.core.peermanager.messaging.MessageException)

Example 4 with PeerItem

use of com.biglybt.core.peermanager.peerdb.PeerItem in project BiglyBT by BiglySoftware.

the class ClientStatsView method addPeer.

protected void addPeer(PEPeer peer) {
    byte[] bloomId;
    long now = SystemTime.getCurrentTime();
    // Bloom Filter is based on the first 8 bytes of peer id + ip address
    // This captures more duplicates than peer id because most clients
    // randomize their peer id on restart.  IP address, however, changes
    // less often.
    byte[] address = null;
    byte[] peerId = peer.getId();
    InetAddress ip = peer.getAlternativeIPv6();
    if (ip == null) {
        try {
            ip = AddressUtils.getByName(peer.getIp());
            address = ip.getAddress();
        } catch (Throwable e) {
            String ipString = peer.getIp();
            if (ipString != null) {
                address = ByteFormatter.intToByteArray(ipString.hashCode());
            }
        }
    } else {
        address = ip.getAddress();
    }
    if (address == null) {
        bloomId = peerId;
    } else {
        bloomId = new byte[8 + address.length];
        System.arraycopy(peerId, 0, bloomId, 0, 8);
        System.arraycopy(address, 0, bloomId, 8, address.length);
    }
    synchronized (mapData) {
        // break on month.. assume user didn't last use this on the same month in a different year
        calendar.setTimeInMillis(now);
        int thisMonth = calendar.get(Calendar.MONTH);
        if (thisMonth != lastAddMonth) {
            if (lastAddMonth == 0) {
                lastAddMonth = thisMonth;
            } else {
                String s = new SimpleDateFormat("yyyy-MM").format(new Date(lastAdd));
                String filename = CONFIG_FILE_ARCHIVE.replace("%1", s);
                save(filename);
                lastAddMonth = thisMonth;
                lastAdd = 0;
                bloomFilter = BloomFilterFactory.createRotating(BloomFilterFactory.createAddOnly(BLOOMFILTER_SIZE), 2);
                bloomFilterPeerId = BloomFilterFactory.createRotating(BloomFilterFactory.createAddOnly(BLOOMFILTER_PEERID_SIZE), 2);
                overall = new ClientStatsOverall();
                mapData.clear();
                if (tv != null) {
                    tv.removeAllTableRows();
                }
                totalTime = 0;
                startedListeningOn = 0;
            }
        }
        String id = getID(peer);
        ClientStatsDataSource stat;
        stat = mapData.get(id);
        boolean needNew = stat == null;
        if (needNew) {
            stat = new ClientStatsDataSource();
            stat.overall = overall;
            stat.client = id;
            mapData.put(id, stat);
        }
        boolean inBloomFilter = bloomFilter.contains(bloomId) || bloomFilterPeerId.contains(peerId);
        if (!inBloomFilter) {
            bloomFilter.add(bloomId);
            bloomFilterPeerId.add(peerId);
            lastAdd = now;
            synchronized (overall) {
                overall.count++;
            }
            stat.count++;
        }
        stat.current++;
        long existingBytesReceived = peer.getStats().getTotalDataBytesReceived();
        long existingBytesSent = peer.getStats().getTotalDataBytesSent();
        long existingBytesDiscarded = peer.getStats().getTotalBytesDiscarded();
        if (existingBytesReceived > 0) {
            stat.bytesReceived -= existingBytesReceived;
            if (stat.bytesReceived < 0) {
                stat.bytesReceived = 0;
            }
        }
        if (existingBytesSent > 0) {
            stat.bytesSent -= existingBytesSent;
            if (stat.bytesSent < 0) {
                stat.bytesSent = 0;
            }
        }
        if (existingBytesDiscarded > 0) {
            stat.bytesDiscarded -= existingBytesDiscarded;
            if (stat.bytesDiscarded < 0) {
                stat.bytesDiscarded = 0;
            }
        }
        if (peer instanceof PEPeerTransport) {
            PeerItem identity = ((PEPeerTransport) peer).getPeerItemIdentity();
            if (identity != null) {
                String network = identity.getNetwork();
                if (network != null) {
                    Map<String, Object> map = stat.perNetworkStats.get(network);
                    if (map == null) {
                        map = new HashMap<>();
                        stat.perNetworkStats.put(network, map);
                    }
                    if (!inBloomFilter) {
                        long count = MapUtils.getMapLong(map, "count", 0);
                        map.put("count", count + 1);
                    }
                    if (existingBytesReceived > 0) {
                        long bytesReceived = MapUtils.getMapLong(map, "bytesReceived", 0);
                        bytesReceived -= existingBytesReceived;
                        if (bytesReceived < 0) {
                            bytesReceived = 0;
                        }
                        map.put("bytesReceived", bytesReceived);
                    }
                    if (existingBytesSent > 0) {
                        long bytesSent = MapUtils.getMapLong(map, "bytesSent", 0);
                        bytesSent -= existingBytesSent;
                        if (bytesSent < 0) {
                            bytesSent = 0;
                        }
                        map.put("bytesSent", bytesSent);
                    }
                    if (existingBytesDiscarded > 0) {
                        long bytesDiscarded = MapUtils.getMapLong(map, "bytesDiscarded", 0);
                        bytesDiscarded -= existingBytesDiscarded;
                        if (bytesDiscarded < 0) {
                            bytesDiscarded = 0;
                        }
                        map.put("bytesDiscarded", bytesDiscarded);
                    }
                }
            }
        }
        if (tv != null) {
            if (needNew) {
                tv.addDataSource(stat);
            } else {
                TableRowCore row = tv.getRow(stat);
                if (row != null) {
                    row.invalidate();
                }
            }
        }
    }
}
Also used : PeerItem(com.biglybt.core.peermanager.peerdb.PeerItem) TableRowCore(com.biglybt.ui.common.table.TableRowCore) PEPeerTransport(com.biglybt.core.peer.impl.PEPeerTransport) InetAddress(java.net.InetAddress) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with PeerItem

use of com.biglybt.core.peermanager.peerdb.PeerItem in project BiglyBT by BiglySoftware.

the class UTPeerExchange method insertPeers.

private void insertPeers(String key_name, Map root_map, boolean include_flags, List peers, int peer_byte_size) {
    if (peers == null) {
        return;
    }
    if (peers.isEmpty()) {
        return;
    }
    byte[] raw_peers = new byte[peers.size() * peer_byte_size];
    byte[] peer_flags = (include_flags) ? new byte[peers.size()] : null;
    PeerItem peer;
    for (int i = 0; i < peers.size(); i++) {
        peer = (PeerItem) peers.get(i);
        byte[] serialised_peer = peer.getSerialization();
        if (serialised_peer.length != peer_byte_size) {
            Debug.out("invalid serialization- " + serialised_peer.length + ":" + peer_byte_size);
        }
        System.arraycopy(serialised_peer, 0, raw_peers, i * peer_byte_size, peer_byte_size);
        if (peer_flags != null && NetworkManager.getCryptoRequired(peer.getCryptoLevel())) {
            // Encrypted connection.
            peer_flags[i] |= 0x01;
        }
    // 0x02 indicates if the peer is a seed, but that's difficult to determine
    // so we'll leave it.
    }
    // end for
    root_map.put(key_name, raw_peers);
    if (peer_flags != null) {
        root_map.put(key_name + ".f", peer_flags);
    }
}
Also used : PeerItem(com.biglybt.core.peermanager.peerdb.PeerItem)

Aggregations

PeerItem (com.biglybt.core.peermanager.peerdb.PeerItem)9 PEPeerTransport (com.biglybt.core.peer.impl.PEPeerTransport)3 MessageException (com.biglybt.core.peermanager.messaging.MessageException)3 LogEvent (com.biglybt.core.logging.LogEvent)2 PeerExchangerItem (com.biglybt.core.peermanager.peerdb.PeerExchangerItem)2 TableRowCore (com.biglybt.ui.common.table.TableRowCore)2 MessageStreamEncoder (com.biglybt.core.peermanager.messaging.MessageStreamEncoder)1 InetAddress (java.net.InetAddress)1 SimpleDateFormat (java.text.SimpleDateFormat)1