1
-
2
-
3
1
#include <stdio.h>
4
2
#include <stdlib.h>
5
3
#include <string.h>
4
+ #include <time.h>
6
5
7
6
#include "capshift.h"
8
7
9
- #define SWVERSION "v0.2 alpha "
8
+ #define SWVERSION "v0.3 beta "
10
9
#define SWRELEASEDATE "February 2018"
11
10
12
11
// 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[]){
19
18
// Returns a struct with various parameters or NULL if invalid
20
19
unsigned int i = 1 ;
21
20
char * timestring = NULL ,
22
- * endptr = NULL ;
21
+ * endptr = NULL ,
22
+ * datestring = NULL ,
23
+ * offsetstring = NULL ;
23
24
params_t * parameters = (params_t * )malloc (sizeof (params_t ));
24
25
if (parameters == NULL ) return (NULL );
25
26
26
- // There must be 4 parameters
27
- if (argc != 7 ) return (NULL );
28
-
29
27
// Set some defaults
30
28
parameters -> infile = NULL ;
31
29
parameters -> outfile = NULL ;
30
+ parameters -> abs = 0 ;
31
+ parameters -> sign = ADD ;
32
+
32
33
33
34
// Look for the various flags, then store the corresponding value
34
35
while (i < argc ){
@@ -43,6 +44,16 @@ params_t *parseParams(int argc, char *argv[]){
43
44
continue ;
44
45
}
45
46
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 ){
46
57
timestring = argv [++ i ];
47
58
i ++ ;
48
59
continue ;
@@ -54,44 +65,82 @@ params_t *parseParams(int argc, char *argv[]){
54
65
// If the input files still aren't set, bomb
55
66
if ((parameters -> infile == NULL ) || (parameters -> outfile == NULL )) return (NULL );
56
67
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 );
70
127
}
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 );
71
141
72
- // If there are non-numeric characters present, bail out
73
- if ((timestring [0 ] < '0' ) || (timestring [0 ] > '9' )) return (NULL );
74
142
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 ;
93
143
94
- if (endptr [0 ] != '\x00' ) return (NULL );
95
144
96
145
return (parameters );
97
146
}
@@ -101,6 +150,7 @@ int parse_pcap(FILE *capfile, FILE *outfile, guint32 sign, guint32 secs, guint32
101
150
guint32 caplen = 0 ;
102
151
int count = 0 ;
103
152
pcaprec_hdr_t * rechdr = NULL ;
153
+ int first_timestamp_found = 0 ;
104
154
105
155
if (sign == ADD ) {
106
156
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
154
204
}
155
205
156
206
// 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
+ }
158
212
if (sign == SUBTRACT ){
159
213
rechdr -> ts_sec -= secs ;
160
214
if (usecs > rechdr -> ts_usec ){
@@ -201,6 +255,27 @@ int parse_pcap(FILE *capfile, FILE *outfile, guint32 sign, guint32 secs, guint32
201
255
return (count );
202
256
}
203
257
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
+
204
279
int main (int argc , char * argv []){
205
280
// The main function basically just calls other functions to do the work.
206
281
params_t * parameters = NULL ;
@@ -209,14 +284,28 @@ int main(int argc, char *argv[]){
209
284
210
285
// Parse our command line parameters and verify they are usable. If not, show help.
211
286
parameters = parseParams (argc , argv );
287
+
212
288
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" );
214
299
printf ("Version %s, %s\n\n" , SWVERSION , SWRELEASEDATE );
215
300
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 ]);
217
302
printf ("Where inputcapfile is a tcpdump-style .cap file\n" );
218
303
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" );
220
309
return (1 );
221
310
}
222
311
@@ -242,3 +331,4 @@ int main(int argc, char *argv[]){
242
331
}
243
332
244
333
334
+
0 commit comments