SNMP++  3.3.11
target.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## target.h
4  _##
5  _## SNMP++ v3.3
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2013 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 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.
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  * Overloeaded 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  protected:
264  bool validity; ///< Validity of the object
265  unsigned long timeout; ///< xmit timeout in 10 milli secs
266  int retries; ///< number of retries
267  snmp_version version; ///< SNMP version to use
268  target_type ttype; ///< Type of the target
269  GenAddress my_address; ///< Address object
270 
271  static unsigned long default_timeout; ///< default timeout for new objects
272  static int default_retries; ///< default retries for new objects
273 };
274 
275 //----[ CTarget class ]----------------------------------------------
276 /**
277  * Community based target object.
278  * This target can be used for SNMPv1 and SNMPv2c messages.
279  */
280 class DLLOPT CTarget: public SnmpTarget
281 {
282  public:
283  /**
284  * Constructor with no args.
285  * The validity of the target will be false.
286  */
287  CTarget();
288 
289  /**
290  * Constructor with all args.
291  *
292  * @param address - Address of the target host (cann be any address object)
293  * @param read_community_name - Community for get requests
294  * @param write_community_name - Community for set requests
295  */
296  CTarget(const Address &address,
297  const char *read_community_name,
298  const char *write_community_name);
299 
300  /**
301  * Constructor with all args.
302  *
303  * @param address - Address of the target host (cann be any address object)
304  * @param read_community_name - Community for get requests
305  * @param write_community_name - Community for set requests
306  */
307  CTarget(const Address &address,
308  const OctetStr &read_community_name,
309  const OctetStr &write_community_name);
310 
311  /**
312  * Constructor with only address.
313  *
314  * The read and write community names will be set to "public".
315  *
316  * @param address - Address of the target host (cann be any address object)
317  */
318  CTarget(const Address &address);
319 
320  /**
321  * Constructor from existing CTarget.
322  */
323  CTarget(const CTarget &target);
324 
325  /**
326  * Destructor, that has nothing to do.
327  */
328  ~CTarget() {};
329 
330  /**
331  * Clone operator.
332  *
333  * Clone operation for creating a new CTarget from an existing
334  * CTarget.
335  *
336  * @note The caller MUST use the delete operation on the return
337  * value when done.
338  *
339  * @return A pointer to the new object on success, 0 on failure.
340  */
341  SnmpTarget *clone() const { return (SnmpTarget *) new CTarget(*this); };
342 
343  /**
344  * Get the read community name.
345  *
346  * @return C string of the read community.
347  */
348  const char * get_readcommunity() const
349  { return (const char *) read_community.get_printable(); };
350 
351  /**
352  * Get the read community name.
353  *
354  * @param oct - OctetStr that will be filled with the read community name.
355  */
356  void get_readcommunity(OctetStr& oct) const { oct = read_community; };
357 
358  /**
359  * Set the read community name.
360  *
361  * @param str - The new read community name
362  */
363  void set_readcommunity(const char * str) { read_community = str; };
364 
365  /**
366  * Set the read community name.
367  *
368  * @param oct - The new read community name
369  */
370  void set_readcommunity(const OctetStr& oct) { read_community = oct; };
371 
372  /**
373  * Get the write community name.
374  *
375  * @return C string of the write community.
376  */
377  const char * get_writecommunity() const
378  { return (const char *) write_community.get_printable(); };
379 
380  /**
381  * Get the write community name.
382  *
383  * @param oct - OctetStr that will be filled with the write community name.
384  */
385  void get_writecommunity(OctetStr &oct) const { oct = write_community; };
386 
387  /**
388  * Set the write community name.
389  *
390  * @param str - The new write community name
391  */
392  void set_writecommunity(const char *str) { write_community = str; };
393 
394  /**
395  * Set the write community name.
396  *
397  * @param oct - The new write community name
398  */
399  void set_writecommunity(const OctetStr &oct) { write_community = oct; };
400 
401  /**
402  * Overloaded assignment operator.
403  */
404  CTarget& operator=(const CTarget& target);
405 
406  /**
407  * Overloeaded compare operator.
408  *
409  * Two CTarget objects are considered equal, if all member variables
410  * and the base classes are equal.
411  *
412  * @return 1 if targets are equal, 0 if not.
413  */
414  int operator==(const CTarget &rhs) const;
415 
416  /**
417  * Get all values of a CTarget object.
418  *
419  * @param read_comm - Read community name
420  * @param write_comm - Write community name
421  * @param address - Address of the target
422  * @param t - Timeout value
423  * @param r - Retries value
424  * @param v - The SNMP version of this target
425  *
426  * @return true on success and FALSE on failure.
427  */
428  bool resolve_to_C(OctetStr& read_comm,
429  OctetStr& write_comm,
430  GenAddress &address,
431  unsigned long &t,
432  int &r,
433  unsigned char &v) const;
434 
435  /**
436  * Reset the object.
437  */
438  void clear();
439 
440  protected:
441  OctetStr read_community; // get community
442  OctetStr write_community; // set community
443 };
444 
445 // create OidCollection type
447 
448 #ifdef _SNMPv3
449 #define INITIAL_USER "initial"
450 #else
451 #define INITIAL_USER "public"
452 #endif
453 
454 //----[ UTarget class ]----------------------------------------------
455 /**
456  * User based Target.
457  *
458  * This class is used for SNMPv3 targets.
459  */
460 class DLLOPT UTarget: public SnmpTarget
461 {
462  public:
463  /**
464  * Constructor with no args.
465  * The validity of the target will be false.
466  */
467  UTarget();
468 
469  /**
470  * Constructor with all args.
471  *
472  * @param address - Address of the target host (cann be any address object)
473  * @param sec_name - The security name
474  * @param sec_model - The security model to use
475  */
476  UTarget(const Address &address,
477  const char *sec_name,
478  const int sec_model);
479 
480  /**
481  * Constructor with all args.
482  *
483  * @param address - Address of the target host (cann be any address object)
484  * @param sec_name - The security name
485  * @param sec_model - The security model to use
486  */
487  UTarget(const Address &address,
488  const OctetStr &sec_name,
489  const int sec_model);
490 
491  /**
492  * Constructor with only address.
493  *
494  * Assumes the following defaults: security_name: initial, version: SNMPv3,
495  * security_model: v3MP.
496  *
497  * @param address - Address of the target host (cann be any address object)
498  */
499  UTarget(const Address &address);
500 
501  /**
502  * Constructor from existing UTarget.
503  */
504  UTarget(const UTarget &target);
505 
506  /**
507  * Destructor, that has nothing to do.
508  */
509  ~UTarget() {};
510 
511  /**
512  * Clone operator.
513  *
514  * Clone operation for creating a new UTarget from an existing
515  * UTarget.
516  *
517  * @note The caller MUST use the delete operation on the return
518  * value when done.
519  *
520  * @return A pointer to the new object on success, 0 on failure.
521  */
522  SnmpTarget *clone() const { return (SnmpTarget *) new UTarget(*this); };
523 
524  /**
525  * Set the address object.
526  *
527  * This method is the same as in SnmpTarget, but it deletes engine_id.
528  *
529  * @param address - The address that this target should use.
530  * @return true on success.
531  */
532  bool set_address(const Address &address);
533 
534  /**
535  * Get the security name.
536  *
537  * @return A const reference to the security name.
538  */
539  const OctetStr& get_security_name() const { return security_name;} ;
540 
541  /**
542  * Get the security name.
543  *
544  * @param oct - OctetStr that will be filled with the security name.
545  */
546  void get_security_name(OctetStr& oct) const { oct = security_name; };
547 
548  /**
549  * Set the security name.
550  *
551  * @param str - The new security name
552  */
553  void set_security_name(const char * str) { security_name = str; };
554 
555  /**
556  * Set the security name.
557  *
558  * @param oct - The new security name
559  */
560  void set_security_name(const OctetStr& oct) { security_name = oct; };
561 
562 #ifdef _SNMPv3
563  /**
564  * Set the engine id.
565  *
566  * In most cases it is not necessary for the user to set the engine
567  * id as snmp++ performs engine id discovery. If the engine id is
568  * set by the user, no engine_id discovery is made, even if the
569  * engine id set by the user is wrong.
570  *
571  * @param str - The engine id to use
572  */
573  void set_engine_id(const char * str) { engine_id = str; };
574 
575  /**
576  * Set the engine id.
577  *
578  * In most cases it is not necessary for the user to set the engine
579  * id as snmp++ performs engine id discovery. If the engine id is
580  * set by the user, no engine_id discovery is made, even if the
581  * engine id set by the user is wrong.
582  *
583  * @param oct - The engine id to use
584  */
585  void set_engine_id(const OctetStr &oct) { engine_id = oct; };
586 
587  /**
588  * Get the engine id.
589  *
590  * @return A const reference to the enigne id of this target.
591  */
592  const OctetStr& get_engine_id() const { return engine_id; };
593 
594  /**
595  * Get the engine id.
596  *
597  * @param oct - OctetStr that will be filled with the engine id
598  */
599  void get_engine_id(OctetStr& oct) const { oct = engine_id; };
600 #endif
601 
602  /**
603  * Get the security_model.
604  *
605  * @return An integer representing the security_model of this target.
606  */
607  int get_security_model() const { return security_model; };
608 
609  /**
610  * Set the security_model.
611  *
612  * @param sec_model - The security model to use.
613  */
614  void set_security_model(int sec_model) { security_model = sec_model; };
615 
616  /**
617  * Overloaded assignment operator.
618  */
619  UTarget& operator=(const UTarget& target);
620 
621  /**
622  * Overloeaded compare operator.
623  *
624  * Two UTarget objects are considered equal, if all member variables
625  * (beside the engine id) and the base classes are equal.
626  *
627  * @return 1 if targets are equal, 0 if not.
628  */
629  virtual int operator==(const UTarget &rhs) const;
630 
631  /**
632  * Get all values of a UTarget object.
633  *
634  * @param sec_name - security name
635  * @param sec_model - security model
636  * @param address - Address of the target
637  * @param t - Timeout value
638  * @param r - Retries value
639  * @param v - The SNMP version of this target
640  *
641  * @return TRUE on success and FALSE on failure.
642  */
643  bool resolve_to_U(OctetStr& sec_name,
644  int &sec_model,
645  GenAddress &address,
646  unsigned long &t,
647  int &r,
648  unsigned char &v) const;
649 
650  /**
651  * Reset the object.
652  */
653  void clear();
654 
655  protected:
658 #ifdef _SNMPv3
660 #endif
661 };
662 
663 #ifdef SNMP_PP_NAMESPACE
664 } // end of namespace Snmp_pp
665 #endif
666 
667 #endif // _SNMP_TARGET_H_
int retries
number of retries
Definition: target.h:266
GenAddress my_address
Address object.
Definition: target.h:269
OctetStr write_community
Definition: target.h:442
void set_engine_id(const char *str)
Set the engine id.
Definition: target.h:573
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:356
target_type ttype
Type of the target.
Definition: target.h:268
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:522
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:399
void set_security_name(const char *str)
Set the security name.
Definition: target.h:553
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:264
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_engine_id(const OctetStr &oct)
Set the engine id.
Definition: target.h:585
const char * get_writecommunity() const
Get the write community name.
Definition: target.h:377
OctetStr read_community
Definition: target.h:441
SnmpTarget()
Create a SnmpTarget object with default values.
Definition: target.h:111
Community based target object.
Definition: target.h:280
#define DLLOPT
const OctetStr & get_security_name() const
Get the security name.
Definition: target.h:539
static int default_retries
default retries for new objects
Definition: target.h:272
void get_security_name(OctetStr &oct) const
Get the security name.
Definition: target.h:546
static unsigned long default_timeout
default timeout for new objects
Definition: target.h:271
It is a SnmpTarget object.
Definition: target.h:102
void set_security_name(const OctetStr &oct)
Set the security name.
Definition: target.h:560
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:125
~UTarget()
Destructor, that has nothing to do.
Definition: target.h:509
void set_readcommunity(const char *str)
Set the read community name.
Definition: target.h:363
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:267
int get_retry() const
Get the retry value.
Definition: target.h:157
~CTarget()
Destructor, that has nothing to do.
Definition: target.h:328
void set_writecommunity(const char *str)
Set the write community name.
Definition: target.h:392
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:656
void set_security_model(int sec_model)
Set the security_model.
Definition: target.h:614
unsigned long timeout
xmit timeout in 10 milli secs
Definition: target.h:265
void get_engine_id(OctetStr &oct) const
Get the engine id.
Definition: target.h:599
(1) SNMPv2c
Definition: target.h:79
int get_security_model() const
Get the security_model.
Definition: target.h:607
void set_readcommunity(const OctetStr &oct)
Set the read community name.
Definition: target.h:370
int security_model
Definition: target.h:657
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:592
OctetStr engine_id
Definition: target.h:659
(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:446
const char * get_readcommunity() const
Get the read community name.
Definition: target.h:348
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:341
User based Target.
Definition: target.h:460
void get_writecommunity(OctetStr &oct) const
Get the write community name.
Definition: target.h:385