Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Artur Satayev | 0e45d78 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | ac5968e | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 24 | import android.os.Build; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
| 27 | |
Chalard Jean | fa159c1 | 2020-08-19 16:07:22 +0900 | [diff] [blame] | 28 | import com.android.net.module.util.NetUtils; |
paulhu | ee4cd63 | 2021-03-04 10:53:27 +0800 | [diff] [blame^] | 29 | import com.android.net.module.util.NetworkStackConstants; |
Chalard Jean | fa159c1 | 2020-08-19 16:07:22 +0900 | [diff] [blame] | 30 | |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 31 | import java.lang.annotation.Retention; |
| 32 | import java.lang.annotation.RetentionPolicy; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 33 | import java.net.Inet4Address; |
| 34 | import java.net.Inet6Address; |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 35 | import java.net.InetAddress; |
| 36 | import java.net.UnknownHostException; |
Robert Greenwalt | bd49221 | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 38 | import java.util.Objects; |
Robert Greenwalt | bd49221 | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 39 | |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 40 | /** |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 41 | * Represents a network route. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 42 | * <p> |
| 43 | * This is used both to describe static network configuration and live network |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 44 | * configuration information. |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 45 | * |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 46 | * A route contains three pieces of information: |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 47 | * <ul> |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 48 | * <li>a destination {@link IpPrefix} specifying the network destinations covered by this route. |
| 49 | * If this is {@code null} it indicates a default route of the address family (IPv4 or IPv6) |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 50 | * implied by the gateway IP address. |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 51 | * <li>a gateway {@link InetAddress} indicating the next hop to use. If this is {@code null} it |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 52 | * indicates a directly-connected route. |
| 53 | * <li>an interface (which may be unspecified). |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 54 | * </ul> |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 55 | * Either the destination or the gateway may be {@code null}, but not both. If the |
| 56 | * destination and gateway are both specified, they must be of the same address family |
| 57 | * (IPv4 or IPv6). |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 58 | */ |
Robert Greenwalt | cccf73a | 2014-06-12 16:24:38 -0700 | [diff] [blame] | 59 | public final class RouteInfo implements Parcelable { |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 60 | /** @hide */ |
| 61 | @IntDef(value = { |
| 62 | RTN_UNICAST, |
| 63 | RTN_UNREACHABLE, |
| 64 | RTN_THROW, |
| 65 | }) |
| 66 | @Retention(RetentionPolicy.SOURCE) |
| 67 | public @interface RouteType {} |
| 68 | |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 69 | /** |
| 70 | * The IP destination address for this route. |
| 71 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 72 | @NonNull |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 73 | private final IpPrefix mDestination; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * The gateway address for this route. |
| 77 | */ |
Mathew Inwood | 0b8f861 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 78 | @UnsupportedAppUsage |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 79 | @Nullable |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 80 | private final InetAddress mGateway; |
| 81 | |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 82 | /** |
| 83 | * The interface for this route. |
| 84 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 85 | @Nullable |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 86 | private final String mInterface; |
| 87 | |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 88 | |
| 89 | /** Unicast route. @hide */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 90 | @SystemApi |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 91 | public static final int RTN_UNICAST = 1; |
| 92 | |
| 93 | /** Unreachable route. @hide */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 94 | @SystemApi |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 95 | public static final int RTN_UNREACHABLE = 7; |
| 96 | |
| 97 | /** Throw route. @hide */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 98 | @SystemApi |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 99 | public static final int RTN_THROW = 9; |
| 100 | |
| 101 | /** |
| 102 | * The type of this route; one of the RTN_xxx constants above. |
| 103 | */ |
| 104 | private final int mType; |
| 105 | |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 106 | /** |
| 107 | * The maximum transmission unit size for this route. |
| 108 | */ |
| 109 | private final int mMtu; |
| 110 | |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 111 | // Derived data members. |
| 112 | // TODO: remove these. |
Mathew Inwood | ac5968e | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 113 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Robert Greenwalt | e8d2a4a | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 114 | private final boolean mIsHost; |
Robert Greenwalt | 24bc534 | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 115 | private final boolean mHasGateway; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 116 | |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Constructs a RouteInfo object. |
| 119 | * |
Lorenzo Colitti | ab7ad3a | 2013-03-12 04:30:47 +0900 | [diff] [blame] | 120 | * If destination is null, then gateway must be specified and the |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 121 | * constructed route is either the IPv4 default route <code>0.0.0.0</code> |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 122 | * if the gateway is an instance of {@link Inet4Address}, or the IPv6 default |
Lorenzo Colitti | ab7ad3a | 2013-03-12 04:30:47 +0900 | [diff] [blame] | 123 | * route <code>::/0</code> if gateway is an instance of |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 124 | * {@link Inet6Address}. |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 125 | * <p> |
Lorenzo Colitti | ab7ad3a | 2013-03-12 04:30:47 +0900 | [diff] [blame] | 126 | * destination and gateway may not both be null. |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 127 | * |
| 128 | * @param destination the destination prefix |
| 129 | * @param gateway the IP address to route packets through |
| 130 | * @param iface the interface name to send packets on |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 131 | * @param type the type of this route |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 132 | * |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 133 | * @hide |
| 134 | */ |
Remi NGUYEN VAN | 76be723 | 2019-01-24 00:55:43 +0900 | [diff] [blame] | 135 | @SystemApi |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 136 | public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway, |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 137 | @Nullable String iface, @RouteType int type) { |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 138 | this(destination, gateway, iface, type, 0); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Constructs a RouteInfo object. |
| 143 | * |
| 144 | * If destination is null, then gateway must be specified and the |
| 145 | * constructed route is either the IPv4 default route <code>0.0.0.0</code> |
| 146 | * if the gateway is an instance of {@link Inet4Address}, or the IPv6 default |
| 147 | * route <code>::/0</code> if gateway is an instance of |
| 148 | * {@link Inet6Address}. |
| 149 | * <p> |
| 150 | * destination and gateway may not both be null. |
| 151 | * |
| 152 | * @param destination the destination prefix |
| 153 | * @param gateway the IP address to route packets through |
| 154 | * @param iface the interface name to send packets on |
| 155 | * @param type the type of this route |
| 156 | * @param mtu the maximum transmission unit size for this route |
| 157 | * |
| 158 | * @hide |
| 159 | */ |
| 160 | @SystemApi |
| 161 | public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway, |
| 162 | @Nullable String iface, @RouteType int type, int mtu) { |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 163 | switch (type) { |
| 164 | case RTN_UNICAST: |
| 165 | case RTN_UNREACHABLE: |
| 166 | case RTN_THROW: |
| 167 | // TODO: It would be nice to ensure that route types that don't have nexthops or |
| 168 | // interfaces, such as unreachable or throw, can't be created if an interface or |
| 169 | // a gateway is specified. This is a bit too complicated to do at the moment |
| 170 | // because: |
| 171 | // |
| 172 | // - LinkProperties sets the interface on routes added to it, and modifies the |
| 173 | // interfaces of all the routes when its interface name changes. |
| 174 | // - Even when the gateway is null, we store a non-null gateway here. |
| 175 | // |
| 176 | // For now, we just rely on the code that sets routes to do things properly. |
| 177 | break; |
| 178 | default: |
| 179 | throw new IllegalArgumentException("Unknown route type " + type); |
| 180 | } |
| 181 | |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 182 | if (destination == null) { |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 183 | if (gateway != null) { |
| 184 | if (gateway instanceof Inet4Address) { |
paulhu | ee4cd63 | 2021-03-04 10:53:27 +0800 | [diff] [blame^] | 185 | destination = new IpPrefix(NetworkStackConstants.IPV4_ADDR_ANY, 0); |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 186 | } else { |
paulhu | ee4cd63 | 2021-03-04 10:53:27 +0800 | [diff] [blame^] | 187 | destination = new IpPrefix(NetworkStackConstants.IPV6_ADDR_ANY, 0); |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 188 | } |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 189 | } else { |
| 190 | // no destination, no gateway. invalid. |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 191 | throw new IllegalArgumentException("Invalid arguments passed in: " + gateway + "," + |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 192 | destination); |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 193 | } |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 194 | } |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 195 | // TODO: set mGateway to null if there is no gateway. This is more correct, saves space, and |
| 196 | // matches the documented behaviour. Before we can do this we need to fix all callers (e.g., |
| 197 | // ConnectivityService) to stop doing things like r.getGateway().equals(), ... . |
Kazuhiro Ondo | efa66d9 | 2011-05-11 14:55:19 -0500 | [diff] [blame] | 198 | if (gateway == null) { |
| 199 | if (destination.getAddress() instanceof Inet4Address) { |
paulhu | ee4cd63 | 2021-03-04 10:53:27 +0800 | [diff] [blame^] | 200 | gateway = NetworkStackConstants.IPV4_ADDR_ANY; |
Kazuhiro Ondo | efa66d9 | 2011-05-11 14:55:19 -0500 | [diff] [blame] | 201 | } else { |
paulhu | ee4cd63 | 2021-03-04 10:53:27 +0800 | [diff] [blame^] | 202 | gateway = NetworkStackConstants.IPV6_ADDR_ANY; |
Kazuhiro Ondo | efa66d9 | 2011-05-11 14:55:19 -0500 | [diff] [blame] | 203 | } |
| 204 | } |
Robert Greenwalt | 24bc534 | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 205 | mHasGateway = (!gateway.isAnyLocalAddress()); |
| 206 | |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 207 | if ((destination.getAddress() instanceof Inet4Address |
| 208 | && !(gateway instanceof Inet4Address)) |
| 209 | || (destination.getAddress() instanceof Inet6Address |
| 210 | && !(gateway instanceof Inet6Address))) { |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 211 | throw new IllegalArgumentException("address family mismatch in RouteInfo constructor"); |
| 212 | } |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 213 | mDestination = destination; // IpPrefix objects are immutable. |
| 214 | mGateway = gateway; // InetAddress objects are immutable. |
| 215 | mInterface = iface; // Strings are immutable. |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 216 | mType = type; |
Robert Greenwalt | e8d2a4a | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 217 | mIsHost = isHost(); |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 218 | mMtu = mtu; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 221 | /** |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 222 | * Constructs a {@code RouteInfo} object. |
| 223 | * |
| 224 | * If destination is null, then gateway must be specified and the |
| 225 | * constructed route is either the IPv4 default route <code>0.0.0.0</code> |
| 226 | * if the gateway is an instance of {@link Inet4Address}, or the IPv6 default |
| 227 | * route <code>::/0</code> if gateway is an instance of {@link Inet6Address}. |
| 228 | * <p> |
| 229 | * Destination and gateway may not both be null. |
| 230 | * |
| 231 | * @param destination the destination address and prefix in an {@link IpPrefix} |
| 232 | * @param gateway the {@link InetAddress} to route packets through |
| 233 | * @param iface the interface name to send packets on |
| 234 | * |
| 235 | * @hide |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 236 | */ |
Mathew Inwood | 5a09a71 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 237 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 238 | public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway, |
| 239 | @Nullable String iface) { |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 240 | this(destination, gateway, iface, RTN_UNICAST); |
| 241 | } |
| 242 | |
| 243 | /** |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 244 | * @hide |
| 245 | */ |
Mathew Inwood | 0b8f861 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 246 | @UnsupportedAppUsage |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 247 | public RouteInfo(@Nullable LinkAddress destination, @Nullable InetAddress gateway, |
| 248 | @Nullable String iface) { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 249 | this(destination == null ? null : |
| 250 | new IpPrefix(destination.getAddress(), destination.getPrefixLength()), |
| 251 | gateway, iface); |
| 252 | } |
| 253 | |
| 254 | /** |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 255 | * Constructs a {@code RouteInfo} object. |
| 256 | * |
| 257 | * If destination is null, then gateway must be specified and the |
| 258 | * constructed route is either the IPv4 default route <code>0.0.0.0</code> |
| 259 | * if the gateway is an instance of {@link Inet4Address}, or the IPv6 default |
| 260 | * route <code>::/0</code> if gateway is an instance of {@link Inet6Address}. |
| 261 | * <p> |
| 262 | * Destination and gateway may not both be null. |
| 263 | * |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 264 | * @param destination the destination address and prefix in an {@link IpPrefix} |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 265 | * @param gateway the {@link InetAddress} to route packets through |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 266 | * |
| 267 | * @hide |
| 268 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 269 | public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway) { |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 270 | this(destination, gateway, null); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @hide |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 275 | * |
| 276 | * TODO: Remove this. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 277 | */ |
Mathew Inwood | 0b8f861 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 278 | @UnsupportedAppUsage |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 279 | public RouteInfo(@Nullable LinkAddress destination, @Nullable InetAddress gateway) { |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 280 | this(destination, gateway, null); |
| 281 | } |
| 282 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Constructs a default {@code RouteInfo} object. |
| 285 | * |
| 286 | * @param gateway the {@link InetAddress} to route packets through |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 287 | * |
| 288 | * @hide |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 289 | */ |
Mathew Inwood | 0b8f861 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 290 | @UnsupportedAppUsage |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 291 | public RouteInfo(@NonNull InetAddress gateway) { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 292 | this((IpPrefix) null, gateway, null); |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 295 | /** |
| 296 | * Constructs a {@code RouteInfo} object representing a direct connected subnet. |
| 297 | * |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 298 | * @param destination the {@link IpPrefix} describing the address and prefix |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 299 | * length of the subnet. |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 300 | * |
| 301 | * @hide |
| 302 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 303 | public RouteInfo(@NonNull IpPrefix destination) { |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 304 | this(destination, null, null); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @hide |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 309 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 310 | public RouteInfo(@NonNull LinkAddress destination) { |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 311 | this(destination, null, null); |
Robert Greenwalt | e110d00 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 314 | /** |
| 315 | * @hide |
| 316 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 317 | public RouteInfo(@NonNull IpPrefix destination, @RouteType int type) { |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 318 | this(destination, null, null, type); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @hide |
| 323 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 324 | public static RouteInfo makeHostRoute(@NonNull InetAddress host, @Nullable String iface) { |
Lorenzo Colitti | 697e1c0 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 325 | return makeHostRoute(host, null, iface); |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 328 | /** |
| 329 | * @hide |
| 330 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 331 | public static RouteInfo makeHostRoute(@Nullable InetAddress host, @Nullable InetAddress gateway, |
| 332 | @Nullable String iface) { |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 333 | if (host == null) return null; |
| 334 | |
| 335 | if (host instanceof Inet4Address) { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 336 | return new RouteInfo(new IpPrefix(host, 32), gateway, iface); |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 337 | } else { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 338 | return new RouteInfo(new IpPrefix(host, 128), gateway, iface); |
Robert Greenwalt | 355205c | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Mathew Inwood | ac5968e | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 342 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Robert Greenwalt | e8d2a4a | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 343 | private boolean isHost() { |
Lorenzo Colitti | cdc56fe | 2013-03-27 13:07:18 +0900 | [diff] [blame] | 344 | return (mDestination.getAddress() instanceof Inet4Address && |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 345 | mDestination.getPrefixLength() == 32) || |
Lorenzo Colitti | cdc56fe | 2013-03-27 13:07:18 +0900 | [diff] [blame] | 346 | (mDestination.getAddress() instanceof Inet6Address && |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 347 | mDestination.getPrefixLength() == 128); |
Robert Greenwalt | e8d2a4a | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 350 | /** |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 351 | * Retrieves the destination address and prefix length in the form of an {@link IpPrefix}. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 352 | * |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 353 | * @return {@link IpPrefix} specifying the destination. This is never {@code null}. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 354 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 355 | @NonNull |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 356 | public IpPrefix getDestination() { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 357 | return mDestination; |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * TODO: Convert callers to use IpPrefix and then remove. |
| 362 | * @hide |
| 363 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 364 | @NonNull |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 365 | public LinkAddress getDestinationLinkAddress() { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 366 | return new LinkAddress(mDestination.getAddress(), mDestination.getPrefixLength()); |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 369 | /** |
| 370 | * Retrieves the gateway or next hop {@link InetAddress} for this route. |
| 371 | * |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 372 | * @return {@link InetAddress} specifying the gateway or next hop. This may be |
Robert Greenwalt | cccf73a | 2014-06-12 16:24:38 -0700 | [diff] [blame] | 373 | * {@code null} for a directly-connected route." |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 374 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 375 | @Nullable |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 376 | public InetAddress getGateway() { |
| 377 | return mGateway; |
| 378 | } |
| 379 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 380 | /** |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 381 | * Retrieves the interface used for this route if specified, else {@code null}. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 382 | * |
| 383 | * @return The name of the interface used for this route. |
| 384 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 385 | @Nullable |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 386 | public String getInterface() { |
| 387 | return mInterface; |
| 388 | } |
| 389 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 390 | /** |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 391 | * Retrieves the type of this route. |
| 392 | * |
| 393 | * @return The type of this route; one of the {@code RTN_xxx} constants defined in this class. |
| 394 | * |
| 395 | * @hide |
| 396 | */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 397 | @SystemApi |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 398 | @RouteType |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 399 | public int getType() { |
| 400 | return mType; |
| 401 | } |
| 402 | |
| 403 | /** |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 404 | * Retrieves the MTU size for this route. |
| 405 | * |
| 406 | * @return The MTU size, or 0 if it has not been set. |
| 407 | * @hide |
| 408 | */ |
| 409 | @SystemApi |
| 410 | public int getMtu() { |
| 411 | return mMtu; |
| 412 | } |
| 413 | |
| 414 | /** |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 415 | * Indicates if this route is a default route (ie, has no destination specified). |
| 416 | * |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 417 | * @return {@code true} if the destination has a prefix length of 0. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 418 | */ |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 419 | public boolean isDefaultRoute() { |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 420 | return mType == RTN_UNICAST && mDestination.getPrefixLength() == 0; |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
Rubin Xu | fa42901 | 2020-03-30 14:37:05 +0100 | [diff] [blame] | 424 | * Indicates if this route is an unreachable default route. |
| 425 | * |
| 426 | * @return {@code true} if it's an unreachable route with prefix length of 0. |
| 427 | * @hide |
| 428 | */ |
| 429 | private boolean isUnreachableDefaultRoute() { |
| 430 | return mType == RTN_UNREACHABLE && mDestination.getPrefixLength() == 0; |
| 431 | } |
| 432 | |
| 433 | /** |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 434 | * Indicates if this route is an IPv4 default route. |
| 435 | * @hide |
| 436 | */ |
| 437 | public boolean isIPv4Default() { |
| 438 | return isDefaultRoute() && mDestination.getAddress() instanceof Inet4Address; |
| 439 | } |
| 440 | |
| 441 | /** |
Rubin Xu | fa42901 | 2020-03-30 14:37:05 +0100 | [diff] [blame] | 442 | * Indicates if this route is an IPv4 unreachable default route. |
| 443 | * @hide |
| 444 | */ |
| 445 | public boolean isIPv4UnreachableDefault() { |
| 446 | return isUnreachableDefaultRoute() && mDestination.getAddress() instanceof Inet4Address; |
| 447 | } |
| 448 | |
| 449 | /** |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 450 | * Indicates if this route is an IPv6 default route. |
| 451 | * @hide |
| 452 | */ |
| 453 | public boolean isIPv6Default() { |
| 454 | return isDefaultRoute() && mDestination.getAddress() instanceof Inet6Address; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 457 | /** |
Rubin Xu | fa42901 | 2020-03-30 14:37:05 +0100 | [diff] [blame] | 458 | * Indicates if this route is an IPv6 unreachable default route. |
| 459 | * @hide |
| 460 | */ |
| 461 | public boolean isIPv6UnreachableDefault() { |
| 462 | return isUnreachableDefaultRoute() && mDestination.getAddress() instanceof Inet6Address; |
| 463 | } |
| 464 | |
| 465 | /** |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 466 | * Indicates if this route is a host route (ie, matches only a single host address). |
| 467 | * |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 468 | * @return {@code true} if the destination has a prefix length of 32 or 128 for IPv4 or IPv6, |
| 469 | * respectively. |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 470 | * @hide |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 471 | */ |
Robert Greenwalt | e8d2a4a | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 472 | public boolean isHostRoute() { |
| 473 | return mIsHost; |
| 474 | } |
| 475 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 476 | /** |
| 477 | * Indicates if this route has a next hop ({@code true}) or is directly-connected |
| 478 | * ({@code false}). |
| 479 | * |
| 480 | * @return {@code true} if a gateway is specified |
| 481 | */ |
Robert Greenwalt | 24bc534 | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 482 | public boolean hasGateway() { |
| 483 | return mHasGateway; |
| 484 | } |
| 485 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 486 | /** |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 487 | * Determines whether the destination and prefix of this route includes the specified |
| 488 | * address. |
| 489 | * |
| 490 | * @param destination A {@link InetAddress} to test to see if it would match this route. |
| 491 | * @return {@code true} if the destination and prefix length cover the given address. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 492 | */ |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 493 | public boolean matches(InetAddress destination) { |
Erik Kline | 8a100e3 | 2015-04-13 15:33:34 +0900 | [diff] [blame] | 494 | return mDestination.contains(destination); |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Find the route from a Collection of routes that best matches a given address. |
| 499 | * May return null if no routes are applicable. |
| 500 | * @param routes a Collection of RouteInfos to chose from |
| 501 | * @param dest the InetAddress your trying to get to |
| 502 | * @return the RouteInfo from the Collection that best fits the given address |
| 503 | * |
| 504 | * @hide |
| 505 | */ |
Mathew Inwood | 5a09a71 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 506 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 507 | @Nullable |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 508 | public static RouteInfo selectBestRoute(Collection<RouteInfo> routes, InetAddress dest) { |
Aaron Huang | 2a21ea7 | 2019-12-17 00:33:18 +0800 | [diff] [blame] | 509 | return NetUtils.selectBestRoute(routes, dest); |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 512 | /** |
| 513 | * Returns a human-readable description of this object. |
| 514 | */ |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 515 | public String toString() { |
| 516 | String val = ""; |
| 517 | if (mDestination != null) val = mDestination.toString(); |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 518 | if (mType == RTN_UNREACHABLE) { |
| 519 | val += " unreachable"; |
| 520 | } else if (mType == RTN_THROW) { |
| 521 | val += " throw"; |
| 522 | } else { |
| 523 | val += " ->"; |
| 524 | if (mGateway != null) val += " " + mGateway.getHostAddress(); |
| 525 | if (mInterface != null) val += " " + mInterface; |
| 526 | if (mType != RTN_UNICAST) { |
| 527 | val += " unknown type " + mType; |
| 528 | } |
| 529 | } |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 530 | val += " mtu " + mMtu; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 531 | return val; |
| 532 | } |
| 533 | |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 534 | /** |
| 535 | * Compares this RouteInfo object against the specified object and indicates if they are equal. |
| 536 | * @return {@code true} if the objects are equal, {@code false} otherwise. |
| 537 | */ |
Roman Kalukiewicz | 1f69a5e | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 538 | public boolean equals(@Nullable Object obj) { |
Wink Saville | f23fb20 | 2011-05-18 15:59:04 -0700 | [diff] [blame] | 539 | if (this == obj) return true; |
| 540 | |
| 541 | if (!(obj instanceof RouteInfo)) return false; |
| 542 | |
| 543 | RouteInfo target = (RouteInfo) obj; |
| 544 | |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 545 | return Objects.equals(mDestination, target.getDestination()) && |
Robert Greenwalt | 791a40d | 2014-05-18 23:33:07 -0700 | [diff] [blame] | 546 | Objects.equals(mGateway, target.getGateway()) && |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 547 | Objects.equals(mInterface, target.getInterface()) && |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 548 | mType == target.getType() && mMtu == target.getMtu(); |
Wink Saville | f23fb20 | 2011-05-18 15:59:04 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 551 | /** |
Rubin Xu | ad524de | 2020-06-11 16:40:13 +0100 | [diff] [blame] | 552 | * A helper class that contains the destination, the gateway and the interface in a |
| 553 | * {@code RouteInfo}, used by {@link ConnectivityService#updateRoutes} or |
junyulai | 21c26da | 2020-03-23 20:49:43 +0800 | [diff] [blame] | 554 | * {@link LinkProperties#addRoute} to calculate the list to be updated. |
Rubin Xu | ad524de | 2020-06-11 16:40:13 +0100 | [diff] [blame] | 555 | * {@code RouteInfo} objects with different interfaces are treated as different routes because |
| 556 | * *usually* on Android different interfaces use different routing tables, and moving a route |
| 557 | * to a new routing table never constitutes an update, but is always a remove and an add. |
Tyler Wear | a8978d4 | 2019-12-05 14:55:30 -0800 | [diff] [blame] | 558 | * |
| 559 | * @hide |
| 560 | */ |
Rubin Xu | ad524de | 2020-06-11 16:40:13 +0100 | [diff] [blame] | 561 | public static class RouteKey { |
| 562 | @NonNull private final IpPrefix mDestination; |
| 563 | @Nullable private final InetAddress mGateway; |
| 564 | @Nullable private final String mInterface; |
| 565 | |
| 566 | RouteKey(@NonNull IpPrefix destination, @Nullable InetAddress gateway, |
| 567 | @Nullable String iface) { |
| 568 | mDestination = destination; |
| 569 | mGateway = gateway; |
| 570 | mInterface = iface; |
| 571 | } |
| 572 | |
| 573 | @Override |
Roman Kalukiewicz | 1f69a5e | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 574 | public boolean equals(@Nullable Object o) { |
Rubin Xu | ad524de | 2020-06-11 16:40:13 +0100 | [diff] [blame] | 575 | if (!(o instanceof RouteKey)) { |
| 576 | return false; |
| 577 | } |
| 578 | RouteKey p = (RouteKey) o; |
| 579 | // No need to do anything special for scoped addresses. Inet6Address#equals does not |
| 580 | // consider the scope ID, but the netd route IPCs (e.g., INetd#networkAddRouteParcel) |
| 581 | // and the kernel ignore scoped addresses both in the prefix and in the nexthop and only |
| 582 | // look at RTA_OIF. |
| 583 | return Objects.equals(p.mDestination, mDestination) |
| 584 | && Objects.equals(p.mGateway, mGateway) |
| 585 | && Objects.equals(p.mInterface, mInterface); |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public int hashCode() { |
| 590 | return Objects.hash(mDestination, mGateway, mInterface); |
Tyler Wear | a8978d4 | 2019-12-05 14:55:30 -0800 | [diff] [blame] | 591 | } |
junyulai | 21c26da | 2020-03-23 20:49:43 +0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Get {@code RouteKey} of this {@code RouteInfo}. |
| 596 | * @return a {@code RouteKey} object. |
| 597 | * |
| 598 | * @hide |
| 599 | */ |
| 600 | @NonNull |
| 601 | public RouteKey getRouteKey() { |
Rubin Xu | ad524de | 2020-06-11 16:40:13 +0100 | [diff] [blame] | 602 | return new RouteKey(mDestination, mGateway, mInterface); |
Tyler Wear | a8978d4 | 2019-12-05 14:55:30 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
Sreeram Ramachandran | 8162a5e | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 606 | * Returns a hashcode for this <code>RouteInfo</code> object. |
| 607 | */ |
Wink Saville | f23fb20 | 2011-05-18 15:59:04 -0700 | [diff] [blame] | 608 | public int hashCode() { |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 609 | return (mDestination.hashCode() * 41) |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 610 | + (mGateway == null ? 0 :mGateway.hashCode() * 47) |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 611 | + (mInterface == null ? 0 :mInterface.hashCode() * 67) |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 612 | + (mType * 71) + (mMtu * 89); |
Wink Saville | f23fb20 | 2011-05-18 15:59:04 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 615 | /** |
| 616 | * Implement the Parcelable interface |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 617 | */ |
| 618 | public int describeContents() { |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Implement the Parcelable interface |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 624 | */ |
| 625 | public void writeToParcel(Parcel dest, int flags) { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 626 | dest.writeParcelable(mDestination, flags); |
| 627 | byte[] gatewayBytes = (mGateway == null) ? null : mGateway.getAddress(); |
| 628 | dest.writeByteArray(gatewayBytes); |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 629 | dest.writeString(mInterface); |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 630 | dest.writeInt(mType); |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 631 | dest.writeInt(mMtu); |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Implement the Parcelable interface. |
Robert Greenwalt | 8b57c41 | 2014-05-18 12:05:05 -0700 | [diff] [blame] | 636 | */ |
Jeff Sharkey | 9286f91 | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 637 | public static final @android.annotation.NonNull Creator<RouteInfo> CREATOR = |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 638 | new Creator<RouteInfo>() { |
| 639 | public RouteInfo createFromParcel(Parcel in) { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 640 | IpPrefix dest = in.readParcelable(null); |
| 641 | |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 642 | InetAddress gateway = null; |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 643 | byte[] addr = in.createByteArray(); |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 644 | try { |
Lorenzo Colitti | 4a0ff02 | 2014-08-12 12:37:50 +0900 | [diff] [blame] | 645 | gateway = InetAddress.getByAddress(addr); |
Lorenzo Colitti | 1b72e05 | 2014-06-23 21:27:53 +0900 | [diff] [blame] | 646 | } catch (UnknownHostException e) {} |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 647 | |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 648 | String iface = in.readString(); |
Lorenzo Colitti | 8eac7b3 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 649 | int type = in.readInt(); |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 650 | int mtu = in.readInt(); |
Lorenzo Colitti | 0580598 | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 651 | |
Sarah Chin | 707c0e9 | 2020-01-16 11:19:52 -0800 | [diff] [blame] | 652 | return new RouteInfo(dest, gateway, iface, type, mtu); |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | public RouteInfo[] newArray(int size) { |
| 656 | return new RouteInfo[size]; |
| 657 | } |
| 658 | }; |
Robert Greenwalt | 5a90129 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 659 | } |