Changeset de48741 for core


Ignore:
Timestamp:
Sep 14, 2017 3:04:16 PM (3 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
30b9f06
Parents:
bcd7b7e
Message:

Router: Don't lookup hostnames in addresses (proposal #141)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/Addresses.java

    rbcd7b7e rde48741  
    341341                }
    342342            }
     343            //I2PAppContext.getGlobalContext().logManager().getLog(Addresses.class).error("lookup of " + host, new Exception("I did it"));
    343344            try {
    344345                rv = InetAddress.getByName(host).getAddress();
     
    354355                    _negativeCache.put(host, Long.valueOf(System.currentTimeMillis()));
    355356                }
     357            }
     358        }
     359        return rv;
     360    }
     361
     362    /**
     363     *  Caching version of InetAddress.getByName(host).getAddress(), which is slow.
     364     *  Resolves literal IP addresses only, will not cause a DNS lookup.
     365     *  Will return null for host names.
     366     *
     367     *  Unlike InetAddress.getByName(), we do NOT allow numeric IPs
     368     *  of the form d.d.d, d.d, or d, as these are almost certainly mistakes.
     369     *
     370     *  @param host literal IPv4 or IPv6 address; if null returns null
     371     *  @return IP or null
     372     *  @since 0.9.32
     373     */
     374    public static byte[] getIPOnly(String host) {
     375        if (host == null)
     376            return null;
     377        byte[] rv;
     378        synchronized (_IPAddress) {
     379            rv = _IPAddress.get(host);
     380        }
     381        if (rv == null) {
     382            if (InetAddressUtils.isIPv4Address(host) ||
     383                InetAddressUtils.isIPv6Address(host)) {
     384                try {
     385                    rv = InetAddress.getByName(host).getAddress();
     386                    synchronized (_IPAddress) {
     387                        _IPAddress.put(host, rv);
     388                    }
     389                } catch (UnknownHostException uhe) {}
     390            //} else {
     391            //    I2PAppContext.getGlobalContext().logManager().getLog(Addresses.class).warn("Not looking up " + host, new Exception("I did it"));
    356392            }
    357393        }
     
    417453     *  Number of results may also change based on caching at various layers,
    418454     *  even if the ultimate name server results did not change.
     455     *
     456     *  Note: Unused
    419457     *
    420458     *  @param host DNS or IPv4 or IPv6 host name; if null returns null
Note: See TracChangeset for help on using the changeset viewer.