I'm trying to send ethernet packet to choosed destination MAC address using jPcap:

public void sendPacket(Packet packet, byte[] srcMac, byte[] dstMac, Interface i) throws IOException 
{
  JpcapSender sender = JpcapSender.openDevice(i.netInterface);
  EthernetPacket ether = new EthernetPacket();
  ether.frametype = EthernetPacket.ETHERTYPE_IP;
  ether.src_mac = srcMac;  // MAC address of selected interface
  ether.dst_mac = dstMac;  // MAC addr. choosed somwhere on form
  packet.datalink = ether;

  sender.sendPacket(packet);
  sender.close();
}

It works, but it's always sent to the selected interface not to the dst_mac!

So I don't understand the relation between selected interface and scr_mac:

  • why I have to choose both (interface and scr_mac)?
  • why I have to add dst_mac even if it's not used?
  • how to send packet out of my computer then?
有帮助吗?

解决方案

why I have to choose both (interface and scr_mac)?

The interface is what the software is using to communicate (to send or receive packets). This is usually your ethernet card. You need to specify it so that Jpcap knows how to send the information. The src_mac address is part of the packet header. It is intended to be used dynamically so that as the packets are being sent they are updated with the appropriate information. The src_mac does not necessarily play a role in how the packet is sent.

why I have to add dst_mac even if it's not used?

It is used. Make sure that you have the other device with the specified mac address linked to your source by a direct ethernet connection, and also make sure that it is ready to receive the data. Right now, what I suspect is happening, is you're trying to read back through your same interface on the host computer.

Jpcap's website has some tutorials and samples I found useful. I've worked quite a bit with the Jpcap library, and I would be happy to help you if you have any more questions.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top