|
1 /* |
|
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package java.net; |
|
27 |
|
28 /** |
|
29 * This class represents a Network Interface address. In short it's an |
|
30 * IP address, a subnet mask and a broadcast address when the address is |
|
31 * an IPv4 one. An IP address and a network prefix length in the case |
|
32 * of IPv6 address. |
|
33 * |
|
34 * @see java.net.NetworkInterface |
|
35 * @since 1.6 |
|
36 */ |
|
37 public class InterfaceAddress { |
|
38 private InetAddress address = null; |
|
39 private Inet4Address broadcast = null; |
|
40 private short maskLength = 0; |
|
41 |
|
42 /* |
|
43 * Package private constructor. Can't be built directly, instances are |
|
44 * obtained through the NetworkInterface class. |
|
45 */ |
|
46 InterfaceAddress() { |
|
47 } |
|
48 |
|
49 /** |
|
50 * Returns an <code>InetAddress</code> for this address. |
|
51 * |
|
52 * @return the <code>InetAddress</code> for this address. |
|
53 */ |
|
54 public InetAddress getAddress() { |
|
55 return address; |
|
56 } |
|
57 |
|
58 /** |
|
59 * Returns an <code>InetAddress</code> for the brodcast address |
|
60 * for this InterfaceAddress. |
|
61 * <p> |
|
62 * Only IPv4 networks have broadcast address therefore, in the case |
|
63 * of an IPv6 network, <code>null</code> will be returned. |
|
64 * |
|
65 * @return the <code>InetAddress</code> representing the broadcast |
|
66 * address or <code>null</code> if there is no broadcast address. |
|
67 */ |
|
68 public InetAddress getBroadcast() { |
|
69 return broadcast; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Returns the network prefix length for this address. This is also known |
|
74 * as the subnet mask in the context of IPv4 addresses. |
|
75 * Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0) |
|
76 * or 24 (255.255.255.0). <p> |
|
77 * Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10) |
|
78 * |
|
79 * @return a <code>short</code> representing the prefix length for the |
|
80 * subnet of that address. |
|
81 */ |
|
82 public short getNetworkPrefixLength() { |
|
83 return maskLength; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Compares this object against the specified object. |
|
88 * The result is <code>true</code> if and only if the argument is |
|
89 * not <code>null</code> and it represents the same interface address as |
|
90 * this object. |
|
91 * <p> |
|
92 * Two instances of <code>InterfaceAddress</code> represent the same |
|
93 * address if the InetAddress, the prefix length and the broadcast are |
|
94 * the same for both. |
|
95 * |
|
96 * @param obj the object to compare against. |
|
97 * @return <code>true</code> if the objects are the same; |
|
98 * <code>false</code> otherwise. |
|
99 * @see java.net.InterfaceAddress#hashCode() |
|
100 */ |
|
101 public boolean equals(Object obj) { |
|
102 if (!(obj instanceof InterfaceAddress)) { |
|
103 return false; |
|
104 } |
|
105 InterfaceAddress cmp = (InterfaceAddress) obj; |
|
106 if ((address != null & cmp.address == null) || |
|
107 (!address.equals(cmp.address))) |
|
108 return false; |
|
109 if ((broadcast != null & cmp.broadcast == null) || |
|
110 (!broadcast.equals(cmp.broadcast))) |
|
111 return false; |
|
112 if (maskLength != cmp.maskLength) |
|
113 return false; |
|
114 return true; |
|
115 } |
|
116 |
|
117 /** |
|
118 * Returns a hashcode for this Interface address. |
|
119 * |
|
120 * @return a hash code value for this Interface address. |
|
121 */ |
|
122 public int hashCode() { |
|
123 return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Converts this Interface address to a <code>String</code>. The |
|
128 * string returned is of the form: InetAddress / prefix length [ broadcast address ]. |
|
129 * |
|
130 * @return a string representation of this Interface address. |
|
131 */ |
|
132 public String toString() { |
|
133 return address + "/" + maskLength + " [" + broadcast + "]"; |
|
134 } |
|
135 |
|
136 } |