|
| 1 | +// This file is part of SNMP#NET. |
| 2 | +// |
| 3 | +// SNMP#NET is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU Lesser Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// SNMP#NET is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with SNMP#NET. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +// |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Text; |
| 19 | +using System.Net; |
| 20 | + |
| 21 | +namespace SnmpSharpNet |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// SNMP Agent specific values. |
| 25 | + /// </summary> |
| 26 | + /// <remarks> |
| 27 | + /// This class stores values to access SNMP version 1 and version 2 |
| 28 | + /// agents. |
| 29 | + /// |
| 30 | + /// Pass this class with your request data (Pdu) to the request method of the target class to make |
| 31 | + /// a request. |
| 32 | + /// </remarks> |
| 33 | + public class AgentParameters: IAgentParameters |
| 34 | + { |
| 35 | + /// <summary> |
| 36 | + /// Agent protocol version |
| 37 | + /// </summary> |
| 38 | + protected Integer32 _version; |
| 39 | + /// <summary> |
| 40 | + /// SNMP community name for SNMP v1 and v2 protocol versions |
| 41 | + /// </summary> |
| 42 | + protected OctetString _community; |
| 43 | + /// <summary> |
| 44 | + /// Flag that disables checking of host IP address and port number from which reply is received. If not disabled, only |
| 45 | + /// replies from the host IP/port to which request was sent will be considered valid and all others will be ignored. |
| 46 | + /// |
| 47 | + /// Default value is: false (reply source check is enabled) |
| 48 | + /// |
| 49 | + /// Set to true if you wish to disable this check. |
| 50 | + /// </summary> |
| 51 | + protected bool _disableReplySourceCheck; |
| 52 | + /// <summary> |
| 53 | + /// Standard constructor |
| 54 | + /// </summary> |
| 55 | + public AgentParameters() |
| 56 | + { |
| 57 | + _version = new Integer32((int)SnmpVersion.Ver1); |
| 58 | + _community = new OctetString("public"); |
| 59 | + _disableReplySourceCheck = false; |
| 60 | + } |
| 61 | + /// <summary> |
| 62 | + /// Copy constructor. Initialize the class with the values of the parameter class values. |
| 63 | + /// </summary> |
| 64 | + /// <param name="second">Parameter class.</param> |
| 65 | + public AgentParameters(AgentParameters second) |
| 66 | + { |
| 67 | + _version.Value = (int)second.Version; |
| 68 | + _community.Set(second.Community); |
| 69 | + _disableReplySourceCheck = second.DisableReplySourceCheck; |
| 70 | + } |
| 71 | + /// <summary> |
| 72 | + /// Constructor |
| 73 | + /// </summary> |
| 74 | + /// <param name="version">SNMP protocol version. Acceptable values are SnmpConstants.SNMPV1 and |
| 75 | + /// SnmpConstants.SNMPV2</param> |
| 76 | + public AgentParameters(SnmpVersion version) |
| 77 | + : this() |
| 78 | + { |
| 79 | + _version.Value = (int)version; |
| 80 | + } |
| 81 | + /// <summary> |
| 82 | + /// Constructor |
| 83 | + /// </summary> |
| 84 | + /// <param name="community">Agent SNMP community name</param> |
| 85 | + public AgentParameters(OctetString community) |
| 86 | + : this() |
| 87 | + { |
| 88 | + _community.Set(community); |
| 89 | + } |
| 90 | + /// <summary> |
| 91 | + /// Constructor |
| 92 | + /// </summary> |
| 93 | + /// <param name="version">SNMP Protocol version</param> |
| 94 | + /// <param name="community">SNMP community name</param> |
| 95 | + public AgentParameters(SnmpVersion version, OctetString community) |
| 96 | + : this(version) |
| 97 | + { |
| 98 | + _community.Set(community); |
| 99 | + } |
| 100 | + /// <summary> |
| 101 | + /// Constructor |
| 102 | + /// </summary> |
| 103 | + /// <param name="version">SNMP Protocol version</param> |
| 104 | + /// <param name="community">SNMP community name</param> |
| 105 | + /// <param name="disableReplySourceCheck">Should reply source IP address/port number be checked on reply reception</param> |
| 106 | + public AgentParameters(SnmpVersion version, OctetString community, bool disableReplySourceCheck) |
| 107 | + : this(version, community) |
| 108 | + { |
| 109 | + _disableReplySourceCheck = disableReplySourceCheck; |
| 110 | + } |
| 111 | + /// <summary> |
| 112 | + /// Get/Set SNMP protocol version. |
| 113 | + /// </summary> |
| 114 | + /// <exception cref="SnmpInvalidVersionException">Thrown when attempting to set protocol version |
| 115 | + /// other then version 1 or 2c</exception> |
| 116 | + public virtual SnmpVersion Version |
| 117 | + { |
| 118 | + get |
| 119 | + { |
| 120 | + return (SnmpVersion)_version.Value; |
| 121 | + } |
| 122 | + set |
| 123 | + { |
| 124 | + if (value != SnmpVersion.Ver1 && value != SnmpVersion.Ver2) |
| 125 | + throw new SnmpInvalidVersionException("Valid SNMP versions are 1 or 2"); |
| 126 | + _version.Value = (int)value; |
| 127 | + } |
| 128 | + } |
| 129 | + /// <summary> |
| 130 | + /// Return SNMP version Integer32 object |
| 131 | + /// </summary> |
| 132 | + /// <returns>Integer32 object</returns> |
| 133 | + public Integer32 GetVersion() |
| 134 | + { |
| 135 | + return _version; |
| 136 | + } |
| 137 | + /// <summary> |
| 138 | + /// Get SNMP version 1 or 2 community name object |
| 139 | + /// </summary> |
| 140 | + virtual public OctetString Community |
| 141 | + { |
| 142 | + get |
| 143 | + { |
| 144 | + return _community; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Get/Set flag that disables checking of host IP address and port number from which reply is received. If not disabled, only |
| 150 | + /// replies from the host IP/port to which request was sent will be considered valid and all others will be ignored. |
| 151 | + /// </summary> |
| 152 | + public bool DisableReplySourceCheck |
| 153 | + { |
| 154 | + get |
| 155 | + { |
| 156 | + return _disableReplySourceCheck; |
| 157 | + } |
| 158 | + set |
| 159 | + { |
| 160 | + _disableReplySourceCheck = value; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Validate object. |
| 166 | + /// </summary> |
| 167 | + /// <returns>true if object is valid, otherwise false</returns> |
| 168 | + public bool Valid() |
| 169 | + { |
| 170 | + if (_community != null && _community.Length > 0 && _version != null) |
| 171 | + { |
| 172 | + if (_version.Value == (int)SnmpVersion.Ver1 || _version.Value == (int)SnmpVersion.Ver2) |
| 173 | + return true; |
| 174 | + } |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Initialize SNMP packet class with agent parameters. In this class, SNMP community name is |
| 180 | + /// set in SNMPv1 and SNMPv2 packets. |
| 181 | + /// </summary> |
| 182 | + /// <param name="packet">Packet class to initialize</param> |
| 183 | + public void InitializePacket(SnmpPacket packet) |
| 184 | + { |
| 185 | + if (packet is SnmpV1Packet) |
| 186 | + { |
| 187 | + SnmpV1Packet pkt = (SnmpV1Packet)packet; |
| 188 | + pkt.Community.Set(_community); |
| 189 | + } |
| 190 | + else if (packet is SnmpV2Packet) |
| 191 | + { |
| 192 | + SnmpV2Packet pkt = (SnmpV2Packet)packet; |
| 193 | + pkt.Community.Set(_community); |
| 194 | + } |
| 195 | + else |
| 196 | + throw new SnmpInvalidVersionException("Invalid SNMP version."); |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Clone current object |
| 201 | + /// </summary> |
| 202 | + /// <returns>Duplicate object initialized with values from this class.</returns> |
| 203 | + public object Clone() |
| 204 | + { |
| 205 | + return new AgentParameters(this.Version,this.Community, this.DisableReplySourceCheck); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments