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>
44 #include <wsutil/inet_addr.h>
48 #include <netinet/tcp.h>
51 #include <wsutil/strtoi.h>
56 /* for windows support TCP sockets */
57 # define SHARKD_TCP_SUPPORT
59 /* for other system support only local sockets */
60 # define SHARKD_UNIX_SUPPORT
63 static int _use_stdinout = 0;
64 static socket_handle_t _server_fd = INVALID_SOCKET;
66 static socket_handle_t
67 socket_init(char *path)
69 socket_handle_t fd = INVALID_SOCKET;
75 result = WSAStartup(MAKEWORD(1, 1), &wsaData);
77 g_warning("ERROR: WSAStartup failed with error: %d", result);
78 return INVALID_SOCKET;
82 #ifdef SHARKD_UNIX_SUPPORT
83 if (!strncmp(path, "unix:", 5))
85 struct sockaddr_un s_un;
90 if (strlen(path) + 1 > sizeof(s_un.sun_path))
91 return INVALID_SOCKET;
93 fd = socket(AF_UNIX, SOCK_STREAM, 0);
94 if (fd == INVALID_SOCKET)
95 return INVALID_SOCKET;
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));
101 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
103 if (s_un.sun_path[0] == '@')
104 s_un.sun_path[0] = '\0';
106 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
109 return INVALID_SOCKET;
115 #ifdef SHARKD_TCP_SUPPORT
116 if (!strncmp(path, "tcp:", 4))
118 struct sockaddr_in s_in;
125 port_sep = strchr(path, ':');
127 return INVALID_SOCKET;
131 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
132 return INVALID_SOCKET;
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);
139 fd = socket(AF_INET, SOCK_STREAM, 0);
141 if (fd == INVALID_SOCKET)
142 return INVALID_SOCKET;
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);
149 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
151 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
154 return INVALID_SOCKET;
160 return INVALID_SOCKET;
163 if (listen(fd, SOMAXCONN))
166 return INVALID_SOCKET;
173 sharkd_init(int argc, char **argv)
182 fprintf(stderr, "Usage: %s <-|socket>\n", argv[0]);
183 fprintf(stderr, "\n");
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");
189 #ifdef SHARKD_TCP_SUPPORT
190 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
192 fprintf(stderr, "\n");
197 signal(SIGCHLD, SIG_IGN);
200 if (!strcmp(argv[1], "-"))
206 fd = socket_init(argv[1]);
207 if (fd == INVALID_SOCKET)
214 /* all good - try to daemonize */
218 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 closesocket(_server_fd);
264 /* redirect stdin, stdout to socket */
269 exit(sharkd_session_main());
274 fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
278 memset(&pi, 0, sizeof(pi));
279 memset(&si, 0, 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);
287 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
288 commandline = g_utf8_to_utf16("sharkd.exe -", -1, NULL, NULL, NULL);
290 if (!CreateProcess(utf_8to16(exename), commandline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
292 fprintf(stderr, "CreateProcess(%s) failed\n", exename);
296 CloseHandle(pi.hThread);
309 * Editor modelines - http://www.wireshark.org/tools/modelines.html
314 * indent-tabs-mode: t
317 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
318 * :indentSize=8:tabSize=8:noTabs=false: