NetConfClient.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.NetConf;
  4. using System.Xml;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Net;
  7. namespace Renci.SshNet
  8. {
  9. // TODO: Please help with documentation here, as I don't know the details, specially for the methods not documented.
  10. /// <summary>
  11. /// Contains operation for working with NetConf server.
  12. /// </summary>
  13. public class NetConfClient : BaseClient
  14. {
  15. private int _operationTimeout;
  16. /// <summary>
  17. /// Holds <see cref="INetConfSession"/> instance that used to communicate to the server
  18. /// </summary>
  19. private INetConfSession _netConfSession;
  20. /// <summary>
  21. /// Gets or sets the operation timeout.
  22. /// </summary>
  23. /// <value>
  24. /// The timeout to wait until an operation completes. The default value is negative
  25. /// one (-1) milliseconds, which indicates an infinite time-out period.
  26. /// </value>
  27. /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="Int32.MaxValue"/> milliseconds.</exception>
  28. public TimeSpan OperationTimeout {
  29. get { return TimeSpan.FromMilliseconds(_operationTimeout); }
  30. set
  31. {
  32. var timeoutInMilliseconds = value.TotalMilliseconds;
  33. if (timeoutInMilliseconds < -1d || timeoutInMilliseconds > int.MaxValue)
  34. throw new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.");
  35. _operationTimeout = (int) timeoutInMilliseconds;
  36. }
  37. }
  38. /// <summary>
  39. /// Gets the current NetConf session.
  40. /// </summary>
  41. /// <value>
  42. /// The current NetConf session.
  43. /// </value>
  44. internal INetConfSession NetConfSession
  45. {
  46. get { return _netConfSession; }
  47. }
  48. #region Constructors
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  51. /// </summary>
  52. /// <param name="connectionInfo">The connection info.</param>
  53. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  54. public NetConfClient(ConnectionInfo connectionInfo)
  55. : this(connectionInfo, false)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  60. /// </summary>
  61. /// <param name="host">Connection host.</param>
  62. /// <param name="port">Connection port.</param>
  63. /// <param name="username">Authentication username.</param>
  64. /// <param name="password">Authentication password.</param>
  65. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <c>null</c>.</exception>
  66. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  67. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
  68. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  69. public NetConfClient(string host, int port, string username, string password)
  70. : this(new PasswordConnectionInfo(host, port, username, password), true)
  71. {
  72. }
  73. /// <summary>
  74. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  75. /// </summary>
  76. /// <param name="host">Connection host.</param>
  77. /// <param name="username">Authentication username.</param>
  78. /// <param name="password">Authentication password.</param>
  79. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <c>null</c>.</exception>
  80. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  81. public NetConfClient(string host, string username, string password)
  82. : this(host, ConnectionInfo.DefaultPort, username, password)
  83. {
  84. }
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  87. /// </summary>
  88. /// <param name="host">Connection host.</param>
  89. /// <param name="port">Connection port.</param>
  90. /// <param name="username">Authentication username.</param>
  91. /// <param name="keyFiles">Authentication private key file(s) .</param>
  92. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
  93. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  94. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
  95. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  96. public NetConfClient(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  97. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), true)
  98. {
  99. }
  100. /// <summary>
  101. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  102. /// </summary>
  103. /// <param name="host">Connection host.</param>
  104. /// <param name="username">Authentication username.</param>
  105. /// <param name="keyFiles">Authentication private key file(s) .</param>
  106. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
  107. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  108. public NetConfClient(string host, string username, params PrivateKeyFile[] keyFiles)
  109. : this(host, ConnectionInfo.DefaultPort, username, keyFiles)
  110. {
  111. }
  112. /// <summary>
  113. /// Initializes a new instance of the <see cref="NetConfClient"/> class.
  114. /// </summary>
  115. /// <param name="connectionInfo">The connection info.</param>
  116. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  117. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  118. /// <remarks>
  119. /// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
  120. /// connection info will be disposed when this instance is disposed.
  121. /// </remarks>
  122. private NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
  123. : this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
  124. {
  125. }
  126. /// <summary>
  127. /// Initializes a new instance of the <see cref="NetConfClient"/> class.
  128. /// </summary>
  129. /// <param name="connectionInfo">The connection info.</param>
  130. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  131. /// <param name="serviceFactory">The factory to use for creating new services.</param>
  132. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  133. /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <c>null</c>.</exception>
  134. /// <remarks>
  135. /// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
  136. /// connection info will be disposed when this instance is disposed.
  137. /// </remarks>
  138. internal NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
  139. : base(connectionInfo, ownsConnectionInfo, serviceFactory)
  140. {
  141. _operationTimeout = SshNet.Session.Infinite;
  142. AutomaticMessageIdHandling = true;
  143. }
  144. #endregion
  145. /// <summary>
  146. /// Gets the NetConf server capabilities.
  147. /// </summary>
  148. /// <value>
  149. /// The NetConf server capabilities.
  150. /// </value>
  151. public XmlDocument ServerCapabilities
  152. {
  153. get { return _netConfSession.ServerCapabilities; }
  154. }
  155. /// <summary>
  156. /// Gets the NetConf client capabilities.
  157. /// </summary>
  158. /// <value>
  159. /// The NetConf client capabilities.
  160. /// </value>
  161. public XmlDocument ClientCapabilities
  162. {
  163. get { return _netConfSession.ClientCapabilities; }
  164. }
  165. /// <summary>
  166. /// Gets or sets a value indicating whether automatic message id handling is
  167. /// enabled.
  168. /// </summary>
  169. /// <value>
  170. /// <c>true</c> if automatic message id handling is enabled; otherwise, <c>false</c>.
  171. /// The default value is <c>true</c>.
  172. /// </value>
  173. public bool AutomaticMessageIdHandling { get; set; }
  174. /// <summary>
  175. /// Sends the receive RPC.
  176. /// </summary>
  177. /// <param name="rpc">The RPC.</param>
  178. /// <returns>Reply message to RPC request</returns>
  179. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  180. public XmlDocument SendReceiveRpc(XmlDocument rpc)
  181. {
  182. return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
  183. }
  184. /// <summary>
  185. /// Sends the receive RPC.
  186. /// </summary>
  187. /// <param name="xml">The XML.</param>
  188. /// <returns>Reply message to RPC request</returns>
  189. public XmlDocument SendReceiveRpc(string xml)
  190. {
  191. var rpc = new XmlDocument();
  192. rpc.LoadXml(xml);
  193. return SendReceiveRpc(rpc);
  194. }
  195. /// <summary>
  196. /// Sends the close RPC.
  197. /// </summary>
  198. /// <returns>Reply message to closing RPC request</returns>
  199. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  200. public XmlDocument SendCloseRpc()
  201. {
  202. var rpc = new XmlDocument();
  203. rpc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><rpc message-id=\"6666\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><close-session/></rpc>");
  204. return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
  205. }
  206. /// <summary>
  207. /// Called when client is connected to the server.
  208. /// </summary>
  209. protected override void OnConnected()
  210. {
  211. base.OnConnected();
  212. _netConfSession = CreateAndConnectNetConfSession();
  213. }
  214. /// <summary>
  215. /// Called when client is disconnecting from the server.
  216. /// </summary>
  217. protected override void OnDisconnecting()
  218. {
  219. base.OnDisconnecting();
  220. _netConfSession.Disconnect();
  221. }
  222. /// <summary>
  223. /// Releases unmanaged and - optionally - managed resources
  224. /// </summary>
  225. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  226. protected override void Dispose(bool disposing)
  227. {
  228. base.Dispose(disposing);
  229. if (disposing)
  230. {
  231. if (_netConfSession != null)
  232. {
  233. _netConfSession.Dispose();
  234. _netConfSession = null;
  235. }
  236. }
  237. }
  238. private INetConfSession CreateAndConnectNetConfSession()
  239. {
  240. var netConfSession = ServiceFactory.CreateNetConfSession(Session, _operationTimeout);
  241. try
  242. {
  243. netConfSession.Connect();
  244. return netConfSession;
  245. }
  246. catch
  247. {
  248. netConfSession.Dispose();
  249. throw;
  250. }
  251. }
  252. }
  253. }