Skip to content

Commit

Permalink
Writing pcap file functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Balvarezpandozi committed Oct 6, 2022
1 parent 44d62a4 commit db2d6fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
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 db2d6fa

Please sign in to comment.