You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2017/12/31 13:37:42 UTC

[incubator-plc4x] branch feature/PLC4X-18--raw-sockets created (now 386c49d)

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

cdutz pushed a change to branch feature/PLC4X-18--raw-sockets
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git.


      at 386c49d  PLC4X-18 - Implement a Netty Pipeline that allows creating pipelines for low level protocols below TCP and UDP

This branch includes the following new commits:

     new 386c49d  PLC4X-18 - Implement a Netty Pipeline that allows creating pipelines for low level protocols below TCP and UDP

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


-- 
To stop receiving notification emails like this one, please contact
['"commits@plc4x.apache.org" <co...@plc4x.apache.org>'].

[incubator-plc4x] 01/01: PLC4X-18 - Implement a Netty Pipeline that allows creating pipelines for low level protocols below TCP and UDP

Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/PLC4X-18--raw-sockets
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 386c49db11229830758c75087fa794d975f74bbf
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sun Dec 31 14:37:38 2017 +0100

    PLC4X-18 - Implement a Netty Pipeline that allows creating pipelines for low level protocols below TCP and UDP
    
    - Initial commit of this effort
    - Added a new module hierarchy for utility modules
---
 plc4j/pom.xml                                      |   1 +
 plc4j/utils/pom.xml                                |  42 +++++
 plc4j/utils/raw-sockets/pom.xml                    |  60 +++++++
 .../utils/rawsockets/netty/RawSocketChannel.java   | 186 +++++++++++++++++++++
 4 files changed, 289 insertions(+)

diff --git a/plc4j/pom.xml b/plc4j/pom.xml
index fb73861..e9932cb 100644
--- a/plc4j/pom.xml
+++ b/plc4j/pom.xml
@@ -39,6 +39,7 @@
     <module>api</module>
     <module>core</module>
     <module>protocols</module>
+    <module>utils</module>
   </modules>
 
   <dependencies>
diff --git a/plc4j/utils/pom.xml b/plc4j/utils/pom.xml
new file mode 100644
index 0000000..52834be
--- /dev/null
+++ b/plc4j/utils/pom.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+  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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.plc4x</groupId>
+    <artifactId>plc4j</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>plc4j-utils</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <name>PLC4J: Utils</name>
+  <description>A collection of utilities used in multiple modules.</description>
+
+  <modules>
+    <module>raw-sockets</module>
+  </modules>
+
+</project>
\ No newline at end of file
diff --git a/plc4j/utils/raw-sockets/pom.xml b/plc4j/utils/raw-sockets/pom.xml
new file mode 100644
index 0000000..f93813b
--- /dev/null
+++ b/plc4j/utils/raw-sockets/pom.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+  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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.plc4x</groupId>
+    <artifactId>plc4j-utils</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>plc4j-utils-raw-sockets</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+
+  <name>PLC4J: Utils: Raw-Sockets</name>
+  <description>An implementation of a Netty Channel that allows implementing protocols below the TCP and UCP level.</description>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>io.netty</groupId>
+      <artifactId>netty-transport</artifactId>
+      <version>4.1.17.Final</version>
+    </dependency>
+    <dependency>
+      <groupId>com.github.mlaccetti</groupId>
+      <artifactId>rocksaw</artifactId>
+      <version>1.1.0</version>
+      <!--
+        The jar-with-dependencies doesn't pull in third party dependencies,
+        but rather include all JNI libraries for about 22 platforms.
+      -->
+      <classifier>jar-with-dependencies</classifier>
+    </dependency>
+  </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketChannel.java b/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketChannel.java
new file mode 100644
index 0000000..8ac21dd
--- /dev/null
+++ b/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketChannel.java
@@ -0,0 +1,186 @@
+/*
+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.utils.rawsockets.netty;
+
+import com.savarese.rocksaw.net.RawSocket;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.*;
+import io.netty.channel.nio.AbstractNioByteChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+import org.apache.plc4x.java.api.exceptions.PlcIoException;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.channels.SelectableChannel;
+
+/**
+ * Netty channel implementation that uses RockSaw to create a raw socket connection to implement
+ * IP-socket based protocols not based on TCP or UDP.
+ *
+ * NOTE: This class is currently a WIP (Work in progress) it should only be used with great care.
+ */
+public class RawSocketChannel extends AbstractNioByteChannel {
+
+    private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioSocketChannel.class);
+
+    // The protocol number is defined in the IP protocol and indicates the type of protocol the payload
+    // the IP packet uses. This number is assigned by the IESG. A full list of the registered protocol
+    // numbers can be found here: https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
+    private int protocolNumber;
+
+    private RawSocket socket;
+    private InetSocketAddress localAddress;
+    private InetSocketAddress remoteAddress;
+
+    /**
+     * Initializes a raw socket that is able to communicate with raw IPv4 and IPv6 sockets, hereby
+     * allowing to implement protocols below TCP and UDP.
+     *
+     * For a list of public known protocol numbers see:
+     * https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
+     *
+     * @param parent
+     * @param ch
+     * @param protocolNumber protocol number identifying the protocol.
+     * @throws PlcIoException
+     */
+    public RawSocketChannel(Channel parent, SelectableChannel ch, int protocolNumber) throws PlcIoException {
+        super(parent, ch);
+
+        this.protocolNumber = protocolNumber;
+
+        try {
+            socket = new RawSocket();
+            socket.setIPHeaderInclude(true);
+        } catch (IOException e) {
+            throw new PlcIoException("Error setting up raw socket", e);
+        }
+    }
+
+    /**
+     * Opens a connection to the given remote address.
+     *
+     * @param remoteAddress
+     * @param localAddress
+     * @return
+     * @throws Exception
+     */
+    @Override
+    protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
+        if(!(remoteAddress instanceof InetSocketAddress) || !(localAddress instanceof InetSocketAddress)) {
+            throw new PlcIoException("Both remoteAddress and localAddress must be of type InetSocketAddress");
+        }
+
+        try {
+            this.localAddress = (InetSocketAddress) localAddress;
+            this.remoteAddress = (InetSocketAddress) remoteAddress;
+
+            socket.open(RawSocket.PF_INET, protocolNumber);
+
+            return socket.isOpen();
+        } catch (IllegalStateException | IOException e) {
+            return false;
+        }
+    }
+
+    @Override
+    protected void doFinishConnect() throws Exception {
+
+    }
+
+    /**
+     * Opens a listening socket.
+     *
+     * @param localAddress
+     * @throws Exception
+     */
+    @Override
+    protected void doBind(SocketAddress localAddress) throws Exception {
+        if(socket.isOpen()) {
+            throw new PlcIoException("Raw socket already opened.");
+        }
+        if(localAddress instanceof InetSocketAddress) {
+            this.localAddress = (InetSocketAddress) localAddress;
+            socket.bind(this.localAddress.getAddress());
+        } else {
+            throw new PlcIoException("Unsupported type of local address. Only InetSocketAddress supported.");
+        }
+    }
+
+    /**
+     * Closes the connection.
+     *
+     * @throws Exception
+     */
+    @Override
+    protected void doDisconnect() throws Exception {
+        if(socket.isOpen()) {
+            socket.close();
+        }
+    }
+
+    @Override
+    protected ChannelFuture shutdownInput() {
+        return null;
+    }
+
+    @Override
+    protected int doReadBytes(ByteBuf buf) throws Exception {
+        byte[] byteBuf = new byte[1024];
+        int readBytes = socket.read(byteBuf);
+        buf.writeBytes(byteBuf, 0, readBytes);
+        return readBytes;
+    }
+
+    @Override
+    protected int doWriteBytes(ByteBuf buf) throws Exception {
+        byte[] readableBytes = new byte[buf.readableBytes()];
+        buf.readBytes(readableBytes);
+        socket.write(remoteAddress.getAddress(), readableBytes);
+        return readableBytes.length;
+    }
+
+    @Override
+    protected long doWriteFileRegion(FileRegion region) throws Exception {
+        throw new UnsupportedOperationException("doWriteFileRegion not implemented");
+    }
+
+    @Override
+    protected SocketAddress localAddress0() {
+        return localAddress;
+    }
+
+    @Override
+    protected SocketAddress remoteAddress0() {
+        return remoteAddress;
+    }
+
+    @Override
+    public ChannelConfig config() {
+        return null;
+    }
+
+    @Override
+    public boolean isActive() {
+        return socket.isOpen();
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@plc4x.apache.org" <co...@plc4x.apache.org>.