-rw-r--r-- | libopie2/opienet/onetwork.cpp | 39 | ||||
-rw-r--r-- | libopie2/opienet/onetwork.h | 49 |
2 files changed, 74 insertions, 14 deletions
diff --git a/libopie2/opienet/onetwork.cpp b/libopie2/opienet/onetwork.cpp index f0094c7..be2736a 100644 --- a/libopie2/opienet/onetwork.cpp +++ b/libopie2/opienet/onetwork.cpp @@ -87,48 +87,54 @@ void ONetwork::synchronize() QTextStream s( &f ); s.readLine(); s.readLine(); while ( !s.atEnd() ) { s >> str; str.truncate( str.find( ':' ) ); qDebug( "ONetwork: found interface '%s'", (const char*) str ); ONetworkInterface* iface; if ( isWirelessInterface( str ) ) { iface = new OWirelessNetworkInterface( this, (const char*) str ); qDebug( "ONetwork: interface '%s' has Wireless Extensions", (const char*) str ); } else { iface = new ONetworkInterface( this, (const char*) str ); } _interfaces.insert( str, iface ); s.readLine(); } } +int ONetwork::count() const +{ + return _interfaces.count(); +} + + ONetworkInterface* ONetwork::interface( const QString& iface ) const { return _interfaces[iface]; } ONetwork* ONetwork::instance() { if ( !_instance ) _instance = new ONetwork(); return _instance; } ONetwork::InterfaceIterator ONetwork::iterator() const { return ONetwork::InterfaceIterator( _interfaces ); } bool ONetwork::isWirelessInterface( const char* name ) const { int sfd = socket( AF_INET, SOCK_STREAM, 0 ); struct iwreq iwr; memset( &iwr, 0, sizeof( struct iwreq ) ); @@ -190,82 +196,115 @@ bool ONetworkInterface::ioctl( int call ) const 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; } +void ONetworkInterface::setIPV4Address( const QHostAddress& addr ) +{ + struct sockaddr_in *sa = (struct sockaddr_in *) &_ifr.ifr_addr; + sa->sin_family = AF_INET; + sa->sin_port = 0; + sa->sin_addr.s_addr = htonl( addr.ip4Addr() ); + ioctl( SIOCSIFADDR ); +} + + 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; } } +void ONetworkInterface::setIPV4Netmask( const QHostAddress& addr ) +{ + struct sockaddr_in *sa = (struct sockaddr_in *) &_ifr.ifr_addr; + sa->sin_family = AF_INET; + sa->sin_port = 0; + sa->sin_addr.s_addr = htonl( addr.ip4Addr() ); + ioctl( SIOCSIFNETMASK ); +} + + +QString ONetworkInterface::ipV4Netmask() const +{ + if ( ioctl( SIOCGIFNETMASK ) ) + { + struct sockaddr_in* sa = (struct sockaddr_in *) &_ifr.ifr_addr; + //FIXME: Use QHostAddress here + return QString( inet_ntoa( sa->sin_addr ) ); + } + else + return "<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; } diff --git a/libopie2/opienet/onetwork.h b/libopie2/opienet/onetwork.h index db8e702..2348bbc 100644 --- a/libopie2/opienet/onetwork.h +++ b/libopie2/opienet/onetwork.h @@ -35,102 +35,111 @@ #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 +// 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 *======================================================================================*/ /** - * @brief A container class for all network devices. + * @brief A container class for all network interfaces * - * This class provides access to all available network devices of your computer. + * This class provides access to all available network interfaces of your computer. * @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. */ // 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 * @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 cardctl insert + */ + void synchronize(); protected: ONetwork(); - void synchronize(); private: static ONetwork* _instance; InterfaceMap _interfaces; }; /*====================================================================================== * ONetworkInterface *======================================================================================*/ /** * @brief A network interface wrapper. * * This class provides a wrapper for a network interface. All the cumbersume details of * Linux ioctls are hidden under a convenient high-level interface. * @warning Most of the setting methods contained in this class require the appropriate * process permissions to work. * @author Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> */ class ONetworkInterface : public QObject { friend class OMonitoringInterface; friend class OCiscoMonitoringInterface; @@ -153,72 +162,84 @@ class ONetworkInterface : public QObject * @note This is currently only useful with @ref OWirelessNetworkInterface objects. */ void setMonitoring( OMonitoringInterface* monitoring ); /** * @returns the currently associated monitoring interface or 0, if no monitoring is associated. */ OMonitoringInterface* monitoring() const; /** * Setting an interface to promiscuous mode enables the device to receive * all packets on the shared medium - as opposed to packets which are addressed to this interface. */ bool setPromiscuousMode( bool ); /** * @returns true if the interface is set to promiscuous mode. */ 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; - /* - * @returns the IPv4 address associated with this interface. + /** + * Associate the IP address @ addr with the interface. + */ + void setIPV4Address( const QHostAddress& addr ); + /** + * @returns the IPv4 address associated with the interface. */ QString ipV4Address() const; - /* + /** * 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 this interface. + /** + * @returns the MAC address associated with the interface. */ OMacAddress macAddress() const; - /* - * @returns the data link type currently associated with this interface. + /** + * Associate the IPv4 @a netmask with the interface. + */ + void setIPV4Netmask( const QHostAddress& netmask ); + /** + * @returns the IPv4 netmask associated with the interface. + */ + QString ipV4Netmask() const; + /** + * @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. * * This class provides a channel hopper for radio frequencies. A channel hopper frequently |