2 * Copyright (c) 2009, 2010, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "pcap-file.h"
24 #include "byte-order.h"
30 VLOG_DEFINE_THIS_MODULE(pcap);
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 */
41 BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
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 */
49 BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
52 pcap_open(const char *file_name, const char *mode)
58 ovs_assert(!strcmp(mode, "rb") ||
59 !strcmp(mode, "wb") ||
62 file = fopen(file_name, mode);
64 VLOG_WARN("%s: failed to open pcap file for %s (%s)", file_name,
65 (mode[0] == 'r' ? "reading"
66 : mode[0] == 'w' ? "writing"
74 error = pcap_read_header(file);
83 pcap_write_header(file);
87 if (!fstat(fileno(file), &s) && !s.st_size) {
88 pcap_write_header(file);
99 pcap_read_header(FILE *file)
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));
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);
116 pcap_write_header(FILE *file)
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. */
121 ph.magic_number = 0xa1b2c3d4;
122 ph.version_major = 2;
123 ph.version_minor = 4;
127 ph.network = 1; /* Ethernet */
128 ignore(fwrite(&ph, sizeof ph, 1, file));
132 pcap_read(FILE *file, struct ofpbuf **bufp, long long int *when)
134 struct pcaprec_hdr prh;
143 if (fread(&prh, sizeof prh, 1, file) != 1) {
146 VLOG_WARN("failed to read pcap record header: %s",
147 ovs_retval_to_string(error));
154 /* Calculate length. */
158 len = uint32_byteswap(len);
160 VLOG_WARN("bad packet length %"PRIuSIZE" or %"PRIu32
162 len, uint32_byteswap(len));
167 /* Calculate time. */
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;
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));
189 pcap_write(FILE *file, struct ofpbuf *buf)
191 struct pcaprec_hdr prh;
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));