summaryrefslogtreecommitdiff
path: root/examples/opienet/miniwellenreiter/miniwellenreiter.cpp
blob: 284b35a76ee3e765b9e61acd666d571b6651bf77 (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
#include <qdict.h>
#include <qsocketnotifier.h>
#include <qstring.h>
#include <opie2/onetwork.h>
#include <qapplication.h>
#include <opie2/opcap.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>


using namespace Opie::Net;
//======================== Station help class ===============================

class Station
{
  public:
    Station( QString t, int c, bool w ) : type(t), channel(c), wep(w), beacons(1) {};
    ~Station() {};

    QString type;
    int channel;
    bool wep;
    int beacons;
};

QDict<Station> stations;

//======================== Application class ===============================

class Wellenreiter : public QApplication
{
Q_OBJECT
public:
  Wellenreiter( int argc, char** argv ) : QApplication( argc, argv ), channel( 1 )
  {

    ONetwork* net = ONetwork::instance();

    if ( argc < 3 )
    {
        printf( "Usage: ./%s <interface> <driver> <interval>\n", argv[0] );
        printf( "\n" );
        printf( "Valid wireless interfaces (detected) are:\n" );

        ONetwork::InterfaceIterator it = net->iterator();
        while ( it.current() )
        {
            if ( it.current()->isWireless() )
            {
                printf( " - '%s' (MAC=%s) (IPv4=%s)\n", (const char*) it.current()->name(),
                                                    (const char*) it.current()->macAddress().toString(),
                                                    (const char*) it.current()->ipV4Address().toString() );
            }
            ++it;
        }
    exit( -1 );
    }

    printf( "************************************************************************\n" );
    printf( "* Wellenreiter mini edition 1.0.0 (C) 2003-2005 Michael 'Mickey' Lauer *\n" );
    printf( "************************************************************************\n" );
    printf( "\n\n" );

    QString interface( argv[1] );
    QString driver( argv[2] );

    printf( "Trying to use '%s' as %s-controlled device...\n", (const char*) interface, (const char*) driver );

    // sanity checks before downcasting
    ONetworkInterface* iface = net->interface( interface );
    if ( !iface )
    {
        printf( "Interface '%s' doesn't exist. Exiting.\n", (const char*) interface );
        exit( -1 );
    }
    if ( !iface->isWireless() )
    {
        printf( "Interface '%s' doesn't support wireless extensions. Exiting.\n", (const char*) interface );
        exit( -1 );
    }

    // downcast should be safe now
    wiface = (OWirelessNetworkInterface*) iface;
    printf( "Using wireless interface '%s' for scanning (current SSID is '%s')...\n", (const char*) interface, (const char*) wiface->SSID() );

    // ifconfig +promisc the interface to receive all packets
    if ( !wiface->promiscuousMode() )
    {
        printf( "Interface status is not promisc... switching to promisc... " );
        wiface->setPromiscuousMode( true );
        if ( !wiface->promiscuousMode() )
        {
            printf( "failed (%s). Exiting.\n", strerror( errno ) );
            exit( -1 );
        }
        else
        {
            printf( "ok.\n" );
        }
    }
    else
        printf( "Interface status is already promisc - good.\n" );

    // connect a monitoring strategy to the interface
    if ( driver == "orinoco" )
        new OOrinocoMonitoringInterface( wiface, false );
    else
    if ( driver == "hostap" )
        new OHostAPMonitoringInterface( wiface, false );
    else
    if ( driver == "wlan-ng" )
        new OWlanNGMonitoringInterface( wiface, false );
    else
    {
        printf( "Unknown driver. Exiting\n" );
        exit( -1 );
    }

    // enable monitoring mode
    printf( "Enabling monitor mode...\n" );
    wiface->setMode( "monitor" );

    // open a packet capturer
    cap = new OPacketCapturer();
    printf( "OPacketCapturer using libpcap %s", (const char*) cap->version() );
    cap->open( interface );
    if ( !cap->isOpen() )
    {
        printf( "Unable to open libpcap (%s). Exiting.\n", strerror( errno ) );
        exit( -1 );
    }

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

    // start channel hopper
    //wiface->setChannelHopping( 1000 );

    // connect
    connect( cap, SIGNAL( receivedPacket(OPacket*) ), this, SLOT( receivePacket(OPacket*) ) );
    // timer
    startTimer( 1000 );

  }

  ~Wellenreiter() {};

public slots:
  virtual void timerEvent(QTimerEvent* e)
  {
    wiface->setChannel( channel++ );
    if ( channel == 14 ) channel = 1;
  }

  void receivePacket(OPacket* p)
  {
    if (!p)
    {
        printf( "(empty packet received)\n" );
        return;
    }

    OWaveLanManagementPacket* beacon = (OWaveLanManagementPacket*) p->child( "802.11 Management" );
    if ( beacon )
    {
        OWaveLanManagementSSID* ssid = static_cast<OWaveLanManagementSSID*>( p->child( "802.11 SSID" ) );
        QString essid = ssid ? ssid->ID() : QString::fromLatin1( "<unknown>" );

        if ( stations.find( essid ) )
            stations[essid]->beacons++;
        else
        {
            printf( "found new network @ channel %d, SSID = '%s'\n", wiface->channel(), (const char*) essid );
            stations.insert( essid, new Station( "unknown", wiface->channel(),
            ((OWaveLanPacket*) beacon->parent())->usesWep() ) );
        }
        return;
    }

    OWaveLanDataPacket* data = (OWaveLanDataPacket*) p->child( "802.11 Data" );
    if ( data )
    {
        OWaveLanPacket* wlan = (OWaveLanPacket*) p->child( "802.11" );
        if ( wlan->fromDS() && !wlan->toDS() )
        {
            printf( "FromDS: '%s' -> '%s' via '%s'\n",
                (const char*) wlan->macAddress3().toString(true),
                (const char*) wlan->macAddress1().toString(true),
                (const char*) wlan->macAddress2().toString(true) );
        }
        else
        if ( !wlan->fromDS() && wlan->toDS() )
        {
            printf( "ToDS: '%s' -> '%s' via '%s'\n",
                (const char*) wlan->macAddress2().toString(true),
                (const char*) wlan->macAddress3().toString(true),
                (const char*) wlan->macAddress1().toString(true) );
        }
        else
        if ( wlan->fromDS() && wlan->toDS() )
        {
            printf( "WSD(bridge): '%s' -> '%s' via '%s' and '%s'\n",
                (const char*) wlan->macAddress4().toString(true),
                (const char*) wlan->macAddress3().toString(true),
                (const char*) wlan->macAddress1().toString(true),
                (const char*) wlan->macAddress2().toString(true) );
        }
        else
        {
            printf( "IBSS(AdHoc): '%s' -> '%s' (Cell: '%s')'\n",
                (const char*) wlan->macAddress2().toString(true),
                (const char*) wlan->macAddress1().toString(true),
                (const char*) wlan->macAddress3().toString(true) );
        }
        return;
    }
  }
private:
  OPacketCapturer* cap;
  OWirelessNetworkInterface* wiface;
  int channel;
};


int main( int argc, char** argv )
{
    Wellenreiter w( argc, argv );
    w.exec();
    return 0;
}

#include "miniwellenreiter.moc"