v3.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*_############################################################################
  2. _##
  3. _## v3.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. // $Id: v3.h 287 2007-03-22 22:37:09Z katz $
  30. #ifndef _V3_H
  31. #define _V3_H
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #include "snmp_pp/config_snmp_pp.h"
  35. #ifdef SNMP_PP_NAMESPACE
  36. namespace Snmp_pp {
  37. #endif
  38. class OctetStr;
  39. #ifndef TRUE
  40. #define TRUE 1
  41. #endif
  42. #ifndef FALSE
  43. #define FALSE 0
  44. #endif
  45. /** @name SNMPv3 Security Model values
  46. */
  47. //@{
  48. #define SNMP_SECURITY_MODEL_ANY 0 //!< Not used in SNMP++.
  49. #define SNMP_SECURITY_MODEL_V1 1 //!< Can be used for SNMPv1 only.
  50. #define SNMP_SECURITY_MODEL_V2 2 //!< Can be used for SNMPv2 only.
  51. #define SNMP_SECURITY_MODEL_USM 3 //!< Can be used for SNMPv3 only.
  52. //@}
  53. /**
  54. * Set the amount of log messages you want to get. To disable all
  55. * messages, set the level to -1
  56. *
  57. * @param db_level - New level
  58. */
  59. DLLOPT void debug_set_level(const int db_level);
  60. #ifdef _DEBUG
  61. /**
  62. * SNMP++ logging function.
  63. *
  64. * The default is to log all messages with a level < 19. To change
  65. * that either edit v3.cpp or use a "extern unsigned int debug_level"
  66. * to change the level.
  67. *
  68. * @param db_level - Priority of the message (0 = high)
  69. * @param format - Just like printf
  70. */
  71. DLLOPT void debugprintf(int db_level, const char *format, ...);
  72. /**
  73. * SNMP++ logging function for hex strings.
  74. *
  75. * @param db_level - Priority of the message (0 = high)
  76. * @param comment - Comment printed before the hex dump (may be 0)
  77. * @param data - pointer to the hex data
  78. * @param len - length of the hex data
  79. */
  80. DLLOPT void debughexcprintf(int db_level, const char* comment,
  81. const unsigned char *data, const unsigned int len);
  82. //! Wrapper for debughexcprintf() without comment.
  83. #define debughexprintf(db_level, data, len) \
  84. debughexcprintf(db_level, NULL, data, len);
  85. #else
  86. #ifndef _MSC_VER
  87. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  88. #define debugprintf(db_level,format...)
  89. #else
  90. void debugprintf(int db_level, const char *format, ...);
  91. #endif
  92. #else
  93. // disable any warning for wrong number of arguments in macro
  94. #pragma warning(disable:4002)
  95. #define debugprintf(db_level,format)
  96. #endif //_MSC_VER
  97. #define debughexprintf( db_level, data, len)
  98. #define debughexcprintf(db_level, comment, data, len)
  99. #endif
  100. #ifdef _SNMPv3
  101. #define MAXLENGTH_ENGINEID 32
  102. #define MAXLENGTH_CONTEXT_NAME 32
  103. #define MAXLENGTH_FILENAME 255
  104. #define MAXLENGTH_GLOBALDATA 42 // (2 + 1) + 7 + 7 + 3 + 7 + security
  105. #define oidV3SnmpEngine "1.3.6.1.6.3.10.2.1"
  106. #define oidV3SnmpEngineID "1.3.6.1.6.3.10.2.1.1.0"
  107. #define oidV3SnmpEngineBoots "1.3.6.1.6.3.10.2.1.2.0"
  108. #define oidV3SnmpEngineTime "1.3.6.1.6.3.10.2.1.3.0"
  109. #define oidV3SnmpEngineMaxMessageSize "1.3.6.1.6.3.10.2.1.4.0"
  110. // also defined in agent++/include/vacm.h
  111. #ifndef oidSnmpUnavailableContexts
  112. #define oidSnmpUnavailableContexts "1.3.6.1.6.3.12.1.4.0"
  113. #define oidSnmpUnknownContexts "1.3.6.1.6.3.12.1.5.0"
  114. #endif
  115. /** @name Error codes (storing engineBoots)
  116. *
  117. * These values are returned by getBootCounter() and saveBootCounter().
  118. */
  119. //@{
  120. #define SNMPv3_OK 0 //!< No error
  121. #define SNMPv3_NO_ENTRY_ERROR -1 //!< No line for the engine id found
  122. #define SNMPv3_FILEOPEN_ERROR -2 //!< Unable to open file
  123. #define SNMPv3_TOO_LONG_ERROR -3 //!< The given engineID is too long
  124. #define SNMPv3_FILE_ERROR -4 //!< The given file contains a wrong line
  125. #define SNMPv3_FILECREATE_ERROR -5 //!< The File could not be created
  126. #define SNMPv3_FILERENAME_ERROR -6 //!< Error renaming the temporary file
  127. //@}
  128. /**
  129. * Compare two strings.
  130. *
  131. * @param str1 - The first byte array
  132. * @param ptr1len - Length of first array
  133. * @param str2 - The second byte array
  134. * @param ptr2len - Length of second array
  135. *
  136. * @return 1 if the strings are identical, 0 if not.
  137. */
  138. DLLOPT int unsignedCharCompare(const unsigned char *str1,
  139. const long int ptr1len,
  140. const unsigned char *str2,
  141. const long int ptr2len);
  142. /**
  143. * String copy function.
  144. *
  145. * @note The returned string has to be deleted with "delete []".
  146. *
  147. * @param src - Source string
  148. * @param srclen - Length of source string
  149. *
  150. * @return Pointer to a null terminated copy of src (or 0 on error).
  151. */
  152. DLLOPT unsigned char *v3strcpy(const unsigned char *src, const int srclen);
  153. /**
  154. * Encode the given string into the output buffer. For each byte
  155. * of the string two bytes in the output buffer are used. The
  156. * output buffer will contain chars from 0x64 to 0x79.
  157. *
  158. * @param in - The string (for example engine id) to encode
  159. * @param in_length - The length of the engineID
  160. * @param out - The output buffer for the encoded string, must have
  161. * lenth 2 * in_length
  162. */
  163. DLLOPT void encodeString(const unsigned char* in, const int in_length,
  164. char* out);
  165. /**
  166. * Decode the given encoded string into the output buffer.
  167. *
  168. * @param in - The encoded string
  169. * @param in_length - The length of the encoded string
  170. * @param out - Buffer for the decoded string (half size of input
  171. * string). The String will be null terminated.
  172. */
  173. DLLOPT void decodeString(const unsigned char* in, const int in_length,
  174. char* out);
  175. /**
  176. * Read the bootCounter of the given engineID stored in the given file.
  177. *
  178. * @param fileName - The name of the file
  179. * @param engineId - Read the bootCounter for this enigneID
  180. * @param boot - OUT: the bootCounter that was read
  181. *
  182. * @return One of SNMPv3_OK, SNMPv3_TOO_LONG_ERROR, SNMPv3_FILE_ERROR,
  183. * SNMPv3_NO_ENTRY_ERROR, SNMPv3_FILEOPEN_ERROR
  184. *
  185. */
  186. DLLOPT int getBootCounter(const char *fileName,
  187. const OctetStr &engineId, unsigned int &boot);
  188. /**
  189. * Store the bootCounter of the given engineID in the given file.
  190. *
  191. * @param fileName - The name of the file
  192. * @param engineId - Store the bootCounter for this enigneID
  193. * @param boot - The bootCounter
  194. *
  195. * @return One of SNMPv3_OK, SNMPv3_FILEOPEN_ERROR, SNMPv3_FILECREATE_ERROR,
  196. * SNMPv3_FILERENAME_ERROR.
  197. *
  198. */
  199. DLLOPT int saveBootCounter(const char *fileName,
  200. const OctetStr &engineId, const unsigned int boot);
  201. #endif // _SNMPv3
  202. /**
  203. * Tool class for easy allocation of buffer space.
  204. */
  205. template <class T> class Buffer
  206. {
  207. public:
  208. /// Constructor: Allocate a buffer for size objects.
  209. Buffer(const unsigned int size)
  210. {
  211. ptr = new T[size];
  212. if (ptr)
  213. len = size;
  214. else
  215. len = 0;
  216. }
  217. /// Destructor: Free allocated buffer
  218. ~Buffer()
  219. {
  220. if (ptr) delete [] ptr;
  221. }
  222. /// Get the buffer pointer
  223. T *get_ptr()
  224. {
  225. return ptr;
  226. }
  227. /// Overwrite the buffer space with zero.
  228. void clear()
  229. {
  230. if (ptr)
  231. memset(ptr, 0, len * sizeof(T));
  232. }
  233. private:
  234. T *ptr;
  235. unsigned int len;
  236. };
  237. // only for compatibility do not use these values:
  238. #define SecurityModel_any SNMP_SECURITY_MODEL_ANY
  239. #define SecurityModel_v1 SNMP_SECURITY_MODEL_V1
  240. #define SecurityModel_v2 SNMP_SECURITY_MODEL_V2
  241. #define SecurityModel_USM SNMP_SECURITY_MODEL_USM
  242. #ifdef SNMP_PP_NAMESPACE
  243. } // end of namespace Snmp_pp
  244. #endif
  245. #endif // _V3_H