logo

class

inet::IpAddress

sys::Obj
  inet::IpAddress
   1  //
   2  // Copyright (c) 2007, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   9 Feb 07  Brian Frank  Creation
   7  //
   8  
   9  **
  10  ** IpAddress models both IPv4 and IPv6 numeric addresses as well
  11  ** as provide DNS hostname resolution.
  12  **
  13  final class IpAddress
  14  {
  15  
  16  //////////////////////////////////////////////////////////////////////////
  17  // Construction
  18  //////////////////////////////////////////////////////////////////////////
  19  
  20    **
  21    ** Parse an IP address formated as an IPv4 numeric address, IPv6
  22    ** numeric address, or a DNS hostname.  If a hostname if provided,
  23    ** then it is resolved to an IP address potentially blocking the
  24    ** calling thread.  If the address is invalid or a hostname cannot
  25    ** be resolved then UnknownHostErr is thrown.
  26    **
  27    ** Examples:
  28    **   IpAddress.make("169.200.3.103")
  29    **   IpAddress.make("1080:0:0:0:8:800:200C:417A")
  30    **   IpAddress.make("1080::8:800:200C:417A")
  31    **   IpAddress.make("::ffff:129.144.52.38")
  32    **   IpAddress.make("somehost")
  33    **   IpAddress.make("www.acme.com")
  34    **
  35    native static IpAddress make(Str s)
  36  
  37    **
  38    ** Resolve a hostname to all of its configured IP addresses. If a
  39    ** numeric IPv4 or IPv6 address is specified then a list of one
  40    ** IpAddress is returned.  If a hostname if provided, then it is
  41    ** resolved to all its configured IP addresses potentially blocking
  42    ** the calling thread.  If the address is invalid or a hostname
  43    ** cannot be resolved then UnknownHostErr is thrown.
  44    **
  45    native static IpAddress[] makeAll(Str s)
  46  
  47    **
  48    ** Make an IpAddress for the specified raw bytes.  The size of
  49    ** the byte buffer must be 4 for IPv4 or 16 for IPv6, otherwise
  50    ** ArgErr is thrown.
  51    **
  52    native static IpAddress makeBytes(Buf bytes)
  53  
  54    **
  55    ** Return the IpAddress for the local machine.
  56    **
  57    native static IpAddress local()
  58  
  59    **
  60    ** Private constructor.
  61    **
  62    internal new internalMake() {}
  63  
  64  //////////////////////////////////////////////////////////////////////////
  65  // Identity
  66  //////////////////////////////////////////////////////////////////////////
  67  
  68    **
  69    ** Hash code is based the address bytes.
  70    **
  71    override native Int hash()
  72  
  73    **
  74    ** Equality is based on equivalent address bytes.
  75    **
  76    override native Bool equals(Obj obj)
  77  
  78    **
  79    ** Return the exact string passed to the constructor.
  80    **
  81    override native Str toStr()
  82  
  83  //////////////////////////////////////////////////////////////////////////
  84  // Methods
  85  //////////////////////////////////////////////////////////////////////////
  86  
  87    **
  88    ** Is this a 32 bit (four byte) IP version 4 address.
  89    **
  90    native Bool isIPv4()
  91  
  92    **
  93    ** Is this a 128 bit (sixteen byte) IP version 6 address.
  94    **
  95    native Bool isIPv6()
  96  
  97    **
  98    ** Get the raw bytes of this address as a Buf of 4 or 16 bytes
  99    ** for IPv4 or IPv6 respectively.  The buf position is zero.
 100    **
 101    native Buf bytes()
 102  
 103    **
 104    ** Get this address as a Str in its numeric notation.  For IPv4
 105    ** this is four decimal digits separated by dots.  For IPv6 this
 106    ** is eight hexadecimal digits separated by colons.
 107    **
 108    native Str numeric()
 109  
 110    **
 111    ** Return the hostname of this address.  If a hostname was specified
 112    ** in make, then that string is used.  Otherwise this method will perform
 113    ** a reverse DNS lookup potentially blocking the calling thread.  If
 114    ** the address cannot be mapped to a hostname, then return the address
 115    ** in its numeric format.
 116    **
 117    native Str hostname()
 118  
 119  }