Search in sources :

Example 21 with InterfaceAddress

use of java.net.InterfaceAddress in project android_frameworks_base by AOSPA.

the class NetworkManagementService method modifyNat.

private void modifyNat(String action, String internalInterface, String externalInterface) throws SocketException {
    final Command cmd = new Command("nat", action, internalInterface, externalInterface);
    final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(internalInterface);
    if (internalNetworkInterface == null) {
        cmd.appendArg("0");
    } else {
        // Don't touch link-local routes, as link-local addresses aren't routable,
        // kernel creates link-local routes on all interfaces automatically
        List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(internalNetworkInterface.getInterfaceAddresses());
        cmd.appendArg(interfaceAddresses.size());
        for (InterfaceAddress ia : interfaceAddresses) {
            InetAddress addr = NetworkUtils.getNetworkPart(ia.getAddress(), ia.getNetworkPrefixLength());
            cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
        }
    }
    try {
        mConnector.execute(cmd);
    } catch (NativeDaemonConnectorException e) {
        throw e.rethrowAsParcelableException();
    }
}
Also used : Command(com.android.server.NativeDaemonConnector.Command) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 22 with InterfaceAddress

use of java.net.InterfaceAddress in project cxf by apache.

the class UDPDestination method findNetworkInterface.

private NetworkInterface findNetworkInterface() throws SocketException {
    String name = (String) this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE);
    NetworkInterface ret = null;
    if (!StringUtils.isEmpty(name)) {
        ret = NetworkInterface.getByName(name);
    }
    if (ret == null) {
        Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
        List<NetworkInterface> possibles = new ArrayList<>();
        while (ifcs.hasMoreElements()) {
            NetworkInterface ni = ifcs.nextElement();
            if (ni.supportsMulticast() && ni.isUp()) {
                for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                    if (ia.getAddress() instanceof java.net.Inet4Address && !ia.getAddress().isLoopbackAddress() && !ni.getDisplayName().startsWith("vnic")) {
                        possibles.add(ni);
                    }
                }
            }
        }
        ret = possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);
    }
    return ret;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface)

Example 23 with InterfaceAddress

use of java.net.InterfaceAddress in project Payara by payara.

the class DomainDiscoveryService method addLocalNodes.

private void addLocalNodes(List<DiscoveryNode> nodes, int port) throws SocketException, NumberFormatException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) e.nextElement();
        if (!ni.isLoopback()) {
            for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                if (ia.getAddress() instanceof Inet4Address && !ia.getAddress().isLoopbackAddress()) {
                    logger.log(Level.FINE, "Adding network interface {0}", ia.getAddress());
                    nodes.add(new SimpleDiscoveryNode(new Address(ia.getAddress(), port)));
                }
            }
        }
    }
}
Also used : Inet4Address(java.net.Inet4Address) Enumeration(java.util.Enumeration) Address(com.hazelcast.nio.Address) InetAddress(java.net.InetAddress) InterfaceAddress(java.net.InterfaceAddress) Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode)

Example 24 with InterfaceAddress

use of java.net.InterfaceAddress in project scheduling by ow2-proactive.

the class BroadcastDiscoveryClient method broadcastToAllInterfaces.

private void broadcastToAllInterfaces(DatagramSocket c) {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            if (isLoopBack(networkInterface)) {
                continue;
            }
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                InetAddress broadcast = interfaceAddress.getBroadcast();
                if (broadcast == null) {
                    continue;
                }
                try {
                    DatagramPacket sendPacket = new DatagramPacket(new byte[0], 0, broadcast, port);
                    c.send(sendPacket);
                } catch (Exception e) {
                    logger.warn("Could not broadcast to interface " + networkInterface.getName(), e);
                }
            }
        }
    } catch (SocketException e) {
        logger.warn("Could not broadcast to all interfaces", e);
    }
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 25 with InterfaceAddress

use of java.net.InterfaceAddress 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)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)94 NetworkInterface (java.net.NetworkInterface)63 InetAddress (java.net.InetAddress)52 Inet4Address (java.net.Inet4Address)26 SocketException (java.net.SocketException)24 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)15 IOException (java.io.IOException)9 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 Inet6Address (java.net.Inet6Address)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 InetSocketAddress (java.net.InetSocketAddress)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3