]> sjero.net Git - iperf/commitdiff
Add a --bindport option to specify the client source port
authorSamuel Jero <sjero@purdue.edu>
Sun, 15 Jun 2014 20:43:15 +0000 (15:43 -0500)
committerSamuel Jero <sjero@purdue.edu>
Sun, 15 Jun 2014 20:43:15 +0000 (15:43 -0500)
include/Settings.hpp
src/Locale.c
src/PerfSocket.cpp
src/Settings.cpp

index 6383f48a02a510b9643e8601ddac2a57fb863559..05d2f4f6efffdae590254103fd14bff0e901cdd7 100644 (file)
@@ -161,6 +161,7 @@ typedef struct thread_Settings {
     int mMSS;                       // -M
     int mWinSize;                    // -w
     int mCCID;                         // -Z
+    int mBindPort;                     // -O
     /*   flags is a BitMask of old bools
         bool   mBufLenSet;              // -l
         bool   mCompat;                 // -C
index 8c5ca16cf132643fc55d5b26e2b0894ebc16f2c4..f03d3a0a9c76de86eb09120604981d6ba1917120 100644 (file)
@@ -89,6 +89,7 @@ Client/Server:\n\
   -N, --nodelay            set TCP no delay, disabling Nagle's Algorithm\n\
   -V, --IPv6Version        Set the domain to IPv6\n\
   -Z, --ccid      #        Set DCCP congestion control algorithm\n\
+  -O, --bindport  <port>   bind to <port>\
 \n\
 Server specific:\n\
   -s, --server             run in server mode\n\
index de88a753f8709600ddd91ee1b30da4e9b842511d..3a3b4561d69b9947859d69779cf8b94c418cb443 100644 (file)
@@ -179,7 +179,7 @@ void SetSocketOptions( thread_Settings *inSettings )
     }
 
     // reuse the address, so we can run if a former server was killed off
-    if (inSettings->mThreadMode == kMode_Listener) {
+    if (inSettings->mThreadMode == kMode_Listener || inSettings->mBindPort > 0) {
        val = 1;
        rc = setsockopt(inSettings->mSock, SOL_SOCKET, SO_REUSEADDR, &val, len);
         WARN_errno( rc == SOCKET_ERROR, "setsockopt SO_REUSEADDR" );
@@ -191,6 +191,7 @@ void MakeSocket(thread_Settings *inSettings)
        struct addrinfo *local = NULL, *src,
                        *remote = NULL, *dst, hints;
        char            port[6];
+       char            lport[6];
        int             rc, socktype = sockType(inSettings->mProtocol);
 
        assert(inSettings->mLocalhost || inSettings->mHost);
@@ -198,6 +199,11 @@ void MakeSocket(thread_Settings *inSettings)
         memset(&inSettings->local, 0, sizeof(inSettings->local));
         memset(&inSettings->peer,  0, sizeof(inSettings->peer));
        sprintf(port, "%u", inSettings->mPort);
+       if(inSettings->mBindPort > 0){
+               sprintf(lport, "%u", inSettings->mBindPort);
+       }else{
+               sprintf(lport, "%u", inSettings->mPort);
+       }
 
        /*
         *      Set up address hint structure
@@ -222,12 +228,12 @@ void MakeSocket(thread_Settings *inSettings)
        /*
         *      Obtain local/remote address information
         */
-       if (inSettings->mLocalhost || inSettings->mThreadMode == kMode_Listener) {
-               if (inSettings->mLocalhost == NULL)
+       if (inSettings->mLocalhost || inSettings->mBindPort > 0 || inSettings->mThreadMode == kMode_Listener) {
+               if (inSettings->mThreadMode == kMode_Listener)
                        hints.ai_flags |= AI_PASSIVE;
-               if ((rc = getaddrinfo(inSettings->mLocalhost, port, &hints, &local)))
+               if ((rc = getaddrinfo(inSettings->mLocalhost, lport, &hints, &local)))
                        die("Can not resolve local address %s#%s: %s",
-                           inSettings->mLocalhost ? : "(local)", port, gai_strerror(rc));
+                           inSettings->mLocalhost ? : "(local)", lport, gai_strerror(rc));
        }
 
        if (inSettings->mHost && inSettings->mThreadMode != kMode_Listener) {
index 42c21a2bf5b22630f0c329478a2510c81ff36998..0b59f5c67650347d4128c50bbf9c0f0ffef2764f 100644 (file)
@@ -125,6 +125,7 @@ const struct option long_options[] =
 {"ipv6_domian",      no_argument, NULL, 'V'},
 {"suggest_win_size", no_argument, NULL, 'W'},
 {"ccid",          required_argument, NULL, 'Z'},
+{"bindport",   required_argument, NULL, 'O'},
 {0, 0, 0, 0}
 };
 
@@ -171,13 +172,14 @@ const struct option env_options[] =
 {"IPERF_IPV6_DOMAIN",      no_argument, NULL, 'V'},
 {"IPERF_SUGGEST_WIN_SIZE", required_argument, NULL, 'W'},
 {"IPERF_CCID", required_argument, NULL, 'Z'},
+{"IPERF_BINDPORT",   required_argument, NULL, 'O'},
 {0, 0, 0, 0}
 };
 
 #define SHORT_OPTIONS()
 
 const char short_options[] =
-      "12b::c:df:hi:j:l:mn:o:p:rst:uvw:x:y:A:B:CDF:IJ:L:M:NP:RS:T:UV:WZ:";
+      "12b::c:df:hi:j:l:mn:o:p:rst:uvw:x:y:A:B:CDF:IJ:L:M:NP:RS:T:UV:WZ:0:";
 
 /* -------------------------------------------------------------------
  * defaults
@@ -678,6 +680,13 @@ void Settings_Interpret( char option, const char *optarg, thread_Settings *mExtS
                }
                break;
 
+       case 'O': //Bind Port
+               mExtSettings->mBindPort=atoi(optarg);
+               if(mExtSettings->mBindPort <0 || mExtSettings->mBindPort > 65535){
+                       fprintf( stderr, "Bind Port %s is invalid\n", optarg);
+               }
+               break;
+
         default: // ignore unknown
             break;
     }