SessionServicePacket.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System.IO;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Netbios
  20. {
  21. public abstract class SessionServicePacket
  22. {
  23. internal const int SessionMessage = unchecked(0x00);
  24. internal const int SessionRequest = unchecked(0x81);
  25. public const int PositiveSessionResponse = unchecked(0x82);
  26. public const int NegativeSessionResponse = unchecked(0x83);
  27. internal const int SessionRetargetResponse = unchecked(0x84);
  28. internal const int SessionKeepAlive = unchecked(0x85);
  29. internal const int MaxMessageSize = unchecked(0x0001FFFF);
  30. internal const int HeaderLength = 4;
  31. // session service packet types
  32. internal static void WriteInt2(int val, byte[] dst, int dstIndex)
  33. {
  34. dst[dstIndex++] = unchecked((byte)((val >> 8) & unchecked(0xFF)));
  35. dst[dstIndex] = unchecked((byte)(val & unchecked(0xFF)));
  36. }
  37. internal static void WriteInt4(int val, byte[] dst, int dstIndex)
  38. {
  39. dst[dstIndex++] = unchecked((byte)((val >> 24) & unchecked(0xFF)));
  40. dst[dstIndex++] = unchecked((byte)((val >> 16) & unchecked(0xFF)));
  41. dst[dstIndex++] = unchecked((byte)((val >> 8) & unchecked(0xFF)));
  42. dst[dstIndex] = unchecked((byte)(val & unchecked(0xFF)));
  43. }
  44. internal static int ReadInt2(byte[] src, int srcIndex)
  45. {
  46. return ((src[srcIndex] & unchecked(0xFF)) << 8) + (src[srcIndex + 1] & unchecked(
  47. 0xFF));
  48. }
  49. internal static int ReadInt4(byte[] src, int srcIndex)
  50. {
  51. return ((src[srcIndex] & unchecked(0xFF)) << 24) + ((src[srcIndex + 1] & unchecked(
  52. 0xFF)) << 16) + ((src[srcIndex + 2] & unchecked(0xFF)) << 8) + (src
  53. [srcIndex + 3] & unchecked(0xFF));
  54. }
  55. internal static int ReadLength(byte[] src, int srcIndex)
  56. {
  57. srcIndex++;
  58. return ((src[srcIndex++] & unchecked(0x01)) << 16) + ((src[srcIndex++] & unchecked(
  59. 0xFF)) << 8) + (src[srcIndex++] & unchecked(0xFF));
  60. }
  61. /// <exception cref="System.IO.IOException"></exception>
  62. internal static int Readn(InputStream @in, byte[] b, int off, int len)
  63. {
  64. int i = 0;
  65. int n;
  66. while (i < len)
  67. {
  68. n = @in.Read(b, off + i, len - i);
  69. if (n <= 0)
  70. {
  71. break;
  72. }
  73. i += n;
  74. }
  75. return i;
  76. }
  77. /// <exception cref="System.IO.IOException"></exception>
  78. internal static int ReadPacketType(InputStream @in, byte[] buffer, int bufferIndex
  79. )
  80. {
  81. int n;
  82. if ((n = Readn(@in, buffer, bufferIndex, HeaderLength)) != HeaderLength)
  83. {
  84. if (n == -1)
  85. {
  86. return -1;
  87. }
  88. throw new IOException("unexpected EOF reading netbios session header");
  89. }
  90. int t = buffer[bufferIndex] & unchecked(0xFF);
  91. return t;
  92. }
  93. internal int Type;
  94. internal int Length;
  95. public virtual int WriteWireFormat(byte[] dst, int dstIndex)
  96. {
  97. Length = WriteTrailerWireFormat(dst, dstIndex + HeaderLength);
  98. WriteHeaderWireFormat(dst, dstIndex);
  99. return HeaderLength + Length;
  100. }
  101. /// <exception cref="System.IO.IOException"></exception>
  102. internal virtual int ReadWireFormat(InputStream @in, byte[] buffer, int bufferIndex
  103. )
  104. {
  105. ReadHeaderWireFormat(@in, buffer, bufferIndex);
  106. return HeaderLength + ReadTrailerWireFormat(@in, buffer, bufferIndex);
  107. }
  108. internal virtual int WriteHeaderWireFormat(byte[] dst, int dstIndex)
  109. {
  110. dst[dstIndex++] = unchecked((byte)Type);
  111. if (Length > unchecked(0x0000FFFF))
  112. {
  113. dst[dstIndex] = unchecked(unchecked(0x01));
  114. }
  115. dstIndex++;
  116. WriteInt2(Length, dst, dstIndex);
  117. return HeaderLength;
  118. }
  119. /// <exception cref="System.IO.IOException"></exception>
  120. internal virtual int ReadHeaderWireFormat(InputStream @in, byte[] buffer, int bufferIndex
  121. )
  122. {
  123. Type = buffer[bufferIndex++] & unchecked(0xFF);
  124. Length = ((buffer[bufferIndex] & unchecked(0x01)) << 16) + ReadInt2(buffer
  125. , bufferIndex + 1);
  126. return HeaderLength;
  127. }
  128. internal abstract int WriteTrailerWireFormat(byte[] dst, int dstIndex);
  129. /// <exception cref="System.IO.IOException"></exception>
  130. internal abstract int ReadTrailerWireFormat(InputStream @in, byte[] buffer, int bufferIndex
  131. );
  132. }
  133. }