networkaddressmanager.java
来自「java 开发的sip软电话 源码 jain sip」· Java 代码 · 共 469 行 · 第 1/2 页
JAVA
469 行
Enumeration localIfaces = NetworkInterface.getNetworkInterfaces();
//interfaces loop
interfaces_loop:
while (localIfaces.hasMoreElements()) {
NetworkInterface iFace = (NetworkInterface) localIfaces.
nextElement();
Enumeration addresses = iFace.getInetAddresses();
//addresses loop
while (addresses.hasMoreElements()) {
InetAddress address = (InetAddress) addresses.nextElement();
//ignore link local addresses
if (!address.isAnyLocalAddress()
&& !address.isLinkLocalAddress()
&& !address.isLoopbackAddress()
&& !isWindowsAutoConfiguredIPv4Address(address)) {
if (mappedAddress != null
&& mappedAddress.equals(address)) {
System.out.println("OOOPPPPA");
return address;
}
else if (isLinkLocalIPv4Address(address)) {
linkLocalAddress = address;
}
else {
if (//bail out if we already have the address chosen by the user
(linkLocalAddress != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS") != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS").equals(linkLocalAddress.getHostAddress()))
//bail out if we already have an address on an interface chosen by the user
|| (linkLocalAddress != null
&& selectedInterface != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_INTERFACE") != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_INTERFACE").equals(selectedInterface))
//bail out if we already have the address chosen by the user
|| (publicAddress != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS") != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS").equals(publicAddress.getHostAddress()))
//bail out if we already have an address on an interface chosen by the user
|| (publicAddress != null
&& selectedInterface != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_INTERFACE") != null
&& Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_INTERFACE").equals(selectedInterface))
//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;
selectedInterface = iFace.getDisplayName();
}
}
}//addresses loop
}//interfaces loop
System.out.println("PREF: "+Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS"));
if(Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS")==null && !Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS").equals("")){
if (publicAddress != null) System.out.println("PUBLIC: "+publicAddress.getHostAddress());
if (linkLocalAddress != null) System.out.println("LINK: "+linkLocalAddress.getHostAddress());
if (publicAddress != null && Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS").equals(publicAddress.getHostAddress())) {
System.out.println("SELECTED: "+publicAddress);
return publicAddress;
}
if (linkLocalAddress != null && Utils.getProperty("net.java.mais.common.PREFERRED_NETWORK_ADDRESS").equals(linkLocalAddress.getHostAddress())) {
System.out.println("SELECTED: "+linkLocalAddress);
return linkLocalAddress;
}
}
if (publicAddress != null) {
System.out.println("SELECTED: "+publicAddress);
return publicAddress;
}
if (linkLocalAddress != null) {
System.out.println("SELECTED: "+linkLocalAddress);
return linkLocalAddress;
}
if (anyAddressIsAccepted)
localHost = new InetSocketAddress(RANDOM_PORT).getAddress();
else
localHost = InetAddress.getLocalHost();
}
catch (Exception ex) {
System.out.println("EXCEPTION NET DETECT: "+ex.toString());
//get the address part of an InetSocketAddress for a random port.
localHost = new InetSocketAddress(RANDOM_PORT).getAddress();
}
System.out.println("SELECTED: "+localHost);
return localHost;
}
finally {
}
}
//EMIL: I don't see where this method is used. Should probbaly Take it away
/**
* 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 {
return new InetSocketAddress(address, port);
}
finally {
}
}
/**
* 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 {
return new InetSocketAddress(getLocalHost(), port);
}
finally {
}
}
/**
* 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));
}
}
public static boolean setProxy(){
String proxy=Utils.getProperty("net.java.mais.common.PREFERRED_PROXY");
if(proxy==null||proxy.equals("")){
proxy=RegReader.getDefaultProxy(); }
if(proxy==null||proxy.equals("")){
proxy=JOptionPane.showInputDialog("Configura玢o de Proxy:","ip:porta");
}
System.out.println(proxy);
if(!proxy.equals("")){
String p[]=proxy.split(":");
System.getProperties().put("proxySet","true");
if(p.length>1)
System.getProperties().put("proxyPort",p[1]);
System.getProperties().put("proxyHost",p[0]);
Utils.setProperty("net.java.mais.common.PREFERRED_PROXY",proxy);
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?