SNMP++  3.3.11
Snmp Class Reference

SNMP class definition. More...

#include <uxsnmp.h>

Inheritance diagram for Snmp:
Collaboration diagram for Snmp:

Classes

struct  V3CallBackData
 Internal used callback data structure for async v3 requests. More...
 

Public Member Functions

virtual int cancel (const unsigned long rid)
 Cancel a pending request. More...
 
virtual int send_raw_data (unsigned char *send_buf, size_t send_len, UdpAddress &address, SnmpSocket fd=INVALID_SOCKET)
 Send raw UDP data. More...
 
const IpAddressget_listen_address () const
 
bool start_poll_thread (const int timeout)
 Start one thread listening for responses and notifications. More...
 
void stop_poll_thread ()
 Stop the thread listening for responses and notifications. More...
 
EventListHolderget_eventListHolder ()
 
Constructors and Destructor
 Snmp (int &status, const unsigned short port=0, const bool bind_ipv6=false)
 Construct a new SNMP session using the given UDP port. More...
 
 Snmp (int &status, const UdpAddress &addr)
 Construct a new SNMP session using the given UDP address. More...
 
 Snmp (int &status, const UdpAddress &addr_v4, const UdpAddress &addr_v6)
 Construct a new SNMP session using the given UDP addresses. More...
 
virtual ~Snmp ()
 Destructor. More...
 
Sending SNMP Pdus
virtual int get (Pdu &pdu, SnmpTarget &target)
 Send a blocking SNMP-GET request. More...
 
virtual int get (Pdu &pdu, SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-GET request. More...
 
virtual int get_next (Pdu &pdu, SnmpTarget &target)
 Send a blocking SNMP-GETNEXT request. More...
 
virtual int get_next (Pdu &pdu, SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-GETNEXT request. More...
 
virtual int set (Pdu &pdu, SnmpTarget &target)
 Send a blocking SNMP-SET request. More...
 
virtual int set (Pdu &pdu, SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-SET request. More...
 
virtual int get_bulk (Pdu &pdu, SnmpTarget &target, const int non_repeaters, const int max_reps)
 Send a blocking SNMP-GETBULK request. More...
 
virtual int get_bulk (Pdu &pdu, SnmpTarget &target, const int non_repeaters, const int max_reps, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-GETBULK request. More...
 
virtual int trap (Pdu &pdu, const SnmpTarget &target)
 Send a SNMP-TRAP. More...
 
virtual int report (Pdu &pdu, SnmpTarget &target)
 Send a SNMPv3-REPORT. More...
 
virtual int inform (Pdu &pdu, SnmpTarget &target)
 Send a blocking INFORM-REQ. More...
 
virtual int inform (Pdu &pdu, SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async INFORM-REQ. More...
 
virtual int response (Pdu &pdu, SnmpTarget &target, SnmpSocket fd=INVALID_SOCKET)
 Send a RESPONSE. More...
 
virtual int broadcast_discovery (UdpAddressCollection &addresses, const int timeout_sec, const UdpAddress &addr, const snmp_version version, const OctetStr *community=0)
 Send a SNMP Broadcast message. More...
 
virtual int engine_id_discovery (OctetStr &engine_id, const int timeout_sec, const UdpAddress &addr)
 
Trap and Inform handling
virtual void notify_set_listen_port (const int port)
 Set the port for listening to traps and informs. More...
 
virtual int notify_get_listen_port ()
 Get the port that is used for listening to traps and informs. More...
 
virtual int notify_register (const OidCollection &trapids, const TargetCollection &targets, const snmp_callback callback, const void *callback_data=0)
 Register to get traps and informs. More...
 
virtual int notify_unregister ()
 Unregister to get traps and informs. More...
 
virtual int get_notify_filter (OidCollection &trapids, TargetCollection &targets)
 Get notify register info. More...
 
snmp_callback get_notify_callback ()
 Get a pointer to the callback function used for trap reception. More...
 
void * get_notify_callback_data ()
 Get a pointer to the data that is passed to the callback function. More...
 
- Public Member Functions inherited from SnmpSynchronized
 SnmpSynchronized ()
 
virtual ~SnmpSynchronized ()
 
void lock ()
 
void unlock ()
 

Static Public Member Functions

static const char * get_version ()
 Get the version of the snmp++ library. More...
 
static const char * error_msg (const int c)
 Returns a human readable error string. More...
 
static int error_code (const Oid &v3Oid)
 Returns the error code for a SNMPv3 report Oid. More...
 
static const char * error_msg (const Oid &v3Oid)
 Returns a human readable error string. More...
 
static void socket_startup ()
 Initialize the Winsock library (WSAStartup). More...
 
static void socket_cleanup ()
 Shut down the Winsock library (WSACleanup). More...
 

Protected Member Functions

bool is_running () const
 Check for the status of the worker thread. More...
 
long MyMakeReqId ()
 Generate a unique (for this Snmp obect) request id. More...
 
void init (int &status, IpAddress *[2], const unsigned short port_v4, const unsigned short port_v6)
 Common init function used by constructors. More...
 
void check_notify_timestamp (Pdu &pdu)
 Set the notify timestamp of a trap pdu if the user did not set it. More...
 
int snmp_engine (Pdu &pdu, long int non_reps, long int max_reps, SnmpTarget &target, const snmp_callback cb, const void *cbd, SnmpSocket fd=INVALID_SOCKET, int reports_received=0)
 gets, sets and get nexts go through here.... More...
 
void map_action (unsigned short action, unsigned short &pdu_action)
 

Static Protected Member Functions

static void * process_thread (void *snmp)
 This is a working thread for the recovery of the pending events. More...
 

Protected Attributes

SnmpSocket iv_snmp_session
 
IpAddress listen_address
 
long current_rid
 
snmp_callback notifycallback
 
void * notifycallback_data
 
EventListHoldereventListHolder
 

Private Attributes

bool m_isThreadRunning
 
int m_pollTimeOut
 
pthread_t m_hThread
 

Friends

void v3CallBack (int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *v3cd)
 
void deleteV3Callback (struct Snmp::V3CallBackData *&cbData)
 

Additional Inherited Members

- Public Attributes inherited from SnmpSynchronized
pthread_mutex_t _mutex
 

Detailed Description

SNMP class definition.

The Snmp class provides an object oriented approach to SNMP. The SNMP class is an encapsulation of SNMP sessions, get, set and get next operations. The class manages all SNMP resources and provides complete retry and timeout capability.

This class is thread save.

Note
If you use the async methods to send requests you MUST call Snmp::eventListHolder->SNMPProcessPendingEvents() while waiting for the responses. This function triggers the resend of packets and calls your callback function if the response is received.
Call srand() before creating the first Snmp object.

Definition at line 107 of file uxsnmp.h.

Constructor & Destructor Documentation

◆ Snmp() [1/3]

Snmp::Snmp ( int &  status,
const unsigned short  port = 0,
const bool  bind_ipv6 = false 
)

Construct a new SNMP session using the given UDP port.

Parameters
statusafter creation of the session this parameter will hold the creation status.
portan UDP port to be used for the session
bind_ipv6Set this to true if IPv6 should be used. The default is IPv4.

◆ Snmp() [2/3]

Snmp::Snmp ( int &  status,
const UdpAddress addr 
)

Construct a new SNMP session using the given UDP address.

Thus, binds the session on a specific IPv4 or IPv6 address.

Parameters
statusafter creation of the session this parameter will hold the creation status.
addran UDP address to be used for the session

◆ Snmp() [3/3]

Snmp::Snmp ( int &  status,
const UdpAddress addr_v4,
const UdpAddress addr_v6 
)

Construct a new SNMP session using the given UDP addresses.

Using this constructor will bind to both IPv4 and IPv6 ports.

Parameters
statusafter creation of the session this parameter will hold the creation status.
addr_v4an IPv4 UDP address to be used for the session
addr_v6an IPv6 UDP address to be used for the session

◆ ~Snmp()

virtual Snmp::~Snmp ( )
virtual

Destructor.

Member Function Documentation

◆ broadcast_discovery()

virtual int Snmp::broadcast_discovery ( UdpAddressCollection addresses,
const int  timeout_sec,
const UdpAddress addr,
const snmp_version  version,
const OctetStr community = 0 
)
virtual

Send a SNMP Broadcast message.

This member function sends out a valid SNMP message to a broadcast address and waits for responses. The source addresses of the response messages are added to the collection.

The message is sent only once.

Note
SNMP_BROADCAST has to be defined in config_snmp_pp.h.
There is no SNMP standard that defines "SNMP Broadcast discovery". SNMP agents are not forced to answer requests that are sent to a broadcast address.
Do not use this method while waiting for other responses, as these responses will be added to the collection and dropped by this method. Solution for this problem: Use a special Snmp object only for broadcasts.
Parameters
addresses- The addresses of the agents, that answered.
timeout_sec- Timeout in seconds
addr- Broadcast address
version- SNMP version to use
community- Only needed for SNMPv1/v2c, defaults to "public"

◆ cancel()

virtual int Snmp::cancel ( const unsigned long  rid)
virtual

Cancel a pending request.

Parameters
rid- The request id to cancel
Returns
SNMP_CLASS_SUCCES or SNMP_CLASS_INVALID_REQID on failure

◆ check_notify_timestamp()

void Snmp::check_notify_timestamp ( Pdu pdu)
protected

Set the notify timestamp of a trap pdu if the user did not set it.

◆ engine_id_discovery()

virtual int Snmp::engine_id_discovery ( OctetStr engine_id,
const int  timeout_sec,
const UdpAddress addr 
)
virtual

◆ error_code()

static int Snmp::error_code ( const Oid v3Oid)
static

Returns the error code for a SNMPv3 report Oid.

If a report message is returned, then the contained Oid can be used to get a error code.

Parameters
v3Oid- Oid of a SNMPv3 report Pdu
Returns
Error code.

◆ error_msg() [1/2]

static const char* Snmp::error_msg ( const int  c)
static

Returns a human readable error string.

Parameters
c- Error code returned by any method of this class
Returns
Null terminated error string.

◆ error_msg() [2/2]

static const char* Snmp::error_msg ( const Oid v3Oid)
inlinestatic

Returns a human readable error string.

If a report message is returned, then the contained Oid can be used to get a error string.

Parameters
v3Oid- Oid of a SNMPv3 report Pdu
Returns
Null terminated error string.

Definition at line 200 of file uxsnmp.h.

◆ get() [1/2]

virtual int Snmp::get ( Pdu pdu,
SnmpTarget target 
)
virtual

Send a blocking SNMP-GET request.

Parameters
pdu- Pdu to send
target- Target for the get
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ get() [2/2]

virtual int Snmp::get ( Pdu pdu,
SnmpTarget target,
const snmp_callback  callback,
const void *  callback_data = 0 
)
virtual

Send a async SNMP-GET request.

Parameters
pdu- Pdu to send
target- Target for the get
callback- User callback function to use
callback_data- User definable data pointer
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ get_bulk() [1/2]

virtual int Snmp::get_bulk ( Pdu pdu,
SnmpTarget target,
const int  non_repeaters,
const int  max_reps 
)
virtual

Send a blocking SNMP-GETBULK request.

Parameters
pdu- Pdu to send
target- Target for the getbulk
non_repeaters- number of non repeaters
max_reps- maximum number of repetitions
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ get_bulk() [2/2]

virtual int Snmp::get_bulk ( Pdu pdu,
SnmpTarget target,
const int  non_repeaters,
const int  max_reps,
const snmp_callback  callback,
const void *  callback_data = 0 
)
virtual

Send a async SNMP-GETBULK request.

Parameters
pdu- Pdu to send
target- Target for the getbulk
non_repeaters- number of non repeaters
max_reps- maximum number of repetitions
callback- User callback function to use
callback_data- User definable data pointer
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ get_eventListHolder()

EventListHolder* Snmp::get_eventListHolder ( )
inline

Definition at line 552 of file uxsnmp.h.

◆ get_listen_address()

const IpAddress& Snmp::get_listen_address ( ) const
inline

Definition at line 524 of file uxsnmp.h.

◆ get_next() [1/2]

virtual int Snmp::get_next ( Pdu pdu,
SnmpTarget target 
)
virtual

Send a blocking SNMP-GETNEXT request.

Parameters
pdu- Pdu to send
target- Target for the getnext
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ get_next() [2/2]

virtual int Snmp::get_next ( Pdu pdu,
SnmpTarget target,
const snmp_callback  callback,
const void *  callback_data = 0 
)
virtual

Send a async SNMP-GETNEXT request.

Parameters
pdu- Pdu to send
target- Target for the getnext
callback- User callback function to use
callback_data- User definable data pointer
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ get_notify_callback()

snmp_callback Snmp::get_notify_callback ( )
inline

Get a pointer to the callback function used for trap reception.

Returns
Pointer to the function set through notify_register()

Definition at line 497 of file uxsnmp.h.

◆ get_notify_callback_data()

void* Snmp::get_notify_callback_data ( )
inline

Get a pointer to the data that is passed to the callback function.

Returns
Pointer to the data set through notify_register()

Definition at line 504 of file uxsnmp.h.

◆ get_notify_filter()

virtual int Snmp::get_notify_filter ( OidCollection trapids,
TargetCollection targets 
)
virtual

Get notify register info.

Parameters
trapids- ids listened for
targets- targets listened for
Returns
SNMP_CLASS_SUCCESS or SNMP_CLASS_INVALID if not registered

◆ get_version()

static const char* Snmp::get_version ( )
static

Get the version of the snmp++ library.

Returns
The version of the snmp++ lib at runtime.

◆ inform() [1/2]

virtual int Snmp::inform ( Pdu pdu,
SnmpTarget target 
)
virtual

Send a blocking INFORM-REQ.

Parameters
pdu- Pdu to send
target- Target for the inform
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ inform() [2/2]

virtual int Snmp::inform ( Pdu pdu,
SnmpTarget target,
const snmp_callback  callback,
const void *  callback_data = 0 
)
virtual

Send a async INFORM-REQ.

Parameters
pdu- Pdu to send
target- Target for the inform
callback- User callback function to use
callback_data- User definable data pointer
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ init()

void Snmp::init ( int &  status,
IpAddress [2],
const unsigned short  port_v4,
const unsigned short  port_v6 
)
protected

Common init function used by constructors.

◆ is_running()

bool Snmp::is_running ( ) const
inlineprotected

Check for the status of the worker thread.

Returns
true - if running, false - otherwise

Definition at line 560 of file uxsnmp.h.

◆ map_action()

void Snmp::map_action ( unsigned short  action,
unsigned short &  pdu_action 
)
protected

◆ MyMakeReqId()

long Snmp::MyMakeReqId ( )
protected

Generate a unique (for this Snmp obect) request id.

Returns
Unique id between PDU_MIN_RID and PDU_MAX_RID

◆ notify_get_listen_port()

virtual int Snmp::notify_get_listen_port ( )
virtual

Get the port that is used for listening to traps and informs.

◆ notify_register()

virtual int Snmp::notify_register ( const OidCollection trapids,
const TargetCollection targets,
const snmp_callback  callback,
const void *  callback_data = 0 
)
virtual

Register to get traps and informs.

Note
Every call to one of the notify_register() methods overwrites the previous given values.
Parameters
trapids- ids to listen for
targets- targets to listen for
callback- User callback function to use
callback_data- User definable data pointer
Returns
SNMP_CLASS_SUCCESS, SNMP_CLASS_TL_FAILED or SNMP_CLASS_TL_IN_USE

◆ notify_set_listen_port()

virtual void Snmp::notify_set_listen_port ( const int  port)
virtual

Set the port for listening to traps and informs.

Note
This function must be called before notify_register(), otherwise the default port is used.

◆ notify_unregister()

virtual int Snmp::notify_unregister ( )
virtual

Unregister to get traps and informs.

Undo the call to notify_register().

Returns
Always SNMP_CLASS_SUCCESS

◆ process_thread()

static void* Snmp::process_thread ( void *  snmp)
staticprotected

This is a working thread for the recovery of the pending events.

Parameters
snmp[in] pointer to the whole object
Returns
0 - if successful, 1 - in the case of error

◆ report()

virtual int Snmp::report ( Pdu pdu,
SnmpTarget target 
)
virtual

Send a SNMPv3-REPORT.

Parameters
pdu- Pdu to send
target- Target for the report (must be a UTarget)
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ response()

virtual int Snmp::response ( Pdu pdu,
SnmpTarget target,
SnmpSocket  fd = INVALID_SOCKET 
)
virtual

Send a RESPONSE.

Parameters
pdu- Pdu to send
target- Target for the response
fd- file descriptor to use, should be the one that was passed to the callback function
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ send_raw_data()

virtual int Snmp::send_raw_data ( unsigned char *  send_buf,
size_t  send_len,
UdpAddress address,
SnmpSocket  fd = INVALID_SOCKET 
)
virtual

Send raw UDP data.

This method may be used to send any data to the recepient.

Parameters
send_buf- Data buffer
send_len- Length of the data
address- Recipient
fd- socket to use, if not specified, the socket of the object is used
Returns
0 on success, -1 on failure

◆ set() [1/2]

virtual int Snmp::set ( Pdu pdu,
SnmpTarget target 
)
virtual

Send a blocking SNMP-SET request.

Parameters
pdu- Pdu to send
target- Target for the set
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ set() [2/2]

virtual int Snmp::set ( Pdu pdu,
SnmpTarget target,
const snmp_callback  callback,
const void *  callback_data = 0 
)
virtual

Send a async SNMP-SET request.

Parameters
pdu- Pdu to send
target- Target for the set
callback- User callback function to use
callback_data- User definable data pointer
Returns
SNMP_CLASS_SUCCES or a negative error code

◆ snmp_engine()

int Snmp::snmp_engine ( Pdu pdu,
long int  non_reps,
long int  max_reps,
SnmpTarget target,
const snmp_callback  cb,
const void *  cbd,
SnmpSocket  fd = INVALID_SOCKET,
int  reports_received = 0 
)
protected

gets, sets and get nexts go through here....

This mf does all snmp sending and reception except for traps which are sent using trap().

Note
that for a UTarget with an empty engine id the Utarget::set_engine_id() may be called.

◆ socket_cleanup()

static void Snmp::socket_cleanup ( )
static

Shut down the Winsock library (WSACleanup).

◆ socket_startup()

static void Snmp::socket_startup ( )
static

Initialize the Winsock library (WSAStartup).

Note
on Win32 this method must be called before creating Snmp or Address objects.

◆ start_poll_thread()

bool Snmp::start_poll_thread ( const int  timeout)

Start one thread listening for responses and notifications.

This method is used to start response and notification processing in a multi threaded setup.

Note
start_poll_thread() itself is not thread safe. The caller must make sure that only one thread is calling start_poll_thread() or stop_poll_thread() at any point in time.
Parameters
timeout- Timeout for each call of the select() or poll() system call.
Returns
true if the thread is now running and false if it failed to start.

◆ stop_poll_thread()

void Snmp::stop_poll_thread ( )

Stop the thread listening for responses and notifications.

This method is used to stop the thread started with start_poll_thread().

Note
stop_poll_thread() itself is not thread safe. The caller must make sure that only one thread is calling start_poll_thread() or stop_poll_thread() at any point in time.

◆ trap()

virtual int Snmp::trap ( Pdu pdu,
const SnmpTarget target 
)
virtual

Send a SNMP-TRAP.

Parameters
pdu- Pdu to send
target- Target for the trap
Returns
SNMP_CLASS_SUCCES or a negative error code

Friends And Related Function Documentation

◆ deleteV3Callback

void deleteV3Callback ( struct Snmp::V3CallBackData *&  cbData)
friend

◆ v3CallBack

void v3CallBack ( int  reason,
Snmp snmp,
Pdu pdu,
SnmpTarget target,
void *  v3cd 
)
friend

Member Data Documentation

◆ current_rid

long Snmp::current_rid
protected

Definition at line 646 of file uxsnmp.h.

◆ eventListHolder

EventListHolder* Snmp::eventListHolder
protected

Definition at line 653 of file uxsnmp.h.

◆ iv_snmp_session

SnmpSocket Snmp::iv_snmp_session
protected

Definition at line 640 of file uxsnmp.h.

◆ listen_address

IpAddress Snmp::listen_address
protected

Definition at line 645 of file uxsnmp.h.

◆ m_hThread

pthread_t Snmp::m_hThread
private

Definition at line 667 of file uxsnmp.h.

◆ m_isThreadRunning

bool Snmp::m_isThreadRunning
private

Definition at line 657 of file uxsnmp.h.

◆ m_pollTimeOut

int Snmp::m_pollTimeOut
private

Definition at line 658 of file uxsnmp.h.

◆ notifycallback

snmp_callback Snmp::notifycallback
protected

Definition at line 649 of file uxsnmp.h.

◆ notifycallback_data

void* Snmp::notifycallback_data
protected

Definition at line 650 of file uxsnmp.h.


The documentation for this class was generated from the following file: