NetConfClient.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 partial class NetConfClient : BaseClient
  13. {
  14. /// <summary>
  15. /// Holds <see cref="NetConfSession"/> instance that used to communicate to the server
  16. /// </summary>
  17. private NetConfSession _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.DEFAULT_PORT, 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.DEFAULT_PORT, 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. : base(connectionInfo, ownsConnectionInfo)
  102. {
  103. this.OperationTimeout = new TimeSpan(0, 0, 0, 0, -1);
  104. this.AutomaticMessageIdHandling = true;
  105. }
  106. #endregion
  107. /// <summary>
  108. /// Gets NetConf server capabilities.
  109. /// </summary>
  110. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  111. public XmlDocument ServerCapabilities
  112. {
  113. get { return this._netConfSession.ServerCapabilities; }
  114. }
  115. /// <summary>
  116. /// Gets NetConf client capabilities.
  117. /// </summary>
  118. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  119. public XmlDocument ClientCapabilities
  120. {
  121. get { return this._netConfSession.ClientCapabilities; }
  122. }
  123. /// <summary>
  124. /// Gets or sets a value indicating whether automatic message id handling is
  125. /// enabled.
  126. /// </summary>
  127. /// <value>
  128. /// <c>true</c> if automatic message id handling is enabled; otherwise, <c>false</c>.
  129. /// The default value is <c>true</c>.
  130. /// </value>
  131. public bool AutomaticMessageIdHandling { get; set; }
  132. /// <summary>
  133. /// Sends the receive RPC.
  134. /// </summary>
  135. /// <param name="rpc">The RPC.</param>
  136. /// <returns>Reply message to RPC request</returns>
  137. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  138. public XmlDocument SendReceiveRpc(XmlDocument rpc)
  139. {
  140. return this._netConfSession.SendReceiveRpc(rpc, this.AutomaticMessageIdHandling);
  141. }
  142. /// <summary>
  143. /// Sends the receive RPC.
  144. /// </summary>
  145. /// <param name="xml">The XML.</param>
  146. /// <returns>Reply message to RPC request</returns>
  147. public XmlDocument SendReceiveRpc(string xml)
  148. {
  149. var rpc = new XmlDocument();
  150. rpc.LoadXml(xml);
  151. return SendReceiveRpc(rpc);
  152. }
  153. /// <summary>
  154. /// Sends the close RPC.
  155. /// </summary>
  156. /// <returns>Reply message to closing RPC request</returns>
  157. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  158. public XmlDocument SendCloseRpc()
  159. {
  160. var rpc = new XmlDocument();
  161. 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>");
  162. return this._netConfSession.SendReceiveRpc(rpc, this.AutomaticMessageIdHandling);
  163. }
  164. /// <summary>
  165. /// Called when client is connected to the server.
  166. /// </summary>
  167. protected override void OnConnected()
  168. {
  169. base.OnConnected();
  170. this._netConfSession = new NetConfSession(this.Session, this.OperationTimeout);
  171. this._netConfSession.Connect();
  172. }
  173. /// <summary>
  174. /// Called when client is disconnecting from the server.
  175. /// </summary>
  176. protected override void OnDisconnecting()
  177. {
  178. base.OnDisconnecting();
  179. this._netConfSession.Disconnect();
  180. }
  181. /// <summary>
  182. /// Releases unmanaged and - optionally - managed resources
  183. /// </summary>
  184. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  185. protected override void Dispose(bool disposing)
  186. {
  187. if (this._netConfSession != null)
  188. {
  189. this._netConfSession.Dispose();
  190. this._netConfSession = null;
  191. }
  192. base.Dispose(disposing);
  193. }
  194. }
  195. }