]> sjero.net Git - iperf/blob - src/Settings.cpp
Support for UDP-Lite in iperf
[iperf] / src / Settings.cpp
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  * Settings.cpp
48  * by Mark Gates <mgates@nlanr.net>
49  * & Ajay Tirumala <tirumala@ncsa.uiuc.edu>
50  * -------------------------------------------------------------------
51  * Stores and parses the initial values for all the global variables.
52  * -------------------------------------------------------------------
53  * headers
54  * uses
55  *   <stdlib.h>
56  *   <stdio.h>
57  *   <string.h>
58  *
59  *   <unistd.h>
60  * ------------------------------------------------------------------- */
61
62 #define HEADERS()
63
64 #include "headers.h"
65
66 #include "Settings.hpp"
67 #include "Locale.h"
68 #include "SocketAddr.h"
69
70 #include "util.h"
71
72 #include "gnu_getopt.h"
73
74 void Settings_Interpret( char option, const char *optarg, thread_Settings *mExtSettings );
75
76 /* -------------------------------------------------------------------
77  * command line options
78  *
79  * The option struct essentially maps a long option name (--foobar)
80  * or environment variable ($FOOBAR) to its short option char (f).
81  * ------------------------------------------------------------------- */
82 #define LONG_OPTIONS()
83
84 const struct option long_options[] =
85 {
86 {"singleclient",     no_argument, NULL, '1'},
87 {"dualtest",         no_argument, NULL, '2'},
88 {"bandwidth",  required_argument, NULL, 'b'},
89 {"client",     required_argument, NULL, 'c'},
90 {"dccp",             no_argument, NULL, 'd'},
91 {"format",     required_argument, NULL, 'f'},
92 {"help",             no_argument, NULL, 'h'},
93 {"interval",   required_argument, NULL, 'i'},
94 {"mcast_iface",required_argument, NULL, 'j'},
95 {"len",        required_argument, NULL, 'l'},
96 {"print_mss",        no_argument, NULL, 'm'},
97 {"num",        required_argument, NULL, 'n'},
98 {"output",     required_argument, NULL, 'o'},
99 {"port",       required_argument, NULL, 'p'},
100 {"tradeoff",         no_argument, NULL, 'r'},
101 {"server",           no_argument, NULL, 's'},
102 {"time",       required_argument, NULL, 't'},
103 {"udp",              no_argument, NULL, 'u'},
104 {"udplite",    required_argument, NULL, 'u'},
105 {"version",          no_argument, NULL, 'v'},
106 {"window",     required_argument, NULL, 'w'},
107 {"reportexclude", required_argument, NULL, 'x'},
108 {"reportstyle",required_argument, NULL, 'y'},
109
110 // more esoteric options
111 {"algorithm",  required_argument, NULL, 'A'},
112 {"bind",       required_argument, NULL, 'B'},
113 {"compatibility",    no_argument, NULL, 'C'},
114 {"daemon",           no_argument, NULL, 'D'},
115 {"file_input", required_argument, NULL, 'F'},
116 {"stdin_input",      no_argument, NULL, 'I'},
117 {"mss",        required_argument, NULL, 'M'},
118 {"nodelay",          no_argument, NULL, 'N'},
119 {"listenport", required_argument, NULL, 'L'},
120 {"parallel",   required_argument, NULL, 'P'},
121 {"remove",           no_argument, NULL, 'R'},
122 {"dscp",       required_argument, NULL, 'S'},
123 {"ttl",        required_argument, NULL, 'T'},
124 {"single_udp",       no_argument, NULL, 'U'},
125 {"ipv6_domian",      no_argument, NULL, 'V'},
126 {"suggest_win_size", no_argument, NULL, 'W'},
127 {0, 0, 0, 0}
128 };
129
130 #define ENV_OPTIONS()
131
132 const struct option env_options[] =
133 {
134 {"IPERF_SINGLECLIENT",     no_argument, NULL, '1'},
135 {"IPERF_DUALTEST",         no_argument, NULL, '2'},
136 {"IPERF_BANDWIDTH",  required_argument, NULL, 'b'},
137 {"IPERF_CLIENT",     required_argument, NULL, 'c'},
138 {"IPERF_DCCP",             no_argument, NULL, 'd'},
139 {"IPERF_FORMAT",     required_argument, NULL, 'f'},
140 // skip help
141 {"IPERF_INTERVAL",   required_argument, NULL, 'i'},
142 {"IPERF_MCASTIFACE", required_argument, NULL, 'j'},
143 {"IPERF_LEN",        required_argument, NULL, 'l'},
144 {"IPERF_PRINT_MSS",        no_argument, NULL, 'm'},
145 {"IPERF_NUM",        required_argument, NULL, 'n'},
146 {"IPERF_PORT",       required_argument, NULL, 'p'},
147 {"IPERF_TRADEOFF",         no_argument, NULL, 'r'},
148 {"IPERF_SERVER",           no_argument, NULL, 's'},
149 {"IPERF_TIME",       required_argument, NULL, 't'},
150 {"IPERF_UDP",              no_argument, NULL, 'u'},
151 // skip version
152 {"TCP_WINDOW_SIZE",  required_argument, NULL, 'w'},
153 {"IPERF_REPORTEXCLUDE", required_argument, NULL, 'x'},
154 {"IPERF_REPORTSTYLE",required_argument, NULL, 'y'},
155
156 // more esoteric options
157 {"IPERF_CC",         required_argument, NULL, 'A'},
158 {"IPERF_BIND",       required_argument, NULL, 'B'},
159 {"IPERF_COMPAT",           no_argument, NULL, 'C'},
160 {"IPERF_DAEMON",           no_argument, NULL, 'D'},
161 {"IPERF_FILE_INPUT", required_argument, NULL, 'F'},
162 {"IPERF_STDIN_INPUT",      no_argument, NULL, 'I'},
163 {"IPERF_MSS",        required_argument, NULL, 'M'},
164 {"IPERF_NODELAY",          no_argument, NULL, 'N'},
165 {"IPERF_LISTENPORT", required_argument, NULL, 'L'},
166 {"IPERF_PARALLEL",   required_argument, NULL, 'P'},
167 {"IPERF_DSCP",       required_argument, NULL, 'S'},
168 {"IPERF_TTL",        required_argument, NULL, 'T'},
169 {"IPERF_SINGLE_UDP",       no_argument, NULL, 'U'},
170 {"IPERF_IPV6_DOMAIN",      no_argument, NULL, 'V'},
171 {"IPERF_SUGGEST_WIN_SIZE", required_argument, NULL, 'W'},
172 {0, 0, 0, 0}
173 };
174
175 #define SHORT_OPTIONS()
176
177 const char short_options[] =
178       "12b::c:df:hi:j:l:mn:o:p:rst:u::vw:x:y:A:B:CDF:IJ:L:M:NP:RS:T:UV:W";
179
180 /* -------------------------------------------------------------------
181  * defaults
182  * ------------------------------------------------------------------- */
183 #define DEFAULTS()
184
185 const long kDefault_DgramRate = 1024 * 1024; // -u  if set, 1 Mbit/sec
186 const int  kDefault_UDPBufLen = 1470;        // -u  if set, read/write 1470 bytes
187 // 1470 bytes is small enough to be sending one packet per datagram on ethernet
188
189 // 1450 bytes is small enough to be sending one packet per datagram on ethernet
190 //  **** with IPv6 ****
191 const int  kDefault_DCCPBufLen = 1420;       // -d
192 // old DCCPv4: MPS=1424; new DCCPv4: MPS=1440; new DCCPv6: MPS=1420 (above)
193
194 /* -------------------------------------------------------------------
195  * Initialize all settings to defaults.
196  * ------------------------------------------------------------------- */
197
198 void Settings_Initialize( thread_Settings *main ) {
199     // Everything defaults to zero or NULL with
200     // this memset. Only need to set non-zero values
201     // below.
202     memset( main, 0, sizeof(thread_Settings) );
203     main->mSock         = INVALID_SOCKET;
204     //main->mSockAF     = AF_UNSPEC
205     main->mReportMode   = kReport_Default;
206     // option, defaults
207     main->flags         = FLAG_MODETIME | FLAG_STDOUT; // Default time and stdout
208     //main->mDgramRate  = 0;             // -b,  ie. TCP mode
209     main->mProtocol     = kProto_TCP;    // -u / -d
210     //main->mHost       = NULL;          // -c,  none, required for client
211     main->mMode         = kTest_Normal;  // -2,  mMode == kTest_DualTest
212     main->mFormat       = 'a';           // -f,  adaptive bits
213     // skip help                         // -h,
214     //main->mBufLenSet  = false;         // -l, 
215     main->mBufLen       = 8 * 1024;      // -l,  8 Kbyte
216     //main->mInterval   = 0;             // -i,  ie. no periodic bw reports
217     //main->mPrintMSS   = false;         // -m,  don't print MSS
218     // mAmount is time also              // -n,  N/A
219     //main->mOutputFileName = NULL;      // -o,  filename
220     main->mPort         = 5001;          // -p,  ttcp port
221     // mMode            = kTest_Normal;  // -r,  mMode == kTest_TradeOff
222     main->mThreadMode   = kMode_Unknown; // -s,  or -c, none
223     main->mAmount       = 1000;          // -t,  10 seconds
224     // mDgramRate                        // -u,  N/A, see kDefault_DgramRate
225     // skip version                      // -v,
226     //main->mWinSize       = 0;          // -w,  ie. don't set window
227
228     // more esoteric options
229     //main->mLocalhost  = NULL;          // -B,  bind address - none
230     //main->mCompat     = false;         // -C,  run in Compatibility mode
231     //main->mDaemon     = false;         // -D,  run as a daemon
232     //main->mFileInput  = false;         // -F,
233     //main->mFileName   = NULL;          // -F,  filename
234     //main->mStdin      = false;         // -I,  default not stdin
235     //main->mMcastIface = 0;             // -J,  default: host chooses interface
236     //main->mListenPort = 0;             // -L,  listen port
237     //main->mMSS        = 0;             // -M,  ie. don't set MSS
238     //main->mNodelay    = false;         // -N,  don't set nodelay
239     //main->mThreads    = 0;             // -P,
240     //main->mRemoveService = false;      // -R,
241     //main->mTOS        = 0;             // -S,  ie. don't set type of service
242     main->mTTL          = 1;             // -T,  link-local TTL
243     //main->mDomain     = kMode_IPv4;    // -V,
244     //main->mSuggestWin = false;         // -W,  Suggest the window size.
245
246 } // end Settings
247
248 void Settings_Copy( thread_Settings *from, thread_Settings **into ) {
249     *into = new thread_Settings;
250     memcpy( *into, from, sizeof(thread_Settings) );
251     if ( from->mHost != NULL ) {
252         (*into)->mHost = new char[ strlen(from->mHost) + 1];
253         strcpy( (*into)->mHost, from->mHost );
254     }
255     if ( from->mOutputFileName != NULL ) {
256         (*into)->mOutputFileName = new char[ strlen(from->mOutputFileName) + 1];
257         strcpy( (*into)->mOutputFileName, from->mOutputFileName );
258     }
259     if ( from->mLocalhost != NULL ) {
260         (*into)->mLocalhost = new char[ strlen(from->mLocalhost) + 1];
261         strcpy( (*into)->mLocalhost, from->mLocalhost );
262     }
263     if ( from->mFileName != NULL ) {
264         (*into)->mFileName = new char[ strlen(from->mFileName) + 1];
265         strcpy( (*into)->mFileName, from->mFileName );
266     }
267     // Zero out certain entries
268     (*into)->mTID = thread_zeroid();
269     (*into)->runNext = NULL;
270     (*into)->runNow = NULL;
271 }
272
273 /* -------------------------------------------------------------------
274  * Delete memory: Does not clean up open file pointers or ptr_parents
275  * ------------------------------------------------------------------- */
276
277 void Settings_Destroy( thread_Settings *mSettings) {
278     DELETE_ARRAY( mSettings->mHost      );
279     DELETE_ARRAY( mSettings->mLocalhost );
280     DELETE_ARRAY( mSettings->mFileName  );
281     DELETE_ARRAY( mSettings->mOutputFileName );
282     DELETE_PTR( mSettings );
283 } // end ~Settings
284
285 /* -------------------------------------------------------------------
286  * Parses settings from user's environment variables.
287  * ------------------------------------------------------------------- */
288 void Settings_ParseEnvironment( thread_Settings *mSettings ) {
289     char *theVariable;
290
291     int i = 0;
292     while ( env_options[i].name != NULL ) {
293         theVariable = getenv( env_options[i].name );
294         if ( theVariable != NULL ) {
295             Settings_Interpret( env_options[i].val, theVariable, mSettings );
296         }
297         i++;
298     }
299 } // end ParseEnvironment
300
301 /* -------------------------------------------------------------------
302  * Parse settings from app's command line.
303  * ------------------------------------------------------------------- */
304
305 void Settings_ParseCommandLine( int argc, char **argv, thread_Settings *mSettings ) {
306     int option;
307     while ( (option =
308              gnu_getopt_long( argc, argv, short_options,
309                               long_options, NULL )) != EOF ) {
310         Settings_Interpret( option, gnu_optarg, mSettings );
311     }
312
313     for ( int i = gnu_optind; i < argc; i++ ) {
314         fprintf( stderr, "%s: ignoring extra argument -- %s\n", argv[0], argv[i] );
315     }
316 } // end ParseCommandLine
317
318 /* -------------------------------------------------------------------
319  * Interpret individual options, either from the command line
320  * or from environment variables.
321  * ------------------------------------------------------------------- */
322
323 void Settings_Interpret( char option, const char *optarg, thread_Settings *mExtSettings ) {
324     char outarg[100];
325
326     switch ( option ) {
327         case '1': // Single Client
328             setSingleClient( mExtSettings );
329             break;
330
331         case '2': // Dual-test Mode
332             if ( mExtSettings->mThreadMode != kMode_Client ) {
333                 fprintf( stderr, warn_invalid_server_option, option );
334                 break;
335             }
336             if ( isCompat( mExtSettings ) ) {
337                 fprintf( stderr, warn_invalid_compatibility_option, option );
338             }
339 #ifdef HAVE_THREAD
340             mExtSettings->mMode = kTest_DualTest;
341 #else
342             fprintf( stderr, warn_invalid_single_threaded, option );
343             mExtSettings->mMode = kTest_TradeOff;
344 #endif
345             break;
346
347         case 'b':
348             // This sets packet-oriented mode. The optional
349             // argument sets datagram bandwidth (as before).
350             // If not given, a default bandwith is used.
351             setPacketOriented(mExtSettings);
352
353             if (optarg) {
354                 Settings_GetLowerCaseArg(optarg, outarg);
355                 mExtSettings->mDgramRate = byte_atoi(outarg);
356              } else
357                 mExtSettings->mDgramRate = kDefault_DgramRate;
358             break;
359
360         case 'c': // client mode w/ server host to connect to
361             mExtSettings->mHost = new char[ strlen( optarg ) + 1 ];
362             strcpy( mExtSettings->mHost, optarg );
363
364             if ( mExtSettings->mThreadMode == kMode_Unknown ) {
365                 mExtSettings->mThreadMode = kMode_Client;
366                 mExtSettings->mThreads = 1;
367             }
368             break;
369
370         case 'd': // DCCP as transport
371             mExtSettings->mProtocol  = kProto_DCCP;
372
373             // if -l has already been processed, mBufLenSet is true
374             // so don't overwrite that value.
375             if ( !isBuflenSet( mExtSettings ) )
376                 mExtSettings->mBufLen = kDefault_DCCPBufLen;
377
378             break;
379
380         case 'f': // format to print in
381             mExtSettings->mFormat = (*optarg);
382             break;
383
384         case 'h': // print help and exit
385             die(usage_long);
386
387         case 'i': // specify interval between periodic bw reports
388             mExtSettings->mInterval = atof( optarg );
389             if ( mExtSettings->mInterval < 0.5 ) {
390                 fprintf (stderr, report_interval_small, mExtSettings->mInterval);
391                 mExtSettings->mInterval = 0.5;
392             }
393             break;
394
395         case 'j':
396         case 'J': // multicast interface to join multicast address on
397             mExtSettings->mMcastIface = if_nametoindex(optarg);
398             if (!mExtSettings->mMcastIface)
399                     die("Interface \"%s\" does not work for -j/-J.", optarg);
400             break;
401
402         case 'l': // length of each buffer
403             Settings_GetUpperCaseArg(optarg,outarg);
404             mExtSettings->mBufLen = byte_atoi( outarg );
405             setBuflenSet( mExtSettings );
406
407             if ( !isPacketOriented( mExtSettings ) ) {
408                  if ( mExtSettings->mBufLen < (int) sizeof( client_hdr ) &&
409                       !isCompat( mExtSettings ) ) {
410                     setCompat( mExtSettings );
411                     fprintf( stderr, warn_implied_compatibility, option );
412                  }
413             } else {
414                 if ( mExtSettings->mBufLen < (int) sizeof( dgram_record ) ) {
415                     mExtSettings->mBufLen = sizeof( dgram_record );
416                     fprintf( stderr, warn_buffer_too_small, mExtSettings->mBufLen );
417                 }
418                 if ( !isCompat( mExtSettings ) &&
419                             mExtSettings->mBufLen < (int) ( sizeof( dgram_record )
420                             + sizeof( client_hdr ) ) ) {
421                     setCompat( mExtSettings );
422                     fprintf( stderr, warn_implied_compatibility, option );
423                 }
424             }
425
426             break;
427
428         case 'm': // print TCP MSS
429             setPrintMSS( mExtSettings );
430             break;
431
432         case 'n': // bytes of data
433             // amount mode (instead of time mode)
434             unsetModeTime( mExtSettings );
435             Settings_GetUpperCaseArg(optarg,outarg);
436             mExtSettings->mAmount = byte_atoi( outarg );
437             break;
438
439         case 'o' : // output the report and other messages into the file
440             unsetSTDOUT( mExtSettings );
441             mExtSettings->mOutputFileName = new char[strlen(optarg)+1];
442             strcpy( mExtSettings->mOutputFileName, optarg);
443             break;
444
445         case 'p': // server port
446             mExtSettings->mPort = atoi( optarg );
447             break;
448
449         case 'r': // test mode tradeoff
450             if ( mExtSettings->mThreadMode != kMode_Client ) {
451                 fprintf( stderr, warn_invalid_server_option, option );
452                 break;
453             }
454             if ( isCompat( mExtSettings ) ) {
455                 fprintf( stderr, warn_invalid_compatibility_option, option );
456             }
457
458             mExtSettings->mMode = kTest_TradeOff;
459             break;
460
461         case 's': // server mode
462             if ( mExtSettings->mThreadMode != kMode_Unknown ) {
463                 fprintf( stderr, warn_invalid_client_option, option );
464                 break;
465             }
466
467             mExtSettings->mThreadMode = kMode_Listener;
468             break;
469
470         case 't': // seconds to write for
471             // time mode (instead of amount mode)
472             setModeTime( mExtSettings );
473             mExtSettings->mAmount = (int) (atof( optarg ) * 100.0);
474             break;
475
476         case 'u': // UDP(-Lite) instead of TCP
477              if (optarg) {
478                 mExtSettings->mProtocol = kProto_UDPLITE;
479                 // Set partial checksum coverage:
480                 // - 0 means entire datagram,
481                 // - 1..7 is illegal and will be rounded up to 8;
482                 // - 8 and greater mean genuine partial coverage.
483                 mExtSettings->cscov = atoi(optarg);
484              } else
485                 mExtSettings->mProtocol = kProto_UDP;
486
487             setPacketOriented(mExtSettings);
488             // if -b has already been processed, UDP rate will
489             // already be non-zero, so don't overwrite that value
490             if ( mExtSettings->mDgramRate == 0 )
491                 mExtSettings->mDgramRate = kDefault_DgramRate;
492
493             // if -l has already been processed, mBufLenSet is true
494             // so don't overwrite that value.
495             if ( !isBuflenSet( mExtSettings ) ) {
496                 mExtSettings->mBufLen = kDefault_UDPBufLen;
497             } else if ( mExtSettings->mBufLen < (int) ( sizeof( dgram_record )
498                         + sizeof( client_hdr ) ) &&
499                         !isCompat( mExtSettings ) ) {
500                 setCompat( mExtSettings );
501                 fprintf( stderr, warn_implied_compatibility, option );
502             }
503             break;
504
505         case 'v': // print version and exit
506             die(version);
507
508         case 'w': // TCP window size or socket send-buffer size (UDP/DCCP)
509             Settings_GetUpperCaseArg(optarg,outarg);
510             mExtSettings->mWinSize = byte_atoi(outarg);
511             break;
512
513         case 'x': // Limit Reports
514             while ( *optarg != '\0' ) {
515                 switch ( *optarg ) {
516                     case 's':
517                     case 'S':
518                         setNoSettReport( mExtSettings );
519                         break;
520                     case 'c':
521                     case 'C':
522                         setNoConnReport( mExtSettings );
523                         break;
524                     case 'd':
525                     case 'D':
526                         setNoDataReport( mExtSettings );
527                         break;
528                     case 'v':
529                     case 'V':
530                         setNoServReport( mExtSettings );
531                         break;
532                     case 'm':
533                     case 'M':
534                         setNoMultReport( mExtSettings );
535                         break;
536                     default:
537                         fprintf(stderr, warn_invalid_report, *optarg);
538                 }
539                 optarg++;
540             }
541             break;
542
543         case 'y': // Reporting Style
544             switch ( *optarg ) {
545                 case 'c':
546                 case 'C':
547                     mExtSettings->mReportMode = kReport_CSV;
548                     break;
549                 default:
550                     fprintf( stderr, warn_invalid_report_style, optarg );
551             }
552             break;
553
554
555             // more esoteric options
556         case 'A': // set TCP congestion control algorithm
557             mExtSettings->congAlgo = new char[ strlen( optarg ) + 1 ];
558             strcpy( mExtSettings->congAlgo, optarg );
559             break;
560
561         case 'B': // specify bind address
562             mExtSettings->mLocalhost = new char[ strlen( optarg ) + 1 ];
563             strcpy( mExtSettings->mLocalhost, optarg );
564             break;
565
566         case 'C': // Run in Compatibility Mode
567             setCompat( mExtSettings );
568             if ( mExtSettings->mMode != kTest_Normal ) {
569                 fprintf( stderr, warn_invalid_compatibility_option,
570                         ( mExtSettings->mMode == kTest_DualTest ?
571                           'd' : 'r' ) );
572                 mExtSettings->mMode = kTest_Normal;
573             }
574             break;
575
576         case 'D': // Run as a daemon
577             setDaemon( mExtSettings );
578             break;
579
580         case 'F' : // Get the input for the data stream from a file
581             if ( mExtSettings->mThreadMode != kMode_Client ) {
582                 fprintf( stderr, warn_invalid_server_option, option );
583                 break;
584             }
585
586             setFileInput( mExtSettings );
587             mExtSettings->mFileName = new char[strlen(optarg)+1];
588             strcpy( mExtSettings->mFileName, optarg);
589             break;
590
591         case 'I' : // Set the stdin as the input source
592             if ( mExtSettings->mThreadMode != kMode_Client ) {
593                 fprintf( stderr, warn_invalid_server_option, option );
594                 break;
595             }
596
597             setFileInput( mExtSettings );
598             setSTDIN( mExtSettings );
599             mExtSettings->mFileName = new char[strlen("<stdin>")+1];
600             strcpy( mExtSettings->mFileName,"<stdin>");
601             break;
602
603         case 'L': // Listen Port (bidirectional testing client-side)
604             if ( mExtSettings->mThreadMode != kMode_Client ) {
605                 fprintf( stderr, warn_invalid_server_option, option );
606                 break;
607             }
608
609             mExtSettings->mListenPort = atoi( optarg );
610             break;
611
612         case 'M': // specify TCP MSS (maximum segment size)
613             Settings_GetUpperCaseArg(optarg,outarg);
614
615             mExtSettings->mMSS = byte_atoi( outarg );
616             break;
617
618         case 'N': // specify TCP nodelay option (disable Jacobson's Algorithm)
619             setNoDelay( mExtSettings );
620             break;
621
622         case 'P': // number of client threads
623 #ifdef HAVE_THREAD
624             mExtSettings->mThreads = atoi( optarg );
625 #else
626             if ( mExtSettings->mThreadMode != kMode_Server ) {
627                 fprintf( stderr, warn_invalid_single_threaded, option );
628             } else {
629                 mExtSettings->mThreads = atoi( optarg );
630             }
631 #endif
632             break;
633
634         case 'R':
635             setRemoveService( mExtSettings );
636             break;
637
638         case 'S':
639             // Convert into IPv4/v6 6-bit DiffServ codepoint. If string
640             //  - consists of six 0/1 digits, it is seen as binary number;
641             //  - is preceded by `0x', it is interpreted as hex number;
642             //  - is preceded by `0', it is interpreted as octal number;
643             //  - otherwise it is interpreted as decimal number.
644             // The maximum possible 6-bit value is 252 (0xfc).
645             if ( strspn(optarg, "01") == 6 )
646                mExtSettings->mTOS = strtol(optarg, NULL, 2);
647             else
648                mExtSettings->mTOS = strtol(optarg, NULL, 0);
649             if ( mExtSettings->mTOS < 0 || mExtSettings->mTOS  > 0xfc )
650                die("Invalid DiffServ codepoint %s", optarg);
651             mExtSettings->mTOS <<= 2;
652             break;
653
654         case 'T': // time-to-live for multicast
655             mExtSettings->mTTL = atoi( optarg );
656             break;
657
658         case 'V': // IP Domain: the optional ar
659             mExtSettings->mSockAF = AF_INET6;
660             if ( optarg  && optarg[0] == '4' )
661                 mExtSettings->mSockAF = AF_INET;
662             break;
663         case 'U': // single threaded UDP server
664             setSingleUDP( mExtSettings );
665             break;
666
667         case 'W' :
668             setSuggestWin( mExtSettings );
669             fprintf( stderr, "The -W option is not available in this release\n");
670             break;
671
672         default: // ignore unknown
673             break;
674     }
675 } // end Interpret
676
677 void Settings_GetUpperCaseArg(const char *inarg, char *outarg) {
678
679     int len = strlen(inarg);
680     strcpy(outarg,inarg);
681
682     if ( (len > 0) && (inarg[len-1] >='a') 
683          && (inarg[len-1] <= 'z') )
684         outarg[len-1]= outarg[len-1]+'A'-'a';
685 }
686
687 void Settings_GetLowerCaseArg(const char *inarg, char *outarg) {
688
689     int len = strlen(inarg);
690     strcpy(outarg,inarg);
691
692     if ( (len > 0) && (inarg[len-1] >='A') 
693          && (inarg[len-1] <= 'Z') )
694         outarg[len-1]= outarg[len-1]-'A'+'a';
695 }
696
697 /*
698  * Settings_GenerateListenerSettings
699  * Called to generate the settings to be passed to the Listener
700  * instance that will handle dual testings from the client side
701  * this should only return an instance if it was called on 
702  * the thread_Settings instance generated from the command line 
703  * for client side execution 
704  */
705 void Settings_GenerateListenerSettings( thread_Settings *client, thread_Settings **listener ) {
706     if ( !isCompat( client ) && 
707          (client->mMode == kTest_DualTest || client->mMode == kTest_TradeOff) ) {
708         *listener = new thread_Settings;
709         memcpy(*listener, client, sizeof( thread_Settings ));
710         setCompat( (*listener) );
711         unsetDaemon( (*listener) );
712         if ( client->mListenPort != 0 ) {
713             (*listener)->mPort   = client->mListenPort;
714         } else {
715             (*listener)->mPort   = client->mPort;
716         }
717         (*listener)->mFileName   = NULL;
718         (*listener)->mHost       = NULL;
719         (*listener)->mLocalhost  = NULL;
720         (*listener)->mOutputFileName = NULL;
721         (*listener)->mMode       = kTest_Normal;
722         (*listener)->mThreadMode = kMode_Listener;
723         if ( client->mHost != NULL ) {
724             (*listener)->mHost = new char[strlen( client->mHost ) + 1];
725             strcpy( (*listener)->mHost, client->mHost );
726         }
727         if ( client->mLocalhost != NULL ) {
728             (*listener)->mLocalhost = new char[strlen( client->mLocalhost ) + 1];
729             strcpy( (*listener)->mLocalhost, client->mLocalhost );
730         }
731     } else {
732         *listener = NULL;
733     }
734 }
735
736 /*
737  * Settings_GenerateSpeakerSettings
738  * Called to generate the settings to be passed to the Speaker
739  * instance that will handle dual testings from the server side
740  * this should only return an instance if it was called on 
741  * the thread_Settings instance generated from the command line 
742  * for server side execution. This should be an inverse operation
743  * of GenerateClientHdr. 
744  */
745 void Settings_GenerateClientSettings( thread_Settings *server, 
746                                       thread_Settings **client,
747                                       client_hdr *hdr ) {
748     int flags = ntohl(hdr->flags);
749     if ( (flags & HEADER_VERSION1) != 0 ) {
750         *client = new thread_Settings;
751         memcpy(*client, server, sizeof( thread_Settings ));
752         setCompat( (*client) );
753         (*client)->mTID        = thread_zeroid();
754         (*client)->mPort       = (unsigned short) ntohl(hdr->mPort);
755         (*client)->mThreads    = ntohl(hdr->numThreads);
756         if ( hdr->bufferlen != 0 ) {
757             (*client)->mBufLen = ntohl(hdr->bufferlen);
758         }
759         if ( hdr->mWinBand != 0 ) {
760             if ( isPacketOriented( server ) ) {
761                 (*client)->mDgramRate = ntohl(hdr->mWinBand);
762             } else {
763                 (*client)->mWinSize = ntohl(hdr->mWinBand);
764             }
765         }
766         (*client)->mAmount     = ntohl(hdr->mAmount);
767         if ( ((*client)->mAmount & 0x80000000) > 0 ) {
768             setModeTime( (*client) );
769             (*client)->mAmount |= 0xFFFFFFFF00000000LL;
770             (*client)->mAmount = -(*client)->mAmount;
771         }
772         (*client)->mFileName   = NULL;
773         (*client)->mHost       = NULL;
774         (*client)->mLocalhost  = NULL;
775         (*client)->mOutputFileName = NULL;
776         (*client)->mMode       = ((flags & RUN_NOW) == 0 ?
777                                    kTest_TradeOff : kTest_DualTest);
778         (*client)->mThreadMode = kMode_Client;
779         if ( server->mLocalhost != NULL ) {
780             (*client)->mLocalhost = new char[strlen( server->mLocalhost ) + 1];
781             strcpy( (*client)->mLocalhost, server->mLocalhost );
782         }
783         (*client)->mHost = new char[REPORT_ADDRLEN];
784         SockAddr_name(&server->peer, (*client)->mHost, REPORT_ADDRLEN);
785     } else {
786         *client = NULL;
787     }
788 }
789
790 /*
791  * Settings_GenerateClientHdr
792  * Called to generate the client header to be passed to the
793  * server that will handle dual testings from the server side
794  * This should be an inverse operation of GenerateSpeakerSettings
795  */
796 void Settings_GenerateClientHdr( thread_Settings *client, client_hdr *hdr ) {
797     if ( client->mMode != kTest_Normal ) {
798         hdr->flags  = htonl(HEADER_VERSION1);
799     } else {
800         hdr->flags  = 0;
801     }
802     if ( isBuflenSet( client ) ) {
803         hdr->bufferlen = htonl(client->mBufLen);
804     } else {
805         hdr->bufferlen = 0;
806     }
807     if ( isPacketOriented( client ) ) {
808         hdr->mWinBand  = htonl(client->mDgramRate);
809     } else {
810         hdr->mWinBand  = htonl(client->mWinSize);
811     }
812     if ( client->mListenPort != 0 ) {
813         hdr->mPort  = htonl(client->mListenPort);
814     } else {
815         hdr->mPort  = htonl(client->mPort);
816     }
817     hdr->numThreads = htonl(client->mThreads);
818     if ( isModeTime( client ) ) {
819         hdr->mAmount    = htonl(-(long)client->mAmount);
820     } else {
821         hdr->mAmount    = htonl((long)client->mAmount);
822         hdr->mAmount &= htonl( 0x7FFFFFFF );
823     }
824     if ( client->mMode == kTest_DualTest ) {
825         hdr->flags |= htonl(RUN_NOW);
826     }
827 }