SmbTree.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.Smb
  20. {
  21. class SmbTree
  22. {
  23. private static int _treeConnCounter;
  24. internal int ConnectionState;
  25. internal int Tid;
  26. internal string Share;
  27. internal string Service = "?????";
  28. internal string Service0;
  29. internal SmbSession Session;
  30. internal bool InDfs;
  31. internal bool InDomainDfs;
  32. internal int TreeNum;
  33. internal SmbTree(SmbSession session, string share, string service)
  34. {
  35. // used by SmbFile.isOpen
  36. this.Session = session;
  37. this.Share = share.ToUpper();
  38. if (service != null && service.StartsWith("??") == false)
  39. {
  40. this.Service = service;
  41. }
  42. Service0 = this.Service;
  43. ConnectionState = 0;
  44. }
  45. internal virtual bool Matches(string share, string service)
  46. {
  47. return Runtime.EqualsIgnoreCase(this.Share, share)
  48. && (service == null
  49. || service.StartsWith("??")
  50. || Runtime.EqualsIgnoreCase(this.Service, service));
  51. }
  52. public override bool Equals(object obj)
  53. {
  54. if (obj is SmbTree)
  55. {
  56. SmbTree tree = (SmbTree)obj;
  57. return Matches(tree.Share, tree.Service);
  58. }
  59. return false;
  60. }
  61. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  62. internal virtual void Send(ServerMessageBlock request, ServerMessageBlock response)
  63. {
  64. lock (Session.Transport())
  65. {
  66. if (response != null)
  67. {
  68. response.Received = false;
  69. }
  70. TreeConnect(request, response);
  71. if (request == null || (response != null && response.Received))
  72. {
  73. return;
  74. }
  75. if (Service.Equals("A:") == false)
  76. {
  77. switch (request.Command)
  78. {
  79. case ServerMessageBlock.SmbComOpenAndx:
  80. case ServerMessageBlock.SmbComNtCreateAndx:
  81. case ServerMessageBlock.SmbComReadAndx:
  82. case ServerMessageBlock.SmbComWriteAndx:
  83. case ServerMessageBlock.SmbComClose:
  84. case ServerMessageBlock.SmbComTreeDisconnect:
  85. {
  86. break;
  87. }
  88. case ServerMessageBlock.SmbComTransaction:
  89. case ServerMessageBlock.SmbComTransaction2:
  90. {
  91. switch (((SmbComTransaction)request).SubCommand
  92. & unchecked(0xFF))
  93. {
  94. case SmbComTransaction.NetShareEnum:
  95. case SmbComTransaction.NetServerEnum2:
  96. case SmbComTransaction.NetServerEnum3:
  97. case SmbComTransaction.TransPeekNamedPipe:
  98. case SmbComTransaction.TransWaitNamedPipe:
  99. case SmbComTransaction.TransCallNamedPipe:
  100. case SmbComTransaction.TransTransactNamedPipe:
  101. case SmbComTransaction.Trans2GetDfsReferral:
  102. {
  103. break;
  104. }
  105. default:
  106. {
  107. throw new SmbException(
  108. "Invalid operation for " + Service + " service");
  109. }
  110. }
  111. break;
  112. }
  113. default:
  114. {
  115. throw new SmbException(
  116. "Invalid operation for " + Service + " service" + request);
  117. }
  118. }
  119. }
  120. request.Tid = Tid;
  121. if (InDfs
  122. && !Service.Equals("IPC")
  123. && !string.IsNullOrEmpty(request.Path))
  124. {
  125. request.Flags2 = SmbConstants.Flags2ResolvePathsInDfs;
  126. request.Path = '\\' + Session.Transport().TconHostName
  127. + '\\' + Share + request.Path;
  128. }
  129. try
  130. {
  131. Session.Send(request, response);
  132. }
  133. catch (SmbException se)
  134. {
  135. if (se.GetNtStatus() == NtStatus.NtStatusNetworkNameDeleted)
  136. {
  137. TreeDisconnect(true);
  138. }
  139. throw;
  140. }
  141. }
  142. }
  143. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  144. internal virtual void TreeConnect(ServerMessageBlock andx,
  145. ServerMessageBlock andxResponse)
  146. {
  147. lock (Session.Transport())
  148. {
  149. string unc;
  150. while (ConnectionState != 0)
  151. {
  152. if (ConnectionState == 2 || ConnectionState == 3)
  153. {
  154. // connected or disconnecting
  155. return;
  156. }
  157. try
  158. {
  159. Runtime.Wait(Session.transport);
  160. }
  161. catch (Exception ie)
  162. {
  163. throw new SmbException(ie.Message, ie);
  164. }
  165. }
  166. ConnectionState = 1;
  167. // trying ...
  168. try
  169. {
  170. Session.transport.Connect();
  171. unc = "\\\\" + Session.transport.TconHostName + '\\' + Share;
  172. Service = Service0;
  173. if (Session.transport.Log.Level >= 4)
  174. {
  175. Session.transport.Log.WriteLine(
  176. "treeConnect: unc=" + unc
  177. + ",service=" + Service);
  178. }
  179. SmbComTreeConnectAndXResponse response
  180. = new SmbComTreeConnectAndXResponse(andxResponse);
  181. SmbComTreeConnectAndX request
  182. = new SmbComTreeConnectAndX(Session, unc, Service, andx);
  183. Session.Send(request, response);
  184. Tid = response.Tid;
  185. Service = response.Service;
  186. InDfs = response.ShareIsInDfs;
  187. TreeNum = _treeConnCounter++;
  188. ConnectionState = 2;
  189. }
  190. catch (SmbException se)
  191. {
  192. // connected
  193. TreeDisconnect(true);
  194. ConnectionState = 0;
  195. throw;
  196. }
  197. }
  198. }
  199. internal virtual void TreeDisconnect(bool inError)
  200. {
  201. lock (Session.Transport())
  202. {
  203. if (ConnectionState != 2)
  204. {
  205. // not-connected
  206. return;
  207. }
  208. ConnectionState = 3;
  209. // disconnecting
  210. if (!inError && Tid != 0)
  211. {
  212. try
  213. {
  214. Send(new SmbComTreeDisconnect(), null);
  215. }
  216. catch (SmbException se)
  217. {
  218. if (Session.transport.Log.Level > 1)
  219. {
  220. Runtime.PrintStackTrace(se, Session.transport.Log);
  221. }
  222. }
  223. }
  224. InDfs = false;
  225. InDomainDfs = false;
  226. ConnectionState = 0;
  227. Runtime.NotifyAll(Session.transport);
  228. }
  229. }
  230. public override string ToString()
  231. {
  232. return "SmbTree[share=" + Share
  233. + ",service=" + Service
  234. + ",tid=" + Tid
  235. + ",inDfs=" + InDfs
  236. + ",inDomainDfs=" + InDomainDfs
  237. + ",connectionState=" + ConnectionState + "]";
  238. }
  239. }
  240. }