/* * OpenVPN-GUI -- A Windows GUI for OpenVPN. * * Copyright (C) 2004 Mathias Sundman * * 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 program 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 program (see the file COPYING included with this * distribution); if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include "config.h" #include "tray.h" #include "service.h" #include "openvpn.h" #include "options.h" #include "scripts.h" #include "main.h" #include "openvpn-gui-res.h" #include "localization.h" extern options_t o; int MyStartService() { SC_HANDLE schSCManager; SC_HANDLE schService; SERVICE_STATUS ssStatus; DWORD dwOldCheckPoint; DWORD dwStartTickCount; DWORD dwWaitTime; int i; /* Set Service Status = Connecting */ o.service_state = service_connecting; SetServiceMenuStatus(); CheckAndSetTrayIcon(); // Open a handle to the SC Manager database. schSCManager = OpenSCManager( NULL, // local machine NULL, // ServicesActive database SC_MANAGER_CONNECT); // Connect rights if (NULL == schSCManager) { /* open SC manager failed */ ShowLocalizedMsg(IDS_ERR_OPEN_SCMGR); goto failed; } schService = OpenService( schSCManager, // SCM database _T("OpenVPNService"), // service name SERVICE_START | SERVICE_QUERY_STATUS); if (schService == NULL) { /* can't open VPN service */ ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE); goto failed; } /* Run Pre-connect script */ for (i=0; i 5000 ) dwWaitTime = 5000; Sleep( dwWaitTime ); // Check the status again. if (!QueryServiceStatus( schService, // handle to service &ssStatus) ) // address of structure break; if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) { // The service is making progress. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; } else { if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) { // No progress made within the wait hint break; } } } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); if (ssStatus.dwCurrentState != SERVICE_RUNNING) { /* service hasn't started */ ShowLocalizedMsg(IDS_ERR_SERVICE_START_FAILED); goto failed; } /* Run Connect script */ for (i=0; i