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>
29 #include <wsutil/win32-utils.h>
32 #include <wsutil/socket.h>
33 #include <wsutil/inet_addr.h>
34 #include <wsutil/please_report_bug.h>
38 #include <netinet/tcp.h>
41 #include <wsutil/strtoi.h>
46 /* for windows support TCP sockets */
47 # define SHARKD_TCP_SUPPORT
49 /* for other system support only local sockets */
50 # define SHARKD_UNIX_SUPPORT
53 static int _use_stdinout = 0;
54 static socket_handle_t _server_fd = INVALID_SOCKET;
56 static socket_handle_t
57 socket_init(char *path)
59 socket_handle_t fd = INVALID_SOCKET;
62 err_msg = ws_init_sockets();
63 if (err_msg != NULL) {
64 g_warning("ERROR: %s", err_msg);
66 g_warning("%s", please_report_bug());
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;
238 fd = accept(_server_fd, NULL, NULL);
239 if (fd == INVALID_SOCKET)
241 fprintf(stderr, "cannot accept(): %s\n", g_strerror(errno));
245 /* wireshark is not ready for handling multiple capture files in single process, so fork(), and handle it in separate process */
250 closesocket(_server_fd);
251 /* redirect stdin, stdout to socket */
256 exit(sharkd_session_main());
261 fprintf(stderr, "cannot fork(): %s\n", g_strerror(errno));
265 memset(&pi, 0, sizeof(pi));
266 memset(&si, 0, sizeof(si));
269 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
270 si.hStdInput = (HANDLE) fd;
271 si.hStdOutput = (HANDLE) fd;
272 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
274 exename = g_strdup_printf("%s\\%s", get_progfile_dir(), "sharkd.exe");
276 if (!win32_create_process(exename, "sharkd.exe -", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
278 fprintf(stderr, "win32_create_process(%s) failed\n", exename);
282 CloseHandle(pi.hThread);
294 * Editor modelines - https://www.wireshark.org/tools/modelines.html
299 * indent-tabs-mode: t
302 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
303 * :indentSize=8:tabSize=8:noTabs=false: