1

我已经编写了这个简单的代码来使用 RAW 套接字接收带有 IP 和 UDP 标头的数据包。它适用于我的测试应用程序,它通过 RAW 套接字发送 UDP 数据包。现在我用 pcap 文件测试我的代码。我使用了 Ostineto 实用程序并创建了一个简单的 IP/UDP 数据包。在我的情况下,我通过 eth1 发送它。此数据包中的源和目标 MAC 以及 IP 地址是相同的。Wireshark 可以捕捉到这一点,一切看起来都很好。但我的 RAW 套接字永远不会收到。这确实阻碍了我的进一步发展。请帮忙。

顺便说一句,我正在 Ubuntu 12.04 上对此进行测试。

这是我的代码

int main(void)
{
    int s, bytesrecvd;
    struct sockaddr_in saddr;
    char packet[100];
    char *payload;

    if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) {
    perror("error:");
    exit(EXIT_FAILURE);
    }


    memset(packet, 0, sizeof(packet));
    int fromlen = sizeof(saddr);

    while(1) {
        bytesrecvd = recvfrom(s, (char *)&packet, sizeof(packet), 0,
                             (struct sockaddr *)&saddr, (socklen_t *)&fromlen);
        if(bytesrecvd < 0) {
            perror("packet receive error:");
        }
        printf("bytes received %d\n",bytesrecvd);

        parse_ip_header(packet, bytesrecvd);
        parse_udp_header(packet, bytesrecvd);

        payload = packet + sizeof(struct iphdr) + sizeof (struct udphdr);
        printf("payload: %s\n", payload);
    }

    return 0;
}

Stackoverflow 不允许我发布图片,否则我将分享 ostinato 的屏幕截图和数据包内容以供审核。

这是 pcap 的文本

Frame 1: 60 bytes on wire (480 bits), 60 bytes captured (480 bits)
WTAP_ENCAP: 1
Arrival Time: Dec 31, 1969 18:00:00.000000000 CST
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 0.000000000 seconds
[Time delta from previous captured frame: 0.000000000 seconds]
[Time delta from previous displayed frame: 0.000000000 seconds]
[Time since reference or first frame: 0.000000000 seconds]
Frame Number: 1
Frame Length: 60 bytes (480 bits)
Capture Length: 60 bytes (480 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: eth:ip:udp:data]
Ethernet II, Src: HewlettP_72:2e:08 (ac:16:2d:72:2e:08), Dst: HewlettP_72:2e:08     (ac:16:2d:72:2e:08)
Destination: HewlettP_72:2e:08 (ac:16:2d:72:2e:08)
    Address: HewlettP_72:2e:08 (ac:16:2d:72:2e:08)
    .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
    .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Source: HewlettP_72:2e:08 (ac:16:2d:72:2e:08)
    Address: HewlettP_72:2e:08 (ac:16:2d:72:2e:08)
    .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
    .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Type: IP (0x0800)
Internet Protocol Version 4, Src: 10.1.15.47 (10.1.15.47), Dst: 10.1.15.47 (10.1.15.47)
Version: 4
Header length: 20 bytes
Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-  Capable Transport))
    0000 00.. = Differentiated Services Codepoint: Default (0x00)
    .... ..00 = Explicit Congestion Notification: Not-ECT (Not ECN-Capable Transport)   (0x00)
Total Length: 46
Identification: 0x04d2 (1234)
Flags: 0x00
    0... .... = Reserved bit: Not set
    .0.. .... = Don't fragment: Not set
    ..0. .... = More fragments: Not set
Fragment offset: 0
Time to live: 127
Protocol: UDP (17)
Header checksum: 0x048e [correct]
    [Good: True]
    [Bad: False]
Source: 10.1.15.47 (10.1.15.47)
Destination: 10.1.15.47 (10.1.15.47)

用户数据报协议,Src 端口:0 (0),Dst 端口:0 (0) 源端口:0 (0) 目标端口:0 (0) 长度:26 校验和:0x0000(无)[Good Checksum: False]

4

0 回答 0