| 1 7 8 package java.net; 9 10 import java.util.HashMap ; 11 import java.util.LinkedHashMap ; 12 import java.util.Random ; 13 import java.util.Iterator ; 14 import java.util.LinkedList ; 15 import java.security.AccessController ; 16 import java.io.ObjectStreamException ; 17 import java.io.IOException ; 18 import sun.security.action.*; 19 import sun.net.InetAddressCachePolicy; 20 import sun.net.util.IPAddressUtil; 21 import sun.misc.Service; 22 import sun.net.spi.nameservice.*; 23 24 162 public 163 class InetAddress implements java.io.Serializable { 164 168 static final int IPv4 = 1; 169 170 174 static final int IPv6 = 2; 175 176 177 static transient boolean preferIPv6Address = false; 178 179 182 String hostName; 183 184 189 int address; 190 191 197 int family; 198 199 200 private static NameService nameService = null; 201 202 203 private transient String canonicalHostName = null; 204 205 206 private static final long serialVersionUID = 3286316764910316507L; 207 208 211 static { 212 preferIPv6Address = 213 ((Boolean )java.security.AccessController.doPrivileged( 214 new GetBooleanAction("java.net.preferIPv6Addresses"))).booleanValue(); 215 AccessController.doPrivileged(new LoadLibraryAction("net")); 216 init(); 217 } 218 219 225 InetAddress() { 226 } 227 228 236 private Object readResolve() throws ObjectStreamException { 237 return new Inet4Address (this.hostName, this.address); 239 } 240 241 248 public boolean isMulticastAddress() { 249 return false; 250 } 251 252 258 public boolean isAnyLocalAddress() { 259 return false; 260 } 261 262 269 public boolean isLoopbackAddress() { 270 return false; 271 } 272 273 280 public boolean isLinkLocalAddress() { 281 return false; 282 } 283 284 291 public boolean isSiteLocalAddress() { 292 return false; 293 } 294 295 303 public boolean isMCGlobal() { 304 return false; 305 } 306 307 315 public boolean isMCNodeLocal() { 316 return false; 317 } 318 319 327 public boolean isMCLinkLocal() { 328 return false; 329 } 330 331 339 public boolean isMCSiteLocal() { 340 return false; 341 } 342 343 352 public boolean isMCOrgLocal() { 353 return false; 354 } 355 356 357 377 public boolean isReachable(int timeout) throws IOException { 378 return isReachable(null, 0 , timeout); 379 } 380 381 412 public boolean isReachable(NetworkInterface netif, int ttl, 413 int timeout) throws IOException { 414 if (ttl < 0) 415 throw new IllegalArgumentException ("ttl can't be negative"); 416 if (timeout < 0) 417 throw new IllegalArgumentException ("timeout can't be negative"); 418 419 return impl.isReachable(this, timeout, netif, ttl); 420 } 421 422 447 public String getHostName() { 448 return getHostName(true); 449 } 450 451 474 String getHostName(boolean check) { 475 if (hostName == null) { 476 hostName = InetAddress.getHostFromNameService(this, check); 477 } 478 return hostName; 479 } 480 481 502 public String getCanonicalHostName() { 503 if (canonicalHostName == null) { 504 canonicalHostName = 505 InetAddress.getHostFromNameService(this, true); 506 } 507 return canonicalHostName; 508 } 509 510 529 private static String getHostFromNameService(InetAddress addr, boolean check) { 530 String host; 531 try { 532 host = nameService.getHostByAddr(addr.getAddress()); 534 535 538 if (check) { 539 SecurityManager sec = System.getSecurityManager(); 540 if (sec != null) { 541 sec.checkConnect(host, -1); 542 } 543 } 544 545 549 550 InetAddress [] arr = InetAddress.getAllByName0(host, check); 551 boolean ok = false; 552 553 if(arr != null) { 554 for(int i = 0; !ok && i < arr.length; i++) { 555 ok = addr.equals(arr[i]); 556 } 557 } 558 559 if (!ok) { 561 host = addr.getHostAddress(); 562 return host; 563 } 564 565 } catch (SecurityException e) { 566 host = addr.getHostAddress(); 567 } catch (UnknownHostException e) { 568 host = addr.getHostAddress(); 569 } 570 return host; 571 } 572 573 580 public byte[] getAddress() { 581 return null; 582 } 583 584 590 public String getHostAddress() { 591 return null; 592 } 593 594 599 public int hashCode() { 600 return -1; 601 } 602 603 619 public boolean equals(Object obj) { 620 return false; 621 } 622 623 633 public String toString() { 634 return ((hostName != null) ? hostName : "") 635 + "/" + getHostAddress(); 636 } 637 638 641 private static Cache addressCache = new Cache(Cache.Type.Positive); 642 643 private static Cache negativeCache = new Cache(Cache.Type.Negative); 644 645 private static boolean addressCacheInit = false; 646 647 static InetAddress [] unknown_array; 649 static InetAddressImpl impl; 650 651 private static HashMap lookupTable = new HashMap (); 652 653 656 static final class CacheEntry { 657 658 CacheEntry(Object address, long expiration) { 659 this.address = address; 660 this.expiration = expiration; 661 } 662 663 Object address; 664 long expiration; 665 } 666 667 671 static final class Cache { 672 private LinkedHashMap cache; 673 private Type type; 674 675 enum Type {Positive, Negative}; 676 677 680 public Cache(Type type) { 681 this.type = type; 682 cache = new LinkedHashMap (); 683 } 684 685 private int getPolicy() { 686 if (type == Type.Positive) { 687 return InetAddressCachePolicy.get(); 688 } else { 689 return InetAddressCachePolicy.getNegative(); 690 } 691 } 692 693 698 public Cache put(String host, Object address) { 699 int policy = getPolicy(); 700 if (policy == InetAddressCachePolicy.NEVER) { 701 return this; 702 } 703 704 706 if (policy != InetAddressCachePolicy.FOREVER) { 707 708 LinkedList expired = new LinkedList (); 711 Iterator i = cache.keySet().iterator(); 712 long now = System.currentTimeMillis(); 713 while (i.hasNext()) { 714 String key = (String )i.next(); 715 CacheEntry entry = (CacheEntry)cache.get(key); 716 717 if (entry.expiration >= 0 && entry.expiration < now) { 718 expired.add(key); 719 } else { 720 break; 721 } 722 } 723 724 i = expired.iterator(); 725 while (i.hasNext()) { 726 cache.remove(i.next()); 727 } 728 } 729 730 long expiration; 735 if (policy == InetAddressCachePolicy.FOREVER) { 736 expiration = -1; 737 } else { 738 expiration = System.currentTimeMillis() + (policy * 1000); 739 } 740 CacheEntry entry = new CacheEntry(address, expiration); 741 cache.put(host, entry); 742 return this; 743 } 744 745 749 public CacheEntry get(String host) { 750 int policy = getPolicy(); 751 if (policy == InetAddressCachePolicy.NEVER) { 752 return null; 753 } 754 CacheEntry entry = (CacheEntry)cache.get(host); 755 756 if (entry != null && policy != InetAddressCachePolicy.FOREVER) { 758 if (entry.expiration >= 0 && 759 entry.expiration < System.currentTimeMillis()) { 760 cache.remove(host); 761 entry = null; 762 } 763 } 764 765 return entry; 766 } 767 } 768 769 773 private static void cacheInitIfNeeded() { 774 assert Thread.holdsLock(addressCache); 775 if (addressCacheInit) { 776 return; 777 } 778 unknown_array = new InetAddress [1]; 779 unknown_array[0] = impl.anyLocalAddress(); 780 781 addressCache.put(impl.anyLocalAddress().getHostName(), 782 unknown_array); 783 784 addressCacheInit = true; 785 } 786 787 790 private static void cacheAddress(String hostname, Object address, 791 boolean success) { 792 hostname = hostname.toLowerCase(); 793 synchronized (addressCache) { 794 cacheInitIfNeeded(); 795 if (success) { 796 addressCache.put(hostname, address); 797 } else { 798 negativeCache.put(hostname, address); 799 } 800 } 801 } 802 803 807 private static Object getCachedAddress(String hostname) { 808 hostname = hostname.toLowerCase(); 809 810 812 synchronized (addressCache) { 813 CacheEntry entry; 814 815 cacheInitIfNeeded(); 816 817 entry = (CacheEntry)addressCache.get(hostname); 818 if (entry == null) { 819 entry = (CacheEntry)negativeCache.get(hostname); 820 } 821 822 if (entry != null) { 823 return entry.address; 824 } 825 } 826 827 return null; 829 } 830 831 static { 832 impl = (new InetAddressImplFactory()).create(); 834 835 String provider = null;; 837 String propPrefix = "sun.net.spi.nameservice.provider."; 838 int n = 1; 839 while (nameService == null) { 840 provider 841 = (String )AccessController.doPrivileged( 842 new GetPropertyAction(propPrefix+n, "default")); 843 n++; 844 if (provider.equals("default")) { 845 nameService = new NameService() { 847 public InetAddress [] lookupAllHostAddr(String host) 848 throws UnknownHostException { 849 return impl.lookupAllHostAddr(host); 850 } 851 public String getHostByAddr(byte[] addr) 852 throws UnknownHostException { 853 return impl.getHostByAddr(addr); 854 } 855 }; 856 break; 857 } 858 859 final String providerName = provider; 860 861 try { 862 java.security.AccessController.doPrivileged( 863 new java.security.PrivilegedExceptionAction () { 864 public Object run() { 865 Iterator itr 866 = Service.providers(NameServiceDescriptor.class); 867 while (itr.hasNext()) { 868 NameServiceDescriptor nsd 869 = (NameServiceDescriptor)itr.next(); 870 if (providerName. 871 equalsIgnoreCase(nsd.getType()+"," 872 +nsd.getProviderName())) { 873 try { 874 nameService = nsd.createNameService(); 875 break; 876 } catch (Exception e) { 877 e.printStackTrace(); 878 System.err.println( 879 "Cannot create name service:" 880 +providerName+": " + e); 881 } 882 } 883 } 884 return null; 885 } 886 }); 887 } catch (java.security.PrivilegedActionException e) { 888 } 889 890 } 891 } 892 893 915 public static InetAddress getByAddress(String host, byte[] addr) 916 throws UnknownHostException { 917 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 918 if (host.charAt(host.length()-1) == ']') { 919 host = host.substring(1, host.length() -1); 920 } 921 } 922 if (addr != null) { 923 if (addr.length == Inet4Address.INADDRSZ) { 924 return new Inet4Address (host, addr); 925 } else if (addr.length == Inet6Address.INADDRSZ) { 926 byte[] newAddr 927 = IPAddressUtil.convertFromIPv4MappedAddress(addr); 928 if (newAddr != null) { 929 return new Inet4Address (host, newAddr); 930 } else { 931 return new Inet6Address (host, addr); 932 } 933 } 934 } 935 throw new UnknownHostException ("addr is of illegal length"); 936 } 937 938 939 967 public static InetAddress getByName(String host) 968 throws UnknownHostException { 969 return InetAddress.getAllByName(host)[0]; 970 } 971 972 private static InetAddress getByName(String host, InetAddress reqAddr) 974 throws UnknownHostException { 975 return InetAddress.getAllByName(host, reqAddr)[0]; 976 } 977 978 |