MSWSP: WIP: dissect_CPMSetBindings()
[wireshark-wip.git] / reordercap.c
blob6b1cc95cb7a12b6a7bac1135bd3990dd029f2c69
1 /* Reorder the frames from an input dump file, and write to output dump file.
2 * Martin Mathieson and Jakub Jawadzki
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <glib.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #include "wtap.h"
39 #ifndef HAVE_GETOPT
40 #include "wsutil/wsgetopt.h"
41 #endif
43 /* Show command-line usage */
44 static void usage(void)
46 fprintf(stderr, "Reordercap %s"
47 #ifdef SVNVERSION
48 " (" SVNVERSION " from " SVNPATH ")"
49 #endif
50 "\n", VERSION);
51 fprintf(stderr, "Reorder timestamps of input file frames into output file.\n");
52 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
53 fprintf(stderr, "\n");
54 fprintf(stderr, "Usage: reordercap [options] <infile> <outfile>\n");
55 fprintf(stderr, "\n");
56 fprintf(stderr, "Options:\n");
57 fprintf(stderr, " -n don't write to output file if the input file is ordered.\n");
60 /* Remember where this frame was in the file */
61 typedef struct FrameRecord_t {
62 gint64 offset;
63 guint32 length;
64 guint num;
66 nstime_t time;
67 } FrameRecord_t;
70 /**************************************************/
71 /* Debugging only */
73 /* Enable this symbol to see debug output */
74 /* #define REORDER_DEBUG */
76 #ifdef REORDER_DEBUG
77 #define DEBUG_PRINT printf
78 #else
79 #define DEBUG_PRINT(...)
80 #endif
81 /**************************************************/
84 static void
85 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
86 const char *infile)
88 int err;
89 gchar *err_info;
90 struct wtap_pkthdr phdr;
92 DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n",
93 frame->offset, frame->length);
96 /* Re-read the first frame from the stored location */
97 if (!wtap_seek_read(wth, frame->offset, &phdr, buf, frame->length,
98 &err, &err_info)) {
99 if (err != 0) {
100 /* Print a message noting that the read failed somewhere along the line. */
101 fprintf(stderr,
102 "reordercap: An error occurred while re-reading \"%s\": %s.\n",
103 infile, wtap_strerror(err));
104 switch (err) {
106 case WTAP_ERR_UNSUPPORTED:
107 case WTAP_ERR_UNSUPPORTED_ENCAP:
108 case WTAP_ERR_BAD_FILE:
109 fprintf(stderr, "(%s)\n", err_info);
110 g_free(err_info);
111 break;
113 exit(1);
117 /* Copy, and set length and timestamp from item. */
118 /* TODO: remove when wtap_seek_read() will read phdr */
119 phdr.ts = frame->time;
121 /* Dump frame to outfile */
122 if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
123 fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
124 wtap_strerror(err));
125 exit(1);
129 /* Comparing timestamps between 2 frames.
130 -1 if (t1 < t2)
131 0 if (t1 == t2)
132 1 if (t1 > t2)
134 static int
135 frames_compare(gconstpointer a, gconstpointer b)
137 const FrameRecord_t *frame1 = *(const FrameRecord_t **) a;
138 const FrameRecord_t *frame2 = *(const FrameRecord_t **) b;
140 const nstime_t *time1 = &frame1->time;
141 const nstime_t *time2 = &frame2->time;
143 if (time1->secs > time2->secs)
144 return 1;
145 if (time1->secs < time2->secs)
146 return -1;
148 /* time1->secs == time2->secs */
149 if (time1->nsecs > time2->nsecs)
150 return 1;
151 if (time1->nsecs < time2->nsecs)
152 return -1;
154 /* time1->nsecs == time2->nsecs */
156 if (frame1->num > frame2->num)
157 return 1;
158 if (frame1->num < frame2->num)
159 return -1;
160 return 0;
164 /********************************************************************/
165 /* Main function. */
166 /********************************************************************/
167 int main(int argc, char *argv[])
169 wtap *wth = NULL;
170 wtap_dumper *pdh = NULL;
171 Buffer buf;
172 int err;
173 gchar *err_info;
174 gint64 data_offset;
175 const struct wtap_pkthdr *phdr;
176 guint wrong_order_count = 0;
177 gboolean write_output_regardless = TRUE;
178 guint i;
179 wtapng_section_t *shb_hdr;
180 wtapng_iface_descriptions_t *idb_inf;
182 GPtrArray *frames;
183 FrameRecord_t *prevFrame = NULL;
185 int opt;
186 int file_count;
187 char *infile;
188 char *outfile;
190 /* Process the options first */
191 while ((opt = getopt(argc, argv, "n")) != -1) {
192 switch (opt) {
193 case 'n':
194 write_output_regardless = FALSE;
195 break;
196 case '?':
197 usage();
198 exit(1);
202 /* Remaining args are file names */
203 file_count = argc - optind;
204 if (file_count == 2) {
205 infile = argv[optind];
206 outfile = argv[optind+1];
208 else {
209 usage();
210 exit(1);
213 /* Open infile */
214 wth = wtap_open_offline(infile, &err, &err_info, TRUE);
215 if (wth == NULL) {
216 fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
217 wtap_strerror(err));
218 switch (err) {
220 case WTAP_ERR_UNSUPPORTED:
221 case WTAP_ERR_UNSUPPORTED_ENCAP:
222 case WTAP_ERR_BAD_FILE:
223 fprintf(stderr, "(%s)\n", err_info);
224 g_free(err_info);
225 break;
227 exit(1);
229 DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
231 shb_hdr = wtap_file_get_shb_info(wth);
232 idb_inf = wtap_file_get_idb_info(wth);
234 /* Open outfile (same filetype/encap as input file) */
235 pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
236 65535, FALSE, shb_hdr, idb_inf, &err);
237 g_free(idb_inf);
238 if (pdh == NULL) {
239 fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
240 outfile, wtap_strerror(err));
241 g_free(shb_hdr);
242 exit(1);
245 /* Allocate the array of frame pointers. */
246 frames = g_ptr_array_new();
248 /* Read each frame from infile */
249 while (wtap_read(wth, &err, &err_info, &data_offset)) {
250 FrameRecord_t *newFrameRecord;
252 phdr = wtap_phdr(wth);
254 newFrameRecord = g_slice_new(FrameRecord_t);
255 newFrameRecord->num = frames->len + 1;
256 newFrameRecord->offset = data_offset;
257 newFrameRecord->length = phdr->len;
258 newFrameRecord->time = phdr->ts;
260 if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
261 wrong_order_count++;
264 g_ptr_array_add(frames, newFrameRecord);
265 prevFrame = newFrameRecord;
267 if (err != 0) {
268 /* Print a message noting that the read failed somewhere along the line. */
269 fprintf(stderr,
270 "reordercap: An error occurred while reading \"%s\": %s.\n",
271 infile, wtap_strerror(err));
272 switch (err) {
274 case WTAP_ERR_UNSUPPORTED:
275 case WTAP_ERR_UNSUPPORTED_ENCAP:
276 case WTAP_ERR_BAD_FILE:
277 fprintf(stderr, "(%s)\n", err_info);
278 g_free(err_info);
279 break;
283 printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
285 /* Sort the frames */
286 if (wrong_order_count > 0) {
287 g_ptr_array_sort(frames, frames_compare);
290 /* Write out each sorted frame in turn */
291 buffer_init(&buf, 1500);
292 for (i = 0; i < frames->len; i++) {
293 FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
295 /* Avoid writing if already sorted and configured to */
296 if (write_output_regardless || (wrong_order_count > 0)) {
297 frame_write(frame, wth, pdh, &buf, infile);
299 g_slice_free(FrameRecord_t, frame);
301 buffer_free(&buf);
303 if (!write_output_regardless && (wrong_order_count == 0)) {
304 printf("Not writing output file because input file is already in order!\n");
307 /* Free the whole array */
308 g_ptr_array_free(frames, TRUE);
310 /* Close outfile */
311 if (!wtap_dump_close(pdh, &err)) {
312 fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
313 wtap_strerror(err));
314 g_free(shb_hdr);
315 exit(1);
317 g_free(shb_hdr);
319 /* Finally, close infile */
320 wtap_fdclose(wth);
322 return 0;