123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #pragma once
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/ip_icmp.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <cstring>
- #include <fcntl.h>
- #include <cerrno>
- #define REQ_DATASIZE 32 // Echo请求数据的大小
- class CMyPingip
- {
- public:
- CMyPingip(void);
- ~CMyPingip(void);
- public:
- bool PingFunction(const char* pstrHost);
- int WaitForEchoReply(int s);
- // ICMP回应的请求和回答函数
- int SendEchoRequest(int sock, struct sockaddr_in* dest_addr);
- long RecvEchoReply(int sock, struct sockaddr_in* from_addr, u_char* pTTL);
- unsigned short in_cksum(unsigned short* addr, int len);
- // 获取毫秒级时间戳
- long GetTickCount();
- protected:
- int m_sockfd; // 套接字文件描述符
- };
- // 自定义IP头结构(Linux下已经定义,但为了兼容性保留)
- typedef struct IP_HDR
- {
- u_char VIHL; // Version and IHL
- u_char TOS; // Type Of Service
- short TotLen; // 总长度
- short ID; // 标识
- short FlagOff; // 标记
- u_char TTL; // 生命期
- u_char Protocol; // 协议
- u_short Checksum; // 检查和
- struct in_addr iaSrc; // 源地址
- struct in_addr iaDst; // 目的地址
- } IPHDR, * PIPHDR;
- // ICMP头结构
- typedef struct IC_MPHDR
- {
- u_char Type; // 类型
- u_char Code; // 编码
- u_short Checksum; // 检查和
- u_short ID; // 标识
- u_short Seq; // 顺序
- char Data; // 数据
- } ICMPHDR, * PICMPHDR;
- // ICMP回应请求结构
- typedef struct ICMPECHOREQUEST
- {
- ICMPHDR icmpHdr;
- long dwTime;
- char cData[REQ_DATASIZE];
- } ECHOREQUEST, * PECHOREQUEST;
- // ICMP回应结构
- typedef struct ICMPECHOREPLY
- {
- IPHDR iphdr;
- ECHOREQUEST echorequest;
- char cFiller[256];
- } ECHOREPLY, * PECHOREPLY;
|