Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Net/TFTP/TFTP_Option.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__TFTP_Option_CPP__
00002 #define __J2K__TFTP_Option_CPP__
00003 
00004 #include <j2k/Net/TFTP/TFTP_Option.hpp>
00005 
00006 /**************************************************************************
00007  ***  Default Constructor: Initializing every variables...              ***
00008  **************************************************************************/
00009 TFTP_Option::TFTP_Option()
00010 { 
00011   rwlock1_global.write();
00012   fpath      = new char[  FILEPATH_LENGTH  ];  // 2 lines
00013   openedFile = new char[  FILEPATH_LENGTH  ];  
00014   filename   = new char[  FILENAME_LENGTH  ]; 
00015   hostname   = new char[  HOSTNAME_LENGTH  ];
00016 
00017   filesize  = new char[   FILESIZE_LENGTH  ];  // < 100 TB
00018  windowsize = new char[ WINDOWSIZE_LENGTH  ];
00019   blocksize = new char[  BLOCKSIZE_LENGTH  ];  
00020   timeout   = new char[    TIMEOUT_LENGTH  ];
00021   mode      = new char[       MODE_LENGTH  ];
00022 
00023   pgmType     = TFTP_Type_Undefined;
00024   state       = TFTP_Uninitialized;
00025   OACK_arg    = FALSE;
00026   myFile      = NULL;
00027   sender      = NULL;
00028   fileFactory = NULL;
00029   sendFactory = NULL;
00030   dataFactory = NULL;
00031   TopAck      = 0;
00032   LastData    = 0;
00033   RW_command  = 0;
00034   Done        = FALSE;
00035   waitingAck_queue = NULL;
00036   data_queue  = NULL;
00037   debug       = 0.0;  
00038 
00039         fpathLength = 0;
00040      filenameLength = 0;
00041      filesizeLength = 0;
00042    windowsizeLength = 2; // "1"
00043     blocksizeLength = 4; // "512"
00044       timeoutLength = 2; // "5"
00045          modeLength = 6; // "octet"
00046 
00047   memset( fpath,      0,   FILEPATH_LENGTH );
00048   memset( openedFile, 0,   FILEPATH_LENGTH );
00049   memset( filename,   0,   FILENAME_LENGTH );
00050   memset( hostname,   0,   HOSTNAME_LENGTH );
00051   memset( filesize,   0,   FILESIZE_LENGTH );
00052   memset( windowsize, 0, WINDOWSIZE_LENGTH );
00053   memset( blocksize,  0,  BLOCKSIZE_LENGTH );
00054   memset( timeout,    0,    TIMEOUT_LENGTH );
00055   memset( mode,       0,       MODE_LENGTH );
00056 
00057   port = DEFAULT_PORT;
00058 
00059   char* temp;
00060 
00061   temp = DEFAULT_BLOCKSIZE;
00062   memcpy( blocksize, temp, 4 );
00063 
00064   temp = DEFAULT_WINDOWSIZE;
00065   memcpy( windowsize, temp, 4 );
00066 
00067   temp = DEFAULT_TIMEOUT;
00068   memcpy( timeout,   temp, 2 );
00069 
00070   temp = DEFAULT_MODE;
00071   memcpy( mode,   temp, 6 );
00072 
00073   temp = DEFAULT_HOSTNAME;
00074   memcpy( hostname, temp, 20 );
00075 
00076   rwlock1_global.releaseWrite();
00077 
00078 
00079   rwlock3_templates.write();
00080 
00081   RRQ_WRQ_template = new char[ RRQ_WRQ_TEMPLATE_LENGTH ];
00082   Option_template  = new char[  OPTION_TEMPLATE_LENGTH ];
00083 
00084   RRQ_WRQ_templateLength = 6 + 2 + 2;
00085   Option_templateLength  = 0;
00086 
00087   memset( RRQ_WRQ_template, 0, RRQ_WRQ_TEMPLATE_LENGTH );
00088   memset(  Option_template, 0,  OPTION_TEMPLATE_LENGTH );
00089 
00090   rwlock3_templates.releaseWrite();
00091 }
00092 
00093 /**************************************************************************
00094  ***  Destructor: Deallocating every variables...                       ***
00095  **************************************************************************/
00096 TFTP_Option::~TFTP_Option() 
00097 {
00098 #if 0
00099   rwlock1_global.write();
00100   if ( fpath     != NULL ) delete [] fpath;    
00101   if ( filename  != NULL ) delete [] filename;
00102   if ( hostname  != NULL ) delete [] hostname;
00103   if ( filesize  != NULL ) delete [] filesize;
00104   if ( blocksize != NULL ) delete [] blocksize;
00105   if ( timeout   != NULL ) delete [] timeout;
00106   if ( mode      != NULL ) delete [] mode;
00107   rwlock1_global.releaseWrite();
00108 #endif
00109 
00110   rwlock2_file.write();
00111   if ( myFile    != NULL ) delete  myFile;
00112   if ( sender    != NULL ) delete sender;
00113   rwlock2_file.releaseWrite();
00114 
00115   rwlock3_templates.write();
00116   if ( RRQ_WRQ_template != NULL ) delete [] RRQ_WRQ_template;
00117   if ( Option_template  != NULL ) delete []  Option_template;
00118   rwlock3_templates.releaseWrite();
00119 }
00120 
00121 
00122 /**************************************************************************
00123  ***  Argument Parser: Parse the arguments passed on the command line.  ***
00124  **************************************************************************/
00125 void TFTP_Option::ArgParser( int argc, char* argv[] )
00126 {
00127   int i = 1;
00128   if (argc == 1)
00129   {
00130       char service;
00131       printf("Please tell me what kind of service you want. (S)erver or (C)lient?\n");
00132       cin>>service;
00133 
00134       if (service == 'S' || service == 's')
00135       {
00136           ConfigServer( NULL );
00137       }
00138       else
00139       {
00140           //Configures the hostname and port of the client
00141           ConfigClient( NULL );
00142       }
00143             
00144   }
00145   
00146   for ( ; i < argc; i++)
00147   {
00148      // Does the first character is a - or /
00149      if ( argv[i][0] == '-' || argv[i][0] == '/' )
00150      {
00151        char* s = argv[i] + 1;
00152        printf("(%s)", s );
00153        fflush(stdout);
00154        // Do we give param x by space or =
00155        char* p = strchr (argv[i], '=');
00156        if ( p > 0 )
00157        {
00158          // We have to -arg=x or --arg=x
00159          if ( argv[i][1] == '-' ) s++;
00160          *p = '\0';
00161          ArgDetect( s, p+1 );
00162  
00163        } else {
00164  
00165          // We have to -arg x or --arg x
00166          if ( argv[i][1] == '-' ) s++;
00167          
00168          char* opt = NULL;
00169          if ( (i+1) < argc ) opt = argv[i+1];
00170          fflush(stdout);
00171        
00172          ArgDetect( s, opt );
00173          i++;
00174        }
00175      } else {
00176        printf("\nInvalid parameter: [%s]\n", argv[i] );
00177      }
00178   }
00179 }
00180 
00181 /**************************************************************************
00182  ***  Argument Detection:                                               ***
00183  ***   Detect what kind of command line argument it is                  ***
00184  ***   and initialize variables in consequence to that given argument.  ***
00185  **************************************************************************/
00186 void TFTP_Option::ArgDetect( const char* s, char* o )
00187 {
00188     char* s2  = NULL;
00189     char* opt = o;
00190     size_t val = 0;
00191     
00192     if ( s != NULL ) 
00193     {
00194         s2 = strlwr( s );
00195     }
00196     
00197     if ( o != NULL ) 
00198     {
00199         opt = strlwr( o );
00200         val  = atol( opt );
00201         printf("{%s}", o );
00202     }
00203     
00204     printf("[%s]", s );
00205     fflush(stdout);
00206     if ( !strcmp( s2, "b" ) || !strcmp( s2, "blksize"   ) \
00207          || !strcmp( s2, "blocksize" ) )
00208     {
00209         OACK_arg = TRUE;   // This is an OACK extension
00210         setBlockSize( val );  
00211     }
00212     else if ( !strcmp( s2, "f" ) || !strcmp( s2, "file"     ) \
00213               || !strcmp( s2, "filename" ) )
00214     {
00215         setFileName( o );
00216     }
00217     else if ( !strcmp( s2, "put" )   || !strcmp( s2, "save"   ) || \
00218              !strcmp( s2, "write" ) || !strcmp( s2, "upload" ) )
00219     {
00220         setFileName( o );
00221         RW_command = 2;
00222         
00223         // Set Write bit and clear Read bit, if any.
00224         pgmType = (TFTP_Type_t)(  ((int)pgmType & 0x0B) | 0x08  );
00225     }
00226     else if ( !strcmp( s2, "get"  ) || !strcmp( s2, "load"     ) ||  \
00227               !strcmp( s2, "read" ) || !strcmp( s2, "download" ) )
00228     {
00229         setFileName( o );
00230         RW_command = 1;
00231         
00232         // Set Read bit and clear Write bit, if any.
00233         pgmType = (TFTP_Type_t)(  ((int)pgmType & 0x07) | 0x04  );
00234         
00235     }
00236     else if ( !strcmp( s2, "t" ) || !strcmp( s2, "timeout" ) )
00237     {
00238         OACK_arg = TRUE;   // This is an OACK extension
00239         setTimeOut( val );
00240     }
00241     else if ( !strcmp( s2, "m" ) || !strcmp( s2, "mode" ) )
00242     {
00243         setMode( opt );
00244     }
00245     else if ( !strcmp( s2, "s" ) || !strcmp( s2, "fsize" )  \
00246               || !strcmp( s2, "filesize" ) )
00247     {
00248         OACK_arg = TRUE;   // This is an OACK extension
00249         setFileSize( val );  
00250     }
00251     else if ( !strcmp( s2, "w" )      || !strcmp( s2, "wsize" ) || \
00252               !strcmp( s2, "window" ) || !strcmp( s2, "windowsize" ) )
00253     {
00254         OACK_arg = TRUE;   // This is an OACK extension
00255         setWindowSize( val );  
00256     }
00257     else if ( !strcmp( s2, "h" ) || !strcmp( s2, "host" )   \
00258               || !strcmp( s2, "hostname" ) )
00259     {
00260         setHostName( o );
00261     }
00262     else if ( !strcmp( s2, "p" ) || !strcmp( s2, "port" ) )
00263     {
00264         setPort( val );
00265     }
00266     else if ( !strcmp( s2, "c" ) || !strcmp( s2, "client" ) \
00267            || !strcmp( s2, "open" ) )
00268     {
00269         ConfigClient( opt );
00270     }
00271     else if ( !strcmp( s2, "d" )   || !strcmp( s2, "deamon" ) ||  \
00272               !strcmp( s2, "srv" ) || !strcmp( s2, "server" ) )
00273     {
00274         ConfigServer( opt );
00275     }
00276 }
00277 /**************************************************************************
00278  ***  Configures the port for the server                                ***
00279  **************************************************************************/
00280 
00281 void TFTP_Option::ConfigServer( char* opt )
00282 {
00283    port = DEFAULT_PORT;
00284 
00285    if (opt != NULL)
00286    {
00287      // Look for :
00288      char* q = strchr( opt, ':' );
00289             
00290      // If it's there the word after it is the port
00291      if ( q > 0 )
00292      {
00293        port = atol( q+1 );
00294      } else {
00295        port = atol( opt );
00296      }
00297    }
00298        
00299     setPort( port );
00300 
00301     // Set Server bit and clear Clients bit, if any.
00302     pgmType = (TFTP_Type_t)(  ((int)pgmType & 0x01) | 0x01  );
00303     
00304 }
00305 
00306 /**************************************************************************
00307  ***  Configures the hostname and port for the client                   ***
00308  **************************************************************************/
00309 
00310 void TFTP_Option::ConfigClient(char* opt )
00311 {
00312     
00313         short port = DEFAULT_PORT;
00314         if (opt != NULL)
00315         {
00316             //Look for :
00317             char* q = strchr( opt, ':' );
00318             
00319             //If it's there the word after it is the port
00320             if ( q > 0 )
00321             {
00322                 port    = atol( q+1 );
00323                 opt[ (int)(q-opt) ] = '\0';
00324                 setPort( port );
00325                 //And the hostname is right before.
00326                 setHostName( opt);
00327             }
00328         }
00329         else
00330         {
00331             char hn[121];
00332             short pt;
00333             printf("Please enter hostname: \n");
00334             cin>>hn;
00335             printf("Please enter port: \n");
00336             cin>>pt;
00337             setPort (pt);
00338             setHostName(hn);
00339         }
00340         
00341         // Set Client bit and clear Server bit, if any.
00342         pgmType = (TFTP_Type_t)(  ((int)pgmType & 0xFE) | 0x02  );        
00343 }
00344 
00345 
00346 /**************************************************************************
00347  ***  RRQ & WRQ Template Builder:                                       ***
00348  ***   Build a template char[] that will be used                        ***
00349  ***   by RRQ and WRQ related functions in Packet class.                ***
00350  **************************************************************************/
00351 void TFTP_Option::build_RRQ_WRQ_TemplatePacket()
00352 {
00353   rwlock1_global.read();
00354   rwlock3_templates.write();
00355 
00356   printf("I'm in = (\n");
00357   fflush( stdout );
00358 
00359   showOptions();
00360   printf("\n);" );
00361   memset( RRQ_WRQ_template, 0, RRQ_WRQ_TEMPLATE_LENGTH );
00362   
00363   short op = 1;
00364 
00365   RRQ_WRQ_templateLength = filenameLength + modeLength + 2;
00366   
00367   memcpy( RRQ_WRQ_template, (char*)&op, 2 ); 
00368   memcpy( RRQ_WRQ_template + 2, filename, filenameLength );
00369   memcpy( RRQ_WRQ_template + 2 + filenameLength, mode, modeLength );
00370 
00371   rwlock1_global.releaseRead();
00372   rwlock3_templates.releaseWrite();
00373 }
00374 
00375 /**************************************************************************
00376  ***  Option Extension Template Builder:                                ***
00377  ***   Build a template char[] that will be used                        ***
00378  ***   by RRQ, WRQ and OACK related functions in Packet class.          ***
00379  **************************************************************************/
00380 void TFTP_Option::build_OptionPacket() 
00381 {
00382   size_t sz  = 2;
00383   size_t pos = 0;
00384   char* txt_blksize = "blksize";  // 8
00385   char* txt_tsize   = "tsize";    // 6
00386   char* txt_timeout = "timeout";  // 8
00387   char* txt_wsize   = "window";   // 7
00388   
00389   rwlock1_global.read();
00390 
00391   if ( blocksize != NULL ) {
00392     sz += blocksizeLength + 8;
00393   }
00394 
00395   if ( filesize  != NULL ) {
00396     sz += filesizeLength + 6;
00397   }
00398 
00399   if ( timeout   != NULL ) { 
00400     sz += timeoutLength + 8;
00401   }
00402 
00403   if ( windowsize != NULL ) { 
00404     sz += windowsizeLength + 7;
00405   }
00406 
00407   rwlock3_templates.write();
00408 
00409   memset( Option_template, 0, OPTION_TEMPLATE_LENGTH );  
00410 
00411   short oack_op = 6;
00412 
00413   memcpy( Option_template, (char*)&oack_op, 2 );
00414 
00415   pos = 2;
00416 
00417   if ( blocksize != NULL )
00418   {
00419     memcpy( Option_template + pos, txt_blksize, 8 );
00420     Option_template[ pos + 8 - 1 ] = '\0';
00421 
00422     memcpy( Option_template + pos + 8, blocksize, blocksizeLength );
00423     Option_template[ pos + 8 + blocksizeLength - 1 ] = '\0';
00424 
00425     pos += blocksizeLength + 8;
00426   }
00427 
00428 /*
00429   if ( filesize  != NULL )
00430   {  memset( blocksize,    0, blocksizeLength );
00431 
00432     memcpy( Option_template + pos, txt_tsize, 6 );
00433     Option_template[ pos + 6 - 1 ] = '\0';
00434 
00435     memcpy( Option_template + pos + 6, filesize, filesizeLength );
00436     Option_template[ pos + 6 + filesizeLength - 1 ] = '\0';
00437 
00438     pos += filesizeLength + 6;
00439   }
00440 */
00441 
00442   if ( timeout   != NULL )
00443   {
00444     memcpy( Option_template + pos, txt_timeout, 8 );
00445     Option_template[ pos + 8 - 1 ] = '\0';
00446 
00447     memcpy( Option_template + pos + 8, timeout, timeoutLength );
00448     Option_template[ pos + 8 + timeoutLength - 1 ] = '\0';
00449 
00450     pos += timeoutLength + 8;
00451   }
00452 
00453   if ( windowsize != NULL )
00454   {
00455     memcpy( Option_template + pos, txt_wsize, 7 );
00456     Option_template[ pos + 7 - 1 ] = '\0';
00457 
00458     memcpy( Option_template + pos + 7, windowsize, windowsizeLength );
00459     Option_template[ pos + 7 + windowsizeLength - 1 ] = '\0';
00460 
00461     pos += windowsizeLength + 7;
00462   }
00463 
00464   Option_templateLength = sz;
00465 
00466   rwlock1_global.releaseRead();
00467   rwlock3_templates.releaseWrite();
00468 }
00469 
00470 /**************************************************************************
00471  ***  Set the filename                                                  ***
00472  **************************************************************************/
00473 inline void TFTP_Option::setFileName( const char* fname ) 
00474 {
00475   rwlock1_global.write();
00476 
00477   fpathLength = strlen( fname ) + 1;
00478 
00479   memset( fpath,    0, FILENAME_LENGTH );  
00480   memset( filename, 0, FILENAME_LENGTH );  
00481 
00482   filenameLength = fpathLength;
00483   memcpy( fpath,    fname, fpathLength );
00484   memcpy( filename, fname, fpathLength );
00485   rwlock1_global.releaseWrite();
00486 }
00487  
00488 /**************************************************************************
00489  ***  Get the filename                                                  ***
00490  **************************************************************************/
00491 const char* TFTP_Option::getFileName() 
00492 {
00493   rwlock1_global.read();
00494   char* temp = new char[ filenameLength ]; 
00495   memcpy( temp, filename, filenameLength );
00496   rwlock1_global.releaseRead();
00497   return (const char*)temp;
00498 }
00499 
00500 /**************************************************************************
00501  ***  Set the BlockSize                                                 ***
00502  **************************************************************************/
00503 inline void TFTP_Option::setBlockSize( size_t bsize ) 
00504 {
00505   rwlock1_global.write();
00506 
00507   char* temp = DEFAULT_BLOCKSIZE;
00508   memset( blocksize, 0, BLOCKSIZE_LENGTH );
00509   
00510   if ( bsize >= 8 && bsize <= 65464 ) 
00511   {
00512       blocksize = ulltostr( bsize, blocksize + BLOCKSIZE_LENGTH );
00513       blocksizeLength = strlen( blocksize ) + 1;      
00514   } else {
00515       blocksizeLength = strlen( temp ) + 1;
00516       memcpy( blocksize, temp, blocksizeLength );
00517   }
00518 
00519   rwlock1_global.releaseWrite();
00520 }
00521 
00522 /**************************************************************************
00523  ***  Requested by client Block Size                                    ***
00524  **************************************************************************/
00525 inline void TFTP_Option::requestedBlockSize( size_t bsize )
00526 {
00527   if ( blocksize == NULL ) return;          // Rejected.
00528   if ( bsize < 8 || bsize > 65464 ) return; // Rejected.
00529 
00530   setBlockSize( bsize );
00531 }
00532 
00533 /**************************************************************************
00534  ***  Get the BlockSize                                                 ***
00535  **************************************************************************/
00536 inline size_t TFTP_Option::getBlockSize()
00537 {
00538   rwlock1_global.read();
00539   long sz = atol( blocksize );
00540   rwlock1_global.releaseRead();
00541   return (size_t)sz;
00542 }
00543 
00544 /**************************************************************************
00545  ***  Set Port number                                                   ***
00546  **************************************************************************/
00547 inline void TFTP_Option::setPort( size_t portNo ) 
00548 {
00549   rwlock1_global.write();
00550   if ( portNo >= 0 && portNo <= 65535 ) 
00551   {
00552     port = portNo;
00553   }
00554   rwlock1_global.releaseWrite();
00555 }
00556 
00557 /**************************************************************************
00558  ***  Set Host name                                                     ***
00559  **************************************************************************/
00560 inline void TFTP_Option::setHostName( const char* host )
00561 {
00562   rwlock1_global.write();
00563   hostnameLength = strlen( host ) + 1;
00564   memset( hostname, 0, HOSTNAME_LENGTH );
00565   memcpy( hostname, host, hostnameLength );
00566   rwlock1_global.releaseWrite();
00567 }
00568 
00569 /**************************************************************************
00570  ***  Set the Window Size                                               ***
00571  **************************************************************************/
00572 inline void TFTP_Option::setWindowSize( size_t wsize ) 
00573 {
00574   rwlock1_global.write();
00575   char* temp = DEFAULT_WINDOWSIZE;
00576 
00577   memset( windowsize, 0, WINDOWSIZE_LENGTH );
00578   
00579   if ( wsize >= 1 && wsize <= 65464 ) 
00580   {
00581       windowsize = ulltostr( wsize, windowsize + WINDOWSIZE_LENGTH );
00582       windowsizeLength = strlen( windowsize ) + 1;      
00583   } else {
00584       windowsizeLength = strlen( temp ) + 1;
00585       memcpy( windowsize, temp, windowsizeLength );
00586   }
00587 
00588   rwlock1_global.releaseWrite();
00589 }
00590 
00591 /**************************************************************************
00592  ***  Requested by client Window Size                                   ***
00593  **************************************************************************/
00594 inline void TFTP_Option::requestedWindowSize( size_t wsize )
00595 {
00596   if ( windowsize == NULL ) return;          // Rejected.
00597   if ( wsize < 1 || wsize > 65464 ) return;  // Rejected.
00598 
00599   setWindowSize( wsize );
00600 }
00601 
00602 /**************************************************************************
00603  ***  Get the Window Size                                               ***
00604  **************************************************************************/
00605 inline size_t TFTP_Option::getWindowSize()
00606 {
00607   rwlock1_global.read();
00608   long sz = atol( windowsize );
00609   rwlock1_global.releaseRead();
00610   return (size_t)sz;
00611 }
00612 
00613 /**************************************************************************
00614  ***  Set the File Size                                                 ***
00615  **************************************************************************/
00616 inline void TFTP_Option::setFileSize( size_t fsize )
00617 {
00618     rwlock1_global.write();
00619   char* temp = "0";
00620 
00621   memset( filesize, 0, FILESIZE_LENGTH );
00622   
00623   if ( fsize > 0 ) 
00624   {
00625       filesize = ulltostr( fsize, filesize + FILESIZE_LENGTH );
00626       filesizeLength = strlen( filesize ) + 1;      
00627   } else {
00628       filesizeLength = strlen( temp ) + 1;
00629       memcpy( filesize, temp, filesizeLength );
00630   }
00631 
00632   rwlock1_global.releaseWrite();
00633 }
00634 
00635 /**************************************************************************
00636  ***  Requested by client File Size                                     ***
00637  **************************************************************************/
00638 inline void TFTP_Option::requestedFileSize( size_t fsize )
00639 {
00640   setFileSize( fsize );
00641 }
00642 
00643 /**************************************************************************
00644  ***  Get the File Size                                                 ***
00645  **************************************************************************/
00646 inline const char* TFTP_Option::getFileSize()
00647 {
00648   rwlock1_global.read();
00649   char* temp = new char[ filesizeLength ]; 
00650   memcpy( temp, filesize, filesizeLength );
00651   rwlock1_global.releaseRead();
00652   return (const char*)temp;
00653 }
00654 
00655 /**************************************************************************
00656  ***  Set the Transfer Mode                                             ***
00657  **************************************************************************/
00658 inline void TFTP_Option::setMode( char* m )   
00659 {
00660   if ( *m == 'o' ) {     // octet
00661     if ( *mode != 'o' )
00662     {
00663       rwlock1_global.write();
00664       char* temp = DEFAULT_MODE;
00665       modeLength = 6;
00666       memcpy( mode, temp, 6 );
00667       rwlock1_global.releaseWrite();
00668     }
00669   } else if ( *m == 'n' ) { // NetAscii
00670 
00671     printf( "NetASCII TFTP Mode NOT SUPPORTED! \n" );
00672     exit( 2 );
00673 
00674   } else if ( *m == 'm' ) { // Mail
00675 
00676     printf( "Mail TFTP Mode NOT SUPPORTED! \n" );
00677     exit( 2 );
00678 
00679   } else {
00680 
00681     printf( "Unknown TFTP Mode\n" );
00682     exit( 2 );
00683   }
00684 }
00685 
00686 /**************************************************************************
00687  ***  Requested by client Transfer mode                                 ***
00688  **************************************************************************/
00689 inline void TFTP_Option::requestedMode( const char* m )
00690 {
00691   if ( *m == 'o' ) return;
00692   SendNotDefinedError( "The requested mode is not supported." );
00693 }
00694 
00695 /**************************************************************************
00696  ***  Get the Transfer Mode                                             ***
00697  **************************************************************************/
00698 inline const char* TFTP_Option::getMode() 
00699 {
00700   rwlock1_global.read();
00701   char* temp = new char[ modeLength ];
00702   memcpy( temp, mode, modeLength );
00703   rwlock1_global.releaseRead();
00704   return (const char*)temp;
00705 }
00706 
00707 /**************************************************************************
00708  ***  Set the Transfer Timeout                                          ***
00709  **************************************************************************/
00710 inline void TFTP_Option::setTimeOut( size_t t ) 
00711 {    
00712   rwlock1_global.write();
00713   char* temp = "5";
00714 
00715   memset( timeout, 0, TIMEOUT_LENGTH );
00716   
00717   if ( t >= 1 && t <= 255 )
00718   {
00719       timeout = ulltostr( t, timeout + TIMEOUT_LENGTH );
00720       timeoutLength = strlen( timeout ) + 1;      
00721   } else {
00722       timeoutLength = strlen( temp ) + 1;
00723       memcpy( timeout, temp, timeoutLength );
00724   }
00725 
00726   rwlock1_global.releaseWrite();
00727 }
00728 
00729 /**************************************************************************
00730  ***  Requested by client Timeout                                       ***
00731  **************************************************************************/
00732 inline void TFTP_Option::requestedTimeOut( size_t t )
00733 {
00734   if ( timeout == NULL )  return;  // Rejected.
00735   if ( t < 1 || t > 255 ) return;  // Rejected.
00736 
00737   setTimeOut( t );
00738 }
00739 
00740 /**************************************************************************
00741  ***  Get the Transfer Timeout                                          ***
00742  **************************************************************************/
00743 inline const char* TFTP_Option::getTimeOut() 
00744 {
00745   rwlock1_global.read();
00746   char* temp = new char[ timeoutLength ]; 
00747   memcpy( temp, timeout, timeoutLength );
00748   rwlock1_global.releaseRead();
00749   return (const char*)temp;
00750 }
00751 
00752 /**************************************************************************
00753  ***  Get the Transfer Timeout value                                    ***
00754  **************************************************************************/
00755 inline ULONG TFTP_Option::getTimeOutValue()
00756 {
00757   rwlock1_global.read();
00758   long val = atol( timeout );
00759   rwlock1_global.releaseRead();
00760   return (ULONG)val;
00761 }
00762 
00763 /**************************************************************************
00764  ***  Get the Transfer Block size value                                 ***
00765  **************************************************************************/
00766 inline ULONG TFTP_Option::getBlockSizeValue()
00767 {
00768   rwlock1_global.read();
00769   long val = atol( blocksize );
00770   rwlock1_global.releaseRead();
00771   return (ULONG)val;
00772 }
00773 
00774 /**************************************************************************
00775  ***  Get the Transfer Window size value                                 ***
00776  **************************************************************************/
00777 inline ULONG TFTP_Option::getWindowSizeValue()
00778 {
00779   rwlock1_global.read();
00780   long val = atol( windowsize );
00781   rwlock1_global.releaseRead();
00782   return (ULONG)val;
00783 }
00784 
00785 /**************************************************************************
00786  ***  Open a JFile for the transfer:                                    ***
00787  ***    "rb" for read, "wb" for write                                   ***
00788  **************************************************************************/
00789 void TFTP_Option::openFile( char* rwmode = "rb" ) 
00790 {
00791   if ( !strcmp( openedFile, fpath ) ) return;
00792 
00793   char* rw = rwmode;
00794 
00795   if ( isClient() ) 
00796   {
00797     if ( isReading() ) rw = "wb";
00798     if ( isWriting() ) rw = "rb";    
00799   } else if ( isServer() ) 
00800   {
00801     if ( isReading() ) rw = "rb";
00802     if ( isWriting() ) rw = "wb";    
00803   }
00804 
00805   rwlock1_global.read();
00806   rwlock2_file.write();
00807   if ( myFile != NULL ) delete myFile;
00808 
00809   printf( "Building myFile for %s \n", rw ); 
00810   fflush( stdout );
00811 
00812   memcpy( openedFile, fpath, FILEPATH_LENGTH );
00813   myFile = new JFile( fpath, rw, (size_t)atol( blocksize ) );
00814 
00815   memset( filename, 0, FILENAME_LENGTH );
00816   
00817   myFile->getFileName( filename );
00818   filenameLength = strlen( filename ) + 1;
00819   rwlock1_global.releaseRead();
00820   rwlock2_file.releaseWrite();
00821 }
00822 
00823 /************************************************************************** 
00824  ************************************************************************** 
00825  ***   bit 0 Init                                                       *** 
00826  ***   bit 1 Connected                                                  *** 
00827  ***   bit 2 Ack for RRQ/WRQ                                            *** 
00828  ***   bit 3 Reading                                                    *** 
00829  ***   bit 4 Writing                                                    *** 
00830  ***   bit 5 End of Read/Write                                          *** 
00831  ***   bit 6 Error                                                      *** 
00832  ***   bit 7 Finished, program exit signal                              *** 
00833  ************************************************************************** 
00834  ***   Disconnect means that we are not connected and that              *** 
00835  ***   all the variables in TFTP_Option are still initialized.          *** 
00836  ************************************************************************** 
00837  **************************************************************************/
00838 
00839 
00840 /**************************************************************************
00841  ***  Does the variables in TFTP_Option are initialized ?               *** 
00842  **************************************************************************/
00843 inline int TFTP_Option::isInitialized()
00844 {
00845   return (( (int)state & 0x01 ) == 0x01 );
00846 }
00847 
00848 /**************************************************************************
00849  ***  Does the program is connected to a foreign host  ?                *** 
00850  **************************************************************************/
00851 inline int TFTP_Option::isConnected()
00852 {
00853   return (( (int)state & 0x02 ) == 0x02 );
00854 }
00855 
00856 /**************************************************************************
00857  ***  Does the program is waiting for an acknowledgement to continue ?  *** 
00858  **************************************************************************/
00859 inline int TFTP_Option::isWaitingForAck()
00860 {
00861   return (( (int)state & 0x04 ) == 0x04 );
00862 }
00863 
00864 /**************************************************************************
00865  ***  Does the program is performing a reading request ?                *** 
00866  **************************************************************************/
00867 inline int TFTP_Option::isReading()
00868 {
00869   return (( (int)state & 0x08 ) == 0x08 );
00870 }
00871 
00872 /**************************************************************************
00873  ***  Does the program is performing a writing request ?                *** 
00874  **************************************************************************/
00875 inline int TFTP_Option::isWriting()
00876 {
00877   return (( (int)state & 0x10 ) == 0x10 );
00878 }
00879 
00880 /**************************************************************************
00881  ***  Does the program achieved an read/write end of file tranfert ?    *** 
00882  **************************************************************************/
00883 inline int TFTP_Option::isEnding()
00884 {
00885   return (( (int)state & 0x20 ) == 0x20 );
00886 }
00887 
00888 /**************************************************************************
00889  ***  Does the program sended/received an ERROR packet ?                ***
00890  **************************************************************************/
00891 inline int TFTP_Option::hasError()
00892 {
00893   return (( (int)state & 0x40 ) == 0x40 );
00894 }
00895 
00896 /**************************************************************************
00897  ***  Does the program sended a signal to abort all other threads ?     ***
00898  **************************************************************************/
00899 inline int TFTP_Option::isKilling()
00900 {
00901   return (( (int)state & 0x80 ) == 0x80 );
00902 }
00903 
00904 /**************************************************************************
00905  ***  This portion is used by TFTP_Parser to decide                     ***
00906  ***  if the incomming Packet shall be valid or not ?                   ***
00907  **************************************************************************/
00908 
00909 /**************************************************************************
00910  ************************************************************************** 
00911  ***   bit 0 Server                                                     *** 
00912  ***   bit 1 Client                                                     *** 
00913  ***   bit 2 Reading Command Line request                               *** 
00914  ***   bit 3 Writing Command Line request                               *** 
00915  ************************************************************************** 
00916  **************************************************************************/
00917 inline int TFTP_Option::isServer()
00918 {
00919   return (( (int)pgmType & 0x01 ) == 0x01 );
00920 }
00921 
00922 inline int TFTP_Option::isClient()
00923 {
00924   return (( (int)pgmType & 0x02 ) == 0x02 );
00925 }
00926 
00927 /**************************************************************************
00928  ***  Does the RRQ request in the given program state is valid ?        ***
00929  ***  We were waiting for a connection and we are the server            ***
00930  **************************************************************************/
00931 inline int TFTP_Option::isRRQ_Valid()
00932 {
00933   return ( state == TFTP_Connected  &&  isServer() );
00934 }
00935 
00936 /**************************************************************************
00937  ***  Does the WRQ request in the given program state is valid ?        ***
00938  ***  We were waiting for a connection and we are the server            ***
00939  **************************************************************************/
00940 inline int TFTP_Option::isWRQ_Valid()
00941 {
00942   return ( state == TFTP_Connected  &&  isServer() );
00943 }
00944 
00945 /**************************************************************************
00946  ***  Does the program state allowed DATA packet to be received ?       ***
00947  ***                                                                    ***
00948  ***  We are the client, we want to read and we receive                 ***
00949  ***  a Data Packet as an Acknowledgement or after the Acknowledgement. ***
00950  ***                                                                    ***
00951  ***  We are the server, the client side wants to write on the server   ***
00952  ***  and we receive data after acknowledgement #0 or OACK              ***
00953  **************************************************************************/
00954 inline int TFTP_Option::isDATA_Valid()
00955 {
00956   return ( ( state == TFTP_ReadingNotAck  &&  isClient()  ) || \
00957            ( state == TFTP_Reading        &&  isClient()  ) || \
00958            ( state == TFTP_Writing        &&  isServer()  )    );
00959 }
00960 
00961 /**************************************************************************
00962  ***  Are we waiting for an OACK packet to be received or not ?         ***
00963  ***                                                                    ***
00964  ***  We are the client and we received an option extensions            ***
00965  ***  acknowledgement for reading or writing request                    ***
00966  **************************************************************************/
00967 inline int TFTP_Option::isOACK_Valid()
00968 {
00969   return (  (( state == TFTP_ReadingNotAck ) ||  \
00970              ( state == TFTP_WritingNotAck ))  &&  isClient()  );
00971 }
00972 
00973 /**************************************************************************
00974  ***  Are we waiting for an ACK packet to be received or not ?          ***
00975  ***                                                                    ***
00976  ***  We are reading or writing on either client or server side         ***
00977  ***  waiting for RRQ, WRQ or DATA packet acknowledgement.              ***
00978  **************************************************************************/
00979 inline int TFTP_Option::isACK_Valid()
00980 {
00981   return ( state == TFTP_Reading       ||  state == TFTP_Writing  \
00982         || state == TFTP_ReadingNotAck ||  state == TFTP_WritingNotAck );
00983 }
00984 
00985 /**************************************************************************
00986  ***  Create a socket with the given parsed parameters                  ***
00987  **************************************************************************/
00988 void TFTP_Option::createSocket()
00989 {
00990   rwlock2_file.write();
00991   if ( sender != NULL ) {
00992     delete sender;
00993   }
00994 
00995   if ( isServer() ) {
00996     sender = new UDP_SocketServer( port );
00997   } else
00998   if ( isClient() ) {
00999     sender = new UDP_SocketClient( hostname, port );
01000   } else {
01001     printf( "The program type, Server or Client, was not specified on the command line.\n");
01002     exit( 1 );
01003   }
01004   rwlock2_file.releaseWrite();
01005 }
01006 
01007 /************************************************************/
01008 
01009 void TFTP_Option::showStr( const char* n, const char* s )
01010 {
01011     printf( "%s[", n );
01012     if ( s != NULL ) {   
01013         printf( "%s]\n", s );
01014     } else {             
01015         printf( "Null]\n" );
01016     }
01017 }
01018 
01019 void TFTP_Option::showVal( const char* n, size_t s ) 
01020 {
01021     printf( "%s[%u]\n", n, s );
01022 }
01023 
01024 void TFTP_Option::showChar( const char*n, const char* s, size_t sz )
01025 {
01026     printf("\n%s = {", n );
01027 
01028     for( int i = 0; i < sz; i++ )
01029         printf("[%u]%c)", s[i], s[i] );
01030         
01031     printf("};\n");
01032 }
01033 
01034 void TFTP_Option::showOptions()
01035 {
01036   showStr( "fpath",  fpath);     // Full path          "/home/fred/test.txt"
01037   showStr( "filename",  filename);  // Only the filename  "test.txt"
01038   showStr( "filesize",  filesize);
01039   showStr( "timeout",  timeout);
01040   showStr( "blocksize",  blocksize);
01041   showStr( "mode",  mode);
01042   showStr( "windowsize",  windowsize);
01043 
01044   short  port;
01045   showStr( "hostname",  hostname);
01046   showVal( "hostnameLength ", hostnameLength);
01047 
01048   showVal("fpathLength ",       fpathLength);
01049   showVal("filenameLength ",    filenameLength);
01050   
01051   showVal("filesizeLength ",    filesizeLength); 
01052   showVal("blocksizeLength ",   blocksizeLength);
01053   showVal("windowsizeLength ",  windowsizeLength);
01054   showVal("timeoutLength ",     timeoutLength);
01055   showVal("modeLength ",        modeLength);
01056 
01057   showChar( "RRQ_WRQ_template",  RRQ_WRQ_template, RRQ_WRQ_templateLength );
01058   showVal( "RRQ_WRQ_templateLength ", RRQ_WRQ_templateLength);
01059 
01060   showChar( "Option_template",  Option_template, Option_templateLength );
01061   showVal( "Option_templateLength ", Option_templateLength);
01062 
01063   showVal( "state", (int)state );
01064   showVal( "pgmType", (int)pgmType );
01065 
01066   showVal( "OCAK_arg", OACK_arg );
01067   showVal( "Done", Done );
01068 
01069   showVal("LastData ",          LastData);
01070 
01071   printf( "CurrentTime[%d]\n", currentTime);
01072   showVal("windowsizeLeft ", windowsizeLeft);
01073 
01074   showVal("TopAck ",          TopAck);  
01075 }
01076 
01077 #endif

Generated on Sun Oct 14 18:46:34 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001