SNMP++  3.6.3
target.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## target.h
4  _##
5  _## SNMP++ v3.4
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2024 Jochen Katz, Frank Fock
8  _##
9  _## This software is based on SNMP++2.6 from Hewlett Packard:
10  _##
11  _## Copyright (c) 1996
12  _## Hewlett-Packard Company
13  _##
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, Frank Fock, and Jochen Katz make no representations
21  _## about the 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.
25  _##
26  _##########################################################################*/
27 /*===================================================================
28 
29  Copyright (c) 1999
30  Hewlett-Packard Company
31 
32  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
33  Permission to use, copy, modify, distribute and/or sell this software
34  and/or its documentation is hereby granted without fee. User agrees
35  to display the above copyright notice and this license notice in all
36  copies of the software and any documentation of the software. User
37  agrees to assume all liability for the use of the software; Hewlett-Packard
38  makes no representations about the suitability of this software for any
39  purpose. It is provided "AS-IS" without warranty of any kind,either express
40  or implied. User hereby grants a royalty-free license to any and all
41  derivatives based upon this software code base.
42 
43 
44  SNMP++ T A R G E T . H
45 
46  TARGET CLASS DEFINITION
47 
48  DESIGN + AUTHOR: Peter E Mellquist
49 
50  DESCRIPTION:
51  Target class defines target SNMP agents.
52 
53 =====================================================================*/
54 // $Id$
55 
56 #ifndef _SNMP_TARGET_H_
57 #define _SNMP_TARGET_H_
58 
59 //----[ includes ]-----------------------------------------------------
60 
61 #include <libsnmp.h>
62 #include "snmp_pp/config_snmp_pp.h"
63 #include "snmp_pp/address.h"
64 #include "snmp_pp/octet.h"
65 #include "snmp_pp/collect.h"
66 
67 #ifdef SNMP_PP_NAMESPACE
68 namespace Snmp_pp {
69 #endif
70 
71 
72 //----[ enumerated types for SNMP versions ]---------------------------
73 /**
74  * The SNMP version to use is passed with this enum.
75  */
77 {
78  version1, ///< (0) SNMPv1
79  version2c ///< (1) SNMPv2c
80 #ifdef _SNMPv3
81  ,version2stern, ///< (2) Dont use this!
82  version3 ///< (3) SNMPv3
83 #endif
84 };
85 
86 //----[ Target class ]-------------------------------------------------
87 /**
88  * Abstract class used to provide a virtual interface into Targets.
89  *
90  * @note Although it is possible to create an object of this class,
91  * you won't be happy with that...
92  */
94 {
95  public:
96 
97  /**
98  * Enum to identify a target object through SnmpTarget::get_type() method.
99  */
101  {
102  type_base, ///< It is a SnmpTarget object
103  type_ctarget, ///< It is a CTarget object
104  type_utarget ///< It is a Utarget object
105  };
106 
107  /**
108  * Create a SnmpTarget object with default values.
109  * The validity of the target will be false.
110  */
112  : validity(false), timeout(default_timeout), retries(default_retries),
113  version(version1), ttype(type_base) {};
114 
115  /**
116  * Create a SnmpTarget object with the given Address.
117  */
118  SnmpTarget(const Address &address)
119  : validity(false), timeout(default_timeout), retries(default_retries),
120  version(version1), ttype(type_base), my_address(address)
121  { if (my_address.valid()) validity = true; };
122 
123  /**
124  * Destructor that has nothing to do.
125  */
126  virtual ~SnmpTarget() {};
127 
128  /**
129  * Return the type of the target object.
130  *
131  * If a SNMP message is received through a callback (that only
132  * passes a SnmpTarget pointer to the callback function), this
133  * method can be used to check the type of the object before doing a
134  * cast to CTarget or UTarget.
135  */
136  target_type get_type() const { return ttype; };
137 
138  /**
139  * Returns the validity of the target object.
140  *
141  * @return true, if the target is valid.
142  */
143  bool valid() const { return validity;};
144 
145  /**
146  * Set the retry value.
147  *
148  * @param r - The number of retries if no response is received.
149  */
150  void set_retry(const int r) { retries = r; };
151 
152  /**
153  * Get the retry value.
154  *
155  * @return The number of retries on timeout.
156  */
157  int get_retry() const { return retries; };
158 
159 
160  /**
161  * Set the timeout for requests.
162  *
163  * The default timeout for requests is 1 second (100).
164  *
165  * @param t - Timeout in 10ms, so 100 will set the timeout to 1 second.
166  */
167  void set_timeout(const unsigned long t) { timeout = t; };
168 
169  /**
170  * Get the timeout.
171  *
172  * @return The timeout for requests sent using this target object.
173  */
174  unsigned long get_timeout() const { return timeout; };
175 
176  /**
177  * Change the default timeout.
178  *
179  * Changing the default timeout value will only have an effect for
180  * target objects that are created after setting this value.
181  *
182  * @param t - The new default timeout value
183  */
184  static void set_default_timeout(const unsigned long t)
185  { default_timeout = t; };
186 
187  /**
188  * Change the default retries vlaue.
189  *
190  * Changing the default retries value will only have an effect for
191  * target objects that are created after setting this value.
192  *
193  * @param r - The new retries value
194  */
195  static void set_default_retries(const int r) { default_retries = r; };
196 
197  /**
198  * Clone operator.
199  *
200  * Virtual clone operation for creating a new SnmpTarget from an existing
201  * SnmpTarget.
202  *
203  * @note The caller MUST use the delete operation on the return
204  * value when done.
205  *
206  * @return A pointer to the new object on success, 0 on failure.
207  */
208  virtual SnmpTarget *clone() const;
209 
210  /**
211  * Get the address object.
212  *
213  * @param address - GenAddress object to store the target address.
214  * @return true on success.
215  */
216  bool get_address(GenAddress &address) const;
217 
218  /**
219  * Get the address object.
220  *
221  * @return The target address.
222  */
223  const GenAddress &get_address() const { return my_address; };
224 
225  /**
226  * Set the address object.
227  *
228  * @param address - The address that this target should use.
229  * @return true on success.
230  */
231  virtual bool set_address(const Address &address);
232 
233  /**
234  * Get the SNMP version for this target.
235  *
236  * @return The SNMP version of this target object.
237  * @see enum snmp_version
238  */
239  snmp_version get_version() const { return version;};
240 
241  /**
242  * Set the SNMP version of this target.
243  *
244  * @param v - The SNMP version that should be used for sending messages.
245  */
246  void set_version(const snmp_version v) { version = v; };
247 
248  /**
249  * Overloaded compare operator.
250  *
251  * Two SnmpTarget objects are considered equal, if all member
252  * variables are equal.
253  *
254  * @return 1 if targets are equal, 0 if not.
255  */
256  int operator==(const SnmpTarget &rhs) const;
257 
258  /**
259  * Reset the object.
260  */
261  virtual void clear();
262 
263  /**
264  * Get a human readable string for the given SNMP version.
265  * @param version enum
266  * @return String for a valid version or "unknown".
267  */
268  static const char* version_to_string(const snmp_version version);
269 
270  protected:
271  bool validity; ///< Validity of the object
272  unsigned long timeout; ///< xmit timeout in 10 milli secs
273  int retries; ///< number of retries
274  snmp_version version; ///< SNMP version to use
275  target_type ttype; ///< Type of the target
276  GenAddress my_address; ///< Address object
277 
278  static unsigned long default_timeout; ///< default timeout for new objects
279  static int default_retries; ///< default retries for new objects
280 };
281 
282 //----[ CTarget class ]----------------------------------------------
283 /**
284  * Community based target object.
285  * This target can be used for SNMPv1 and SNMPv2c messages.
286  */
287 class DLLOPT CTarget: public SnmpTarget
288 {
289  public:
290  /**
291  * Constructor with no args.
292  * The validity of the target will be false.
293  */
294  CTarget();
295 
296  /**
297  * Constructor with all args.
298  *
299  * @param address - Address of the target host (cann be any address object)
300  * @param read_community_name - Community for get requests
301  * @param write_community_name - Community for set requests
302  */
303  CTarget(const Address &address,
304  const char *read_community_name,
305  const char *write_community_name);
306 
307  /**
308  * Constructor with all args.
309  *
310  * @param address - Address of the target host (cann be any address object)
311  * @param read_community_name - Community for get requests
312  * @param write_community_name - Community for set requests
313  */
314  CTarget(const Address &address,
315  const OctetStr &read_community_name,
316  const OctetStr &write_community_name);
317 
318  /**
319  * Constructor with only address.
320  *
321  * The read and write community names will be set to "public".
322  *
323  * @param address - Address of the target host (cann be any address object)
324  */
325  CTarget(const Address &address);
326 
327  /**
328  * Constructor from existing CTarget.
329  */
330  CTarget(const CTarget &target);
331 
332  /**
333  * Destructor, that has nothing to do.
334  */
335  ~CTarget() {};
336 
337  /**
338  * Clone operator.
339  *
340  * Clone operation for creating a new CTarget from an existing
341  * CTarget.
342  *
343  * @note The caller MUST use the delete operation on the return
344  * value when done.
345  *
346  * @return A pointer to the new object on success, 0 on failure.
347  */
348  SnmpTarget *clone() const { return (SnmpTarget *) new CTarget(*this); };
349 
350  /**
351  * Get the read community name.
352  *
353  * @return C string of the read community.
354  */
355  const char * get_readcommunity() const
356  { return (const char *) read_community.get_printable(); };
357 
358  /**
359  * Get the read community name.
360  *
361  * @param oct - OctetStr that will be filled with the read community name.
362  */
363  void get_readcommunity(OctetStr& oct) const { oct = read_community; };
364 
365  /**
366  * Set the read community name.
367  *
368  * @param str - The new read community name
369  */
370  void set_readcommunity(const char * str) { read_community = str; };
371 
372  /**
373  * Set the read community name.
374  *
375  * @param oct - The new read community name
376  */
377  void set_readcommunity(const OctetStr& oct) { read_community = oct; };
378 
379  /**
380  * Get the write community name.
381  *
382  * @return C string of the write community.
383  */
384  const char * get_writecommunity() const
385  { return (const char *) write_community.get_printable(); };
386 
387  /**
388  * Get the write community name.
389  *
390  * @param oct - OctetStr that will be filled with the write community name.
391  */
392  void get_writecommunity(OctetStr &oct) const { oct = write_community; };
393 
394  /**
395  * Set the write community name.
396  *
397  * @param str - The new write community name
398  */
399  void set_writecommunity(const char *str) { write_community = str; };
400 
401  /**
402  * Set the write community name.
403  *
404  * @param oct - The new write community name
405  */
406  void set_writecommunity(const OctetStr &oct) { write_community = oct; };
407 
408  /**
409  * Overloaded assignment operator.
410  */
411  CTarget& operator=(const CTarget& target);
412 
413  /**
414  * Overloeaded compare operator.
415  *
416  * Two CTarget objects are considered equal, if all member variables
417  * and the base classes are equal.
418  *
419  * @return 1 if targets are equal, 0 if not.
420  */
421  int operator==(const CTarget &rhs) const;
422 
423  /**
424  * Get all values of a CTarget object.
425  *
426  * @param read_comm - Read community name
427  * @param write_comm - Write community name
428  * @param address - Address of the target
429  * @param t - Timeout value
430  * @param r - Retries value
431  * @param v - The SNMP version of this target
432  *
433  * @return true on success and FALSE on failure.
434  */
435  bool resolve_to_C(OctetStr& read_comm,
436  OctetStr& write_comm,
437  GenAddress &address,
438  unsigned long &t,
439  int &r,
440  unsigned char &v) const;
441 
442  /**
443  * Reset the object.
444  */
445  void clear();
446 
447  protected:
448  OctetStr read_community; // get community
449  OctetStr write_community; // set community
450 };
451 
452 // create OidCollection type
454 
455 #ifdef _SNMPv3
456 #define INITIAL_USER "initial"
457 #else
458 #define INITIAL_USER "public"
459 #endif
460 
461 //----[ UTarget class ]----------------------------------------------
462 /**
463  * User based Target.
464  *
465  * This class is used for SNMPv3 targets.
466  */
467 class DLLOPT UTarget: public SnmpTarget
468 {
469  public:
470  /**
471  * Constructor with no args.
472  * The validity of the target will be false.
473  */
474  UTarget();
475 
476  /**
477  * Constructor with all args.
478  *
479  * @param address - Address of the target host (cann be any address object)
480  * @param sec_name - The security name
481  * @param sec_model - The security model to use
482  */
483  UTarget(const Address &address,
484  const char *sec_name,
485  const int sec_model);
486 
487  /**
488  * Constructor with all args.
489  *
490  * @param address - Address of the target host (cann be any address object)
491  * @param sec_name - The security name
492  * @param sec_model - The security model to use
493  */
494  UTarget(const Address &address,
495  const OctetStr &sec_name,
496  const int sec_model);
497 
498  /**
499  * Constructor with only address.
500  *
501  * Assumes the following defaults: security_name: initial, version: SNMPv3,
502  * security_model: v3MP.
503  *
504  * @param address - Address of the target host (cann be any address object)
505  */
506  UTarget(const Address &address);
507 
508  /**
509  * Constructor from existing UTarget.
510  */
511  UTarget(const UTarget &target);
512 
513  /**
514  * Destructor, that deletes coexistence security name if present.
515  */
517 # ifdef _SNMPv3
518  if (coexistence_security_name) {
519  delete coexistence_security_name;
520  }
521 # endif
522  };
523 
524  /**
525  * Clone operator.
526  *
527  * Clone operation for creating a new UTarget from an existing
528  * UTarget.
529  *
530  * @note The caller MUST use the delete operation on the return
531  * value when done.
532  *
533  * @return A pointer to the new object on success, 0 on failure.
534  */
535  SnmpTarget *clone() const { return (SnmpTarget *) new UTarget(*this); };
536 
537  /**
538  * Set the address object.
539  *
540  * This method is the same as in SnmpTarget, but it deletes engine_id.
541  *
542  * @param address - The address that this target should use.
543  * @return true on success.
544  */
545  bool set_address(const Address &address);
546 
547  /**
548  * Get the security name.
549  *
550  * @return A const reference to the security name.
551  */
552  const OctetStr& get_security_name() const { return security_name;} ;
553 
554  /**
555  * Get the security name.
556  *
557  * @param oct - OctetStr that will be filled with the security name.
558  */
559  void get_security_name(OctetStr& oct) const { oct = security_name; };
560 
561  /**
562  * Set the security name.
563  *
564  * @param str - The new security name
565  */
566  void set_security_name(const char * str) { security_name = str; };
567 
568  /**
569  * Set the security name.
570  *
571  * @param oct - The new security name
572  */
573  void set_security_name(const OctetStr& oct) { security_name = oct; };
574 
575 #ifdef _SNMPv3
576  /**
577  * Set the engine id.
578  *
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.
583  *
584  * @param str - The engine id to use
585  */
586  void set_engine_id(const char * str) { engine_id = str; };
587 
588  /**
589  * Set the engine id.
590  *
591  * In most cases it is not necessary for the user to set the engine
592  * id as snmp++ performs engine id discovery. If the engine id is
593  * set by the user, no engine_id discovery is made, even if the
594  * engine id set by the user is wrong.
595  *
596  * @param oct - The engine id to use
597  */
598  void set_engine_id(const OctetStr &oct) { engine_id = oct; };
599 
600  /**
601  * Get the engine id.
602  *
603  * @return A const reference to the engine id of this target.
604  */
605  const OctetStr& get_engine_id() const { return engine_id; };
606 
607  /**
608  * Get the engine id.
609  *
610  * @param oct - OctetStr that will be filled with the engine id
611  */
612  void get_engine_id(OctetStr& oct) const { oct = engine_id; };
613 
614  /**
615  * Set the security name and store the previously set security name as
616  * coexistence_security_name that can be restored with unset_coexistence_security_name.
617  *
618  * @param oct - The new security name mapped through coexistence info
619  * @since 3.6.0
620  */
622  if (coexistence_security_name) {
623  delete coexistence_security_name;
624  }
625  coexistence_security_name = new OctetStr(security_name);
626  security_name = oct;
627  };
628 
629  /**
630  * Set the security_name to the previously saved coexistence_security_name (if present) and delete that saved value.
631  * Otherwise, do nothing.
632  * @since 3.6.0
633  */
635  if (coexistence_security_name) {
636  security_name = *coexistence_security_name;
637  delete coexistence_security_name;
638  coexistence_security_name = nullptr;
639  }
640  }
641 
642 #endif
643 
644  /**
645  * Get the security_model.
646  *
647  * @return An integer representing the security_model of this target.
648  */
649  int get_security_model() const { return security_model; };
650 
651  /**
652  * Set the security_model.
653  *
654  * @param sec_model - The security model to use.
655  */
656  void set_security_model(int sec_model) { security_model = sec_model; };
657 
658  /**
659  * Overloaded assignment operator.
660  */
661  UTarget& operator=(const UTarget& target);
662 
663  /**
664  * Overloaded compare operator.
665  *
666  * Two UTarget objects are considered equal, if all member variables
667  * (beside the engine id) and the base classes are equal.
668  *
669  * @return 1 if targets are equal, 0 if not.
670  */
671  virtual int operator==(const UTarget &rhs) const;
672 
673  /**
674  * Get all values of a UTarget object.
675  *
676  * @param sec_name - security name
677  * @param sec_model - security model
678  * @param address - Address of the target
679  * @param t - Timeout value
680  * @param r - Retries value
681  * @param v - The SNMP version of this target
682  *
683  * @return TRUE on success and FALSE on failure.
684  */
685  bool resolve_to_U(OctetStr& sec_name,
686  int &sec_model,
687  GenAddress &address,
688  unsigned long &t,
689  int &r,
690  unsigned char &v) const;
691 
692  /**
693  * Reset the object.
694  */
695  void clear();
696 
697  protected:
700 #ifdef _SNMPv3
703 #endif
704 };
705 
706 #ifdef SNMP_PP_NAMESPACE
707 } // end of namespace Snmp_pp
708 #endif
709 
710 #endif // _SNMP_TARGET_H_
int retries
number of retries
Definition: target.h:273
GenAddress my_address
Address object.
Definition: target.h:276
OctetStr write_community
Definition: target.h:449
void set_engine_id(const char *str)
Set the engine id.
Definition: target.h:586
bool valid() const
Returns the validity of the target object.
Definition: target.h:143
void get_readcommunity(OctetStr &oct) const
Get the read community name.
Definition: target.h:363
target_type ttype
Type of the target.
Definition: target.h:275
SnmpTarget(const Address &address)
Create a SnmpTarget object with the given Address.
Definition: target.h:118
target_type
Enum to identify a target object through SnmpTarget::get_type() method.
Definition: target.h:100
SnmpTarget * clone() const
Clone operator.
Definition: target.h:535
unsigned long get_timeout() const
Get the timeout.
Definition: target.h:174
void set_writecommunity(const OctetStr &oct)
Set the write community name.
Definition: target.h:406
void set_security_name(const char *str)
Set the security name.
Definition: target.h:566
void set_version(const snmp_version v)
Set the SNMP version of this target.
Definition: target.h:246
bool validity
Validity of the object.
Definition: target.h:271
static void set_default_retries(const int r)
Change the default retries vlaue.
Definition: target.h:195
void set_timeout(const unsigned long t)
Set the timeout for requests.
Definition: target.h:167
void set_coexistence_security_name(const OctetStr &oct)
Set the security name and store the previously set security name as coexistence_security_name that ca...
Definition: target.h:621
void set_engine_id(const OctetStr &oct)
Set the engine id.
Definition: target.h:598
const char * get_writecommunity() const
Get the write community name.
Definition: target.h:384
OctetStr read_community
Definition: target.h:448
SnmpTarget()
Create a SnmpTarget object with default values.
Definition: target.h:111
Community based target object.
Definition: target.h:287
#define DLLOPT
const OctetStr & get_security_name() const
Get the security name.
Definition: target.h:552
static int default_retries
default retries for new objects
Definition: target.h:279
void get_security_name(OctetStr &oct) const
Get the security name.
Definition: target.h:559
static unsigned long default_timeout
default timeout for new objects
Definition: target.h:278
It is a SnmpTarget object.
Definition: target.h:102
void set_security_name(const OctetStr &oct)
Set the security name.
Definition: target.h:573
snmp_version get_version() const
Get the SNMP version for this target.
Definition: target.h:239
Base class of all Address classes.
Definition: address.h:127
~UTarget()
Destructor, that deletes coexistence security name if present.
Definition: target.h:516
void set_readcommunity(const char *str)
Set the read community name.
Definition: target.h:370
void set_retry(const int r)
Set the retry value.
Definition: target.h:150
snmp_version
The SNMP version to use is passed with this enum.
Definition: target.h:76
snmp_version version
SNMP version to use.
Definition: target.h:274
int get_retry() const
Get the retry value.
Definition: target.h:157
~CTarget()
Destructor, that has nothing to do.
Definition: target.h:335
void set_writecommunity(const char *str)
Set the write community name.
Definition: target.h:399
target_type get_type() const
Return the type of the target object.
Definition: target.h:136
Definition: octet.h:67
(2) Dont use this!
Definition: target.h:81
virtual ~SnmpTarget()
Destructor that has nothing to do.
Definition: target.h:126
OctetStr security_name
Definition: target.h:698
void set_security_model(int sec_model)
Set the security_model.
Definition: target.h:656
unsigned long timeout
xmit timeout in 10 milli secs
Definition: target.h:272
void get_engine_id(OctetStr &oct) const
Get the engine id.
Definition: target.h:612
(1) SNMPv2c
Definition: target.h:79
void unset_coexistence_security_name()
Set the security_name to the previously saved coexistence_security_name (if present) and delete that ...
Definition: target.h:634
int get_security_model() const
Get the security_model.
Definition: target.h:649
OctetStr * coexistence_security_name
Definition: target.h:702
void set_readcommunity(const OctetStr &oct)
Set the read community name.
Definition: target.h:377
int security_model
Definition: target.h:699
It is a CTarget object.
Definition: target.h:103
(0) SNMPv1
Definition: target.h:78
const OctetStr & get_engine_id() const
Get the engine id.
Definition: target.h:605
OctetStr engine_id
Definition: target.h:701
(3) SNMPv3
Definition: target.h:82
const GenAddress & get_address() const
Get the address object.
Definition: target.h:223
SnmpCollection< SnmpTarget > TargetCollection
Definition: target.h:453
const char * get_readcommunity() const
Get the read community name.
Definition: target.h:355
Abstract class used to provide a virtual interface into Targets.
Definition: target.h:93
static void set_default_timeout(const unsigned long t)
Change the default timeout.
Definition: target.h:184
SnmpTarget * clone() const
Clone operator.
Definition: target.h:348
User based Target.
Definition: target.h:467
void get_writecommunity(OctetStr &oct) const
Get the write community name.
Definition: target.h:392