From db2d6fa49214b06bb9a79571d4b15ae5da8dd260 Mon Sep 17 00:00:00 2001 From: Bryan Date: Wed, 5 Oct 2022 20:01:05 -0500 Subject: [PATCH 1/2] Writing pcap file functionality --- src/main/java/com/softwaredesign/Main.java | 5 ++-- .../NetworkInterfaceHandler.java | 8 +++--- .../PcapFileWriterAndReader.java | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/softwaredesign/PcapFileWriterAndReader.java 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(); + } +} From 03b18bb461990634bbb78cdf3a4b305a0ebb7583 Mon Sep 17 00:00:00 2001 From: Bryan Date: Wed, 5 Oct 2022 20:06:39 -0500 Subject: [PATCH 2/2] Updated README For Sprint 4 --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 36586fe..f09e1ce 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,12 @@ You will extract the core functional and non-functional requirements, and protot - Task5: Add junit dependency in pom.xml - Task6: ipv4 or ipv6 address checker #### Sprint 3: Sep 28, 2022 - Oct 5, 2022 -- Task2: Create command line class to handle user input -- Task4: Integrate different components on main class - Task7: Writing packets into a file #### Sprint 4: Oct 5, 2022 - Oct 19, 2022 +- Task2: Create command line class to handle user input +- Task4: Integrate different components on main class +- Task8: Reading packets from pcap file +- Task9: Add reading packets given an amount of time on a interface #### Sprint 5: Oct 19, 2022 - Nov 2, 2022 #### Sprint 6: Nov 2, 2022 - Nov 16, 2022 #### Project Presentation: Nov 28, 2022 @@ -53,9 +55,9 @@ You will extract the core functional and non-functional requirements, and protot #### Tasks ToDo - Task2: Create command line class to handle user input - Task4: Integrate different components on main class +- Task9: Add reading packets given an amount of time on a interface #### WIP (Task name, team member(s) working on it) -- Task7: Writing packets into a file, Bryan - +- Task8: Reading packets from pcap file, Bryan #### Tasks Done - Created repository - Added ReadMe file @@ -64,6 +66,7 @@ You will extract the core functional and non-functional requirements, and protot - Task1: Add packet capturing library, Bryan - Task3: Add basic packet capture functionality, Bryan - Task6: ipv4 or ipv6 address checker, Diogo +- Task7: Writing packets into a file, Bryan ### Running Instructions: