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