C++ 如何在收到的包中获取有关ICMP的信息?
我正在写一个发送和接收包裹的程序。我在通过ICMP协议发送数据方面没有问题,但在获取主机IP或ICMP代码等信息方面存在问题 我通过我的程序发送代码为8(“回送请求”)(正常工作),我的计算机接收代码0(“回送回复”)或代码11(“超出时间”)。我在Wireshark登记了 我不知道如何在收到的包中获取有关ICMP的信息。我计划的一部分:C++ 如何在收到的包中获取有关ICMP的信息?,c++,sockets,icmp,C++,Sockets,Icmp,我正在写一个发送和接收包裹的程序。我在通过ICMP协议发送数据方面没有问题,但在获取主机IP或ICMP代码等信息方面存在问题 我通过我的程序发送代码为8(“回送请求”)(正常工作),我的计算机接收代码0(“回送回复”)或代码11(“超出时间”)。我在Wireshark登记了 我不知道如何在收到的包中获取有关ICMP的信息。我计划的一部分: socklen_t addrlen = sizeof(connection); if (recvfrom(sockfd, buffer, sizeof(str
socklen_t addrlen = sizeof(connection);
if (recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen) == -1) {
perror("recv");
} else {
ip_reply = (struct iphdr*) buffer;
printf("ID: %d\n", ntohs(ip_reply->id));
printf("TTL: %d\n", ip_reply->ttl);
}
我想知道有关接收主机的IP和ICMP代码的信息
我知道在“iphdr”结构中有名为“saddr”和“daddr”的字段。但也有'u be32'类型。我不知道如何将其转换为“char*”
提前感谢:)#包括
...
...
...
你的记录
...
//首先,验证ip数据包是否包含ICMP
如果(ip_应答->协议==1)
{
//好的,它包含ICMP数据
printf(“从IP接收到ICMP消息:%s\n”,inet_ntoa(connection.sin_addr));
//使icmphdr结构指向右侧偏移量
结构icmphdr*icmp=(结构icmphdr*)((uint32_t*)ip_回复+ip_回复->ihl);
//打印你想要的
printf(“ICMP类型:%u\n”,ICMP->type);
printf(“ICMP代码:%u\n”,ICMP->code);
}
#包括
...
...
...
你的记录
...
//首先,验证ip数据包是否包含ICMP
如果(ip_应答->协议==1)
{
//好的,它包含ICMP数据
printf(“从IP接收到ICMP消息:%s\n”,inet_ntoa(connection.sin_addr));
//使icmphdr结构指向右侧偏移量
结构icmphdr*icmp=(结构icmphdr*)((uint32_t*)ip_回复+ip_回复->ihl);
//打印你想要的
printf(“ICMP类型:%u\n”,ICMP->type);
printf(“ICMP代码:%u\n”,ICMP->code);
}
#include <netinet/ip_icmp.h>
...
...
...
your recvfrom
...
// first, verify the ip packet contains a ICMP
if (ip_reply->protocol == 1)
{
// ok, it contains ICMP data
printf("Received ICMP message from IP: %s\n", inet_ntoa(connection.sin_addr));
// make the icmphdr struct point to the right offset
struct icmphdr *icmp = (struct icmphdr *)((uint32_t *)ip_reply + ip_reply->ihl);
// print what you want
printf("ICMP type: %u\n", icmp->type);
printf("ICMP code: %u\n", icmp->code);
}