2019-09-01 13:06:59 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
2023-12-19 16:25:48 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
2019-09-08 15:44:26 +00:00
|
|
|
import java.net.Inet6Address;
|
2019-09-01 13:06:59 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.NetworkInterface;
|
|
|
|
import java.net.SocketException;
|
2019-09-08 15:44:26 +00:00
|
|
|
import java.net.UnknownHostException;
|
2019-09-01 13:06:59 +00:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2019-09-01 13:06:59 +00:00
|
|
|
|
|
|
|
public class DirectConnectionUtils {
|
|
|
|
|
2023-12-19 16:25:48 +00:00
|
|
|
public static List<InetAddress> getLocalAddresses() {
|
|
|
|
final ImmutableList.Builder<InetAddress> inetAddresses = new ImmutableList.Builder<>();
|
2019-09-01 13:06:59 +00:00
|
|
|
final Enumeration<NetworkInterface> interfaces;
|
|
|
|
try {
|
|
|
|
interfaces = NetworkInterface.getNetworkInterfaces();
|
2023-12-19 16:25:48 +00:00
|
|
|
} catch (final SocketException e) {
|
|
|
|
return inetAddresses.build();
|
2019-09-01 13:06:59 +00:00
|
|
|
}
|
|
|
|
while (interfaces.hasMoreElements()) {
|
|
|
|
NetworkInterface networkInterface = interfaces.nextElement();
|
|
|
|
final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
|
|
|
|
while (inetAddressEnumeration.hasMoreElements()) {
|
|
|
|
final InetAddress inetAddress = inetAddressEnumeration.nextElement();
|
2019-09-05 11:17:45 +00:00
|
|
|
if (inetAddress.isLoopbackAddress() || inetAddress.isLinkLocalAddress()) {
|
|
|
|
continue;
|
2019-09-01 13:06:59 +00:00
|
|
|
}
|
2019-09-08 15:44:26 +00:00
|
|
|
if (inetAddress instanceof Inet6Address) {
|
|
|
|
//let's get rid of scope
|
|
|
|
try {
|
2023-12-19 16:25:48 +00:00
|
|
|
inetAddresses.add(Inet6Address.getByAddress(inetAddress.getAddress()));
|
2019-09-08 15:44:26 +00:00
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
//ignored
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-19 16:25:48 +00:00
|
|
|
inetAddresses.add(inetAddress);
|
2019-09-08 15:44:26 +00:00
|
|
|
}
|
2019-09-01 13:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 16:25:48 +00:00
|
|
|
return inetAddresses.build();
|
2019-09-01 13:06:59 +00:00
|
|
|
}
|
|
|
|
}
|