debug keys
[metze/wireshark/wip.git] / sharkd_daemon.c
1 /* sharkd_daemon.c
2  *
3  * Copyright (C) 2016 Jakub Zawadzki
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include <config.h>
13
14 #include <glib.h>
15
16 #include <stdio.h>
17 #include <errno.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <signal.h>
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #ifdef _WIN32
27 #include <wsutil/unicode-utils.h>
28 #include <wsutil/filesystem.h>
29 #endif
30
31 #include <wsutil/socket.h>
32 #include <wsutil/inet_addr.h>
33
34 #ifndef _WIN32
35 #include <sys/un.h>
36 #include <netinet/tcp.h>
37 #endif
38
39 #include <wsutil/strtoi.h>
40 #include <wsutil/win32-utils.h>
41
42 #include "sharkd.h"
43
44 #ifdef _WIN32
45 /* for windows support TCP sockets */
46 # define SHARKD_TCP_SUPPORT
47 #else
48 /* for other system support only local sockets */
49 # define SHARKD_UNIX_SUPPORT
50 #endif
51
52 static int _use_stdinout = 0;
53 static socket_handle_t _server_fd = INVALID_SOCKET;
54
55 static socket_handle_t
56 socket_init(char *path)
57 {
58         socket_handle_t fd = INVALID_SOCKET;
59
60 #ifdef _WIN32
61         WSADATA wsaData;
62         int result;
63
64         result = WSAStartup(MAKEWORD(1, 1), &wsaData);
65         if (result != 0) {
66                 g_warning("ERROR: WSAStartup failed with error: %d", result);
67                 return INVALID_SOCKET;
68         }
69 #endif
70
71 #ifdef SHARKD_UNIX_SUPPORT
72         if (!strncmp(path, "unix:", 5))
73         {
74                 struct sockaddr_un s_un;
75                 socklen_t s_un_len;
76
77                 path += 5;
78
79                 if (strlen(path) + 1 > sizeof(s_un.sun_path))
80                         return INVALID_SOCKET;
81
82                 fd = socket(AF_UNIX, SOCK_STREAM, 0);
83                 if (fd == INVALID_SOCKET)
84                         return INVALID_SOCKET;
85
86                 memset(&s_un, 0, sizeof(s_un));
87                 s_un.sun_family = AF_UNIX;
88                 g_strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path));
89
90                 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
91
92                 if (s_un.sun_path[0] == '@')
93                         s_un.sun_path[0] = '\0';
94
95                 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
96                 {
97                         closesocket(fd);
98                         return INVALID_SOCKET;
99                 }
100         }
101         else
102 #endif
103
104 #ifdef SHARKD_TCP_SUPPORT
105         if (!strncmp(path, "tcp:", 4))
106         {
107                 struct sockaddr_in s_in;
108                 int one = 1;
109                 char *port_sep;
110                 guint16 port;
111
112                 path += 4;
113
114                 port_sep = strchr(path, ':');
115                 if (!port_sep)
116                         return INVALID_SOCKET;
117
118                 *port_sep = '\0';
119
120                 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
121                         return INVALID_SOCKET;
122
123 #ifdef _WIN32
124                 /* Need to use WSASocket() to disable overlapped I/O operations,
125                    this way on windows SOCKET can be used as HANDLE for stdin/stdout */
126                 fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
127 #else
128                 fd = socket(AF_INET, SOCK_STREAM, 0);
129 #endif
130                 if (fd == INVALID_SOCKET)
131                         return INVALID_SOCKET;
132
133                 s_in.sin_family = AF_INET;
134                 ws_inet_pton4(path, &(s_in.sin_addr.s_addr));
135                 s_in.sin_port = g_htons(port);
136                 *port_sep = ':';
137
138                 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
139
140                 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
141                 {
142                         closesocket(fd);
143                         return INVALID_SOCKET;
144                 }
145         }
146         else
147 #endif
148         {
149                 return INVALID_SOCKET;
150         }
151
152         if (listen(fd, SOMAXCONN))
153         {
154                 closesocket(fd);
155                 return INVALID_SOCKET;
156         }
157
158         return fd;
159 }
160
161 int
162 sharkd_init(int argc, char **argv)
163 {
164 #ifndef _WIN32
165         pid_t pid;
166 #endif
167         socket_handle_t fd;
168
169         if (argc != 2)
170         {
171                 fprintf(stderr, "Usage: %s <-|socket>\n", argv[0]);
172                 fprintf(stderr, "\n");
173
174                 fprintf(stderr, "<socket> examples:\n");
175 #ifdef SHARKD_UNIX_SUPPORT
176                 fprintf(stderr, " - unix:/tmp/sharkd.sock - listen on unix file /tmp/sharkd.sock\n");
177 #endif
178 #ifdef SHARKD_TCP_SUPPORT
179                 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
180 #endif
181                 fprintf(stderr, "\n");
182                 return -1;
183         }
184
185 #ifndef _WIN32
186         signal(SIGCHLD, SIG_IGN);
187 #endif
188
189         if (!strcmp(argv[1], "-"))
190         {
191                 _use_stdinout = 1;
192         }
193         else
194         {
195                 fd = socket_init(argv[1]);
196                 if (fd == INVALID_SOCKET)
197                         return -1;
198                 _server_fd = fd;
199         }
200
201         if (!_use_stdinout)
202         {
203                 /* all good - try to daemonize */
204 #ifndef _WIN32
205                 pid = fork();
206                 if (pid == -1)
207                         fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
208
209                 if (pid != 0)
210                 {
211                         /* parent */
212                         exit(0);
213                 }
214 #endif
215         }
216
217         return 0;
218 }
219
220 int
221 sharkd_loop(void)
222 {
223         if (_use_stdinout)
224         {
225                 return sharkd_session_main();
226         }
227
228         while (1)
229         {
230 #ifndef _WIN32
231                 pid_t pid;
232 #else
233                 PROCESS_INFORMATION pi;
234                 STARTUPINFO si;
235                 char *exename;
236 #endif
237                 socket_handle_t fd;
238
239                 fd = accept(_server_fd, NULL, NULL);
240                 if (fd == INVALID_SOCKET)
241                 {
242                         fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
243                         continue;
244                 }
245
246                 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
247 #ifndef _WIN32
248                 pid = fork();
249                 if (pid == 0)
250                 {
251                         closesocket(_server_fd);
252                         /* redirect stdin, stdout to socket */
253                         dup2(fd, 0);
254                         dup2(fd, 1);
255                         close(fd);
256
257                         exit(sharkd_session_main());
258                 }
259
260                 if (pid == -1)
261                 {
262                         fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
263                 }
264
265 #else
266                 memset(&pi, 0, sizeof(pi));
267                 memset(&si, 0, sizeof(si));
268
269                 si.cb = sizeof(si);
270                 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
271                 si.hStdInput = (HANDLE) fd;
272                 si.hStdOutput = (HANDLE) fd;
273                 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
274
275                 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
276
277                 if (!win32_create_process(exename, "sharkd.exe -", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
278                 {
279                         fprintf(stderr, "win32_create_process(%s) failed\n", exename);
280                 }
281                 else
282                 {
283                         CloseHandle(pi.hThread);
284                 }
285
286                 g_free(exename);
287 #endif
288
289                 closesocket(fd);
290         }
291         return 0;
292 }
293
294 /*
295  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
296  *
297  * Local variables:
298  * c-basic-offset: 8
299  * tab-width: 8
300  * indent-tabs-mode: t
301  * End:
302  *
303  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
304  * :indentSize=8:tabSize=8:noTabs=false:
305  */