2009-07-08 13:19:16 -07:00
|
|
|
/*
|
2013-09-23 10:27:10 -07:00
|
|
|
* Copyright (c) 2009, 2010, 2012, 2013 Nicira, Inc.
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-07-08 13:19:16 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2013-03-15 02:19:31 -07:00
|
|
|
#include "pcap-file.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2013-09-23 10:27:10 -07:00
|
|
|
#include <stdlib.h>
|
2009-07-08 13:19:16 -07:00
|
|
|
#include <string.h>
|
2013-09-23 10:27:10 -07:00
|
|
|
#include <sys/stat.h>
|
2013-12-02 14:53:27 -08:00
|
|
|
#include "byte-order.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "compiler.h"
|
|
|
|
#include "ofpbuf.h"
|
2013-11-22 11:42:06 -08:00
|
|
|
#include "timeval.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "vlog.h"
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
VLOG_DEFINE_THIS_MODULE(pcap);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
struct pcap_hdr {
|
|
|
|
uint32_t magic_number; /* magic number */
|
|
|
|
uint16_t version_major; /* major version number */
|
|
|
|
uint16_t version_minor; /* minor version number */
|
|
|
|
int32_t thiszone; /* GMT to local correction */
|
|
|
|
uint32_t sigfigs; /* accuracy of timestamps */
|
|
|
|
uint32_t snaplen; /* max length of captured packets */
|
|
|
|
uint32_t network; /* data link type */
|
2010-02-12 13:56:12 -08:00
|
|
|
};
|
|
|
|
BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
struct pcaprec_hdr {
|
|
|
|
uint32_t ts_sec; /* timestamp seconds */
|
|
|
|
uint32_t ts_usec; /* timestamp microseconds */
|
|
|
|
uint32_t incl_len; /* number of octets of packet saved in file */
|
|
|
|
uint32_t orig_len; /* actual length of packet */
|
2010-02-12 13:56:12 -08:00
|
|
|
};
|
|
|
|
BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
FILE *
|
|
|
|
pcap_open(const char *file_name, const char *mode)
|
|
|
|
{
|
2013-09-23 10:27:10 -07:00
|
|
|
struct stat s;
|
2009-07-08 13:19:16 -07:00
|
|
|
FILE *file;
|
2013-09-23 10:27:10 -07:00
|
|
|
int error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2013-09-23 10:27:10 -07:00
|
|
|
ovs_assert(!strcmp(mode, "rb") ||
|
|
|
|
!strcmp(mode, "wb") ||
|
|
|
|
!strcmp(mode, "ab"));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
file = fopen(file_name, mode);
|
|
|
|
if (file == NULL) {
|
2013-09-23 10:27:10 -07:00
|
|
|
VLOG_WARN("%s: failed to open pcap file for %s (%s)", file_name,
|
|
|
|
(mode[0] == 'r' ? "reading"
|
|
|
|
: mode[0] == 'w' ? "writing"
|
|
|
|
: "appending"),
|
|
|
|
ovs_strerror(errno));
|
2009-07-08 13:19:16 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-23 10:27:10 -07:00
|
|
|
switch (mode[0]) {
|
|
|
|
case 'r':
|
|
|
|
error = pcap_read_header(file);
|
|
|
|
if (error) {
|
|
|
|
errno = error;
|
2009-07-08 13:19:16 -07:00
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-23 10:27:10 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
2009-07-08 13:19:16 -07:00
|
|
|
pcap_write_header(file);
|
2013-09-23 10:27:10 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
if (!fstat(fileno(file), &s) && !s.st_size) {
|
|
|
|
pcap_write_header(file);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-12-17 10:32:12 -08:00
|
|
|
OVS_NOT_REACHED();
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pcap_read_header(FILE *file)
|
|
|
|
{
|
|
|
|
struct pcap_hdr ph;
|
|
|
|
if (fread(&ph, sizeof ph, 1, file) != 1) {
|
|
|
|
int error = ferror(file) ? errno : EOF;
|
2011-01-30 11:29:14 -08:00
|
|
|
VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
|
2009-07-08 13:19:16 -07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
|
|
|
|
VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
|
|
|
|
"(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number);
|
|
|
|
return EPROTO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pcap_write_header(FILE *file)
|
|
|
|
{
|
|
|
|
/* The pcap reader is responsible for figuring out endianness based on the
|
|
|
|
* magic number, so the lack of htonX calls here is intentional. */
|
|
|
|
struct pcap_hdr ph;
|
|
|
|
ph.magic_number = 0xa1b2c3d4;
|
|
|
|
ph.version_major = 2;
|
|
|
|
ph.version_minor = 4;
|
|
|
|
ph.thiszone = 0;
|
|
|
|
ph.sigfigs = 0;
|
|
|
|
ph.snaplen = 1518;
|
|
|
|
ph.network = 1; /* Ethernet */
|
2011-04-12 10:40:15 -07:00
|
|
|
ignore(fwrite(&ph, sizeof ph, 1, file));
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-11-22 11:42:06 -08:00
|
|
|
pcap_read(FILE *file, struct ofpbuf **bufp, long long int *when)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
struct pcaprec_hdr prh;
|
|
|
|
struct ofpbuf *buf;
|
|
|
|
void *data;
|
|
|
|
size_t len;
|
2013-11-22 11:42:06 -08:00
|
|
|
bool swap;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
*bufp = NULL;
|
|
|
|
|
|
|
|
/* Read header. */
|
|
|
|
if (fread(&prh, sizeof prh, 1, file) != 1) {
|
2013-09-23 10:14:35 -07:00
|
|
|
if (ferror(file)) {
|
|
|
|
int error = errno;
|
|
|
|
VLOG_WARN("failed to read pcap record header: %s",
|
|
|
|
ovs_retval_to_string(error));
|
|
|
|
return error;
|
|
|
|
} else {
|
|
|
|
return EOF;
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate length. */
|
|
|
|
len = prh.incl_len;
|
2013-11-22 11:42:06 -08:00
|
|
|
swap = len > 0xffff;
|
|
|
|
if (swap) {
|
|
|
|
len = uint32_byteswap(len);
|
|
|
|
if (len > 0xffff) {
|
|
|
|
VLOG_WARN("bad packet length %"PRIuSIZE" or %"PRIu32
|
2009-07-08 13:19:16 -07:00
|
|
|
"reading pcap file",
|
2013-11-22 11:42:06 -08:00
|
|
|
len, uint32_byteswap(len));
|
2009-07-08 13:19:16 -07:00
|
|
|
return EPROTO;
|
|
|
|
}
|
2013-11-22 11:42:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate time. */
|
|
|
|
if (when) {
|
|
|
|
uint32_t ts_sec = swap ? uint32_byteswap(prh.ts_sec) : prh.ts_sec;
|
|
|
|
uint32_t ts_usec = swap ? uint32_byteswap(prh.ts_usec) : prh.ts_usec;
|
|
|
|
*when = ts_sec * 1000LL + ts_usec / 1000;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read packet. */
|
|
|
|
buf = ofpbuf_new(len);
|
|
|
|
data = ofpbuf_put_uninit(buf, len);
|
|
|
|
if (fread(data, len, 1, file) != 1) {
|
|
|
|
int error = ferror(file) ? errno : EOF;
|
|
|
|
VLOG_WARN("failed to read pcap packet: %s",
|
2011-01-30 11:29:14 -08:00
|
|
|
ovs_retval_to_string(error));
|
2009-07-08 13:19:16 -07:00
|
|
|
ofpbuf_delete(buf);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
*bufp = buf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pcap_write(FILE *file, struct ofpbuf *buf)
|
|
|
|
{
|
|
|
|
struct pcaprec_hdr prh;
|
2013-11-22 11:42:06 -08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
xgettimeofday(&tv);
|
|
|
|
prh.ts_sec = tv.tv_sec;
|
|
|
|
prh.ts_usec = tv.tv_usec;
|
2009-07-08 13:19:16 -07:00
|
|
|
prh.incl_len = buf->size;
|
|
|
|
prh.orig_len = buf->size;
|
2011-04-12 10:40:15 -07:00
|
|
|
ignore(fwrite(&prh, sizeof prh, 1, file));
|
|
|
|
ignore(fwrite(buf->data, buf->size, 1, file));
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|