Client::Client( thread_Settings *inSettings ) { mSettings = inSettings; mBuf = NULL; // initialize buffer mBuf = new char[ mSettings->mBufLen ]; pattern( mBuf, mSettings->mBufLen ); if ( isFileInput( mSettings ) ) { if ( !isSTDIN( mSettings ) ) Extractor_Initialize( mSettings->mFileName, mSettings->mBufLen, mSettings ); else Extractor_InitializeFile( stdin, mSettings->mBufLen, mSettings ); if ( !Extractor_canRead( mSettings ) ) { unsetFileInput( mSettings ); } } // connect Connect( ); if ( isReport( inSettings ) ) { ReportSettings( inSettings ); if ( mSettings->multihdr && isMultipleReport( inSettings ) ) { mSettings->multihdr->report->connection.peer = mSettings->peer; mSettings->multihdr->report->connection.size_peer = mSettings->size_peer; mSettings->multihdr->report->connection.local = mSettings->local; SockAddr_setPortAny( &mSettings->multihdr->report->connection.local ); mSettings->multihdr->report->connection.size_local = mSettings->size_local; } } } // end Client
/* * Prints reports conditionally */ int reporter_condprintstats( ReporterData *stats, MultiHeader *multireport, int force ) { if ( force != 0 ) { stats->info.cntOutofOrder = stats->cntOutofOrder; // assume most of the time out-of-order packets are not // duplicate packets, so conditionally subtract them from the lost packets. stats->info.cntError = stats->cntError; if ( stats->info.cntError > stats->info.cntOutofOrder ) { stats->info.cntError -= stats->info.cntOutofOrder; } stats->info.cntDatagrams = (isUDP(stats) ? stats->PacketID : stats->cntDatagrams); stats->info.TotalLen = stats->TotalLen; stats->info.startTime = 0; stats->info.endTime = TimeDifference( stats->packetTime, stats->startTime ); stats->info.free = 1; reporter_print( stats, TRANSFER_REPORT, force ); if ( isMultipleReport(stats) ) { reporter_handle_multiple_reports( multireport, &stats->info, force ); } } else while ((stats->intervalTime.tv_sec != 0 || stats->intervalTime.tv_usec != 0) && TimeDifference( stats->nextTime, stats->packetTime ) < 0 ) { stats->info.cntOutofOrder = stats->cntOutofOrder - stats->lastOutofOrder; stats->lastOutofOrder = stats->cntOutofOrder; // assume most of the time out-of-order packets are not // duplicate packets, so conditionally subtract them from the lost packets. stats->info.cntError = stats->cntError - stats->lastError; if ( stats->info.cntError > stats->info.cntOutofOrder ) { stats->info.cntError -= stats->info.cntOutofOrder; } stats->lastError = stats->cntError; stats->info.cntDatagrams = (isUDP( stats ) ? stats->PacketID - stats->lastDatagrams : stats->cntDatagrams - stats->lastDatagrams); stats->lastDatagrams = (isUDP( stats ) ? stats->PacketID : stats->cntDatagrams); stats->info.TotalLen = stats->TotalLen - stats->lastTotal; stats->lastTotal = stats->TotalLen; stats->info.startTime = stats->info.endTime; stats->info.endTime = TimeDifference( stats->nextTime, stats->startTime ); TimeAdd( stats->nextTime, stats->intervalTime ); stats->info.free = 0; reporter_print( stats, TRANSFER_REPORT, force ); if ( isMultipleReport(stats) ) { reporter_handle_multiple_reports( multireport, &stats->info, force ); } } return force; }
/* * Process reports starting with "reporthdr" */ int reporter_process_report ( ReportHeader *reporthdr ) { int need_free = 0; // Recursively process reports if ( reporthdr->next != NULL ) { if ( reporter_process_report( reporthdr->next ) ) { // If we are done with this report then free it ReportHeader *temp = reporthdr->next; reporthdr->next = reporthdr->next->next; free( temp ); } } if ( (reporthdr->report.type & SETTINGS_REPORT) != 0 ) { reporthdr->report.type &= ~SETTINGS_REPORT; return reporter_print( &reporthdr->report, SETTINGS_REPORT, 1 ); } else if ( (reporthdr->report.type & CONNECTION_REPORT) != 0 ) { reporthdr->report.type &= ~CONNECTION_REPORT; reporter_print( &reporthdr->report, CONNECTION_REPORT, (reporthdr->report.type == 0 ? 1 : 0) ); if ( reporthdr->multireport != NULL && isMultipleReport( (&reporthdr->report) )) { if ( (reporthdr->multireport->report->type & CONNECTION_REPORT) != 0 ) { reporthdr->multireport->report->type &= ~CONNECTION_REPORT; reporter_print( reporthdr->multireport->report, CONNECTION_REPORT, (reporthdr->report.type == 0 ? 1 : 0) ); } } } else if ( (reporthdr->report.type & SERVER_RELAY_REPORT) != 0 ) { reporthdr->report.type &= ~SERVER_RELAY_REPORT; return reporter_print( &reporthdr->report, SERVER_RELAY_REPORT, 1 ); } if ( (reporthdr->report.type & TRANSFER_REPORT) != 0 ) { // If there are more packets to process then handle them if ( reporthdr->reporterindex >= 0 ) { // Need to make sure we do not pass the "agent" while ( reporthdr->reporterindex != reporthdr->agentindex - 1 ) { if ( reporthdr->reporterindex == NUM_REPORT_STRUCTS - 1 ) { if ( reporthdr->agentindex == 0 ) { break; } else { reporthdr->reporterindex = 0; } } else { reporthdr->reporterindex++; } if ( reporter_handle_packet( reporthdr ) ) { // No more packets to process //reporthdr->reporterindex = -1; break; } } } // If the agent is done with the report then free it if ( reporthdr->agentindex == -1 ) { need_free = 1; } } return need_free; }
Client::Client( thread_Settings *inSettings ) { mSettings = inSettings; mBuf = NULL; // initialize buffer mBuf = new char[ mSettings->mBufLen ]; pattern( mBuf, mSettings->mBufLen ); //strcpy(outFile,mSettings->mLogFileName); pthread_t currentThread = pthread_self(); long tid = (long)currentThread; sprintf(outFileName,"%s%ld",mSettings->mLogFileName,tid); outFile = NULL; outFile = fopen(outFileName,"w"); if ( isFileInput( mSettings ) ) { if ( !isSTDIN( mSettings ) ) Extractor_Initialize( mSettings->mFileName, mSettings->mBufLen, mSettings ); else Extractor_InitializeFile( stdin, mSettings->mBufLen, mSettings ); if ( !Extractor_canRead( mSettings ) ) { unsetFileInput( mSettings ); } } // connect Connect( ); if ( isReport( inSettings ) ) { ReportSettings( inSettings ); if ( mSettings->multihdr && isMultipleReport( inSettings ) ) { mSettings->multihdr->report->connection.peer = mSettings->peer; mSettings->multihdr->report->connection.size_peer = mSettings->size_peer; mSettings->multihdr->report->connection.local = mSettings->local; SockAddr_setPortAny( &mSettings->multihdr->report->connection.local ); mSettings->multihdr->report->connection.size_local = mSettings->size_local; } } } // end Client
/* * Prints reports conditionally */ int reporter_condprintstats( ReporterData *stats, MultiHeader *multireport, int force ) { if ( force ) { #ifdef HAVE_STRUCT_TCP_INFO_TCPI_TOTAL_RETRANS gettcpistats(stats); #endif stats->info.cntOutofOrder = stats->cntOutofOrder; // assume most of the time out-of-order packets are not // duplicate packets, so conditionally subtract them from the lost packets. stats->info.cntError = stats->cntError; if ( stats->info.cntError > stats->info.cntOutofOrder ) { stats->info.cntError -= stats->info.cntOutofOrder; } stats->info.cntDatagrams = ((stats->info.mUDP == kMode_Server) ? stats->PacketID : stats->cntDatagrams); stats->info.TotalLen = stats->TotalLen; stats->info.startTime = 0; stats->info.endTime = TimeDifference( stats->packetTime, stats->startTime ); stats->info.transit.minTransit = stats->info.transit.totminTransit; stats->info.transit.maxTransit = stats->info.transit.totmaxTransit; stats->info.transit.cntTransit = stats->info.transit.totcntTransit; stats->info.transit.sumTransit = stats->info.transit.totsumTransit; stats->info.transit.meanTransit = stats->info.transit.totmeanTransit; stats->info.transit.m2Transit = stats->info.transit.totm2Transit; stats->info.transit.vdTransit = stats->info.transit.totvdTransit; if (stats->info.mTCP == kMode_Client) { stats->info.tcp.write.WriteErr = stats->info.tcp.write.totWriteErr; stats->info.tcp.write.WriteCnt = stats->info.tcp.write.totWriteCnt; stats->info.tcp.write.TCPretry = stats->info.tcp.write.totTCPretry; } if (stats->info.mTCP == kMode_Server) { int ix; stats->info.tcp.read.cntRead = stats->info.tcp.read.totcntRead; for (ix = 0; ix < 8; ix++) { stats->info.tcp.read.bins[ix] = stats->info.tcp.read.totbins[ix]; } } if (stats->info.endTime > 0) { stats->info.IPGcnt = (int) (stats->cntDatagrams / stats->info.endTime); } else { stats->info.IPGcnt = 0; } stats->info.IPGsum = 1; stats->info.free = 1; reporter_print( stats, TRANSFER_REPORT, force ); if ( isMultipleReport(stats) ) { reporter_handle_multiple_reports( multireport, &stats->info, force ); } } else while ((stats->intervalTime.tv_sec != 0 || stats->intervalTime.tv_usec != 0) && TimeDifference( stats->nextTime, stats->packetTime ) < 0 ) { #ifdef HAVE_STRUCT_TCP_INFO_TCPI_TOTAL_RETRANS gettcpistats(stats); #endif stats->info.cntOutofOrder = stats->cntOutofOrder - stats->lastOutofOrder; stats->lastOutofOrder = stats->cntOutofOrder; // assume most of the time out-of-order packets are not // duplicate packets, so conditionally subtract them from the lost packets. stats->info.cntError = stats->cntError - stats->lastError; if ( stats->info.cntError > stats->info.cntOutofOrder ) { stats->info.cntError -= stats->info.cntOutofOrder; } stats->lastError = stats->cntError; stats->info.cntDatagrams = ((stats->info.mUDP == kMode_Server) ? stats->PacketID - stats->lastDatagrams : stats->cntDatagrams - stats->lastDatagrams); stats->lastDatagrams = ((stats->info.mUDP == kMode_Server) ? stats->PacketID : stats->cntDatagrams); stats->info.TotalLen = stats->TotalLen - stats->lastTotal; stats->lastTotal = stats->TotalLen; stats->info.startTime = stats->info.endTime; stats->info.endTime = TimeDifference( stats->nextTime, stats->startTime ); TimeAdd( stats->nextTime, stats->intervalTime ); stats->info.free = 0; reporter_print( stats, TRANSFER_REPORT, force ); if ( isMultipleReport(stats) ) { reporter_handle_multiple_reports( multireport, &stats->info, force ); } // Reset stats used by SUM now that the SUM has been reported if (stats->info.mEnhanced) { if (stats->info.mUDP) { stats->info.IPGcnt = 0; stats->info.IPGsum = 0; } else if (stats->info.mTCP == (char)kMode_Client) { stats->info.tcp.write.WriteCnt = 0; stats->info.tcp.write.WriteErr = 0; } else if (stats->info.mTCP == (char)kMode_Server) { int ix; stats->info.tcp.read.cntRead = 0; for (ix = 0; ix < 8; ix++) { stats->info.tcp.read.bins[ix] = 0; } } } } return force; }
/* * InitReport is called by a transfer agent (client or * server) to setup the needed structures to communicate * traffic. */ ReportHeader* InitReport( thread_Settings *agent ) { ReportHeader *reporthdr = NULL; ReporterData *data = NULL; if ( isDataReport( agent ) ) { /* * Create in one big chunk */ reporthdr = malloc( sizeof(ReportHeader) + NUM_REPORT_STRUCTS * sizeof(ReportStruct) ); if ( reporthdr != NULL ) { // Only need to make sure the headers are clean memset( reporthdr, 0, sizeof(ReportHeader)); reporthdr->data = (ReportStruct*)(reporthdr+1); reporthdr->multireport = agent->multihdr; data = &reporthdr->report; reporthdr->reporterindex = NUM_REPORT_STRUCTS - 1; data->info.transferID = agent->mSock; data->info.groupID = (agent->multihdr != NULL ? agent->multihdr->groupID : 128); data->type = TRANSFER_REPORT; if ( agent->mInterval != 0.0 ) { struct timeval *interval = &data->intervalTime; interval->tv_sec = (long) agent->mInterval; interval->tv_usec = (long) ((agent->mInterval - interval->tv_sec) * rMillion); } data->mHost = agent->mHost; data->mLocalhost = agent->mLocalhost; data->mBufLen = agent->mBufLen; data->mMSS = agent->mMSS; data->mTCPWin = agent->mTCPWin; data->flags = agent->flags; data->mThreadMode = agent->mThreadMode; data->mode = agent->mReportMode; data->info.mFormat = agent->mFormat; data->info.mTTL = agent->mTTL; if (data->mThreadMode == kMode_Server) data->info.tcp.read.binsize = data->mBufLen / 8; if ( isUDP( agent ) ) { gettimeofday(&data->IPGstart, NULL); reporthdr->report.info.mUDP = (char)agent->mThreadMode; } else { reporthdr->report.info.mTCP = (char)agent->mThreadMode; } if ( isEnhanced( agent ) ) { data->info.mEnhanced = 1; } else { data->info.mEnhanced = 0; } } else { FAIL(1, "Out of Memory!!\n", agent); } } if ( isConnectionReport( agent ) ) { if ( reporthdr == NULL ) { /* * Create in one big chunk */ reporthdr = malloc( sizeof(ReportHeader) ); if ( reporthdr != NULL ) { // Only need to make sure the headers are clean memset( reporthdr, 0, sizeof(ReportHeader)); data = &reporthdr->report; data->info.transferID = agent->mSock; data->info.groupID = 128; } else { FAIL(1, "Out of Memory!!\n", agent); } } if ( reporthdr != NULL ) { data->type |= CONNECTION_REPORT; data->connection.peer = agent->peer; data->connection.size_peer = agent->size_peer; data->connection.local = agent->local; data->connection.size_local = agent->size_local; } else { FAIL(1, "Out of Memory!!\n", agent); } } if ( isConnectionReport( agent ) || isDataReport( agent ) ) { #ifdef HAVE_THREAD /* * Update the ReportRoot to include this report. */ if ( reporthdr->report.mThreadMode == kMode_Client && reporthdr->multireport != NULL ) { // syncronize watches on my mark...... BarrierClient( reporthdr ); } else { if ( reporthdr->multireport != NULL && isMultipleReport( agent )) { reporthdr->multireport->threads++; if ( reporthdr->multireport->report->startTime.tv_sec == 0 ) { gettimeofday( &(reporthdr->multireport->report->startTime), NULL ); } reporthdr->report.startTime = reporthdr->multireport->report->startTime; } else { // set start time gettimeofday( &(reporthdr->report.startTime), NULL ); } reporthdr->report.nextTime = reporthdr->report.startTime; TimeAdd( reporthdr->report.nextTime, reporthdr->report.intervalTime ); } Condition_Lock( ReportCond ); reporthdr->next = ReportRoot; ReportRoot = reporthdr; Condition_Signal( &ReportCond ); Condition_Unlock( ReportCond ); #else // set start time gettimeofday( &(reporthdr->report.startTime), NULL ); /* * Process the report in this thread */ reporthdr->next = NULL; process_report ( reporthdr ); #endif } if ( !isDataReport( agent ) ) { reporthdr = NULL; } return reporthdr; }
MultiHeader* InitMulti( thread_Settings *agent, int inID ) { MultiHeader *multihdr = NULL; if ( agent->mThreads > 1 || agent->mThreadMode == kMode_Server ) { if ( isMultipleReport( agent ) ) { multihdr = malloc(sizeof(MultiHeader) + sizeof(ReporterData) + NUM_MULTI_SLOTS * sizeof(Transfer_Info)); } else { multihdr = malloc(sizeof(MultiHeader)); } if ( multihdr != NULL ) { memset( multihdr, 0, sizeof(MultiHeader) ); Condition_Initialize( &multihdr->barrier ); multihdr->groupID = inID; multihdr->threads = agent->mThreads; if ( isMultipleReport( agent ) ) { int i; ReporterData *data = NULL; multihdr->report = (ReporterData*)(multihdr + 1); memset(multihdr->report, 0, sizeof(ReporterData)); multihdr->data = (Transfer_Info*)(multihdr->report + 1); data = multihdr->report; for ( i = 0; i < NUM_MULTI_SLOTS; i++ ) { multihdr->data[i].startTime = -1; multihdr->data[i].transferID = inID; multihdr->data[i].groupID = -2; } data->type = TRANSFER_REPORT; if ( agent->mInterval != 0.0 ) { struct timeval *interval = &data->intervalTime; interval->tv_sec = (long) agent->mInterval; interval->tv_usec = (long) ((agent->mInterval - interval->tv_sec) * rMillion); } data->mHost = agent->mHost; data->mLocalhost = agent->mLocalhost; data->mBufLen = agent->mBufLen; data->mMSS = agent->mMSS; data->mTCPWin = agent->mTCPWin; data->flags = agent->flags; data->mThreadMode = agent->mThreadMode; data->mode = agent->mReportMode; data->info.mFormat = agent->mFormat; data->info.mTTL = agent->mTTL; if (data->mThreadMode == kMode_Server) data->info.tcp.read.binsize = data->mBufLen / 8; if ( isEnhanced( agent ) ) { data->info.mEnhanced = 1; } else { data->info.mEnhanced = 0; } if ( isUDP( agent ) ) { multihdr->report->info.mUDP = (char)agent->mThreadMode; multihdr->report->info.mUDP = 0; } else { multihdr->report->info.mTCP = (char)agent->mThreadMode; } if ( isConnectionReport( agent ) ) { data->type |= CONNECTION_REPORT; data->connection.peer = agent->peer; data->connection.size_peer = agent->size_peer; SockAddr_setPortAny( &data->connection.peer ); data->connection.local = agent->local; data->connection.size_local = agent->size_local; SockAddr_setPortAny( &data->connection.local ); } } } else { FAIL(1, "Out of Memory!!\n", agent); } } return multihdr; }