mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-09-27 10:09:32 +02:00
4b6de6647b
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6517 b35dd754-fafc-0310-a699-88a17e54d16e
103 lines
3.6 KiB
Java
103 lines
3.6 KiB
Java
package org.jivesoftware.smackx.jingle.nat;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import java.net.InetAddress;
|
|
import java.net.NetworkInterface;
|
|
import java.net.SocketException;
|
|
import java.util.Enumeration;
|
|
|
|
/**
|
|
* Basic Resolver takes all IP addresses of the interfaces and uses the
|
|
* first non-loopback address.
|
|
* A very simple and easy to use resolver.
|
|
*/
|
|
public class BasicResolver extends TransportResolver {
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public BasicResolver() {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* Resolve the IP address.
|
|
* <p/>
|
|
* The BasicResolver takes the IP addresses of the interfaces and uses the
|
|
* first non-loopback, non-linklocal and non-sitelocal address.
|
|
*/
|
|
public synchronized void resolve() throws XMPPException {
|
|
|
|
setResolveInit();
|
|
|
|
clearCandidates();
|
|
|
|
Enumeration ifaces = null;
|
|
|
|
try {
|
|
ifaces = NetworkInterface.getNetworkInterfaces();
|
|
} catch (SocketException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
while (ifaces.hasMoreElements()) {
|
|
|
|
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
|
Enumeration iaddresses = iface.getInetAddresses();
|
|
|
|
while (iaddresses.hasMoreElements()) {
|
|
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
|
|
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
|
|
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
|
|
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
|
|
addCandidate(tr);
|
|
setResolveEnd();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
ifaces = NetworkInterface.getNetworkInterfaces();
|
|
} catch (SocketException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
while (ifaces.hasMoreElements()) {
|
|
|
|
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
|
Enumeration iaddresses = iface.getInetAddresses();
|
|
|
|
while (iaddresses.hasMoreElements()) {
|
|
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
|
|
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
|
|
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
|
|
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
|
|
addCandidate(tr);
|
|
setResolveEnd();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
TransportCandidate tr = new TransportCandidate.Fixed(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName(), getFreePort());
|
|
tr.setLocalIp(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName());
|
|
addCandidate(tr);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
setResolveEnd();
|
|
|
|
}
|
|
|
|
public void initialize() throws XMPPException {
|
|
setInitialized();
|
|
}
|
|
|
|
public void cancel() throws XMPPException {
|
|
// Nothing to do here
|
|
}
|
|
}
|