首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >套接字(SOCK_RAW + IPPROTO_ICMP)无法读取TTL响应

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

Stack Overflow用户
提问于 2017-04-06 03:19:46
回答 2查看 2.9K关注 0票数 1

我已经在Windows上创建了一个ping实用程序。我使用的是带有ICMP协议的原始套接字。我是我的计算机的本地管理员。

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

我下载它,测试我得出结论,它有和我一样的问题。当TTL过期(在IP报头中)时,我没有收到响应。我以为使用原始套接字可以让我读到它?

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

代码语言:javascript
复制
ping -i 2 -n 1 google.com

这给了我以下结果

代码语言:javascript
复制
Reply from 204.80.6.137: TTL expired in transit.
Reply from 204.80.6.137: TTL expired in transit.

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

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

作为参考,这是如何创建套接字的:

代码语言:javascript
复制
#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报头中。因此,如果IP报头有问题,那么操作系统将丢弃该消息,并且我的套接字无法读取它。因此,我尝试使用套接字IPPROTO_IP:

代码语言:javascript
复制
sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);

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

EN

回答 2

Stack Overflow用户

发布于 2019-05-20 00:58:12

这看起来就像你所怀疑的那样,Windows正在转移超出TTL的响应,并且它们不会到达原始套接字。我也遇到了同样的问题,到目前为止,我找到的最好的解释是this discussion of porting the MTR traceroute-style utility to Windows

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

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

票数 2
EN

Stack Overflow用户

发布于 2020-04-11 14:04:10

你应该在windows防火墙中简单地允许ICMP。创建入站规则,在协议中选择ICMP并设置'allow‘。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43239862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档