详解S7源码3-COTP and TPKT

摘要:
连接S7COMM的介绍https://www.anquanke.com/post/id/186099OSIlayerProtocolApplicationLayerS7communicationPresentationLayerS7communicationTCP上的(COTP)SessionLayerS7communication(TPKT)TransportLayerISO(RFC1006)

连接 S7COMM简介 https://www.anquanke.com/post/id/186099

OSI layer    Protocol
Application Layer    S7 communication
Presentation Layer    S7 communication(COTP)
Session Layer    S7 communication(TPKT)
Transport Layer    ISO-on-TCP (RFC 1006)
Network Layer    IP
Data Link Layer    Ethernet
Physical Layer    Ethernet

1,TPKT(默认端口号102)

tpkt主要为

version1byte0x033
resrved1byte0x000
length2byte0x1622

length为包括这4字节+Payload在内的字节数.(Payload)为data包装后数据.

1,TPKT类,返回TPKT信息和数据包.根据length-4求得data.就是Payload的数据.(解封).

internal class TPKT
    {
        public byte Version;
        public byte Reserved1;
        public int Length;
        public byte[] Data;

        /// <summary>
        /// Reads a TPKT from the socket
        /// </summary>
        /// <param name="stream">The stream to read from</param>
        /// <returns>TPKT Instance</returns>
        public static TPKT Read(Stream stream)
        {
            var buf = new byte[4];
            int len = stream.Read(buf, 0, 4);
            if (len < 4) throw new TPKTInvalidException("TPKT is incomplete / invalid");
            var pkt = new TPKT
            {
                Version = buf[0],
                Reserved1 = buf[1],
                Length = buf[2] * 256 + buf[3] //BigEndian
            };
            if (pkt.Length > 0)
            {
                pkt.Data = new byte[pkt.Length - 4];
                len = stream.Read(pkt.Data, 0, pkt.Length - 4);
                if (len < pkt.Length - 4)
                    throw new TPKTInvalidException("TPKT is incomplete / invalid");
            }
            return pkt;
        }

        /// <summary>
        /// Reads a TPKT from the socket Async
        /// </summary>
        /// <param name="stream">The stream to read from</param>
        /// <returns>Task TPKT Instace</returns>
        public static async Task<TPKT> ReadAsync(Stream stream)
        {
            var buf = new byte[4];
            int len = await stream.ReadAsync(buf, 0, 4);
            if (len < 4) throw new TPKTInvalidException("TPKT is incomplete / invalid");
            var pkt = new TPKT
            {
                Version = buf[0],
                Reserved1 = buf[1],
                Length = buf[2] * 256 + buf[3] //BigEndian
            };
            if (pkt.Length > 0)
            {
                pkt.Data = new byte[pkt.Length - 4];
                len = await stream.ReadAsync(pkt.Data, 0, pkt.Length - 4);
                if (len < pkt.Length - 4) throw new TPKTInvalidException("TPKT is incomplete / invalid");
            }
            return pkt;
        }

        public override string ToString()
        {
            return string.Format("Version: {0} Length: {1} Data: {2}",
                Version,
                Length,
                BitConverter.ToString(Data)
                );
        }
    }


2,TPDU-----Transport protocol data unit---一个基本数据传输单元(COTP)


第一个byte--标识为length---不包括本身.标识

第二给byte---标识为PDUType--分为以下几种


0x0d连接确认0x05拒绝
0x0e连接请求0xf0数据包
0x08断开请求
0x0c断开确认

当其为0xf0的时候:

第三个byte---flags:其位7标志是否为最后的TPDU,位0-6标识块NUMBER---TPDU NUMBER.


从stream---->TPKT---->TPDU---->再从TPDU中读取数据TSDU.(减去4个字节的TPKT包裹+headerLength).

 public class TPDU
        {
            public byte HeaderLength;
            public byte PDUType;
            public int TPDUNumber;
            public byte[] Data;
            public bool LastDataUnit;

            public TPDU(TPKT tPKT)
            {
                var br = new BinaryReader(new MemoryStream(tPKT.Data));
                  
                HeaderLength = br.ReadByte();
                if (HeaderLength >= 2)
                {
                    PDUType = br.ReadByte();
                    if (PDUType == 0xf0) //DT Data
                    {
                        var flags = br.ReadByte();
                        TPDUNumber = flags & 0x7F;
                        LastDataUnit = (flags & 0x80) > 0;
                        Data = br.ReadBytes(tPKT.Length - HeaderLength - 4); //4 = TPKT Size
                        return;
                    }
                    //TODO: Handle other PDUTypes
                }
                Data = new byte[0];
            }

            /// <summary>
            /// Reads COTP TPDU (Transport protocol data unit) from the network stream
            /// See: https://tools.ietf.org/html/rfc905
            /// </summary>
            /// <param name="stream">The socket to read from</param>
            /// <returns>COTP DPDU instance</returns>
            public static TPDU Read(Stream stream)
            {
                var tpkt = TPKT.Read(stream);
                if (tpkt.Length > 0) return new TPDU(tpkt);
                return null;
            }

            /// <summary>
            /// Reads COTP TPDU (Transport protocol data unit) from the network stream
            /// See: https://tools.ietf.org/html/rfc905
            /// </summary>
            /// <param name="stream">The socket to read from</param>
            /// <returns>COTP DPDU instance</returns>
            public static async Task<TPDU> ReadAsync(Stream stream)
            {
                var tpkt = await TPKT.ReadAsync(stream);
                if (tpkt.Length > 0) return new TPDU(tpkt);
                return null;
            }

            public override string ToString()
            {
                return string.Format("Length: {0} PDUType: {1} TPDUNumber: {2} Last: {3} Segment Data: {4}",
                    HeaderLength,
                    PDUType,
                    TPDUNumber,
                    LastDataUnit,
                    BitConverter.ToString(Data)
                    );
            }

        }

3,TSDU---服务数据单元---定义了将连续的TPDU转换为一个TSDU单元.

 public class TSDU
        {
            /// <summary>
            /// Reads the full COTP TSDU (Transport service data unit)
            /// See: https://tools.ietf.org/html/rfc905
            /// </summary>
            /// <param name="stream">The stream to read from</param>
            /// <returns>Data in TSDU</returns>
            public static byte[] Read(Stream stream)
            {
                var segment = TPDU.Read(stream);
                if (segment == null) return null;

                var output = new MemoryStream(segment.Data.Length);
                output.Write(segment.Data, 0, segment.Data.Length);

                while (!segment.LastDataUnit)
                {
                    segment = TPDU.Read(stream);
                    output.Write(segment.Data, (int)output.Position, segment.Data.Length);
                }
                return output.GetBuffer().Take((int)output.Position).ToArray();
            }

            /// <summary>
            /// Reads the full COTP TSDU (Transport service data unit)
            /// See: https://tools.ietf.org/html/rfc905
            /// </summary>
            /// <param name="stream">The stream to read from</param>
            /// <returns>Data in TSDU</returns>
            public static async Task<byte[]> ReadAsync(Stream stream)
            {
                var segment = await TPDU.ReadAsync(stream);
                if (segment == null) return null;

                var output = new MemoryStream(segment.Data.Length);
                output.Write(segment.Data, 0, segment.Data.Length);

                while (!segment.LastDataUnit)
                {
                    segment = await TPDU.ReadAsync(stream);
                    output.Write(segment.Data, (int)output.Position, segment.Data.Length);
                }
                return output.GetBuffer().Take((int)output.Position).ToArray();
            }
        }

免责声明:文章转载自《详解S7源码3-COTP and TPKT》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇dapper 多对多查询对象和对象列表C# 开源日志框架配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

centos7源码安装cloud-init

<template> <name>centos72-source</name> <os> <name>CentOS-7</name> <version>2</version> <arch>x86_64</arc...

ruby中rsa加密加签以及md5、hash加密以及des加解密方法整理

#RSA公钥加密,加签;des 不同加密算法的加密解密方法 # encoding:utf-8# author:anionrequire 'openssl'require 'base64'require 'digest' #一 rsa加签 base64编码  def rsa_sign data, key  rkey =OpenSSL::PKey::RSA.n...

数据挖掘:理论与算法(导论)

清华大学研究生公开课 数据挖掘是数据科学,是多领域交叉学科:数据挖掘 = 机器学习 + 人工智能 + 模式识别 + 统计学 数据挖掘的广泛应用: Business Intelligence Data Analytics Big Data Decision Support Customer Relationship Management "Educatio...

NGINX----源码阅读一(main函数)

1、ngx_debug_init(); 初始化debug函数,一般为空。 2、ngx_strerror_init(); 将系统错误码+错误信息,以ngx_str_t数组保存。 3、ngx_get_options(int argc, char *const *argv) nginx启动函数选项, 4、ngx_show_version_info(); 如果上一...

快速入门vue-render函数

render 函数,大部分工老油条,应该是比较了解了,但是可能有些初出茅庐的小年轻们,不是很了解,并且严老湿也去网上查阅了一些相关的文章,总结了一下,不够系统,所以今天简单聊一下,循环渐进 render 函数是什么 ​ 平常我们写 <template> 里面所使用模板HTML语法组建页面的,其实在 vue 中都会编译成 render 函数,因为...

opendpi 源码分析(二)

先介绍opendpi如何设置各个协议的处理,然后介绍如何处理每一个包,最后是软件的流程图。 第一:首先看下要用到的重要的宏 View Code #define IPOQUE_SAVE_AS_BITMASK(bitmask,value) (bitmask)=(((IPOQUE_PROTOCOL_BITMASK)1)<<(value)) #de...