torservice.h

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If 
00004 **  you did not receive the LICENSE file with this file, you may obtain it
00005 **  from the Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to
00008 **  the terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file torservice.h
00013 ** \version $Id: torservice.h 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Starts, stops, installs, and uninstalls a Tor service (Win32).
00015 */
00016 
00017 #ifndef _TORSERVICE_H
00018 #define _TORSERVICE_H
00019 
00020 #include <QObject>
00021 #include <QProcess>
00022 
00023 #include <windows.h>
00024 #define TOR_SERVICE_NAME "tor"
00025 #define TOR_SERVICE_DISP "Tor Win32 Service"
00026 #define TOR_SERVICE_DESC \
00027   TEXT("Provides an anonymous Internet communication system.")
00028 #define TOR_SERVICE_ACCESS SERVICE_ALL_ACCESS
00029 #define SERVICE_ERROR 8
00030 
00031 /* NT service function prototypes. This code is adapted from Tor's
00032  * nt_service_load_library() in main.c. See LICENSE for details on
00033  * Tor's license. */
00034 typedef BOOL (WINAPI *ChangeServiceConfig2A_fn)(
00035                              SC_HANDLE hService,
00036                              DWORD dwInfoLevel,
00037                              LPVOID lpInfo);
00038 typedef BOOL (WINAPI *CloseServiceHandle_fn)(
00039                              SC_HANDLE hSCObject);
00040 typedef BOOL (WINAPI *ControlService_fn)(
00041                              SC_HANDLE hService,
00042                              DWORD dwControl,
00043                              LPSERVICE_STATUS lpServiceStatus);
00044 typedef SC_HANDLE (WINAPI *CreateServiceA_fn)(
00045                              SC_HANDLE hSCManager,
00046                              LPCTSTR lpServiceName,
00047                              LPCTSTR lpDisplayName,
00048                              DWORD dwDesiredAccess,
00049                              DWORD dwServiceType,
00050                              DWORD dwStartType,
00051                              DWORD dwErrorControl,
00052                              LPCTSTR lpBinaryPathName,
00053                              LPCTSTR lpLoadOrderGroup,
00054                              LPDWORD lpdwTagId,
00055                              LPCTSTR lpDependencies,
00056                              LPCTSTR lpServiceStartName,
00057                              LPCTSTR lpPassword);
00058 typedef BOOL (WINAPI *DeleteService_fn)(
00059                              SC_HANDLE hService);
00060 typedef SC_HANDLE (WINAPI *OpenSCManagerA_fn)(
00061                              LPCTSTR lpMachineName,
00062                              LPCTSTR lpDatabaseName,
00063                              DWORD dwDesiredAccess);
00064 typedef SC_HANDLE (WINAPI *OpenServiceA_fn)(
00065                              SC_HANDLE hSCManager,
00066                              LPCTSTR lpServiceName,
00067                              DWORD dwDesiredAccess);
00068 typedef BOOL (WINAPI *QueryServiceStatus_fn)(
00069                              SC_HANDLE hService,
00070                              LPSERVICE_STATUS lpServiceStatus);
00071 typedef BOOL (WINAPI *SetServiceStatus_fn)(SERVICE_STATUS_HANDLE,
00072                              LPSERVICE_STATUS);
00073 typedef BOOL (WINAPI *StartServiceA_fn)(
00074                              SC_HANDLE hService,
00075                              DWORD dwNumServiceArgs,
00076                              LPCTSTR* lpServiceArgVectors);
00077 
00078 /** Table of NT service related functions. */
00079 struct ServiceFunctions {
00080   bool loaded;
00081   ChangeServiceConfig2A_fn ChangeServiceConfig2A;
00082   CloseServiceHandle_fn    CloseServiceHandle;
00083   ControlService_fn        ControlService;
00084   CreateServiceA_fn        CreateServiceA;
00085   DeleteService_fn         DeleteService;
00086   OpenSCManagerA_fn        OpenSCManagerA;
00087   OpenServiceA_fn          OpenServiceA;
00088   QueryServiceStatus_fn    QueryServiceStatus;
00089   SetServiceStatus_fn      SetServiceStatus;
00090   StartServiceA_fn         StartServiceA;
00091 };
00092 
00093 
00094 class TorService : public QObject
00095 {
00096   Q_OBJECT
00097 
00098 public:
00099   /** Returns if services are supported. */
00100   static bool isSupported();
00101   /** Dynamically loads NT service related functions from advapi32.dll. */
00102   static bool loadServiceFunctions();
00103 
00104   /** Default ctor. */
00105   TorService(QObject* parent = 0);
00106   /** Default dtor. */
00107   ~TorService();
00108 
00109   /** Returns true if the Tor service is installed. */
00110   bool isInstalled();
00111   /** Returns true if the Tor service is running. */
00112   bool isRunning();
00113   /** Starts the Tor service. Emits started on success. */
00114   void start();
00115   /** Stops the Tor service. Emits finished on success. */
00116   bool stop();
00117   /** Returns the exit code of the last Tor service that finished. */
00118   int exitCode();
00119   /** Returns the exit status of the last Tor service that finished. */
00120   QProcess::ExitStatus exitStatus();
00121   /** Installs the Tor service. */
00122   bool install(const QString &torPath, const QString &torrc,
00123                quint16 controlPort);
00124   /** Removes the Tor service. */
00125   bool remove();
00126 
00127 signals:
00128   /** Called when the service gets started. */
00129   void started();
00130   /** Called when the service gets stopped. */
00131   void finished(int exitCode, QProcess::ExitStatus);
00132   /** Called when there is an error in starting the service. */
00133   void startFailed(QString error);
00134 
00135 private:
00136   /** Opens a handle to the Tor service. Returns NULL on error. */
00137   SC_HANDLE openService();
00138   /** Opens a handle to the service control manager. Returns NULL on error. */
00139   static SC_HANDLE openSCM();
00140   /** Closes the service <b>handle</b>. */
00141   static void closeHandle(SC_HANDLE handle);
00142   /** Gets the status of the Tor service. */
00143   DWORD status(); 
00144 
00145   /** Handle to the service control manager. */ 
00146   SC_HANDLE _scm;
00147   /** List of dynamically loaded NT service functions. */
00148   static ServiceFunctions _service_fns;
00149 };
00150 
00151 #endif
00152 

Generated on Wed Nov 26 21:04:20 2008 for Vidalia by  doxygen 1.5.7.1