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