Skip to content

Commit 686f597

Browse files
Update CapHandling.java
1 parent c457835 commit 686f597

File tree

1 file changed

+155
-155
lines changed

1 file changed

+155
-155
lines changed

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

Lines changed: 155 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ void analyze() {
282282
/** only for tcp usage */
283283
long expServerSeq = -1;
284284
long expClientSeq = -1;
285+
try (PcapHandle pcapHandle = Pcaps.openOffline(fileName)) {
285286

286-
while (true) {
287-
double time;
288-
try (PcapHandle pcapHandle = Pcaps.openOffline(fileName)) {
287+
while (true) {
288+
double time;
289289
try {
290290
nextEthPkt = pcapHandle.getNextPacketEx(); // get next Ethernet packet
291291
} catch (Exception e) {
@@ -294,183 +294,183 @@ void analyze() {
294294

295295
Timestamp timestamp = pcapHandle.getTimestamp();
296296
time = timestamp.getSeconds() + (double) timestamp.getNanos() / 1000000000; // time
297-
} catch (PcapNativeException e) {
298-
throw new IllegalStateException("open pcap file failed", e);
299-
}
300-
double dtime = time;
301-
double pktTime = 0;
302-
if (lastTime == 0) {
303-
pktTime = 0;
304-
} else {
305-
pktTime = dtime - lastTime;
306-
}
307-
lastTime = dtime;
308-
309-
IpPacket l3;
310-
Packet next = nextEthPkt.getPayload(); // eth data, ip layer
311-
if (next instanceof IpV4Packet) {
312-
l3 = (IpV4Packet) next;
313-
} else if (next instanceof IpV6Packet) {
314-
l3 = (IpV6Packet) next;
315-
} else {
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));
320-
}
297+
double dtime = time;
298+
double pktTime = 0;
299+
if (lastTime == 0) {
300+
pktTime = 0;
301+
} else {
302+
pktTime = dtime - lastTime;
303+
}
304+
lastTime = dtime;
305+
306+
IpPacket l3;
307+
Packet next = nextEthPkt.getPayload(); // eth data, ip layer
308+
if (next instanceof IpV4Packet) {
309+
l3 = (IpV4Packet) next;
310+
} else if (next instanceof IpV6Packet) {
311+
l3 = (IpV6Packet) next;
312+
} else {
313+
throw new IllegalStateException(
314+
String.format(
315+
"Error for file %s: Packet #%s in pcap is not IPv4 or IPv6!",
316+
this.fileName, index));
317+
}
321318

322-
// get ip packet header
323-
IpPacket.IpHeader ipHeader = l3.getHeader();
319+
// get ip packet header
320+
IpPacket.IpHeader ipHeader = l3.getHeader();
324321

325-
if (clientIp == null) {
326-
clientIp = ipHeader.getSrcAddr();
327-
serverIp = ipHeader.getDstAddr();
328-
direction = SideType.Client;
329-
} else {
330-
if (clientIp.equals(ipHeader.getSrcAddr()) && serverIp.equals(ipHeader.getDstAddr())) {
322+
if (clientIp == null) {
323+
clientIp = ipHeader.getSrcAddr();
324+
serverIp = ipHeader.getDstAddr();
331325
direction = SideType.Client;
332-
} else if (serverIp.equals(ipHeader.getSrcAddr())
333-
&& clientIp.equals(ipHeader.getDstAddr())) {
334-
direction = SideType.Server;
335326
} else {
336-
this.fail(
337-
String.format(
338-
"Only one session is allowed in a file. Packet %s is from different session",
339-
index));
327+
if (clientIp.equals(ipHeader.getSrcAddr()) && serverIp.equals(ipHeader.getDstAddr())) {
328+
direction = SideType.Client;
329+
} else if (serverIp.equals(ipHeader.getSrcAddr())
330+
&& clientIp.equals(ipHeader.getDstAddr())) {
331+
direction = SideType.Server;
332+
} else {
333+
this.fail(
334+
String.format(
335+
"Only one session is allowed in a file. Packet %s is from different session",
336+
index));
337+
}
340338
}
341-
}
342339

343-
Packet l4 = l3.getPayload(); // Transport Layer ,l4
344-
TcpPacket tcp = null;
345-
UdpPacket udp = null;
340+
Packet l4 = l3.getPayload(); // Transport Layer ,l4
341+
TcpPacket tcp = null;
342+
UdpPacket udp = null;
346343

347-
if (l4 instanceof UdpPacket) {
348-
udp = (UdpPacket) l4;
349-
}
350-
if (l4 instanceof TcpPacket) {
351-
tcp = (TcpPacket) l4;
352-
}
344+
if (l4 instanceof UdpPacket) {
345+
udp = (UdpPacket) l4;
346+
}
347+
if (l4 instanceof TcpPacket) {
348+
tcp = (TcpPacket) l4;
349+
}
353350

354-
if (tcp == null && udp == null) {
355-
this.fail(String.format("Packet #%s in pcap has is not TCP or UDP", index));
356-
}
351+
if (tcp == null && udp == null) {
352+
this.fail(String.format("Packet #%s in pcap has is not TCP or UDP", index));
353+
}
357354

358-
if (tcp != null && udp != null) {
359-
this.fail(String.format("Packet #%s in pcap has both TCP and UDP", index));
360-
}
355+
if (tcp != null && udp != null) {
356+
this.fail(String.format("Packet #%s in pcap has both TCP and UDP", index));
357+
}
361358

362-
String typel4 = getType(tcp, udp);
359+
String typel4 = getType(tcp, udp);
363360

364-
if (isTcp == null) {
365-
isTcp = typel4;
366-
} else if (!isTcp.equals(typel4)) {
367-
this.fail(String.format("Packet #%s in pcap is %s and flow is %s", index, typel4, isTcp));
368-
}
361+
if (isTcp == null) {
362+
isTcp = typel4;
363+
} else if (!isTcp.equals(typel4)) {
364+
this.fail(String.format("Packet #%s in pcap is %s and flow is %s", index, typel4, isTcp));
365+
}
369366

370-
// TCP scenario
371-
if (tcp != null) {
372-
TcpPacket.TcpHeader l4Header = tcp.getHeader();
373-
374-
// SYN , connection status
375-
if (l4Header.getSyn()) {
376-
// SYN is true & ACK is true
377-
if (l4Header.getAck()) {
378-
// s_tcp_opts =tcp.opts
379-
sTcpWin = l4Header.getWindowAsInt();
380-
if (state == states.get(INIT)) {
381-
this.fail(String.format("Packet #%s is SYN+ACK, but there was no SYN yet", index));
382-
} else if (state != states.get(SYN)) {
383-
this.fail(
384-
String.format(
385-
"Packet #%s is SYN+ACK, but there was already SYN+ACK in cap file", index));
367+
// TCP scenario
368+
if (tcp != null) {
369+
TcpPacket.TcpHeader l4Header = tcp.getHeader();
370+
371+
// SYN , connection status
372+
if (l4Header.getSyn()) {
373+
// SYN is true & ACK is true
374+
if (l4Header.getAck()) {
375+
// s_tcp_opts =tcp.opts
376+
sTcpWin = l4Header.getWindowAsInt();
377+
if (state == states.get(INIT)) {
378+
this.fail(String.format("Packet #%s is SYN+ACK, but there was no SYN yet", index));
379+
} else if (state != states.get(SYN)) {
380+
this.fail(
381+
String.format(
382+
"Packet #%s is SYN+ACK, but there was already SYN+ACK in cap file", index));
383+
}
384+
state = states.get(SYN_ACK);
385+
expServerSeq = l4Header.getSequenceNumberAsLong() + 1;
386386
}
387-
state = states.get(SYN_ACK);
388-
expServerSeq = l4Header.getSequenceNumberAsLong() + 1;
389-
}
390-
// SYN - no ACK. Should be first packet client->server
391-
else {
392-
cTcpWin = l4Header.getWindowAsInt();
393-
expClientSeq = l4Header.getSequenceNumberAsLong() + 1;
394-
// allowing syn retransmission because cap2/https.pcap contains this
395-
if (state > states.get(SYN)) {
396-
this.fail(
397-
String.format(
398-
"Packet #%s is TCP SYN, but there was already TCP SYN in cap file", index));
399-
} else {
400-
state = states.get(SYN);
387+
// SYN - no ACK. Should be first packet client->server
388+
else {
389+
cTcpWin = l4Header.getWindowAsInt();
390+
expClientSeq = l4Header.getSequenceNumberAsLong() + 1;
391+
// allowing syn retransmission because cap2/https.pcap contains this
392+
if (state > states.get(SYN)) {
393+
this.fail(
394+
String.format(
395+
"Packet #%s is TCP SYN, but there was already TCP SYN in cap file", index));
396+
} else {
397+
state = states.get(SYN);
398+
}
401399
}
400+
} else if (state != states.get(SYN_ACK)) {
401+
this.fail("Cap file must start with syn, syn+ack sequence");
402402
}
403-
} else if (state != states.get(SYN_ACK)) {
404-
this.fail("Cap file must start with syn, syn+ack sequence");
405-
}
406-
if (!(l4Type == null || l4Type.equals(TCP))) {
407-
this.fail(
408-
String.format(
409-
"PCAP contains both TCP and %s. This is not supported currently.", l4Type));
410-
}
411-
l4Type = TCP;
403+
if (!(l4Type == null || l4Type.equals(TCP))) {
404+
this.fail(
405+
String.format(
406+
"PCAP contains both TCP and %s. This is not supported currently.", l4Type));
407+
}
408+
l4Type = TCP;
412409

413-
if (srcPort == -1) {
414-
srcPort = l4Header.getSrcPort().valueAsInt();
415-
dstPort = l4Header.getDstPort().valueAsInt();
416-
}
417-
}
418-
// UDP scenario
419-
else if (udp != null) {
420-
UdpPacket.UdpHeader l4Header = udp.getHeader();
421-
if (!(l4Type == null || l4Type.equals(UDP))) {
422-
this.fail(
423-
String.format(
424-
"PCAP contains both UDP and %s. This is not supported currently.", l4Type));
410+
if (srcPort == -1) {
411+
srcPort = l4Header.getSrcPort().valueAsInt();
412+
dstPort = l4Header.getDstPort().valueAsInt();
413+
}
425414
}
426-
l4Type = UDP;
415+
// UDP scenario
416+
else if (udp != null) {
417+
UdpPacket.UdpHeader l4Header = udp.getHeader();
418+
if (!(l4Type == null || l4Type.equals(UDP))) {
419+
this.fail(
420+
String.format(
421+
"PCAP contains both UDP and %s. This is not supported currently.", l4Type));
422+
}
423+
l4Type = UDP;
427424

428-
if (srcPort == -1) {
429-
srcPort = l4Header.getSrcPort().valueAsInt();
430-
dstPort = l4Header.getDstPort().valueAsInt();
425+
if (srcPort == -1) {
426+
srcPort = l4Header.getSrcPort().valueAsInt();
427+
dstPort = l4Header.getDstPort().valueAsInt();
428+
}
429+
} else {
430+
this.fail(String.format("Packet #%s in pcap is not TCP or UDP.", index));
431431
}
432-
} else {
433-
this.fail(String.format("Packet #%s in pcap is not TCP or UDP.", index));
434-
}
435432

436-
int l4PayloadLen = 0;
437-
if (l4.getPayload() != null) {
438-
l4PayloadLen = l4.getPayload().length();
439-
totalPayloadLen += l4PayloadLen;
440-
pkts.add(new CPacketData(direction, l4.getPayload().getRawData()));
441-
} else {
442-
pkts.add(new CPacketData(direction, null));
443-
}
444-
times.add(pktTime);
445-
dirs.add(direction);
433+
int l4PayloadLen = 0;
434+
if (l4.getPayload() != null) {
435+
l4PayloadLen = l4.getPayload().length();
436+
totalPayloadLen += l4PayloadLen;
437+
pkts.add(new CPacketData(direction, l4.getPayload().getRawData()));
438+
} else {
439+
pkts.add(new CPacketData(direction, null));
440+
}
441+
times.add(pktTime);
442+
dirs.add(direction);
446443

447-
// special handling for TCP FIN
448-
if (tcp != null && tcp.getHeader().getFin()) {
449-
l4PayloadLen = 1;
450-
}
444+
// special handling for TCP FIN
445+
if (tcp != null && tcp.getHeader().getFin()) {
446+
l4PayloadLen = 1;
447+
}
451448

452-
// verify there is no packet loss or retransmission in cap file
453-
// don't check for SYN
454-
if (tcp != null && !tcp.getHeader().getSyn()) {
455-
if (tcp.getHeader().getSrcPort().valueAsInt() == this.srcPort) {
456-
if (expClientSeq != tcp.getHeader().getSequenceNumberAsLong()) {
457-
this.fail(
458-
String.format(
459-
"TCP seq in packet %s is %s. We expected %s. Please check that there are no packet loss or retransmission in cap file",
460-
index, tcp.getHeader().getSequenceNumberAsLong(), expClientSeq));
461-
}
462-
expClientSeq = tcp.getHeader().getSequenceNumberAsLong() + l4PayloadLen;
463-
} else {
464-
if (expServerSeq != tcp.getHeader().getSequenceNumberAsLong()) {
465-
this.fail(
466-
String.format(
467-
"TCP seq in packet %s is %s. We expected %s. Please check that there are no packet loss or retransmission in cap file",
468-
index, tcp.getHeader().getSequenceNumberAsLong(), expServerSeq));
449+
// verify there is no packet loss or retransmission in cap file
450+
// don't check for SYN
451+
if (tcp != null && !tcp.getHeader().getSyn()) {
452+
if (tcp.getHeader().getSrcPort().valueAsInt() == this.srcPort) {
453+
if (expClientSeq != tcp.getHeader().getSequenceNumberAsLong()) {
454+
this.fail(
455+
String.format(
456+
"TCP seq in packet %s is %s. We expected %s. Please check that there are no packet loss or retransmission in cap file",
457+
index, tcp.getHeader().getSequenceNumberAsLong(), expClientSeq));
458+
}
459+
expClientSeq = tcp.getHeader().getSequenceNumberAsLong() + l4PayloadLen;
460+
} else {
461+
if (expServerSeq != tcp.getHeader().getSequenceNumberAsLong()) {
462+
this.fail(
463+
String.format(
464+
"TCP seq in packet %s is %s. We expected %s. Please check that there are no packet loss or retransmission in cap file",
465+
index, tcp.getHeader().getSequenceNumberAsLong(), expServerSeq));
466+
}
467+
expServerSeq = tcp.getHeader().getSequenceNumberAsLong() + l4PayloadLen;
469468
}
470-
expServerSeq = tcp.getHeader().getSequenceNumberAsLong() + l4PayloadLen;
471469
}
470+
index++;
472471
}
473-
index++;
472+
} catch (PcapNativeException e) {
473+
throw new IllegalStateException("open pcap file failed", e);
474474
}
475475
this.analyzed = true;
476476
}

0 commit comments

Comments
 (0)