From 58cbe1ec522f688a1c2d2e848c9876b306904e4d Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 4 Sep 2023 11:53:38 +0200 Subject: Factor out sending a command with an FD into its own method --- .../openvpn/core/OpenVpnManagementThread.java | 34 +++++++++++++--------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java b/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java index 5d4486b9..8edae328 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java @@ -616,14 +616,8 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { managmentCommand(cmd); } - private boolean sendTunFD(String needed, String extra) { - if (!extra.equals("tun")) { - // We only support tun - VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra)); - return false; - } - ParcelFileDescriptor pfd = mOpenVPNService.openTun(); + private boolean sendCommandWithFd(String cmd, ParcelFileDescriptor pfd) { if (pfd == null) return false; @@ -636,26 +630,38 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { setInt.invoke(fdtosend, fdint); FileDescriptor[] fds = {fdtosend}; - mSocket.setFileDescriptorsForSend(fds); // Trigger a send so we can close the fd on our side of the channel // The API documentation fails to mention that it will not reset the file descriptor to // be send and will happily send the file descriptor on every write ... - String cmd = String.format("needok '%s' %s\n", needed, "ok"); + mSocket.setFileDescriptorsForSend(fds); + managmentCommand(cmd); // Set the FileDescriptor to null to stop this mad behavior mSocket.setFileDescriptorsForSend(null); - pfd.close(); - return true; - } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | - IOException | IllegalAccessException exp) { + + } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | + IOException exp) { VpnStatus.logException("Could not send fd over socket", exp); + return false; } + return true; + } - return false; + private boolean sendTunFD(String needed, String extra) { + if (!extra.equals("tun")) { + // We only support tun + VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra)); + + return false; + } + ParcelFileDescriptor pfd = mOpenVPNService.openTun(); + + String cmd = String.format("needok '%s' %s\n", needed, "ok"); + return sendCommandWithFd(cmd, pfd); } private void processPWCommand(String argument) { -- cgit v1.2.3