SNMP++  3.3.4
Snmp Class Reference

SNMP class defintion. 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, const SnmpTarget &target)
 Send a blocking SNMP-GET request. More...
 
virtual int get (Pdu &pdu, const SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-GET request. More...
 
virtual int get_next (Pdu &pdu, const SnmpTarget &target)
 Send a blocking SNMP-GETNEXT request. More...
 
virtual int get_next (Pdu &pdu, const SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-GETNEXT request. More...
 
virtual int set (Pdu &pdu, const SnmpTarget &target)
 Send a blocking SNMP-SET request. More...
 
virtual int set (Pdu &pdu, const SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async SNMP-SET request. More...
 
virtual int get_bulk (Pdu &pdu, const SnmpTarget &target, const int non_repeaters, const int max_reps)
 Send a blocking SNMP-GETBULK request. More...
 
virtual int get_bulk (Pdu &pdu, const 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, const SnmpTarget &target)
 Send a SNMPv3-REPORT. More...
 
virtual int inform (Pdu &pdu, const SnmpTarget &target)
 Send a blocking INFORM-REQ. More...
 
virtual int inform (Pdu &pdu, const SnmpTarget &target, const snmp_callback callback, const void *callback_data=0)
 Send a async INFORM-REQ. More...
 
virtual int response (Pdu &pdu, const SnmpTarget &target, const 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 (void) 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, const 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 *arg)
 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_bThreadRunning
 
int m_iPollTimeOut
 
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 defintion.

The Snmp class provides an object oriented approach to SNMP. The SNMP class is an encapsulation of SNMP sessions, gets, sets and get nexts. 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 99 of file uxsnmp.h.

Constructor & Destructor Documentation

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::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::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
virtual Snmp::~Snmp ( )
virtual

Destructor.

Member Function Documentation

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"
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
void Snmp::check_notify_timestamp ( Pdu pdu)
protected

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

virtual int Snmp::engine_id_discovery ( OctetStr engine_id,
const int  timeout_sec,
const UdpAddress addr 
)
virtual
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.
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.
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 192 of file uxsnmp.h.

virtual int Snmp::get ( Pdu pdu,
const 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
virtual int Snmp::get ( Pdu pdu,
const 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
virtual int Snmp::get_bulk ( Pdu pdu,
const 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
virtual int Snmp::get_bulk ( Pdu pdu,
const 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
EventListHolder* Snmp::get_eventListHolder ( )
inline

Definition at line 544 of file uxsnmp.h.

const IpAddress& Snmp::get_listen_address ( ) const
inline

Definition at line 516 of file uxsnmp.h.

virtual int Snmp::get_next ( Pdu pdu,
const 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
virtual int Snmp::get_next ( Pdu pdu,
const 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
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 489 of file uxsnmp.h.

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 496 of file uxsnmp.h.

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
static const char* Snmp::get_version ( )
static

Get the version of the snmp++ library.

Returns
The version of the snmp++ lib at runtime.
virtual int Snmp::inform ( Pdu pdu,
const 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
virtual int Snmp::inform ( Pdu pdu,
const 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
void Snmp::init ( int &  status,
IpAddress [2],
const unsigned short  port_v4,
const unsigned short  port_v6 
)
protected

Common init function used by constructors.

bool Snmp::is_running ( void  ) const
inlineprotected

Check for the status of the worker thread.

Returns
BOOL - TRUE - if running, FALSE - otherwise

Definition at line 552 of file uxsnmp.h.

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

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

Returns
Unique id between PDU_MIN_RID and PDU_MAX_RID
virtual int Snmp::notify_get_listen_port ( )
virtual

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

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
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.
virtual int Snmp::notify_unregister ( )
virtual

Unregister to get traps and informs.

Undo the call to notify_register().

Returns
Always SNMP_CLASS_SUCCESS
static void* Snmp::process_thread ( void *  arg)
staticprotected

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

Parameters
pSnmp[in] pointer to the whole object
Returns
int 0 - if succesful, 1 - in the case of error
virtual int Snmp::report ( Pdu pdu,
const 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
virtual int Snmp::response ( Pdu pdu,
const SnmpTarget target,
const 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
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- Recepient
fd- socket to use, if not specified, the socket of the object is used
Returns
0 on success, -1 on failure
virtual int Snmp::set ( Pdu pdu,
const 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
virtual int Snmp::set ( Pdu pdu,
const 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
int Snmp::snmp_engine ( Pdu pdu,
long int  non_reps,
long int  max_reps,
const 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.
static void Snmp::socket_cleanup ( )
static

Shut down the Winsock library (WSACleanup).

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.
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 threadded 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.
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.
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

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

Member Data Documentation

long Snmp::current_rid
protected

Definition at line 639 of file uxsnmp.h.

EventListHolder* Snmp::eventListHolder
protected

Definition at line 646 of file uxsnmp.h.

SnmpSocket Snmp::iv_snmp_session
protected

Definition at line 633 of file uxsnmp.h.

IpAddress Snmp::listen_address
protected

Definition at line 638 of file uxsnmp.h.

bool Snmp::m_bThreadRunning
private

Definition at line 650 of file uxsnmp.h.

pthread_t Snmp::m_hThread
private

Definition at line 661 of file uxsnmp.h.

int Snmp::m_iPollTimeOut
private

Definition at line 651 of file uxsnmp.h.

snmp_callback Snmp::notifycallback
protected

Definition at line 642 of file uxsnmp.h.

void* Snmp::notifycallback_data
protected

Definition at line 643 of file uxsnmp.h.


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