Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 22 | import java.net.Inet4Address; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 23 | import java.net.InetAddress; |
| 24 | import java.net.InterfaceAddress; |
| 25 | import java.net.UnknownHostException; |
| 26 | |
| 27 | /** |
| 28 | * Identifies an address of a network link |
| 29 | * @hide |
| 30 | */ |
| 31 | public class LinkAddress implements Parcelable { |
| 32 | /** |
| 33 | * IPv4 or IPv6 address. |
| 34 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame^] | 35 | private InetAddress address; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 38 | * Network prefix length |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 39 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame^] | 40 | private int prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 41 | |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame^] | 42 | private void init(InetAddress address, int prefixLength) { |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 43 | if (address == null || prefixLength < 0 || |
| 44 | ((address instanceof Inet4Address) && prefixLength > 32) || |
| 45 | (prefixLength > 128)) { |
| 46 | throw new IllegalArgumentException("Bad LinkAddress params " + address + |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame^] | 47 | "/" + prefixLength); |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 48 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 49 | this.address = address; |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 50 | this.prefixLength = prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame^] | 53 | public LinkAddress(InetAddress address, int prefixLength) { |
| 54 | init(address, prefixLength); |
| 55 | } |
| 56 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 57 | public LinkAddress(InterfaceAddress interfaceAddress) { |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame^] | 58 | init(interfaceAddress.getAddress(), |
| 59 | interfaceAddress.getNetworkPrefixLength()); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or |
| 64 | * "2001:db8::1/64". |
| 65 | * @param string The string to parse. |
| 66 | */ |
| 67 | public LinkAddress(String address) { |
| 68 | InetAddress inetAddress = null; |
| 69 | int prefixLength = -1; |
| 70 | try { |
| 71 | String [] pieces = address.split("/", 2); |
| 72 | prefixLength = Integer.parseInt(pieces[1]); |
| 73 | inetAddress = InetAddress.parseNumericAddress(pieces[0]); |
| 74 | } catch (NullPointerException e) { // Null string. |
| 75 | } catch (ArrayIndexOutOfBoundsException e) { // No prefix length. |
| 76 | } catch (NumberFormatException e) { // Non-numeric prefix. |
| 77 | } catch (IllegalArgumentException e) { // Invalid IP address. |
| 78 | } |
| 79 | |
| 80 | if (inetAddress == null || prefixLength == -1) { |
| 81 | throw new IllegalArgumentException("Bad LinkAddress params " + address); |
| 82 | } |
| 83 | |
| 84 | init(inetAddress, prefixLength); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public String toString() { |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 89 | return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength)); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Compares this {@code LinkAddress} instance against the specified address |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 94 | * in {@code obj}. Two addresses are equal if their InetAddress and prefixLength |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 95 | * are equal |
| 96 | * |
| 97 | * @param obj the object to be tested for equality. |
| 98 | * @return {@code true} if both objects are equal, {@code false} otherwise. |
| 99 | */ |
| 100 | @Override |
| 101 | public boolean equals(Object obj) { |
| 102 | if (!(obj instanceof LinkAddress)) { |
| 103 | return false; |
| 104 | } |
| 105 | LinkAddress linkAddress = (LinkAddress) obj; |
| 106 | return this.address.equals(linkAddress.address) && |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 107 | this.prefixLength == linkAddress.prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 108 | } |
| 109 | |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 110 | @Override |
| 111 | /* |
| 112 | * generate hashcode based on significant fields |
| 113 | */ |
| 114 | public int hashCode() { |
| 115 | return ((null == address) ? 0 : address.hashCode()) + prefixLength; |
| 116 | } |
| 117 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Returns the InetAddress for this address. |
| 120 | */ |
| 121 | public InetAddress getAddress() { |
| 122 | return address; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get network prefix length |
| 127 | */ |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 128 | public int getNetworkPrefixLength() { |
| 129 | return prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Implement the Parcelable interface |
| 134 | * @hide |
| 135 | */ |
| 136 | public int describeContents() { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Implement the Parcelable interface. |
| 142 | * @hide |
| 143 | */ |
| 144 | public void writeToParcel(Parcel dest, int flags) { |
| 145 | if (address != null) { |
| 146 | dest.writeByte((byte)1); |
| 147 | dest.writeByteArray(address.getAddress()); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 148 | dest.writeInt(prefixLength); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 149 | } else { |
| 150 | dest.writeByte((byte)0); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Implement the Parcelable interface. |
| 156 | * @hide |
| 157 | */ |
| 158 | public static final Creator<LinkAddress> CREATOR = |
| 159 | new Creator<LinkAddress>() { |
| 160 | public LinkAddress createFromParcel(Parcel in) { |
| 161 | InetAddress address = null; |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 162 | int prefixLength = 0; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 163 | if (in.readByte() == 1) { |
| 164 | try { |
| 165 | address = InetAddress.getByAddress(in.createByteArray()); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 166 | prefixLength = in.readInt(); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 167 | } catch (UnknownHostException e) { } |
| 168 | } |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 169 | return new LinkAddress(address, prefixLength); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | public LinkAddress[] newArray(int size) { |
| 173 | return new LinkAddress[size]; |
| 174 | } |
| 175 | }; |
| 176 | } |