tools: add automatic C skeleton dissector generator.
[gd/wireshark/.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 #include <wsutil/win32-utils.h>
30 #endif
31
32 #include <wsutil/socket.h>
33 #include <wsutil/inet_addr.h>
34 #include <wsutil/please_report_bug.h>
35
36 #ifndef _WIN32
37 #include <sys/un.h>
38 #include <netinet/tcp.h>
39 #endif
40
41 #include <wsutil/strtoi.h>
42
43 #include "sharkd.h"
44
45 #ifdef _WIN32
46 /* for windows support TCP sockets */
47 # define SHARKD_TCP_SUPPORT
48 #else
49 /* for other system support only local sockets */
50 # define SHARKD_UNIX_SUPPORT
51 #endif
52
53 static int _use_stdinout = 0;
54 static socket_handle_t _server_fd = INVALID_SOCKET;
55
56 static socket_handle_t
57 socket_init(char *path)
58 {
59         socket_handle_t fd = INVALID_SOCKET;
60         char *err_msg;
61
62         err_msg = ws_init_sockets();
63         if (err_msg != NULL) {
64                 g_warning("ERROR: %s", err_msg);
65                 g_free(err_msg);
66                 g_warning("%s", please_report_bug());
67                 return fd;
68         }
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 #endif
236                 socket_handle_t fd;
237
238                 fd = accept(_server_fd, NULL, NULL);
239                 if (fd == INVALID_SOCKET)
240                 {
241                         fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
242                         continue;
243                 }
244
245                 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
246 #ifndef _WIN32
247                 pid = fork();
248                 if (pid == 0)
249                 {
250                         closesocket(_server_fd);
251                         /* redirect stdin, stdout to socket */
252                         dup2(fd, 0);
253                         dup2(fd, 1);
254                         close(fd);
255
256                         exit(sharkd_session_main());
257                 }
258
259                 if (pid == -1)
260                 {
261                         fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
262                 }
263
264 #else
265                 memset(&pi, 0, sizeof(pi));
266                 memset(&si, 0, sizeof(si));
267
268                 si.cb = sizeof(si);
269                 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
270                 si.hStdInput = (HANDLE) fd;
271                 si.hStdOutput = (HANDLE) fd;
272                 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
273
274                 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
275
276                 if (!win32_create_process(exename, "sharkd.exe -", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
277                 {
278                         fprintf(stderr, "win32_create_process(%s) failed\n", exename);
279                 }
280                 else
281                 {
282                         CloseHandle(pi.hThread);
283                 }
284
285                 g_free(exename);
286 #endif
287
288                 closesocket(fd);
289         }
290         return 0;
291 }
292
293 /*
294  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
295  *
296  * Local variables:
297  * c-basic-offset: 8
298  * tab-width: 8
299  * indent-tabs-mode: t
300  * End:
301  *
302  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
303  * :indentSize=8:tabSize=8:noTabs=false:
304  */