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