From RD Thrush: Fix for 'editcap relies on gnu extension to strptime(3)'
[obnox/wireshark/wip.git] / editcap.c
index 4a033d351a9725f6e6dc45a6cdf342372529cf01..de86cf6d4d2d51a0644a05621e170b868c17b6d0 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -106,7 +106,8 @@ struct time_adjustment {
   int is_negative;
 };
 
-static struct select_item selectfrm[100];
+#define MAX_SELECTIONS 512
+static struct select_item selectfrm[MAX_SELECTIONS];
 static int max_selected = -1;
 static int keep_em = 0;
 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
@@ -120,14 +121,17 @@ static gboolean check_startstop = FALSE;
 static gboolean dup_detect = FALSE;
 
 /* Add a selection item, a simple parser for now */
-
-static void add_selection(char *sel)
+static gboolean
+add_selection(char *sel)
 {
   char *locn;
   char *next;
 
-  if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
-    return;
+  if (++max_selected >= MAX_SELECTIONS) {
+    /* Let the user know we stopped selecting */
+    printf("Out of room for packet selections!\n");
+    return(FALSE);
+  }
 
   printf("Add_Selected: %s\n", sel);
 
@@ -135,7 +139,6 @@ static void add_selection(char *sel)
 
     printf("Not inclusive ...");
 
-    max_selected++;
     selectfrm[max_selected].inclusive = 0;
     selectfrm[max_selected].first = atoi(sel);
 
@@ -147,7 +150,6 @@ static void add_selection(char *sel)
     printf("Inclusive ...");
 
     next = locn + 1;
-    max_selected++;
     selectfrm[max_selected].inclusive = 1;
     selectfrm[max_selected].first = atoi(sel);
     selectfrm[max_selected].second = atoi(next);
@@ -156,12 +158,13 @@ static void add_selection(char *sel)
 
   }
 
-
+  return(TRUE);
 }
 
 /* Was the packet selected? */
 
-static int selected(int recno)
+static int
+selected(int recno)
 {
   int i = 0;
 
@@ -182,9 +185,18 @@ static int selected(int recno)
 }
 
 /* is the packet in the selected timeframe */
-static gboolean check_timestamp(wtap *wth) {
-       struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
-       return ( (time_t) pkthdr->ts.secs >= starttime ) && ( (time_t) pkthdr->ts.secs <= stoptime );
+static gboolean
+check_timestamp(wtap *wth)
+{
+  static int i = 0;
+  struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
+
+  if (!((i++)%250))
+    printf("== %d starttime=%lu stoptime=%lu ts=%lu\n",i,
+           (unsigned long)starttime,
+          (unsigned long)stoptime,
+          (unsigned long)pkthdr->ts.secs);
+  return ( pkthdr->ts.secs >= starttime ) && ( pkthdr->ts.secs <= stoptime );
 }
 
 static void
@@ -297,19 +309,21 @@ static void usage(void)
   fprintf(stderr, "\n");
   fprintf(stderr, "A single packet or a range of packets can be selected.\n");
   fprintf(stderr, "\n");
-  fprintf(stderr, "Packets:\n");
-  fprintf(stderr, "  -C <choplen>           chop each packet at the end by <choplen> bytes\n");
-  fprintf(stderr, "  -d                     remove duplicate packets\n");
-  fprintf(stderr, "  -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
-  fprintf(stderr, "                         that a particular packet byte will be randomly changed\n");
+  fprintf(stderr, "Packet selection:\n");
   fprintf(stderr, "  -r                     keep the selected packets, default is to delete them\n");
-  fprintf(stderr, "  -s <snaplen>           truncate packets to max. <snaplen> bytes of data\n");
-  fprintf(stderr, "  -t <time adjustment>   adjust the timestamp of selected packets,\n");
-  fprintf(stderr, "                         <time adjustment> is in relative seconds (e.g. -0.5)\n");
   fprintf(stderr, "  -A <start time>        don't output packets whose timestamp is before the\n");
   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
   fprintf(stderr, "  -B <stop time>         don't output packets whose timestamp is after the\n");
   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
+  fprintf(stderr, "  -d                     remove duplicate packets\n");
+  fprintf(stderr, "\n");
+  fprintf(stderr, "Packet manipulation:\n");
+  fprintf(stderr, "  -s <snaplen>           truncate each packet to max. <snaplen> bytes of data\n");
+  fprintf(stderr, "  -C <choplen>           chop each packet at the end by <choplen> bytes\n");
+  fprintf(stderr, "  -t <time adjustment>   adjust the timestamp of each packet,\n");
+  fprintf(stderr, "                         <time adjustment> is in relative seconds (e.g. -0.5)\n");
+  fprintf(stderr, "  -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
+  fprintf(stderr, "                         that a particular packet byte will be randomly changed\n");
   fprintf(stderr, "\n");
   fprintf(stderr, "Output File(s):\n");
   fprintf(stderr, "  -c <packets per file>  split the packet output to different files,\n");
@@ -380,18 +394,20 @@ int main(int argc, char *argv[])
   int split_packet_count = 0;
   int written_count = 0;
   char *filename;
+  gboolean check_ts;
+#ifdef HAVE_PLUGINS
   char* init_progfile_dir_error;
-  
+
   /* Register wiretap plugins */
-    if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
-       g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
-       g_free(init_progfile_dir_error);
+  if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
+         g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
+         g_free(init_progfile_dir_error);
     } else {
-       init_report_err(failure_message,NULL,NULL);
-       init_plugins();
-       register_all_wiretap_modules();
+               init_report_err(failure_message,NULL,NULL);
+               init_plugins();
     }
-    
+#endif
+
   /* Process the options */
   while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
 
@@ -498,40 +514,41 @@ int main(int argc, char *argv[])
       verbose = !verbose;  /* Just invert */
       break;
 
-       case 'A':
-       {
-               struct tm starttm;
+    case 'A':
+    {
+      struct tm starttm;
 
-               memset(&starttm,0,sizeof(struct tm));
+      memset(&starttm,0,sizeof(struct tm));
 
-               if(!strptime(optarg,"%F %T",&starttm)) {
-                       fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
-                                       optarg);
-                       exit(1);
-               }
+      if(!strptime(optarg,"%Y-%m-%d %T",&starttm)) {
+        fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
+        exit(1);
+      }
 
-               check_startstop = TRUE;
-               starttm.tm_isdst = -1;
+      check_startstop = TRUE;
+      starttm.tm_isdst = -1;
 
-               starttime = mktime(&starttm);
-               break;
-       }
-       case 'B':
-       {
-               struct tm stoptm;
-
-               memset(&stoptm,0,sizeof(struct tm));
-
-               if(!strptime(optarg,"%F %T",&stoptm)) {
-                       fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
-                                       optarg);
-                       exit(1);
-               }
-               check_startstop = TRUE;
-               stoptm.tm_isdst = -1;
-               stoptime = mktime(&stoptm);
-               break;
-       }
+      starttime = mktime(&starttm);
+      printf("=START=> given='%s' stoptime=%lu\n",optarg,(unsigned long)starttime);
+      break;
+    }
+
+    case 'B':
+    {
+      struct tm stoptm;
+
+      memset(&stoptm,0,sizeof(struct tm));
+
+      if(!strptime(optarg,"%Y-%m-%d %T",&stoptm)) {
+        fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
+        exit(1);
+      }
+      check_startstop = TRUE;
+      stoptm.tm_isdst = -1;
+      stoptime = mktime(&stoptm);
+      printf("=STOP=> given='%s' stoptime=%lu\n",optarg,(unsigned long)stoptime);
+      break;
+    }
     }
 
   }
@@ -548,20 +565,22 @@ int main(int argc, char *argv[])
   }
 
   if (check_startstop && !stoptime) {
-         struct tm stoptm;
-         /* XXX: will work until 2035 */
-         memset(&stoptm,0,sizeof(struct tm));
-         stoptm.tm_year = 135;
-         stoptm.tm_mday = 31;
-         stoptm.tm_mon = 11;
-
-         stoptime = mktime(&stoptm);
+    struct tm stoptm;
+    /* XXX: will work until 2035 */
+    memset(&stoptm,0,sizeof(struct tm));
+    stoptm.tm_year = 135;
+    stoptm.tm_mday = 31;
+    stoptm.tm_mon = 11;
+
+    stoptime = mktime(&stoptm);
+    printf("=STOP=NEVER=> stoptime=%lu\n",(unsigned long)stoptime);
   }
 
   if (starttime > stoptime) {
-         fprintf(stderr, "editcap: start time is after the stop time\n");
-         exit(1);
+    fprintf(stderr, "editcap: start time is after the stop time\n");
+    exit(1);
   }
+  printf("==> stoptime=%lu stoptime=%lu\n",(unsigned long)starttime,(unsigned long)stoptime);
 
   wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
 
@@ -619,7 +638,8 @@ int main(int argc, char *argv[])
     }
 
     for (i = optind + 2; i < argc; i++)
-      add_selection(argv[i]);
+      if (add_selection(argv[i]) == FALSE)
+       break;
 
     while (wtap_read(wth, &err, &err_info, &data_offset)) {
 
@@ -648,7 +668,9 @@ int main(int argc, char *argv[])
        }
       }
 
-      if ( ((check_startstop && check_timestamp(wth)) || (!check_startstop && !check_timestamp(wth))) && ((!selected(count) && !keep_em) ||
+      check_ts = check_timestamp(wth);
+
+      if ( ((check_startstop && check_ts) || (!check_startstop && !check_ts)) && ((!selected(count) && !keep_em) ||
           (selected(count) && keep_em)) ) {
 
         if (verbose)