00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00043
00044
00045
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
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
00083
00084
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
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
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
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
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 }