SessionServicePacket.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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)
  47. + (src[srcIndex + 1] & unchecked(0xFF));
  48. }
  49. internal static int ReadInt4(byte[] src, int srcIndex)
  50. {
  51. return ((src[srcIndex] & unchecked(0xFF)) << 24)
  52. + ((src[srcIndex + 1] & unchecked(0xFF)) << 16)
  53. + ((src[srcIndex + 2] & unchecked(0xFF)) << 8)
  54. + (src[srcIndex + 3] & unchecked(0xFF));
  55. }
  56. internal static int ReadLength(byte[] src, int srcIndex)
  57. {
  58. srcIndex++;
  59. return ((src[srcIndex++] & unchecked(0x01)) << 16)
  60. + ((src[srcIndex++] & unchecked(0xFF)) << 8)
  61. + (src[srcIndex++] & unchecked(0xFF));
  62. }
  63. /// <exception cref="System.IO.IOException"></exception>
  64. internal static int Readn(InputStream @in, byte[] b, int off, int len)
  65. {
  66. int i = 0;
  67. int n;
  68. while (i < len)
  69. {
  70. n = @in.Read(b, off + i, len - i);
  71. if (n <= 0)
  72. {
  73. break;
  74. }
  75. i += n;
  76. }
  77. return i;
  78. }
  79. /// <exception cref="System.IO.IOException"></exception>
  80. internal static int ReadPacketType(InputStream @in, byte[] buffer, int bufferIndex)
  81. {
  82. int n;
  83. if ((n = Readn(@in, buffer, bufferIndex, HeaderLength)) != HeaderLength)
  84. {
  85. if (n == -1)
  86. {
  87. return -1;
  88. }
  89. throw new IOException("unexpected EOF reading netbios session header");
  90. }
  91. int t = buffer[bufferIndex] & unchecked(0xFF);
  92. return t;
  93. }
  94. internal int Type;
  95. internal int Length;
  96. public virtual int WriteWireFormat(byte[] dst, int dstIndex)
  97. {
  98. Length = WriteTrailerWireFormat(dst, dstIndex + HeaderLength);
  99. WriteHeaderWireFormat(dst, dstIndex);
  100. return HeaderLength + Length;
  101. }
  102. /// <exception cref="System.IO.IOException"></exception>
  103. internal virtual int ReadWireFormat(InputStream @in, byte[] buffer, int bufferIndex)
  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. Type = buffer[bufferIndex++] & unchecked(0xFF);
  123. Length = ((buffer[bufferIndex] & unchecked(0x01)) << 16)
  124. + ReadInt2(buffer, bufferIndex + 1);
  125. return HeaderLength;
  126. }
  127. internal abstract int WriteTrailerWireFormat(byte[] dst, int dstIndex);
  128. /// <exception cref="System.IO.IOException"></exception>
  129. internal abstract int ReadTrailerWireFormat(InputStream @in, byte[] buffer, int bufferIndex);
  130. }
  131. }