packet-smb-direct: only handle RDMA_SEND_* messages
[metze/wireshark/wip.git] / reordercap.c
1 /* Reorder the frames from an input dump file, and write to output dump file.
2  * Martin Mathieson and Jakub Jawadzki
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  *
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <glib.h>
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "wtap.h"
38
39 #ifndef HAVE_GETOPT
40 #include "wsutil/wsgetopt.h"
41 #endif
42
43 /* Show command-line usage */
44 static void usage(gboolean is_error)
45 {
46     FILE *output;
47
48     if (!is_error) {
49         output = stdout;
50     }
51     else {
52         output = stderr;
53     }
54
55     fprintf(output, "Reordercap %s"
56 #ifdef GITVERSION
57                       " (" GITVERSION " from " GITBRANCH ")"
58 #endif
59                       "\n", VERSION);
60     fprintf(output, "Reorder timestamps of input file frames into output file.\n");
61     fprintf(output, "See http://www.wireshark.org for more information.\n");
62     fprintf(output, "\n");
63     fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
64     fprintf(output, "\n");
65     fprintf(output, "Options:\n");
66     fprintf(output, "  -n        don't write to output file if the input file is ordered.\n");
67     fprintf(output, "  -h        display this help and exit.\n");
68 }
69
70 /* Remember where this frame was in the file */
71 typedef struct FrameRecord_t {
72     gint64       offset;
73     guint        num;
74
75     nstime_t     time;
76 } FrameRecord_t;
77
78
79 /**************************************************/
80 /* Debugging only                                 */
81
82 /* Enable this symbol to see debug output */
83 /* #define REORDER_DEBUG */
84
85 #ifdef REORDER_DEBUG
86 #define DEBUG_PRINT printf
87 #else
88 #define DEBUG_PRINT(...)
89 #endif
90 /**************************************************/
91
92
93 static void
94 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
95             const char *infile)
96 {
97     int    err;
98     gchar  *err_info;
99     struct wtap_pkthdr phdr;
100
101     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
102                 frame->offset);
103
104
105     /* Re-read the first frame from the stored location */
106     if (!wtap_seek_read(wth, frame->offset, &phdr, buf, &err, &err_info)) {
107         if (err != 0) {
108             /* Print a message noting that the read failed somewhere along the line. */
109             fprintf(stderr,
110                     "reordercap: An error occurred while re-reading \"%s\": %s.\n",
111                     infile, wtap_strerror(err));
112             switch (err) {
113
114             case WTAP_ERR_UNSUPPORTED:
115             case WTAP_ERR_UNSUPPORTED_ENCAP:
116             case WTAP_ERR_BAD_FILE:
117                 fprintf(stderr, "(%s)\n", err_info);
118                 g_free(err_info);
119                 break;
120             }
121             exit(1);
122         }
123     }
124
125     /* Copy, and set length and timestamp from item. */
126     /* TODO: remove when wtap_seek_read() will read phdr */
127     phdr.ts = frame->time;
128
129     /* Dump frame to outfile */
130     if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
131         fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
132                 wtap_strerror(err));
133         exit(1);
134     }
135 }
136
137 /* Comparing timestamps between 2 frames.
138    -1 if (t1 < t2)
139    0  if (t1 == t2)
140    1  if (t1 > t2)
141 */
142 static int
143 frames_compare(gconstpointer a, gconstpointer b)
144 {
145     const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
146     const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
147
148     const nstime_t *time1 = &frame1->time;
149     const nstime_t *time2 = &frame2->time;
150
151     if (time1->secs > time2->secs)
152         return 1;
153     if (time1->secs < time2->secs)
154         return -1;
155
156     /* time1->secs == time2->secs */
157     if (time1->nsecs > time2->nsecs)
158         return 1;
159     if (time1->nsecs < time2->nsecs)
160         return -1;
161
162     /* time1->nsecs == time2->nsecs */
163
164     if (frame1->num > frame2->num)
165         return 1;
166     if (frame1->num < frame2->num)
167         return -1;
168     return 0;
169 }
170
171
172 /********************************************************************/
173 /* Main function.                                                   */
174 /********************************************************************/
175 int main(int argc, char *argv[])
176 {
177     wtap *wth = NULL;
178     wtap_dumper *pdh = NULL;
179     Buffer buf;
180     int err;
181     gchar *err_info;
182     gint64 data_offset;
183     const struct wtap_pkthdr *phdr;
184     guint wrong_order_count = 0;
185     gboolean write_output_regardless = TRUE;
186     guint i;
187     wtapng_section_t            *shb_hdr;
188     wtapng_iface_descriptions_t *idb_inf;
189
190     GPtrArray *frames;
191     FrameRecord_t *prevFrame = NULL;
192
193     int opt;
194     int file_count;
195     char *infile;
196     char *outfile;
197
198     /* Process the options first */
199     while ((opt = getopt(argc, argv, "hn")) != -1) {
200         switch (opt) {
201             case 'n':
202                 write_output_regardless = FALSE;
203                 break;
204             case 'h':
205                 usage(FALSE);
206                 exit(0);
207             case '?':
208                 usage(TRUE);
209                 exit(1);
210         }
211     }
212
213     /* Remaining args are file names */
214     file_count = argc - optind;
215     if (file_count == 2) {
216         infile  = argv[optind];
217         outfile = argv[optind+1];
218     }
219     else {
220         usage(TRUE);
221         exit(1);
222     }
223
224     init_open_routines();
225
226     /* Open infile */
227     wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
228     if (wth == NULL) {
229         fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
230                 wtap_strerror(err));
231         switch (err) {
232
233         case WTAP_ERR_UNSUPPORTED:
234         case WTAP_ERR_UNSUPPORTED_ENCAP:
235         case WTAP_ERR_BAD_FILE:
236             fprintf(stderr, "(%s)\n", err_info);
237             g_free(err_info);
238             break;
239         }
240         exit(1);
241     }
242     DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
243
244     shb_hdr = wtap_file_get_shb_info(wth);
245     idb_inf = wtap_file_get_idb_info(wth);
246
247     /* Open outfile (same filetype/encap as input file) */
248     pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
249                             65535, FALSE, shb_hdr, idb_inf, &err);
250     g_free(idb_inf);
251     if (pdh == NULL) {
252         fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
253                 outfile, wtap_strerror(err));
254         g_free(shb_hdr);
255         exit(1);
256     }
257
258     /* Allocate the array of frame pointers. */
259     frames = g_ptr_array_new();
260
261     /* Read each frame from infile */
262     while (wtap_read(wth, &err, &err_info, &data_offset)) {
263         FrameRecord_t *newFrameRecord;
264
265         phdr = wtap_phdr(wth);
266
267         newFrameRecord = g_slice_new(FrameRecord_t);
268         newFrameRecord->num = frames->len + 1;
269         newFrameRecord->offset = data_offset;
270         newFrameRecord->time = phdr->ts;
271
272         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
273            wrong_order_count++;
274         }
275
276         g_ptr_array_add(frames, newFrameRecord);
277         prevFrame = newFrameRecord;
278     }
279     if (err != 0) {
280       /* Print a message noting that the read failed somewhere along the line. */
281       fprintf(stderr,
282               "reordercap: An error occurred while reading \"%s\": %s.\n",
283               infile, wtap_strerror(err));
284       switch (err) {
285
286       case WTAP_ERR_UNSUPPORTED:
287       case WTAP_ERR_UNSUPPORTED_ENCAP:
288       case WTAP_ERR_BAD_FILE:
289           fprintf(stderr, "(%s)\n", err_info);
290           g_free(err_info);
291           break;
292       }
293     }
294
295     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
296
297     /* Sort the frames */
298     if (wrong_order_count > 0) {
299         g_ptr_array_sort(frames, frames_compare);
300     }
301
302     /* Write out each sorted frame in turn */
303     buffer_init(&buf, 1500);
304     for (i = 0; i < frames->len; i++) {
305         FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
306
307         /* Avoid writing if already sorted and configured to */
308         if (write_output_regardless || (wrong_order_count > 0)) {
309             frame_write(frame, wth, pdh, &buf, infile);
310         }
311         g_slice_free(FrameRecord_t, frame);
312     }
313     buffer_free(&buf);
314
315     if (!write_output_regardless && (wrong_order_count == 0)) {
316         printf("Not writing output file because input file is already in order!\n");
317     }
318
319     /* Free the whole array */
320     g_ptr_array_free(frames, TRUE);
321
322     /* Close outfile */
323     if (!wtap_dump_close(pdh, &err)) {
324         fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
325                 wtap_strerror(err));
326         g_free(shb_hdr);
327         exit(1);
328     }
329     g_free(shb_hdr);
330
331     /* Finally, close infile */
332     wtap_fdclose(wth);
333
334     return 0;
335 }
336
337 /*
338  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
339  *
340  * Local variables:
341  * c-basic-offset: 4
342  * tab-width: 8
343  * indent-tabs-mode: nil
344  * End:
345  *
346  * vi: set shiftwidth=4 tabstop=8 expandtab:
347  * :indentSize=4:tabSize=8:noTabs=true:
348  */