3 * Copyright (C) 2016 Jakub Zawadzki
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
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.
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.
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.
39 #include <wsutil/unicode-utils.h>
40 #include <wsutil/filesystem.h>
43 #include <wsutil/socket.h>
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
51 #include <netinet/tcp.h>
54 #include <wsutil/strtoi.h>
59 /* for windows support TCP sockets */
60 # define SHARKD_TCP_SUPPORT
62 /* for other system support only local sockets */
63 # define SHARKD_UNIX_SUPPORT
66 static int _use_stdinout = 0;
67 static socket_handle_t _server_fd = INVALID_SOCKET;
69 static socket_handle_t
70 socket_init(char *path)
72 socket_handle_t fd = INVALID_SOCKET;
78 result = WSAStartup(MAKEWORD(1, 1), &wsaData);
80 g_warning("ERROR: WSAStartup failed with error: %d", result);
81 return INVALID_SOCKET;
85 #ifdef SHARKD_UNIX_SUPPORT
86 if (!strncmp(path, "unix:", 5))
88 struct sockaddr_un s_un;
93 if (strlen(path) + 1 > sizeof(s_un.sun_path))
94 return INVALID_SOCKET;
96 fd = socket(AF_UNIX, SOCK_STREAM, 0);
97 if (fd == INVALID_SOCKET)
98 return INVALID_SOCKET;
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));
104 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
106 if (s_un.sun_path[0] == '@')
107 s_un.sun_path[0] = '\0';
109 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
112 return INVALID_SOCKET;
118 #ifdef SHARKD_TCP_SUPPORT
119 if (!strncmp(path, "tcp:", 4))
121 struct sockaddr_in s_in;
128 port_sep = strchr(path, ':');
130 return INVALID_SOCKET;
134 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
135 return INVALID_SOCKET;
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);
142 fd = socket(AF_INET, SOCK_STREAM, 0);
144 if (fd == INVALID_SOCKET)
145 return INVALID_SOCKET;
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);
152 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
154 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
157 return INVALID_SOCKET;
163 return INVALID_SOCKET;
166 if (listen(fd, SOMAXCONN))
169 return INVALID_SOCKET;
176 sharkd_init(int argc, char **argv)
185 fprintf(stderr, "Usage: %s <socket>\n", argv[0]);
186 fprintf(stderr, "\n");
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");
192 #ifdef SHARKD_TCP_SUPPORT
193 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
195 fprintf(stderr, "\n");
200 signal(SIGCHLD, SIG_IGN);
203 if (!strcmp(argv[1], "-"))
209 fd = socket_init(argv[1]);
210 if (fd == INVALID_SOCKET)
216 /* all good - try to daemonize */
219 fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
236 return sharkd_session_main();
244 PROCESS_INFORMATION pi;
247 gunichar2 *commandline;
251 fd = accept(_server_fd, NULL, NULL);
252 if (fd == INVALID_SOCKET)
254 fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
258 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
263 /* redirect stdin, stdout to socket */
268 exit(sharkd_session_main());
273 fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
277 memset(&pi, 0, sizeof(pi));
278 memset(&si, 0, 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);
286 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
287 commandline = g_utf8_to_utf16("sharkd.exe -", -1, NULL, NULL, NULL);
289 if (!CreateProcess(utf_8to16(exename), commandline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
291 fprintf(stderr, "CreateProcess(%s) failed\n", exename);
295 CloseHandle(pi.hThread);
308 * Editor modelines - http://www.wireshark.org/tools/modelines.html
313 * indent-tabs-mode: t
316 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
317 * :indentSize=8:tabSize=8:noTabs=false: