-rw-r--r-- | libopie2/examples/opienet/onetworkdemo/onetworkdemo.cpp | 21 | ||||
-rw-r--r-- | libopie2/opienet/onetwork.cpp | 51 | ||||
-rw-r--r-- | libopie2/opienet/onetwork.h | 58 |
3 files changed, 105 insertions, 25 deletions
diff --git a/libopie2/examples/opienet/onetworkdemo/onetworkdemo.cpp b/libopie2/examples/opienet/onetworkdemo/onetworkdemo.cpp index f801b15..fd68772 100644 --- a/libopie2/examples/opienet/onetworkdemo/onetworkdemo.cpp +++ b/libopie2/examples/opienet/onetworkdemo/onetworkdemo.cpp @@ -1,62 +1,75 @@ #include <opie2/onetwork.h> int main( int argc, char** argv ) { qDebug( "OPIE Network Demo" ); ONetwork* net = ONetwork::instance(); ONetwork::InterfaceIterator it = net->iterator(); while ( it.current() ) { qDebug( "DEMO: ONetwork contains Interface '%s'", (const char*) it.current()->name() ); qDebug( "DEMO: MAC Address is '%s'", (const char*) it.current()->macAddress().toString() ); qDebug( "DEMO: MAC Address is '%s'", (const char*) it.current()->macAddress().toString(true) ); qDebug( "Demo: IPv4 Address is '%s'", (const char*) it.current()->ipV4Address() ); if ( it.current()->isWireless() ) { OWirelessNetworkInterface* iface = static_cast<OWirelessNetworkInterface*>( it.current() ); qDebug( "DEMO: '%s' seems to feature the wireless extensions.", (const char*) iface->name() ); qDebug( "DEMO: Current SSID is '%s'", (const char*) iface->SSID() ); - qDebug( "DEMO: Current NickName is '%s'", (const char*) iface->nickName() ); qDebug( "DEMO: Antenna is tuned to '%f', that is channel %d", iface->frequency(), iface->channel() ); //if ( iface->mode() == OWirelessNetworkInterface::adhoc ) //{ qDebug( "DEMO: Associated AP has MAC Address '%s'", (const char*) iface->associatedAP() ); //} - // try to set monitor mode + // nickname + qDebug( "DEMO: Current NickName is '%s'", (const char*) iface->nickName() ); + iface->setNickName( "MyNickName" ); + if ( iface->nickName() != "MyNickName" ) + qDebug( "DEMO: Warning! Can't change nickname" ); + else + qDebug( "DEMO: Nickname change successful." ); + + // operation mode + qDebug( "DEMO: Current OperationMode is '%s'", (const char*) iface->mode() ); + iface->setMode( "adhoc" ); + if ( iface->mode() != "adhoc" ) + qDebug( "DEMO: Warning! Can't change operation mode" ); + else + qDebug( "DEMO: Operation Mode change successful." ); + + iface->setMode( "managed" ); /* // first some wrong calls to check if this is working iface->setPrivate( "seppel", 10 ); iface->setPrivate( "monitor", 0 ); // now the real deal iface->setPrivate( "monitor", 2, 2, 3 ); - */ - // trying to set hw address to 12:34:56:AB:CD:EF /* OMacAddress addr = OMacAddress::fromString( "12:34:56:AB:CD:EF" ); iface->setUp( false ); iface->setMacAddress( addr ); iface->setUp( true ); qDebug( "DEMO: MAC Address now is '%s'", (const char*) iface->macAddress().toString() ); */ } ++it; } return 0; } diff --git a/libopie2/opienet/onetwork.cpp b/libopie2/opienet/onetwork.cpp index 95e7043..2dfff1d 100644 --- a/libopie2/opienet/onetwork.cpp +++ b/libopie2/opienet/onetwork.cpp @@ -21,48 +21,49 @@ _.=:. : :=>`: 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. */ /* OPIE */ #include <opie2/onetwork.h> /* QT */ #include <qfile.h> #include <qtextstream.h> /* UNIX */ +#include <assert.h> #include <arpa/inet.h> #include <cerrno> #include <cstring> #include <cstdlib> #include <math.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <linux/sockios.h> #include <net/if_arp.h> #include <stdarg.h> using namespace std; /*====================================================================================== * ONetwork *======================================================================================*/ ONetwork* ONetwork::_instance = 0; ONetwork::ONetwork() { qDebug( "ONetwork::ONetwork()" ); @@ -618,65 +619,115 @@ int OWirelessNetworkInterface::channels() const } void OWirelessNetworkInterface::setChannelHopping( int interval ) { if ( !_hopper ) _hopper = new OChannelHopper( this ); _hopper->setInterval( interval ); //FIXME: When and by whom will the channel hopper be deleted? //TODO: rely on QObject hierarchy } int OWirelessNetworkInterface::channelHopping() const { return _hopper->interval(); } OChannelHopper* OWirelessNetworkInterface::channelHopper() const { return _hopper; } +void OWirelessNetworkInterface::setMode( const QString& mode ) +{ + if ( mode == "auto" ) _iwr.u.mode = IW_MODE_AUTO; + else if ( mode == "adhoc" ) _iwr.u.mode = IW_MODE_ADHOC; + else if ( mode == "managed" ) _iwr.u.mode = IW_MODE_INFRA; + else if ( mode == "master" ) _iwr.u.mode = IW_MODE_MASTER; + else if ( mode == "repeater" ) _iwr.u.mode = IW_MODE_REPEAT; + else if ( mode == "secondary" ) _iwr.u.mode = IW_MODE_SECOND; + #if WIRELESS_EXT > 14 + else if ( mode == "monitor" ) _iwr.u.mode = IW_MODE_MONITOR; + #endif + else + { + qDebug( "ONetwork: Warning! Invalid IEEE 802.11 mode '%s' specified.", (const char*) mode ); + return; + } + wioctl( SIOCSIWMODE ); +} + + +QString OWirelessNetworkInterface::mode() const +{ + if ( !wioctl( SIOCGIWMODE ) ) + { + return "<unknown>"; + } + switch ( _iwr.u.mode ) + { + case IW_MODE_AUTO: return "auto"; + case IW_MODE_ADHOC: return "adhoc"; + case IW_MODE_INFRA: return "managed"; + case IW_MODE_MASTER: return "master"; + case IW_MODE_REPEAT: return "repeater"; + case IW_MODE_SECOND: return "secondary"; + #if WIRELESS_EXT > 14 + case IW_MODE_MONITOR: return "monitor"; + #endif + default: assert( 0 ); // shouldn't happen + } +} + + void OWirelessNetworkInterface::setMonitorMode( bool b ) { if ( _mon ) _mon->setEnabled( b ); else qDebug( "ONetwork(): can't switch monitor mode without installed monitoring interface" ); } bool OWirelessNetworkInterface::monitorMode() const { qDebug( "dataLinkType = %d", dataLinkType() ); return ( dataLinkType() == ARPHRD_IEEE80211 || dataLinkType() == 802 ); // 802 is the header type for PRISM - Linux support for this is pending... } +void OWirelessNetworkInterface::setNickName( const QString& nickname ) +{ + _iwr.u.essid.pointer = const_cast<char*>( (const char*) nickname ); + _iwr.u.essid.length = nickname.length(); + wioctl( SIOCSIWNICKN ); +} + + 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; diff --git a/libopie2/opienet/onetwork.h b/libopie2/opienet/onetwork.h index f052317..a29b29d 100644 --- a/libopie2/opienet/onetwork.h +++ b/libopie2/opienet/onetwork.h @@ -13,60 +13,60 @@ .="- .-=="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 ONETWORK_H #define ONETWORK_H +/* OPIE */ + +#include <opie2/onetutils.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 need a user header for the Wireless Extensions, something like <net/wireless.h> // ML: I will drop Jean a mail on that subject #include <net/if.h> #define _LINUX_IF_H #include <linux/wireless.h> class ONetworkInterface; class OWirelessNetworkInterface; class OChannelHopper; class OMonitoringInterface; /*====================================================================================== * ONetwork @@ -79,54 +79,54 @@ class OMonitoringInterface; * @author Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> */ class ONetwork : public QObject { Q_OBJECT public: typedef QDict<ONetworkInterface> InterfaceMap; typedef QDictIterator<ONetworkInterface> InterfaceIterator; public: /** * @returns the number of available interfaces */ int count() const; /** * @returns a pointer to the (one and only) @ref ONetwork instance. */ static ONetwork* instance(); /** * @returns an iterator usable for iterating through all network interfaces. */ InterfaceIterator iterator() const; /** - * @returns true, if the @p interface supports the wireless extension protocol. + * @returns true, if the @a interface supports the wireless extension protocol. */ // FIXME QString? -zecke bool isWirelessInterface( const char* interface ) const; /** - * @returns a pointer to the @ref ONetworkInterface object for the specified @p interface or 0, if not found + * @returns a pointer to the @ref ONetworkInterface object for the specified @a interface or 0, if not found * @see ONetworkInterface */ // FIXME: const QString& is prefered over QString!!! -zecke ONetworkInterface* interface( const QString& interface ) const; /** * @internal Rebuild the internal interface database * @note Sometimes it might be useful to call this from client code, * e.g. after issuing a cardctl insert */ void synchronize(); protected: ONetwork(); private: static ONetwork* _instance; InterfaceMap _interfaces; }; /*====================================================================================== * ONetworkInterface *======================================================================================*/ @@ -177,67 +177,67 @@ class ONetworkInterface : public QObject bool promiscuousMode() const; /** * Setting an interface to up enables it to receive packets. */ bool setUp( bool ); /** * @returns true if the interface is up. */ bool isUp() const; /** * @returns true if the interface is a loopback interface. */ bool isLoopback() const; /** * @returns true if the interface is featuring supports the wireless extension protocol. */ bool isWireless() const; /** * Associate the IP address @ addr with the interface. */ void setIPV4Address( const QHostAddress& addr ); /** * @returns the IPv4 address associated with the interface. */ - QString ipV4Address() const; + QString ipV4Address() const; //TODO: make this return an OHostAddress /** * Associate the MAC address @a addr with the interface. * @note It can be necessary to shut down the interface prior to calling this method. * @warning This is not supported by all drivers. */ void setMacAddress( const OMacAddress& addr ); /** * @returns the MAC address associated with the interface. */ OMacAddress macAddress() const; /** * Associate the IPv4 @a netmask with the interface. */ void setIPV4Netmask( const QHostAddress& netmask ); /** * @returns the IPv4 netmask associated with the interface. */ - QString ipV4Netmask() const; + QString ipV4Netmask() const; //TODO: make this return an OHostAddress /** * @returns the data link type currently associated with the interface. * @see #include <net/if_arp.h> for possible values. */ 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 *======================================================================================*/ /** * @brief A radio frequency channel hopper. @@ -296,134 +296,150 @@ class OChannelHopper : public QObject QValueList<int>::Iterator _channel; }; /*====================================================================================== * OWirelessNetworkInterface *======================================================================================*/ /** * @brief A network interface wrapper for interfaces supporting the wireless extensions protocol. * * This class provides a high-level encapsulation of the Linux wireless extension API. */ 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 }; - /** * Constructor. */ OWirelessNetworkInterface( QObject* parent, const char* name ); /** * Destructor. */ virtual ~OWirelessNetworkInterface(); /** * Setting the @a channel of the interface changes the radio frequency (RF) * of the corresponding wireless network device. */ virtual void setChannel( int channel ) const; /** * @returns the channel index of the current radio frequency. */ virtual int channel() const; /** * @returns the current radio frequency (in MHz). */ virtual double frequency() const; /** * @returns the number of radio frequency channels for the * corresponding wireless network device. * @note European devices usually have 14 channels, while American typically feature 11 channels. */ virtual int channels() const; - //virtual double frequency(int) const; - - virtual void setMode( Mode ) {}; //FIXME: Implement and document this - virtual bool mode() const {}; //FIXME: Implement and document this - + /** + * Set the IEEE 802.11 operation @a mode. + * Valid values are <ul><li>adhoc<li>managed<li>monitor<li>master + * @warning Not all drivers support the all modes. + * @note You might have to change the SSID to get the operation mode change into effect. + */ + virtual void setMode( const QString& mode ); + /** + * @returns the current IEEE 802.11 operation mode. + * Possible values are <ul><li>adhoc<li>managed<li>monitor<li>master or <li>unknown + */ + virtual QString mode() const; /** * Setting the monitor mode on a wireless network interface enables * listening to IEEE 802.11 data and management frames which normally * are handled by the device firmware. This can be used to detect * other wireless network devices, e.g. Access Points or Ad-hoc stations. * @warning Standard wireless network drives don't support the monitor mode. * @warning You need a patched driver for this to work. * @note Enabling the monitor mode is highly driver dependent and requires * the proper @ref OMonitoringInterface to be associated with the interface. * @see OMonitoringInterface */ - virtual void setMonitorMode( bool ); + virtual void setMonitorMode( bool ); //FIXME: ==> setMode( "monitor" ); /** * @returns true if the device is listening in IEEE 802.11 monitor mode */ - virtual bool monitorMode() const; + virtual bool monitorMode() const; //FIXME: ==> mode() /** * Set the channel hopping @a interval. An @a interval of 0 disables channel hopping. * @see OChannelHopper */ virtual void setChannelHopping( int interval = 0 ); /** * @returns the channel hopping interval or 0, if channel hopping is disabled. */ virtual int channelHopping() const; /** * @returns the @ref OChannelHopper of this interface or 0, if channel hopping has not been activated before */ virtual OChannelHopper* channelHopper() const; /** * Set the station @a nickname. */ - virtual void setNickName( const QString& nickname ) {}; //FIXME: Implement this + virtual void setNickName( const QString& nickname ); /** * @returns the current station nickname. */ virtual QString nickName() const; /** * Invoke the private IOCTL @a command with a @number of parameters on the network interface. * @see OPrivateIOCTL */ virtual void setPrivate( const QString& command, int number, ... ); /** * @returns true if the interface is featuring the private IOCTL @command. */ virtual bool hasPrivate( const QString& command ); virtual void getPrivate( const QString& command ); //FIXME: Implement and document this virtual bool isAssociated() const {}; //FIXME: Implement and document this - virtual QString associatedAP() const; //FIXME: Implement and document this - - virtual void setSSID( const QString& ); + /** + * @returns the MAC address of the Access Point if the + * device is in infrastructure mode. @returns a (more or less random) CELL + * address if the device is in adhoc mode. + */ + virtual QString associatedAP() const; + /** + * Set the @a ssid (Service Set ID) string. This is used to decide + * which network to associate with (use "any" to let the driver decide). + */ + virtual void setSSID( const QString& ssid ); + /** + * @returns the current SSID (Service Set ID). + */ 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 *======================================================================================*/ |