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