Inet4Address.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Inet4Address.java --
  2. Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.net;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.ObjectStreamException;
  34. /*
  35. * Written using on-line Java Platform 1.4 API Specification and
  36. * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt),
  37. * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt),
  38. * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt)
  39. *
  40. * @author Michael Koch
  41. * @status Believed complete and correct.
  42. */
  43. public final class Inet4Address extends InetAddress
  44. {
  45. /**
  46. * For compatability with Sun's JDK 1.4.2 rev. 5
  47. */
  48. static final long serialVersionUID = 3286316764910316507L;
  49. /**
  50. * The address family of these addresses (used for serialization).
  51. */
  52. private static final int AF_INET = 2;
  53. /**
  54. * Inet4Address objects are serialized as InetAddress objects.
  55. */
  56. private Object writeReplace() throws ObjectStreamException
  57. {
  58. return new InetAddress(addr, hostName, AF_INET);
  59. }
  60. /**
  61. * Initializes this object's addr instance variable from the passed in
  62. * byte array. Note that this constructor is protected and is called
  63. * only by static methods in this class.
  64. *
  65. * @param addr The IP number of this address as an array of bytes
  66. * @param host The hostname of this IP address.
  67. */
  68. Inet4Address(byte[] addr, String host)
  69. {
  70. super(addr, host, AF_INET);
  71. }
  72. /**
  73. * Checks if the address is a multicast address
  74. *
  75. * @since 1.1
  76. */
  77. public boolean isMulticastAddress()
  78. {
  79. return (addr[0] & 0xf0) == 0xe0;
  80. }
  81. /**
  82. * Checks if this address is a loopback address
  83. */
  84. public boolean isLoopbackAddress()
  85. {
  86. return (addr[0] & 0xff) == 0x7f;
  87. }
  88. /**
  89. * Checks if this address is a wildcard address
  90. *
  91. * @since 1.4
  92. */
  93. public boolean isAnyLocalAddress()
  94. {
  95. return equals(InetAddress.ANY_IF);
  96. }
  97. /**
  98. * Checks if this address is a link local address
  99. *
  100. * @since 1.4
  101. */
  102. public boolean isLinkLocalAddress()
  103. {
  104. return false;
  105. }
  106. /**
  107. * Checks if this address is a site local address
  108. *
  109. * @since 1.4
  110. */
  111. public boolean isSiteLocalAddress()
  112. {
  113. // 10.0.0.0/8
  114. if ((addr[0] & 0xff) == 0x0a)
  115. return true;
  116. // 172.16.0.0/12
  117. if ((addr[0] & 0xff) == 0xac && (addr[1] & 0xf0) == 0x10)
  118. return true;
  119. // 192.168.0.0/16
  120. if ((addr[0] & 0xff) == 0xc0 && (addr[1] & 0xff) == 0xa8)
  121. return true;
  122. return false;
  123. }
  124. /**
  125. * Checks if this multicast address has global scope
  126. *
  127. * @since 1.4
  128. */
  129. public boolean isMCGlobal()
  130. {
  131. return false;
  132. }
  133. /**
  134. * Checks if this multicast address has node scope
  135. *
  136. * @since 1.4
  137. */
  138. public boolean isMCNodeLocal()
  139. {
  140. return false;
  141. }
  142. /**
  143. * Checks if this multicast address has link scope
  144. *
  145. * @since 1.4
  146. */
  147. public boolean isMCLinkLocal()
  148. {
  149. if (! isMulticastAddress())
  150. return false;
  151. return ((addr[0] & 0xff) == 0xe0
  152. && (addr[1] & 0xff) == 0x00
  153. && (addr[2] & 0xff) == 0x00);
  154. }
  155. /**
  156. * Checks if this multicast address has site scope
  157. *
  158. * @since 1.4
  159. */
  160. public boolean isMCSiteLocal()
  161. {
  162. return false;
  163. }
  164. /**
  165. * Checks if this multicast address has organization scope
  166. *
  167. * @since 1.4
  168. */
  169. public boolean isMCOrgLocal()
  170. {
  171. return false;
  172. }
  173. /**
  174. * Returns the address of the current instance
  175. */
  176. public byte[] getAddress()
  177. {
  178. return (byte[]) addr.clone();
  179. }
  180. /**
  181. * Returns the address as string
  182. *
  183. * @since 1.0.2
  184. */
  185. public String getHostAddress()
  186. {
  187. CPStringBuilder sb = new CPStringBuilder(40);
  188. int len = addr.length;
  189. int i = 0;
  190. for ( ; ; )
  191. {
  192. sb.append(addr[i] & 0xff);
  193. i++;
  194. if (i == len)
  195. break;
  196. sb.append('.');
  197. }
  198. return sb.toString();
  199. }
  200. /**
  201. * Computes the hashcode of the instance
  202. */
  203. public int hashCode()
  204. {
  205. int hash = 0;
  206. int len = addr.length;
  207. int i = len > 4 ? len - 4 : 0;
  208. for (; i < len; i++)
  209. hash = (hash << 8) | (addr[i] & 0xFF);
  210. return hash;
  211. }
  212. /**
  213. * Compare the current Inet4Address instance with obj
  214. *
  215. * @param obj Object to compare with
  216. */
  217. public boolean equals(Object obj)
  218. {
  219. if (! (obj instanceof InetAddress))
  220. return false;
  221. byte[] addr1 = addr;
  222. byte[] addr2 = ((InetAddress) obj).addr;
  223. if (addr1.length != addr2.length)
  224. return false;
  225. for (int i = addr1.length; --i >= 0;)
  226. if (addr1[i] != addr2[i])
  227. return false;
  228. return true;
  229. }
  230. }