asn1.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*_############################################################################
  2. _##
  3. _## asn1.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: asn1.h 306 2007-09-14 20:01:45Z katz $
  30. #ifndef _ASN1
  31. #define _ASN1
  32. #include "snmp_pp/config_snmp_pp.h"
  33. #include "snmp_pp/target.h"
  34. #ifdef SNMP_PP_NAMESPACE
  35. namespace Snmp_pp {
  36. #endif
  37. #ifndef EIGHTBIT_SUBIDS
  38. typedef unsigned long oid;
  39. #define MAX_SUBID 0xFFFFFFFF
  40. #else
  41. typedef unsigned char oid;
  42. #define MAX_SUBID 0xFF
  43. #endif
  44. #define MAX_OID_LEN 128 /* max subid's in an oid */
  45. // asn.1 values
  46. #define ASN_BOOLEAN (0x01)
  47. #ifndef ASN_INTEGER
  48. #define ASN_INTEGER (0x02)
  49. #endif
  50. #define ASN_BIT_STR (0x03)
  51. #define ASN_OCTET_STR (0x04)
  52. #ifndef ASN_NULL
  53. #define ASN_NULL (0x05)
  54. #endif
  55. #define ASN_OBJECT_ID (0x06)
  56. #ifndef ASN_SEQUENCE
  57. #define ASN_SEQUENCE (0x10)
  58. #endif
  59. #define ASN_SET (0x11)
  60. #ifndef ASN_UNIVERSAL
  61. #define ASN_UNIVERSAL (0x00)
  62. #endif
  63. #ifndef ASN_APPLICATION
  64. #define ASN_APPLICATION (0x40)
  65. #endif
  66. #ifndef ASN_CONTEXT
  67. #define ASN_CONTEXT (0x80)
  68. #endif
  69. #ifndef ASN_PRIVATE
  70. #define ASN_PRIVATE (0xC0)
  71. #endif
  72. #ifndef ASN_PRIMITIVE
  73. #define ASN_PRIMITIVE (0x00)
  74. #endif
  75. #ifndef ASN_CONSTRUCTOR
  76. #define ASN_CONSTRUCTOR (0x20)
  77. #endif
  78. #define ASN_LONG_LEN (0x80)
  79. #define ASN_EXTENSION_ID (0x1F)
  80. #define ASN_BIT8 (0x80)
  81. #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
  82. #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
  83. #define ASN_MAX_NAME_LEN 128
  84. #define SNMP_VERSION_1 0
  85. #define SNMP_VERSION_2C 1
  86. #define SNMP_VERSION_2STERN 2
  87. #define SNMP_VERSION_3 3
  88. // defined types (from the SMI, RFC 1065)
  89. #define SMI_IPADDRESS (ASN_APPLICATION | 0)
  90. #define SMI_COUNTER (ASN_APPLICATION | 1)
  91. #define SMI_GAUGE (ASN_APPLICATION | 2)
  92. #define SMI_TIMETICKS (ASN_APPLICATION | 3)
  93. #define SMI_OPAQUE (ASN_APPLICATION | 4)
  94. #define SMI_NSAP (ASN_APPLICATION | 5)
  95. #define SMI_COUNTER64 (ASN_APPLICATION | 6)
  96. #define SMI_UINTEGER (ASN_APPLICATION | 7)
  97. #define GET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
  98. #define GETNEXT_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
  99. #define GET_RSP_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
  100. #define SET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
  101. #define TRP_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
  102. #define GETBULK_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
  103. #define INFORM_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
  104. #define TRP2_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
  105. #define REPORT_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
  106. #define SNMP_NOSUCHOBJECT (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
  107. #define SNMP_NOSUCHINSTANCE (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
  108. #define SNMP_ENDOFMIBVIEW (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)
  109. #ifdef _DEBUG
  110. #define ASNERROR( string) debugprintf(3, "ASN parse error (%s)\n", string )
  111. #else
  112. #define ASNERROR( string)
  113. #endif
  114. typedef struct sockaddr_in ipaddr;
  115. // pdu
  116. struct snmp_pdu {
  117. int command; // pdu type
  118. unsigned long reqid; // Request id
  119. #ifdef _SNMPv3
  120. unsigned long msgid;
  121. unsigned long maxsize_scopedpdu;
  122. #endif
  123. unsigned long errstat; // Error status
  124. unsigned long errindex; // Error index
  125. // Trap information
  126. oid *enterprise; // System OID
  127. int enterprise_length;
  128. ipaddr agent_addr; // address of object generating trap
  129. int trap_type; // trap type
  130. int specific_type; // specific type
  131. unsigned long time; // Uptime
  132. // vb list
  133. struct variable_list *variables;
  134. };
  135. // vb list
  136. struct variable_list {
  137. struct variable_list *next_variable; // NULL for last variable
  138. oid *name; // Object identifier of variable
  139. int name_length; // number of subid's in name
  140. unsigned char type; // ASN type of variable
  141. union { // value of variable
  142. long *integer;
  143. unsigned char *string;
  144. oid *objid;
  145. unsigned char *bitstring;
  146. struct counter64 *counter64;
  147. } val;
  148. int val_len;
  149. };
  150. struct counter64 {
  151. unsigned long high;
  152. unsigned long low;
  153. };
  154. // prototypes for encoding routines
  155. DLLOPT unsigned char *asn_parse_int( unsigned char *data, int *datalength,
  156. unsigned char *type,
  157. long int *intp, int intsize);
  158. DLLOPT unsigned char *asn_parse_unsigned_int( unsigned char *data,
  159. int *datalength,
  160. unsigned char *type,
  161. unsigned long *intp,
  162. int intsize);
  163. DLLOPT unsigned char *asn_build_int(unsigned char *data, int *datalength,
  164. const unsigned char type,
  165. const long *intp, int intsize);
  166. DLLOPT unsigned char *asn_build_unsigned_int( unsigned char *data,
  167. int *datalength,
  168. unsigned char type,
  169. unsigned long *intp,
  170. int intsize);
  171. DLLOPT unsigned char *asn_parse_string( unsigned char *data, int *datalength,
  172. unsigned char *type,
  173. unsigned char *string,
  174. int *strlength);
  175. DLLOPT unsigned char *asn_build_string( unsigned char *data, int *datalength,
  176. const unsigned char type,
  177. const unsigned char *string,
  178. const int strlength);
  179. DLLOPT unsigned char *asn_parse_header( unsigned char *data, int *datalength,
  180. unsigned char *type);
  181. DLLOPT unsigned char *asn_build_header( unsigned char *data, int *datalength,
  182. unsigned char type,
  183. int length);
  184. DLLOPT unsigned char *asn_build_sequence( unsigned char *data,
  185. int *datalength,
  186. unsigned char type,
  187. int length);
  188. DLLOPT unsigned char *asn_parse_length( unsigned char *data,
  189. unsigned long *length);
  190. DLLOPT unsigned char *asn_build_length( unsigned char *data, int *datalength,
  191. int length);
  192. DLLOPT unsigned char *asn_parse_objid( unsigned char *data, int *datalength,
  193. unsigned char *type,
  194. oid *objid,
  195. int *objidlength);
  196. DLLOPT unsigned char *asn_build_objid( unsigned char *data,
  197. int *datalength,
  198. unsigned char type,
  199. oid *objid,
  200. int objidlength);
  201. DLLOPT unsigned char *asn_parse_null(unsigned char *data, int *datalength,
  202. unsigned char *type);
  203. DLLOPT unsigned char *asn_build_null( unsigned char *data,int *datalength,
  204. unsigned char type);
  205. DLLOPT unsigned char *asn_parse_bitstring( unsigned char *data,
  206. int *datalength,
  207. unsigned char *type,
  208. unsigned char *string,
  209. int *strlength);
  210. DLLOPT unsigned char *asn_build_bitstring( unsigned char *data,
  211. int *datalength,
  212. unsigned char type,
  213. unsigned char *string,
  214. int strlength);
  215. DLLOPT unsigned char *asn_parse_unsigned_int64( unsigned char *data,
  216. int *datalength,
  217. unsigned char *type,
  218. struct counter64 *cp,
  219. int countersize);
  220. DLLOPT unsigned char *asn_build_unsigned_int64( unsigned char *data,
  221. int *datalength,
  222. unsigned char type,
  223. struct counter64 *cp,
  224. int countersize);
  225. DLLOPT struct snmp_pdu *snmp_pdu_create( int command);
  226. DLLOPT void snmp_free_pdu( struct snmp_pdu *pdu);
  227. DLLOPT int snmp_build(struct snmp_pdu *pdu,
  228. unsigned char *packet,
  229. int *out_length,
  230. const long version,
  231. const unsigned char* community,
  232. const int community_len);
  233. DLLOPT void snmp_add_var(struct snmp_pdu *pdu,
  234. oid *name, int name_length,
  235. SmiVALUE *smival);
  236. DLLOPT int snmp_parse(struct snmp_pdu *pdu,
  237. unsigned char *data,
  238. int data_length,
  239. unsigned char *community_name,
  240. int &community_len,
  241. snmp_version &version);
  242. DLLOPT unsigned char *build_vb(struct snmp_pdu *pdu,
  243. unsigned char *buf, int *buf_len);
  244. DLLOPT unsigned char *build_data_pdu(struct snmp_pdu *pdu,
  245. unsigned char *buf, int *buf_len,
  246. unsigned char *vb_buf, int vb_buf_len);
  247. DLLOPT unsigned char *snmp_build_var_op(unsigned char *data,
  248. oid * var_name,
  249. int *var_name_len,
  250. unsigned char var_val_type,
  251. int var_val_len,
  252. unsigned char *var_val,
  253. int *listlength);
  254. DLLOPT unsigned char *snmp_parse_var_op( unsigned char *data,
  255. oid *var_name,
  256. int *var_name_len,
  257. unsigned char *var_val_type,
  258. int *var_val_len,
  259. unsigned char **var_val,
  260. int *listlength);
  261. DLLOPT int snmp_parse_data_pdu(struct snmp_pdu *pdu,
  262. unsigned char *&data, int &length);
  263. DLLOPT int snmp_parse_vb(struct snmp_pdu *pdu,
  264. unsigned char *&data, int &data_len);
  265. DLLOPT void clear_pdu(struct snmp_pdu *pdu, bool clear_all = false);
  266. /**
  267. * Encode the given values for the HeaderData into the buffer.
  268. * <pre>
  269. * HeaderData ::= SEQUENCE {
  270. * msgID INTEGER (0..2147483647),
  271. * msgMaxSize INTEGER (484..2147483647),
  272. * msgFlags OCTET STRING (SIZE(1)),
  273. * msgSecurityModel INTEGER (0..2147483647)
  274. * }
  275. * </pre>
  276. * @param outBuf - The buffer
  277. * @param maxLength - IN: length of the buffer
  278. * OUT: free bytes left in the buffer
  279. * @param msgID - The message ID
  280. * @param maxMessageSize - The maximum size of a SNMPv3 message
  281. * @param msgFlags - The message Flags
  282. * @param securityModel - The security model
  283. *
  284. * @return - Pointer to the first free byte in the buffer or
  285. * NULL if an error occured
  286. */
  287. DLLOPT unsigned char *asn1_build_header_data(unsigned char *outBuf,
  288. int *maxLength,
  289. long msgID,
  290. long maxMessageSize,
  291. unsigned char msgFlags,
  292. long securityModel);
  293. /**
  294. * Parse the filled HeaderData of a SNMPv3 message and return
  295. * the encoded values.
  296. * <pre>
  297. * HeaderData ::= SEQUENCE {
  298. * msgID INTEGER (0..2147483647),
  299. * msgMaxSize INTEGER (484..2147483647),
  300. * msgFlags OCTET STRING (SIZE(1)),
  301. * msgSecurityModel INTEGER (0..2147483647)
  302. * }
  303. * </pre>
  304. *
  305. * @param buf - The buffer to parse
  306. * @param buf_len - IN: The length of the buffer
  307. * OUT: The number of bytes after this object
  308. * int the buffer
  309. * @param msg_id - OUT: The message id
  310. * @param msg_max_size - OUT: THe maximum message size of the sender
  311. * @param msg_flags - OUT: The message flags
  312. * @param msg_security_model - OUT: The security model used to build this
  313. * message
  314. *
  315. * @return - Returns a pointer to the first byte past the end of
  316. * the object HeaderData (i.e. the start of the next object).
  317. * Returns NULL on any error.
  318. */
  319. DLLOPT unsigned char *asn1_parse_header_data(unsigned char *buf, int *buf_len,
  320. long *msg_id, long *msg_max_size,
  321. unsigned char *msg_flags,
  322. long *msg_security_model);
  323. /**
  324. * Parse the ScopedPDU and return the encoded values.
  325. * <pre>
  326. * ScopedPDU ::= SEQUENCE {
  327. * contextEngineID OCTET STRING,
  328. * contextName OCTET STRING,
  329. * data ANY -- e.g., PDUs as defined in RFC 1905
  330. * }
  331. * </pre>
  332. *
  333. * @param scoped_pdu - The buffer to parse
  334. * @param scoped_pdu_len - IN: The length of the buffer
  335. * OUT: The number of bytes after this object
  336. * int the buffer
  337. * @param context_engine_id - OUT: The parsed contextEngineID
  338. * @param context_engine_id_len - OUT: The length of the contextEngineID
  339. * @param context_name - OUT: The parsed contextName
  340. * @param context_name_len - OUT: The length of the contextName
  341. *
  342. * @return - Pointer to the data object of the scopedPDU or
  343. * NULL on any error.
  344. */
  345. DLLOPT unsigned char *asn1_parse_scoped_pdu(
  346. unsigned char *scoped_pdu, int *scoped_pdu_len,
  347. unsigned char *context_engine_id, int *context_engine_id_len,
  348. unsigned char *context_name, int *context_name_len );
  349. /**
  350. * Encode the given values for the scopedPDU into the buffer.
  351. * <pre>
  352. * ScopedPDU ::= SEQUENCE {
  353. * contextEngineID OCTET STRING
  354. * contextName OCTET STRING
  355. * data ANY -- PDU
  356. * }
  357. * </pre>
  358. * param outBuf - The buffer
  359. * param max_len - IN: length of the buffer
  360. * OUT: free bytes left in the buffer
  361. * param contextEngineID - The contextEngineID
  362. * param contextEngineIDLength - The length of the contextEngineID
  363. * param contextName - The contextName
  364. * param contextNameLength - The length of the contextName
  365. * param data - The already encoded data
  366. * param dataLength - The length of the data
  367. *
  368. * @return - Pointer to the first free byte in the buffer or
  369. * NULL if an error occured
  370. */
  371. DLLOPT unsigned char *asn1_build_scoped_pdu(
  372. unsigned char *outBuf, int *max_len,
  373. unsigned char *contextEngineID, long contextEngineIDLength,
  374. unsigned char *contextName, long contextNameLength,
  375. unsigned char *data, long dataLength);
  376. #ifdef SNMP_PP_NAMESPACE
  377. } // end of namespace Snmp_pp
  378. #endif
  379. #endif // _ASN1