bluetooth: Update bluetooth_company_id_vals
[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_message.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 #define INVALID_OPTION 1
53 #define INVALID_TYPE 2
54 #define CLOSE_ERROR 2
55
56 /*
57  * General errors and warnings are reported with an console message
58  * in randpkt.
59  */
60 static void
61 failure_warning_message(const char *msg_format, va_list ap)
62 {
63         fprintf(stderr, "randpkt: ");
64         vfprintf(stderr, msg_format, ap);
65         fprintf(stderr, "\n");
66 }
67
68 /*
69  * Report additional information for an error in command-line arguments.
70  */
71 static void
72 failure_message_cont(const char *msg_format, va_list ap)
73 {
74         vfprintf(stderr, msg_format, ap);
75         fprintf(stderr, "\n");
76 }
77
78 /* Print usage statement and exit program */
79 static void
80 usage(gboolean is_error)
81 {
82         FILE *output;
83         char** abbrev_list;
84         char** longname_list;
85         unsigned i = 0;
86
87         if (!is_error) {
88                 output = stdout;
89         }
90         else {
91                 output = stderr;
92         }
93
94         fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
95         fprintf(output, "Default max bytes (per packet) is 5000\n");
96         fprintf(output, "Default count is 1000.\n");
97         fprintf(output, "-r: random packet type selection\n");
98         fprintf(output, "\n");
99         fprintf(output, "Types:\n");
100
101         /* Get the examples list */
102         randpkt_example_list(&abbrev_list, &longname_list);
103         while (abbrev_list[i] && longname_list[i]) {
104                 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
105                 i++;
106         }
107
108         g_strfreev(abbrev_list);
109         g_strfreev(longname_list);
110
111         fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
112 }
113
114 int
115 main(int argc, char **argv)
116 {
117         char                   *init_progfile_dir_error;
118         int                     opt;
119         int                     produce_type = -1;
120         char                    *produce_filename = NULL;
121         int                     produce_max_bytes = 5000;
122         int                     produce_count = 1000;
123         randpkt_example         *example;
124         guint8*                 type = NULL;
125         int                     allrandom = FALSE;
126         wtap_dumper             *savedump;
127         int                      ret = EXIT_SUCCESS;
128         static const struct option long_options[] = {
129                 {"help", no_argument, NULL, 'h'},
130                 {0, 0, 0, 0 }
131         };
132
133         /*
134          * Get credential information for later use.
135          */
136         init_process_policies();
137
138         /*
139          * Attempt to get the pathname of the directory containing the
140          * executable file.
141          */
142         init_progfile_dir_error = init_progfile_dir(argv[0], main);
143         if (init_progfile_dir_error != NULL) {
144                 fprintf(stderr,
145                     "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
146                     init_progfile_dir_error);
147                 g_free(init_progfile_dir_error);
148         }
149
150         wtap_init();
151
152         cmdarg_err_init(failure_warning_message, failure_message_cont);
153
154 #ifdef _WIN32
155         arg_list_utf_16to8(argc, argv);
156         create_app_running_mutex();
157 #endif /* _WIN32 */
158
159 #ifdef HAVE_PLUGINS
160         /* Register wiretap plugins */
161         init_report_message(failure_warning_message, failure_warning_message,
162             NULL, NULL, NULL);
163
164         /* Scan for plugins.  This does *not* call their registration routines;
165            that's done later.
166
167            Don't report failures to load plugins because most
168            (non-wiretap) plugins *should* fail to load (because
169            we're not linked against libwireshark and dissector
170            plugins need libwireshark). */
171         scan_plugins(DONT_REPORT_LOAD_FAILURE);
172
173         /* Register all libwiretap plugin modules. */
174         register_all_wiretap_modules();
175 #endif
176
177         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
178                 switch (opt) {
179                         case 'b':       /* max bytes */
180                                 produce_max_bytes = get_positive_int(optarg, "max bytes");
181                                 if (produce_max_bytes > 65536) {
182                                         cmdarg_err("max bytes is > 65536");
183                                         ret = INVALID_OPTION;
184                                         goto clean_exit;
185                                 }
186                                 break;
187
188                         case 'c':       /* count */
189                                 produce_count = get_positive_int(optarg, "count");
190                                 break;
191
192                         case 't':       /* type of packet to produce */
193                                 type = g_strdup(optarg);
194                                 break;
195
196                         case 'h':
197                                 usage(FALSE);
198                                 goto clean_exit;
199                                 break;
200
201                         case 'r':
202                                 allrandom = TRUE;
203                                 break;
204
205                         default:
206                                 usage(TRUE);
207                                 ret = INVALID_OPTION;
208                                 goto clean_exit;
209                                 break;
210                 }
211         }
212
213         /* any more command line parameters? */
214         if (argc > optind) {
215                 produce_filename = argv[optind];
216         }
217         else {
218                 usage(TRUE);
219                 ret = INVALID_OPTION;
220                 goto clean_exit;
221         }
222
223         if (!allrandom) {
224                 produce_type = randpkt_parse_type(type);
225                 g_free(type);
226
227                 example = randpkt_find_example(produce_type);
228                 if (!example) {
229                         ret = INVALID_OPTION;
230                         goto clean_exit;
231                 }
232
233                 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
234                 if (ret != EXIT_SUCCESS)
235                         goto clean_exit;
236                 randpkt_loop(example, produce_count);
237         } else {
238                 if (type) {
239                         fprintf(stderr, "Can't set type in random mode\n");
240                         ret = INVALID_TYPE;
241                         goto clean_exit;
242                 }
243
244                 produce_type = randpkt_parse_type(NULL);
245                 example = randpkt_find_example(produce_type);
246                 if (!example) {
247                         ret = INVALID_OPTION;
248                         goto clean_exit;
249                 }
250                 ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
251                 if (ret != EXIT_SUCCESS)
252                         goto clean_exit;
253
254                 while (produce_count-- > 0) {
255                         randpkt_loop(example, 1);
256                         produce_type = randpkt_parse_type(NULL);
257
258                         savedump = example->dump;
259
260                         example = randpkt_find_example(produce_type);
261                         if (!example) {
262                                 ret = INVALID_OPTION;
263                                 goto clean_exit;
264                         }
265                         example->dump = savedump;
266                 }
267         }
268         if (!randpkt_example_close(example)) {
269                 ret = CLOSE_ERROR;
270         }
271
272 clean_exit:
273         wtap_cleanup();
274         return ret;
275 }
276
277 /*
278  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
279  *
280  * Local variables:
281  * c-basic-offset: 8
282  * tab-width: 8
283  * indent-tabs-mode: t
284  * End:
285  *
286  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
287  * :indentSize=8:tabSize=8:noTabs=false:
288  */