blob: 3f03a2a875d5106ce24c837a3609157cd84210ae [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
22import java.net.InetAddress;
23import java.net.InterfaceAddress;
24import java.net.UnknownHostException;
25
26/**
27 * Identifies an address of a network link
28 * @hide
29 */
30public class LinkAddress implements Parcelable {
31 /**
32 * IPv4 or IPv6 address.
33 */
34 private final InetAddress address;
35
36 /**
Irfan Sheriff96ca9172010-10-05 16:12:25 -070037 * Network prefix length
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070038 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -070039 private final int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070040
41 public LinkAddress(InetAddress address, InetAddress mask) {
42 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -070043 this.prefixLength = computeprefixLength(mask);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070044 }
45
Irfan Sheriff96ca9172010-10-05 16:12:25 -070046 public LinkAddress(InetAddress address, int prefixLength) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070047 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -070048 this.prefixLength = prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070049 }
50
51 public LinkAddress(InterfaceAddress interfaceAddress) {
52 this.address = interfaceAddress.getAddress();
Irfan Sheriff96ca9172010-10-05 16:12:25 -070053 this.prefixLength = interfaceAddress.getNetworkPrefixLength();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070054 }
55
Irfan Sheriff96ca9172010-10-05 16:12:25 -070056 private static int computeprefixLength(InetAddress mask) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070057 int count = 0;
58 for (byte b : mask.getAddress()) {
59 for (int i = 0; i < 8; ++i) {
60 if ((b & (1 << i)) != 0) {
61 ++count;
62 }
63 }
64 }
65 return count;
66 }
67
68 @Override
69 public String toString() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -070070 return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength));
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070071 }
72
73 /**
74 * Compares this {@code LinkAddress} instance against the specified address
Irfan Sheriff96ca9172010-10-05 16:12:25 -070075 * in {@code obj}. Two addresses are equal if their InetAddress and prefixLength
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070076 * are equal
77 *
78 * @param obj the object to be tested for equality.
79 * @return {@code true} if both objects are equal, {@code false} otherwise.
80 */
81 @Override
82 public boolean equals(Object obj) {
83 if (!(obj instanceof LinkAddress)) {
84 return false;
85 }
86 LinkAddress linkAddress = (LinkAddress) obj;
87 return this.address.equals(linkAddress.address) &&
Irfan Sheriff96ca9172010-10-05 16:12:25 -070088 this.prefixLength == linkAddress.prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070089 }
90
91 /**
92 * Returns the InetAddress for this address.
93 */
94 public InetAddress getAddress() {
95 return address;
96 }
97
98 /**
99 * Get network prefix length
100 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700101 public int getNetworkPrefixLength() {
102 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700103 }
104
105 /**
106 * Implement the Parcelable interface
107 * @hide
108 */
109 public int describeContents() {
110 return 0;
111 }
112
113 /**
114 * Implement the Parcelable interface.
115 * @hide
116 */
117 public void writeToParcel(Parcel dest, int flags) {
118 if (address != null) {
119 dest.writeByte((byte)1);
120 dest.writeByteArray(address.getAddress());
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700121 dest.writeInt(prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700122 } else {
123 dest.writeByte((byte)0);
124 }
125 }
126
127 /**
128 * Implement the Parcelable interface.
129 * @hide
130 */
131 public static final Creator<LinkAddress> CREATOR =
132 new Creator<LinkAddress>() {
133 public LinkAddress createFromParcel(Parcel in) {
134 InetAddress address = null;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700135 int prefixLength = 0;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700136 if (in.readByte() == 1) {
137 try {
138 address = InetAddress.getByAddress(in.createByteArray());
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700139 prefixLength = in.readInt();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700140 } catch (UnknownHostException e) { }
141 }
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700142 return new LinkAddress(address, prefixLength);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700143 }
144
145 public LinkAddress[] newArray(int size) {
146 return new LinkAddress[size];
147 }
148 };
149}