Skip to content

Commit 33e2d6a

Browse files
author
Niels Jakob Buch
committed
Adding first code to support absolute timeshifts.
1 parent 9ac0500 commit 33e2d6a

File tree

5 files changed

+156
-51
lines changed

5 files changed

+156
-51
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
out.pcap
2+
raw_mac_capture1.pcap
3+
raw_mac_capture2.pcap
4+
raw_mac_capture3.pcap

README

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
capshift v0.2 Alpha
1+
capshift v0.3 Beta
22
Original by Foeh Mannay, January 2015
33
Current version by Niels Jakob Buch, February 2018
44

@@ -9,11 +9,13 @@ PURPOSE
99
you have two pcap files taken from different devices whose clocks are not synchronised
1010
and you can't be bothered to repeatedly hand-correct the timestamps.
1111

12-
Please see http://networkingbodges.blogspot.com/ for more information on this if you
13-
are interested.
12+
Or, you are analysing network traffic for different purposes, and needs test-data that
13+
are matching specific time or dates.
1414

1515
INSTALLATION
1616
============
17+
The library has on purpose been built to follow POSIX standards and should be cross-platform
18+
compatible with no challenges.
1719

1820
For Linux / Mac / MinGW it should be possible to build from source using:
1921

@@ -24,17 +26,25 @@ USAGE
2426

2527
There are only three parameters and all are mandatory. You must specify your
2628
input capture file (original pcap format) with the -r flag, your output capture file
27-
with the -w flag and your time offset with the -o flag. Here's an example:
29+
with the -w flag and your time options with the -o, -t, -d or -t AND -d flag. Here's the four examples:
2830

2931
./capshift -r original.cap -w shifted.cap -o +14.5
32+
./capshift -r original.cap -w shifted.cap -t 20:03
33+
./capshift -r original.cap -w shifted.cap -d 21-12-2019
34+
./capshift -r original.cap -w shifted.cap -t 23:30 -d 20-7-2017
3035

31-
Parsing capfile, attempting to shift forward by 14.5s...
36+
The purpose of -t is to shift the time-stamps to another time, keeping the date.
3237

33-
45 frames processed.
38+
The purpose of the -d is to shift the time-stamps to another date, but keeping the time-of-day.
39+
40+
The purpose of using both -t and -d is the give a totally fresh time-stamp.
41+
42+
Please note that all pcap records will be time-stamped relatively to the first record, based on the existing timestamps.
3443

3544
CHANGE LOG
3645
==========
3746

3847
v0.1a First working release.
3948
v0.2a Bugs for larger timeshifts, and larger files fixed.
49+
v0.3b Adding absolute timeshifts
4050

capshift

184 Bytes
Binary file not shown.

capshift.c

+135-45
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
2-
31
#include <stdio.h>
42
#include <stdlib.h>
53
#include <string.h>
4+
#include <time.h>
65

76
#include "capshift.h"
87

9-
#define SWVERSION "v0.2 alpha"
8+
#define SWVERSION "v0.3 beta"
109
#define SWRELEASEDATE "February 2018"
1110

1211
// capshift (pCAP time SHIFT) shifts the timestamps in pcap files by the specified time
@@ -19,16 +18,18 @@ params_t *parseParams(int argc, char *argv[]){
1918
// Returns a struct with various parameters or NULL if invalid
2019
unsigned int i = 1;
2120
char *timestring = NULL,
22-
*endptr = NULL;
21+
*endptr = NULL,
22+
*datestring = NULL,
23+
*offsetstring = NULL;
2324
params_t *parameters = (params_t*)malloc(sizeof(params_t));
2425
if(parameters == NULL) return(NULL);
2526

26-
// There must be 4 parameters
27-
if(argc != 7) return(NULL);
28-
2927
// Set some defaults
3028
parameters->infile = NULL;
3129
parameters->outfile = NULL;
30+
parameters->abs = 0;
31+
parameters->sign = ADD;
32+
3233

3334
// Look for the various flags, then store the corresponding value
3435
while(i < argc){
@@ -43,6 +44,16 @@ params_t *parseParams(int argc, char *argv[]){
4344
continue;
4445
}
4546
if(strcmp(argv[i],"-o") == 0){
47+
offsetstring = argv[++i];
48+
i++;
49+
continue;
50+
}
51+
if(strcmp(argv[i],"-d") == 0){
52+
datestring = argv[++i];
53+
i++;
54+
continue;
55+
}
56+
if(strcmp(argv[i],"-t") == 0){
4657
timestring = argv[++i];
4758
i++;
4859
continue;
@@ -54,44 +65,82 @@ params_t *parseParams(int argc, char *argv[]){
5465
// If the input files still aren't set, bomb
5566
if((parameters->infile == NULL) || (parameters->outfile == NULL)) return(NULL);
5667

57-
// Try to parse the time offset string
58-
if(timestring == NULL) return NULL;
59-
60-
// If there is a + or - present, set the sign accordingly
61-
switch(timestring[0]){
62-
case '-':
63-
parameters->sign = SUBTRACT;
64-
timestring++;
65-
break;
66-
case '+':
67-
parameters->sign = ADD;
68-
timestring++;
69-
break;
68+
if ((datestring != NULL) && (timestring != NULL) && (offsetstring == NULL)) {
69+
// the case of exact time AND DATE, set parameters abs, secs, usecs and sign
70+
parameters->abs = 0; // Means absolate displacement
71+
72+
return(parameters);
73+
}
74+
75+
if ((datestring != NULL) && (timestring == NULL) && (offsetstring == NULL)) {
76+
// the case of exact date only (keep time-of-day), set parameters abs, secs, usecs and sign
77+
parameters->abs = 0; // Means absolute
78+
return(parameters);
79+
}
80+
81+
if ((datestring == NULL) && (timestring != NULL) && (offsetstring == NULL)) {
82+
// the case of exact time only, set parameters abs, secs, usecs and sign
83+
parameters->abs = 0; // Means absolute
84+
return(parameters);
85+
}
86+
87+
if ((datestring == NULL) && (timestring == NULL) && (offsetstring != NULL)) {
88+
printf("DEBUG: A relative offset is the case...%s\n", offsetstring);
89+
// the case of exact offset, set parameters abs, secs, usecs and sign
90+
parameters->abs = 1; // Means relative
91+
// If there is a + or - present, set the sign accordingly
92+
switch(offsetstring[0]){
93+
case '-':
94+
parameters->sign = SUBTRACT;
95+
offsetstring++;
96+
break;
97+
case '+':
98+
parameters->sign = ADD;
99+
offsetstring++;
100+
break;
101+
}
102+
// If there are non-numeric characters present, bail out
103+
if((offsetstring[0] < '0') || (offsetstring[0] > '9')) return(NULL);
104+
105+
// Grab the seconds
106+
parameters->secs = strtol(offsetstring, &endptr, 10);
107+
// Look for a decimal point, if present then grab and scale out microseconds
108+
if(endptr[0] == '.'){
109+
offsetstring = endptr + 1;
110+
parameters->usecs = strtol(offsetstring, &endptr, 10);
111+
112+
// scale the usecs field as appropriate for place value
113+
i = endptr - offsetstring;
114+
while(i < 6){
115+
parameters->usecs *= 10;
116+
i++;
117+
}
118+
while(i > 6){
119+
parameters->usecs /= 10;
120+
i--;
121+
}
122+
} else parameters->usecs = 0;
123+
124+
if(endptr[0] != '\x00') return(NULL);
125+
126+
return(parameters);
70127
}
128+
129+
char *token;
130+
token = strsep(&datestring, "-");
131+
int dd;
132+
dd = strtol(token,NULL,10);
133+
134+
token = strsep(&datestring, "-");
135+
int mm;
136+
mm = strtol(token,NULL,10);
137+
token = strsep(&datestring, "-");
138+
int yy;
139+
yy = strtol(token,NULL,10);
140+
printf("Dato er %d/%d/%d", dd, mm, yy);
71141

72-
// If there are non-numeric characters present, bail out
73-
if((timestring[0] < '0') || (timestring[0] > '9')) return(NULL);
74142

75-
// Grab the seconds
76-
parameters->secs = strtol(timestring, &endptr, 10);
77-
// Look for a decimal point, if present then grab and scale out microseconds
78-
if(endptr[0] == '.'){
79-
timestring = endptr + 1;
80-
parameters->usecs = strtol(timestring, &endptr, 10);
81-
82-
// scale the usecs field as appropriate for place value
83-
i = endptr - timestring;
84-
while(i < 6){
85-
parameters->usecs *= 10;
86-
i++;
87-
}
88-
while(i > 6){
89-
parameters->usecs /= 10;
90-
i--;
91-
}
92-
} else parameters->usecs = 0;
93143

94-
if(endptr[0] != '\x00') return(NULL);
95144

96145
return(parameters);
97146
}
@@ -101,6 +150,7 @@ int parse_pcap(FILE *capfile, FILE *outfile, guint32 sign, guint32 secs, guint32
101150
guint32 caplen = 0;
102151
int count = 0;
103152
pcaprec_hdr_t *rechdr = NULL;
153+
int first_timestamp_found = 0;
104154

105155
if(sign == ADD) {
106156
printf("\nParsing capfile, attempting to shift forward by %u.%u seconds...\n", secs, usecs);
@@ -154,7 +204,11 @@ int parse_pcap(FILE *capfile, FILE *outfile, guint32 sign, guint32 secs, guint32
154204
}
155205

156206
// Adjust timestamp as required, handling over/underflow
157-
207+
if (first_timestamp_found == 0) {
208+
printf("Nu er vi ved første RAW -> %d", (int)rechdr->ts_sec );
209+
printf("Nu er vi ved første since midnigt-> %d", (int)rechdr->ts_sec % 86400 );
210+
first_timestamp_found = 1;
211+
}
158212
if(sign == SUBTRACT){
159213
rechdr->ts_sec -= secs;
160214
if (usecs > rechdr->ts_usec){
@@ -201,6 +255,27 @@ int parse_pcap(FILE *capfile, FILE *outfile, guint32 sign, guint32 secs, guint32
201255
return(count);
202256
}
203257

258+
int findoffset() {
259+
260+
time_t curtime;
261+
time_t newtime;
262+
time(&curtime);
263+
/*
264+
struct tm *dayoffset;
265+
dayoffset = localtime(&curtime);
266+
dayoffset->tm_mday = 0;
267+
dayoffset->tm_mon = 0;
268+
dayoffset->tm_year = 0;
269+
dayoffset->tm_wday = 0;
270+
newtime = mktime(dayoffset); */
271+
time_t seconds_since_midnight = curtime % 86400;
272+
printf("Current time = %s", ctime(&curtime));
273+
printf("Current time = %d", (int)curtime);
274+
// printf("New time = %s", ctime(&newtime));
275+
printf("New time = %d", (int)seconds_since_midnight);
276+
return(0);
277+
}
278+
204279
int main(int argc, char *argv[]){
205280
// The main function basically just calls other functions to do the work.
206281
params_t *parameters = NULL;
@@ -209,14 +284,28 @@ int main(int argc, char *argv[]){
209284

210285
// Parse our command line parameters and verify they are usable. If not, show help.
211286
parameters = parseParams(argc, argv);
287+
212288
if(parameters == NULL){
213-
printf("capshift: a utility to adjust the timestamps of pcap files by a fixed offset.\n");
289+
printf("\n\n _ _ __ _ \n");
290+
printf(" | | (_)/ _| | \n");
291+
printf(" ___ __ _ _ __ ___| |__ _| |_| |_ \n");
292+
printf(" / __/ _` | '_ \\/ __| '_ \\| | _| __|\n");
293+
printf("| (_| (_| | |_) \\__ \\ | | | | | | |_ \n");
294+
printf(" \\___\\__,_| .__/|___/_| |_|_|_| \\__|\n");
295+
printf(" | | \n");
296+
printf(" |_| \n");
297+
printf("\ncapshift: a utility to adjust the timestamps of pcap files.\n");
298+
printf("Written by Niels Jakob Buch & Foeh Mannay.\n");
214299
printf("Version %s, %s\n\n", SWVERSION, SWRELEASEDATE);
215300
printf("Usage:\n");
216-
printf("%s -r inputcapfile -w outputcapfile -o offset \n\n",argv[0]);
301+
printf("%s -r inputcapfile -w outputcapfile [time option]\n\n",argv[0]);
217302
printf("Where inputcapfile is a tcpdump-style .cap file\n");
218303
printf("outputcapfile is the file where the time-shifted version will be saved\n");
219-
printf("offset is the number of seconds to shift by (e.g. -1.5, +0.200)\n");
304+
printf("[time option] is:\n");
305+
printf(" -o offset : offset is the number of seconds to shift by (e.g. -1.5, +0.200)\n");
306+
printf(" -d date : where date is the day shift to, keeping the time-of-day.\n");
307+
printf(" -t time : where time is the time-of-day to shift to, keeping the day.\n");
308+
printf(" -d date -t time : where date and time is the time AND day to shift to.\n\n\n");
220309
return(1);
221310
}
222311

@@ -242,3 +331,4 @@ int main(int argc, char *argv[]){
242331
}
243332

244333

334+

capshift.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct params_s {
2727
guint32 secs;
2828
guint32 usecs;
2929
guint32 sign;
30+
guint32 abs; /* indicate if its a relative or absolate displacement 0=abs 1=rel */
3031
} params_t;
3132

3233

0 commit comments

Comments
 (0)