diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-09-03 19:50:42 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-09-03 19:50:42 +0200 |
commit | 71e4bcf9cb807e2f3954a5e968a44a409aaeebd7 (patch) | |
tree | e1f92e73467a3c5cad8da7943fbc8dbf0eb1f6be /src/main/java/de/pixart/messenger/xmpp/jingle/DirectConnectionUtils.java | |
parent | edcfdb974dc544c0e79e457f50c12e5942619251 (diff) |
bare minimum direct connections
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/xmpp/jingle/DirectConnectionUtils.java | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/DirectConnectionUtils.java b/src/main/java/de/pixart/messenger/xmpp/jingle/DirectConnectionUtils.java new file mode 100644 index 000000000..3292e65e4 --- /dev/null +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/DirectConnectionUtils.java @@ -0,0 +1,51 @@ +package de.pixart.messenger.xmpp.jingle; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.UUID; + +import rocks.xmpp.addr.Jid; + +public class DirectConnectionUtils { + + private static List<InetAddress> getLocalAddresses() { + final List<InetAddress> addresses = new ArrayList<>(); + final Enumeration<NetworkInterface> interfaces; + try { + interfaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + return addresses; + } + while (interfaces.hasMoreElements()) { + NetworkInterface networkInterface = interfaces.nextElement(); + final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); + while (inetAddressEnumeration.hasMoreElements()) { + final InetAddress inetAddress = inetAddressEnumeration.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + addresses.add(inetAddress); + } + } + } + return addresses; + } + + public static List<JingleCandidate> getLocalCandidates(Jid jid) { + SecureRandom random = new SecureRandom(); + ArrayList<JingleCandidate> candidates = new ArrayList<>(); + for (InetAddress inetAddress : getLocalAddresses()) { + final JingleCandidate candidate = new JingleCandidate(UUID.randomUUID().toString(), true); + candidate.setHost(inetAddress.getHostAddress()); + candidate.setPort(random.nextInt(60000) + 1024); + candidate.setType(JingleCandidate.TYPE_DIRECT); + candidate.setJid(jid); + candidate.setPriority(8257536 + candidates.size()); + candidates.add(candidate); + } + return candidates; + } +}
\ No newline at end of file |