简体   繁体   中英

How to figure out an IPv6 address (by name) of a host in my LAN

I have two computers plugged in the same router of a network which I know supports IPv6. Let's call them "PC-A" and "PC-B

I want "PC-A" to figure out "PC-B"s IPv6 address and vice-versa

The first thing I do is

setSystem.setProperty("java.net.preferIPv6Addresses", "true");

If I then say

InetAddress IPAddress = InetAddress.getLocalHost();

I can get my own address which will be in IpV6 format

However, neither of the following two statements gives me "PC-B"s IPv6 address:

Inet6Address IPAddress6 = (Inet6Address)InetAddress.getByName("PC-B");
InetAddress IPAddress = InetAddress.getByName("PC-B");

I also tried to import

import com.lavantech.net.dns.SimpleDNSLookup;
import com.lavantech.net.dns.DNSLookup

The first one I am using as:

SimpleDNSLookup d = new SimpleDNSLookup();
System.out.println(d.getInet6Address("PC-B"));

and the second one as:

DNSLookup dnsLookup = new DNSLookup("PC-B", DNSLookup.QTYPE_AAAA, DNSLookup.QCLASS_IN, 3000, null);
// Get all Address Records.
ResourceRecord[] ansRecords = dnsLookup.getAAAARecords();
System.out.println(ansRecords[0]);

none of which works.

I also tried to use the following

import org.xbill.DNS.*;

int type = Type.AAAA;
Name name = Name.fromString("PC-B");
Lookup lookup = new Lookup(name, type);
lookup.run();
int result = lookup.getResult();
Record[] answers = lookup.getAnswers();
System.out.println(answers[0]); 
// (where, for brevity, i am skipping the parts where I check whether result == Lookup.SUCCESSFUL
  • Note that if I substitute "PC-B" for, say, "ipv6.google.com" I get all the desired results!
  • Also note that if I just use InetAddress and Type_A wherever applicable in the above approaches, my program returns "PC-B"s IPv4 address without problem.

What am I missing? Any help is greatly appreciated!

Your question is -unfortunately- a yet unsolved network problem dealing with host discovery on a local subnet (regardless if that subnet has a router or not).

Your desired output is clearly an IPv6 address, but it is unclear what exactly your input is.

Let's focus on PC-B. How exactly do you identify PC-B? It clear that you call it "PC-B", but that name should be configured somewhere before your PC know that that's its name. Where exactly is that configured? Is that the hostname you set on PC-B itself, or is there a domain name server (DNS) where you have given that name? If it is the name in the DNS system, you can indeed query the DNS system for the AAAA record to get the IPv6 address, but you need the fully qualified domain name (FQDN). Eg "PC-B.yourdomain.com" rather than just "PC-B".

If you know the MAC address of PC-B, you can use the neighbour discovery protocol (NDP) to find out the IP address of PC-B.

There are network protocols that allow PC-A and PC-B to announce their names themselves, once you configured them on the local machines. Such protocols are called "service discovery" protocols, and your options here are (1) multicast DNS (mDNS) and possibly DNS service discovery (DNS-SD) on top of that; or (2) Simple Service Discovery Protocol (SSDP) in UPnP on the other hand. The advantage is that some operating systems already implement this. Eg if PC-B is a Mac OS X host, all you need to do is query DNS for "pc-b.local" to get the answer. Unfortunately, while implementations of mDNS exist for Linux (Avahi) and Windows (Bonjour), they're not installed by default. A third alternative is to write your own host discovery protocol, and have your hosts run that protocol.

Considerations are which platforms you want to support, if installing third-party software is an option, if the discovery needs to be secure (the above options are not, look into Secure Neighbour Discovery -SEND- if this is a concern), and what input you have in the first place (the hostname "PC-B", or the type of service that runs on PC-B, eg _http._tcp for a webserver).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM