f604e77812a944f2a8c3082cefe479eae944fec6
[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/unicode-utils.h>
31 #include <wsutil/file_util.h>
32 #include <wsutil/filesystem.h>
33 #include <wsutil/privileges.h>
34
35 #ifdef HAVE_PLUGINS
36 #include <wsutil/plugins.h>
37 #endif
38
39 #include <wsutil/report_err.h>
40 #ifdef HAVE_GETOPT_H
41 #include <getopt.h>
42 #endif
43
44 #ifndef HAVE_GETOPT_LONG
45 #include "wsutil/wsgetopt.h"
46 #endif
47
48 #include "randpkt_core/randpkt_core.h"
49
50 #ifdef HAVE_PLUGINS
51 /*
52  *  Don't report failures to load plugins because most (non-wiretap) plugins
53  *  *should* fail to load (because we're not linked against libwireshark and
54  *  dissector plugins need libwireshark).
55  */
56 static void
57 failure_message(const char *msg_format _U_, va_list ap _U_)
58 {
59   return;
60 }
61 #endif
62
63 /* Print usage statement and exit program */
64 static void
65 usage(gboolean is_error)
66 {
67         FILE *output;
68         char** abbrev_list;
69         char** longname_list;
70         unsigned i = 0;
71
72         if (!is_error) {
73                 output = stdout;
74         }
75         else {
76                 output = stderr;
77         }
78
79         fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
80         fprintf(output, "Default max bytes (per packet) is 5000\n");
81         fprintf(output, "Default count is 1000.\n");
82         fprintf(output, "-r: random packet type selection\n");
83         fprintf(output, "\n");
84         fprintf(output, "Types:\n");
85
86         /* Get the examples list */
87         randpkt_example_list(&abbrev_list, &longname_list);
88         while (abbrev_list[i] && longname_list[i]) {
89                 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
90                 i++;
91         }
92
93         g_strfreev(abbrev_list);
94         g_strfreev(longname_list);
95
96         fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
97
98         exit(is_error ? 1 : 0);
99 }
100 int
101 main(int argc, char **argv)
102 {
103         int                     opt;
104         int                     produce_type = -1;
105         char                    *produce_filename = NULL;
106         int                     produce_max_bytes = 5000;
107         int                     produce_count = 1000;
108         randpkt_example         *example;
109         guint8*                 type = NULL;
110         int                     allrandom = FALSE;
111         wtap_dumper             *savedump;
112         static const struct option long_options[] = {
113                 {"help", no_argument, NULL, 'h'},
114                 {0, 0, 0, 0 }
115         };
116
117 #ifdef HAVE_PLUGINS
118         char  *init_progfile_dir_error;
119 #endif
120
121   /*
122    * Get credential information for later use.
123    */
124   init_process_policies();
125   init_open_routines();
126
127 #ifdef _WIN32
128         arg_list_utf_16to8(argc, argv);
129         create_app_running_mutex();
130 #endif /* _WIN32 */
131
132 #ifdef HAVE_PLUGINS
133         /* Register wiretap plugins */
134         if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
135                 g_warning("randpkt: init_progfile_dir(): %s", init_progfile_dir_error);
136                 g_free(init_progfile_dir_error);
137         } else {
138                 /* Register all the plugin types we have. */
139                 wtap_register_plugin_types(); /* Types known to libwiretap */
140
141                 init_report_err(failure_message,NULL,NULL,NULL);
142
143                 /* Scan for plugins.  This does *not* call their registration routines;
144                    that's done later. */
145                 scan_plugins();
146
147                 /* Register all libwiretap plugin modules. */
148                 register_all_wiretap_modules();
149         }
150 #endif
151
152         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
153                 switch (opt) {
154                         case 'b':       /* max bytes */
155                                 produce_max_bytes = atoi(optarg);
156                                 if (produce_max_bytes > 65536) {
157                                         fprintf(stderr, "randpkt: Max bytes is 65536\n");
158                                         return 1;
159                                 }
160                                 break;
161
162                         case 'c':       /* count */
163                                 produce_count = atoi(optarg);
164                                 break;
165
166                         case 't':       /* type of packet to produce */
167                                 type = g_strdup(optarg);
168                                 break;
169
170                         case 'h':
171                                 usage(FALSE);
172                                 break;
173
174                         case 'r':
175                                 allrandom = TRUE;
176                                 break;
177
178                         default:
179                                 usage(TRUE);
180                                 break;
181                 }
182         }
183
184         /* any more command line parameters? */
185         if (argc > optind) {
186                 produce_filename = argv[optind];
187         }
188         else {
189                 usage(TRUE);
190         }
191
192         if (!allrandom) {
193                 produce_type = randpkt_parse_type(type);
194                 g_free(type);
195
196                 example = randpkt_find_example(produce_type);
197                 if (!example)
198                         return 1;
199
200                 randpkt_example_init(example, produce_filename, produce_max_bytes);
201                 randpkt_loop(example, produce_count);
202         } else {
203                 if (type) {
204                         fprintf(stderr, "Can't set type in random mode\n");
205                         return 2;
206                 }
207
208                 produce_type = randpkt_parse_type(NULL);
209                 example = randpkt_find_example(produce_type);
210                 if (!example)
211                         return 1;
212                 randpkt_example_init(example, produce_filename, produce_max_bytes);
213
214                 while (produce_count-- > 0) {
215                         randpkt_loop(example, 1);
216                         produce_type = randpkt_parse_type(NULL);
217
218                         savedump = example->dump;
219
220                         example = randpkt_find_example(produce_type);
221                         if (!example)
222                                 return 1;
223                         example->dump = savedump;
224                 }
225         }
226         if (!randpkt_example_close(example))
227                 return 2;
228         return 0;
229
230 }
231
232 /*
233  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
234  *
235  * Local variables:
236  * c-basic-offset: 8
237  * tab-width: 8
238  * indent-tabs-mode: t
239  * End:
240  *
241  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
242  * :indentSize=8:tabSize=8:noTabs=false:
243  */