diff --git a/src/main/java/com/softwaredesign/Main.java b/src/main/java/com/softwaredesign/Main.java index 8533908..b524e08 100644 --- a/src/main/java/com/softwaredesign/Main.java +++ b/src/main/java/com/softwaredesign/Main.java @@ -1,11 +1,10 @@ package com.softwaredesign; - import org.pcap4j.core.NotOpenException; +import org.pcap4j.core.PcapHandle; import org.pcap4j.core.PcapNativeException; -import org.pcap4j.core.PcapNetworkInterface; -import java.util.List; +import java.io.IOException; public class Main { public static void main(String[] args) { diff --git a/src/main/java/com/softwaredesign/NetworkInterfaceHandler.java b/src/main/java/com/softwaredesign/NetworkInterfaceHandler.java index 501db33..7ca852f 100644 --- a/src/main/java/com/softwaredesign/NetworkInterfaceHandler.java +++ b/src/main/java/com/softwaredesign/NetworkInterfaceHandler.java @@ -37,7 +37,7 @@ public List getAllDevices() throws IOException { * @param maxPackets: Maximum amount of packets to capture from the network interface * @throws PcapNativeException */ - public void listenForPacketsOnDevice(PcapNetworkInterface device, int snapshotLength, int readTimeout, int maxPackets) throws PcapNativeException { + public PcapHandle listenForPacketsOnDevice(PcapNetworkInterface device, int snapshotLength, int readTimeout, int maxPackets) throws PcapNativeException, NotOpenException { final PcapHandle handle = device.openLive(snapshotLength, PromiscuousMode.PROMISCUOUS, readTimeout); packets = new ArrayList<>(); @@ -56,7 +56,7 @@ public void gotPacket(Packet packet) { e.printStackTrace(); } - handle.close(); + return handle; } /** @@ -64,11 +64,11 @@ public void gotPacket(Packet packet) { * @param device * @throws PcapNativeException */ - public void listenForPacketsOnDevice(PcapNetworkInterface device) throws PcapNativeException { + public PcapHandle listenForPacketsOnDevice(PcapNetworkInterface device) throws PcapNativeException, NotOpenException { int snapshotLength = 65536; // in bytes int readTimeout = 50; // in milliseconds int maxPackets = 50; - listenForPacketsOnDevice(device, snapshotLength, readTimeout, maxPackets); + return listenForPacketsOnDevice(device, snapshotLength, readTimeout, maxPackets); } /** diff --git a/src/main/java/com/softwaredesign/PcapFileWriterAndReader.java b/src/main/java/com/softwaredesign/PcapFileWriterAndReader.java new file mode 100644 index 0000000..467a91e --- /dev/null +++ b/src/main/java/com/softwaredesign/PcapFileWriterAndReader.java @@ -0,0 +1,25 @@ +package com.softwaredesign; + +import org.pcap4j.core.NotOpenException; +import org.pcap4j.core.PcapDumper; +import org.pcap4j.core.PcapHandle; +import org.pcap4j.core.PcapNativeException; +import org.pcap4j.packet.Packet; + +import java.util.ArrayList; + +public class PcapFileWriterAndReader { + public void writePacketsToFile(String fileName, ArrayList packets, PcapHandle handle) throws NotOpenException, PcapNativeException { + PcapDumper dumper = handle.dumpOpen(fileName + ".pcap"); + + for (Packet packet: packets) { + try { + dumper.dump(packet, handle.getTimestamp()); + } catch (NotOpenException e) { + e.printStackTrace(); + } + } + + dumper.close(); + } +}