Merge "Remove last NetworkStack usage of hidden APIs" am: 16ec5d038c
am: 4845df3ced
Change-Id: I488fc028b8fd2aa5a09464082c65a4889a8a6b62
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package android.net;
|
||||
|
||||
import android.annotation.UnsupportedAppUsage;
|
||||
import android.net.shared.InetAddressUtils;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
@@ -73,19 +74,21 @@ public final class DhcpResults implements Parcelable {
|
||||
public StaticIpConfiguration toStaticIpConfiguration() {
|
||||
final StaticIpConfiguration s = new StaticIpConfiguration();
|
||||
// All these except dnsServers are immutable, so no need to make copies.
|
||||
s.ipAddress = ipAddress;
|
||||
s.gateway = gateway;
|
||||
s.dnsServers.addAll(dnsServers);
|
||||
s.domains = domains;
|
||||
s.setIpAddress(ipAddress);
|
||||
s.setGateway(gateway);
|
||||
for (InetAddress addr : dnsServers) {
|
||||
s.addDnsServer(addr);
|
||||
}
|
||||
s.setDomains(domains);
|
||||
return s;
|
||||
}
|
||||
|
||||
public DhcpResults(StaticIpConfiguration source) {
|
||||
if (source != null) {
|
||||
ipAddress = source.ipAddress;
|
||||
gateway = source.gateway;
|
||||
dnsServers.addAll(source.dnsServers);
|
||||
domains = source.domains;
|
||||
ipAddress = source.getIpAddress();
|
||||
gateway = source.getGateway();
|
||||
dnsServers.addAll(source.getDnsServers());
|
||||
domains = source.getDomains();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +180,7 @@ public final class DhcpResults implements Parcelable {
|
||||
toStaticIpConfiguration().writeToParcel(dest, flags);
|
||||
dest.writeInt(leaseDuration);
|
||||
dest.writeInt(mtu);
|
||||
NetworkUtils.parcelInetAddress(dest, serverAddress, flags);
|
||||
InetAddressUtils.parcelInetAddress(dest, serverAddress, flags);
|
||||
dest.writeString(vendorInfo);
|
||||
}
|
||||
|
||||
@@ -191,7 +194,7 @@ public final class DhcpResults implements Parcelable {
|
||||
final DhcpResults dhcpResults = new DhcpResults(s);
|
||||
dhcpResults.leaseDuration = in.readInt();
|
||||
dhcpResults.mtu = in.readInt();
|
||||
dhcpResults.serverAddress = (Inet4Address) NetworkUtils.unparcelInetAddress(in);
|
||||
dhcpResults.serverAddress = (Inet4Address) InetAddressUtils.unparcelInetAddress(in);
|
||||
dhcpResults.vendorInfo = in.readString();
|
||||
return dhcpResults;
|
||||
}
|
||||
@@ -200,7 +203,7 @@ public final class DhcpResults implements Parcelable {
|
||||
// Not part of the superclass because they're only used by the JNI iterface to the DHCP daemon.
|
||||
public boolean setIpAddress(String addrString, int prefixLength) {
|
||||
try {
|
||||
Inet4Address addr = (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
|
||||
Inet4Address addr = (Inet4Address) InetAddresses.parseNumericAddress(addrString);
|
||||
ipAddress = new LinkAddress(addr, prefixLength);
|
||||
} catch (IllegalArgumentException|ClassCastException e) {
|
||||
Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength);
|
||||
@@ -211,7 +214,7 @@ public final class DhcpResults implements Parcelable {
|
||||
|
||||
public boolean setGateway(String addrString) {
|
||||
try {
|
||||
gateway = NetworkUtils.numericToInetAddress(addrString);
|
||||
gateway = InetAddresses.parseNumericAddress(addrString);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.e(TAG, "setGateway failed with addrString " + addrString);
|
||||
return true;
|
||||
@@ -222,7 +225,7 @@ public final class DhcpResults implements Parcelable {
|
||||
public boolean addDns(String addrString) {
|
||||
if (TextUtils.isEmpty(addrString) == false) {
|
||||
try {
|
||||
dnsServers.add(NetworkUtils.numericToInetAddress(addrString));
|
||||
dnsServers.add(InetAddresses.parseNumericAddress(addrString));
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.e(TAG, "addDns failed with addrString " + addrString);
|
||||
return true;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package android.net;
|
||||
|
||||
import android.net.INetworkMonitorCallbacks;
|
||||
import android.net.NetworkParcelable;
|
||||
import android.net.dhcp.DhcpServingParamsParcel;
|
||||
import android.net.dhcp.IDhcpServerCallbacks;
|
||||
import android.net.ip.IIpClientCallbacks;
|
||||
@@ -24,6 +25,7 @@ import android.net.ip.IIpClientCallbacks;
|
||||
oneway interface INetworkStackConnector {
|
||||
void makeDhcpServer(in String ifName, in DhcpServingParamsParcel params,
|
||||
in IDhcpServerCallbacks cb);
|
||||
void makeNetworkMonitor(int netId, String name, in INetworkMonitorCallbacks cb);
|
||||
void makeNetworkMonitor(in NetworkParcelable network, String name,
|
||||
in INetworkMonitorCallbacks cb);
|
||||
void makeIpClient(in String ifName, in IIpClientCallbacks callbacks);
|
||||
}
|
||||
@@ -104,10 +104,11 @@ public class NetworkStack {
|
||||
*
|
||||
* <p>The INetworkMonitor will be returned asynchronously through the provided callbacks.
|
||||
*/
|
||||
public void makeNetworkMonitor(Network network, String name, INetworkMonitorCallbacks cb) {
|
||||
public void makeNetworkMonitor(
|
||||
NetworkParcelable network, String name, INetworkMonitorCallbacks cb) {
|
||||
requestConnector(connector -> {
|
||||
try {
|
||||
connector.makeNetworkMonitor(network.netId, name, cb);
|
||||
connector.makeNetworkMonitor(network, name, cb);
|
||||
} catch (RemoteException e) {
|
||||
e.rethrowFromSystemServer();
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package android.net;
|
||||
import android.annotation.UnsupportedAppUsage;
|
||||
import android.net.shared.Inet4AddressUtils;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.system.ErrnoException;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
@@ -246,32 +245,6 @@ public class NetworkUtils {
|
||||
return InetAddress.parseNumericAddress(addrString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an InetAddress to a parcel. The address may be null. This is likely faster than
|
||||
* calling writeSerializable.
|
||||
*/
|
||||
protected static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) {
|
||||
byte[] addressArray = (address != null) ? address.getAddress() : null;
|
||||
parcel.writeByteArray(addressArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an InetAddress from a parcel. Returns null if the address that was written was null
|
||||
* or if the data is invalid.
|
||||
*/
|
||||
protected static InetAddress unparcelInetAddress(Parcel in) {
|
||||
byte[] addressArray = in.createByteArray();
|
||||
if (addressArray == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return InetAddress.getByAddress(addressArray);
|
||||
} catch (UnknownHostException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Masks a raw IP address byte array with the specified prefix length.
|
||||
*/
|
||||
|
||||
@@ -19,6 +19,7 @@ package android.net;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.TestApi;
|
||||
import android.annotation.UnsupportedAppUsage;
|
||||
import android.net.shared.InetAddressUtils;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
@@ -232,10 +233,10 @@ public final class StaticIpConfiguration implements Parcelable {
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeParcelable(ipAddress, flags);
|
||||
NetworkUtils.parcelInetAddress(dest, gateway, flags);
|
||||
InetAddressUtils.parcelInetAddress(dest, gateway, flags);
|
||||
dest.writeInt(dnsServers.size());
|
||||
for (InetAddress dnsServer : dnsServers) {
|
||||
NetworkUtils.parcelInetAddress(dest, dnsServer, flags);
|
||||
InetAddressUtils.parcelInetAddress(dest, dnsServer, flags);
|
||||
}
|
||||
dest.writeString(domains);
|
||||
}
|
||||
@@ -244,11 +245,11 @@ public final class StaticIpConfiguration implements Parcelable {
|
||||
public static StaticIpConfiguration readFromParcel(Parcel in) {
|
||||
final StaticIpConfiguration s = new StaticIpConfiguration();
|
||||
s.ipAddress = in.readParcelable(null);
|
||||
s.gateway = NetworkUtils.unparcelInetAddress(in);
|
||||
s.gateway = InetAddressUtils.unparcelInetAddress(in);
|
||||
s.dnsServers.clear();
|
||||
int size = in.readInt();
|
||||
for (int i = 0; i < size; i++) {
|
||||
s.dnsServers.add(NetworkUtils.unparcelInetAddress(in));
|
||||
s.dnsServers.add(InetAddressUtils.unparcelInetAddress(in));
|
||||
}
|
||||
s.domains = in.readString();
|
||||
return s;
|
||||
|
||||
@@ -31,10 +31,6 @@ import android.os.Parcelable;
|
||||
public final class DhcpClientEvent implements IpConnectivityLog.Event {
|
||||
|
||||
// Names for recording DhcpClient pseudo-state transitions.
|
||||
/** {@hide} Represents transitions from DhcpInitState to DhcpBoundState */
|
||||
public static final String INITIAL_BOUND = "InitialBoundState";
|
||||
/** {@hide} Represents transitions from and to DhcpBoundState via DhcpRenewingState */
|
||||
public static final String RENEWING_BOUND = "RenewingBoundState";
|
||||
|
||||
/** @hide */
|
||||
public final String msg;
|
||||
|
||||
@@ -21,15 +21,15 @@ import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.net.util.SocketUtils;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.MessageQueue;
|
||||
import android.system.ErrnoException;
|
||||
import android.system.OsConstants;
|
||||
|
||||
import libcore.io.IoUtils;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
@@ -81,7 +81,10 @@ public abstract class FdEventsReader<BufferType> {
|
||||
private long mPacketsReceived;
|
||||
|
||||
protected static void closeFd(FileDescriptor fd) {
|
||||
IoUtils.closeQuietly(fd);
|
||||
try {
|
||||
SocketUtils.closeSocket(fd);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
protected FdEventsReader(@NonNull Handler h, @NonNull BufferType buffer) {
|
||||
@@ -136,8 +139,8 @@ public abstract class FdEventsReader<BufferType> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses MUST create the listening socket here, including setting
|
||||
* all desired socket options, interface or address/port binding, etc.
|
||||
* Subclasses MUST create the listening socket here, including setting all desired socket
|
||||
* options, interface or address/port binding, etc. The socket MUST be created nonblocking.
|
||||
*/
|
||||
@Nullable
|
||||
protected abstract FileDescriptor createFd();
|
||||
@@ -181,10 +184,6 @@ public abstract class FdEventsReader<BufferType> {
|
||||
|
||||
try {
|
||||
mFd = createFd();
|
||||
if (mFd != null) {
|
||||
// Force the socket to be non-blocking.
|
||||
IoUtils.setBlocking(mFd, false);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logError("Failed to create socket: ", e);
|
||||
closeFd(mFd);
|
||||
|
||||
58
core/java/android/net/shared/InetAddressUtils.java
Normal file
58
core/java/android/net/shared/InetAddressUtils.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2012 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.shared;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* Collection of utilities to interact with {@link InetAddress}
|
||||
* @hide
|
||||
*/
|
||||
public class InetAddressUtils {
|
||||
|
||||
/**
|
||||
* Writes an InetAddress to a parcel. The address may be null. This is likely faster than
|
||||
* calling writeSerializable.
|
||||
* @hide
|
||||
*/
|
||||
public static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) {
|
||||
byte[] addressArray = (address != null) ? address.getAddress() : null;
|
||||
parcel.writeByteArray(addressArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an InetAddress from a parcel. Returns null if the address that was written was null
|
||||
* or if the data is invalid.
|
||||
* @hide
|
||||
*/
|
||||
public static InetAddress unparcelInetAddress(Parcel in) {
|
||||
byte[] addressArray = in.createByteArray();
|
||||
if (addressArray == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return InetAddress.getByAddress(addressArray);
|
||||
} catch (UnknownHostException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private InetAddressUtils() {}
|
||||
}
|
||||
Reference in New Issue
Block a user