4 * The secure anycast tunneling protocol (satp) defines a protocol used
\r
5 * for communication between any combination of unicast and anycast
\r
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
\r
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
\r
8 * ethernet, ip, arp ...). satp directly includes cryptography and
\r
9 * message authentication based on the methodes used by SRTP. It is
\r
10 * intended to deliver a generic, scaleable and secure solution for
\r
11 * tunneling and relaying of packets of any protocol.
\r
14 * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
\r
15 * Christian Pointner <satp@wirdorange.org>
\r
17 * This file is part of Anytun.
\r
19 * Anytun is free software: you can redistribute it and/or modify
\r
20 * it under the terms of the GNU General Public License as published by
\r
21 * the Free Software Foundation, either version 3 of the License, or
\r
22 * any later version.
\r
24 * Anytun is distributed in the hope that it will be useful,
\r
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
27 * GNU General Public License for more details.
\r
29 * You should have received a copy of the GNU General Public License
\r
30 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
\r
37 #include <windows.h>
\r
39 #include "winService.h"
\r
41 #include "../anytunError.h"
\r
42 #include "../threadUtils.hpp"
\r
44 void WinService::install()
\r
46 SC_HANDLE schSCManager;
\r
47 SC_HANDLE schService;
\r
48 char szPath[MAX_PATH];
\r
50 if(!GetModuleFileNameA(NULL, szPath, MAX_PATH))
\r
51 AnytunError::throwErr() << "Error on GetModuleFileName: " << AnytunErrno(GetLastError());
\r
53 schSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
\r
54 if(NULL == schSCManager)
\r
55 AnytunError::throwErr() << "Error on OpenSCManager: " << AnytunErrno(GetLastError());
\r
57 schService = CreateServiceA(schSCManager, SVC_NAME, SVC_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
\r
58 SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szPath, NULL, NULL, NULL, NULL, NULL);
\r
59 if(schService == NULL) {
\r
60 CloseServiceHandle(schSCManager);
\r
61 AnytunError::throwErr() << "Error on CreateService: " << AnytunErrno(GetLastError());
\r
64 std::cout << "Service installed successfully" << std::endl;
\r
66 CloseServiceHandle(schService);
\r
67 CloseServiceHandle(schSCManager);
\r
70 void WinService::uninstall()
\r
72 SC_HANDLE schSCManager;
\r
73 SC_HANDLE schService;
\r
75 schSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
\r
76 if(NULL == schSCManager)
\r
77 AnytunError::throwErr() << "Error on OpenSCManager: " << AnytunErrno(GetLastError());
\r
79 schService = OpenServiceA(schSCManager, SVC_NAME, SERVICE_ALL_ACCESS);
\r
80 if(schService == NULL) {
\r
81 CloseServiceHandle(schSCManager);
\r
82 AnytunError::throwErr() << "Error on CreateService: " << AnytunErrno(GetLastError());
\r
85 if(!DeleteService(schService)) {
\r
86 CloseServiceHandle(schService);
\r
87 CloseServiceHandle(schSCManager);
\r
88 AnytunError::throwErr() << "Error on DeleteService: " << AnytunErrno(GetLastError());
\r
91 std::cout << "Service uninstalled successfully" << std::endl;
\r
93 CloseServiceHandle(schService);
\r
94 CloseServiceHandle(schSCManager);
\r
97 void WinService::start()
\r
99 SERVICE_TABLE_ENTRY DispatchTable[] = {
\r
100 {SVC_NAME, (LPSERVICE_MAIN_FUNCTION)WinService::main },
\r
104 if(!StartServiceCtrlDispatcherA(DispatchTable))
\r
105 AnytunError::throwErr() << "Error on StartServiceCtrlDispatcher: " << AnytunErrno(GetLastError());
\r
108 int real_main(int argc, char* argv[], WinService& service);
\r
110 VOID WINAPI WinService::main(DWORD dwArgc, LPTSTR *lpszArgv)
\r
112 WinService service;
\r
114 service.status_handle_ = RegisterServiceCtrlHandlerA(SVC_NAME, WinService::ctrlHandler);
\r
115 if(!service.status_handle_) {
\r
116 cLog.msg(Log::PRIO_ERROR) << "Error on RegisterServiceCtrlHandler: " << AnytunErrno(GetLastError());
\r
119 service.status_.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
\r
120 service.status_.dwServiceSpecificExitCode = 0;
\r
121 service.reportStatus(SERVICE_START_PENDING, NO_ERROR);
\r
123 real_main(dwArgc, lpszArgv, service);
\r
125 service.reportStatus(SERVICE_STOPPED, NO_ERROR);
\r
128 VOID WINAPI WinService::ctrlHandler(DWORD dwCtrl)
\r
130 gSignalController.inject(dwCtrl);
\r
133 int WinService::handleCtrlSignal(int sig, const std::string& msg)
\r
136 case SERVICE_CONTROL_STOP: {
\r
137 reportStatus(SERVICE_STOP_PENDING, NO_ERROR);
\r
138 cLog.msg(Log::PRIO_NOTICE) << "received service stop signal, exitting";
\r
141 case SERVICE_CONTROL_INTERROGATE: break;
\r
144 reportStatus(status_.dwCurrentState, NO_ERROR);
\r
149 void WinService::reportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode)
\r
151 static DWORD dwCheckPoint = 1;
\r
153 status_.dwCurrentState = dwCurrentState;
\r
154 status_.dwWin32ExitCode = dwWin32ExitCode;
\r
155 status_.dwWaitHint = 0;
\r
157 if((dwCurrentState == SERVICE_START_PENDING) ||
\r
158 (dwCurrentState == SERVICE_STOP_PENDING))
\r
159 status_.dwControlsAccepted = 0;
\r
161 status_.dwControlsAccepted = SERVICE_ACCEPT_STOP;
\r
163 if((dwCurrentState == SERVICE_RUNNING) ||
\r
164 (dwCurrentState == SERVICE_STOPPED))
\r
165 status_.dwCheckPoint = 0;
\r
167 status_.dwCheckPoint = dwCheckPoint++;
\r
169 SetServiceStatus(status_handle_, &status_);
\r
172 void WinService::initPrivs(std::string const& username, std::string const& groupname)
\r
177 void WinService::dropPrivs()
\r
182 void WinService::chroot(std::string const& dir)
\r
187 void WinService::daemonize()
\r
192 bool WinService::isDaemonized()
\r