home *** CD-ROM | disk | FTP | other *** search
- package org.xbill.DNS;
-
- import java.net.InetAddress;
- import java.net.UnknownHostException;
-
- public final class Address {
- private Address() {
- }
-
- public static int[] toArray(String s) {
- int[] values = new int[4];
- int length = s.length();
- int currentOctet = 0;
- int numDigits = 0;
-
- for(int i = 0; i < length; ++i) {
- char c = s.charAt(i);
- if (c >= '0' && c <= '9') {
- if (numDigits == 3) {
- return null;
- }
-
- if (numDigits > 0 && values[currentOctet] == 0) {
- return null;
- }
-
- ++numDigits;
- values[currentOctet] *= 10;
- values[currentOctet] += c - 48;
- if (values[currentOctet] > 255) {
- return null;
- }
- } else {
- if (c != '.') {
- return null;
- }
-
- if (currentOctet == 3) {
- return null;
- }
-
- if (numDigits == 0) {
- return null;
- }
-
- ++currentOctet;
- numDigits = 0;
- }
- }
-
- if (currentOctet != 3) {
- return null;
- } else if (numDigits == 0) {
- return null;
- } else {
- return values;
- }
- }
-
- public static boolean isDottedQuad(String s) {
- int[] address = toArray(s);
- return address != null;
- }
-
- public static String toDottedQuad(byte[] addr) {
- return (addr[0] & 255) + "." + (addr[1] & 255) + "." + (addr[2] & 255) + "." + (addr[3] & 255);
- }
-
- public static String toDottedQuad(int[] addr) {
- return addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3];
- }
-
- private static Record[] lookupHostName(String name) throws UnknownHostException {
- try {
- Record[] records = (new Lookup(name)).run();
- if (records == null) {
- throw new UnknownHostException("unknown host");
- } else {
- return records;
- }
- } catch (TextParseException var2) {
- throw new UnknownHostException("invalid name");
- }
- }
-
- public static InetAddress getByName(String name) throws UnknownHostException {
- if (isDottedQuad(name)) {
- return InetAddress.getByName(name);
- } else {
- Record[] records = lookupHostName(name);
- ARecord a = (ARecord)records[0];
- return a.getAddress();
- }
- }
-
- public static InetAddress[] getAllByName(String name) throws UnknownHostException {
- if (isDottedQuad(name)) {
- return InetAddress.getAllByName(name);
- } else {
- Record[] records = lookupHostName(name);
- InetAddress[] addrs = new InetAddress[records.length];
-
- for(int i = 0; i < records.length; ++i) {
- ARecord a = (ARecord)records[i];
- addrs[i] = a.getAddress();
- }
-
- return addrs;
- }
- }
-
- public static String getHostName(InetAddress addr) throws UnknownHostException {
- Name name = ReverseMap.fromAddress(addr);
- Record[] records = (new Lookup(name, 12)).run();
- if (records == null) {
- throw new UnknownHostException("unknown address");
- } else {
- PTRRecord ptr = (PTRRecord)records[0];
- return ptr.getTarget().toString();
- }
- }
- }
-