00001
00002
00003
00004
00005
00006
00007 #ifndef MCFTPID_H
00008 #define MCFTPID_H
00009
00010 #ifdef USE_STD
00011 #include <iostream>
00012 #include <string>
00013 using namespace std;
00014 #else
00015 #include <iostream.h>
00016 #include <string.h>
00017 #endif
00018
00019 #include "utility.h"
00020 #include "BufferBase.h"
00021
00022 struct MCFTPid
00023 {
00024 public:
00025
00026 MCFTPid();
00027 MCFTPid( const MCFTPid& );
00028 MCFTPid(
00029 unsigned int dst_ip, unsigned int dst_port,
00030 unsigned int src_ip, unsigned int src_port,
00031 unsigned int mssgnum
00032 );
00033
00034 MCFTPid& operator () ();
00035 MCFTPid& operator () ( const MCFTPid& );
00036 MCFTPid& operator () (
00037 unsigned int dst_ip, unsigned int dst_port,
00038 unsigned int src_ip, unsigned int src_port,
00039 unsigned int mssgnum
00040 );
00041
00042 MCFTPid& copy( const MCFTPid id );
00043 MCFTPid& operator = ( const MCFTPid& rhs );
00044
00045 bool is_equal( const MCFTPid& rhs );
00046 bool operator == ( const MCFTPid& rhs );
00047 bool operator != ( const MCFTPid& rhs );
00048
00049 void write( BufferBase& );
00050 MCFTPid& operator << ( BufferBase& );
00051 void read( BufferBase& );
00052 MCFTPid& operator >> ( BufferBase& );
00053
00054 void write( istream& );
00055
00056 void read( ostream& );
00057
00058
00059 void set_dst_ip( unsigned int dst_ip );
00060 void set_dst_ip(
00061 unsigned char ip3,
00062 unsigned char ip2,
00063 unsigned char ip1,
00064 unsigned char ip0
00065 );
00066 void set_dst_port( unsigned int dst_port );
00067 void set_src_ip( unsigned int src_ip );
00068 void set_src_ip(
00069 unsigned char ip3,
00070 unsigned char ip2,
00071 unsigned char ip1,
00072 unsigned char ip0
00073 );
00074 void set_src_port( unsigned int src_port );
00075 void set_mssgnum( unsigned int mssgnum );
00076
00077 unsigned int dst_ip();
00078 unsigned char dst_ip( int b );
00079 unsigned int dst_port();
00080 unsigned int src_ip();
00081 unsigned char src_ip( int b );
00082 unsigned int src_port();
00083 unsigned int mssgnum();
00084
00085 string hex();
00086 string filename();
00087
00088 void report( ostream& = cout );
00089
00090 private:
00091 unsigned int _dst_ip;
00092 unsigned int _dst_port;
00093 unsigned int _src_ip;
00094 unsigned int _src_port;
00095 unsigned int _mssgnum;
00096
00097 friend BufferBase& operator >> ( BufferBase&, MCFTPid& );
00098 friend BufferBase& operator << ( BufferBase&, MCFTPid& );
00099
00100 friend istream& operator >> ( istream&, MCFTPid& );
00101 friend ostream& operator << ( ostream&, MCFTPid& );
00102
00103 };
00104
00105 inline
00106 MCFTPid::MCFTPid()
00107 : _dst_ip(0), _dst_port(0),
00108 _src_ip(0), _src_port(0),
00109 _mssgnum(0)
00110 {}
00111
00112 inline
00113 MCFTPid::MCFTPid( const MCFTPid& id )
00114 : _dst_ip(id._dst_ip), _dst_port(id._dst_port),
00115 _src_ip(id._src_ip), _src_port(id._src_port),
00116 _mssgnum(id._mssgnum)
00117 {}
00118
00119 inline
00120 MCFTPid::MCFTPid(
00121 unsigned int dst_ip, unsigned int dst_port,
00122 unsigned int src_ip, unsigned int src_port,
00123 unsigned int mssgnum
00124 ) : _dst_ip(dst_ip), _dst_port(dst_port),
00125 _src_ip(src_ip), _src_port(src_port),
00126 _mssgnum(mssgnum)
00127 {}
00128
00129 inline
00130 MCFTPid& MCFTPid::operator () ()
00131 {
00132 _dst_ip = 0;
00133 _dst_port = 0;
00134 _src_ip = 0;
00135 _src_port = 0;
00136 _mssgnum = 0;
00137 return *this;
00138 }
00139
00140 inline
00141 MCFTPid& MCFTPid::operator () ( const MCFTPid& id )
00142 {
00143 _dst_ip = id._dst_ip;
00144 _dst_port = id._dst_port;
00145 _src_ip = id._src_ip;
00146 _src_port = id._src_port;
00147 _mssgnum = id._mssgnum;
00148 return *this;
00149 }
00150
00151 inline
00152 MCFTPid& MCFTPid::operator() (
00153 unsigned int dst_ip, unsigned int dst_port,
00154 unsigned int src_ip, unsigned int src_port,
00155 unsigned int mssgnum
00156 )
00157 {
00158 _dst_ip = dst_ip;
00159 _dst_port = dst_port;
00160 _src_ip = src_ip;
00161 _src_port = src_port;
00162 _mssgnum = mssgnum;
00163 return *this;
00164 }
00165
00166 inline
00167 MCFTPid& MCFTPid::copy( const MCFTPid id )
00168 {
00169 _dst_ip = id._dst_ip;
00170 _dst_port = id._dst_port;
00171 _src_ip = id._src_ip;
00172 _src_port = id._src_port;
00173 _mssgnum = id._mssgnum;
00174 return *this;
00175 }
00176
00177 inline
00178 MCFTPid& MCFTPid::operator = ( const MCFTPid& rhs )
00179 {
00180 return copy(rhs);
00181 }
00182
00183 inline
00184 bool MCFTPid::is_equal( const MCFTPid& id )
00185 {
00186 if ( _dst_ip != id._dst_ip ) return false;
00187 if ( _dst_port != id._dst_port ) return false;
00188 if ( _src_ip != id._src_ip ) return false;
00189 if ( _src_port != id._src_port ) return false;
00190 if ( _mssgnum != id._mssgnum ) return false;
00191 return true;
00192 }
00193
00194 inline
00195 bool MCFTPid::operator == ( const MCFTPid& rhs )
00196 {
00197 return is_equal(rhs);
00198 }
00199
00200 inline
00201 bool MCFTPid::operator != ( const MCFTPid& rhs )
00202 {
00203 return !is_equal(rhs);
00204 }
00205
00206 inline
00207 void MCFTPid::write( BufferBase& buffer )
00208 {
00209 bytepack bp;
00210 buffer >> bp( 0, 4 ); _dst_ip = int(bp);
00211 buffer >> bp( 0, 2 ); _dst_port = int(bp);
00212 buffer >> bp( 0, 4 ); _src_ip = int(bp);
00213 buffer >> bp( 0, 2 ); _src_port = int(bp);
00214 buffer >> bp( 0, 4 ); _mssgnum = int(bp);
00215 }
00216
00217 inline
00218 MCFTPid& MCFTPid::operator << ( BufferBase& buffer )
00219 {
00220 write(buffer);
00221 return *this;
00222 }
00223
00224 inline
00225 void MCFTPid::read( BufferBase& buffer )
00226 {
00227 bytepack bp;
00228 buffer << bp( int(_dst_ip), 4 );
00229 buffer << bp( int(_dst_port), 2 );
00230 buffer << bp( int(_src_ip), 4 );
00231 buffer << bp( int(_src_port), 2 );
00232 buffer << bp( int(_mssgnum), 4 );
00233 }
00234
00235 inline
00236 MCFTPid& MCFTPid::operator >> ( BufferBase& buffer )
00237 {
00238 read(buffer);
00239 return *this;
00240 }
00241
00242 inline
00243 void MCFTPid::write( istream& is )
00244 {
00245 bytepack bp;
00246 is >> bp( 0, 4 ); _dst_ip = int(bp);
00247 is >> bp( 0, 2 ); _dst_port = int(bp);
00248 is >> bp( 0, 4 ); _src_ip = int(bp);
00249 is >> bp( 0, 2 ); _src_port = int(bp);
00250 is >> bp( 0, 4 ); _mssgnum = int(bp);
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260 inline
00261 void MCFTPid::read( ostream& os )
00262 {
00263 bytepack bp;
00264 os << bp( int(_dst_ip), 4 );
00265 os << bp( int(_dst_port), 2 );
00266 os << bp( int(_src_ip), 4 );
00267 os << bp( int(_src_port), 2 );
00268 os << bp( int(_mssgnum), 4 );
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278 inline
00279 void MCFTPid::set_dst_ip( unsigned int dst_ip ) { _dst_ip = dst_ip; }
00280
00281 inline
00282 void MCFTPid::set_dst_ip(
00283 unsigned char ip3, unsigned char ip2, unsigned char ip1, unsigned char ip0
00284 )
00285 {
00286 _dst_ip = ip3;
00287 _dst_ip = _dst_ip*256 + ip2;
00288 _dst_ip = _dst_ip*256 + ip1;
00289 _dst_ip = _dst_ip*256 + ip0;
00290 }
00291
00292 inline
00293 void MCFTPid::set_dst_port( unsigned int dst_port ) { _dst_port = dst_port; }
00294
00295 inline
00296 void MCFTPid::set_src_ip( unsigned int src_ip ) { _src_ip = src_ip; }
00297
00298 inline
00299 void MCFTPid::set_src_ip(
00300 unsigned char ip3, unsigned char ip2, unsigned char ip1, unsigned char ip0
00301 )
00302 {
00303 _src_ip = ip3;
00304 _src_ip = _src_ip*256 + ip2;
00305 _src_ip = _src_ip*256 + ip1;
00306 _src_ip = _src_ip*256 + ip0;
00307 }
00308
00309 inline
00310 void MCFTPid::set_src_port( unsigned int src_port ) { _src_port = src_port; }
00311
00312 inline
00313 void MCFTPid::set_mssgnum( unsigned int mssgnum ) { _mssgnum = mssgnum; }
00314
00315 inline
00316 unsigned int MCFTPid::dst_ip() { return _dst_ip; }
00317
00318 inline
00319 unsigned char MCFTPid::dst_ip( int b )
00320 {
00321 int itmp = 0;
00322 switch ( b ) {
00323 case 0:
00324 itmp = _dst_ip & 0x000000FF;
00325 break;
00326 case 1:
00327 itmp = (_dst_ip & 0x0000FF00) >> 8;
00328 break;
00329 case 2:
00330 itmp = (_dst_ip & 0x00FF0000) >> 16;
00331 break;
00332 case 3:
00333 itmp = (_dst_ip & 0xFF000000) >> 24;
00334 break;
00335 }
00336 return (unsigned char)(itmp);
00337 }
00338
00339 inline
00340 unsigned int MCFTPid::dst_port() { return _dst_port; }
00341
00342 inline
00343 unsigned int MCFTPid::src_ip() { return _src_ip; }
00344
00345 inline
00346 unsigned char MCFTPid::src_ip( int b )
00347 {
00348 int itmp = 0;
00349 switch ( b ) {
00350 case 0:
00351 itmp = _src_ip & 0x000000FF;
00352 break;
00353 case 1:
00354 itmp = (_src_ip & 0x0000FF00) >> 8;
00355 break;
00356 case 2:
00357 itmp = (_src_ip & 0x00FF0000) >> 16;
00358 break;
00359 case 3:
00360 itmp = (_src_ip & 0xFF000000) >> 24;
00361 break;
00362 }
00363 return (unsigned char)(itmp);
00364 }
00365
00366 inline
00367 unsigned int MCFTPid::src_port() { return _src_port; }
00368
00369 inline
00370 unsigned int MCFTPid::mssgnum() { return _mssgnum; }
00371
00372 inline
00373 string MCFTPid::hex()
00374 {
00375 string s = "";
00376
00377 char* tmp2 = new char[4+1];
00378 char* tmp4 = new char[8+1];
00379
00380 inttohex( int(_dst_ip), tmp4, 4 ); s += tmp4;
00381 inttohex( _dst_port, tmp2, 2 ); s += tmp2;
00382 inttohex( _src_ip, tmp4, 4 ); s += tmp4;
00383 inttohex( _src_port, tmp2, 2 ); s += tmp2;
00384 inttohex( _mssgnum, tmp4, 4 ); s += tmp4;
00385
00386 return s;
00387 }
00388
00389 inline
00390 string MCFTPid::filename()
00391 {
00392 return hex() + ".mcftp";
00393 }
00394
00395 inline
00396 void MCFTPid::report( ostream& os )
00397 {
00398 os << "MCFTPid::report(){\n";
00399 os << "dst_ip = " << _dst_ip << " = "
00400 << int(dst_ip(3)) << '.' << int(dst_ip(2)) << '.'
00401 << int(dst_ip(1)) << '.' << int(dst_ip(0)) << '\n';
00402 os << "dst_port = " << _dst_port << '\n';
00403 os << "src_ip = " << _src_ip << '\n';
00404 os << "src_ip = " << _src_ip << " = "
00405 << int(src_ip(3)) << '.' << int(src_ip(2)) << '.'
00406 << int(src_ip(1)) << '.' << int(src_ip(0)) << '\n';
00407 os << "src_port = " << _src_port << '\n';
00408 os << "mssgnum = " << _mssgnum << '\n';
00409 os << "hex = " << hex() << '\n';
00410 os << "}\n";
00411 }
00412
00413
00414
00415 inline
00416 BufferBase& operator << ( BufferBase& buffer, MCFTPid& id )
00417 {
00418 id.read(buffer);
00419 return buffer;
00420 }
00421
00422 inline
00423 BufferBase& operator >> ( BufferBase& buffer, MCFTPid& id )
00424 {
00425 id.write(buffer);
00426 return buffer;
00427 }
00428
00429 inline
00430 ostream& operator << ( ostream& os, MCFTPid& id )
00431 {
00432 id.read(os);
00433 return os;
00434 }
00435
00436 inline
00437 istream& operator >> ( istream& is, MCFTPid& id )
00438 {
00439 id.write(is);
00440 return is;
00441 }
00442
00443 #endif
00444