Java原始以太网库

4

我正在寻找一个Java库,可以让我读取和发送原始以太网帧。我的最终目标是创建一个BACnet以太网网络扫描器。

请注意,我不需要TCP/IP。

有没有人知道一个好的库来完成这个任务?

1个回答

6
也许Jpcap可以帮助。请注意,有一个同名的Sourceforge项目,但似乎不是同一个项目。
这里是一些示例代码(来自该库的教程),使用Jpcap发送TCP数据包和以太网帧:
编辑:示例代码确实创建了一个TCPPacket,但您也可以创建一个常规的Packet
//open a network interface to send a packet to
JpcapSender sender=JpcapSender.openDevice(devices[index]);

//create a TCP packet with specified port numbers, flags, and other parameters
TCPPacket p=new TCPPacket(12,34,56,78,false,false,false,false,true,true,true,true,10,10);

//specify IPv4 header parameters
p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,1010101,100,IPPacket.IPPROTO_TCP,
  InetAddress.getByName("www.microsoft.com"),InetAddress.getByName("www.google.com"));

//set the data field of the packet
p.data=("data").getBytes();

//create an Ethernet packet (frame)
EthernetPacket ether=new EthernetPacket();
//set frame type as IP
ether.frametype=EthernetPacket.ETHERTYPE_IP;
//set source and destination MAC addresses
ether.src_mac=new byte[]{(byte)0,(byte)1,(byte)2,(byte)3,(byte)4,(byte)5};
ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10};

//set the datalink frame of the packet p as ether
p.datalink=ether;

//send the packet p
sender.sendPacket(p);

sender.close();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接