Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 62de7421 authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change I4fe2a47a into eclair

* changes:
  Fix stopping all vpn daemons before connect and more.
parents 9825ec61 c8217638
Loading
Loading
Loading
Loading
+4 −11
Original line number Diff line number Diff line
@@ -31,24 +31,17 @@ class L2tpIpsecPskService extends VpnService<L2tpIpsecPskProfile> {
    protected void connect(String serverIp, String username, String password)
            throws IOException {
        L2tpIpsecPskProfile p = getProfile();
        VpnDaemons daemons = getDaemons();

        // IPSEC
        DaemonProxy ipsec = startDaemon(IPSEC);
        ipsec.sendCommand(serverIp, L2tpService.L2TP_PORT, p.getPresharedKey());
        ipsec.closeControlSocket();
        daemons.startIpsecForL2tp(serverIp, p.getPresharedKey())
                .closeControlSocket();

        sleep(2000); // 2 seconds

        // L2TP
        MtpdHelper.sendCommand(this, L2tpService.L2TP_DAEMON, serverIp,
                L2tpService.L2TP_PORT,
        daemons.startL2tp(serverIp,
                (p.isSecretEnabled() ? p.getSecretString() : null),
                username, password);
    }

    @Override
    protected void stopPreviouslyRunDaemons() {
        stopDaemon(IPSEC);
        stopDaemon(MtpdHelper.MTPD);
    }
}
+4 −10
Original line number Diff line number Diff line
@@ -31,9 +31,10 @@ class L2tpIpsecService extends VpnService<L2tpIpsecProfile> {
    protected void connect(String serverIp, String username, String password)
            throws IOException {
        L2tpIpsecProfile p = getProfile();
        VpnDaemons daemons = getDaemons();

        // IPSEC
        DaemonProxy ipsec = startDaemon(IPSEC);
        ipsec.sendCommand(serverIp, L2tpService.L2TP_PORT,
        DaemonProxy ipsec = daemons.startIpsecForL2tp(serverIp,
                Credentials.USER_PRIVATE_KEY + p.getUserCertificate(),
                Credentials.USER_CERTIFICATE + p.getUserCertificate(),
                Credentials.CA_CERTIFICATE + p.getCaCertificate());
@@ -42,15 +43,8 @@ class L2tpIpsecService extends VpnService<L2tpIpsecProfile> {
        sleep(2000); // 2 seconds

        // L2TP
        MtpdHelper.sendCommand(this, L2tpService.L2TP_DAEMON, serverIp,
                L2tpService.L2TP_PORT,
        daemons.startL2tp(serverIp,
                (p.isSecretEnabled() ? p.getSecretString() : null),
                username, password);
    }

    @Override
    protected void stopPreviouslyRunDaemons() {
        stopDaemon(IPSEC);
        stopDaemon(MtpdHelper.MTPD);
    }
}
+1 −9
Original line number Diff line number Diff line
@@ -24,20 +24,12 @@ import java.io.IOException;
 * The service that manages the L2TP VPN connection.
 */
class L2tpService extends VpnService<L2tpProfile> {
    static final String L2TP_DAEMON = "l2tp";
    static final String L2TP_PORT = "1701";

    @Override
    protected void connect(String serverIp, String username, String password)
            throws IOException {
        L2tpProfile p = getProfile();
        MtpdHelper.sendCommand(this, L2TP_DAEMON, serverIp, L2TP_PORT,
        getDaemons().startL2tp(serverIp,
                (p.isSecretEnabled() ? p.getSecretString() : null),
                username, password);
    }

    @Override
    protected void stopPreviouslyRunDaemons() {
        stopDaemon(MtpdHelper.MTPD);
    }
}
+2 −10
Original line number Diff line number Diff line
@@ -24,19 +24,11 @@ import java.io.IOException;
 * The service that manages the PPTP VPN connection.
 */
class PptpService extends VpnService<PptpProfile> {
    static final String PPTP_DAEMON = "pptp";
    static final String PPTP_PORT = "1723";

    @Override
    protected void connect(String serverIp, String username, String password)
            throws IOException {
        PptpProfile p = getProfile();
        MtpdHelper.sendCommand(this, PPTP_DAEMON, serverIp, PPTP_PORT, null,
                username, password, p.isEncryptionEnabled());
    }

    @Override
    protected void stopPreviouslyRunDaemons() {
        stopDaemon(MtpdHelper.MTPD);
        getDaemons().startPptp(serverIp, username, password,
                p.isEncryptionEnabled());
    }
}
+147 −0
Original line number Diff line number Diff line
@@ -16,26 +16,106 @@

package com.android.server.vpn;

import android.util.Log;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * A helper class for sending commands to the MTP daemon (mtpd).
 * A helper class for managing native VPN daemons.
 */
class MtpdHelper {
    static final String MTPD = "mtpd";
class VpnDaemons implements Serializable {
    static final long serialVersionUID = 1L;
    private final String TAG = VpnDaemons.class.getSimpleName();

    private static final String MTPD = "mtpd";
    private static final String IPSEC = "racoon";

    private static final String L2TP = "l2tp";
    private static final String L2TP_PORT = "1701";

    private static final String PPTP = "pptp";
    private static final String PPTP_PORT = "1723";

    private static final String VPN_LINKNAME = "vpn";
    private static final String PPP_ARGS_SEPARATOR = "";

    static void sendCommand(VpnService<?> vpnService, String protocol,
            String serverIp, String port, String secret, String username,
            String password) throws IOException {
        sendCommand(vpnService, protocol, serverIp, port, secret, username,
                password, false);
    private List<DaemonProxy> mDaemonList = new ArrayList<DaemonProxy>();

    public DaemonProxy startL2tp(String serverIp, String secret,
            String username, String password) throws IOException {
        return startMtpd(L2TP, serverIp, L2TP_PORT, secret, username, password,
                false);
    }

    public DaemonProxy startPptp(String serverIp, String username,
            String password, boolean encryption) throws IOException {
        return startMtpd(PPTP, serverIp, PPTP_PORT, null, username, password,
                encryption);
    }

    public DaemonProxy startIpsecForL2tp(String serverIp, String pskKey)
            throws IOException {
        DaemonProxy ipsec = startDaemon(IPSEC);
        ipsec.sendCommand(serverIp, L2TP_PORT, pskKey);
        return ipsec;
    }

    public DaemonProxy startIpsecForL2tp(String serverIp, String userKeyKey,
            String userCertKey, String caCertKey) throws IOException {
        DaemonProxy ipsec = startDaemon(IPSEC);
        ipsec.sendCommand(serverIp, L2TP_PORT, userKeyKey, userCertKey,
                caCertKey);
        return ipsec;
    }

    public synchronized void stopAll() {
        new DaemonProxy(MTPD).stop();
        new DaemonProxy(IPSEC).stop();
    }

    static void sendCommand(VpnService<?> vpnService, String protocol,
    public synchronized void closeSockets() {
        for (DaemonProxy s : mDaemonList) s.closeControlSocket();
    }

    public synchronized boolean anyDaemonStopped() {
        for (DaemonProxy s : mDaemonList) {
            if (s.isStopped()) {
                Log.w(TAG, "    VPN daemon gone: " + s.getName());
                return true;
            }
        }
        return false;
    }

    public synchronized int getSocketError() {
        for (DaemonProxy s : mDaemonList) {
            int errCode = getResultFromSocket(s);
            if (errCode != 0) return errCode;
        }
        return 0;
    }

    private synchronized DaemonProxy startDaemon(String daemonName)
            throws IOException {
        DaemonProxy daemon = new DaemonProxy(daemonName);
        mDaemonList.add(daemon);
        daemon.start();
        return daemon;
    }

    private int getResultFromSocket(DaemonProxy s) {
        try {
            return s.getResultFromSocket();
        } catch (IOException e) {
            return -1;
        }
    }

    private DaemonProxy startMtpd(String protocol,
            String serverIp, String port, String secret, String username,
            String password, boolean encryption) throws IOException {
        ArrayList<String> args = new ArrayList<String>();
@@ -44,8 +124,9 @@ class MtpdHelper {
        args.add(PPP_ARGS_SEPARATOR);
        addPppArguments(args, serverIp, username, password, encryption);

        DaemonProxy mtpd = vpnService.startDaemon(MTPD);
        DaemonProxy mtpd = startDaemon(MTPD);
        mtpd.sendCommand(args.toArray(new String[args.size()]));
        return mtpd;
    }

    private static void addPppArguments(ArrayList<String> args, String serverIp,
@@ -63,7 +144,4 @@ class MtpdHelper {
            args.add("+mppe");
        }
    }

    private MtpdHelper() {
    }
}
Loading