Sending packets

Send a hand-crafted packet to the network.

Docs: libpcap, javadoc.

public static void main(String[] _args)
    throws ErrorException, PermissionDeniedException, PromiscuousModePermissionDeniedException,
        TimestampPrecisionNotSupportedException, RadioFrequencyModeNotSupportedException,
        NoSuchDeviceException, ActivatedException, InterfaceNotUpException,
        InterfaceNotSupportTimestampTypeException {
  var service = Service.Creator.create("PcapService");
  try (var pcap = service.live(service.interfaces(), new DefaultLiveOptions())) {
    PacketBuffer buffer = pcap.allocate(PacketBuffer.class).capacity(14);
    buffer.writeBytes(
        new byte[] {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255});
    buffer.writeBytes(
        new byte[] {(byte) 222, (byte) 173, (byte) 190, (byte) 239, (byte) 192, (byte) 254});
    buffer.writeShortRE(0x0806);
    try {
      pcap.sendPacket(buffer);
    } catch (ErrorException e) {
      System.err.println(e.getMessage());
    }
    buffer.release();
  }
}

Last updated