address = (Inet4Address)InetAddress.getByName(strAddress);
} catch (UnknownHostException e1) {
throw new IllegalArgumentException("args[0]: " + strAddress);
}
final IcmpV4Type type;
try {
type = IcmpV4Type.getInstance(Byte.parseByte(strType));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("args[1]: " + strType, e);
}
if (
!type.equals(IcmpV4Type.DESTINATION_UNREACHABLE)
&& !type.equals(IcmpV4Type.TIME_EXCEEDED)
&& !type.equals(IcmpV4Type.PARAMETER_PROBLEM)
) { throw new IllegalArgumentException("args[1]: " + strType); }
IcmpV4Code code;
try {
code
= IcmpV4Code.getInstance(
type.value(),
Byte.parseByte(strCode)
);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("args[1]: " + strType, e);
}
PcapNetworkInterface nif;
try {
nif = new NifSelector().selectNetworkInterface();
} catch (IOException e) {
e.printStackTrace();
return;
}
if (nif == null) {
return;
}
System.out.println(nif.getName() + "(" + nif.getDescription() + ")");
final PcapHandle handle4capture
= nif.openLive(65536, PromiscuousMode.PROMISCUOUS, 10);
final PcapHandle handle4send
= nif.openLive(65536, PromiscuousMode.PROMISCUOUS, 10);
handle4capture.setFilter(
"(ether dst " + MAC_ADDR + ") or (arp and ether dst "
+ Pcaps.toBpfString(MacAddress.ETHER_BROADCAST_ADDRESS) +")",
BpfCompileMode.OPTIMIZE
);
Packet.Builder tmp;
if (type.equals(IcmpV4Type.DESTINATION_UNREACHABLE)) {
tmp = new IcmpV4DestinationUnreachablePacket.Builder();
}
else if (type.equals(IcmpV4Type.TIME_EXCEEDED)) {
tmp = new IcmpV4TimeExceededPacket.Builder();
}
else if (type.equals(IcmpV4Type.PARAMETER_PROBLEM)) {
tmp = new IcmpV4ParameterProblemPacket.Builder();
}
else { throw new AssertionError(); }
final Packet.Builder icmpV4errb = tmp;
IcmpV4CommonPacket.Builder icmpV4b = new IcmpV4CommonPacket.Builder();
icmpV4b.type(type)
.code(code)
.payloadBuilder(icmpV4errb)
.correctChecksumAtBuild(true);
final IpV4Packet.Builder ipv4b = new IpV4Packet.Builder();
ipv4b.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte)0))
.identification((short)100)
.ttl((byte)100)
.protocol(IpNumber.ICMPV4)
.payloadBuilder(icmpV4b)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true);
final EthernetPacket.Builder eb = new EthernetPacket.Builder();
eb.type(EtherType.IPV4)
.payloadBuilder(ipv4b)
.paddingAtBuild(true);
final PacketListener listener
= new PacketListener() {
public void gotPacket(Packet packet) {
if (packet.contains(IcmpV4EchoPacket.class)) {
if (type.equals(IcmpV4Type.DESTINATION_UNREACHABLE)) {
((IcmpV4DestinationUnreachablePacket.Builder)icmpV4errb).payload(
IcmpV4Helper.makePacketForInvokingPacketField(packet.get(IpV4Packet.class))
);
}
else if (type.equals(IcmpV4Type.TIME_EXCEEDED)) {
((IcmpV4TimeExceededPacket.Builder)icmpV4errb).payload(
IcmpV4Helper.makePacketForInvokingPacketField(packet.get(IpV4Packet.class))
);
}
else if (type.equals(IcmpV4Type.PARAMETER_PROBLEM)) {
((IcmpV4ParameterProblemPacket.Builder)icmpV4errb).payload(
IcmpV4Helper.makePacketForInvokingPacketField(packet.get(IpV4Packet.class))
);
}