Changeset cdcbc802


Ignore:
Timestamp:
Dec 26, 2010 10:57:13 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
fefcb6c
Parents:
443abce
Message:

Addresses cleanup

File:
1 edited

Legend:

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

    r443abce rcdcbc802  
    5656     */
    5757    public static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6) {
     58        boolean haveIPv4 = false;
     59        boolean haveIPv6 = false;
    5860        SortedSet<String> rv = new TreeSet();
    5961        try {
     
    6264            if (allMyIps != null) {
    6365                for (int i = 0; i < allMyIps.length; i++) {
     66                    if (allMyIps[i] instanceof Inet4Address)
     67                        haveIPv4 = true;
     68                    else
     69                        haveIPv6 = true;
    6470                    if (shouldInclude(allMyIps[i], includeLocal, includeIPv6))
    6571                        rv.add(allMyIps[i].getHostAddress());
     
    7379                for(Enumeration<InetAddress> addrs =  ifc.getInetAddresses(); addrs.hasMoreElements();) {
    7480                    InetAddress addr = addrs.nextElement();
     81                    if (addr instanceof Inet4Address)
     82                        haveIPv4 = true;
     83                    else
     84                        haveIPv6 = true;
    7585                    if (shouldInclude(addr, includeLocal, includeIPv6))
    7686                        rv.add(addr.getHostAddress());
     
    7989        } catch (SocketException e) {}
    8090
    81         if (includeLocal)
     91        if (includeLocal && haveIPv4)
    8292            rv.add("0.0.0.0");
    83         if (includeLocal && includeIPv6) {
    84             boolean ipv6 = false;
    85             for (String a : rv) {
    86                 if (a.indexOf(':') >= 0) {
    87                     ipv6 = true;
    88                     break;
    89                 }
    90             }
    91             if (ipv6)
    92                 rv.add("0:0:0:0:0:0:0:0");  // we could do "::" but all the other ones are probably in long form
    93         }
     93        if (includeLocal && includeIPv6 && haveIPv6)
     94            rv.add("0:0:0:0:0:0:0:0");  // we could do "::" but all the other ones are probably in long form
    9495        return rv;
    9596    }
Note: See TracChangeset for help on using the changeset viewer.