Changeset 68e5fd6 for router


Ignore:
Timestamp:
Nov 16, 2016 6:05:40 PM (4 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
66c2664
Parents:
37d3204
Message:

Profiles: Pull same-IP detection into a utility class, for use by netdb

Location:
router/java/src/net/i2p/router
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/RouterVersion.java

    r37d3204 r68e5fd6  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 8;
     21    public final static long BUILD = 9;
    2222
    2323    /** for example "-test" */
  • router/java/src/net/i2p/router/peermanager/ProfileOrganizer.java

    r37d3204 r68e5fd6  
    2525import net.i2p.router.RouterContext;
    2626import net.i2p.router.tunnel.pool.TunnelPeerSelector;
     27import net.i2p.router.util.MaskedIPSet;
    2728import net.i2p.router.util.RandomIterator;
    2829import net.i2p.stat.Rate;
     
    12461247    private void locked_selectPeers(Map<Hash, PeerProfile> peers, int howMany, Set<Hash> toExclude, Set<Hash> matches, int mask) {
    12471248        List<Hash> all = new ArrayList<Hash>(peers.keySet());
    1248         Set<String> IPSet = new HashSet<String>(8);
     1249        MaskedIPSet IPSet = new MaskedIPSet(8);
    12491250        // use RandomIterator to avoid shuffling the whole thing
    12501251        for (Iterator<Hash> iter = new RandomIterator<Hash>(all); (matches.size() < howMany) && iter.hasNext(); ) {
     
    12781279     * @param IPMatches all IPs so far, modified by this routine
    12791280     */
    1280     private boolean notRestricted(Hash peer, Set<String> IPSet, int mask) {
    1281         Set<String> peerIPs = maskedIPSet(peer, mask);
    1282         if (containsAny(IPSet, peerIPs))
     1281    private boolean notRestricted(Hash peer, MaskedIPSet IPSet, int mask) {
     1282        Set<String> peerIPs = new MaskedIPSet(_context, peer, mask);
     1283        if (IPSet.containsAny(peerIPs))
    12831284            return false;
    12841285        IPSet.addAll(peerIPs);
    12851286        return true;
    1286     }
    1287 
    1288     /**
    1289       * The Set of IPs for this peer, with a given mask.
    1290       * Includes the comm system's record of the IP, and all netDb addresses.
    1291       *
    1292       * As of 0.9.24, returned set will include netdb family as well.
    1293       *
    1294       * @return an opaque set of masked IPs for this peer
    1295       */
    1296     private Set<String> maskedIPSet(Hash peer, int mask) {
    1297         Set<String> rv = new HashSet<String>(4);
    1298         byte[] commIP = _context.commSystem().getIP(peer);
    1299         if (commIP != null)
    1300             rv.add(maskedIP(commIP, mask));
    1301         RouterInfo pinfo = _context.netDb().lookupRouterInfoLocally(peer);
    1302         if (pinfo == null)
    1303             return rv;
    1304         Collection<RouterAddress> paddr = pinfo.getAddresses();
    1305         for (RouterAddress pa : paddr) {
    1306             byte[] pib = pa.getIP();
    1307             if (pib == null) continue;
    1308             rv.add(maskedIP(pib, mask));
    1309         }
    1310         String family = pinfo.getOption("family");
    1311         if (family != null) {
    1312             // TODO should KNDF put a family-verified indicator in the RI,
    1313             // after checking the sig, or does it matter?
    1314             // What's the threat here of not avoid ding a router
    1315             // falsely claiming to be in the family?
    1316             // Prefix with something so an IP can't be spoofed
    1317             rv.add('x' + family);
    1318         }
    1319         return rv;
    1320     }
    1321 
    1322     /**
    1323      * generate an arbitrary unique value for this ip/mask (mask = 1-4)
    1324      * If IPv6, force mask = 6.
    1325      */
    1326     private static String maskedIP(byte[] ip, int mask) {
    1327         final StringBuilder buf = new StringBuilder(1 + (mask*2));
    1328         final char delim;
    1329         if (ip.length == 16) {
    1330             mask = 6;
    1331             delim = ':';
    1332         } else {
    1333             delim = '.';
    1334         }
    1335         buf.append(delim);
    1336         buf.append(Long.toHexString(DataHelper.fromLong(ip, 0, mask)));
    1337         return buf.toString();
    1338     }
    1339 
    1340     /** does a contain any of the elements in b? */
    1341     private static <T> boolean  containsAny(Set<T> a, Set<T> b) {
    1342         if (a.isEmpty() || b.isEmpty())
    1343             return false;
    1344         for (T o : b) {
    1345             if (a.contains(o))
    1346                 return true;
    1347         }
    1348         return false;
    13491287    }
    13501288
Note: See TracChangeset for help on using the changeset viewer.