Vidalia 0.2.12

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