C++ PING

阿萨三大 阿萨三大 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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));
}
}

这是什么情况???
...全文
评论 2
收藏 已收藏
分享
返回顶部