NodeStatusResponse.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Netbios
  20. {
  21. internal class NodeStatusResponse : NameServicePacket
  22. {
  23. private NbtAddress _queryAddress;
  24. private int _numberOfNames;
  25. private byte[] _macAddress;
  26. private byte[] _stats;
  27. internal NbtAddress[] AddressArray;
  28. internal NodeStatusResponse(NbtAddress queryAddress)
  29. {
  30. this._queryAddress = queryAddress;
  31. RecordName = new Name();
  32. _macAddress = new byte[6];
  33. }
  34. internal override int WriteBodyWireFormat(byte[] dst, int dstIndex)
  35. {
  36. return 0;
  37. }
  38. internal override int ReadBodyWireFormat(byte[] src, int srcIndex)
  39. {
  40. return ReadResourceRecordWireFormat(src, srcIndex);
  41. }
  42. internal override int WriteRDataWireFormat(byte[] dst, int dstIndex)
  43. {
  44. return 0;
  45. }
  46. internal override int ReadRDataWireFormat(byte[] src, int srcIndex)
  47. {
  48. int start = srcIndex;
  49. _numberOfNames = src[srcIndex] & unchecked(0xFF);
  50. int namesLength = _numberOfNames * 18;
  51. int statsLength = RDataLength - namesLength - 1;
  52. _numberOfNames = src[srcIndex++] & unchecked(0xFF);
  53. // gotta read the mac first so we can populate addressArray with it
  54. Array.Copy(src, srcIndex + namesLength, _macAddress, 0, 6);
  55. srcIndex += ReadNodeNameArray(src, srcIndex);
  56. _stats = new byte[statsLength];
  57. Array.Copy(src, srcIndex, _stats, 0, statsLength);
  58. srcIndex += statsLength;
  59. return srcIndex - start;
  60. }
  61. private int ReadNodeNameArray(byte[] src, int srcIndex)
  62. {
  63. int start = srcIndex;
  64. AddressArray = new NbtAddress[_numberOfNames];
  65. string n;
  66. int hexCode;
  67. string scope = _queryAddress.HostName.Scope;
  68. bool groupName;
  69. int ownerNodeType;
  70. bool isBeingDeleted;
  71. bool isInConflict;
  72. bool isActive;
  73. bool isPermanent;
  74. int j;
  75. bool addrFound = false;
  76. try
  77. {
  78. for (int i = 0; i < _numberOfNames; srcIndex += 18, i++)
  79. {
  80. for (j = srcIndex + 14; src[j] == unchecked(0x20); j--)
  81. {
  82. }
  83. n = Runtime.GetStringForBytes(src, srcIndex, j - srcIndex + 1, Name.OemEncoding
  84. );
  85. hexCode = src[srcIndex + 15] & unchecked(0xFF);
  86. groupName = ((src[srcIndex + 16] & unchecked(0x80)) == unchecked(0x80)) ? true : false;
  87. ownerNodeType = (src[srcIndex + 16] & unchecked(0x60)) >> 5;
  88. isBeingDeleted = ((src[srcIndex + 16] & unchecked(0x10)) == unchecked(0x10)) ? true : false;
  89. isInConflict = ((src[srcIndex + 16] & unchecked(0x08)) == unchecked(0x08)) ? true : false;
  90. isActive = ((src[srcIndex + 16] & unchecked(0x04)) == unchecked(0x04)) ? true : false;
  91. isPermanent = ((src[srcIndex + 16] & unchecked(0x02)) == unchecked(0x02)) ? true : false;
  92. if (!addrFound && _queryAddress.HostName.HexCode == hexCode && (_queryAddress.HostName
  93. == NbtAddress.UnknownName || _queryAddress.HostName.name.Equals(n)))
  94. {
  95. if (_queryAddress.HostName == NbtAddress.UnknownName)
  96. {
  97. _queryAddress.HostName = new Name(n, hexCode, scope);
  98. }
  99. _queryAddress.GroupName = groupName;
  100. _queryAddress.NodeType = ownerNodeType;
  101. _queryAddress.isBeingDeleted = isBeingDeleted;
  102. _queryAddress.isInConflict = isInConflict;
  103. _queryAddress.isActive = isActive;
  104. _queryAddress.isPermanent = isPermanent;
  105. _queryAddress.MacAddress = _macAddress;
  106. _queryAddress.IsDataFromNodeStatus = true;
  107. addrFound = true;
  108. AddressArray[i] = _queryAddress;
  109. }
  110. else
  111. {
  112. AddressArray[i] = new NbtAddress(new Name(n, hexCode, scope), _queryAddress.Address
  113. , groupName, ownerNodeType, isBeingDeleted, isInConflict, isActive, isPermanent,
  114. _macAddress);
  115. }
  116. }
  117. }
  118. catch (UnsupportedEncodingException)
  119. {
  120. }
  121. return srcIndex - start;
  122. }
  123. public override string ToString()
  124. {
  125. return "NodeStatusResponse[" + base.ToString() + "]";
  126. }
  127. }
  128. }