java InetAddress.isSiteLocalAddress() 实际上是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5619345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does InetAddress.isSiteLocalAddress() actually mean?
提问by TiGz
Here is some code to determine the local host name that is supposed to work on a multi-homed box:
下面是一些代码来确定应该在多宿主机上工作的本地主机名:
/**
* Work out the first local host name by iterating the network interfaces
*
* @return
* @throws SocketException
*/
private String findFirstLocalHostName() throws SocketException {
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress add = addresses.nextElement();
if (!add.isLoopbackAddress() && add.isSiteLocalAddress()) {
return add.getHostName();
}
}
}
throw new RuntimeException("Failed to determine local hostname");
}
Does the call to isSiteLocalAddress introduce a bug? I can't find any useful information about this method, but I have a feeling that it relates to IP v 6 only and is deprecated.
对 isSiteLocalAddress 的调用是否引入了错误?我找不到有关此方法的任何有用信息,但我感觉它仅与 IP v 6 相关并且已弃用。
回答by Joachim Sauer
The method is definitely not deprecatedand it's definitely not just used in IPv6.
该方法绝对不会被弃用,而且绝对不仅仅在 IPv6 中使用。
In IPv4 there are 3 network address ranges that are defined for site-local addresses: 10/8, 172.16/12 and 192.168/16.
在 IPv4 中,为站点本地地址定义了 3 个网络地址范围:10/8、172.16/12 和 192.168/16。
Reading Inet4Address.isSiteLocalAddress()
shows that addresses from exactly those 3 networks will return true
on those methods.
阅读Inet4Address.isSiteLocalAddress()
表明,来自这 3 个网络的地址将true
在这些方法上返回。
IPv6 has a similar concept, here these addresses are called unqieu local addresses.
IPv6 也有类似的概念,这里这些地址称为unqieu 本地地址。
Effectively this tells you if the address you have is definitely not a public one (note that even if this method returns false
, the address might still not be public).
这有效地告诉您您拥有的地址是否绝对不是公开地址(请注意,即使此方法返回false
,地址可能仍然不是公开的)。
回答by dty
Looking at the implementation...
看实现...
For an Inet4Address
, it checks to see if it's one of the RFC1918 "unrouteable" addresses: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16.
对于Inet4Address
,它会检查它是否是 RFC1918“不可路由”地址之一:10.0.0.0/8、172.16.0.0/12、192.168.0.0/16。
For an Inet6Address
, it checks the first two octets to see if it's a real "site local" address.
对于 an Inet6Address
,它检查前两个八位字节以查看它是否是真正的“站点本地”地址。
回答by jmac
'Site local' is a deprecated name for private IP space. (Some nuances, but basically right.) See RFC 1918.
“站点本地”是私有 IP 空间的弃用名称。(一些细微差别,但基本上是正确的。)参见 RFC 1918。
回答by mateuscb
I just came across what I believe is a similar problem: trying to determine what IPv6 I should use for LAN comuncation:
我刚刚遇到了我认为是类似的问题:试图确定我应该使用什么 IPv6 进行 LAN 通信:
IMHO,
Inet6Address.isSiteLocalAddress()
is useless. Given that the0xFEC0
prefix has been depricated by RFC 3879as @tigz mentioned. I have yet to see any device (android, win, osx) actually have a0xFEC0
(with limited testing)//from java.net.Inet6Address (1.8.0_45) boolean isSiteLocalAddress() { return ((ipaddress[0] & 0xff) == 0xfe && (ipaddress[1] & 0xc0) == 0xc0); }
0xFE80
address although not supposed be used for traffic (from my understanding and reading (www.cisco.com)) did work for LAN communication with my single router (ping6, curl, http).My Global Unicast (which is just another name for public IP)
2601::/20
from Comcast worked for my LAN communication. So I would say that this is the correct address to use.
恕我直言,
Inet6Address.isSiteLocalAddress()
没用。鉴于@tigz 提到的0xFEC0
前缀已被RFC 3879弃用。我还没有看到任何设备(android、win、osx)实际上有一个0xFEC0
(有限的测试)//from java.net.Inet6Address (1.8.0_45) boolean isSiteLocalAddress() { return ((ipaddress[0] & 0xff) == 0xfe && (ipaddress[1] & 0xc0) == 0xc0); }
0xFE80
地址虽然不应该用于流量(根据我的理解和阅读(www.cisco.com))确实适用于与我的单个路由器(ping6、curl、http)的 LAN 通信。我
2601::/20
来自 Comcast 的Global Unicast(这只是公共 IP 的另一个名称)用于我的 LAN 通信。所以我会说这是要使用的正确地址。
Prefix table: www.iana.org
前缀表:www.iana.org
回答by Koekiebox
As far as I know the isSiteLocalAddress method is not deprecated.
据我所知 isSiteLocalAddress 方法没有被弃用。
isSiteLocalAddress - Explanation
isSiteLocalAddress - 说明
indicating if the InetAddress is a site local address; or false if address is not a site local unicast address.
指示 InetAddress 是否是站点本地地址;如果地址不是站点本地单播地址,则为 false。
The InetAddress even have two direct subclasses;
InetAddress 甚至有两个直接子类;
The best bet is to read the JavaDocs.
最好的办法是阅读 JavaDocs。
Which version of the JDK are you using?
您使用的是哪个版本的 JDK?