home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / Address.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  3.2 KB  |  123 lines

  1. package org.xbill.DNS;
  2.  
  3. import java.net.InetAddress;
  4. import java.net.UnknownHostException;
  5.  
  6. public final class Address {
  7.    private Address() {
  8.    }
  9.  
  10.    public static int[] toArray(String s) {
  11.       int[] values = new int[4];
  12.       int length = s.length();
  13.       int currentOctet = 0;
  14.       int numDigits = 0;
  15.  
  16.       for(int i = 0; i < length; ++i) {
  17.          char c = s.charAt(i);
  18.          if (c >= '0' && c <= '9') {
  19.             if (numDigits == 3) {
  20.                return null;
  21.             }
  22.  
  23.             if (numDigits > 0 && values[currentOctet] == 0) {
  24.                return null;
  25.             }
  26.  
  27.             ++numDigits;
  28.             values[currentOctet] *= 10;
  29.             values[currentOctet] += c - 48;
  30.             if (values[currentOctet] > 255) {
  31.                return null;
  32.             }
  33.          } else {
  34.             if (c != '.') {
  35.                return null;
  36.             }
  37.  
  38.             if (currentOctet == 3) {
  39.                return null;
  40.             }
  41.  
  42.             if (numDigits == 0) {
  43.                return null;
  44.             }
  45.  
  46.             ++currentOctet;
  47.             numDigits = 0;
  48.          }
  49.       }
  50.  
  51.       if (currentOctet != 3) {
  52.          return null;
  53.       } else if (numDigits == 0) {
  54.          return null;
  55.       } else {
  56.          return values;
  57.       }
  58.    }
  59.  
  60.    public static boolean isDottedQuad(String s) {
  61.       int[] address = toArray(s);
  62.       return address != null;
  63.    }
  64.  
  65.    public static String toDottedQuad(byte[] addr) {
  66.       return (addr[0] & 255) + "." + (addr[1] & 255) + "." + (addr[2] & 255) + "." + (addr[3] & 255);
  67.    }
  68.  
  69.    public static String toDottedQuad(int[] addr) {
  70.       return addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3];
  71.    }
  72.  
  73.    private static Record[] lookupHostName(String name) throws UnknownHostException {
  74.       try {
  75.          Record[] records = (new Lookup(name)).run();
  76.          if (records == null) {
  77.             throw new UnknownHostException("unknown host");
  78.          } else {
  79.             return records;
  80.          }
  81.       } catch (TextParseException var2) {
  82.          throw new UnknownHostException("invalid name");
  83.       }
  84.    }
  85.  
  86.    public static InetAddress getByName(String name) throws UnknownHostException {
  87.       if (isDottedQuad(name)) {
  88.          return InetAddress.getByName(name);
  89.       } else {
  90.          Record[] records = lookupHostName(name);
  91.          ARecord a = (ARecord)records[0];
  92.          return a.getAddress();
  93.       }
  94.    }
  95.  
  96.    public static InetAddress[] getAllByName(String name) throws UnknownHostException {
  97.       if (isDottedQuad(name)) {
  98.          return InetAddress.getAllByName(name);
  99.       } else {
  100.          Record[] records = lookupHostName(name);
  101.          InetAddress[] addrs = new InetAddress[records.length];
  102.  
  103.          for(int i = 0; i < records.length; ++i) {
  104.             ARecord a = (ARecord)records[i];
  105.             addrs[i] = a.getAddress();
  106.          }
  107.  
  108.          return addrs;
  109.       }
  110.    }
  111.  
  112.    public static String getHostName(InetAddress addr) throws UnknownHostException {
  113.       Name name = ReverseMap.fromAddress(addr);
  114.       Record[] records = (new Lookup(name, 12)).run();
  115.       if (records == null) {
  116.          throw new UnknownHostException("unknown address");
  117.       } else {
  118.          PTRRecord ptr = (PTRRecord)records[0];
  119.          return ptr.getTarget().toString();
  120.       }
  121.    }
  122. }
  123.