1/*
2 * Copyright (C) 2009 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 java.net;
18
19/**
20 * Identifies one of a network interface's addresses.
21 * These are passed back from the JNI behind NetworkInterface.getNetworkInterfaces.
22 * Multiple addresses for the same interface are collected together on the Java side.
23 *
24 * @since 1.6
25 */
26public class InterfaceAddress {
27    /**
28     * An IPv4 or IPv6 address.
29     */
30    private final InetAddress address;
31
32    /**
33     * The IPv4 broadcast address, or null for IPv6.
34     */
35    private final InetAddress broadcastAddress;
36
37    private final short prefixLength;
38
39    /**
40     * For IPv4.
41     */
42    InterfaceAddress(Inet4Address address, Inet4Address broadcastAddress, Inet4Address mask) {
43        this.address = address;
44        this.broadcastAddress = broadcastAddress;
45        this.prefixLength = countPrefixLength(mask);
46    }
47
48    /**
49     * For IPv6.
50     */
51    InterfaceAddress(Inet6Address address, short prefixLength) {
52        this.address = address;
53        this.broadcastAddress = null;
54        this.prefixLength = prefixLength;
55    }
56
57    private static short countPrefixLength(Inet4Address mask) {
58        short count = 0;
59        for (byte b : mask.ipaddress) {
60            for (int i = 0; i < 8; ++i) {
61                if ((b & (1 << i)) != 0) {
62                    ++count;
63                }
64            }
65        }
66        return count;
67    }
68
69    /**
70     * Tests whether this object is equal to another one. Returns true if
71     * the address, broadcast address and prefix length are all equal.
72     *
73     * @param obj the object to be compared.
74     * @return true if 'obj' is equal to this InterfaceAddress, false otherwise.
75     */
76    @Override
77    public boolean equals(Object obj) {
78        if (obj == this){
79            return true;
80        }
81        if (!(obj instanceof InterfaceAddress)) {
82            return false;
83        }
84        InterfaceAddress rhs = (InterfaceAddress) obj;
85        return ((address == null) ? rhs.address == null : address.equals(rhs.address)) &&
86                (rhs.prefixLength == prefixLength) &&
87                ((broadcastAddress == null) ? rhs.broadcastAddress == null : broadcastAddress.equals(rhs.broadcastAddress));
88    }
89
90    @Override
91    public int hashCode() {
92        int hashCode = address == null ? 0 : -address.hashCode();
93        hashCode += broadcastAddress == null ? 0 : broadcastAddress.hashCode();
94        hashCode += prefixLength;
95        return hashCode;
96    }
97
98    /**
99     * Returns a string containing this interface's address, prefix length, and broadcast address.
100     * For example: {@code "/172.18.103.112/23 [/172.18.103.255]"} or
101     * {@code "/0:0:0:0:0:0:0:1%1/128 [null]"}.
102     */
103    @Override public String toString() {
104        return address + "/" + prefixLength + " [" + broadcastAddress + "]";
105    }
106
107    /**
108     * Returns the InetAddress for this address.
109     */
110    public InetAddress getAddress() {
111        return address;
112    }
113
114    /**
115     * Returns the subnet-directed broadcast address if this is an IPv4 interface, null otherwise.
116     */
117    public InetAddress getBroadcast() {
118        return broadcastAddress;
119    }
120
121    /**
122     * Returns the network prefix length in bits.
123     * (In IPv4 parlance, this is known as the subnet mask,
124     * but this method applies to IPv6 addresses too.)
125     */
126    public short getNetworkPrefixLength() {
127        return prefixLength;
128    }
129}
130