pub struct SnmpMsg {
pub scoped_pdu_data: ScopedPduData,
/* private fields */
}Expand description
SNMP message that can be encoded and sent over the network.
The default security model is the User-based Security Model (USM) for version 3 of the Simple Network Management Protocol (SNMPv3).
§Examples
use snmp_mp::SnmpMsg;
let mut msg = SnmpMsg::new(1);
msg.set_reportable_flag();Fields§
§scoped_pdu_data: ScopedPduDataImplementations§
Source§impl SnmpMsg
impl SnmpMsg
Sourcepub const MSG_ID_MIN: u32 = 0u32
pub const MSG_ID_MIN: u32 = 0u32
Sourcepub const MSG_ID_MAX: u32 = 2_147_483_647u32
pub const MSG_ID_MAX: u32 = 2_147_483_647u32
The largest value that can be used as a message ID.
§Examples
assert_eq!(SnmpMsg::MSG_ID_MAX, 2_147_483_647);Sourcepub const MAX_UDP_PACKET_SIZE: usize = 65_507usize
pub const MAX_UDP_PACKET_SIZE: usize = 65_507usize
Sourcepub const USER_BASE_SECURITY_MODEL: u32 = 3u32
pub const USER_BASE_SECURITY_MODEL: u32 = 3u32
Sourcepub fn with_scoped_pdu(id: u32, scoped_pdu: ScopedPdu) -> Self
pub fn with_scoped_pdu(id: u32, scoped_pdu: ScopedPdu) -> Self
Constructs a new empty SnmpMsg with the specified ID and scoped PDU.
§Examples
let msg = SnmpMsg::with_scoped_pdu(1, ScopedPdu::new(1));Sourcepub fn id(&self) -> u32
pub fn id(&self) -> u32
Returns the message ID.
The message ID is used to coordinate request messages and responses. It should be generated in a manner that avoids re-use of any outstanding values.
§Examples
let msg_id = msg.id();Sourcepub fn set_id(&mut self, id: u32) -> &mut Self
pub fn set_id(&mut self, id: u32) -> &mut Self
Sets the message ID.
The expected range is is between MSG_ID_MIN and MSG_ID_MAX. The maximum value is not enforced.
§Examples
msg.set_id(1234);
assert_eq!(msg.id(), 1234);Sourcepub fn max_size(&self) -> u32
pub fn max_size(&self) -> u32
Returns the maximum allowed size supported by the sender of the message when encoded.
The default value is MAX_UDP_PACKET_SIZE.
§Examples
let msg = SnmpMsg::new(1);
assert_eq!(msg.max_size(), SnmpMsg::MAX_UDP_PACKET_SIZE as u32);Sourcepub fn set_max_size(&mut self, max_size: u32) -> &Self
pub fn set_max_size(&mut self, max_size: u32) -> &Self
Sets the maximum allowed size supported by the sender of the message when encoded.
The default value is MAX_UDP_PACKET_SIZE.
§Examples
msg.set_max_size(576);
assert_eq!(msg.max_size(), 576);Sourcepub fn is_reportable(&self) -> bool
pub fn is_reportable(&self) -> bool
Returns true if the message is reportable.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_reportable_flag();
assert!(msg.is_reportable());Sourcepub fn set_reportable_flag(&mut self) -> &mut Self
pub fn set_reportable_flag(&mut self) -> &mut Self
Sets the reportable flag.
The reportable flag is a secondary aid in determining whether a Report PDU MUST be sent. It is only used in cases where the PDU portion of a message cannot be decoded, due to, for example, an incorrect encryption key.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_reportable_flag();
assert!(msg.is_reportable());Sourcepub fn is_auth(&self) -> bool
pub fn is_auth(&self) -> bool
Returns true if the message is authenticated.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_auth_flag();
assert!(msg.is_auth());Sourcepub fn set_auth_flag(&mut self) -> &mut Self
pub fn set_auth_flag(&mut self) -> &mut Self
Sets the authentication flag.
The security model must identify the security name on whose behalf the SNMP message was generated whether or no the authentication flag is set.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_auth_flag();
assert!(msg.is_auth());Sourcepub fn security_model(&self) -> u32
pub fn security_model(&self) -> u32
Returns the security model used by the sender.
The default value is USER_BASE_SECURITY_MODEL.
§Examples
let msg = SnmpMsg::new(1);
assert_eq!(msg.security_model(), SnmpMsg::USER_BASE_SECURITY_MODEL);Sourcepub fn set_security_model(&mut self, security_model: u32) -> &Self
pub fn set_security_model(&mut self, security_model: u32) -> &Self
Sets the security model used by the sender.
The default value is USER_BASE_SECURITY_MODEL. The expected value range is between 1 and 2_147_483_647.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_security_model(1);
assert_eq!(msg.security_model(), 1);Sourcepub fn security_params(&self) -> &[u8] ⓘ
pub fn security_params(&self) -> &[u8] ⓘ
Returns the security parameters.
The security parameters are exclusively used by the security model.
§Examples
let security_params = msg.security_params();Sourcepub fn set_security_params(&mut self, params: &[u8])
pub fn set_security_params(&mut self, params: &[u8])
Sets the security parameters.
§Examples
let mut msg = SnmpMsg::new(1);
let security_params = msg.set_security_params(b"security_params");
assert_eq!(msg.security_params(), b"security_params");Sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Returns true if the message is private.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_encrypted_scoped_pdu(encrypted_scoped_pdu);
assert!(msg.is_private());Sourcepub fn set_privacy_flag(&mut self) -> &Self
pub fn set_privacy_flag(&mut self) -> &Self
Sets the privacy flag.
This flag has to be set if the encrypted scoped PDU is directly assigned (see set_encrypted_scoped_pdu).
§Examples
let mut msg = SnmpMsg::new(1);
let security_params = msg.set_privacy_flag();
assert!(msg.is_private());Sourcepub fn set_encrypted_scoped_pdu(&mut self, encrypted_scoped_pdu: Vec<u8>)
pub fn set_encrypted_scoped_pdu(&mut self, encrypted_scoped_pdu: Vec<u8>)
Sets the encrypted scoped PDU and the privacy flag.
It consumes the encrypted scoped PDU.
§Examples
let mut msg = SnmpMsg::new(1);
msg.set_encrypted_scoped_pdu(encrypted_scoped_pdu.clone());
assert!(msg.is_private());
assert_eq!(msg.scoped_pdu_data.encrypted().unwrap(), &encrypted_scoped_pdu[..]);Sourcepub fn encrypt_scoped_pdu<F>(&mut self, encrypt: F)
pub fn encrypt_scoped_pdu<F>(&mut self, encrypt: F)
Encrypts the scoped PDU of the message.
It takes a closure that accepts an encoded scoped PDU and returns an encrypted scoped PDU. If the message’s scoped PDU is already encrypted this function does nothing.
§Examples
let mut msg = SnmpMsg::new(1);
msg.encrypt_scoped_pdu(|scoped_pdu| {
// Use the security model to encrypt the scoped PDU. Ex.:
// `let encrypted_scoped_pdu = priv_key.encrypt(scoped_pdu, &security_params, salt);`
// Return the encrypted scoped PDU.
encrypted_scoped_pdu
});Sourcepub fn decrypt_scoped_pdu<F>(
&mut self,
decrypt: F,
) -> Result<&ScopedPdu, MsgProcessingError>
pub fn decrypt_scoped_pdu<F>( &mut self, decrypt: F, ) -> Result<&ScopedPdu, MsgProcessingError>
Decrypts the encrypted scoped PDU of the message.
It takes a closure that accepts an encrypted scoped PDU and returns an encoded scoped PDU. If the message’s scoped PDU is already decrypted this function does nothing and returns a reference to the scoped PDU.
§Examples
let mut msg = SnmpMsg::new(1);
msg.decrypt_scoped_pdu(|encrypted_scoped_pdu| {
// Use the security model to decrypt the scoped PDU. Ex.:
// `let scoped_pdu = priv_key.decrypt(encrypted_scoped_pdu, &security_params);`
// Return the decrypted scoped PDU.
scoped_pdu
});Sourcepub fn decode(buf: &[u8]) -> MsgProcessingResult<Self>
pub fn decode(buf: &[u8]) -> MsgProcessingResult<Self>
Decodes an incoming message.
§Errors
When a value field in a variable binding is invalid a result with BadValue error is returned.
If the version of the message is not 3 a result with BadVersion error is returned.
When the message is not properly formed a result with MalformedMsg error is returned.
§Examples
let msg = SnmpMsg::decode(&encoded_msg)?;