You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@plc4x.apache.org by GitBox <gi...@apache.org> on 2022/06/30 21:50:27 UTC

[GitHub] [plc4x] github-code-scanning[bot] commented on a diff in pull request #405: feat(eip): update eip driver to include an option for connected messaging.

github-code-scanning[bot] commented on code in PR #405:
URL: https://github.com/apache/plc4x/pull/405#discussion_r911470305


##########
plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/protocol/EipProtocolLogic.java:
##########
@@ -0,0 +1,1408 @@
+/*
+ * 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
+ *
+ *   https://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.eip.base.protocol;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+import org.apache.plc4x.java.api.messages.*;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.api.value.*;
+import org.apache.plc4x.java.eip.base.configuration.EIPConfiguration;
+import org.apache.plc4x.java.eip.base.field.EipField;
+import org.apache.plc4x.java.eip.logix.configuration.LogixConfiguration;
+import org.apache.plc4x.java.eip.readwrite.*;
+import org.apache.plc4x.java.spi.ConversationContext;
+import org.apache.plc4x.java.spi.Plc4xProtocolBase;
+import org.apache.plc4x.java.spi.configuration.HasConfiguration;
+import org.apache.plc4x.java.spi.generation.*;
+import org.apache.plc4x.java.spi.messages.*;
+import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
+import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
+import org.apache.plc4x.java.spi.values.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.time.temporal.TemporalUnit;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket> implements HasConfiguration<EIPConfiguration> {
+
+    private static final Logger logger = LoggerFactory.getLogger(EipProtocolLogic.class);
+    public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(10000);
+
+    private static final byte[] DEFAULT_SENDER_CONTEXT = "PLC4X   ".getBytes(StandardCharsets.US_ASCII);
+    private static final long EMPTY_SESSION_HANDLE = 0L;
+    private static final long EMPTY_INTERFACE_HANDLE = 0L;
+    private NullAddressItem nullAddressItem;
+    private byte[] senderContext;
+    private long connectionId = 0L;
+    private int sequenceCount = 1;
+    private EIPConfiguration configuration;
+
+    private final AtomicInteger transactionCounterGenerator = new AtomicInteger(10);
+    private RequestTransactionManager tm;
+    private long sessionHandle;
+
+    private boolean useConnectionManager = false;
+
+    private boolean cipEncapsulationAvailable = false;
+
+    private boolean useMessageRouter = false;
+
+    private List<PathSegment> routingAddress = new ArrayList<>();
+    short connectionPathSize = 0;
+    private final int connectionSerialNumber = ThreadLocalRandom.current().nextInt();
+
+    @Override
+    public void setConfiguration(EIPConfiguration configuration) {
+        this.configuration = configuration;
+        this.nullAddressItem = new NullAddressItem(this.configuration.getByteOrder());
+
+        if (configuration instanceof LogixConfiguration) {
+            // Use a connection path instead of the backplane and slot if it is available
+            LogixConfiguration logixConfigutation = (LogixConfiguration) configuration;
+            if (logixConfigutation.getCommunicationPath() != null) {
+                String[] splitConnectionPath = logixConfigutation.getCommunicationPath().split(",");
+                if (splitConnectionPath.length % 2 == 0) {
+                    for (int i = 0; (i + 1) < splitConnectionPath.length; i += 2 ) {
+                        switch(splitConnectionPath[i]) {
+                            case "1":
+                                int backplanePortId = Integer.parseInt(splitConnectionPath[i]);
+                                int slot = Integer.parseInt(splitConnectionPath[i + 1]);
+                                routingAddress.add(new PortSegment(new PortSegmentNormal((byte) backplanePortId, (short)  slot, this.configuration.getByteOrder()),this.configuration.getByteOrder()));
+                                break;
+                            case "2":
+                                int ethernetPortId = Integer.parseInt(splitConnectionPath[i]);
+                                String ipAddress = splitConnectionPath[i+1];
+                                if ((ipAddress.length() % 2) != 0) {
+                                    ipAddress += " ";
+                                }
+                                routingAddress.add(new PortSegment(new PortSegmentExtended((byte) ethernetPortId, (short) ipAddress.length(), ipAddress, this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+                                break;
+                            default:
+                                logger.error("Only backplane or Ethernet module routing is supported");
+                        }
+
+                    }
+                }
+            } else {
+                routingAddress.add(new PortSegment(new PortSegmentNormal((byte) 1, (short)  this.configuration.getSlot(), this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+            }
+        } else {
+            routingAddress.add(new PortSegment(new PortSegmentNormal((byte) 1, (short)  this.configuration.getSlot(), this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+        }
+
+        routingAddress.add(new LogicalSegment(new ClassID((byte) 0, (short) 2, this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+        routingAddress.add(new LogicalSegment(new InstanceID((byte) 0, (short) 1, this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+
+        for (PathSegment segment : this.routingAddress) {
+            this.connectionPathSize += segment.getLengthInBytes();
+        }
+        if ((this.connectionPathSize % 2) != 0) {
+            this.connectionPathSize += 1;

Review Comment:
   ## Implicit narrowing conversion in compound assignment
   
   Implicit cast of source type int to narrower destination type short.
   
   [Show more details](https://github.com/apache/plc4x/security/code-scanning/1065)



##########
plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/protocol/EipProtocolLogic.java:
##########
@@ -0,0 +1,1408 @@
+/*
+ * 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
+ *
+ *   https://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.eip.base.protocol;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+import org.apache.plc4x.java.api.messages.*;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.api.value.*;
+import org.apache.plc4x.java.eip.base.configuration.EIPConfiguration;
+import org.apache.plc4x.java.eip.base.field.EipField;
+import org.apache.plc4x.java.eip.logix.configuration.LogixConfiguration;
+import org.apache.plc4x.java.eip.readwrite.*;
+import org.apache.plc4x.java.spi.ConversationContext;
+import org.apache.plc4x.java.spi.Plc4xProtocolBase;
+import org.apache.plc4x.java.spi.configuration.HasConfiguration;
+import org.apache.plc4x.java.spi.generation.*;
+import org.apache.plc4x.java.spi.messages.*;
+import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
+import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
+import org.apache.plc4x.java.spi.values.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.time.temporal.TemporalUnit;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class EipProtocolLogic extends Plc4xProtocolBase<EipPacket> implements HasConfiguration<EIPConfiguration> {
+
+    private static final Logger logger = LoggerFactory.getLogger(EipProtocolLogic.class);
+    public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(10000);
+
+    private static final byte[] DEFAULT_SENDER_CONTEXT = "PLC4X   ".getBytes(StandardCharsets.US_ASCII);
+    private static final long EMPTY_SESSION_HANDLE = 0L;
+    private static final long EMPTY_INTERFACE_HANDLE = 0L;
+    private NullAddressItem nullAddressItem;
+    private byte[] senderContext;
+    private long connectionId = 0L;
+    private int sequenceCount = 1;
+    private EIPConfiguration configuration;
+
+    private final AtomicInteger transactionCounterGenerator = new AtomicInteger(10);
+    private RequestTransactionManager tm;
+    private long sessionHandle;
+
+    private boolean useConnectionManager = false;
+
+    private boolean cipEncapsulationAvailable = false;
+
+    private boolean useMessageRouter = false;
+
+    private List<PathSegment> routingAddress = new ArrayList<>();
+    short connectionPathSize = 0;
+    private final int connectionSerialNumber = ThreadLocalRandom.current().nextInt();
+
+    @Override
+    public void setConfiguration(EIPConfiguration configuration) {
+        this.configuration = configuration;
+        this.nullAddressItem = new NullAddressItem(this.configuration.getByteOrder());
+
+        if (configuration instanceof LogixConfiguration) {
+            // Use a connection path instead of the backplane and slot if it is available
+            LogixConfiguration logixConfigutation = (LogixConfiguration) configuration;
+            if (logixConfigutation.getCommunicationPath() != null) {
+                String[] splitConnectionPath = logixConfigutation.getCommunicationPath().split(",");
+                if (splitConnectionPath.length % 2 == 0) {
+                    for (int i = 0; (i + 1) < splitConnectionPath.length; i += 2 ) {
+                        switch(splitConnectionPath[i]) {
+                            case "1":
+                                int backplanePortId = Integer.parseInt(splitConnectionPath[i]);
+                                int slot = Integer.parseInt(splitConnectionPath[i + 1]);
+                                routingAddress.add(new PortSegment(new PortSegmentNormal((byte) backplanePortId, (short)  slot, this.configuration.getByteOrder()),this.configuration.getByteOrder()));
+                                break;
+                            case "2":
+                                int ethernetPortId = Integer.parseInt(splitConnectionPath[i]);
+                                String ipAddress = splitConnectionPath[i+1];
+                                if ((ipAddress.length() % 2) != 0) {
+                                    ipAddress += " ";
+                                }
+                                routingAddress.add(new PortSegment(new PortSegmentExtended((byte) ethernetPortId, (short) ipAddress.length(), ipAddress, this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+                                break;
+                            default:
+                                logger.error("Only backplane or Ethernet module routing is supported");
+                        }
+
+                    }
+                }
+            } else {
+                routingAddress.add(new PortSegment(new PortSegmentNormal((byte) 1, (short)  this.configuration.getSlot(), this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+            }
+        } else {
+            routingAddress.add(new PortSegment(new PortSegmentNormal((byte) 1, (short)  this.configuration.getSlot(), this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+        }
+
+        routingAddress.add(new LogicalSegment(new ClassID((byte) 0, (short) 2, this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+        routingAddress.add(new LogicalSegment(new InstanceID((byte) 0, (short) 1, this.configuration.getByteOrder()), this.configuration.getByteOrder()));
+
+        for (PathSegment segment : this.routingAddress) {
+            this.connectionPathSize += segment.getLengthInBytes();

Review Comment:
   ## Implicit narrowing conversion in compound assignment
   
   Implicit cast of source type int to narrower destination type short.
   
   [Show more details](https://github.com/apache/plc4x/security/code-scanning/1064)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org