/src/org/jgroups/stack/IpAddress.java

https://github.com/vasilev/JGroups · Java · 226 lines · 156 code · 50 blank · 20 comment · 46 complexity · 007d7e1360e7b45cff1fbc6eed8e6a15 MD5 · raw file

  1. package org.jgroups.stack;
  2. import org.jgroups.logging.Log;
  3. import org.jgroups.logging.LogFactory;
  4. import org.jgroups.Address;
  5. import org.jgroups.Global;
  6. import org.jgroups.PhysicalAddress;
  7. import org.jgroups.util.Util;
  8. import java.io.*;
  9. import java.net.InetAddress;
  10. import java.net.InetSocketAddress;
  11. import java.net.UnknownHostException;
  12. import java.net.Inet6Address;
  13. /**
  14. * Network-dependent address (Internet). Generated by the bottommost layer of the protocol
  15. * stack (UDP). Contains an InetAddress and port.
  16. * @author Bela Ban
  17. */
  18. public class IpAddress implements PhysicalAddress {
  19. private InetAddress ip_addr=null;
  20. private int port=0;
  21. protected static final Log log=LogFactory.getLog(IpAddress.class);
  22. static boolean resolve_dns=false;
  23. protected int size=-1;
  24. static {
  25. /* Trying to get value of resolve_dns. PropertyPermission not granted if
  26. * running in an untrusted environment with JNLP */
  27. try {
  28. String tmp=Util.getProperty(new String[]{Global.RESOLVE_DNS, "resolve.dns"}, null, null, false, "false");
  29. resolve_dns=Boolean.valueOf(tmp).booleanValue();
  30. }
  31. catch (SecurityException ex){
  32. resolve_dns=false;
  33. }
  34. }
  35. // Used only by Externalization
  36. public IpAddress() {
  37. }
  38. public IpAddress(String i, int p) throws UnknownHostException {
  39. port=p;
  40. ip_addr=InetAddress.getByName(i);
  41. }
  42. public IpAddress(InetAddress i, int p) {
  43. ip_addr=i; port=p;
  44. if(this.ip_addr == null)
  45. setAddressToLocalHost();
  46. }
  47. private void setAddressToLocalHost() {
  48. try {
  49. ip_addr=InetAddress.getLocalHost(); // get first NIC found (on multi-homed systems)
  50. // size=size();
  51. }
  52. catch(Exception e) {
  53. if(log.isWarnEnabled()) log.warn("exception: " + e);
  54. }
  55. }
  56. public IpAddress(int port) {
  57. this(port, true);
  58. }
  59. public IpAddress(int port, boolean set_default_host) {
  60. this.port=port;
  61. if(set_default_host)
  62. setAddressToLocalHost();
  63. }
  64. public IpAddress(InetSocketAddress sock_addr) {
  65. port=sock_addr.getPort();
  66. ip_addr=sock_addr.getAddress();
  67. }
  68. public final InetAddress getIpAddress() {return ip_addr;}
  69. public final int getPort() {return port;}
  70. /**
  71. * implements the java.lang.Comparable interface
  72. * @see java.lang.Comparable
  73. * @param o - the Object to be compared
  74. * @return a negative integer, zero, or a positive integer as this object is less than,
  75. * equal to, or greater than the specified object.
  76. * @exception java.lang.ClassCastException - if the specified object's type prevents it
  77. * from being compared to this Object.
  78. */
  79. public final int compareTo(Address o) {
  80. int h1, h2, rc; // added Nov 7 2005, makes sense with canonical addresses
  81. if(this == o) return 0;
  82. if(!(o instanceof IpAddress))
  83. throw new ClassCastException("comparison between different classes: the other object is " +
  84. (o != null? o.getClass() : o));
  85. IpAddress other = (IpAddress) o;
  86. if(ip_addr == null)
  87. if (other.ip_addr == null) return port < other.port ? -1 : (port > other.port ? 1 : 0);
  88. else return -1;
  89. h1=ip_addr.hashCode();
  90. h2=other.ip_addr.hashCode();
  91. rc=h1 < h2? -1 : h1 > h2? 1 : 0;
  92. return rc != 0 ? rc : port < other.port ? -1 : (port > other.port ? 1 : 0);
  93. }
  94. public final boolean equals(Object obj) {
  95. if(this == obj) return true; // added Nov 7 2005, makes sense with canonical addresses
  96. if(!(obj instanceof IpAddress))
  97. return false;
  98. IpAddress other=(IpAddress)obj;
  99. boolean sameIP;
  100. if(this.ip_addr != null)
  101. sameIP=this.ip_addr.equals(other.ip_addr);
  102. else
  103. sameIP=(other.ip_addr == null);
  104. return sameIP && (this.port == other.port);
  105. }
  106. public final int hashCode() {
  107. return ip_addr != null ? ip_addr.hashCode() + port : port;
  108. }
  109. public String toString() {
  110. StringBuilder sb=new StringBuilder();
  111. if(ip_addr == null)
  112. sb.append("<null>");
  113. else {
  114. if(ip_addr.isMulticastAddress())
  115. sb.append(ip_addr.getHostAddress());
  116. else {
  117. String host_name;
  118. if(resolve_dns) {
  119. host_name=ip_addr.getHostName();
  120. }
  121. else {
  122. host_name=ip_addr.getHostAddress();
  123. }
  124. sb.append(host_name);
  125. }
  126. }
  127. sb.append(":").append(port);
  128. return sb.toString();
  129. }
  130. public void writeTo(DataOutput out) throws Exception {
  131. if(ip_addr != null) {
  132. byte[] address=ip_addr.getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6)
  133. out.writeByte(address.length); // 1 byte
  134. out.write(address, 0, address.length);
  135. if(ip_addr instanceof Inet6Address)
  136. out.writeInt(((Inet6Address)ip_addr).getScopeId());
  137. }
  138. else {
  139. out.writeByte(0);
  140. }
  141. out.writeShort(port);
  142. }
  143. public void readFrom(DataInput in) throws Exception {
  144. int len=in.readByte();
  145. if(len > 0 && (len != Global.IPV4_SIZE && len != Global.IPV6_SIZE))
  146. throw new IOException("length has to be " + Global.IPV4_SIZE + " or " + Global.IPV6_SIZE + " bytes (was " +
  147. len + " bytes)");
  148. byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
  149. in.readFully(a);
  150. if(len == Global.IPV6_SIZE) {
  151. int scope_id=in.readInt();
  152. this.ip_addr=Inet6Address.getByAddress(null, a, scope_id);
  153. }
  154. else {
  155. this.ip_addr=InetAddress.getByAddress(a);
  156. }
  157. // changed from readShort(): we need the full 65535, with a short we'd only get up to 32K !
  158. port=in.readUnsignedShort();
  159. }
  160. public int size() {
  161. if(size >= 0)
  162. return size;
  163. // length (1 bytes) + 4 bytes for port
  164. int tmp_size=Global.BYTE_SIZE+ Global.SHORT_SIZE;
  165. if(ip_addr != null) {
  166. tmp_size+=ip_addr.getAddress().length; // 4 bytes for IPv4
  167. if(ip_addr instanceof Inet6Address)
  168. tmp_size+=Global.INT_SIZE;
  169. }
  170. size=tmp_size;
  171. return tmp_size;
  172. }
  173. public IpAddress copy() {
  174. return new IpAddress(ip_addr, port);
  175. }
  176. }