ws_strou16() now takes three arguments.
[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  * 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.
14  *
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.
19  *
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.
23  */
24
25 #include "config.h"
26
27 #include "extcap-base.h"
28
29 #include "randpkt_core/randpkt_core.h"
30
31 #define RANDPKT_EXTCAP_INTERFACE "randpkt"
32 #define RANDPKTDUMP_VERSION_MAJOR "0"
33 #define RANDPKTDUMP_VERSION_MINOR "1"
34 #define RANDPKTDUMP_VERSION_RELEASE "0"
35
36 enum {
37         EXTCAP_BASE_OPTIONS_ENUM,
38         OPT_HELP,
39         OPT_VERSION,
40         OPT_MAXBYTES,
41         OPT_COUNT,
42         OPT_RANDOM_TYPE,
43         OPT_ALL_RANDOM,
44         OPT_TYPE
45 };
46
47 static struct option longopts[] = {
48         EXTCAP_BASE_OPTIONS,
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},
56     { 0, 0, 0, 0 }
57 };
58
59
60 static void help(extcap_parameters* extcap_conf)
61 {
62         unsigned i = 0;
63         char** abbrev_list;
64         char** longname_list;
65
66         extcap_help_print(extcap_conf);
67
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]);
72                 i++;
73         }
74         printf("\n");
75         g_strfreev(abbrev_list);
76         g_strfreev(longname_list);
77 }
78
79 static int list_config(char *interface)
80 {
81         unsigned inc = 0;
82         unsigned i = 0;
83         char** abbrev_list;
84         char** longname_list;
85
86         if (!interface) {
87                 g_warning("No interface specified.");
88                 return EXIT_FAILURE;
89         }
90
91         if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
92                 g_warning("Interface must be %s", RANDPKT_EXTCAP_INTERFACE);
93                 return EXIT_FAILURE;
94         }
95
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",
98                 inc++);
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",
101                 inc++);
102         printf("arg {number=%u}{call=--random-type}{display=Random type}"
103                 "{type=boolean}{default=false}{tooltip=The packets type is randomly chosen}\n",
104                 inc++);
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",
107                 inc++);
108
109         /* Now the types */
110         printf("arg {number=%u}{call=--type}{display=Type of packet}"
111                 "{type=selector}{tooltip=Type of packet to generate}\n",
112                 inc);
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]);
116                 i++;
117         }
118         g_strfreev(abbrev_list);
119         g_strfreev(longname_list);
120         inc++;
121
122         return EXIT_SUCCESS;
123 }
124
125 int main(int argc, char *argv[])
126 {
127         int option_idx = 0;
128         int result;
129         int maxbytes = 5000;
130         guint64 count = 1000;
131         int random_type = FALSE;
132         int all_random = FALSE;
133         char* type = NULL;
134         int produce_type = -1;
135         randpkt_example *example;
136         wtap_dumper* savedump;
137         int i;
138         int ret = EXIT_FAILURE;
139
140 #ifdef _WIN32
141         WSADATA wsaData;
142 #endif  /* _WIN32 */
143
144         extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
145         char* help_header = NULL;
146
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");
150
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);
159         g_free(help_header);
160
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");
169
170         if (argc == 1) {
171                 help(extcap_conf);
172                 goto end;
173         }
174
175 #ifdef _WIN32
176         attach_parent_console();
177 #endif  /* _WIN32 */
178
179         for (i = 0; i < argc; i++)
180                 g_debug("%s ", argv[i]);
181
182         while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
183                 switch (result) {
184                 case OPT_VERSION:
185                         printf("%s\n", extcap_conf->version);
186                         ret = EXIT_SUCCESS;
187                         goto end;
188
189                 case OPT_HELP:
190                         help(extcap_conf);
191                         ret = EXIT_SUCCESS;
192                         goto end;
193
194                 case OPT_MAXBYTES:
195                         maxbytes = atoi(optarg);
196                         if (maxbytes > MAXBYTES_LIMIT) {
197                                 g_warning("randpktdump: Max bytes is %d", MAXBYTES_LIMIT);
198                                 goto end;
199                         }
200                         break;
201
202                 case OPT_COUNT:
203                         count = g_ascii_strtoull(optarg, NULL, 10);
204                         break;
205
206                 case OPT_RANDOM_TYPE:
207                         if (!g_ascii_strcasecmp("true", optarg)) {
208                                 random_type = TRUE;
209                         }
210                         break;
211
212                 case OPT_ALL_RANDOM:
213                         if (!g_ascii_strcasecmp("true", optarg)) {
214                                 all_random = TRUE;
215                         }
216                         break;
217
218                 case OPT_TYPE:
219                         g_free(type);
220                         type = g_strdup(optarg);
221                         break;
222
223                 case ':':
224                         /* missing option argument */
225                         g_warning("Option '%s' requires an argument", argv[optind - 1]);
226                         break;
227
228                 default:
229                         /* Handle extcap specific options */
230                         if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg))
231                         {
232                                 g_warning("Invalid option: %s", argv[optind - 1]);
233                                 goto end;
234                         }
235                 }
236         }
237
238         if (optind != argc) {
239                 g_warning("Invalid option: %s", argv[optind]);
240                 goto end;
241         }
242
243         if (extcap_base_handle_interface(extcap_conf)) {
244                 ret = EXIT_SUCCESS;
245                 goto end;
246         }
247
248         if (extcap_conf->show_config) {
249                 ret = list_config(extcap_conf->interface);
250                 goto end;
251         }
252
253         /* Some sanity checks */
254         if ((random_type) && (all_random)) {
255                 g_warning("You can specify only one between: --random-type, --all-random");
256                 goto end;
257         }
258
259         /* Wireshark sets the type, even when random options are selected. We don't want it */
260         if (random_type || all_random) {
261                 g_free(type);
262                 type = NULL;
263         }
264
265 #ifdef _WIN32
266         result = WSAStartup(MAKEWORD(1,1), &wsaData);
267         if (result != 0) {
268                 g_warning("ERROR: WSAStartup failed with error: %d", result);
269                 goto end;
270         }
271 #endif  /* _WIN32 */
272
273         if (extcap_conf->capture) {
274
275                 if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) {
276                         g_warning("ERROR: invalid interface");
277                         goto end;
278                 }
279
280                 if (!all_random) {
281                         produce_type = randpkt_parse_type(type);
282
283                         example = randpkt_find_example(produce_type);
284                         if (!example)
285                                 goto end;
286
287                         g_debug("Generating packets: %s", example->abbrev);
288
289                         randpkt_example_init(example, extcap_conf->fifo, maxbytes);
290                         randpkt_loop(example, count);
291                         randpkt_example_close(example);
292                 } else {
293                         produce_type = randpkt_parse_type(NULL);
294                         example = randpkt_find_example(produce_type);
295                         if (!example)
296                                 goto end;
297                         randpkt_example_init(example, extcap_conf->fifo, maxbytes);
298
299                         while (count-- > 0) {
300                                 randpkt_loop(example, 1);
301                                 produce_type = randpkt_parse_type(NULL);
302
303                                 savedump = example->dump;
304
305                                 example = randpkt_find_example(produce_type);
306                                 if (!example)
307                                         goto end;
308                                 example->dump = savedump;
309                         }
310                         randpkt_example_close(example);
311                 }
312                 ret = EXIT_SUCCESS;
313         }
314
315 end:
316         /* clean up stuff */
317         g_free(type);
318         extcap_base_cleanup(&extcap_conf);
319
320         return ret;
321 }
322
323 #ifdef _WIN32
324 int _stdcall
325 WinMain (struct HINSTANCE__ *hInstance,
326         struct HINSTANCE__ *hPrevInstance,
327         char               *lpszCmdLine,
328         int                 nCmdShow)
329 {
330         return main(__argc, __argv);
331 }
332 #endif
333
334 /*
335  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
336  *
337  * Local variables:
338  * c-basic-offset: 8
339  * tab-width: 8
340  * indent-tabs-mode: t
341  * End:
342  *
343  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
344  * :indentSize=8:tabSize=8:noTabs=false:
345  */