00001 #include <qdict.h>
00002 #include <qsocketnotifier.h>
00003 #include <qstring.h>
00004 #include <opie2/onetwork.h>
00005 #include <qapplication.h>
00006 #include <opie2/opcap.h>
00007 #include <cerrno>
00008 #include <cstdio>
00009 #include <cstdlib>
00010 #include <cstring>
00011
00012
00013 using namespace Opie::Net;
00014
00015
00016 class Station
00017 {
00018 public:
00019 Station( QString t, int c, bool w ) : type(t), channel(c), wep(w), beacons(1) {};
00020 ~Station() {};
00021
00022 QString type;
00023 int channel;
00024 bool wep;
00025 int beacons;
00026 };
00027
00028 QDict<Station> stations;
00029
00030
00031
00032 class Wellenreiter : public QApplication
00033 {
00034 Q_OBJECT
00035 public:
00036 Wellenreiter( int argc, char** argv ) : QApplication( argc, argv ), channel( 1 )
00037 {
00038
00039 ONetwork* net = ONetwork::instance();
00040
00041 if ( argc < 3 )
00042 {
00043 printf( "Usage: ./%s <interface> <driver> <interval>\n", argv[0] );
00044 printf( "\n" );
00045 printf( "Valid wireless interfaces (detected) are:\n" );
00046
00047 ONetwork::InterfaceIterator it = net->iterator();
00048 while ( it.current() )
00049 {
00050 if ( it.current()->isWireless() )
00051 {
00052 printf( " - '%s' (MAC=%s) (IPv4=%s)\n", (const char*) it.current()->name(),
00053 (const char*) it.current()->macAddress().toString(),
00054 (const char*) it.current()->ipV4Address().toString() );
00055 }
00056 ++it;
00057 }
00058 exit( -1 );
00059 }
00060
00061 printf( "************************************************************************\n" );
00062 printf( "* Wellenreiter mini edition 1.0.0 (C) 2003-2005 Michael 'Mickey' Lauer *\n" );
00063 printf( "************************************************************************\n" );
00064 printf( "\n\n" );
00065
00066 QString interface( argv[1] );
00067 QString driver( argv[2] );
00068
00069 printf( "Trying to use '%s' as %s-controlled device...\n", (const char*) interface, (const char*) driver );
00070
00071
00072 ONetworkInterface* iface = net->interface( interface );
00073 if ( !iface )
00074 {
00075 printf( "Interface '%s' doesn't exist. Exiting.\n", (const char*) interface );
00076 exit( -1 );
00077 }
00078 if ( !iface->isWireless() )
00079 {
00080 printf( "Interface '%s' doesn't support wireless extensions. Exiting.\n", (const char*) interface );
00081 exit( -1 );
00082 }
00083
00084
00085 wiface = (OWirelessNetworkInterface*) iface;
00086 printf( "Using wireless interface '%s' for scanning (current SSID is '%s')...\n", (const char*) interface, (const char*) wiface->SSID() );
00087
00088
00089 if ( !wiface->promiscuousMode() )
00090 {
00091 printf( "Interface status is not promisc... switching to promisc... " );
00092 wiface->setPromiscuousMode( true );
00093 if ( !wiface->promiscuousMode() )
00094 {
00095 printf( "failed (%s). Exiting.\n", strerror( errno ) );
00096 exit( -1 );
00097 }
00098 else
00099 {
00100 printf( "ok.\n" );
00101 }
00102 }
00103 else
00104 printf( "Interface status is already promisc - good.\n" );
00105
00106
00107 if ( driver == "orinoco" )
00108 new OOrinocoMonitoringInterface( wiface, false );
00109 else
00110 if ( driver == "hostap" )
00111 new OHostAPMonitoringInterface( wiface, false );
00112 else
00113 if ( driver == "wlan-ng" )
00114 new OWlanNGMonitoringInterface( wiface, false );
00115 else
00116 {
00117 printf( "Unknown driver. Exiting\n" );
00118 exit( -1 );
00119 }
00120
00121
00122 printf( "Enabling monitor mode...\n" );
00123 wiface->setMode( "monitor" );
00124
00125
00126 cap = new OPacketCapturer();
00127 printf( "OPacketCapturer using libpcap %s", (const char*) cap->version() );
00128 cap->open( interface );
00129 if ( !cap->isOpen() )
00130 {
00131 printf( "Unable to open libpcap (%s). Exiting.\n", strerror( errno ) );
00132 exit( -1 );
00133 }
00134
00135
00136 cap->setBlocking( false );
00137
00138
00139
00140
00141
00142 connect( cap, SIGNAL( receivedPacket(OPacket*) ), this, SLOT( receivePacket(OPacket*) ) );
00143
00144 startTimer( 1000 );
00145
00146 }
00147
00148 ~Wellenreiter() {};
00149
00150 public slots:
00151 virtual void timerEvent(QTimerEvent* e)
00152 {
00153 wiface->setChannel( channel++ );
00154 if ( channel == 14 ) channel = 1;
00155 }
00156
00157 void receivePacket(OPacket* p)
00158 {
00159 if (!p)
00160 {
00161 printf( "(empty packet received)\n" );
00162 return;
00163 }
00164
00165 OWaveLanManagementPacket* beacon = (OWaveLanManagementPacket*) p->child( "802.11 Management" );
00166 if ( beacon )
00167 {
00168 OWaveLanManagementSSID* ssid = static_cast<OWaveLanManagementSSID*>( p->child( "802.11 SSID" ) );
00169 QString essid = ssid ? ssid->ID() : QString::fromLatin1( "<unknown>" );
00170
00171 if ( stations.find( essid ) )
00172 stations[essid]->beacons++;
00173 else
00174 {
00175 printf( "found new network @ channel %d, SSID = '%s'\n", wiface->channel(), (const char*) essid );
00176 stations.insert( essid, new Station( "unknown", wiface->channel(),
00177 ((OWaveLanPacket*) beacon->parent())->usesWep() ) );
00178 }
00179 return;
00180 }
00181
00182 OWaveLanDataPacket* data = (OWaveLanDataPacket*) p->child( "802.11 Data" );
00183 if ( data )
00184 {
00185 OWaveLanPacket* wlan = (OWaveLanPacket*) p->child( "802.11" );
00186 if ( wlan->fromDS() && !wlan->toDS() )
00187 {
00188 printf( "FromDS: '%s' -> '%s' via '%s'\n",
00189 (const char*) wlan->macAddress3().toString(true),
00190 (const char*) wlan->macAddress1().toString(true),
00191 (const char*) wlan->macAddress2().toString(true) );
00192 }
00193 else
00194 if ( !wlan->fromDS() && wlan->toDS() )
00195 {
00196 printf( "ToDS: '%s' -> '%s' via '%s'\n",
00197 (const char*) wlan->macAddress2().toString(true),
00198 (const char*) wlan->macAddress3().toString(true),
00199 (const char*) wlan->macAddress1().toString(true) );
00200 }
00201 else
00202 if ( wlan->fromDS() && wlan->toDS() )
00203 {
00204 printf( "WSD(bridge): '%s' -> '%s' via '%s' and '%s'\n",
00205 (const char*) wlan->macAddress4().toString(true),
00206 (const char*) wlan->macAddress3().toString(true),
00207 (const char*) wlan->macAddress1().toString(true),
00208 (const char*) wlan->macAddress2().toString(true) );
00209 }
00210 else
00211 {
00212 printf( "IBSS(AdHoc): '%s' -> '%s' (Cell: '%s')'\n",
00213 (const char*) wlan->macAddress2().toString(true),
00214 (const char*) wlan->macAddress1().toString(true),
00215 (const char*) wlan->macAddress3().toString(true) );
00216 }
00217 return;
00218 }
00219 }
00220 private:
00221 OPacketCapturer* cap;
00222 OWirelessNetworkInterface* wiface;
00223 int channel;
00224 };
00225
00226
00227 int main( int argc, char** argv )
00228 {
00229 Wellenreiter w( argc, argv );
00230 w.exec();
00231 return 0;
00232 }
00233
00234 #include "miniwellenreiter.moc"
00235