2 * udpdump is an extcap tool used to get packets exported from a source (like a network device or a GSMTAP producer) that
3 * are dumped to a pcap file
5 * Copyright 2016, Dario Lombardo <lomato@gmail.com>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include <extcap/extcap-base.h>
19 #include <glib/gprintf.h>
22 #ifdef HAVE_SYS_TIME_H
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
45 #include <writecap/pcapio.h>
46 #include <wiretap/wtap.h>
47 #include <wsutil/strtoi.h>
48 #include <wsutil/inet_addr.h>
49 #include <wsutil/filesystem.h>
51 #define PCAP_SNAPLEN 0xffff
53 #define UDPDUMP_DEFAULT_PORT 5555
55 #define UDPDUMP_EXTCAP_INTERFACE "udpdump"
56 #define UDPDUMP_VERSION_MAJOR "0"
57 #define UDPDUMP_VERSION_MINOR "1"
58 #define UDPDUMP_VERSION_RELEASE "0"
60 #define PKT_BUF_SIZE 65535
62 #define UDPDUMP_EXPORT_HEADER_LEN 40
64 /* Tags (from exported_pdu.h) */
65 #define EXP_PDU_TAG_PROTO_NAME 12
66 #define EXP_PDU_TAG_IPV4_SRC 20
67 #define EXP_PDU_TAG_IPV4_DST 21
68 #define EXP_PDU_TAG_SRC_PORT 25
69 #define EXP_PDU_TAG_DST_PORT 26
71 static gboolean run_loop = TRUE;
74 EXTCAP_BASE_OPTIONS_ENUM,
81 static struct option longopts[] = {
83 /* Generic application options */
84 { "help", no_argument, NULL, OPT_HELP},
85 { "version", no_argument, NULL, OPT_VERSION},
86 /* Interfaces options */
87 { "port", required_argument, NULL, OPT_PORT},
88 { "payload", required_argument, NULL, OPT_PAYLOAD},
92 static int list_config(char *interface)
97 g_warning("No interface specified.");
101 printf("arg {number=%u}{call=--port}{display=Listen port}"
102 "{type=unsigned}{range=1,65535}{default=%u}{tooltip=The port the receiver listens on}\n",
103 inc++, UDPDUMP_DEFAULT_PORT);
104 printf("arg {number=%u}{call=--payload}{display=Payload type}"
105 "{type=string}{default=data}{tooltip=The type used to describe the payload in the exported pdu format}\n",
108 extcap_config_debug(&inc);
113 static int setup_listener(const guint16 port, socket_handle_t* sock)
116 struct sockaddr_in serveraddr;
118 struct timeval timeout = { 1, 0 };
121 *sock = socket(AF_INET, SOCK_DGRAM, 0);
123 if (*sock == INVALID_SOCKET) {
124 g_warning("Error opening socket: %s", strerror(errno));
129 if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, (socklen_t)sizeof(int)) < 0) {
130 g_warning("Can't set socket option SO_REUSEADDR: %s", strerror(errno));
131 goto cleanup_setup_listener;
135 if (setsockopt (*sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, (socklen_t)sizeof(timeout)) < 0) {
136 g_warning("Can't set socket option SO_RCVTIMEO: %s", strerror(errno));
137 goto cleanup_setup_listener;
141 memset(&serveraddr, 0x0, sizeof(serveraddr));
142 serveraddr.sin_family = AF_INET;
143 serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
144 serveraddr.sin_port = htons(port);
146 if (bind(*sock, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(serveraddr)) < 0) {
147 g_warning("Error on binding: %s", strerror(errno));
148 goto cleanup_setup_listener;
153 cleanup_setup_listener:
159 static void exit_from_loop(int signo _U_)
161 g_warning("Exiting from main loop");
165 static int setup_dumpfile(const char* fifo, FILE** fp)
167 guint64 bytes_written = 0;
170 if (!g_strcmp0(fifo, "-")) {
175 *fp = fopen(fifo, "wb");
177 g_warning("Error creating output file: %s", g_strerror(errno));
181 if (!libpcap_write_file_header(*fp, 252, PCAP_SNAPLEN, FALSE, &bytes_written, &err)) {
182 g_warning("Can't write pcap file header: %s", g_strerror(err));
189 static void add_proto_name(char* mbuf, guint* offset, const char* proto_name)
191 size_t proto_str_len = strlen(proto_name);
192 guint16 proto_name_len = (guint16)((proto_str_len + 3) & 0xfffffffc);
195 mbuf[*offset+1] = EXP_PDU_TAG_PROTO_NAME;
197 mbuf[*offset] = proto_name_len >> 8;
198 mbuf[*offset+1] = proto_name_len & 0xff;
201 memcpy(mbuf + *offset, proto_name, proto_str_len);
202 *offset += proto_name_len;
205 static void add_ip_source_address(char* mbuf, guint* offset, uint32_t source_address)
207 mbuf[*offset] = 0x00;
208 mbuf[*offset+1] = EXP_PDU_TAG_IPV4_SRC;
212 memcpy(mbuf + *offset, &source_address, 4);
216 static void add_ip_dest_address(char* mbuf, guint* offset, uint32_t dest_address)
219 mbuf[*offset+1] = EXP_PDU_TAG_IPV4_DST;
223 memcpy(mbuf + *offset, &dest_address, 4);
227 static void add_udp_source_port(char* mbuf, guint* offset, uint16_t src_port)
229 uint32_t port = htonl(src_port);
232 mbuf[*offset+1] = EXP_PDU_TAG_SRC_PORT;
236 memcpy(mbuf + *offset, &port, 4);
240 static void add_udp_dst_port(char* mbuf, guint* offset, uint16_t dst_port)
242 uint32_t port = htonl(dst_port);
245 mbuf[*offset+1] = EXP_PDU_TAG_DST_PORT;
249 memcpy(mbuf + *offset, &port, 4);
253 static void add_end_options(char* mbuf, guint* offset)
255 memset(mbuf + *offset, 0x0, 4);
259 static int dump_packet(const char* proto_name, const guint16 listenport, const char* buf,
260 const ssize_t buflen, const struct sockaddr_in clientaddr, FILE* fp)
264 time_t curtime = time(NULL);
265 guint64 bytes_written = 0;
267 int ret = EXIT_SUCCESS;
269 /* The space we need is the standard header + variable lengths */
270 mbuf = (char*)g_malloc0(UDPDUMP_EXPORT_HEADER_LEN + ((strlen(proto_name) + 3) & 0xfffffffc) + buflen);
272 add_proto_name(mbuf, &offset, proto_name);
273 add_ip_source_address(mbuf, &offset, clientaddr.sin_addr.s_addr);
274 add_ip_dest_address(mbuf, &offset, WS_IN4_LOOPBACK);
275 add_udp_source_port(mbuf, &offset, clientaddr.sin_port);
276 add_udp_dst_port(mbuf, &offset, listenport);
277 add_end_options(mbuf, &offset);
279 memcpy(mbuf + offset, buf, buflen);
280 offset += (guint)buflen;
282 if (!libpcap_write_packet(fp, curtime, (guint32)(curtime / 1000), offset, offset, mbuf, &bytes_written, &err)) {
283 g_warning("Can't write packet: %s", g_strerror(err));
293 static void run_listener(const char* fifo, const guint16 port, const char* proto_name)
295 struct sockaddr_in clientaddr;
296 int clientlen = sizeof(clientaddr);
297 socket_handle_t sock;
302 if (signal(SIGINT, exit_from_loop) == SIG_ERR) {
303 g_warning("Can't set signal handler");
307 if (setup_dumpfile(fifo, &fp) == EXIT_FAILURE) {
313 if (setup_listener(port, &sock) == EXIT_FAILURE)
316 g_debug("Listener running on port %u", port);
318 buf = (char*)g_malloc(PKT_BUF_SIZE);
319 while(run_loop == TRUE) {
320 memset(buf, 0x0, PKT_BUF_SIZE);
322 buflen = recvfrom(sock, buf, PKT_BUF_SIZE, 0, (struct sockaddr *)&clientaddr, &clientlen);
331 wchar_t *errmsg = NULL;
332 int err = WSAGetLastError();
333 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
335 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
336 (LPWSTR)&errmsg, 0, NULL);
337 g_warning("Error in recvfrom: %S (err=%d)", errmsg, err);
341 g_warning("Error in recvfrom: %s (errno=%d)", strerror(errno), errno);
347 if (dump_packet(proto_name, port, buf, buflen, clientaddr, fp) == EXIT_FAILURE)
357 int real_main(int argc, char *argv[])
362 int ret = EXIT_FAILURE;
363 extcap_parameters* extcap_conf = g_new0(extcap_parameters, 1);
365 char* help_header = NULL;
366 char* payload = NULL;
367 char* port_msg = NULL;
372 help_url = data_file_url("udpdump.html");
373 extcap_base_set_util_info(extcap_conf, argv[0], UDPDUMP_VERSION_MAJOR, UDPDUMP_VERSION_MINOR, UDPDUMP_VERSION_RELEASE,
376 extcap_base_register_interface(extcap_conf, UDPDUMP_EXTCAP_INTERFACE, "UDP Listener remote capture", 252, "Exported PDUs");
378 help_header = g_strdup_printf(
379 " %s --extcap-interfaces\n"
380 " %s --extcap-interface=%s --extcap-dlts\n"
381 " %s --extcap-interface=%s --extcap-config\n"
382 " %s --extcap-interface=%s --port 5555 --fifo myfifo --capture",
383 argv[0], argv[0], UDPDUMP_EXTCAP_INTERFACE, argv[0], UDPDUMP_EXTCAP_INTERFACE, argv[0], UDPDUMP_EXTCAP_INTERFACE);
384 extcap_help_add_header(extcap_conf, help_header);
386 extcap_help_add_option(extcap_conf, "--help", "print this help");
387 extcap_help_add_option(extcap_conf, "--version", "print the version");
388 port_msg = g_strdup_printf("the port to listens on. Default: %u", UDPDUMP_DEFAULT_PORT);
389 extcap_help_add_option(extcap_conf, "--port <port>", port_msg);
396 extcap_help_print(extcap_conf);
400 while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
404 extcap_help_print(extcap_conf);
409 printf("%s\n", extcap_conf->version);
413 if (!ws_strtou16(optarg, NULL, &port)) {
414 g_warning("Invalid port: %s", optarg);
421 payload = g_strdup(optarg);
425 /* missing option argument */
426 g_warning("Option '%s' requires an argument", argv[optind - 1]);
430 if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) {
431 g_warning("Invalid option: %s", argv[optind - 1]);
437 extcap_cmdline_debug(argv, argc);
439 if (optind != argc) {
440 g_warning("Unexpected extra option: %s", argv[optind]);
444 if (extcap_base_handle_interface(extcap_conf)) {
449 if (extcap_conf->show_config) {
450 ret = list_config(extcap_conf->interface);
455 payload = g_strdup("data");
458 result = WSAStartup(MAKEWORD(1,1), &wsaData);
460 g_warning("Error: WSAStartup failed with error: %d", result);
466 port = UDPDUMP_DEFAULT_PORT;
468 if (extcap_conf->capture)
469 run_listener(extcap_conf->fifo, port, payload);
473 extcap_base_cleanup(&extcap_conf);
480 wmain(int argc, wchar_t *wc_argv[])
484 argv = arg_list_utf_16to8(argc, wc_argv);
485 return real_main(argc, argv);
489 main(int argc, char *argv[])
491 return real_main(argc, argv);
496 * Editor modelines - http://www.wireshark.org/tools/modelines.html
501 * indent-tabs-mode: t
504 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
505 * :indentSize=8:tabSize=8:noTabs=false: