00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __J2K__TFTP_Parser_CPP__
00012 #define __J2K__TFTP_Parser_CPP__
00013
00014 #include <j2k/Net/TFTP/TFTP_Parser.hpp>
00015
00016 TFTP_Parser::TFTP_Parser()
00017 {
00018 OACK_arg = FALSE;
00019 array_sz = 0;
00020 expectedBlockSize = option.getBlockSizeValue();
00021
00022 blocksizeDefault = 1;
00023 windowsizeDefault = 1;
00024 timeoutDefault = 1;
00025
00026 opTable = new TFTP_ParserCallback[6];
00027
00028 opTable[0] = &TFTP_Parser::RRQ_driver;
00029 opTable[1] = &TFTP_Parser::WRQ_driver;
00030 opTable[2] = &TFTP_Parser::DATA_driver;
00031 opTable[3] = &TFTP_Parser::ACK_driver;
00032 opTable[4] = &TFTP_Parser::ERROR_driver;
00033 opTable[5] = &TFTP_Parser::OACK_driver;
00034 }
00035
00036 TFTP_Parser::~TFTP_Parser()
00037 {
00038 if ( opTable != NULL ) delete[] opTable;
00039 }
00040
00041 void TFTP_Parser::parseOptions( char* str, size_t length, size_t offset )
00042 {
00043
00044 char* tmp = new char[ length ];
00045 memcpy( tmp, str, length );
00046 delete [] str;
00047
00048 array_sz = 0;
00049
00050 blocksizeDefault = 1;
00051 windowsizeDefault = 1;
00052 timeoutDefault = 1;
00053
00054 getParams( tmp, offset );
00055
00056 printf( "%d", array_sz );
00057 fflush( stdout );
00058 for( int i = 0; i < array_sz; i++ ) {
00059 safePrint( array[i] );
00060 printf( "\t%d-%d\n", i, array_sz );
00061 fflush( stdout );
00062 i += select( i );
00063 }
00064
00065
00066 if ( blocksizeDefault ) {
00067 option.setBlockSize( atol( DEFAULT_BLOCKSIZE ) );
00068 }
00069
00070 if ( windowsizeDefault ) {
00071 option.setWindowSize( atol( DEFAULT_WINDOWSIZE ) );
00072 }
00073
00074 if ( timeoutDefault ) {
00075 option.setTimeOut( atol( DEFAULT_TIMEOUT ) );
00076 }
00077
00078 expectedBlockSize = option.getBlockSizeValue();
00079
00080 str = new char[ length ];
00081 memcpy( str, tmp, length );
00082 }
00083
00084 char* TFTP_Parser::scanParam( char* str, size_t* len )
00085 {
00086 size_t l = strlen( str );
00087 char* t = str + l + 1;
00088
00089 *len = l;
00090 return t;
00091 }
00092
00093 void TFTP_Parser::getParams( char* str, size_t offset )
00094 {
00095 size_t l = 1;
00096 char* f = str + offset;
00097 char* p = f;
00098 char* q = p;
00099
00100 int k = 0;
00101 while( l != 0 )
00102 {
00103
00104 l = strlen( p );
00105 q = p + l + 1;
00106 array[ k ] = new char[ l ];
00107 memcpy( array[ k ], p, l );
00108 k++;
00109 p = q;
00110 }
00111 array_sz = k - 1;
00112 }
00113
00114
00115
00116 int TFTP_Parser::select( int i )
00117 {
00118 char* s2 = array[i];
00119
00120 if ( s2 != NULL ) s2 = strlwr( s2 );
00121
00122 char* opt = NULL;
00123
00124 if ( (i+1) <= array_sz ) {
00125 opt = strlwr( array[i+1] );
00126 }
00127
00128 if ( !strcmp( s2, "blksize" ) || !strcmp( s2, "blocksize" ) )
00129 {
00130 long bsize = atol( opt );
00131 printf("BlockSize[%d]", bsize );
00132 fflush( stdout );
00133
00134 option.requestedBlockSize( bsize );
00135 blocksizeDefault = 0;
00136 return 1;
00137 } else
00138 if ( !strcmp( s2, "timeout" ) )
00139 {
00140 size_t t = atol( opt );
00141 printf("Timeout[%d]", t );
00142 fflush( stdout );
00143
00144 option.requestedTimeOut( t );
00145 timeoutDefault = 0;
00146 return 1;
00147 } else
00148 if ( !strcmp( s2, "tsize" ) )
00149 {
00150 size_t sz = atol( opt );
00151 printf("FileSize[%d]", sz );
00152 fflush( stdout );
00153
00154 option.requestedFileSize( sz );
00155 return 1;
00156 } else
00157 if ( !strcmp( s2, "wsize" ) || !strcmp( s2, "windowsize" ) \
00158 || !strcmp( s2, "window" ) )
00159 {
00160 long wsize = atol( opt );
00161 printf("WindowSize[%d]", wsize );
00162 fflush( stdout );
00163
00164 option.requestedWindowSize( wsize );
00165 windowsizeDefault = 0;
00166 return 1;
00167 }
00168
00169 if ( i == 0 ) {
00170 printf("Filename[%s]\n", array[i] );
00171 fflush( stdout );
00172 option.setFileName( array[i] );
00173 return 0;
00174 }
00175
00176 if ( i == 1 ) {
00177 printf("Mode[%s]\n", array[i] );
00178 fflush( stdout );
00179 option.requestedMode( array[i] );
00180 return 0;
00181 }
00182
00183 return 0;
00184 }
00185
00186 #endif