gpt4 book ai didi

sockets - 套接字 (SOCK_RAW + IPPROTO_ICMP) 无法读取 TTL 响应

转载 作者:行者123 更新时间:2023-12-03 12:03:54 29 4
gpt4 key购买 nike

我在 Windows 上创建了一个 ping 实用程序。我正在使用带有 ICMP 协议(protocol)的原始套接字。我是我电脑的本地管理员。

由于代码很多,我不想在这里粘贴,但我找到了一个与我的非常相似的示例 winsock2advancedrawsocket11b http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedrawsocket11b.html

我下载它,测试我得出结论它和我的问题相同。 TTL 过期时(在 IP header 中)我没有收到响应。我虽然使用 RAW 套接字让我读到了?

假设我想在 ping 上强制“ttl expired”,所以我向“google.com”发送一个 ttl 为 2 的 ping。

ping -i 2 -n 1 google.com

这给了我以下结果
Reply from 204.80.6.137: TTL expired in transit.
Reply from 204.80.6.137: TTL expired in transit.

使用我的应用程序,我发送相同的 ping 请求并查看我在 Wireshark 中收到的内容。我收到一个 ICMP 数据包发送给谷歌,另一个数据包来自我的路由器,告诉我 TTL 已过期。那么,为什么 Windows 上的原始套接字也没有收到此消息?即使 TTL 已过期,是否有强制读取 ip header 的选项?

所以我假设 Windows ping.exe实用程序过滤数据包比我们可以用 Winsock API 做的更好/不同?

作为引用,这是创建套接字的方式:
#include <winsock2.h> 
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <windows.h>
#include <stdint.h>
#include <vector>
#include <algorithm>

struct IPV4_HDR
{
unsigned char ip_header_len : 4;
unsigned char ip_version : 4;
unsigned char ip_tos;
unsigned short ip_total_length;
unsigned short ip_id;

unsigned char ip_frag_offset : 5;

unsigned char ip_more_fragment : 1;
unsigned char ip_dont_fragment : 1;
unsigned char ip_reserved_zero : 1;

unsigned char ip_frag_offset1;

unsigned char ip_ttl;
unsigned char ip_protocol;
unsigned short ip_checksum;
unsigned int ip_srcaddr;
unsigned int ip_destaddr;
};

struct ICMP_HDR
{
BYTE type;
BYTE code;
USHORT checksum;
USHORT id;
USHORT seq;
};

unsigned short compute_checksum(unsigned short* buffer, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(unsigned short);
}
if (size)
{
cksum += *(char*)buffer;
}

cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (unsigned short)(~cksum);
}

void send_receive_ping(SOCKET icmp_sock)
{
std::vector<char> receive_buffer;
receive_buffer.resize(65536);
std::fill(receive_buffer.begin(), receive_buffer.end(), 0);
char *Buffer = receive_buffer.data();

int recv_bytes = 0;
DWORD start_time = GetTickCount();
bool first_time_in_loop = true;
do
{
if ( (first_time_in_loop == true
|| GetTickCount() - start_time > 5000))
{
OutputDebugString(L"Sending an ICMP packet....\n");
send_icmp_packet(icmp_sock);

first_time_in_loop = false;
start_time = GetTickCount();
}

recv_bytes = recvfrom(icmp_sock, Buffer, 65536, 0, 0, 0);
if (recv_bytes > 0)
{
// Handle received packet
}
else
{
break;
}
} while (recv_bytes > 0);
}

void send_icmp_packet(SOCKET icmp_sock)
{
sockaddr_in sockaddr_in_dst = {};
sockaddr_in_dst.sin_family = AF_INET;
sockaddr_in_dst.sin_port = 0;
sockaddr_in_dst.sin_addr.s_addr = inet_addr("184.150.168.247"); // google.com

std::vector<char> send_buffer;
send_buffer.resize(sizeof(IPV4_HDR) + sizeof(ICMP_HDR));
std::fill(send_buffer.begin(), send_buffer.end(), 0);

IPV4_HDR *ipv4_header = (IPV4_HDR *)send_buffer.data();
ipv4_header->ip_header_len = 5;
ipv4_header->ip_version = 4;
ipv4_header->ip_tos = 16;
ipv4_header->ip_total_length = htons( send_buffer.size() );
ipv4_header->ip_id = htons(0);
ipv4_header->ip_ttl = 64;
//ipv4_header->ip_ttl = 2;
ipv4_header->ip_protocol = IPPROTO_ICMP;
ipv4_header->ip_srcaddr = dest.sin_addr.s_addr;
ipv4_header->ip_destaddr = sockaddr_in_dst.sin_addr.s_addr;
ipv4_header->ip_checksum = compute_checksum((unsigned short *)ipv4_header, sizeof(IPV4_HDR));

static unsigned short seq = 0;
ICMP_HDR *icmp_header = (ICMP_HDR *)(send_buffer.data() + sizeof(IPV4_HDR));
icmp_header->type = 8;
icmp_header->seq = seq++;
icmp_header->id = 888;
icmp_header->checksum = compute_checksum((unsigned short *)icmp_header, sizeof(ICMP_HDR));

ret = sendto(icmp_sock, (char *)send_buffer.data(), send_buffer.size(),
0, (sockaddr *)&sockaddr_in_dst, sizeof(sockaddr_in_dst));
}


int main()
{
WSADATA ws;
WSAStartup(MAKEWORD(2, 2), &ws);

SOCKET icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);

char hostname[256];
gethostname(hostname, sizeof(hostname));

hostent *local = gethostbyname(hostname);

sockaddr_in source;
memset(&source, 0, sizeof(source));
memcpy(&source.sin_addr.s_addr, local->h_addr_list[0], sizeof(source.sin_addr.s_addr));
source.sin_family = AF_INET;
source.sin_port = 0;

bind(icmp_sock, (sockaddr *)&source, sizeof(source));

int recv_all_opt = 1;
int ioctl_read = 0;
WSAIoctl(icmp_sock, SIO_RCVALL, &recv_all_opt, sizeof(recv_all_opt), 0, 0, (LPDWORD)&ioctl_read, 0, 0);

int ip_header_include = 1;
setsockopt(icmp_sock, IPPROTO_IP, IP_HDRINCL, (char *)&ip_header_include, sizeof(ip_header_include));

send_receive_ping(icmp_sock);

closesocket(icmp_sock);
WSACleanup();

return 0;
}

前面的代码似乎运行良好,但我仍然无法从 IP 消息中获取过期的 TLL。就像操作系统窃取了软件包一样。我敢打赌是因为我要求阅读 ICMP 消息,而 TLL 在 IP header 中。因此,如果 IP header 有问题,操作系统会丢弃该消息并且我的套接字无法读取它。所以我尝试使用套接字 IPPROTO_IP :
sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);

我仍然没有收到 TTL 过期消息,最糟糕的是我有时会丢失数据包。我在 Wireshark 中看到了它们,但我没有将它们放在我的 socket 上。有人知道为什么吗?

最佳答案

这似乎正是您所怀疑的,Windows 正在转移 TTL Exceeded 响应并且它们没有到达原始套接字。我遇到了同样的问题,到目前为止我找到的最好的解释是 this discussion of porting the MTR traceroute-style utility to Windows .

真正可悲的是,当我第一次开始在 Windows 环境中使用原始套接字时,它曾在 2012-2013 年在 Windows 7 上工作。几年过去了,突然之间,同一台机器上的相同代码不再收到 TTL Exceeded 消息。

根据您的应用程序,您可以通过直接调用 ICMP.DLL 来解决问题,如上面链接中所述。此代码 from the P2PScrapper project可能是一个很好的起点。

关于sockets - 套接字 (SOCK_RAW + IPPROTO_ICMP) 无法读取 TTL 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239862/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com