Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

onetutils.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003               =.             (C) 2003-2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
00004             .=l.
00005            .>+-=
00006  _;:,     .>    :=|.         This program is free software; you can
00007 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00008 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00009 .="- .-=="i,     .._         License as published by the Free Software
00010  - .   .-<_>     .<>         Foundation; version 2 of the License.
00011      ._= =}       :           
00012     .%`+i>       _;_.
00013     .i_,=:_.      -<s.       This program is distributed in the hope that
00014      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00015     : ..    .:,     . . .    without even the implied warranty of
00016     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00018 ..}^=.=       =       ;      Library General Public License for more
00019 ++=   -.     .`     .:       details.
00020  :     =  ...= . :.=-
00021  -.   .:....=;==+<;          You should have received a copy of the GNU
00022   -_. . .   )=.  =           Library General Public License along with
00023     --        :-=`           this library; see the file COPYING.LIB.
00024                              If not, write to the Free Software Foundation,
00025                              Inc., 59 Temple Place - Suite 330,
00026                              Boston, MA 02111-1307, USA.
00027 
00028 */
00029 
00030 #include <opie2/onetutils.h>
00031 #include <opie2/onetwork.h>
00032 #include <opie2/omanufacturerdb.h>
00033 
00034 #include <net/if.h>
00035 #include <assert.h>
00036 #include <stdio.h>
00037 
00038 namespace Opie {
00039 namespace Net  {
00040 
00041 /*======================================================================================
00042  * OMacAddress
00043  *======================================================================================*/
00044 
00045 // static initializer for broadcast and unknown MAC Adresses
00046 const unsigned char __broadcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
00047 const OMacAddress& OMacAddress::broadcast = OMacAddress( __broadcast );
00048 const unsigned char __unknown[6] = { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 };
00049 const OMacAddress& OMacAddress::unknown = OMacAddress( __unknown );
00050 
00051 //TODO: Incorporate Ethernet Manufacturer database here! (inline or so)
00052 
00053 OMacAddress::OMacAddress()
00054 {
00055     memcpy( _bytes, __unknown, 6 );
00056 }
00057 
00058 
00059 OMacAddress::OMacAddress( unsigned char* p )
00060 {
00061     memcpy( _bytes, p, 6 );
00062 }
00063 
00064 
00065 OMacAddress::OMacAddress( const unsigned char* p )
00066 {
00067     memcpy( _bytes, p, 6 );
00068 }
00069 
00070 
00071 OMacAddress::OMacAddress( struct ifreq& ifr )
00072 {
00073     memcpy( _bytes, ifr.ifr_hwaddr.sa_data, 6 );
00074 }
00075 
00076 
00077 OMacAddress::~OMacAddress()
00078 {
00079 }
00080 
00081 
00082 //#ifdef QT_NO_DEBUG
00083 //inline
00084 //#endif
00085 const unsigned char* OMacAddress::native() const
00086 {
00087     return (const unsigned char*) &_bytes;
00088 }
00089 
00090 
00091 OMacAddress OMacAddress::fromString( const QString& str )
00092 {
00093     QString addr( str );
00094     unsigned char buf[6];
00095     bool ok = true;
00096     int index = 14;
00097     for ( int i = 5; i >= 0; --i )
00098     {
00099         buf[i] = addr.right( 2 ).toUShort( &ok, 16 );
00100         if ( !ok ) return OMacAddress::unknown;
00101         addr.truncate( index );
00102         index -= 3;
00103     }
00104     return (const unsigned char*) &buf;
00105 }
00106 
00107 
00108 QString OMacAddress::toString( bool substitute ) const
00109 {
00110     QString manu;
00111     manu.sprintf( "%.2X:%.2X:%.2X", _bytes[0]&0xff, _bytes[1]&0xff, _bytes[2]&0xff );
00112     QString serial;
00113     serial.sprintf( ":%.2X:%.2X:%.2X", _bytes[3]&0xff, _bytes[4]&0xff, _bytes[5]&0xff );
00114     if ( !substitute ) return manu+serial;
00115     // fallback - if no vendor is found, just use the number
00116     QString textmanu = OManufacturerDB::instance()->lookup( manu );
00117     return textmanu.isNull() ? manu+serial : textmanu+serial;
00118 }
00119 
00120 
00121 QString OMacAddress::manufacturer() const
00122 {
00123     return OManufacturerDB::instance()->lookupExt( toString() );
00124 }
00125 
00126 
00127 bool operator==( const OMacAddress &m1, const OMacAddress &m2 )
00128 {
00129     return memcmp( &m1._bytes, &m2._bytes, 6 ) == 0;
00130 }
00131 
00132 
00133 /*======================================================================================
00134  * OHostAddress
00135  *======================================================================================*/
00136 
00137 OHostAddress::OHostAddress()
00138              :QHostAddress()
00139 {
00140 }
00141 
00142 
00143 OHostAddress::OHostAddress( Q_UINT32 ip4Addr )
00144              :QHostAddress( ip4Addr )
00145 {
00146 }
00147 
00148 
00149 OHostAddress::~OHostAddress()
00150 {
00151 }
00152 
00153 /*======================================================================================
00154  * OPrivateIOCTL
00155  *======================================================================================*/
00156 
00157 OPrivateIOCTL::OPrivateIOCTL( QObject* parent, const char* name, int cmd, int getargs, int setargs )
00158               :QObject( parent, name ), _ioctl( cmd ), _getargs( getargs ), _setargs( setargs )
00159 {
00160 }
00161 
00162 
00163 OPrivateIOCTL::~OPrivateIOCTL()
00164 {
00165 }
00166 
00167 
00168 int OPrivateIOCTL::numberGetArgs() const
00169 {
00170     return _getargs & IW_PRIV_SIZE_MASK;
00171 }
00172 
00173 
00174 int OPrivateIOCTL::typeGetArgs() const
00175 {
00176     return _getargs & IW_PRIV_TYPE_MASK >> 12;
00177 }
00178 
00179 
00180 int OPrivateIOCTL::numberSetArgs() const
00181 {
00182     return _setargs & IW_PRIV_SIZE_MASK;
00183 }
00184 
00185 
00186 int OPrivateIOCTL::typeSetArgs() const
00187 {
00188     return _setargs & IW_PRIV_TYPE_MASK >> 12;
00189 }
00190 
00191 
00192 void OPrivateIOCTL::invoke() const
00193 {
00194     ( (OWirelessNetworkInterface*) parent() )->wioctl( _ioctl );
00195 }
00196 
00197 
00198 void OPrivateIOCTL::setParameter( int num, u_int32_t value )
00199 {
00200     u_int32_t* arglist = (u_int32_t*) &( (OWirelessNetworkInterface*) parent() )->_iwr.u.name;
00201     arglist[num] = value;
00202 }
00203 
00204 
00205 
00206 namespace Internal {
00207 /*======================================================================================
00208  * assorted functions
00209  *======================================================================================*/
00210 
00211 void dumpBytes( const unsigned char* data, int num )
00212 {
00213     printf( "Dumping %d bytes @ 0x%p", num, data );
00214     printf( "-------------------------------------------\n" );
00215 
00216     for ( int i = 0; i < num; ++i )
00217     {
00218         printf( "%02x ", data[i] );
00219         if ( !((i+1) % 32) ) printf( "\n" );
00220     }
00221     printf( "\n\n" );
00222 }
00223 
00224 
00225 int stringToMode( const QString& mode )
00226 {
00227     if ( mode == "auto" )            return IW_MODE_AUTO;
00228     else if ( mode == "adhoc" )      return IW_MODE_ADHOC;
00229     else if ( mode == "managed" )    return IW_MODE_INFRA;
00230     else if ( mode == "master" )     return IW_MODE_MASTER;
00231     else if ( mode == "repeater" )   return IW_MODE_REPEAT;
00232     else if ( mode == "secondary" )  return IW_MODE_SECOND;
00233     else if ( mode == "monitor" )    return IW_MODE_MONITOR;
00234     else assert( 0 );
00235 }
00236 
00237 
00238 QString modeToString( int mode )
00239 {
00240     switch ( mode )
00241     {
00242         case IW_MODE_AUTO:          return "auto";
00243         case IW_MODE_ADHOC:         return "adhoc";
00244         case IW_MODE_INFRA:         return "managed";
00245         case IW_MODE_MASTER:        return "master";
00246         case IW_MODE_REPEAT:        return "repeater";
00247         case IW_MODE_SECOND:        return "second";
00248         case IW_MODE_MONITOR:       return "monitor";
00249         default: assert( 0 );
00250     }
00251 }
00252 }
00253 }
00254 }

Generated on Sat Nov 5 16:16:12 2005 for OPIE by  doxygen 1.4.2