Skip to content

Commit f4d82a7

Browse files
author
Emil Gustafsson
committed
update IPv6NeighborDiscoveryService
update exception handling Signed-off-by: Emil Gustafsson <[email protected]>
1 parent 2f68a33 commit f4d82a7

File tree

1 file changed

+57
-41
lines changed

1 file changed

+57
-41
lines changed

src/main/java/com/cisco/trex/stateless/IPv6NeighborDiscoveryService.java

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.google.common.collect.Lists;
1515
import com.google.common.net.InetAddresses;
1616
import java.net.Inet6Address;
17+
import java.net.InetAddress;
1718
import java.net.UnknownHostException;
1819
import java.util.AbstractMap;
1920
import java.util.ArrayList;
@@ -173,7 +174,7 @@ public EthernetPacket sendIcmpV6Echo(
173174
while (endTimeSec > System.currentTimeMillis()) {
174175
List<EthernetPacket> rxQueue =
175176
tRexClient.getRxQueue(portIdx, pkt -> pkt.contains(IcmpV6EchoReplyPacket.class));
176-
if (rxQueue.size() > 0) {
177+
if (!rxQueue.isEmpty()) {
177178
icmpUnicastReply = rxQueue.get(0);
178179
}
179180
}
@@ -221,19 +222,18 @@ public EthernetPacket sendNeighborSolicitation(
221222
String dstAddr = ipV6Header.getDstAddr().toString().substring(1);
222223

223224
try {
224-
Inet6Address dstIPv6Addr = (Inet6Address) Inet6Address.getByName(dstAddr);
225+
Inet6Address dstIPv6Addr = (Inet6Address) InetAddress.getByName(dstAddr);
225226
Inet6Address srcIPv6Addr =
226-
(Inet6Address) Inet6Address.getByName(generateIPv6AddrFromMAC(srcMac));
227+
(Inet6Address) InetAddress.getByName(generateIPv6AddrFromMAC(srcMac));
227228

228-
Inet6Address nodeIpv6 = (Inet6Address) Inet6Address.getByName(nodeIp);
229-
Inet6Address targetIpv6inNS = (Inet6Address) Inet6Address.getByName(dstIp);
229+
Inet6Address nodeIpv6 = (Inet6Address) InetAddress.getByName(nodeIp);
230+
Inet6Address targetIpv6inNS = (Inet6Address) InetAddress.getByName(dstIp);
230231
return icmpV6NaHdr.getSolicitedFlag()
231232
&& nodeIpv6.equals(targetIpv6inNS)
232233
&& dstIPv6Addr.equals(srcIPv6Addr);
233-
} catch (UnknownHostException ignored) {
234-
// Do nothing
234+
} catch (UnknownHostException e) {
235+
throw new IllegalArgumentException("Invalid address", e);
235236
}
236-
return false;
237237
};
238238

239239
EthernetPacket na = null;
@@ -312,14 +312,13 @@ private Map<String, EthernetPacket> sendNSandIcmpV6Req(
312312
String dstAddr = ipV6Header.getDstAddr().toString().substring(1);
313313

314314
try {
315-
Inet6Address dstIPv6Addr = (Inet6Address) Inet6Address.getByName(dstAddr);
315+
Inet6Address dstIPv6Addr = (Inet6Address) InetAddress.getByName(dstAddr);
316316
Inet6Address srcIPv6Addr =
317-
(Inet6Address) Inet6Address.getByName(generateIPv6AddrFromMAC(srcMac));
317+
(Inet6Address) InetAddress.getByName(generateIPv6AddrFromMAC(srcMac));
318318
return !naIncomingRequests.containsKey(nodeIp) && dstIPv6Addr.equals(srcIPv6Addr);
319-
} catch (UnknownHostException ignored) {
320-
// Do nothing
319+
} catch (UnknownHostException e) {
320+
throw new IllegalArgumentException("Invalid address", e);
321321
}
322-
return false;
323322
};
324323

325324
while (endTs > System.currentTimeMillis()) {
@@ -337,7 +336,7 @@ private Map<String, EthernetPacket> sendNSandIcmpV6Req(
337336
return naIncomingRequests;
338337
}
339338

340-
private com.cisco.trex.stateless.model.Stream buildStream(Packet pkt) {
339+
private static com.cisco.trex.stateless.model.Stream buildStream(Packet pkt) {
341340
int streamId = (int) (Math.random() * 1000);
342341
return new com.cisco.trex.stateless.model.Stream(
343342
streamId,
@@ -360,7 +359,7 @@ private com.cisco.trex.stateless.model.Stream buildStream(Packet pkt) {
360359
null);
361360
}
362361

363-
private Packet buildICMPV6NSPkt(
362+
private static Packet buildICMPV6NSPkt(
364363
PortVlan vlan, String srcMac, String dstMac, String dstIp, String srcIp) {
365364
EthernetPacket.Builder ethBuilder = new EthernetPacket.Builder();
366365
try {
@@ -375,23 +374,23 @@ private Packet buildICMPV6NSPkt(
375374
new IcmpV6NeighborSolicitationPacket.Builder();
376375
ipv6NSBuilder
377376
.options(Arrays.asList(sourceLLAddr))
378-
.targetAddress((Inet6Address) Inet6Address.getByName(dstIp));
377+
.targetAddress((Inet6Address) InetAddress.getByName(dstIp));
379378

380379
final String specifiedSrcIP = srcIp != null ? srcIp : generateIPv6AddrFromMAC(srcMac);
381380

382381
IcmpV6CommonPacket.Builder icmpCommonPktBuilder = new IcmpV6CommonPacket.Builder();
383382
icmpCommonPktBuilder
384-
.srcAddr((Inet6Address) Inet6Address.getByName(specifiedSrcIP))
385-
.dstAddr((Inet6Address) Inet6Address.getByName(dstIp))
383+
.srcAddr((Inet6Address) InetAddress.getByName(specifiedSrcIP))
384+
.dstAddr((Inet6Address) InetAddress.getByName(dstIp))
386385
.type(IcmpV6Type.NEIGHBOR_SOLICITATION)
387386
.code(IcmpV6Code.NO_CODE)
388387
.correctChecksumAtBuild(true)
389388
.payloadBuilder(ipv6NSBuilder);
390389

391390
IpV6Packet.Builder ipV6Builder = new IpV6Packet.Builder();
392391
ipV6Builder
393-
.srcAddr((Inet6Address) Inet6Address.getByName(specifiedSrcIP))
394-
.dstAddr((Inet6Address) Inet6Address.getByName(dstIp))
392+
.srcAddr((Inet6Address) InetAddress.getByName(specifiedSrcIP))
393+
.dstAddr((Inet6Address) InetAddress.getByName(dstIp))
395394
.version(IpVersion.IPV6)
396395
.hopLimit((byte) -1)
397396
.trafficClass(IpV6SimpleTrafficClass.newInstance((byte) 0))
@@ -413,8 +412,8 @@ private Packet buildICMPV6NSPkt(
413412
.payloadBuilder(payload.getValue())
414413
.paddingAtBuild(true);
415414

416-
} catch (UnknownHostException ignored) {
417-
// Do nothing
415+
} catch (UnknownHostException e) {
416+
throw new IllegalArgumentException("Invalid address", e);
418417
}
419418
return ethBuilder.build();
420419
}
@@ -447,7 +446,7 @@ private static String getLinkLayerAddress(IpV6Packet pkt) {
447446
return ByteArrays.toHexString(linkLayerAddress, ":");
448447
}
449448

450-
private Packet buildICMPV6NAPkt(
449+
private static Packet buildICMPV6NAPkt(
451450
PortVlan vlan, String srcMac, String dstMac, String dstIp, String srcIP) {
452451
final String specifiedSrcIP = srcIP != null ? srcIP : generateIPv6AddrFromMAC(srcMac);
453452

@@ -467,21 +466,21 @@ private Packet buildICMPV6NAPkt(
467466
.options(Arrays.asList(tLLAddr))
468467
.solicitedFlag(true)
469468
.overrideFlag(true)
470-
.targetAddress((Inet6Address) Inet6Address.getByName(specifiedSrcIP));
469+
.targetAddress((Inet6Address) InetAddress.getByName(specifiedSrcIP));
471470

472471
IcmpV6CommonPacket.Builder icmpCommonPktBuilder = new IcmpV6CommonPacket.Builder();
473472
icmpCommonPktBuilder
474-
.srcAddr((Inet6Address) Inet6Address.getByName(specifiedSrcIP))
475-
.dstAddr((Inet6Address) Inet6Address.getByName(dstIp))
473+
.srcAddr((Inet6Address) InetAddress.getByName(specifiedSrcIP))
474+
.dstAddr((Inet6Address) InetAddress.getByName(dstIp))
476475
.type(IcmpV6Type.NEIGHBOR_ADVERTISEMENT)
477476
.code(IcmpV6Code.NO_CODE)
478477
.correctChecksumAtBuild(true)
479478
.payloadBuilder(ipv6NABuilder);
480479

481480
IpV6Packet.Builder ipV6Builder = new IpV6Packet.Builder();
482481
ipV6Builder
483-
.srcAddr((Inet6Address) Inet6Address.getByName(specifiedSrcIP))
484-
.dstAddr((Inet6Address) Inet6Address.getByName(dstIp))
482+
.srcAddr((Inet6Address) InetAddress.getByName(specifiedSrcIP))
483+
.dstAddr((Inet6Address) InetAddress.getByName(dstIp))
485484
.version(IpVersion.IPV6)
486485
.trafficClass(IpV6SimpleTrafficClass.newInstance((byte) 0))
487486
.flowLabel(IpV6SimpleFlowLabel.newInstance(0))
@@ -503,8 +502,8 @@ private Packet buildICMPV6NAPkt(
503502
.payloadBuilder(payload.getValue())
504503
.paddingAtBuild(true);
505504

506-
} catch (UnknownHostException ignored) {
507-
// Do nothing
505+
} catch (UnknownHostException e) {
506+
throw new IllegalArgumentException("Invalid address", e);
508507
}
509508

510509
return ethBuilder.build();
@@ -527,6 +526,19 @@ public static EthernetPacket buildICMPV6EchoReq(
527526
String dstIp,
528527
int icmpId,
529528
int icmpSeq) {
529+
PortVlan vlan = new PortVlan();
530+
vlan.setTags(new ArrayList<Integer>());
531+
return buildICMPV6EchoReq(vlan, srcIp, srcMacString, dstMacString, dstIp, icmpId, icmpSeq);
532+
}
533+
534+
public static EthernetPacket buildICMPV6EchoReq(
535+
PortVlan vlan,
536+
String srcIp,
537+
String srcMacString,
538+
String dstMacString,
539+
String dstIp,
540+
int icmpId,
541+
int icmpSeq) {
530542
/*
531543
*
532544
* mld_pkt = (Ether(src = self.src_mac, dst = self.dst_mld_mac) / IPv6(src = self.src_ip, dst =
@@ -544,18 +556,18 @@ public static EthernetPacket buildICMPV6EchoReq(
544556
IcmpV6CommonPacket.Builder icmpCommonPktBuilder = new IcmpV6CommonPacket.Builder();
545557
try {
546558
icmpCommonPktBuilder
547-
.srcAddr((Inet6Address) Inet6Address.getByName(specifiedSrcIP))
559+
.srcAddr((Inet6Address) InetAddress.getByName(specifiedSrcIP))
548560
.dstAddr(
549-
(Inet6Address) Inet6Address.getByName(dstIp != null ? dstIp : "ff02:0:0:0:0:0:0:1"))
561+
(Inet6Address) InetAddress.getByName(dstIp != null ? dstIp : "ff02:0:0:0:0:0:0:1"))
550562
.type(IcmpV6Type.ECHO_REQUEST)
551563
.code(IcmpV6Code.NO_CODE)
552564
.correctChecksumAtBuild(true)
553565
.payloadBuilder(icmpV6ERBuilder);
554566
IpV6Packet.Builder ipV6Builder = new IpV6Packet.Builder();
555567
ipV6Builder
556-
.srcAddr((Inet6Address) Inet6Address.getByName(specifiedSrcIP))
568+
.srcAddr((Inet6Address) InetAddress.getByName(specifiedSrcIP))
557569
.dstAddr(
558-
(Inet6Address) Inet6Address.getByName(dstIp != null ? dstIp : "ff02:0:0:0:0:0:0:1"))
570+
(Inet6Address) InetAddress.getByName(dstIp != null ? dstIp : "ff02:0:0:0:0:0:0:1"))
559571
.version(IpVersion.IPV6)
560572
.trafficClass(IpV6SimpleTrafficClass.newInstance((byte) 0))
561573
.flowLabel(IpV6SimpleFlowLabel.newInstance(0))
@@ -573,18 +585,24 @@ public static EthernetPacket buildICMPV6EchoReq(
573585
}
574586

575587
EthernetPacket.Builder ethBuilder = new EthernetPacket.Builder();
588+
AbstractMap.SimpleEntry<EtherType, Packet.Builder> payload =
589+
new AbstractMap.SimpleEntry<>(EtherType.IPV6, ipV6Builder);
590+
if (!vlan.getTags().isEmpty()) {
591+
payload = buildVlan(ipV6Builder, vlan);
592+
}
593+
576594
ethBuilder
577-
.type(EtherType.IPV6)
578595
.srcAddr(MacAddress.getByName(srcMacString))
579596
.dstAddr(dstMac)
580-
.payloadBuilder(ipV6Builder)
597+
.dstAddr(dstMac)
598+
.type(payload.getKey())
599+
.payloadBuilder(payload.getValue())
581600
.paddingAtBuild(true);
582601

583602
return ethBuilder.build();
584-
} catch (UnknownHostException ignore) {
585-
// Do nothing
603+
} catch (UnknownHostException e) {
604+
throw new IllegalArgumentException("Invalid address", e);
586605
}
587-
return null;
588606
}
589607

590608
public static EthernetPacket buildICMPV6EchoReq(
@@ -613,10 +631,8 @@ private static MacAddress multicastMacFromIPv6(String ipV6) {
613631

614632
private static long[] divMod(long a, long b) {
615633
long[] result = new long[2];
616-
617634
result[1] = a % b;
618635
result[0] = (a - result[1]) / b;
619-
620636
return result;
621637
}
622638

0 commit comments

Comments
 (0)