Skip to content

Workaround for android bug 64819 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/src/main/java/xyz/hexene/localvpn/UDPOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
return addr;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
}