forked from gawen947/wsn-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcap-write.c
101 lines (81 loc) · 3.11 KB
/
pcap-write.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* File: pcap.c
Copyright (C) 2013 David Hauweele <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <sys/time.h>
#include <stdbool.h>
#include <stdint.h>
#include <err.h>
#include "iobuf.h"
#include "pcap.h"
static iofile_t pcap;
#define WRITE(size) \
static void write ## size (uint ## size ## _t value) { \
ssize_t n = iobuf_write(pcap, &value, sizeof(value)); \
if(n != sizeof(value)) \
err(EXIT_FAILURE, "cannot write to pcap file"); \
}
WRITE(32)
WRITE(16)
void open_writing_pcap(const char *path)
{
/* TODO: Append to the file if it already exists.
Well we could do this but will have to take
care of endianness. We can also do a tool
to merge PCAP files (which I will do later). */
pcap = iobuf_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(!pcap)
err(EXIT_FAILURE, "cannot open pcap file");
write32(PCAP_MAGIC); /* magic number */
write16(PCAP_MAJOR); /* PCAP version */
write16(PCAP_MINOR);
write32(0); /* timezone in seconds (GMT) */
write32(0); /* accuracy of timestamps */
write32(0xff); /* max length of packets */
write32(LINKTYPE_IEEE802_15_4); /* data link type */
}
void pcap_append_frame(const unsigned char *frame, unsigned int size)
{
ssize_t n;
struct timeval tv;
/* If the pcap was not initialized we do nothing. */
if(!pcap)
return;
/* Sometime we are asked to write an empty frame.
This may be due to an error in the firmare which
sends empty frame. When such a case arise we just
ignore it. */
if(!size)
return;
/* Note that POSIX.1-2008 marks this function obsolete and it is recommended
to use clock_gettime() with nanoseconds timestamps instead. Since we just
want a microsecond timestamp and we don't care so much about time for now I
guess it would do perfectly well. */
gettimeofday(&tv, NULL);
write32(tv.tv_sec); /* timestamp seconds */
write32(tv.tv_usec); /* timestamp microseconds */
write32(size); /* number of octets of packet saved in file */
write32(size); /* actual length of packet */
n = iobuf_write(pcap, frame, size);
if(n != size)
err(EXIT_FAILURE, "cannot write to pcap file");
}
void pcap_write_flush(void)
{
if(pcap)
iobuf_flush(pcap);
}
void close_writing_pcap(void)
{
if(pcap)
iobuf_close(pcap);
}