Jnetpcap 官方实例(5)- 发送数据包

来源:互联网 发布:神泣数据库修改 编辑:程序博客网 时间:2024/05/23 15:47

发送一个数据包

这个例子使用网络接口一次发送一个数据包,并且需要从数据链路层开始创建整个的packet,包括IP层,应用层,都创建

package org.jnetpcap.examples;  import java.nio.ByteBuffer;  import java.util.ArrayList;  import java.util.Arrays;  import java.util.List;  import org.jnetpcap.Pcap;  import org.jnetpcap.PcapIf;  public class PcapSendPacketExample {    public static void main(String[] args) {      List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs      StringBuilder errbuf = new StringBuilder(); // For any error msgs      /***************************************************************************      * 获取设备列表     **************************************************************************/      int r = Pcap.findAllDevs(alldevs, errbuf);      if (r == Pcap.NOT_OK || alldevs.isEmpty()) {        System.err.printf("Can't read list of devices, error is %s", errbuf.toString());        return;      }      PcapIf device = alldevs.get(0); // We know we have atleast 1 device      /*****************************************      * 打开网络接口     *****************************************/      int snaplen = 64 * 1024; // Capture all packets, no trucation      int flags = Pcap.MODE_PROMISCUOUS; // capture all packets      int timeout = 10 * 1000; // 10 seconds in millis      Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);      /*******************************************************      * 创建一个未加工的packet     *******************************************************/      byte[] a = new byte[14];      Arrays.fill(a, (byte) 0xff);      ByteBuffer b = ByteBuffer.wrap(a);      /*******************************************************      * 再一个open状态接口发送这个packet     *******************************************************/      if (pcap.sendPacket(b) != Pcap.OK) {        System.err.println(pcap.getErr());      }      /********************************************************      * 最后关闭     ********************************************************/      pcap.close();    }  }

此时如果用wireshark对发送报文的接口进行监听,是会发现这个报文的

发送数据包队列

package org.jnetpcap.examples;  import java.util.ArrayList;  import java.util.Arrays;  import java.util.List;  import org.jnetpcap.Pcap;  import org.jnetpcap.PcapIf;  import org.jnetpcap.PcapPktHdr;  import org.jnetpcap.winpcap.WinPcap;  import org.jnetpcap.winpcap.WinPcapSendQueue;  public class WinPcapSendQueueTransmitExample {    public static void main(String[] args) {      List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs      StringBuilder errbuf = new StringBuilder(); // For any error msgs      /***************************************************************************      * 首先获取设备列表     **************************************************************************/      int r = Pcap.findAllDevs(alldevs, errbuf);      if (r == Pcap.NOT_OK || alldevs.isEmpty()) {        System.err.printf("Can't read list of devices, error is %s", errbuf.toString());        return;      }      PcapIf device = alldevs.get(0); // We know we have atleast 1 device      /***************************************************************************      * 打开一个接口     **************************************************************************/      int snaplen = 64 * 1024; // Capture all packets, no trucation      int flags = Pcap.MODE_PROMISCUOUS; // capture all packets      int timeout = 10 * 1000; // 10 seconds in millis      WinPcap pcap = WinPcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);      /***************************************************************************      * 创建一个数据包队列     **************************************************************************/      WinPcapSendQueue queue = WinPcap.sendQueueAlloc(512);      PcapPktHdr hdr = new PcapPktHdr(128, 128);      byte[] pkt = new byte[128];      Arrays.fill(pkt, (byte) 255); // Broadcast      queue.queue(hdr, pkt); // Packet #1      queue.queue(hdr, pkt); // Packet #2      Arrays.fill(pkt, (byte) 0x11); // Junk packet      queue.queue(hdr, pkt); // Packet #3      /***************************************************************************      * 发送我们的数据包队列      **************************************************************************/      r = pcap.sendQueueTransmit(queue, WinPcap.TRANSMIT_SYNCH_ASAP);      if (r != queue.getLen()) {        System.err.println(pcap.getErr());        return;      }      /***************************************************************************      * 最后关闭     **************************************************************************/      pcap.close();    }  } 
0 0