blob: 1f4071cd6bdb9b9cbcab8fa96b62bf6b4d097a11 [file] [log] [blame]
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +09001/*
2 * Copyright (C) 2023 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;
18
19import android.annotation.IntDef;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.util.ArraySet;
25import android.util.Log;
26
27import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29import java.net.Inet6Address;
30import java.net.UnknownHostException;
31import java.util.Arrays;
32import java.util.Collections;
33import java.util.Set;
34
35/**
36 * A class representing a configuration for multicast routing.
37 *
38 * Internal usage to Connectivity
39 * @hide
40 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +090041// @SystemApi(client = MODULE_LIBRARIES)
42public final class MulticastRoutingConfig implements Parcelable {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +090043 private static final String TAG = MulticastRoutingConfig.class.getSimpleName();
44
45 /** Do not forward any multicast packets. */
46 public static final int FORWARD_NONE = 0;
47 /**
48 * Forward only multicast packets with destination in the list of listening addresses.
49 * Ignore the min scope.
50 */
51 public static final int FORWARD_SELECTED = 1;
52 /**
53 * Forward all multicast packets with scope greater or equal than the min scope.
54 * Ignore the list of listening addresses.
55 */
56 public static final int FORWARD_WITH_MIN_SCOPE = 2;
57
Chalard Jeanbb2557a2023-10-09 15:44:56 +090058 /** @hide */
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +090059 @Retention(RetentionPolicy.SOURCE)
60 @IntDef(prefix = { "FORWARD_" }, value = {
61 FORWARD_NONE,
62 FORWARD_SELECTED,
63 FORWARD_WITH_MIN_SCOPE
64 })
65 public @interface MulticastForwardingMode {}
66
67 /**
68 * Not a multicast scope, for configurations that do not use the min scope.
69 */
70 public static final int MULTICAST_SCOPE_NONE = -1;
71
Chalard Jeanbb2557a2023-10-09 15:44:56 +090072 /** @hide */
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +090073 public static final MulticastRoutingConfig CONFIG_FORWARD_NONE =
74 new MulticastRoutingConfig(FORWARD_NONE, MULTICAST_SCOPE_NONE, null);
75
76 @MulticastForwardingMode
77 private final int mForwardingMode;
78
79 private final int mMinScope;
80
81 @NonNull
82 private final Set<Inet6Address> mListeningAddresses;
83
84 private MulticastRoutingConfig(@MulticastForwardingMode final int mode, final int scope,
85 @Nullable final Set<Inet6Address> addresses) {
86 mForwardingMode = mode;
87 mMinScope = scope;
88 if (null != addresses) {
89 mListeningAddresses = Collections.unmodifiableSet(new ArraySet<>(addresses));
90 } else {
91 mListeningAddresses = Collections.emptySet();
92 }
93 }
94
95 /**
96 * Returns the forwarding mode.
97 */
98 @MulticastForwardingMode
99 public int getForwardingMode() {
100 return mForwardingMode;
101 }
102
103 /**
104 * Returns the minimal group address scope that is allowed for forwarding.
105 * If the forwarding mode is not FORWARD_WITH_MIN_SCOPE, will be MULTICAST_SCOPE_NONE.
106 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900107 public int getMinimumScope() {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900108 return mMinScope;
109 }
110
111 /**
112 * Returns the list of group addresses listened by the outgoing interface.
113 * The list will be empty if the forwarding mode is not FORWARD_SELECTED.
114 */
115 @NonNull
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900116 public Set<Inet6Address> getListeningAddresses() {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900117 return mListeningAddresses;
118 }
119
120 private MulticastRoutingConfig(Parcel in) {
121 mForwardingMode = in.readInt();
122 mMinScope = in.readInt();
123 final int count = in.readInt();
124 final ArraySet<Inet6Address> listeningAddresses = new ArraySet<>(count);
125 final byte[] buffer = new byte[16]; // Size of an Inet6Address
126 for (int i = 0; i < count; ++i) {
127 in.readByteArray(buffer);
128 try {
129 listeningAddresses.add((Inet6Address) Inet6Address.getByAddress(buffer));
130 } catch (UnknownHostException e) {
131 Log.wtf(TAG, "Can't read inet6address : " + Arrays.toString(buffer));
132 }
133 }
134 mListeningAddresses = Collections.unmodifiableSet(listeningAddresses);
135 }
136
137 @Override
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900138 public void writeToParcel(@NonNull Parcel dest, int flags) {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900139 dest.writeInt(mForwardingMode);
140 dest.writeInt(mMinScope);
141 dest.writeInt(mListeningAddresses.size());
142 for (final Inet6Address addr : mListeningAddresses) {
143 dest.writeByteArray(addr.getAddress());
144 }
145 }
146
147 @Override
148 public int describeContents() {
149 return 0;
150 }
151
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900152 @NonNull
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900153 public static final Creator<MulticastRoutingConfig> CREATOR = new Creator<>() {
154 @Override
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900155 public MulticastRoutingConfig createFromParcel(@NonNull Parcel in) {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900156 return new MulticastRoutingConfig(in);
157 }
158
159 @Override
160 public MulticastRoutingConfig[] newArray(int size) {
161 return new MulticastRoutingConfig[size];
162 }
163 };
164
Chalard Jean4fe23392023-10-17 23:02:27 +0900165 @Override
166 public String toString() {
167 return "MulticastRoutingConfig{"
168 + "ForwardingMode=" + forwardingModeToString(mForwardingMode)
169 + ", MinScope=" + mMinScope
170 + ", ListeningAddresses=" + mListeningAddresses
171 + '}';
172 }
173
174 private static String forwardingModeToString(final int forwardingMode) {
175 switch (forwardingMode) {
176 case FORWARD_NONE: return "NONE";
177 case FORWARD_SELECTED: return "SELECTED";
178 case FORWARD_WITH_MIN_SCOPE: return "WITH_MIN_SCOPE";
179 default: return "UNKNOWN";
180 }
181 }
182
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900183 public static final class Builder {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900184 @MulticastForwardingMode
185 private final int mForwardingMode;
186 private int mMinScope;
187 private final ArraySet<Inet6Address> mListeningAddresses;
188
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900189 // The two constructors with runtime checks for the mode and scope are arguably
190 // less convenient than three static factory methods, but API guidelines mandates
191 // that Builders are built with a constructor and not factory methods.
192 /**
193 * Create a new builder for forwarding mode FORWARD_NONE or FORWARD_SELECTED.
194 *
195 * <p>On a Builder for FORWARD_NONE, no properties can be set.
196 * <p>On a Builder for FORWARD_SELECTED, listening addresses can be added and removed
197 * but the minimum scope can't be set.
198 *
199 * @param mode {@link #FORWARD_NONE} or {@link #FORWARD_SELECTED}. Any other
200 * value will result in IllegalArgumentException.
201 * @see #Builder(int, int)
202 */
203 public Builder(@MulticastForwardingMode final int mode) {
204 if (FORWARD_NONE != mode && FORWARD_SELECTED != mode) {
205 if (FORWARD_WITH_MIN_SCOPE == mode) {
206 throw new IllegalArgumentException("FORWARD_WITH_MIN_SCOPE requires "
207 + "passing the scope as a second argument");
208 } else {
209 throw new IllegalArgumentException("Unknown forwarding mode : " + mode);
210 }
211 }
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900212 mForwardingMode = mode;
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900213 mMinScope = MULTICAST_SCOPE_NONE;
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900214 mListeningAddresses = new ArraySet<>();
215 }
216
217 /**
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900218 * Create a new builder for forwarding mode FORWARD_WITH_MIN_SCOPE.
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900219 *
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900220 * <p>On this Builder the scope can be set with {@link #setMinimumScope}, but
221 * listening addresses can't be added or removed.
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900222 *
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900223 * @param mode Must be {@link #FORWARD_WITH_MIN_SCOPE}.
224 * @param scope the minimum scope for this multicast routing config.
225 * @see Builder#Builder(int)
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900226 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900227 public Builder(@MulticastForwardingMode final int mode, int scope) {
228 if (FORWARD_WITH_MIN_SCOPE != mode) {
229 throw new IllegalArgumentException("Forwarding with a min scope must "
230 + "use forward mode FORWARD_WITH_MIN_SCOPE");
231 }
232 mForwardingMode = mode;
233 mMinScope = scope;
234 mListeningAddresses = new ArraySet<>();
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900235 }
236
237 /**
238 * Sets the minimum scope for this multicast routing config.
239 * This is only meaningful (indeed, allowed) for configs in FORWARD_WITH_MIN_SCOPE mode.
240 * @return this builder
241 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900242 @NonNull
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900243 public Builder setMinimumScope(final int scope) {
244 if (FORWARD_WITH_MIN_SCOPE != mForwardingMode) {
245 throw new IllegalArgumentException("Can't set the scope on a builder in mode "
246 + modeToString(mForwardingMode));
247 }
248 mMinScope = scope;
249 return this;
250 }
251
252 /**
253 * Add an address to the set of listening addresses.
254 *
255 * This is only meaningful (indeed, allowed) for configs in FORWARD_SELECTED mode.
256 * If this address was already added, this is a no-op.
257 * @return this builder
258 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900259 @NonNull
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900260 public Builder addListeningAddress(@NonNull final Inet6Address address) {
261 if (FORWARD_SELECTED != mForwardingMode) {
262 throw new IllegalArgumentException("Can't add an address on a builder in mode "
263 + modeToString(mForwardingMode));
264 }
265 // TODO : should we check that this is a multicast address ?
266 mListeningAddresses.add(address);
267 return this;
268 }
269
270 /**
271 * Remove an address from the set of listening addresses.
272 *
273 * This is only meaningful (indeed, allowed) for configs in FORWARD_SELECTED mode.
274 * If this address was not added, or was already removed, this is a no-op.
275 * @return this builder
276 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900277 @NonNull
278 public Builder clearListeningAddress(@NonNull final Inet6Address address) {
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900279 if (FORWARD_SELECTED != mForwardingMode) {
280 throw new IllegalArgumentException("Can't remove an address on a builder in mode "
281 + modeToString(mForwardingMode));
282 }
283 mListeningAddresses.remove(address);
284 return this;
285 }
286
287 /**
288 * Build the config.
289 */
Chalard Jeanbb2557a2023-10-09 15:44:56 +0900290 @NonNull
Chalard Jeanf9d0e3e2023-10-17 13:23:17 +0900291 public MulticastRoutingConfig build() {
292 return new MulticastRoutingConfig(mForwardingMode, mMinScope, mListeningAddresses);
293 }
294 }
295
296 private static String modeToString(@MulticastForwardingMode final int mode) {
297 switch (mode) {
298 case FORWARD_NONE: return "FORWARD_NONE";
299 case FORWARD_SELECTED: return "FORWARD_SELECTED";
300 case FORWARD_WITH_MIN_SCOPE: return "FORWARD_WITH_MIN_SCOPE";
301 default: return "unknown multicast routing mode " + mode;
302 }
303 }
304}