diff --git a/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java b/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java index 58afa6c..2f6f563 100644 --- a/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java +++ b/app/src/main/java/xyz/hexene/localvpn/UDPOutput.java @@ -19,13 +19,19 @@ import android.util.Log; import java.io.IOException; +import java.net.BindException; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; +import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; @@ -86,6 +92,14 @@ public void run() DatagramChannel outputChannel = channelCache.get(ipAndPort); if (outputChannel == null) { outputChannel = DatagramChannel.open(); + // Workaround for bug 64819 ( https://code.google.com/p/android/issues/detail?id=64819 ) + InetSocketAddress sa = new InetSocketAddress(getIPAddress(), sourcePort); + try { + outputChannel.socket().setReuseAddress(true); + outputChannel.socket().bind(sa); + } catch (BindException e) { + Log.d(TAG, sa.toString() + " " + e.toString(), e); + } try { outputChannel.connect(new InetSocketAddress(destinationAddress, destinationPort)); @@ -158,4 +172,22 @@ private void closeChannel(DatagramChannel channel) // Ignore } } + + //http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device + private InetAddress getIPAddress() { + try { + List interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); + for (NetworkInterface intf : interfaces) { + List addrs = Collections.list(intf.getInetAddresses()); + for (InetAddress addr : addrs) { + if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) { + return addr; + } + } + } + } catch (SocketException e) { + e.printStackTrace(); + } + return null; + } }