Java Code Examples for android.net.LinkAddress#getAddress()

The following examples show how to use android.net.LinkAddress#getAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Nat464Xlat.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
    LinkProperties stacked = new LinkProperties();
    stacked.setInterfaceName(mIface);

    // Although the clat interface is a point-to-point tunnel, we don't
    // point the route directly at the interface because some apps don't
    // understand routes without gateways (see, e.g., http://b/9597256
    // http://b/9597516). Instead, set the next hop of the route to the
    // clat IPv4 address itself (for those apps, it doesn't matter what
    // the IP of the gateway is, only that there is one).
    RouteInfo ipv4Default = new RouteInfo(
            new LinkAddress(Inet4Address.ANY, 0),
            clatAddress.getAddress(), mIface);
    stacked.addRoute(ipv4Default);
    stacked.addLinkAddress(clatAddress);
    return stacked;
}
 
Example 2
Source File: NetworkManagementService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 3
Source File: NetworkDiagnostics.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void prepareExplicitSourceIcmpMeasurements(InetAddress target) {
    for (LinkAddress l : mLinkProperties.getLinkAddresses()) {
        InetAddress source = l.getAddress();
        if (source instanceof Inet6Address && l.isGlobalPreferred()) {
            Pair<InetAddress, InetAddress> srcTarget = new Pair<>(source, target);
            if (!mExplicitSourceIcmpChecks.containsKey(srcTarget)) {
                Measurement measurement = new Measurement();
                measurement.thread = new Thread(new IcmpCheck(source, target, measurement));
                mExplicitSourceIcmpChecks.put(srcTarget, measurement);
            }
        }
    }
}
 
Example 4
Source File: TetherInterfaceStateMachine.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void updateUpstreamIPv6LinkProperties(LinkProperties v6only) {
    if (mRaDaemon == null) return;

    // Avoid unnecessary work on spurious updates.
    if (Objects.equals(mLastIPv6LinkProperties, v6only)) {
        return;
    }

    RaParams params = null;

    if (v6only != null) {
        params = new RaParams();
        params.mtu = v6only.getMtu();
        params.hasDefaultRoute = v6only.hasIPv6DefaultRoute();

        for (LinkAddress linkAddr : v6only.getLinkAddresses()) {
            if (linkAddr.getPrefixLength() != RFC7421_PREFIX_LENGTH) continue;

            final IpPrefix prefix = new IpPrefix(
                    linkAddr.getAddress(), linkAddr.getPrefixLength());
            params.prefixes.add(prefix);

            final Inet6Address dnsServer = getLocalDnsIpFor(prefix);
            if (dnsServer != null) {
                params.dnses.add(dnsServer);
            }
        }
    }
    // If v6only is null, we pass in null to setRaParams(), which handles
    // deprecation of any existing RA data.

    setRaParams(params);
    mLastIPv6LinkProperties = v6only;
}
 
Example 5
Source File: OffloadController.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static Set<String> computeLocalPrefixStrings(
        Set<IpPrefix> localPrefixes, LinkProperties upstreamLinkProperties) {
    // Create an editable copy.
    final Set<IpPrefix> prefixSet = new HashSet<>(localPrefixes);

    // TODO: If a downstream interface (not currently passed in) is reusing
    // the /64 of the upstream (64share) then:
    //
    //     [a] remove that /64 from the local prefixes
    //     [b] add in /128s for IP addresses on the downstream interface
    //     [c] add in /128s for IP addresses on the upstream interface
    //
    // Until downstream information is available here, simply add /128s from
    // the upstream network; they'll just be redundant with their /64.
    if (upstreamLinkProperties != null) {
        for (LinkAddress linkAddr : upstreamLinkProperties.getLinkAddresses()) {
            if (!linkAddr.isGlobalPreferred()) continue;
            final InetAddress ip = linkAddr.getAddress();
            if (!(ip instanceof Inet6Address)) continue;
            prefixSet.add(new IpPrefix(ip, 128));
        }
    }

    final HashSet<String> localPrefixStrs = new HashSet<>();
    for (IpPrefix pfx : prefixSet) localPrefixStrs.add(pfx.toString());
    return localPrefixStrs;
}
 
Example 6
Source File: Tethering.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void sendOffloadExemptPrefixes(final Set<IpPrefix> localPrefixes) {
    // Add in well-known minimum set.
    PrefixUtils.addNonForwardablePrefixes(localPrefixes);
    // Add tragically hardcoded prefixes.
    localPrefixes.add(PrefixUtils.DEFAULT_WIFI_P2P_PREFIX);

    // Maybe add prefixes or addresses for downstreams, depending on
    // the IP serving mode of each.
    for (TetherInterfaceStateMachine tism : mNotifyList) {
        final LinkProperties lp = tism.linkProperties();

        switch (tism.servingMode()) {
            case IControlsTethering.STATE_UNAVAILABLE:
            case IControlsTethering.STATE_AVAILABLE:
                // No usable LinkProperties in these states.
                continue;
            case IControlsTethering.STATE_TETHERED:
                // Only add IPv4 /32 and IPv6 /128 prefixes. The
                // directly-connected prefixes will be sent as
                // downstream "offload-able" prefixes.
                for (LinkAddress addr : lp.getAllLinkAddresses()) {
                    final InetAddress ip = addr.getAddress();
                    if (ip.isLinkLocalAddress()) continue;
                    localPrefixes.add(PrefixUtils.ipAddressAsPrefix(ip));
                }
                break;
            case IControlsTethering.STATE_LOCAL_ONLY:
                // Add prefixes covering all local IPs.
                localPrefixes.addAll(PrefixUtils.localPrefixesFrom(lp));
                break;
        }
    }

    mOffloadController.setLocalPrefixes(localPrefixes);
}
 
Example 7
Source File: LocalIPUtil.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private String getIp(LinkProperties lp) {
    List<LinkAddress> linkAddresses = lp.getLinkAddresses();
    for(LinkAddress linkAddress: linkAddresses) {
        InetAddress inetAddress = linkAddress.getAddress();
        if (inetAddress instanceof Inet4Address) {
            return inetAddress.getHostAddress();
        }
    }
    return null;
}