1 /* 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 17 package android.net; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 /** 25 * A class to communicate configuration info about a local network through {@link NetworkAgent}. 26 * @hide 27 */ 28 // TODO : @SystemApi 29 public final class LocalNetworkConfig implements Parcelable { 30 @Nullable 31 private final NetworkRequest mUpstreamSelector; 32 33 @NonNull 34 private final MulticastRoutingConfig mUpstreamMulticastRoutingConfig; 35 36 @NonNull 37 private final MulticastRoutingConfig mDownstreamMulticastRoutingConfig; 38 LocalNetworkConfig(@ullable final NetworkRequest upstreamSelector, @Nullable final MulticastRoutingConfig upstreamConfig, @Nullable final MulticastRoutingConfig downstreamConfig)39 private LocalNetworkConfig(@Nullable final NetworkRequest upstreamSelector, 40 @Nullable final MulticastRoutingConfig upstreamConfig, 41 @Nullable final MulticastRoutingConfig downstreamConfig) { 42 mUpstreamSelector = upstreamSelector; 43 if (null != upstreamConfig) { 44 mUpstreamMulticastRoutingConfig = upstreamConfig; 45 } else { 46 mUpstreamMulticastRoutingConfig = MulticastRoutingConfig.CONFIG_FORWARD_NONE; 47 } 48 if (null != downstreamConfig) { 49 mDownstreamMulticastRoutingConfig = downstreamConfig; 50 } else { 51 mDownstreamMulticastRoutingConfig = MulticastRoutingConfig.CONFIG_FORWARD_NONE; 52 } 53 } 54 55 /** 56 * Get the request choosing which network traffic from this network is forwarded to and from. 57 * 58 * This may be null if the local network doesn't forward the traffic anywhere. 59 */ 60 @Nullable getUpstreamSelector()61 public NetworkRequest getUpstreamSelector() { 62 return mUpstreamSelector; 63 } 64 65 /** 66 * Get the upstream multicast routing config 67 */ 68 @NonNull getUpstreamMulticastRoutingConfig()69 public MulticastRoutingConfig getUpstreamMulticastRoutingConfig() { 70 return mUpstreamMulticastRoutingConfig; 71 } 72 73 /** 74 * Get the downstream multicast routing config 75 */ 76 @NonNull getDownstreamMulticastRoutingConfig()77 public MulticastRoutingConfig getDownstreamMulticastRoutingConfig() { 78 return mDownstreamMulticastRoutingConfig; 79 } 80 81 @Override describeContents()82 public int describeContents() { 83 return 0; 84 } 85 86 @Override writeToParcel(@onNull final Parcel dest, final int flags)87 public void writeToParcel(@NonNull final Parcel dest, final int flags) { 88 dest.writeParcelable(mUpstreamSelector, flags); 89 dest.writeParcelable(mUpstreamMulticastRoutingConfig, flags); 90 dest.writeParcelable(mDownstreamMulticastRoutingConfig, flags); 91 } 92 93 @Override toString()94 public String toString() { 95 return "LocalNetworkConfig{" 96 + "UpstreamSelector=" + mUpstreamSelector 97 + ", UpstreamMulticastConfig=" + mUpstreamMulticastRoutingConfig 98 + ", DownstreamMulticastConfig=" + mDownstreamMulticastRoutingConfig 99 + '}'; 100 } 101 102 public static final @NonNull Creator<LocalNetworkConfig> CREATOR = new Creator<>() { 103 public LocalNetworkConfig createFromParcel(Parcel in) { 104 final NetworkRequest upstreamSelector = in.readParcelable(null); 105 final MulticastRoutingConfig upstreamConfig = in.readParcelable(null); 106 final MulticastRoutingConfig downstreamConfig = in.readParcelable(null); 107 return new LocalNetworkConfig( 108 upstreamSelector, upstreamConfig, downstreamConfig); 109 } 110 111 @Override 112 public LocalNetworkConfig[] newArray(final int size) { 113 return new LocalNetworkConfig[size]; 114 } 115 }; 116 117 118 public static final class Builder { 119 @Nullable 120 private NetworkRequest mUpstreamSelector; 121 122 @Nullable 123 private MulticastRoutingConfig mUpstreamMulticastRoutingConfig; 124 125 @Nullable 126 private MulticastRoutingConfig mDownstreamMulticastRoutingConfig; 127 128 /** 129 * Create a Builder 130 */ Builder()131 public Builder() { 132 } 133 134 /** 135 * Set to choose where this local network should forward its traffic to. 136 * 137 * The system will automatically choose the best network matching the request as an 138 * upstream, and set up forwarding between this local network and the chosen upstream. 139 * If no network matches the request, there is no upstream and the traffic is not forwarded. 140 * The caller can know when this changes by listening to link properties changes of 141 * this network with the {@link android.net.LinkProperties#getForwardedNetwork()} getter. 142 * 143 * Set this to null if the local network shouldn't be forwarded. Default is null. 144 */ 145 @NonNull setUpstreamSelector(@ullable NetworkRequest upstreamSelector)146 public Builder setUpstreamSelector(@Nullable NetworkRequest upstreamSelector) { 147 mUpstreamSelector = upstreamSelector; 148 return this; 149 } 150 151 /** 152 * Set the upstream multicast routing config. 153 * 154 * If null, don't route multicast packets upstream. This is equivalent to a 155 * MulticastRoutingConfig in mode FORWARD_NONE. The default is null. 156 */ 157 @NonNull setUpstreamMulticastRoutingConfig(@ullable MulticastRoutingConfig cfg)158 public Builder setUpstreamMulticastRoutingConfig(@Nullable MulticastRoutingConfig cfg) { 159 mUpstreamMulticastRoutingConfig = cfg; 160 return this; 161 } 162 163 /** 164 * Set the downstream multicast routing config. 165 * 166 * If null, don't route multicast packets downstream. This is equivalent to a 167 * MulticastRoutingConfig in mode FORWARD_NONE. The default is null. 168 */ 169 @NonNull setDownstreamMulticastRoutingConfig(@ullable MulticastRoutingConfig cfg)170 public Builder setDownstreamMulticastRoutingConfig(@Nullable MulticastRoutingConfig cfg) { 171 mDownstreamMulticastRoutingConfig = cfg; 172 return this; 173 } 174 175 /** 176 * Build the LocalNetworkConfig object. 177 */ 178 @NonNull build()179 public LocalNetworkConfig build() { 180 return new LocalNetworkConfig(mUpstreamSelector, 181 mUpstreamMulticastRoutingConfig, 182 mDownstreamMulticastRoutingConfig); 183 } 184 } 185 } 186