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