NetConfClient.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using Renci.SshNet.Sftp;
  6. using System.Text;
  7. using Renci.SshNet.Common;
  8. using System.Globalization;
  9. using System.Threading;
  10. using Renci.SshNet.NetConf;
  11. using System.Xml;
  12. using System.Diagnostics.CodeAnalysis;
  13. namespace Renci.SshNet
  14. {
  15. // TODO: Please help with documentation here, as I don't know the details, specially for the methods not documented.
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public partial class NetConfClient : BaseClient
  20. {
  21. /// <summary>
  22. /// Holds SftpSession instance that used to communicate to the SFTP server
  23. /// </summary>
  24. private NetConfSession _netConfSession;
  25. private bool _disposeConnectionInfo;
  26. /// <summary>
  27. /// Gets or sets the operation timeout.
  28. /// </summary>
  29. /// <value>The operation timeout.</value>
  30. public TimeSpan OperationTimeout { get; set; }
  31. #region Constructors
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  34. /// </summary>
  35. /// <param name="connectionInfo">The connection info.</param>
  36. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  37. public NetConfClient(ConnectionInfo connectionInfo)
  38. : base(connectionInfo)
  39. {
  40. this.AutomaticMessageIdHandling = true;
  41. this.OperationTimeout = new TimeSpan(0, 0, 0, 0, -1);
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  45. /// </summary>
  46. /// <param name="host">Connection host.</param>
  47. /// <param name="port">Connection port.</param>
  48. /// <param name="username">Authentication username.</param>
  49. /// <param name="password">Authentication password.</param>
  50. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  51. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  52. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  53. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  54. public NetConfClient(string host, int port, string username, string password)
  55. : this(new PasswordConnectionInfo(host, port, username, password))
  56. {
  57. this._disposeConnectionInfo = true;
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  61. /// </summary>
  62. /// <param name="host">Connection host.</param>
  63. /// <param name="username">Authentication username.</param>
  64. /// <param name="password">Authentication password.</param>
  65. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  66. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  67. public NetConfClient(string host, string username, string password)
  68. : this(host, 22, username, password)
  69. {
  70. }
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  73. /// </summary>
  74. /// <param name="host">Connection host.</param>
  75. /// <param name="port">Connection port.</param>
  76. /// <param name="username">Authentication username.</param>
  77. /// <param name="keyFiles">Authentication private key file(s) .</param>
  78. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is null.</exception>
  79. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is null or contains whitespace characters.</exception>
  80. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  81. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  82. public NetConfClient(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  83. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles))
  84. {
  85. this._disposeConnectionInfo = true;
  86. }
  87. /// <summary>
  88. /// Initializes a new instance of the <see cref="SftpClient"/> class.
  89. /// </summary>
  90. /// <param name="host">Connection host.</param>
  91. /// <param name="username">Authentication username.</param>
  92. /// <param name="keyFiles">Authentication private key file(s) .</param>
  93. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is null.</exception>
  94. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is null or contains whitespace characters.</exception>
  95. public NetConfClient(string host, string username, params PrivateKeyFile[] keyFiles)
  96. : this(host, 22, username, keyFiles)
  97. {
  98. }
  99. #endregion
  100. /// <summary>
  101. /// Gets NetConf server capabilities.
  102. /// </summary>
  103. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  104. public XmlDocument ServerCapabilities
  105. {
  106. get
  107. {
  108. this.EnsureConnection();
  109. return this._netConfSession.ServerCapabilities;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets NetConf client capabilities.
  114. /// </summary>
  115. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  116. public XmlDocument ClientCapabilities
  117. {
  118. get
  119. {
  120. this.EnsureConnection();
  121. return this._netConfSession.ClientCapabilities;
  122. }
  123. }
  124. public bool AutomaticMessageIdHandling { get; set; }
  125. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  126. public XmlDocument SendReceiveRpc(XmlDocument rpc)
  127. {
  128. this.EnsureConnection();
  129. return this._netConfSession.SendReceiveRpc(rpc, this.AutomaticMessageIdHandling);
  130. }
  131. public XmlDocument SendReceiveRpc(string xml)
  132. {
  133. var rpc = new XmlDocument();
  134. rpc.LoadXml(xml);
  135. return SendReceiveRpc(rpc);
  136. }
  137. public XmlDocument SendCloseRpc()
  138. {
  139. this.EnsureConnection();
  140. XmlDocument rpc = new XmlDocument();
  141. 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>");
  142. return this._netConfSession.SendReceiveRpc(rpc, this.AutomaticMessageIdHandling);
  143. }
  144. /// <summary>
  145. /// Called when client is connected to the server.
  146. /// </summary>
  147. protected override void OnConnected()
  148. {
  149. base.OnConnected();
  150. this._netConfSession = new NetConfSession(this.Session, this.OperationTimeout);
  151. this._netConfSession.Connect();
  152. }
  153. /// <summary>
  154. /// Called when client is disconnecting from the server.
  155. /// </summary>
  156. protected override void OnDisconnecting()
  157. {
  158. base.OnDisconnecting();
  159. this._netConfSession.Disconnect();
  160. }
  161. /// <summary>
  162. /// Releases unmanaged and - optionally - managed resources
  163. /// </summary>
  164. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  165. protected override void Dispose(bool disposing)
  166. {
  167. if (this._netConfSession != null)
  168. {
  169. this._netConfSession.Dispose();
  170. this._netConfSession = null;
  171. }
  172. base.Dispose(disposing);
  173. if (this._disposeConnectionInfo)
  174. ((IDisposable)this.ConnectionInfo).Dispose();
  175. }
  176. }
  177. }