Search in sources :

Example 66 with Inet6Address

use of java.net.Inet6Address in project android_packages_apps_Settings by DirtyUnicorns.

the class WifiDetailPreferenceController method updateIpLayerInfo.

private void updateIpLayerInfo() {
    mSignInButton.setVisibility(canSignIntoNetwork() ? View.VISIBLE : View.INVISIBLE);
    mButtonsPref.setVisible(mForgetButton.getVisibility() == View.VISIBLE || mSignInButton.getVisibility() == View.VISIBLE);
    if (mNetwork == null || mLinkProperties == null) {
        mIpAddressPref.setVisible(false);
        mSubnetPref.setVisible(false);
        mGatewayPref.setVisible(false);
        mDnsPref.setVisible(false);
        mIpv6Category.setVisible(false);
        return;
    }
    // Find IPv4 and IPv6 addresses.
    String ipv4Address = null;
    String subnet = null;
    StringJoiner ipv6Addresses = new StringJoiner("\n");
    for (LinkAddress addr : mLinkProperties.getLinkAddresses()) {
        if (addr.getAddress() instanceof Inet4Address) {
            ipv4Address = addr.getAddress().getHostAddress();
            subnet = ipv4PrefixLengthToSubnetMask(addr.getPrefixLength());
        } else if (addr.getAddress() instanceof Inet6Address) {
            ipv6Addresses.add(addr.getAddress().getHostAddress());
        }
    }
    // Find IPv4 default gateway.
    String gateway = null;
    for (RouteInfo routeInfo : mLinkProperties.getRoutes()) {
        if (routeInfo.isIPv4Default() && routeInfo.hasGateway()) {
            gateway = routeInfo.getGateway().getHostAddress();
            break;
        }
    }
    // Find all (IPv4 and IPv6) DNS addresses.
    String dnsServers = mLinkProperties.getDnsServers().stream().map(InetAddress::getHostAddress).collect(Collectors.joining("\n"));
    // Update UI.
    updatePreference(mIpAddressPref, ipv4Address);
    updatePreference(mSubnetPref, subnet);
    updatePreference(mGatewayPref, gateway);
    updatePreference(mDnsPref, dnsServers);
    if (ipv6Addresses.length() > 0) {
        mIpv6AddressPref.setSummary(BidiFormatter.getInstance().unicodeWrap(ipv6Addresses.toString()));
        mIpv6Category.setVisible(true);
    } else {
        mIpv6Category.setVisible(false);
    }
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) RouteInfo(android.net.RouteInfo) StringJoiner(java.util.StringJoiner)

Example 67 with Inet6Address

use of java.net.Inet6Address in project rocketmq by apache.

the class RemotingUtil method getLocalAddress.

public static String getLocalAddress() {
    try {
        // Traversal Network interface to get the first non-loopback and non-private address
        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        ArrayList<String> ipv4Result = new ArrayList<String>();
        ArrayList<String> ipv6Result = new ArrayList<String>();
        while (enumeration.hasMoreElements()) {
            final NetworkInterface networkInterface = enumeration.nextElement();
            final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
            while (en.hasMoreElements()) {
                final InetAddress address = en.nextElement();
                if (!address.isLoopbackAddress()) {
                    if (address instanceof Inet6Address) {
                        ipv6Result.add(normalizeHostAddress(address));
                    } else {
                        ipv4Result.add(normalizeHostAddress(address));
                    }
                }
            }
        }
        // prefer ipv4
        if (!ipv4Result.isEmpty()) {
            for (String ip : ipv4Result) {
                if (ip.startsWith("127.0") || ip.startsWith("192.168")) {
                    continue;
                }
                return ip;
            }
            return ipv4Result.get(ipv4Result.size() - 1);
        } else if (!ipv6Result.isEmpty()) {
            return ipv6Result.get(0);
        }
        // If failed to find,fall back to localhost
        final InetAddress localHost = InetAddress.getLocalHost();
        return normalizeHostAddress(localHost);
    } catch (Exception e) {
        log.error("Failed to obtain local address", e);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Example 68 with Inet6Address

use of java.net.Inet6Address in project rocketmq by apache.

the class MixAll method getLocalhostByNetworkInterface.

// Reverse logic comparing to RemotingUtil method, consider refactor in RocketMQ 5.0
public static String getLocalhostByNetworkInterface() throws SocketException {
    List<String> candidatesHost = new ArrayList<String>();
    Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    while (enumeration.hasMoreElements()) {
        NetworkInterface networkInterface = enumeration.nextElement();
        // Workaround for docker0 bridge
        if ("docker0".equals(networkInterface.getName()) || !networkInterface.isUp()) {
            continue;
        }
        Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
        while (addrs.hasMoreElements()) {
            InetAddress address = addrs.nextElement();
            if (address.isLoopbackAddress()) {
                continue;
            }
            // ip4 highter priority
            if (address instanceof Inet6Address) {
                candidatesHost.add(address.getHostAddress());
                continue;
            }
            return address.getHostAddress();
        }
    }
    if (!candidatesHost.isEmpty()) {
        return candidatesHost.get(0);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 69 with Inet6Address

use of java.net.Inet6Address in project narayana by jbosstm.

the class Utility method calculateHostInetAddr.

private static synchronized void calculateHostInetAddr() throws UnknownHostException {
    if (myAddr == null) {
        myAddr = new long[2];
        myAddr[0] = 0;
        myAddr[1] = 0;
        byte[] b = null;
        InetAddress addr;
        try {
            addr = InetAddress.getLocalHost();
        } catch (final UnknownHostException uhe) {
            tsLogger.i18NLogger.warn_utils_Utility_2();
            addr = InetAddress.getByName(null);
        }
        if (addr instanceof Inet6Address) {
            // 16 bytes to work with.
            b = addr.getAddress();
        } else {
            /*
                 * Convert ipv4 to ipv6
                 * 
                 * We only have 4 bytes here.
                 * 
                 * ::FFFF:129.144.52.38
                 */
            byte[] v4Address = addr.getAddress();
            if (v4Address.length > 4)
                throw new UnknownHostException();
            b = new byte[16];
            for (int i = 0; i < 10; i++) b[i] = 0;
            b[10] = b[11] = (byte) 255;
            System.arraycopy(v4Address, 0, b, 12, v4Address.length);
        }
        for (int i = 0; i < 8; i++) {
            /*
                 * Convert signed byte into unsigned.
                 */
            int l = 0x7f & b[i];
            l += (0x80 & b[i]);
            myAddr[0] = (myAddr[0] << 8) | l;
        }
        for (int i = 8; i < 16; i++) {
            /*
                 * Convert signed byte into unsigned.
                 */
            int l = 0x7f & b[i];
            l += (0x80 & b[i]);
            myAddr[1] = (myAddr[1] << 8) | l;
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 70 with Inet6Address

use of java.net.Inet6Address in project i2p.i2p by i2p.

the class MTU method getMTU.

/**
 * The MTU for the socket interface, if available.
 * Not available for Java 5.
 *
 * Note that we don't return the value for the default interface if
 * we can't find the address. Finding the default interface is hard,
 * altough we could perhaps just look for the first non-loopback address.
 * But the MTU of the default route probably isn't relevant.
 *
 * @param ia null ok
 * @return 0 if Java 5, or if not bound to an address;
 *         limited to range MIN_MTU to LARGE_MTU.
 */
public static int getMTU(InetAddress ia) {
    if (ia == null || !hasMTU)
        return 0;
    Enumeration<NetworkInterface> ifcs;
    try {
        ifcs = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException se) {
        return 0;
    } catch (java.lang.Error e) {
        // at net.i2p.util.Addresses.getAddresses ...
        return 0;
    }
    if (ifcs != null) {
        while (ifcs.hasMoreElements()) {
            NetworkInterface ifc = ifcs.nextElement();
            for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements(); ) {
                InetAddress addr = addrs.nextElement();
                if (ia.equals(addr)) {
                    try {
                        // testing
                        // return ifc.getMTU();
                        boolean isIPv6 = addr instanceof Inet6Address;
                        int mtu = ifc.getMTU();
                        if ((isIPv6 && mtu < PeerState.MIN_IPV6_MTU) || (!isIPv6 && mtu < PeerState.MIN_MTU)) {
                            Log log = I2PAppContext.getGlobalContext().logManager().getLog(MTU.class);
                            log.logAlways(Log.WARN, "Unusually low MTU " + mtu + " for interface " + ia + ", consider disabling");
                        }
                        return rectify(isIPv6, mtu);
                    } catch (SocketException se) {
                    // ignore
                    } catch (Throwable t) {
                        // version detection wrong or the JVM doesn't support it
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}
Also used : SocketException(java.net.SocketException) Log(net.i2p.util.Log) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Aggregations

Inet6Address (java.net.Inet6Address)286 InetAddress (java.net.InetAddress)196 Inet4Address (java.net.Inet4Address)110 NetworkInterface (java.net.NetworkInterface)54 UnknownHostException (java.net.UnknownHostException)51 SocketException (java.net.SocketException)32 InetSocketAddress (java.net.InetSocketAddress)31 IOException (java.io.IOException)29 LinkAddress (android.net.LinkAddress)28 Test (org.junit.Test)26 RouteInfo (android.net.RouteInfo)21 IpPrefix (android.net.IpPrefix)19 ArrayList (java.util.ArrayList)19 LinkProperties (android.net.LinkProperties)15 ByteBuffer (java.nio.ByteBuffer)9 InterfaceAddress (java.net.InterfaceAddress)7 StringJoiner (java.util.StringJoiner)7 PrintWriter (java.io.PrintWriter)6 HashMap (java.util.HashMap)6 Test (org.junit.jupiter.api.Test)6