session_api.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Portions of this file are subject to the following copyright(s). See
  3. * the Net-SNMP's COPYING file for more details and other copyrights
  4. * that may apply:
  5. *
  6. * Portions of this file are copyrighted by:
  7. * Copyright (c) 2016 VMware, 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. #ifndef NET_SNMP_SESSION_API_H
  12. #define NET_SNMP_SESSION_API_H
  13. /**
  14. * Library API routines concerned with specifying and using SNMP "sessions"
  15. * including sending and receiving requests.
  16. */
  17. #include <net-snmp/types.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct session_list;
  22. struct timeval;
  23. NETSNMP_IMPORT
  24. void snmp_sess_init(netsnmp_session *);
  25. /*
  26. * netsnmp_session *snmp_open(session)
  27. * netsnmp_session *session;
  28. *
  29. * Sets up the session with the snmp_session information provided
  30. * by the user. Then opens and binds the necessary UDP port.
  31. * A handle to the created session is returned (this is different than
  32. * the pointer passed to snmp_open()). On any error, NULL is returned
  33. * and snmp_errno is set to the appropriate error code.
  34. */
  35. NETSNMP_IMPORT
  36. netsnmp_session *snmp_open(netsnmp_session *);
  37. /*
  38. * int snmp_close(session)
  39. * netsnmp_session *session;
  40. *
  41. * Close the input session. Frees all data allocated for the session,
  42. * dequeues any pending requests, and closes any sockets allocated for
  43. * the session. Returns 0 on error, 1 otherwise.
  44. *
  45. * snmp_close_sessions() does the same thing for all open sessions
  46. */
  47. NETSNMP_IMPORT
  48. int snmp_close(netsnmp_session *);
  49. NETSNMP_IMPORT
  50. int snmp_close_sessions(void);
  51. NETSNMP_IMPORT
  52. int
  53. _build_initial_pdu_packet(struct session_list *slp, netsnmp_pdu *pdu,
  54. int bulk);
  55. /*
  56. * int snmp_send(session, pdu)
  57. * netsnmp_session *session;
  58. * netsnmp_pdu *pdu;
  59. *
  60. * Sends the input pdu on the session after calling snmp_build to create
  61. * a serialized packet. If necessary, set some of the pdu data from the
  62. * session defaults. Add a request corresponding to this pdu to the list
  63. * of outstanding requests on this session, then send the pdu.
  64. * Returns the request id of the generated packet if applicable, otherwise 1.
  65. * (There is a special case: if the request id is 0, 1 will be returned).
  66. * On any error, 0 is returned.
  67. * The pdu is freed by snmp_send() unless a failure occured.
  68. */
  69. NETSNMP_IMPORT
  70. int snmp_send(netsnmp_session *, netsnmp_pdu *);
  71. /*
  72. * int snmp_async_send(session, pdu, callback, cb_data)
  73. * netsnmp_session *session;
  74. * netsnmp_pdu *pdu;
  75. * netsnmp_callback callback;
  76. * void *cb_data;
  77. *
  78. * Sends the input pdu on the session after calling snmp_build to create
  79. * a serialized packet. If necessary, set some of the pdu data from the
  80. * session defaults. Add a request corresponding to this pdu to the list
  81. * of outstanding requests on this session and store callback and data,
  82. * then send the pdu.
  83. * Returns the request id of the generated packet if applicable, otherwise 1.
  84. * On any error, 0 is returned.
  85. * The pdu is freed by snmp_send() unless a failure occured.
  86. */
  87. NETSNMP_IMPORT
  88. int snmp_async_send(netsnmp_session *, netsnmp_pdu *,
  89. netsnmp_callback, void *);
  90. /*
  91. * void snmp_read(fdset)
  92. * fd_set *fdset;
  93. *
  94. * Checks to see if any of the fd's set in the fdset belong to
  95. * snmp. Each socket with it's fd set has a packet read from it
  96. * and snmp_parse is called on the packet received. The resulting pdu
  97. * is passed to the callback routine for that session. If the callback
  98. * routine returns successfully, the pdu and it's request are deleted.
  99. */
  100. NETSNMP_IMPORT
  101. void snmp_read(fd_set *);
  102. /*
  103. * snmp_read2() is similar to snmp_read(), but accepts a pointer to a
  104. * large file descriptor set instead of a pointer to a regular file
  105. * descriptor set.
  106. */
  107. NETSNMP_IMPORT
  108. void snmp_read2(netsnmp_large_fd_set *);
  109. NETSNMP_IMPORT
  110. int snmp_synch_response(netsnmp_session *, netsnmp_pdu *,
  111. netsnmp_pdu **);
  112. /*
  113. * int snmp_select_info(numfds, fdset, timeout, block)
  114. * int *numfds;
  115. * fd_set *fdset;
  116. * struct timeval *timeout;
  117. * int *block;
  118. *
  119. * Returns info about what snmp requires from a select statement.
  120. * numfds is the number of fds in the list that are significant.
  121. * All file descriptors opened for SNMP are OR'd into the fdset.
  122. * If activity occurs on any of these file descriptors, snmp_read
  123. * should be called with that file descriptor set.
  124. *
  125. * The timeout is the latest time that SNMP can wait for a timeout. The
  126. * select should be done with the minimum time between timeout and any other
  127. * timeouts necessary. This should be checked upon each invocation of select.
  128. * If a timeout is received, snmp_timeout should be called to check if the
  129. * timeout was for SNMP. (snmp_timeout is idempotent)
  130. *
  131. * Block is 1 if the select is requested to block indefinitely, rather than
  132. * time out. If block is input as 1, the timeout value will be treated as
  133. * undefined, but it must be available for setting in snmp_select_info. On
  134. * return, if block is true, the value of timeout will be undefined.
  135. *
  136. * snmp_select_info returns the number of open sockets. (i.e. The number
  137. * of sessions open)
  138. */
  139. NETSNMP_IMPORT
  140. int snmp_select_info(int *, fd_set *, struct timeval *,
  141. int *);
  142. /*
  143. * snmp_select_info2() is similar to snmp_select_info(), but accepts a
  144. * pointer to a large file descriptor set instead of a pointer to a
  145. * regular file descriptor set.
  146. */
  147. NETSNMP_IMPORT
  148. int snmp_select_info2(int *, netsnmp_large_fd_set *,
  149. struct timeval *, int *);
  150. #define NETSNMP_SELECT_NOFLAGS 0x00
  151. #define NETSNMP_SELECT_NOALARMS 0x01
  152. NETSNMP_IMPORT
  153. int snmp_sess_select_info_flags(void *, int *, fd_set *,
  154. struct timeval *, int *, int);
  155. int snmp_sess_select_info2_flags(void *, int *,
  156. netsnmp_large_fd_set *,
  157. struct timeval *, int *, int);
  158. /*
  159. * void snmp_timeout();
  160. *
  161. * snmp_timeout should be called whenever the timeout from snmp_select_info
  162. * expires, but it is idempotent, so snmp_timeout can be polled (probably a
  163. * cpu expensive proposition). snmp_timeout checks to see if any of the
  164. * sessions have an outstanding request that has timed out. If it finds one
  165. * (or more), and that pdu has more retries available, a new packet is formed
  166. * from the pdu and is resent. If there are no more retries available, the
  167. * callback for the session is used to alert the user of the timeout.
  168. */
  169. NETSNMP_IMPORT
  170. void snmp_timeout(void);
  171. /*
  172. * single session API.
  173. *
  174. * These functions perform similar actions as snmp_XX functions,
  175. * but operate on a single session only.
  176. *
  177. * Synopsis:
  178. void * sessp;
  179. netsnmp_session session, *ss;
  180. netsnmp_pdu *pdu, *response;
  181. snmp_sess_init(&session);
  182. session.retries = ...
  183. sessp = snmp_sess_open(&session);
  184. ss = snmp_sess_session(sessp);
  185. if (ss == NULL)
  186. exit(1);
  187. ...
  188. if (ss->community) free(ss->community);
  189. ss->community = strdup(gateway);
  190. ss->community_len = strlen(gateway);
  191. ...
  192. snmp_sess_synch_response(sessp, pdu, &response);
  193. ...
  194. snmp_sess_close(sessp);
  195. * See also:
  196. * snmp_sess_synch_response, in snmp_client.h.
  197. * Notes:
  198. * 1. Invoke snmp_sess_session after snmp_sess_open.
  199. * 2. snmp_sess_session return value is an opaque pointer.
  200. * 3. Do NOT free memory returned by snmp_sess_session.
  201. * 4. Replace snmp_send(ss,pdu) with snmp_sess_send(sessp,pdu)
  202. */
  203. NETSNMP_IMPORT
  204. void *snmp_sess_open(netsnmp_session *);
  205. NETSNMP_IMPORT
  206. void *snmp_sess_pointer(netsnmp_session *);
  207. NETSNMP_IMPORT
  208. netsnmp_session *snmp_sess_session(void *);
  209. NETSNMP_IMPORT
  210. netsnmp_session *snmp_sess_session_lookup(void *);
  211. NETSNMP_IMPORT
  212. netsnmp_session *snmp_sess_lookup_by_name(const char *paramName);
  213. /*
  214. * use return value from snmp_sess_open as void * parameter
  215. */
  216. NETSNMP_IMPORT
  217. int snmp_sess_send(void *, netsnmp_pdu *);
  218. NETSNMP_IMPORT
  219. int snmp_sess_async_send(void *, netsnmp_pdu *,
  220. netsnmp_callback, void *);
  221. NETSNMP_IMPORT
  222. int snmp_sess_select_info(void *, int *, fd_set *,
  223. struct timeval *, int *);
  224. NETSNMP_IMPORT
  225. int snmp_sess_select_info2(void *, int *,
  226. netsnmp_large_fd_set *,
  227. struct timeval *, int *);
  228. /*
  229. * Returns 0 if success, -1 if fail.
  230. */
  231. NETSNMP_IMPORT
  232. int snmp_sess_read(void *, fd_set *);
  233. /*
  234. * Similar to snmp_sess_read(), but accepts a pointer to a large file
  235. * descriptor set instead of a pointer to a file descriptor set.
  236. */
  237. NETSNMP_IMPORT
  238. int snmp_sess_read2(void *,
  239. netsnmp_large_fd_set *);
  240. NETSNMP_IMPORT
  241. void snmp_sess_timeout(void *);
  242. NETSNMP_IMPORT
  243. int snmp_sess_close(void *);
  244. NETSNMP_IMPORT
  245. int snmp_sess_synch_response(void *, netsnmp_pdu *,
  246. netsnmp_pdu **);
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. /*
  251. * Having extracted the main ("public API") calls relevant
  252. * to this area of the Net-SNMP project, the next step is to
  253. * identify the related "public internal API" routines.
  254. *
  255. * In due course, these should probably be gathered
  256. * together into a companion 'library/session_api.h' header file.
  257. * [Or some suitable name]
  258. *
  259. * But for the time being, the expectation is that the
  260. * traditional headers that provided the above definitions
  261. * will probably also cover the relevant internal API calls.
  262. * Hence they are listed here:
  263. */
  264. #include <net-snmp/library/snmp_api.h>
  265. #include <net-snmp/library/snmp_client.h>
  266. #include <net-snmp/library/asn1.h>
  267. #include <net-snmp/library/callback.h>
  268. #include <net-snmp/library/snmp_transport.h>
  269. #include <net-snmp/library/snmp_service.h>
  270. #include <net-snmp/library/snmpCallbackDomain.h>
  271. #ifdef NETSNMP_TRANSPORT_UNIX_DOMAIN
  272. #include <net-snmp/library/snmpUnixDomain.h>
  273. #endif
  274. #ifdef NETSNMP_TRANSPORT_UDP_DOMAIN
  275. #include <net-snmp/library/snmpUDPDomain.h>
  276. #endif
  277. #ifdef NETSNMP_TRANSPORT_TCP_DOMAIN
  278. #include <net-snmp/library/snmpTCPDomain.h>
  279. #endif
  280. #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
  281. #include <net-snmp/library/snmpUDPIPv6Domain.h>
  282. #endif
  283. #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN
  284. #include <net-snmp/library/snmpTCPIPv6Domain.h>
  285. #endif
  286. #ifdef NETSNMP_TRANSPORT_IPX_DOMAIN
  287. #include <net-snmp/library/snmpIPXDomain.h>
  288. #endif
  289. #ifdef NETSNMP_TRANSPORT_AAL5PVC_DOMAIN
  290. #include <net-snmp/library/snmpAAL5PVCDomain.h>
  291. #endif
  292. #include <net-snmp/library/ucd_compat.h>
  293. #endif /* NET_SNMP_SESSION_API_H */