]> sjero.net Git - iperf/blob - src/Reporter.c
Native IPv6 support for iperf
[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->type = TRANSFER_REPORT;
227             if ( agent->mInterval != 0.0 ) {
228                 struct timeval *interval = &data->intervalTime;
229                 interval->tv_sec = (long) agent->mInterval;
230                 interval->tv_usec = (long) ((agent->mInterval - interval->tv_sec) 
231                                             * rMillion);
232             }
233             data->mHost = agent->mHost;
234             data->mLocalhost = agent->mLocalhost;
235             data->mBufLen = agent->mBufLen;
236             data->mMSS = agent->mMSS;
237             data->mWinSize = agent->mWinSize;
238             data->flags = agent->flags;
239             data->mProtocol = agent->mProtocol;
240             data->mThreadMode = agent->mThreadMode;
241             data->mode = agent->mReportMode;
242             data->info.mFormat = agent->mFormat;
243             data->info.mTTL = agent->mTTL;
244             if ( isPacketOriented( agent ) ) {
245                 reporthdr->report.info.mUDP = (char)agent->mThreadMode;
246             }
247         } else {
248             FAIL(1, "Out of Memory!!\n", agent);
249         }
250     }
251     if ( isConnectionReport( agent ) ) {
252         if ( reporthdr == NULL ) {
253             /*
254              * Create in one big chunk
255              */
256             reporthdr = malloc( sizeof(ReportHeader) );
257             if ( reporthdr != NULL ) {
258                 // Only need to make sure the headers are clean
259                 memset( reporthdr, 0, sizeof(ReportHeader));
260                 data = &reporthdr->report;
261                 data->info.transferID = agent->mSock;
262                 data->info.groupID = -1;
263             } else {
264                 FAIL(1, "Out of Memory!!\n", agent);
265             }
266         }
267         if ( reporthdr != NULL ) {
268             data->type |= CONNECTION_REPORT;
269             data->connection.peer = agent->peer;
270             data->connection.local = agent->local;
271         } else {
272             FAIL(1, "Out of Memory!!\n", agent);
273         }
274     }
275     if ( isConnectionReport( agent ) || isDataReport( agent ) ) {
276
277 #ifdef HAVE_THREAD
278         /*
279          * Update the ReportRoot to include this report.
280          */
281         if ( reporthdr->report.mThreadMode == kMode_Client &&
282              reporthdr->multireport != NULL ) {
283             // syncronize watches on my mark......
284             BarrierClient( reporthdr );
285         } else {
286             if ( reporthdr->multireport != NULL && isMultipleReport( agent )) {
287                 reporthdr->multireport->threads++;
288                 if ( reporthdr->multireport->report->startTime.tv_sec == 0 ) {
289                     gettimeofday( &(reporthdr->multireport->report->startTime), NULL );
290                 }
291                 reporthdr->report.startTime = reporthdr->multireport->report->startTime;
292             } else {
293                 // set start time
294                 gettimeofday( &(reporthdr->report.startTime), NULL );
295             }
296             reporthdr->report.nextTime = reporthdr->report.startTime;
297             TimeAdd( reporthdr->report.nextTime, reporthdr->report.intervalTime );
298         }
299         Condition_Lock( ReportCond );
300         reporthdr->next = ReportRoot;
301         ReportRoot = reporthdr;
302         Condition_Signal( &ReportCond );
303         Condition_Unlock( ReportCond );
304 #else
305         // set start time
306         gettimeofday( &(reporthdr->report.startTime), NULL );
307         /*
308          * Process the report in this thread
309          */
310         reporthdr->next = NULL;
311         process_report ( reporthdr );
312 #endif 
313     }
314     if ( !isDataReport( agent ) ) {
315         reporthdr = NULL;
316     }
317     return reporthdr;
318 }
319
320 /*
321  * ReportPacket is called by a transfer agent to record
322  * the arrival or departure of a "packet" (for TCP it 
323  * will actually represent many packets). This needs to
324  * be as simple and fast as possible as it gets called for
325  * every "packet".
326  */
327 void ReportPacket( ReportHeader* agent, ReportStruct *packet ) {
328     if ( agent != NULL ) {
329         int index = agent->reporterindex;
330         /*
331          * First find the appropriate place to put the information
332          */
333         if ( agent->agentindex == NUM_REPORT_STRUCTS ) {
334             // Just need to make sure that reporter is not on the first
335             // item
336             while ( index == 0 ) {
337                 Condition_Signal( &ReportCond );
338                 Condition_Wait( &ReportDoneCond );
339                 index = agent->reporterindex;
340             }
341             agent->agentindex = 0;
342         }
343         // Need to make sure that reporter is not about to be "lapped"
344         while ( index - 1 == agent->agentindex ) {
345             Condition_Signal( &ReportCond );
346             Condition_Wait( &ReportDoneCond );
347             index = agent->reporterindex;
348         }
349         
350         // Put the information there
351         memcpy( agent->data + agent->agentindex, packet, sizeof(ReportStruct) );
352         
353         // Updating agentindex MUST be the last thing done
354         agent->agentindex++;
355 #ifndef HAVE_THREAD
356         /*
357          * Process the report in this thread
358          */
359         process_report ( agent );
360 #endif 
361     }
362 }
363
364 /*
365  * CloseReport is called by a transfer agent to finalize
366  * the report and signal transfer is over.
367  */
368 void CloseReport( ReportHeader *agent, ReportStruct *packet ) {
369     if ( agent != NULL) {
370         /*
371          * Using PacketID of -1 ends reporting
372          */
373         packet->packetID = -1;
374         packet->packetLen = 0;
375         ReportPacket( agent, packet );
376         packet->packetID = agent->report.cntDatagrams;
377     }
378 }
379
380 /*
381  * EndReport signifies the agent no longer is interested
382  * in the report. Calls to GetReport will no longer be
383  * filled
384  */
385 void EndReport( ReportHeader *agent ) {
386     if ( agent != NULL ) {
387         int index = agent->reporterindex;
388         while ( index != -1 ) {
389             thread_rest();
390             index = agent->reporterindex;
391         }
392         agent->agentindex = -1;
393 #ifndef HAVE_THREAD
394         /*
395          * Process the report in this thread
396          */
397         process_report ( agent );
398 #endif
399     }
400 }
401
402 /*
403  * GetReport is called by the agent after a CloseReport
404  * but before an EndReport to get the stats generated
405  * by the reporter thread.
406  */
407 Transfer_Info *GetReport( ReportHeader *agent ) {
408     int index = agent->reporterindex;
409     while ( index != -1 ) {
410         thread_rest();
411         index = agent->reporterindex;
412     }
413     return &agent->report.info;
414 }
415
416 /*
417  * ReportSettings will generate a summary report for
418  * settings being used with Listeners or Clients
419  */
420 void ReportSettings( thread_Settings *agent ) {
421     if ( isSettingsReport( agent ) ) {
422         /*
423          * Create in one big chunk
424          */
425         ReportHeader *reporthdr = malloc( sizeof(ReportHeader) );
426     
427         if ( reporthdr != NULL ) {
428             ReporterData *data = &reporthdr->report;
429             data->info.transferID = agent->mSock;
430             data->info.groupID = -1;
431             reporthdr->agentindex = -1;
432             reporthdr->reporterindex = -1;
433         
434             data->mHost = agent->mHost;
435             data->mLocalhost = agent->mLocalhost;
436             data->mode = agent->mReportMode;
437             data->type = SETTINGS_REPORT;
438             data->mBufLen = agent->mBufLen;
439             data->mMSS = agent->mMSS;
440             data->mWinSize = agent->mWinSize;
441             data->flags = agent->flags;
442             data->mProtocol = agent->mProtocol;
443             data->mThreadMode = agent->mThreadMode;
444             data->mPort = agent->mPort;
445             data->mMcastIface = agent->mMcastIface;
446             data->info.mFormat = agent->mFormat;
447             data->info.mTTL = agent->mTTL;
448             data->connection.peer = agent->peer;
449             data->connection.local = agent->local;
450     
451     #ifdef HAVE_THREAD
452             /*
453              * Update the ReportRoot to include this report.
454              */
455             Condition_Lock( ReportCond );
456             reporthdr->next = ReportRoot;
457             ReportRoot = reporthdr;
458             Condition_Signal( &ReportCond );
459             Condition_Unlock( ReportCond );
460     #else
461             /*
462              * Process the report in this thread
463              */
464             reporthdr->next = NULL;
465             process_report ( reporthdr );
466     #endif 
467         } else {
468             FAIL(1, "Out of Memory!!\n", agent);
469         }
470     }
471 }
472
473 /*
474  * ReportServerUDP will generate a report of the UDP
475  * statistics as reported by the server on the client
476  * side.
477  */
478 void ReportServerUDP( thread_Settings *agent, server_hdr *server ) {
479     if ( (ntohl(server->flags) & HEADER_VERSION1) != 0 &&
480          isServerReport( agent ) ) {
481         /*
482          * Create in one big chunk
483          */
484         ReportHeader *reporthdr = malloc( sizeof(ReportHeader) );
485         Transfer_Info *stats = &reporthdr->report.info;
486
487         if ( reporthdr != NULL ) {
488             stats->transferID = agent->mSock;
489             stats->groupID = (agent->multihdr != NULL ? agent->multihdr->groupID 
490                                                       : -1);
491             reporthdr->agentindex = -1;
492             reporthdr->reporterindex = -1;
493
494             reporthdr->report.type = SERVER_RELAY_REPORT;
495             reporthdr->report.mode = agent->mReportMode;
496             stats->mFormat = agent->mFormat;
497             stats->jitter = ntohl( server->jitter1 );
498             stats->jitter += ntohl( server->jitter2 ) / (double)rMillion;
499             stats->TotalLen = (((max_size_t) ntohl( server->total_len1 )) << 32) +
500                                   ntohl( server->total_len2 ); 
501             stats->startTime = 0;
502             stats->endTime = ntohl( server->stop_sec );
503             stats->endTime += ntohl( server->stop_usec ) / (double)rMillion;
504             stats->cntError = ntohl( server->error_cnt );
505             stats->cntOutofOrder = ntohl( server->outorder_cnt );
506             stats->cntDatagrams = ntohl( server->datagrams );
507             stats->mUDP = (char)kMode_Server;
508             reporthdr->report.connection.peer = agent->local;
509             reporthdr->report.connection.local = agent->peer;
510             
511 #ifdef HAVE_THREAD
512             /*
513              * Update the ReportRoot to include this report.
514              */
515             Condition_Lock( ReportCond );
516             reporthdr->next = ReportRoot;
517             ReportRoot = reporthdr;
518             Condition_Signal( &ReportCond );
519             Condition_Unlock( ReportCond );
520 #else
521             /*
522              * Process the report in this thread
523              */
524             reporthdr->next = NULL;
525             process_report ( reporthdr );
526 #endif 
527         } else {
528             FAIL(1, "Out of Memory!!\n", agent);
529         }
530     }
531 }
532
533 /*
534  * This function is called only when the reporter thread
535  * This function is the loop that the reporter thread processes
536  */
537 void reporter_spawn( thread_Settings *thread ) {
538     do {
539         // This section allows for safe exiting with Ctrl-C
540         Condition_Lock ( ReportCond );
541         if ( ReportRoot == NULL ) {
542             // Allow main thread to exit if Ctrl-C is received
543             thread_setignore();
544             Condition_Wait ( &ReportCond );
545             // Stop main thread from exiting until done with all reports
546             thread_unsetignore();
547         }
548         Condition_Unlock ( ReportCond );
549
550 again:
551         if ( ReportRoot != NULL ) {
552             ReportHeader *temp = ReportRoot;
553             //Condition_Unlock ( ReportCond );
554             if ( reporter_process_report ( temp ) ) {
555                 // This section allows for more reports to be added while
556                 // the reporter is processing reports without needing to
557                 // stop the reporter or immediately notify it
558                 Condition_Lock ( ReportCond );
559                 if ( temp == ReportRoot ) {
560                     // no new reports
561                     ReportRoot = temp->next;
562                 } else {
563                     // new reports added
564                     ReportHeader *itr = ReportRoot;
565                     while ( itr->next != temp ) {
566                         itr = itr->next;
567                     }
568                     itr->next = temp->next;
569                 }
570                 // finished with report so free it
571                 free( temp );
572                 Condition_Unlock ( ReportCond );
573                 Condition_Signal( &ReportDoneCond );
574                 if (ReportRoot)
575                     goto again;
576             }
577             Condition_Signal( &ReportDoneCond );
578             usleep(10000);
579         } else {
580             //Condition_Unlock ( ReportCond );
581         }
582     } while ( 1 );
583 }
584
585 /*
586  * Used for single threaded reporting
587  */
588 void process_report ( ReportHeader *report ) {
589     if ( report != NULL ) {
590         if ( reporter_process_report( report ) ) {
591             free( report );
592         }
593     }
594 }
595
596 /*
597  * Process reports starting with "reporthdr"
598  */
599 int reporter_process_report ( ReportHeader *reporthdr ) {
600     int need_free = 0;
601
602     // Recursively process reports
603     if ( reporthdr->next != NULL ) {
604         if ( reporter_process_report( reporthdr->next ) ) {
605             // If we are done with this report then free it
606             ReportHeader *temp = reporthdr->next;
607             reporthdr->next = reporthdr->next->next;
608             free( temp );
609         }
610     }
611
612     if ( (reporthdr->report.type & SETTINGS_REPORT) != 0 ) {
613         reporthdr->report.type &= ~SETTINGS_REPORT;
614         return reporter_print( &reporthdr->report, SETTINGS_REPORT, 1 );
615     } else if ( (reporthdr->report.type & CONNECTION_REPORT) != 0 ) {
616         reporthdr->report.type &= ~CONNECTION_REPORT;
617         reporter_print( &reporthdr->report, CONNECTION_REPORT,
618                                (reporthdr->report.type == 0 ? 1 : 0) );
619         if ( reporthdr->multireport != NULL && isMultipleReport( (&reporthdr->report) )) {
620             if ( (reporthdr->multireport->report->type & CONNECTION_REPORT) != 0 ) {
621                 reporthdr->multireport->report->type &= ~CONNECTION_REPORT;
622                 reporter_print( reporthdr->multireport->report, CONNECTION_REPORT,
623                                 (reporthdr->report.type == 0 ? 1 : 0) );
624             }
625         }
626     } else if ( (reporthdr->report.type & SERVER_RELAY_REPORT) != 0 ) {
627         reporthdr->report.type &= ~SERVER_RELAY_REPORT;
628         return reporter_print( &reporthdr->report, SERVER_RELAY_REPORT, 1 );
629     }
630     if ( (reporthdr->report.type & TRANSFER_REPORT) != 0 ) {
631         // If there are more packets to process then handle them
632         if ( reporthdr->reporterindex >= 0 ) {
633             // Need to make sure we do not pass the "agent"
634             while ( reporthdr->reporterindex != reporthdr->agentindex - 1 ) {
635                 if ( reporthdr->reporterindex == NUM_REPORT_STRUCTS - 1 ) {
636                     if ( reporthdr->agentindex == 0 ) {
637                         break;
638                     } else {
639                         reporthdr->reporterindex = 0;
640                     }
641                 } else {
642                     reporthdr->reporterindex++;
643                 }
644                 if ( reporter_handle_packet( reporthdr ) ) {
645                     // No more packets to process
646                     reporthdr->reporterindex = -1;
647                     break;
648                 }
649             }
650         }
651         // If the agent is done with the report then free it
652         if ( reporthdr->agentindex == -1 ) {
653             need_free = 1;
654         }
655     }
656     return need_free;
657 }
658
659 /*
660  * Updates connection stats
661  */
662 int reporter_handle_packet( ReportHeader *reporthdr ) {
663     ReportStruct *packet = &reporthdr->data[reporthdr->reporterindex];
664     ReporterData *data = &reporthdr->report;
665     Transfer_Info *stats = &reporthdr->report.info;
666     int finished = 0;
667
668     // update received amount and time
669     data->TotalLen  += packet->packetLen;
670     data->packetTime = packet->packetTime;
671     data->cntDatagrams++;
672
673     if (packet->packetID < 0) {
674         // This is the last packet (FIN)
675         finished = 1;
676         if (isConnectionLess(&reporthdr->report)) {
677             // connectionless protocols don't count the last payload
678             if (reporthdr->report.mThreadMode == kMode_Client)
679                 data->TotalLen -= packet->packetLen;
680         } else {
681             // connection-oriented protocols dont't count the FIN
682             data->cntDatagrams--;
683         }
684     } else if ( packet->packetID != 0 ) {
685             // UDP or DCCP packet
686             double transit, deltaTransit;
687
688             // from RFC 1889, Real Time Protocol (RTP) 
689             // J = J + ( | D(i-1,i) | - J ) / 16 
690             transit = TimeDifference( packet->packetTime, packet->sentTime );
691             if ( data->lastTransit != 0.0 ) {
692                 deltaTransit = transit - data->lastTransit;
693                 if ( deltaTransit < 0.0 ) {
694                     deltaTransit = -deltaTransit;
695                 }
696                 stats->jitter += (deltaTransit - stats->jitter) / (16.0);
697             }
698             data->lastTransit = transit;
699
700             // packet loss occured if the datagram numbers aren't sequential 
701             if ( packet->packetID != data->PacketID + 1 ) {
702                 if ( packet->packetID < data->PacketID + 1 ) {
703                     data->cntOutofOrder++;
704                 } else {
705                     data->cntError += packet->packetID - data->PacketID - 1;
706                 }
707             }
708             // never decrease datagramID (e.g. if we get an out-of-order packet) 
709             if ( packet->packetID > data->PacketID ) {
710                 data->PacketID = packet->packetID;
711             }
712     }
713     // Print a report if appropriate
714     return reporter_condprintstats( &reporthdr->report, reporthdr->multireport, finished );
715 }
716
717 /*
718  * Handles summing of threads
719  */
720 void reporter_handle_multiple_reports( MultiHeader *reporthdr, Transfer_Info *stats, int force ) {
721     if ( reporthdr != NULL ) {
722         if ( reporthdr->threads > 1 ) {
723             int i;
724             Transfer_Info *current = NULL;
725             // Search for start Time
726             for ( i = 0; i < NUM_MULTI_SLOTS; i++ ) {
727                 current = &reporthdr->data[i];
728                 if ( current->startTime == stats->startTime ) {
729                     break;
730                 }
731             }
732             if ( current->startTime != stats->startTime ) {
733                 // Find first available
734                 for ( i = 0; i < NUM_MULTI_SLOTS; i++ ) {
735                     current = &reporthdr->data[i];
736                     if ( current->startTime < 0 ) {
737                         break;
738                     }
739                 }
740                 current->cntDatagrams = stats->cntDatagrams;
741                 current->cntError = stats->cntError;
742                 current->cntOutofOrder = stats->cntOutofOrder;
743                 current->TotalLen = stats->TotalLen;
744                 current->mFormat = stats->mFormat;
745                 current->endTime = stats->endTime;
746                 current->jitter = stats->jitter;
747                 current->startTime = stats->startTime;
748                 current->free = 1;
749             } else {
750                 current->cntDatagrams += stats->cntDatagrams;
751                 current->cntError += stats->cntError;
752                 current->cntOutofOrder += stats->cntOutofOrder;
753                 current->TotalLen += stats->TotalLen;
754                 current->mFormat = stats->mFormat;
755                 if ( current->endTime < stats->endTime ) {
756                     current->endTime = stats->endTime;
757                 }
758                 if ( current->jitter < stats->jitter ) {
759                     current->jitter = stats->jitter;
760                 }
761                 current->free++;
762                 if ( current->free == reporthdr->threads ) {
763                     void *reserved = reporthdr->report->info.reserved_delay;
764                     current->free = force;
765                     memcpy( &reporthdr->report->info, current, sizeof(Transfer_Info) );
766                     current->startTime = -1;
767                     reporthdr->report->info.reserved_delay = reserved;
768                     reporter_print( reporthdr->report, MULTIPLE_REPORT, force );
769                 }
770             }
771         }
772     }
773 }
774
775 /*
776  * Prints reports conditionally
777  */
778 int reporter_condprintstats( ReporterData *stats, MultiHeader *multireport, int force ) {
779     if ( force != 0 ) {
780         stats->info.cntOutofOrder = stats->cntOutofOrder;
781         // assume most of the time out-of-order packets are not
782         // duplicate packets, so conditionally subtract them from the lost packets.
783         stats->info.cntError = stats->cntError;
784         if ( stats->info.cntError > stats->info.cntOutofOrder ) {
785             stats->info.cntError -= stats->info.cntOutofOrder;
786         }
787         if (isConnectionLess(stats))
788                 stats->info.cntDatagrams = stats->PacketID;
789         else
790                 stats->info.cntDatagrams = stats->cntDatagrams;
791         stats->info.TotalLen  = stats->TotalLen;
792         stats->info.startTime = 0;
793         stats->info.endTime   = TimeDifference( stats->packetTime, stats->startTime );
794         stats->info.free      = 1;
795         reporter_print( stats, TRANSFER_REPORT, force );
796         if ( isMultipleReport(stats) ) {
797             reporter_handle_multiple_reports( multireport, &stats->info, force );
798         }
799     } else while ((stats->intervalTime.tv_sec != 0 || 
800                    stats->intervalTime.tv_usec != 0) && 
801                   TimeDifference( stats->nextTime, 
802                                   stats->packetTime ) < 0 ) {
803         stats->info.cntOutofOrder = stats->cntOutofOrder - stats->lastOutofOrder;
804         stats->lastOutofOrder = stats->cntOutofOrder;
805         // assume most of the time out-of-order packets are not
806         // duplicate packets, so conditionally subtract them from the lost packets.
807         stats->info.cntError = stats->cntError - stats->lastError;
808         if ( stats->info.cntError > stats->info.cntOutofOrder ) {
809             stats->info.cntError -= stats->info.cntOutofOrder;
810         }
811         stats->lastError = stats->cntError;
812         if (isConnectionLess(stats)) {
813                 stats->info.cntDatagrams = stats->PacketID - stats->lastDatagrams;
814                 stats->lastDatagrams     = stats->PacketID;
815         } else {
816                 stats->info.cntDatagrams = stats->cntDatagrams - stats->lastDatagrams;
817                 stats->lastDatagrams     = stats->cntDatagrams;
818         }
819         stats->info.TotalLen = stats->TotalLen - stats->lastTotal;
820         stats->lastTotal = stats->TotalLen;
821         stats->info.startTime = stats->info.endTime;
822         stats->info.endTime = TimeDifference( stats->nextTime, stats->startTime );
823         TimeAdd( stats->nextTime, stats->intervalTime );
824         stats->info.free = 0;
825         reporter_print( stats, TRANSFER_REPORT, force );
826         if ( isMultipleReport(stats) ) {
827             reporter_handle_multiple_reports( multireport, &stats->info, force );
828         }
829     }
830     return force;
831 }
832
833 /*
834  * This function handles multiple format printing by sending to the
835  * appropriate dispatch function
836  */
837 int reporter_print( ReporterData *stats, int type, int end ) {
838     switch ( type ) {
839         case TRANSFER_REPORT:
840             statistics_reports[stats->mode]( &stats->info );
841             if ( end != 0 && isPrintMSS( stats ) && !isConnectionLess( stats ) ) {
842                 PrintMSS( stats );
843             }
844             break;
845         case SERVER_RELAY_REPORT:
846             serverstatistics_reports[stats->mode]( &stats->connection, &stats->info );
847             break;
848         case SETTINGS_REPORT:
849             settings_reports[stats->mode]( stats );
850             break;
851         case CONNECTION_REPORT:
852             stats->info.reserved_delay = connection_reports[stats->mode]( 
853                                                &stats->connection,
854                                                stats->info.transferID );
855             break;
856         case MULTIPLE_REPORT:
857             multiple_reports[stats->mode]( &stats->info );
858             break;
859         default:
860             fprintf( stderr, "Printing type not implemented! No Output\n" );
861     }
862     fflush( stdout );
863     return end;
864 }
865
866 /* -------------------------------------------------------------------
867  * Report the MSS and MTU, given the MSS (or a guess thereof)
868  * This works for connection-oriented protocols only: it expects
869  * the protocol to be either TCP or DCCP and will give error otherwise
870  * ------------------------------------------------------------------- */
871
872 // compare the MSS against the (MTU - 40) to (MTU - 80) bytes.
873 // 40 byte IP header and somewhat arbitrarily, 40 more bytes of IP options.
874
875 #define checkMSS_MTU( inMSS, inMTU ) (inMTU-40) >= inMSS  &&  inMSS >= (inMTU-80)
876
877 void PrintMSS( ReporterData *stats )
878 {
879     int inMSS = stats->mProtocol == kProto_TCP
880               ?   getsock_tcp_mss( stats->info.transferID )
881               :   getsock_dccp_mps( stats->info.transferID );
882
883     if ( inMSS <= 0 ) {
884         printf( report_mss_unsupported, stats->info.transferID );
885     } else {
886         char* net;
887         int mtu = 0;
888
889         if ( checkMSS_MTU( inMSS, 1500 ) ) {
890             net = "ethernet";
891             mtu = 1500;
892         } else if ( checkMSS_MTU( inMSS, 4352 ) ) {
893             net = "FDDI";
894             mtu = 4352;
895         } else if ( checkMSS_MTU( inMSS, 9180 ) ) {
896             net = "ATM";
897             mtu = 9180;
898         } else if ( checkMSS_MTU( inMSS, 65280 ) ) {
899             net = "HIPPI";
900             mtu = 65280;
901         } else if ( checkMSS_MTU( inMSS, 576 ) ) {
902             net = "minimum";
903             mtu = 576;
904             printf( warn_no_pathmtu );
905         } else {
906             mtu = inMSS + 40;
907             net = "unknown interface";
908         }
909
910         printf( report_mss,
911                 stats->info.transferID, inMSS, mtu, net );
912     }
913 }
914 // end ReportMSS
915
916 #ifdef __cplusplus
917 } /* end extern "C" */
918 #endif