COTP.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace S7.Net
  6. {
  7. /// <summary>
  8. /// COTP Protocol functions and types
  9. /// </summary>
  10. internal class COTP
  11. {
  12. public enum PduType : byte
  13. {
  14. Data = 0xf0,
  15. ConnectionConfirmed = 0xd0
  16. }
  17. /// <summary>
  18. /// Describes a COTP TPDU (Transport protocol data unit)
  19. /// </summary>
  20. public class TPDU
  21. {
  22. public TPKT TPkt { get; }
  23. public byte HeaderLength;
  24. public PduType PDUType;
  25. public int TPDUNumber;
  26. public byte[] Data;
  27. public bool LastDataUnit;
  28. public TPDU(TPKT tPKT)
  29. {
  30. TPkt = tPKT;
  31. HeaderLength = tPKT.Data[0]; // Header length excluding this length byte
  32. if (HeaderLength >= 2)
  33. {
  34. PDUType = (PduType)tPKT.Data[1];
  35. if (PDUType == PduType.Data) //DT Data
  36. {
  37. var flags = tPKT.Data[2];
  38. TPDUNumber = flags & 0x7F;
  39. LastDataUnit = (flags & 0x80) > 0;
  40. Data = new byte[tPKT.Data.Length - HeaderLength - 1]; // substract header length byte + header length.
  41. Array.Copy(tPKT.Data, HeaderLength + 1, Data, 0, Data.Length);
  42. return;
  43. }
  44. //TODO: Handle other PDUTypes
  45. }
  46. Data = new byte[0];
  47. }
  48. /// <summary>
  49. /// Reads COTP TPDU (Transport protocol data unit) from the network stream
  50. /// See: https://tools.ietf.org/html/rfc905
  51. /// </summary>
  52. /// <param name="stream">The socket to read from</param>
  53. /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
  54. /// <returns>COTP DPDU instance</returns>
  55. public static async Task<TPDU> ReadAsync(Stream stream, CancellationToken cancellationToken)
  56. {
  57. var tpkt = await TPKT.ReadAsync(stream, cancellationToken).ConfigureAwait(false);
  58. if (tpkt.Length == 0)
  59. {
  60. throw new TPDUInvalidException("No protocol data received");
  61. }
  62. return new TPDU(tpkt);
  63. }
  64. public override string ToString()
  65. {
  66. return string.Format("Length: {0} PDUType: {1} TPDUNumber: {2} Last: {3} Segment Data: {4}",
  67. HeaderLength,
  68. PDUType,
  69. TPDUNumber,
  70. LastDataUnit,
  71. BitConverter.ToString(Data)
  72. );
  73. }
  74. }
  75. /// <summary>
  76. /// Describes a COTP TSDU (Transport service data unit). One TSDU consist of 1 ore more TPDUs
  77. /// </summary>
  78. public class TSDU
  79. {
  80. /// <summary>
  81. /// Reads the full COTP TSDU (Transport service data unit)
  82. /// See: https://tools.ietf.org/html/rfc905
  83. /// </summary>
  84. /// <param name="stream">The stream to read from</param>
  85. /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
  86. /// <returns>Data in TSDU</returns>
  87. public static async Task<byte[]> ReadAsync(Stream stream, CancellationToken cancellationToken)
  88. {
  89. var segment = await TPDU.ReadAsync(stream, cancellationToken).ConfigureAwait(false);
  90. if (segment.LastDataUnit)
  91. {
  92. return segment.Data;
  93. }
  94. // More segments are expected, prepare a buffer to store all data
  95. var buffer = new byte[segment.Data.Length];
  96. Array.Copy(segment.Data, buffer, segment.Data.Length);
  97. while (!segment.LastDataUnit)
  98. {
  99. segment = await TPDU.ReadAsync(stream, cancellationToken).ConfigureAwait(false);
  100. var previousLength = buffer.Length;
  101. Array.Resize(ref buffer, buffer.Length + segment.Data.Length);
  102. Array.Copy(segment.Data, 0, buffer, previousLength, segment.Data.Length);
  103. }
  104. return buffer;
  105. }
  106. }
  107. }
  108. }