tags:

views:

72

answers:

4

I parse various data sources with network information in them.

I have been using java.net.InetAddress to represent and parse hosts. It works fine being initialized with IP.

I have to parse a new source now. It contains hostnames instead of IP's. InetAddress.getByName() throws UnknownHostException if a hostname argument can't be resolved to an IP. Host IP isn't absolutely neccessary for my goal. Dropping the data just because of DNS failure is unacceptable for me.

I'd like to have an IP address if it is obtainable or a hostname otherwise.

How do I prevent resolve of given hostnames? Is there another class that is more suited for my needs?

A: 
Roman
So it is impossible to make getByName silently accept nonexistent domain, isn't it?
Basilevs
@Basilevs: welcome to Java World! Use try/catch block. I recommend you to read some manual about exceptions in java.
Roman
@Roman: Be so kind to show how to use try/catch in this issue, for example for `InetAddress ia=InetAddress.getByName("google.invalid"); System.out.println(ia.getHostname());`
Michael Konietzka
@Roman: Well, that's the point. You get no `InetAddress` without an exiting IP-address. But that was the original question, if it is possible to get an InetAddress object without hostname resolution: `Can InetAddress represent host names that can't be resolved?``
Michael Konietzka
@Michael Konietzka: you could downvote me without asking to add the code snippet.
Roman
A: 

I guess that this is not possible, because the core function of InetAddress is to handle IP-Addresses:

This class represents an Internet Protocol (IP) address.

An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built. The IP address architecture is defined by RFC 790: Assigned Numbers, RFC 1918: Address Allocation for Private Internets, RFC 2365: Administratively Scoped IP Multicast, and RFC 2373: IP Version 6 Addressing Architecture. An instance of an InetAddress consists of an IP address and possibly its corresponding host name (depending on whether it is constructed with a host name or whether it has already done reverse host name resolution).

You have no possibility to construct an InetAddress object without an IP-address.

For you purpose you need to define a Hostname.class, which holds the hostname and maybe additional data. This class should/can handle all the domainname internationalization via IDN, if you are using Java 6. There are separate IDN libraries available if you are on Java 5.

A separate Hostname.class can also define guards or constructors, which assure only valid hostnames according to the relevant RFCs.

Michael Konietzka
@Michael Konietzka: just try to make up a simple program with line `InetAddress.getByName("google.com")` and you'll see that you're wrong here.
Roman
Of course, because `google.com` has an IP-address. If you want me to prove wrong show me how to get an InetAddress object for an hostname without an existing IP-address from somekind of DNS. This is the all about in this question. And remove your downvote, because you are wrong.
Michael Konietzka
@Michael Konietzka: I think, I get what you and @Basilevs are talking about. And the question seems to me very strange now because there is actually no any difference between simple string and 'inet address' of host without real ip address 'under the hood'.
Roman
@Roman: This is the question: `Can InetAddress represent host names that can't be resolved?`
Michael Konietzka
@Roman: read the docs - "this class represents an Internet Protocol (IP) address". There are various static helper methods that create InetAddress objects by performing a DNS lookup, but without a matching DNS record an InetAddress cannot be created - in fact, it's meaningless to do so (since there's no IP address to begin with).
SimonJ
A: 

The method InetAddress.getByAddress(String host, byte[] addr) does not perform a query to the DNS and allows you to create an InetAddress having arbitrary hostname and IP address, possibly the IPv4 unspecified address (0.0.0.0).

Try creating the InetAddress with getByName, if it throws you can create it by using getByAddress. Note that you will need to check the IP addresses are valid before actually using them.


Example code:

  public static void main(String arg[])
    throws  UnknownHostException
  {
    InetAddress a;
    byte[] unspec = new byte[4];
    unspec[0]=0;
    unspec[1]=0;
    unspec[2]=0;
    unspec[3]=0;
    try
    {
      a = InetAddress.getByName(arg[0]);
    }
    catch(UnknownHostException e)
    {
      a = InetAddress.getByAddress(arg[0],unspec);
    }
    System.out.println(a);
  }
Giacomo Verticale
This will generate InetAddress objects, where hostname and ip-address will not fit together. Especially `0.0.0.0` is reserved for the `default network`.
Michael Konietzka
I beleive this answer is the closest to what I need. Unfortunately zeroed IP breaks comparison functionality of InetAddress which is bad for my needs.
Basilevs
A: 

How about writing a new class? It would only need to be able to store a String and an InetAddress.

thejh