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 | */ |
| 35 | private final InetAddress address; |
| 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 | */ |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 40 | private final int prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 41 | |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 42 | public LinkAddress(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 + |
| 47 | prefixLength); |
| 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 | |
| 53 | public LinkAddress(InterfaceAddress interfaceAddress) { |
| 54 | this.address = interfaceAddress.getAddress(); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 55 | this.prefixLength = interfaceAddress.getNetworkPrefixLength(); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 58 | @Override |
| 59 | public String toString() { |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 60 | return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength)); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Compares this {@code LinkAddress} instance against the specified address |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 65 | * in {@code obj}. Two addresses are equal if their InetAddress and prefixLength |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 66 | * 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 Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 78 | this.prefixLength == linkAddress.prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the InetAddress for this address. |
| 83 | */ |
| 84 | public InetAddress getAddress() { |
| 85 | return address; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get network prefix length |
| 90 | */ |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 91 | public int getNetworkPrefixLength() { |
| 92 | return prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Implement the Parcelable interface |
| 97 | * @hide |
| 98 | */ |
| 99 | public int describeContents() { |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Implement the Parcelable interface. |
| 105 | * @hide |
| 106 | */ |
| 107 | public void writeToParcel(Parcel dest, int flags) { |
| 108 | if (address != null) { |
| 109 | dest.writeByte((byte)1); |
| 110 | dest.writeByteArray(address.getAddress()); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 111 | dest.writeInt(prefixLength); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 112 | } else { |
| 113 | dest.writeByte((byte)0); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Implement the Parcelable interface. |
| 119 | * @hide |
| 120 | */ |
| 121 | public static final Creator<LinkAddress> CREATOR = |
| 122 | new Creator<LinkAddress>() { |
| 123 | public LinkAddress createFromParcel(Parcel in) { |
| 124 | InetAddress address = null; |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 125 | int prefixLength = 0; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 126 | if (in.readByte() == 1) { |
| 127 | try { |
| 128 | address = InetAddress.getByAddress(in.createByteArray()); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 129 | prefixLength = in.readInt(); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 130 | } catch (UnknownHostException e) { } |
| 131 | } |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 132 | return new LinkAddress(address, prefixLength); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public LinkAddress[] newArray(int size) { |
| 136 | return new LinkAddress[size]; |
| 137 | } |
| 138 | }; |
| 139 | } |