我已经将这个简单的代码编写成接收到的IP&数据包。使用RAW套接字的UDP标头。它适用于我的测试应用程序,它通过RAW套接字发送UDP数据包。现在我用pcap文件测试我的代码。我使用Ostineto实用程序并创建了一个简单的IP / UDP数据包。在我的情况下,我将它发送到eth1。源和目标MAC以及IP地址在此数据包中是相同的。 Wireshark可以捕获这一切,一切看起来都很好。但我的RAW套接字永远不会收到。这实际上阻碍了我的进一步发展。请帮忙。
BTW,我在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(无) [好的校验和:错误]