001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: import java.io.ObjectStreamException;
021:
022: public final class Inet4Address extends InetAddress {
023:
024: private static final long serialVersionUID = 3286316764910316507L;
025:
026: Inet4Address(byte[] address) {
027: ipaddress = address;
028: }
029:
030: Inet4Address(byte[] address, String name) {
031: ipaddress = address;
032: hostName = name;
033: }
034:
035: /**
036: * Answers true if the address is a multicast address. Valid IPv4 multicast
037: * addresses are prefixed with 1110 = 0xE
038: *
039: * @return boolean
040: */
041: @Override
042: public boolean isMulticastAddress() {
043: return (ipaddress[0] & 0xF0) == 0xE0;
044: }
045:
046: /**
047: * Answers if the address is the ANY Address
048: *
049: * @return boolean
050: */
051: @Override
052: public boolean isAnyLocalAddress() {
053: for (int i = 0; i < ipaddress.length; i++) {
054: if (ipaddress[i] != 0) {
055: return false;
056: }
057: }
058: return true;
059: }
060:
061: /**
062: * Answers true if the address is a loopback address. Loopback ipv4
063: * addresses are prefixed with: 011111111 = 127
064: *
065: * @return boolean
066: */
067: @Override
068: public boolean isLoopbackAddress() {
069: return (ipaddress[0] & 255) == 127;
070: }
071:
072: /**
073: * Answers whether this address has link-local scope.
074: *
075: * RFC 3484 Default Address Selection for Internet Protocol version 6 (IPv6)
076: * states IPv4 auto-configuration addresses, prefix 169.254/16, IPv4
077: * loopback addresses, prefix 127/8, are assigned link-local scope.
078: *
079: * @return boolean
080: */
081: @Override
082: public boolean isLinkLocalAddress() {
083: // The reference implementation does not return true for loopback
084: // addresses even though RFC 3484 says to do so
085: return (((ipaddress[0] & 255) == 169) && ((ipaddress[1] & 255) == 254));
086: }
087:
088: /**
089: * Answers whether this address has site-local scope. RFC 3484 Default
090: * Address Selection for Internet Protocol version 6 (IPv6) states IPv4
091: * private addresses, prefixes 10/8, 172.16/12, and 192.168/16, are assigned
092: * site-local scope.
093: *
094: * @return boolean
095: */
096: @Override
097: public boolean isSiteLocalAddress() {
098: return ((ipaddress[0] & 255) == 10)
099: || ((ipaddress[0] & 255) == 172)
100: && (((ipaddress[1] & 255) > 15) && (ipaddress[1] & 255) < 32)
101: || ((ipaddress[0] & 255) == 192)
102: && ((ipaddress[1] & 255) == 168);
103: }
104:
105: /**
106: * Answers true if an address is a global multicast address. Valid MCGlobal
107: * IPv4 addresses are 224.0.1.0 - 238.255.255.255
108: *
109: * @return boolean true, if the address is in the global multicast group,
110: * false otherwise
111: */
112: @Override
113: public boolean isMCGlobal() {
114:
115: // Check if we have a prefix of 1110
116: if (!isMulticastAddress()) {
117: return false;
118: }
119:
120: int address = InetAddress.bytesToInt(ipaddress, 0);
121: /*
122: * Now check the boundaries of the global space if we have an address
123: * that is prefixed by something less than 111000000000000000000001
124: * (fortunately we don't have to worry about sign after shifting 8 bits
125: * right) it is not multicast. ( < 224.0.1.0)
126: */
127: if (address >>> 8 < 0xE00001) {
128: return false;
129: }
130:
131: /*
132: * Now check the high boundary which is prefixed by 11101110 = 0xEE. If
133: * the value is higher than this than it is not MCGlobal ( >
134: * 238.255.255.255 )
135: */
136: if (address >>> 24 > 0xEE) {
137: return false;
138: }
139:
140: return true;
141: }
142:
143: /**
144: * Answers false for all IPv4 addresses. There are no valid IPv4 Node-local
145: * addresses
146: *
147: * @return boolean
148: */
149: @Override
150: public boolean isMCNodeLocal() {
151: return false;
152: }
153:
154: /**
155: * Answers true if the address is a link-local address.The valid range for
156: * IPv4 link-local addresses is: 224.0.0.0 to 239.0.0.255 Hence a mask of
157: * 111000000000000000000000 = 0xE00000
158: *
159: * @return boolean
160: */
161: @Override
162: public boolean isMCLinkLocal() {
163: return InetAddress.bytesToInt(ipaddress, 0) >>> 8 == 0xE00000;
164: }
165:
166: /**
167: * Answers true if the address is a site-local address.The valid range for
168: * IPv4 site-local addresses is: 239.255.0.0 to 239.255.255.255 Hence a mask
169: * of 11101111 11111111 = 0xEFFF.
170: *
171: * @return boolean
172: */
173: @Override
174: public boolean isMCSiteLocal() {
175: return (InetAddress.bytesToInt(ipaddress, 0) >>> 16) == 0xEFFF;
176: }
177:
178: /**
179: * Answers true if the address is a organization-local address. The valid
180: * range for IPv4 org-local addresses is: 239.192.0.0 to 239.195.255.255
181: * Hence masks of 11101111 11000000 to 11101111 11000011 are valid. 0xEFC0
182: * to 0xEFC3
183: *
184: * @return true if org local address, false otherwise
185: */
186: @Override
187: public boolean isMCOrgLocal() {
188: int prefix = InetAddress.bytesToInt(ipaddress, 0) >>> 16;
189: return prefix >= 0xEFC0 && prefix <= 0xEFC3;
190: }
191:
192: /**
193: * Returns a String representation of the IP address.
194: *
195: * @return Host address
196: */
197: @Override
198: public String getHostAddress() {
199: String hostAddress = ""; //$NON-NLS-1$
200: for (int i = 0; i < 4; i++) {
201: hostAddress += ipaddress[i] & 255;
202: if (i != 3) {
203: hostAddress += "."; //$NON-NLS-1$
204: }
205: }
206: return hostAddress;
207: }
208:
209: /**
210: * Overrides the basic hashcode function.
211: *
212: * @return the hash code
213: */
214: @Override
215: public int hashCode() {
216: return InetAddress.bytesToInt(ipaddress, 0);
217: }
218:
219: /**
220: * Returns true if obj is of the same type as the IPv4 address and they have
221: * the same IP address, false otherwise.
222: *
223: * @return true if equal and false otherwise
224: */
225: @Override
226: public boolean equals(Object obj) {
227: return super .equals(obj);
228: }
229:
230: private Object writeReplace() throws ObjectStreamException {
231: return new InetAddress(ipaddress, hostName);
232: }
233: }
|