Return IPv6 in Java

2020-02-10 08:18发布

Is there a way in Java to tell it to return IPv6 only? I've tried everything and can't get it to work.

try
    {
        InetAddress inet = InetAddress.getByName(hostName);

        boolean status = inet.isReachable(5000);

        if (status)
        {
            System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());
        }
        else
        {
            System.out.println(inet.getCanonicalHostName() + " Host Unreachable");
        }

    }
    catch (UnknownHostException e)
    {
        System.err.println("Host does not exists");
    }
    catch (IOException e)
    {
        System.err.println("Error in reaching the Host");
    }

The line I use to try to return IPv6 only:

System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());

This keeps returning IPv4. Anyone have any idea of why its doing this?

标签: java ipv6
2条回答
仙女界的扛把子
2楼-- · 2020-02-10 09:21

java.net.Inet6Address does not override getByName()
so it will always return the specific IPv4-Address, unless your parameter itself is in the form of an valid IPv6-Address, in this case this method will return an Inet6Address-Object.

For example:
getByName("stackoverflow.com") --> Inet4Address
getByName("2001:0db8:85a3:08d3:1319:8a2e:0370:7344") --> Inet6Address

InetAddress.getByName()-Documentation

Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

> For host specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted.<

So if you want to get an IPv6-Address you need to define it within your parameter, or configure a DNS-Server to return the IPv6-Address instead of the IPv4-Address.

Another way to retrieve the IPv6-Address is using InetAddress.getAllByName("www.google.at") which returns all known IP-Addresses of the host.

For example you can use this method to filter the returned array, which return the first IPv6-Address or null if the host don't have one:

public Inet6Address getIPv6Addresses(InetAddress[] addresses) {
    for (InetAddress addr : addresses) {
        if (addr instanceof Inet6Address) {
            return (Inet6Address) addr;
        }
    }
    return null;
}

UPDATE: For more functions, especially those affecting DNS-Servers, I recommend using the external library DNSJava, because the plain Java implementation of DNS support is poor.
http://www.dnsjava.org/

Current Code:

public class Ping 
{
public void pingHost (String hostName)
{
    try
    {
        InetAddress[] inet = InetAddress.getAllByName(hostName);

        String address = this.getIPv4Addresses(inet).getHostAddress();

        boolean status = this.getIPv6Addresses(inet).isReachable(5000);

        if (status)
        {

            System.out.println(reverseDns(address) + " Host Reached\t" + this.getIPv6Addresses(inet).getHostAddress());
        }
        else
        {
            System.out.println(this.getIPv6Addresses(inet).getCanonicalHostName() + " Host Unreachable");
        }

    }
    catch (UnknownHostException e)
    {
        System.err.println("Host does not exists");
    }
    catch (IOException e)
    {
        System.err.println("Error in reaching the Host");
    }
}

public Inet6Address getIPv6Addresses(InetAddress[] addresses) 
{
    for (InetAddress addr : addresses) 
    {
        if (addr instanceof Inet6Address) 
        {
            return (Inet6Address) addr;
        }
    }
    return null;
}

public Inet4Address getIPv4Addresses(InetAddress[] addresses) 
{
    for (InetAddress addr : addresses) 
    {
        if (addr instanceof Inet4Address) 
        {
            return (Inet4Address) addr;
        }
    }
    return null;
}

public static String reverseDns(String hostIp) throws IOException 
{
    Resolver res = new ExtendedResolver();

    Name name = ReverseMap.fromAddress(hostIp);
    int type = Type.PTR;
    int dclass = DClass.IN;
    Record rec = Record.newRecord(name, type, dclass);
    Message query = Message.newQuery(rec);
    Message response = res.send(query);

    Record[] answers = response.getSectionArray(Section.ANSWER);
    if (answers.length == 0)
       return hostIp;
    else
       return answers[0].rdataToString();
  }

}
查看更多
够拽才男人
3楼-- · 2020-02-10 09:21

You could try to define JVM_ARGS

-Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Addresses=true

with that props it will prefer IPv6 addr on InetAddress#getByName

More info: https://docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/

查看更多
登录 后发表回答