Merge "Fix linter errors in IpServer and its dependencies"
This commit is contained in:
@@ -34,32 +34,53 @@ import java.util.ArrayList;
|
||||
* @hide
|
||||
*/
|
||||
public class TetheringDependencies {
|
||||
/**
|
||||
* Get a reference to the offload hardware interface to be used by tethering.
|
||||
*/
|
||||
public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) {
|
||||
return new OffloadHardwareInterface(h, log);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a reference to the UpstreamNetworkMonitor to be used by tethering.
|
||||
*/
|
||||
public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, StateMachine target,
|
||||
SharedLog log, int what) {
|
||||
return new UpstreamNetworkMonitor(ctx, target, log, what);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a reference to the IPv6TetheringCoordinator to be used by tethering.
|
||||
*/
|
||||
public IPv6TetheringCoordinator getIPv6TetheringCoordinator(
|
||||
ArrayList<IpServer> notifyList, SharedLog log) {
|
||||
return new IPv6TetheringCoordinator(notifyList, log);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dependencies to be used by IpServer.
|
||||
*/
|
||||
public IpServer.Dependencies getIpServerDependencies() {
|
||||
return new IpServer.Dependencies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether tethering is supported on the device.
|
||||
*/
|
||||
public boolean isTetheringSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the NetworkRequest that should be fulfilled by the default network.
|
||||
*/
|
||||
public NetworkRequest getDefaultNetworkRequest() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a reference to the EntitlementManager to be used by tethering.
|
||||
*/
|
||||
public EntitlementManager getEntitlementManager(Context ctx, SharedLog log,
|
||||
MockableSystemProperties systemProperties) {
|
||||
return new EntitlementManager(ctx, log, systemProperties);
|
||||
|
||||
@@ -58,6 +58,11 @@ public class DhcpLease {
|
||||
mHostname = hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the clientId associated with this lease, if any.
|
||||
*
|
||||
* <p>If the lease is not associated to a clientId, this returns null.
|
||||
*/
|
||||
@Nullable
|
||||
public byte[] getClientId() {
|
||||
if (mClientId == null) {
|
||||
@@ -97,6 +102,11 @@ public class DhcpLease {
|
||||
(hostname == null ? mHostname : hostname));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether this lease matches a client with the specified parameters.
|
||||
* @param clientId clientId of the client if any, or null otherwise.
|
||||
* @param hwAddr Hardware address of the client.
|
||||
*/
|
||||
public boolean matchesClient(@Nullable byte[] clientId, @NonNull MacAddress hwAddr) {
|
||||
if (mClientId != null) {
|
||||
return Arrays.equals(mClientId, clientId);
|
||||
@@ -110,7 +120,7 @@ public class DhcpLease {
|
||||
if (!(obj instanceof DhcpLease)) {
|
||||
return false;
|
||||
}
|
||||
final DhcpLease other = (DhcpLease)obj;
|
||||
final DhcpLease other = (DhcpLease) obj;
|
||||
return Arrays.equals(mClientId, other.mClientId)
|
||||
&& mHwAddr.equals(other.mHwAddr)
|
||||
&& mNetAddr.equals(other.mNetAddr)
|
||||
|
||||
@@ -29,8 +29,8 @@ import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.net.IpPrefix;
|
||||
import android.net.MacAddress;
|
||||
import android.net.util.SharedLog;
|
||||
import android.net.dhcp.DhcpServer.Clock;
|
||||
import android.net.util.SharedLog;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
@@ -117,7 +117,7 @@ class DhcpLeaseRepository {
|
||||
*/
|
||||
private final LinkedHashMap<Inet4Address, Long> mDeclinedAddrs = new LinkedHashMap<>();
|
||||
|
||||
public DhcpLeaseRepository(@NonNull IpPrefix prefix, @NonNull Set<Inet4Address> reservedAddrs,
|
||||
DhcpLeaseRepository(@NonNull IpPrefix prefix, @NonNull Set<Inet4Address> reservedAddrs,
|
||||
long leaseTimeMs, @NonNull SharedLog log, @NonNull Clock clock) {
|
||||
updateParams(prefix, reservedAddrs, leaseTimeMs);
|
||||
mLog = log;
|
||||
@@ -250,8 +250,8 @@ class DhcpLeaseRepository {
|
||||
// reqAddr null (RENEWING/REBINDING): client renewing its own lease for clientAddr.
|
||||
// reqAddr set with sid not set (INIT-REBOOT): client verifying configuration.
|
||||
// In both cases, throw if clientAddr or reqAddr does not match the known lease.
|
||||
throw new InvalidAddressException("Incorrect address for client in " +
|
||||
(reqAddr != null ? "INIT-REBOOT" : "RENEWING/REBINDING"));
|
||||
throw new InvalidAddressException("Incorrect address for client in "
|
||||
+ (reqAddr != null ? "INIT-REBOOT" : "RENEWING/REBINDING"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,32 +32,32 @@ import java.net.InetSocketAddress;
|
||||
*/
|
||||
abstract class DhcpPacketListener extends FdEventsReader<DhcpPacketListener.Payload> {
|
||||
static final class Payload {
|
||||
final byte[] bytes = new byte[DhcpPacket.MAX_LENGTH];
|
||||
Inet4Address srcAddr;
|
||||
int srcPort;
|
||||
protected final byte[] mBytes = new byte[DhcpPacket.MAX_LENGTH];
|
||||
protected Inet4Address mSrcAddr;
|
||||
protected int mSrcPort;
|
||||
}
|
||||
|
||||
public DhcpPacketListener(@NonNull Handler handler) {
|
||||
DhcpPacketListener(@NonNull Handler handler) {
|
||||
super(handler, new Payload());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int recvBufSize(@NonNull Payload buffer) {
|
||||
return buffer.bytes.length;
|
||||
return buffer.mBytes.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void handlePacket(@NonNull Payload recvbuf, int length) {
|
||||
if (recvbuf.srcAddr == null) {
|
||||
if (recvbuf.mSrcAddr == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final DhcpPacket packet = DhcpPacket.decodeFullPacket(recvbuf.bytes, length,
|
||||
final DhcpPacket packet = DhcpPacket.decodeFullPacket(recvbuf.mBytes, length,
|
||||
DhcpPacket.ENCAP_BOOTP);
|
||||
onReceive(packet, recvbuf.srcAddr, recvbuf.srcPort);
|
||||
onReceive(packet, recvbuf.mSrcAddr, recvbuf.mSrcPort);
|
||||
} catch (DhcpPacket.ParseException e) {
|
||||
logParseError(recvbuf.bytes, length, e);
|
||||
logParseError(recvbuf.mBytes, length, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +66,11 @@ abstract class DhcpPacketListener extends FdEventsReader<DhcpPacketListener.Payl
|
||||
throws Exception {
|
||||
final InetSocketAddress addr = new InetSocketAddress();
|
||||
final int read = Os.recvfrom(
|
||||
fd, packetBuffer.bytes, 0, packetBuffer.bytes.length, 0 /* flags */, addr);
|
||||
fd, packetBuffer.mBytes, 0, packetBuffer.mBytes.length, 0 /* flags */, addr);
|
||||
|
||||
// Buffers with null srcAddr will be dropped in handlePacket()
|
||||
packetBuffer.srcAddr = inet4AddrOrNull(addr);
|
||||
packetBuffer.srcPort = addr.getPort();
|
||||
packetBuffer.mSrcAddr = inet4AddrOrNull(addr);
|
||||
packetBuffer.mSrcPort = addr.getPort();
|
||||
return read;
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,13 @@ public class DhcpServer {
|
||||
@NonNull
|
||||
private DhcpServingParams mServingParams;
|
||||
|
||||
/**
|
||||
* Clock to be used by DhcpServer to track time for lease expiration.
|
||||
*
|
||||
* <p>The clock should track time as may be measured by clients obtaining a lease. It does not
|
||||
* need to be monotonous across restarts of the server as long as leases are cleared when the
|
||||
* server is stopped.
|
||||
*/
|
||||
public static class Clock {
|
||||
/**
|
||||
* @see SystemClock#elapsedRealtime()
|
||||
@@ -110,13 +117,43 @@ public class DhcpServer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dependencies for the DhcpServer. Useful to be mocked in tests.
|
||||
*/
|
||||
public interface Dependencies {
|
||||
/**
|
||||
* Send a packet to the specified datagram socket.
|
||||
*
|
||||
* @param fd File descriptor of the socket.
|
||||
* @param buffer Data to be sent.
|
||||
* @param dst Destination address of the packet.
|
||||
*/
|
||||
void sendPacket(@NonNull FileDescriptor fd, @NonNull ByteBuffer buffer,
|
||||
@NonNull InetAddress dst) throws ErrnoException, IOException;
|
||||
|
||||
/**
|
||||
* Create a DhcpLeaseRepository for the server.
|
||||
* @param servingParams Parameters used to serve DHCP requests.
|
||||
* @param log Log to be used by the repository.
|
||||
* @param clock Clock that the repository must use to track time.
|
||||
*/
|
||||
DhcpLeaseRepository makeLeaseRepository(@NonNull DhcpServingParams servingParams,
|
||||
@NonNull SharedLog log, @NonNull Clock clock);
|
||||
|
||||
/**
|
||||
* Create a packet listener that will send packets to be processed.
|
||||
*/
|
||||
DhcpPacketListener makePacketListener();
|
||||
|
||||
/**
|
||||
* Create a clock that the server will use to track time.
|
||||
*/
|
||||
Clock makeClock();
|
||||
|
||||
/**
|
||||
* Add an entry to the ARP cache table.
|
||||
* @param fd Datagram socket file descriptor that must use the new entry.
|
||||
*/
|
||||
void addArpEntry(@NonNull Inet4Address ipv4Addr, @NonNull MacAddress ethAddr,
|
||||
@NonNull String ifname, @NonNull FileDescriptor fd) throws IOException;
|
||||
}
|
||||
@@ -134,7 +171,7 @@ public class DhcpServer {
|
||||
return new DhcpLeaseRepository(
|
||||
DhcpServingParams.makeIpPrefix(servingParams.serverAddr),
|
||||
servingParams.excludedAddrs,
|
||||
servingParams.dhcpLeaseTimeSecs*1000, log.forSubComponent(REPO_TAG), clock);
|
||||
servingParams.dhcpLeaseTimeSecs * 1000, log.forSubComponent(REPO_TAG), clock);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -212,7 +249,7 @@ public class DhcpServer {
|
||||
}
|
||||
|
||||
private class ServerHandler extends Handler {
|
||||
public ServerHandler(@NonNull Looper looper) {
|
||||
ServerHandler(@NonNull Looper looper) {
|
||||
super(looper);
|
||||
}
|
||||
|
||||
@@ -496,22 +533,24 @@ public class DhcpServer {
|
||||
}
|
||||
|
||||
private class PacketListener extends DhcpPacketListener {
|
||||
public PacketListener() {
|
||||
PacketListener() {
|
||||
super(mHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReceive(DhcpPacket packet, Inet4Address srcAddr, int srcPort) {
|
||||
protected void onReceive(@NonNull DhcpPacket packet, @NonNull Inet4Address srcAddr,
|
||||
int srcPort) {
|
||||
processPacket(packet, srcPort);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void logError(String msg, Exception e) {
|
||||
protected void logError(@NonNull String msg, Exception e) {
|
||||
mLog.e("Error receiving packet: " + msg, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void logParseError(byte[] packet, int length, DhcpPacket.ParseException e) {
|
||||
protected void logParseError(@NonNull byte[] packet, int length,
|
||||
@NonNull DhcpPacket.ParseException e) {
|
||||
mLog.e("Error parsing packet", e);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,8 +35,8 @@ import java.util.StringJoiner;
|
||||
* @hide
|
||||
*/
|
||||
public class SharedLog {
|
||||
private final static int DEFAULT_MAX_RECORDS = 500;
|
||||
private final static String COMPONENT_DELIMITER = ".";
|
||||
private static final int DEFAULT_MAX_RECORDS = 500;
|
||||
private static final String COMPONENT_DELIMITER = ".";
|
||||
|
||||
private enum Category {
|
||||
NONE,
|
||||
@@ -69,6 +69,9 @@ public class SharedLog {
|
||||
mComponent = component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a SharedLog based on this log with an additional component prefix on each logged line.
|
||||
*/
|
||||
public SharedLog forSubComponent(String component) {
|
||||
if (!isRootLogInstance()) {
|
||||
component = mComponent + COMPONENT_DELIMITER + component;
|
||||
@@ -76,6 +79,11 @@ public class SharedLog {
|
||||
return new SharedLog(mLocalLog, mTag, component);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the contents of this log.
|
||||
*
|
||||
* <p>This method may be called on any thread.
|
||||
*/
|
||||
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
|
||||
mLocalLog.readOnlyLocalLog().dump(fd, writer, args);
|
||||
}
|
||||
@@ -84,10 +92,21 @@ public class SharedLog {
|
||||
// Methods that both log an entry and emit it to the system log.
|
||||
//////
|
||||
|
||||
/**
|
||||
* Log an error due to an exception. This does not include the exception stacktrace.
|
||||
*
|
||||
* <p>The log entry will be also added to the system log.
|
||||
* @see #e(String, Throwable)
|
||||
*/
|
||||
public void e(Exception e) {
|
||||
Log.e(mTag, record(Category.ERROR, e.toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an error message.
|
||||
*
|
||||
* <p>The log entry will be also added to the system log.
|
||||
*/
|
||||
public void e(String msg) {
|
||||
Log.e(mTag, record(Category.ERROR, msg));
|
||||
}
|
||||
@@ -96,7 +115,7 @@ public class SharedLog {
|
||||
* Log an error due to an exception, with the exception stacktrace if provided.
|
||||
*
|
||||
* <p>The error and exception message appear in the shared log, but the stacktrace is only
|
||||
* logged in general log output (logcat).
|
||||
* logged in general log output (logcat). The log entry will be also added to the system log.
|
||||
*/
|
||||
public void e(@NonNull String msg, @Nullable Throwable exception) {
|
||||
if (exception == null) {
|
||||
@@ -106,10 +125,20 @@ public class SharedLog {
|
||||
Log.e(mTag, record(Category.ERROR, msg + ": " + exception.getMessage()), exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an informational message.
|
||||
*
|
||||
* <p>The log entry will be also added to the system log.
|
||||
*/
|
||||
public void i(String msg) {
|
||||
Log.i(mTag, record(Category.NONE, msg));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a warning message.
|
||||
*
|
||||
* <p>The log entry will be also added to the system log.
|
||||
*/
|
||||
public void w(String msg) {
|
||||
Log.w(mTag, record(Category.WARN, msg));
|
||||
}
|
||||
@@ -118,14 +147,30 @@ public class SharedLog {
|
||||
// Methods that only log an entry (and do NOT emit to the system log).
|
||||
//////
|
||||
|
||||
/**
|
||||
* Log a general message to be only included in the in-memory log.
|
||||
*
|
||||
* <p>The log entry will *not* be added to the system log.
|
||||
*/
|
||||
public void log(String msg) {
|
||||
record(Category.NONE, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a general, formatted message to be only included in the in-memory log.
|
||||
*
|
||||
* <p>The log entry will *not* be added to the system log.
|
||||
* @see String#format(String, Object...)
|
||||
*/
|
||||
public void logf(String fmt, Object... args) {
|
||||
log(String.format(fmt, args));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message with MARK level.
|
||||
*
|
||||
* <p>The log entry will *not* be added to the system log.
|
||||
*/
|
||||
public void mark(String msg) {
|
||||
record(Category.MARK, msg);
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.net.IpPrefix;
|
||||
import android.net.MacAddress;
|
||||
import android.net.util.SharedLog;
|
||||
import android.net.dhcp.DhcpServer.Clock;
|
||||
import android.net.util.SharedLog;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
@@ -126,7 +126,7 @@ public class DhcpLeaseRepositoryTest {
|
||||
mRepo.updateParams(new IpPrefix(TEST_SERVER_ADDR, 28), TEST_EXCL_SET, TEST_LEASE_TIME_MS);
|
||||
|
||||
// /28 should have 16 addresses, 14 w/o the first/last, 11 w/o excluded addresses
|
||||
requestAddresses((byte)11);
|
||||
requestAddresses((byte) 11);
|
||||
|
||||
try {
|
||||
mRepo.getOffer(null, TEST_MAC_2,
|
||||
|
||||
@@ -16,6 +16,16 @@
|
||||
|
||||
package android.net.ip;
|
||||
|
||||
import static android.net.ConnectivityManager.TETHERING_BLUETOOTH;
|
||||
import static android.net.ConnectivityManager.TETHERING_USB;
|
||||
import static android.net.ConnectivityManager.TETHERING_WIFI;
|
||||
import static android.net.ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR;
|
||||
import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
|
||||
import static android.net.ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR;
|
||||
import static android.net.ip.IpServer.STATE_AVAILABLE;
|
||||
import static android.net.ip.IpServer.STATE_TETHERED;
|
||||
import static android.net.ip.IpServer.STATE_UNAVAILABLE;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -31,16 +41,6 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import static android.net.ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR;
|
||||
import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
|
||||
import static android.net.ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR;
|
||||
import static android.net.ConnectivityManager.TETHERING_BLUETOOTH;
|
||||
import static android.net.ConnectivityManager.TETHERING_USB;
|
||||
import static android.net.ConnectivityManager.TETHERING_WIFI;
|
||||
import static android.net.ip.IpServer.STATE_AVAILABLE;
|
||||
import static android.net.ip.IpServer.STATE_TETHERED;
|
||||
import static android.net.ip.IpServer.STATE_UNAVAILABLE;
|
||||
|
||||
import android.net.INetworkStatsService;
|
||||
import android.net.InterfaceConfiguration;
|
||||
import android.net.IpPrefix;
|
||||
@@ -60,8 +60,6 @@ import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -71,6 +69,8 @@ import org.mockito.InOrder;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class IpServerTest {
|
||||
|
||||
Reference in New Issue
Block a user