Do one or more of the following:
[metze/wireshark/wip.git] / reordercap.c
index 1e293039089544e9e80985f105a2d489df0de8aa..6b1cc95cb7a12b6a7bac1135bd3990dd029f2c69 100644 (file)
@@ -59,11 +59,11 @@ static void usage(void)
 
 /* Remember where this frame was in the file */
 typedef struct FrameRecord_t {
-    gint64               offset;
-    guint32              length;
-    guint                num;
+    gint64       offset;
+    guint32      length;
+    guint        num;
 
-    struct wtap_nstime   time;
+    nstime_t     time;
 } FrameRecord_t;
 
 
@@ -82,10 +82,11 @@ typedef struct FrameRecord_t {
 
 
 static void
-frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf)
+frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
+            const char *infile)
 {
     int    err;
-    gchar  *errinfo;
+    gchar  *err_info;
     struct wtap_pkthdr phdr;
 
     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n", 
@@ -93,14 +94,25 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf)
 
     
     /* Re-read the first frame from the stored location */
-    wtap_seek_read(wth,
-                   frame->offset,
-                   &phdr,
-                   buf,
-                   frame->length,
-                   &err,
-                   &errinfo);
-    DEBUG_PRINT("re-read: err is %d, buf is (%s)\n", err, buf);
+    if (!wtap_seek_read(wth, frame->offset, &phdr, buf, frame->length,
+                        &err, &err_info)) {
+        if (err != 0) {
+            /* Print a message noting that the read failed somewhere along the line. */
+            fprintf(stderr,
+                    "reordercap: An error occurred while re-reading \"%s\": %s.\n",
+                    infile, wtap_strerror(err));
+            switch (err) {
+
+            case WTAP_ERR_UNSUPPORTED:
+            case WTAP_ERR_UNSUPPORTED_ENCAP:
+            case WTAP_ERR_BAD_FILE:
+                fprintf(stderr, "(%s)\n", err_info);
+                g_free(err_info);
+                break;
+            }
+            exit(1);
+        }
+    }
 
     /* Copy, and set length and timestamp from item. */
     /* TODO: remove when wtap_seek_read() will read phdr */
@@ -125,8 +137,8 @@ frames_compare(gconstpointer a, gconstpointer b)
     const FrameRecord_t *frame1 = *(const FrameRecord_t **) a;
     const FrameRecord_t *frame2 = *(const FrameRecord_t **) b;
 
-    const struct wtap_nstime *time1 = &frame1->time;
-    const struct wtap_nstime *time2 = &frame2->time;
+    const nstime_t *time1 = &frame1->time;
+    const nstime_t *time2 = &frame2->time;
 
     if (time1->secs > time2->secs)
         return 1;
@@ -203,15 +215,24 @@ int main(int argc, char *argv[])
     if (wth == NULL) {
         fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
                 wtap_strerror(err));
+        switch (err) {
+
+        case WTAP_ERR_UNSUPPORTED:
+        case WTAP_ERR_UNSUPPORTED_ENCAP:
+        case WTAP_ERR_BAD_FILE:
+            fprintf(stderr, "(%s)\n", err_info);
+            g_free(err_info);
+            break;
+        }
         exit(1);
     }
-    DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth));
+    DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
 
     shb_hdr = wtap_file_get_shb_info(wth);
     idb_inf = wtap_file_get_idb_info(wth);
 
     /* Open outfile (same filetype/encap as input file) */
-    pdh = wtap_dump_open_ng(outfile, wtap_file_type(wth), wtap_file_encap(wth),
+    pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
                             65535, FALSE, shb_hdr, idb_inf, &err);
     g_free(idb_inf);
     if (pdh == NULL) {
@@ -243,6 +264,22 @@ int main(int argc, char *argv[])
         g_ptr_array_add(frames, newFrameRecord);
         prevFrame = newFrameRecord;
     }
+    if (err != 0) {
+      /* Print a message noting that the read failed somewhere along the line. */
+      fprintf(stderr,
+              "reordercap: An error occurred while reading \"%s\": %s.\n",
+              infile, wtap_strerror(err));
+      switch (err) {
+
+      case WTAP_ERR_UNSUPPORTED:
+      case WTAP_ERR_UNSUPPORTED_ENCAP:
+      case WTAP_ERR_BAD_FILE:
+          fprintf(stderr, "(%s)\n", err_info);
+          g_free(err_info);
+          break;
+      }
+    }
+
     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
 
     /* Sort the frames */
@@ -257,7 +294,7 @@ int main(int argc, char *argv[])
 
         /* Avoid writing if already sorted and configured to */
         if (write_output_regardless || (wrong_order_count > 0)) {
-            frame_write(frame, wth, pdh, &buf);
+            frame_write(frame, wth, pdh, &buf, infile);
         }
         g_slice_free(FrameRecord_t, frame);
     }