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 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "extcap-base.h"
29 #include "randpkt_core/randpkt_core.h"
31 #define RANDPKT_EXTCAP_INTERFACE "randpkt"
32 #define RANDPKTDUMP_VERSION_MAJOR "0"
33 #define RANDPKTDUMP_VERSION_MINOR "1"
34 #define RANDPKTDUMP_VERSION_RELEASE "0"
37 EXTCAP_BASE_OPTIONS_ENUM,
47 static struct option longopts[] = {
49 { "help", no_argument, NULL, OPT_HELP},
50 { "version", no_argument, NULL, OPT_VERSION},
51 { "maxbytes", required_argument, NULL, OPT_MAXBYTES},
52 { "count", required_argument, NULL, OPT_COUNT},
53 { "random-type", required_argument, NULL, OPT_RANDOM_TYPE},
54 { "all-random", required_argument, NULL, OPT_ALL_RANDOM},
55 { "type", required_argument, NULL, OPT_TYPE},
60 static void help(extcap_parameters* extcap_conf)
66 extcap_help_print(extcap_conf);
68 printf("\nPacket types:\n");
69 randpkt_example_list(&abbrev_list, &longname_list);
70 while (abbrev_list[i] && longname_list[i]) {
71 printf("\t%-16s%s\n", abbrev_list[i], longname_list[i]);
75 g_strfreev(abbrev_list);
76 g_strfreev(longname_list);
79 static int list_config(char *interface)
87 g_warning("No interface specified.");
91 if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
92 g_warning("Interface must be %s", RANDPKT_EXTCAP_INTERFACE);
96 printf("arg {number=%u}{call=--maxbytes}{display=Max bytes in a packet}"
97 "{type=unsigned}{range=1,5000}{default=5000}{tooltip=The max number of bytes in a packet}\n",
99 printf("arg {number=%u}{call=--count}{display=Number of packets}"
100 "{type=long}{default=1000}{tooltip=Number of packets to generate (-1 for infinite)}\n",
102 printf("arg {number=%u}{call=--random-type}{display=Random type}"
103 "{type=boolean}{default=false}{tooltip=The packets type is randomly chosen}\n",
105 printf("arg {number=%u}{call=--all-random}{display=All random packets}"
106 "{type=boolean}{default=false}{tooltip=Packet type for each packet is randomly chosen}\n",
110 printf("arg {number=%u}{call=--type}{display=Type of packet}"
111 "{type=selector}{tooltip=Type of packet to generate}\n",
113 randpkt_example_list(&abbrev_list, &longname_list);
114 while (abbrev_list[i] && longname_list[i]) {
115 printf("value {arg=%u}{value=%s}{display=%s}\n", inc, abbrev_list[i], longname_list[i]);
118 g_strfreev(abbrev_list);
119 g_strfreev(longname_list);
125 int main(int argc, char *argv[])
130 guint64 count = 1000;
131 int random_type = FALSE;
132 int all_random = FALSE;
134 int produce_type = -1;
135 randpkt_example *example;
136 wtap_dumper* savedump;
138 int ret = EXIT_FAILURE;
144 extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
145 char* help_header = NULL;
147 extcap_base_set_util_info(extcap_conf, argv[0], RANDPKTDUMP_VERSION_MAJOR, RANDPKTDUMP_VERSION_MINOR,
148 RANDPKTDUMP_VERSION_RELEASE, NULL);
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, "--verbose", "verbose mode");
164 extcap_help_add_option(extcap_conf, "--maxbytes <bytes>", "max bytes per pack");
165 extcap_help_add_option(extcap_conf, "--count <num>", "number of packets to generate");
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");
176 attach_parent_console();
179 for (i = 0; i < argc; i++)
180 g_debug("%s ", argv[i]);
182 while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
185 printf("%s\n", extcap_conf->version);
195 maxbytes = atoi(optarg);
196 if (maxbytes > MAXBYTES_LIMIT) {
197 g_warning("randpktdump: Max bytes is %d", MAXBYTES_LIMIT);
203 count = g_ascii_strtoull(optarg, NULL, 10);
206 case OPT_RANDOM_TYPE:
207 if (!g_ascii_strcasecmp("true", optarg)) {
213 if (!g_ascii_strcasecmp("true", optarg)) {
220 type = g_strdup(optarg);
224 /* missing option argument */
225 g_warning("Option '%s' requires an argument", argv[optind - 1]);
229 /* Handle extcap specific options */
230 if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg))
232 g_warning("Invalid option: %s", argv[optind - 1]);
238 if (optind != argc) {
239 g_warning("Invalid option: %s", argv[optind]);
243 if (extcap_base_handle_interface(extcap_conf)) {
248 if (extcap_conf->show_config) {
249 ret = list_config(extcap_conf->interface);
253 /* Some sanity checks */
254 if ((random_type) && (all_random)) {
255 g_warning("You can specify only one between: --random-type, --all-random");
259 /* Wireshark sets the type, even when random options are selected. We don't want it */
260 if (random_type || all_random) {
266 result = WSAStartup(MAKEWORD(1,1), &wsaData);
268 g_warning("ERROR: WSAStartup failed with error: %d", result);
273 if (extcap_conf->capture) {
275 if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) {
276 g_warning("ERROR: invalid interface");
281 produce_type = randpkt_parse_type(type);
283 example = randpkt_find_example(produce_type);
287 g_debug("Generating packets: %s", example->abbrev);
289 randpkt_example_init(example, extcap_conf->fifo, maxbytes);
290 randpkt_loop(example, count);
291 randpkt_example_close(example);
293 produce_type = randpkt_parse_type(NULL);
294 example = randpkt_find_example(produce_type);
297 randpkt_example_init(example, extcap_conf->fifo, maxbytes);
299 while (count-- > 0) {
300 randpkt_loop(example, 1);
301 produce_type = randpkt_parse_type(NULL);
303 savedump = example->dump;
305 example = randpkt_find_example(produce_type);
308 example->dump = savedump;
310 randpkt_example_close(example);
318 extcap_base_cleanup(&extcap_conf);
325 WinMain (struct HINSTANCE__ *hInstance,
326 struct HINSTANCE__ *hPrevInstance,
330 return main(__argc, __argv);
335 * Editor modelines - https://www.wireshark.org/tools/modelines.html
340 * indent-tabs-mode: t
343 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
344 * :indentSize=8:tabSize=8:noTabs=false: