NetConfClient.cs 11 KB

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