1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
14 _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15 _## Permission to use, copy, modify, distribute and/or sell this software
16 _## and/or its documentation is hereby granted without fee. User agrees
17 _## to display the above copyright notice and this license notice in all
18 _## copies of the software and any documentation of the software. User
19 _## agrees to assume all liability for the use of the software;
20 _## Hewlett-Packard and Jochen Katz make no representations about the
21 _## suitability of this software for any purpose. It is provided
22 _## "AS-IS" without warranty of any kind, either express or implied. User
23 _## hereby grants a royalty-free license to any and all derivatives based
24 _## upon this software code base.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
29 /*===================================================================
32 Hewlett-Packard Company
34 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35 Permission to use, copy, modify, distribute and/or sell this software
36 and/or its documentation is hereby granted without fee. User agrees
37 to display the above copyright notice and this license notice in all
38 copies of the software and any documentation of the software. User
39 agrees to assume all liability for the use of the software; Hewlett-Packard
40 makes no representations about the suitability of this software for any
41 purpose. It is provided "AS-IS" without warranty of any kind,either express
42 or implied. User hereby grants a royalty-free license to any and all
43 derivatives based upon this software code base.
46 SNMP++ T A R G E T . H
48 TARGET CLASS DEFINITION
50 DESIGN + AUTHOR: Peter E Mellquist
53 Target class defines target SNMP agents.
55 =====================================================================*/
56 // $Id: target.h 1539 2009-05-27 22:12:53Z katz $
61 //----[ includes ]-----------------------------------------------------
63 #include "snmp_pp/config_snmp_pp.h"
64 #include "snmp_pp/address.h"
65 #include "snmp_pp/octet.h"
66 #include "snmp_pp/collect.h"
68 #ifdef SNMP_PP_NAMESPACE
73 //----[ enumerated types for SNMP versions ]---------------------------
75 * The SNMP version to use is passed with this enum.
79 version1, ///< (0) SNMPv1
80 version2c ///< (1) SNMPv2c
82 ,version2stern, ///< (2) Dont use this!
83 version3 ///< (3) SNMPv3
87 //----[ Target class ]-------------------------------------------------
89 * Abstract class used to provide a virtual interface into Targets.
91 * @note Although it is possible to create an object of this class,
92 * you won't be happy with that...
94 class DLLOPT SnmpTarget
99 * Enum to identify a target object through SnmpTarget::get_type() method.
103 type_base, ///< It is a SnmpTarget object
104 type_ctarget, ///< It is a CTarget object
105 type_utarget ///< It is a Utarget object
109 * Create a SnmpTarget object with default values.
110 * The validity of the target will be false.
113 : validity(false), timeout(default_timeout), retries(default_retries),
114 version(version1), ttype(type_base) {};
117 * Create a SnmpTarget object with the given Address.
119 SnmpTarget(const Address &address)
120 : validity(false), timeout(default_timeout), retries(default_retries),
121 version(version1), ttype(type_base), my_address(address)
122 { if (my_address.valid()) validity = true; };
125 * Destructor that has nothing to do.
127 virtual ~SnmpTarget() {};
130 * Return the type of the target object.
132 * If a SNMP message is received through a callback (that only
133 * passes a SnmpTarget pointer to the callback function), this
134 * method can be used to check the type of the object before doing a
135 * cast to CTarget or UTarget.
137 target_type get_type() const { return ttype; };
140 * Returns the validity of the target object.
142 * @return true, if the target is valid.
144 bool valid() const { return validity;};
147 * Set the retry value.
149 * @param r - The number of retries if no response is received.
151 void set_retry(const int r) { retries = r; };
154 * Get the retry value.
156 * @return The number of retries on timeout.
158 int get_retry() const { return retries; };
162 * Set the timeout for requests.
164 * The default timeout for requests is 1 second (100).
166 * @param t - Timeout in 10ms, so 100 will set the timeout to 1 second.
168 void set_timeout(const unsigned long t) { timeout = t; };
173 * @return The timeout for requests sent using this target object.
175 unsigned long get_timeout() const { return timeout; };
178 * Change the default timeout.
180 * Changing the default timeout value will only have an effect for
181 * target objects that are created after setting this value.
183 * @param t - The new default timeout value
185 static void set_default_timeout(const unsigned long t)
186 { default_timeout = t; };
189 * Change the default retries vlaue.
191 * Changing the default retries value will only have an effect for
192 * target objects that are created after setting this value.
194 * @param r - The new retries value
196 static void set_default_retries(const int r) { default_retries = r; };
201 * Virtual clone operation for creating a new SnmpTarget from an existing
204 * @note The caller MUST use the delete operation on the return
207 * @return A pointer to the new object on success, 0 on failure.
209 virtual SnmpTarget *clone() const;
212 * Get the address object.
214 * @param address - GenAddress object to store the target address.
215 * @return TRUE on success.
217 int get_address(GenAddress &address) const;
220 * Get the address object.
222 * @return The target address.
224 const GenAddress &get_address() const { return my_address; };
227 * Set the address object.
229 * @param address - The address that this target should use.
230 * @return TRUE on success.
232 virtual int set_address(const Address &address);
235 * Get the SNMP version for this target.
237 * @return The SNMP version of this target object.
238 * @see enum snmp_version
240 snmp_version get_version() const { return version;};
243 * Set the SNMP version of this target.
245 * @param v - The SNMP version that should be used for sending messages.
247 void set_version(const snmp_version v) { version = v; };
250 * Overloeaded compare operator.
252 * Two SnmpTarget objects are considered equal, if all member
253 * variables are equal.
255 * @return 1 if targets are equal, 0 if not.
257 int operator==(const SnmpTarget &rhs) const;
262 virtual void clear();
265 bool validity; ///< Validity of the object
266 unsigned long timeout; ///< xmit timeout in 10 milli secs
267 int retries; ///< number of retries
268 snmp_version version; ///< SNMP version to use
269 target_type ttype; ///< Type of the target
270 GenAddress my_address; ///< Address object
272 static unsigned long default_timeout; ///< default timeout for new objects
273 static int default_retries; ///< default retries for new objects
276 //----[ CTarget class ]----------------------------------------------
278 * Community based target object.
279 * This target can be used for SNMPv1 and SNMPv2c messages.
281 class DLLOPT CTarget: public SnmpTarget
285 * Constructor with no args.
286 * The validity of the target will be false.
291 * Constructor with all args.
293 * @param address - Address of the target host (cann be any address object)
294 * @param read_community_name - Community for get requests
295 * @param write_community_name - Community for set requests
297 CTarget(const Address &address,
298 const char *read_community_name,
299 const char *write_community_name);
302 * Constructor with all args.
304 * @param address - Address of the target host (cann be any address object)
305 * @param read_community_name - Community for get requests
306 * @param write_community_name - Community for set requests
308 CTarget(const Address &address,
309 const OctetStr &read_community_name,
310 const OctetStr &write_community_name);
313 * Constructor with only address.
315 * The read and write community names will be set to "public".
317 * @param address - Address of the target host (cann be any address object)
319 CTarget(const Address &address);
322 * Constructor from existing CTarget.
324 CTarget(const CTarget &target);
327 * Destructor, that has nothing to do.
334 * Clone operation for creating a new CTarget from an existing
337 * @note The caller MUST use the delete operation on the return
340 * @return A pointer to the new object on success, 0 on failure.
342 SnmpTarget *clone() const { return (SnmpTarget *) new CTarget(*this); };
345 * Get the read community name.
347 * @return C string of the read community.
349 const char * get_readcommunity() const
350 { return (const char *) read_community.get_printable(); };
353 * Get the read community name.
355 * @param oct - OctetStr that will be filled with the read community name.
357 void get_readcommunity(OctetStr& oct) const { oct = read_community; };
360 * Set the read community name.
362 * @param str - The new read community name
364 void set_readcommunity(const char * str) { read_community = str; };
367 * Set the read community name.
369 * @param oct - The new read community name
371 void set_readcommunity(const OctetStr& oct) { read_community = oct; };
374 * Get the write community name.
376 * @return C string of the write community.
378 const char * get_writecommunity() const
379 { return (const char *) write_community.get_printable(); };
382 * Get the write community name.
384 * @param oct - OctetStr that will be filled with the write community name.
386 void get_writecommunity(OctetStr &oct) const { oct = write_community; };
389 * Set the write community name.
391 * @param str - The new write community name
393 void set_writecommunity(const char *str) { write_community = str; };
396 * Set the write community name.
398 * @param oct - The new write community name
400 void set_writecommunity(const OctetStr &oct) { write_community = oct; };
403 * Overloaded assignment operator.
405 CTarget& operator=(const CTarget& target);
408 * Overloeaded compare operator.
410 * Two CTarget objects are considered equal, if all member variables
411 * and the base classes are equal.
413 * @return 1 if targets are equal, 0 if not.
415 int operator==(const CTarget &rhs) const;
418 * Get all values of a CTarget object.
420 * @param read_comm - Read community name
421 * @param write_comm - Write community name
422 * @param address - Address of the target
423 * @param t - Timeout value
424 * @param r - Retries value
425 * @param v - The SNMP version of this target
427 * @return TRUE on success and FALSE on failure.
429 int resolve_to_C(OctetStr& read_comm,
430 OctetStr& write_comm,
434 unsigned char &v) const;
442 OctetStr read_community; // get community
443 OctetStr write_community; // set community
446 // create OidCollection type
447 typedef SnmpCollection<SnmpTarget> TargetCollection;
450 #define INITIAL_USER "initial"
452 #define INITIAL_USER "public"
455 //----[ UTarget class ]----------------------------------------------
459 * This class is used for SNMPv3 targets.
461 class DLLOPT UTarget: public SnmpTarget
465 * Constructor with no args.
466 * The validity of the target will be false.
471 * Constructor with all args.
473 * @param address - Address of the target host (cann be any address object)
474 * @param sec_name - The security name
475 * @param sec_model - The security model to use
477 UTarget(const Address &address,
478 const char *sec_name,
479 const int sec_model);
482 * Constructor with all args.
484 * @param address - Address of the target host (cann be any address object)
485 * @param sec_name - The security name
486 * @param sec_model - The security model to use
488 UTarget(const Address &address,
489 const OctetStr &sec_name,
490 const int sec_model);
493 * Constructor with only address.
495 * Assumes the following defaults: security_name: initial, version: SNMPv3,
496 * security_model: v3MP.
498 * @param address - Address of the target host (cann be any address object)
500 UTarget(const Address &address);
503 * Constructor from existing UTarget.
505 UTarget(const UTarget &target);
508 * Destructor, that has nothing to do.
515 * Clone operation for creating a new UTarget from an existing
518 * @note The caller MUST use the delete operation on the return
521 * @return A pointer to the new object on success, 0 on failure.
523 SnmpTarget *clone() const { return (SnmpTarget *) new UTarget(*this); };
526 * Set the address object.
528 * This method is the same as in SnmpTarget, but it deletes engine_id.
530 * @param address - The address that this target should use.
531 * @return TRUE on success.
533 int set_address(const Address &address);
536 * Get the security name.
538 * @return A const reference to the security name.
540 const OctetStr& get_security_name() const { return security_name;} ;
543 * Get the security name.
545 * @param oct - OctetStr that will be filled with the security name.
547 void get_security_name(OctetStr& oct) const { oct = security_name; };
550 * Set the security name.
552 * @param str - The new security name
554 void set_security_name(const char * str) { security_name = str; };
557 * Set the security name.
559 * @param oct - The new security name
561 void set_security_name(const OctetStr& oct) { security_name = oct; };
567 * In most cases it is not necessary for the user to set the engine
568 * id as snmp++ performs engine id discovery. If the engine id is
569 * set by the user, no engine_id discovery is made, even if the
570 * engine id set by the user is wrong.
572 * @param str - The engine id to use
574 void set_engine_id(const char * str) { engine_id = str; };
579 * In most cases it is not necessary for the user to set the engine
580 * id as snmp++ performs engine id discovery. If the engine id is
581 * set by the user, no engine_id discovery is made, even if the
582 * engine id set by the user is wrong.
584 * @param oct - The engine id to use
586 void set_engine_id(const OctetStr &oct) { engine_id = oct; };
591 * @return A const reference to the enigne id of this target.
593 const OctetStr& get_engine_id() const { return engine_id; };
598 * @param oct - OctetStr that will be filled with the engine id
600 void get_engine_id(OctetStr& oct) const { oct = engine_id; };
604 * Get the security_model.
606 * @return An integer representing the security_model of this target.
608 int get_security_model() const { return security_model; };
611 * Set the security_model.
613 * @param sec_model - The security model to use.
615 void set_security_model(int sec_model) { security_model = sec_model; };
618 * Overloaded assignment operator.
620 UTarget& operator=(const UTarget& target);
623 * Overloeaded compare operator.
625 * Two UTarget objects are considered equal, if all member variables
626 * (beside the engine id) and the base classes are equal.
628 * @return 1 if targets are equal, 0 if not.
630 virtual int operator==(const UTarget &rhs) const;
633 * Get all values of a UTarget object.
635 * @param sec_name - security name
636 * @param sec_model - security model
637 * @param address - Address of the target
638 * @param t - Timeout value
639 * @param r - Retries value
640 * @param v - The SNMP version of this target
642 * @return TRUE on success and FALSE on failure.
644 int resolve_to_U(OctetStr& sec_name,
649 unsigned char &v) const;
657 OctetStr security_name;
664 #ifdef SNMP_PP_NAMESPACE
665 } // end of namespace Snmp_pp