Changeset afd2971


Ignore:
Timestamp:
Oct 20, 2012 3:30:12 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
fef1440
Parents:
cbc9165
Message:
  • Addresses:
    • Add methods for connectivity detection
    • Remove Hamachi restriction
Files:
2 edited

Legend:

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

    rcbc9165 rafd2971  
    2626public abstract class Addresses {
    2727   
     28    /**
     29     *  Do we have any non-loop, non-wildcard IPv4 address at all?
     30     *  @since 0.9.4
     31     */
     32    public static boolean isConnected() {
     33        // not as good as using a Java DBus implementation to talk to NetworkManager...
     34        return !getAddresses(true, false, false).isEmpty();
     35    }
     36
    2837    /** @return the first non-local address it finds, or null */
    2938    public static String getAnyAddress() {
     
    5261
    5362    /**
    54      *  @return a sorted array of all addresses
     63     *  @return a sorted set of all addresses including wildcard
    5564     *  @param includeLocal whether to include local
    5665     *  @param includeIPv6 whether to include IPV6
     
    5968     */
    6069    public static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6) {
     70        return getAddresses(includeLocal, includeLocal, includeIPv6);
     71    }
     72
     73    /**
     74     *  @return a sorted set of all addresses
     75     *  @param includeSiteLocal whether to include private like 192.168.x.x
     76     *  @param includeLoopAndWildcard whether to include 127.x.x.x and 0.0.0.0
     77     *  @param includeIPv6 whether to include IPV6
     78     *  @param includeWildCard whether to include 0.0.0.0 and/or 0:0:0:0:0:0 (includeLocal must be true)
     79     *  @return an array of all addresses
     80     *  @since 0.9.4
     81     */
     82    public static SortedSet<String> getAddresses(boolean includeSiteLocal,
     83                                                 boolean includeLoopbackAndWildcard,
     84                                                 boolean includeIPv6) {
    6185        boolean haveIPv4 = false;
    6286        boolean haveIPv6 = false;
     
    7195                    else
    7296                        haveIPv6 = true;
    73                     if (shouldInclude(allMyIps[i], includeLocal, includeIPv6))
     97                    if (shouldInclude(allMyIps[i], includeSiteLocal,
     98                                      includeLoopbackAndWildcard, includeIPv6))
    7499                        rv.add(allMyIps[i].getHostAddress());
    75100                }
     
    88113                        else
    89114                            haveIPv6 = true;
    90                         if (shouldInclude(addr, includeLocal, includeIPv6))
     115                        if (shouldInclude(addr, includeSiteLocal,
     116                                          includeLoopbackAndWildcard, includeIPv6))
    91117                            rv.add(addr.getHostAddress());
    92118                    }
     
    95121        } catch (SocketException e) {}
    96122
    97         if (includeLocal && haveIPv4)
    98             rv.add("0.0.0.0");
    99         if (includeLocal && includeIPv6 && haveIPv6)
    100             rv.add("0:0:0:0:0:0:0:0");  // we could do "::" but all the other ones are probably in long form
     123        if (includeLoopbackAndWildcard) {
     124            if (haveIPv4)
     125                rv.add("0.0.0.0");
     126            if (includeIPv6 && haveIPv6)
     127                rv.add("0:0:0:0:0:0:0:0");  // we could do "::" but all the other ones are probably in long form
     128        }
    101129        return rv;
    102130    }
    103131
    104     private static boolean shouldInclude(InetAddress ia, boolean includeLocal, boolean includeIPv6) {
     132    private static boolean shouldInclude(InetAddress ia, boolean includeSiteLocal,
     133                                         boolean includeLoopbackAndWildcard, boolean includeIPv6) {
    105134        return
    106             (!ia.isLinkLocalAddress()) &&
     135            (!ia.isLinkLocalAddress()) &&     // 169.254.x.x
    107136            (!ia.isMulticastAddress()) &&
    108             (includeLocal ||
     137            (includeLoopbackAndWildcard ||
    109138             ((!ia.isAnyLocalAddress()) &&
    110               (!ia.isLoopbackAddress()) &&
    111               (!ia.isSiteLocalAddress()))) &&
     139              (!ia.isLoopbackAddress()))) &&
     140            (includeSiteLocal ||
     141              !ia.isSiteLocalAddress()) &&
    112142            // Hamachi 5/8 allocated to RIPE (30 November 2010)
    113143            // Removed from TransportImpl.isPubliclyRoutable()
    114144            // Check moved to here, for now, but will eventually need to
    115145            // remove it from here also.
    116             (includeLocal ||
    117             (!ia.getHostAddress().startsWith("5."))) &&
     146            //(includeLocal ||
     147            //(!ia.getHostAddress().startsWith("5."))) &&
    118148            (includeIPv6 ||
    119149             (ia instanceof Inet4Address));
     
    248278     */
    249279    public static void main(String[] args) {
    250         System.err.println("External Addresses:");
    251         Set<String> a = getAddresses(false, false);
     280        System.err.println("External IPv4 Addresses:");
     281        Set<String> a = getAddresses(false, false, false);
    252282        for (String s : a)
    253283            System.err.println(s);
    254         System.err.println("All addresses:");
    255         a = getAddresses(true, true);
     284        System.err.println("\nExternal and Local IPv4 Addresses:");
     285        a = getAddresses(true, false, false);
    256286        for (String s : a)
    257287            System.err.println(s);
     288        System.err.println("\nAll addresses:");
     289        a = getAddresses(true, true, true);
     290        for (String s : a)
     291            System.err.println(s);
     292        System.err.println("\nIs connected? " + isConnected());
    258293    }
    259294}
  • router/java/src/net/i2p/router/transport/UPnPManager.java

    rcbc9165 rafd2971  
    8282            // Do we have a non-loopback, non-broadcast address?
    8383            // If not, that's why it failed (HTTPServer won't start)
    84             Set<String> addrs = Addresses.getAddresses(true, false);
    85             addrs.remove("0.0.0.0");
    86             for (Iterator<String> iter = addrs.iterator(); iter.hasNext(); ) {
    87                 if (iter.next().startsWith("127."))
    88                     iter.remove();
    89             }
    90             if (addrs.isEmpty())
     84            if (!Addresses.isConnected())
    9185                _log.logAlways(Log.WARN, "UPnP start failed - no network connection?");
    9286            else
Note: See TracChangeset for help on using the changeset viewer.