Revert "Override optimization and other flags for make based generators."
[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 "randpkt-core.h"
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <wsutil/ws_diag_control.h>
30 #include <wsutil/unicode-utils.h>
31 #include <wsutil/file_util.h>
32
33 #ifdef HAVE_GETOPT_H
34 #include <getopt.h>
35 #endif
36
37 #ifndef HAVE_GETOPT_LONG
38 #include "wsutil/wsgetopt.h"
39 #endif
40
41 /* Print usage statement and exit program */
42 static void
43 usage(gboolean is_error)
44 {
45         FILE *output;
46         const char** abbrev_list;
47         const char** longname_list;
48         unsigned list_num;
49         unsigned i;
50
51         if (!is_error) {
52                 output = stdout;
53         }
54         else {
55                 output = stderr;
56         }
57
58         fprintf(output, "Usage: randpkt [-b maxbytes] [-c count] [-t type] [-r] filename\n");
59         fprintf(output, "Default max bytes (per packet) is 5000\n");
60         fprintf(output, "Default count is 1000.\n");
61         fprintf(output, "-r: random packet type selection\n");
62         fprintf(output, "\n");
63         fprintf(output, "Types:\n");
64
65         /* Get the examples list */
66         randpkt_example_list(&abbrev_list, &longname_list, &list_num);
67         for (i = 0; i < list_num; i++) {
68                 fprintf(output, "\t%-16s%s\n", abbrev_list[i], longname_list[i]);
69         }
70         g_free((char**)abbrev_list);
71         g_free((char**)longname_list);
72
73         fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
74
75         exit(is_error ? 1 : 0);
76 }
77 int
78 main(int argc, char **argv)
79 {
80         int                     opt;
81         int                     produce_type = -1;
82         char                    *produce_filename = NULL;
83         int                     produce_max_bytes = 5000;
84         int                     produce_count = 1000;
85         randpkt_example         *example;
86         guint8*                 type = NULL;
87         int                     allrandom = FALSE;
88         wtap_dumper             *savedump;
89 DIAG_OFF(cast-qual)
90         static const struct option long_options[] = {
91                 {(char *)"help", no_argument, NULL, 'h'},
92                 {0, 0, 0, 0 }
93         };
94 DIAG_ON(cast-qual)
95
96 #ifdef _WIN32
97         arg_list_utf_16to8(argc, argv);
98         create_app_running_mutex();
99 #endif /* _WIN32 */
100
101         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
102                 switch (opt) {
103                         case 'b':       /* max bytes */
104                                 produce_max_bytes = atoi(optarg);
105                                 if (produce_max_bytes > 65536) {
106                                         fprintf(stderr, "randpkt: Max bytes is 65536\n");
107                                         return 1;
108                                 }
109                                 break;
110
111                         case 'c':       /* count */
112                                 produce_count = atoi(optarg);
113                                 break;
114
115                         case 't':       /* type of packet to produce */
116                                 type = g_strdup(optarg);
117                                 break;
118
119                         case 'h':
120                                 usage(FALSE);
121                                 break;
122
123                         case 'r':
124                                 allrandom = TRUE;
125                                 break;
126
127                         default:
128                                 usage(TRUE);
129                                 break;
130                 }
131         }
132
133         /* any more command line parameters? */
134         if (argc > optind) {
135                 produce_filename = argv[optind];
136         }
137         else {
138                 usage(TRUE);
139         }
140
141         randpkt_seed();
142
143         if (!allrandom) {
144                 produce_type = randpkt_parse_type(type);
145                 g_free(type);
146
147                 example = randpkt_find_example(produce_type);
148                 if (!example)
149                         return 1;
150
151                 randpkt_example_init(example, produce_filename, produce_max_bytes);
152                 randpkt_loop(example, produce_count);
153         } else {
154                 if (type) {
155                         fprintf(stderr, "Can't set type in random mode\n");
156                         return 2;
157                 }
158
159                 produce_type = randpkt_parse_type(NULL);
160                 example = randpkt_find_example(produce_type);
161                 if (!example)
162                         return 1;
163                 randpkt_example_init(example, produce_filename, produce_max_bytes);
164
165                 while (produce_count-- > 0) {
166                         randpkt_loop(example, 1);
167                         produce_type = randpkt_parse_type(NULL);
168
169                         savedump = example->dump;
170
171                         example = randpkt_find_example(produce_type);
172                         if (!example)
173                                 return 1;
174                         example->dump = savedump;
175                 }
176         }
177         if (!randpkt_example_close(example))
178                 return 2;
179         return 0;
180
181 }
182
183 /*
184  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
185  *
186  * Local variables:
187  * c-basic-offset: 8
188  * tab-width: 8
189  * indent-tabs-mode: t
190  * End:
191  *
192  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
193  * :indentSize=8:tabSize=8:noTabs=false:
194  */