2
0

NetConfClient.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. public TimeSpan OperationTimeout {
  28. get { return TimeSpan.FromMilliseconds(_operationTimeout); }
  29. set
  30. {
  31. var timeoutInMilliseconds = value.TotalMilliseconds;
  32. if (timeoutInMilliseconds < -1d || timeoutInMilliseconds > int.MaxValue)
  33. throw new ArgumentOutOfRangeException("timeout", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.");
  34. _operationTimeout = (int) timeoutInMilliseconds;
  35. }
  36. }
  37. #region Constructors
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  40. /// </summary>
  41. /// <param name="connectionInfo">The connection info.</param>
  42. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  43. public NetConfClient(ConnectionInfo connectionInfo)
  44. : this(connectionInfo, false)
  45. {
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  49. /// </summary>
  50. /// <param name="host">Connection host.</param>
  51. /// <param name="port">Connection port.</param>
  52. /// <param name="username">Authentication username.</param>
  53. /// <param name="password">Authentication password.</param>
  54. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <c>null</c>.</exception>
  55. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  56. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
  57. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  58. public NetConfClient(string host, int port, string username, string password)
  59. : this(new PasswordConnectionInfo(host, port, username, password), true)
  60. {
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  64. /// </summary>
  65. /// <param name="host">Connection host.</param>
  66. /// <param name="username">Authentication username.</param>
  67. /// <param name="password">Authentication password.</param>
  68. /// <exception cref="ArgumentNullException"><paramref name="password"/> is <c>null</c>.</exception>
  69. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  70. public NetConfClient(string host, string username, string password)
  71. : this(host, ConnectionInfo.DefaultPort, username, password)
  72. {
  73. }
  74. /// <summary>
  75. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  76. /// </summary>
  77. /// <param name="host">Connection host.</param>
  78. /// <param name="port">Connection port.</param>
  79. /// <param name="username">Authentication username.</param>
  80. /// <param name="keyFiles">Authentication private key file(s) .</param>
  81. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
  82. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  83. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
  84. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  85. public NetConfClient(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  86. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), true)
  87. {
  88. }
  89. /// <summary>
  90. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  91. /// </summary>
  92. /// <param name="host">Connection host.</param>
  93. /// <param name="username">Authentication username.</param>
  94. /// <param name="keyFiles">Authentication private key file(s) .</param>
  95. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <c>null</c>.</exception>
  96. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <c>null</c> or contains only whitespace characters.</exception>
  97. public NetConfClient(string host, string username, params PrivateKeyFile[] keyFiles)
  98. : this(host, ConnectionInfo.DefaultPort, username, keyFiles)
  99. {
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="NetConfClient"/> class.
  103. /// </summary>
  104. /// <param name="connectionInfo">The connection info.</param>
  105. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  106. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  107. /// <remarks>
  108. /// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
  109. /// connection info will be disposed when this instance is disposed.
  110. /// </remarks>
  111. private NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
  112. : this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
  113. {
  114. }
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="NetConfClient"/> class.
  117. /// </summary>
  118. /// <param name="connectionInfo">The connection info.</param>
  119. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  120. /// <param name="serviceFactory">The factory to use for creating new services.</param>
  121. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  122. /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <c>null</c>.</exception>
  123. /// <remarks>
  124. /// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
  125. /// connection info will be disposed when this instance is disposed.
  126. /// </remarks>
  127. internal NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
  128. : base(connectionInfo, ownsConnectionInfo, serviceFactory)
  129. {
  130. _operationTimeout = SshNet.Session.Infinite;
  131. AutomaticMessageIdHandling = true;
  132. }
  133. #endregion
  134. /// <summary>
  135. /// Gets the NetConf server capabilities.
  136. /// </summary>
  137. /// <value>
  138. /// The NetConf server capabilities.
  139. /// </value>
  140. public XmlDocument ServerCapabilities
  141. {
  142. get { return _netConfSession.ServerCapabilities; }
  143. }
  144. /// <summary>
  145. /// Gets the NetConf client capabilities.
  146. /// </summary>
  147. /// <value>
  148. /// The NetConf client capabilities.
  149. /// </value>
  150. public XmlDocument ClientCapabilities
  151. {
  152. get { return _netConfSession.ClientCapabilities; }
  153. }
  154. /// <summary>
  155. /// Gets or sets a value indicating whether automatic message id handling is
  156. /// enabled.
  157. /// </summary>
  158. /// <value>
  159. /// <c>true</c> if automatic message id handling is enabled; otherwise, <c>false</c>.
  160. /// The default value is <c>true</c>.
  161. /// </value>
  162. public bool AutomaticMessageIdHandling { get; set; }
  163. /// <summary>
  164. /// Sends the receive RPC.
  165. /// </summary>
  166. /// <param name="rpc">The RPC.</param>
  167. /// <returns>Reply message to RPC request</returns>
  168. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  169. public XmlDocument SendReceiveRpc(XmlDocument rpc)
  170. {
  171. return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
  172. }
  173. /// <summary>
  174. /// Sends the receive RPC.
  175. /// </summary>
  176. /// <param name="xml">The XML.</param>
  177. /// <returns>Reply message to RPC request</returns>
  178. public XmlDocument SendReceiveRpc(string xml)
  179. {
  180. var rpc = new XmlDocument();
  181. rpc.LoadXml(xml);
  182. return SendReceiveRpc(rpc);
  183. }
  184. /// <summary>
  185. /// Sends the close RPC.
  186. /// </summary>
  187. /// <returns>Reply message to closing RPC request</returns>
  188. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  189. public XmlDocument SendCloseRpc()
  190. {
  191. var rpc = new XmlDocument();
  192. 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>");
  193. return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
  194. }
  195. /// <summary>
  196. /// Called when client is connected to the server.
  197. /// </summary>
  198. protected override void OnConnected()
  199. {
  200. base.OnConnected();
  201. _netConfSession = ServiceFactory.CreateNetConfSession(Session, _operationTimeout);
  202. _netConfSession.Connect();
  203. }
  204. /// <summary>
  205. /// Called when client is disconnecting from the server.
  206. /// </summary>
  207. protected override void OnDisconnecting()
  208. {
  209. base.OnDisconnecting();
  210. _netConfSession.Disconnect();
  211. }
  212. /// <summary>
  213. /// Releases unmanaged and - optionally - managed resources
  214. /// </summary>
  215. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  216. protected override void Dispose(bool disposing)
  217. {
  218. base.Dispose(disposing);
  219. if (disposing)
  220. {
  221. if (_netConfSession != null)
  222. {
  223. _netConfSession.Dispose();
  224. _netConfSession = null;
  225. }
  226. }
  227. }
  228. }
  229. }