Skip to content

Commit

Permalink
Merge pull request #9 from Balvarezpandozi/task7-writing-packets-info…
Browse files Browse the repository at this point in the history
…-to-a-file

Task7 writing packets info to a file
  • Loading branch information
Blooverh authored Oct 6, 2022
2 parents 44d62a4 + 03b18bb commit 8ead0b2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/softwaredesign/Main.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/softwaredesign/NetworkInterfaceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<PcapNetworkInterface> 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<>();

Expand All @@ -56,19 +56,19 @@ public void gotPacket(Packet packet) {
e.printStackTrace();
}

handle.close();
return handle;
}

/**
* This function allows overloading for the previous listenForPacketsOnDevice function. Sets up default parameters.
* @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);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/softwaredesign/PcapFileWriterAndReader.java
Original file line number Diff line number Diff line change
@@ -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<Packet> 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();
}
}

0 comments on commit 8ead0b2

Please sign in to comment.