📄 networkaddressmanager.java
字号:
console.debug(
"Returninng localhost: Mapped "
+ "address = Public address = "
+ address);
//the address matches the one seen by the STUN server
//no doubt that it's a working public address.
return address;
}
else if (isLinkLocalIPv4Address(address)) {
if (console.isDebugEnabled())
console.debug(
"Found Linklocal ipv4 address "
+ address);
linkLocalAddress = address;
}
else {
if (console.isDebugEnabled())
console.debug("Found public address "
+ address);
if ( //in case we have an ipv4 addr and don't want to change it for an ipv6
(publicAddress != null
&& publicAddress instanceof Inet4Address
&& address instanceof Inet6Address
&& preferIPv4Stack)
//in case we have an ipv6 addr and don't want to change it for an ipv4
|| (publicAddress != null
&&
publicAddress instanceof Inet6Address
&& address instanceof Inet4Address
&& !preferIPv4Stack)
)
continue;
publicAddress = address;
}
}
}
}
if (publicAddress != null) {
console.debug("Returning public address");
return publicAddress;
}
if (linkLocalAddress != null) {
console.debug("Returning link local address");
return linkLocalAddress;
}
if (anyAddressIsAccepted)
localHost = new InetSocketAddress(RANDOM_PORT).getAddress();
else
localHost = InetAddress.getLocalHost();
}
catch (Exception ex) {
console.error("Failed to create localhost address, returning "
+ "the any address (0.0.0.0)", ex);
//get the address part of an InetSocketAddress for a random port.
localHost = new InetSocketAddress(RANDOM_PORT).getAddress();
}
if (console.isDebugEnabled())
console.debug("Returning localhost address=" + localHost);
return localHost;
}
finally {
console.logExit();
}
}
/**
* Tries to obtain a mapped/public address for the specified address and
* port. If the STUN lib fails, tries to retrieve localhost, if that fails
* too, returns null.
*
* @param address the address to resolve
* @param port the port whose mapping we are interested in.
* @return a public address corresponding to the specified port or null if
* all attempts to retrieve such an address have failed.
*/
public static InetSocketAddress getPublicAddressFor(String address,
int port)
{
try {
console.logEntry();
if (!manager.useStun) {
console.debug(
"Stun is disabled, skipping mapped address recovery.");
return new InetSocketAddress(address, port);
}
StunAddress mappedAddress = null;
if (manager.detector != null)
try {
mappedAddress = manager.detector.getMappingFor(new
StunAddress(address, port));
if (console.isDebugEnabled())
console.debug("For [" + address + "]:"
+ port
+
"a Stun server returned the following mapping ["
+ mappedAddress);
}
catch (StunException ex) {
console.error("Failed to retrive mapped address for [" +
address + "]:" + port, ex);
}
InetSocketAddress result = null;
if (mappedAddress != null)
result = mappedAddress.getSocketAddress();
else
result = new InetSocketAddress(address, port);
if (console.isDebugEnabled())
console.debug("Returning mapping for [" + address + "]:" + port +
" as follows: " + result);
return result;
}
finally {
console.logExit();
}
}
/**
* Tries to obtain a mapped/public address for the specified port. If the
* STUN lib fails, tries to retrieve localhost, if that fails too, returns
* null.
*
* @param port the port whose mapping we are interested in.
* @return a public address corresponding to the specified port or null if
* all attempts to retrieve such an address have failed.
*/
public static InetSocketAddress getPublicAddressFor(int port)
{
try {
console.logEntry();
if (!manager.useStun) {
console.debug(
"Stun is disabled, skipping mapped address recovery.");
//we can call local method getLocalHost here since stun usage
//is disabled and getLocalHost won't call us on it's turn
return new InetSocketAddress(getLocalHost(), port);
}
StunAddress mappedAddress = null;
if (manager != null
&& manager.detector != null)
try {
mappedAddress = manager.detector.getMappingFor(port);
if (console.isDebugEnabled())
console.debug("For port:"
+ port
+
"a Stun server returned the following mapping ["
+ mappedAddress);
}
catch (StunException ex) {
console.error("Failed to retrive mapped address port:" +
port, ex);
}
InetSocketAddress result = null;
if (mappedAddress != null)
result = mappedAddress.getSocketAddress();
else {
//do not call local method get localhost 'cos it will call us
//and we'll be off for an endless recursion
InetAddress localHost = null;
try {
localHost = InetAddress.getLocalHost();
result = new InetSocketAddress(localHost, port);
}
catch (UnknownHostException ex1) {
//since a public address is wanted then 0.0.0.0 will certainly
//not do. We should rather return loopback and hope we'll
//be communicating with localhost (no comment).
result = new InetSocketAddress("127.0.0.1", port);
}
}
if (console.isDebugEnabled())
console.debug("Returning mapping for port:" + port +
" as follows: " + result);
return result;
}
finally {
console.logExit();
}
}
/**
* Determines whether the address is the result of windows auto configuration.
* (i.e. One that is in the 169.254.0.0 network)
* @param add the address to inspect
* @return true if the address is autoconfigured by windows, false otherwise.
*/
public static boolean isWindowsAutoConfiguredIPv4Address(InetAddress add)
{
return (add.getAddress()[0] & 0xFF) == 169
&& (add.getAddress()[1] & 0xFF) == 254;
}
/**
* Determines whether the address is an IPv4 link local address. IPv4 link
* local addresses are those in the following networks:
*
* 10.0.0.0 to 10.255.255.255
* 172.16.0.0 to 172.31.255.255
* 192.168.0.0 to 192.168.255.255
*
* @param add the address to inspect
* @return true if add is a link local ipv4 address and false if not.
*/
public static boolean isLinkLocalIPv4Address(InetAddress add)
{
byte address[] = add.getAddress();
if ( (address[0] & 0xFF) == 10)
return true;
if ( (address[0] & 0xFF) == 172
&& (address[1] & 0xFF) >= 16 && address[1] <= 31)
return true;
if ( (address[0] & 0xFF) == 192
&& (address[1] & 0xFF) == 168)
return true;
return false;
}
/**
* Determines whether the address could be used in a VoIP session. Attention,
* routable address as determined by this method are not only globally routable
* addresses in the general sense of the term. Link local addresses such as
* 192.168.x.x or fe80::xxxx are also considered usable.
* @param address the address to test.
* @return true if the address could be used in a VoIP session.
*/
public static boolean isRoutable(InetAddress address)
{
if(address instanceof Inet6Address)
{
return !address.isLoopbackAddress();
}
else
{
return (!address.isLoopbackAddress())
&& (!isWindowsAutoConfiguredIPv4Address(address));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -