Exemplo n.º 1
0
int main()
{
    InitWinsock();

    char szDestIp[] = "172.20.250.21";
    SOCKET sRaw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if (sRaw == INVALID_SOCKET)
    {
        printf("Create raw socket failed: %d\n", GetLastError());
        return -1;
    }
    SetTimeout(sRaw, 1000, TRUE); // This function not found on Win API

    // Set destination
    SOCKADDR_IN dest;
    dest.sin_family = AF_INET;
    dest.sin_port = htons(0);
    dest.sin_addr.S_un.S_addr = inet_addr(szDestIp);

    // Create ICMP
    char buff[sizeof(ICMP_HDR) + 32]; // ICMP_HDR + IPHeader
    PICMP_HDR pIcmp = (PICMP_HDR) buff;
    pIcmp->icmp_type = 8; // Echo
    pIcmp->icmp_code = 0;
    pIcmp->icmp_id = (USHORT) GetCurrentProcessId();
    pIcmp->icmp_checksum = 0;
    pIcmp->icmp_sequence = 0;
    memset(&buff[sizeof(ICMP_HDR)], 'E', 32); // filled with whatever

    // Send / Recv
    USHORT nSeq = 0;
    char recvBuf[1024];
    SOCKADDR_IN from;
    int nLen = sizeof(from);

    while (TRUE)
    {
        static int nCount = 0;
        int nRet = 0;
        if (nCount ++ == 4)
            break;

        pIcmp->icmp_checksum = 0;
        pIcmp->icmp_timestamp = GetTickCount();
        pIcmp->icmp_sequence = nSeq ++;
        pIcmp->icmp_checksum = checksum((USHORT *) buff, sizeof(ICMP_HDR) + 32);

        // Send
        nRet = sendto(sRaw, buff, sizeof(ICMP_HDR) + 32, 0, (SOCKADDR *) &dest,
                      sizeof(dest));
        if (nRet == SOCKET_ERROR)
        {
            printf("sendto() failed: %d\n", WSAGetLastError());
            return -1;
        }

        // Recv
        nRet = recvfrom(sRaw, recvBuf, 1024, 0, (SOCKADDR *) &from, &nLen);
        if (nRet == SOCKET_ERROR)
        {
            if (WSAGetLastError() == WSAETIMEDOUT)
            {
                printf("time out\n");
                continue;
            }
            printf("recvfrom() failed: %d\n", WSAGetLastError());
            return -1;
        }

        // Unpack
        int nTick = GetTickCount();
        if (nRet < 20 + sizeof(ICMP_HDR))
        {
            printf("Too few bytes from %s\n", inet_ntoa(from.sin_addr));
            return -1;
        }
        PICMP_HDR pRecvIcmp = (PICMP_HDR)(recvBuf + 20); // size of IP header
        if (pRecvIcmp->icmp_type != 0)
        {
            printf("nonecho type %d received\n", pRecvIcmp->icmp_type);
            return -1;
        }
        if (pRecvIcmp->icmp_id != GetCurrentProcessId())
        {
            printf("someone else's packet!\n");
            return -1;
        }

        printf(" %d types from %s:", nRet, inet_ntoa(from.sin_addr));
        printf(" icmp_seq = %d.", pRecvIcmp->icmp_sequence);
        printf(" time: %d ms\n", nTick - pRecvIcmp->icmp_timestamp);
        Sleep(1000);
    }

    CleanupWinsock();
    return 0;
}
Exemplo n.º 2
0
/**
 * main
 * 
 * executable entry point
 */
int __cdecl main( int argc, char **argv ) 
{
    /* local variables */
    int err;
    WORD wVersionRequested;
    WSADATA wsaData;
    DWORD dwError;
    
    HANDLE hEvent;

    struct sockaddr_in mySockaddr;
    SOCKET sock = INVALID_SOCKET;



    /* PAL initialization */
    if ( (PAL_Initialize(argc, argv)) != 0 )
    {
        return( FAIL );
    }


    /* initialize winsock version 2.2 */
    wVersionRequested = MAKEWORD( 2, 2 );

    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 )
    {
        Fail( "Unexpected WSAStartup call failed with error code %d\n", 
              err ); 
    }

    /* Confirm that the WinSock DLL supports the specified version. */
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 )
    {
        /* Tell the user that we could not find a usable winsock DLL. */
        CleanupWinsock();
        Fail(   "Requested winsock version unsupported, "
                "returned version %d.%d\n", 
                LOBYTE( wsaData.wVersion ),
                HIBYTE( wsaData.wVersion ) ); 
    }


    
    /* create an event for socket notifications */    
    hEvent = CreateEvent( NULL, FALSE, FALSE, "foo" );
    if( hEvent == NULL )
    {
        dwError = GetLastError();
        Trace(  "CreateEvent() call failed with error code %lu\n",
                dwError );
        CleanupWinsock();
        Fail( "test failed\n" );
    }

    
    /* create a stream socket in Internet domain */
    sock = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );
    if ( sock == INVALID_SOCKET )
    {
        /* check the socket-specific error code */    
        dwError = GetLastError();
        Trace( "socket() call failed with error code %lu\n",
                dwError );
        MyCloseHandle( hEvent );
        CleanupWinsock();
        Fail( "test failed\n" );
    }

    /* call WSAEventSelect -- this should set the socket to non-blocking */
    err = WSAEventSelect(   sock,
                            hEvent,
                            FD_CONNECT );


    /* prepare the sockaddr structure */
    mySockaddr.sin_family = AF_INET;
    mySockaddr.sin_port = getRotorTestPort();
    mySockaddr.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1" );
    memset( &(mySockaddr.sin_zero), 0, 8 );

    /* try to connect, there should be no listening socket */
    err = connect(  sock,
                    (struct sockaddr *)&mySockaddr,
                    sizeof(struct sockaddr) );
                    
    if( (err == 0) ||
        (err == SOCKET_ERROR &&
                (((dwError = GetLastError()) != WSAEWOULDBLOCK)
                    &&
                    (dwError!=WSAECONNREFUSED)
                ) 
        )
      )
    {
        if( err == SOCKET_ERROR )
        {
            Trace(  "connect called failed with error code %lu, "
                    " expected WSAEWOULDBLOCK or WSAECONNREFUSED\n",
                    dwError );
        }
        else
        {
            Trace( "connect call succeeded, expected WSAEWOULDBLOCK\n" );
        }
        
        CloseSocket( sock );
        MyCloseHandle( hEvent );
        CleanupWinsock();
        Fail( "test failed\n" );
    }


    /* close the socket */
    err = closesocket( sock );
    if ( err != 0 )
    {
        Trace( "closesocket() call failed for accept socket\n" );
        MyCloseHandle( hEvent );
        CleanupWinsock();
        Fail( "test failed\n" );
    }


    /* close the handle */
    if( ! CloseHandle( hEvent ) )
    {
        dwError = GetLastError();
        Trace(  "CloseHandle() call failed with error code %lu\n",
                dwError );
        CleanupWinsock();
        Fail( "test failed\n" );
    }



    /* cleanup the winsock library */
    err = WSACleanup();
    if ( err != 0 )
    {
        /* check the socket-specific error code */    
        dwError = GetLastError();
        Fail(   "WSACleanup call failed with error code %d\n",
                dwError ); 
    }
    
    
    /* PAL termination */
    PAL_Terminate();
    return PASS; 
}