简体   繁体   English

jPcap-将数据包发送到选定的MAC(不是选定的接口)

[英]jPcap - send packet to selected MAC (not the selected interface)

I'm trying to send ethernet packet to choosed destination MAC address using jPcap : 我正在尝试使用jPcap将以太网数据包发送 jPcap 目标MAC地址

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 ! 它可以工作,但是总是发送到选定的接口,而不是dst_mac

So I don't understand the relation between selected interface and scr_mac : 所以我不明白所选接口和scr_mac之间的关系:

  • why I have to choose both ( interface and scr_mac )? 为什么我必须同时选择两者( interfacescr_mac )?
  • why I have to add dst_mac even if it's not used? 为什么即使不使用它也必须添加dst_mac
  • how to send packet out of my computer then? 那么如何将数据包发送出我的计算机呢?

why I have to choose both (interface and scr_mac)? 为什么我必须同时选择两者(接口和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. 您需要指定它,以便Jpcap知道如何发送信息。 The src_mac address is part of the packet header. src_mac地址是数据包头的一部分。 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. src_mac不一定在数据包发送方式中起作用。

why I have to add dst_mac even if it's not used? 为什么即使不使用它也必须添加dst_mac?

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. 确保具有通过直接以太网连接链接到源的具有指定mac地址的另一台设备,并确保已准备好接收数据。 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. Jpcap的网站上有一些我认为有用的教程和示例。 I've worked quite a bit with the Jpcap library, and I would be happy to help you if you have any more questions. 我已经在Jpcap库上做了很多工作,如果您还有其他问题,我很乐意为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM