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