torservice.h

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file torservice.h
00024  * \version $Id: torservice.h 1579 2007-01-10 06:26:27Z edmanm $
00025  * \brief Starts, stops, installs, and uninstalls a Tor service (Win32).
00026  */
00027 
00028 #ifndef _TORSERVICE_H
00029 #define _TORSERVICE_H
00030 
00031 #include <QObject>
00032 #include <QProcess>
00033 
00034 #include <windows.h>
00035 #define TOR_SERVICE_NAME "tor"
00036 #define TOR_SERVICE_DISP "Tor Win32 Service"
00037 #define TOR_SERVICE_DESC \
00038   TEXT("Provides an anonymous Internet communication system.")
00039 #define TOR_SERVICE_ACCESS SERVICE_ALL_ACCESS
00040 #define SERVICE_ERROR 8
00041 
00042 /* NT service function prototypes. This code is adapted from Tor's
00043  * nt_service_load_library() in main.c. See LICENSE for details on
00044  * Tor's license. */
00045 typedef BOOL (WINAPI *ChangeServiceConfig2A_fn)(
00046                              SC_HANDLE hService,
00047                              DWORD dwInfoLevel,
00048                              LPVOID lpInfo);
00049 typedef BOOL (WINAPI *CloseServiceHandle_fn)(
00050                              SC_HANDLE hSCObject);
00051 typedef BOOL (WINAPI *ControlService_fn)(
00052                              SC_HANDLE hService,
00053                              DWORD dwControl,
00054                              LPSERVICE_STATUS lpServiceStatus);
00055 typedef SC_HANDLE (WINAPI *CreateServiceA_fn)(
00056                              SC_HANDLE hSCManager,
00057                              LPCTSTR lpServiceName,
00058                              LPCTSTR lpDisplayName,
00059                              DWORD dwDesiredAccess,
00060                              DWORD dwServiceType,
00061                              DWORD dwStartType,
00062                              DWORD dwErrorControl,
00063                              LPCTSTR lpBinaryPathName,
00064                              LPCTSTR lpLoadOrderGroup,
00065                              LPDWORD lpdwTagId,
00066                              LPCTSTR lpDependencies,
00067                              LPCTSTR lpServiceStartName,
00068                              LPCTSTR lpPassword);
00069 typedef BOOL (WINAPI *DeleteService_fn)(
00070                              SC_HANDLE hService);
00071 typedef SC_HANDLE (WINAPI *OpenSCManagerA_fn)(
00072                              LPCTSTR lpMachineName,
00073                              LPCTSTR lpDatabaseName,
00074                              DWORD dwDesiredAccess);
00075 typedef SC_HANDLE (WINAPI *OpenServiceA_fn)(
00076                              SC_HANDLE hSCManager,
00077                              LPCTSTR lpServiceName,
00078                              DWORD dwDesiredAccess);
00079 typedef BOOL (WINAPI *QueryServiceStatus_fn)(
00080                              SC_HANDLE hService,
00081                              LPSERVICE_STATUS lpServiceStatus);
00082 typedef BOOL (WINAPI *SetServiceStatus_fn)(SERVICE_STATUS_HANDLE,
00083                              LPSERVICE_STATUS);
00084 typedef BOOL (WINAPI *StartServiceA_fn)(
00085                              SC_HANDLE hService,
00086                              DWORD dwNumServiceArgs,
00087                              LPCTSTR* lpServiceArgVectors);
00088 
00089 /** Table of NT service related functions. */
00090 typedef struct ServiceFunctions {
00091   bool loaded;
00092   ChangeServiceConfig2A_fn ChangeServiceConfig2A;
00093   CloseServiceHandle_fn    CloseServiceHandle;
00094   ControlService_fn        ControlService;
00095   CreateServiceA_fn        CreateServiceA;
00096   DeleteService_fn         DeleteService;
00097   OpenSCManagerA_fn        OpenSCManagerA;
00098   OpenServiceA_fn          OpenServiceA;
00099   QueryServiceStatus_fn    QueryServiceStatus;
00100   SetServiceStatus_fn      SetServiceStatus;
00101   StartServiceA_fn         StartServiceA;
00102 };
00103 
00104 
00105 class TorService : public QObject
00106 {
00107   Q_OBJECT
00108 
00109 public:
00110   /** Returns if services are supported. */
00111   static bool isSupported();
00112   /** Dynamically loads NT service related functions from advapi32.dll. */
00113   static bool loadServiceFunctions();
00114 
00115   /** Default ctor. */
00116   TorService(QObject* parent = 0);
00117   /** Default dtor. */
00118   ~TorService();
00119 
00120   /** Returns true if the Tor service is installed. */
00121   bool isInstalled();
00122   /** Returns true if the Tor service is running. */
00123   bool isRunning();
00124   /** Starts the Tor service. Emits started on success. */
00125   void start();
00126   /** Stops the Tor service. Emits finished on success. */
00127   bool stop();
00128   /** Returns the exit code of the last Tor service that finished. */
00129   int exitCode();
00130   /** Returns the exit status of the last Tor service that finished. */
00131   QProcess::ExitStatus exitStatus();
00132   /** Installs the Tor service. */
00133   bool install(const QString &torPath, const QString &torrc,
00134                quint16 controlPort);
00135   /** Removes the Tor service. */
00136   bool remove();
00137 
00138 signals:
00139   /** Called when the service gets started. */
00140   void started();
00141   /** Called when the service gets stopped. */
00142   void finished(int exitCode, QProcess::ExitStatus);
00143   /** Called when there is an error in starting the service. */
00144   void startFailed(QString error);
00145 
00146 private:
00147   /** Opens a handle to the Tor service. Returns NULL on error. */
00148   SC_HANDLE openService();
00149   /** Opens a handle to the service control manager. Returns NULL on error. */
00150   static SC_HANDLE openSCM();
00151   /** Closes the service <b>handle</b>. */
00152   static void closeHandle(SC_HANDLE handle);
00153   /** Gets the status of the Tor service. */
00154   DWORD status(); 
00155 
00156   /** Handle to the service control manager. */ 
00157   SC_HANDLE _scm;
00158   /** List of dynamically loaded NT service functions. */
00159   static ServiceFunctions _service_fns;
00160 };
00161 
00162 #endif
00163 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3