DcerpcMessage.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 SharpCifs.Dcerpc.Ndr;
  18. namespace SharpCifs.Dcerpc
  19. {
  20. public abstract class DcerpcMessage : NdrObject
  21. {
  22. protected internal int Ptype = -1;
  23. protected internal int Flags;
  24. protected internal int Length;
  25. protected internal int CallId;
  26. protected internal int AllocHint;
  27. protected internal int Result;
  28. public virtual bool IsFlagSet(int flag)
  29. {
  30. return (Flags & flag) == flag;
  31. }
  32. public virtual void UnsetFlag(int flag)
  33. {
  34. Flags &= ~flag;
  35. }
  36. public virtual void SetFlag(int flag)
  37. {
  38. Flags |= flag;
  39. }
  40. public virtual DcerpcException GetResult()
  41. {
  42. if (Result != 0)
  43. {
  44. return new DcerpcException(Result);
  45. }
  46. return null;
  47. }
  48. internal virtual void Encode_header(NdrBuffer buf)
  49. {
  50. buf.Enc_ndr_small(5);
  51. buf.Enc_ndr_small(0);
  52. buf.Enc_ndr_small(Ptype);
  53. buf.Enc_ndr_small(Flags);
  54. buf.Enc_ndr_long(unchecked(0x00000010));
  55. buf.Enc_ndr_short(Length);
  56. buf.Enc_ndr_short(0);
  57. buf.Enc_ndr_long(CallId);
  58. }
  59. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  60. internal virtual void Decode_header(NdrBuffer buf)
  61. {
  62. if (buf.Dec_ndr_small() != 5 || buf.Dec_ndr_small() != 0)
  63. {
  64. throw new NdrException("DCERPC version not supported");
  65. }
  66. Ptype = buf.Dec_ndr_small();
  67. Flags = buf.Dec_ndr_small();
  68. if (buf.Dec_ndr_long() != unchecked(0x00000010))
  69. {
  70. throw new NdrException("Data representation not supported");
  71. }
  72. Length = buf.Dec_ndr_short();
  73. if (buf.Dec_ndr_short() != 0)
  74. {
  75. throw new NdrException("DCERPC authentication not supported");
  76. }
  77. CallId = buf.Dec_ndr_long();
  78. }
  79. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  80. public override void Encode(NdrBuffer buf)
  81. {
  82. int start = buf.GetIndex();
  83. int allocHintIndex = 0;
  84. buf.Advance(16);
  85. if (Ptype == 0)
  86. {
  87. allocHintIndex = buf.GetIndex();
  88. buf.Enc_ndr_long(0);
  89. buf.Enc_ndr_short(0);
  90. buf.Enc_ndr_short(GetOpnum());
  91. }
  92. Encode_in(buf);
  93. Length = buf.GetIndex() - start;
  94. if (Ptype == 0)
  95. {
  96. buf.SetIndex(allocHintIndex);
  97. AllocHint = Length - allocHintIndex;
  98. buf.Enc_ndr_long(AllocHint);
  99. }
  100. buf.SetIndex(start);
  101. Encode_header(buf);
  102. buf.SetIndex(start + Length);
  103. }
  104. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  105. public override void Decode(NdrBuffer buf)
  106. {
  107. Decode_header(buf);
  108. if (Ptype != 12 && Ptype != 2 && Ptype != 3 && Ptype != 13)
  109. {
  110. throw new NdrException("Unexpected ptype: " + Ptype);
  111. }
  112. if (Ptype == 2 || Ptype == 3)
  113. {
  114. AllocHint = buf.Dec_ndr_long();
  115. buf.Dec_ndr_short();
  116. buf.Dec_ndr_short();
  117. }
  118. if (Ptype == 3 || Ptype == 13)
  119. {
  120. Result = buf.Dec_ndr_long();
  121. }
  122. else
  123. {
  124. Decode_out(buf);
  125. }
  126. }
  127. public abstract int GetOpnum();
  128. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  129. public abstract void Encode_in(NdrBuffer dst);
  130. /// <exception cref="SharpCifs.Dcerpc.Ndr.NdrException"></exception>
  131. public abstract void Decode_out(NdrBuffer src);
  132. }
  133. }