我正在编写两个程序来发送和接收原始以太网数据包。我可以发送数据包并且可以接收,但是我的接收器有两个问题。
第一:所有的packtes都是重复的,这意味着所有在comming packet中都显示了两次。它仅与 2 个虚拟网络接口上的连接有关。(例如 src tap0 mac 00:00:8F:00:00:02 -> dest tap1 mac 00:00:8F:00:00:03)
第二:网络中的所有数据包都是从我的程序接收的,但它应该从特定接口接收数据包。
信息:我在 VirtualBox 中使用 Ubuntu 12.04.01 LTS
这是我的接收器代码:
/*read while first char not * */
while(frame.data[0] != '*'){
length_in_byte = recvfrom(sock_desc, buffer, ETH_FRAME_LEN,0,NULL,NULL);
if(length_in_byte == -1){
error handling
}
frame.dest_mac[0] = buffer[0];
frame.dest_mac[1] = buffer[1];
frame.dest_mac[2] = buffer[2];
frame.dest_mac[3] = buffer[3];
frame.dest_mac[4] = buffer[4];
frame.dest_mac[5] = buffer[5];
frame.src_mac[0] = buffer[6];
frame.src_mac[1] = buffer[7];
frame.src_mac[2] = buffer[8];
frame.src_mac[3] = buffer[9];
frame.src_mac[4] = buffer[10];
frame.src_mac[5] = buffer[11];
for(dataRun = 14; dataRun < length_in_byte; dataRun++){
frame.data[dataRun-14] = buffer[dataRun];
}
/*print struct*/
printf("\n");
printf("src: %02x:%02x:%02x:%02x:%02x:%02x\t->\tdest: %02x:%02x:%02x:%02x:%02x:%02x\n",frame.src_mac[0],frame.src_mac[1],frame.src_mac[2],frame.src_mac[3],frame.src_mac[4],frame.src_mac[5],frame.dest_mac[0],frame.dest_mac[1],frame.dest_mac[2],frame.dest_mac[3],frame.dest_mac[4],frame.dest_mac[5]);
printf("Data: %s\n", frame.data);
}
终端输出:发送数据=测试
src: 00:00:8f:00:00:03 -> dest: 00:00:8f:00:00:02
Data: test
src: 00:00:8f:00:00:03 -> dest: 00:00:8f:00:00:02
Data: test
发送数据=一个
src: 00:00:8f:00:00:03 -> dest: 00:00:8f:00:00:02
Data: a
src: 00:00:8f:00:00:03 -> dest: 00:00:8f:00:00:02
Data: a
src: 00:00:8f:00:00:03 -> dest: 33:33:00:00:00:fb
Data: `
src: 00:00:8f:00:00:03 -> dest: 33:33:00:00:00:fb
Data: `