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 "randpkt-core.h"
30 #include <glib/gprintf.h>
37 #ifndef HAVE_GETOPT_LONG
38 #include "wsutil/wsgetopt.h"
45 #if defined(_WIN32) && !defined(__CYGWIN__)
52 #ifdef HAVE_WINSOCK2_H
58 #define socket_handle_t SOCKET
61 * UN*X, or Windows pretending to be UN*X with the aid of Cygwin.
63 #define closesocket(socket) close(socket)
64 #define socket_handle_t int
65 #define INVALID_SOCKET (-1)
66 #define SOCKET_ERROR (-1)
69 #define verbose_print(...) { if (verbose) printf(__VA_ARGS__); }
70 #define errmsprintf(...) { printf(__VA_ARGS__); printf("\n"); }
72 #define RANDPKT_EXTCAP_INTERFACE "randpkt"
73 #define RANDPKTDUMP_VERSION_MAJOR 0
74 #define RANDPKTDUMP_VERSION_MINOR 1
75 #define RANDPKTDUMP_VERSION_RELEASE 0
77 static gboolean verbose = TRUE;
97 static struct option longopts[] = {
98 /* Generic application options */
99 { "help", no_argument, NULL, OPT_HELP},
100 { "version", no_argument, NULL, OPT_VERSION},
101 { "verbose", optional_argument, NULL, OPT_VERBOSE},
103 { "extcap-interfaces", no_argument, NULL, OPT_LIST_INTERFACES},
104 { "extcap-dlts", no_argument, NULL, OPT_LIST_DLTS},
105 { "extcap-interface", required_argument, NULL, OPT_INTERFACE},
106 { "extcap-config", no_argument, NULL, OPT_CONFIG},
107 { "capture", no_argument, NULL, OPT_CAPTURE},
108 { "extcap-capture-filter ", required_argument, NULL, OPT_CAPTURE_FILTER},
109 { "fifo", required_argument, NULL, OPT_FIFO},
110 /* Interfaces options */
111 { "maxbytes", required_argument, NULL, OPT_MAXBYTES},
112 { "count", required_argument, NULL, OPT_COUNT},
113 { "random-type", required_argument, NULL, OPT_RANDOM_TYPE},
114 { "all-random", required_argument, NULL, OPT_ALL_RANDOM},
115 { "type", required_argument, NULL, OPT_TYPE},
120 BOOLEAN IsHandleRedirected(DWORD handle)
122 HANDLE h = GetStdHandle(handle);
124 BY_HANDLE_FILE_INFORMATION fi;
125 if (GetFileInformationByHandle(h, &fi)) {
132 static void attach_parent_console()
134 BOOL outRedirected, errRedirected;
136 outRedirected = IsHandleRedirected(STD_OUTPUT_HANDLE);
137 errRedirected = IsHandleRedirected(STD_ERROR_HANDLE);
139 if (outRedirected && errRedirected) {
140 /* Both standard output and error handles are redirected.
141 * There is no point in attaching to parent process console.
146 if (AttachConsole(ATTACH_PARENT_PROCESS) == 0) {
147 /* Console attach failed. */
151 /* Console attach succeeded */
152 if (outRedirected == FALSE) {
153 freopen("CONOUT$", "w", stdout);
156 if (errRedirected == FALSE) {
157 freopen("CONOUT$", "w", stderr);
162 static void help(const char* binname)
165 const char** abbrev_list;
166 const char** longname_list;
171 printf(" %s --extcap-interfaces\n", binname);
172 printf(" %s --extcap-interface=INTERFACE --extcap-dlts\n", binname);
173 printf(" %s --extcap-interface=INTERFACE --extcap-config\n", binname);
174 printf(" %s --extcap-interface=INTERFACE --type dns --count 10"
175 "--fifo=FILENAME --capture\n", binname);
177 printf(" --help: print this help\n");
178 printf(" --version: print the version\n");
179 printf(" --verbose: verbose mode\n");
180 printf(" --extcap-interfaces: list the extcap Interfaces\n");
181 printf(" --extcap-dlts: list the DLTs\n");
182 printf(" --extcap-interface <iface>: specify the extcap interface\n");
183 printf(" --extcap-config: list the additional configuration for an interface\n");
184 printf(" --capture: run the capture\n");
185 printf(" --extcap-capture-filter <filter>: the capture filter\n");
186 printf(" --fifo <file>: dump data to file or fifo\n");
187 printf(" --maxbytes <bytes>: max bytes per packet");
188 printf(" --count <num>: number of packets to generate\n");
189 printf(" --random-type: one random type is choosen for all packets\n");
190 printf(" --all-random: a random type is choosen for each packet\n");
191 printf(" --type <type>: the packet type\n");
192 printf("\n\nPacket types:\n");
193 randpkt_example_list(&abbrev_list, &longname_list, &list_num);
194 for (i = 0; i < list_num; i++) {
195 printf("\t%-16s%s\n", abbrev_list[i], longname_list[i]);
197 g_free((char**)abbrev_list);
198 g_free((char**)longname_list);
202 static int list_interfaces(void)
204 printf("interface {value=%s}{display=Random packet generator}\n", RANDPKT_EXTCAP_INTERFACE);
208 static int list_config(char *interface)
212 const char** abbrev_list;
213 const char** longname_list;
217 g_fprintf(stderr, "ERROR: No interface specified.\n");
221 if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
222 errmsprintf("ERROR: interface must be %s\n", RANDPKT_EXTCAP_INTERFACE);
226 printf("arg {number=%u}{call=--maxbytes}{display=Max bytes in a packet}"
227 "{type=unsigned}{range=1,5000}{default=5000}{tooltip=The max number of bytes in a packet}\n",
229 printf("arg {number=%u}{call=--count}{display=Number of packets}"
230 "{type=long}{default=1000}{tooltip=Number of packets to generate (-1 for infinite)}\n",
232 printf("arg {number=%u}{call=--random-type}{display=Random type}"
233 "{type=boolean}{default=false}{tooltip=The packets type is randomly choosen}\n",
235 printf("arg {number=%u}{call=--all-random}{display=All random packets}"
236 "{type=boolean}{default=false}{tooltip=Packet type for each packet is randomly choosen}\n",
240 printf("arg {number=%u}{call=--type}{display=Type of packet}"
241 "{type=selector}{tooltip=Type of packet to generate}\n",
243 randpkt_example_list(&abbrev_list, &longname_list, &list_num);
244 for (i = 0; i < list_num; i++) {
245 printf("value {arg=%u}{value=%s}{display=%s}\n", inc, abbrev_list[i], longname_list[i]);
247 g_free((char**)abbrev_list);
248 g_free((char**)longname_list);
254 static int list_dlts(const char *interface)
257 printf("ERROR: No interface specified.\n");
261 if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
262 printf("ERROR: interface must be %s\n", RANDPKT_EXTCAP_INTERFACE);
266 printf("dlt {number=147}{name=%s}{display=Generator dependent DLT}\n", RANDPKT_EXTCAP_INTERFACE);
271 int main(int argc, char *argv[])
277 int do_list_interfaces = 0;
280 char* interface = NULL;
282 guint64 count = 1000;
283 int random_type = FALSE;
284 int all_random = FALSE;
286 int produce_type = -1;
287 randpkt_example *example;
288 wtap_dumper* savedump;
299 attach_parent_console();
302 for (i = 0; i < argc; i++) {
303 verbose_print("%s ", argv[i]);
307 while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
310 printf("%u.%u.%u\n", RANDPKTDUMP_VERSION_MAJOR, RANDPKTDUMP_VERSION_MINOR, RANDPKTDUMP_VERSION_RELEASE);
316 case OPT_LIST_INTERFACES:
317 do_list_interfaces = 1;
327 interface = g_strdup(optarg);
338 case OPT_CAPTURE_FILTER:
339 /* currently unused */
345 fifo = g_strdup(optarg);
353 maxbytes = atoi(optarg);
354 if (maxbytes > MAXBYTES_LIMIT) {
355 errmsprintf("randpktdump: Max bytes is %u\n", MAXBYTES_LIMIT);
361 count = g_ascii_strtoull(optarg, NULL, 10);
364 case OPT_RANDOM_TYPE:
365 if (!g_ascii_strcasecmp("true", optarg)) {
371 if (!g_ascii_strcasecmp("true", optarg)) {
377 type = g_strdup(optarg);
381 /* missing option argument */
382 printf("Option '%s' requires an argument\n", argv[optind - 1]);
386 printf("Invalid option 1: %s\n", argv[optind - 1]);
391 if (optind != argc) {
392 printf("Invalid option: %s\n", argv[optind]);
396 if (do_list_interfaces)
397 return list_interfaces();
400 return list_config(interface);
403 return list_dlts(interface);
405 /* Some sanity checks */
406 if ((random_type) && (all_random)) {
407 errmsprintf("You can specify only one between: --random-type, --all-random\n");
411 /* Wireshark sets the type, even when random options are selected. We don't want it */
412 if (random_type || all_random) {
418 result = WSAStartup(MAKEWORD(1,1), &wsaData);
421 errmsprintf("ERROR: WSAStartup failed with error: %d\n", result);
428 errmsprintf("ERROR: No FIFO or file specified\n");
432 if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
433 errmsprintf("ERROR: invalid interface\n");
440 produce_type = randpkt_parse_type(type);
443 example = randpkt_find_example(produce_type);
447 verbose_print("Generating packets: %s\n", example->abbrev);
449 randpkt_example_init(example, fifo, maxbytes);
450 randpkt_loop(example, count);
451 randpkt_example_close(example);
453 produce_type = randpkt_parse_type(NULL);
454 example = randpkt_find_example(produce_type);
457 randpkt_example_init(example, fifo, maxbytes);
459 while (count-- > 0) {
460 randpkt_loop(example, 1);
461 produce_type = randpkt_parse_type(NULL);
463 savedump = example->dump;
465 example = randpkt_find_example(produce_type);
468 example->dump = savedump;
470 randpkt_example_close(example);
488 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
489 LPSTR lpCmdLine, int nCmdShow) {
490 return main(__argc, __argv);
495 * Editor modelines - http://www.wireshark.org/tools/modelines.html
500 * indent-tabs-mode: t
503 * vi: set shiftwidth=4 tabstop=4 expandtab:
504 * :indentSize=4:tabSize=4:noTabs=false: