This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch feature/Beckhoff_ADS_protocol
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/feature/Beckhoff_ADS_protocol 
by this push:
     new 2a2d181  try to create a .pcap file with pcap4j
2a2d181 is described below

commit 2a2d181d6409447008b8b024047508c649f68ff3
Author: Sebastian Rühl <sru...@apache.org>
AuthorDate: Fri Feb 2 17:31:21 2018 +0100

    try to create a .pcap file with pcap4j
---
 plc4j/protocols/ads/pom.xml                        |  10 +-
 .../java/org/apache/plc4x/java/ads/ADSDumper.java  | 110 +++++++++++++++++++++
 2 files changed, 119 insertions(+), 1 deletion(-)

diff --git a/plc4j/protocols/ads/pom.xml b/plc4j/protocols/ads/pom.xml
index c277e83..cce37d3 100644
--- a/plc4j/protocols/ads/pom.xml
+++ b/plc4j/protocols/ads/pom.xml
@@ -30,7 +30,8 @@
 
   <artifactId>plc4j-protocol-ads</artifactId>
   <name>PLC4J: Protocol: ADS</name>
-  <description>Implementation of a PLC4X driver able to speak with Beckhoff 
ADS devices using the ADS protocol.</description>
+  <description>Implementation of a PLC4X driver able to speak with Beckhoff 
ADS devices using the ADS protocol.
+  </description>
 
   <dependencies>
     <dependency>
@@ -74,6 +75,13 @@
       <version>1.11</version>
       <scope>test</scope>
     </dependency>
+    <!-- Used to create a pcap dump file for wireshark. -->
+    <dependency>
+      <groupId>org.pcap4j</groupId>
+      <artifactId>pcap4j-core</artifactId>
+      <version>1.7.3</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
\ No newline at end of file
diff --git 
a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSDumper.java 
b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSDumper.java
new file mode 100644
index 0000000..d5a7630
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSDumper.java
@@ -0,0 +1,110 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+package org.apache.plc4x.java.ads;
+
+import org.apache.plc4x.java.ads.api.commands.ADSWriteRequest;
+import org.apache.plc4x.java.ads.api.commands.types.Data;
+import org.apache.plc4x.java.ads.api.commands.types.IndexGroup;
+import org.apache.plc4x.java.ads.api.commands.types.IndexOffset;
+import org.apache.plc4x.java.ads.api.generic.AMSHeader;
+import org.apache.plc4x.java.ads.api.generic.AMSTCPHeader;
+import org.apache.plc4x.java.ads.api.generic.types.*;
+import org.pcap4j.core.PcapDumper;
+import org.pcap4j.core.PcapHandle;
+import org.pcap4j.core.Pcaps;
+import org.pcap4j.packet.*;
+import org.pcap4j.packet.namednumber.*;
+import org.pcap4j.util.MacAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class ADSDumper {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(ADSDumper.class);
+
+    public static final 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())) {
+
+            ADSWriteRequest adsWriteRequest = new ADSWriteRequest(
+                AMSTCPHeader.of(13),
+                new AMSHeader(
+                    AMSNetId.of("0.0.0.0.0.0"),
+                    AMSPort.of(13),
+                    AMSNetId.of("0.0.0.0.0.0"),
+                    AMSPort.of(13),
+                    Command.ADS_Write,
+                    State.ADS_REQUEST_TCP,
+                    DataLength.of(13),
+                    AMSError.of((byte) 0, (byte) 0, (byte) 0, (byte) 0),
+                    Invoke.of((byte) 0, (byte) 0, (byte) 0, (byte) 0),
+                    org.apache.plc4x.java.ads.api.generic.types.Data.of((byte) 
0)
+                ),
+                IndexGroup.of((byte) 0, (byte) 0, (byte) 0, (byte) 0),
+                IndexOffset.of((byte) 0, (byte) 0, (byte) 0, (byte) 0),
+                Length.of(13),
+                Data.of((byte) 0x42)
+            );
+
+            UnknownPacket.Builder amsPacket = new UnknownPacket.Builder();
+            amsPacket.rawData(adsWriteRequest.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);
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
sru...@apache.org.

Reply via email to