Search in sources :

Example 11 with Inet6Address

use of java.net.Inet6Address in project chromeview by pwnall.

the class AndroidNetworkLibrary method getNetworkList.

/**
     * @return the network interfaces list (if any) string. The items in
     *         the list string are delimited by a semicolon ";", each item
     *         is a network interface name and address pair and formatted
     *         as "name,address". e.g.
     *           eth0,10.0.0.2;eth0,fe80::5054:ff:fe12:3456
     *         represents a network list string which containts two items.
     */
@CalledByNative
public static String getNetworkList() {
    Enumeration<NetworkInterface> list = null;
    try {
        list = NetworkInterface.getNetworkInterfaces();
        if (list == null)
            return "";
    } catch (SocketException e) {
        Log.w(TAG, "Unable to get network interfaces: " + e);
        return "";
    }
    StringBuilder result = new StringBuilder();
    while (list.hasMoreElements()) {
        NetworkInterface netIf = list.nextElement();
        try {
            // Skip loopback interfaces, and ones which are down.
            if (!netIf.isUp() || netIf.isLoopback())
                continue;
            Enumeration<InetAddress> addressList = netIf.getInetAddresses();
            while (addressList.hasMoreElements()) {
                InetAddress address = addressList.nextElement();
                // Skip loopback addresses configured on non-loopback interfaces.
                if (address.isLoopbackAddress())
                    continue;
                StringBuilder addressString = new StringBuilder();
                addressString.append(netIf.getName());
                addressString.append(",");
                String ipAddress = address.getHostAddress();
                if (address instanceof Inet6Address && ipAddress.contains("%")) {
                    ipAddress = ipAddress.substring(0, ipAddress.lastIndexOf("%"));
                }
                addressString.append(ipAddress);
                if (result.length() != 0)
                    result.append(";");
                result.append(addressString.toString());
            }
        } catch (SocketException e) {
            continue;
        }
    }
    return result.toString();
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) CalledByNative(org.chromium.base.CalledByNative)

Example 12 with Inet6Address

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

the class URLTest method testEqualsDoesNotDoHostnameResolution.

/**
     * Android's URL.equals() works as if the network is down. This is different
     * from the RI, which does potentially slow and inconsistent DNS lookups in
     * URL.equals.
     */
public void testEqualsDoesNotDoHostnameResolution() throws Exception {
    for (InetAddress inetAddress : InetAddress.getAllByName("localhost")) {
        String address = inetAddress.getHostAddress();
        if (inetAddress instanceof Inet6Address) {
            address = "[" + address + "]";
        }
        URL urlByHostName = new URL("http://localhost/foo?bar=baz#quux");
        URL urlByAddress = new URL("http://" + address + "/foo?bar=baz#quux");
        assertFalse("Expected " + urlByHostName + " to not equal " + urlByAddress, // fails on RI, which does DNS
        urlByHostName.equals(urlByAddress));
    }
}
Also used : Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) URL(java.net.URL)

Example 13 with Inet6Address

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

the class OldSocketTest method test_getLocalSocketAddress.

public void test_getLocalSocketAddress() throws IOException {
    // set up server connect and then validate that we get the right
    // response for the local address
    int sport = startServer("SServer getLocSocketAddress");
    s = new Socket(InetAddress.getLocalHost(), sport, null, 0);
    assertEquals(new InetSocketAddress(InetAddress.getLocalHost(), s.getLocalPort()), s.getLocalSocketAddress());
    s.close();
    // now create a socket that is not bound and validate we get the
    // right answer
    Socket theSocket = new Socket();
    assertNull("Returned incorrect InetSocketAddress -unbound socket- Expected null", theSocket.getLocalSocketAddress());
    // now bind the socket and make sure we get the right answer
    theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
    assertEquals(new InetSocketAddress(InetAddress.getLocalHost(), theSocket.getLocalPort()), theSocket.getLocalSocketAddress());
    theSocket.close();
    // now validate that behavior when the any address is returned
    s = new Socket();
    s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
    assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), ((InetSocketAddress) s.getLocalSocketAddress()).getAddress() instanceof Inet6Address);
    s.close();
    // now validate the same for getLocalAddress
    s = new Socket();
    s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
    assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), ((InetSocketAddress) s.getLocalSocketAddress()).getAddress() instanceof Inet6Address);
    s.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 14 with Inet6Address

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

the class IPv6TetheringCoordinator method getIPv6OnlyLinkProperties.

private static LinkProperties getIPv6OnlyLinkProperties(LinkProperties lp) {
    final LinkProperties v6only = new LinkProperties();
    if (lp == null) {
        return v6only;
    }
    // NOTE: At this time we don't copy over any information about any
    // stacked links. No current stacked link configuration has IPv6.
    v6only.setInterfaceName(lp.getInterfaceName());
    v6only.setMtu(lp.getMtu());
    for (LinkAddress linkAddr : lp.getLinkAddresses()) {
        if (linkAddr.isGlobalPreferred() && linkAddr.getPrefixLength() == 64) {
            v6only.addLinkAddress(linkAddr);
        }
    }
    for (RouteInfo routeInfo : lp.getRoutes()) {
        final IpPrefix destination = routeInfo.getDestination();
        if ((destination.getAddress() instanceof Inet6Address) && (destination.getPrefixLength() <= 64)) {
            v6only.addRoute(routeInfo);
        }
    }
    for (InetAddress dnsServer : lp.getDnsServers()) {
        if (isIPv6GlobalAddress(dnsServer)) {
            // For now we include ULAs.
            v6only.addDnsServer(dnsServer);
        }
    }
    v6only.setDomains(lp.getDomains());
    return v6only;
}
Also used : LinkAddress(android.net.LinkAddress) IpPrefix(android.net.IpPrefix) Inet6Address(java.net.Inet6Address) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Example 15 with Inet6Address

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

the class Vpn method makeLinkProperties.

private LinkProperties makeLinkProperties() {
    boolean allowIPv4 = mConfig.allowIPv4;
    boolean allowIPv6 = mConfig.allowIPv6;
    LinkProperties lp = new LinkProperties();
    lp.setInterfaceName(mInterface);
    if (mConfig.addresses != null) {
        for (LinkAddress address : mConfig.addresses) {
            lp.addLinkAddress(address);
            allowIPv4 |= address.getAddress() instanceof Inet4Address;
            allowIPv6 |= address.getAddress() instanceof Inet6Address;
        }
    }
    if (mConfig.routes != null) {
        for (RouteInfo route : mConfig.routes) {
            lp.addRoute(route);
            InetAddress address = route.getDestination().getAddress();
            allowIPv4 |= address instanceof Inet4Address;
            allowIPv6 |= address instanceof Inet6Address;
        }
    }
    if (mConfig.dnsServers != null) {
        for (String dnsServer : mConfig.dnsServers) {
            InetAddress address = InetAddress.parseNumericAddress(dnsServer);
            lp.addDnsServer(address);
            allowIPv4 |= address instanceof Inet4Address;
            allowIPv6 |= address instanceof Inet6Address;
        }
    }
    if (!allowIPv4) {
        lp.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE));
    }
    if (!allowIPv6) {
        lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE));
    }
    // Concatenate search domains into a string.
    StringBuilder buffer = new StringBuilder();
    if (mConfig.searchDomains != null) {
        for (String domain : mConfig.searchDomains) {
            buffer.append(domain).append(' ');
        }
    }
    lp.setDomains(buffer.toString().trim());
    return lp;
}
Also used : LinkAddress(android.net.LinkAddress) IpPrefix(android.net.IpPrefix) Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Aggregations

Inet6Address (java.net.Inet6Address)137 InetAddress (java.net.InetAddress)92 Inet4Address (java.net.Inet4Address)45 NetworkInterface (java.net.NetworkInterface)28 LinkAddress (android.net.LinkAddress)21 IpPrefix (android.net.IpPrefix)19 IOException (java.io.IOException)18 UnknownHostException (java.net.UnknownHostException)17 LinkProperties (android.net.LinkProperties)15 SocketException (java.net.SocketException)15 RouteInfo (android.net.RouteInfo)14 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuffer (java.nio.ByteBuffer)9 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 RaParams (android.net.ip.RouterAdvertisementDaemon.RaParams)5 Pair (android.util.Pair)5 ProvisioningChange (android.net.LinkProperties.ProvisioningChange)4 StructNdMsg (android.net.netlink.StructNdMsg)4