• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lextudio / sharpsnmplib / 11823966615

13 Nov 2024 06:58PM UTC coverage: 49.626% (-3.4%) from 53.042%
11823966615

push

github

lextm
Removed .NET 6.

819 of 1881 branches covered (43.54%)

Branch coverage included in aggregate %.

2232 of 4267 relevant lines covered (52.31%)

15136.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/SharpSnmpLib/Messaging/GetBulkRequestMessage.cs
1
// GET BULK request message type.
2
// Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors.
3
// 
4
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
5
// software and associated documentation files (the "Software"), to deal in the Software
6
// without restriction, including without limitation the rights to use, copy, modify, merge,
7
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
8
// to whom the Software is furnished to do so, subject to the following conditions:
9
// 
10
// The above copyright notice and this permission notice shall be included in all copies or
11
// substantial portions of the Software.
12
// 
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
15
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
16
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
// DEALINGS IN THE SOFTWARE.
19

20
/*
21
 * Created by SharpDevelop.
22
 * User: lextm
23
 * Date: 2008/8/3
24
 * Time: 15:13
25
 * 
26
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
27
 */
28
using System;
29
using System.Collections.Generic;
30
using System.Globalization;
31
using Lextm.SharpSnmpLib.Security;
32

33
namespace Lextm.SharpSnmpLib.Messaging
34
{
35
    /// <summary>
36
    /// GETBULK request message.
37
    /// </summary>
38
    public sealed class GetBulkRequestMessage : ISnmpMessage
39
    {
40
        private readonly byte[] _bytes;
41

42
        /// <summary>
43
        /// Creates a <see cref="GetBulkRequestMessage"/> with all contents.
44
        /// </summary>
45
        /// <param name="requestId">The request ID.</param>
46
        /// <param name="version">Protocol version.</param>
47
        /// <param name="community">Community name.</param>
48
        /// <param name="nonRepeaters">Non-repeaters.</param>
49
        /// <param name="maxRepetitions">Max repetitions.</param>
50
        /// <param name="variables">Variables.</param>
51
        public GetBulkRequestMessage(int requestId, VersionCode version, OctetString community, int nonRepeaters, int maxRepetitions, IList<Variable> variables)
×
52
        {
53
            if (variables == null)
×
54
            {
55
                throw new ArgumentNullException(nameof(variables));
×
56
            }
57

58
            if (community == null)
×
59
            {
60
                throw new ArgumentNullException(nameof(community));
×
61
            }
62

63
            if (version != VersionCode.V2)
×
64
            {
65
                throw new ArgumentException("Only v2c are supported.", nameof(version));
×
66
            }
67

68
            if (nonRepeaters > variables.Count)
×
69
            {
70
                throw new ArgumentException("nonRepeaters should not be greater than variable count.", nameof(nonRepeaters));
×
71
            }
72

73
            if (maxRepetitions < 1)
×
74
            {
75
                throw new ArgumentException("maxRepetitions should be greater than 0.", nameof(maxRepetitions));
×
76
            }
77

78
            Version = version;
×
79
            Header = Header.Empty;
×
80
            Parameters = SecurityParameters.Create(community);
×
81
            var pdu = new GetBulkRequestPdu(
×
82
                requestId,
×
83
                nonRepeaters,
×
84
                maxRepetitions,
×
85
                variables);
×
86
            Scope = new Scope(pdu);
×
87
            Privacy = DefaultPrivacyProvider.DefaultPair;
×
88

89
            _bytes = this.PackMessage(null).ToBytes();
×
90
        }
×
91

92
        /// <summary>
93
        /// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
94
        /// </summary>
95
        /// <param name="version">The version.</param>
96
        /// <param name="messageId">The message id.</param>
97
        /// <param name="requestId">The request id.</param>
98
        /// <param name="userName">Name of the user.</param>
99
        /// <param name="nonRepeaters">The non repeaters.</param>
100
        /// <param name="maxRepetitions">The max repetitions.</param>
101
        /// <param name="variables">The variables.</param>
102
        /// <param name="privacy">The privacy provider.</param>
103
        /// <param name="report">The report.</param>
104
        [Obsolete("Please use other overloading ones.")]
105
        public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, int nonRepeaters, int maxRepetitions, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
106
            : this(version, messageId, requestId, userName, nonRepeaters, maxRepetitions, variables, privacy, 0xFFE3, report)
×
107
        {
108
        }
×
109

110
        /// <summary>
111
        /// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
112
        /// </summary>
113
        /// <param name="version">The version.</param>
114
        /// <param name="messageId">The message id.</param>
115
        /// <param name="requestId">The request id.</param>
116
        /// <param name="userName">Name of the user.</param>
117
        /// <param name="contextName">Context name.</param>
118
        /// <param name="nonRepeaters">The non repeaters.</param>
119
        /// <param name="maxRepetitions">The max repetitions.</param>
120
        /// <param name="variables">The variables.</param>
121
        /// <param name="privacy">The privacy provider.</param>
122
        /// <param name="maxMessageSize">Size of the max message.</param>
123
        /// <param name="report">The report.</param>
124
        public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, OctetString contextName, int nonRepeaters, int maxRepetitions, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
×
125
        {
126
            if (variables == null)
×
127
            {
128
                throw new ArgumentNullException(nameof(variables));
×
129
            }
130

131
            if (userName == null)
×
132
            {
133
                throw new ArgumentNullException(nameof(userName));
×
134
            }
135

136
            if (contextName == null)
×
137
            {
138
                throw new ArgumentNullException(nameof(contextName));
×
139
            }
140

141
            if (version != VersionCode.V3)
×
142
            {
143
                throw new ArgumentException("Only v3 is supported.", nameof(version));
×
144
            }
145

146
            if (report == null)
×
147
            {
148
                throw new ArgumentNullException(nameof(report));
×
149
            }
150

151
            if (nonRepeaters > variables.Count)
×
152
            {
153
                throw new ArgumentException("nonRepeaters should not be greater than variable count.", nameof(nonRepeaters));
×
154
            }
155

156
            if (maxRepetitions < 1)
×
157
            {
158
                throw new ArgumentException("maxRepetitions should be greater than 0.", nameof(maxRepetitions));
×
159
            }
160

161
            Version = version;
×
162
            Privacy = privacy ?? throw new ArgumentNullException(nameof(privacy));
×
163
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
×
164
            var parameters = report.Parameters;
×
165
            var authenticationProvider = Privacy.AuthenticationProvider;
×
166
            Parameters = new SecurityParameters(
×
167
                parameters.EngineId,
×
168
                parameters.EngineBoots,
×
169
                parameters.EngineTime,
×
170
                userName,
×
171
                authenticationProvider.CleanDigest,
×
172
                Privacy.Salt);
×
173
            var pdu = new GetBulkRequestPdu(
×
174
                requestId,
×
175
                nonRepeaters,
×
176
                maxRepetitions,
×
177
                variables);
×
178
            var scope = report.Scope;
×
179
            var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
×
180
            if (contextEngineId == null)
×
181
            {
182
                throw new SnmpException("invalid REPORT message");
×
183
            }
184

185
            Scope = new Scope(contextEngineId, contextName, pdu);
×
186

187
            Privacy.ComputeHash(Version, Header, Parameters, Scope);
×
188
            _bytes = this.PackMessage(null).ToBytes();
×
189
        }
×
190

191

192
        /// <summary>
193
        /// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
194
        /// </summary>
195
        /// <param name="version">The version.</param>
196
        /// <param name="messageId">The message id.</param>
197
        /// <param name="requestId">The request id.</param>
198
        /// <param name="userName">Name of the user.</param>
199
        /// <param name="nonRepeaters">The non repeaters.</param>
200
        /// <param name="maxRepetitions">The max repetitions.</param>
201
        /// <param name="variables">The variables.</param>
202
        /// <param name="privacy">The privacy provider.</param>
203
        /// <param name="maxMessageSize">Size of the max message.</param>
204
        /// <param name="report">The report.</param>
205
        [Obsolete("Please use other overloading ones.")]
206
        public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, int nonRepeaters, int maxRepetitions, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
207
            : this(version, messageId, requestId, userName, OctetString.Empty, nonRepeaters, maxRepetitions, variables, privacy, maxMessageSize, report)
×
208
        {
209
        }
×
210

211
        internal GetBulkRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[]? length)
×
212
        {
213
            Version = version;
×
214
            Header = header ?? throw new ArgumentNullException(nameof(header));
×
215
            Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
×
216
            Scope = scope ?? throw new ArgumentNullException(nameof(scope));
×
217
            Privacy = privacy ?? throw new ArgumentNullException(nameof(privacy));
×
218

219
            _bytes = this.PackMessage(length).ToBytes();
×
220
        }
×
221

222
        /// <summary>
223
        /// Gets the header.
224
        /// </summary>
225
        public Header Header { get; }
226

227
        /// <summary>
228
        /// Gets the privacy provider.
229
        /// </summary>
230
        /// <value>The privacy provider.</value>
231
        public IPrivacyProvider Privacy { get; }
232

233
        /// <summary>
234
        /// Converts to byte format.
235
        /// </summary>
236
        /// <returns></returns>
237
        public byte[] ToBytes()
238
        {
239
            return _bytes;
×
240
        }
241

242
        /// <summary>
243
        /// Gets the parameters.
244
        /// </summary>
245
        /// <value>The parameters.</value>
246
        public SecurityParameters Parameters { get; }
247

248
        /// <summary>
249
        /// Gets the scope.
250
        /// </summary>
251
        /// <value>The scope.</value>
252
        public Scope Scope { get; }
253

254
        /// <summary>
255
        /// Gets the version.
256
        /// </summary>
257
        /// <value>The version.</value>
258
        public VersionCode Version { get; }
259

260
        /// <summary>
261
        /// Returns a <see cref="string"/> that represents this <see cref="GetBulkRequestMessage"/>.
262
        /// </summary>
263
        /// <returns></returns>
264
        public override string ToString()
265
        {
266
            return string.Format(CultureInfo.InvariantCulture, "GET BULK request message: version: {0}; {1}; {2}", Version, Parameters.UserName, Scope.Pdu);
×
267
        }
268
    }
269
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc