⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contest.c

📁 闻停开发板视频程序
💻 C
📖 第 1 页 / 共 4 页
字号:
//--------------------------------------------------------------------------
// IP Stack Console Demonstration Program
//--------------------------------------------------------------------------
// ConTest.c
//
// Example TCP/UDP sockets program - TEST
//
// Author: Michael A. Denio
// Copyright 2000 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <netmain.h>
#include <_stack.h>
#include <_oskern.h>
#include "console.h"

static void ConfigTest();
static void EchoTest( IPN IPAddr );
static void SendTest( IPN IPAddr );
static void RaceTest( IPN IPAddr );
static void ShutdownTest( IPN IPAddr );
static void ReuseTest( IPN IPAddr );
static void ShareTest( IPN IPAddr );
static void TcpShareRX( HANDLE hTaskParent, SOCKET s, int *pFlag );
static void TcpShareTX( HANDLE hTaskParent, SOCKET s );
static void TimeoutTest();
static void ConnectTest1( IPN IPAddr );
static void ConnectTest2( IPN IPAddr );
static void ConnectFlood( IPN IPAddr );
static void ShutdownTest1( IPN IPAddr, int fClose );
static void ShutdownTest2( IPN IPAddr, int fClose );
static void CloseTask( HANDLE hTaskParent, SOCKET s );
static void ShutdownTask( HANDLE hTaskParent, SOCKET s );
static void StatusTest( IPN IPAddr );
static void PollCounter();
static volatile UINT32 PollCount = 0;
static volatile UINT32 PollRunning = 0;


//-------------------------------------------------------------------------
// ConCmdTest()
//
// Function to run tests
//-------------------------------------------------------------------------
void ConCmdTest( int ntok, char *tok1, char *tok2 )
{
    IPN IPTmp;

    // Check for 'test echo x.x.x.x'
    if( ntok == 2 && !strcmp( tok1, "echo" ) )
    {
        if( !ConStrToIPN( tok2, &IPTmp ) )
            ConPrintf("Invalid address\n\n");
        else
            EchoTest( IPTmp );
    }
    // Check for 'test send x.x.x.x'
    else if( ntok == 2 && !strcmp( tok1, "send" ) )
    {
        if( !ConStrToIPN( tok2, &IPTmp ) )
            ConPrintf("Invalid address\n\n");
        else
            SendTest( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "race" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            RaceTest( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "shutdown" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ShutdownTest( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "close1" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ShutdownTest1( IPTmp, 1 );
    }
    else if( ntok == 1 && !strcmp( tok1, "shutdown1" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ShutdownTest1( IPTmp, 0 );
    }
    else if( ntok == 1 && !strcmp( tok1, "close2" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ShutdownTest2( IPTmp, 1 );
    }
    else if( ntok == 1 && !strcmp( tok1, "shutdown2" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ShutdownTest2( IPTmp, 0 );
    }
    else if( ntok == 1 && !strcmp( tok1, "status" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            StatusTest( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "reuse" ) )
    {
        // Get the public IP address
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ReuseTest( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "config" ) )
    {
        ConfigTest();
    }
    else if( ntok == 1 && !strcmp( tok1, "timeout" ) )
    {
        TimeoutTest();
    }
    else if( ntok == 2 && !strcmp( tok1, "shared" ) )
    {
        if( !ConStrToIPN( tok2, &IPTmp ) )
            ConPrintf("Invalid address\n\n");
        else
            ShareTest( IPTmp );
    }
    else if( ntok == 2 && !strcmp( tok1, "connect1" ) )
    {
        if( !ConStrToIPN( tok2, &IPTmp ) )
            ConPrintf("Invalid address\n\n");
        else
            ConnectTest1( IPTmp );
    }
    else if( ntok == 2 && !strcmp( tok1, "connect2" ) )
    {
        if( !ConStrToIPN( tok2, &IPTmp ) )
            ConPrintf("Invalid address\n\n");
        else
            ConnectTest2( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "conflood" ) )
    {
        if( !NtGetPublicHost( &IPTmp, 0, 0 ) )
            ConPrintf("Device not configured\n");
        else
            ConnectFlood( IPTmp );
    }
    else if( ntok == 1 && !strcmp( tok1, "counter" ) )
    {
        if( !PollRunning )
        {
            ConPrintf("[Launching Background Task]\n");
            TaskCreate( PollCounter, "Counter", 1, 1000, 0, 0, 0 );
        }
        ConPrintf("Counter = %d\n",PollCount);
        PollCount = 0;
    }
    else if( ntok == 0 )
    {
        ConPrintf("\n[Test Command]\n");
        ConPrintf("\nRun various TCP/IP stack tests.\n\n");
        ConPrintf("test echo x.x.x.x     - High volume TCP echo test\n");
        ConPrintf("test send x.x.x.x     - High volume TCP send test\n");
        ConPrintf("test race             - Race condition\n");
        ConPrintf("test shutdown         - Shutdown function\n");
        ConPrintf("test close1           - fdClose on LISTEN/ACCEPT socket\n");
        ConPrintf("test close2           - fdClose on LISTEN/SELECT socket\n");
        ConPrintf("test shutdown1        - Shutdown on LISTEN/ACCEPT socket\n");
        ConPrintf("test shutdown2        - Shutdown on LISTEN/SELECT socket\n");
        ConPrintf("test status           - Test the fdStatus() call\n");
        ConPrintf("test reuse            - Error socket resue (uses echo)\n");
        ConPrintf("test config           - Getting various config info\n");
        ConPrintf("test timeout          - Select and socket timeouts\n");
        ConPrintf("test shared x.x.x.x   - Shared sockets (uses echo)\n");
        ConPrintf("test connect1 x.x.x.x - Non-block Connect (uses echo)\n");
        ConPrintf("test connect2 x.x.x.x - Non-block Connect w/fdSelect (uses echo)\n");
        ConPrintf("test conflood         - Connection (TIMEWAIT) Flood (uses echo)\n");
        ConPrintf("test counter          - Install/Check backgound counting task\n\n");
    }
    else
        ConPrintf("\nCommand error. Type 'test' for help\n");
}


//----------------------------------------------------------------------
// ConfigTest()
//
// Test getting various config info
//----------------------------------------------------------------------
static void ConfigTest()
{
    char        IPString[16];
    IPN         IPAddr;
    int         i,j,ifcnt,rc;
    CI_IPNET    NA;
    CI_ROUTE    RT;

    // Get the number of IF's in the system
    llEnter();
    ifcnt = IFMaxIndex();
    llExit();

    // Print it
    ConPrintf("\nNumber of physical interfaces : %d\n",ifcnt);

    // Scan all IF's in the CFG for network information
    ConPrintf("\nIP Networks Installed:\n");
    for( i=1; i<=ifcnt; i++ )
    {
        j = 1;
        for(;;)
        {
            // Try and get a IP network address
            rc = CfgGetImmediate( 0, CFGTAG_IPNET, i, j,
                                  sizeof(NA), (UINT8 *)&NA );
            if( rc != sizeof(NA) )
                break;

            // We got something

            // Convert IP to a string:
            NtIPN2Str( NA.IPAddr, IPString );
            ConPrintf("On IF-%d, IP Addr='%s', ",i,IPString);
            NtIPN2Str( NA.IPMask, IPString );
            ConPrintf("NetMask='%s', Domain='%s'\n",IPString, NA.Domain);
            j++;
        }
    }

    // Now scan all routes entered via the configuration
    ConPrintf("\nManually Configured Routes:\n");
    for(i=1;;i++)
    {
        // Try and get a route
        rc = CfgGetImmediate( 0, CFGTAG_ROUTE, 0, i,
                             sizeof(RT), (UINT8 *)&RT );
        if( rc != sizeof(RT) )
            break;

        // We got something

        // Convert IP to a string:
        NtIPN2Str( RT.IPDestAddr, IPString );
        ConPrintf("Route, Dest='%s', ",IPString);
        NtIPN2Str( RT.IPDestMask, IPString );
        ConPrintf("Mask='%s', ",IPString);
        NtIPN2Str( RT.IPGateAddr, IPString );
        ConPrintf("Gateway='%s'\n",IPString);
    }
    if( i==1 )
        ConPrintf("None\n");

    // Now scan all DNS servers entered via the configuration
    ConPrintf("\nExternal DNS Server Information:\n");
    for(i=1;;i++)
    {
        // Try and get a DNS server
        rc = CfgGetImmediate( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
                              i, 4, (UINT8 *)&IPAddr );
        if( rc != 4 )
            break;

        // We got something

        // Convert IP to a string:
        NtIPN2Str( IPAddr, IPString );
        ConPrintf("DNS Server %d = '%s'\n", i, IPString);
    }
    if( i==1 )
        ConPrintf("None\n\n");
    else
        ConPrintf("\n");

    // Now scan all NBNS servers entered via the configuration
    ConPrintf("External NBNS Server Information:\n");
    for(i=1;;i++)
    {
        // Try and get a DNS server
        rc = CfgGetImmediate( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_NBNS,
                              i, 4, (UINT8 *)&IPAddr );
        if( rc != 4 )
            break;

        // We got something

        // Convert IP to a string:
        NtIPN2Str( IPAddr, IPString );
        ConPrintf("NBNS Server %d = '%s'\n", i, IPString);
    }
    if( i==1 )
        ConPrintf("None\n\n");
    else
        ConPrintf("\n");
}

//----------------------------------------------------------------------
// EchoTest()
//
// Test ECHO with a TCP socket
//----------------------------------------------------------------------
#define TEST_ITER       2500
#define		PC_ADDR		"10.129.8.69"
#define		PC_PORT		5050

static void EchoTest( IPN IPAddr )
{
    SOCKET  s;
    struct  sockaddr_in sin1;
    UINT32  test,i,j;
    int     k;
    char    *pBuf = 0;
    char    *pBuf_rec = 0;
    char    *pBufRx;
    HANDLE  hBuffer;
    struct  timeval timeout;
    UINT32  startS, startMS;
    UINT32  endS, endMS;
	fd_set  ibits;
    
    ConPrintf("\n== Start TCP Echo Client Test ==\n");

    // Create test socket
    s = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
    if( s == INVALID_SOCKET )
    {
        ConPrintf("failed socket create (%d)\n",fdError());
        goto leave;
    }

    // Prepare address for connect
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family      = AF_INET;
    sin1.sin_len         = sizeof( sin1 );
    sin1.sin_addr.s_addr = IPAddr;
    sin1.sin_port        = htons(7);

//	sin1.sin_addr.s_addr = inet_addr(PC_ADDR);
 //   sin1.sin_port        = htons(PC_PORT);// ( ((a>>8)&0xff) + ((a<<8)&0xff00) )

    // Configure our timeout to be 5 seconds
    timeout.tv_sec  = 5;
    timeout.tv_usec = 0;


    setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) );
    setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );

#if 1
 // Allocate a working buffer
 	if ( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed connect (%d)\n",fdError());
        goto leave;
    }

    if( !(pBuf = mmBulkAlloc( 8192 )) )
    {
        ConPrintf("failed temp buffer allocation\n");
        goto leave;
    }   
    
    if( !(pBuf_rec = mmBulkAlloc( 8192 )) )
    {
        ConPrintf("failed temp buffer allocation\n");
        goto leave;
    }
	
	test = 8192;
    startS = llTimerGetTime( &startMS );

    for( j=0; j<TEST_ITER; j++ )
    {
        // Send the buffer
	//	  cc = sendto( s, pBuf, DATALEN, 0, &to, sizeof(to) );
		for(i=0;i<8192;i++)
			pBuf[i] = j*i;
		for(i=0;i<8192;i++)
			pBuf_rec[i] = 0;

		if( send( s, pBuf, (int)test, 0 ) < 0 )
        {
            ConPrintf("send failed (%d)\n",fdError());
            break;
        }

        // Try and receive the buffer
        i = 0;
        while( i < test )
        {	int	fromlen;
           // k = recvnc( s, (void **)&pBufRx, 0, &hBuffer );
			k = recv( s, pBuf_rec, 1460, 0 );

           // fromlen = sizeof(sin1);
		 //   cc = (int)recvfrom( s, pBuf, MAXPACKET, 0, &from, &fromlen );
         //   k = (int)recvfrom( s, pBuf_rec, 1460, 0, &sin1, &fromlen );
            if( k < 0 )
            {
                ConPrintf("recv failed (%d)\n",fdError());
                goto leave;
            }
            if( !k )
            {
                ConPrintf("connection dropped\n");
                goto leave;
            }
        //    recvncfree( hBuffer );
            i += (UINT32)k;
        }

        // Verify reception size
        if( i != test )
        {
            ConPrintf("received %d (not %d) bytes\n",i,test);
            break;
        }
    }

#else
    // Connect socket
    if ( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed connect (%d)\n",fdError());
        goto leave;
    }

    // Allocate a working buffer
    if( !(pBuf = mmBulkAlloc( 8192 )) )
    {
        ConPrintf("failed temp buffer allocation\n");
        goto leave;
    }

    startS = llTimerGetTime( &startMS );

    // Start Test
    test = 8192;
    for( j=0; j<TEST_ITER; j++ )
    {
        // Send the buffer
		i = 0;
		for(i=0;i<8192;i++)
		{
			pBuf[i] = j*i;
			i = i;
		}
        if( send( s, pBuf, (int)test, 0 ) < 0 )
        {
            ConPrintf("send failed (%d)\n",fdError());
            break;
        }

        // Try and receive the buffer
        i = 0;
        while( i < test )
        {
            k = recvnc( s, (void **)&pBufRx, 0, &hBuffer );
            if( k < 0 )
            {
                ConPrintf("recv failed (%d)\n",fdError());
                goto leave;
            }
            if( !k )
            {
                ConPrintf("connection dropped\n");
                goto leave;
            }
            recvncfree( hBuffer );
            i += (UINT32)k;
        }

        // Verify reception size
        if( i != test )
        {
            ConPrintf("received %d (not %d) bytes\n",i,test);
            break;
        }
    }
#endif


    if( j == TEST_ITER )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -