Search in sources :

Example 31 with IpPrefix

use of android.net.IpPrefix in project android_frameworks_base by crdroidandroid.

the class VpnConfig method addLegacyRoutes.

public void addLegacyRoutes(String routesStr) {
    if (routesStr.trim().equals("")) {
        return;
    }
    String[] routes = routesStr.trim().split(" ");
    for (String route : routes) {
        //each route is ip/prefix
        RouteInfo info = new RouteInfo(new IpPrefix(route), null);
        this.routes.add(info);
        updateAllowedFamilies(info.getDestination().getAddress());
    }
}
Also used : IpPrefix(android.net.IpPrefix) RouteInfo(android.net.RouteInfo)

Example 32 with IpPrefix

use of android.net.IpPrefix in project android_frameworks_base by crdroidandroid.

the class RouterAdvertisementDaemon method assembleRaLocked.

private void assembleRaLocked() {
    final ByteBuffer ra = ByteBuffer.wrap(mRA);
    ra.order(ByteOrder.BIG_ENDIAN);
    boolean shouldSendRA = false;
    try {
        putHeader(ra, mRaParams != null && mRaParams.hasDefaultRoute);
        putSlla(ra, mHwAddr);
        mRaLength = ra.position();
        if (mRaParams != null) {
            putMtu(ra, mRaParams.mtu);
            mRaLength = ra.position();
            for (IpPrefix ipp : mRaParams.prefixes) {
                putPio(ra, ipp, DEFAULT_LIFETIME, DEFAULT_LIFETIME);
                mRaLength = ra.position();
                shouldSendRA = true;
            }
            if (mRaParams.dnses.size() > 0) {
                putRdnss(ra, mRaParams.dnses, DEFAULT_LIFETIME);
                mRaLength = ra.position();
                shouldSendRA = true;
            }
        }
        for (IpPrefix ipp : mDeprecatedInfoTracker.getPrefixes()) {
            putPio(ra, ipp, 0, 0);
            mRaLength = ra.position();
            shouldSendRA = true;
        }
        final Set<Inet6Address> deprecatedDnses = mDeprecatedInfoTracker.getDnses();
        if (!deprecatedDnses.isEmpty()) {
            putRdnss(ra, deprecatedDnses, 0);
            mRaLength = ra.position();
            shouldSendRA = true;
        }
    } catch (BufferOverflowException e) {
        // The packet up to mRaLength  is valid, since it has been updated
        // progressively as the RA was built. Log an error, and continue
        // on as best as possible.
        Log.e(TAG, "Could not construct new RA: " + e);
    }
    // We have nothing worth announcing; indicate as much to maybeSendRA().
    if (!shouldSendRA) {
        mRaLength = 0;
    }
}
Also used : IpPrefix(android.net.IpPrefix) Inet6Address(java.net.Inet6Address) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer)

Example 33 with IpPrefix

use of android.net.IpPrefix in project android_frameworks_base by crdroidandroid.

the class LinkPropertiesTest method testIsReachable.

@SmallTest
// Failing.
@Suppress
public void testIsReachable() {
    final LinkProperties v4lp = new LinkProperties();
    assertFalse(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Add an on-link route, making the on-link DNS server reachable,
    // but there is still no IPv4 address.
    assertTrue(v4lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("75.208.0.0"), 16))));
    assertFalse(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Adding an IPv4 address (right now, any IPv4 address) means we use
    // the routes to compute likely reachability.
    assertTrue(v4lp.addLinkAddress(new LinkAddress(ADDRV4, 16)));
    assertTrue(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Adding a default route makes the off-link DNS server reachable.
    assertTrue(v4lp.addRoute(new RouteInfo(GATEWAY1)));
    assertTrue(v4lp.isReachable(DNS1));
    assertTrue(v4lp.isReachable(DNS2));
    final LinkProperties v6lp = new LinkProperties();
    final InetAddress kLinkLocalDns = NetworkUtils.numericToInetAddress("fe80::6:1");
    final InetAddress kLinkLocalDnsWithScope = NetworkUtils.numericToInetAddress("fe80::6:2%43");
    final InetAddress kOnLinkDns = NetworkUtils.numericToInetAddress("2001:db8:85a3::53");
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertFalse(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a link-local route, making the link-local DNS servers reachable. Because
    // we assume the presence of an IPv6 link-local address, link-local DNS servers
    // are considered reachable, but only those with a non-zero scope identifier.
    assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("fe80::"), 64))));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a link-local address--nothing changes.
    assertTrue(v6lp.addLinkAddress(LINKADDRV6LINKLOCAL));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a global route on link, but no global address yet. DNS servers reachable
    // via a route that doesn't require a gateway: give them the benefit of the
    // doubt and hope the link-local source address suffices for communication.
    assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("2001:db8:85a3::"), 64))));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a global address; the on-link global address DNS server is (still)
    // presumed reachable.
    assertTrue(v6lp.addLinkAddress(new LinkAddress(ADDRV6, 64)));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Adding a default route makes the off-link DNS server reachable.
    assertTrue(v6lp.addRoute(new RouteInfo(GATEWAY62)));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertTrue(v6lp.isReachable(DNS6));
    // Check isReachable on stacked links. This requires that the source IP address be assigned
    // on the interface returned by the route lookup.
    LinkProperties stacked = new LinkProperties();
    // Can't add a stacked link without an interface name.
    stacked.setInterfaceName("v4-test0");
    v6lp.addStackedLink(stacked);
    InetAddress stackedAddress = Address("192.0.0.4");
    LinkAddress stackedLinkAddress = new LinkAddress(stackedAddress, 32);
    assertFalse(v6lp.isReachable(stackedAddress));
    stacked.addLinkAddress(stackedLinkAddress);
    assertFalse(v6lp.isReachable(stackedAddress));
    stacked.addRoute(new RouteInfo(stackedLinkAddress));
    assertTrue(stacked.isReachable(stackedAddress));
    assertTrue(v6lp.isReachable(stackedAddress));
    assertFalse(v6lp.isReachable(DNS1));
    stacked.addRoute(new RouteInfo((IpPrefix) null, stackedAddress));
    assertTrue(v6lp.isReachable(DNS1));
}
Also used : IpPrefix(android.net.IpPrefix) LinkAddress(android.net.LinkAddress) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress) SmallTest(android.test.suitebuilder.annotation.SmallTest) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 34 with IpPrefix

use of android.net.IpPrefix in project android_frameworks_base by crdroidandroid.

the class RouteInfoTest method testMatches.

public void testMatches() {
    class PatchedRouteInfo {

        private final RouteInfo mRouteInfo;

        public PatchedRouteInfo(IpPrefix destination, InetAddress gateway, String iface) {
            mRouteInfo = new RouteInfo(destination, gateway, iface);
        }

        public boolean matches(InetAddress destination) {
            return mRouteInfo.matches(destination);
        }
    }
    PatchedRouteInfo r;
    r = new PatchedRouteInfo(Prefix("2001:db8:f00::ace:d00d/127"), null, "rmnet0");
    assertTrue(r.matches(Address("2001:db8:f00::ace:d00c")));
    assertTrue(r.matches(Address("2001:db8:f00::ace:d00d")));
    assertFalse(r.matches(Address("2001:db8:f00::ace:d00e")));
    assertFalse(r.matches(Address("2001:db8:f00::bad:d00d")));
    assertFalse(r.matches(Address("2001:4868:4860::8888")));
    assertFalse(r.matches(Address("8.8.8.8")));
    r = new PatchedRouteInfo(Prefix("192.0.2.0/23"), null, "wlan0");
    assertTrue(r.matches(Address("192.0.2.43")));
    assertTrue(r.matches(Address("192.0.3.21")));
    assertFalse(r.matches(Address("192.0.0.21")));
    assertFalse(r.matches(Address("8.8.8.8")));
    PatchedRouteInfo ipv6Default = new PatchedRouteInfo(Prefix("::/0"), null, "rmnet0");
    assertTrue(ipv6Default.matches(Address("2001:db8::f00")));
    assertFalse(ipv6Default.matches(Address("192.0.2.1")));
    PatchedRouteInfo ipv4Default = new PatchedRouteInfo(Prefix("0.0.0.0/0"), null, "rmnet0");
    assertTrue(ipv4Default.matches(Address("255.255.255.255")));
    assertTrue(ipv4Default.matches(Address("192.0.2.1")));
    assertFalse(ipv4Default.matches(Address("2001:db8::f00")));
}
Also used : IpPrefix(android.net.IpPrefix) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress)

Example 35 with IpPrefix

use of android.net.IpPrefix in project android_frameworks_base by crdroidandroid.

the class IpPrefixTest method testEquals.

@SmallTest
public void testEquals() {
    IpPrefix p1, p2;
    p1 = new IpPrefix("192.0.2.251/23");
    p2 = new IpPrefix(new byte[] { (byte) 192, (byte) 0, (byte) 2, (byte) 251 }, 23);
    assertAreEqual(p1, p2);
    p1 = new IpPrefix("192.0.2.5/23");
    assertAreEqual(p1, p2);
    p1 = new IpPrefix("192.0.2.5/24");
    assertAreNotEqual(p1, p2);
    p1 = new IpPrefix("192.0.4.5/23");
    assertAreNotEqual(p1, p2);
    p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122");
    p2 = new IpPrefix(IPV6_BYTES, 122);
    assertEquals("2001:db8:dead:beef:f00::80/122", p2.toString());
    assertAreEqual(p1, p2);
    p1 = new IpPrefix("2001:db8:dead:beef:f00::bf/122");
    assertAreEqual(p1, p2);
    p1 = new IpPrefix("2001:db8:dead:beef:f00::8:0/123");
    assertAreNotEqual(p1, p2);
    p1 = new IpPrefix("2001:db8:dead:beef::/122");
    assertAreNotEqual(p1, p2);
    // 192.0.2.4/32 != c000:0204::/32.
    byte[] ipv6bytes = new byte[16];
    System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length);
    p1 = new IpPrefix(ipv6bytes, 32);
    assertAreEqual(p1, new IpPrefix("c000:0204::/32"));
    p2 = new IpPrefix(IPV4_BYTES, 32);
    assertAreNotEqual(p1, p2);
}
Also used : IpPrefix(android.net.IpPrefix) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

IpPrefix (android.net.IpPrefix)89 SmallTest (android.test.suitebuilder.annotation.SmallTest)40 RouteInfo (android.net.RouteInfo)35 InetAddress (java.net.InetAddress)25 LinkAddress (android.net.LinkAddress)20 Inet6Address (java.net.Inet6Address)19 LinkProperties (android.net.LinkProperties)15 StaticIpConfiguration (android.net.StaticIpConfiguration)5 RaParams (android.net.ip.RouterAdvertisementDaemon.RaParams)5 Parcel (android.os.Parcel)5 Suppress (android.test.suitebuilder.annotation.Suppress)5 Inet4Address (java.net.Inet4Address)5 Random (java.util.Random)5 BufferOverflowException (java.nio.BufferOverflowException)4 ByteBuffer (java.nio.ByteBuffer)4