]> sjero.net Git - iperf/blob - src/Reporter.c
Actually add CCID selection ability
[iperf] / src / Reporter.c
1 /*--------------------------------------------------------------- 
2  * Copyright (c) 1999,2000,2001,2002,2003                              
3  * The Board of Trustees of the University of Illinois            
4  * All Rights Reserved.                                           
5  *--------------------------------------------------------------- 
6  * Permission is hereby granted, free of charge, to any person    
7  * obtaining a copy of this software (Iperf) and associated       
8  * documentation files (the "Software"), to deal in the Software  
9  * without restriction, including without limitation the          
10  * rights to use, copy, modify, merge, publish, distribute,        
11  * sublicense, and/or sell copies of the Software, and to permit     
12  * persons to whom the Software is furnished to do
13  * so, subject to the following conditions: 
14  *
15  *     
16  * Redistributions of source code must retain the above 
17  * copyright notice, this list of conditions and 
18  * the following disclaimers. 
19  *
20  *     
21  * Redistributions in binary form must reproduce the above 
22  * copyright notice, this list of conditions and the following 
23  * disclaimers in the documentation and/or other materials 
24  * provided with the distribution. 
25  * 
26  *     
27  * Neither the names of the University of Illinois, NCSA, 
28  * nor the names of its contributors may be used to endorse 
29  * or promote products derived from this Software without
30  * specific prior written permission. 
31  * 
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
34  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT 
36  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
37  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
38  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
40  * ________________________________________________________________
41  * National Laboratory for Applied Network Research 
42  * National Center for Supercomputing Applications 
43  * University of Illinois at Urbana-Champaign 
44  * http://www.ncsa.uiuc.edu
45  * ________________________________________________________________ 
46  *
47  * Reporter.c
48  * by Kevin Gibbs <kgibbs@nlanr.net>
49  *
50  * ________________________________________________________________ */
51
52 #include "headers.h"
53 #include "Settings.hpp"
54 #include "util.h"
55 #include "Reporter.h"
56 #include "Thread.h"
57 #include "Locale.h"
58 #include "PerfSocket.hpp"
59 #include "SocketAddr.h"
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 /*
66   The following 4 functions are provided for Reporting
67   styles that do not have all the reporting formats. For
68   instance the provided CSV format does not have a settings
69   report so it uses settings_notimpl.
70   */
71 void* connection_notimpl( Connection_Info * nused, int nuse ) { 
72     return NULL; 
73 }
74 void settings_notimpl( ReporterData * nused ) { }
75 void statistics_notimpl( Transfer_Info * nused ) { }
76 void serverstatistics_notimpl( Connection_Info *nused1, Transfer_Info *nused2 ) { }
77
78 // To add a reporting style include its header here.
79 #include "report_default.h"
80 #include "report_CSV.h"
81
82 // The following array of report structs contains the
83 // pointers required for reporting in different reporting
84 // styles. To add a reporting style add a report struct
85 // below.
86 report_connection connection_reports[kReport_MAXIMUM] = {
87     reporter_reportpeer,
88     CSV_peer
89 };
90
91 report_settings settings_reports[kReport_MAXIMUM] = {
92     reporter_reportsettings,
93     settings_notimpl
94 };
95
96 report_statistics statistics_reports[kReport_MAXIMUM] = {
97     reporter_printstats,
98     CSV_stats
99 };
100
101 report_serverstatistics serverstatistics_reports[kReport_MAXIMUM] = {
102     reporter_serverstats,
103     CSV_serverstats
104 };
105
106 report_statistics multiple_reports[kReport_MAXIMUM] = {
107     reporter_multistats,
108     CSV_stats
109 };
110
111 char buffer[64]; // Buffer for printing
112 ReportHeader *ReportRoot = NULL;
113 extern Condition ReportCond;
114 extern Condition ReportDoneCond;
115 int reporter_process_report ( ReportHeader *report );
116 void process_report ( ReportHeader *report );
117 int reporter_handle_packet( ReportHeader *report );
118 int reporter_condprintstats( ReporterData *stats, MultiHeader *multireport, int force );
119 int reporter_print( ReporterData *stats, int type, int end );
120 void PrintMSS( ReporterData *stats );
121
122 MultiHeader* InitMulti( thread_Settings *agent, int inID ) {
123     MultiHeader *multihdr = NULL;
124     if ( agent->mThreads > 1 || agent->mThreadMode == kMode_Server ) {
125         if ( isMultipleReport( agent ) ) {
126             multihdr = malloc(sizeof(MultiHeader) +  sizeof(ReporterData) +
127                               NUM_MULTI_SLOTS * sizeof(Transfer_Info));
128         } else {
129             multihdr = malloc(sizeof(MultiHeader));
130         }
131         if ( multihdr != NULL ) {
132             memset( multihdr, 0, sizeof(MultiHeader) );
133             Condition_Initialize( &multihdr->barrier );
134             multihdr->groupID = inID;
135             multihdr->threads = agent->mThreads;
136             if ( isMultipleReport( agent ) ) {
137                 int i;
138                 ReporterData *data = NULL;
139                 multihdr->report = (ReporterData*)(multihdr + 1);
140                 memset(multihdr->report, 0, sizeof(ReporterData));
141                 multihdr->data = (Transfer_Info*)(multihdr->report + 1);
142                 data = multihdr->report;
143                 for ( i = 0; i < NUM_MULTI_SLOTS; i++ ) {
144                     multihdr->data[i].startTime = -1;
145                     multihdr->data[i].transferID = inID;
146                     multihdr->data[i].groupID = -2;
147                 }
148                 data->type = TRANSFER_REPORT;
149                 if ( agent->mInterval != 0.0 ) {
150                     struct timeval *interval = &data->intervalTime;
151                     interval->tv_sec = (long) agent->mInterval;
152                     interval->tv_usec = (long) ((agent->mInterval - interval->tv_sec) 
153                                                 * rMillion);
154                 }
155                 data->mHost = agent->mHost;
156                 data->mLocalhost = agent->mLocalhost;
157                 data->mBufLen = agent->mBufLen;
158                 data->mMSS = agent->mMSS;
159                 data->mWinSize = agent->mWinSize;
160                 data->flags = agent->flags;
161                 data->mProtocol = agent->mProtocol;
162                 data->mThreadMode = agent->mThreadMode;
163                 data->mode = agent->mReportMode;
164                 data->info.mFormat = agent->mFormat;
165                 data->info.mTTL = agent->mTTL;
166                 if ( isPacketOriented( agent ) ) {
167                     multihdr->report->info.mUDP = (char)agent->mThreadMode;
168                 }
169                 if ( isConnectionReport( agent ) ) {
170                     data->type |= CONNECTION_REPORT;
171                     data->connection.peer = agent->peer;
172                     data->connection.local = agent->local;
173                 }
174             }
175         } else {
176             FAIL(1, "Out of Memory!!\n", agent);
177         }
178     }
179     return multihdr;
180 }
181
182 /*
183  * BarrierClient allows for multiple stream clients to be syncronized
184  */
185 void BarrierClient( ReportHeader *agent ) {
186     Condition_Lock(agent->multireport->barrier);
187     agent->multireport->threads--;
188     if ( agent->multireport->threads == 0 ) {
189         // last one set time and wake up everyone
190         gettimeofday( &(agent->multireport->startTime), NULL );
191         Condition_Broadcast( &agent->multireport->barrier );
192     } else {
193         Condition_Wait( &agent->multireport->barrier );
194     }
195     agent->multireport->threads++;
196     Condition_Unlock( agent->multireport->barrier );
197     agent->report.startTime = agent->multireport->startTime;
198     agent->report.nextTime = agent->report.startTime;
199     TimeAdd( agent->report.nextTime, agent->report.intervalTime );
200 }
201
202 /*
203  * InitReport is called by a transfer agent (client or
204  * server) to setup the needed structures to communicate
205  * traffic.
206  */
207 ReportHeader* InitReport( thread_Settings *agent ) {
208     ReportHeader *reporthdr = NULL;
209     ReporterData *data = NULL;
210     if ( isDataReport( agent ) ) {
211         /*
212          * Create in one big chunk
213          */
214         reporthdr = malloc( sizeof(ReportHeader) +
215                             NUM_REPORT_STRUCTS * sizeof(ReportStruct) );
216         if ( reporthdr != NULL ) {
217             // Only need to make sure the headers are clean
218             memset( reporthdr, 0, sizeof(ReportHeader));
219             reporthdr->data = (ReportStruct*)(reporthdr+1);
220             reporthdr->multireport = agent->multihdr;
221             data = &reporthdr->report;
222             reporthdr->reporterindex = NUM_REPORT_STRUCTS - 1;
223             data->info.transferID = agent->mSock;
224             data->info.groupID = (agent->multihdr != NULL ? agent->multihdr->groupID 
225                                                           : -1);
226             data->info.congAlgo = agent->congAlgo;
227             data->type = TRANSFER_REPORT;
228             if ( agent->mInterval != 0.0 ) {
229                 struct timeval *interval = &data->intervalTime;
230                 interval->tv_sec = (long) agent->mInterval;
231                 interval->tv_usec = (long) ((agent->mInterval - interval->tv_sec) 
232                                             * rMillion);
233             }
234             data->mHost = agent->mHost;
235             data->mLocalhost = agent->mLocalhost;
236             data->mBufLen = agent->mBufLen;
237             data->mMSS = agent->mMSS;
238             data->mWinSize = agent->mWinSize;
239             data->flags = agent->flags;
240             data->mProtocol = agent->mProtocol;
241             data->mThreadMode = agent->mThreadMode;
242             data->mode = agent->mReportMode;
243             data->info.mFormat = agent->mFormat;
244             data->info.mTTL = agent->mTTL;
245             if ( isPacketOriented( agent ) ) {
246                 reporthdr->report.info.mUDP = (char)agent->mThreadMode;
247             }
248         } else {
249             FAIL(1, "Out of Memory!!\n", agent);
250         }
251     }
252     if ( isConnectionReport( agent ) ) {
253         if ( reporthdr == NULL ) {
254             /*
255              * Create in one big chunk
256              */
257             reporthdr = malloc( sizeof(ReportHeader) );
258             if ( reporthdr != NULL ) {
259                 // Only need to make sure the headers are clean
260                 memset( reporthdr, 0, sizeof(ReportHeader));
261                 data = &reporthdr->report;
262                 data->info.transferID = agent->mSock;
263                 data->info.groupID = -1;
264                 data->info.congAlgo = agent->congAlgo;
265             } else {
266                 FAIL(1, "Out of Memory!!\n", agent);
267             }
268         }
269         if ( reporthdr != NULL ) {
270             data->type |= CONNECTION_REPORT;
271             data->connection.peer = agent->peer;
272             data->connection.local = agent->local;
273         } else {
274             FAIL(1, "Out of Memory!!\n", agent);
275         }
276     }
277     if ( isConnectionReport( agent ) || isDataReport( agent ) ) {
278
279 #ifdef HAVE_THREAD
280         /*
281          * Update the ReportRoot to include this report.
282          */
283         if ( reporthdr->report.mThreadMode == kMode_Client &&
284              reporthdr->multireport != NULL ) {
285             // syncronize watches on my mark......
286             BarrierClient( reporthdr );
287         } else {
288             if ( reporthdr->multireport != NULL && isMultipleReport( agent )) {
289                 reporthdr->multireport->threads++;
290                 if ( reporthdr->multireport->report->startTime.tv_sec == 0 ) {
291                     gettimeofday( &(reporthdr->multireport->report->startTime), NULL );
292                 }
293                 reporthdr->report.startTime = reporthdr->multireport->report->startTime;
294             } else {
295                 // set start time
296                 gettimeofday( &(reporthdr->report.startTime), NULL );
297             }
298             reporthdr->report.nextTime = reporthdr->report.startTime;
299             TimeAdd( reporthdr->report.nextTime, reporthdr->report.intervalTime );
300         }
301         Condition_Lock( ReportCond );
302         reporthdr->next = ReportRoot;
303         ReportRoot = reporthdr;
304         Condition_Signal( &ReportCond );
305         Condition_Unlock( ReportCond );
306 #else
307         // set start time
308         gettimeofday( &(reporthdr->report.startTime), NULL );
309         /*
310          * Process the report in this thread
311          */
312         reporthdr->next = NULL;
313         process_report ( reporthdr );
314 #endif 
315     }
316     if ( !isDataReport( agent ) ) {
317         reporthdr = NULL;
318     }
319     return reporthdr;
320 }
321
322 /*
323  * ReportPacket is called by a transfer agent to record
324  * the arrival or departure of a "packet" (for TCP it 
325  * will actually represent many packets). This needs to
326  * be as simple and fast as possible as it gets called for
327  * every "packet".
328  */
329 void ReportPacket( ReportHeader* agent, ReportStruct *packet ) {
330     if ( agent != NULL ) {
331         int index = agent->reporterindex;
332         /*
333          * First find the appropriate place to put the information
334          */
335         if ( agent->agentindex == NUM_REPORT_STRUCTS ) {
336             // Just need to make sure that reporter is not on the first
337             // item
338             while ( index == 0 ) {
339                 Condition_Signal( &ReportCond );
340                 Condition_Wait( &ReportDoneCond );
341                 index = agent->reporterindex;
342             }
343             agent->agentindex = 0;
344         }
345         // Need to make sure that reporter is not about to be "lapped"
346         while ( index - 1 == agent->agentindex ) {
347             Condition_Signal( &ReportCond );
348             Condition_Wait( &ReportDoneCond );
349             index = agent->reporterindex;
350         }
351         
352         // Put the information there
353         memcpy( agent->data + agent->agentindex, packet, sizeof(ReportStruct) );
354         
355         // Updating agentindex MUST be the last thing done
356         agent->agentindex++;
357 #ifndef HAVE_THREAD
358         /*
359          * Process the report in this thread
360          */
361         process_report ( agent );
362 #endif 
363     }
364 }
365
366 /*
367  * CloseReport is called by a transfer agent to finalize
368  * the report and signal transfer is over.
369  */
370 void CloseReport( ReportHeader *agent, ReportStruct *packet ) {
371     if ( agent != NULL) {
372         /*
373          * Using PacketID of -1 ends reporting
374          */
375         packet->packetID = -1;
376         packet->packetLen = 0;
377         ReportPacket( agent, packet );
378         packet->packetID = agent->report.cntDatagrams;
379     }
380 }
381
382 /*
383  * EndReport signifies the agent no longer is interested
384  * in the report. Calls to GetReport will no longer be
385  * filled
386  */
387 void EndReport( ReportHeader *agent ) {
388     if ( agent != NULL ) {
389         int index = agent->reporterindex;
390         while ( index != -1 ) {
391             thread_rest();
392             index = agent->reporterindex;
393         }
394         agent->agentindex = -1;
395 #ifndef HAVE_THREAD
396         /*
397          * Process the report in this thread
398          */
399         process_report ( agent );
400 #endif
401     }
402 }
403
404 /*
405  * GetReport is called by the agent after a CloseReport
406  * but before an EndReport to get the stats generated
407  * by the reporter thread.
408  */
409 Transfer_Info *GetReport( ReportHeader *agent ) {
410     int index = agent->reporterindex;
411     while ( index != -1 ) {
412         thread_rest();
413         index = agent->reporterindex;
414     }
415     return &agent->report.info;
416 }
417
418 /*
419  * ReportSettings will generate a summary report for
420  * settings being used with Listeners or Clients
421  */
422 void ReportSettings( thread_Settings *agent ) {
423     if ( isSettingsReport( agent ) ) {
424         /*
425          * Create in one big chunk
426          */
427         ReportHeader *reporthdr = malloc( sizeof(ReportHeader) );
428     
429         if ( reporthdr != NULL ) {
430             ReporterData *data = &reporthdr->report;
431             data->info.transferID = agent->mSock;
432             data->info.groupID = -1;
433             reporthdr->agentindex = -1;
434             reporthdr->reporterindex = -1;
435         
436             data->mHost = agent->mHost;
437             data->mLocalhost = agent->mLocalhost;
438             data->mode = agent->mReportMode;
439             data->type = SETTINGS_REPORT;
440             data->mBufLen = agent->mBufLen;
441             data->mMSS = agent->mMSS;
442             data->mWinSize = agent->mWinSize;
443             data->flags = agent->flags;
444             data->mProtocol = agent->mProtocol;
445             data->mThreadMode = agent->mThreadMode;
446             data->mPort = agent->mPort;
447             data->mMcastIface = agent->mMcastIface;
448             data->info.mFormat = agent->mFormat;
449             data->info.mTTL = agent->mTTL;
450             data->info.congAlgo = agent->congAlgo;
451             data->connection.peer = agent->peer;
452             data->connection.local = agent->local;
453             data->mCCID=agent->mCCID;
454     
455     #ifdef HAVE_THREAD
456             /*
457              * Update the ReportRoot to include this report.
458              */
459             Condition_Lock( ReportCond );
460             reporthdr->next = ReportRoot;
461             ReportRoot = reporthdr;
462             Condition_Signal( &ReportCond );
463             Condition_Unlock( ReportCond );
464     #else
465             /*
466              * Process the report in this thread
467              */
468             reporthdr->next = NULL;
469             process_report ( reporthdr );
470     #endif 
471         } else {
472             FAIL(1, "Out of Memory!!\n", agent);
473         }
474     }
475 }
476
477 /*
478  * ReportServerUDP will generate a report of the UDP
479  * statistics as reported by the server on the client
480  * side.
481  */
482 void ReportServerUDP( thread_Settings *agent, server_hdr *server ) {
483     if ( (ntohl(server->flags) & HEADER_VERSION1) != 0 &&
484          isServerReport( agent ) ) {
485         /*
486          * Create in one big chunk
487          */
488         ReportHeader *reporthdr = malloc( sizeof(ReportHeader) );
489         Transfer_Info *stats = &reporthdr->report.info;
490
491         if ( reporthdr != NULL ) {
492             stats->transferID = agent->mSock;
493             stats->groupID = (agent->multihdr != NULL ? agent->multihdr->groupID 
494                                                       : -1);
495             reporthdr->agentindex = -1;
496             reporthdr->reporterindex = -1;
497
498             reporthdr->report.type = SERVER_RELAY_REPORT;
499             reporthdr->report.mode = agent->mReportMode;
500             stats->mFormat = agent->mFormat;
501             stats->jitter = ntohl( server->jitter1 );
502             stats->jitter += ntohl( server->jitter2 ) / (double)rMillion;
503             stats->TotalLen = (((max_size_t) ntohl( server->total_len1 )) << 32) +
504                                   ntohl( server->total_len2 ); 
505             stats->startTime = 0;
506             stats->endTime = ntohl( server->stop_sec );
507             stats->endTime += ntohl( server->stop_usec ) / (double)rMillion;
508             stats->cntError = ntohl( server->error_cnt );
509             stats->cntOutofOrder = ntohl( server->outorder_cnt );
510             stats->cntDatagrams = ntohl( server->datagrams );
511             stats->mUDP = (char)kMode_Server;
512             reporthdr->report.connection.peer = agent->local;
513             reporthdr->report.connection.local = agent->peer;
514             
515 #ifdef HAVE_THREAD
516             /*
517              * Update the ReportRoot to include this report.
518              */
519             Condition_Lock( ReportCond );
520             reporthdr->next = ReportRoot;
521             ReportRoot = reporthdr;
522             Condition_Signal( &ReportCond );
523             Condition_Unlock( ReportCond );
524 #else
525             /*
526              * Process the report in this thread
527              */
528             reporthdr->next = NULL;
529             process_report ( reporthdr );
530 #endif 
531         } else {
532             FAIL(1, "Out of Memory!!\n", agent);
533         }
534     }
535 }
536
537 /*
538  * This function is called only when the reporter thread
539  * This function is the loop that the reporter thread processes
540  */
541 void reporter_spawn( thread_Settings *thread ) {
542     do {
543         // This section allows for safe exiting with Ctrl-C
544         Condition_Lock ( ReportCond );
545         if ( ReportRoot == NULL ) {
546             // Allow main thread to exit if Ctrl-C is received
547             thread_setignore();
548             Condition_Wait ( &ReportCond );
549             // Stop main thread from exiting until done with all reports
550             thread_unsetignore();
551         }
552         Condition_Unlock ( ReportCond );
553
554 again:
555         if ( ReportRoot != NULL ) {
556             ReportHeader *temp = ReportRoot;
557             //Condition_Unlock ( ReportCond );
558             if ( reporter_process_report ( temp ) ) {
559                 // This section allows for more reports to be added while
560                 // the reporter is processing reports without needing to
561                 // stop the reporter or immediately notify it
562                 Condition_Lock ( ReportCond );
563                 if ( temp == ReportRoot ) {
564                     // no new reports
565                     ReportRoot = temp->next;
566                 } else {
567                     // new reports added
568                     ReportHeader *itr = ReportRoot;
569                     while ( itr->next != temp ) {
570                         itr = itr->next;
571                     }
572                     itr->next = temp->next;
573                 }
574                 // finished with report so free it
575                 free( temp );
576                 Condition_Unlock ( ReportCond );
577                 Condition_Signal( &ReportDoneCond );
578                 if (ReportRoot)
579                     goto again;
580             }
581             Condition_Signal( &ReportDoneCond );
582             usleep(10000);
583         } else {
584             //Condition_Unlock ( ReportCond );
585         }
586     } while ( 1 );
587 }
588
589 /*
590  * Used for single threaded reporting
591  */
592 void process_report ( ReportHeader *report ) {
593     if ( report != NULL ) {
594         if ( reporter_process_report( report ) ) {
595             free( report );
596         }
597     }
598 }
599
600 /*
601  * Process reports starting with "reporthdr"
602  */
603 int reporter_process_report ( ReportHeader *reporthdr ) {
604     int need_free = 0;
605
606     // Recursively process reports
607     if ( reporthdr->next != NULL ) {
608         if ( reporter_process_report( reporthdr->next ) ) {
609             // If we are done with this report then free it
610             ReportHeader *temp = reporthdr->next;
611             reporthdr->next = reporthdr->next->next;
612             free( temp );
613         }
614     }
615
616     if ( (reporthdr->report.type & SETTINGS_REPORT) != 0 ) {
617         reporthdr->report.type &= ~SETTINGS_REPORT;
618         return reporter_print( &reporthdr->report, SETTINGS_REPORT, 1 );
619     } else if ( (reporthdr->report.type & CONNECTION_REPORT) != 0 ) {
620         reporthdr->report.type &= ~CONNECTION_REPORT;
621         reporter_print( &reporthdr->report, CONNECTION_REPORT,
622                                (reporthdr->report.type == 0 ? 1 : 0) );
623         if ( reporthdr->multireport != NULL && isMultipleReport( (&reporthdr->report) )) {
624             if ( (reporthdr->multireport->report->type & CONNECTION_REPORT) != 0 ) {
625                 reporthdr->multireport->report->type &= ~CONNECTION_REPORT;
626                 reporter_print( reporthdr->multireport->report, CONNECTION_REPORT,
627                                 (reporthdr->report.type == 0 ? 1 : 0) );
628             }
629         }
630     } else if ( (reporthdr->report.type & SERVER_RELAY_REPORT) != 0 ) {
631         reporthdr->report.type &= ~SERVER_RELAY_REPORT;
632         return reporter_print( &reporthdr->report, SERVER_RELAY_REPORT, 1 );
633     }
634     if ( (reporthdr->report.type & TRANSFER_REPORT) != 0 ) {
635         // If there are more packets to process then handle them
636         if ( reporthdr->reporterindex >= 0 ) {
637             // Need to make sure we do not pass the "agent"
638             while ( reporthdr->reporterindex != reporthdr->agentindex - 1 ) {
639                 if ( reporthdr->reporterindex == NUM_REPORT_STRUCTS - 1 ) {
640                     if ( reporthdr->agentindex == 0 ) {
641                         break;
642                     } else {
643                         reporthdr->reporterindex = 0;
644                     }
645                 } else {
646                     reporthdr->reporterindex++;
647                 }
648                 if ( reporter_handle_packet( reporthdr ) ) {
649                     // No more packets to process
650                     reporthdr->reporterindex = -1;
651                     break;
652                 }
653             }
654         }
655         // If the agent is done with the report then free it
656         if ( reporthdr->agentindex == -1 ) {
657             need_free = 1;
658         }
659     }
660     return need_free;
661 }
662
663 /*
664  * Updates connection stats
665  */
666 int reporter_handle_packet( ReportHeader *reporthdr ) {
667     ReportStruct *packet = &reporthdr->data[reporthdr->reporterindex];
668     ReporterData *data = &reporthdr->report;
669     Transfer_Info *stats = &reporthdr->report.info;
670     int finished = 0;
671
672     // update received amount and time
673     data->TotalLen  += packet->packetLen;
674     data->packetTime = packet->packetTime;
675     data->cntDatagrams++;
676
677     if (packet->packetID < 0) {
678         // This is the last packet (FIN)
679         finished = 1;
680         if (isConnectionLess(&reporthdr->report)) {
681             // connectionless protocols don't count the last payload
682             if (reporthdr->report.mThreadMode == kMode_Client)
683                 data->TotalLen -= packet->packetLen;
684         } else {
685             // connection-oriented protocols dont't count the FIN
686             data->cntDatagrams--;
687         }
688     } else if ( packet->packetID != 0 ) {
689             // UDP or DCCP packet
690             double transit, deltaTransit;
691
692             // from RFC 1889, Real Time Protocol (RTP) 
693             // J = J + ( | D(i-1,i) | - J ) / 16 
694             transit = TimeDifference( packet->packetTime, packet->sentTime );
695             if ( data->lastTransit != 0.0 ) {
696                 deltaTransit = transit - data->lastTransit;
697                 if ( deltaTransit < 0.0 ) {
698                     deltaTransit = -deltaTransit;
699                 }
700                 stats->jitter += (deltaTransit - stats->jitter) / (16.0);
701             }
702             data->lastTransit = transit;
703
704             // packet loss occured if the datagram numbers aren't sequential 
705             if ( packet->packetID != data->PacketID + 1 ) {
706                 if ( packet->packetID < data->PacketID + 1 ) {
707                     data->cntOutofOrder++;
708                 } else {
709                     data->cntError += packet->packetID - data->PacketID - 1;
710                 }
711             }
712             // never decrease datagramID (e.g. if we get an out-of-order packet) 
713             if ( packet->packetID > data->PacketID ) {
714                 data->PacketID = packet->packetID;
715             }
716     }
717     // Print a report if appropriate
718     return reporter_condprintstats( &reporthdr->report, reporthdr->multireport, finished );
719 }
720
721 /*
722  * Handles summing of threads
723  */
724 void reporter_handle_multiple_reports( MultiHeader *reporthdr, Transfer_Info *stats, int force ) {
725     if ( reporthdr != NULL ) {
726         if ( reporthdr->threads > 1 ) {
727             int i;
728             Transfer_Info *current = NULL;
729             // Search for start Time
730             for ( i = 0; i < NUM_MULTI_SLOTS; i++ ) {
731                 current = &reporthdr->data[i];
732                 if ( current->startTime == stats->startTime ) {
733                     break;
734                 }
735             }
736             if ( current->startTime != stats->startTime ) {
737                 // Find first available
738                 for ( i = 0; i < NUM_MULTI_SLOTS; i++ ) {
739                     current = &reporthdr->data[i];
740                     if ( current->startTime < 0 ) {
741                         break;
742                     }
743                 }
744                 current->cntDatagrams = stats->cntDatagrams;
745                 current->cntError = stats->cntError;
746                 current->cntOutofOrder = stats->cntOutofOrder;
747                 current->TotalLen = stats->TotalLen;
748                 current->mFormat = stats->mFormat;
749                 current->endTime = stats->endTime;
750                 current->jitter = stats->jitter;
751                 current->startTime = stats->startTime;
752                 current->free = 1;
753             } else {
754                 current->cntDatagrams += stats->cntDatagrams;
755                 current->cntError += stats->cntError;
756                 current->cntOutofOrder += stats->cntOutofOrder;
757                 current->TotalLen += stats->TotalLen;
758                 current->mFormat = stats->mFormat;
759                 if ( current->endTime < stats->endTime ) {
760                     current->endTime = stats->endTime;
761                 }
762                 if ( current->jitter < stats->jitter ) {
763                     current->jitter = stats->jitter;
764                 }
765                 current->free++;
766                 if ( current->free == reporthdr->threads ) {
767                     void *reserved = reporthdr->report->info.reserved_delay;
768                     current->free = force;
769                     memcpy( &reporthdr->report->info, current, sizeof(Transfer_Info) );
770                     current->startTime = -1;
771                     reporthdr->report->info.reserved_delay = reserved;
772                     reporter_print( reporthdr->report, MULTIPLE_REPORT, force );
773                 }
774             }
775         }
776     }
777 }
778
779 /*
780  * Prints reports conditionally
781  */
782 int reporter_condprintstats( ReporterData *stats, MultiHeader *multireport, int force ) {
783     if ( force != 0 ) {
784         stats->info.cntOutofOrder = stats->cntOutofOrder;
785         // assume most of the time out-of-order packets are not
786         // duplicate packets, so conditionally subtract them from the lost packets.
787         stats->info.cntError = stats->cntError;
788         if ( stats->info.cntError > stats->info.cntOutofOrder ) {
789             stats->info.cntError -= stats->info.cntOutofOrder;
790         }
791         if (isConnectionLess(stats))
792                 stats->info.cntDatagrams = stats->PacketID;
793         else
794                 stats->info.cntDatagrams = stats->cntDatagrams;
795         stats->info.TotalLen  = stats->TotalLen;
796         stats->info.startTime = 0;
797         stats->info.endTime   = TimeDifference( stats->packetTime, stats->startTime );
798         stats->info.free      = 1;
799         reporter_print( stats, TRANSFER_REPORT, force );
800         if ( isMultipleReport(stats) ) {
801             reporter_handle_multiple_reports( multireport, &stats->info, force );
802         }
803     } else while ((stats->intervalTime.tv_sec != 0 || 
804                    stats->intervalTime.tv_usec != 0) && 
805                   TimeDifference( stats->nextTime, 
806                                   stats->packetTime ) < 0 ) {
807         stats->info.cntOutofOrder = stats->cntOutofOrder - stats->lastOutofOrder;
808         stats->lastOutofOrder = stats->cntOutofOrder;
809         // assume most of the time out-of-order packets are not
810         // duplicate packets, so conditionally subtract them from the lost packets.
811         stats->info.cntError = stats->cntError - stats->lastError;
812         if ( stats->info.cntError > stats->info.cntOutofOrder ) {
813             stats->info.cntError -= stats->info.cntOutofOrder;
814         }
815         stats->lastError = stats->cntError;
816         if (isConnectionLess(stats)) {
817                 stats->info.cntDatagrams = stats->PacketID - stats->lastDatagrams;
818                 stats->lastDatagrams     = stats->PacketID;
819         } else {
820                 stats->info.cntDatagrams = stats->cntDatagrams - stats->lastDatagrams;
821                 stats->lastDatagrams     = stats->cntDatagrams;
822         }
823         stats->info.TotalLen = stats->TotalLen - stats->lastTotal;
824         stats->lastTotal = stats->TotalLen;
825         stats->info.startTime = stats->info.endTime;
826         stats->info.endTime = TimeDifference( stats->nextTime, stats->startTime );
827         TimeAdd( stats->nextTime, stats->intervalTime );
828         stats->info.free = 0;
829         reporter_print( stats, TRANSFER_REPORT, force );
830         if ( isMultipleReport(stats) ) {
831             reporter_handle_multiple_reports( multireport, &stats->info, force );
832         }
833     }
834     return force;
835 }
836
837 /*
838  * This function handles multiple format printing by sending to the
839  * appropriate dispatch function
840  */
841 int reporter_print( ReporterData *stats, int type, int end ) {
842     switch ( type ) {
843         case TRANSFER_REPORT:
844             statistics_reports[stats->mode]( &stats->info );
845             if ( end != 0 && isPrintMSS( stats ) && !isConnectionLess( stats ) ) {
846                 PrintMSS( stats );
847             }
848             break;
849         case SERVER_RELAY_REPORT:
850             serverstatistics_reports[stats->mode]( &stats->connection, &stats->info );
851             break;
852         case SETTINGS_REPORT:
853             settings_reports[stats->mode]( stats );
854             break;
855         case CONNECTION_REPORT:
856             stats->info.reserved_delay = connection_reports[stats->mode]( 
857                                                &stats->connection,
858                                                stats->info.transferID );
859             break;
860         case MULTIPLE_REPORT:
861             multiple_reports[stats->mode]( &stats->info );
862             break;
863         default:
864             fprintf( stderr, "Printing type not implemented! No Output\n" );
865     }
866     fflush( stdout );
867     return end;
868 }
869
870 /* -------------------------------------------------------------------
871  * Report the MSS and MTU, given the MSS (or a guess thereof)
872  * This works for connection-oriented protocols only: it expects
873  * the protocol to be either TCP or DCCP and will give error otherwise
874  * ------------------------------------------------------------------- */
875
876 // compare the MSS against the (MTU - 40) to (MTU - 80) bytes.
877 // 40 byte IP header and somewhat arbitrarily, 40 more bytes of IP options.
878
879 #define checkMSS_MTU( inMSS, inMTU ) (inMTU-40) >= inMSS  &&  inMSS >= (inMTU-80)
880
881 void PrintMSS( ReporterData *stats )
882 {
883     int inMSS = stats->mProtocol == kProto_TCP
884               ?   getsock_tcp_mss( stats->info.transferID )
885               :   getsock_dccp_mps( stats->info.transferID );
886
887     if ( inMSS <= 0 ) {
888         printf( report_mss_unsupported, stats->info.transferID );
889     } else {
890         char* net;
891         int mtu = 0;
892
893         if ( checkMSS_MTU( inMSS, 1500 ) ) {
894             net = "ethernet";
895             mtu = 1500;
896         } else if ( checkMSS_MTU( inMSS, 4352 ) ) {
897             net = "FDDI";
898             mtu = 4352;
899         } else if ( checkMSS_MTU( inMSS, 9180 ) ) {
900             net = "ATM";
901             mtu = 9180;
902         } else if ( checkMSS_MTU( inMSS, 65280 ) ) {
903             net = "HIPPI";
904             mtu = 65280;
905         } else if ( checkMSS_MTU( inMSS, 576 ) ) {
906             net = "minimum";
907             mtu = 576;
908             printf( warn_no_pathmtu );
909         } else {
910             mtu = inMSS + 40;
911             net = "unknown interface";
912         }
913
914         printf( report_mss,
915                 stats->info.transferID, inMSS, mtu, net );
916     }
917 }
918 // end ReportMSS
919
920 #ifdef __cplusplus
921 } /* end extern "C" */
922 #endif