WiX: Disable desktop icon and shortcut checkboxes.
[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  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <config.h>
25
26 #include <glib.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <wsutil/clopts_common.h>
31 #include <wsutil/cmdarg_err.h>
32 #include <wsutil/unicode-utils.h>
33 #include <wsutil/file_util.h>
34 #include <wsutil/filesystem.h>
35 #include <wsutil/privileges.h>
36
37 #ifdef HAVE_PLUGINS
38 #include <wsutil/plugins.h>
39 #endif
40
41 #include <wsutil/report_err.h>
42 #ifdef HAVE_GETOPT_H
43 #include <getopt.h>
44 #endif
45
46 #ifndef HAVE_GETOPT_LONG
47 #include "wsutil/wsgetopt.h"
48 #endif
49
50 #include "randpkt_core/randpkt_core.h"
51
52 /*
53  * General errors are reported with an console message in randpkt.
54  */
55 static void
56 failure_message(const char *msg_format, va_list ap)
57 {
58         fprintf(stderr, "randpkt: ");
59         vfprintf(stderr, msg_format, ap);
60         fprintf(stderr, "\n");
61 }
62
63 /*
64  * Report additional information for an error in command-line arguments.
65  */
66 static void
67 failure_message_cont(const char *msg_format, va_list ap)
68 {
69         vfprintf(stderr, msg_format, ap);
70         fprintf(stderr, "\n");
71 }
72
73 /* Print usage statement and exit program */
74 static void
75 usage(gboolean is_error)
76 {
77         FILE *output;
78         char** abbrev_list;
79         char** longname_list;
80         unsigned i = 0;
81
82         if (!is_error) {
83                 output = stdout;
84         }
85         else {
86                 output = stderr;
87         }
88
89         fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
90         fprintf(output, "Default max bytes (per packet) is 5000\n");
91         fprintf(output, "Default count is 1000.\n");
92         fprintf(output, "-r: random packet type selection\n");
93         fprintf(output, "\n");
94         fprintf(output, "Types:\n");
95
96         /* Get the examples list */
97         randpkt_example_list(&abbrev_list, &longname_list);
98         while (abbrev_list[i] && longname_list[i]) {
99                 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
100                 i++;
101         }
102
103         g_strfreev(abbrev_list);
104         g_strfreev(longname_list);
105
106         fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
107
108         exit(is_error ? 1 : 0);
109 }
110 int
111 main(int argc, char **argv)
112 {
113         int                     opt;
114         int                     produce_type = -1;
115         char                    *produce_filename = NULL;
116         int                     produce_max_bytes = 5000;
117         int                     produce_count = 1000;
118         randpkt_example         *example;
119         guint8*                 type = NULL;
120         int                     allrandom = FALSE;
121         wtap_dumper             *savedump;
122         static const struct option long_options[] = {
123                 {"help", no_argument, NULL, 'h'},
124                 {0, 0, 0, 0 }
125         };
126
127 #ifdef HAVE_PLUGINS
128         char  *init_progfile_dir_error;
129 #endif
130
131         /*
132          * Get credential information for later use.
133          */
134         init_process_policies();
135         init_open_routines();
136
137         cmdarg_err_init(failure_message, failure_message_cont);
138
139 #ifdef _WIN32
140         arg_list_utf_16to8(argc, argv);
141         create_app_running_mutex();
142 #endif /* _WIN32 */
143
144 #ifdef HAVE_PLUGINS
145         /* Register wiretap plugins */
146         if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
147                 g_warning("randpkt: init_progfile_dir(): %s", init_progfile_dir_error);
148                 g_free(init_progfile_dir_error);
149         } else {
150                 /* Register all the plugin types we have. */
151                 wtap_register_plugin_types(); /* Types known to libwiretap */
152
153                 init_report_err(failure_message,NULL,NULL,NULL);
154
155                 /* Scan for plugins.  This does *not* call their registration routines;
156                    that's done later.
157
158                    Don't report failures to load plugins because most
159                    (non-wiretap) plugins *should* fail to load (because
160                    we're not linked against libwireshark and dissector
161                    plugins need libwireshark). */
162                 scan_plugins(DONT_REPORT_LOAD_FAILURE);
163
164                 /* Register all libwiretap plugin modules. */
165                 register_all_wiretap_modules();
166         }
167 #endif
168
169         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
170                 switch (opt) {
171                         case 'b':       /* max bytes */
172                                 produce_max_bytes = get_positive_int(optarg, "max bytes");
173                                 if (produce_max_bytes > 65536) {
174                                         cmdarg_err("max bytes is > 65536");
175                                         return 1;
176                                 }
177                                 break;
178
179                         case 'c':       /* count */
180                                 produce_count = get_positive_int(optarg, "count");
181                                 break;
182
183                         case 't':       /* type of packet to produce */
184                                 type = g_strdup(optarg);
185                                 break;
186
187                         case 'h':
188                                 usage(FALSE);
189                                 break;
190
191                         case 'r':
192                                 allrandom = TRUE;
193                                 break;
194
195                         default:
196                                 usage(TRUE);
197                                 break;
198                 }
199         }
200
201         /* any more command line parameters? */
202         if (argc > optind) {
203                 produce_filename = argv[optind];
204         }
205         else {
206                 usage(TRUE);
207         }
208
209         if (!allrandom) {
210                 produce_type = randpkt_parse_type(type);
211                 g_free(type);
212
213                 example = randpkt_find_example(produce_type);
214                 if (!example)
215                         return 1;
216
217                 randpkt_example_init(example, produce_filename, produce_max_bytes);
218                 randpkt_loop(example, produce_count);
219         } else {
220                 if (type) {
221                         fprintf(stderr, "Can't set type in random mode\n");
222                         return 2;
223                 }
224
225                 produce_type = randpkt_parse_type(NULL);
226                 example = randpkt_find_example(produce_type);
227                 if (!example)
228                         return 1;
229                 randpkt_example_init(example, produce_filename, produce_max_bytes);
230
231                 while (produce_count-- > 0) {
232                         randpkt_loop(example, 1);
233                         produce_type = randpkt_parse_type(NULL);
234
235                         savedump = example->dump;
236
237                         example = randpkt_find_example(produce_type);
238                         if (!example)
239                                 return 1;
240                         example->dump = savedump;
241                 }
242         }
243         if (!randpkt_example_close(example))
244                 return 2;
245         return 0;
246
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  */