summaryrefslogtreecommitdiff
path: root/libopie2/opienet
Side-by-side diff
Diffstat (limited to 'libopie2/opienet') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/onetutils.cpp28
-rw-r--r--libopie2/opienet/onetutils.h3
-rw-r--r--libopie2/opienet/onetwork.cpp50
-rw-r--r--libopie2/opienet/onetwork.h8
4 files changed, 55 insertions, 34 deletions
diff --git a/libopie2/opienet/onetutils.cpp b/libopie2/opienet/onetutils.cpp
index b317810..0fb21ff 100644
--- a/libopie2/opienet/onetutils.cpp
+++ b/libopie2/opienet/onetutils.cpp
@@ -17,117 +17,143 @@
    : ..    .:,     . . . without even the implied warranty of
    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ; Library General Public License for more
++=   -.     .`     .: details.
 :     =  ...= . :.=-
 -.   .:....=;==+<; You should have received a copy of the GNU
  -_. . .   )=.  = Library General Public License along with
    --        :-=` this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <opie2/onetutils.h>
#include <opie2/onetwork.h>
#include <net/if.h>
#include <cstdio>
using namespace std;
#define IW_PRIV_TYPE_MASK 0x7000
#define IW_PRIV_TYPE_NONE 0x0000
#define IW_PRIV_TYPE_BYTE 0x1000
#define IW_PRIV_TYPE_CHAR 0x2000
#define IW_PRIV_TYPE_INT 0x4000
#define IW_PRIV_TYPE_FLOAT 0x5000
#define IW_PRIV_TYPE_ADDR 0x6000
#define IW_PRIV_SIZE_FIXED 0x0800
#define IW_PRIV_SIZE_MASK 0x07FF
/*======================================================================================
* OMacAddress
*======================================================================================*/
// static initializer for broadcast and unknown MAC Adresses
const unsigned char __broadcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
const OMacAddress& OMacAddress::broadcast = OMacAddress( __broadcast );
const unsigned char __unknown[6] = { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 };
const OMacAddress& OMacAddress::unknown = OMacAddress( __unknown );
//TODO: Incorporate Ethernet Manufacturer database here!
OMacAddress::OMacAddress( unsigned char* p )
{
- memcpy( _bytes, p, 6 ); // D'OH! memcpy in my sources... eeek...
+ memcpy( _bytes, p, 6 );
}
OMacAddress::OMacAddress( const unsigned char* p )
{
memcpy( _bytes, p, 6 );
}
OMacAddress::OMacAddress( struct ifreq& ifr )
{
memcpy( _bytes, ifr.ifr_hwaddr.sa_data, 6 );
}
OMacAddress::~OMacAddress()
{
}
+#ifdef QT_NO_DEBUG
+inline
+#endif
+const unsigned char* OMacAddress::native() const
+{
+ return (const unsigned char*) &_bytes;
+}
+
+
+OMacAddress OMacAddress::fromString( const QString& str )
+{
+ QString addr( str );
+ unsigned char buf[6];
+ bool ok = true;
+ int index = 14;
+ for ( int i = 5; i >= 0; --i )
+ {
+ buf[i] = addr.right( 2 ).toUShort( &ok, 16 );
+ if ( !ok ) return OMacAddress::unknown;
+ addr.truncate( index );
+ index -= 3;
+ }
+ return (const unsigned char*) &buf;
+}
+
+
QString OMacAddress::toString() const
{
QString s;
s.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
_bytes[0]&0xff, _bytes[1]&0xff, _bytes[2]&0xff,
_bytes[3]&0xff, _bytes[4]&0xff, _bytes[5]&0xff );
return s;
}
bool operator==( const OMacAddress &m1, const OMacAddress &m2 )
{
return memcmp( &m1._bytes, &m2._bytes, 6 ) == 0;
}
/*======================================================================================
* OHostAddress
*======================================================================================*/
/*======================================================================================
* OPrivateIOCTL
*======================================================================================*/
OPrivateIOCTL::OPrivateIOCTL( QObject* parent, const char* name, int cmd, int getargs, int setargs )
:QObject( parent, name ), _ioctl( cmd ), _getargs( getargs ), _setargs( setargs )
{
}
OPrivateIOCTL::~OPrivateIOCTL()
{
}
#ifdef QT_NO_DEBUG
inline
#endif
int OPrivateIOCTL::numberGetArgs() const
{
return _getargs & IW_PRIV_SIZE_MASK;
}
#ifdef QT_NO_DEBUG
inline
#endif
diff --git a/libopie2/opienet/onetutils.h b/libopie2/opienet/onetutils.h
index 8be042b..73d52cc 100644
--- a/libopie2/opienet/onetutils.h
+++ b/libopie2/opienet/onetutils.h
@@ -11,96 +11,99 @@
.="- .-=="i,     .._ License as published by the Free Software
 - .   .-<_>     .<> Foundation; either version 2 of the License,
     ._= =}       : or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s. This program 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
..}^=.=       =       ; Library General Public License for more
++=   -.     .`     .: details.
 :     =  ...= . :.=-
 -.   .:....=;==+<; You should have received a copy of the GNU
  -_. . .   )=.  = Library General Public License along with
    --        :-=` this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifndef ONETUTILS_H
#define ONETUTILS_H
#include <qdict.h>
#include <qmap.h>
#include <qstring.h>
#include <qhostaddress.h>
#include <qobject.h>
#include <sys/types.h>
struct ifreq;
class OWirelessNetworkInterface;
/*======================================================================================
* OMacAddress
*======================================================================================*/
class OMacAddress
{
public:
OMacAddress( unsigned char* );
OMacAddress( const unsigned char* );
OMacAddress( struct ifreq& );
~OMacAddress();
QString toString() const;
+ const unsigned char* native() const;
+
+ static OMacAddress fromString( const QString& );
public:
static const OMacAddress& broadcast; // ff:ff:ff:ff:ff:ff
static const OMacAddress& unknown; // 44:44:44:44:44:44
private:
unsigned char _bytes[6];
friend bool operator==( const OMacAddress &m1, const OMacAddress &m2 );
};
bool operator==( const OMacAddress &m1, const OMacAddress &m2 );
/*======================================================================================
* OHostAddress
*======================================================================================*/
class OHostAddress : public QHostAddress
{
public:
OHostAddress();
~OHostAddress();
};
/*======================================================================================
* OPrivateIOCTL
*======================================================================================*/
class OPrivateIOCTL : public QObject
{
public:
OPrivateIOCTL( QObject* parent, const char* name, int cmd, int getargs, int setargs );
~OPrivateIOCTL();
int numberGetArgs() const;
int typeGetArgs() const;
int numberSetArgs() const;
int typeSetArgs() const;
void invoke() const;
void setParameter( int, u_int32_t );
private:
u_int32_t _ioctl;
u_int16_t _getargs;
diff --git a/libopie2/opienet/onetwork.cpp b/libopie2/opienet/onetwork.cpp
index f52279a..e916c44 100644
--- a/libopie2/opienet/onetwork.cpp
+++ b/libopie2/opienet/onetwork.cpp
@@ -179,96 +179,104 @@ bool ONetworkInterface::ioctl( int call, struct ifreq& ifreq ) const
qDebug( "ONetworkInterface::ioctl (%s) call %d - Status: Ok.", name(), call );
return ( result != -1 );
}
bool ONetworkInterface::ioctl( int call ) const
{
strcpy( _ifr.ifr_name, name() );
return ioctl( call, _ifr );
}
bool ONetworkInterface::isLoopback() const
{
ioctl( SIOCGIFFLAGS );
return _ifr.ifr_flags & IFF_LOOPBACK;
}
bool ONetworkInterface::setUp( bool b )
{
ioctl( SIOCGIFFLAGS );
if ( b ) _ifr.ifr_flags |= IFF_UP;
else _ifr.ifr_flags &= (~IFF_UP);
return ioctl( SIOCSIFFLAGS );
}
bool ONetworkInterface::isUp() const
{
ioctl( SIOCGIFFLAGS );
return _ifr.ifr_flags & IFF_UP;
}
QString ONetworkInterface::ipV4Address() const
{
if ( ioctl( SIOCGIFADDR ) )
{
struct sockaddr_in *sa = (struct sockaddr_in *) &_ifr.ifr_addr;
//FIXME: Use QHostAddress here
return QString( inet_ntoa( sa->sin_addr ) );
}
else
return "<unknown>";
}
+void ONetworkInterface::setMacAddress( const OMacAddress& addr )
+{
+ _ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+ memcpy( &_ifr.ifr_hwaddr.sa_data, addr.native(), 6 );
+ ioctl( SIOCSIFHWADDR );
+}
+
+
OMacAddress ONetworkInterface::macAddress() const
{
if ( ioctl( SIOCGIFHWADDR ) )
{
return OMacAddress( _ifr );
}
else
{
return OMacAddress::unknown;
}
}
int ONetworkInterface::dataLinkType() const
{
if ( ioctl( SIOCGIFHWADDR ) )
{
return _ifr.ifr_hwaddr.sa_family;
}
else
{
return -1;
}
}
void ONetworkInterface::setMonitoring( OMonitoringInterface* m )
{
_mon = m;
qDebug( "ONetwork::setMonitoring(): Installed monitoring driver '%s' on interface '%s'", (const char*) m->name(), name() );
}
OMonitoringInterface* ONetworkInterface::monitoring() const
{
return _mon;
}
ONetworkInterface::~ONetworkInterface()
{
qDebug( "ONetworkInterface::~ONetworkInterface()" );
if ( _sfd != -1 ) ::close( _sfd );
}
bool ONetworkInterface::setPromiscuousMode( bool b )
{
@@ -602,96 +610,103 @@ bool OWirelessNetworkInterface::monitorMode() const
QString OWirelessNetworkInterface::nickName() const
{
char str[IW_ESSID_MAX_SIZE];
_iwr.u.data.pointer = &str[0];
_iwr.u.data.length = IW_ESSID_MAX_SIZE;
if ( !wioctl( SIOCGIWNICKN ) )
{
return "<unknown>";
}
else
{
str[_iwr.u.data.length] = 0x0; // some drivers (e.g. wlan-ng) don't zero-terminate the string
return str;
}
}
void OWirelessNetworkInterface::setPrivate( const QString& call, int numargs, ... )
{
OPrivateIOCTL* priv = static_cast<OPrivateIOCTL*>( child( (const char*) call ) );
if ( !priv )
{
qDebug( "OWirelessNetworkInterface::setPrivate(): interface '%s' does not support private ioctl '%s'", name(), (const char*) call );
return;
}
if ( priv->numberSetArgs() != numargs )
{
qDebug( "OWirelessNetworkInterface::setPrivate(): parameter count not matching. '%s' expects %d arguments, but got %d", (const char*) call, priv->numberSetArgs(), numargs );
return;
}
qDebug( "OWirelessNetworkInterface::setPrivate(): about to call '%s' on interface '%s'", (const char*) call, name() );
memset( &_iwr, 0, sizeof _iwr );
va_list argp;
va_start( argp, numargs );
for ( int i = 0; i < numargs; ++i )
{
priv->setParameter( i, va_arg( argp, int ) );
}
va_end( argp );
priv->invoke();
}
void OWirelessNetworkInterface::getPrivate( const QString& call )
{
+ qWarning( "OWirelessNetworkInterface::getPrivate() is not implemented yet." );
+}
+
+
+bool OWirelessNetworkInterface::hasPrivate( const QString& call )
+{
+ return child( (const char*) call );
}
QString OWirelessNetworkInterface::SSID() const
{
char str[IW_ESSID_MAX_SIZE];
_iwr.u.essid.pointer = &str[0];
_iwr.u.essid.length = IW_ESSID_MAX_SIZE;
if ( !wioctl( SIOCGIWESSID ) )
{
return "<unknown>";
}
else
{
return str;
}
}
void OWirelessNetworkInterface::setSSID( const QString& ssid )
{
_iwr.u.essid.pointer = const_cast<char*>( (const char*) ssid );
_iwr.u.essid.length = ssid.length();
wioctl( SIOCSIWESSID );
}
bool OWirelessNetworkInterface::wioctl( int call, struct iwreq& iwreq ) const
{
int result = ::ioctl( _sfd, call, &iwreq );
if ( result == -1 )
qDebug( "ONetworkInterface::wioctl (%s) call %d - Status: Failed: %d (%s)", name(), call, result, strerror( errno ) );
else
qDebug( "ONetworkInterface::wioctl (%s) call %d - Status: Ok.", name(), call );
return ( result != -1 );
}
bool OWirelessNetworkInterface::wioctl( int call ) const
{
strcpy( _iwr.ifr_name, name() );
return wioctl( call, _iwr );
}
/*======================================================================================
* OMonitoringInterface
*======================================================================================*/
@@ -789,128 +804,107 @@ OWlanNGMonitoringInterface::OWlanNGMonitoringInterface( ONetworkInterface* iface
OWlanNGMonitoringInterface::~OWlanNGMonitoringInterface()
{
}
void OWlanNGMonitoringInterface::setEnabled( bool b )
{
//FIXME: do nothing if its already in the same mode
QString enable = b ? "true" : "false";
QString cmd;
cmd.sprintf( "$(which wlanctl-ng) %s lnxreq_wlansniff channel=%d enable=%s", (const char*) _if->name(), 1, (const char*) enable );
system( cmd );
}
QString OWlanNGMonitoringInterface::name() const
{
return "wlan-ng";
}
void OWlanNGMonitoringInterface::setChannel( int )
{
// wlan-ng devices automatically switch channels when in monitor mode
}
/*======================================================================================
* OHostAPMonitoringInterface
*======================================================================================*/
OHostAPMonitoringInterface::OHostAPMonitoringInterface( ONetworkInterface* iface )
:OMonitoringInterface( iface )
{
iface->setMonitoring( this );
}
OHostAPMonitoringInterface::~OHostAPMonitoringInterface()
{
}
void OHostAPMonitoringInterface::setEnabled( bool b )
{
// IW_MODE_MONITOR was introduced in Wireless Extensions Version 15
// Wireless Extensions < Version 15 need iwpriv commandos for monitoring
+ //TODO: check wireless extensions version on runtime and use
+ //TODO: SIOCSIWMODE( IW_MODE_MONITOR ) if running on WE >= 15
+
if ( b )
{
- #if WIRELESS_EXT > 14
- _if->_iwr.u.mode = IW_MODE_MONITOR;
- _if->wioctl( SIOCSIWMODE );
- #else
- int* args = (int*) &_if->_iwr.u.name;
- args[0] = 2;
- args[1] = 0;
- _if->wioctl( SIOCDEVPRIVATE );
- #endif
+ _if->setPrivate( "monitor", 1, 2 );
}
else
{
- #if WIRELESS_EXT > 14
- _if->_iwr.u.mode = IW_MODE_INFRA;
- _if->wioctl( SIOCSIWMODE );
- #else
- int* args = (int*) &_if->_iwr.u.name;
- args[0] = 0;
- args[1] = 0;
- _if->wioctl( SIOCDEVPRIVATE );
- #endif
+ _if->setPrivate( "monitor", 1, 0 );
}
}
QString OHostAPMonitoringInterface::name() const
{
return "hostap";
}
/*======================================================================================
* OOrinocoNetworkInterface
*======================================================================================*/
OOrinocoMonitoringInterface::OOrinocoMonitoringInterface( ONetworkInterface* iface )
:OMonitoringInterface( iface )
{
iface->setMonitoring( this );
}
OOrinocoMonitoringInterface::~OOrinocoMonitoringInterface()
{
}
void OOrinocoMonitoringInterface::setChannel( int c )
{
- // call iwpriv <device> monitor 2 <channel>
- int* args = (int*) &_if->_iwr.u.name;
- args[0] = 2;
- args[1] = c;
- _if->wioctl( SIOCIWFIRSTPRIV + 0x8 );
+ _if->setPrivate( "monitor", 2, 2, c );
}
void OOrinocoMonitoringInterface::setEnabled( bool b )
{
if ( b )
{
setChannel( 1 );
}
else
{
- // call iwpriv <device> monitor 0 0
- int* args = (int*) &_if->_iwr.u.name;
- args[0] = 0;
- args[1] = 0;
- _if->wioctl( SIOCIWFIRSTPRIV + 0x8 );
+ _if->setPrivate( "monitor", 2, 0, 0 );
}
}
QString OOrinocoMonitoringInterface::name() const
{
return "orinoco";
}
diff --git a/libopie2/opienet/onetwork.h b/libopie2/opienet/onetwork.h
index 4cadbeb..e249aee 100644
--- a/libopie2/opienet/onetwork.h
+++ b/libopie2/opienet/onetwork.h
@@ -18,283 +18,281 @@
     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
    : ..    .:,     . . . without even the implied warranty of
    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ; Library General Public License for more
++=   -.     .`     .: details.
 :     =  ...= . :.=-
 -.   .:....=;==+<; You should have received a copy of the GNU
  -_. . .   )=.  = Library General Public License along with
    --        :-=` this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifndef ONETWORK_H
#define ONETWORK_H
/* QT */
#include <qvaluelist.h>
#include <qdict.h>
#include <qmap.h>
#include <qobject.h>
#include <qhostaddress.h>
/* OPIE */
#include <opie2/onetutils.h>
#ifndef IFNAMSIZ
#define IFNAMSIZ 16
#endif
#ifndef IW_MAX_PRIV_DEF
#define IW_MAX_PRIV_DEF 128
#endif
// ML: Yeah, I hate to include kernel headers, but it's necessary here
// ML: Here comes an ugly hack to prevent <linux/wireless.h> including <linux/if.h>
// ML: which conflicts with the user header <net/if.h>
// ML: We really a user header for the Wireless Extensions, something like <net/wireless.h>
// ML: I will drop Jean an mail on that subject
#include <net/if.h>
#define _LINUX_IF_H
#include <linux/wireless.h>
-#ifndef SIOCIWFIRSTPRIV
-#define SIOCIWFIRSTPRIV SIOCDEVPRIVATE
-#endif
-
class ONetworkInterface;
class OWirelessNetworkInterface;
class OChannelHopper;
class OMonitoringInterface;
/*======================================================================================
* ONetwork
*======================================================================================*/
class ONetwork : public QObject
{
Q_OBJECT
public:
typedef QDict<ONetworkInterface> InterfaceMap;
typedef QDictIterator<ONetworkInterface> InterfaceIterator;
public:
static ONetwork* instance();
InterfaceIterator iterator() const;
bool isWirelessInterface( const char* ) const;
ONetworkInterface* interface( QString ) const;
protected:
ONetwork();
void synchronize();
private:
static ONetwork* _instance;
InterfaceMap _interfaces;
};
/*======================================================================================
* ONetworkInterface
*======================================================================================*/
class ONetworkInterface : public QObject
{
friend class OMonitoringInterface;
friend class OCiscoMonitoringInterface;
friend class OWlanNGMonitoringInterface;
friend class OHostAPMonitoringInterface;
friend class OOrinocoMonitoringInterface;
public:
ONetworkInterface( QObject* parent, const char* name );
virtual ~ONetworkInterface();
void setMonitoring( OMonitoringInterface* );
OMonitoringInterface* monitoring() const;
bool setPromiscuousMode( bool );
bool promiscuousMode() const;
bool setUp( bool );
bool isUp() const;
bool isLoopback() const;
bool isWireless() const;
QString ipV4Address() const;
+ void setMacAddress( const OMacAddress& );
OMacAddress macAddress() const;
int dataLinkType() const;
protected:
const int _sfd;
mutable ifreq _ifr;
OMonitoringInterface* _mon;
protected:
struct ifreq& ifr() const;
virtual void init();
bool ioctl( int call ) const;
bool ioctl( int call, struct ifreq& ) const;
};
/*======================================================================================
* OChannelHopper
*======================================================================================*/
class OChannelHopper : public QObject
{
public:
OChannelHopper( OWirelessNetworkInterface* );
virtual ~OChannelHopper();
bool isActive() const;
int channel() const;
virtual void timerEvent( QTimerEvent* );
void setInterval( int );
int interval() const;
private:
OWirelessNetworkInterface* _iface;
int _interval;
int _tid;
QValueList<int> _channels;
QValueList<int>::Iterator _channel;
};
/*======================================================================================
* OWirelessNetworkInterface
*======================================================================================*/
class OWirelessNetworkInterface : public ONetworkInterface
{
friend class OMonitoringInterface;
friend class OCiscoMonitoringInterface;
friend class OWlanNGMonitoringInterface;
friend class OHostAPMonitoringInterface;
friend class OOrinocoMonitoringInterface;
friend class OPrivateIOCTL;
public:
enum Mode { AdHoc, Managed, Monitor };
OWirelessNetworkInterface( QObject* parent, const char* name );
virtual ~OWirelessNetworkInterface();
virtual void setChannel( int ) const;
virtual int channel() const;
virtual double frequency() const;
virtual int channels() const;
//virtual double frequency(int) const;
virtual void setMode( Mode ) {};
virtual bool mode() const {};
virtual void setMonitorMode( bool );
virtual bool monitorMode() const;
virtual void setChannelHopping( int interval = 0 );
virtual int channelHopping() const;
virtual void setNickName( const QString& ) {};
virtual QString nickName() const;
virtual void setPrivate( const QString&, int, ... );
+ virtual bool hasPrivate( const QString& );
virtual void getPrivate( const QString& );
virtual bool isAssociated() const {};
virtual QString associatedAP() const;
virtual void setSSID( const QString& );
virtual QString SSID() const;
protected:
void buildChannelList();
void buildPrivateList();
virtual void init();
struct iwreq& iwr() const;
bool wioctl( int call ) const;
bool wioctl( int call, struct iwreq& ) const;
protected:
mutable struct iwreq _iwr;
QMap<int,int> _channels;
private:
OChannelHopper* _hopper;
};
/*======================================================================================
* OMonitoringInterface
*======================================================================================*/
class OMonitoringInterface
{
public:
OMonitoringInterface();
OMonitoringInterface( ONetworkInterface* );
virtual ~OMonitoringInterface();
public:
virtual void setEnabled( bool );
virtual bool enabled() const;
virtual void setChannel( int );
virtual QString name() const = 0;
protected:
- const OWirelessNetworkInterface* _if;
+ OWirelessNetworkInterface* _if;
};
/*======================================================================================
* OCiscoMonitoring
*======================================================================================*/
class OCiscoMonitoringInterface : public OMonitoringInterface
{
public:
OCiscoMonitoringInterface( ONetworkInterface* );
virtual ~OCiscoMonitoringInterface();
virtual void setEnabled( bool );
virtual QString name() const;
virtual void setChannel( int );
};
/*======================================================================================
* OWlanNGMonitoringInterface
*======================================================================================*/
class OWlanNGMonitoringInterface : public OMonitoringInterface
{
public:
OWlanNGMonitoringInterface( ONetworkInterface* );
virtual ~OWlanNGMonitoringInterface();
public:
virtual void setEnabled( bool );
virtual QString name() const;
virtual void setChannel( int );
};
/*======================================================================================
* OHostAPMonitoringInterface
*======================================================================================*/
class OHostAPMonitoringInterface : public OMonitoringInterface
{
public:
OHostAPMonitoringInterface( ONetworkInterface* );
virtual ~OHostAPMonitoringInterface();