File: vihasource.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 (233 lines) | stat: -rw-r--r-- 6,295 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 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 "config.h"

#include "vihasource.h"

#ifdef HAVE_VIHAHEADERS

int VihaSource::OpenSource() {
    pthread_t read_loop;

    /* Open source */
    wlsource = new WLPacketSource(11);
    wlsource->open();
    wldi = wlsource->getDriverInterface();

    /* Make pipe */
    if ( pipe(pipe_fds) < 0 ) {
        snprintf(errstr, 1024, "VihaSource could not create pipe: %d %s",
                 errno, strerror(errno));
        return -1;
    }

    // Initialize our locking controls
    frame_full = 0;
    frame = NULL;
    pthread_mutex_init(&capture_lock, NULL);
    pthread_cond_init(&capture_wait, NULL);

    /* Start read loop */
    pthread_create(&read_loop, NULL, ReadPacketLoop, this);

    return 1;
}

int VihaSource::CloseSource() {
    /* Stop capture */
    wlsource->close();
    delete wlsource;

    /* Unlock the capture so he can exit */
    pthread_mutex_unlock(&capture_lock);

    close(pipe_fds[0]);
    close(pipe_fds[1]);

    return 1;
}

int VihaSource::LocalSetChannel(int in_ch, char *in_err) {
    // Will this work inside the chanchange process?  I can only
    // hope it will...
    wldi->stopCapture();
    wldi->startCapture(in_ch);

    return 1;
}

int VihaSource::FetchChannel() {
    return (wldi->getChannel());
}

int VihaSource::FetchPacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int bytes;
    char byte;

    /* Read byte from pipe */
    read(pipe_fds[0], &byte, 1);

    /* This should never be locked for very long so it shouldn't cause blocking issues */
    pthread_mutex_lock(&capture_lock);
    if (frame_full != 1) {
        // If the frame isn't full something funny is happening since we had a byte in the
        // pipe, but just return 0.  we don't have to sleep since it won't have another byte
        // in the pipe until theres another packet
        pthread_mutex_unlock(&capture_lock);
        return 0;
    }

    if ( frame == NULL ) {

        snprintf(errstr, 1024, "Something bad happened, frame was null. -_-");

        bytes = -1;
    }
    else {
        bytes = Viha2Common(packet, data, moddata);
    }

    frame_full = 0;
    frame = NULL;

    pthread_mutex_unlock(&capture_lock);
    pthread_cond_broadcast(&capture_wait);

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

    return bytes;
}

int VihaSource::Viha2Common(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int header_len;
    uint8_t byte;

    /* Alias our frame control */
    frame_control *fc = (frame_control *)&frame->frameControl;
    /* Alias our frame */
    uint8_t *intframe = (uint8_t *)frame;

    /* is it a 4-mac or 3-mac header? */
    if (fc->from_ds && fc->to_ds)
        header_len = 30;
    else
        header_len = 24;

    memset(packet, 0, sizeof(kis_packet));

    gettimeofday(&packet->ts, NULL);

    packet->data = data;

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

    if ( frame->dataLen + header_len > MAX_PACKET_LEN ) {
        snprintf(errstr, 1024, "Packet was too big. -_-");
        return -1;
    }

    packet->caplen = frame->dataLen + header_len;
    packet->len = packet->caplen;
    packet->signal = frame->signal;
    packet->noise = frame->silence;

    packet->channel = FetchChannel();

    memcpy(packet->data, &intframe[14], header_len);
    memcpy(packet->data+header_len, &intframe[sizeof(WLFrame)], frame->dataLen);

    for ( int i = 0; i < header_len; i+=2 ) {
        byte = data[i];
        data[i] = data[i+1];
        data[i+1] = byte;
    }

    packet->carrier = carrier_80211b;
    packet->datarate = frame->rate;

    if (gpsd != NULL) {
        gpsd->FetchLoc(&packet->gps_lat, &packet->gps_lon, &packet->gps_alt,
                       &packet->gps_spd, &packet->gps_heading, &packet->gps_fix);
    }


    return packet->len + header_len;
}

/* Thread proc for blocking readPacket() calls */
void *ReadPacketLoop(void *vsp) {
    char byte = 42;

    VihaSource *vs = (VihaSource *)vsp;

    pthread_detach(pthread_self());

    while ( 1 ) {
        /* Wait until we can fetch a packet */
        pthread_mutex_lock(&(vs->capture_lock));

        /* Has frame been read? */
        if (vs->frame_full != 0) {
            /* Unlock capture lock and wait */
            pthread_mutex_unlock(&(vs->capture_lock));

            pthread_cond_wait(&(vs->capture_wait), &(vs->capture_lock));
            // continue;
        }

        vs->frame_full = 0;
        vs->frame = NULL;
        
        /* Get next frame */
        while ( vs->frame == NULL ) {
            vs->frame = vs->wlsource->readPacket();
        }

        vs->frame_full = 1;

        /* We have a packet.  Unlock ourselves and write a byte to trigger select */
        pthread_mutex_unlock(&(vs->capture_lock));
        write(vs->pipe_fds[1], &byte, 1);

    }

    return NULL;
}

KisPacketSource *vihasource_registrant(string in_name, string in_device,
                                       char *in_err) {
    return new VihaSource(in_name, in_device);
}

int chancontrol_viha(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    if (in_ext == NULL) {
        snprintf(in_err, STATUS_MAX, "No reference to vihasource instance to change "
                 "channel.");
        return -1;
    }

    // hook into the instance to change channel.  This is really not good, but 
    // I can't figure out how to avoid it.  This is the only source type we have
    // to do this for, but it makes us change the code for all of them.
    return ((VihaSource *) in_ext)->LocalSetChannel(in_ch, in_err);
}

#endif