Skip to content

Commit c457835

Browse files
fix resource leak
1 parent c1a4054 commit c457835

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/main/java/com/cisco/trex/stateful/api/lowlevel/CapHandling.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
import org.pcap4j.core.PcapHandle;
1111
import org.pcap4j.core.PcapNativeException;
1212
import org.pcap4j.core.Pcaps;
13-
import org.pcap4j.packet.*;
13+
import org.pcap4j.packet.IpPacket;
14+
import org.pcap4j.packet.IpV4Packet;
15+
import org.pcap4j.packet.IpV6Packet;
16+
import org.pcap4j.packet.Packet;
17+
import org.pcap4j.packet.TcpPacket;
18+
import org.pcap4j.packet.UdpPacket;
1419

1520
/** CapHandling to parse pcap file */
1621
class CapHandling {
@@ -129,7 +134,7 @@ int payloadLen() {
129134
}
130135

131136
class CpcapReaderHelp {
132-
private static final HashMap<String, Integer> states = new HashMap();
137+
private static final HashMap<String, Integer> states = new HashMap<>();
133138
private static final String OTHER = "other";
134139
private static final String TCP = "tcp";
135140
private static final String UDP = "udp";
@@ -162,9 +167,9 @@ class CpcapReaderHelp {
162167

163168
CpcapReaderHelp(String fileName) {
164169
this.fileName = fileName;
165-
pkts = new ArrayList();
166-
times = new ArrayList();
167-
dirs = new ArrayList();
170+
pkts = new ArrayList<>();
171+
times = new ArrayList<>();
172+
dirs = new ArrayList<>();
168173
clientIp = null;
169174
serverIp = null;
170175
isTcp = null;
@@ -229,8 +234,8 @@ void condensePktData() {
229234
}
230235
CPacketData combinedData = null;
231236

232-
List<CPacketData> newPkts = new ArrayList();
233-
List<SideType> newDirs = new ArrayList();
237+
List<CPacketData> newPkts = new ArrayList<>();
238+
List<SideType> newDirs = new ArrayList<>();
234239

235240
for (CPacketData pkt : pkts) {
236241
if (pkt.getPayload() == null) {
@@ -267,12 +272,6 @@ void analyze() {
267272
if (analyzed) {
268273
return;
269274
}
270-
PcapHandle pcapHandle;
271-
try {
272-
pcapHandle = Pcaps.openOffline(fileName);
273-
} catch (PcapNativeException e) {
274-
throw new IllegalStateException("open pcap file failed", e);
275-
}
276275

277276
int index = 0; // index
278277
Packet nextEthPkt;
@@ -285,15 +284,19 @@ void analyze() {
285284
long expClientSeq = -1;
286285

287286
while (true) {
288-
try {
289-
nextEthPkt = pcapHandle.getNextPacketEx(); // get next Ethernet packet
290-
} catch (Exception e) {
291-
break;
292-
}
293-
294-
Timestamp timestamp = pcapHandle.getTimestamp();
295-
double time = timestamp.getSeconds() + (double) timestamp.getNanos() / 1000000000; // time
287+
double time;
288+
try (PcapHandle pcapHandle = Pcaps.openOffline(fileName)) {
289+
try {
290+
nextEthPkt = pcapHandle.getNextPacketEx(); // get next Ethernet packet
291+
} catch (Exception e) {
292+
break;
293+
}
296294

295+
Timestamp timestamp = pcapHandle.getTimestamp();
296+
time = timestamp.getSeconds() + (double) timestamp.getNanos() / 1000000000; // time
297+
} catch (PcapNativeException e) {
298+
throw new IllegalStateException("open pcap file failed", e);
299+
}
297300
double dtime = time;
298301
double pktTime = 0;
299302
if (lastTime == 0) {
@@ -303,16 +306,17 @@ void analyze() {
303306
}
304307
lastTime = dtime;
305308

306-
IpPacket l3 = null;
307-
309+
IpPacket l3;
308310
Packet next = nextEthPkt.getPayload(); // eth data, ip layer
309-
310311
if (next instanceof IpV4Packet) {
311312
l3 = (IpV4Packet) next;
312313
} else if (next instanceof IpV6Packet) {
313314
l3 = (IpV6Packet) next;
314315
} else {
315-
this.fail(String.format("Packet #%s in pcap is not IPv4 or IPv6!", index));
316+
throw new IllegalStateException(
317+
String.format(
318+
"Error for file %s: Packet #%s in pcap is not IPv4 or IPv6!",
319+
this.fileName, index));
316320
}
317321

318322
// get ip packet header
@@ -347,7 +351,7 @@ void analyze() {
347351
tcp = (TcpPacket) l4;
348352
}
349353

350-
if (tcp == null & udp == null) {
354+
if (tcp == null && udp == null) {
351355
this.fail(String.format("Packet #%s in pcap has is not TCP or UDP", index));
352356
}
353357

@@ -432,7 +436,6 @@ else if (udp != null) {
432436
int l4PayloadLen = 0;
433437
if (l4.getPayload() != null) {
434438
l4PayloadLen = l4.getPayload().length();
435-
byte[] rawData = l4.getPayload().getRawData();
436439
totalPayloadLen += l4PayloadLen;
437440
pkts.add(new CPacketData(direction, l4.getPayload().getRawData()));
438441
} else {
@@ -476,7 +479,7 @@ private void fail(String msg) {
476479
throw new IllegalStateException(String.format("Error for file %s: %s", this.fileName, msg));
477480
}
478481

479-
private String getType(TcpPacket tcpPacket, UdpPacket udpPacket) {
482+
private static String getType(TcpPacket tcpPacket, UdpPacket udpPacket) {
480483
if (tcpPacket != null && udpPacket == null) {
481484
return TCP;
482485
}

0 commit comments

Comments
 (0)