home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 May / PCW596.iso / wtest / clico / sunsoft / java / solaris.lzh / SOLARIS.TAZ / SOLARIS / hotjava / classsrc / net / InetAddress.java < prev    next >
Encoding:
Java Source  |  1995-05-15  |  3.9 KB  |  153 lines

  1. /*
  2.  * @(#)InetAddress.java    1.18 95/05/15 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package net;
  21.  
  22. import java.util.Hashtable;
  23. import java.io.*;
  24. import java.util.Linker;
  25. import net.Socket;
  26.  
  27. public final class InetAddress {
  28.  
  29.     /** Link libnet.so into the runtime -- must be first static routine! */
  30.     static {
  31.     Linker.loadLibrary("net");
  32.     }
  33.  
  34.     /** Cached addresses - our own litle nis, not! */
  35.     static Hashtable        addressCache = new Hashtable();
  36.     public static String    localHostName;
  37.     static InetAddress        unknownAddress = new InetAddress();
  38.     static InetAddress        anyLocalAddress = new InetAddress(null);
  39.  
  40.     static {
  41.     Socket.initialize();
  42.     localHostName = getLocalHostName();
  43.     }
  44.  
  45.     /**
  46.      * Private copy of the hostName. This is declared private so it
  47.      * can be checked securely by the Firewall class.
  48.      */
  49.     private String    pHost;
  50.  
  51.     /** Hostname this address is for - also the key in the
  52.     above hash table. */
  53.     public String   hostName;
  54.  
  55.     /** Address number of this host in network byte order. */
  56.     public int        address;
  57.  
  58.     /* private copy of address */
  59.     private int     pAddress = 0;
  60.  
  61.     /** Address family. */
  62.     int            family;
  63.  
  64.     public static InetAddress getByName(String host) {
  65.     InetAddress addr;
  66.  
  67.     if (host == null) {
  68.         return anyLocalAddress;
  69.     }
  70.  
  71.     if ((addr = (InetAddress) addressCache.get(host)) == null) {
  72.         try {
  73.         addr = new InetAddress(host);
  74.         } catch (UnknownHostException e) {
  75.         addr  = unknownAddress;
  76.         }
  77.         addressCache.put(host, addr);
  78.     }
  79.     if (addr == unknownAddress) {
  80.         throw new UnknownHostException(host);
  81.     }
  82.     return addr;
  83.     }
  84.  
  85.     public static InetAddress getByName(String host, InetAddress DNSHost) {
  86.     InetAddress addr;
  87.  
  88.     if (host == null) {
  89.         return anyLocalAddress;
  90.     }
  91.  
  92.     try {
  93.         addr = new InetAddress(host, DNSHost);
  94.         } catch (UnknownHostException e) {
  95.         addr  = unknownAddress;
  96.         }
  97.         addressCache.put(host, addr);
  98.     if (addr == unknownAddress) {
  99.         throw new UnknownHostException(host);
  100.     }
  101.     return addr;
  102.     }
  103.  
  104.  
  105.     public static native String getLocalHostName();
  106.  
  107.     private static synchronized native void lookup(InetAddress addr);
  108.  
  109.     private static synchronized native void lookupWithDNS(InetAddress addr, 
  110.                     InetAddress DNSServer);
  111.  
  112.     public static synchronized native int getPortByName(String name);
  113.  
  114.     /** Package private constructor for the Socket.accept() method.
  115.     This creates an empty InetAddress, which is filled in by
  116.     the accept() method.  This InetAddress, however, is not
  117.     put in the address cache, since it is not created by name. */
  118.     InetAddress() {}
  119.  
  120.     public InetAddress(String host) {
  121.     pHost = host;
  122.     hostName = host;
  123.     lookup(this);
  124.     pAddress = address;
  125.     }
  126.  
  127.     public InetAddress(String host, InetAddress DNShost) {
  128.     pHost = host;
  129.     hostName = host;
  130.     lookupWithDNS(this, DNShost);
  131.     pAddress = address;
  132.     }
  133.  
  134.     public String toString() {
  135.     int shift;
  136.     int addr = address;
  137.     String cmd = "";
  138.  
  139.     for (shift = 32; (shift -= 8) >= 0; ) {
  140.         cmd = cmd + ((addr >>> shift) & 0xff);
  141.         if (shift > 0)
  142.         cmd = cmd + ",";
  143.     }
  144.  
  145.     return "InetAddress[" + cmd + "]";
  146.     }
  147.  
  148.     public boolean equals(Object o) {
  149.     return (o instanceof InetAddress
  150.         && ((InetAddress) o).address == address);
  151.     }
  152. }
  153.