00001 /*_############################################################################ 00002 _## 00003 _## uxsnmp.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 #ifndef _UXSNMP_H_ 00031 #define _UXSNMP_H_ 00032 00033 #include "snmp_pp/reentrant.h" 00034 #include "snmp_pp/target.h" 00035 #include "snmp_pp/oid.h" 00036 #include "snmp_pp/address.h" 00037 00038 #ifdef SNMP_PP_NAMESPACE 00039 namespace Snmp_pp { 00040 #endif 00041 00042 #define SNMP_PP_WITH_UDPADDR // Snmp class has constructor with UdpAddress 00043 00044 //-----[ internally used defines ]---------------------------------------- 00045 #define MAXNAME 80 // maximum name length 00046 #define MAX_ADDR_LEN 10 // maximum address len, ipx is 4+6 00047 #define SNMP_SHUTDOWN_MSG 0x0400+177 // shut down msg for stoping a blocked message 00048 #ifndef INVALID_SOCKET 00049 #define INVALID_SOCKET ((SNMPHANDLE)(~0)) // value for invalid socket 00050 #endif 00051 00052 //-----[ async defines for engine ]--------------------------------------- 00053 #define sNMP_PDU_GET_ASYNC 21 00054 #define sNMP_PDU_GETNEXT_ASYNC 22 00055 #define sNMP_PDU_SET_ASYNC 23 00056 #define sNMP_PDU_GETBULK_ASYNC 24 00057 #define sNMP_PDU_INFORM_ASYNC 25 00058 00059 //-----[ trap / notify macros ]------------------------------------------- 00060 #define IP_NOTIFY 162 // IP notification 00061 #define IPX_NOTIFY 0x2121 // IPX notification 00062 00063 00064 //------[ forward declaration of Snmp class ]----------------------------- 00065 class Snmp; 00066 class EventListHolder; 00067 class Pdu; 00068 class v3MP; 00069 00070 //-----------[ async methods callback ]----------------------------------- 00071 /** 00072 * Async methods of the class Snmp require the caller to provide a 00073 * callback address of a function with this typedef. 00074 * 00075 * @note It is not allowed to call any synchronous Snmp methods within the 00076 * callback. Async methods are allowed. 00077 * 00078 * @param reason - Reason for callback (see snmperrs.h) 00079 * @param session - Pointer to Snmp object that was used to send the request 00080 * @param pdu - The received Pdu if reason indicates a received message 00081 * @param target - source target 00082 * @param data - Pointer passed to the async method 00083 */ 00084 typedef void (*snmp_callback)(int reason, Snmp *session, 00085 Pdu &pdu, SnmpTarget &target, void *data); 00086 00087 00088 //------------[ SNMP Class Def ]--------------------------------------------- 00089 // 00090 /** 00091 * SNMP class defintion. The Snmp class provides an object oriented 00092 * approach to SNMP. The SNMP class is an encapsulation of SNMP 00093 * sessions, gets, sets and get nexts. The class manages all SNMP 00094 * resources and provides complete retry and timeout capability. 00095 * 00096 * This class is thread save. 00097 * 00098 * @note If you use the async methods to send requests you MUST call 00099 * Snmp::eventListHolder->SNMPProcessPendingEvents() while waiting 00100 * for the responses. This function triggers the resend of 00101 * packets and calls your callback function if the response is 00102 * received. 00103 * 00104 * @note Call srand() before creating the first Snmp object. 00105 */ 00106 class DLLOPT Snmp: public SnmpSynchronized 00107 { 00108 public: 00109 //------------------[ constructors ]---------------------------------- 00110 00111 /** @name Constructors and Destructor */ 00112 //@{ 00113 00114 /** 00115 * Construct a new SNMP session using the given UDP port. 00116 * 00117 * @param status 00118 * after creation of the session this parameter will 00119 * hold the creation status. 00120 * @param port 00121 * an UDP port to be used for the session 00122 * @param bind_ipv6 00123 * Set this to true if IPv6 should be used. The default is 00124 * IPv4. 00125 */ 00126 Snmp(int &status, const unsigned short port = 0, 00127 const bool bind_ipv6 = false); 00128 00129 /** 00130 * Construct a new SNMP session using the given UDP address. 00131 * Thus, binds the session on a specific IPv4 or IPv6 address. 00132 * 00133 * @param status 00134 * after creation of the session this parameter will 00135 * hold the creation status. 00136 * @param addr 00137 * an UDP address to be used for the session 00138 */ 00139 Snmp(int &status, const UdpAddress &addr); 00140 00141 /** 00142 * Construct a new SNMP session using the given UDP addresses. 00143 * Using this constructor will bind to both IPv4 and IPv6 ports. 00144 * 00145 * @param status 00146 * after creation of the session this parameter will 00147 * hold the creation status. 00148 * @param addr_v4 00149 * an IPv4 UDP address to be used for the session 00150 * @param addr_v6 00151 * an IPv6 UDP address to be used for the session 00152 */ 00153 Snmp(int &status, const UdpAddress& addr_v4, const UdpAddress& addr_v6); 00154 00155 //-------------------[ destructor ]------------------------------------ 00156 /** 00157 * Destructor. 00158 */ 00159 virtual ~Snmp(); 00160 00161 //@} 00162 00163 //--------[ Get the version of the snmp++ library ]-------------------- 00164 /** 00165 * Get the version of the snmp++ library. 00166 * 00167 * @return The version of the snmp++ lib at runtime. 00168 */ 00169 static const char *get_version(); 00170 00171 00172 //-------------------[ returns error string ]-------------------------- 00173 /** 00174 * Returns a human readable error string. 00175 * 00176 * @param c - Error code returned by any method of this class 00177 * @return Null terminated error string. 00178 */ 00179 static const char *error_msg(const int c); 00180 #ifdef _SNMPv3 00181 /** 00182 * Returns a human readable error string. 00183 * If a report message is returned, then the contained Oid can be 00184 * used to get a error string. 00185 * 00186 * @param v3Oid - Oid of a SNMPv3 report Pdu 00187 * @return Null terminated error string. 00188 */ 00189 static const char* error_msg(const Oid& v3Oid); 00190 #endif 00191 00192 //------------------------[ Windows Sockets ]---------------------------- 00193 00194 /** 00195 * Initialize the Winsock library (WSAStartup). 00196 * 00197 * @note on Win32 this method *must* be called before creating Snmp or 00198 * Address objects. 00199 */ 00200 static void socket_startup(); 00201 00202 00203 /** 00204 * Shut down the Winsock library (WSACleanup). 00205 */ 00206 static void socket_cleanup(); 00207 00208 //------------------------[ send requests ]------------------------------ 00209 00210 /** @name Sending SNMP Pdus 00211 */ 00212 //@{ 00213 00214 /** 00215 * Send a blocking SNMP-GET request. 00216 * 00217 * @param pdu - Pdu to send 00218 * @param target - Target for the get 00219 * 00220 * @return SNMP_CLASS_SUCCES or a negative error code 00221 */ 00222 virtual int get(Pdu &pdu, const SnmpTarget &target); 00223 00224 /** 00225 * Send a async SNMP-GET request. 00226 * 00227 * @param pdu - Pdu to send 00228 * @param target - Target for the get 00229 * @param callback - User callback function to use 00230 * @param callback_data - User definable data pointer 00231 * 00232 * @return SNMP_CLASS_SUCCES or a negative error code 00233 */ 00234 virtual int get(Pdu &pdu, const SnmpTarget &target, 00235 const snmp_callback callback, 00236 const void *callback_data = 0); 00237 00238 /** 00239 * Send a blocking SNMP-GETNEXT request. 00240 * 00241 * @param pdu - Pdu to send 00242 * @param target - Target for the getnext 00243 * 00244 * @return SNMP_CLASS_SUCCES or a negative error code 00245 */ 00246 virtual int get_next(Pdu &pdu, const SnmpTarget &target); 00247 00248 /** 00249 * Send a async SNMP-GETNEXT request. 00250 * 00251 * @param pdu - Pdu to send 00252 * @param target - Target for the getnext 00253 * @param callback - User callback function to use 00254 * @param callback_data - User definable data pointer 00255 * 00256 * @return SNMP_CLASS_SUCCES or a negative error code 00257 */ 00258 virtual int get_next(Pdu &pdu, const SnmpTarget &target, 00259 const snmp_callback callback, 00260 const void *callback_data = 0); 00261 00262 /** 00263 * Send a blocking SNMP-SET request. 00264 * 00265 * @param pdu - Pdu to send 00266 * @param target - Target for the set 00267 * 00268 * @return SNMP_CLASS_SUCCES or a negative error code 00269 */ 00270 virtual int set(Pdu &pdu, const SnmpTarget &target); 00271 00272 /** 00273 * Send a async SNMP-SET request. 00274 * 00275 * @param pdu - Pdu to send 00276 * @param target - Target for the set 00277 * @param callback - User callback function to use 00278 * @param callback_data - User definable data pointer 00279 * 00280 * @return SNMP_CLASS_SUCCES or a negative error code 00281 */ 00282 virtual int set(Pdu &pdu, const SnmpTarget &target, 00283 const snmp_callback callback, 00284 const void * callback_data = 0); 00285 00286 /** 00287 * Send a blocking SNMP-GETBULK request. 00288 * 00289 * @param pdu - Pdu to send 00290 * @param target - Target for the getbulk 00291 * @param non_repeaters - number of non repeaters 00292 * @param max_reps - maximum number of repetitions 00293 * 00294 * @return SNMP_CLASS_SUCCES or a negative error code 00295 */ 00296 virtual int get_bulk(Pdu &pdu, const SnmpTarget &target, 00297 const int non_repeaters, const int max_reps); 00298 00299 /** 00300 * Send a async SNMP-GETBULK request. 00301 * 00302 * @param pdu - Pdu to send 00303 * @param target - Target for the getbulk 00304 * @param non_repeaters - number of non repeaters 00305 * @param max_reps - maximum number of repetitions 00306 * @param callback - User callback function to use 00307 * @param callback_data - User definable data pointer 00308 * 00309 * @return SNMP_CLASS_SUCCES or a negative error code 00310 */ 00311 virtual int get_bulk(Pdu &pdu, const SnmpTarget &target, 00312 const int non_repeaters, const int max_reps, 00313 const snmp_callback callback, 00314 const void *callback_data = 0); 00315 00316 /** 00317 * Send a SNMP-TRAP. 00318 * 00319 * @param pdu - Pdu to send 00320 * @param target - Target for the trap 00321 * 00322 * @return SNMP_CLASS_SUCCES or a negative error code 00323 */ 00324 virtual int trap(Pdu &pdu, const SnmpTarget &target); 00325 00326 00327 /** 00328 * Send a SNMPv3-REPORT. 00329 * 00330 * @param pdu - Pdu to send 00331 * @param target - Target for the report (must be a UTarget) 00332 * 00333 * @return SNMP_CLASS_SUCCES or a negative error code 00334 */ 00335 virtual int report(Pdu &pdu, const SnmpTarget &target); 00336 00337 /** 00338 * Send a blocking INFORM-REQ. 00339 * 00340 * @param pdu - Pdu to send 00341 * @param target - Target for the inform 00342 * 00343 * @return SNMP_CLASS_SUCCES or a negative error code 00344 */ 00345 virtual int inform(Pdu &pdu, const SnmpTarget &target); 00346 00347 /** 00348 * Send a async INFORM-REQ. 00349 * 00350 * @param pdu - Pdu to send 00351 * @param target - Target for the inform 00352 * @param callback - User callback function to use 00353 * @param callback_data - User definable data pointer 00354 * 00355 * @return SNMP_CLASS_SUCCES or a negative error code 00356 */ 00357 virtual int inform(Pdu &pdu, const SnmpTarget &target, 00358 const snmp_callback callback, 00359 const void * callback_data = 0); 00360 00361 /** 00362 * Send a RESPONSE. 00363 * 00364 * @param pdu - Pdu to send 00365 * @param target - Target for the response 00366 * 00367 * @return SNMP_CLASS_SUCCES or a negative error code 00368 */ 00369 virtual int response(Pdu &pdu, const SnmpTarget &target); 00370 00371 00372 /** 00373 * Send a SNMP Broadcast message. 00374 * 00375 * This member function sends out a valid SNMP message to a 00376 * broadcast address and waits for responses. The source addresses 00377 * of the response messages are added to the collection. 00378 * 00379 * The message is sent only once. 00380 * 00381 * @note SNMP_BROADCAST has to be defined in config_snmp_pp.h. 00382 * 00383 * @note There is no SNMP standard that defines "SNMP Broadcast 00384 * discovery". SNMP agents are not forced to answer requests 00385 * that are sent to a broadcast address. 00386 * 00387 * @note Do not use this method while waiting for other responses, 00388 * as these responses will be added to the collection and dropped 00389 * by this method. Solution for this problem: Use a special 00390 * Snmp object only for broadcasts. 00391 * 00392 * @param addresses - The addresses of the agents, that answered. 00393 * @param timeout_sec - Timeout in seconds 00394 * @param addr - Broadcast address 00395 * @param version - SNMP version to use 00396 * @param community - Only needed for SNMPv1/v2c, defaults to "public" 00397 * 00398 */ 00399 virtual int broadcast_discovery(UdpAddressCollection &addresses, 00400 const int timeout_sec, 00401 const UdpAddress &addr, 00402 const snmp_version version, 00403 const OctetStr *community = 0); 00404 00405 //@} 00406 00407 /** 00408 * Cancel a pending request. 00409 * 00410 * @param rid - The request id to cancel 00411 * 00412 * @return SNMP_CLASS_SUCCES or SNMP_CLASS_INVALID_REQID on failure 00413 */ 00414 virtual int cancel(const unsigned long rid); 00415 00416 00417 /** @name Trap and Inform handling 00418 */ 00419 //@{ 00420 00421 /** 00422 * Register to get traps and informs. 00423 * 00424 * @note Every call to one of the notify_register() methods overwrites 00425 * the previous given values. 00426 * 00427 * @param trapids - ids to listen for 00428 * @param targets - targets to listen for 00429 * @param callback - User callback function to use 00430 * @param callback_data - User definable data pointer 00431 * 00432 * @return SNMP_CLASS_SUCCESS, SNMP_CLASS_TL_FAILED or SNMP_CLASS_TL_IN_USE 00433 */ 00434 virtual int notify_register(const OidCollection &trapids, 00435 const TargetCollection &targets, 00436 const snmp_callback callback, 00437 const void *callback_data=0); 00438 /** 00439 * Register to get traps and informs. 00440 * 00441 * @note listen_addresses param is ignored! 00442 * 00443 * @note Every call to one of the notify_register() methods overwrites 00444 * the previous given values. 00445 * 00446 * @param trapids - ids to listen for 00447 * @param targets - targets to listen for 00448 * @param listen_addresses - interfaces to listen on 00449 * @param callback - User callback function to use 00450 * @param callback_data - User definable data pointer 00451 * 00452 * @return SNMP_CLASS_SUCCESS, SNMP_CLASS_TL_FAILED or SNMP_CLASS_TL_IN_USE 00453 */ 00454 virtual int notify_register( const OidCollection &trapids, 00455 const TargetCollection &targets, 00456 const AddressCollection &listen_addresses, 00457 const snmp_callback callback, 00458 const void *callback_data=0); 00459 /** 00460 * Unregister to get traps and informs. 00461 * Undo the call to notify_register(). 00462 * 00463 * @return Always SNMP_CLASS_SUCCESS 00464 */ 00465 virtual int notify_unregister(); 00466 00467 /** 00468 * Get notify register info. 00469 * 00470 * @param trapids - ids listened for 00471 * @param targets - targets listened for 00472 * 00473 * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_INVALID if not registered 00474 */ 00475 virtual int get_notify_filter( OidCollection &trapids, 00476 TargetCollection &targets) 00477 { AddressCollection a; return get_notify_filter(trapids, targets, a); } 00478 00479 /** 00480 * Get notify register info. 00481 * 00482 * @param trapids - ids listened for 00483 * @param targets - targets listened for 00484 * @param listen_addresses - interfaces listened on 00485 * 00486 * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_INVALID if not registered 00487 */ 00488 virtual int get_notify_filter( OidCollection &trapids, 00489 TargetCollection &targets, 00490 AddressCollection &listen_addresses); 00491 00492 00493 //-----------------------[ access the trap reception info ]--------------- 00494 /** 00495 * Get a pointer to the callback function used for trap reception. 00496 * 00497 * @return Pointer to the function set through notify_register() 00498 */ 00499 snmp_callback get_notify_callback() { return notifycallback; }; 00500 00501 /** 00502 * Get a pointer to the data that is passed to the callback function. 00503 * 00504 * @return Pointer to the data set through notify_register() 00505 */ 00506 void *get_notify_callback_data() { return notifycallback_data; }; 00507 00508 //@} 00509 00510 /** 00511 * Send raw UDP data. 00512 * This method may be used to send any data to the recepient. 00513 * 00514 * @param send_buf - Data buffer 00515 * @param send_len - Length of the data 00516 * @param address - Recepient 00517 * @param fd - socket to use, if not specified, the socket of the 00518 * object is used 00519 * 00520 * @return 0 on success, -1 on failure 00521 */ 00522 virtual int send_raw_data( unsigned char *send_buf, 00523 size_t send_len, UdpAddress &address, 00524 int fd = 0); 00525 00526 const IpAddress &get_listen_address() const {return listen_address; }; 00527 00528 // this member var will simulate a global var 00529 EventListHolder *eventListHolder; 00530 00531 #ifdef _SNMPv3 00532 // lock for v3 cache information 00533 static SnmpSynchronized v3Lock; 00534 #endif 00535 00536 protected: 00537 00538 /** 00539 * Generate a unique (for this Snmp obect) request id. 00540 * 00541 * @return Unique id between PDU_MIN_RID and PDU_MAX_RID 00542 */ 00543 long MyMakeReqId(); 00544 00545 //-----------[ Snmp Engine ]---------------------------------------- 00546 /** 00547 * gets, sets and get nexts go through here.... 00548 * This mf does all snmp sending and reception 00549 * except for traps which are sent using trap(). 00550 * 00551 * @note that for a UTarget with an empty enngine id the 00552 * Utarget::set_engine_id() may be called. 00553 */ 00554 int snmp_engine( Pdu &pdu, // pdu to use 00555 long int non_reps, // get bulk only 00556 long int max_reps, // get bulk only 00557 const SnmpTarget &target, // destination target 00558 const snmp_callback cb, // async callback function 00559 const void *cbd); // callback data 00560 00561 00562 //--------[ map action ]------------------------------------------------ 00563 // map the snmp++ action to a SMI pdu type 00564 void map_action(unsigned short action, unsigned short &pdu_action); 00565 00566 #ifdef _SNMPv3 00567 friend void v3CallBack( int reason, Snmp *snmp, Pdu &pdu, 00568 SnmpTarget &target, void *v3cd); 00569 00570 /** 00571 * Internal used callback data structure for async v3 requests. 00572 */ 00573 struct V3CallBackData 00574 { 00575 Pdu *pdu; ///< The Pdu that was sent 00576 long int non_reps; ///< For GET-BULK requests 00577 long int max_reps; ///< For GET-BULK requests 00578 SnmpTarget *target; ///< Pointer to the Target object to use 00579 snmp_callback oldCallback; ///< User callback function 00580 const void *cbd; ///< User callback data 00581 }; 00582 #endif 00583 00584 //---[ instance variables ] 00585 #ifdef WIN32 00586 unsigned long iv_snmp_session; 00587 unsigned long iv_snmp_session_ipv6; 00588 #else 00589 SNMPHANDLE iv_snmp_session; // session handle 00590 SNMPHANDLE iv_snmp_session_ipv6; // session handle 00591 #endif 00592 IpAddress listen_address; 00593 int iv_notify_fd; // fd for notify session - DLD 00594 SNMPHANDLE pdu_handler; // pdu handler win proc 00595 SNMPHANDLE pdu_handler_ipv6; // pdu handler win proc 00596 int construct_status; // status of construction 00597 int construct_status_ipv6; // status of construction ipv6 00598 long current_rid; // current rid to use 00599 00600 // inform receive member variables 00601 snmp_callback notifycallback; 00602 void * notifycallback_data; 00603 00604 void init(int& status, IpAddress*[2], 00605 const unsigned short port_v4, const unsigned short port_v6); 00606 00607 /** 00608 * Set the notify timestamp of a trap pdu if the user did not set it. 00609 */ 00610 void check_notify_timestamp(Pdu &pdu); 00611 }; 00612 00613 #ifdef SNMP_PP_NAMESPACE 00614 }; // end of namespace Snmp_pp 00615 #endif 00616 00617 #endif
1.3.2