+#ifdef HAVE_MINGW
+extern char *identname;
+extern char *program_name;
+extern char **g_argv;
+
+static SC_HANDLE manager = NULL;
+static SC_HANDLE service = NULL;
+static SERVICE_STATUS status = {0};
+static SERVICE_STATUS_HANDLE statushandle = 0;
+
+bool install_service(void) {
+ char command[4096] = "";
+ char **argp;
+
+ manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+ if(!manager) {
+ logger(LOG_ERR, _("Could not open service manager: %s"), winerror(GetLastError()));
+ return false;
+ }
+
+ if(!strchr(program_name, '\\')) {
+ GetCurrentDirectory(sizeof(command), command);
+ strncat(command, "\\", sizeof(command));
+ }
+
+ strncat(command, program_name, sizeof(command));
+ for(argp = g_argv + 1; *argp; argp++) {
+ strncat(command, " ", sizeof(command));
+ strncat(command, *argp, sizeof(command));
+ }
+
+ service = CreateService(manager, identname, identname,
+ SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
+ command, "NDIS", NULL, NULL, NULL, NULL);
+
+ if(!service) {
+ logger(LOG_ERR, _("Could not create %s service: %s"), identname, winerror(GetLastError()));
+ return false;
+ }
+
+ logger(LOG_INFO, _("%s service installed"), identname);
+
+ if(!StartService(service, 0, NULL))
+ logger(LOG_WARNING, _("Could not start %s service: %s"), identname, winerror(GetLastError()));
+ else
+ logger(LOG_INFO, _("%s service started"), identname);
+
+ return true;
+}
+
+bool remove_service(void) {
+ manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+ if(!manager) {
+ logger(LOG_ERR, _("Could not open service manager: %s"), winerror(GetLastError()));
+ return false;
+ }
+
+ service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
+
+ if(!service) {
+ logger(LOG_ERR, _("Could not open %s service: %s"), identname, winerror(GetLastError()));
+ return false;
+ }
+
+ if(!ControlService(service, SERVICE_CONTROL_STOP, &status))
+ logger(LOG_ERR, _("Could not stop %s service: %s"), identname, winerror(GetLastError()));
+ else
+ logger(LOG_INFO, _("%s service stopped"), identname);
+
+ if(!DeleteService(service)) {
+ logger(LOG_ERR, _("Could not remove %s service: %s"), identname, winerror(GetLastError()));
+ return false;
+ }
+
+ logger(LOG_INFO, _("%s service removed"), identname);
+
+ return true;
+}
+
+DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
+ switch(request) {
+ case SERVICE_CONTROL_STOP:
+ logger(LOG_NOTICE, _("Got %s request"), "SERVICE_CONTROL_STOP");
+ running = false;
+ break;
+ case SERVICE_CONTROL_SHUTDOWN:
+ logger(LOG_NOTICE, _("Got %s request"), "SERVICE_CONTROL_SHUTDOWN");
+ running = false;
+ break;
+ default:
+ logger(LOG_WARNING, _("Got unexpected request %d"), request);
+ return ERROR_CALL_NOT_IMPLEMENTED;
+ }
+
+ if(!running) {
+ status.dwCurrentState = SERVICE_STOP_PENDING;
+ SetServiceStatus(statushandle, &status);
+ }
+
+ return NO_ERROR;
+}
+
+VOID WINAPI run_service(DWORD argc, LPTSTR* argv)