2 * randpktdump is an extcap tool used to generate random data for testing/educational purpose
4 * Copyright 2015, Dario Lombardo
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include "extcap-base.h"
17 #include "randpkt_core/randpkt_core.h"
18 #include <wsutil/strtoi.h>
19 #include <wsutil/filesystem.h>
21 #define RANDPKT_EXTCAP_INTERFACE "randpkt"
22 #define RANDPKTDUMP_VERSION_MAJOR "0"
23 #define RANDPKTDUMP_VERSION_MINOR "1"
24 #define RANDPKTDUMP_VERSION_RELEASE "0"
27 EXTCAP_BASE_OPTIONS_ENUM,
38 static struct option longopts[] = {
40 { "help", no_argument, NULL, OPT_HELP},
41 { "version", no_argument, NULL, OPT_VERSION},
42 { "maxbytes", required_argument, NULL, OPT_MAXBYTES},
43 { "count", required_argument, NULL, OPT_COUNT},
44 { "delay", required_argument, NULL, OPT_DELAY},
45 { "random-type", no_argument, NULL, OPT_RANDOM_TYPE},
46 { "all-random", no_argument, NULL, OPT_ALL_RANDOM},
47 { "type", required_argument, NULL, OPT_TYPE},
52 static void help(extcap_parameters* extcap_conf)
58 extcap_help_print(extcap_conf);
60 printf("\nPacket types:\n");
61 randpkt_example_list(&abbrev_list, &longname_list);
62 while (abbrev_list[i] && longname_list[i]) {
63 printf("\t%-16s%s\n", abbrev_list[i], longname_list[i]);
67 g_strfreev(abbrev_list);
68 g_strfreev(longname_list);
71 static int list_config(char *interface)
79 g_warning("No interface specified.");
83 if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
84 g_warning("Interface must be %s", RANDPKT_EXTCAP_INTERFACE);
88 printf("arg {number=%u}{call=--maxbytes}{display=Max bytes in a packet}"
89 "{type=unsigned}{range=1,5000}{default=5000}{tooltip=The max number of bytes in a packet}\n",
91 printf("arg {number=%u}{call=--count}{display=Number of packets}"
92 "{type=long}{default=1000}{tooltip=Number of packets to generate (-1 for infinite)}\n",
94 printf("arg {number=%u}{call=--delay}{display=Packet delay (ms)}"
95 "{type=long}{default=0}{tooltip=Milliseconds to wait after writing each packet}\n",
97 printf("arg {number=%u}{call=--random-type}{display=Random type}"
98 "{type=boolflag}{default=false}{tooltip=The packets type is randomly chosen}\n",
100 printf("arg {number=%u}{call=--all-random}{display=All random packets}"
101 "{type=boolflag}{default=false}{tooltip=Packet type for each packet is randomly chosen}\n",
105 printf("arg {number=%u}{call=--type}{display=Type of packet}"
106 "{type=selector}{tooltip=Type of packet to generate}\n",
108 randpkt_example_list(&abbrev_list, &longname_list);
109 while (abbrev_list[i] && longname_list[i]) {
110 printf("value {arg=%u}{value=%s}{display=%s}\n", inc, abbrev_list[i], longname_list[i]);
113 g_strfreev(abbrev_list);
114 g_strfreev(longname_list);
117 extcap_config_debug(&inc);
122 int real_main(int argc, char *argv[])
126 guint16 maxbytes = 5000;
127 guint64 count = 1000;
128 guint64 packet_delay_ms = 0;
129 int random_type = FALSE;
130 int all_random = FALSE;
132 int produce_type = -1;
133 randpkt_example *example;
134 wtap_dumper* savedump;
135 int ret = EXIT_FAILURE;
141 extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
143 char* help_header = NULL;
145 help_url = data_file_url("randpktdump.html");
146 extcap_base_set_util_info(extcap_conf, argv[0], RANDPKTDUMP_VERSION_MAJOR, RANDPKTDUMP_VERSION_MINOR,
147 RANDPKTDUMP_VERSION_RELEASE, help_url);
149 extcap_base_register_interface(extcap_conf, RANDPKT_EXTCAP_INTERFACE, "Random packet generator", 147, "Generator dependent DLT");
151 help_header = g_strdup_printf(
152 " %s --extcap-interfaces\n"
153 " %s --extcap-interface=%s --extcap-dlts\n"
154 " %s --extcap-interface=%s --extcap-config\n"
155 " %s --extcap-interface=%s --type dns --count 10 "
156 "--fifo=FILENAME --capture\n", argv[0], argv[0], RANDPKT_EXTCAP_INTERFACE, argv[0], RANDPKT_EXTCAP_INTERFACE,
157 argv[0], RANDPKT_EXTCAP_INTERFACE);
158 extcap_help_add_header(extcap_conf, help_header);
161 extcap_help_add_option(extcap_conf, "--help", "print this help");
162 extcap_help_add_option(extcap_conf, "--version", "print the version");
163 extcap_help_add_option(extcap_conf, "--maxbytes <bytes>", "max bytes per pack");
164 extcap_help_add_option(extcap_conf, "--count <num>", "number of packets to generate");
165 extcap_help_add_option(extcap_conf, "--delay <ms>", "milliseconds to wait after writing each packet");
166 extcap_help_add_option(extcap_conf, "--random-type", "one random type is chosen for all packets");
167 extcap_help_add_option(extcap_conf, "--all-random", "a random type is chosen for each packet");
168 extcap_help_add_option(extcap_conf, "--type <type>", "the packet type");
175 while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
178 printf("%s\n", extcap_conf->version);
188 if (!ws_strtou16(optarg, NULL, &maxbytes)) {
189 g_warning("Invalid parameter maxbytes: %s (max value is %u)",
190 optarg, G_MAXUINT16);
196 if (!ws_strtou64(optarg, NULL, &count)) {
197 g_warning("Invalid packet count: %s", optarg);
203 if (!ws_strtou64(optarg, NULL, &packet_delay_ms)) {
204 g_warning("Invalid packet delay: %s", optarg);
209 case OPT_RANDOM_TYPE:
219 type = g_strdup(optarg);
223 /* missing option argument */
224 g_warning("Option '%s' requires an argument", argv[optind - 1]);
228 /* Handle extcap specific options */
229 if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg))
231 g_warning("Invalid option: %s", argv[optind - 1]);
237 extcap_cmdline_debug(argv, argc);
239 if (extcap_base_handle_interface(extcap_conf)) {
244 if (extcap_conf->show_config) {
245 ret = list_config(extcap_conf->interface);
249 /* Some sanity checks */
250 if ((random_type) && (all_random)) {
251 g_warning("You can specify only one between: --random-type, --all-random");
255 /* Wireshark sets the type, even when random options are selected. We don't want it */
256 if (random_type || all_random) {
262 result = WSAStartup(MAKEWORD(1,1), &wsaData);
264 g_warning("ERROR: WSAStartup failed with error: %d", result);
269 if (extcap_conf->capture) {
271 if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) {
272 g_warning("ERROR: invalid interface");
279 produce_type = randpkt_parse_type(type);
281 example = randpkt_find_example(produce_type);
285 g_debug("Generating packets: %s", example->abbrev);
287 randpkt_example_init(example, extcap_conf->fifo, maxbytes);
288 randpkt_loop(example, count, packet_delay_ms);
289 randpkt_example_close(example);
291 produce_type = randpkt_parse_type(NULL);
292 example = randpkt_find_example(produce_type);
295 randpkt_example_init(example, extcap_conf->fifo, maxbytes);
297 while (count-- > 0) {
298 randpkt_loop(example, 1, packet_delay_ms);
299 produce_type = randpkt_parse_type(NULL);
301 savedump = example->dump;
303 example = randpkt_find_example(produce_type);
306 example->dump = savedump;
308 randpkt_example_close(example);
316 extcap_base_cleanup(&extcap_conf);
323 wmain(int argc, wchar_t *wc_argv[])
327 argv = arg_list_utf_16to8(argc, wc_argv);
328 return real_main(argc, argv);
332 main(int argc, char *argv[])
334 return real_main(argc, argv);
339 * Editor modelines - https://www.wireshark.org/tools/modelines.html
344 * indent-tabs-mode: t
347 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
348 * :indentSize=8:tabSize=8:noTabs=false: