blob: f6a114c83402d7d675e4e553156d262c21e9b036 [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/**
28 * Identifies an address of a network link
29 * @hide
30 */
31public class LinkAddress implements Parcelable {
32 /**
33 * IPv4 or IPv6 address.
34 */
35 private final InetAddress address;
36
37 /**
Irfan Sheriff96ca9172010-10-05 16:12:25 -070038 * Network prefix length
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070039 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -070040 private final int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070041
Irfan Sheriff96ca9172010-10-05 16:12:25 -070042 public LinkAddress(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 +
47 prefixLength);
48 }
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
53 public LinkAddress(InterfaceAddress interfaceAddress) {
54 this.address = interfaceAddress.getAddress();
Irfan Sheriff96ca9172010-10-05 16:12:25 -070055 this.prefixLength = interfaceAddress.getNetworkPrefixLength();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070056 }
57
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070058 @Override
59 public String toString() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -070060 return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength));
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070061 }
62
63 /**
64 * Compares this {@code LinkAddress} instance against the specified address
Irfan Sheriff96ca9172010-10-05 16:12:25 -070065 * in {@code obj}. Two addresses are equal if their InetAddress and prefixLength
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070066 * are equal
67 *
68 * @param obj the object to be tested for equality.
69 * @return {@code true} if both objects are equal, {@code false} otherwise.
70 */
71 @Override
72 public boolean equals(Object obj) {
73 if (!(obj instanceof LinkAddress)) {
74 return false;
75 }
76 LinkAddress linkAddress = (LinkAddress) obj;
77 return this.address.equals(linkAddress.address) &&
Irfan Sheriff96ca9172010-10-05 16:12:25 -070078 this.prefixLength == linkAddress.prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070079 }
80
John Wang4e900092011-04-04 12:35:42 -070081 @Override
82 /*
83 * generate hashcode based on significant fields
84 */
85 public int hashCode() {
86 return ((null == address) ? 0 : address.hashCode()) + prefixLength;
87 }
88
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070089 /**
90 * Returns the InetAddress for this address.
91 */
92 public InetAddress getAddress() {
93 return address;
94 }
95
96 /**
97 * Get network prefix length
98 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -070099 public int getNetworkPrefixLength() {
100 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700101 }
102
103 /**
104 * Implement the Parcelable interface
105 * @hide
106 */
107 public int describeContents() {
108 return 0;
109 }
110
111 /**
112 * Implement the Parcelable interface.
113 * @hide
114 */
115 public void writeToParcel(Parcel dest, int flags) {
116 if (address != null) {
117 dest.writeByte((byte)1);
118 dest.writeByteArray(address.getAddress());
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700119 dest.writeInt(prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700120 } else {
121 dest.writeByte((byte)0);
122 }
123 }
124
125 /**
126 * Implement the Parcelable interface.
127 * @hide
128 */
129 public static final Creator<LinkAddress> CREATOR =
130 new Creator<LinkAddress>() {
131 public LinkAddress createFromParcel(Parcel in) {
132 InetAddress address = null;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700133 int prefixLength = 0;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700134 if (in.readByte() == 1) {
135 try {
136 address = InetAddress.getByAddress(in.createByteArray());
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700137 prefixLength = in.readInt();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700138 } catch (UnknownHostException e) { }
139 }
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700140 return new LinkAddress(address, prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700141 }
142
143 public LinkAddress[] newArray(int size) {
144 return new LinkAddress[size];
145 }
146 };
147}