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