Ethereal-dev: [Ethereal-dev] Response to bug 379 [PATCH]

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Jaap Keuter <jaap.keuter@xxxxxxxxx>
Date: Fri, 16 Dec 2005 14:07:19 +0100 (CET)
Hi list,

Mikko Tiihonen filed bug 379 including a patch for editcap. This wasn't
picked up so far. I've ported the patch to svn 16820 and included a
documentation patch.

Thanx,
Jaap
Index: editcap.c
===================================================================
--- editcap.c	(revision 16820)
+++ editcap.c	(working copy)
@@ -212,8 +212,10 @@
 
   fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-E <probability>]\n");
   fprintf(stderr, "               [-F <capture type>] [-s <snaplen>] [-t <time adjustment>]\n");
+  fprintf(stderr, "               [-c <packets per file>]\n");
   fprintf(stderr, "               <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
   fprintf(stderr, "  where\n");
+  fprintf(stderr, "       \t-c <packets per file> If given splits the output to different files\n");
   fprintf(stderr, "       \t-E <probability> specifies the probability (between 0 and 1)\n");
   fprintf(stderr, "       \t    that a particular byte will will have an error.\n");
   fprintf(stderr, "       \t-F <capture type> specifies the capture file type to write:\n");
@@ -260,10 +262,13 @@
   const struct wtap_pkthdr *phdr;
   int err_type;
   guint8 *buf;
+  int split_packet_count = 0;
+  int written_count = 0;
+  char *filename;
 
   /* Process the options first */
 
-  while ((opt = getopt(argc, argv, "E:F:hrs:t:T:v")) !=-1) {
+  while ((opt = getopt(argc, argv, "E:F:hrs:c:t:T:v")) !=-1) {
 
     switch (opt) {
 
@@ -286,6 +291,20 @@
       }
       break;
 
+    case 'c':
+      split_packet_count = strtol(optarg, &p, 10);
+      if (p == optarg || *p != '\0') {
+      	fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
+      	    optarg);
+      	exit(1);
+      }
+      if (split_packet_count <= 0) {
+      	fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
+		split_packet_count);
+      	exit(1);
+      }
+      break;
+
     case 'h':
     case '?':              /* Bad options if GNU getopt */
       usage();
@@ -372,11 +391,21 @@
     if (out_frame_type == -2)
       out_frame_type = wtap_file_encap(wth);
 
-    pdh = wtap_dump_open(argv[optind + 1], out_file_type,
+    if (split_packet_count > 0) {
+      filename = (char *) malloc(strlen(argv[optind+1]) + 20);
+      if (!filename) {
+	exit(5);
+      }
+      sprintf(filename, "%s-%05d", argv[optind+1], 0);
+    } else {
+      filename = argv[optind+1];
+    }
+  
+    pdh = wtap_dump_open(filename, out_file_type,
 			 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
     if (pdh == NULL) {
 
-      fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
+      fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
 	      wtap_strerror(err));
       exit(1);
 
@@ -387,6 +416,31 @@
 
     while (wtap_read(wth, &err, &err_info, &data_offset)) {
 
+      if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
+	if (!wtap_dump_close(pdh, &err)) {
+
+	  fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
+		  wtap_strerror(err));
+	  exit(1);
+	}
+
+	sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
+
+	if (verbose) {
+	  fprintf(stderr, "Continuing writing in file %s\n", filename);
+	}
+
+	pdh = wtap_dump_open(filename, out_file_type,
+			     out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
+	if (pdh == NULL) {
+	  
+	  fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
+		  wtap_strerror(err));
+	  exit(1);
+	  
+	}
+      }
+
       if ((!selected(count) && !keep_em) ||
           (selected(count) && keep_em)) {
 
@@ -486,11 +540,13 @@
                        &err)) {
 
           fprintf(stderr, "editcap: Error writing to %s: %s\n",
-                  argv[optind + 1], wtap_strerror(err));
+                  filename, wtap_strerror(err));
           exit(1);
 
 	}
 
+	written_count++;
+
       }
 
       count++;
@@ -514,7 +570,7 @@
 
     if (!wtap_dump_close(pdh, &err)) {
 
-      fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[optind + 1],
+      fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
 	      wtap_strerror(err));
       exit(1);
 
Index: editcap.pod
===================================================================
--- editcap.pod	(revision 16820)
+++ editcap.pod	(working copy)
@@ -6,6 +6,7 @@
 =head1 SYNOPSYS
 
 B<editcap>
+S<[ B<-c> packets per file]>
 S<[ B<-E> error probability]>
 S<[ B<-F> file format ]>
 S<[ B<-h> ]>
@@ -135,6 +136,11 @@
 I<end> (removing them all if B<-r> isn't specified, including them all
 if B<-r> is specified).
 
+If the B<-c> flag is used to specify the amount of packets in a capture
+file, the output file will be created with a suffix -nnnnn. The suffix
+starts at 00000. No more then the specified number of packets are written
+in the output file before the next output file is opened. 
+
 If the B<-s> flag is used to specify a snapshot length, frames in the
 input file with more captured data than the specified snapshot length
 will have only the amount of data specified by the snapshot length
@@ -175,6 +181,10 @@
 
 =over 4
 
+=item -c
+
+Sets the number of packets per output file.
+
 =item -E
 
 Sets the probabilty that bytes in the output file are randomly changed.