SnmpMsg

Struct SnmpMsg 

Source
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: ScopedPduData

Implementations§

Source§

impl SnmpMsg

Source

pub const MSG_ID_MIN: u32 = 0u32

The smallest value that can be used as a message ID.

§Examples
assert_eq!(SnmpMsg::MSG_ID_MIN, 0);
Source

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);
Source

pub const MAX_UDP_PACKET_SIZE: usize = 65_507usize

The largest UDP packet size for IPv4.

§Examples
assert_eq!(SnmpMsg::MAX_UDP_PACKET_SIZE, 65_507);
Source

pub const USER_BASE_SECURITY_MODEL: u32 = 3u32

Security model used for each SnmpMsg.

§Examples
assert_eq!(SnmpMsg::USER_BASE_SECURITY_MODEL, 3);
Source

pub fn new(id: u32) -> Self

Constructs a new empty SnmpMsg with the specified ID.

§Examples
let msg = SnmpMsg::new(1);
Source

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));
Source

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();
Source

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);
Source

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);
Source

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);
Source

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());
Source

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());
Source

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());
Source

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());
Source

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);
Source

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);
Source

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();
Source

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");
Source

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());
Source

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());
Source

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[..]);
Source

pub fn encrypt_scoped_pdu<F>(&mut self, encrypt: F)
where F: FnOnce(Vec<u8>) -> Vec<u8>,

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
});
Source

pub fn decrypt_scoped_pdu<F>( &mut self, decrypt: F, ) -> Result<&ScopedPdu, MsgProcessingError>
where F: FnOnce(Vec<u8>) -> Option<Vec<u8>>,

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
});
Source

pub fn encode(&self) -> Vec<u8>

Encodes an outgoing message.

§Examples
let msg = SnmpMsg::new(1);
let encoded_msg = msg.encode();
Source

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)?;

Trait Implementations§

Source§

impl Clone for SnmpMsg

Source§

fn clone(&self) -> SnmpMsg

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SnmpMsg

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SnmpMsg

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Hash for SnmpMsg

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for SnmpMsg

Source§

fn eq(&self, other: &SnmpMsg) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SnmpMsg

Source§

impl StructuralPartialEq for SnmpMsg

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.