Merge "Remove most of the NetworkStack dependencies on frameworks/base." into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
46b8e08243
@@ -37,15 +37,7 @@ java_library {
|
||||
name: "services.net-module-wifi",
|
||||
srcs: [
|
||||
":framework-services-net-module-wifi-shared-srcs",
|
||||
":net-module-utils-srcs",
|
||||
":net-utils-services-common-srcs",
|
||||
"java/android/net/ip/IpClientCallbacks.java",
|
||||
"java/android/net/ip/IpClientManager.java",
|
||||
"java/android/net/ip/IpClientUtil.java",
|
||||
"java/android/net/util/KeepalivePacketDataUtil.java",
|
||||
"java/android/net/util/NetworkConstants.java",
|
||||
"java/android/net/IpMemoryStore.java",
|
||||
"java/android/net/NetworkMonitorManager.java",
|
||||
],
|
||||
sdk_version: "module_current",
|
||||
min_sdk_version: "30",
|
||||
@@ -89,11 +81,7 @@ filegroup {
|
||||
filegroup {
|
||||
name: "services-connectivity-shared-srcs",
|
||||
srcs: [
|
||||
// TODO: move to networkstack-client
|
||||
"java/android/net/IpMemoryStore.java",
|
||||
"java/android/net/NetworkMonitorManager.java",
|
||||
// TODO: move to libs/net
|
||||
"java/android/net/util/KeepalivePacketDataUtil.java",
|
||||
"java/android/net/util/NetworkConstants.java",
|
||||
],
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.content.Context;
|
||||
import android.net.networkstack.ModuleNetworkStackClient;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Manager class used to communicate with the ip memory store service in the network stack,
|
||||
* which is running in a separate module.
|
||||
* @hide
|
||||
*/
|
||||
public class IpMemoryStore extends IpMemoryStoreClient {
|
||||
private static final String TAG = IpMemoryStore.class.getSimpleName();
|
||||
@NonNull private final CompletableFuture<IIpMemoryStore> mService;
|
||||
@NonNull private final AtomicReference<CompletableFuture<IIpMemoryStore>> mTailNode;
|
||||
|
||||
public IpMemoryStore(@NonNull final Context context) {
|
||||
super(context);
|
||||
mService = new CompletableFuture<>();
|
||||
mTailNode = new AtomicReference<CompletableFuture<IIpMemoryStore>>(mService);
|
||||
getModuleNetworkStackClient(context).fetchIpMemoryStore(
|
||||
new IIpMemoryStoreCallbacks.Stub() {
|
||||
@Override
|
||||
public void onIpMemoryStoreFetched(@NonNull final IIpMemoryStore memoryStore) {
|
||||
mService.complete(memoryStore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInterfaceVersion() {
|
||||
return this.VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInterfaceHash() {
|
||||
return this.HASH;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* If the IpMemoryStore is ready, this function will run the request synchronously.
|
||||
* Otherwise, it will enqueue the requests for execution immediately after the
|
||||
* service becomes ready. The requests are guaranteed to be executed in the order
|
||||
* they are sumbitted.
|
||||
*/
|
||||
@Override
|
||||
protected void runWhenServiceReady(Consumer<IIpMemoryStore> cb) throws ExecutionException {
|
||||
mTailNode.getAndUpdate(future -> future.handle((store, exception) -> {
|
||||
if (exception != null) {
|
||||
// this should never happens since we also catch the exception below
|
||||
Log.wtf(TAG, "Error fetching IpMemoryStore", exception);
|
||||
return store;
|
||||
}
|
||||
|
||||
try {
|
||||
cb.accept(store);
|
||||
} catch (Exception e) {
|
||||
Log.wtf(TAG, "Exception occured: " + e.getMessage());
|
||||
}
|
||||
return store;
|
||||
}));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected ModuleNetworkStackClient getModuleNetworkStackClient(Context context) {
|
||||
return ModuleNetworkStackClient.getInstance(context);
|
||||
}
|
||||
|
||||
/** Gets an instance of the memory store */
|
||||
@NonNull
|
||||
public static IpMemoryStore getMemoryStore(final Context context) {
|
||||
return new IpMemoryStore(context);
|
||||
}
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net;
|
||||
|
||||
import android.annotation.Hide;
|
||||
import android.annotation.NonNull;
|
||||
import android.os.Binder;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* A convenience wrapper for INetworkMonitor.
|
||||
*
|
||||
* Wraps INetworkMonitor calls, making them a bit more friendly to use. Currently handles:
|
||||
* - Clearing calling identity
|
||||
* - Ignoring RemoteExceptions
|
||||
* - Converting to stable parcelables
|
||||
*
|
||||
* By design, all methods on INetworkMonitor are asynchronous oneway IPCs and are thus void. All the
|
||||
* wrapper methods in this class return a boolean that callers can use to determine whether
|
||||
* RemoteException was thrown.
|
||||
*/
|
||||
@Hide
|
||||
public class NetworkMonitorManager {
|
||||
|
||||
@NonNull private final INetworkMonitor mNetworkMonitor;
|
||||
@NonNull private final String mTag;
|
||||
|
||||
public NetworkMonitorManager(@NonNull INetworkMonitor networkMonitorManager,
|
||||
@NonNull String tag) {
|
||||
mNetworkMonitor = networkMonitorManager;
|
||||
mTag = tag;
|
||||
}
|
||||
|
||||
public NetworkMonitorManager(@NonNull INetworkMonitor networkMonitorManager) {
|
||||
this(networkMonitorManager, NetworkMonitorManager.class.getSimpleName());
|
||||
}
|
||||
|
||||
private void log(String s, Throwable e) {
|
||||
Log.e(mTag, s, e);
|
||||
}
|
||||
|
||||
// CHECKSTYLE:OFF Generated code
|
||||
|
||||
public boolean start() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.start();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in start", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean launchCaptivePortalApp() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.launchCaptivePortalApp();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in launchCaptivePortalApp", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyCaptivePortalAppFinished(int response) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyCaptivePortalAppFinished(response);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyCaptivePortalAppFinished", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setAcceptPartialConnectivity() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.setAcceptPartialConnectivity();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in setAcceptPartialConnectivity", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean forceReevaluation(int uid) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.forceReevaluation(uid);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in forceReevaluation", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyPrivateDnsChanged(PrivateDnsConfigParcel config) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyPrivateDnsChanged(config);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyPrivateDnsChanged", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyDnsResponse(int returnCode) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyDnsResponse(returnCode);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyDnsResponse", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyNetworkConnected(LinkProperties lp, NetworkCapabilities nc) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyNetworkConnected(lp, nc);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyNetworkConnected", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyNetworkDisconnected() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyNetworkDisconnected();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyNetworkDisconnected", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyLinkPropertiesChanged(LinkProperties lp) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyLinkPropertiesChanged(lp);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyLinkPropertiesChanged", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean notifyNetworkCapabilitiesChanged(NetworkCapabilities nc) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mNetworkMonitor.notifyNetworkCapabilitiesChanged(nc);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error in notifyNetworkCapabilitiesChanged", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
// CHECKSTYLE:ON Generated code
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.ip;
|
||||
|
||||
import android.net.DhcpResultsParcelable;
|
||||
import android.net.Layer2PacketParcelable;
|
||||
import android.net.LinkProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Callbacks for handling IpClient events.
|
||||
*
|
||||
* This is a convenience class to allow clients not to override all methods of IIpClientCallbacks,
|
||||
* and avoid unparceling arguments.
|
||||
* These methods are called asynchronously on a Binder thread, as IpClient lives in a different
|
||||
* process.
|
||||
* @hide
|
||||
*/
|
||||
public class IpClientCallbacks {
|
||||
|
||||
/**
|
||||
* Callback called upon IpClient creation.
|
||||
*
|
||||
* @param ipClient The Binder token to communicate with IpClient.
|
||||
*/
|
||||
public void onIpClientCreated(IIpClient ipClient) {}
|
||||
|
||||
/**
|
||||
* Callback called prior to DHCP discovery/renewal.
|
||||
*
|
||||
* <p>In order to receive onPreDhcpAction(), call #withPreDhcpAction() when constructing a
|
||||
* ProvisioningConfiguration.
|
||||
*
|
||||
* <p>Implementations of onPreDhcpAction() must call IpClient#completedPreDhcpAction() to
|
||||
* indicate that DHCP is clear to proceed.
|
||||
*/
|
||||
public void onPreDhcpAction() {}
|
||||
|
||||
/**
|
||||
* Callback called after DHCP discovery/renewal.
|
||||
*/
|
||||
public void onPostDhcpAction() {}
|
||||
|
||||
/**
|
||||
* Callback called when new DHCP results are available.
|
||||
*
|
||||
* <p>This is purely advisory and not an indication of provisioning success or failure. This is
|
||||
* only here for callers that want to expose DHCPv4 results to other APIs
|
||||
* (e.g., WifiInfo#setInetAddress).
|
||||
*
|
||||
* <p>DHCPv4 or static IPv4 configuration failure or success can be determined by whether or not
|
||||
* the passed-in DhcpResults object is null.
|
||||
*/
|
||||
public void onNewDhcpResults(DhcpResultsParcelable dhcpResults) {
|
||||
// In general callbacks would not use a parcelable directly (DhcpResultsParcelable), and
|
||||
// would use a wrapper instead, because of the lack of safety of stable parcelables. But
|
||||
// there are already two classes in the tree for DHCP information: DhcpInfo and DhcpResults,
|
||||
// and neither of them exposes an appropriate API (they are bags of mutable fields and can't
|
||||
// be changed because they are public API and @UnsupportedAppUsage, being no better than the
|
||||
// stable parcelable). Adding a third class would cost more than the gain considering that
|
||||
// the only client of this callback is WiFi, which will end up converting the results to
|
||||
// DhcpInfo anyway.
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates that provisioning was successful.
|
||||
*/
|
||||
public void onProvisioningSuccess(LinkProperties newLp) {}
|
||||
|
||||
/**
|
||||
* Indicates that provisioning failed.
|
||||
*/
|
||||
public void onProvisioningFailure(LinkProperties newLp) {}
|
||||
|
||||
/**
|
||||
* Invoked on LinkProperties changes.
|
||||
*/
|
||||
public void onLinkPropertiesChange(LinkProperties newLp) {}
|
||||
|
||||
/**Called when the internal IpReachabilityMonitor (if enabled) has
|
||||
* detected the loss of a critical number of required neighbors.
|
||||
*/
|
||||
public void onReachabilityLost(String logMsg) {}
|
||||
|
||||
/**
|
||||
* Called when the IpClient state machine terminates.
|
||||
*/
|
||||
public void onQuit() {}
|
||||
|
||||
/**
|
||||
* Called to indicate that a new APF program must be installed to filter incoming packets.
|
||||
*/
|
||||
public void installPacketFilter(byte[] filter) {}
|
||||
|
||||
/**
|
||||
* Called to indicate that the APF Program & data buffer must be read asynchronously from the
|
||||
* wifi driver.
|
||||
*
|
||||
* <p>Due to Wifi HAL limitations, the current implementation only supports dumping the entire
|
||||
* buffer. In response to this request, the driver returns the data buffer asynchronously
|
||||
* by sending an IpClient#EVENT_READ_PACKET_FILTER_COMPLETE message.
|
||||
*/
|
||||
public void startReadPacketFilter() {}
|
||||
|
||||
/**
|
||||
* If multicast filtering cannot be accomplished with APF, this function will be called to
|
||||
* actuate multicast filtering using another means.
|
||||
*/
|
||||
public void setFallbackMulticastFilter(boolean enabled) {}
|
||||
|
||||
/**
|
||||
* Enabled/disable Neighbor Discover offload functionality. This is called, for example,
|
||||
* whenever 464xlat is being started or stopped.
|
||||
*/
|
||||
public void setNeighborDiscoveryOffload(boolean enable) {}
|
||||
|
||||
/**
|
||||
* Invoked on starting preconnection process.
|
||||
*/
|
||||
public void onPreconnectionStart(List<Layer2PacketParcelable> packets) {}
|
||||
}
|
||||
@@ -1,326 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.ip;
|
||||
|
||||
import android.annotation.Hide;
|
||||
import android.annotation.NonNull;
|
||||
import android.net.NattKeepalivePacketData;
|
||||
import android.net.ProxyInfo;
|
||||
import android.net.TcpKeepalivePacketData;
|
||||
import android.net.TcpKeepalivePacketDataParcelable;
|
||||
import android.net.shared.Layer2Information;
|
||||
import android.net.shared.ProvisioningConfiguration;
|
||||
import android.net.util.KeepalivePacketDataUtil;
|
||||
import android.os.Binder;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* A convenience wrapper for IpClient.
|
||||
*
|
||||
* Wraps IIpClient calls, making them a bit more friendly to use. Currently handles:
|
||||
* - Clearing calling identity
|
||||
* - Ignoring RemoteExceptions
|
||||
* - Converting to stable parcelables
|
||||
*
|
||||
* By design, all methods on IIpClient are asynchronous oneway IPCs and are thus void. All the
|
||||
* wrapper methods in this class return a boolean that callers can use to determine whether
|
||||
* RemoteException was thrown.
|
||||
*/
|
||||
@Hide
|
||||
public class IpClientManager {
|
||||
@NonNull private final IIpClient mIpClient;
|
||||
@NonNull private final String mTag;
|
||||
|
||||
public IpClientManager(@NonNull IIpClient ipClient, @NonNull String tag) {
|
||||
mIpClient = ipClient;
|
||||
mTag = tag;
|
||||
}
|
||||
|
||||
public IpClientManager(@NonNull IIpClient ipClient) {
|
||||
this(ipClient, IpClientManager.class.getSimpleName());
|
||||
}
|
||||
|
||||
private void log(String s, Throwable e) {
|
||||
Log.e(mTag, s, e);
|
||||
}
|
||||
|
||||
/**
|
||||
* For clients using {@link ProvisioningConfiguration.Builder#withPreDhcpAction()}, must be
|
||||
* called after {@link IIpClientCallbacks#onPreDhcpAction} to indicate that DHCP is clear to
|
||||
* proceed.
|
||||
*/
|
||||
public boolean completedPreDhcpAction() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.completedPreDhcpAction();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error completing PreDhcpAction", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm the provisioning configuration.
|
||||
*/
|
||||
public boolean confirmConfiguration() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.confirmConfiguration();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error confirming IpClient configuration", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that packet filter read is complete.
|
||||
*/
|
||||
public boolean readPacketFilterComplete(byte[] data) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.readPacketFilterComplete(data);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error notifying IpClient of packet filter read", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shut down this IpClient instance altogether.
|
||||
*/
|
||||
public boolean shutdown() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.shutdown();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error shutting down IpClient", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start provisioning with the provided parameters.
|
||||
*/
|
||||
public boolean startProvisioning(ProvisioningConfiguration prov) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.startProvisioning(prov.toStableParcelable());
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error starting IpClient provisioning", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop this IpClient.
|
||||
*
|
||||
* <p>This does not shut down the StateMachine itself, which is handled by {@link #shutdown()}.
|
||||
*/
|
||||
public boolean stop() {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.stop();
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error stopping IpClient", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the TCP buffer sizes to use.
|
||||
*
|
||||
* This may be called, repeatedly, at any time before or after a call to
|
||||
* #startProvisioning(). The setting is cleared upon calling #stop().
|
||||
*/
|
||||
public boolean setTcpBufferSizes(String tcpBufferSizes) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.setTcpBufferSizes(tcpBufferSizes);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error setting IpClient TCP buffer sizes", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the HTTP Proxy configuration to use.
|
||||
*
|
||||
* This may be called, repeatedly, at any time before or after a call to
|
||||
* #startProvisioning(). The setting is cleared upon calling #stop().
|
||||
*/
|
||||
public boolean setHttpProxy(ProxyInfo proxyInfo) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.setHttpProxy(proxyInfo);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error setting IpClient proxy", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable the multicast filter. Attempts to use APF to accomplish the filtering,
|
||||
* if not, Callback.setFallbackMulticastFilter() is called.
|
||||
*/
|
||||
public boolean setMulticastFilter(boolean enabled) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.setMulticastFilter(enabled);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error setting multicast filter", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a TCP keepalive packet filter before setting up keepalive offload.
|
||||
*/
|
||||
public boolean addKeepalivePacketFilter(int slot, TcpKeepalivePacketData pkt) {
|
||||
return addKeepalivePacketFilter(slot, KeepalivePacketDataUtil.toStableParcelable(pkt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a TCP keepalive packet filter before setting up keepalive offload.
|
||||
* @deprecated This method is for use on pre-S platforms where TcpKeepalivePacketData is not
|
||||
* system API. On newer platforms use
|
||||
* addKeepalivePacketFilter(int, TcpKeepalivePacketData) instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean addKeepalivePacketFilter(int slot, TcpKeepalivePacketDataParcelable pkt) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.addKeepalivePacketFilter(slot, pkt);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error adding Keepalive Packet Filter ", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a NAT-T keepalive packet filter before setting up keepalive offload.
|
||||
*/
|
||||
public boolean addKeepalivePacketFilter(int slot, NattKeepalivePacketData pkt) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.addNattKeepalivePacketFilter(
|
||||
slot, KeepalivePacketDataUtil.toStableParcelable(pkt));
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error adding NAT-T Keepalive Packet Filter ", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a keepalive packet filter after stopping keepalive offload.
|
||||
*/
|
||||
public boolean removeKeepalivePacketFilter(int slot) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.removeKeepalivePacketFilter(slot);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error removing Keepalive Packet Filter ", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the L2 key and group hint for storing info into the memory store.
|
||||
*/
|
||||
public boolean setL2KeyAndGroupHint(String l2Key, String groupHint) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.setL2KeyAndGroupHint(l2Key, groupHint);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Failed setL2KeyAndGroupHint", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify IpClient that preconnection is complete and that the link is ready for use.
|
||||
* The success parameter indicates whether the packets passed in by 'onPreconnectionStart'
|
||||
* were successfully sent to the network or not.
|
||||
*/
|
||||
public boolean notifyPreconnectionComplete(boolean success) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.notifyPreconnectionComplete(success);
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error notifying IpClient Preconnection completed", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the bssid, L2 key and group hint layer2 information.
|
||||
*/
|
||||
public boolean updateLayer2Information(Layer2Information info) {
|
||||
final long token = Binder.clearCallingIdentity();
|
||||
try {
|
||||
mIpClient.updateLayer2Information(info.toStableParcelable());
|
||||
return true;
|
||||
} catch (RemoteException e) {
|
||||
log("Error updating layer2 information", e);
|
||||
return false;
|
||||
} finally {
|
||||
Binder.restoreCallingIdentity(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.ip;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.DhcpResultsParcelable;
|
||||
import android.net.Layer2PacketParcelable;
|
||||
import android.net.LinkProperties;
|
||||
import android.net.networkstack.ModuleNetworkStackClient;
|
||||
import android.os.ConditionVariable;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Utilities and wrappers to simplify communication with IpClient, which lives in the NetworkStack
|
||||
* process.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public class IpClientUtil {
|
||||
// TODO: remove with its callers
|
||||
public static final String DUMP_ARG = "ipclient";
|
||||
|
||||
/**
|
||||
* Subclass of {@link IpClientCallbacks} allowing clients to block until provisioning is
|
||||
* complete with {@link WaitForProvisioningCallbacks#waitForProvisioning()}.
|
||||
*/
|
||||
public static class WaitForProvisioningCallbacks extends IpClientCallbacks {
|
||||
private final ConditionVariable mCV = new ConditionVariable();
|
||||
private LinkProperties mCallbackLinkProperties;
|
||||
|
||||
/**
|
||||
* Block until either {@link #onProvisioningSuccess(LinkProperties)} or
|
||||
* {@link #onProvisioningFailure(LinkProperties)} is called.
|
||||
*/
|
||||
public LinkProperties waitForProvisioning() {
|
||||
mCV.block();
|
||||
return mCallbackLinkProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProvisioningSuccess(LinkProperties newLp) {
|
||||
mCallbackLinkProperties = newLp;
|
||||
mCV.open();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProvisioningFailure(LinkProperties newLp) {
|
||||
mCallbackLinkProperties = null;
|
||||
mCV.open();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IpClient.
|
||||
*
|
||||
* <p>This is a convenience method to allow clients to use {@link IpClientCallbacks} instead of
|
||||
* {@link IIpClientCallbacks}.
|
||||
* @see {@link ModuleNetworkStackClient#makeIpClient(String, IIpClientCallbacks)}
|
||||
*/
|
||||
public static void makeIpClient(Context context, String ifName, IpClientCallbacks callback) {
|
||||
ModuleNetworkStackClient.getInstance(context)
|
||||
.makeIpClient(ifName, new IpClientCallbacksProxy(callback));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to relay calls from {@link IIpClientCallbacks} to {@link IpClientCallbacks}.
|
||||
*/
|
||||
private static class IpClientCallbacksProxy extends IIpClientCallbacks.Stub {
|
||||
protected final IpClientCallbacks mCb;
|
||||
|
||||
/**
|
||||
* Create a new IpClientCallbacksProxy.
|
||||
*/
|
||||
public IpClientCallbacksProxy(IpClientCallbacks cb) {
|
||||
mCb = cb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIpClientCreated(IIpClient ipClient) {
|
||||
mCb.onIpClientCreated(ipClient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPreDhcpAction() {
|
||||
mCb.onPreDhcpAction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPostDhcpAction() {
|
||||
mCb.onPostDhcpAction();
|
||||
}
|
||||
|
||||
// This is purely advisory and not an indication of provisioning
|
||||
// success or failure. This is only here for callers that want to
|
||||
// expose DHCPv4 results to other APIs (e.g., WifiInfo#setInetAddress).
|
||||
// DHCPv4 or static IPv4 configuration failure or success can be
|
||||
// determined by whether or not the passed-in DhcpResults object is
|
||||
// null or not.
|
||||
@Override
|
||||
public void onNewDhcpResults(DhcpResultsParcelable dhcpResults) {
|
||||
mCb.onNewDhcpResults(dhcpResults);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProvisioningSuccess(LinkProperties newLp) {
|
||||
mCb.onProvisioningSuccess(newLp);
|
||||
}
|
||||
@Override
|
||||
public void onProvisioningFailure(LinkProperties newLp) {
|
||||
mCb.onProvisioningFailure(newLp);
|
||||
}
|
||||
|
||||
// Invoked on LinkProperties changes.
|
||||
@Override
|
||||
public void onLinkPropertiesChange(LinkProperties newLp) {
|
||||
mCb.onLinkPropertiesChange(newLp);
|
||||
}
|
||||
|
||||
// Called when the internal IpReachabilityMonitor (if enabled) has
|
||||
// detected the loss of a critical number of required neighbors.
|
||||
@Override
|
||||
public void onReachabilityLost(String logMsg) {
|
||||
mCb.onReachabilityLost(logMsg);
|
||||
}
|
||||
|
||||
// Called when the IpClient state machine terminates.
|
||||
@Override
|
||||
public void onQuit() {
|
||||
mCb.onQuit();
|
||||
}
|
||||
|
||||
// Install an APF program to filter incoming packets.
|
||||
@Override
|
||||
public void installPacketFilter(byte[] filter) {
|
||||
mCb.installPacketFilter(filter);
|
||||
}
|
||||
|
||||
// Asynchronously read back the APF program & data buffer from the wifi driver.
|
||||
// Due to Wifi HAL limitations, the current implementation only supports dumping the entire
|
||||
// buffer. In response to this request, the driver returns the data buffer asynchronously
|
||||
// by sending an IpClient#EVENT_READ_PACKET_FILTER_COMPLETE message.
|
||||
@Override
|
||||
public void startReadPacketFilter() {
|
||||
mCb.startReadPacketFilter();
|
||||
}
|
||||
|
||||
// If multicast filtering cannot be accomplished with APF, this function will be called to
|
||||
// actuate multicast filtering using another means.
|
||||
@Override
|
||||
public void setFallbackMulticastFilter(boolean enabled) {
|
||||
mCb.setFallbackMulticastFilter(enabled);
|
||||
}
|
||||
|
||||
// Enabled/disable Neighbor Discover offload functionality. This is
|
||||
// called, for example, whenever 464xlat is being started or stopped.
|
||||
@Override
|
||||
public void setNeighborDiscoveryOffload(boolean enable) {
|
||||
mCb.setNeighborDiscoveryOffload(enable);
|
||||
}
|
||||
|
||||
// Invoked on starting preconnection process.
|
||||
@Override
|
||||
public void onPreconnectionStart(List<Layer2PacketParcelable> packets) {
|
||||
mCb.onPreconnectionStart(packets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInterfaceVersion() {
|
||||
return this.VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInterfaceHash() {
|
||||
return this.HASH;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump logs for the specified IpClient.
|
||||
* TODO: remove callers and delete
|
||||
*/
|
||||
public static void dumpIpClient(
|
||||
IIpClient connector, FileDescriptor fd, PrintWriter pw, String[] args) {
|
||||
pw.println("IpClient logs have moved to dumpsys network_stack");
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.util;
|
||||
|
||||
import static android.net.shared.IpConfigurationParcelableUtil.unparcelAddress;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.net.DhcpResults;
|
||||
import android.net.DhcpResultsParcelable;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
|
||||
/**
|
||||
* Compatibility utility for code that still uses DhcpResults.
|
||||
*
|
||||
* TODO: remove this class when all usages of DhcpResults (including Wifi in AOSP) are removed.
|
||||
*/
|
||||
public class DhcpResultsCompatUtil {
|
||||
|
||||
/**
|
||||
* Convert a DhcpResultsParcelable to DhcpResults.
|
||||
*
|
||||
* contract {
|
||||
* returns(null) implies p == null
|
||||
* returnsNotNull() implies p != null
|
||||
* }
|
||||
*/
|
||||
@Nullable
|
||||
public static DhcpResults fromStableParcelable(@Nullable DhcpResultsParcelable p) {
|
||||
if (p == null) return null;
|
||||
final DhcpResults results = new DhcpResults(p.baseConfiguration);
|
||||
results.leaseDuration = p.leaseDuration;
|
||||
results.mtu = p.mtu;
|
||||
results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress);
|
||||
results.vendorInfo = p.vendorInfo;
|
||||
results.serverHostName = p.serverHostName;
|
||||
results.captivePortalApiUrl = p.captivePortalApiUrl;
|
||||
return results;
|
||||
}
|
||||
}
|
||||
@@ -1,223 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.util;
|
||||
|
||||
import static android.net.SocketKeepalive.ERROR_INVALID_IP_ADDRESS;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.net.InvalidPacketException;
|
||||
import android.net.KeepalivePacketData;
|
||||
import android.net.NattKeepalivePacketData;
|
||||
import android.net.NattKeepalivePacketDataParcelable;
|
||||
import android.net.TcpKeepalivePacketData;
|
||||
import android.net.TcpKeepalivePacketDataParcelable;
|
||||
import android.os.Build;
|
||||
import android.system.OsConstants;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.net.module.util.IpUtils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
/**
|
||||
* Utility class to convert to/from keepalive data parcelables.
|
||||
*
|
||||
* TODO: move to networkstack-client library when it is moved to frameworks/libs/net.
|
||||
* This class cannot go into other shared libraries as it depends on NetworkStack AIDLs.
|
||||
* @hide
|
||||
*/
|
||||
public final class KeepalivePacketDataUtil {
|
||||
private static final int IPV4_HEADER_LENGTH = 20;
|
||||
private static final int IPV6_HEADER_LENGTH = 40;
|
||||
private static final int TCP_HEADER_LENGTH = 20;
|
||||
|
||||
private static final String TAG = KeepalivePacketDataUtil.class.getSimpleName();
|
||||
|
||||
/**
|
||||
* Convert a NattKeepalivePacketData to a NattKeepalivePacketDataParcelable.
|
||||
*/
|
||||
@NonNull
|
||||
public static NattKeepalivePacketDataParcelable toStableParcelable(
|
||||
@NonNull NattKeepalivePacketData pkt) {
|
||||
final NattKeepalivePacketDataParcelable parcel = new NattKeepalivePacketDataParcelable();
|
||||
final InetAddress srcAddress = pkt.getSrcAddress();
|
||||
final InetAddress dstAddress = pkt.getDstAddress();
|
||||
parcel.srcAddress = srcAddress.getAddress();
|
||||
parcel.srcPort = pkt.getSrcPort();
|
||||
parcel.dstAddress = dstAddress.getAddress();
|
||||
parcel.dstPort = pkt.getDstPort();
|
||||
return parcel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a TcpKeepalivePacketData to a TcpKeepalivePacketDataParcelable.
|
||||
*/
|
||||
@NonNull
|
||||
public static TcpKeepalivePacketDataParcelable toStableParcelable(
|
||||
@NonNull TcpKeepalivePacketData pkt) {
|
||||
final TcpKeepalivePacketDataParcelable parcel = new TcpKeepalivePacketDataParcelable();
|
||||
final InetAddress srcAddress = pkt.getSrcAddress();
|
||||
final InetAddress dstAddress = pkt.getDstAddress();
|
||||
parcel.srcAddress = srcAddress.getAddress();
|
||||
parcel.srcPort = pkt.getSrcPort();
|
||||
parcel.dstAddress = dstAddress.getAddress();
|
||||
parcel.dstPort = pkt.getDstPort();
|
||||
parcel.seq = pkt.getTcpSeq();
|
||||
parcel.ack = pkt.getTcpAck();
|
||||
parcel.rcvWnd = pkt.getTcpWindow();
|
||||
parcel.rcvWndScale = pkt.getTcpWindowScale();
|
||||
parcel.tos = pkt.getIpTos();
|
||||
parcel.ttl = pkt.getIpTtl();
|
||||
return parcel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create tcp keepalive packet structure.
|
||||
* @hide
|
||||
*/
|
||||
public static TcpKeepalivePacketData fromStableParcelable(
|
||||
TcpKeepalivePacketDataParcelable tcpDetails) throws InvalidPacketException {
|
||||
final byte[] packet;
|
||||
try {
|
||||
if ((tcpDetails.srcAddress != null) && (tcpDetails.dstAddress != null)
|
||||
&& (tcpDetails.srcAddress.length == 4 /* V4 IP length */)
|
||||
&& (tcpDetails.dstAddress.length == 4 /* V4 IP length */)) {
|
||||
packet = buildV4Packet(tcpDetails);
|
||||
} else {
|
||||
// TODO: support ipv6
|
||||
throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
|
||||
}
|
||||
return new TcpKeepalivePacketData(
|
||||
InetAddress.getByAddress(tcpDetails.srcAddress),
|
||||
tcpDetails.srcPort,
|
||||
InetAddress.getByAddress(tcpDetails.dstAddress),
|
||||
tcpDetails.dstPort,
|
||||
packet,
|
||||
tcpDetails.seq, tcpDetails.ack, tcpDetails.rcvWnd, tcpDetails.rcvWndScale,
|
||||
tcpDetails.tos, tcpDetails.ttl);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Build ipv4 tcp keepalive packet, not including the link-layer header.
|
||||
*/
|
||||
// TODO : if this code is ever moved to the network stack, factorize constants with the ones
|
||||
// over there.
|
||||
private static byte[] buildV4Packet(TcpKeepalivePacketDataParcelable tcpDetails) {
|
||||
final int length = IPV4_HEADER_LENGTH + TCP_HEADER_LENGTH;
|
||||
ByteBuffer buf = ByteBuffer.allocate(length);
|
||||
buf.order(ByteOrder.BIG_ENDIAN);
|
||||
buf.put((byte) 0x45); // IP version and IHL
|
||||
buf.put((byte) tcpDetails.tos); // TOS
|
||||
buf.putShort((short) length);
|
||||
buf.putInt(0x00004000); // ID, flags=DF, offset
|
||||
buf.put((byte) tcpDetails.ttl); // TTL
|
||||
buf.put((byte) OsConstants.IPPROTO_TCP);
|
||||
final int ipChecksumOffset = buf.position();
|
||||
buf.putShort((short) 0); // IP checksum
|
||||
buf.put(tcpDetails.srcAddress);
|
||||
buf.put(tcpDetails.dstAddress);
|
||||
buf.putShort((short) tcpDetails.srcPort);
|
||||
buf.putShort((short) tcpDetails.dstPort);
|
||||
buf.putInt(tcpDetails.seq); // Sequence Number
|
||||
buf.putInt(tcpDetails.ack); // ACK
|
||||
buf.putShort((short) 0x5010); // TCP length=5, flags=ACK
|
||||
buf.putShort((short) (tcpDetails.rcvWnd >> tcpDetails.rcvWndScale)); // Window size
|
||||
final int tcpChecksumOffset = buf.position();
|
||||
buf.putShort((short) 0); // TCP checksum
|
||||
// URG is not set therefore the urgent pointer is zero.
|
||||
buf.putShort((short) 0); // Urgent pointer
|
||||
|
||||
buf.putShort(ipChecksumOffset, com.android.net.module.util.IpUtils.ipChecksum(buf, 0));
|
||||
buf.putShort(tcpChecksumOffset, IpUtils.tcpChecksum(
|
||||
buf, 0, IPV4_HEADER_LENGTH, TCP_HEADER_LENGTH));
|
||||
|
||||
return buf.array();
|
||||
}
|
||||
|
||||
// TODO: add buildV6Packet.
|
||||
|
||||
/**
|
||||
* Get a {@link TcpKeepalivePacketDataParcelable} from {@link KeepalivePacketData}, if the
|
||||
* generic class actually contains TCP keepalive data.
|
||||
*
|
||||
* @deprecated This method is used on R platforms where android.net.TcpKeepalivePacketData was
|
||||
* not yet system API. Newer platforms should use android.net.TcpKeepalivePacketData directly.
|
||||
*
|
||||
* @param data A {@link KeepalivePacketData} that may contain TCP keepalive data.
|
||||
* @return A parcelable containing TCP keepalive data, or null if the input data does not
|
||||
* contain TCP keepalive data.
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("AndroidFrameworkCompatChange") // API version check used to Log.wtf
|
||||
@Nullable
|
||||
public static TcpKeepalivePacketDataParcelable parseTcpKeepalivePacketData(
|
||||
@Nullable KeepalivePacketData data) {
|
||||
if (data == null) return null;
|
||||
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
|
||||
Log.wtf(TAG, "parseTcpKeepalivePacketData should not be used after R, use "
|
||||
+ "TcpKeepalivePacketData instead.");
|
||||
}
|
||||
|
||||
// Reconstruct TcpKeepalivePacketData from the packet contained in KeepalivePacketData
|
||||
final ByteBuffer buffer = ByteBuffer.wrap(data.getPacket());
|
||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
// Most of the fields are accessible from the KeepalivePacketData superclass: instead of
|
||||
// using Struct to parse everything, just extract the extra fields necessary for
|
||||
// TcpKeepalivePacketData.
|
||||
final int tcpSeq;
|
||||
final int tcpAck;
|
||||
final int wndSize;
|
||||
final int ipTos;
|
||||
final int ttl;
|
||||
try {
|
||||
// This only support IPv4, because TcpKeepalivePacketData only supports IPv4 for R and
|
||||
// below, and this method should not be used on newer platforms.
|
||||
tcpSeq = buffer.getInt(IPV4_HEADER_LENGTH + 4);
|
||||
tcpAck = buffer.getInt(IPV4_HEADER_LENGTH + 8);
|
||||
wndSize = buffer.getShort(IPV4_HEADER_LENGTH + 14);
|
||||
ipTos = buffer.get(1);
|
||||
ttl = buffer.get(8);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final TcpKeepalivePacketDataParcelable p = new TcpKeepalivePacketDataParcelable();
|
||||
p.srcAddress = data.getSrcAddress().getAddress();
|
||||
p.srcPort = data.getSrcPort();
|
||||
p.dstAddress = data.getDstAddress().getAddress();
|
||||
p.dstPort = data.getDstPort();
|
||||
p.seq = tcpSeq;
|
||||
p.ack = tcpAck;
|
||||
// TcpKeepalivePacketData could actually use non-zero wndScale, but this does not affect
|
||||
// actual functionality as generated packets will be the same (no wndScale option added)
|
||||
p.rcvWnd = wndSize;
|
||||
p.rcvWndScale = 0;
|
||||
p.tos = ipTos;
|
||||
p.ttl = ttl;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user