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