RouteInfo.java revision aa70f101e08098ed9cb190abe2d7f952561026b8
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 17package android.net; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21 22import java.net.UnknownHostException; 23import java.net.InetAddress; 24import java.net.Inet4Address; 25import java.net.Inet6Address; 26/** 27 * A simple container for route information. 28 * 29 * @hide 30 */ 31public class RouteInfo implements Parcelable { 32 /** 33 * The IP destination address for this route. 34 */ 35 private final LinkAddress mDestination; 36 37 /** 38 * The gateway address for this route. 39 */ 40 private final InetAddress mGateway; 41 42 private final boolean mIsDefault; 43 44 public RouteInfo(LinkAddress destination, InetAddress gateway) { 45 if (destination == null) { 46 try { 47 if ((gateway != null) && (gateway instanceof Inet4Address)) { 48 destination = new LinkAddress(InetAddress.getByName("0.0.0.0"), 32); 49 } else { 50 destination = new LinkAddress(InetAddress.getByName("::0"), 128); 51 } 52 } catch (Exception e) {} 53 } 54 mDestination = destination; 55 mGateway = gateway; 56 mIsDefault = isDefault(); 57 } 58 59 public RouteInfo(InetAddress gateway) { 60 LinkAddress destination = null; 61 try { 62 if ((gateway != null) && (gateway instanceof Inet4Address)) { 63 destination = new LinkAddress(InetAddress.getByName("0.0.0.0"), 32); 64 } else { 65 destination = new LinkAddress(InetAddress.getByName("::0"), 128); 66 } 67 } catch (Exception e) {} 68 mDestination = destination; 69 mGateway = gateway; 70 mIsDefault = isDefault(); 71 } 72 73 private boolean isDefault() { 74 boolean val = false; 75 if (mGateway != null) { 76 if (mGateway instanceof Inet4Address) { 77 val = (mDestination == null || mDestination.getNetworkPrefixLength() == 32); 78 } else { 79 val = (mDestination == null || mDestination.getNetworkPrefixLength() == 128); 80 } 81 } 82 return val; 83 } 84 85 public LinkAddress getDestination() { 86 return mDestination; 87 } 88 89 public InetAddress getGateway() { 90 return mGateway; 91 } 92 93 public boolean isDefaultRoute() { 94 return mIsDefault; 95 } 96 97 public String toString() { 98 String val = ""; 99 if (mDestination != null) val = mDestination.toString(); 100 if (mGateway != null) val += " -> " + mGateway.getHostAddress(); 101 return val; 102 } 103 104 public int describeContents() { 105 return 0; 106 } 107 108 public void writeToParcel(Parcel dest, int flags) { 109 if (mDestination == null) { 110 dest.writeByte((byte) 0); 111 } else { 112 dest.writeByte((byte) 1); 113 dest.writeByteArray(mDestination.getAddress().getAddress()); 114 dest.writeInt(mDestination.getNetworkPrefixLength()); 115 } 116 117 if (mGateway == null) { 118 dest.writeByte((byte) 0); 119 } else { 120 dest.writeByte((byte) 1); 121 dest.writeByteArray(mGateway.getAddress()); 122 } 123 } 124 125 public static final Creator<RouteInfo> CREATOR = 126 new Creator<RouteInfo>() { 127 public RouteInfo createFromParcel(Parcel in) { 128 InetAddress destAddr = null; 129 int prefix = 0; 130 InetAddress gateway = null; 131 132 if (in.readByte() == 1) { 133 byte[] addr = in.createByteArray(); 134 prefix = in.readInt(); 135 136 try { 137 destAddr = InetAddress.getByAddress(addr); 138 } catch (UnknownHostException e) {} 139 } 140 141 if (in.readByte() == 1) { 142 byte[] addr = in.createByteArray(); 143 144 try { 145 gateway = InetAddress.getByAddress(addr); 146 } catch (UnknownHostException e) {} 147 } 148 149 LinkAddress dest = null; 150 151 if (destAddr != null) { 152 dest = new LinkAddress(destAddr, prefix); 153 } 154 155 return new RouteInfo(dest, gateway); 156 } 157 158 public RouteInfo[] newArray(int size) { 159 return new RouteInfo[size]; 160 } 161 }; 162} 163