ssl: fix RSA key matching with Client certs
[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         static const struct option long_options[] = {
90                 {"help", no_argument, NULL, 'h'},
91                 {0, 0, 0, 0 }
92         };
93
94 #ifdef _WIN32
95         arg_list_utf_16to8(argc, argv);
96         create_app_running_mutex();
97 #endif /* _WIN32 */
98
99         while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
100                 switch (opt) {
101                         case 'b':       /* max bytes */
102                                 produce_max_bytes = atoi(optarg);
103                                 if (produce_max_bytes > 65536) {
104                                         fprintf(stderr, "randpkt: Max bytes is 65536\n");
105                                         return 1;
106                                 }
107                                 break;
108
109                         case 'c':       /* count */
110                                 produce_count = atoi(optarg);
111                                 break;
112
113                         case 't':       /* type of packet to produce */
114                                 type = g_strdup(optarg);
115                                 break;
116
117                         case 'h':
118                                 usage(FALSE);
119                                 break;
120
121                         case 'r':
122                                 allrandom = TRUE;
123                                 break;
124
125                         default:
126                                 usage(TRUE);
127                                 break;
128                 }
129         }
130
131         /* any more command line parameters? */
132         if (argc > optind) {
133                 produce_filename = argv[optind];
134         }
135         else {
136                 usage(TRUE);
137         }
138
139         randpkt_seed();
140
141         if (!allrandom) {
142                 produce_type = randpkt_parse_type(type);
143                 g_free(type);
144
145                 example = randpkt_find_example(produce_type);
146                 if (!example)
147                         return 1;
148
149                 randpkt_example_init(example, produce_filename, produce_max_bytes);
150                 randpkt_loop(example, produce_count);
151         } else {
152                 if (type) {
153                         fprintf(stderr, "Can't set type in random mode\n");
154                         return 2;
155                 }
156
157                 produce_type = randpkt_parse_type(NULL);
158                 example = randpkt_find_example(produce_type);
159                 if (!example)
160                         return 1;
161                 randpkt_example_init(example, produce_filename, produce_max_bytes);
162
163                 while (produce_count-- > 0) {
164                         randpkt_loop(example, 1);
165                         produce_type = randpkt_parse_type(NULL);
166
167                         savedump = example->dump;
168
169                         example = randpkt_find_example(produce_type);
170                         if (!example)
171                                 return 1;
172                         example->dump = savedump;
173                 }
174         }
175         if (!randpkt_example_close(example))
176                 return 2;
177         return 0;
178
179 }
180
181 /*
182  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
183  *
184  * Local variables:
185  * c-basic-offset: 8
186  * tab-width: 8
187  * indent-tabs-mode: t
188  * End:
189  *
190  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
191  * :indentSize=8:tabSize=8:noTabs=false:
192  */