corda/classpath/java/net/InetAddress.java
Joel Dice 47d9039b69 switch from gethostbyname to getaddrinfo on POSIX systems
gethostbyname may return any combination of IPv4 and IPv6 addresses,
and it's not safe to assume the first address is IPv4, which is all
our code is currently prepared to handle.  In contrast, getaddrinfo
allows us to specify whether we want IPv4, IPv6, or both.

We should eventually make this switch on Windows as well, but the
status of getaddrinfo in Windows 2000 is not clear, and MinGW's
ws2tcpip.h only declares it for XP and above.

This commit also adds InetAddress.getByName for explicit DNS lookups.
2010-06-14 16:09:56 -06:00

54 lines
1.3 KiB
Java

/* Copyright (c) 2010, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.net;
import java.io.IOException;
public class InetAddress {
private final String address;
private InetAddress(String address) {
this.address = address;
}
public String getHostAddress() {
return address;
}
public static InetAddress getByName(String name)
throws UnknownHostException
{
try {
Socket.init();
} catch (IOException e) {
UnknownHostException uhe = new UnknownHostException(name);
uhe.initCause(e);
throw uhe;
}
int address = ipv4AddressForName(name);
if (address == 0) {
throw new UnknownHostException(name);
} else {
return new InetAddress(ipv4AddressToString(address));
}
}
private static String ipv4AddressToString(int address) {
return (((address >>> 24) ) + "." +
((address >>> 16) & 0xFF) + "." +
((address >>> 8 ) & 0xFF) + "." +
((address ) & 0xFF));
}
private static native int ipv4AddressForName(String name);
}