Example #1
0
/* -------------------------------------------------------------------
 * 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) );

    if ( isCongestionControl( inSettings ) ) {
#ifdef TCP_CONGESTION
	Socklen_t len = strlen( inSettings->mCongestion ) + 1;
	int rc = setsockopt( inSettings->mSock, IPPROTO_TCP, TCP_CONGESTION,
			     inSettings->mCongestion, len);
	FAIL( rc == SOCKET_ERROR, ( "Attempt to set '%s' congestion control failed: %s\r\n", inSettings->mCongestion, strerror(errno) ), NULL );
#else
	fprintf( stderr, "The -Z option is not available on this operating system\r\n");
#endif /* TCP_CONGESTION */
    }

    // check if we're sending multicast, and set TTL
    if ( isMulticast( inSettings ) && ( inSettings->mTTL > 0 ) ) {
#ifdef HAVE_MULTICAST
    int val = inSettings->mTTL;
	if ( !SockAddr_isIPv6( &inSettings->local ) ) {
	    int rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_MULTICAST_TTL,
		    (const char*) &val, (Socklen_t) sizeof(val));

	    WARN_errno( rc == SOCKET_ERROR, ( "Failed to set multicast TTL.\r\n" ) );
	}
#ifdef HAVE_IPV6_MULTICAST
	else {
	    int rc = setsockopt( inSettings->mSock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
		    (const char*) &val, (Socklen_t) sizeof(val));
	    WARN_errno( rc == SOCKET_ERROR, ( "Failed to set multicast TTL.\r\n" ) );
	}
#endif /* HAVE_IPV6_MULTICAST */
#endif /* HAVE_MULTICAST */
    }

#ifdef IP_TOS
    // set IP TOS (type-of-service) field
//    if ( inSettings->mTOS > 0 ) {
        int  tos, rc;

        tos = inSettings->mTOS;
        Socklen_t len = sizeof(tos);
        rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_TOS,(char*) &tos, len );
        WARN_errno( rc == SOCKET_ERROR, ( "Failed to set IP_TOS.\r\n" ) );
//    }
#endif /* IP_TOS */

    if ( !isUDP( inSettings ) ) {
        // set the TCP maximum segment size
        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 );
            WARN_errno( rc == SOCKET_ERROR, ( "Failed to set TCP_NODELAY.\r\n" ) );
        }
#endif /* TCP_NODELAY */
    }
} // end SetSocketOptions
Example #2
0
 virtual int set_tcp_windowsize( int a, int b ) {
     return setsock_tcp_windowsize( a, b, 0 );
 };