00001 /*_############################################################################ 00002 _## 00003 _## target.h 00004 _## 00005 _## SNMP++v3.2.15 00006 _## ----------------------------------------------- 00007 _## Copyright (c) 2001-2004 Jochen Katz, Frank Fock 00008 _## 00009 _## This software is based on SNMP++2.6 from Hewlett Packard: 00010 _## 00011 _## Copyright (c) 1996 00012 _## Hewlett-Packard Company 00013 _## 00014 _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. 00015 _## Permission to use, copy, modify, distribute and/or sell this software 00016 _## and/or its documentation is hereby granted without fee. User agrees 00017 _## to display the above copyright notice and this license notice in all 00018 _## copies of the software and any documentation of the software. User 00019 _## agrees to assume all liability for the use of the software; 00020 _## Hewlett-Packard and Jochen Katz make no representations about the 00021 _## suitability of this software for any purpose. It is provided 00022 _## "AS-IS" without warranty of any kind, either express or implied. User 00023 _## hereby grants a royalty-free license to any and all derivatives based 00024 _## upon this software code base. 00025 _## 00026 _## Stuttgart, Germany, Tue Jan 4 21:42:42 CET 2005 00027 _## 00028 _##########################################################################*/ 00029 /*=================================================================== 00030 00031 Copyright (c) 1999 00032 Hewlett-Packard Company 00033 00034 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. 00035 Permission to use, copy, modify, distribute and/or sell this software 00036 and/or its documentation is hereby granted without fee. User agrees 00037 to display the above copyright notice and this license notice in all 00038 copies of the software and any documentation of the software. User 00039 agrees to assume all liability for the use of the software; Hewlett-Packard 00040 makes no representations about the suitability of this software for any 00041 purpose. It is provided "AS-IS" without warranty of any kind,either express 00042 or implied. User hereby grants a royalty-free license to any and all 00043 derivatives based upon this software code base. 00044 00045 00046 SNMP++ T A R G E T . H 00047 00048 TARGET CLASS DEFINITION 00049 00050 DESIGN + AUTHOR: 00051 Peter E Mellquist 00052 00053 LANGUAGE: 00054 ANSI C++ 00055 00056 OPERATING SYSTEMS: 00057 DOS/WINDOWS 3.1 00058 BSD UNIX 00059 00060 DESCRIPTION: 00061 Target class defines target SNMP agents. 00062 00063 =====================================================================*/ 00064 // $Id: target.h,v 1.7 2004/06/20 18:49:21 katz Exp $ 00065 00066 #ifndef _TARGET 00067 #define _TARGET 00068 00069 //----[ includes ]----------------------------------------------------- 00070 00071 #include "snmp_pp/config_snmp_pp.h" 00072 #include "snmp_pp/address.h" 00073 #include "snmp_pp/octet.h" 00074 #include "snmp_pp/collect.h" 00075 00076 #ifdef SNMP_PP_NAMESPACE 00077 namespace Snmp_pp { 00078 #endif 00079 00080 00081 //----[ enumerated types for SNMP versions ]--------------------------- 00082 /** 00083 * The SNMP version to use is passed with this enum. 00084 */ 00085 enum snmp_version 00086 { 00087 version1, ///< (0) SNMPv1 00088 version2c ///< (1) SNMPv2c 00089 #ifdef _SNMPv3 00090 ,version2stern, ///< (2) Dont use this! 00091 version3 ///< (3) SNMPv3 00092 #endif 00093 }; 00094 00095 //----[ Target class ]------------------------------------------------- 00096 /** 00097 * Abstract class used to provide a virtual interface into Targets. 00098 * 00099 * @note Although it is possible to create an object of this class, 00100 * you won't be happy with that... 00101 */ 00102 class DLLOPT SnmpTarget 00103 { 00104 public: 00105 00106 /** 00107 * Enum to identify a target object through SnmpTarget::get_type() method. 00108 */ 00109 enum target_type 00110 { 00111 type_base, ///< It is a SnmpTarget object 00112 type_ctarget, ///< It is a CTarget object 00113 type_utarget ///< It is a Utarget object 00114 }; 00115 00116 /** 00117 * Create a SnmpTarget object with default values. 00118 * The validity of the target will be false. 00119 */ 00120 SnmpTarget() 00121 : validity(false), timeout(default_timeout), retries(default_retries), 00122 version(version1), ttype(type_base) {}; 00123 00124 /** 00125 * Create a SnmpTarget object with the given Address. 00126 */ 00127 SnmpTarget( const Address &address) 00128 : validity(false), timeout(default_timeout), retries(default_retries), 00129 version(version1), ttype(type_base), my_address(address) 00130 { if (my_address.valid()) validity = true; }; 00131 00132 /** 00133 * Destructor that has nothing to do. 00134 */ 00135 virtual ~SnmpTarget() {}; 00136 00137 /** 00138 * Return the type of the target object. 00139 * 00140 * If a SNMP message is received through a callback (that only 00141 * passes a SnmpTarget pointer to the callback function), this 00142 * method can be used to check the type of the object before doing a 00143 * cast to CTarget or UTarget. 00144 */ 00145 target_type get_type() const { return ttype; }; 00146 00147 /** 00148 * Returns the validity of the target object. 00149 * 00150 * @return true, if the target is valid. 00151 */ 00152 bool valid() const { return validity;}; 00153 00154 /** 00155 * Set the retry value. 00156 * 00157 * @param r - The number of retries if no response is received. 00158 */ 00159 void set_retry( const int r) { retries = r; }; 00160 00161 /** 00162 * Get the retry value. 00163 * 00164 * @return The number of retries on timeout. 00165 */ 00166 int get_retry() const { return retries; }; 00167 00168 00169 /** 00170 * Set the timeout for requests. 00171 * 00172 * The default timeout for requests is 1 second (100). 00173 * 00174 * @param t - Timeout in 10ms, so 100 will set the timeout to 1 second. 00175 */ 00176 void set_timeout( const unsigned long t) { timeout = t; }; 00177 00178 /** 00179 * Get the timeout. 00180 * 00181 * @return The timeout for requests sent using this target object. 00182 */ 00183 unsigned long get_timeout() const { return timeout; }; 00184 00185 /** 00186 * Change the default timeout. 00187 * 00188 * Changing the default timeout value will only have an effect for 00189 * target objects that are created after setting this value. 00190 * 00191 * @param t - The new default timeout value 00192 */ 00193 static void set_default_timeout( const unsigned long t) 00194 { default_timeout = t; }; 00195 00196 /** 00197 * Change the default retries vlaue. 00198 * 00199 * Changing the default retries value will only have an effect for 00200 * target objects that are created after setting this value. 00201 * 00202 * @param r - The new retries value 00203 */ 00204 static void set_default_retries( const int r) { default_retries = r; }; 00205 00206 /** 00207 * Clone operator. 00208 * 00209 * Virtual clone operation for creating a new SnmpTarget from an existing 00210 * SnmpTarget. 00211 * 00212 * @note The caller MUST use the delete operation on the return 00213 * value when done. 00214 * 00215 * @return A pointer to the new object on success, 0 on failure. 00216 */ 00217 virtual SnmpTarget *clone() const; 00218 00219 /** 00220 * Get the address object. 00221 * 00222 * @param address - GenAddress object to store the target address. 00223 * @return TRUE on success. 00224 */ 00225 int get_address( GenAddress &address) const; 00226 00227 /** 00228 * Get the address object. 00229 * 00230 * @return The target address. 00231 */ 00232 const GenAddress &get_address() const { return my_address; }; 00233 00234 /** 00235 * Set the address object. 00236 * 00237 * @param address - The address that this target should use. 00238 * @return TRUE on success. 00239 */ 00240 virtual int set_address( const Address &address); 00241 00242 /** 00243 * Get the SNMP version for this target. 00244 * 00245 * @return The SNMP version of this target object. 00246 * @see enum snmp_version 00247 */ 00248 snmp_version get_version() const { return version;}; 00249 00250 /** 00251 * Set the SNMP version of this target. 00252 * 00253 * @param v - The SNMP version that should be used for sending messages. 00254 */ 00255 void set_version( const snmp_version v) { version = v; }; 00256 00257 /** 00258 * Overloeaded compare operator. 00259 * 00260 * Two SnmpTarget objects are considered equal, if all member 00261 * variables are equal. 00262 * 00263 * @return 1 if targets are equal, 0 if not. 00264 */ 00265 int operator==(const SnmpTarget &rhs) const; 00266 00267 /** 00268 * Reset the object. 00269 */ 00270 void clear(); 00271 00272 protected: 00273 bool validity; ///< Validity of the object 00274 unsigned long timeout; ///< xmit timeout in 10 milli secs 00275 int retries; ///< number of retries 00276 snmp_version version; ///< SNMP version to use 00277 target_type ttype; ///< Type of the target 00278 GenAddress my_address; ///< Address object 00279 00280 static unsigned long default_timeout; ///< default timeout for new objects 00281 static int default_retries; ///< default retries for new objects 00282 }; 00283 00284 //----[ CTarget class ]---------------------------------------------- 00285 /** 00286 * Community based target object. 00287 * This target can be used for SNMPv1 and SNMPv2c messages. 00288 */ 00289 class DLLOPT CTarget: public SnmpTarget 00290 { 00291 public: 00292 /** 00293 * Constructor with no args. 00294 * The validity of the target will be false. 00295 */ 00296 CTarget(); 00297 00298 /** 00299 * Constructor with all args. 00300 * 00301 * @param address - Address of the target host (cann be any address object) 00302 * @param read_community_name - Community for get requests 00303 * @param write_community_name - Community for set requests 00304 */ 00305 CTarget( const Address &address, 00306 const char *read_community_name, 00307 const char *write_community_name); 00308 00309 /** 00310 * Constructor with all args. 00311 * 00312 * @param address - Address of the target host (cann be any address object) 00313 * @param read_community_name - Community for get requests 00314 * @param write_community_name - Community for set requests 00315 */ 00316 CTarget( const Address &address, 00317 const OctetStr &read_community_name, 00318 const OctetStr &write_community_name); 00319 00320 /** 00321 * Constructor with only address. 00322 * 00323 * The read and write community names will be set to "public". 00324 * 00325 * @param address - Address of the target host (cann be any address object) 00326 */ 00327 CTarget( const Address &address); 00328 00329 /** 00330 * Constructor from existing CTarget. 00331 */ 00332 CTarget( const CTarget &target); 00333 00334 /** 00335 * Destructor, that has nothing to do. 00336 */ 00337 ~CTarget() {}; 00338 00339 /** 00340 * Clone operator. 00341 * 00342 * Clone operation for creating a new CTarget from an existing 00343 * CTarget. 00344 * 00345 * @note The caller MUST use the delete operation on the return 00346 * value when done. 00347 * 00348 * @return A pointer to the new object on success, 0 on failure. 00349 */ 00350 SnmpTarget *clone() const { return (SnmpTarget *) new CTarget(*this); }; 00351 00352 /** 00353 * Get the read community name. 00354 * 00355 * @return C string of the read community. 00356 */ 00357 const char * get_readcommunity() const 00358 { return (const char *) read_community.get_printable(); }; 00359 00360 /** 00361 * Get the read community name. 00362 * 00363 * @param oct - OctetStr that will be filled with the read community name. 00364 */ 00365 void get_readcommunity( OctetStr& oct) const { oct = read_community; }; 00366 00367 /** 00368 * Set the read community name. 00369 * 00370 * @param str - The new read community name 00371 */ 00372 void set_readcommunity( const char * str) { read_community = str; }; 00373 00374 /** 00375 * Set the read community name. 00376 * 00377 * @param oct - The new read community name 00378 */ 00379 void set_readcommunity( const OctetStr& oct) { read_community = oct; }; 00380 00381 /** 00382 * Get the write community name. 00383 * 00384 * @return C string of the write community. 00385 */ 00386 const char * get_writecommunity() const 00387 { return (const char *) write_community.get_printable(); }; 00388 00389 /** 00390 * Get the write community name. 00391 * 00392 * @param oct - OctetStr that will be filled with the write community name. 00393 */ 00394 void get_writecommunity( OctetStr &oct) const { oct = write_community; }; 00395 00396 /** 00397 * Set the write community name. 00398 * 00399 * @param str - The new write community name 00400 */ 00401 void set_writecommunity( const char * str) { write_community = str; }; 00402 00403 /** 00404 * Set the write community name. 00405 * 00406 * @param oct - The new write community name 00407 */ 00408 void set_writecommunity( const OctetStr &oct) { write_community = oct; }; 00409 00410 /** 00411 * Overloaded assignment operator. 00412 */ 00413 CTarget& operator=( const CTarget& target); 00414 00415 /** 00416 * Overloeaded compare operator. 00417 * 00418 * Two CTarget objects are considered equal, if all member variables 00419 * and the base classes are equal. 00420 * 00421 * @return 1 if targets are equal, 0 if not. 00422 */ 00423 int operator==( const CTarget &rhs) const; 00424 00425 /** 00426 * Get all values of a CTarget object. 00427 * 00428 * @param read_comm - Read community name 00429 * @param write_comm - Write community name 00430 * @param address - Address of the target 00431 * @param t - Timeout value 00432 * @param r - Retries value 00433 * @param v - The SNMP version of this target 00434 * 00435 * @return TRUE on success and FALSE on failure. 00436 */ 00437 int resolve_to_C( OctetStr& read_comm, 00438 OctetStr& write_comm, 00439 GenAddress &address, 00440 unsigned long &t, 00441 int &r, 00442 unsigned char &v) const; 00443 00444 /** 00445 * Reset the object. 00446 */ 00447 void clear(); 00448 00449 protected: 00450 OctetStr read_community; // get community 00451 OctetStr write_community; // set community 00452 }; 00453 00454 // create OidCollection type 00455 typedef SnmpCollection<SnmpTarget> TargetCollection; 00456 00457 #ifdef _SNMPv3 00458 #define INITIAL_USER "initial" 00459 #else 00460 #define INITIAL_USER "public" 00461 #endif 00462 00463 //----[ UTarget class ]---------------------------------------------- 00464 /** 00465 * User based Target. 00466 * 00467 * This class is used for SNMPv3 targets. 00468 */ 00469 class DLLOPT UTarget: public SnmpTarget 00470 { 00471 public: 00472 /** 00473 * Constructor with no args. 00474 * The validity of the target will be false. 00475 */ 00476 UTarget(); 00477 00478 /** 00479 * Constructor with all args. 00480 * 00481 * @param address - Address of the target host (cann be any address object) 00482 * @param sec_name - The security name 00483 * @param sec_model - The security model to use 00484 */ 00485 UTarget( const Address &address, 00486 const char *sec_name, 00487 const int sec_model); 00488 00489 /** 00490 * Constructor with all args. 00491 * 00492 * @param address - Address of the target host (cann be any address object) 00493 * @param sec_name - The security name 00494 * @param sec_model - The security model to use 00495 */ 00496 UTarget( const Address &address, 00497 const OctetStr &sec_name, 00498 const int sec_model); 00499 00500 /** 00501 * Constructor with only address. 00502 * 00503 * Assumes the following defaults: security_name: initial, version: SNMPv3, 00504 * security_model: v3MP. 00505 * 00506 * @param address - Address of the target host (cann be any address object) 00507 */ 00508 UTarget( const Address &address); 00509 00510 /** 00511 * Constructor from existing UTarget. 00512 */ 00513 UTarget( const UTarget &target); 00514 00515 /** 00516 * Destructor, that has nothing to do. 00517 */ 00518 ~UTarget() {}; 00519 00520 /** 00521 * Clone operator. 00522 * 00523 * Clone operation for creating a new UTarget from an existing 00524 * UTarget. 00525 * 00526 * @note The caller MUST use the delete operation on the return 00527 * value when done. 00528 * 00529 * @return A pointer to the new object on success, 0 on failure. 00530 */ 00531 SnmpTarget *clone() const { return (SnmpTarget *) new UTarget(*this); }; 00532 00533 /** 00534 * Set the address object. 00535 * 00536 * This method is the same as in SnmpTarget, but it deletes engine_id. 00537 * 00538 * @param address - The address that this target should use. 00539 * @return TRUE on success. 00540 */ 00541 int set_address(const Address &address); 00542 00543 /** 00544 * Get the security name. 00545 * 00546 * @return A const reference to the security name. 00547 */ 00548 const OctetStr& get_security_name() const { return security_name;} ; 00549 00550 /** 00551 * Get the security name. 00552 * 00553 * @param oct - OctetStr that will be filled with the security name. 00554 */ 00555 void get_security_name( OctetStr& oct) const { oct = security_name; }; 00556 00557 /** 00558 * Set the security name. 00559 * 00560 * @param str - The new security name 00561 */ 00562 void set_security_name( const char * str) { security_name = str; }; 00563 00564 /** 00565 * Set the security name. 00566 * 00567 * @param oct - The new security name 00568 */ 00569 void set_security_name( const OctetStr& oct) { security_name = oct; }; 00570 00571 #ifdef _SNMPv3 00572 /** 00573 * Set the engine id. 00574 * 00575 * In most cases it is not necessary for the user to set the engine 00576 * id as snmp++ performs engine id discovery. If the engine id is 00577 * set by the user, no engine_id discovery is made, even if the 00578 * engine id set by the user is wrong. 00579 * 00580 * @param str - The engine id to use 00581 */ 00582 void set_engine_id( const char * str) { engine_id = str; }; 00583 00584 /** 00585 * Set the engine id. 00586 * 00587 * In most cases it is not necessary for the user to set the engine 00588 * id as snmp++ performs engine id discovery. If the engine id is 00589 * set by the user, no engine_id discovery is made, even if the 00590 * engine id set by the user is wrong. 00591 * 00592 * @param oct - The engine id to use 00593 */ 00594 void set_engine_id( const OctetStr &oct) { engine_id = oct; }; 00595 00596 /** 00597 * Get the engine id. 00598 * 00599 * @return A const reference to the enigne id of this target. 00600 */ 00601 const OctetStr& get_engine_id() const { return engine_id; }; 00602 00603 /** 00604 * Get the engine id. 00605 * 00606 * @param oct - OctetStr that will be filled with the engine id 00607 */ 00608 void get_engine_id( OctetStr& oct) const { oct = engine_id; }; 00609 #endif 00610 00611 /** 00612 * Get the security_model. 00613 * 00614 * @return An integer representing the security_model of this target. 00615 */ 00616 int get_security_model() const { return security_model; }; 00617 00618 /** 00619 * Set the security_model. 00620 * 00621 * @param sec_model - The security model to use. 00622 */ 00623 void set_security_model(int sec_model) { security_model = sec_model; }; 00624 00625 /** 00626 * Overloaded assignment operator. 00627 */ 00628 UTarget& operator=( const UTarget& target); 00629 00630 /** 00631 * Overloeaded compare operator. 00632 * 00633 * Two UTarget objects are considered equal, if all member variables 00634 * (beside the engine id) and the base classes are equal. 00635 * 00636 * @return 1 if targets are equal, 0 if not. 00637 */ 00638 virtual int operator==( const UTarget &rhs) const; 00639 00640 /** 00641 * Get all values of a UTarget object. 00642 * 00643 * @param sec_name - security name 00644 * @param sec_model - security model 00645 * @param address - Address of the target 00646 * @param t - Timeout value 00647 * @param r - Retries value 00648 * @param v - The SNMP version of this target 00649 * 00650 * @return TRUE on success and FALSE on failure. 00651 */ 00652 int resolve_to_U( OctetStr& sec_name, 00653 int &sec_model, 00654 GenAddress &address, 00655 unsigned long &t, 00656 int &r, 00657 unsigned char &v) const; 00658 00659 /** 00660 * Reset the object. 00661 */ 00662 void clear(); 00663 00664 protected: 00665 OctetStr security_name; 00666 int security_model; 00667 #ifdef _SNMPv3 00668 OctetStr engine_id; 00669 #endif 00670 }; 00671 00672 #ifdef SNMP_PP_NAMESPACE 00673 }; // end of namespace Snmp_pp 00674 #endif 00675 00676 #endif //_TARGET
1.3.2