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