Do the full string in get_{compiled,runtime}_version_info().
[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  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <glib.h>
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #ifdef HAVE_GETOPT_H
36 #include <getopt.h>
37 #endif
38
39 #ifdef HAVE_LIBZ
40 #include <zlib.h>      /* to get the libz version number */
41 #endif
42
43 #include "wtap.h"
44
45 #ifndef HAVE_GETOPT_LONG
46 #include "wsutil/wsgetopt.h"
47 #endif
48
49 #include <wsutil/file_util.h>
50 #include <wsutil/crash_info.h>
51 #include <wsutil/ws_version_info.h>
52
53 /* Show command-line usage */
54 static void
55 print_usage(FILE *output)
56 {
57     fprintf(output, "\n");
58     fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
59     fprintf(output, "\n");
60     fprintf(output, "Options:\n");
61     fprintf(output, "  -n        don't write to output file if the input file is ordered.\n");
62     fprintf(output, "  -h        display this help and exit.\n");
63 }
64
65 /* Remember where this frame was in the file */
66 typedef struct FrameRecord_t {
67     gint64       offset;
68     guint        num;
69
70     nstime_t     time;
71 } FrameRecord_t;
72
73
74 /**************************************************/
75 /* Debugging only                                 */
76
77 /* Enable this symbol to see debug output */
78 /* #define REORDER_DEBUG */
79
80 #ifdef REORDER_DEBUG
81 #define DEBUG_PRINT printf
82 #else
83 #define DEBUG_PRINT(...)
84 #endif
85 /**************************************************/
86
87
88 static void
89 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
90             struct wtap_pkthdr *phdr, Buffer *buf, const char *infile)
91 {
92     int    err;
93     gchar  *err_info;
94
95     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
96                 frame->offset);
97
98
99     /* Re-read the frame from the stored location */
100     if (!wtap_seek_read(wth, frame->offset, phdr, buf, &err, &err_info)) {
101         if (err != 0) {
102             /* Print a message noting that the read failed somewhere along the line. */
103             fprintf(stderr,
104                     "reordercap: An error occurred while re-reading \"%s\": %s.\n",
105                     infile, wtap_strerror(err));
106             if (err_info != NULL) {
107                 fprintf(stderr, "(%s)\n", err_info);
108                 g_free(err_info);
109             }
110             exit(1);
111         }
112     }
113
114     /* Copy, and set length and timestamp from item. */
115     /* TODO: remove when wtap_seek_read() fills in phdr,
116        including time stamps, for all file types  */
117     phdr->ts = frame->time;
118
119     /* Dump frame to outfile */
120     if (!wtap_dump(pdh, phdr, ws_buffer_start_ptr(buf), &err, &err_info)) {
121         fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
122                 wtap_strerror(err));
123         if (err_info != NULL) {
124             fprintf(stderr, "(%s)\n", err_info);
125             g_free(err_info);
126         }
127         exit(1);
128     }
129 }
130
131 /* Comparing timestamps between 2 frames.
132    negative if (t1 < t2)
133    zero     if (t1 == t2)
134    positive if (t1 > t2)
135 */
136 static int
137 frames_compare(gconstpointer a, gconstpointer b)
138 {
139     const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
140     const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
141
142     const nstime_t *time1 = &frame1->time;
143     const nstime_t *time2 = &frame2->time;
144
145     return nstime_cmp(time1, time2);
146 }
147
148 static void
149 get_reordercap_compiled_info(GString *str)
150 {
151     /* LIBZ */
152     g_string_append(str, ", ");
153 #ifdef HAVE_LIBZ
154     g_string_append(str, "with libz ");
155 #ifdef ZLIB_VERSION
156     g_string_append(str, ZLIB_VERSION);
157 #else /* ZLIB_VERSION */
158     g_string_append(str, "(version unknown)");
159 #endif /* ZLIB_VERSION */
160 #else /* HAVE_LIBZ */
161     g_string_append(str, "without libz");
162 #endif /* HAVE_LIBZ */
163 }
164
165 static void
166 get_reordercap_runtime_info(GString *str)
167 {
168     /* zlib */
169 #if defined(HAVE_LIBZ) && !defined(_WIN32)
170     g_string_append_printf(str, ", with libz %s", zlibVersion());
171 #endif
172 }
173
174 /********************************************************************/
175 /* Main function.                                                   */
176 /********************************************************************/
177 int
178 main(int argc, char *argv[])
179 {
180     GString *comp_info_str;
181     GString *runtime_info_str;
182     wtap *wth = NULL;
183     wtap_dumper *pdh = NULL;
184     struct wtap_pkthdr dump_phdr;
185     Buffer buf;
186     int err;
187     gchar *err_info;
188     gint64 data_offset;
189     const struct wtap_pkthdr *phdr;
190     guint wrong_order_count = 0;
191     gboolean write_output_regardless = TRUE;
192     guint i;
193     wtapng_section_t            *shb_hdr;
194     wtapng_iface_descriptions_t *idb_inf;
195
196     GPtrArray *frames;
197     FrameRecord_t *prevFrame = NULL;
198
199     int opt;
200     static const struct option long_options[] = {
201         {(char *)"help", no_argument, NULL, 'h'},
202         {(char *)"version", no_argument, NULL, 'v'},
203         {0, 0, 0, 0 }
204     };
205     int file_count;
206     char *infile;
207     char *outfile;
208
209     /* Get the compile-time version information string */
210     comp_info_str = get_compiled_version_info(NULL, get_reordercap_compiled_info);
211
212     /* Get the run-time version information string */
213     runtime_info_str = get_runtime_version_info(get_reordercap_runtime_info);
214
215     /* Add it to the information to be reported on a crash. */
216     ws_add_crash_info("Reordercap (Wireshark) %s\n"
217          "\n"
218          "%s"
219          "\n"
220          "%s",
221       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
222
223     /* Process the options first */
224     while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) {
225         switch (opt) {
226             case 'n':
227                 write_output_regardless = FALSE;
228                 break;
229             case 'h':
230                 printf("Reordercap (Wireshark) %s\n"
231                        "Reorder timestamps of input file frames into output file.\n"
232                        "See http://www.wireshark.org for more information.\n",
233                        get_ws_vcs_version_info());
234                 print_usage(stdout);
235                 exit(0);
236             case 'v':
237                 show_version("Reordercap (Wireshark)", comp_info_str, runtime_info_str);
238                 g_string_free(comp_info_str, TRUE);
239                 g_string_free(runtime_info_str, TRUE);
240                 exit(0);
241             case '?':
242                 print_usage(stderr);
243                 exit(1);
244         }
245     }
246
247     /* Remaining args are file names */
248     file_count = argc - optind;
249     if (file_count == 2) {
250         infile  = argv[optind];
251         outfile = argv[optind+1];
252     }
253     else {
254         print_usage(stderr);
255         exit(1);
256     }
257
258     /* Open infile */
259     /* TODO: if reordercap is ever changed to give the user a choice of which
260        open_routine reader to use, then the following needs to change. */
261     wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
262     if (wth == NULL) {
263         fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
264                 wtap_strerror(err));
265         if (err_info != NULL) {
266             fprintf(stderr, "(%s)\n", err_info);
267             g_free(err_info);
268         }
269         exit(1);
270     }
271     DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
272
273     shb_hdr = wtap_file_get_shb_info(wth);
274     idb_inf = wtap_file_get_idb_info(wth);
275
276     /* Open outfile (same filetype/encap as input file) */
277     pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
278                             65535, FALSE, shb_hdr, idb_inf, &err);
279     g_free(idb_inf);
280     if (pdh == NULL) {
281         fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
282                 outfile, wtap_strerror(err));
283         g_free(shb_hdr);
284         exit(1);
285     }
286
287     /* Allocate the array of frame pointers. */
288     frames = g_ptr_array_new();
289
290     /* Read each frame from infile */
291     while (wtap_read(wth, &err, &err_info, &data_offset)) {
292         FrameRecord_t *newFrameRecord;
293
294         phdr = wtap_phdr(wth);
295
296         newFrameRecord = g_slice_new(FrameRecord_t);
297         newFrameRecord->num = frames->len + 1;
298         newFrameRecord->offset = data_offset;
299         if (phdr->presence_flags & WTAP_HAS_TS) {
300             newFrameRecord->time = phdr->ts;
301         } else {
302             nstime_set_unset(&newFrameRecord->time);
303         }
304
305         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
306            wrong_order_count++;
307         }
308
309         g_ptr_array_add(frames, newFrameRecord);
310         prevFrame = newFrameRecord;
311     }
312     if (err != 0) {
313       /* Print a message noting that the read failed somewhere along the line. */
314       fprintf(stderr,
315               "reordercap: An error occurred while reading \"%s\": %s.\n",
316               infile, wtap_strerror(err));
317       if (err_info != NULL) {
318           fprintf(stderr, "(%s)\n", err_info);
319           g_free(err_info);
320       }
321     }
322
323     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
324
325     /* Sort the frames */
326     if (wrong_order_count > 0) {
327         g_ptr_array_sort(frames, frames_compare);
328     }
329
330     /* Write out each sorted frame in turn */
331     wtap_phdr_init(&dump_phdr);
332     ws_buffer_init(&buf, 1500);
333     for (i = 0; i < frames->len; i++) {
334         FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
335
336         /* Avoid writing if already sorted and configured to */
337         if (write_output_regardless || (wrong_order_count > 0)) {
338             frame_write(frame, wth, pdh, &dump_phdr, &buf, infile);
339         }
340         g_slice_free(FrameRecord_t, frame);
341     }
342     wtap_phdr_cleanup(&dump_phdr);
343     ws_buffer_free(&buf);
344
345     if (!write_output_regardless && (wrong_order_count == 0)) {
346         printf("Not writing output file because input file is already in order!\n");
347     }
348
349     /* Free the whole array */
350     g_ptr_array_free(frames, TRUE);
351
352     /* Close outfile */
353     if (!wtap_dump_close(pdh, &err)) {
354         fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
355                 wtap_strerror(err));
356         g_free(shb_hdr);
357         exit(1);
358     }
359     g_free(shb_hdr);
360
361     /* Finally, close infile */
362     wtap_fdclose(wth);
363
364     return 0;
365 }
366
367 /*
368  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
369  *
370  * Local variables:
371  * c-basic-offset: 4
372  * tab-width: 8
373  * indent-tabs-mode: nil
374  * End:
375  *
376  * vi: set shiftwidth=4 tabstop=8 expandtab:
377  * :indentSize=4:tabSize=8:noTabs=true:
378  */