From: Samuel Jero Date: Sun, 15 Jun 2014 20:43:15 +0000 (-0500) Subject: Add a --bindport option to specify the client source port X-Git-Url: http://sjero.net/git/?p=iperf;a=commitdiff_plain;h=8e8d30a741b113ae729258010bd3a1f8bd7efdd0 Add a --bindport option to specify the client source port --- diff --git a/include/Settings.hpp b/include/Settings.hpp index 6383f48..05d2f4f 100644 --- a/include/Settings.hpp +++ b/include/Settings.hpp @@ -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 diff --git a/src/Locale.c b/src/Locale.c index 8c5ca16..f03d3a0 100644 --- a/src/Locale.c +++ b/src/Locale.c @@ -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 bind to \ \n\ Server specific:\n\ -s, --server run in server mode\n\ diff --git a/src/PerfSocket.cpp b/src/PerfSocket.cpp index de88a75..3a3b456 100644 --- a/src/PerfSocket.cpp +++ b/src/PerfSocket.cpp @@ -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) { diff --git a/src/Settings.cpp b/src/Settings.cpp index 42c21a2..0b59f5c 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -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; }