Search in sources :

Example 56 with InterfaceAddress

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

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 {
        Collection<InterfaceAddress> interfaceAddresses = 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 57 with InterfaceAddress

use of java.net.InterfaceAddress in project robovm by robovm.

the class NetworkInterfaceTest method testLoopback.

// RoboVM note: Tests a method we have removed.
// // http://code.google.com/p/android/issues/detail?id=34022
// public void test_collectIpv6Addresses_3digitInterfaceIndex() throws Exception {
//     String lines[] = new String[] {
//             "fe800000000000000000000000000000 407 40 20 80    wlan0" };
//     List<InetAddress> addresses = new ArrayList<InetAddress>(1);
//     List<InterfaceAddress> ifAddresses = new ArrayList<InterfaceAddress>(1);
//     NetworkInterface.collectIpv6Addresses("wlan0", 1, addresses,
//             ifAddresses, lines);
//     assertEquals(1, addresses.size());
//     assertEquals(1, ifAddresses.size());
//     // Make sure the prefix length (field #3) is parsed correctly
//     assertEquals(4*16 + 0, ifAddresses.get(0).getNetworkPrefixLength());
// }
// RoboVM note: Tests a method we have removed.
// public void test_collectIpv6Addresses_skipsUnmatchedLines() throws Exception {
//     String[] lines = new String[] {
//             "fe800000000000000000000000000000 40 40 20 80    wlan0",
//             "fe100000000000000000000000000000 41 40 20 80    wlan1",
//             "feb00000000000000000000000000000 42 40 20 80    wlan2" };
//     List<InetAddress> addresses = new ArrayList<InetAddress>(1);
//     List<InterfaceAddress> ifAddresses = new ArrayList<InterfaceAddress>(1);
//     NetworkInterface.collectIpv6Addresses("wlan0", 1, addresses,
//             ifAddresses, lines);
//     assertEquals(1, addresses.size());
//     assertEquals(1, ifAddresses.size());
// }
public void testLoopback() throws Exception {
    // We know lo shouldn't have a hardware address or an IPv4 broadcast address.
    NetworkInterface lo = NetworkInterface.getByName(LO);
    assertNull(lo.getHardwareAddress());
    for (InterfaceAddress ia : lo.getInterfaceAddresses()) {
        assertNull(ia.getBroadcast());
    }
    // But eth0, if it exists, should...
    NetworkInterface eth0 = NetworkInterface.getByName(ETH0);
    if (eth0 != null) {
        assertEquals(6, eth0.getHardwareAddress().length);
        for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
            if (ia.getAddress() instanceof Inet4Address) {
                assertNotNull(ia.getBroadcast());
            }
        }
    }
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface)

Example 58 with InterfaceAddress

use of java.net.InterfaceAddress in project robovm by robovm.

the class Issue1137Test method testIssue1137.

@Test
public void testIssue1137() throws SocketException {
    // Tests the new implementation of {@link NetworkInterface#getInterfaces()} 
    // based on getifaddr() instead of ioctl to retrieve ipv4 addresses.
    // Just a smoke test checking if the loopback device is available.
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    boolean found = false;
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface itf = networkInterfaces.nextElement();
        for (InterfaceAddress addr : itf.getInterfaceAddresses()) {
            if (addr.getAddress() instanceof Inet4Address) {
                if (addr.getAddress().isLoopbackAddress()) {
                    found = true;
                    break;
                }
            }
        }
    }
    assertTrue(found);
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Test(org.junit.Test)

Example 59 with InterfaceAddress

use of java.net.InterfaceAddress in project platform_frameworks_base by android.

the class LinkAddressTest method testConstructors.

public void testConstructors() throws SocketException {
    LinkAddress address;
    // Valid addresses work as expected.
    address = new LinkAddress(V4_ADDRESS, 25);
    assertEquals(V4_ADDRESS, address.getAddress());
    assertEquals(25, address.getPrefixLength());
    assertEquals(0, address.getFlags());
    assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
    address = new LinkAddress(V6_ADDRESS, 127);
    assertEquals(V6_ADDRESS, address.getAddress());
    assertEquals(127, address.getPrefixLength());
    assertEquals(0, address.getFlags());
    assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
    // Nonsensical flags/scopes or combinations thereof are acceptable.
    address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
    assertEquals(V6_ADDRESS, address.getAddress());
    assertEquals(64, address.getPrefixLength());
    assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
    assertEquals(RT_SCOPE_LINK, address.getScope());
    address = new LinkAddress(V4 + "/23", 123, 456);
    assertEquals(V4_ADDRESS, address.getAddress());
    assertEquals(23, address.getPrefixLength());
    assertEquals(123, address.getFlags());
    assertEquals(456, address.getScope());
    // InterfaceAddress doesn't have a constructor. Fetch some from an interface.
    List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
    // We expect to find 127.0.0.1/8 and ::1/128, in any order.
    LinkAddress ipv4Loopback, ipv6Loopback;
    assertEquals(2, addrs.size());
    if (addrs.get(0).getAddress() instanceof Inet4Address) {
        ipv4Loopback = new LinkAddress(addrs.get(0));
        ipv6Loopback = new LinkAddress(addrs.get(1));
    } else {
        ipv4Loopback = new LinkAddress(addrs.get(1));
        ipv6Loopback = new LinkAddress(addrs.get(0));
    }
    assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
    assertEquals(8, ipv4Loopback.getPrefixLength());
    assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
    assertEquals(128, ipv6Loopback.getPrefixLength());
    // Null addresses are rejected.
    try {
        address = new LinkAddress(null, 24);
        fail("Null InetAddress should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("Null string should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress((InterfaceAddress) null);
        fail("Null string should cause NullPointerException");
    } catch (NullPointerException expected) {
    }
    // Invalid prefix lengths are rejected.
    try {
        address = new LinkAddress(V4_ADDRESS, -1);
        fail("Negative IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6_ADDRESS, -1);
        fail("Negative IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V4_ADDRESS, 33);
        fail("/33 IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/33 IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/129 IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/129 IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    // Multicast addresses are rejected.
    try {
        address = new LinkAddress("224.0.0.2/32");
        fail("IPv4 multicast address should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress("ff02::1/128");
        fail("IPv6 multicast address should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress)

Example 60 with InterfaceAddress

use of java.net.InterfaceAddress in project platform_frameworks_base by android.

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)

Aggregations

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