summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/gui/wellenreiter.cpp
blob: be2a86ed438ab72a56ae0979fed2673cc25c32c1 (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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**********************************************************************
** Copyright (C) 2002 Michael 'Mickey' Lauer.  All rights reserved.
**
** This file is part of Opie Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
***********************************************************************/

// Opie

#ifdef QWS
#include <opie/odevice.h>
using namespace Opie;
#endif

#ifdef QWS
#include <opie2/oapplication.h>
#else
#include <qapplication.h>
#endif
#include <opie2/onetwork.h>
#include <opie2/opcap.h>

// Qt

#include <qcheckbox.h>
#include <qcombobox.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qspinbox.h>
#include <qtoolbutton.h>
#include <qmainwindow.h>

// Standard

#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>

// Local

#include "wellenreiter.h"
#include "scanlist.h"
#include "logwindow.h"
#include "hexwindow.h"
#include "configwindow.h"
#include "statwindow.h"
#include "manufacturers.h"

Wellenreiter::Wellenreiter( QWidget* parent )
    : WellenreiterBase( parent, 0, 0 ),
      sniffing( false ), iface( 0 ), manufacturerdb( 0 ), configwindow( 0 )
{

    //
    // construct manufacturer database
    //

    QString manufile;
    #ifdef QWS
    manufile.sprintf( "%s/share/wellenreiter/manufacturers.dat", (const char*) QPEApplication::qpeDir() );
    #else
    manufile.sprintf( "/usr/local/share/wellenreiter/manufacturers.dat" );
    #endif
    manufacturerdb = new ManufacturerDB( manufile );

    logwindow->log( "(i) Wellenreiter has been started." );

    //
    // detect operating system
    //

    #ifdef QWS
    QString sys;
    sys.sprintf( "(i) Running on '%s'.", (const char*) ODevice::inst()->systemString() );
    _system = ODevice::inst()->system();
    logwindow->log( sys );
    #endif

    // setup GUI
    netview->setColumnWidthMode( 1, QListView::Manual );

    if ( manufacturerdb )
        netview->setManufacturerDB( manufacturerdb );

    pcap = new OPacketCapturer();

}


Wellenreiter::~Wellenreiter()
{
    // no need to delete child widgets, Qt does it all for us

    delete manufacturerdb;
    delete pcap;
}


void Wellenreiter::setConfigWindow( WellenreiterConfigWindow* cw )
{
    configwindow = cw;
}


void Wellenreiter::channelHopped(int c)
{
    QString title = "Wellenreiter II -scan- [";
    QString left;
    if ( c > 1 ) left.fill( '.', c-1 );
    title.append( left );
    title.append( '|' );
    if ( c < iface->channels() )
    {
        QString right;
        right.fill( '.', iface->channels()-c );
        title.append( right );
    }
    title.append( "]" );
    //title.append( QString().sprintf( " %02d", c ) );
    assert( parent() );
    ( (QMainWindow*) parent() )->setCaption( title );
}


void Wellenreiter::receivePacket(OPacket* p)
{
    hexWindow()->log( p->dump( 8 ) );

    // check if we received a beacon frame
    OWaveLanManagementPacket* beacon = static_cast<OWaveLanManagementPacket*>( p->child( "802.11 Management" ) );
    if ( beacon && beacon->managementType() == "Beacon" )
    {
        QString type;
        if ( beacon->canIBSS() )
        {
            type = "adhoc";
        }
        else if ( beacon->canESS() )
        {
            type = "managed";
        }
        else
        {
            qDebug( "Wellenreiter::invalid frame detected: '%s'", (const char*) p->dump( 16 ) );
            return;
        }

        OWaveLanManagementSSID* ssid = static_cast<OWaveLanManagementSSID*>( p->child( "802.11 SSID" ) );
        QString essid = ssid ? ssid->ID() : QString("<unknown>");
        OWaveLanManagementDS* ds = static_cast<OWaveLanManagementDS*>( p->child( "802.11 DS" ) );
        int channel = ds ? ds->channel() : -1;

        OWaveLanPacket* header = static_cast<OWaveLanPacket*>( p->child( "802.11" ) );
        netView()->addNewItem( type, essid, header->macAddress2().toString(), beacon->canPrivacy(), channel, 0 );
        return;
    }

    // check for a data frame
    OWaveLanDataPacket* data = static_cast<OWaveLanDataPacket*>( p->child( "802.11 Data" ) );
    if ( data )
    {
        OWaveLanPacket* wlan = (OWaveLanPacket*) p->child( "802.11" );
        if ( wlan->fromDS() && !wlan->toDS() )
        {
            qDebug( "FromDS traffic: '%s' -> '%s' via '%s'",
                (const char*) wlan->macAddress3().toString(true),
                (const char*) wlan->macAddress1().toString(true),
                (const char*) wlan->macAddress2().toString(true) );
            netView()->traffic( "fromDS", wlan->macAddress3().toString(),
                                        wlan->macAddress1().toString(),
                                        wlan->macAddress2().toString() );
        }
        else
        if ( !wlan->fromDS() && wlan->toDS() )
        {
            qDebug( "ToDS traffic: '%s' -> '%s' via '%s'",
                (const char*) wlan->macAddress2().toString(true),
                (const char*) wlan->macAddress3().toString(true),
                (const char*) wlan->macAddress1().toString(true) );
            netView()->traffic( "toDS", wlan->macAddress2().toString(),
                                      wlan->macAddress3().toString(),
                                      wlan->macAddress1().toString() );
        }
        else
        if ( wlan->fromDS() && wlan->toDS() )
        {
            qDebug( "WSD(bridge) traffic: '%s' -> '%s' via '%s' and '%s'",
                (const char*) wlan->macAddress4().toString(true),
                (const char*) wlan->macAddress3().toString(true),
                (const char*) wlan->macAddress1().toString(true),
                (const char*) wlan->macAddress2().toString(true) );
            netView()->traffic( "WSD", wlan->macAddress4().toString(),
                                     wlan->macAddress3().toString(),
                                     wlan->macAddress1().toString(),
                                     wlan->macAddress2().toString() );
        }
        else
        {
            qDebug( "IBSS(AdHoc) traffic: '%s' -> '%s' (Cell: '%s')'",
                (const char*) wlan->macAddress2().toString(true),
                (const char*) wlan->macAddress1().toString(true),
                (const char*) wlan->macAddress3().toString(true) );
            netView()->traffic( "IBSS", wlan->macAddress2().toString(),
                                        wlan->macAddress1().toString(),
                                        wlan->macAddress3().toString() );
        }
        return;
    }
}


void Wellenreiter::stopClicked()
{
    if ( iface )
    {
        disconnect( SIGNAL( receivedPacket(OPacket*) ), this, SLOT( receivePacket(OPacket*) ) );
        disconnect( SIGNAL( hopped(int) ), this, SLOT( channelHopped(int) ) );
        iface->setChannelHopping(); // stop hopping channels
    }
    else
        killTimers();

    pcap->close();
    sniffing = false;

    if ( iface )
    {
        // switch off monitor mode
        iface->setMonitorMode( false );
        // switch off promisc flag
        iface->setPromiscuousMode( false );

        system( "cardctl reset; sleep 1" ); //FIXME: Use OProcess
    }

    logwindow->log( "(i) Stopped Scanning." );
    assert( parent() );
    ( (QMainWindow*) parent() )->setCaption( "Wellenreiter II" );

    // message the user
    QMessageBox::information( this, "Wellenreiter II", "Your wireless card\nshould now be usable again." );

    sniffing = false;
    emit( stoppedSniffing() );

    // print out statistics
    statwindow->log( "-----------------------------------------" );
    statwindow->log( "- Wellenreiter II Capturing Statistic -" );
    statwindow->log( "-----------------------------------------" );
    statwindow->log( "Packet Type         |   Receive Count" );

    for( QMap<QString,int>::ConstIterator it = pcap->statistics().begin(); it != pcap->statistics().end(); ++it )
    {
        QString left;
        left.sprintf( "%s", (const char*) it.key() );
        left = left.leftJustify( 20 );
        left.append( '|' );
        QString right;
        right.sprintf( "%d", it.data() );
        right = right.rightJustify( 7 );
        statwindow->log( left + right );
    }

}


void Wellenreiter::startClicked()
{
    // get configuration from config window

    const QString& interface = configwindow->interfaceName->currentText();
    const int cardtype = configwindow->daemonDeviceType();
    const int interval = configwindow->daemonHopInterval();

    if ( ( interface == "" ) || ( cardtype == 0 ) )
    {
        QMessageBox::information( this, "Wellenreiter II", "Your device is not\nproperly configured. Please reconfigure!" );
        return;
    }

    // configure device

    ONetwork* net = ONetwork::instance();
    iface = static_cast<OWirelessNetworkInterface*>(net->interface( interface ));

    // set monitor mode

    switch ( cardtype )
    {
        case DEVTYPE_CISCO: iface->setMonitoring( new OCiscoMonitoringInterface( iface ) ); break;
        case DEVTYPE_WLAN_NG: iface->setMonitoring( new OWlanNGMonitoringInterface( iface ) ); break;
        case DEVTYPE_HOSTAP: iface->setMonitoring( new OHostAPMonitoringInterface( iface ) ); break;
        case DEVTYPE_ORINOCO: iface->setMonitoring( new OOrinocoMonitoringInterface( iface ) ); break;
        case DEVTYPE_MANUAL: QMessageBox::information( this, "Wellenreiter II", "Bring your device into\nmonitor mode now." ); break;
        case DEVTYPE_FILE: qDebug( "Wellenreiter: Capturing from file '%s'", (const char*) interface ); break;
        default: assert( 0 ); // shouldn't reach this
    }

    // switch device into monitor mode
    if ( cardtype < DEVTYPE_FILE )
    {
        if ( cardtype != DEVTYPE_MANUAL )
            iface->setMonitorMode( true );
        if ( !iface->monitorMode() )
        {
            QMessageBox::warning( this, "Wellenreiter II", "Can't set device into monitor mode." );
            return;
        }
    }

    // open pcap and start sniffing
    if ( cardtype != DEVTYPE_FILE )
    {
        if ( configwindow->writeCaptureFile->isEnabled() )
        {
            pcap->open( interface, configwindow->captureFileName->text() );
        }
        else
        {
            pcap->open( interface );
        }
    }
    else
    {
        pcap->open( QFile( interface ) );
    }

    if ( !pcap->isOpen() )
    {
        QMessageBox::warning( this, "Wellenreiter II", "Can't open packet capturer:\n" + QString(strerror( errno ) ));
        return;
    }

    // set capturer to non-blocking mode
    pcap->setBlocking( false );

    // start channel hopper
    if ( cardtype != DEVTYPE_FILE )
        iface->setChannelHopping( 1000 ); //use interval from config window

    if ( cardtype != DEVTYPE_FILE )
    {
        // connect socket notifier and start channel hopper
        connect( pcap, SIGNAL( receivedPacket(OPacket*) ), this, SLOT( receivePacket(OPacket*) ) );
        connect( iface->channelHopper(), SIGNAL( hopped(int) ), this, SLOT( channelHopped(int) ) );
    }
    else
    {
        // start timer for reading packets
        startTimer( 100 );
    }

    logwindow->log( "(i) Started Scanning." );
    sniffing = true;
    emit( startedSniffing() );
}


void Wellenreiter::timerEvent( QTimerEvent* )
{
    qDebug( "Wellenreiter::timerEvent()" );
    OPacket* p = pcap->next();
    receivePacket( p );
    delete p;
}