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,
37 static struct option longopts[] = {
39 { "help", no_argument, NULL, OPT_HELP},
40 { "version", no_argument, NULL, OPT_VERSION},
41 { "maxbytes", required_argument, NULL, OPT_MAXBYTES},
42 { "count", required_argument, NULL, OPT_COUNT},
43 { "random-type", no_argument, NULL, OPT_RANDOM_TYPE},
44 { "all-random", no_argument, NULL, OPT_ALL_RANDOM},
45 { "type", required_argument, NULL, OPT_TYPE},
50 static void help(extcap_parameters* extcap_conf)
56 extcap_help_print(extcap_conf);
58 printf("\nPacket types:\n");
59 randpkt_example_list(&abbrev_list, &longname_list);
60 while (abbrev_list[i] && longname_list[i]) {
61 printf("\t%-16s%s\n", abbrev_list[i], longname_list[i]);
65 g_strfreev(abbrev_list);
66 g_strfreev(longname_list);
69 static int list_config(char *interface)
77 g_warning("No interface specified.");
81 if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
82 g_warning("Interface must be %s", RANDPKT_EXTCAP_INTERFACE);
86 printf("arg {number=%u}{call=--maxbytes}{display=Max bytes in a packet}"
87 "{type=unsigned}{range=1,5000}{default=5000}{tooltip=The max number of bytes in a packet}\n",
89 printf("arg {number=%u}{call=--count}{display=Number of packets}"
90 "{type=long}{default=1000}{tooltip=Number of packets to generate (-1 for infinite)}\n",
92 printf("arg {number=%u}{call=--random-type}{display=Random type}"
93 "{type=boolflag}{default=false}{tooltip=The packets type is randomly chosen}\n",
95 printf("arg {number=%u}{call=--all-random}{display=All random packets}"
96 "{type=boolflag}{default=false}{tooltip=Packet type for each packet is randomly chosen}\n",
100 printf("arg {number=%u}{call=--type}{display=Type of packet}"
101 "{type=selector}{tooltip=Type of packet to generate}\n",
103 randpkt_example_list(&abbrev_list, &longname_list);
104 while (abbrev_list[i] && longname_list[i]) {
105 printf("value {arg=%u}{value=%s}{display=%s}\n", inc, abbrev_list[i], longname_list[i]);
108 g_strfreev(abbrev_list);
109 g_strfreev(longname_list);
112 extcap_config_debug(&inc);
117 int main(int argc, char *argv[])
121 guint16 maxbytes = 5000;
122 guint64 count = 1000;
123 int random_type = FALSE;
124 int all_random = FALSE;
126 int produce_type = -1;
127 randpkt_example *example;
128 wtap_dumper* savedump;
129 int ret = EXIT_FAILURE;
135 extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
137 char* help_header = NULL;
139 help_url = data_file_url("randpktdump.html");
140 extcap_base_set_util_info(extcap_conf, argv[0], RANDPKTDUMP_VERSION_MAJOR, RANDPKTDUMP_VERSION_MINOR,
141 RANDPKTDUMP_VERSION_RELEASE, help_url);
143 extcap_base_register_interface(extcap_conf, RANDPKT_EXTCAP_INTERFACE, "Random packet generator", 147, "Generator dependent DLT");
145 help_header = g_strdup_printf(
146 " %s --extcap-interfaces\n"
147 " %s --extcap-interface=%s --extcap-dlts\n"
148 " %s --extcap-interface=%s --extcap-config\n"
149 " %s --extcap-interface=%s --type dns --count 10 "
150 "--fifo=FILENAME --capture\n", argv[0], argv[0], RANDPKT_EXTCAP_INTERFACE, argv[0], RANDPKT_EXTCAP_INTERFACE,
151 argv[0], RANDPKT_EXTCAP_INTERFACE);
152 extcap_help_add_header(extcap_conf, help_header);
155 extcap_help_add_option(extcap_conf, "--help", "print this help");
156 extcap_help_add_option(extcap_conf, "--version", "print the version");
157 extcap_help_add_option(extcap_conf, "--maxbytes <bytes>", "max bytes per pack");
158 extcap_help_add_option(extcap_conf, "--count <num>", "number of packets to generate");
159 extcap_help_add_option(extcap_conf, "--random-type", "one random type is chosen for all packets");
160 extcap_help_add_option(extcap_conf, "--all-random", "a random type is chosen for each packet");
161 extcap_help_add_option(extcap_conf, "--type <type>", "the packet type");
169 attach_parent_console();
172 while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
175 printf("%s\n", extcap_conf->version);
185 if (!ws_strtou16(optarg, NULL, &maxbytes)) {
186 g_warning("Invalid parameter maxbytes: %s (max value is %u)",
187 optarg, G_MAXUINT16);
193 if (!ws_strtou64(optarg, NULL, &count)) {
194 g_warning("Invalid packet count: %s", optarg);
199 case OPT_RANDOM_TYPE:
209 type = g_strdup(optarg);
213 /* missing option argument */
214 g_warning("Option '%s' requires an argument", argv[optind - 1]);
218 /* Handle extcap specific options */
219 if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg))
221 g_warning("Invalid option: %s", argv[optind - 1]);
227 extcap_cmdline_debug(argv, argc);
229 if (extcap_base_handle_interface(extcap_conf)) {
234 if (extcap_conf->show_config) {
235 ret = list_config(extcap_conf->interface);
239 /* Some sanity checks */
240 if ((random_type) && (all_random)) {
241 g_warning("You can specify only one between: --random-type, --all-random");
245 /* Wireshark sets the type, even when random options are selected. We don't want it */
246 if (random_type || all_random) {
252 result = WSAStartup(MAKEWORD(1,1), &wsaData);
254 g_warning("ERROR: WSAStartup failed with error: %d", result);
259 if (extcap_conf->capture) {
261 if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) {
262 g_warning("ERROR: invalid interface");
269 produce_type = randpkt_parse_type(type);
271 example = randpkt_find_example(produce_type);
275 g_debug("Generating packets: %s", example->abbrev);
277 randpkt_example_init(example, extcap_conf->fifo, maxbytes);
278 randpkt_loop(example, count);
279 randpkt_example_close(example);
281 produce_type = randpkt_parse_type(NULL);
282 example = randpkt_find_example(produce_type);
285 randpkt_example_init(example, extcap_conf->fifo, maxbytes);
287 while (count-- > 0) {
288 randpkt_loop(example, 1);
289 produce_type = randpkt_parse_type(NULL);
291 savedump = example->dump;
293 example = randpkt_find_example(produce_type);
296 example->dump = savedump;
298 randpkt_example_close(example);
306 extcap_base_cleanup(&extcap_conf);
313 WinMain (struct HINSTANCE__ *hInstance,
314 struct HINSTANCE__ *hPrevInstance,
318 return main(__argc, __argv);
323 * Editor modelines - https://www.wireshark.org/tools/modelines.html
328 * indent-tabs-mode: t
331 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
332 * :indentSize=8:tabSize=8:noTabs=false: