]> sjero.net Git - iperf/blobdiff - src/PerfSocket.cpp
DCCP support for iperf
[iperf] / src / PerfSocket.cpp
index c25a0b6f80ad9118c9726c219a22c3767c1825e8..d149d596dd6ed2b9fd6d7d94693510c06b1f778d 100644 (file)
@@ -68,8 +68,6 @@
  *   <netinet/in.h>
  *   <sys/socket.h>
  * ------------------------------------------------------------------- */
-
-
 #define HEADERS()
 
 #include "headers.h"
 #include "SocketAddr.h"
 #include "util.h"
 
+// create an internet socket
+void MakeSocket(thread_Settings *inSettings)
+{
+   int type = 0, proto = 0, domain =
+                SockAddr_isIPv6(&inSettings->local) ? AF_INET6 :
+                AF_INET;
+
+    switch (inSettings->mProtocol) {
+        case kProto_TCP:     type  = SOCK_STREAM;     break;
+        case kProto_UDP:     type  = SOCK_DGRAM;      break;
+        case kProto_DCCP:    type  = SOCK_DCCP;       break;
+    }
+    inSettings->mSock = socket( domain, type, proto );
+
+    WARN_errno( inSettings->mSock == INVALID_SOCKET, "socket" );
+}
+
 /* -------------------------------------------------------------------
  * Set socket options before the listen() or connect() calls.
  * These are optional performance tuning factors.
  * ------------------------------------------------------------------- */
-
-void SetSocketOptions( thread_Settings *inSettings ) {
-    // set the TCP window size (socket buffer sizes)
-    // also the UDP buffer size
-    // must occur before call to accept() for large window sizes
-    setsock_tcp_windowsize( inSettings->mSock, inSettings->mTCPWin,
-                            (inSettings->mThreadMode == kMode_Client ? 1 : 0) );
+void SetSocketOptions( thread_Settings *inSettings )
+{
+    int rc, val;
+    Socklen_t len = sizeof(int);
 
     // check if we're sending multicast, and set TTL
     if ( isMulticast( inSettings ) && ( inSettings->mTTL > 0 ) ) {
-       int val = inSettings->mTTL;
+        val = inSettings->mTTL;
 #ifdef HAVE_MULTICAST
        if ( !SockAddr_isIPv6( &inSettings->local ) ) {
-           int rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_MULTICAST_TTL,
-                   (const void*) &val, (Socklen_t) sizeof(val));
+            rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_MULTICAST_TTL,
+                             &val, len);
 
            WARN_errno( rc == SOCKET_ERROR, "multicast ttl" );
        }
 #ifdef HAVE_IPV6_MULTICAST
        else {
-           int rc = setsockopt( inSettings->mSock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
-                   (const void*) &val, (Socklen_t) sizeof(val));
+            rc = setsockopt( inSettings->mSock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
+                             &val, len);
            WARN_errno( rc == SOCKET_ERROR, "multicast ttl" );
        }
-#endif
-#endif
+#endif /* IPV6_MULTICAST */
+#endif /* MULTICAST      */
     }
 
 
-#ifdef IP_TOS
-
     // set IP TOS (type-of-service) field
+#ifdef IP_TOS
     if ( inSettings->mTOS > 0 ) {
-        int  tos = inSettings->mTOS;
-        Socklen_t len = sizeof(tos);
-        int rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_TOS,
-                             (char*) &tos, len );
+        val = inSettings->mTOS;
+        rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_TOS, &val, len );
         WARN_errno( rc == SOCKET_ERROR, "setsockopt IP_TOS" );
     }
 #endif
 
-    if ( !isUDP( inSettings ) ) {
-        // set the TCP maximum segment size
+
+    // TCP-specific options
+    if ( inSettings->mProtocol == kProto_TCP ) {
+
+        // set the TCP window size (socket buffer sizes)
+        // must occur before call to accept() for large window sizes
+        setsock_tcp_windowsize(inSettings->mSock, inSettings->mWinSize,
+                               inSettings->mThreadMode == kMode_Client);
         setsock_tcp_mss( inSettings->mSock, inSettings->mMSS );
 
 #ifdef TCP_NODELAY
-
-        // set TCP nodelay option
         if ( isNoDelay( inSettings ) ) {
-            int nodelay = 1;
-            Socklen_t len = sizeof(nodelay);
-            int rc = setsockopt( inSettings->mSock, IPPROTO_TCP, TCP_NODELAY,
-                                 (char*) &nodelay, len );
+            val = 1;
+            rc = setsockopt( inSettings->mSock, IPPROTO_TCP, TCP_NODELAY,
+                             &val, len );
             WARN_errno( rc == SOCKET_ERROR, "setsockopt TCP_NODELAY" );
         }
 #endif
+    } else {
+        rc = set_buffer_sock_size(inSettings->mSock, inSettings->mWinSize,
+                                  inSettings->mThreadMode == kMode_Client);
+        WARN_errno( rc < 0 , "setsockopt for buffer size" );
+    }
+    // DCCP-specific options
+    if ( inSettings->mProtocol == kProto_DCCP ) {
+        /*
+         * We use the service code SC:PERF (0x50455246) from
+        * draft-fairhurst-dccp-serv-codes to identify this service.
+         */
+        val = htonl(0x50455246);                       /* ALWAYS use htonl */
+        rc = setsockopt( inSettings->mSock, SOL_DCCP, DCCP_SOCKOPT_SERVICE,
+                         &val, len );
+        WARN_errno( rc == SOCKET_ERROR, "setsockopt DCCP_SOCKOPT_SERVICE" );
     }
 }
-// end SetSocketOptions