Search in sources :

Example 36 with Inet6Address

use of java.net.Inet6Address in project guava by hceylan.

the class InetAddressesTest method testMappedIPv4Addresses.

public void testMappedIPv4Addresses() throws UnknownHostException {
    /*
     * Verify that it is not possible to instantiate an Inet6Address
     * from an "IPv4 mapped" IPv6 address.  Our String-based method can
     * at least identify them, however.
     */
    String mappedStr = "::ffff:192.168.0.1";
    assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
    InetAddress mapped = InetAddresses.forString(mappedStr);
    assertFalse(mapped instanceof Inet6Address);
    assertEquals(InetAddress.getByName("192.168.0.1"), mapped);
    // check upper case
    mappedStr = "::FFFF:192.168.0.1";
    assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
    mapped = InetAddresses.forString(mappedStr);
    assertFalse(mapped instanceof Inet6Address);
    assertEquals(InetAddress.getByName("192.168.0.1"), mapped);
    mappedStr = "0:00:000:0000:0:ffff:1.2.3.4";
    assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
    mapped = InetAddresses.forString(mappedStr);
    assertFalse(mapped instanceof Inet6Address);
    assertEquals(InetAddress.getByName("1.2.3.4"), mapped);
    mappedStr = "::ffff:0102:0304";
    assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
    mapped = InetAddresses.forString(mappedStr);
    assertFalse(mapped instanceof Inet6Address);
    assertEquals(InetAddress.getByName("1.2.3.4"), mapped);
    assertFalse(InetAddresses.isMappedIPv4Address("::"));
    assertFalse(InetAddresses.isMappedIPv4Address("::ffff"));
    assertFalse(InetAddresses.isMappedIPv4Address("::ffff:0"));
    assertFalse(InetAddresses.isMappedIPv4Address("::fffe:0:0"));
    assertFalse(InetAddresses.isMappedIPv4Address("::1:ffff:0:0"));
    assertFalse(InetAddresses.isMappedIPv4Address("foo"));
    assertFalse(InetAddresses.isMappedIPv4Address("192.0.2.1"));
}
Also used : Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 37 with Inet6Address

use of java.net.Inet6Address in project guava by hceylan.

the class InetAddressesTest method testCompatIPv4Addresses.

public void testCompatIPv4Addresses() {
    String[] nonCompatAddresses = { "3ffe::1", "::", "::1" };
    for (int i = 0; i < nonCompatAddresses.length; i++) {
        InetAddress ip = InetAddresses.forString(nonCompatAddresses[i]);
        assertFalse(InetAddresses.isCompatIPv4Address((Inet6Address) ip));
        try {
            InetAddresses.getCompatIPv4Address((Inet6Address) ip);
            fail("IllegalArgumentException expected for '" + nonCompatAddresses[i] + "'");
        } catch (IllegalArgumentException expected) {
        // expected behavior
        }
    }
    String[] validCompatAddresses = { "::1.2.3.4", "::102:304" };
    String compatStr = "1.2.3.4";
    InetAddress compat = InetAddresses.forString(compatStr);
    for (int i = 0; i < validCompatAddresses.length; i++) {
        InetAddress ip = InetAddresses.forString(validCompatAddresses[i]);
        assertTrue("checking '" + validCompatAddresses[i] + "'", ip instanceof Inet6Address);
        assertTrue("checking '" + validCompatAddresses[i] + "'", InetAddresses.isCompatIPv4Address((Inet6Address) ip));
        assertEquals("checking '" + validCompatAddresses[i] + "'", compat, InetAddresses.getCompatIPv4Address((Inet6Address) ip));
    }
}
Also used : Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 38 with Inet6Address

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

the class IpReachabilityMonitor method handleNeighborLost.

private void handleNeighborLost(String msg) {
    InetAddress ip = null;
    final ProvisioningChange delta;
    synchronized (mLock) {
        LinkProperties whatIfLp = new LinkProperties(mLinkProperties);
        for (Map.Entry<InetAddress, Short> entry : mIpWatchList.entrySet()) {
            if (entry.getValue() != StructNdMsg.NUD_FAILED) {
                continue;
            }
            ip = entry.getKey();
            for (RouteInfo route : mLinkProperties.getRoutes()) {
                if (ip.equals(route.getGateway())) {
                    whatIfLp.removeRoute(route);
                }
            }
            if (avoidingBadLinks() || !(ip instanceof Inet6Address)) {
                // We should do this unconditionally, but alas we cannot: b/31827713.
                whatIfLp.removeDnsServer(ip);
            }
        }
        delta = LinkProperties.compareProvisioning(mLinkProperties, whatIfLp);
    }
    if (delta == ProvisioningChange.LOST_PROVISIONING) {
        final String logMsg = "FAILURE: LOST_PROVISIONING, " + msg;
        Log.w(TAG, logMsg);
        if (mCallback != null) {
            // TODO: remove |ip| when the callback signature no longer has
            // an InetAddress argument.
            mCallback.notifyLost(ip, logMsg);
        }
    }
    logNudFailed(delta);
}
Also used : Inet6Address(java.net.Inet6Address) ProvisioningChange(android.net.LinkProperties.ProvisioningChange) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 39 with Inet6Address

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

the class RouterAdvertisementDaemon method putRdnss.

private static void putRdnss(ByteBuffer ra, Set<Inet6Address> dnses, int lifetime) {
    /**
            Recursive DNS Server (RDNSS) Option

             0                   1                   2                   3
             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |     Type      |     Length    |           Reserved            |
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |                           Lifetime                            |
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |                                                               |
            :            Addresses of IPv6 Recursive DNS Servers            :
            |                                                               |
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         */
    final byte ND_OPTION_RDNSS = 25;
    final byte RDNSS_NUM_8OCTETS = asByte(dnses.size() * 2 + 1);
    ra.put(ND_OPTION_RDNSS).put(RDNSS_NUM_8OCTETS).putShort(asShort(0)).putInt(lifetime);
    for (Inet6Address dns : dnses) {
        // NOTE: If the full of list DNS servers doesn't fit in the packet,
        // this code will cause a buffer overflow and the RA won't include
        // this instance of the option at all.
        //
        // TODO: Consider looking at ra.remaining() to determine how many
        // DNS servers will fit, and adding only those.
        ra.put(dns.getAddress());
    }
}
Also used : Inet6Address(java.net.Inet6Address)

Example 40 with Inet6Address

use of java.net.Inet6Address in project nhin-d by DirectProject.

the class IPUtils method getDNSLocalIps.

public static String[] getDNSLocalIps() {
    final List<String> ips = new ArrayList<String>();
    ips.add("127.0.0.1");
    try {
        final Enumeration<NetworkInterface> netInts = NetworkInterface.getNetworkInterfaces();
        while (netInts.hasMoreElements()) {
            final NetworkInterface netInt = netInts.nextElement();
            final Enumeration<InetAddress> addrs = netInt.getInetAddresses();
            while (addrs.hasMoreElements()) {
                final InetAddress addr = addrs.nextElement();
                if (!(addr instanceof Inet6Address))
                    ips.add(addr.getHostAddress());
            }
        }
    } catch (Exception e) {
    }
    return ips.toArray(new String[ips.size()]);
}
Also used : ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) 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