解决方案 »

  1.   

    http://www.copathway.com/vchelp/zsrc/win_ping_src.zip
    图形界面的Ping工具
      

  2.   

    /****************************************************************************\
    *  ping.c -- sample program demonstrating NWLink.
    *
    *       Microsoft Developer Support
    *       Copyright (c) 1992-1997 Microsoft Corporation
    *
    *  This program is a simple example of opening a socket,
    *   binding to the socket, receiving a packet and sending
    *  that packet back to the original sender.

    ****************************************************************************/
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
    #include <wsipx.h>
    #include <wsnwlink.h>
    #include "../testlib/testlib.h"/*
    *   Sockaddr structures 
    */SOCKADDR_IPX addr;
    SOCKADDR_IPX baddr;
    SOCKADDR_IPX raddr;
    int addrlen;/*
    *   Function Prototypes 
    */extern int main(int, char **);
    extern int net_init(SOCKET *);
    extern int do_ping(SOCKET);/****************************************************************************
    *
    *    FUNCTION:  main( int argc, char **argv )
    *
    *    PURPOSE:   This is the main entry for the program
    *        
    *
    *    ARGUMENTS: argc = Number of arguments
    *               argv = Array of ptrs to cmd line args
    *                
    *
    *  RETURNS:   Exit code for the program
    *
    *\***************************************************************************/
    int main(int argc, char **argv)
    {
        SOCKET s;    /*
        *   Set our default values before calling parse_cmd_line 
        */    *Local_Socket_Number = 0x30;
        *(Local_Socket_Number+1) = 0x00;
        Receive_Length = 2048;    /*
        *   Get any command line options 
        */    parse_cmd_line(argc, argv);    /*
        *   Initialize the network and set up our socket 
        */    if (net_init(&s))
            return 1;    do_ping(s);    /*
        *   All done (We only get here on error) 
        */    if (verbose)
            printf("calling closesocket()");    closesocket(s);
        return 0;
    }/****************************************************************************
    *
    *    FUNCTION:  net_init( SOCKET *skt )
    *
    *    PURPOSE:   Initializes the WinSock stuff and sets up our socket.
    *        
    *
    *    ARGUMENTS: SOCKET * => struct to receive our socket info
    *
    *  RETURNS:   0 if ok
    * 1 if error
    *
    *\***************************************************************************/
    int net_init(SOCKET *skt)
    {
        int rc, addrlen;
        WSADATA wsdata;
        SOCKET s;
        WORD    wVersionRequested;    /*
        *   Initialize with the WINSOCK library 
        */    if (verbose)
            printf("calling WSAStartup(), ");    wVersionRequested = MAKEWORD(1,1);
        rc = WSAStartup(wVersionRequested, &wsdata);    if (verbose)
            printf("return = 0x%X (%d)\n", rc, rc);    if (rc) {
            printf("WSAStartup failed: error code = %d\n", rc);
            return 1;
        }    if (verbose) {
            printf("contents of wsdata struct: \n");
            print_wsa(&wsdata);
        }    if (verbose)
            printf("calling socket(address family = %d, socket type = %d, protocol = %d)\n", Local_Address_Family, Socket_Type, Protocol);    /*
        *   Open a DATAGRAM socket with IPX 
        */    s = socket(AF_NS, SOCK_DGRAM, NSPROTO_IPX);    if (verbose)
            printf("socket() returned 0x%lX\n", s);    if (s == INVALID_SOCKET) {
            dos_net_perror("Socket call failed");
            exit(1);
        }    /*
        *   Bind to a socket.  We want to bind to a well known
        *   socket so that the app. that sends us a packet will
        *   know where to send it.
        */    addr.sa_family = Local_Address_Family;    memcpy(&addr.sa_netnum, Local_Network_Number, 4);
        memcpy(&addr.sa_nodenum, Local_Node_Number, 6);
        memcpy(&addr.sa_socket, Local_Socket_Number, 2);    if (verbose) {
            printf("calling bind():\n  ");
            print_saddr(&addr);
        }    rc = bind(s, (const struct sockaddr *) &addr, 16);    if (verbose)
            printf("bind() returned 0x%X\n", rc);    if (rc == SOCKET_ERROR) {
            dos_net_perror("Error binding to socket");
            closesocket(s);
            return 1;
        }
        /*
        *   Set the packet type for this socket 
        */    if (verbose)
            printf("Calling setsockopt for packet type %d\n", Local_Packet_Type);    rc = setsockopt(s, SOL_SOCKET, IPX_PTYPE, (const char *) &Local_Packet_Type, 4);    if (rc == SOCKET_ERROR)
            dos_net_perror("setsockopt() call failed");
        /*
        *   Get the address we bound to and print it out 
        */    if (verbose)
            printf("Calling getsockname(socket = %d), ");    addrlen = 16;
        rc = getsockname(s, (struct sockaddr *) &baddr, &addrlen);    if (verbose)
            printf("return = 0x%X (%d)\n", rc, rc);    if (rc == SOCKET_ERROR) {
            dos_net_perror("Error getting socket name");
            closesocket(s);
            return 1;
        }    /*
        *   Print out the network address 
        */    if (verbose) {
            printf("addrlen = %d\n", addrlen);
            print_netaddr(baddr.sa_netnum, "Bound address = ", "\n");
        }    if (verbose)
            printf("Allocating %d byte receive buffer\n", Receive_Length);    /*
        *   Set up socket to send back 
        */    *skt = s;    return 0;
    }
      

  3.   

    http://www.ittide.com/document/source/netware/ping.zip
    一个支持PING协议的MFC类CPing
      

  4.   

    /****************************************************************************\
    *  ping.c -- sample program demonstrating NWLink.
    *
    *       Microsoft Developer Support
    *       Copyright (c) 1992-1997 Microsoft Corporation
    *
    *  This program is a simple example of opening a socket,
    *   binding to the socket, receiving a packet and sending
    *  that packet back to the original sender.

    ****************************************************************************/
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
    #include <wsipx.h>
    #include <wsnwlink.h>
    #include "../testlib/testlib.h"/*
    *   Sockaddr structures 
    */SOCKADDR_IPX addr;
    SOCKADDR_IPX baddr;
    SOCKADDR_IPX raddr;
    int addrlen;/*
    *   Function Prototypes 
    */extern int main(int, char **);
    extern int net_init(SOCKET *);
    extern int do_ping(SOCKET);/****************************************************************************
    *
    *    FUNCTION:  main( int argc, char **argv )
    *
    *    PURPOSE:   This is the main entry for the program
    *        
    *
    *    ARGUMENTS: argc = Number of arguments
    *               argv = Array of ptrs to cmd line args
    *                
    *
    *  RETURNS:   Exit code for the program
    *
    *\***************************************************************************/
    int main(int argc, char **argv)
    {
        SOCKET s;    /*
        *   Set our default values before calling parse_cmd_line 
        */    *Local_Socket_Number = 0x30;
        *(Local_Socket_Number+1) = 0x00;
        Receive_Length = 2048;    /*
        *   Get any command line options 
        */    parse_cmd_line(argc, argv);    /*
        *   Initialize the network and set up our socket 
        */    if (net_init(&s))
            return 1;    do_ping(s);    /*
        *   All done (We only get here on error) 
        */    if (verbose)
            printf("calling closesocket()");    closesocket(s);
        return 0;
    }/****************************************************************************
    *
    *    FUNCTION:  net_init( SOCKET *skt )
    *
    *    PURPOSE:   Initializes the WinSock stuff and sets up our socket.
    *        
    *
    *    ARGUMENTS: SOCKET * => struct to receive our socket info
    *
    *  RETURNS:   0 if ok
    * 1 if error
    *
    *\***************************************************************************/
    int net_init(SOCKET *skt)
    {
        int rc, addrlen;
        WSADATA wsdata;
        SOCKET s;
        WORD    wVersionRequested;    /*
        *   Initialize with the WINSOCK library 
        */    if (verbose)
            printf("calling WSAStartup(), ");    wVersionRequested = MAKEWORD(1,1);
        rc = WSAStartup(wVersionRequested, &wsdata);    if (verbose)
            printf("return = 0x%X (%d)\n", rc, rc);    if (rc) {
            printf("WSAStartup failed: error code = %d\n", rc);
            return 1;
        }    if (verbose) {
            printf("contents of wsdata struct: \n");
            print_wsa(&wsdata);
        }    if (verbose)
            printf("calling socket(address family = %d, socket type = %d, protocol = %d)\n", Local_Address_Family, Socket_Type, Protocol);    /*
        *   Open a DATAGRAM socket with IPX 
        */    s = socket(AF_NS, SOCK_DGRAM, NSPROTO_IPX);    if (verbose)
            printf("socket() returned 0x%lX\n", s);    if (s == INVALID_SOCKET) {
            dos_net_perror("Socket call failed");
            exit(1);
        }    /*
        *   Bind to a socket.  We want to bind to a well known
        *   socket so that the app. that sends us a packet will
        *   know where to send it.
        */    addr.sa_family = Local_Address_Family;    memcpy(&addr.sa_netnum, Local_Network_Number, 4);
        memcpy(&addr.sa_nodenum, Local_Node_Number, 6);
        memcpy(&addr.sa_socket, Local_Socket_Number, 2);    if (verbose) {
            printf("calling bind():\n  ");
            print_saddr(&addr);
        }    rc = bind(s, (const struct sockaddr *) &addr, 16);    if (verbose)
            printf("bind() returned 0x%X\n", rc);    if (rc == SOCKET_ERROR) {
            dos_net_perror("Error binding to socket");
            closesocket(s);
            return 1;
        }
        /*
        *   Set the packet type for this socket 
        */    if (verbose)
            printf("Calling setsockopt for packet type %d\n", Local_Packet_Type);    rc = setsockopt(s, SOL_SOCKET, IPX_PTYPE, (const char *) &Local_Packet_Type, 4);    if (rc == SOCKET_ERROR)
            dos_net_perror("setsockopt() call failed");
        /*
        *   Get the address we bound to and print it out 
        */    if (verbose)
            printf("Calling getsockname(socket = %d), ");    addrlen = 16;
        rc = getsockname(s, (struct sockaddr *) &baddr, &addrlen);    if (verbose)
            printf("return = 0x%X (%d)\n", rc, rc);    if (rc == SOCKET_ERROR) {
            dos_net_perror("Error getting socket name");
            closesocket(s);
            return 1;
        }    /*
        *   Print out the network address 
        */    if (verbose) {
            printf("addrlen = %d\n", addrlen);
            print_netaddr(baddr.sa_netnum, "Bound address = ", "\n");
        }    if (verbose)
            printf("Allocating %d byte receive buffer\n", Receive_Length);    /*
        *   Set up socket to send back 
        */    *skt = s;    return 0;
    }
      

  5.   

    http://www.ittide.com/document/source/netware/ping.zip
    一个支持PING协议的MFC类CPing
      

  6.   

    * PING.C
    /*
    /* ping source code distribute by cpu || digger.
    /* for unix family only. compil and link success in sco unix.
    /* i think linux no problem too. u can try it.
    /* before read this code, you shoud know about the principle of
    /* tcp/ip, especially icmp protocol, u also should also know some
    /* about BSD socket API, and unix system signal programming.
    /*
    /* cc -o ping ping.c -lsocket, then u will get executable file,
    /* but must act as root when cc it, and then set euid attribute
    /* for this ping, then u can execute it as common user.
    /* because only root can have authority to creat raw socket.
    /*
    /* i love socket, if so do u,
    /* call me, cpu == digger# include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include # define ICMP_ECHO 8 /* icmp echo requir */
    # define ICMP_ECHOREPLY 0 /* icmp echo reply */
    # define ICMP_HEADSIZE 8 /* icmp packet header size */
    # define IP_HEADSIZE 20 /* ip packet header size */typedef struct tagIpHead /* icmp packet header */
    {
    u_char ip_verlen; /* ip version and ip header lenth*/
    u_char ip_tos; /* ip type of service */
    u_short ip_len; /* ip packet lenghth */
    u_short ip_id; /* ip packet identification */
    u_short ip_fragoff; /* ip packet fragment and offset */
    u_char ip_ttl; /* ip packet time to live */
    u_char ip_proto; /* ip packet protocol type */
    u_short ip_chksum; /* ip packet header checksum */
    u_long ip_src_addr; /* ip source ip adress */
    u_long ip_dst_addr; /* ip destination ip adress */
    } IPHEAD;typedef struct tagIcmpHead /* icmp header */
    {
    u_char icmp_type; /* icmp service type */
    /* 8 echo require, 0 echo reply */
    u_char icmp_code; /* icmp header code */
    u_short icmp_chksum; /* icmp header chksum */
    u_short icmp_id; /* icmp packet identification */
    u_short icmp_seq; /* icmp packet sequent */
    u_char icmp_data[1]; /* icmp data, use as pointer */
    } ICMPHEAD;u_short ChkSum( u_short * pIcmpData, int iDataLen )
    /* for check sum of icmp header */
    {
    u_short iSum;
    u_short iOddByte;iSum = 0;while ( iDataLen > 1 ) { /* xor the next unsigned int data */
    iSum ^= *pIcmpData++;
    iDataLen -= 2;
    }if ( iDataLen == 1 ) { /* the rest odd byte */
    iOddByte = 0;
    *((u_char *)&iOddByte) = *(u_char *)pIcmpData;
    iSum ^= iOddByte;
    }iSum ^= 0xffff; /* xor 0xffff == not it */
    return(iSum);
    } long time_now() /* return time passed by */
    /* since 1970.1.1 00:00:00, */
    /* in 1/1000000 second */
    {
    struct timeval now;
    long lPassed;
    gettimeofday(&now, 0);
    lPassed = now.tv_sec * 1000000 + now.tv_usec;
    /* now.tv_sec in second */
    /* now.tv_usec in 1/1000000 second */
    return lPassed;
    }char* host; /* destination host */
    char* prog; /* program name */
    extern errno; /* system global parameter */
    long lSendTime; /* each time when send, change it */
    u_short seq; /* the icmp packet seqence */
    int iTimeOut; /* time out parameter */
    int sock, sent, recvd, max, min, total;
    /* sent : icmp packet already sent */
    /* recvd: proper icmp packet received */
    /* max, min: max min round trip time */
    /* total: total round trip time */
    /* store to calculate average */
    u_long lHostIp; /* host ip adress */
    struct sockaddr_in it; /* destination host information */int ping();
    void stat(); main(int argc, char** argv)
    {
    struct hostent* h;
    char buf[200];
    char dst_host[32];
    int i, namelen;
    IPHEAD* pIpHead;
    ICMPHEAD* pIcmpHead;if (argc < 2) { /* ping the destination host */
    /* every timeout second */
    /* default timeout is 1 second */printf("usage: %s [-timeout] host|IP\n", argv[0]);
    exit(0);
    }
    prog = argv[0];
    host = argc == 2 ? argv[1] : argv[2];
    iTimeOut = argc == 2 ? 1 : atoi(argv[1]);/* creat the raw socket for icmp */if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
    perror("socket");
    exit(2);
    }/* set destination host information */bzero(&it, sizeof(it));
    it.sin_family = AF_INET;/* check host format */if ( ( lHostIp = inet_addr(host) ) != INADDR_NONE ) {
    /* is available ip adress */
    it.sin_addr.s_addr = lHostIp;
    strcpy( dst_host, host );
    } else if ( h = gethostbyname(host) ) {
    /* is available host name */
    /* from hosts file of local host */
    /* or from DNS */ 
    bcopy(h->h_addr, &it.sin_addr, h->h_length);
    sprintf( dst_host, "%s (%s)", host,
    inet_ntoa(it.sin_addr) ); 
    } else {
    /* bad ip adress or host name */
    /* exit */
    fprintf( stderr, "bad IP or host\n" );
    exit(3);
    }
    namelen = sizeof(it);printf("\nDigger pinging %s, send %d bytes\n", 
    dst_host,
    IP_HEADSIZE + ICMP_HEADSIZE + sizeof(long)
    );seq = 0; /* first icmp_seq = 0 */
    sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */
    /* to statistic the result , and then exit */
    sigset(SIGALRM, ping); /* hook ping function to timer */
    alarm(iTimeOut); /* start timer, call ping every timeout */
    /* seconds */
    ping();
    for ( ;; ) { /* waiting for every echo back */
    /* icmp packet and check it */
    register size;
    register u_char ttl;
    register delta;
    register iIpHeadLen;/* block to received echo back datagram */size = recvfrom(sock, buf, sizeof(buf), 0, 
    (struct sockaddr *)&it, &namelen);
    if (size == -1 && errno == EINTR) {
    /* receive error or system call */
    /* interrupted */
    continue;
    }/* calculate the round trip time, */
    /* time when receive minus time when send */delta = (int)((time_now() - lSendTime)/1000);/* get echo back packet and check its ip header */pIpHead = (IPHEAD *)buf;/* get the ip packet lenth */
    /* if too small, not the icmp echoreply packet */
    /* give it up */iIpHeadLen = (int)((pIpHead->ip_verlen & 0x0f) << 2);
    if (size < iIpHeadLen + ICMP_HEADSIZE) {
    continue;
    }
    ttl = pIpHead->ip_ttl; /* time to live param *//* get the icmp header information */
    pIcmpHead = (ICMPHEAD *)(buf + iIpHeadLen);/* not icmp echo reply packet, give it up */
    if (pIcmpHead->icmp_type != ICMP_ECHOREPLY) {
    continue;
    }/* not proper icmp sequent number, give it up */
    if (pIcmpHead->icmp_id != seq || pIcmpHead->icmp_seq != seq) {
    continue;
    }/* print out result for each icmp */
    /* echo reply information */
    sprintf( buf, "icmp_seq=%u bytes=%d ttl=%d", 
    pIcmpHead->icmp_seq, size, ttl );
    fprintf(stderr, "reply from %s: %s time=%d ms\n",
    host, buf, delta);/* calculate some statistic information */
    /* max, min, average round trip time */
    /* received icmp echo reply packet numbers */
    max = MAX(delta, max);
    min = min ? MIN(delta, min) : delta;
    total += delta;
    ++ recvd;/* for next icmp sequence */++ seq;
    }
    }ping() 
    {
    char buf[200];
    int iPacketSize;/* make the icmp header information */ICMPHEAD *pIcmpHead = (ICMPHEAD *)buf;
    pIcmpHead->icmp_type = ICMP_ECHO;
    pIcmpHead->icmp_code = 0;
    pIcmpHead->icmp_id = seq;
    pIcmpHead->icmp_seq = seq;
    pIcmpHead->icmp_chksum = 0;/* store time information as icmp packet content, 4 bytes */
    /* u may store other information instead */*((long *)pIcmpHead->icmp_data) = time_now();iPacketSize = ICMP_HEADSIZE + 4; /* icmp packet length *//* icmp header check sum */pIcmpHead->icmp_chksum = ChkSum((u_short *)pIcmpHead,
    iPacketSize );/* remember the time when send for calculate round trip time */
    lSendTime = time_now();/* send the icmp packet to des host */
    if ( sendto(sock, buf, iPacketSize, 0, (struct sockaddr *)&it,
    sizeof(it) ) < 0) {
    perror("send failed");
    exit(6);
    }/* packet number been sent */
    ++sent;/* reset the timer hooker to me again */
    alarm(iTimeOut);
    }void stat() /* print the statistic information for this time's ping */
    {
    if (sent) {
    printf("\n----- %s ping statistics summerized by Digger-----\n"
    , host ); 
    printf("%d packets sent, %d packets received, %.2f%% lost\n",
    sent, recvd, (float)(sent-recvd)/(float)sent*100 );
    }
    if (recvd) {
    printf("round_trip min/avg/max: %d/%d/%d ms\n\n",
    min, total/recvd, max ); 
    }
    exit(0);
    }
      

  7.   

    在msdn里输入Ping sample就看到了
      

  8.   

    http://www.ittide.com/document/source/netware/ping.zip
    一个支持PING协议的MFC类CPing
      

  9.   

    * PING.C
    /*
    /* ping source code distribute by cpu || digger.
    /* for unix family only. compil and link success in sco unix.
    /* i think linux no problem too. u can try it.
    /* before read this code, you shoud know about the principle of
    /* tcp/ip, especially icmp protocol, u also should also know some
    /* about BSD socket API, and unix system signal programming.
    /*
    /* cc -o ping ping.c -lsocket, then u will get executable file,
    /* but must act as root when cc it, and then set euid attribute
    /* for this ping, then u can execute it as common user.
    /* because only root can have authority to creat raw socket.
    /*
    /* i love socket, if so do u,
    /* call me, cpu == digger# include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include # define ICMP_ECHO 8 /* icmp echo requir */
    # define ICMP_ECHOREPLY 0 /* icmp echo reply */
    # define ICMP_HEADSIZE 8 /* icmp packet header size */
    # define IP_HEADSIZE 20 /* ip packet header size */typedef struct tagIpHead /* icmp packet header */
    {
    u_char ip_verlen; /* ip version and ip header lenth*/
    u_char ip_tos; /* ip type of service */
    u_short ip_len; /* ip packet lenghth */
    u_short ip_id; /* ip packet identification */
    u_short ip_fragoff; /* ip packet fragment and offset */
    u_char ip_ttl; /* ip packet time to live */
    u_char ip_proto; /* ip packet protocol type */
    u_short ip_chksum; /* ip packet header checksum */
    u_long ip_src_addr; /* ip source ip adress */
    u_long ip_dst_addr; /* ip destination ip adress */
    } IPHEAD;typedef struct tagIcmpHead /* icmp header */
    {
    u_char icmp_type; /* icmp service type */
    /* 8 echo require, 0 echo reply */
    u_char icmp_code; /* icmp header code */
    u_short icmp_chksum; /* icmp header chksum */
    u_short icmp_id; /* icmp packet identification */
    u_short icmp_seq; /* icmp packet sequent */
    u_char icmp_data[1]; /* icmp data, use as pointer */
    } ICMPHEAD;u_short ChkSum( u_short * pIcmpData, int iDataLen )
    /* for check sum of icmp header */
    {
    u_short iSum;
    u_short iOddByte;iSum = 0;while ( iDataLen > 1 ) { /* xor the next unsigned int data */
    iSum ^= *pIcmpData++;
    iDataLen -= 2;
    }if ( iDataLen == 1 ) { /* the rest odd byte */
    iOddByte = 0;
    *((u_char *)&iOddByte) = *(u_char *)pIcmpData;
    iSum ^= iOddByte;
    }iSum ^= 0xffff; /* xor 0xffff == not it */
    return(iSum);
    } long time_now() /* return time passed by */
    /* since 1970.1.1 00:00:00, */
    /* in 1/1000000 second */
    {
    struct timeval now;
    long lPassed;
    gettimeofday(&now, 0);
    lPassed = now.tv_sec * 1000000 + now.tv_usec;
    /* now.tv_sec in second */
    /* now.tv_usec in 1/1000000 second */
    return lPassed;
    }char* host; /* destination host */
    char* prog; /* program name */
    extern errno; /* system global parameter */
    long lSendTime; /* each time when send, change it */
    u_short seq; /* the icmp packet seqence */
    int iTimeOut; /* time out parameter */
    int sock, sent, recvd, max, min, total;
    /* sent : icmp packet already sent */
    /* recvd: proper icmp packet received */
    /* max, min: max min round trip time */
    /* total: total round trip time */
    /* store to calculate average */
    u_long lHostIp; /* host ip adress */
    struct sockaddr_in it; /* destination host information */int ping();
    void stat(); main(int argc, char** argv)
    {
    struct hostent* h;
    char buf[200];
    char dst_host[32];
    int i, namelen;
    IPHEAD* pIpHead;
    ICMPHEAD* pIcmpHead;if (argc < 2) { /* ping the destination host */
    /* every timeout second */
    /* default timeout is 1 second */printf("usage: %s [-timeout] host|IP\n", argv[0]);
    exit(0);
    }
    prog = argv[0];
    host = argc == 2 ? argv[1] : argv[2];
    iTimeOut = argc == 2 ? 1 : atoi(argv[1]);/* creat the raw socket for icmp */if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
    perror("socket");
    exit(2);
    }/* set destination host information */bzero(&it, sizeof(it));
    it.sin_family = AF_INET;/* check host format */if ( ( lHostIp = inet_addr(host) ) != INADDR_NONE ) {
    /* is available ip adress */
    it.sin_addr.s_addr = lHostIp;
    strcpy( dst_host, host );
    } else if ( h = gethostbyname(host) ) {
    /* is available host name */
    /* from hosts file of local host */
    /* or from DNS */ 
    bcopy(h->h_addr, &it.sin_addr, h->h_length);
    sprintf( dst_host, "%s (%s)", host,
    inet_ntoa(it.sin_addr) ); 
    } else {
    /* bad ip adress or host name */
    /* exit */
    fprintf( stderr, "bad IP or host\n" );
    exit(3);
    }
    namelen = sizeof(it);printf("\nDigger pinging %s, send %d bytes\n", 
    dst_host,
    IP_HEADSIZE + ICMP_HEADSIZE + sizeof(long)
    );seq = 0; /* first icmp_seq = 0 */
    sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */
    /* to statistic the result , and then exit */
    sigset(SIGALRM, ping); /* hook ping function to timer */
    alarm(iTimeOut); /* start timer, call ping every timeout */
    /* seconds */
    ping();
    for ( ;; ) { /* waiting for every echo back */
    /* icmp packet and check it */
    register size;
    register u_char ttl;
    register delta;
    register iIpHeadLen;/* block to received echo back datagram */size = recvfrom(sock, buf, sizeof(buf), 0, 
    (struct sockaddr *)&it, &namelen);
    if (size == -1 && errno == EINTR) {
    /* receive error or system call */
    /* interrupted */
    continue;
    }/* calculate the round trip time, */
    /* time when receive minus time when send */delta = (int)((time_now() - lSendTime)/1000);/* get echo back packet and check its ip header */pIpHead = (IPHEAD *)buf;/* get the ip packet lenth */
    /* if too small, not the icmp echoreply packet */
    /* give it up */iIpHeadLen = (int)((pIpHead->ip_verlen & 0x0f) << 2);
    if (size < iIpHeadLen + ICMP_HEADSIZE) {
    continue;
    }
    ttl = pIpHead->ip_ttl; /* time to live param *//* get the icmp header information */
    pIcmpHead = (ICMPHEAD *)(buf + iIpHeadLen);/* not icmp echo reply packet, give it up */
    if (pIcmpHead->icmp_type != ICMP_ECHOREPLY) {
    continue;
    }/* not proper icmp sequent number, give it up */
    if (pIcmpHead->icmp_id != seq || pIcmpHead->icmp_seq != seq) {
    continue;
    }/* print out result for each icmp */
    /* echo reply information */
    sprintf( buf, "icmp_seq=%u bytes=%d ttl=%d", 
    pIcmpHead->icmp_seq, size, ttl );
    fprintf(stderr, "reply from %s: %s time=%d ms\n",
    host, buf, delta);/* calculate some statistic information */
    /* max, min, average round trip time */
    /* received icmp echo reply packet numbers */
    max = MAX(delta, max);
    min = min ? MIN(delta, min) : delta;
    total += delta;
    ++ recvd;/* for next icmp sequence */++ seq;
    }
    }ping() 
    {
    char buf[200];
    int iPacketSize;/* make the icmp header information */ICMPHEAD *pIcmpHead = (ICMPHEAD *)buf;
    pIcmpHead->icmp_type = ICMP_ECHO;
    pIcmpHead->icmp_code = 0;
    pIcmpHead->icmp_id = seq;
    pIcmpHead->icmp_seq = seq;
    pIcmpHead->icmp_chksum = 0;/* store time information as icmp packet content, 4 bytes */
    /* u may store other information instead */*((long *)pIcmpHead->icmp_data) = time_now();iPacketSize = ICMP_HEADSIZE + 4; /* icmp packet length *//* icmp header check sum */pIcmpHead->icmp_chksum = ChkSum((u_short *)pIcmpHead,
    iPacketSize );/* remember the time when send for calculate round trip time */
    lSendTime = time_now();/* send the icmp packet to des host */
    if ( sendto(sock, buf, iPacketSize, 0, (struct sockaddr *)&it,
    sizeof(it) ) < 0) {
    perror("send failed");
    exit(6);
    }/* packet number been sent */
    ++sent;/* reset the timer hooker to me again */
    alarm(iTimeOut);
    }void stat() /* print the statistic information for this time's ping */
    {
    if (sent) {
    printf("\n----- %s ping statistics summerized by Digger-----\n"
    , host ); 
    printf("%d packets sent, %d packets received, %.2f%% lost\n",
    sent, recvd, (float)(sent-recvd)/(float)sent*100 );
    }
    if (recvd) {
    printf("round_trip min/avg/max: %d/%d/%d ms\n\n",
    min, total/recvd, max ); 
    }
    exit(0);
    }
      

  10.   

    http://www.gamehigh.net/document/netdocs/docs/ping_src.htm
      

  11.   

    * PING.C
    /*
    /* ping source code distribute by cpu || digger.
    /* for unix family only. compil and link success in sco unix.
    /* i think linux no problem too. u can try it.
    /* before read this code, you shoud know about the principle of
    /* tcp/ip, especially icmp protocol, u also should also know some
    /* about BSD socket API, and unix system signal programming.
    /*
    /* cc -o ping ping.c -lsocket, then u will get executable file,
    /* but must act as root when cc it, and then set euid attribute
    /* for this ping, then u can execute it as common user.
    /* because only root can have authority to creat raw socket.
    /*
    /* i love socket, if so do u,
    /* call me, cpu == digger# include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include 
    # include # define ICMP_ECHO 8 /* icmp echo requir */
    # define ICMP_ECHOREPLY 0 /* icmp echo reply */
    # define ICMP_HEADSIZE 8 /* icmp packet header size */
    # define IP_HEADSIZE 20 /* ip packet header size */typedef struct tagIpHead /* icmp packet header */
    {
    u_char ip_verlen; /* ip version and ip header lenth*/
    u_char ip_tos; /* ip type of service */
    u_short ip_len; /* ip packet lenghth */
    u_short ip_id; /* ip packet identification */
    u_short ip_fragoff; /* ip packet fragment and offset */
    u_char ip_ttl; /* ip packet time to live */
    u_char ip_proto; /* ip packet protocol type */
    u_short ip_chksum; /* ip packet header checksum */
    u_long ip_src_addr; /* ip source ip adress */
    u_long ip_dst_addr; /* ip destination ip adress */
    } IPHEAD;typedef struct tagIcmpHead /* icmp header */
    {
    u_char icmp_type; /* icmp service type */
    /* 8 echo require, 0 echo reply */
    u_char icmp_code; /* icmp header code */
    u_short icmp_chksum; /* icmp header chksum */
    u_short icmp_id; /* icmp packet identification */
    u_short icmp_seq; /* icmp packet sequent */
    u_char icmp_data[1]; /* icmp data, use as pointer */
    } ICMPHEAD;u_short ChkSum( u_short * pIcmpData, int iDataLen )
    /* for check sum of icmp header */
    {
    u_short iSum;
    u_short iOddByte;iSum = 0;while ( iDataLen > 1 ) { /* xor the next unsigned int data */
    iSum ^= *pIcmpData++;
    iDataLen -= 2;
    }if ( iDataLen == 1 ) { /* the rest odd byte */
    iOddByte = 0;
    *((u_char *)&iOddByte) = *(u_char *)pIcmpData;
    iSum ^= iOddByte;
    }iSum ^= 0xffff; /* xor 0xffff == not it */
    return(iSum);
    } long time_now() /* return time passed by */
    /* since 1970.1.1 00:00:00, */
    /* in 1/1000000 second */
    {
    struct timeval now;
    long lPassed;
    gettimeofday(&now, 0);
    lPassed = now.tv_sec * 1000000 + now.tv_usec;
    /* now.tv_sec in second */
    /* now.tv_usec in 1/1000000 second */
    return lPassed;
    }char* host; /* destination host */
    char* prog; /* program name */
    extern errno; /* system global parameter */
    long lSendTime; /* each time when send, change it */
    u_short seq; /* the icmp packet seqence */
    int iTimeOut; /* time out parameter */
    int sock, sent, recvd, max, min, total;
    /* sent : icmp packet already sent */
    /* recvd: proper icmp packet received */
    /* max, min: max min round trip time */
    /* total: total round trip time */
    /* store to calculate average */
    u_long lHostIp; /* host ip adress */
    struct sockaddr_in it; /* destination host information */int ping();
    void stat(); main(int argc, char** argv)
    {
    struct hostent* h;
    char buf[200];
    char dst_host[32];
    int i, namelen;
    IPHEAD* pIpHead;
    ICMPHEAD* pIcmpHead;if (argc < 2) { /* ping the destination host */
    /* every timeout second */
    /* default timeout is 1 second */printf("usage: %s [-timeout] host|IP\n", argv[0]);
    exit(0);
    }
    prog = argv[0];
    host = argc == 2 ? argv[1] : argv[2];
    iTimeOut = argc == 2 ? 1 : atoi(argv[1]);/* creat the raw socket for icmp */if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
    perror("socket");
    exit(2);
    }/* set destination host information */bzero(&it, sizeof(it));
    it.sin_family = AF_INET;/* check host format */if ( ( lHostIp = inet_addr(host) ) != INADDR_NONE ) {
    /* is available ip adress */
    it.sin_addr.s_addr = lHostIp;
    strcpy( dst_host, host );
    } else if ( h = gethostbyname(host) ) {
    /* is available host name */
    /* from hosts file of local host */
    /* or from DNS */ 
    bcopy(h->h_addr, &it.sin_addr, h->h_length);
    sprintf( dst_host, "%s (%s)", host,
    inet_ntoa(it.sin_addr) ); 
    } else {
    /* bad ip adress or host name */
    /* exit */
    fprintf( stderr, "bad IP or host\n" );
    exit(3);
    }
    namelen = sizeof(it);printf("\nDigger pinging %s, send %d bytes\n", 
    dst_host,
    IP_HEADSIZE + ICMP_HEADSIZE + sizeof(long)
    );seq = 0; /* first icmp_seq = 0 */
    sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */
    /* to statistic the result , and then exit */
    sigset(SIGALRM, ping); /* hook ping function to timer */
    alarm(iTimeOut); /* start timer, call ping every timeout */
    /* seconds */
    ping();
    for ( ;; ) { /* waiting for every echo back */
    /* icmp packet and check it */
    register size;
    register u_char ttl;
    register delta;
    register iIpHeadLen;/* block to received echo back datagram */size = recvfrom(sock, buf, sizeof(buf), 0, 
    (struct sockaddr *)&it, &namelen);
    if (size == -1 && errno == EINTR) {
    /* receive error or system call */
    /* interrupted */
    continue;
    }/* calculate the round trip time, */
    /* time when receive minus time when send */delta = (int)((time_now() - lSendTime)/1000);/* get echo back packet and check its ip header */pIpHead = (IPHEAD *)buf;/* get the ip packet lenth */
    /* if too small, not the icmp echoreply packet */
    /* give it up */iIpHeadLen = (int)((pIpHead->ip_verlen & 0x0f) << 2);
    if (size < iIpHeadLen + ICMP_HEADSIZE) {
    continue;
    }
    ttl = pIpHead->ip_ttl; /* time to live param *//* get the icmp header information */
    pIcmpHead = (ICMPHEAD *)(buf + iIpHeadLen);/* not icmp echo reply packet, give it up */
    if (pIcmpHead->icmp_type != ICMP_ECHOREPLY) {
    continue;
    }/* not proper icmp sequent number, give it up */
    if (pIcmpHead->icmp_id != seq || pIcmpHead->icmp_seq != seq) {
    continue;
    }/* print out result for each icmp */
    /* echo reply information */
    sprintf( buf, "icmp_seq=%u bytes=%d ttl=%d", 
    pIcmpHead->icmp_seq, size, ttl );
    fprintf(stderr, "reply from %s: %s time=%d ms\n",
    host, buf, delta);/* calculate some statistic information */
    /* max, min, average round trip time */
    /* received icmp echo reply packet numbers */
    max = MAX(delta, max);
    min = min ? MIN(delta, min) : delta;
    total += delta;
    ++ recvd;/* for next icmp sequence */++ seq;
    }
    }ping() 
    {
    char buf[200];
    int iPacketSize;/* make the icmp header information */ICMPHEAD *pIcmpHead = (ICMPHEAD *)buf;
    pIcmpHead->icmp_type = ICMP_ECHO;
    pIcmpHead->icmp_code = 0;
    pIcmpHead->icmp_id = seq;
    pIcmpHead->icmp_seq = seq;
    pIcmpHead->icmp_chksum = 0;/* store time information as icmp packet content, 4 bytes */
    /* u may store other information instead */*((long *)pIcmpHead->icmp_data) = time_now();iPacketSize = ICMP_HEADSIZE + 4; /* icmp packet length *//* icmp header check sum */pIcmpHead->icmp_chksum = ChkSum((u_short *)pIcmpHead,
    iPacketSize );/* remember the time when send for calculate round trip time */
    lSendTime = time_now();/* send the icmp packet to des host */
    if ( sendto(sock, buf, iPacketSize, 0, (struct sockaddr *)&it,
    sizeof(it) ) < 0) {
    perror("send failed");
    exit(6);
    }/* packet number been sent */
    ++sent;/* reset the timer hooker to me again */
    alarm(iTimeOut);
    }void stat() /* print the statistic information for this time's ping */
    {
    if (sent) {
    printf("\n----- %s ping statistics summerized by Digger-----\n"
    , host ); 
    printf("%d packets sent, %d packets received, %.2f%% lost\n",
    sent, recvd, (float)(sent-recvd)/(float)sent*100 );
    }
    if (recvd) {
    printf("round_trip min/avg/max: %d/%d/%d ms\n\n",
    min, total/recvd, max ); 
    }
    exit(0);
    }