1

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?
gaffcz
  • 3,469
  • 14
  • 68
  • 108

1 Answers1

1

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.

Rob Wagner
  • 4,391
  • 15
  • 24
  • Thank you for your answer. That's how I understand it. My situation: i have two instances of my jPcap application started. I'm always choosing the interface. At first instance, I'm trying to send packet to dst_mac (interface on the same computer). Second instance is sniffering packets. It doesn't matter which dstMac is selected, I receive packet only when the selected interfaces are the same in both instances. (I have about 6 interfaces on my computer) – gaffcz Jun 19 '12 at 13:24
  • The MAC address and the interface are not the same thing. A MAC address represents the machines address within the network. The interface is the tool being used to communicate. You are only ever going to be able to send and receive on the same computer through one interface, because it is the only active interface. I bet that if you hooked up another computer, via ethernet link (good testing any way) you would see different results. I think you need to try and get a better fundamental grasp on what an interface is, and what a MAC address represents. – Rob Wagner Jun 19 '12 at 13:29
  • It sounds logical:-) I've supposed some behaviour based on the fact, that each interface has its own MAC address, but you're right, I should look on fundamentals a bit deeply :-) Anyway, I'll try it in network tomorrow.. – gaffcz Jun 19 '12 at 14:07
  • You're right. It works as I xpected in network :-)) Thank you! – gaffcz Jun 20 '12 at 06:25