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>
40 #include <wsutil/win32-utils.h>
45 /* for windows support TCP sockets */
46 # define SHARKD_TCP_SUPPORT
48 /* for other system support only local sockets */
49 # define SHARKD_UNIX_SUPPORT
52 static int _use_stdinout = 0;
53 static socket_handle_t _server_fd = INVALID_SOCKET;
55 static socket_handle_t
56 socket_init(char *path)
58 socket_handle_t fd = INVALID_SOCKET;
64 result = WSAStartup(MAKEWORD(1, 1), &wsaData);
66 g_warning("ERROR: WSAStartup failed with error: %d", result);
67 return INVALID_SOCKET;
71 #ifdef SHARKD_UNIX_SUPPORT
72 if (!strncmp(path, "unix:", 5))
74 struct sockaddr_un s_un;
79 if (strlen(path) + 1 > sizeof(s_un.sun_path))
80 return INVALID_SOCKET;
82 fd = socket(AF_UNIX, SOCK_STREAM, 0);
83 if (fd == INVALID_SOCKET)
84 return INVALID_SOCKET;
86 memset(&s_un, 0, sizeof(s_un));
87 s_un.sun_family = AF_UNIX;
88 g_strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path));
90 s_un_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s_un.sun_path));
92 if (s_un.sun_path[0] == '@')
93 s_un.sun_path[0] = '\0';
95 if (bind(fd, (struct sockaddr *) &s_un, s_un_len))
98 return INVALID_SOCKET;
104 #ifdef SHARKD_TCP_SUPPORT
105 if (!strncmp(path, "tcp:", 4))
107 struct sockaddr_in s_in;
114 port_sep = strchr(path, ':');
116 return INVALID_SOCKET;
120 if (ws_strtou16(port_sep + 1, NULL, &port) == FALSE)
121 return INVALID_SOCKET;
124 /* Need to use WSASocket() to disable overlapped I/O operations,
125 this way on windows SOCKET can be used as HANDLE for stdin/stdout */
126 fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
128 fd = socket(AF_INET, SOCK_STREAM, 0);
130 if (fd == INVALID_SOCKET)
131 return INVALID_SOCKET;
133 s_in.sin_family = AF_INET;
134 ws_inet_pton4(path, &(s_in.sin_addr.s_addr));
135 s_in.sin_port = g_htons(port);
138 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
140 if (bind(fd, (struct sockaddr *) &s_in, sizeof(struct sockaddr_in)))
143 return INVALID_SOCKET;
149 return INVALID_SOCKET;
152 if (listen(fd, SOMAXCONN))
155 return INVALID_SOCKET;
162 sharkd_init(int argc, char **argv)
171 fprintf(stderr, "Usage: %s <-|socket>\n", argv[0]);
172 fprintf(stderr, "\n");
174 fprintf(stderr, "<socket> examples:\n");
175 #ifdef SHARKD_UNIX_SUPPORT
176 fprintf(stderr, " - unix:/tmp/sharkd.sock - listen on unix file /tmp/sharkd.sock\n");
178 #ifdef SHARKD_TCP_SUPPORT
179 fprintf(stderr, " - tcp:127.0.0.1:4446 - listen on TCP port 4446\n");
181 fprintf(stderr, "\n");
186 signal(SIGCHLD, SIG_IGN);
189 if (!strcmp(argv[1], "-"))
195 fd = socket_init(argv[1]);
196 if (fd == INVALID_SOCKET)
203 /* all good - try to daemonize */
207 fprintf(stderr, "cannot go to background fork() failed: %s\n", g_strerror(errno));
225 return sharkd_session_main();
233 PROCESS_INFORMATION pi;
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");
277 if (!win32_create_process(exename, "sharkd.exe -", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
279 fprintf(stderr, "win32_create_process(%s) failed\n", exename);
283 CloseHandle(pi.hThread);
295 * Editor modelines - http://www.wireshark.org/tools/modelines.html
300 * indent-tabs-mode: t
303 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
304 * :indentSize=8:tabSize=8:noTabs=false: