uxsnmp.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*_############################################################################
  2. _##
  3. _## uxsnmp.h
  4. _##
  5. _## SNMP++v3.2.23
  6. _## -----------------------------------------------
  7. _## Copyright (c) 2001-2007 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. _## Stuttgart, Germany, Sun Nov 11 15:10:59 CET 2007
  27. _##
  28. _##########################################################################*/
  29. #ifndef _UXSNMP_H_
  30. #define _UXSNMP_H_
  31. #include "snmp_pp/reentrant.h"
  32. #include "snmp_pp/target.h"
  33. #include "snmp_pp/oid.h"
  34. #include "snmp_pp/address.h"
  35. #ifdef SNMP_PP_NAMESPACE
  36. namespace Snmp_pp {
  37. #endif
  38. #define SNMP_PP_WITH_UDPADDR // Snmp class has constructor with UdpAddress
  39. //-----[ internally used defines ]----------------------------------------
  40. #define MAXNAME 80 // maximum name length
  41. #define MAX_ADDR_LEN 10 // maximum address len, ipx is 4+6
  42. #define SNMP_SHUTDOWN_MSG 0x0400+177 // shut down msg for stoping a blocked message
  43. #ifndef INVALID_SOCKET
  44. #define INVALID_SOCKET ((SnmpSocket)(~0)) // value for invalid socket
  45. #endif
  46. //-----[ async defines for engine ]---------------------------------------
  47. #define sNMP_PDU_GET_ASYNC 21
  48. #define sNMP_PDU_GETNEXT_ASYNC 22
  49. #define sNMP_PDU_SET_ASYNC 23
  50. #define sNMP_PDU_GETBULK_ASYNC 24
  51. #define sNMP_PDU_INFORM_ASYNC 25
  52. //-----[ trap / notify macros ]-------------------------------------------
  53. #define IP_NOTIFY 162 // IP notification
  54. #define IPX_NOTIFY 0x2121 // IPX notification
  55. //------[ forward declaration of Snmp class ]-----------------------------
  56. class Snmp;
  57. class EventListHolder;
  58. class Pdu;
  59. class v3MP;
  60. //-----------[ async methods callback ]-----------------------------------
  61. /**
  62. * Async methods of the class Snmp require the caller to provide a
  63. * callback address of a function with this typedef.
  64. *
  65. * @note It is not allowed to call any synchronous Snmp methods within the
  66. * callback. Async methods are allowed.
  67. *
  68. * @param reason - Reason for callback (see snmperrs.h)
  69. * @param session - Pointer to Snmp object that was used to send the request
  70. * @param pdu - The received Pdu if reason indicates a received message
  71. * @param target - source target
  72. * @param data - Pointer passed to the async method
  73. */
  74. typedef void (*snmp_callback)(int reason, Snmp *session,
  75. Pdu &pdu, SnmpTarget &target, void *data);
  76. //------------[ SNMP Class Def ]---------------------------------------------
  77. //
  78. /**
  79. * SNMP class defintion. The Snmp class provides an object oriented
  80. * approach to SNMP. The SNMP class is an encapsulation of SNMP
  81. * sessions, gets, sets and get nexts. The class manages all SNMP
  82. * resources and provides complete retry and timeout capability.
  83. *
  84. * This class is thread save.
  85. *
  86. * @note If you use the async methods to send requests you MUST call
  87. * Snmp::eventListHolder->SNMPProcessPendingEvents() while waiting
  88. * for the responses. This function triggers the resend of
  89. * packets and calls your callback function if the response is
  90. * received.
  91. *
  92. * @note Call srand() before creating the first Snmp object.
  93. */
  94. class DLLOPT Snmp: public SnmpSynchronized
  95. {
  96. public:
  97. //------------------[ constructors ]----------------------------------
  98. /** @name Constructors and Destructor */
  99. //@{
  100. /**
  101. * Construct a new SNMP session using the given UDP port.
  102. *
  103. * @param status
  104. * after creation of the session this parameter will
  105. * hold the creation status.
  106. * @param port
  107. * an UDP port to be used for the session
  108. * @param bind_ipv6
  109. * Set this to true if IPv6 should be used. The default is
  110. * IPv4.
  111. */
  112. Snmp(int &status, const unsigned short port = 0,
  113. const bool bind_ipv6 = false);
  114. /**
  115. * Construct a new SNMP session using the given UDP address.
  116. * Thus, binds the session on a specific IPv4 or IPv6 address.
  117. *
  118. * @param status
  119. * after creation of the session this parameter will
  120. * hold the creation status.
  121. * @param addr
  122. * an UDP address to be used for the session
  123. */
  124. Snmp(int &status, const UdpAddress &addr);
  125. /**
  126. * Construct a new SNMP session using the given UDP addresses.
  127. * Using this constructor will bind to both IPv4 and IPv6 ports.
  128. *
  129. * @param status
  130. * after creation of the session this parameter will
  131. * hold the creation status.
  132. * @param addr_v4
  133. * an IPv4 UDP address to be used for the session
  134. * @param addr_v6
  135. * an IPv6 UDP address to be used for the session
  136. */
  137. Snmp(int &status, const UdpAddress& addr_v4, const UdpAddress& addr_v6);
  138. //-------------------[ destructor ]------------------------------------
  139. /**
  140. * Destructor.
  141. */
  142. virtual ~Snmp();
  143. //@}
  144. //--------[ Get the version of the snmp++ library ]--------------------
  145. /**
  146. * Get the version of the snmp++ library.
  147. *
  148. * @return The version of the snmp++ lib at runtime.
  149. */
  150. static const char *get_version();
  151. //-------------------[ returns error string ]--------------------------
  152. /**
  153. * Returns a human readable error string.
  154. *
  155. * @param c - Error code returned by any method of this class
  156. * @return Null terminated error string.
  157. */
  158. static const char *error_msg(const int c);
  159. #ifdef _SNMPv3
  160. /**
  161. * Returns a human readable error string.
  162. * If a report message is returned, then the contained Oid can be
  163. * used to get a error string.
  164. *
  165. * @param v3Oid - Oid of a SNMPv3 report Pdu
  166. * @return Null terminated error string.
  167. */
  168. static const char* error_msg(const Oid& v3Oid);
  169. #endif
  170. //------------------------[ Windows Sockets ]----------------------------
  171. /**
  172. * Initialize the Winsock library (WSAStartup).
  173. *
  174. * @note on Win32 this method *must* be called before creating Snmp or
  175. * Address objects.
  176. */
  177. static void socket_startup();
  178. /**
  179. * Shut down the Winsock library (WSACleanup).
  180. */
  181. static void socket_cleanup();
  182. //------------------------[ send requests ]------------------------------
  183. /** @name Sending SNMP Pdus
  184. */
  185. //@{
  186. /**
  187. * Send a blocking SNMP-GET request.
  188. *
  189. * @param pdu - Pdu to send
  190. * @param target - Target for the get
  191. *
  192. * @return SNMP_CLASS_SUCCES or a negative error code
  193. */
  194. virtual int get(Pdu &pdu, const SnmpTarget &target);
  195. /**
  196. * Send a async SNMP-GET request.
  197. *
  198. * @param pdu - Pdu to send
  199. * @param target - Target for the get
  200. * @param callback - User callback function to use
  201. * @param callback_data - User definable data pointer
  202. *
  203. * @return SNMP_CLASS_SUCCES or a negative error code
  204. */
  205. virtual int get(Pdu &pdu, const SnmpTarget &target,
  206. const snmp_callback callback,
  207. const void *callback_data = 0);
  208. /**
  209. * Send a blocking SNMP-GETNEXT request.
  210. *
  211. * @param pdu - Pdu to send
  212. * @param target - Target for the getnext
  213. *
  214. * @return SNMP_CLASS_SUCCES or a negative error code
  215. */
  216. virtual int get_next(Pdu &pdu, const SnmpTarget &target);
  217. /**
  218. * Send a async SNMP-GETNEXT request.
  219. *
  220. * @param pdu - Pdu to send
  221. * @param target - Target for the getnext
  222. * @param callback - User callback function to use
  223. * @param callback_data - User definable data pointer
  224. *
  225. * @return SNMP_CLASS_SUCCES or a negative error code
  226. */
  227. virtual int get_next(Pdu &pdu, const SnmpTarget &target,
  228. const snmp_callback callback,
  229. const void *callback_data = 0);
  230. /**
  231. * Send a blocking SNMP-SET request.
  232. *
  233. * @param pdu - Pdu to send
  234. * @param target - Target for the set
  235. *
  236. * @return SNMP_CLASS_SUCCES or a negative error code
  237. */
  238. virtual int set(Pdu &pdu, const SnmpTarget &target);
  239. /**
  240. * Send a async SNMP-SET request.
  241. *
  242. * @param pdu - Pdu to send
  243. * @param target - Target for the set
  244. * @param callback - User callback function to use
  245. * @param callback_data - User definable data pointer
  246. *
  247. * @return SNMP_CLASS_SUCCES or a negative error code
  248. */
  249. virtual int set(Pdu &pdu, const SnmpTarget &target,
  250. const snmp_callback callback,
  251. const void * callback_data = 0);
  252. /**
  253. * Send a blocking SNMP-GETBULK request.
  254. *
  255. * @param pdu - Pdu to send
  256. * @param target - Target for the getbulk
  257. * @param non_repeaters - number of non repeaters
  258. * @param max_reps - maximum number of repetitions
  259. *
  260. * @return SNMP_CLASS_SUCCES or a negative error code
  261. */
  262. virtual int get_bulk(Pdu &pdu, const SnmpTarget &target,
  263. const int non_repeaters, const int max_reps);
  264. /**
  265. * Send a async SNMP-GETBULK request.
  266. *
  267. * @param pdu - Pdu to send
  268. * @param target - Target for the getbulk
  269. * @param non_repeaters - number of non repeaters
  270. * @param max_reps - maximum number of repetitions
  271. * @param callback - User callback function to use
  272. * @param callback_data - User definable data pointer
  273. *
  274. * @return SNMP_CLASS_SUCCES or a negative error code
  275. */
  276. virtual int get_bulk(Pdu &pdu, const SnmpTarget &target,
  277. const int non_repeaters, const int max_reps,
  278. const snmp_callback callback,
  279. const void *callback_data = 0);
  280. /**
  281. * Send a SNMP-TRAP.
  282. *
  283. * @param pdu - Pdu to send
  284. * @param target - Target for the trap
  285. *
  286. * @return SNMP_CLASS_SUCCES or a negative error code
  287. */
  288. virtual int trap(Pdu &pdu, const SnmpTarget &target);
  289. /**
  290. * Send a SNMPv3-REPORT.
  291. *
  292. * @param pdu - Pdu to send
  293. * @param target - Target for the report (must be a UTarget)
  294. *
  295. * @return SNMP_CLASS_SUCCES or a negative error code
  296. */
  297. virtual int report(Pdu &pdu, const SnmpTarget &target);
  298. /**
  299. * Send a blocking INFORM-REQ.
  300. *
  301. * @param pdu - Pdu to send
  302. * @param target - Target for the inform
  303. *
  304. * @return SNMP_CLASS_SUCCES or a negative error code
  305. */
  306. virtual int inform(Pdu &pdu, const SnmpTarget &target);
  307. /**
  308. * Send a async INFORM-REQ.
  309. *
  310. * @param pdu - Pdu to send
  311. * @param target - Target for the inform
  312. * @param callback - User callback function to use
  313. * @param callback_data - User definable data pointer
  314. *
  315. * @return SNMP_CLASS_SUCCES or a negative error code
  316. */
  317. virtual int inform(Pdu &pdu, const SnmpTarget &target,
  318. const snmp_callback callback,
  319. const void * callback_data = 0);
  320. /**
  321. * Send a RESPONSE.
  322. *
  323. * @param pdu - Pdu to send
  324. * @param target - Target for the response
  325. * @param fd - file descriptor to use, should be the one
  326. * that was passed to the callback function
  327. *
  328. * @return SNMP_CLASS_SUCCES or a negative error code
  329. */
  330. virtual int response(Pdu &pdu, const SnmpTarget &target,
  331. const SnmpSocket fd = INVALID_SOCKET);
  332. /**
  333. * Send a SNMP Broadcast message.
  334. *
  335. * This member function sends out a valid SNMP message to a
  336. * broadcast address and waits for responses. The source addresses
  337. * of the response messages are added to the collection.
  338. *
  339. * The message is sent only once.
  340. *
  341. * @note SNMP_BROADCAST has to be defined in config_snmp_pp.h.
  342. *
  343. * @note There is no SNMP standard that defines "SNMP Broadcast
  344. * discovery". SNMP agents are not forced to answer requests
  345. * that are sent to a broadcast address.
  346. *
  347. * @note Do not use this method while waiting for other responses,
  348. * as these responses will be added to the collection and dropped
  349. * by this method. Solution for this problem: Use a special
  350. * Snmp object only for broadcasts.
  351. *
  352. * @param addresses - The addresses of the agents, that answered.
  353. * @param timeout_sec - Timeout in seconds
  354. * @param addr - Broadcast address
  355. * @param version - SNMP version to use
  356. * @param community - Only needed for SNMPv1/v2c, defaults to "public"
  357. *
  358. */
  359. virtual int broadcast_discovery(UdpAddressCollection &addresses,
  360. const int timeout_sec,
  361. const UdpAddress &addr,
  362. const snmp_version version,
  363. const OctetStr *community = 0);
  364. #ifdef _SNMPv3
  365. virtual int engine_id_discovery(OctetStr &engine_id,
  366. const int timeout_sec,
  367. const UdpAddress &addr);
  368. #endif
  369. //@}
  370. /**
  371. * Cancel a pending request.
  372. *
  373. * @param rid - The request id to cancel
  374. *
  375. * @return SNMP_CLASS_SUCCES or SNMP_CLASS_INVALID_REQID on failure
  376. */
  377. virtual int cancel(const unsigned long rid);
  378. /** @name Trap and Inform handling
  379. */
  380. //@{
  381. /**
  382. * Set the port for listening to traps and informs.
  383. *
  384. * @note This function must be called before notify_register(),
  385. * otherwise the default port is used.
  386. */
  387. virtual void notify_set_listen_port(const int port);
  388. /**
  389. * Get the port that is used for listening to traps and informs.
  390. */
  391. virtual int notify_get_listen_port();
  392. /**
  393. * Register to get traps and informs.
  394. *
  395. * @note Every call to one of the notify_register() methods overwrites
  396. * the previous given values.
  397. *
  398. * @param trapids - ids to listen for
  399. * @param targets - targets to listen for
  400. * @param callback - User callback function to use
  401. * @param callback_data - User definable data pointer
  402. *
  403. * @return SNMP_CLASS_SUCCESS, SNMP_CLASS_TL_FAILED or SNMP_CLASS_TL_IN_USE
  404. */
  405. virtual int notify_register(const OidCollection &trapids,
  406. const TargetCollection &targets,
  407. const snmp_callback callback,
  408. const void *callback_data=0);
  409. /**
  410. * Register to get traps and informs.
  411. *
  412. * @note The AddressCollection param is currently ignored.
  413. *
  414. * @note Every call to one of the notify_register() methods overwrites
  415. * the previous given values.
  416. *
  417. * @param trapids - ids to listen for
  418. * @param targets - targets to listen for
  419. * @param listen_addresses - interfaces to listen on
  420. * @param callback - User callback function to use
  421. * @param callback_data - User definable data pointer
  422. *
  423. * @return SNMP_CLASS_SUCCESS, SNMP_CLASS_TL_FAILED or SNMP_CLASS_TL_IN_USE
  424. */
  425. virtual int notify_register( const OidCollection &trapids,
  426. const TargetCollection &targets,
  427. const AddressCollection &listen_addresses,
  428. const snmp_callback callback,
  429. const void *callback_data=0);
  430. /**
  431. * Unregister to get traps and informs.
  432. * Undo the call to notify_register().
  433. *
  434. * @return Always SNMP_CLASS_SUCCESS
  435. */
  436. virtual int notify_unregister();
  437. /**
  438. * Get notify register info.
  439. *
  440. * @param trapids - ids listened for
  441. * @param targets - targets listened for
  442. *
  443. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_INVALID if not registered
  444. */
  445. virtual int get_notify_filter( OidCollection &trapids,
  446. TargetCollection &targets)
  447. { AddressCollection a; return get_notify_filter(trapids, targets, a); }
  448. /**
  449. * Get notify register info.
  450. *
  451. * @param trapids - ids listened for
  452. * @param targets - targets listened for
  453. * @param listen_addresses - interfaces listened on
  454. *
  455. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_INVALID if not registered
  456. */
  457. virtual int get_notify_filter( OidCollection &trapids,
  458. TargetCollection &targets,
  459. AddressCollection &listen_addresses);
  460. //-----------------------[ access the trap reception info ]---------------
  461. /**
  462. * Get a pointer to the callback function used for trap reception.
  463. *
  464. * @return Pointer to the function set through notify_register()
  465. */
  466. snmp_callback get_notify_callback() { return notifycallback; };
  467. /**
  468. * Get a pointer to the data that is passed to the callback function.
  469. *
  470. * @return Pointer to the data set through notify_register()
  471. */
  472. void *get_notify_callback_data() { return notifycallback_data; };
  473. //@}
  474. /**
  475. * Send raw UDP data.
  476. * This method may be used to send any data to the recepient.
  477. *
  478. * @param send_buf - Data buffer
  479. * @param send_len - Length of the data
  480. * @param address - Recepient
  481. * @param fd - socket to use, if not specified, the socket of the
  482. * object is used
  483. *
  484. * @return 0 on success, -1 on failure
  485. */
  486. virtual int send_raw_data(unsigned char *send_buf,
  487. size_t send_len, UdpAddress &address,
  488. SnmpSocket fd = INVALID_SOCKET);
  489. const IpAddress &get_listen_address() const {return listen_address; };
  490. // this member var will simulate a global var
  491. EventListHolder *eventListHolder;
  492. bool start_poll_thread(const int select_timeout);
  493. void stop_poll_thread();
  494. protected:
  495. /**
  496. * Check for the status of the worker thread.
  497. * @return BOOL - TRUE - if running, FALSE - otherwise
  498. */
  499. bool is_running(void) const
  500. { return m_bThreadRunning; };
  501. /**
  502. * This is a working thread for the recovery of the pending events.
  503. *
  504. * @param pSnmp [in] pointer to the whole object
  505. *
  506. * @return int
  507. * 0 - if succesful,
  508. * 1 - in the case of error
  509. */
  510. #ifdef WIN32
  511. static int process_thread(Snmp *pSnmp);
  512. #else
  513. static void* process_thread(void *arg);
  514. #endif
  515. protected:
  516. /**
  517. * Generate a unique (for this Snmp obect) request id.
  518. *
  519. * @return Unique id between PDU_MIN_RID and PDU_MAX_RID
  520. */
  521. long MyMakeReqId();
  522. /**
  523. * Common init function used by constructors.
  524. */
  525. void init(int& status, IpAddress*[2],
  526. const unsigned short port_v4, const unsigned short port_v6);
  527. /**
  528. * Set the notify timestamp of a trap pdu if the user did not set it.
  529. */
  530. void check_notify_timestamp(Pdu &pdu);
  531. //-----------[ Snmp Engine ]----------------------------------------
  532. /**
  533. * gets, sets and get nexts go through here....
  534. * This mf does all snmp sending and reception
  535. * except for traps which are sent using trap().
  536. *
  537. * @note that for a UTarget with an empty engine id the
  538. * Utarget::set_engine_id() may be called.
  539. */
  540. int snmp_engine( Pdu &pdu, // pdu to use
  541. long int non_reps, // get bulk only
  542. long int max_reps, // get bulk only
  543. const SnmpTarget &target, // destination target
  544. const snmp_callback cb, // async callback function
  545. const void *cbd, // callback data
  546. SnmpSocket fd = INVALID_SOCKET,
  547. int reports_received = 0);
  548. //--------[ map action ]------------------------------------------------
  549. // map the snmp++ action to a SMI pdu type
  550. void map_action(unsigned short action, unsigned short &pdu_action);
  551. #ifdef _SNMPv3
  552. friend void v3CallBack( int reason, Snmp *snmp, Pdu &pdu,
  553. SnmpTarget &target, void *v3cd);
  554. /**
  555. * Internal used callback data structure for async v3 requests.
  556. */
  557. struct V3CallBackData
  558. {
  559. Pdu *pdu; ///< The Pdu that was sent
  560. long int non_reps; ///< For GET-BULK requests
  561. long int max_reps; ///< For GET-BULK requests
  562. SnmpTarget *target; ///< Pointer to the Target object to use
  563. snmp_callback oldCallback; ///< User callback function
  564. const void *cbd; ///< User callback data
  565. int reports_received; ///< How many reports are already received
  566. };
  567. #endif
  568. //---[ instance variables ]
  569. SnmpSocket iv_snmp_session;
  570. #ifdef SNMP_PP_IPv6
  571. SnmpSocket iv_snmp_session_ipv6;
  572. #endif
  573. IpAddress listen_address;
  574. SnmpSocket iv_notify_fd; // fd for notify session - DLD
  575. long current_rid; // current rid to use
  576. // inform receive member variables
  577. snmp_callback notifycallback;
  578. void * notifycallback_data;
  579. private:
  580. bool m_bThreadRunning;
  581. int m_iPollTimeOut;
  582. // Keep track of the thread.
  583. #ifdef _THREADS
  584. #ifdef WIN32
  585. HANDLE m_hThread;
  586. HANDLE m_hThreadEndEvent;
  587. #elif defined (CPU) && CPU == PPC603
  588. int m_hThread;
  589. #else
  590. pthread_t m_hThread;
  591. #endif
  592. #endif
  593. };
  594. #ifdef SNMP_PP_NAMESPACE
  595. } // end of namespace Snmp_pp
  596. #endif
  597. #endif