]> sjero.net Git - iperf/blobdiff - src/tcp_window_size.c
DCCP support for iperf
[iperf] / src / tcp_window_size.c
index bdd28c5c32e1c95755259d9ab2845f85ca886eae..8bc107202f60238bf9e670d579940f156f477ce7 100644 (file)
 extern "C" {
 #endif
 
+/* -------------------------------------------------------------------
+ * Set the socket buffer size in bytes (returns -1 on error, 0 otherwise).
+ * ------------------------------------------------------------------- */
+int set_buffer_sock_size(int inSock, int inWinSize , bool inSend)
+{
+   // note: results are verified after connect() or listen(),
+   // since some OS's don't show the corrected value until then.
+#ifdef SO_SNDBUF
+    if (inWinSize > 0) {
+        return setsockopt(inSock, SOL_SOCKET,
+                          inSend ? SO_SNDBUF: SO_RCVBUF,
+                          &inWinSize, sizeof(inWinSize));
+    }
+#else
+    fprintf(stderr, "%s: no support for SO_SNDBUF\n", __FUNCTION__);
+#endif
+    return 0;
+}
+
 /* -------------------------------------------------------------------
  * If inTCPWin > 0, set the TCP window size (via the socket buffer
  * sizes) for inSock. Otherwise leave it as the system default.
@@ -69,18 +88,16 @@ extern "C" {
  * This now works on AIX, by enabling RFC1323.
  * returns -1 on error, 0 on no error.
  * ------------------------------------------------------------------- */
-
-int setsock_tcp_windowsize( int inSock, int inTCPWin, int inSend ) {
-#ifdef SO_SNDBUF
-    int rc;
-    int newTCPWin;
-
+int setsock_tcp_windowsize(int inSock, int inTCPWin, int inSend)
+{
     assert( inSock >= 0 );
 
-    if ( inTCPWin > 0 ) {
-
+    if (inTCPWin > 0) {
+       if (inTCPWin < 2048)
+         fprintf(stderr, "WARNING: A TCP window size of %d bytes is "
+                        "considered small and will give poor performance. "
+                         "See the Iperf documentation.\n", inTCPWin);
 #ifdef TCP_WINSHIFT
-
         /* UNICOS requires setting the winshift explicitly */
         if ( inTCPWin > 65535 ) {
             int winShift = 0;
@@ -116,62 +133,33 @@ int setsock_tcp_windowsize( int inSock, int inTCPWin, int inSend ) {
             }
         }
 #endif /* TCP_RFC1323 */
-
-        if ( !inSend ) {
-            /* receive buffer -- set
-             * note: results are verified after connect() or listen(),
-             * since some OS's don't show the corrected value until then. */
-            newTCPWin = inTCPWin;
-            rc = setsockopt( inSock, SOL_SOCKET, SO_RCVBUF,
-                             (char*) &newTCPWin, sizeof( newTCPWin ));
-        } else {
-            /* send buffer -- set
-             * note: results are verified after connect() or listen(),
-             * since some OS's don't show the corrected value until then. */
-            newTCPWin = inTCPWin;
-            rc = setsockopt( inSock, SOL_SOCKET, SO_SNDBUF,
-                             (char*) &newTCPWin, sizeof( newTCPWin ));
-        }
-        if ( rc < 0 ) {
-            return rc;
-        }
     }
-#endif /* SO_SNDBUF */
-
-    return 0;
-} /* end setsock_tcp_windowsize */
+    return set_buffer_sock_size(inSock, inTCPWin, inSend);
+}
 
 /* -------------------------------------------------------------------
- * returns the TCP window size (on the sending buffer, SO_SNDBUF),
+ * returns the send/receive socket buffer size in bytes
  * or -1 on error.
  * ------------------------------------------------------------------- */
-
-int getsock_tcp_windowsize( int inSock, int inSend ) {
-    int theTCPWin = 0;
-
+int get_buffer_sock_size( int inSock, int inSend )
+{
+    int bufSize = 0;
 #ifdef SO_SNDBUF
+    Socklen_t len = sizeof(bufSize);
     int rc;
-    Socklen_t len;
 
-    /* send buffer -- query for buffer size */
-    len = sizeof( theTCPWin );
-    if ( inSend ) {
+    if ( inSend )
         rc = getsockopt( inSock, SOL_SOCKET, SO_SNDBUF,
-                         (char*) &theTCPWin, &len );
-    } else {
+                         (char*) &bufSize, &len );
+    else
         rc = getsockopt( inSock, SOL_SOCKET, SO_RCVBUF,
-                         (char*) &theTCPWin, &len );
-    }
-    if ( rc < 0 ) {
+                         (char*) &bufSize, &len );
+    if (rc < 0)
         return rc;
-    }
-
 #endif
-
-    return theTCPWin;
-} /* end getsock_tcp_windowsize */
+    return bufSize;
+}
 
 #ifdef __cplusplus
 } /* end extern "C" */
 #endif
-