#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdarg.h>
/*
* Just make sure we include the prototype for strptime as well
- * (needed for glibc 2.2)
+ * (needed for glibc 2.2) but make sure we do this only if not
+ * yet defined.
*/
-#define __USE_XOPEN
+#ifndef __USE_XOPEN
+# define __USE_XOPEN
+#endif
+
#include <time.h>
#include <glib.h>
#endif
#include "epan/crypt/crypt-md5.h"
+#include "epan/plugins.h"
+#include "epan/report_err.h"
+#include "epan/filesystem.h"
#include "svnversion.h"
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" */
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);
printf("Not inclusive ...");
- max_selected++;
selectfrm[max_selected].inclusive = 0;
selectfrm[max_selected].first = atoi(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);
}
-
+ return(TRUE);
}
/* Was the packet selected? */
-static int selected(int recno)
+static int
+selected(int recno)
{
int i = 0;
}
/* 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
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");
}
}
+static void
+failure_message(const char *msg_format, va_list ap)
+{
+ fprintf(stderr, "editcap: ");
+ vfprintf(stderr, msg_format, ap);
+ fprintf(stderr, "\n");
+}
+
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);
+ } else {
+ init_report_err(failure_message,NULL,NULL);
+ init_plugins();
+ }
+#endif
- /* Process the options first */
-
+ /* Process the options */
while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
switch (opt) {
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;
+ }
}
}
}
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);
}
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)) {
}
}
- 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)