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