Refactor plugin registration and loading
[metze/wireshark/wip.git] / randpkt.c
1 /*
2  * randpkt.c
3  * ---------
4  * Creates random packet traces. Useful for debugging sniffers by testing
5  * assumptions about the veracity of the data found in the packet.
6  *
7  * Copyright (C) 1999 by Gilbert Ramirez <gram@alumni.rice.edu>
8  *
9  * SPDX-License-Identifier: GPL-2.0+
10  */
11
12 #include <config.h>
13
14 #include <glib.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <wsutil/clopts_common.h>
19 #include <wsutil/cmdarg_err.h>
20 #include <wsutil/unicode-utils.h>
21 #include <wsutil/file_util.h>
22 #include <wsutil/filesystem.h>
23 #include <wsutil/privileges.h>
24
25 #ifdef HAVE_PLUGINS
26 #include <wsutil/plugins.h>
27 #endif
28
29 #include <wsutil/report_message.h>
30 #ifdef HAVE_GETOPT_H
31 #include <getopt.h>
32 #endif
33
34 #ifndef HAVE_GETOPT_LONG
35 #include "wsutil/wsgetopt.h"
36 #endif
37
38 #include "randpkt_core/randpkt_core.h"
39
40 #define INVALID_OPTION 1
41 #define INVALID_TYPE 2
42 #define CLOSE_ERROR 2
43
44 /*
45  * General errors and warnings are reported with an console message
46  * in randpkt.
47  */
48 static void
49 failure_warning_message(const char *msg_format, va_list ap)
50 {
51         fprintf(stderr, "randpkt: ");
52         vfprintf(stderr, msg_format, ap);
53         fprintf(stderr, "\n");
54 }
55
56 /*
57  * Report additional information for an error in command-line arguments.
58  */
59 static void
60 failure_message_cont(const char *msg_format, va_list ap)
61 {
62         vfprintf(stderr, msg_format, ap);
63         fprintf(stderr, "\n");
64 }
65
66 /* Print usage statement and exit program */
67 static void
68 usage(gboolean is_error)
69 {
70         FILE *output;
71         char** abbrev_list;
72         char** longname_list;
73         unsigned i = 0;
74
75         if (!is_error) {
76                 output = stdout;
77         }
78         else {
79                 output = stderr;
80         }
81
82         fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
83         fprintf(output, "Default max bytes (per packet) is 5000\n");
84         fprintf(output, "Default count is 1000.\n");
85         fprintf(output, "-r: random packet type selection\n");
86         fprintf(output, "\n");
87         fprintf(output, "Types:\n");
88
89         /* Get the examples list */
90         randpkt_example_list(&abbrev_list, &longname_list);
91         while (abbrev_list[i] && longname_list[i]) {
92                 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
93                 i++;
94         }
95
96         g_strfreev(abbrev_list);
97         g_strfreev(longname_list);
98
99         fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
100 }
101
102 int
103 main(int argc, char **argv)
104 {
105         char                   *init_progfile_dir_error;
106         int                     opt;
107         int                     produce_type = -1;
108         char                    *produce_filename = NULL;
109         int                     produce_max_bytes = 5000;
110         int                     produce_count = 1000;
111         randpkt_example         *example;
112         guint8*                 type = NULL;
113         int                     allrandom = FALSE;
114         wtap_dumper             *savedump;
115         int                      ret = EXIT_SUCCESS;
116         static const struct option long_options[] = {
117                 {"help", no_argument, NULL, 'h'},
118                 {0, 0, 0, 0 }
119         };
120
121         /*
122          * Get credential information for later use.
123          */
124         init_process_policies();
125
126         /*
127          * Attempt to get the pathname of the directory containing the
128          * executable file.
129          */
130         init_progfile_dir_error = init_progfile_dir(argv[0], main);
131         if (init_progfile_dir_error != NULL) {
132                 fprintf(stderr,
133                     "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
134                     init_progfile_dir_error);
135                 g_free(init_progfile_dir_error);
136         }
137
138         init_report_message(failure_warning_message, failure_warning_message,
139                                 NULL, NULL, NULL);
140
141         wtap_init();
142
143         cmdarg_err_init(failure_warning_message, failure_message_cont);
144
145 #ifdef _WIN32
146         arg_list_utf_16to8(argc, argv);
147         create_app_running_mutex();
148 #endif /* _WIN32 */
149
150         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
151                 switch (opt) {
152                         case 'b':       /* max bytes */
153                                 produce_max_bytes = get_positive_int(optarg, "max bytes");
154                                 if (produce_max_bytes > 65536) {
155                                         cmdarg_err("max bytes is > 65536");
156                                         ret = INVALID_OPTION;
157                                         goto clean_exit;
158                                 }
159                                 break;
160
161                         case 'c':       /* count */
162                                 produce_count = get_positive_int(optarg, "count");
163                                 break;
164
165                         case 't':       /* type of packet to produce */
166                                 type = g_strdup(optarg);
167                                 break;
168
169                         case 'h':
170                                 usage(FALSE);
171                                 goto clean_exit;
172                                 break;
173
174                         case 'r':
175                                 allrandom = TRUE;
176                                 break;
177
178                         default:
179                                 usage(TRUE);
180                                 ret = INVALID_OPTION;
181                                 goto clean_exit;
182                                 break;
183                 }
184         }
185
186         /* any more command line parameters? */
187         if (argc > optind) {
188                 produce_filename = argv[optind];
189         }
190         else {
191                 usage(TRUE);
192                 ret = INVALID_OPTION;
193                 goto clean_exit;
194         }
195
196         if (!allrandom) {
197                 produce_type = randpkt_parse_type(type);
198                 g_free(type);
199
200                 example = randpkt_find_example(produce_type);
201                 if (!example) {
202                         ret = INVALID_OPTION;
203                         goto clean_exit;
204                 }
205
206                 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
207                 if (ret != EXIT_SUCCESS)
208                         goto clean_exit;
209                 randpkt_loop(example, produce_count);
210         } else {
211                 if (type) {
212                         fprintf(stderr, "Can't set type in random mode\n");
213                         ret = INVALID_TYPE;
214                         goto clean_exit;
215                 }
216
217                 produce_type = randpkt_parse_type(NULL);
218                 example = randpkt_find_example(produce_type);
219                 if (!example) {
220                         ret = INVALID_OPTION;
221                         goto clean_exit;
222                 }
223                 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
224                 if (ret != EXIT_SUCCESS)
225                         goto clean_exit;
226
227                 while (produce_count-- > 0) {
228                         randpkt_loop(example, 1);
229                         produce_type = randpkt_parse_type(NULL);
230
231                         savedump = example->dump;
232
233                         example = randpkt_find_example(produce_type);
234                         if (!example) {
235                                 ret = INVALID_OPTION;
236                                 goto clean_exit;
237                         }
238                         example->dump = savedump;
239                 }
240         }
241         if (!randpkt_example_close(example)) {
242                 ret = CLOSE_ERROR;
243         }
244
245 clean_exit:
246         wtap_cleanup();
247         return ret;
248 }
249
250 /*
251  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
252  *
253  * Local variables:
254  * c-basic-offset: 8
255  * tab-width: 8
256  * indent-tabs-mode: t
257  * End:
258  *
259  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
260  * :indentSize=8:tabSize=8:noTabs=false:
261  */