700cdea677151d59846869880a27f238010c21e7
[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 "compiler.h"
25 #include "ofpbuf.h"
26 #include "vlog.h"
27
28 VLOG_DEFINE_THIS_MODULE(pcap);
29
30 struct pcap_hdr {
31     uint32_t magic_number;   /* magic number */
32     uint16_t version_major;  /* major version number */
33     uint16_t version_minor;  /* minor version number */
34     int32_t thiszone;        /* GMT to local correction */
35     uint32_t sigfigs;        /* accuracy of timestamps */
36     uint32_t snaplen;        /* max length of captured packets */
37     uint32_t network;        /* data link type */
38 };
39 BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
40
41 struct pcaprec_hdr {
42     uint32_t ts_sec;         /* timestamp seconds */
43     uint32_t ts_usec;        /* timestamp microseconds */
44     uint32_t incl_len;       /* number of octets of packet saved in file */
45     uint32_t orig_len;       /* actual length of packet */
46 };
47 BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
48
49 FILE *
50 pcap_open(const char *file_name, const char *mode)
51 {
52     struct stat s;
53     FILE *file;
54     int error;
55
56     ovs_assert(!strcmp(mode, "rb") ||
57                !strcmp(mode, "wb") ||
58                !strcmp(mode, "ab"));
59
60     file = fopen(file_name, mode);
61     if (file == NULL) {
62         VLOG_WARN("%s: failed to open pcap file for %s (%s)", file_name,
63                   (mode[0] == 'r' ? "reading"
64                    : mode[0] == 'w' ? "writing"
65                    : "appending"),
66                   ovs_strerror(errno));
67         return NULL;
68     }
69
70     switch (mode[0]) {
71     case 'r':
72         error = pcap_read_header(file);
73         if (error) {
74             errno = error;
75             fclose(file);
76             return NULL;
77         }
78         break;
79
80     case 'w':
81         pcap_write_header(file);
82         break;
83
84     case 'a':
85         if (!fstat(fileno(file), &s) && !s.st_size) {
86             pcap_write_header(file);
87         }
88         break;
89
90     default:
91         NOT_REACHED();
92     }
93     return file;
94 }
95
96 int
97 pcap_read_header(FILE *file)
98 {
99     struct pcap_hdr ph;
100     if (fread(&ph, sizeof ph, 1, file) != 1) {
101         int error = ferror(file) ? errno : EOF;
102         VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
103         return error;
104     }
105     if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
106         VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
107                   "(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number);
108         return EPROTO;
109     }
110     return 0;
111 }
112
113 void
114 pcap_write_header(FILE *file)
115 {
116     /* The pcap reader is responsible for figuring out endianness based on the
117      * magic number, so the lack of htonX calls here is intentional. */
118     struct pcap_hdr ph;
119     ph.magic_number = 0xa1b2c3d4;
120     ph.version_major = 2;
121     ph.version_minor = 4;
122     ph.thiszone = 0;
123     ph.sigfigs = 0;
124     ph.snaplen = 1518;
125     ph.network = 1;             /* Ethernet */
126     ignore(fwrite(&ph, sizeof ph, 1, file));
127 }
128
129 int
130 pcap_read(FILE *file, struct ofpbuf **bufp)
131 {
132     struct pcaprec_hdr prh;
133     struct ofpbuf *buf;
134     void *data;
135     size_t len;
136
137     *bufp = NULL;
138
139     /* Read header. */
140     if (fread(&prh, sizeof prh, 1, file) != 1) {
141         if (ferror(file)) {
142             int error = errno;
143             VLOG_WARN("failed to read pcap record header: %s",
144                       ovs_retval_to_string(error));
145             return error;
146         } else {
147             return EOF;
148         }
149     }
150
151     /* Calculate length. */
152     len = prh.incl_len;
153     if (len > 0xffff) {
154         uint32_t swapped_len = (((len & 0xff000000) >> 24) |
155                                 ((len & 0x00ff0000) >>  8) |
156                                 ((len & 0x0000ff00) <<  8) |
157                                 ((len & 0x000000ff) << 24));
158         if (swapped_len > 0xffff) {
159             VLOG_WARN("bad packet length %zu or %"PRIu32" "
160                       "reading pcap file",
161                       len, swapped_len);
162             return EPROTO;
163         }
164         len = swapped_len;
165     }
166
167     /* Read packet. */
168     buf = ofpbuf_new(len);
169     data = ofpbuf_put_uninit(buf, len);
170     if (fread(data, len, 1, file) != 1) {
171         int error = ferror(file) ? errno : EOF;
172         VLOG_WARN("failed to read pcap packet: %s",
173                   ovs_retval_to_string(error));
174         ofpbuf_delete(buf);
175         return error;
176     }
177     *bufp = buf;
178     return 0;
179 }
180
181 void
182 pcap_write(FILE *file, struct ofpbuf *buf)
183 {
184     struct pcaprec_hdr prh;
185     prh.ts_sec = 0;
186     prh.ts_usec = 0;
187     prh.incl_len = buf->size;
188     prh.orig_len = buf->size;
189     ignore(fwrite(&prh, sizeof prh, 1, file));
190     ignore(fwrite(buf->data, buf->size, 1, file));
191 }