blob: cb302da56b8f30a7c0c596b2f56ca7439b321565 [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 /**
37 * Network prefix
38 */
39 private final int prefix;
40
41 public LinkAddress(InetAddress address, InetAddress mask) {
42 this.address = address;
43 this.prefix = computeprefix(mask);
44 }
45
46 public LinkAddress(InetAddress address, int prefix) {
47 this.address = address;
48 this.prefix = prefix;
49 }
50
51 public LinkAddress(InterfaceAddress interfaceAddress) {
52 this.address = interfaceAddress.getAddress();
53 this.prefix = interfaceAddress.getNetworkPrefixLength();
54 }
55
56 private static int computeprefix(InetAddress mask) {
57 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() {
70 return (address == null ? "" : (address.getHostAddress() + "/" + prefix));
71 }
72
73 /**
74 * Compares this {@code LinkAddress} instance against the specified address
75 * in {@code obj}. Two addresses are equal if their InetAddress and prefix
76 * 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) &&
88 this.prefix == linkAddress.prefix;
89 }
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 */
101 public int getNetworkPrefix() {
102 return prefix;
103 }
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());
121 dest.writeInt(prefix);
122 } 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;
135 int prefix = 0;
136 if (in.readByte() == 1) {
137 try {
138 address = InetAddress.getByAddress(in.createByteArray());
139 prefix = in.readInt();
140 } catch (UnknownHostException e) { }
141 }
142 return new LinkAddress(address, prefix);
143 }
144
145 public LinkAddress[] newArray(int size) {
146 return new LinkAddress[size];
147 }
148 };
149}