snmp_agent.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Portions of this file are subject to the following copyright(s). See
  2. * the Net-SNMP's COPYING file for more details and other copyrights
  3. * that may apply:
  4. */
  5. /*
  6. * Portions of this file are copyrighted by:
  7. * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
  8. * Use is subject to license terms specified in the COPYING file
  9. * distributed with the Net-SNMP package.
  10. *
  11. * Portions of this file are copyrighted by:
  12. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  13. * Use is subject to license terms specified in the COPYING file
  14. * distributed with the Net-SNMP package.
  15. */
  16. /**
  17. * @file snmp_agent.h
  18. *
  19. * @addtogroup agent
  20. * @addtogroup table
  21. * External definitions for functions and variables in snmp_agent.c.
  22. *
  23. * @{
  24. */
  25. #ifndef SNMP_AGENT_H
  26. #define SNMP_AGENT_H
  27. #include <stdint.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include <net-snmp/library/snmp_impl.h>
  32. #include <net-snmp/library/tools.h>
  33. #include <net-snmp/library/data_list.h>
  34. #define SNMP_MAX_PDU_SIZE 64000 /* local constraint on PDU size sent by agent
  35. * (see also SNMP_MAX_MSG_SIZE in snmp_api.h) */
  36. #define SNMP_AGENT_FLAGS_NONE 0x0
  37. #define SNMP_AGENT_FLAGS_CANCEL_IN_PROGRESS 0x1
  38. struct timeval;
  39. /*
  40. * If non-zero, causes the addresses of peers to be logged when receptions
  41. * occur.
  42. */
  43. extern int log_addresses;
  44. /*
  45. * How many ticks since we last aged the address cache entries.
  46. */
  47. extern int lastAddrAge;
  48. /** @typedef struct netsnmp_request_info_s netsnmp_request_info
  49. * Typedefs the netsnmp_request_info_s struct into
  50. * netsnmp_request_info*/
  51. /** @struct netsnmp_request_info_s
  52. * The netsnmp request info structure.
  53. */
  54. typedef struct netsnmp_request_info_s {
  55. /**
  56. * variable bindings
  57. */
  58. netsnmp_variable_list *requestvb;
  59. /**
  60. * can be used to pass information on a per-request basis from a
  61. * helper to the later handlers
  62. */
  63. netsnmp_data_list *parent_data;
  64. /*
  65. * pointer to the agent_request_info for this request
  66. */
  67. struct netsnmp_agent_request_info_s *agent_req_info;
  68. /** don't free, reference to (struct tree)->end */
  69. oid *range_end;
  70. size_t range_end_len;
  71. /*
  72. * flags
  73. */
  74. int delegated;
  75. int processed;
  76. int inclusive;
  77. int status;
  78. /** index in original pdu */
  79. int index;
  80. /** get-bulk */
  81. int repeat;
  82. int orig_repeat;
  83. netsnmp_variable_list *requestvb_start;
  84. /* internal use */
  85. struct netsnmp_request_info_s *next;
  86. struct netsnmp_request_info_s *prev;
  87. struct netsnmp_subtree_s *subtree;
  88. } netsnmp_request_info;
  89. typedef struct netsnmp_set_info_s {
  90. int action;
  91. void *stateRef;
  92. /*
  93. * don't use yet:
  94. */
  95. void **oldData;
  96. int setCleanupFlags;
  97. #define AUTO_FREE_STATEREF 0x01 /* calls free(stateRef) */
  98. #define AUTO_FREE_OLDDATA 0x02 /* calls free(*oldData) */
  99. #define AUTO_UNDO 0x03 /* ... */
  100. } netsnmp_set_info;
  101. typedef struct netsnmp_tree_cache_s {
  102. struct netsnmp_subtree_s *subtree;
  103. netsnmp_request_info *requests_begin;
  104. netsnmp_request_info *requests_end;
  105. } netsnmp_tree_cache;
  106. #define MODE_GET SNMP_MSG_GET
  107. #define MODE_GETNEXT SNMP_MSG_GETNEXT
  108. #define MODE_GETBULK SNMP_MSG_GETBULK
  109. #define MODE_GET_STASH SNMP_MSG_INTERNAL_GET_STASH
  110. #ifndef NETSNMP_NO_WRITE_SUPPORT
  111. #define MODE_IS_GET(x) ((x >= 128) && (x != -1) && (x != SNMP_MSG_SET))
  112. #else /* NETSNMP_NO_WRITE_SUPPORT */
  113. #define MODE_IS_GET(x) ((x >= 128) && (x != -1))
  114. #endif /* NETSNMP_NO_WRITE_SUPPORT */
  115. /* #define MODE_IS_GET(x) ((x == SNMP_MSG_GET) || (x == SNMP_MSG_GETNEXT) || (x == SNMP_MSG_GETBULK) || (x == SNMP_MSG_INTERNAL_GET_STASH)) */
  116. #ifndef NETSNMP_NO_WRITE_SUPPORT
  117. #define MODE_SET_BEGIN SNMP_MSG_INTERNAL_SET_BEGIN
  118. #define MODE_SET_RESERVE1 SNMP_MSG_INTERNAL_SET_RESERVE1
  119. #define MODE_SET_RESERVE2 SNMP_MSG_INTERNAL_SET_RESERVE2
  120. #define MODE_SET_ACTION SNMP_MSG_INTERNAL_SET_ACTION
  121. #define MODE_SET_COMMIT SNMP_MSG_INTERNAL_SET_COMMIT
  122. #define MODE_SET_FREE SNMP_MSG_INTERNAL_SET_FREE
  123. #define MODE_SET_UNDO SNMP_MSG_INTERNAL_SET_UNDO
  124. #define MODE_IS_SET(x) ((x < 128) || (x == -1) || (x == SNMP_MSG_SET))
  125. /* #define MODE_IS_SET(x) (!MODE_IS_GET(x)) */
  126. #endif /* NETSNMP_NO_WRITE_SUPPORT */
  127. #define MODE_BSTEP_PRE_REQUEST SNMP_MSG_INTERNAL_PRE_REQUEST
  128. #define MODE_BSTEP_POST_REQUEST SNMP_MSG_INTERNAL_POST_REQUEST
  129. #define MODE_BSTEP_OBJECT_LOOKUP SNMP_MSG_INTERNAL_OBJECT_LOOKUP
  130. #ifndef NETSNMP_NO_WRITE_SUPPORT
  131. #define MODE_BSTEP_CHECK_VALUE SNMP_MSG_INTERNAL_CHECK_VALUE
  132. #define MODE_BSTEP_ROW_CREATE SNMP_MSG_INTERNAL_ROW_CREATE
  133. #define MODE_BSTEP_UNDO_SETUP SNMP_MSG_INTERNAL_UNDO_SETUP
  134. #define MODE_BSTEP_SET_VALUE SNMP_MSG_INTERNAL_SET_VALUE
  135. #define MODE_BSTEP_CHECK_CONSISTENCY SNMP_MSG_INTERNAL_CHECK_CONSISTENCY
  136. #define MODE_BSTEP_UNDO_SET SNMP_MSG_INTERNAL_UNDO_SET
  137. #define MODE_BSTEP_COMMIT SNMP_MSG_INTERNAL_COMMIT
  138. #define MODE_BSTEP_UNDO_COMMIT SNMP_MSG_INTERNAL_UNDO_COMMIT
  139. #define MODE_BSTEP_IRREVERSIBLE_COMMIT SNMP_MSG_INTERNAL_IRREVERSIBLE_COMMIT
  140. #define MODE_BSTEP_UNDO_CLEANUP SNMP_MSG_INTERNAL_UNDO_CLEANUP
  141. #endif /* NETSNMP_NO_WRITE_SUPPORT */
  142. /** @typedef struct netsnmp_agent_request_info_s netsnmp_agent_request_info
  143. * Typedefs the netsnmp_agent_request_info_s struct into
  144. * netsnmp_agent_request_info
  145. */
  146. /** @struct netsnmp_agent_request_info_s
  147. * The agent transaction request structure
  148. */
  149. typedef struct netsnmp_agent_request_info_s {
  150. int mode;
  151. /** pdu contains authinfo, eg */
  152. /* netsnmp_pdu *pdu; */
  153. struct netsnmp_agent_session_s *asp; /* may not be needed */
  154. /*
  155. * can be used to pass information on a per-pdu basis from a
  156. * helper to the later handlers
  157. */
  158. netsnmp_data_list *agent_data;
  159. } netsnmp_agent_request_info;
  160. typedef struct netsnmp_cachemap_s {
  161. int globalid;
  162. int cacheid;
  163. struct netsnmp_cachemap_s *next;
  164. } netsnmp_cachemap;
  165. typedef struct netsnmp_agent_session_s {
  166. int mode;
  167. netsnmp_session *session;
  168. netsnmp_pdu *pdu;
  169. netsnmp_pdu *orig_pdu;
  170. int rw;
  171. int exact;
  172. int status;
  173. int index;
  174. int oldmode;
  175. struct netsnmp_agent_session_s *next;
  176. /*
  177. * new API pointers
  178. */
  179. netsnmp_agent_request_info *reqinfo;
  180. netsnmp_request_info *requests;
  181. netsnmp_tree_cache *treecache;
  182. netsnmp_variable_list **bulkcache;
  183. int treecache_len; /* length of cache array */
  184. int treecache_num; /* number of current cache entries */
  185. netsnmp_cachemap *cache_store;
  186. int vbcount;
  187. int flags;
  188. } netsnmp_agent_session;
  189. /*
  190. * Address cache handling functions.
  191. */
  192. void netsnmp_addrcache_initialise(void);
  193. void netsnmp_addrcache_destroy(void);
  194. void netsnmp_addrcache_age(void);
  195. /*
  196. * config file parsing routines
  197. */
  198. int handle_snmp_packet(int, netsnmp_session *, int,
  199. netsnmp_pdu *, void *);
  200. void snmp_agent_parse_config(char *, char *);
  201. netsnmp_agent_session *init_agent_snmp_session(netsnmp_session *,
  202. netsnmp_pdu *);
  203. void free_agent_snmp_session(netsnmp_agent_session *);
  204. void
  205. netsnmp_remove_and_free_agent_snmp_session(netsnmp_agent_session
  206. *asp);
  207. #ifdef SNMP_NEED_REQUEST_LIST
  208. void
  209. netsnmp_free_agent_snmp_session_by_session(netsnmp_session * sess,
  210. void (*free_request)
  211. (netsnmp_request_list
  212. *));
  213. #endif
  214. int getNextSessID(void);
  215. void dump_sess_list(void);
  216. int init_master_agent(void);
  217. void shutdown_master_agent(void);
  218. int agent_check_and_process(int block);
  219. void netsnmp_check_delegated_requests(void);
  220. void netsnmp_check_outstanding_agent_requests(void);
  221. int netsnmp_request_set_error(netsnmp_request_info *request,
  222. int error_value);
  223. int netsnmp_check_requests_error(netsnmp_request_info *reqs);
  224. int netsnmp_check_all_requests_error(netsnmp_agent_session *asp,
  225. int look_for_specific);
  226. int
  227. netsnmp_set_all_requests_error(netsnmp_agent_request_info *reqinfo,
  228. netsnmp_request_info *requests,
  229. int error_value);
  230. int netsnmp_request_set_error_idx(netsnmp_request_info *requests,
  231. int error_value, int idx);
  232. int
  233. netsnmp_request_set_error_all(netsnmp_request_info *requests,
  234. int error_value);
  235. /** deprecated, use netsnmp_request_set_error instead */
  236. int netsnmp_set_request_error(netsnmp_agent_request_info
  237. *reqinfo,
  238. netsnmp_request_info
  239. *request, int error_value);
  240. /** deprecated, use netsnmp_request_set_error instead */
  241. int netsnmp_set_mode_request_error(int mode,
  242. netsnmp_request_info
  243. *request,
  244. int error_value);
  245. u_long netsnmp_marker_uptime(marker_t pm);
  246. u_long netsnmp_timeval_uptime(struct timeval *tv);
  247. const_marker_t netsnmp_get_agent_starttime(void);
  248. uint64_t netsnmp_get_agent_runtime(void);
  249. void netsnmp_set_agent_starttime(marker_t s);
  250. u_long netsnmp_get_agent_uptime(void);
  251. void netsnmp_set_agent_uptime(u_long hsec);
  252. int netsnmp_check_transaction_id(int transaction_id);
  253. int netsnmp_agent_check_packet(netsnmp_session *,
  254. struct netsnmp_transport_s
  255. *, void *, int);
  256. int netsnmp_agent_check_parse(netsnmp_session *,
  257. netsnmp_pdu *, int);
  258. int netsnmp_allocate_globalcacheid(void);
  259. int netsnmp_remove_delegated_requests_for_session(netsnmp_session *sess);
  260. /*
  261. * Register and de-register agent NSAPs.
  262. */
  263. struct netsnmp_transport_s;
  264. int netsnmp_register_agent_nsap(struct netsnmp_transport_s
  265. *t);
  266. void netsnmp_deregister_agent_nsap(int handle);
  267. int netsnmp_agent_listen_on(const char *port);
  268. void
  269. netsnmp_agent_add_list_data(netsnmp_agent_request_info *agent,
  270. netsnmp_data_list *node);
  271. int
  272. netsnmp_agent_remove_list_data(netsnmp_agent_request_info *ari,
  273. const char * name);
  274. void *
  275. netsnmp_agent_get_list_data(netsnmp_agent_request_info
  276. *agent, const char *name);
  277. void
  278. netsnmp_free_agent_data_set(netsnmp_agent_request_info *agent);
  279. void
  280. netsnmp_free_agent_data_sets(netsnmp_agent_request_info *agent);
  281. void
  282. netsnmp_free_agent_request_info(netsnmp_agent_request_info *ari);
  283. #ifndef NETSNMP_NO_PDU_STATS
  284. /*
  285. * pdu stats
  286. */
  287. typedef struct netsnmp_pdu_stats_s {
  288. u_long processing_time; /* ms */
  289. time_t timestamp; /* date/time */
  290. netsnmp_pdu *pdu;
  291. } netsnmp_pdu_stats;
  292. netsnmp_container * netsnmp_get_pdu_stats(void);
  293. #endif /* NETSNMP_NO_PDU_STATS */
  294. #ifdef __cplusplus
  295. }
  296. #endif
  297. #endif
  298. /** @} */