Search in sources :

Example 26 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class DhcpPacket method toDhcpResults.

/**
     *  Construct a DhcpResults object from a DHCP reply packet.
     */
public DhcpResults toDhcpResults() {
    Inet4Address ipAddress = mYourIp;
    if (ipAddress.equals(Inet4Address.ANY)) {
        ipAddress = mClientIp;
        if (ipAddress.equals(Inet4Address.ANY)) {
            return null;
        }
    }
    int prefixLength;
    if (mSubnetMask != null) {
        try {
            prefixLength = NetworkUtils.netmaskToPrefixLength(mSubnetMask);
        } catch (IllegalArgumentException e) {
            // Non-contiguous netmask.
            return null;
        }
    } else {
        prefixLength = NetworkUtils.getImplicitNetmask(ipAddress);
    }
    DhcpResults results = new DhcpResults();
    try {
        results.ipAddress = new LinkAddress(ipAddress, prefixLength);
    } catch (IllegalArgumentException e) {
        return null;
    }
    if (mGateways.size() > 0) {
        results.gateway = mGateways.get(0);
    }
    results.dnsServers.addAll(mDnsServers);
    results.domains = mDomainName;
    results.serverAddress = mServerIdentifier;
    results.vendorInfo = mVendorInfo;
    results.leaseDuration = (mLeaseTime != null) ? mLeaseTime : INFINITE_LEASE;
    results.mtu = (mMtu != null && MIN_MTU <= mMtu && mMtu <= MAX_MTU) ? mMtu : 0;
    return results;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) DhcpResults(android.net.DhcpResults)

Example 27 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class IpManager method clearIPv4Address.

private void clearIPv4Address() {
    try {
        final InterfaceConfiguration ifcg = new InterfaceConfiguration();
        ifcg.setLinkAddress(new LinkAddress("0.0.0.0/0"));
        mNwService.setInterfaceConfig(mInterfaceName, ifcg);
    } catch (IllegalStateException | RemoteException e) {
        Log.e(mTag, "ALERT: Failed to clear IPv4 address on interface " + mInterfaceName, e);
    }
}
Also used : LinkAddress(android.net.LinkAddress) RemoteException(android.os.RemoteException) InterfaceConfiguration(android.net.InterfaceConfiguration)

Example 28 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

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 29 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 30 with LinkAddress

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

the class WifiConfigStore method getIpConfiguration.

/**
     * get IP configuration for a given network id
     * TODO: We cannot handle IPv6 addresses for configuration
     *       right now until NetworkUtils is fixed. When we do
     *       that, we should remove handling DhcpInfo and move
     *       to using LinkProperties
     */
static DhcpInfoInternal getIpConfiguration(int netId) {
    DhcpInfoInternal dhcpInfoInternal = new DhcpInfoInternal();
    LinkProperties linkProperties = getLinkProperties(netId);
    if (linkProperties != null) {
        Iterator<LinkAddress> iter = linkProperties.getLinkAddresses().iterator();
        if (iter.hasNext()) {
            LinkAddress linkAddress = iter.next();
            dhcpInfoInternal.ipAddress = linkAddress.getAddress().getHostAddress();
            for (RouteInfo route : linkProperties.getRoutes()) {
                dhcpInfoInternal.addRoute(route);
            }
            dhcpInfoInternal.prefixLength = linkAddress.getNetworkPrefixLength();
            Iterator<InetAddress> dnsIterator = linkProperties.getDnses().iterator();
            dhcpInfoInternal.dns1 = dnsIterator.next().getHostAddress();
            if (dnsIterator.hasNext()) {
                dhcpInfoInternal.dns2 = dnsIterator.next().getHostAddress();
            }
        }
    }
    return dhcpInfoInternal;
}
Also used : LinkAddress(android.net.LinkAddress) DhcpInfoInternal(android.net.DhcpInfoInternal) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

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