#pragma once
#include "stdafx.h"
#include <WS2tcpip.h>
#include <WinSock2.h>
#pragma comment(lib, "WS2_32")
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;
USHORT checksum(USHORT* buff, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buff++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buff;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
int main(int argc, char *argv[])
{
char buff[sizeof(ICMP_HDR) + 32];
ICMP_HDR* pIcmp = (ICMP_HDR*)buff;
pIcmp->icmp_type = 8;
pIcmp->icmp_code = 0;
pIcmp->icmp_id = (unsigned short)GetCurrentProcessId();
pIcmp->icmp_checksum = 0;
pIcmp->icmp_sequence = 0;
memset(&buff[sizeof(ICMP_HDR)], 'A', 32);
struct sockaddr_in destAddr;
struct sockaddr_in fromAddr;
char recvBuff[1024];
int addr_len = sizeof(struct sockaddr_in);
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(3456);
destAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
SOCKET sRaw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
while (pIcmp->icmp_sequence++ < 4)
{
int Ret;
pIcmp->icmp_checksum = 0;
pIcmp->icmp_timestamp = GetTickCount();
pIcmp->icmp_checksum = checksum((USHORT*)buff, sizeof(ICMP_HDR) + 32);
Ret = sendto(sRaw, buff, sizeof(ICMP_HDR) + 32, 0, (struct sockaddr *)&destAddr, sizeof(destAddr));;
printf("%d\n", &Ret);
closesocket(sRaw);
memset(recvBuff, '\0', 1024);
Ret = recvfrom(sRaw, recvBuff, sizeof(recvBuff), 0, (struct sockaddr *)&fromAddr, &addr_len);
printf("%d\n", &Ret);
printf("%s\n", inet_ntoa(fromAddr.sin_addr));
}
}
这是什么情况???