package primer10;

import java.net.*;
import java.io.*;

public class HostLookup {

	public static void main(String[] args) {

		if (args.length > 0) {
			for (int i = 0; i < args.length; i++)
				System.out.println(lookup(args[i]));
		} else {
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter names and IP addresses. Enter \"exit\" to quit.");

			try {
				while (true) {
					String host = in.readLine();
					if (host.equalsIgnoreCase("exit") || host.equalsIgnoreCase("quit"))
						break;
					System.out.println(lookup(host));
				}
			} catch (IOException ex) {
				System.err.println(ex);
			}
		}

	}

	private static String lookup(String host) {

		InetAddress node;

		try {
			node = InetAddress.getByName(host);
		} catch (UnknownHostException ex) {
			return "Cannot find host " + host;
		}

		if (isHostname(host)) {
			return node.getHostAddress();
		} else {
			return node.getHostName();
		}
	}

	private static boolean isHostname(String host) {

		// IPv6 address?
		if (host.indexOf(':') != -1)
			return false;

		char[] ca = host.toCharArray();
		for (int i = 0; i < ca.length; i++) {
			if (!Character.isDigit(ca[i])) {
				if (ca[i] != '.')
					return true;
			}
		}

		return false;

	}

}
