#include "epan/plugins.h"
#include "epan/report_err.h"
#include "epan/filesystem.h"
+#include <epan/privileges.h>
#include "svnversion.h"
static gboolean check_startstop = FALSE;
static gboolean dup_detect = FALSE;
+static int find_dct2000_real_data(guint8 *buf);
+
/* Add a selection item, a simple parser for now */
static gboolean
add_selection(char *sel)
if (selectfrm[i].inclusive) {
if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
- return 1;
+ return 1;
}
else {
if (recno == selectfrm[i].first)
- return 1;
+ return 1;
}
}
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 );
}
return FALSE;
}
-static void usage(void)
+static void
+usage(void)
{
fprintf(stderr, "Editcap %s"
#ifdef SVNVERSION
fprintf(stderr, "\n");
}
-static void list_capture_types(void) {
+static void
+list_capture_types(void) {
int i;
fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
}
}
-static void list_encap_types(void) {
+static void
+list_encap_types(void) {
int i;
const char *string;
}
}
+/*
+ * Don't report failures to load plugins because most (non-wiretap) plugins
+ * *should* fail to load (because we're not linked against libwireshark and
+ * dissector plugins need libwireshark).
+ */
static void
-failure_message(const char *msg_format, va_list ap)
+failure_message(const char *msg_format _U_, va_list ap _U_)
{
- fprintf(stderr, "editcap: ");
- vfprintf(stderr, msg_format, ap);
- fprintf(stderr, "\n");
+ return;
}
-int main(int argc, char *argv[])
-
+int
+main(int argc, char *argv[])
{
wtap *wth;
int i, j, err;
int split_packet_count = 0;
int written_count = 0;
char *filename;
+ size_t filenamelen = 0;
gboolean check_ts;
#ifdef HAVE_PLUGINS
char* init_progfile_dir_error;
+#endif
+
+ /*
+ * Get credential information for later use.
+ */
+ get_credential_info();
+#ifdef HAVE_PLUGINS
/* 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();
- }
+ 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 */
case 'E':
err_prob = strtod(optarg, &p);
if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
- fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
- optarg);
- exit(1);
+ fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
+ optarg);
+ exit(1);
}
srand( (unsigned int) (time(NULL) + getpid()) );
break;
case 'F':
out_file_type = wtap_short_string_to_file_type(optarg);
if (out_file_type < 0) {
- fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
- optarg);
+ fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
+ optarg);
list_capture_types();
- exit(1);
+ exit(1);
}
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);
+ 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);
+ fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
+ split_packet_count);
+ exit(1);
}
break;
case 'C':
choplen = strtol(optarg, &p, 10);
if (p == optarg || *p != '\0') {
- fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
- optarg);
- exit(1);
+ fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
+ optarg);
+ exit(1);
}
break;
case 'd':
dup_detect = TRUE;
for (i = 0; i < DUP_DEPTH; i++) {
- memset(&fd_hash[i].digest, 0, 16);
- fd_hash[i].len = 0;
+ memset(&fd_hash[i].digest, 0, 16);
+ fd_hash[i].len = 0;
}
break;
case 's':
snaplen = strtol(optarg, &p, 10);
if (p == optarg || *p != '\0') {
- fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
- optarg);
- exit(1);
+ fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
+ optarg);
+ exit(1);
}
break;
starttm.tm_isdst = -1;
starttime = mktime(&starttm);
- printf("=START=> given='%s' stoptime=%lu\n",optarg,(unsigned long)starttime);
break;
}
check_startstop = TRUE;
stoptm.tm_isdst = -1;
stoptime = mktime(&stoptm);
- printf("=STOP=> given='%s' stoptime=%lu\n",optarg,(unsigned long)stoptime);
break;
}
}
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);
}
- printf("==> stoptime=%lu stoptime=%lu\n",(unsigned long)starttime,(unsigned long)stoptime);
wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
}
if (verbose) {
-
fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
- wtap_file_type_string(wtap_file_type(wth)));
-
+ wtap_file_type_string(wtap_file_type(wth)));
}
/*
out_frame_type = wtap_file_encap(wth);
if (split_packet_count > 0) {
- filename = (char *) g_malloc(strlen(argv[optind+1]) + 20);
+ filenamelen = strlen(argv[optind+1]) + 20;
+ filename = (char *) g_malloc(filenamelen);
if (!filename) {
exit(5);
}
- sprintf(filename, "%s-%05d", argv[optind+1], 0);
+ g_snprintf(filename, filenamelen, "%s-%05d", argv[optind+1], 0);
} else {
filename = argv[optind+1];
}
if (pdh == NULL) {
fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
- wtap_strerror(err));
+ wtap_strerror(err));
exit(1);
}
exit(1);
}
- sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
+ g_snprintf(filename, filenamelen, "%s-%05d",argv[optind+1], count / split_packet_count);
if (verbose) {
fprintf(stderr, "Continuing writing in file %s\n", filename);
/* Random error mutation */
if (err_prob > 0.0) {
+ int real_data_start = 0;
buf = wtap_buf_ptr(wth);
- for (i = 0; i < (int) phdr->caplen; i++) {
+ /* Protect non-protocol data */
+ if (wtap_file_type(wth) == WTAP_FILE_CATAPULT_DCT2000) {
+ real_data_start = find_dct2000_real_data(buf);
+ }
+ for (i = real_data_start; i < (int) phdr->caplen; i++) {
if (rand() <= err_prob * RAND_MAX) {
err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
&err)) {
-
fprintf(stderr, "editcap: Error writing to %s: %s\n",
filename, wtap_strerror(err));
exit(1);
-
}
-
written_count++;
-
}
-
count++;
-
}
if (err != 0) {
/* Print a message noting that the read failed somewhere along the line. */
fprintf(stderr,
"editcap: An error occurred while reading \"%s\": %s.\n",
- argv[optind], wtap_strerror(err));
+ argv[optind], wtap_strerror(err));
switch (err) {
case WTAP_ERR_UNSUPPORTED:
if (!wtap_dump_close(pdh, &err)) {
fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
- wtap_strerror(err));
+ wtap_strerror(err));
exit(1);
}
return 0;
}
+/* Skip meta-information read from file to return offset of real
+ protocol data */
+static int find_dct2000_real_data(guint8 *buf)
+{
+ int n=0;
+
+ for (n=0; buf[n] != '\0'; n++); /* Context name */
+ n++;
+ n++; /* Context port number */
+ for (; buf[n] != '\0'; n++); /* Timestamp */
+ n++;
+ for (; buf[n] != '\0'; n++); /* Protocol name */
+ n++;
+ for (; buf[n] != '\0'; n++); /* Variant number (as string) */
+ n++;
+ for (; buf[n] != '\0'; n++); /* Outhdr (as string) */
+ n++;
+ n += 2; /* Direction & encap */
+
+ return n;
+}