/contrib/bsnmp/lib/snmp.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 284 lines · 199 code · 37 blank · 48 comment · 4 complexity · d84e4a924b1a79ab9d3cbba9e9788683 MD5 · raw file

  1. /*
  2. * Copyright (c) 2001-2003
  3. * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
  4. * All rights reserved.
  5. *
  6. * Author: Harti Brandt <harti@freebsd.org>
  7. *
  8. * Copyright (c) 2010 The FreeBSD Foundation
  9. * All rights reserved.
  10. *
  11. * Portions of this software were developed by Shteryana Sotirova Shopova
  12. * under sponsorship from the FreeBSD Foundation.
  13. *
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * $Begemot: bsnmp/lib/snmp.h,v 1.30 2004/08/06 08:46:54 brandt Exp $
  37. *
  38. * Header file for SNMP functions.
  39. */
  40. #ifndef snmp_h_
  41. #define snmp_h_
  42. #include <sys/types.h>
  43. #define SNMP_COMMUNITY_MAXLEN 128
  44. #define SNMP_MAX_BINDINGS 100
  45. #define SNMP_CONTEXT_NAME_SIZ (32 + 1)
  46. #define SNMP_ENGINE_ID_SIZ 32
  47. #define SNMP_TIME_WINDOW 150
  48. enum snmp_syntax {
  49. SNMP_SYNTAX_NULL = 0,
  50. SNMP_SYNTAX_INTEGER, /* == INTEGER32 */
  51. SNMP_SYNTAX_OCTETSTRING,
  52. SNMP_SYNTAX_OID,
  53. SNMP_SYNTAX_IPADDRESS,
  54. SNMP_SYNTAX_COUNTER,
  55. SNMP_SYNTAX_GAUGE, /* == UNSIGNED32 */
  56. SNMP_SYNTAX_TIMETICKS,
  57. /* v2 additions */
  58. SNMP_SYNTAX_COUNTER64,
  59. SNMP_SYNTAX_NOSUCHOBJECT, /* exception */
  60. SNMP_SYNTAX_NOSUCHINSTANCE, /* exception */
  61. SNMP_SYNTAX_ENDOFMIBVIEW, /* exception */
  62. };
  63. struct snmp_value {
  64. struct asn_oid var;
  65. enum snmp_syntax syntax;
  66. union snmp_values {
  67. int32_t integer; /* also integer32 */
  68. struct {
  69. u_int len;
  70. u_char *octets;
  71. } octetstring;
  72. struct asn_oid oid;
  73. u_char ipaddress[4];
  74. uint32_t uint32; /* also gauge32, counter32,
  75. unsigned32, timeticks */
  76. uint64_t counter64;
  77. } v;
  78. };
  79. enum snmp_version {
  80. SNMP_Verr = 0,
  81. SNMP_V1 = 1,
  82. SNMP_V2c = 2,
  83. SNMP_V3,
  84. };
  85. #define SNMP_MPM_SNMP_V1 0
  86. #define SNMP_MPM_SNMP_V2c 1
  87. #define SNMP_MPM_SNMP_V3 3
  88. #define SNMP_ADM_STR32_SIZ (32 + 1)
  89. #define SNMP_AUTH_KEY_SIZ 40
  90. #define SNMP_PRIV_KEY_SIZ 32
  91. #define SNMP_USM_AUTH_SIZE 12
  92. #define SNMP_USM_PRIV_SIZE 8
  93. #define SNMP_AUTH_HMACMD5_KEY_SIZ 16
  94. #define SNMP_AUTH_HMACSHA_KEY_SIZ 20
  95. #define SNMP_PRIV_AES_KEY_SIZ 16
  96. #define SNMP_PRIV_DES_KEY_SIZ 8
  97. enum snmp_secmodel {
  98. SNMP_SECMODEL_ANY = 0,
  99. SNMP_SECMODEL_SNMPv1 = 1,
  100. SNMP_SECMODEL_SNMPv2c = 2,
  101. SNMP_SECMODEL_USM = 3,
  102. SNMP_SECMODEL_UNKNOWN
  103. };
  104. enum snmp_usm_level {
  105. SNMP_noAuthNoPriv = 1,
  106. SNMP_authNoPriv = 2,
  107. SNMP_authPriv = 3
  108. };
  109. enum snmp_authentication {
  110. SNMP_AUTH_NOAUTH = 0,
  111. SNMP_AUTH_HMAC_MD5,
  112. SNMP_AUTH_HMAC_SHA
  113. };
  114. enum snmp_privacy {
  115. SNMP_PRIV_NOPRIV = 0,
  116. SNMP_PRIV_DES = 1,
  117. SNMP_PRIV_AES
  118. };
  119. struct snmp_engine {
  120. uint8_t engine_id[SNMP_ENGINE_ID_SIZ];
  121. uint32_t engine_len;
  122. int32_t engine_boots;
  123. int32_t engine_time;
  124. int32_t max_msg_size;
  125. };
  126. struct snmp_user {
  127. char sec_name[SNMP_ADM_STR32_SIZ];
  128. enum snmp_authentication auth_proto;
  129. enum snmp_privacy priv_proto;
  130. uint8_t auth_key[SNMP_AUTH_KEY_SIZ];
  131. uint8_t priv_key[SNMP_PRIV_KEY_SIZ];
  132. };
  133. struct snmp_pdu {
  134. char community[SNMP_COMMUNITY_MAXLEN + 1];
  135. enum snmp_version version;
  136. u_int type;
  137. /* SNMPv3 PDU header fields */
  138. int32_t identifier;
  139. uint8_t flags;
  140. int32_t security_model;
  141. struct snmp_engine engine;
  142. /* Associated USM user parameters */
  143. struct snmp_user user;
  144. uint8_t msg_digest[SNMP_USM_AUTH_SIZE];
  145. uint8_t msg_salt[SNMP_USM_PRIV_SIZE];
  146. /* View-based Access Model */
  147. /* XXX: put in separate structure - conflicts with struct snmp_context */
  148. uint32_t context_engine_len;
  149. uint8_t context_engine[SNMP_ENGINE_ID_SIZ];
  150. char context_name[SNMP_CONTEXT_NAME_SIZ];
  151. /* trap only */
  152. struct asn_oid enterprise;
  153. u_char agent_addr[4];
  154. int32_t generic_trap;
  155. int32_t specific_trap;
  156. uint32_t time_stamp;
  157. /* others */
  158. int32_t request_id;
  159. int32_t error_status;
  160. int32_t error_index;
  161. /* fixes for encoding */
  162. size_t outer_len;
  163. size_t scoped_len;
  164. u_char *outer_ptr;
  165. u_char *digest_ptr;
  166. u_char *encrypted_ptr;
  167. u_char *scoped_ptr;
  168. u_char *pdu_ptr;
  169. u_char *vars_ptr;
  170. struct snmp_value bindings[SNMP_MAX_BINDINGS];
  171. u_int nbindings;
  172. };
  173. #define snmp_v1_pdu snmp_pdu
  174. #define SNMP_PDU_GET 0
  175. #define SNMP_PDU_GETNEXT 1
  176. #define SNMP_PDU_RESPONSE 2
  177. #define SNMP_PDU_SET 3
  178. #define SNMP_PDU_TRAP 4 /* v1 */
  179. #define SNMP_PDU_GETBULK 5 /* v2 */
  180. #define SNMP_PDU_INFORM 6 /* v2 */
  181. #define SNMP_PDU_TRAP2 7 /* v2 */
  182. #define SNMP_PDU_REPORT 8 /* v2 */
  183. #define SNMP_ERR_NOERROR 0
  184. #define SNMP_ERR_TOOBIG 1
  185. #define SNMP_ERR_NOSUCHNAME 2 /* v1 */
  186. #define SNMP_ERR_BADVALUE 3 /* v1 */
  187. #define SNMP_ERR_READONLY 4 /* v1 */
  188. #define SNMP_ERR_GENERR 5
  189. #define SNMP_ERR_NO_ACCESS 6 /* v2 */
  190. #define SNMP_ERR_WRONG_TYPE 7 /* v2 */
  191. #define SNMP_ERR_WRONG_LENGTH 8 /* v2 */
  192. #define SNMP_ERR_WRONG_ENCODING 9 /* v2 */
  193. #define SNMP_ERR_WRONG_VALUE 10 /* v2 */
  194. #define SNMP_ERR_NO_CREATION 11 /* v2 */
  195. #define SNMP_ERR_INCONS_VALUE 12 /* v2 */
  196. #define SNMP_ERR_RES_UNAVAIL 13 /* v2 */
  197. #define SNMP_ERR_COMMIT_FAILED 14 /* v2 */
  198. #define SNMP_ERR_UNDO_FAILED 15 /* v2 */
  199. #define SNMP_ERR_AUTH_ERR 16 /* v2 */
  200. #define SNMP_ERR_NOT_WRITEABLE 17 /* v2 */
  201. #define SNMP_ERR_INCONS_NAME 18 /* v2 */
  202. #define SNMP_TRAP_COLDSTART 0
  203. #define SNMP_TRAP_WARMSTART 1
  204. #define SNMP_TRAP_LINKDOWN 2
  205. #define SNMP_TRAP_LINKUP 3
  206. #define SNMP_TRAP_AUTHENTICATION_FAILURE 4
  207. #define SNMP_TRAP_EGP_NEIGHBOR_LOSS 5
  208. #define SNMP_TRAP_ENTERPRISE 6
  209. enum snmp_code {
  210. SNMP_CODE_OK = 0,
  211. SNMP_CODE_FAILED,
  212. SNMP_CODE_BADVERS,
  213. SNMP_CODE_BADLEN,
  214. SNMP_CODE_BADENC,
  215. SNMP_CODE_OORANGE,
  216. SNMP_CODE_BADSECLEVEL,
  217. SNMP_CODE_NOTINTIME,
  218. SNMP_CODE_BADUSER,
  219. SNMP_CODE_BADENGINE,
  220. SNMP_CODE_BADDIGEST,
  221. SNMP_CODE_EDECRYPT
  222. };
  223. #define SNMP_MSG_AUTH_FLAG 0x1
  224. #define SNMP_MSG_PRIV_FLAG 0x2
  225. #define SNMP_MSG_REPORT_FLAG 0x4
  226. #define SNMP_MSG_AUTODISCOVER 0x80
  227. void snmp_value_free(struct snmp_value *);
  228. int snmp_value_parse(const char *, enum snmp_syntax, union snmp_values *);
  229. int snmp_value_copy(struct snmp_value *, const struct snmp_value *);
  230. void snmp_pdu_free(struct snmp_pdu *);
  231. void snmp_pdu_init_secparams(struct snmp_pdu *);
  232. enum snmp_code snmp_pdu_decode(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *);
  233. enum snmp_code snmp_pdu_decode_header(struct asn_buf *, struct snmp_pdu *);
  234. enum snmp_code snmp_pdu_decode_scoped(struct asn_buf *, struct snmp_pdu *, int32_t *);
  235. enum snmp_code snmp_pdu_encode(struct snmp_pdu *, struct asn_buf *);
  236. enum snmp_code snmp_pdu_decode_secmode(struct asn_buf *, struct snmp_pdu *);
  237. int snmp_pdu_snoop(const struct asn_buf *);
  238. void snmp_pdu_dump(const struct snmp_pdu *pdu);
  239. enum snmp_code snmp_passwd_to_keys(struct snmp_user *, char *);
  240. enum snmp_code snmp_get_local_keys(struct snmp_user *, uint8_t *, uint32_t);
  241. enum snmp_code snmp_calc_keychange(struct snmp_user *, uint8_t *);
  242. extern void (*snmp_error)(const char *, ...);
  243. extern void (*snmp_printf)(const char *, ...);
  244. #define TRUTH_MK(F) ((F) ? 1 : 2)
  245. #define TRUTH_GET(T) (((T) == 1) ? 1 : 0)
  246. #define TRUTH_OK(T) ((T) == 1 || (T) == 2)
  247. #endif