<< Back to man.lupaworld.com

19.              Target Class

 

 

 

The Target class is a C++ class used with SNMP++ to allow definition and usage of targets. A target can be thought of as a management definition of an agent to be used for SNMP communication . A target consists of more than just an network address. Targets contain retransmission and time-out policy information, the SNMP protocol type ( SNMP version 1 or SNMP version 2) and more. Currently, the only Target sub-class is the Community-based CTarget. The CTarget class may be used for SNMP version 1 and SNMP version 2c communication. This allows your code to be reused for V2 communication with no changes. The Target class allows an SNMP++ session to be independent of  a particular agent’s attributes.

 

 

19.1.   Abstract Targets

SNMP++ supports the notion of an abstract Target. This is a Target which may take on any actual derived Target class. All SNMP++ member functions using Targets accept abstract Targets and not specific derived Target objects. This abstract interface allows minimal code change when supporting new Targets.

 

19.2. Target Addresses

Each target has associated with it a Address object. This Address is a GenAddress and therefor may take on the value of any SNMP++ Address, ( IP, IPX or whatever). To specify the address of a managed agent, the agent’s address is simply specified and then attached to a Target via construction parameters or through member functions.

 

19.3. Retransmission Policies

Each Target has a re-transmission policy where a defined time-out and retry specify how long to wait for a SNMP response and how many times to retry if a SNMP response was not heard. Time-out is defined in hundredths of a second where a value of 100 means wait for one second for each response. Retries denote the number of times to retry where the first request is not a retry, it is just a try. So a retry value of 3 equates to retry a maximum of three times when waiting for a response.  The total amount of time to wait can be computed as follows.

 

Total Wait Time = time-out  * (retry+1)

 

If a SNMP++ response does not arrive in the computed total wait time, a SNMP++ time-out error code will be returned. This behavior applies to both blocked and asynchronous calls.

 

 

19.4. Target Class Interface

Target Class Member Functions

Description

Constructors

 

     CTarget::CTarget( void);

Construct an invalid CTarget. Defaults to “public” for community names and retry=1 time_out=1 for re-transmission policy.

     CTarget::CTarget(const Address &address ,

                                   const char *read_community_name,  

                                   const char *write_community_name, 

                                  

Construct a CTarget using community names and an Address object. Defaults to retry=1 and time-out =100ms for re-transmission policy.

     CTarget( const Address &address

                    const OctetStr &read_community_name,   

                    const OctetStr &write_community_name);

Construct a CTarget using OctetStr Communities and an Address.

     CTarget::CTarget( Address &address);

Construct a CTarget using an Address object Defaults to “public” for community names and retry=1 time_out=1 for re-transmission policy.

     CTarget::CTarget( const CTarget &target);

Copy constructor.

Destructor

 

     CTarget::~CTarget();

Deletes CTarget object, frees up all resources.

Member Functions

 

     char * get_readcommunity();

Returns read community name.

     void get_readcommunity( OctetStr& read_community_oct);

Gets the read community as OctetStr.

     void set_readcommunity( const char * get_community);

Sets the read community.

     void set_readcommunity( const OctetStr& read_community);

Set the read community name using an OctetStr.

     char * get_writecommunity();

Get the write community.

     void get_writecommunity( OctetStr &write_community_oct);

Get the write community as an OctetStr.

     void set_writecommunity( const char * new_set_community);

Set the write community.

     void set_writecommunity( const OctetStr& write_community);

 Set the write community using an OctetStr.

     int  get_address( GenAddress &address);

Get the Address object.

     void set_address( Address &address);

Set the Address portion.

     CTarget& operator=( const CTarget& target);

Assign one CTarget to another.

     snmp_version get_version();

Return the SNMP version. (version1 or version2c)

     void set_version( const snmp_version v);

Set the version. (version1 or version2c)

     int operator==( const CTarget &lhs, const CTarget &rhs);

Compare two CTargets.

Abstract Class Member Functions

 

     int valid();

Returns validity of a Target.

     void set_retry( const int r);

Sets the retry.

     int get_retry();

Get the retry value.

     void set_timeout( const unsigned long t);

Set the time-out value.

     unsigned long get_timeout();

Get the time-out value.

 


19.5. CTarget Class ( Community Based Targets)

The CTarget class allows explicit definition of Community based targets. A CTarget defines a SNMP agent using SNMP community based attributes . This includes the read and write community names and an address. The address is represented using the SNMP++ Address class, so an address may be an IP or IPX address. The CTarget class should  be used when the application programmer explicitly knows that the agent supports SNMP community based access ( SNMP version 1 or SNMP version 2c).

 

19.5.1.Constructing CTargets

CTarget objects may be instantiated in three different ways.

 

// ----------[ instantiating CTarget Objects ]-----------------------------

 

// valid complete instantiation

CTarget    ct((IpAddress)”10.10.10.10”,  // Address

                      public”,                              // read community name

                      public”);                            // write community name

 

// valid complete using “public” defaults

CTarget ct( (IpAddress) “1.2.3.4”);

 

// invalid CTarget

CTarget ct;


 

//----[ modifying CTargets ]------------------------------------

ct.set_readcommunity(“private);   // modifying the read community

 

ct.set_writecommunity(“private”);  // modifying the write community

 

ct.set_address( (IpAddress) “15.29.33.210”);

 


19.5.2.Modifying CTargets

19.5.3.Accessing CTargets

 

//-----[ Accessing CTarget member variables ]-------------------------

 

// get the write community name

cout << “Write community” << ct.get_writecommunity(); 

 

// get the read community name

cout << “Read community ” << ct.get_readcommunity();

 

// get the address

GenAddress address;

ct.get_address( address);

 

// check the validity of a target

if ( ct.valid())

   cout << “Target is valid”;


 


19.5.4.                 CTarget Examples

//------------[CTarget class examples ]-----------------------------------------------------------------

 

// create a valid CTarget using a GenAddress

CTarget ct( (GenAddress) “10.20.30.40”);

 

// create a valid CTarget using an IpxAddress

IpxAddress ipxaddress(“01010101-010101010101”);

CTarget my_target( ipxaddress);                           // use default “public” for communities

 

// create an invalid CTarget object

CTarget  ct;                                                    // no construction params therefor invalid

if ( !ct.valid()) 

   cout << “Invalid CTarget instance!”;

 

// get the read community

cout << “Read Community =” << ct.get_readcommunity();

 

// get the write community

cout << “Write Community = ” << ct.get_writecommunity();

 

// modify the get community

ct.set_readcommunity( “pilsner”);

 

// modify the write community

ct.set_writecommunity(“pale ale”);