summaryrefslogtreecommitdiff
path: root/lib/pcap.c
blob: 967bb5c34b8cf9ceb77afd60be504457c6dbe43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (c) 2009 Nicira Networks.
 *
 * 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:
 *
 *     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.
 */

#include <config.h>
#include "pcap.h"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include "compiler.h"
#include "ofpbuf.h"

#define THIS_MODULE VLM_pcap
#include "vlog.h"

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 */
} PACKED;

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 */
} PACKED;

FILE *
pcap_open(const char *file_name, const char *mode)
{
    FILE *file;

    assert(!strcmp(mode, "rb") || !strcmp(mode, "wb"));

    file = fopen(file_name, mode);
    if (file == NULL) {
        VLOG_WARN("%s: failed to open pcap file for %s",
                  file_name, mode[0] == 'r' ? "reading" : "writing");
        return NULL;
    }

    if (mode[0] == 'r') {
        if (!pcap_read_header(file)) {
            fclose(file);
            return NULL;
        }
    } else {
        pcap_write_header(file);
    }
    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;
        VLOG_WARN("failed to read pcap header: %s",
                  error > 0 ? strerror(error) : "end of file");
        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 */
    fwrite(&ph, sizeof ph, 1, file);
}

int
pcap_read(FILE *file, struct ofpbuf **bufp)
{
    struct pcaprec_hdr prh;
    struct ofpbuf *buf;
    void *data;
    size_t len;

    *bufp = NULL;

    /* Read header. */
    if (fread(&prh, sizeof prh, 1, file) != 1) {
        int error = ferror(file) ? errno : EOF;
        VLOG_WARN("failed to read pcap record header: %s",
                  error > 0 ? strerror(error) : "end of file");
        return error;
    }

    /* Calculate length. */
    len = prh.incl_len;
    if (len > 0xffff) {
        uint32_t swapped_len = (((len & 0xff000000) >> 24) |
                                ((len & 0x00ff0000) >>  8) |
                                ((len & 0x0000ff00) <<  8) |
                                ((len & 0x000000ff) << 24));
        if (swapped_len > 0xffff) {
            VLOG_WARN("bad packet length %zu or %"PRIu32" "
                      "reading pcap file",
                      len, swapped_len);
            return EPROTO;
        }
        len = swapped_len;
    }

    /* 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",
                  error > 0 ? strerror(error) : "end of file");
        ofpbuf_delete(buf);
        return error;
    }
    *bufp = buf;
    return 0;
}

void
pcap_write(FILE *file, struct ofpbuf *buf)
{
    struct pcaprec_hdr prh;
    prh.ts_sec = 0;
    prh.ts_usec = 0;
    prh.incl_len = buf->size;
    prh.orig_len = buf->size;
    fwrite(&prh, sizeof prh, 1, file);
    fwrite(buf->data, buf->size, 1, file);
}