blob: 1fe2328f1cdb78693cb81c970f8dc117d3bc72f1 [file] [log] [blame]
markchien74a4fa92019-09-09 20:50:49 +08001/*
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
17package android.net.dhcp;
18
19import static android.net.shared.Inet4AddressUtils.inet4AddressToIntHTH;
20
21import android.annotation.NonNull;
22import android.net.LinkAddress;
23
24import com.google.android.collect.Sets;
25
26import java.net.Inet4Address;
27import java.util.Collection;
28import 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 */
39public 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) {
71 return setDefaultRouters(Sets.newArraySet(defaultRouters));
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) {
99 return setDnsServers(Sets.newArraySet(dnsServers));
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) {
129 return setExcludedAddrs(Sets.newArraySet(excludedAddrs));
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 }
172}