]> sjero.net Git - iperf/blob - include/Settings.hpp
ec81dc9cee9330862057fccc7beb90ef74069c5e
[iperf] / include / Settings.hpp
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.hpp
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  *   <assert.h>
57  * ------------------------------------------------------------------- */
58
59 #ifndef SETTINGS_H
60 #define SETTINGS_H
61
62 #include "headers.h"
63 #include "Thread.h"
64
65 /* -------------------------------------------------------------------
66  * constants
67  * ------------------------------------------------------------------- */
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 // server/client mode
73 typedef enum ThreadMode {
74     kMode_Unknown = 0,
75     kMode_Server,
76     kMode_Client,
77     kMode_Reporter,
78     kMode_Listener
79 } ThreadMode;
80
81 // report mode
82 typedef enum ReportMode {
83     kReport_Default = 0,
84     kReport_CSV,
85     //kReport_XML,
86     kReport_MAXIMUM
87 } ReportMode;
88
89 // test mode
90 typedef enum TestMode {
91     kTest_Normal = 0,
92     kTest_DualTest,
93     kTest_TradeOff,
94     kTest_Unknown
95 } TestMode;
96
97 #include "Reporter.h"
98 /*
99  * The thread_Settings is a structure that holds all
100  * options for a given execution of either a client
101  * or server. By using this structure rather than
102  * a global structure or class we can have multiple
103  * clients or servers running with different settings.
104  * In version 2.0 and above this structure contains 
105  * all the information needed for a thread to execute
106  * and contains only C elements so it can be manipulated
107  * by either C or C++.
108  */
109 typedef struct thread_Settings {
110     // Pointers
111     char*  mFileName;               // -F
112     char*  mHost;                   // -c
113     char*  mLocalhost;              // -B
114     char*  mOutputFileName;         // -o
115     FILE*  Extractor_file;
116     ReportHeader*  reporthdr;
117     MultiHeader*   multihdr;
118     struct thread_Settings *runNow;
119     struct thread_Settings *runNext;
120     // int's
121     int mThreads;                   // -P
122     int mTOS;                       // -S
123     int mSock;
124     int Extractor_size;
125     int mBufLen;                    // -l
126     int mMSS;                       // -M
127     int mTCPWin;                    // -w
128     /*   flags is a BitMask of old bools
129         bool   mBufLenSet;              // -l
130         bool   mCompat;                 // -C
131         bool   mDaemon;                 // -D
132         bool   mDomain;                 // -V
133         bool   mFileInput;              // -F or -I
134         bool   mNodelay;                // -N
135         bool   mPrintMSS;               // -m
136         bool   mRemoveService;          // -R
137         bool   mStdin;                  // -I
138         bool   mStdout;                 // -o
139         bool   mSuggestWin;             // -W
140         bool   mUDP;                    // -u
141         bool   mMode_time;
142         bool   mReportSettings;
143         bool   mMulticast;
144         bool   mNoSettingsReport;       // -x s
145         bool   mNoConnectionReport;     // -x c
146         bool   mNoDataReport;           // -x d
147         bool   mNoServerReport;         // -x 
148         bool   mNoMultReport;           // -x m
149         bool   mSinlgeClient;           // -1 */
150     int flags; 
151     // enums (which should be special int's)
152     ThreadMode mThreadMode;         // -s or -c
153     ReportMode mReportMode;
154     TestMode mMode;                 // -r or -d
155     // Hopefully int64_t's
156     max_size_t mUDPRate;            // -b or -u
157     max_size_t mAmount;             // -n or -t
158     // doubles
159     double mInterval;               // -i
160     // shorts
161     unsigned short mListenPort;     // -L
162     unsigned short mPort;           // -p
163     // chars
164     char   mFormat;                 // -f
165     int mTTL;                    // -T
166     char pad1[2];
167     // structs or miscellaneous
168     iperf_sockaddr peer;
169     Socklen_t size_peer;
170     iperf_sockaddr local;
171     Socklen_t size_local;
172     nthread_t mTID;
173 #if defined( HAVE_WIN32_THREAD )
174     HANDLE mHandle;
175 #endif
176 } thread_Settings;
177
178 /*
179  * Due to the use of thread_Settings in C and C++
180  * we are unable to use bool values. To provide
181  * the functionality of bools we use the following
182  * bitmask over an assumed 32 bit int. This will
183  * work fine on 64bit machines we will just be ignoring
184  * the upper 32bits.
185  *
186  * To add a flag simply define it as the next bit then
187  * add the 3 support functions below.
188  */
189 #define FLAG_BUFLENSET      0x00000001
190 #define FLAG_COMPAT         0x00000002
191 #define FLAG_DAEMON         0x00000004
192 #define FLAG_DOMAIN         0x00000008
193 #define FLAG_FILEINPUT      0x00000010
194 #define FLAG_NODELAY        0x00000020
195 #define FLAG_PRINTMSS       0x00000040
196 #define FLAG_REMOVESERVICE  0x00000080
197 #define FLAG_STDIN          0x00000100
198 #define FLAG_STDOUT         0x00000200
199 #define FLAG_SUGGESTWIN     0x00000400
200 #define FLAG_UDP            0x00000800
201 #define FLAG_MODETIME       0x00001000
202 #define FLAG_REPORTSETTINGS 0x00002000
203 #define FLAG_MULTICAST      0x00004000
204 #define FLAG_NOSETTREPORT   0x00008000
205 #define FLAG_NOCONNREPORT   0x00010000
206 #define FLAG_NODATAREPORT   0x00020000
207 #define FLAG_NOSERVREPORT   0x00040000
208 #define FLAG_NOMULTREPORT   0x00080000
209 #define FLAG_SINGLECLIENT   0x00100000
210 #define FLAG_SINGLEUDP      0x00200000
211
212 #define isBuflenSet(settings)      ((settings->flags & FLAG_BUFLENSET) != 0)
213 #define isCompat(settings)         ((settings->flags & FLAG_COMPAT) != 0)
214 #define isDaemon(settings)         ((settings->flags & FLAG_DAEMON) != 0)
215 #define isIPV6(settings)           ((settings->flags & FLAG_DOMAIN) != 0)
216 #define isFileInput(settings)      ((settings->flags & FLAG_FILEINPUT) != 0)
217 #define isNoDelay(settings)        ((settings->flags & FLAG_NODELAY) != 0)
218 #define isPrintMSS(settings)       ((settings->flags & FLAG_PRINTMSS) != 0)
219 #define isRemoveService(settings)  ((settings->flags & FLAG_REMOVESERVICE) != 0)
220 #define isSTDIN(settings)          ((settings->flags & FLAG_STDIN) != 0)
221 #define isSTDOUT(settings)         ((settings->flags & FLAG_STDOUT) != 0)
222 #define isSuggestWin(settings)     ((settings->flags & FLAG_SUGGESTWIN) != 0)
223 #define isUDP(settings)            ((settings->flags & FLAG_UDP) != 0)
224 #define isModeTime(settings)       ((settings->flags & FLAG_MODETIME) != 0)
225 #define isReport(settings)         ((settings->flags & FLAG_REPORTSETTINGS) != 0)
226 #define isMulticast(settings)      ((settings->flags & FLAG_MULTICAST) != 0)
227 // Active Low for Reports
228 #define isSettingsReport(settings) ((settings->flags & FLAG_NOSETTREPORT) == 0)
229 #define isConnectionReport(settings)  ((settings->flags & FLAG_NOCONNREPORT) == 0)
230 #define isDataReport(settings)     ((settings->flags & FLAG_NODATAREPORT) == 0)
231 #define isServerReport(settings)   ((settings->flags & FLAG_NOSERVREPORT) == 0)
232 #define isMultipleReport(settings) ((settings->flags & FLAG_NOMULTREPORT) == 0)
233 // end Active Low
234 #define isSingleClient(settings)   ((settings->flags & FLAG_SINGLECLIENT) != 0)
235 #define isSingleUDP(settings)      ((settings->flags & FLAG_SINGLEUDP) != 0)
236
237 #define setBuflenSet(settings)     settings->flags |= FLAG_BUFLENSET
238 #define setCompat(settings)        settings->flags |= FLAG_COMPAT
239 #define setDaemon(settings)        settings->flags |= FLAG_DAEMON
240 #define setIPV6(settings)          settings->flags |= FLAG_DOMAIN
241 #define setFileInput(settings)     settings->flags |= FLAG_FILEINPUT
242 #define setNoDelay(settings)       settings->flags |= FLAG_NODELAY
243 #define setPrintMSS(settings)      settings->flags |= FLAG_PRINTMSS
244 #define setRemoveService(settings) settings->flags |= FLAG_REMOVESERVICE
245 #define setSTDIN(settings)         settings->flags |= FLAG_STDIN
246 #define setSTDOUT(settings)        settings->flags |= FLAG_STDOUT
247 #define setSuggestWin(settings)    settings->flags |= FLAG_SUGGESTWIN
248 #define setUDP(settings)           settings->flags |= FLAG_UDP
249 #define setModeTime(settings)      settings->flags |= FLAG_MODETIME
250 #define setReport(settings)        settings->flags |= FLAG_REPORTSETTINGS
251 #define setMulticast(settings)     settings->flags |= FLAG_MULTICAST
252 #define setNoSettReport(settings)  settings->flags |= FLAG_NOSETTREPORT
253 #define setNoConnReport(settings)  settings->flags |= FLAG_NOCONNREPORT
254 #define setNoDataReport(settings)  settings->flags |= FLAG_NODATAREPORT
255 #define setNoServReport(settings)  settings->flags |= FLAG_NOSERVREPORT
256 #define setNoMultReport(settings)  settings->flags |= FLAG_NOMULTREPORT
257 #define setSingleClient(settings)  settings->flags |= FLAG_SINGLECLIENT
258 #define setSingleUDP(settings)     settings->flags |= FLAG_SINGLEUDP
259
260 #define unsetBuflenSet(settings)   settings->flags &= ~FLAG_BUFLENSET
261 #define unsetCompat(settings)      settings->flags &= ~FLAG_COMPAT
262 #define unsetDaemon(settings)      settings->flags &= ~FLAG_DAEMON
263 #define unsetIPV6(settings)        settings->flags &= ~FLAG_DOMAIN
264 #define unsetFileInput(settings)   settings->flags &= ~FLAG_FILEINPUT
265 #define unsetNoDelay(settings)     settings->flags &= ~FLAG_NODELAY
266 #define unsetPrintMSS(settings)    settings->flags &= ~FLAG_PRINTMSS
267 #define unsetRemoveService(settings)  settings->flags &= ~FLAG_REMOVESERVICE
268 #define unsetSTDIN(settings)       settings->flags &= ~FLAG_STDIN
269 #define unsetSTDOUT(settings)      settings->flags &= ~FLAG_STDOUT
270 #define unsetSuggestWin(settings)  settings->flags &= ~FLAG_SUGGESTWIN
271 #define unsetUDP(settings)         settings->flags &= ~FLAG_UDP
272 #define unsetModeTime(settings)    settings->flags &= ~FLAG_MODETIME
273 #define unsetReport(settings)      settings->flags &= ~FLAG_REPORTSETTINGS
274 #define unsetMulticast(settings)   settings->flags &= ~FLAG_MULTICAST
275 #define unsetNoSettReport(settings)   settings->flags &= ~FLAG_NOSETTREPORT
276 #define unsetNoConnReport(settings)   settings->flags &= ~FLAG_NOCONNREPORT
277 #define unsetNoDataReport(settings)   settings->flags &= ~FLAG_NODATAREPORT
278 #define unsetNoServReport(settings)   settings->flags &= ~FLAG_NOSERVREPORT
279 #define unsetNoMultReport(settings)   settings->flags &= ~FLAG_NOMULTREPORT
280 #define unsetSingleClient(settings)   settings->flags &= ~FLAG_SINGLECLIENT
281 #define unsetSingleUDP(settings)      settings->flags &= ~FLAG_SINGLEUDP
282
283
284 #define HEADER_VERSION1 0x80000000
285 #define RUN_NOW         0x00000001
286
287 // used to reference the 4 byte ID number we place in UDP datagrams
288 // use int32_t if possible, otherwise a 32 bit bitfield (e.g. on J90) 
289 typedef struct UDP_datagram {
290 #ifdef HAVE_INT32_T
291     int32_t id;
292     u_int32_t tv_sec;
293     u_int32_t tv_usec;
294 #else
295     signed   int id      : 32;
296     unsigned int tv_sec  : 32;
297     unsigned int tv_usec : 32;
298 #endif
299 } UDP_datagram;
300
301 /*
302  * The client_hdr structure is sent from clients
303  * to servers to alert them of things that need
304  * to happen. Order must be perserved in all 
305  * future releases for backward compatibility.
306  * 1.7 has flags, numThreads, mPort, and bufferlen
307  */
308 typedef struct client_hdr {
309
310 #ifdef HAVE_INT32_T
311
312     /*
313      * flags is a bitmap for different options
314      * the most significant bits are for determining
315      * which information is available. So 1.7 uses
316      * 0x80000000 and the next time information is added
317      * the 1.7 bit will be set and 0x40000000 will be
318      * set signifying additional information. If no 
319      * information bits are set then the header is ignored.
320      * The lowest order diferentiates between dualtest and
321      * tradeoff modes, wheither the speaker needs to start 
322      * immediately or after the audience finishes.
323      */
324     int32_t flags;
325     int32_t numThreads;
326     int32_t mPort;
327     int32_t bufferlen;
328     int32_t mWinBand;
329     int32_t mAmount;
330 #else
331     signed int flags      : 32;
332     signed int numThreads : 32;
333     signed int mPort      : 32;
334     signed int bufferlen  : 32;
335     signed int mWinBand   : 32;
336     signed int mAmount    : 32;
337 #endif
338 } client_hdr;
339
340 /*
341  * The server_hdr structure facilitates the server
342  * report of jitter and loss on the client side.
343  * It piggy_backs on the existing clear to close
344  * packet.
345  */
346 typedef struct server_hdr {
347
348 #ifdef HAVE_INT32_T
349
350     /*
351      * flags is a bitmap for different options
352      * the most significant bits are for determining
353      * which information is available. So 1.7 uses
354      * 0x80000000 and the next time information is added
355      * the 1.7 bit will be set and 0x40000000 will be
356      * set signifying additional information. If no 
357      * information bits are set then the header is ignored.
358      */
359     int32_t flags;
360     int32_t total_len1;
361     int32_t total_len2;
362     int32_t stop_sec;
363     int32_t stop_usec;
364     int32_t error_cnt;
365     int32_t outorder_cnt;
366     int32_t datagrams;
367     int32_t jitter1;
368     int32_t jitter2;
369 #else
370     signed int flags        : 32;
371     signed int total_len1   : 32;
372     signed int total_len2   : 32;
373     signed int stop_sec     : 32;
374     signed int stop_usec    : 32;
375     signed int error_cnt    : 32;
376     signed int outorder_cnt : 32;
377     signed int datagrams    : 32;
378     signed int jitter1      : 32;
379     signed int jitter2      : 32;
380 #endif
381
382 } server_hdr;
383
384     // set to defaults
385     void Settings_Initialize( thread_Settings* main );
386
387     // copy structure
388     void Settings_Copy( thread_Settings* from, thread_Settings** into );
389
390     // free associated memory
391     void Settings_Destroy( thread_Settings *mSettings );
392
393     // parse settings from user's environment variables
394     void Settings_ParseEnvironment( thread_Settings *mSettings );
395
396     // parse settings from app's command line
397     void Settings_ParseCommandLine( int argc, char **argv, thread_Settings *mSettings );
398
399     // convert to lower case for [KMG]bits/sec
400     void Settings_GetLowerCaseArg(const char *,char *);
401
402     // convert to upper case for [KMG]bytes/sec
403     void Settings_GetUpperCaseArg(const char *,char *);
404
405     // generate settings for listener instance
406     void Settings_GenerateListenerSettings( thread_Settings *client, thread_Settings **listener);
407
408     // generate settings for speaker instance
409     void Settings_GenerateClientSettings( thread_Settings *server, 
410                                           thread_Settings **client,
411                                           client_hdr *hdr );
412
413     // generate client header for server
414     void Settings_GenerateClientHdr( thread_Settings *client, client_hdr *hdr );
415
416 #ifdef __cplusplus
417 } /* end extern "C" */
418 #endif
419
420 #endif // SETTINGS_H