markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net.dhcp; |
| 18 | |
| 19 | import static android.net.shared.Inet4AddressUtils.inet4AddressToIntHTH; |
| 20 | |
| 21 | import android.annotation.NonNull; |
| 22 | import android.net.LinkAddress; |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 23 | import android.util.ArraySet; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 24 | |
| 25 | import java.net.Inet4Address; |
| 26 | import java.util.Collection; |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 27 | import java.util.Collections; |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * Subclass of {@link DhcpServingParamsParcel} with additional utility methods for building. |
| 32 | * |
| 33 | * <p>This utility class does not check for validity of the parameters: invalid parameters are |
| 34 | * reported by the receiving module when unparceling the parcel. |
| 35 | * |
| 36 | * @see DhcpServingParams |
| 37 | * @hide |
| 38 | */ |
| 39 | public class DhcpServingParamsParcelExt extends DhcpServingParamsParcel { |
| 40 | public static final int MTU_UNSET = 0; |
| 41 | |
| 42 | /** |
| 43 | * Set the server address and served prefix for the DHCP server. |
| 44 | * |
| 45 | * <p>This parameter is required. |
| 46 | */ |
| 47 | public DhcpServingParamsParcelExt setServerAddr(@NonNull LinkAddress serverAddr) { |
| 48 | this.serverAddr = inet4AddressToIntHTH((Inet4Address) serverAddr.getAddress()); |
| 49 | this.serverAddrPrefixLength = serverAddr.getPrefixLength(); |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Set the default routers to be advertised to DHCP clients. |
| 55 | * |
| 56 | * <p>Each router must be inside the served prefix. This may be an empty set, but it must |
| 57 | * always be set explicitly. |
| 58 | */ |
| 59 | public DhcpServingParamsParcelExt setDefaultRouters(@NonNull Set<Inet4Address> defaultRouters) { |
| 60 | this.defaultRouters = toIntArray(defaultRouters); |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the default routers to be advertised to DHCP clients. |
| 66 | * |
| 67 | * <p>Each router must be inside the served prefix. This may be an empty list of routers, |
| 68 | * but it must always be set explicitly. |
| 69 | */ |
| 70 | public DhcpServingParamsParcelExt setDefaultRouters(@NonNull Inet4Address... defaultRouters) { |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 71 | return setDefaultRouters(newArraySet(defaultRouters)); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Convenience method to build the parameters with no default router. |
| 76 | * |
| 77 | * <p>Equivalent to calling {@link #setDefaultRouters(Inet4Address...)} with no address. |
| 78 | */ |
| 79 | public DhcpServingParamsParcelExt setNoDefaultRouter() { |
| 80 | return setDefaultRouters(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the DNS servers to be advertised to DHCP clients. |
| 85 | * |
| 86 | * <p>This may be an empty set, but it must always be set explicitly. |
| 87 | */ |
| 88 | public DhcpServingParamsParcelExt setDnsServers(@NonNull Set<Inet4Address> dnsServers) { |
| 89 | this.dnsServers = toIntArray(dnsServers); |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set the DNS servers to be advertised to DHCP clients. |
| 95 | * |
| 96 | * <p>This may be an empty list of servers, but it must always be set explicitly. |
| 97 | */ |
| 98 | public DhcpServingParamsParcelExt setDnsServers(@NonNull Inet4Address... dnsServers) { |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 99 | return setDnsServers(newArraySet(dnsServers)); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Convenience method to build the parameters with no DNS server. |
| 104 | * |
| 105 | * <p>Equivalent to calling {@link #setDnsServers(Inet4Address...)} with no address. |
| 106 | */ |
| 107 | public DhcpServingParamsParcelExt setNoDnsServer() { |
| 108 | return setDnsServers(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Set excluded addresses that the DHCP server is not allowed to assign to clients. |
| 113 | * |
| 114 | * <p>This parameter is optional. DNS servers and default routers are always excluded |
| 115 | * and do not need to be set here. |
| 116 | */ |
| 117 | public DhcpServingParamsParcelExt setExcludedAddrs(@NonNull Set<Inet4Address> excludedAddrs) { |
| 118 | this.excludedAddrs = toIntArray(excludedAddrs); |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set excluded addresses that the DHCP server is not allowed to assign to clients. |
| 124 | * |
| 125 | * <p>This parameter is optional. DNS servers and default routers are always excluded |
| 126 | * and do not need to be set here. |
| 127 | */ |
| 128 | public DhcpServingParamsParcelExt setExcludedAddrs(@NonNull Inet4Address... excludedAddrs) { |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 129 | return setExcludedAddrs(newArraySet(excludedAddrs)); |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Set the lease time for leases assigned by the DHCP server. |
| 134 | * |
| 135 | * <p>This parameter is required. |
| 136 | */ |
| 137 | public DhcpServingParamsParcelExt setDhcpLeaseTimeSecs(long dhcpLeaseTimeSecs) { |
| 138 | this.dhcpLeaseTimeSecs = dhcpLeaseTimeSecs; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set the link MTU to be advertised to DHCP clients. |
| 144 | * |
| 145 | * <p>If set to {@link #MTU_UNSET}, no MTU will be advertised to clients. This parameter |
| 146 | * is optional and defaults to {@link #MTU_UNSET}. |
| 147 | */ |
| 148 | public DhcpServingParamsParcelExt setLinkMtu(int linkMtu) { |
| 149 | this.linkMtu = linkMtu; |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Set whether the DHCP server should send the ANDROID_METERED vendor-specific option. |
| 155 | * |
| 156 | * <p>If not set, the default value is false. |
| 157 | */ |
| 158 | public DhcpServingParamsParcelExt setMetered(boolean metered) { |
| 159 | this.metered = metered; |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | private static int[] toIntArray(@NonNull Collection<Inet4Address> addrs) { |
| 164 | int[] res = new int[addrs.size()]; |
| 165 | int i = 0; |
| 166 | for (Inet4Address addr : addrs) { |
| 167 | res[i] = inet4AddressToIntHTH(addr); |
| 168 | i++; |
| 169 | } |
| 170 | return res; |
| 171 | } |
markchien | 6cf0e55 | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 172 | |
| 173 | private static ArraySet<Inet4Address> newArraySet(Inet4Address... addrs) { |
| 174 | ArraySet<Inet4Address> addrSet = new ArraySet<>(addrs.length); |
| 175 | Collections.addAll(addrSet, addrs); |
| 176 | return addrSet; |
| 177 | } |
markchien | 74a4fa9 | 2019-09-09 20:50:49 +0800 | [diff] [blame] | 178 | } |