CMake: Remove SH from the packate list.
[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
41 #include "sharkd.h"
42
43 #ifdef _WIN32
44 /* for windows support TCP sockets */
45 # define SHARKD_TCP_SUPPORT
46 #else
47 /* for other system support only local sockets */
48 # define SHARKD_UNIX_SUPPORT
49 #endif
50
51 static int _use_stdinout = 0;
52 static socket_handle_t _server_fd = INVALID_SOCKET;
53
54 static socket_handle_t
55 socket_init(char *path)
56 {
57         socket_handle_t fd = INVALID_SOCKET;
58
59 #ifdef _WIN32
60         WSADATA wsaData;
61         int result;
62
63         result = WSAStartup(MAKEWORD(1, 1), &wsaData);
64         if (result != 0) {
65                 g_warning("ERROR: WSAStartup failed with error: %d", result);
66                 return INVALID_SOCKET;
67         }
68 #endif
69
70 #ifdef SHARKD_UNIX_SUPPORT
71         if (!strncmp(path, "unix:", 5))
72         {
73                 struct sockaddr_un s_un;
74                 socklen_t s_un_len;
75
76                 path += 5;
77
78                 if (strlen(path) + 1 > sizeof(s_un.sun_path))
79                         return INVALID_SOCKET;
80
81                 fd = socket(AF_UNIX, SOCK_STREAM, 0);
82                 if (fd == INVALID_SOCKET)
83                         return INVALID_SOCKET;
84
85                 memset(&s_un, 0, sizeof(s_un));
86                 s_un.sun_family = AF_UNIX;
87                 g_strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path));
88
89                 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
90
91                 if (s_un.sun_path[0] == '@')
92                         s_un.sun_path[0] = '\0';
93
94                 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
95                 {
96                         closesocket(fd);
97                         return INVALID_SOCKET;
98                 }
99         }
100         else
101 #endif
102
103 #ifdef SHARKD_TCP_SUPPORT
104         if (!strncmp(path, "tcp:", 4))
105         {
106                 struct sockaddr_in s_in;
107                 int one = 1;
108                 char *port_sep;
109                 guint16 port;
110
111                 path += 4;
112
113                 port_sep = strchr(path, ':');
114                 if (!port_sep)
115                         return INVALID_SOCKET;
116
117                 *port_sep = '\0';
118
119                 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
120                         return INVALID_SOCKET;
121
122 #ifdef _WIN32
123                 /* Need to use WSASocket() to disable overlapped I/O operations,
124                    this way on windows SOCKET can be used as HANDLE for stdin/stdout */
125                 fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
126 #else
127                 fd = socket(AF_INET, SOCK_STREAM, 0);
128 #endif
129                 if (fd == INVALID_SOCKET)
130                         return INVALID_SOCKET;
131
132                 s_in.sin_family = AF_INET;
133                 ws_inet_pton4(path, &(s_in.sin_addr.s_addr));
134                 s_in.sin_port = g_htons(port);
135                 *port_sep = ':';
136
137                 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
138
139                 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
140                 {
141                         closesocket(fd);
142                         return INVALID_SOCKET;
143                 }
144         }
145         else
146 #endif
147         {
148                 return INVALID_SOCKET;
149         }
150
151         if (listen(fd, SOMAXCONN))
152         {
153                 closesocket(fd);
154                 return INVALID_SOCKET;
155         }
156
157         return fd;
158 }
159
160 int
161 sharkd_init(int argc, char **argv)
162 {
163 #ifndef _WIN32
164         pid_t pid;
165 #endif
166         socket_handle_t fd;
167
168         if (argc != 2)
169         {
170                 fprintf(stderr, "Usage: %s <-|socket>\n", argv[0]);
171                 fprintf(stderr, "\n");
172
173                 fprintf(stderr, "<socket> examples:\n");
174 #ifdef SHARKD_UNIX_SUPPORT
175                 fprintf(stderr, " - unix:/tmp/sharkd.sock - listen on unix file /tmp/sharkd.sock\n");
176 #endif
177 #ifdef SHARKD_TCP_SUPPORT
178                 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
179 #endif
180                 fprintf(stderr, "\n");
181                 return -1;
182         }
183
184 #ifndef _WIN32
185         signal(SIGCHLD, SIG_IGN);
186 #endif
187
188         if (!strcmp(argv[1], "-"))
189         {
190                 _use_stdinout = 1;
191         }
192         else
193         {
194                 fd = socket_init(argv[1]);
195                 if (fd == INVALID_SOCKET)
196                         return -1;
197                 _server_fd = fd;
198         }
199
200         if (!_use_stdinout)
201         {
202                 /* all good - try to daemonize */
203 #ifndef _WIN32
204                 pid = fork();
205                 if (pid == -1)
206                         fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
207
208                 if (pid != 0)
209                 {
210                         /* parent */
211                         exit(0);
212                 }
213 #endif
214         }
215
216         return 0;
217 }
218
219 int
220 sharkd_loop(void)
221 {
222         if (_use_stdinout)
223         {
224                 return sharkd_session_main();
225         }
226
227         while (1)
228         {
229 #ifndef _WIN32
230                 pid_t pid;
231 #else
232                 PROCESS_INFORMATION pi;
233                 STARTUPINFO si;
234                 char *exename;
235                 gunichar2 *commandline;
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                 commandline = g_utf8_to_utf16("sharkd.exe -", -1, NULL, NULL, NULL);
277
278                 if (!CreateProcess(utf_8to16(exename), commandline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
279                 {
280                         fprintf(stderr, "CreateProcess(%s) failed\n", exename);
281                 }
282                 else
283                 {
284                         CloseHandle(pi.hThread);
285                 }
286
287                 g_free(exename);
288                 g_free(commandline);
289 #endif
290
291                 closesocket(fd);
292         }
293         return 0;
294 }
295
296 /*
297  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
298  *
299  * Local variables:
300  * c-basic-offset: 8
301  * tab-width: 8
302  * indent-tabs-mode: t
303  * End:
304  *
305  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
306  * :indentSize=8:tabSize=8:noTabs=false:
307  */