summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2012-05-05 19:54:55 +0200
committerArne Schwabe <arne@rfc2549.org>2012-05-05 19:54:55 +0200
commitec7e6b920257e52a24f9fc56150c05f3bbc6da7d (patch)
tree64908b1dcbf6c1515d73cbd4302faf0c6eb18572 /src/de/blinkt/openvpn
parent4a7488e0b5dc2a0069d2a4c9b461631016a95d45 (diff)
working protect fd through management
Diffstat (limited to 'src/de/blinkt/openvpn')
-rw-r--r--src/de/blinkt/openvpn/OpenVPN.java12
-rw-r--r--src/de/blinkt/openvpn/OpenVpnManagementThread.java76
2 files changed, 45 insertions, 43 deletions
diff --git a/src/de/blinkt/openvpn/OpenVPN.java b/src/de/blinkt/openvpn/OpenVPN.java
index 3e660bc..8dad6af 100644
--- a/src/de/blinkt/openvpn/OpenVPN.java
+++ b/src/de/blinkt/openvpn/OpenVPN.java
@@ -88,17 +88,7 @@ public class OpenVPN {
public static void setCallback(OpenVpnService openVpnService) {
mOpenVpnService = openVpnService;
}
-
- public static boolean protectSocket (int sockfd)
- {
- boolean p = mOpenVpnService.protect(sockfd);
- if(p)
- Log.d("openvpn","Protected socket "+ sockfd);
- else
- Log.e("openvpn","Error protecting socket "+ sockfd);
- return p;
- }
-
+
public static int openTunDevice() {
Log.d(TAG,"Opening tun device");
return mOpenVpnService.openTun();
diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java
index 093e4d6..2bae9e5 100644
--- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java
+++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java
@@ -5,9 +5,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.net.DatagramSocket;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Vector;
import android.net.LocalSocket;
+import android.os.ParcelFileDescriptor;
import android.util.Log;
public class OpenVpnManagementThread implements Runnable {
@@ -16,7 +20,7 @@ public class OpenVpnManagementThread implements Runnable {
private LocalSocket mSocket;
private VpnProfile mProfile;
private OpenVpnService mOpenVPNService;
- private Vector<Integer> mFDList=new Vector<Integer>();
+ private LinkedList<FileDescriptor> mFDList=new LinkedList<FileDescriptor>();
private static Vector<OpenVpnManagementThread> active=new Vector<OpenVpnManagementThread>();
@@ -65,7 +69,7 @@ public class OpenVpnManagementThread implements Runnable {
int numbytesread = instream.read(buffer);
if(numbytesread==-1)
return;
-
+
FileDescriptor[] fds = null;
try {
fds = mSocket.getAncillaryFileDescriptors();
@@ -73,21 +77,10 @@ public class OpenVpnManagementThread implements Runnable {
e.printStackTrace();
}
if(fds!=null){
- Log.i(TAG, "fds:" + fds);
+
for (FileDescriptor fd : fds) {
- try {
- Method getInt = FileDescriptor.class.getDeclaredMethod("getInt$");
- int fdint = (Integer) getInt.invoke(fd);
- mFDList.add(fdint);
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
+
+ mFDList.add(fd);
}
}
@@ -106,6 +99,30 @@ public class OpenVpnManagementThread implements Runnable {
active.remove(this);
}
+ //! Hack O Rama 2000!
+ private void protectFileDescriptor(FileDescriptor fd) {
+ try {
+ Method getInt = FileDescriptor.class.getDeclaredMethod("getInt$");
+ int fdint = (Integer) getInt.invoke(fd);
+
+ Log.d("Openvpn", "Got FD from socket: " + fd + " " + fdint);
+ ParcelFileDescriptor pfd = ParcelFileDescriptor.fromFd(fdint);
+ mOpenVPNService.protect(fdint);
+ pfd.close();
+ return;
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ Log.d("Openvpn", "Failed to retrieve fd from socket: " + fd);
+ }
private String processInput(String pendingInput) {
@@ -136,27 +153,16 @@ public class OpenVpnManagementThread implements Runnable {
processPWCommand(argument);
} else if (cmd.equals("HOLD")) {
managmentCommand("hold release\n");
- } else if (cmd.equals("PROTECT-FD")) {
- protectFD(argument);
+ } else if (cmd.equals("NEED-OK")) {
+ processPWCommand(argument);
+ } else {
+ Log.i(TAG, "Got unrecognized command" + command);
}
- Log.i(TAG, "Got unrecognized command" + command);
} else {
Log.i(TAG, "Got unrecognized line from managment" + command);
}
}
-
- private void protectFD(String argument) {
- try {
- FileDescriptor[] fds = mSocket.getAncillaryFileDescriptors();
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
-
private void processPWCommand(String argument) {
//argument has the form Need 'Private Key' password
int p1 =argument.indexOf('\'');
@@ -165,6 +171,7 @@ public class OpenVpnManagementThread implements Runnable {
String needed = argument.substring(p1+1, p2);
String pw=null;
+ String response="password";
if(needed.equals("Private Key")) {
pw = mProfile.getPasswordPrivateKey();
@@ -173,9 +180,14 @@ public class OpenVpnManagementThread implements Runnable {
needed, managmentEscape(mProfile.mUsername));
managmentCommand(usercmd);
pw = mProfile.getPasswordAuth();
+ } else if (needed.equals("PROTECTFD")) {
+ FileDescriptor fdtoprotect = mFDList.pollFirst();
+ protectFileDescriptor(fdtoprotect);
+ pw = "ok";
+ response="needok";
}
if(pw!=null) {
- String cmd = String.format("password '%s' %s\n", needed, managmentEscape(pw));
+ String cmd = String.format("%s '%s' %s\n",response, needed, managmentEscape(pw));
managmentCommand(cmd);
}