File: wtapfilesource.cc

package info (click to toggle)
kismet 2008-05-R1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,232 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (150 lines) | stat: -rw-r--r-- 3,955 bytes parent folder | download | duplicates (4)
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
/*
    This file is part of Kismet

    Kismet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kismet is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "wtapfilesource.h"

#ifdef HAVE_LIBWIRETAP

int WtapFileSource::OpenSource() {
    paused = 0;
    int err;

    char *unconst = strdup(interface.c_str());

#ifdef HAVE_WTAP_ARGQUAD
    char *nfo;
    packfile = wtap_open_offline(unconst, &err, &nfo, false);
#else
    packfile = wtap_open_offline(unconst, &err, false);
#endif

    if (packfile == NULL) {
        snprintf(errstr, 1024, "Wtap file source unable to open %s: %s",
                 unconst, strerror(err));
        free(unconst);
        return -1;
    }

    free(unconst);

    // We need to update this someday to handle wtapfiles with other encodings,
    // like we do with pcapfiles
    if (wtap_file_encap(packfile) != WTAP_ENCAP_IEEE_802_11 &&
        wtap_file_encap(packfile) != WTAP_ENCAP_IEEE_802_11_WITH_RADIO) {
        snprintf(errstr, 1024, "Wtap file '%s' not an 802.11 encapsulation.", 
                 interface.c_str());
        return -1;
    }

    num_packets = 0;

    return 1;
}

int WtapFileSource::CloseSource() {

    if (packfile != NULL)
        wtap_close(packfile);
    return 1;
}

int WtapFileSource::FetchPacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int err;
    long int offset;

#ifdef HAVE_WTAPREAD_INTINT
    if (!wtap_read(packfile, &err, (int *) &offset)) 
#elif defined(HAVE_WTAP_ARGQUAD)
    char *nfo;
    if (!wtap_read(packfile, &err, &nfo, &offset))
#else
    if (!wtap_read(packfile, &err, &offset))
#endif
    {
        snprintf(errstr, 1024, "Wtap file source failed to read packet.");
        return -1;
    }

    packet_header = wtap_phdr(packfile);

    if (packet_header == NULL) {
        snprintf(errstr, 1024, "Wtap file source failed to read header.\n");
        return -1;
    }

    packet_data = wtap_buf_ptr(packfile);

    if (packet_data == NULL) {
        snprintf(errstr, 1024, "Wtap file source failed to read data.\n");
        return -1;
    }

    if (paused)
        return 0;

    Wtap2Common(packet, data, moddata);

    num_packets++;

    snprintf(packet->sourcename, 32, "%s", name.c_str());
    packet->parm = parameters;

    return(packet->caplen);

}

int WtapFileSource::Wtap2Common(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    memset(packet, 0, sizeof(kis_packet));

    packet->caplen = kismin(packet_header->caplen, (uint32_t) MAX_PACKET_LEN);
    packet->len = packet->caplen;

    packet->signal = -1;
    packet->noise = -1;

    packet->ts.tv_sec = packet_header->ts.tv_sec;
    packet->ts.tv_usec = packet_header->ts.tv_usec;

    packet->data = data;
    packet->moddata = moddata;
    packet->modified = 0;

    // We don't use GPS for wtapfiles
    /*
    if (gpsd != NULL) {
        gps->FetchLoc(&packet->gps_lat, &packet->gps_lon, &packet->gps_alt,
                      &packet->gps_spd, &packet->gps_fix);
    }
    */

    memcpy(packet->data, packet_data, packet->caplen);

    // We assume all packets are 802.11b for now
    packet->carrier = carrier_80211b;

    return 1;
}

// Nonclass registrant
KisPacketSource *wtapfilesource_registrant(string in_name, string in_device,
                                           char *in_err) {
    return new WtapFileSource(in_name, in_device);
}

#endif