/*****************************************************************************
 * Copyright (C) 2010 S.R.Haque <srhaque@theiet.org>                         *
 * Christoph Thielecke <crissi99@gmx.de> (broken out from kvpnc.h)           *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation; either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * This package is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License         *
 * along with this package; if not, write to the Free Software               *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
 *****************************************************************************/

#include <QtCore/QString>
#include <QtCore/QTimer>
#include "HandlerBase.h"

class QProcess;
class QStringList;

class IpsecHandler:
        public HandlerBase
{
    Q_OBJECT
public:
    IpsecHandler(
        class KVpnc *ui,
        class KVpncConfig *config,
        QString type,
        QString daemons,
        bool _useL2tpAfterFirstConnection);
    bool vpnStart(bool authenticationRetry);
    void vpnStop();
    QString pingDevice() const;

private:
    QString Pkcs11PIN;
    HelperProcess *IpsecStartProcess;
    HelperProcess *IpsecWhackProcess; //< ipsec whack --initate
    HelperProcess *IpsecWhackStatusProcess; //< ipsec whack --status
    HelperProcess *IpsecWhackListenProcess; //< ipsec whack --listen
    HelperProcess *IpsecWhackDeleteProcess; //< ipsec whack --delete
    QTimer IpsecWhackStatusTimer; //< getting ipsec status periodically
    bool IpsecGotError;
    bool IpsecPhase1Up;
    bool IpsecPhase2Up;
    bool IpsecStatusCheck;
    QString IpsecType; //< openswan, strongswan, freeswan
    HelperProcess *IpsecProcess; //< freeswan/openswan/strongswan
    QString tmpInterface;
    HelperProgram m_tool;
    bool m_toolIsStrongSwan;
    bool m_toolIsOpenSwan;

    /**
     * TBD: can this be shared by other handlers?
     */
    bool getAuthenticationData(bool AuthRetry);

private slots:
    void readFromStdout_ipsec();
    void readFromStderr_ipsec();
    void readFromStdout_ipsecwhack();
    void readFromStderr_ipsecwhack();
    void readFromStdout_ipsecwhacklisten();
    void readFromStderr_ipsecwhacklisten();
    void readFromStdout_ipsecwhackstatus();
    void readFromStderr_ipsecwhackstatus();
    void readFromStdout_ipsecwhackdelete();
    void readFromStderr_ipsecwhackdelete();
    void ipsecwhack_exited(int, QProcess::ExitStatus); //< ipsec whack has been exited
    void IpsecWhackStatusProcessExited(int, QProcess::ExitStatus);
    void start_ipsec_initiate();
    void addRouteIpsec();
    void delRouteIpsec();
    void doIptablesFreeswan();
    void removeIptablesFreeswan();
    void ipsecStarted(int, QProcess::ExitStatus);
    void ipsecPhase2Established();
    void checkIpsecWhackStatus();
    void addIpsecConnection();
    void startStopIpsec(bool stop = false, bool silent = false);
};
