3 * Copyright (C) 2016 Jakub Zawadzki
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
27 #include <wsutil/unicode-utils.h>
28 #include <wsutil/filesystem.h>
31 #include <wsutil/socket.h>
32 #include <wsutil/inet_addr.h>
36 #include <netinet/tcp.h>
39 #include <wsutil/strtoi.h>
44 /* for windows support TCP sockets */
45 # define SHARKD_TCP_SUPPORT
47 /* for other system support only local sockets */
48 # define SHARKD_UNIX_SUPPORT
51 static int _use_stdinout = 0;
52 static socket_handle_t _server_fd = INVALID_SOCKET;
54 static socket_handle_t
55 socket_init(char *path)
57 socket_handle_t fd = INVALID_SOCKET;
63 result = WSAStartup(MAKEWORD(1, 1), &wsaData);
65 g_warning("ERROR: WSAStartup failed with error: %d", result);
66 return INVALID_SOCKET;
70 #ifdef SHARKD_UNIX_SUPPORT
71 if (!strncmp(path, "unix:", 5))
73 struct sockaddr_un s_un;
78 if (strlen(path) + 1 > sizeof(s_un.sun_path))
79 return INVALID_SOCKET;
81 fd = socket(AF_UNIX, SOCK_STREAM, 0);
82 if (fd == INVALID_SOCKET)
83 return INVALID_SOCKET;
85 memset(&s_un, 0, sizeof(s_un));
86 s_un.sun_family = AF_UNIX;
87 g_strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path));
89 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
91 if (s_un.sun_path[0] == '@')
92 s_un.sun_path[0] = '\0';
94 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
97 return INVALID_SOCKET;
103 #ifdef SHARKD_TCP_SUPPORT
104 if (!strncmp(path, "tcp:", 4))
106 struct sockaddr_in s_in;
113 port_sep = strchr(path, ':');
115 return INVALID_SOCKET;
119 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
120 return INVALID_SOCKET;
123 /* Need to use WSASocket() to disable overlapped I/O operations,
124 this way on windows SOCKET can be used as HANDLE for stdin/stdout */
125 fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
127 fd = socket(AF_INET, SOCK_STREAM, 0);
129 if (fd == INVALID_SOCKET)
130 return INVALID_SOCKET;
132 s_in.sin_family = AF_INET;
133 ws_inet_pton4(path, &(s_in.sin_addr.s_addr));
134 s_in.sin_port = g_htons(port);
137 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
139 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
142 return INVALID_SOCKET;
148 return INVALID_SOCKET;
151 if (listen(fd, SOMAXCONN))
154 return INVALID_SOCKET;
161 sharkd_init(int argc, char **argv)
170 fprintf(stderr, "Usage: %s <-|socket>\n", argv[0]);
171 fprintf(stderr, "\n");
173 fprintf(stderr, "<socket> examples:\n");
174 #ifdef SHARKD_UNIX_SUPPORT
175 fprintf(stderr, " - unix:/tmp/sharkd.sock - listen on unix file /tmp/sharkd.sock\n");
177 #ifdef SHARKD_TCP_SUPPORT
178 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
180 fprintf(stderr, "\n");
185 signal(SIGCHLD, SIG_IGN);
188 if (!strcmp(argv[1], "-"))
194 fd = socket_init(argv[1]);
195 if (fd == INVALID_SOCKET)
202 /* all good - try to daemonize */
206 fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
224 return sharkd_session_main();
232 PROCESS_INFORMATION pi;
235 gunichar2 *commandline;
239 fd = accept(_server_fd, NULL, NULL);
240 if (fd == INVALID_SOCKET)
242 fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
246 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
251 closesocket(_server_fd);
252 /* redirect stdin, stdout to socket */
257 exit(sharkd_session_main());
262 fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
266 memset(&pi, 0, sizeof(pi));
267 memset(&si, 0, sizeof(si));
270 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
271 si.hStdInput = (HANDLE) fd;
272 si.hStdOutput = (HANDLE) fd;
273 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
275 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
276 commandline = g_utf8_to_utf16("sharkd.exe -", -1, NULL, NULL, NULL);
278 if (!CreateProcess(utf_8to16(exename), commandline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
280 fprintf(stderr, "CreateProcess(%s) failed\n", exename);
284 CloseHandle(pi.hThread);
297 * Editor modelines - http://www.wireshark.org/tools/modelines.html
302 * indent-tabs-mode: t
305 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
306 * :indentSize=8:tabSize=8:noTabs=false: