2 * Copyright (c) 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #define THIS_MODULE VLM_pcap
30 uint32_t magic_number; /* magic number */
31 uint16_t version_major; /* major version number */
32 uint16_t version_minor; /* minor version number */
33 int32_t thiszone; /* GMT to local correction */
34 uint32_t sigfigs; /* accuracy of timestamps */
35 uint32_t snaplen; /* max length of captured packets */
36 uint32_t network; /* data link type */
40 uint32_t ts_sec; /* timestamp seconds */
41 uint32_t ts_usec; /* timestamp microseconds */
42 uint32_t incl_len; /* number of octets of packet saved in file */
43 uint32_t orig_len; /* actual length of packet */
47 pcap_open(const char *file_name, const char *mode)
51 assert(!strcmp(mode, "rb") || !strcmp(mode, "wb"));
53 file = fopen(file_name, mode);
55 VLOG_WARN("%s: failed to open pcap file for %s",
56 file_name, mode[0] == 'r' ? "reading" : "writing");
61 if (!pcap_read_header(file)) {
66 pcap_write_header(file);
72 pcap_read_header(FILE *file)
75 if (fread(&ph, sizeof ph, 1, file) != 1) {
76 int error = ferror(file) ? errno : EOF;
77 VLOG_WARN("failed to read pcap header: %s",
78 error > 0 ? strerror(error) : "end of file");
81 if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
82 VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
83 "(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number);
90 pcap_write_header(FILE *file)
92 /* The pcap reader is responsible for figuring out endianness based on the
93 * magic number, so the lack of htonX calls here is intentional. */
95 ph.magic_number = 0xa1b2c3d4;
101 ph.network = 1; /* Ethernet */
102 fwrite(&ph, sizeof ph, 1, file);
106 pcap_read(FILE *file, struct ofpbuf **bufp)
108 struct pcaprec_hdr prh;
116 if (fread(&prh, sizeof prh, 1, file) != 1) {
117 int error = ferror(file) ? errno : EOF;
118 VLOG_WARN("failed to read pcap record header: %s",
119 error > 0 ? strerror(error) : "end of file");
123 /* Calculate length. */
126 uint32_t swapped_len = (((len & 0xff000000) >> 24) |
127 ((len & 0x00ff0000) >> 8) |
128 ((len & 0x0000ff00) << 8) |
129 ((len & 0x000000ff) << 24));
130 if (swapped_len > 0xffff) {
131 VLOG_WARN("bad packet length %"PRIu32" or %"PRIu32" "
140 buf = ofpbuf_new(len);
141 data = ofpbuf_put_uninit(buf, len);
142 if (fread(data, len, 1, file) != 1) {
143 int error = ferror(file) ? errno : EOF;
144 VLOG_WARN("failed to read pcap packet: %s",
145 error > 0 ? strerror(error) : "end of file");
154 pcap_write(FILE *file, struct ofpbuf *buf)
156 struct pcaprec_hdr prh;
159 prh.incl_len = buf->size;
160 prh.orig_len = buf->size;
161 fwrite(&prh, sizeof prh, 1, file);
162 fwrite(buf->data, buf->size, 1, file);