pcap-file: Add timestamp support for reading and writing pcap files.
[cascardo/ovs.git] / lib / pcap-file.c
1 /*
2  * Copyright (c) 2009, 2010, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "pcap-file.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include "byte-order.h"
25 #include "compiler.h"
26 #include "ofpbuf.h"
27 #include "timeval.h"
28 #include "vlog.h"
29
30 VLOG_DEFINE_THIS_MODULE(pcap);
31
32 struct pcap_hdr {
33     uint32_t magic_number;   /* magic number */
34     uint16_t version_major;  /* major version number */
35     uint16_t version_minor;  /* minor version number */
36     int32_t thiszone;        /* GMT to local correction */
37     uint32_t sigfigs;        /* accuracy of timestamps */
38     uint32_t snaplen;        /* max length of captured packets */
39     uint32_t network;        /* data link type */
40 };
41 BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
42
43 struct pcaprec_hdr {
44     uint32_t ts_sec;         /* timestamp seconds */
45     uint32_t ts_usec;        /* timestamp microseconds */
46     uint32_t incl_len;       /* number of octets of packet saved in file */
47     uint32_t orig_len;       /* actual length of packet */
48 };
49 BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
50
51 FILE *
52 pcap_open(const char *file_name, const char *mode)
53 {
54     struct stat s;
55     FILE *file;
56     int error;
57
58     ovs_assert(!strcmp(mode, "rb") ||
59                !strcmp(mode, "wb") ||
60                !strcmp(mode, "ab"));
61
62     file = fopen(file_name, mode);
63     if (file == NULL) {
64         VLOG_WARN("%s: failed to open pcap file for %s (%s)", file_name,
65                   (mode[0] == 'r' ? "reading"
66                    : mode[0] == 'w' ? "writing"
67                    : "appending"),
68                   ovs_strerror(errno));
69         return NULL;
70     }
71
72     switch (mode[0]) {
73     case 'r':
74         error = pcap_read_header(file);
75         if (error) {
76             errno = error;
77             fclose(file);
78             return NULL;
79         }
80         break;
81
82     case 'w':
83         pcap_write_header(file);
84         break;
85
86     case 'a':
87         if (!fstat(fileno(file), &s) && !s.st_size) {
88             pcap_write_header(file);
89         }
90         break;
91
92     default:
93         OVS_NOT_REACHED();
94     }
95     return file;
96 }
97
98 int
99 pcap_read_header(FILE *file)
100 {
101     struct pcap_hdr ph;
102     if (fread(&ph, sizeof ph, 1, file) != 1) {
103         int error = ferror(file) ? errno : EOF;
104         VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
105         return error;
106     }
107     if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
108         VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
109                   "(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number);
110         return EPROTO;
111     }
112     return 0;
113 }
114
115 void
116 pcap_write_header(FILE *file)
117 {
118     /* The pcap reader is responsible for figuring out endianness based on the
119      * magic number, so the lack of htonX calls here is intentional. */
120     struct pcap_hdr ph;
121     ph.magic_number = 0xa1b2c3d4;
122     ph.version_major = 2;
123     ph.version_minor = 4;
124     ph.thiszone = 0;
125     ph.sigfigs = 0;
126     ph.snaplen = 1518;
127     ph.network = 1;             /* Ethernet */
128     ignore(fwrite(&ph, sizeof ph, 1, file));
129 }
130
131 int
132 pcap_read(FILE *file, struct ofpbuf **bufp, long long int *when)
133 {
134     struct pcaprec_hdr prh;
135     struct ofpbuf *buf;
136     void *data;
137     size_t len;
138     bool swap;
139
140     *bufp = NULL;
141
142     /* Read header. */
143     if (fread(&prh, sizeof prh, 1, file) != 1) {
144         if (ferror(file)) {
145             int error = errno;
146             VLOG_WARN("failed to read pcap record header: %s",
147                       ovs_retval_to_string(error));
148             return error;
149         } else {
150             return EOF;
151         }
152     }
153
154     /* Calculate length. */
155     len = prh.incl_len;
156     swap = len > 0xffff;
157     if (swap) {
158         len = uint32_byteswap(len);
159         if (len > 0xffff) {
160             VLOG_WARN("bad packet length %"PRIuSIZE" or %"PRIu32
161                       "reading pcap file",
162                       len, uint32_byteswap(len));
163             return EPROTO;
164         }
165     }
166
167     /* Calculate time. */
168     if (when) {
169         uint32_t ts_sec = swap ? uint32_byteswap(prh.ts_sec) : prh.ts_sec;
170         uint32_t ts_usec = swap ? uint32_byteswap(prh.ts_usec) : prh.ts_usec;
171         *when = ts_sec * 1000LL + ts_usec / 1000;
172     }
173
174     /* Read packet. */
175     buf = ofpbuf_new(len);
176     data = ofpbuf_put_uninit(buf, len);
177     if (fread(data, len, 1, file) != 1) {
178         int error = ferror(file) ? errno : EOF;
179         VLOG_WARN("failed to read pcap packet: %s",
180                   ovs_retval_to_string(error));
181         ofpbuf_delete(buf);
182         return error;
183     }
184     *bufp = buf;
185     return 0;
186 }
187
188 void
189 pcap_write(FILE *file, struct ofpbuf *buf)
190 {
191     struct pcaprec_hdr prh;
192     struct timeval tv;
193
194     xgettimeofday(&tv);
195     prh.ts_sec = tv.tv_sec;
196     prh.ts_usec = tv.tv_usec;
197     prh.incl_len = buf->size;
198     prh.orig_len = buf->size;
199     ignore(fwrite(&prh, sizeof prh, 1, file));
200     ignore(fwrite(buf->data, buf->size, 1, file));
201 }