2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <pcap.h> // for PCAP_ERRBUF_SIZE
37 #include "portability.h"
41 static SERVICE_STATUS_HANDLE service_status_handle;
42 static SERVICE_STATUS service_status;
44 static void WINAPI svc_main(DWORD argc, char **argv);
45 static void update_svc_status(DWORD state, DWORD progress_indicator);
50 SERVICE_TABLE_ENTRY ste[] =
52 { PROGRAM_NAME, svc_main },
55 char string[PCAP_ERRBUF_SIZE];
57 // This call is blocking. A new thread is created which will launch
58 // the svc_main() function
59 if ((rc = StartServiceCtrlDispatcher(ste)) == 0) {
60 pcap_fmt_errmsg_for_win32_err(string, sizeof (string),
61 GetLastError(), "StartServiceCtrlDispatcher() failed");
62 rpcapd_log(LOGPRIO_ERROR, "%s", string);
65 return rc; // FALSE if this is not started as a service
68 void WINAPI svc_control_handler(DWORD Opcode)
72 case SERVICE_CONTROL_STOP:
74 // XXX - is this sufficient to clean up the service?
75 // To be really honest, only the main socket and
76 // such these stuffs are cleared; however the threads
77 // that are running are not stopped.
78 // This can be seen by placing a breakpoint at the
79 // end of svc_main(), in which you will see that is
80 // never reached. However, as soon as you set the
81 // service status to "stopped", the
82 // StartServiceCtrlDispatcher() returns and the main
83 // thread ends. Then, Win32 has a good automatic
84 // cleanup, so that all the threads which are still
85 // running are stopped when the main thread ends.
87 send_shutdown_notification();
89 update_svc_status(SERVICE_STOP_PENDING, 0);
93 Pause and Continue have an usual meaning and they are used just to be able
94 to change the running parameters at run-time. In other words, they act
95 like the SIGHUP signal on UNIX. All the running threads continue to run and
96 they are not paused at all.
99 - CONTINUE re-reads the configuration file and creates the new threads that
100 can be needed according to the new configuration.
102 case SERVICE_CONTROL_PAUSE:
103 update_svc_status(SERVICE_PAUSED, 0);
106 case SERVICE_CONTROL_CONTINUE:
107 update_svc_status(SERVICE_RUNNING, 0);
109 // Tell the main loop to re-read the configuration.
111 send_reread_configuration_notification();
114 case SERVICE_CONTROL_INTERROGATE:
115 // Fall through to send current status.
116 // WARNING: not implemented
117 update_svc_status(SERVICE_RUNNING, 0);
118 MessageBox(NULL, "Not implemented", "warning", MB_OK);
121 case SERVICE_CONTROL_PARAMCHANGE:
123 // Tell the main loop to re-read the configuration.
125 send_reread_configuration_notification();
129 // Send current status.
133 void WINAPI svc_main(DWORD argc, char **argv)
135 service_status_handle = RegisterServiceCtrlHandler(PROGRAM_NAME, svc_control_handler);
137 if (!service_status_handle)
140 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
141 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_PARAMCHANGE;
142 // | SERVICE_ACCEPT_SHUTDOWN ;
143 update_svc_status(SERVICE_RUNNING, 0);
146 // Service requests until we're told to stop.
151 // It returned, so we were told to stop.
153 update_svc_status(SERVICE_STOPPED, 0);
157 update_svc_status(DWORD state, DWORD progress_indicator)
159 service_status.dwWin32ExitCode = NO_ERROR;
160 service_status.dwCurrentState = state;
161 service_status.dwCheckPoint = progress_indicator;
162 service_status.dwWaitHint = 0;
163 SetServiceStatus(service_status_handle, &service_status);
167 sc create rpcapd DisplayName= "Remote Packet Capture Protocol v.0 (experimental)" binpath= "C:\cvsroot\winpcap\wpcap\PRJ\Debug\rpcapd -d -f rpcapd.ini"
168 sc description rpcapd "Allows to capture traffic on this host from a remote machine."