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