blob: 570b6fe5be0e1754e9fd27607d1adccb7ab115ec [file] [log] [blame]
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001/*
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
17package android.net;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
Robert Greenwaltb979f792011-02-11 17:01:02 -080022import java.net.Inet4Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070023import java.net.InetAddress;
24import java.net.InterfaceAddress;
25import java.net.UnknownHostException;
26
27/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090028 * Identifies an IP address on a network link.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070029 * @hide
30 */
31public class LinkAddress implements Parcelable {
32 /**
33 * IPv4 or IPv6 address.
34 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090035 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070036
37 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090038 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070039 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090040 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070041
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090042 private void init(InetAddress address, int prefixLength) {
Robert Greenwaltb979f792011-02-11 17:01:02 -080043 if (address == null || prefixLength < 0 ||
44 ((address instanceof Inet4Address) && prefixLength > 32) ||
45 (prefixLength > 128)) {
46 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090047 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -080048 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070049 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -070050 this.prefixLength = prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070051 }
52
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090053 /**
54 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
55 * @param address The IP address.
56 * @param prefixLength The prefix length.
57 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090058 public LinkAddress(InetAddress address, int prefixLength) {
59 init(address, prefixLength);
60 }
61
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090062 /**
63 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
64 * @param interfaceAddress The interface address.
65 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070066 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090067 init(interfaceAddress.getAddress(),
68 interfaceAddress.getNetworkPrefixLength());
69 }
70
71 /**
72 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
73 * "2001:db8::1/64".
74 * @param string The string to parse.
75 */
76 public LinkAddress(String address) {
77 InetAddress inetAddress = null;
78 int prefixLength = -1;
79 try {
80 String [] pieces = address.split("/", 2);
81 prefixLength = Integer.parseInt(pieces[1]);
82 inetAddress = InetAddress.parseNumericAddress(pieces[0]);
83 } catch (NullPointerException e) { // Null string.
84 } catch (ArrayIndexOutOfBoundsException e) { // No prefix length.
85 } catch (NumberFormatException e) { // Non-numeric prefix.
86 } catch (IllegalArgumentException e) { // Invalid IP address.
87 }
88
89 if (inetAddress == null || prefixLength == -1) {
90 throw new IllegalArgumentException("Bad LinkAddress params " + address);
91 }
92
93 init(inetAddress, prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070094 }
95
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070096 @Override
97 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090098 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070099 }
100
101 /**
102 * Compares this {@code LinkAddress} instance against the specified address
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700103 * in {@code obj}. Two addresses are equal if their InetAddress and prefixLength
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900104 * are equal.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700105 *
106 * @param obj the object to be tested for equality.
107 * @return {@code true} if both objects are equal, {@code false} otherwise.
108 */
109 @Override
110 public boolean equals(Object obj) {
111 if (!(obj instanceof LinkAddress)) {
112 return false;
113 }
114 LinkAddress linkAddress = (LinkAddress) obj;
115 return this.address.equals(linkAddress.address) &&
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700116 this.prefixLength == linkAddress.prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700117 }
118
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900119 /**
120 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700121 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900122 @Override
John Wang4e900092011-04-04 12:35:42 -0700123 public int hashCode() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900124 return address.hashCode() + 11 * prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700125 }
126
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700127 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900128 * Returns the InetAddress of this address.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700129 */
130 public InetAddress getAddress() {
131 return address;
132 }
133
134 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900135 * Returns the prefix length of this address.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700136 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700137 public int getNetworkPrefixLength() {
138 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700139 }
140
141 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900142 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700143 * @hide
144 */
145 public int describeContents() {
146 return 0;
147 }
148
149 /**
150 * Implement the Parcelable interface.
151 * @hide
152 */
153 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900154 dest.writeByteArray(address.getAddress());
155 dest.writeInt(prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700156 }
157
158 /**
159 * Implement the Parcelable interface.
160 * @hide
161 */
162 public static final Creator<LinkAddress> CREATOR =
163 new Creator<LinkAddress>() {
164 public LinkAddress createFromParcel(Parcel in) {
165 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900166 try {
167 address = InetAddress.getByAddress(in.createByteArray());
168 } catch (UnknownHostException e) {
169 // Nothing we can do here. When we call the constructor, we'll throw an
170 // IllegalArgumentException, because a LinkAddress can't have a null
171 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700172 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900173 int prefixLength = in.readInt();
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700174 return new LinkAddress(address, prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700175 }
176
177 public LinkAddress[] newArray(int size) {
178 return new LinkAddress[size];
179 }
180 };
181}