-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap-subtract-timestamp.c
59 lines (54 loc) · 1.31 KB
/
pcap-subtract-timestamp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pcap.h>
#include <err.h>
#include <string.h>
#include <time.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
pcap_t *in = NULL;
pcap_dumper_t *out = NULL;
char errbuf[PCAP_ERRBUF_SIZE + 1];
struct pcap_pkthdr hdr;
const u_char *data;
char ttime[32];
struct timeval adjust;
long long int L1;
long long int L2;
if (argc < 2) {
fprintf(stderr, "usage: pcap-adjust-timestamp adjust\n");
exit(1);
}
snprintf(ttime, 32, "%8.6f", atof(argv[1]));
if (2 != sscanf(ttime, "%lld.%lld", &L1, &L2)) {
fprintf(stderr, "bad adjust time: %s\n", argv[1]);
exit(1);
}
adjust.tv_sec = (time_t) L1;
adjust.tv_usec = (time_t) L2;
fprintf(stderr, "adjusting timestamps by %lld.%06lld sec\n",
(long long int) adjust.tv_sec, (long long int) adjust.tv_usec);
in = pcap_open_offline("-", errbuf);
if (NULL == in) {
fprintf(stderr, "stdin: %s", errbuf);
exit(1);
}
out = pcap_dump_open(in, "-");
if (NULL == out) {
perror("stdout");
exit(1);
}
while ((data = pcap_next(in, &hdr))) {
struct timeval result;
timersub(&hdr.ts, &adjust, &result);
hdr.ts = result;
pcap_dump((void *) out, &hdr, data);
}
pcap_close(in);
if (out)
pcap_dump_close(out);
exit(0);
}