Search in sources :

Example 51 with LinkAddress

use of android.net.LinkAddress in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class WifiConfigController method validateIpConfigFields.

private int validateIpConfigFields(StaticIpConfiguration staticIpConfiguration) {
    if (mIpAddressView == null)
        return 0;
    String ipAddr = mIpAddressView.getText().toString();
    if (TextUtils.isEmpty(ipAddr))
        return R.string.wifi_ip_settings_invalid_ip_address;
    Inet4Address inetAddr = getIPv4Address(ipAddr);
    if (inetAddr == null || inetAddr.equals(Inet4Address.ANY)) {
        return R.string.wifi_ip_settings_invalid_ip_address;
    }
    int networkPrefixLength = -1;
    try {
        networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString());
        if (networkPrefixLength < 0 || networkPrefixLength > 32) {
            return R.string.wifi_ip_settings_invalid_network_prefix_length;
        }
        staticIpConfiguration.ipAddress = new LinkAddress(inetAddr, networkPrefixLength);
    } catch (NumberFormatException e) {
        // Set the hint as default after user types in ip address
        mNetworkPrefixLengthView.setText(mConfigUi.getContext().getString(R.string.wifi_network_prefix_length_hint));
    } catch (IllegalArgumentException e) {
        return R.string.wifi_ip_settings_invalid_ip_address;
    }
    String gateway = mGatewayView.getText().toString();
    if (TextUtils.isEmpty(gateway)) {
        try {
            //Extract a default gateway from IP address
            InetAddress netPart = NetworkUtils.getNetworkPart(inetAddr, networkPrefixLength);
            byte[] addr = netPart.getAddress();
            addr[addr.length - 1] = 1;
            mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress());
        } catch (RuntimeException ee) {
        } catch (java.net.UnknownHostException u) {
        }
    } else {
        InetAddress gatewayAddr = getIPv4Address(gateway);
        if (gatewayAddr == null) {
            return R.string.wifi_ip_settings_invalid_gateway;
        }
        if (gatewayAddr.isMulticastAddress()) {
            return R.string.wifi_ip_settings_invalid_gateway;
        }
        staticIpConfiguration.gateway = gatewayAddr;
    }
    String dns = mDns1View.getText().toString();
    InetAddress dnsAddr = null;
    if (TextUtils.isEmpty(dns)) {
        //If everything else is valid, provide hint as a default option
        mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint));
    } else {
        dnsAddr = getIPv4Address(dns);
        if (dnsAddr == null) {
            return R.string.wifi_ip_settings_invalid_dns;
        }
        staticIpConfiguration.dnsServers.add(dnsAddr);
    }
    if (mDns2View.length() > 0) {
        dns = mDns2View.getText().toString();
        dnsAddr = getIPv4Address(dns);
        if (dnsAddr == null) {
            return R.string.wifi_ip_settings_invalid_dns;
        }
        staticIpConfiguration.dnsServers.add(dnsAddr);
    }
    return 0;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) InetAddress(java.net.InetAddress) AccessPoint(com.android.settingslib.wifi.AccessPoint)

Example 52 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by DirtyUnicorns.

the class ApfTest method testApfFilterArp.

@LargeTest
public void testApfFilterArp() throws Exception {
    MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
    ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, ALLOW_MULTICAST, mLog);
    // Verify initially ARP request filter is off, and GARP filter is on.
    verifyArpFilter(ipManagerCallback.getApfProgram(), PASS);
    // Inform ApfFilter of our address and verify ARP filtering is on
    LinkAddress linkAddress = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 24);
    LinkProperties lp = new LinkProperties();
    assertTrue(lp.addLinkAddress(linkAddress));
    verifyArpFilter(getProgram(ipManagerCallback, apfFilter, lp), DROP);
    // Inform ApfFilter of loss of IP and verify ARP filtering is off
    verifyArpFilter(getProgram(ipManagerCallback, apfFilter, new LinkProperties()), PASS);
    apfFilter.shutdown();
}
Also used : LinkAddress(android.net.LinkAddress) ApfFilter(android.net.apf.ApfFilter) LinkProperties(android.net.LinkProperties) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 53 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by DirtyUnicorns.

the class ApfTest method testApfFilterMulticast.

@LargeTest
public void testApfFilterMulticast() throws Exception {
    final byte[] unicastIpv4Addr = { (byte) 192, 0, 2, 63 };
    final byte[] broadcastIpv4Addr = { (byte) 192, 0, 2, (byte) 255 };
    final byte[] multicastIpv4Addr = { (byte) 224, 0, 0, 1 };
    final byte[] multicastIpv6Addr = { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xfb };
    MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
    LinkAddress link = new LinkAddress(InetAddress.getByAddress(unicastIpv4Addr), 24);
    LinkProperties lp = new LinkProperties();
    lp.addLinkAddress(link);
    ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, ALLOW_MULTICAST, mLog);
    apfFilter.setLinkProperties(lp);
    byte[] program = ipManagerCallback.getApfProgram();
    // Construct IPv4 and IPv6 multicast packets.
    ByteBuffer mcastv4packet = ByteBuffer.wrap(new byte[100]);
    mcastv4packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(mcastv4packet, IPV4_DEST_ADDR_OFFSET, multicastIpv4Addr);
    ByteBuffer mcastv6packet = ByteBuffer.wrap(new byte[100]);
    mcastv6packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IPV6);
    mcastv6packet.put(IPV6_NEXT_HEADER_OFFSET, (byte) IPPROTO_UDP);
    put(mcastv6packet, IPV6_DEST_ADDR_OFFSET, multicastIpv6Addr);
    // Construct IPv4 broadcast packet.
    ByteBuffer bcastv4packet1 = ByteBuffer.wrap(new byte[100]);
    bcastv4packet1.put(ETH_BROADCAST_MAC_ADDRESS);
    bcastv4packet1.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(bcastv4packet1, IPV4_DEST_ADDR_OFFSET, multicastIpv4Addr);
    ByteBuffer bcastv4packet2 = ByteBuffer.wrap(new byte[100]);
    bcastv4packet2.put(ETH_BROADCAST_MAC_ADDRESS);
    bcastv4packet2.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(bcastv4packet2, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
    // Construct IPv4 broadcast with L2 unicast address packet (b/30231088).
    ByteBuffer bcastv4unicastl2packet = ByteBuffer.wrap(new byte[100]);
    bcastv4unicastl2packet.put(TestApfFilter.MOCK_MAC_ADDR);
    bcastv4unicastl2packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(bcastv4unicastl2packet, IPV4_DEST_ADDR_OFFSET, broadcastIpv4Addr);
    // Verify initially disabled multicast filter is off
    assertPass(program, mcastv4packet.array());
    assertPass(program, mcastv6packet.array());
    assertPass(program, bcastv4packet1.array());
    assertPass(program, bcastv4packet2.array());
    assertPass(program, bcastv4unicastl2packet.array());
    // Turn on multicast filter and verify it works
    ipManagerCallback.resetApfProgramWait();
    apfFilter.setMulticastFilter(true);
    program = ipManagerCallback.getApfProgram();
    assertDrop(program, mcastv4packet.array());
    assertDrop(program, mcastv6packet.array());
    assertDrop(program, bcastv4packet1.array());
    assertDrop(program, bcastv4packet2.array());
    assertDrop(program, bcastv4unicastl2packet.array());
    // Turn off multicast filter and verify it's off
    ipManagerCallback.resetApfProgramWait();
    apfFilter.setMulticastFilter(false);
    program = ipManagerCallback.getApfProgram();
    assertPass(program, mcastv4packet.array());
    assertPass(program, mcastv6packet.array());
    assertPass(program, bcastv4packet1.array());
    assertPass(program, bcastv4packet2.array());
    assertPass(program, bcastv4unicastl2packet.array());
    // Verify it can be initialized to on
    ipManagerCallback.resetApfProgramWait();
    apfFilter.shutdown();
    apfFilter = new TestApfFilter(ipManagerCallback, DROP_MULTICAST, mLog);
    apfFilter.setLinkProperties(lp);
    program = ipManagerCallback.getApfProgram();
    assertDrop(program, mcastv4packet.array());
    assertDrop(program, mcastv6packet.array());
    assertDrop(program, bcastv4packet1.array());
    assertDrop(program, bcastv4unicastl2packet.array());
    // Verify that ICMPv6 multicast is not dropped.
    mcastv6packet.put(IPV6_NEXT_HEADER_OFFSET, (byte) IPPROTO_ICMPV6);
    assertPass(program, mcastv6packet.array());
    apfFilter.shutdown();
}
Also used : LinkAddress(android.net.LinkAddress) ApfFilter(android.net.apf.ApfFilter) LinkProperties(android.net.LinkProperties) ByteBuffer(java.nio.ByteBuffer) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 54 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by DirtyUnicorns.

the class ApfTest method testApfFilterIPv4.

@LargeTest
public void testApfFilterIPv4() throws Exception {
    MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
    LinkAddress link = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 19);
    LinkProperties lp = new LinkProperties();
    lp.addLinkAddress(link);
    ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, DROP_MULTICAST, mLog);
    apfFilter.setLinkProperties(lp);
    byte[] program = ipManagerCallback.getApfProgram();
    // Verify empty packet of 100 zero bytes is passed
    ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
    assertPass(program, packet.array());
    // Verify unicast IPv4 packet is passed
    put(packet, ETH_DEST_ADDR_OFFSET, TestApfFilter.MOCK_MAC_ADDR);
    packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_IPV4_ADDR);
    assertPass(program, packet.array());
    // Verify L2 unicast to IPv4 broadcast addresses is dropped (b/30231088)
    put(packet, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
    assertDrop(program, packet.array());
    put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_BROADCAST_IPV4_ADDR);
    assertDrop(program, packet.array());
    // Verify multicast/broadcast IPv4, not DHCP to us, is dropped
    put(packet, ETH_DEST_ADDR_OFFSET, ETH_BROADCAST_MAC_ADDRESS);
    assertDrop(program, packet.array());
    packet.put(IPV4_VERSION_IHL_OFFSET, (byte) 0x45);
    assertDrop(program, packet.array());
    packet.put(IPV4_PROTOCOL_OFFSET, (byte) IPPROTO_UDP);
    assertDrop(program, packet.array());
    packet.putShort(UDP_DESTINATION_PORT_OFFSET, (short) DHCP_CLIENT_PORT);
    assertDrop(program, packet.array());
    put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_MULTICAST_IPV4_ADDR);
    assertDrop(program, packet.array());
    put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_BROADCAST_IPV4_ADDR);
    assertDrop(program, packet.array());
    put(packet, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
    assertDrop(program, packet.array());
    // Verify broadcast IPv4 DHCP to us is passed
    put(packet, DHCP_CLIENT_MAC_OFFSET, TestApfFilter.MOCK_MAC_ADDR);
    assertPass(program, packet.array());
    // Verify unicast IPv4 DHCP to us is passed
    put(packet, ETH_DEST_ADDR_OFFSET, TestApfFilter.MOCK_MAC_ADDR);
    assertPass(program, packet.array());
    apfFilter.shutdown();
}
Also used : LinkAddress(android.net.LinkAddress) ApfFilter(android.net.apf.ApfFilter) LinkProperties(android.net.LinkProperties) ByteBuffer(java.nio.ByteBuffer) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 55 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacketTest method assertDhcpResults.

private void assertDhcpResults(String ipAddress, String gateway, String dnsServersString, String domains, String serverAddress, String vendorInfo, int leaseDuration, boolean hasMeteredHint, int mtu, DhcpResults dhcpResults) throws Exception {
    assertEquals(new LinkAddress(ipAddress), dhcpResults.ipAddress);
    assertEquals(v4Address(gateway), dhcpResults.gateway);
    String[] dnsServerStrings = dnsServersString.split(",");
    ArrayList dnsServers = new ArrayList();
    for (String dnsServerString : dnsServerStrings) {
        dnsServers.add(v4Address(dnsServerString));
    }
    assertEquals(dnsServers, dhcpResults.dnsServers);
    assertEquals(domains, dhcpResults.domains);
    assertEquals(v4Address(serverAddress), dhcpResults.serverAddress);
    assertEquals(vendorInfo, dhcpResults.vendorInfo);
    assertEquals(leaseDuration, dhcpResults.leaseDuration);
    assertEquals(hasMeteredHint, dhcpResults.hasMeteredHint());
    assertEquals(mtu, dhcpResults.mtu);
}
Also used : LinkAddress(android.net.LinkAddress) ArrayList(java.util.ArrayList)

Aggregations

LinkAddress (android.net.LinkAddress)210 LinkProperties (android.net.LinkProperties)70 InetAddress (java.net.InetAddress)70 RouteInfo (android.net.RouteInfo)52 SmallTest (android.test.suitebuilder.annotation.SmallTest)29 RemoteException (android.os.RemoteException)27 Inet4Address (java.net.Inet4Address)24 InterfaceConfiguration (android.net.InterfaceConfiguration)22 Inet6Address (java.net.Inet6Address)21 IpPrefix (android.net.IpPrefix)20 StaticIpConfiguration (android.net.StaticIpConfiguration)20 IOException (java.io.IOException)15 ApfFilter (android.net.apf.ApfFilter)12 ByteBuffer (java.nio.ByteBuffer)12 ProxyInfo (android.net.ProxyInfo)10 VpnConfig (com.android.internal.net.VpnConfig)10 DhcpResults (android.net.DhcpResults)9 LargeTest (android.test.suitebuilder.annotation.LargeTest)9 Command (com.android.server.NativeDaemonConnector.Command)9 EOFException (java.io.EOFException)6