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