epan/dissectors/packet-xml.c try to decrypt data, but the data doesn't look correct yet
[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-or-later
10  */
11
12 #include <config.h>
13
14 #include <glib.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <ui/clopts_common.h>
19 #include <ui/cmdarg_err.h>
20 #include <wsutil/file_util.h>
21 #include <wsutil/filesystem.h>
22 #include <wsutil/privileges.h>
23 #include <cli_main.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]);
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(TRUE);
142
143         cmdarg_err_init(failure_warning_message, failure_message_cont);
144
145 #ifdef _WIN32
146         create_app_running_mutex();
147 #endif /* _WIN32 */
148
149         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
150                 switch (opt) {
151                         case 'b':       /* max bytes */
152                                 produce_max_bytes = get_positive_int(optarg, "max bytes");
153                                 if (produce_max_bytes > 65536) {
154                                         cmdarg_err("max bytes is > 65536");
155                                         ret = INVALID_OPTION;
156                                         goto clean_exit;
157                                 }
158                                 break;
159
160                         case 'c':       /* count */
161                                 produce_count = get_positive_int(optarg, "count");
162                                 break;
163
164                         case 't':       /* type of packet to produce */
165                                 type = g_strdup(optarg);
166                                 break;
167
168                         case 'h':
169                                 usage(FALSE);
170                                 goto clean_exit;
171                                 break;
172
173                         case 'r':
174                                 allrandom = TRUE;
175                                 break;
176
177                         default:
178                                 usage(TRUE);
179                                 ret = INVALID_OPTION;
180                                 goto clean_exit;
181                                 break;
182                 }
183         }
184
185         /* any more command line parameters? */
186         if (argc > optind) {
187                 produce_filename = argv[optind];
188         }
189         else {
190                 usage(TRUE);
191                 ret = INVALID_OPTION;
192                 goto clean_exit;
193         }
194
195         if (!allrandom) {
196                 produce_type = randpkt_parse_type(type);
197                 g_free(type);
198
199                 example = randpkt_find_example(produce_type);
200                 if (!example) {
201                         ret = INVALID_OPTION;
202                         goto clean_exit;
203                 }
204
205                 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
206                 if (ret != EXIT_SUCCESS)
207                         goto clean_exit;
208                 randpkt_loop(example, produce_count, 0);
209         } else {
210                 if (type) {
211                         fprintf(stderr, "Can't set type in random mode\n");
212                         ret = INVALID_TYPE;
213                         goto clean_exit;
214                 }
215
216                 produce_type = randpkt_parse_type(NULL);
217                 example = randpkt_find_example(produce_type);
218                 if (!example) {
219                         ret = INVALID_OPTION;
220                         goto clean_exit;
221                 }
222                 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
223                 if (ret != EXIT_SUCCESS)
224                         goto clean_exit;
225
226                 while (produce_count-- > 0) {
227                         randpkt_loop(example, 1, 0);
228                         produce_type = randpkt_parse_type(NULL);
229
230                         savedump = example->dump;
231
232                         example = randpkt_find_example(produce_type);
233                         if (!example) {
234                                 ret = INVALID_OPTION;
235                                 goto clean_exit;
236                         }
237                         example->dump = savedump;
238                 }
239         }
240         if (!randpkt_example_close(example)) {
241                 ret = CLOSE_ERROR;
242         }
243
244 clean_exit:
245         wtap_cleanup();
246         return ret;
247 }
248
249 /*
250  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
251  *
252  * Local variables:
253  * c-basic-offset: 8
254  * tab-width: 8
255  * indent-tabs-mode: t
256  * End:
257  *
258  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
259  * :indentSize=8:tabSize=8:noTabs=false:
260  */