Skip to content
Snippets Groups Projects
Commit 74a4fa9b authored by markchien's avatar markchien
Browse files

[Tether03] Migrate IpServer into module

Add IpServer which is used to serve ip configuration, dhcp, dns proxy
and nat for downstream interface.

Bug: 136040414
Test: -build, flash, boot
      -atest TetheringTests
      -atest FrameworksNetTests

Change-Id: I23652ae0b9509abe7d38da96d523eb22ab00a343
parent 43e97e01
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,8 @@ java_defaults {
],
static_libs: [
"androidx.annotation_annotation",
"netd_aidl_interface-java",
"networkstack-aidl-interfaces-java",
"tethering-client",
],
manifest: "AndroidManifestBase.xml",
......@@ -68,5 +70,10 @@ filegroup {
name: "tethering-services-srcs",
srcs: [
"src/com/android/server/connectivity/tethering/TetheringConfiguration.java",
"src/android/net/dhcp/DhcpServerCallbacks.java",
"src/android/net/dhcp/DhcpServingParamsParcelExt.java",
"src/android/net/ip/IpServer.java",
"src/android/net/ip/RouterAdvertisementDaemon.java",
"src/android/net/util/InterfaceSet.java",
],
}
/*
* Copyright (C) 2018 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.dhcp;
/**
* Convenience wrapper around IDhcpServerCallbacks.Stub that implements getInterfaceVersion().
* @hide
*/
public abstract class DhcpServerCallbacks extends IDhcpServerCallbacks.Stub {
/**
* Get the version of the aidl interface implemented by the callbacks.
*/
@Override
public int getInterfaceVersion() {
return IDhcpServerCallbacks.VERSION;
}
}
/*
* Copyright (C) 2018 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.dhcp;
import static android.net.shared.Inet4AddressUtils.inet4AddressToIntHTH;
import android.annotation.NonNull;
import android.net.LinkAddress;
import com.google.android.collect.Sets;
import java.net.Inet4Address;
import java.util.Collection;
import java.util.Set;
/**
* Subclass of {@link DhcpServingParamsParcel} with additional utility methods for building.
*
* <p>This utility class does not check for validity of the parameters: invalid parameters are
* reported by the receiving module when unparceling the parcel.
*
* @see DhcpServingParams
* @hide
*/
public class DhcpServingParamsParcelExt extends DhcpServingParamsParcel {
public static final int MTU_UNSET = 0;
/**
* Set the server address and served prefix for the DHCP server.
*
* <p>This parameter is required.
*/
public DhcpServingParamsParcelExt setServerAddr(@NonNull LinkAddress serverAddr) {
this.serverAddr = inet4AddressToIntHTH((Inet4Address) serverAddr.getAddress());
this.serverAddrPrefixLength = serverAddr.getPrefixLength();
return this;
}
/**
* Set the default routers to be advertised to DHCP clients.
*
* <p>Each router must be inside the served prefix. This may be an empty set, but it must
* always be set explicitly.
*/
public DhcpServingParamsParcelExt setDefaultRouters(@NonNull Set<Inet4Address> defaultRouters) {
this.defaultRouters = toIntArray(defaultRouters);
return this;
}
/**
* Set the default routers to be advertised to DHCP clients.
*
* <p>Each router must be inside the served prefix. This may be an empty list of routers,
* but it must always be set explicitly.
*/
public DhcpServingParamsParcelExt setDefaultRouters(@NonNull Inet4Address... defaultRouters) {
return setDefaultRouters(Sets.newArraySet(defaultRouters));
}
/**
* Convenience method to build the parameters with no default router.
*
* <p>Equivalent to calling {@link #setDefaultRouters(Inet4Address...)} with no address.
*/
public DhcpServingParamsParcelExt setNoDefaultRouter() {
return setDefaultRouters();
}
/**
* Set the DNS servers to be advertised to DHCP clients.
*
* <p>This may be an empty set, but it must always be set explicitly.
*/
public DhcpServingParamsParcelExt setDnsServers(@NonNull Set<Inet4Address> dnsServers) {
this.dnsServers = toIntArray(dnsServers);
return this;
}
/**
* Set the DNS servers to be advertised to DHCP clients.
*
* <p>This may be an empty list of servers, but it must always be set explicitly.
*/
public DhcpServingParamsParcelExt setDnsServers(@NonNull Inet4Address... dnsServers) {
return setDnsServers(Sets.newArraySet(dnsServers));
}
/**
* Convenience method to build the parameters with no DNS server.
*
* <p>Equivalent to calling {@link #setDnsServers(Inet4Address...)} with no address.
*/
public DhcpServingParamsParcelExt setNoDnsServer() {
return setDnsServers();
}
/**
* Set excluded addresses that the DHCP server is not allowed to assign to clients.
*
* <p>This parameter is optional. DNS servers and default routers are always excluded
* and do not need to be set here.
*/
public DhcpServingParamsParcelExt setExcludedAddrs(@NonNull Set<Inet4Address> excludedAddrs) {
this.excludedAddrs = toIntArray(excludedAddrs);
return this;
}
/**
* Set excluded addresses that the DHCP server is not allowed to assign to clients.
*
* <p>This parameter is optional. DNS servers and default routers are always excluded
* and do not need to be set here.
*/
public DhcpServingParamsParcelExt setExcludedAddrs(@NonNull Inet4Address... excludedAddrs) {
return setExcludedAddrs(Sets.newArraySet(excludedAddrs));
}
/**
* Set the lease time for leases assigned by the DHCP server.
*
* <p>This parameter is required.
*/
public DhcpServingParamsParcelExt setDhcpLeaseTimeSecs(long dhcpLeaseTimeSecs) {
this.dhcpLeaseTimeSecs = dhcpLeaseTimeSecs;
return this;
}
/**
* Set the link MTU to be advertised to DHCP clients.
*
* <p>If set to {@link #MTU_UNSET}, no MTU will be advertised to clients. This parameter
* is optional and defaults to {@link #MTU_UNSET}.
*/
public DhcpServingParamsParcelExt setLinkMtu(int linkMtu) {
this.linkMtu = linkMtu;
return this;
}
/**
* Set whether the DHCP server should send the ANDROID_METERED vendor-specific option.
*
* <p>If not set, the default value is false.
*/
public DhcpServingParamsParcelExt setMetered(boolean metered) {
this.metered = metered;
return this;
}
private static int[] toIntArray(@NonNull Collection<Inet4Address> addrs) {
int[] res = new int[addrs.size()];
int i = 0;
for (Inet4Address addr : addrs) {
res[i] = inet4AddressToIntHTH(addr);
i++;
}
return res;
}
}
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (C) 2018 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 java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringJoiner;
/**
* @hide
*/
public class InterfaceSet {
public final Set<String> ifnames;
public InterfaceSet(String... names) {
final Set<String> nameSet = new HashSet<>();
for (String name : names) {
if (name != null) nameSet.add(name);
}
ifnames = Collections.unmodifiableSet(nameSet);
}
@Override
public String toString() {
final StringJoiner sj = new StringJoiner(",", "[", "]");
for (String ifname : ifnames) sj.add(ifname);
return sj.toString();
}
@Override
public boolean equals(Object obj) {
return obj != null
&& obj instanceof InterfaceSet
&& ifnames.equals(((InterfaceSet) obj).ifnames);
}
}
......@@ -43,5 +43,8 @@ filegroup {
name: "tethering-tests-src",
srcs: [
"src/com/android/server/connectivity/tethering/TetheringConfigurationTest.java",
"src/android/net/dhcp/DhcpServingParamsParcelExtTest.java",
"src/android/net/ip/IpServerTest.java",
"src/android/net/util/InterfaceSetTest.java",
],
}
/*
* Copyright (C) 2018 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.dhcp;
import static android.net.InetAddresses.parseNumericAddress;
import static com.google.android.collect.Sets.newHashSet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.net.LinkAddress;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.net.Inet4Address;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class DhcpServingParamsParcelExtTest {
private static final Inet4Address TEST_ADDRESS = inet4Addr("192.168.0.123");
private static final int TEST_ADDRESS_PARCELED = 0xc0a8007b;
private static final int TEST_PREFIX_LENGTH = 17;
private static final int TEST_LEASE_TIME_SECS = 120;
private static final int TEST_MTU = 1000;
private static final Set<Inet4Address> TEST_ADDRESS_SET =
newHashSet(inet4Addr("192.168.1.123"), inet4Addr("192.168.1.124"));
private static final Set<Integer> TEST_ADDRESS_SET_PARCELED =
newHashSet(0xc0a8017b, 0xc0a8017c);
private DhcpServingParamsParcelExt mParcel;
@Before
public void setUp() {
mParcel = new DhcpServingParamsParcelExt();
}
@Test
public void testSetServerAddr() {
mParcel.setServerAddr(new LinkAddress(TEST_ADDRESS, TEST_PREFIX_LENGTH));
assertEquals(TEST_ADDRESS_PARCELED, mParcel.serverAddr);
assertEquals(TEST_PREFIX_LENGTH, mParcel.serverAddrPrefixLength);
}
@Test
public void testSetDefaultRouters() {
mParcel.setDefaultRouters(TEST_ADDRESS_SET);
assertEquals(TEST_ADDRESS_SET_PARCELED, asSet(mParcel.defaultRouters));
}
@Test
public void testSetDnsServers() {
mParcel.setDnsServers(TEST_ADDRESS_SET);
assertEquals(TEST_ADDRESS_SET_PARCELED, asSet(mParcel.dnsServers));
}
@Test
public void testSetExcludedAddrs() {
mParcel.setExcludedAddrs(TEST_ADDRESS_SET);
assertEquals(TEST_ADDRESS_SET_PARCELED, asSet(mParcel.excludedAddrs));
}
@Test
public void testSetDhcpLeaseTimeSecs() {
mParcel.setDhcpLeaseTimeSecs(TEST_LEASE_TIME_SECS);
assertEquals(TEST_LEASE_TIME_SECS, mParcel.dhcpLeaseTimeSecs);
}
@Test
public void testSetLinkMtu() {
mParcel.setLinkMtu(TEST_MTU);
assertEquals(TEST_MTU, mParcel.linkMtu);
}
@Test
public void testSetMetered() {
mParcel.setMetered(true);
assertTrue(mParcel.metered);
mParcel.setMetered(false);
assertFalse(mParcel.metered);
}
private static Inet4Address inet4Addr(String addr) {
return (Inet4Address) parseNumericAddress(addr);
}
private static Set<Integer> asSet(int[] ints) {
return IntStream.of(ints).boxed().collect(Collectors.toSet());
}
}
This diff is collapsed.
/*
* Copyright (C) 2018 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 junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class InterfaceSetTest {
@Test
public void testNullNamesIgnored() {
final InterfaceSet set = new InterfaceSet(null, "if1", null, "if2", null);
assertEquals(2, set.ifnames.size());
assertTrue(set.ifnames.contains("if1"));
assertTrue(set.ifnames.contains("if2"));
}
@Test
public void testToString() {
final InterfaceSet set = new InterfaceSet("if1", "if2");
final String setString = set.toString();
assertTrue(setString.equals("[if1,if2]") || setString.equals("[if2,if1]"));
}
@Test
public void testToString_Empty() {
final InterfaceSet set = new InterfaceSet(null, null);
assertEquals("[]", set.toString());
}
@Test
public void testEquals() {
assertEquals(new InterfaceSet(null, "if1", "if2"), new InterfaceSet("if2", "if1"));
assertEquals(new InterfaceSet(null, null), new InterfaceSet());
assertFalse(new InterfaceSet("if1", "if3").equals(new InterfaceSet("if1", "if2")));
assertFalse(new InterfaceSet("if1", "if2").equals(new InterfaceSet("if1")));
assertFalse(new InterfaceSet().equals(null));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment