Search in sources :

Example 56 with Inet6Address

use of java.net.Inet6Address in project fdroidclient by f-droid.

the class FDroidServiceInfo method writeToParcel.

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(getType());
    dest.writeString(getName());
    dest.writeString(getSubtype());
    dest.writeInt(getPort());
    dest.writeInt(getWeight());
    dest.writeInt(getPriority());
    dest.writeByte(isPersistent() ? (byte) 1 : (byte) 0);
    dest.writeInt(getTextBytes().length);
    dest.writeByteArray(getTextBytes());
    dest.writeInt(getInet4Addresses().length);
    for (int i = 0; i < getInet4Addresses().length; i++) {
        Inet4Address address = getInet4Addresses()[i];
        dest.writeInt(address.getAddress().length);
        dest.writeByteArray(address.getAddress());
    }
    dest.writeInt(getInet6Addresses().length);
    for (int i = 0; i < getInet6Addresses().length; i++) {
        Inet6Address address = getInet6Addresses()[i];
        dest.writeInt(address.getAddress().length);
        dest.writeByteArray(address.getAddress());
    }
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address)

Example 57 with Inet6Address

use of java.net.Inet6Address in project fdroidclient by f-droid.

the class WifiStateChangeService method setIpInfoFromNetworkInterface.

private void setIpInfoFromNetworkInterface() {
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        if (networkInterfaces == null) {
            return;
        }
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface netIf = networkInterfaces.nextElement();
            for (Enumeration<InetAddress> inetAddresses = netIf.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (inetAddress.isLoopbackAddress() || inetAddress instanceof Inet6Address) {
                    continue;
                }
                if (netIf.getDisplayName().contains("wlan0") || netIf.getDisplayName().contains("eth0") || netIf.getDisplayName().contains("ap0")) {
                    FDroidApp.ipAddressString = inetAddress.getHostAddress();
                    for (InterfaceAddress address : netIf.getInterfaceAddresses()) {
                        short networkPrefixLength = address.getNetworkPrefixLength();
                        if (networkPrefixLength > 32) {
                            // java.lang.IllegalArgumentException: Value [64] not in range [0,32]
                            continue;
                        }
                        if (inetAddress.equals(address.getAddress()) && !TextUtils.isEmpty(FDroidApp.ipAddressString)) {
                            String cidr = String.format(Locale.ENGLISH, "%s/%d", FDroidApp.ipAddressString, networkPrefixLength);
                            FDroidApp.subnetInfo = new SubnetUtils(cidr).getInfo();
                            break;
                        }
                    }
                }
            }
        }
    } catch (SocketException e) {
        Log.e(TAG, "Could not get ip address", e);
    }
}
Also used : SocketException(java.net.SocketException) SubnetUtils(org.apache.commons.net.util.SubnetUtils) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 58 with Inet6Address

use of java.net.Inet6Address in project smarthome by eclipse.

the class NetUtil method getIPv4inSubnet.

@Nullable
private String getIPv4inSubnet(String ipAddress, String subnetMask) {
    try {
        final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            final NetworkInterface current = interfaces.nextElement();
            if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) {
                continue;
            }
            for (InterfaceAddress ifAddr : current.getInterfaceAddresses()) {
                InetAddress addr = ifAddr.getAddress();
                if (addr.isLoopbackAddress() || (addr instanceof Inet6Address)) {
                    continue;
                }
                String ipv4AddressOnInterface = addr.getHostAddress();
                String subnetStringOnInterface = getIpv4NetAddress(ipv4AddressOnInterface, ifAddr.getNetworkPrefixLength()) + "/" + String.valueOf(ifAddr.getNetworkPrefixLength());
                String configuredSubnetString = getIpv4NetAddress(ipAddress, Short.parseShort(subnetMask)) + "/" + subnetMask;
                // use first IP within this subnet
                if (subnetStringOnInterface.equals(configuredSubnetString)) {
                    return ipv4AddressOnInterface;
                }
            }
        }
    } catch (SocketException ex) {
        LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex);
    }
    return null;
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 59 with Inet6Address

use of java.net.Inet6Address in project smarthome by eclipse.

the class NetUtil method getLocalIpv4HostAddress.

/**
 * @deprecated Please use the NetworkAddressService with {@link #getPrimaryIpv4HostAddress()}
 *
 *             Get the first candidate for a local IPv4 host address (non loopback, non localhost).
 */
@Deprecated
@Nullable
public static String getLocalIpv4HostAddress() {
    try {
        String hostAddress = null;
        final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            final NetworkInterface current = interfaces.nextElement();
            if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) {
                continue;
            }
            final Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                final InetAddress currentAddr = addresses.nextElement();
                if (currentAddr.isLoopbackAddress() || (currentAddr instanceof Inet6Address)) {
                    continue;
                }
                if (hostAddress != null) {
                    LOGGER.warn("Found multiple local interfaces - ignoring {}", currentAddr.getHostAddress());
                } else {
                    hostAddress = currentAddr.getHostAddress();
                }
            }
        }
        return hostAddress;
    } catch (SocketException ex) {
        LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex);
        return null;
    }
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 60 with Inet6Address

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

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)

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