• 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/SetRequestMessage.cs
1
// SET 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
using System;
21
using System.Collections.Generic;
22
using System.Globalization;
23
using Lextm.SharpSnmpLib.Security;
24

25
namespace Lextm.SharpSnmpLib.Messaging
26
{
27
    /// <summary>
28
    /// SET request message.
29
    /// </summary>
30
    public sealed class SetRequestMessage : ISnmpMessage
31
    {
32
        private readonly byte[] _bytes;
33

34
        /// <summary>
35
        /// Creates a <see cref="SetRequestMessage"/> with all contents.
36
        /// </summary>
37
        /// <param name="requestId">The request id.</param>
38
        /// <param name="version">Protocol version</param>
39
        /// <param name="community">Community name</param>
40
        /// <param name="variables">Variables</param>
41
        public SetRequestMessage(int requestId, VersionCode version, OctetString community, IList<Variable> variables)
×
42
        {
43
            if (variables == null)
×
44
            {
45
                throw new ArgumentNullException(nameof(variables));
×
46
            }
47

48
            if (community == null)
×
49
            {
50
                throw new ArgumentNullException(nameof(community));
×
51
            }
52

53
            if (version == VersionCode.V3)
×
54
            {
55
                throw new ArgumentException("Only v1 and v2c are supported.", nameof(version));
×
56
            }
57

58
            Version = version;
×
59
            Header = Header.Empty;
×
60
            Parameters = SecurityParameters.Create(community);
×
61
            var pdu = new SetRequestPdu(
×
62
                requestId,
×
63
                variables);
×
64
            Scope = new Scope(pdu);
×
65
            Privacy = DefaultPrivacyProvider.DefaultPair;
×
66

67
            _bytes = this.PackMessage(null).ToBytes();
×
68
        }
×
69

70
        /// <summary>
71
        /// Initializes a new instance of the <see cref="SetRequestMessage"/> class.
72
        /// </summary>
73
        /// <param name="version">The version.</param>
74
        /// <param name="messageId">The message id.</param>
75
        /// <param name="requestId">The request id.</param>
76
        /// <param name="userName">Name of the user.</param>
77
        /// <param name="variables">The variables.</param>
78
        /// <param name="privacy">The privacy provider.</param>
79
        /// <param name="report">The report.</param>
80
        [Obsolete("Please use other overloading ones.")]
81
        public SetRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
82
            : this(version, messageId, requestId, userName, OctetString.Empty, variables, privacy, 0xFFE3, report)
×
83
        {
84
        }
×
85

86
        /// <summary>
87
        /// Initializes a new instance of the <see cref="SetRequestMessage"/> class.
88
        /// </summary>
89
        /// <param name="version">The version.</param>
90
        /// <param name="messageId">The message id.</param>
91
        /// <param name="requestId">The request id.</param>
92
        /// <param name="userName">Name of the user.</param>
93
        /// <param name="contextName">The context name.</param>
94
        /// <param name="variables">The variables.</param>
95
        /// <param name="privacy">The privacy provider.</param>
96
        /// <param name="maxMessageSize">Size of the max message.</param>
97
        /// <param name="report">The report.</param>
98
        public SetRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, OctetString contextName, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
×
99
        {
100
            if (variables == null)
×
101
            {
102
                throw new ArgumentNullException(nameof(variables));
×
103
            }
104

105
            if (userName == null)
×
106
            {
107
                throw new ArgumentNullException(nameof(userName));
×
108
            }
109

110
            if (contextName == null)
×
111
            {
112
                throw new ArgumentNullException(nameof(contextName));
×
113
            }
114

115
            if (version != VersionCode.V3)
×
116
            {
117
                throw new ArgumentException("Only v3 is supported.", nameof(version));
×
118
            }
119

120
            if (report == null)
×
121
            {
122
                throw new ArgumentNullException(nameof(report));
×
123
            }
124

125
            Version = version;
×
126
            Privacy = privacy ?? throw new ArgumentNullException(nameof(privacy));
×
127

128
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
×
129
            var parameters = report.Parameters;
×
130
            var authenticationProvider = Privacy.AuthenticationProvider;
×
131
            Parameters = new SecurityParameters(
×
132
                parameters.EngineId,
×
133
                parameters.EngineBoots,
×
134
                parameters.EngineTime,
×
135
                userName,
×
136
                authenticationProvider.CleanDigest,
×
137
                Privacy.Salt);
×
138
            var pdu = new SetRequestPdu(
×
139
                requestId,
×
140
                variables);
×
141
            var scope = report.Scope;
×
142
            var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
×
143
            if (contextEngineId == null)
×
144
            {
145
                throw new SnmpException("invalid REPORT message");
×
146
            }
147

148
            Scope = new Scope(contextEngineId, contextName, pdu);
×
149

150
            Privacy.ComputeHash(Version, Header, Parameters, Scope);
×
151
            _bytes = this.PackMessage(null).ToBytes();
×
152
        }
×
153

154
        /// <summary>
155
        /// Initializes a new instance of the <see cref="SetRequestMessage"/> class.
156
        /// </summary>
157
        /// <param name="version">The version.</param>
158
        /// <param name="messageId">The message id.</param>
159
        /// <param name="requestId">The request id.</param>
160
        /// <param name="userName">Name of the user.</param>
161
        /// <param name="variables">The variables.</param>
162
        /// <param name="privacy">The privacy provider.</param>
163
        /// <param name="maxMessageSize">Size of the max message.</param>
164
        /// <param name="report">The report.</param>
165
        [Obsolete("Please use other overloading ones.")]
166
        public SetRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
167
            : this(version, messageId, requestId, userName, OctetString.Empty, variables, privacy, maxMessageSize, report)
×
168
        {
169
        }
×
170

171
        internal SetRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[]? length)
×
172
        {
173
            Version = version;
×
174
            Header = header ?? throw new ArgumentNullException(nameof(header));
×
175
            Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
×
176
            Scope = scope ?? throw new ArgumentNullException(nameof(scope));
×
177
            Privacy = privacy ?? throw new ArgumentNullException(nameof(privacy));
×
178

179
            _bytes = this.PackMessage(length).ToBytes();
×
180
        }
×
181

182
        /// <summary>
183
        /// Gets the header.
184
        /// </summary>
185
        public Header Header { get; }
186

187
        /// <summary>
188
        /// Gets the privacy provider.
189
        /// </summary>
190
        /// <value>The privacy provider.</value>
191
        public IPrivacyProvider Privacy { get; }
192

193
        /// <summary>
194
        /// Returns a <see cref="string"/> that represents this <see cref="SetRequestMessage"/>.
195
        /// </summary>
196
        /// <returns></returns>
197
        public override string ToString()
198
        {
199
            return string.Format(CultureInfo.InvariantCulture, "SET request message: version: {0}; {1}; {2}", Version, Parameters.UserName, Scope.Pdu);
×
200
        }
201

202
        /// <summary>
203
        /// Converts to byte format.
204
        /// </summary>
205
        /// <returns></returns>
206
        public byte[] ToBytes()
207
        {
208
            return _bytes;
×
209
        }
210

211
        /// <summary>
212
        /// Gets the parameters.
213
        /// </summary>
214
        /// <value>The parameters.</value>
215
        public SecurityParameters Parameters { get; }
216

217
        /// <summary>
218
        /// Gets the scope.
219
        /// </summary>
220
        /// <value>The scope.</value>
221
        public Scope Scope { get; }
222

223
        /// <summary>
224
        /// Gets the version.
225
        /// </summary>
226
        /// <value>The version.</value>
227
        public VersionCode Version { get; }
228
    }
229
}
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

© 2026 Coveralls, Inc