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;
77 WSAStartup(MAKEWORD(1, 1), &wsaData);
80 #ifdef SHARKD_UNIX_SUPPORT
81 if (!strncmp(path, "unix:", 5))
83 struct sockaddr_un s_un;
88 if (strlen(path) + 1 > sizeof(s_un.sun_path))
89 return INVALID_SOCKET;
91 fd = socket(AF_UNIX, SOCK_STREAM, 0);
92 if (fd == INVALID_SOCKET)
93 return INVALID_SOCKET;
95 memset(&s_un, 0, sizeof(s_un));
96 s_un.sun_family = AF_UNIX;
97 g_strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path));
99 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
101 if (s_un.sun_path[0] == '@')
102 s_un.sun_path[0] = '\0';
104 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
107 return INVALID_SOCKET;
113 #ifdef SHARKD_TCP_SUPPORT
114 if (!strncmp(path, "tcp:", 4))
116 struct sockaddr_in s_in;
123 port_sep = strchr(path, ':');
125 return INVALID_SOCKET;
129 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
130 return INVALID_SOCKET;
133 /* Need to use WSASocket() to disable overlapped I/O operations,
134 this way on windows SOCKET can be used as HANDLE for stdin/stdout */
135 fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
137 fd = socket(AF_INET, SOCK_STREAM, 0);
139 if (fd == INVALID_SOCKET)
140 return INVALID_SOCKET;
142 s_in.sin_family = AF_INET;
143 s_in.sin_addr.s_addr = inet_addr(path);
144 s_in.sin_port = g_htons(port);
147 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
149 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
152 return INVALID_SOCKET;
158 return INVALID_SOCKET;
161 if (listen(fd, SOMAXCONN))
164 return INVALID_SOCKET;
171 sharkd_init(int argc, char **argv)
180 fprintf(stderr, "Usage: %s <socket>\n", argv[0]);
181 fprintf(stderr, "\n");
183 fprintf(stderr, "<socket> examples:\n");
184 #ifdef SHARKD_UNIX_SUPPORT
185 fprintf(stderr, " - unix:/tmp/sharkd.sock - listen on unix file /tmp/sharkd.sock\n");
187 #ifdef SHARKD_TCP_SUPPORT
188 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
190 fprintf(stderr, "\n");
195 signal(SIGCHLD, SIG_IGN);
198 if (!strcmp(argv[1], "-"))
204 fd = socket_init(argv[1]);
205 if (fd == INVALID_SOCKET)
211 /* all good - try to daemonize */
214 fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
231 return sharkd_session_main();
239 PROCESS_INFORMATION pi;
242 gunichar2 *commandline;
246 fd = accept(_server_fd, NULL, NULL);
247 if (fd == INVALID_SOCKET)
249 fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
253 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
258 /* redirect stdin, stdout to socket */
263 exit(sharkd_session_main());
268 fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
272 memset(&pi, 0, sizeof(pi));
273 memset(&si, 0, sizeof(si));
276 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
277 si.hStdInput = (HANDLE) fd;
278 si.hStdOutput = (HANDLE) fd;
279 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
281 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
282 commandline = g_utf8_to_utf16("sharkd.exe -", -1, NULL, NULL, NULL);
284 if (!CreateProcess(utf_8to16(exename), commandline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
286 fprintf(stderr, "CreateProcess(%s) failed\n", exename);
290 CloseHandle(pi.hThread);
303 * Editor modelines - http://www.wireshark.org/tools/modelines.html
308 * indent-tabs-mode: t
311 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
312 * :indentSize=8:tabSize=8:noTabs=false: