cmake: move extcap sections to extcap/CMakeLists.txt.
[metze/wireshark/wip.git] / extcap / randpktdump.c
1 /* randpktdump.c
2  * randpktdump is an extcap tool used to generate random data for testing/educational purpose
3  *
4  * Copyright 2015, Dario Lombardo
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include "config.h"
14
15 #include "extcap-base.h"
16
17 #include "randpkt_core/randpkt_core.h"
18 #include <wsutil/strtoi.h>
19 #include <wsutil/filesystem.h>
20
21 #define RANDPKT_EXTCAP_INTERFACE "randpkt"
22 #define RANDPKTDUMP_VERSION_MAJOR "0"
23 #define RANDPKTDUMP_VERSION_MINOR "1"
24 #define RANDPKTDUMP_VERSION_RELEASE "0"
25
26 enum {
27         EXTCAP_BASE_OPTIONS_ENUM,
28         OPT_HELP,
29         OPT_VERSION,
30         OPT_MAXBYTES,
31         OPT_COUNT,
32         OPT_RANDOM_TYPE,
33         OPT_ALL_RANDOM,
34         OPT_TYPE
35 };
36
37 static struct option longopts[] = {
38         EXTCAP_BASE_OPTIONS,
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},
46     { 0, 0, 0, 0 }
47 };
48
49
50 static void help(extcap_parameters* extcap_conf)
51 {
52         unsigned i = 0;
53         char** abbrev_list;
54         char** longname_list;
55
56         extcap_help_print(extcap_conf);
57
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]);
62                 i++;
63         }
64         printf("\n");
65         g_strfreev(abbrev_list);
66         g_strfreev(longname_list);
67 }
68
69 static int list_config(char *interface)
70 {
71         unsigned inc = 0;
72         unsigned i = 0;
73         char** abbrev_list;
74         char** longname_list;
75
76         if (!interface) {
77                 g_warning("No interface specified.");
78                 return EXIT_FAILURE;
79         }
80
81         if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
82                 g_warning("Interface must be %s", RANDPKT_EXTCAP_INTERFACE);
83                 return EXIT_FAILURE;
84         }
85
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",
88                 inc++);
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",
91                 inc++);
92         printf("arg {number=%u}{call=--random-type}{display=Random type}"
93                 "{type=boolflag}{default=false}{tooltip=The packets type is randomly chosen}\n",
94                 inc++);
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",
97                 inc++);
98
99         /* Now the types */
100         printf("arg {number=%u}{call=--type}{display=Type of packet}"
101                 "{type=selector}{tooltip=Type of packet to generate}\n",
102                 inc);
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]);
106                 i++;
107         }
108         g_strfreev(abbrev_list);
109         g_strfreev(longname_list);
110         inc++;
111
112         extcap_config_debug(&inc);
113
114         return EXIT_SUCCESS;
115 }
116
117 int main(int argc, char *argv[])
118 {
119         int option_idx = 0;
120         int result;
121         guint16 maxbytes = 5000;
122         guint64 count = 1000;
123         int random_type = FALSE;
124         int all_random = FALSE;
125         char* type = NULL;
126         int produce_type = -1;
127         randpkt_example *example;
128         wtap_dumper* savedump;
129         int ret = EXIT_FAILURE;
130
131 #ifdef _WIN32
132         WSADATA wsaData;
133 #endif  /* _WIN32 */
134
135         extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
136         char* help_url;
137         char* help_header = NULL;
138
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);
142         g_free(help_url);
143         extcap_base_register_interface(extcap_conf, RANDPKT_EXTCAP_INTERFACE, "Random packet generator", 147, "Generator dependent DLT");
144
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);
153         g_free(help_header);
154
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");
162
163         if (argc == 1) {
164                 help(extcap_conf);
165                 goto end;
166         }
167
168 #ifdef _WIN32
169         attach_parent_console();
170 #endif  /* _WIN32 */
171
172         while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
173                 switch (result) {
174                 case OPT_VERSION:
175                         printf("%s\n", extcap_conf->version);
176                         ret = EXIT_SUCCESS;
177                         goto end;
178
179                 case OPT_HELP:
180                         help(extcap_conf);
181                         ret = EXIT_SUCCESS;
182                         goto end;
183
184                 case OPT_MAXBYTES:
185                         if (!ws_strtou16(optarg, NULL, &maxbytes)) {
186                                 g_warning("Invalid parameter maxbytes: %s (max value is %u)",
187                                         optarg, G_MAXUINT16);
188                                 goto end;
189                         }
190                         break;
191
192                 case OPT_COUNT:
193                         if (!ws_strtou64(optarg, NULL, &count)) {
194                                 g_warning("Invalid packet count: %s", optarg);
195                                 goto end;
196                         }
197                         break;
198
199                 case OPT_RANDOM_TYPE:
200                         random_type = TRUE;
201                         break;
202
203                 case OPT_ALL_RANDOM:
204                         all_random = TRUE;
205                         break;
206
207                 case OPT_TYPE:
208                         g_free(type);
209                         type = g_strdup(optarg);
210                         break;
211
212                 case ':':
213                         /* missing option argument */
214                         g_warning("Option '%s' requires an argument", argv[optind - 1]);
215                         break;
216
217                 default:
218                         /* Handle extcap specific options */
219                         if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg))
220                         {
221                                 g_warning("Invalid option: %s", argv[optind - 1]);
222                                 goto end;
223                         }
224                 }
225         }
226
227         extcap_cmdline_debug(argv, argc);
228
229         if (extcap_base_handle_interface(extcap_conf)) {
230                 ret = EXIT_SUCCESS;
231                 goto end;
232         }
233
234         if (extcap_conf->show_config) {
235                 ret = list_config(extcap_conf->interface);
236                 goto end;
237         }
238
239         /* Some sanity checks */
240         if ((random_type) && (all_random)) {
241                 g_warning("You can specify only one between: --random-type, --all-random");
242                 goto end;
243         }
244
245         /* Wireshark sets the type, even when random options are selected. We don't want it */
246         if (random_type || all_random) {
247                 g_free(type);
248                 type = NULL;
249         }
250
251 #ifdef _WIN32
252         result = WSAStartup(MAKEWORD(1,1), &wsaData);
253         if (result != 0) {
254                 g_warning("ERROR: WSAStartup failed with error: %d", result);
255                 goto end;
256         }
257 #endif  /* _WIN32 */
258
259         if (extcap_conf->capture) {
260
261                 if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) {
262                         g_warning("ERROR: invalid interface");
263                         goto end;
264                 }
265
266                 wtap_init(FALSE);
267
268                 if (!all_random) {
269                         produce_type = randpkt_parse_type(type);
270
271                         example = randpkt_find_example(produce_type);
272                         if (!example)
273                                 goto end;
274
275                         g_debug("Generating packets: %s", example->abbrev);
276
277                         randpkt_example_init(example, extcap_conf->fifo, maxbytes);
278                         randpkt_loop(example, count);
279                         randpkt_example_close(example);
280                 } else {
281                         produce_type = randpkt_parse_type(NULL);
282                         example = randpkt_find_example(produce_type);
283                         if (!example)
284                                 goto end;
285                         randpkt_example_init(example, extcap_conf->fifo, maxbytes);
286
287                         while (count-- > 0) {
288                                 randpkt_loop(example, 1);
289                                 produce_type = randpkt_parse_type(NULL);
290
291                                 savedump = example->dump;
292
293                                 example = randpkt_find_example(produce_type);
294                                 if (!example)
295                                         goto end;
296                                 example->dump = savedump;
297                         }
298                         randpkt_example_close(example);
299                 }
300                 ret = EXIT_SUCCESS;
301         }
302
303 end:
304         /* clean up stuff */
305         g_free(type);
306         extcap_base_cleanup(&extcap_conf);
307
308         return ret;
309 }
310
311 #ifdef _WIN32
312 int _stdcall
313 WinMain (struct HINSTANCE__ *hInstance,
314         struct HINSTANCE__ *hPrevInstance,
315         char               *lpszCmdLine,
316         int                 nCmdShow)
317 {
318         return main(__argc, __argv);
319 }
320 #endif
321
322 /*
323  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
324  *
325  * Local variables:
326  * c-basic-offset: 8
327  * tab-width: 8
328  * indent-tabs-mode: t
329  * End:
330  *
331  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
332  * :indentSize=8:tabSize=8:noTabs=false:
333  */