Search in sources :

Example 31 with LinkAddress

use of android.net.LinkAddress in project XobotOS by xamarin.

the class DataCallState method setLinkProperties.

public SetupResult setLinkProperties(LinkProperties linkProperties, boolean okToUseSystemPropertyDns) {
    SetupResult result;
    // a failure we'll clear again at the bottom of this code.
    if (linkProperties == null)
        linkProperties = new LinkProperties();
    else
        linkProperties.clear();
    if (status == FailCause.NONE.getErrorCode()) {
        String propertyPrefix = "net." + ifname + ".";
        try {
            // set interface name
            linkProperties.setInterfaceName(ifname);
            // set link addresses
            if (addresses != null && addresses.length > 0) {
                for (String addr : addresses) {
                    LinkAddress la;
                    int addrPrefixLen;
                    String[] ap = addr.split("/");
                    if (ap.length == 2) {
                        addr = ap[0];
                        addrPrefixLen = Integer.parseInt(ap[1]);
                    } else {
                        addrPrefixLen = 0;
                    }
                    InetAddress ia;
                    try {
                        ia = NetworkUtils.numericToInetAddress(addr);
                    } catch (IllegalArgumentException e) {
                        throw new UnknownHostException("Non-numeric ip addr=" + addr);
                    }
                    if (!ia.isAnyLocalAddress()) {
                        if (addrPrefixLen == 0) {
                            // Assume point to point
                            addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128;
                        }
                        if (DBG)
                            Log.d(LOG_TAG, "addr/pl=" + addr + "/" + addrPrefixLen);
                        la = new LinkAddress(ia, addrPrefixLen);
                        linkProperties.addLinkAddress(la);
                    }
                }
            } else {
                throw new UnknownHostException("no address for ifname=" + ifname);
            }
            // set dns servers
            if (dnses != null && dnses.length > 0) {
                for (String addr : dnses) {
                    InetAddress ia;
                    try {
                        ia = NetworkUtils.numericToInetAddress(addr);
                    } catch (IllegalArgumentException e) {
                        throw new UnknownHostException("Non-numeric dns addr=" + addr);
                    }
                    if (!ia.isAnyLocalAddress()) {
                        linkProperties.addDns(ia);
                    }
                }
            } else if (okToUseSystemPropertyDns) {
                String[] dnsServers = new String[2];
                dnsServers[0] = SystemProperties.get(propertyPrefix + "dns1");
                dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
                for (String dnsAddr : dnsServers) {
                    InetAddress ia;
                    try {
                        ia = NetworkUtils.numericToInetAddress(dnsAddr);
                    } catch (IllegalArgumentException e) {
                        throw new UnknownHostException("Non-numeric dns addr=" + dnsAddr);
                    }
                    if (!ia.isAnyLocalAddress()) {
                        linkProperties.addDns(ia);
                    }
                }
            } else {
                throw new UnknownHostException("Empty dns response and no system default dns");
            }
            // set gateways
            if ((gateways == null) || (gateways.length == 0)) {
                String sysGateways = SystemProperties.get(propertyPrefix + "gw");
                if (sysGateways != null) {
                    gateways = sysGateways.split(" ");
                } else {
                    gateways = new String[0];
                }
            }
            for (String addr : gateways) {
                InetAddress ia;
                try {
                    ia = NetworkUtils.numericToInetAddress(addr);
                } catch (IllegalArgumentException e) {
                    throw new UnknownHostException("Non-numeric gateway addr=" + addr);
                }
                if (!ia.isAnyLocalAddress()) {
                    linkProperties.addRoute(new RouteInfo(ia));
                }
            }
            result = SetupResult.SUCCESS;
        } catch (UnknownHostException e) {
            Log.d(LOG_TAG, "setLinkProperties: UnknownHostException " + e);
            e.printStackTrace();
            result = SetupResult.ERR_UnacceptableParameter;
        }
    } else {
        if (version < 4) {
            result = SetupResult.ERR_GetLastErrorFromRil;
        } else {
            result = SetupResult.ERR_RilError;
        }
    }
    // An error occurred so clear properties
    if (result != SetupResult.SUCCESS) {
        if (DBG) {
            Log.d(LOG_TAG, "setLinkProperties: error clearing LinkProperties " + "status=" + status + " result=" + result);
        }
        linkProperties.clear();
    }
    return result;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Example 32 with LinkAddress

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

the class NetworkManagementService method setInterfaceConfig.

@Override
public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
    mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
    LinkAddress linkAddr = cfg.getLinkAddress();
    if (linkAddr == null || linkAddr.getAddress() == null) {
        throw new IllegalStateException("Null LinkAddress given");
    }
    final Command cmd = new Command("interface", "setcfg", iface, linkAddr.getAddress().getHostAddress(), linkAddr.getPrefixLength());
    for (String flag : cfg.getFlags()) {
        cmd.appendArg(flag);
    }
    try {
        mConnector.execute(cmd);
    } catch (NativeDaemonConnectorException e) {
        throw e.rethrowAsParcelableException();
    }
}
Also used : LinkAddress(android.net.LinkAddress) Command(com.android.server.NativeDaemonConnector.Command)

Example 33 with LinkAddress

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

the class NetworkManagementService method addLegacyRouteForNetId.

@Override
public void addLegacyRouteForNetId(int netId, RouteInfo routeInfo, int uid) {
    mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
    final Command cmd = new Command("network", "route", "legacy", uid, "add", netId);
    // create triplet: interface dest-ip-addr/prefixlength gateway-ip-addr
    final LinkAddress la = routeInfo.getDestinationLinkAddress();
    cmd.appendArg(routeInfo.getInterface());
    cmd.appendArg(la.getAddress().getHostAddress() + "/" + la.getPrefixLength());
    if (routeInfo.hasGateway()) {
        cmd.appendArg(routeInfo.getGateway().getHostAddress());
    }
    try {
        mConnector.execute(cmd);
    } catch (NativeDaemonConnectorException e) {
        throw e.rethrowAsParcelableException();
    }
}
Also used : LinkAddress(android.net.LinkAddress) Command(com.android.server.NativeDaemonConnector.Command)

Example 34 with LinkAddress

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

the class NetworkManagementServiceTest method testNetworkObservers.

/**
     * Tests that network observers work properly.
     */
public void testNetworkObservers() throws Exception {
    BaseNetworkObserver observer = mock(BaseNetworkObserver.class);
    // Used by registerObserver.
    doReturn(new Binder()).when(observer).asBinder();
    mNMService.registerObserver(observer);
    // Forget everything that happened to the mock so far, so we can explicitly verify
    // everything that happens and does not happen to it from now on.
    reset(observer);
    // Now send NetworkManagementService messages and ensure that the observer methods are
    // called. After every valid message we expect a callback soon after; to ensure that
    // invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end.
    /**
         * Interface changes.
         */
    sendMessage("600 Iface added rmnet12");
    expectSoon(observer).interfaceAdded("rmnet12");
    sendMessage("600 Iface removed eth1");
    expectSoon(observer).interfaceRemoved("eth1");
    sendMessage("607 Iface removed eth1");
    // Invalid code.
    sendMessage("600 Iface borked lo down");
    // Invalid event.
    sendMessage("600 Iface changed clat4 up again");
    // Extra tokens.
    sendMessage("600 Iface changed clat4 up");
    expectSoon(observer).interfaceStatusChanged("clat4", true);
    sendMessage("600 Iface linkstate rmnet0 down");
    expectSoon(observer).interfaceLinkStateChanged("rmnet0", false);
    sendMessage("600 IFACE linkstate clat4 up");
    // Invalid group.
    /**
         * Bandwidth control events.
         */
    sendMessage("601 limit alert data rmnet_usb0");
    expectSoon(observer).limitReached("data", "rmnet_usb0");
    sendMessage("601 invalid alert data rmnet0");
    // Invalid group.
    sendMessage("601 limit increased data rmnet0");
    // Invalid event.
    /**
         * Interface class activity.
         */
    sendMessage("613 IfaceClass active rmnet0");
    expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", true, 0);
    sendMessage("613 IfaceClass active rmnet0 1234");
    expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", true, 1234);
    sendMessage("613 IfaceClass idle eth0");
    expectSoon(observer).interfaceClassDataActivityChanged("eth0", false, 0);
    sendMessage("613 IfaceClass idle eth0 1234");
    expectSoon(observer).interfaceClassDataActivityChanged("eth0", false, 1234);
    sendMessage("613 IfaceClass reallyactive rmnet0 1234");
    expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", false, 1234);
    sendMessage("613 InterfaceClass reallyactive rmnet0");
    // Invalid group.
    /**
         * IP address changes.
         */
    sendMessage("614 Address updated fe80::1/64 wlan0 128 253");
    expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253));
    // There is no "added", so we take this as "removed".
    sendMessage("614 Address added fe80::1/64 wlan0 128 253");
    expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253));
    sendMessage("614 Address removed 2001:db8::1/64 wlan0 1 0");
    expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0));
    sendMessage("614 Address removed 2001:db8::1/64 wlan0 1");
    // Not enough arguments.
    sendMessage("666 Address removed 2001:db8::1/64 wlan0 1 0");
    // Invalid code.
    /**
         * DNS information broadcasts.
         */
    sendMessage("615 DnsInfo servers rmnet_usb0 3600 2001:db8::1");
    expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600, new String[] { "2001:db8::1" });
    sendMessage("615 DnsInfo servers wlan0 14400 2001:db8::1,2001:db8::2");
    expectSoon(observer).interfaceDnsServerInfo("wlan0", 14400, new String[] { "2001:db8::1", "2001:db8::2" });
    // We don't check for negative lifetimes, only for parse errors.
    sendMessage("615 DnsInfo servers wlan0 -3600 ::1");
    expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600, new String[] { "::1" });
    sendMessage("615 DnsInfo servers wlan0 SIXHUNDRED ::1");
    // Non-numeric lifetime.
    sendMessage("615 DnsInfo servers wlan0 2001:db8::1");
    // Missing lifetime.
    sendMessage("615 DnsInfo servers wlan0 3600");
    // No servers.
    sendMessage("615 DnsInfo servers 3600 wlan0 2001:db8::1,2001:db8::2");
    // Non-numeric lifetime.
    sendMessage("615 DnsInfo wlan0 7200 2001:db8::1,2001:db8::2");
    // Invalid tokens.
    sendMessage("666 DnsInfo servers wlan0 5400 2001:db8::1");
    // Invalid code.
    // No syntax checking on the addresses.
    sendMessage("615 DnsInfo servers wlan0 600 ,::,,foo,::1,");
    expectSoon(observer).interfaceDnsServerInfo("wlan0", 600, new String[] { "", "::", "", "foo", "::1" });
    // Make sure nothing else was called.
    verifyNoMoreInteractions(observer);
}
Also used : LinkAddress(android.net.LinkAddress) Binder(android.os.Binder) BaseNetworkObserver(com.android.server.net.BaseNetworkObserver)

Example 35 with LinkAddress

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

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)

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