文档章节

ping的源代码

rise-worlds
 rise-worlds
发布于 2016/06/20 13:38
字数 1153
阅读 11
收藏 0
// Module Name: Ping.c
//
// Code by Rise
//
// Command Line Options/Parameters:
//        Ping [host] [packet-size]
//
//        host        String name of host to ping
//        packet-size    Integer size of packet to send (smaller than 1024 bytes)
//
//#pragma pack(1)

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

#define IP_RECORD_ROUTE 0x7
//
// IP header structure
//
typedef struct _iphdr
{
    unsigned int    h_len:4;        // Length of the header
    unsigned int    version:4;        // Version of IP
    unsigned char     tos;            // Type of service
    unsigned short    total_len;        // Total length of the packet
    unsigned short    ident;            // Unique identifier
    unsigned short    frag_and_flags;    // Flags
    unsigned short    ttl;            // Time to live
    unsigned short    proto;            // Protocol (TCP, UDP, etc.)
    unsigned short    checksum;        // IP checksum
    
    unsigned int        sourceIP;
    unsigned int        destIP;
}IpHeader;

#define ICMP_ECHO        8
#define ICMP_ECHOREPLY    0
#define ICMP_MIN        8    // Minimun 8-byte ICMP packet (header)

//
// ICMP header structure
//
typedef struct _icmphdr
{
    BYTE        i_type;
    BYTE        i_code;        // Type sub code
    USHORT    i_cksum;
    USHORT    i_id;
    USHORT    i_seq;
    // This is not the standard header, but we reserve space for time
    ULONG timestamp;
}IcmpHeader;

//
// IP option header -- use with socket option IP_OPTIONS
//
typedef struct _ipoptionhdr
{
    unsigned char    code;        // Option type
    unsigned char    len;        // Length of option hdr
    unsigned char    prt;        // Offset into options
    unsigned long    addr[9];    // List of IP addrs
}IpOptionHeader;
#define DEF_PACKET_SIZE 32        // Default packet size
#define MAX_PACKET        1024    // Max ICMP packet size
#define MAX_IP_HDR_SIZE    60        // Max IP header size w/options

BOOL    bRecordRoute;
int        datasize;
char*    lpdest;

//
// Function: usage
//
// Description:
//        Print usage information
//
void usage(char* progname)
{
    printf("usage: ping -r <host> [data size]\n");
    printf("    -r        record route\n");
    printf("    host    remote machine to Ping\n");
    printf("    datasize    can be up to 1 KB\n");
    ExitProcess(-1);
}

//
// Function: FillICMPData
//
// Description:
//        Helper function to fill in various fields for our ICMP request
//
void FillICMPData(char* icmp_data, int datasize)
{
    IcmpHeader*    icmp_hdr = NULL;
    char*        datapart = NULL;
    
    icmp_hdr = (IcmpHeader*)icmp_data;
    icmp_hdr->i_type = ICMP_ECHO;        // Request an ICMP echo
    icmp_hdr->i_code = 0;
    icmp_hdr->i_id = (USHORT)GetCurrentProcessId();
    icmp_hdr->i_cksum = 0;
    icmp_hdr->i_seq = 0;
    
    datapart = icmp_data + sizeof(IcmpHeader);
    //
    // Place some junk in the buffer
    //
    memset(datapart, 'E', datasize - sizeof(IcmpHeader));
}

//
// Function: checksum
//
// Description:
//        This funcion calculates the 16-bit one's complement sum
//        of the supplied buffer (ICMP)header
//
USHORT checksum(USHORT* buffer, int size)
{
    unsigned long cksum = 0 ;
    
    while(size > 1)
    {
        cksum += *buffer++;
        size -= sizeof(USHORT);
    }
    if(size)
    {
        cksum += *(UCHAR*)buffer;
    }
    cksum = (cksum>>16)+(cksum&0xffff);
    cksum += (cksum>>16);
    return (USHORT)(~cksum);
}

//
// Function: DecodeIPOptions
//
// Description:
//        If the IP option header is present. find the IP options
//        within the IP header and print the record route option
//        values
//
void DecodeIPOptions(char*buf,int bytes)
{
    IpOptionHeader* ipopt = NULL;
    IN_ADDR    inaddr;
    int     i;
    HOSTENT *host = NULL;
    
    ipopt = (IpOptionHeader*)(buf + 20);
    
    printf("RR:    ");
    for(i=0;i<(ipopt->prt/4)-1;i++)
    {
        inaddr.S_un.S_addr = ipopt->addr[i];
        if(i != 0 )
            printf("    ");
        host = gethostbyaddr((char*)&inaddr.S_un.S_addr, sizeof(inaddr.S_un.S_addr), AF_INET);
        if(host)
            printf("(%-15s) %s\n", inet_ntoa(inaddr), host->h_name);
        else
            printf("(%-15s)\n", inet_ntoa(inaddr));
    }
    return;
}

//
// Function: DecodeICMPHeader
//
// Description:
//        The response is an IP packet. We must decode the IP header to locate the ICMP data.
//
void DecodeICMPHeader(char* buf, int bytes, struct sockaddr_in *from)
{
    IpHeader*    iphdr = NULL;
    IcmpHeader*    icmphdr = NULL;
    unsigned short iphdrlen;
    DWORD        tick;
    static int    icmpcount = 0;
    
    iphdr = (IpHeader*)buf;
    // Number of 32-bit words * 4 = bytes
    iphdrlen = iphdr->h_len * 4;
    tick = GetTickCount();
    
    if((iphdrlen == MAX_IP_HDR_SIZE)&&(!icmpcount))
        DecodeIPOptions(buf,bytes);
    if(bytes < iphdrlen + ICMP_MIN)
    {
        printf("Too Few bytes from %s\n",inet_ntoa(from->sin_addr));
    }
    icmphdr = (IcmpHeader*)(buf + iphdrlen);
    
    if(icmphdr->i_type != ICMP_ECHOREPLY)
    {
        printf("nonecho type %d recvd\n", icmphdr->i_type);
        return;
    }
    // Make sure this is an ICMP reply to something we sent!
    //
    if(icmphdr->i_id != (USHORT)GetCurrentProcessId())
    {
        printf("someone else's packet!\n");
        return;
    }
    printf("%d bytes from %s:", bytes, inet_ntoa(from->sin_addr));
    printf(" icmp->seq = %d.", icmphdr->i_seq);
    printf(" time: %d ms", tick-icmphdr->timestamp);
    printf("\n");
    
    icmpcount++;
    return;
}

void ValidateArgs(int argc, char **argv)
{
    int i;
    bRecordRoute = FALSE;
    lpdest = NULL;
    datasize = DEF_PACKET_SIZE;
    
    for(i = 1; i < argc; i++)
    {
        if((argv[i][0] == '-')||(argv[i][0] == '/'))
        {
            switch(tolower(argv[i][1]))
            {
                case 'r':        // Record route option
                    bRecordRoute = TRUE;
                    break;
                default:
                    usage(argv[0]);
                    break;
            }
        }
        else if(isdigit(argv[i][0]))
            datasize = atoi(argv[i]);
        else
            lpdest = argv[i];
    }
}

//
// Function: main
//
// Description
//        Set up the ICMP raw socket, and create the ICMP header. Add
//        the appropriate IP ooption header, and start sending ICMP
//        echo requests to the endpoint. For each send and receive.
//        we set a timeout value so that we don't wait forever for a
//        response in case the endpoint is not responding. When we
//        receive a packet. decode it.
//
int main(int argc, char** argv)
{
    WSADATA    wsaData;
    SOCKET    sockRaw = INVALID_SOCKET;
    struct sockaddr_in dest, from;
    int        bread, fromlen = sizeof(from), timeout = 1000, ret;
    char    *icmp_data = NULL, *recvbuf = NULL;
    unsigned int addr = 0;
    USHORT    seq_no = 0;
    struct hostent *hp = NULL;
    IpOptionHeader ipopt;
    
    if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("WSAStartup() failed: %d\n", GetLastError());
        return -1;
    }
    ValidateArgs(argc,argv);
    sockRaw = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, WSA_FLAG_OVERLAPPED);
    if(sockRaw == INVALID_SOCKET)
    {
        printf("WSASocket() failed: %d\n", WSAGetLastError());
        return -1;
    }
    if(bRecordRoute)
    {
        // Setup the IP option header to go out on every ICMP packet
        //
        ZeroMemory(&ipopt, sizeof(ipopt));
        ipopt.code = IP_RECORD_ROUTE;    // Record route option
        ipopt.prt  = 4;                    // Point to the first addr offset
        ipopt.len  = 39;                // Length of option header
        
        ret = setsockopt(sockRaw, IPPROTO_IP, IP_OPTIONS, (char*)&ipopt, sizeof(ipopt));
        if(ret == SOCKET_ERROR)
        {
            printf("setsockopt(IP_OPTIONs) failed: %d\n",WSAGetLastError());
        }
    }
    // Set the send/recv timeout values
    //
    bread = setsockopt(sockRaw, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
    if(bread == SOCKET_ERROR)
    {
        printf("setsockopt(SO_RCVTIME0)failed: %d\n", WSAGetLastError());
        return -1;
    }
    timeout = 1000;
    bread = setsockopt(sockRaw, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
    if(bread == SOCKET_ERROR)
    {
        printf("setsockopt(SO_SNDTIME0) failed: %d\n", WSAGetLastError());
        return -1;
    }
    memset(&dest, 0, sizeof(dest));
    //
    // Resolve the endpoint`s name if necessary
    //
    dest.sin_family = AF_INET;
    if((dest.sin_addr.s_addr = inet_addr(lpdest)) == INADDR_NONE)
    {
        if((hp = gethostbyname(lpdest)) != NULL)
        {
            memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);
            dest.sin_family = hp->h_addrtype;
            printf("dest.sin_addr = %s\n", inet_ntoa(dest.sin_addr));
        }
        else
        {
            printf("gethostbyname() failed: %d\n",WSAGetLastError());
            return -1;
        }
    }
    
    //
    // Create the ICMP packet
    //
    datasize += sizeof(IcmpHeader);
    icmp_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PACKET);
    recvbuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PACKET);
    if(!icmp_data)
    {
        printf("HeapAlloc() failed: %d\n",GetLastError());
        return -1;
    }
    memset(icmp_data, 0, MAX_PACKET);
    FillICMPData(icmp_data, datasize);
    //
    // Start sending/receiving ICMP packets
    //
    while(1)
    {
        static int nCount = 0;
        int bwrote;
        
        if(nCount++ ==4)
            break;
        ((IcmpHeader*)icmp_data)->i_cksum = 0;
        ((IcmpHeader*)icmp_data)->timestamp = GetTickCount();
        ((IcmpHeader*)icmp_data)->i_seq = seq_no++;
        ((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data, datasize, 0, (struct sockaddr*)&dest, sizeof(dest));
        
        bwrote = sendto(sockRaw, icmp_data, datasize, 0, (struct sockaddr*)&dest, sizeof(dest));
        if(bwrote == SOCKET_ERROR)
        {
            if(WSAGetLastError() == WSAETIMEDOUT)
            {
                printf("timed out\n");
                continue;
            }
            printf("recvfrom() failed: %d\n", WSAGetLastError());
            return -1;
        }
        if(bwrote < datasize)
        {
            printf("Wrote %d bytes\n", bwrote);
        }
        bread = recvfrom(sockRaw, recvbuf, MAX_PACKET, 0, (struct sockaddr*)&from, &fromlen);
        if(bread == SOCKET_ERROR)
        {
            if(WSAGetLastError() == WSAETIMEDOUT)
            {
                printf("timed out\n");
                continue;
            }
            printf("recvfrom() failed: %d\n", WSAGetLastError());
        }
        DecodeICMPHeader(recvbuf, bread, &from);
        
        Sleep(1000);
    }
    
    //Cleanup
    //
    if(sockRaw != INVALID_SOCKET)
        closesocket(sockRaw);
    HeapFree(GetProcessHeap(), 0, recvbuf);
    HeapFree(GetProcessHeap(), 0, icmp_data);
    
    WSACleanup();
    return 0;
}









cl ping.c /lib Ws2_32.lib

本文转载自:http://www.cnblogs.com/flying_bat/archive/2006/03/12/348250.html

rise-worlds

rise-worlds

粉丝 2
博文 1755
码字总数 0
作品 0
深圳
程序员
私信 提问
快速查看Linux系统工具的源代码

Ubuntu/Debian: which ping dpkg -S /bin/ping apt-get source iputils-ping 同样,你可以自己到软件源去下载: Ubuntu: http://archive.ubuntu.com/ubuntu/pool/main/i/iputils/ 包括二进制程......

eechen
2014/03/08
2.1K
4
zabbix监控系统fping配置

使用zabbix监控服务器系统是否正常存活,当前网络的连接情况,延时以及丢包率,可以使用监控模板中的icmpping,icmppingsec,icmppingloss 1)需要用到fping的支持,系统本身是没有这个工具的,...

zhangdiandong
2013/12/24
0
0
绕过CDN查看网站真实IP的一些办法

验证是否存在CDN最简单的办法 验证IP和域名是否真实对应最简单的办法 ping 分站域名 国外访问 MX及邮件 xss 找phpinfo()之类的探针 DOS 社会工程学 查看历史 DNS社工库 cloudflare 全网扫描 ...

老鹰a
2017/09/13
0
0
kafka开发环境搭建

@FrankHui 你好,想跟你请教个问题:kafka开发环境搭建时,在windows下运行java项目,报:Unable to connect to zookeeper server within timeout: 6000,在windows下ping liunx地址是可以p...

yang009ww
2013/03/08
7.7K
5
分布式系统中的 SWIM 成员协议

我们假设你要建立一个类似于Cassandra的分布式数据库。你所使用的存储系统将在许多商业服务器上存储和处理大量的数据。换句话来说,也就是你所使用的存储系统要管理好这些数据需要运行在100...

oschina
2015/02/16
2.4K
0

没有更多内容

加载失败,请刷新页面

加载更多

计算机实现原理专题--二进制减法器(二)

在计算机实现原理专题--二进制减法器(一)中说明了基本原理,现准备说明如何来实现。 首先第一步255-b运算相当于对b进行按位取反,因此可将8个非门组成如下图的形式: 由于每次做减法时,我...

FAT_mt
今天
4
0
好程序员大数据学习路线分享函数+map映射+元祖

好程序员大数据学习路线分享函数+map映射+元祖,大数据各个平台上的语言实现 hadoop 由java实现,2003年至今,三大块:数据处理,数据存储,数据计算 存储: hbase --> 数据成表 处理: hive --> 数...

好程序员官方
今天
7
0
tabel 中含有复选框的列 数据理解

1、el-ui中实现某一列为复选框 实现多选非常简单: 手动添加一个el-table-column,设type属性为selction即可; 2、@selection-change事件:选项发生勾选状态变化时触发该事件 <el-table @sel...

everthing
今天
6
0
【技术分享】TestFlight测试的流程文档

上架基本需求资料 1、苹果开发者账号(如还没账号先申请-苹果开发者账号申请教程) 2、开发好的APP 通过本篇教程,可以学习到ios证书申请和打包ipa上传到appstoreconnect.apple.com进行TestF...

qtb999
今天
10
0
再见 Spring Boot 1.X,Spring Boot 2.X 走向舞台中心

2019年8月6日,Spring 官方在其博客宣布,Spring Boot 1.x 停止维护,Spring Boot 1.x 生命周期正式结束。 其实早在2018年7月30号,Spring 官方就已经在博客进行过预告,Spring Boot 1.X 将维...

Java技术剑
今天
18
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部