Example #1
0
Server::Server( thread_Settings *inSettings ) {
    mSettings = inSettings;
    mBuf = NULL;

    // initialize buffer
    mBuf = new char[ mSettings->mBufLen ];
    FAIL_errno( mBuf == NULL, "No memory for buffer\n", mSettings );
}
Example #2
0
void Client::Connect( ) {
    int rc;
    SockAddr_remoteAddr( mSettings );

    assert( mSettings->inHostname != NULL );

    // create an internet socket
    int type = ( isUDP( mSettings )  ?  SOCK_DGRAM : SOCK_STREAM);

    int domain = (SockAddr_isIPv6( &mSettings->peer ) ? 
#ifdef HAVE_IPV6
                  AF_INET6
#else
                  AF_INET
#endif
                  : AF_INET);

    mSettings->mSock = socket( domain, type, 0 );
    WARN_errno( mSettings->mSock == INVALID_SOCKET, "socket" );

    SetSocketOptions( mSettings );


    SockAddr_localAddr( mSettings );
    if ( mSettings->mLocalhost != NULL ) {
        // bind socket to local address
        char temp[100] = "cbind:";
        SockAddr_getHostAddress(&mSettings->local, &temp[6], 94);
        rc = bind( mSettings->mSock, (sockaddr*) &mSettings->local, 
                   SockAddr_get_sizeof_sockaddr( &mSettings->local ) );
        WARN_errno( rc == SOCKET_ERROR, temp );
    }

    // connect socket
    rc = connect( mSettings->mSock, (sockaddr*) &mSettings->peer, 
                  SockAddr_get_sizeof_sockaddr( &mSettings->peer ));
    FAIL_errno( rc == SOCKET_ERROR, "connect", mSettings );

    getsockname( mSettings->mSock, (sockaddr*) &mSettings->local, 
                 &mSettings->size_local );
    getpeername( mSettings->mSock, (sockaddr*) &mSettings->peer,
                 &mSettings->size_peer );
} // end Connect
Example #3
0
void Client::write_UDP_FIN( ) {
    int rc; 
    fd_set readSet; 
    struct timeval timeout; 

    int count = 0; 
    while ( count < 10 ) {
        count++; 

        // write data 
        rc=write( mSettings->mSock, mBuf, mSettings->mBufLen );
	WARN_errno( rc < 0, "write");

        // wait until the socket is readable, or our timeout expires 
        FD_ZERO( &readSet ); 
        FD_SET( mSettings->mSock, &readSet ); 
        timeout.tv_sec  = 0; 
        timeout.tv_usec = 250000; // quarter second, 250 ms 

        rc = select( mSettings->mSock+1, &readSet, NULL, NULL, &timeout ); 
        FAIL_errno( rc == SOCKET_ERROR, "select", mSettings ); 

        if ( rc == 0 ) {
            // select timed out 
            continue; 
        } else {
            // socket ready to read 
            rc = read( mSettings->mSock, mBuf, mSettings->mBufLen ); 
            WARN_errno( rc < 0, "read" );
    	    if ( rc < 0 ) {
                break;
            } else if ( rc >= (int) (sizeof(UDP_datagram) + sizeof(server_hdr)) ) {
                ReportServerUDP( mSettings, (server_hdr*) ((UDP_datagram*)mBuf + 1) );
            }

            return; 
        } 
    } 

    fprintf( stderr, warn_no_ack, mSettings->mSock, count ); 
} 
Example #4
0
VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv) {
    
    // report the status to the service control manager.
    //
    if ( !ReportStatusToSCMgr(
                             SERVICE_START_PENDING, // service state
                             NO_ERROR,              // exit code
                             3000) )                 // wait hint
        goto clean;

    thread_Settings* ext_gSettings = (thread_Settings*) malloc(sizeof(thread_Settings));
	FAIL_errno( ext_gSettings == NULL, ( "No memory for thread_Settings ext_gSettings.\n" ), NULL );
	IPERF_DEBUGF( MEMALLOC_DEBUG | IPERF_DBG_TRACE, IPERF_MEMALLOC_MSG( ext_gSettings, sizeof(thread_Settings) ) );

    // Initialize settings to defaults
    Settings_Initialize( ext_gSettings );
#ifndef NO_ENVIRONMENT
    // read settings from environment variables
    Settings_ParseEnvironment( ext_gSettings );
#endif /* NO_ENVIRONMENT */
    // read settings from command-line parameters
    Settings_ParseCommandLine( dwArgc, lpszArgv, ext_gSettings );

    // report the status to the service control manager.
    //
    if ( !ReportStatusToSCMgr(
                             SERVICE_START_PENDING, // service state
                             NO_ERROR,              // exit code
                             3000) )                 // wait hint
        goto clean;

    // if needed, redirect the output into a specified file
    if ( !isSTDOUT( ext_gSettings ) ) {
        redirect( ext_gSettings->mOutputFileName );
    }

    // report the status to the service control manager.
    //
    if ( !ReportStatusToSCMgr(
                             SERVICE_START_PENDING, // service state
                             NO_ERROR,              // exit code
                             3000) )                 // wait hint
        goto clean;
    
    // initialize client(s)
    if ( ext_gSettings->mThreadMode == kMode_Client ) {
        client_init( ext_gSettings );
    }

    // start up the reporter and client(s) or listener
    {
        thread_Settings *into = NULL;
#ifdef HAVE_THREAD
        Settings_Copy( ext_gSettings, &into );
        into->mThreadMode = kMode_Reporter;
        into->runNow = ext_gSettings;
#else
        into = ext_gSettings;
#endif /* HAVE_THREAD */
        thread_start( into );
    }
    
    // report the status to the service control manager.
    //
    if ( !ReportStatusToSCMgr(
                             SERVICE_RUNNING,       // service state
                             NO_ERROR,              // exit code
                             0) )                    // wait hint
        goto clean;

    clean:
    // wait for other (client, server) threads to complete
    thread_joinall();
} // end ServiceStart
void PerfSocket::SetSocketOptions( void ) {
  // set the TCP window size (socket buffer sizes)
  // also the UDP buffer size
  // must occur before call to accept() for large window sizes
  set_tcp_windowsize( mSock, mSettings->mTCPWin );

#ifdef IP_TOS

  // set IP TOS (type-of-service) field
  if ( mSettings->mTOS > 0 ) {
    int  tos = mSettings->mTOS;
    Socklen_t len = sizeof(tos);
    int rc = setsockopt( mSock, IPPROTO_IP, IP_TOS,
			 (char*) &tos, len );
    FAIL_errno( rc == SOCKET_ERROR, "setsockopt IP_TOS" );
  }
#endif

  if ( ! mUDP ) {
    // set the TCP maximum segment size
    if(mSettings->mProtocol != IPPROTO_SCTP) {
      setsock_tcp_mss( mSock, mSettings->mMSS );

#ifdef TCP_NODELAY

      // set TCP nodelay option
      if ( mSettings->mNodelay ) {
	int nodelay = 1;
	Socklen_t len = sizeof(nodelay);
	int rc = setsockopt( mSock, IPPROTO_TCP, TCP_NODELAY,
			     (char*) &nodelay, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt TCP_NODELAY" );
      }
#endif
    } else {
      mSettings->mMSS  = getsock_sctp_mss( mSock );

#ifdef SCTP_NODELAY
      // set TCP nodelay option
      if ( mSettings->mNodelay ) {
	int nodelay = 1;
	Socklen_t len = sizeof(nodelay);
	int rc = setsockopt( mSock, IPPROTO_SCTP, SCTP_NODELAY,
			     (char*) &nodelay, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt SCTP_NODELAY" );
      }
#endif
#if defined(SCTP_PARTIAL_DELIVERY_POINT) && defined(SCTP_EXPLICIT_EOR)
      if(mSettings->mEmulation == true) {
	int rc;
	int val=1;
	Socklen_t len = sizeof(int);
	rc = setsockopt( mSock, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
			     (char*) &val, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt SCTP_PARTIAL_DELIVERY_POINT" );
	rc = setsockopt( mSock, IPPROTO_SCTP, SCTP_EXPLICIT_EOR,
			   (char*) &val, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt SCTP_EXPLICT_EOR" );
      }
#endif

    }
  }
}