use of org.pcap4j.core.PcapDumper in project plc4x by apache.
the class AdsDumper method main.
public static void main(String... args) throws Exception {
Path dumpFile = Files.createTempFile("pcapdump", ".pcap");
try (PcapHandle handle = Pcaps.openDead(DataLinkType.EN10MB, 65536);
PcapDumper dumper = handle.dumpOpen(dumpFile.toAbsolutePath().toString())) {
String randomString = RandomStringUtils.randomAscii(1024);
AdsWriteRequest adsWriteRequest = AdsWriteRequest.of(AmsNetId.of("192.168.99.101.1.1"), AmsPort.of(851), AmsNetId.of("192.168.99.1.1.1"), AmsPort.of(14), Invoke.of(0), IndexGroup.of(1), IndexOffset.of(3), Data.of(randomString.getBytes()));
AmsTCPPacket amsTCPPacket = AmsTCPPacket.of(UserData.of(adsWriteRequest.getBytes()));
// try (TcpHexDumper tcpHexDumper = TcpHexDumper.runOn(55862); Socket localhost = new Socket("localhost", tcpHexDumper.getPort())) {
try (TcpHexDumper tcpHexDumper = TcpHexDumper.runOn(55862);
Socket localhost = new Socket("192.168.99.100", 48898)) {
localhost.getOutputStream().write(amsTCPPacket.getBytes());
}
UnknownPacket.Builder amsPacket = new UnknownPacket.Builder();
amsPacket.rawData(amsTCPPacket.getBytes());
TcpPacket.Builder tcpPacketBuilder = new TcpPacket.Builder();
tcpPacketBuilder.srcAddr(InetAddress.getLocalHost()).srcPort(TcpPort.getInstance((short) 13)).dstAddr(InetAddress.getLocalHost()).dstPort(TcpPort.getInstance((short) 48898)).payloadBuilder(amsPacket).correctChecksumAtBuild(true).correctLengthAtBuild(true);
IpV4Packet.Builder ipv4PacketBuilder = new IpV4Packet.Builder();
ipv4PacketBuilder.version(IpVersion.IPV4).tos(IpV4Rfc1349Tos.newInstance((byte) 0x75)).protocol(IpNumber.TCP).srcAddr((Inet4Address) InetAddress.getLocalHost()).dstAddr((Inet4Address) InetAddress.getLocalHost()).payloadBuilder(tcpPacketBuilder).correctChecksumAtBuild(true).correctLengthAtBuild(true);
EthernetPacket.Builder etherPacketBuilder = new EthernetPacket.Builder();
etherPacketBuilder.srcAddr(MacAddress.getByName("fe:00:00:00:00:01")).dstAddr(MacAddress.getByName("fe:00:00:00:00:02")).type(EtherType.IPV4).payloadBuilder(ipv4PacketBuilder).paddingAtBuild(true);
dumper.dump(etherPacketBuilder.build());
dumper.flush();
logger.info("Wrote {}", dumpFile);
}
}
Aggregations