00001 #ifndef __J2K__TFTP_ParserStatic_HPP__
00002 #define __J2K__TFTP_ParserStatic_HPP__
00003
00004
00005
00006 void RRQ_driver( char* rawPacket, int sz )
00007 {
00008
00009
00010 if ( option.isRRQ_Valid() )
00011 {
00012 option.state = TFTP_Reading;
00013
00014
00015 savedOptions.save();
00016
00017
00018 parseOptions( rawPacket, sz, 2 );
00019
00020 option.showOptions();
00021 fflush( stdout );
00022
00023 option.openFile( "rb" );
00024 fflush( stdout );
00025
00026 if ( option.dataFactory == NULL ) {
00027 option.dataFactory = new DataFactory();
00028 }
00029
00030
00031 option.dataFactory->run();
00032
00033
00034
00035
00036
00037 option.build_OptionPacket();
00038
00039 printf("back\n");
00040 option.showOptions();
00041 fflush( stdout );
00042
00043 OACK_arg = FALSE;
00044
00045 if ( OACK_arg == TRUE )
00046 {
00047 TFTP_Packet* pkt = new TFTP_Packet();
00048 pkt->sendOAck();
00049
00050
00051
00052 } else {
00053
00054 if ( option.sendFactory == NULL ) {
00055 option.sendFactory = new TFTP_Sender();
00056 }
00057
00058 option.sendFactory->start();
00059
00060
00061 sched_yield();
00062
00063
00064
00065
00066 }
00067
00068 } else {
00069
00070 option.state = TFTP_Error;
00071
00072
00073
00074
00075
00076 SendIllegalOpError( "RRQ Rejected: Your Read Request makes no sense. \n" );
00077 }
00078 }
00079
00080
00081
00082 void WRQ_driver( char* rawPacket, int sz )
00083 {
00084
00085
00086
00087 if ( option.isWRQ_Valid() )
00088 {
00089 option.state = TFTP_Writing;
00090
00091
00092 savedOptions.save();
00093
00094
00095 parseOptions( rawPacket, sz, 2 );
00096 option.showOptions();
00097 option.openFile( "wb" );
00098
00099 if ( option.fileFactory != NULL ) {
00100 option.fileFactory = new FileFactory();
00101 }
00102
00103
00104
00105
00106
00107
00108 option.build_RRQ_WRQ_TemplatePacket();
00109 option.build_OptionPacket();
00110
00111
00112 if ( OACK_arg == TRUE )
00113 {
00114
00115 TFTP_Packet* pkt = new TFTP_Packet();
00116 pkt->sendOAck();
00117
00118 } else {
00119
00120 TFTP_Packet* pkt = new TFTP_Packet();
00121 pkt->sendAck( 0 );
00122
00123 }
00124 } else {
00125
00126 option.state = TFTP_Error;
00127
00128
00129
00130
00131
00132 SendIllegalOpError( "WRQ Rejected: Your Write Request makes no sense. \n" );
00133 }
00134 }
00135
00136 int TestTimeout( short blockNo )
00137 {
00138 return 1;
00139
00140 if ( blockNo == 2 )
00141 {
00142 if ( option.debug == 0 )
00143 {
00144 option.debug = option.currentTime + 3;
00145 } else
00146 {
00147 if ( option.debug > option.currentTime ) {
00148 printf( "\nLet NOT TIMEOUT !\n");
00149 fflush( stdout );
00150 return 1;
00151 }
00152 }
00153 printf( "\nTIMEOUT TEST!\n");
00154 fflush( stdout );
00155 return 0;
00156 }
00157
00158 return 1;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 void DATA_driver( char* rawPacket, int sz )
00171 {
00172 if ( option.isDATA_Valid() )
00173 {
00174 TFTP_Packet* pktData = new TFTP_Packet();
00175 pktData->createData( rawPacket, sz );
00176
00177
00178
00179
00180
00181 TFTP_Packet* pktAck = new TFTP_Packet();
00182 short blockNo = 0;
00183 memcpy( (char*)&blockNo, rawPacket + 2, 2 );
00184
00185
00186
00187 option.fileFactory->directWrite( pktData );
00188
00189 sched_yield();
00190
00191 if ( TestTimeout( blockNo ) )
00192 {
00193 printf("Sending Ack for block number %d\n",blockNo);
00194 fflush(stdout);
00195 pktAck->sendAck( blockNo );
00196 }
00197
00198 if (option.isEnding())
00199 {
00200 exit( 0);
00201 }
00202 } else {
00203
00204 option.state = TFTP_Error;
00205
00206
00207 SendIllegalOpError( "DATA Rejected: Your NOT allowed yet to send data. \n" );
00208 }
00209 }
00210
00211
00212
00213
00214 void ACK_driver( char* rawPacket, int sz )
00215 {
00216 if ( option.isACK_Valid() )
00217 {
00218 short blockNo = 0;
00219 memcpy( (char*)&blockNo, rawPacket+2, 2 );
00220
00221
00222 if ( option.isClient() && option.state == TFTP_Writing )
00223 {
00224 if ( option.dataFactory == NULL ) {
00225 option.dataFactory = new DataFactory();
00226 }
00227
00228
00229 option.dataFactory->run();
00230 }
00231
00232 if ( blockNo == 0 && \
00233 ( ( option.isServer() && option.state == TFTP_Reading ) || \
00234 ( option.isClient() && option.state == TFTP_Writing ) ) )
00235 {
00236 if ( option.sendFactory != NULL ) {
00237 option.sendFactory = new TFTP_Sender();
00238 }
00239
00240 option.sendFactory->start();
00241
00242
00243
00244
00245
00246
00247 }
00248
00249
00250
00251 if (blockNo == option.LastData && blockNo != 0 )
00252 {
00253 printf("Got the ACK of the last packet. \n");
00254 option.state= TFTP_Finished;
00255
00256 }
00257
00258 if ( blockNo > 0 ) {
00259 TFTP_DataAck clean( blockNo );
00260 clean.run();
00261 option.windowsizeLeft++;
00262 printf("I received an acknowledgment, for block number %d\n",blockNo);
00263
00264
00265
00266
00267 }
00268
00269
00270 if ( option.isClient() && option.state == TFTP_WritingNotAck ) {
00271
00272
00273 }
00274
00275 if ( option.isServer() && option.isReading() ) {
00276
00277 }
00278
00279 }else {
00280 option.state = TFTP_Error;
00281
00282 SendIllegalOpError( "ACK Rejected ?! \n" );
00283 }
00284 }
00285
00286
00287
00288 void ERROR_driver( char* rawPacket, int sz )
00289 {
00290
00291 option.state = TFTP_Finished;
00292 exit( 0 );
00293 }
00294
00295
00296
00297
00298 void OACK_driver( char* rawPacket, int sz )
00299 {
00300 if ( ( option.state == TFTP_ReadingNotAck || \
00301 option.state == TFTP_WritingNotAck ) && \
00302 option.isClient() )
00303 {
00304
00305
00306
00307 savedOptions.save();
00308
00309
00310 parseOptions( rawPacket, sz, 2 );
00311
00312 if ( option.state == TFTP_ReadingNotAck ) {
00313 option.state = TFTP_Reading;
00314
00315 TFTP_Packet* pkt = new TFTP_Packet();
00316 pkt->sendAck( 0 );
00317 }
00318
00319 if ( option.state == TFTP_WritingNotAck ) {
00320 option.state = TFTP_Writing;
00321
00322
00323
00324 if ( option.dataFactory != NULL ) {
00325 option.dataFactory = new DataFactory();
00326 }
00327
00328 option.dataFactory->run();
00329
00330
00331 sched_yield();
00332
00333 if ( option.sendFactory != NULL ) {
00334 option.sendFactory = new TFTP_Sender();
00335 }
00336
00337 option.sendFactory->run();
00338
00339
00340
00341
00342
00343
00344
00345 }
00346
00347 } else {
00348
00349 option.state = TFTP_Error;
00350
00351 SendIllegalOpError( "OACK Rejected. \n" );
00352 }
00353 }
00354
00355 void opcodeDispatcher( char* rawPacket, int sz )
00356 {
00357
00358
00359
00360
00361
00362
00363
00364 short op;
00365
00366 memcpy( (char*)&op, rawPacket, 2 );
00367
00368 if ( op >= 1 && op <= 6 )
00369 {
00370 char* rawCopy = new char[ sz + 1 ];
00371 memcpy( rawCopy, rawPacket, sz );
00372
00373
00374 switch( op )
00375 {
00376 case 1: RRQ_driver( rawCopy, sz ); break;
00377 case 2: WRQ_driver( rawCopy, sz ); break;
00378 case 3: DATA_driver( rawCopy, sz ); break;
00379 case 4: ACK_driver( rawCopy, sz ); break;
00380 case 5: ERROR_driver( rawCopy, sz ); break;
00381 case 6: OACK_driver( rawCopy, sz ); break;
00382 }
00383
00384
00385
00386 } else {
00387
00388 char buf[80];
00389 sprintf( buf, "Invalid Opcode: %d\n", op );
00390 SendIllegalOpError( buf );
00391 }
00392 }
00393
00394 #endif