我正在使用C語言的原始套接字。 我需要發送並接收原始以太網數據包。 分組應該與IEEE 802.3標頭開始:RAW Socket - 以太網類型和接收的算法 - C
MAC DST [0-5] - MAC SRC [6-11] - ETH TYPE [12-13]
捕捉使用Wireshark我的報文見以下結構:
MAC DST [0-5] - MAC SRC [6-11] - LENGTH [12-13] - TRAILER [14-58] -....
這是我的代碼:
...
sraw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_802_3));
...
retVal = setsockopt(sraw, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
...
val = 3;
retVal = setsockopt(sraw, SOL_SOCKET, SO_PRIORITY, &val, sizeof (val));
...
memcpy(ptr_eth_header->DstMac, dst_mac, 6);
memcpy(ptr_eth_header->SrcMac, src_mac, 6);
ptr_eth_header->Type = htons(ETH_P_802_3);
memcpy(buffer + ETHHDR_SIZE, data, 60);
...
sockaddr.sll_family = htons(PF_PACKET);
sockaddr.sll_protocol = htons(ETH_P_802_3);
sockaddr.sll_ifindex = ifr.ifr_ifru.ifru_ivalue;
sockaddr.sll_halen = 6;
memcpy(&(sockaddr.sll_addr), dst_mac, 6);
...
bytes = sendto(sraw, buffer, sizeof(buffer), 0, (struct sockaddr *) &(sockaddr), sizeof (struct sockaddr_ll));
它只是一個wireshark的「問題」?有任何想法嗎?
我的第二個問題是收到原始消息。 該進程卡在recvfrom上。
這是我的代碼:
sraw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_802_3));
...
retVal = setsockopt(sraw, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
...
val = 3;
retVal = setsockopt(sraw, SOL_SOCKET, SO_PRIORITY, &val, sizeof (val));
...
val = CLIENT_PACKET_SIZE;
retVal = setsockopt(sraw, SOL_SOCKET, SO_RCVBUF, &val, sizeof (val));
sockaddr.sll_family = htons(PF_PACKET);
sockaddr.sll_ifindex = ifr.ifr_ifindex;
sockaddr.sll_protocol = htons(ETH_P_802_3);
buffer = malloc(CLIENT_PACKET_SIZE * sizeof(char));
while (count < PACKET_COUNT) {
bytes = recvfrom(sraw, buffer, CLIENT_PACKET_SIZE, 0, (struct sockaddr *)&sockaddr, (socklen_t*)sizeof(sockaddr));
...
}
你能幫助我嗎?
在此先感謝!
您的標題結構是否包裝?什麼是sizeof(buffer)'? – 2012-03-12 16:43:28
是的。 typedef struct {char dstMac [6]; charr SrcMac [6]; short int類型; } HEADER;' 'sizeof(buffer)'是64. – Simone 2012-03-12 16:47:58
您是否可以驗證NIC使用Wireshark發送/接收數據包時是否獨立於您的程序? – Ioan 2012-03-12 18:51:56