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