#include
#include"ping.h"
#include
CInitSock initSock; // 链接初始化 winsock 库
USHORT checksum(USHORT* buff, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buff++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buff;
}
// 将cksum的高16位和低16位相加,取反后得到校验和
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
int main()
{
char szDestIp[] = "180.97.33.107";
SOCKET sRaw = ::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
SOCKADDR_IN dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(0);
dest.sin_addr.S_un.S_addr = inet_addr(szDestIp);
ECHOREQUEST echoReq;
echoReq.icmphdr.icmp_type = 8;
echoReq.icmphdr.icmp_code = 0;
echoReq.icmphdr.icmp_id = (USHORT)GetCurrentProcessId();
echoReq.icmphdr.icmp_checksum = 0;
echoReq.icmphdr.icmp_sequence = 0;
memset(&echoReq.cData, 'E', 32);
USHORT nSeq = 0;
SOCKADDR_IN from;
int nLen = sizeof(from);
while (TRUE)
{
static int nCount = 0;
int nRet;
if (nCount++ == 4) break;
echoReq.icmphdr.icmp_checksum = 0;
echoReq.icmphdr.icmp_timestamp = GetTickCount();
echoReq.icmphdr.icmp_sequence = nSeq++;
echoReq.icmphdr.icmp_checksum =
checksum((USHORT*)&echoReq, sizeof(echoReq));
nRet = sendto(sRaw, (char*)&echoReq, sizeof(echoReq), 0,
(SOCKADDR*)&dest, sizeof(dest));
if (nRet == SOCKET_ERROR)
{
printf("sendto() failed:%d\n", WSAGetLastError());
return -1;
}
ECHOREPLY echoReply;
nRet = recvfrom(sRaw, (char*)&echoReply, sizeof(ECHOREPLY),
0, (sockaddr*)&from, &nLen);
if (nRet == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAETIMEDOUT)
{
printf("time out\n");
continue;
}
printf("recvfrom() failed:%d\n", WSAGetLastError());
return -1;
}
if (nRet < sizeof(ECHOREPLY))
{
printf("Too few bytes from %s \n", inet_ntoa(from.sin_addr));
}
if (echoReply.echoRequest.icmphdr.icmp_type != 0)
{
printf("nonecho type %d recvd\n",
echoReply.echoRequest.icmphdr.icmp_type);
return -1;
}
if (echoReply.echoRequest.icmphdr.icmp_id != GetCurrentProcessId())
{
printf("someone else's packet!\n");
return -1;
}
printf("%d bytes Reply from %s", nRet,
inet_ntoa(from.sin_addr));
printf("icmp_seq = %d.",
echoReply.echoRequest.icmphdr.icmp_timestamp);
printf("TTL=%d", echoReply.iphdr.ipTTL);
printf("\n");
Sleep(1000);
}
return 0;
}
#include"../../common/initsock.h"
typedef struct icmp_hdr
{
unsigned char icmp_type;
unsigned char icmp_code;
unsigned short icmp_checksum;
unsigned short icmp_id;
unsigned short icmp_sequence;
unsigned long icmp_timestamp;
}ICMP_HDR, *PICMP_HDR;
typedef struct _IPHeader
{
UCHAR iphVerLen; //版本号和头长度
UCHAR ipTOS; //服务类型
USHORT ipLength; //整个IP报文段的长度
USHORT ipID; //封包标识
USHORT ipFlag; //标识和片偏移
UCHAR ipTTL; //生存时间
UCHAR ipProtocol; //协议
USHORT ipChecksum; //校验和
ULONG ipSource; //源IP地址
ULONG ipDestination;//目标IP地址
}IPHeader,*PIPHeader;
typedef struct _EchoRequest{
ICMP_HDR icmphdr;
char cData[32];
}ECHOREQUEST,*PECHOREQUEST;
#define REQ_DATASIZE 32
typedef struct _EchoReply
{
IPHeader iphdr;
ECHOREQUEST echoRequest;
}ECHOREPLY, *PECHOREPLY;
USHORT checksum(USHORT* buff, int size);
这是头文件
使用原始套接字需要管理员权限,用管理员权限运行exe 就好了