1 #ifndef SNMP_API_H
2 #define SNMP_API_H
3 
4 /*
5  * snmp_api.h - API for access to snmp.
6  *
7  * Caution: when using this library in a multi-threaded application,
8  * the values of global variables "snmp_errno" and "snmp_detail"
9  * cannot be reliably determined.  Suggest using snmp_error()
10  * to obtain the library error codes.
11  */
12 
13 #ifndef DONT_SHARE_ERROR_WITH_OTHER_THREADS
14 #define SET_SNMP_ERROR(x) snmp_errno=(x)
15 #else
16 #define SET_SNMP_ERROR(x)
17 #endif
18 
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /***********************************************************
25 	Copyright 1989 by Carnegie Mellon University
26 
27                       All Rights Reserved
28 
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of CMU not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
36 
37 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43 SOFTWARE.
44 ******************************************************************/
45 
46 
47 struct variable_list;
48 struct timeval;
49 
50 
51 	/*
52 	 * Mimic size and alignment of 'struct sockaddr_storage' (see RFC 2553)
53 	 * But retain field names of traditional 'struct sockaddr'
54 	 */
55 
56 #define _UCD_SS_MAXSIZE   92		/* <= sizeof( sockaddr_un ) */
57 #define _UCD_SS_ALIGNSIZE (sizeof (long))
58 
59 #define _UCD_SS_PAD1SIZE  (_UCD_SS_ALIGNSIZE - sizeof( unsigned short ))
60 #define _UCD_SS_PAD2SIZE  (_UCD_SS_MAXSIZE - \
61 		(sizeof( unsigned short ) + _UCD_SS_PAD1SIZE + _UCD_SS_ALIGNSIZE ))
62 
63 typedef struct {
64 
65 #ifdef STRUCT_SOCKADDR_HAS_SA_UNION_SA_GENERIC_SA_FAMILY2
66 	/*
67 	 * Certain systems (notably Irix 6.x) have a non-traditional
68 	 *   socket structure, and #define the traditional field names.
69 	 * This local definition should reproduce this structure, and still
70 	 *    be large enough to handle any necessary Unix domain addresses.
71 	 */
72   union {
73    struct {
74 #ifdef _HAVE_SA_LEN
75     unsigned char	sa_len2;
76     unsigned char	sa_family2;
77 #else
78     unsigned short	sa_family2;
79 #endif
80     char		sa_data2[ _UCD_SS_PAD1SIZE ];
81    } sa_generic;
82     long		sa_align;
83     char		sa_pad2[ _UCD_SS_PAD2SIZE ];
84   } sa_union;
85 
86 #else
87 
88 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
89     unsigned char	sa_len;
90     unsigned char	sa_family;
91 #else
92     unsigned short	sa_family;
93 #endif
94     char		sa_data[ _UCD_SS_PAD1SIZE ];
95     long		sa_align;
96     char		sa_pad2[ _UCD_SS_PAD2SIZE ];
97 #endif
98 
99 } snmp_ipaddr;
100 
101 #define USM_AUTH_KU_LEN     32
102 #define USM_PRIV_KU_LEN     32
103 
104 struct snmp_pdu {
105 
106 	/*
107 	 * Protocol-version independent fields
108 	 */
109     long    version;
110     int	    command;	/* Type of this PDU */
111     long    reqid;	/* Request id - note: not incremented on retries */
112     long    msgid;      /* Message id for V3 messages
113                          * note: incremented for each retry */
114     long    transid;    /* Unique ID for incoming transactions */
115     long    sessid;     /* Session id for AgentX messages */
116     long    errstat;	/* Error status (non_repeaters in GetBulk) */
117     long    errindex;	/* Error index (max_repetitions in GetBulk) */
118     u_long  time;	/* Uptime */
119     u_long  flags;
120 
121     int	    securityModel;
122     int	    securityLevel;  /* noAuthNoPriv, authNoPriv, authPriv */
123     int	    msgParseModel;
124 
125     snmp_ipaddr  address;	/* Address of peer or trap destination */
126 
127     struct variable_list *variables;
128 
129 
130 	/*
131 	 * SNMPv1 & SNMPv2c fields
132 	 */
133     u_char  *community;		/* community for outgoing requests. */
134     size_t  community_len;	/* Length of community name. */
135 
136 	/*
137 	 * Trap information
138 	 */
139     oid	    *enterprise;	/* System OID */
140     size_t  enterprise_length;
141     long    trap_type;		/* trap type */
142     long    specific_type;	/* specific type */
143     snmp_ipaddr	agent_addr;
144 
145 	/*
146 	 * SNMPv3 fields
147 	 */
148     u_char  *contextEngineID;	/* context snmpEngineID */
149     size_t  contextEngineIDLen; /* Length of contextEngineID */
150     char    *contextName;	/* authoritative contextName */
151     size_t  contextNameLen;	/* Length of contextName */
152     u_char  *securityEngineID;	/* authoritative snmpEngineID for security */
153     size_t  securityEngineIDLen;/* Length of securityEngineID */
154     char    *securityName;	/* on behalf of this principal */
155     size_t  securityNameLen;	/* Length of securityName. */
156 
157 	/*
158 	 * AgentX fields
159 	 *	(also uses SNMPv1 community field)
160 	 */
161     int	    priority;
162     int	    range_subid;
163 
164     void * securityStateRef;
165 };
166 
167 struct snmp_session;
168 typedef int (*snmp_callback) (int, struct snmp_session *, int, struct snmp_pdu *, void *);
169 
170 struct snmp_session {
171 	/*
172 	 * Protocol-version independent fields
173 	 */
174     long  version;
175     int	    retries;	/* Number of retries before timeout. */
176     long    timeout;    /* Number of uS until first timeout, then exponential backoff */
177     u_long  flags;
178     struct  snmp_session *subsession;
179     struct  snmp_session *next;
180 
181     char    *peername;	/* Domain name or dotted IP address of default peer */
182     u_short remote_port;/* UDP port number of peer. */
183     u_short local_port; /* My UDP port number, 0 for default, picked randomly */
184     /* Authentication function or NULL if null authentication is used */
185     u_char    *(*authenticator) (u_char *, size_t *, u_char *, size_t);
186     snmp_callback callback; /* Function to interpret incoming data */
187     /* Pointer to data that the callback function may consider important */
188     void    *callback_magic;
189 
190     int     s_errno;        /* copy of system errno */
191     int     s_snmp_errno;   /* copy of library errno */
192     long    sessid;         /* Session id - AgentX only */
193 
194 	/*
195 	 * SNMPv1 & SNMPv2c fields
196 	 */
197     u_char  *community;	        /* community for outgoing requests. */
198     size_t  community_len;      /* Length of community name. */
199 
200 	/*
201 	 * SNMPv3 fields
202 	 */
203     u_char  isAuthoritative;    /* are we the authoritative engine? */
204     u_char  *contextEngineID;	/* authoritative snmpEngineID */
205     size_t  contextEngineIDLen; /* Length of contextEngineID */
206     u_int   engineBoots;        /* initial engineBoots for remote engine */
207     u_int   engineTime;         /* initial engineTime for remote engine */
208     char    *contextName;	/* authoritative contextName */
209     size_t  contextNameLen;     /* Length of contextName */
210     u_char  *securityEngineID;	/* authoritative snmpEngineID */
211     size_t  securityEngineIDLen;  /* Length of contextEngineID */
212     char    *securityName;	/* on behalf of this principal */
213     size_t  securityNameLen;    /* Length of securityName. */
214     oid     *securityAuthProto; /* auth protocol oid */
215     size_t  securityAuthProtoLen; /* Length of auth protocol oid */
216     u_char  securityAuthKey[USM_AUTH_KU_LEN];  /* Ku for auth protocol XXX */
217     size_t  securityAuthKeyLen; /* Length of Ku for auth protocol */
218     oid     *securityPrivProto; /* priv protocol oid */
219     size_t  securityPrivProtoLen; /* Length of priv protocol oid */
220     u_char  securityPrivKey[USM_PRIV_KU_LEN];  /* Ku for privacy protocol XXX */
221     size_t  securityPrivKeyLen; /* Length of Ku for priv protocol */
222     int	    securityModel;
223     int	    securityLevel;  /* noAuthNoPriv, authNoPriv, authPriv */
224 };
225 
226 /*
227  * A list of all the outstanding requests for a particular session.
228  */
229 #ifdef SNMP_NEED_REQUEST_LIST
230 struct request_list {
231     struct request_list *next_request;
232     long  request_id;	/* request id */
233     long  message_id;	/* message id */
234     snmp_callback callback; /* user callback per request (NULL if unused) */
235     void   *cb_data;   /* user callback data per request (NULL if unused) */
236     int	    retries;	/* Number of retries */
237     u_long timeout;	/* length to wait for timeout */
238     struct timeval time; /* Time this request was made */
239     struct timeval expire;  /* time this request is due to expire */
240     struct  snmp_session *session;
241     struct snmp_pdu *pdu;   /* The pdu for this request
242 			       (saved so it can be retransmitted */
243 };
244 #endif /* SNMP_NEED_REQUEST_LIST */
245 
246 /*
247  * Set fields in session and pdu to the following to get a default or unconfigured value.
248  */
249 #define SNMP_DEFAULT_COMMUNITY_LEN  0	/* to get a default community name */
250 #define SNMP_DEFAULT_RETRIES	    -1
251 #define SNMP_DEFAULT_TIMEOUT	    -1
252 #define SNMP_DEFAULT_REMPORT	    0
253 #define SNMP_DEFAULT_REQID	    -1
254 #define SNMP_DEFAULT_MSGID	    -1
255 #define SNMP_DEFAULT_ERRSTAT	    -1
256 #define SNMP_DEFAULT_ERRINDEX	    -1
257 #define SNMP_DEFAULT_ADDRESS	    0
258 #define SNMP_DEFAULT_PEERNAME	    NULL
259 #define SNMP_DEFAULT_ENTERPRISE_LENGTH	0
260 #define SNMP_DEFAULT_TIME	    0
261 #define SNMP_DEFAULT_VERSION	    -1
262 #define SNMP_DEFAULT_CONTEXT        ""
263 #define SNMP_DEFAULT_AUTH_PROTO     usmHMACMD5AuthProtocol
264 #define SNMP_DEFAULT_AUTH_PROTOLEN  USM_LENGTH_OID_TRANSFORM
265 #define SNMP_DEFAULT_PRIV_PROTO     usmDESPrivProtocol
266 #define SNMP_DEFAULT_PRIV_PROTOLEN  USM_LENGTH_OID_TRANSFORM
267 
268 extern const char *snmp_api_errstring (int);
269 extern void snmp_perror (const char *);
270 extern void snmp_set_detail (const char *);
271 
272 #define SNMP_MAX_MSG_SIZE          1472 /* ethernet MTU minus IP/UDP header */
273 #define SNMP_MAX_MSG_V3_HDRS       (4+3+4+7+7+3+7+16) /* fudge factor=16 */
274 #define SNMP_MAX_ENG_SIZE          32
275 #define SNMP_MAX_SEC_NAME_SIZE     256
276 #define SNMP_MAX_CONTEXT_SIZE      256
277 #define SNMP_SEC_PARAM_BUF_SIZE    256
278 
279 /* set to one to ignore unauthenticated Reports */
280 #define SNMPV3_IGNORE_UNAUTH_REPORTS 0
281 
282 /* authoritative engine definitions */
283 #define SNMP_SESS_NONAUTHORITATIVE 0 /* should be 0 to default to this */
284 #define SNMP_SESS_AUTHORITATIVE    1 /* don't learn engineIDs */
285 #define SNMP_SESS_UNKNOWNAUTH      2 /* sometimes (like NRs) */
286 
287 /* to determine type of Report from varbind_list */
288 #define REPORT_STATS_LEN 9
289 #define REPORT_snmpUnknownSecurityModels_NUM 1
290 #define REPORT_snmpInvalidMsgs_NUM 2
291 #define REPORT_usmStatsUnsupportedSecLevels_NUM 1
292 #define REPORT_usmStatsNotInTimeWindows_NUM 2
293 #define REPORT_usmStatsUnknownUserNames_NUM 3
294 #define REPORT_usmStatsUnknownEngineIDs_NUM 4
295 #define REPORT_usmStatsWrongDigests_NUM 5
296 #define REPORT_usmStatsDecryptionErrors_NUM 6
297 
298 #define SNMP_DETAIL_SIZE        512
299 
300 #define SNMP_FLAGS_DONT_PROBE      0x100    /* don't probe for an engineID */
301 #define SNMP_FLAGS_STREAM_SOCKET   0x80
302 #define SNMP_FLAGS_LISTENING       0x40     /* Server stream sockets only */
303 #define SNMP_FLAGS_SUBSESSION      0x20
304 #define SNMP_FLAGS_STRIKE2         0x02
305 #define SNMP_FLAGS_STRIKE1         0x01
306 
307 #define CLEAR_SNMP_STRIKE_FLAGS(x) \
308 	x &= ~(SNMP_FLAGS_STRIKE2|SNMP_FLAGS_STRIKE1)
309 
310 	/*
311 	 * returns '1' if the session is to be regarded as dead,
312 	 * otherwise set the strike flags appropriately, and return 0
313 	 */
314 #define SET_SNMP_STRIKE_FLAGS(x) \
315 	((   x & SNMP_FLAGS_STRIKE2 ) ? 1 :				\
316 	 ((( x & SNMP_FLAGS_STRIKE1 ) ? ( x |= SNMP_FLAGS_STRIKE2 ) :	\
317 	                                ( x |= SNMP_FLAGS_STRIKE1 )),	\
318 	                                0))
319 
320 /*
321  * Error return values.
322  *
323  * SNMPERR_SUCCESS is the non-PDU "success" code.
324  *
325  * XXX	These should be merged with SNMP_ERR_* defines and confined
326  *	to values < 0.  ???
327  */
328 #define SNMPERR_SUCCESS			(0)  /* XXX  Non-PDU "success" code. */
329 #define SNMPERR_GENERR			(-1)
330 #define SNMPERR_BAD_LOCPORT		(-2)
331 #define SNMPERR_BAD_ADDRESS		(-3)
332 #define SNMPERR_BAD_SESSION		(-4)
333 #define SNMPERR_TOO_LONG		(-5)
334 #define SNMPERR_NO_SOCKET		(-6)
335 #define SNMPERR_V2_IN_V1		(-7)
336 #define SNMPERR_V1_IN_V2		(-8)
337 #define SNMPERR_BAD_REPEATERS		(-9)
338 #define SNMPERR_BAD_REPETITIONS		(-10)
339 #define SNMPERR_BAD_ASN1_BUILD		(-11)
340 #define SNMPERR_BAD_SENDTO		(-12)
341 #define SNMPERR_BAD_PARSE		(-13)
342 #define SNMPERR_BAD_VERSION		(-14)
343 #define SNMPERR_BAD_SRC_PARTY		(-15)
344 #define SNMPERR_BAD_DST_PARTY		(-16)
345 #define SNMPERR_BAD_CONTEXT		(-17)
346 #define SNMPERR_BAD_COMMUNITY		(-18)
347 #define SNMPERR_NOAUTH_DESPRIV		(-19)
348 #define SNMPERR_BAD_ACL			(-20)
349 #define SNMPERR_BAD_PARTY		(-21)
350 #define SNMPERR_ABORT			(-22)
351 #define SNMPERR_UNKNOWN_PDU		(-23)
352 #define SNMPERR_TIMEOUT 		(-24)
353 #define SNMPERR_BAD_RECVFROM 		(-25)
354 #define SNMPERR_BAD_ENG_ID 		(-26)
355 #define SNMPERR_BAD_SEC_NAME 		(-27)
356 #define SNMPERR_BAD_SEC_LEVEL 		(-28)
357 #define SNMPERR_ASN_PARSE_ERR           (-29)
358 #define SNMPERR_UNKNOWN_SEC_MODEL 	(-30)
359 #define SNMPERR_INVALID_MSG             (-31)
360 #define SNMPERR_UNKNOWN_ENG_ID          (-32)
361 #define SNMPERR_UNKNOWN_USER_NAME 	(-33)
362 #define SNMPERR_UNSUPPORTED_SEC_LEVEL 	(-34)
363 #define SNMPERR_AUTHENTICATION_FAILURE 	(-35)
364 #define SNMPERR_NOT_IN_TIME_WINDOW 	(-36)
365 #define SNMPERR_DECRYPTION_ERR          (-37)
366 #define SNMPERR_SC_GENERAL_FAILURE	(-38)
367 #define SNMPERR_SC_NOT_CONFIGURED	(-39)
368 #define SNMPERR_KT_NOT_AVAILABLE	(-40)
369 #define SNMPERR_UNKNOWN_REPORT          (-41)
370 #define SNMPERR_USM_GENERICERROR		(-42)
371 #define SNMPERR_USM_UNKNOWNSECURITYNAME		(-43)
372 #define SNMPERR_USM_UNSUPPORTEDSECURITYLEVEL	(-44)
373 #define SNMPERR_USM_ENCRYPTIONERROR		(-45)
374 #define SNMPERR_USM_AUTHENTICATIONFAILURE	(-46)
375 #define SNMPERR_USM_PARSEERROR			(-47)
376 #define SNMPERR_USM_UNKNOWNENGINEID		(-48)
377 #define SNMPERR_USM_NOTINTIMEWINDOW		(-49)
378 #define SNMPERR_USM_DECRYPTIONERROR		(-50)
379 #define SNMPERR_NOMIB			(-51)
380 #define SNMPERR_RANGE			(-52)
381 #define SNMPERR_MAX_SUBID		(-53)
382 #define SNMPERR_BAD_SUBID		(-54)
383 #define SNMPERR_LONG_OID		(-55)
384 #define SNMPERR_BAD_NAME		(-56)
385 #define SNMPERR_VALUE			(-57)
386 #define SNMPERR_UNKNOWN_OBJID		(-58)
387 #define SNMPERR_NULL_PDU		(-59)
388 #define SNMPERR_NO_VARS			(-60)
389 #define SNMPERR_VAR_TYPE		(-61)
390 #define SNMPERR_MALLOC			(-62)
391 
392 #define SNMPERR_MAX			(-62)
393 
394 #define non_repeaters	errstat
395 #define max_repetitions errindex
396 
397 
398 struct variable_list {
399     struct variable_list *next_variable;    /* NULL for last variable */
400     oid	    *name;  /* Object identifier of variable */
401     size_t  name_length;    /* number of subid's in name */
402     u_char  type;   /* ASN type of variable */
403     union { /* value of variable */
404 	long	*integer;
405 	u_char	*string;
406 	oid	*objid;
407 	u_char  *bitstring;
408 	struct counter64 *counter64;
409 #ifdef OPAQUE_SPECIAL_TYPES
410 	float   *floatVal;
411 	double	*doubleVal;
412 /*	t_union *unionVal; */
413 #endif /* OPAQUE_SPECIAL_TYPES */
414     } val;
415     size_t	    val_len;
416     oid name_loc[MAX_OID_LEN];  /* 90 percentile < 24. */
417     u_char buf[40];             /* 90 percentile < 40. */
418     void *data;			/* (Opaque) hook for additional data */
419     int  index;
420 };
421 
422 
423 
424 /*
425  * struct snmp_session *snmp_open(session)
426  *	struct snmp_session *session;
427  *
428  * Sets up the session with the snmp_session information provided
429  * by the user.  Then opens and binds the necessary UDP port.
430  * A handle to the created session is returned (this is different than
431  * the pointer passed to snmp_open()).  On any error, NULL is returned
432  * and snmp_errno is set to the appropriate error code.
433  */
434 struct snmp_session *snmp_open (struct snmp_session *);
435 
436 /*
437  * int snmp_close(session)
438  *     struct snmp_session *session;
439  *
440  * Close the input session.  Frees all data allocated for the session,
441  * dequeues any pending requests, and closes any sockets allocated for
442  * the session.  Returns 0 on error, 1 otherwise.
443  *
444  * snmp_close_sessions() does the same thing for all open sessions
445  */
446 int snmp_close (struct snmp_session *);
447 int snmp_close_sessions (void);
448 
449 
450 /*
451  * int snmp_send(session, pdu)
452  *     struct snmp_session *session;
453  *     struct snmp_pdu	*pdu;
454  *
455  * Sends the input pdu on the session after calling snmp_build to create
456  * a serialized packet.  If necessary, set some of the pdu data from the
457  * session defaults.  Add a request corresponding to this pdu to the list
458  * of outstanding requests on this session, then send the pdu.
459  * Returns the request id of the generated packet if applicable, otherwise 1.
460  * On any error, 0 is returned.
461  * The pdu is freed by snmp_send() unless a failure occured.
462  */
463 int snmp_send (struct snmp_session *, struct snmp_pdu *);
464 
465 /*
466  * int snmp_async_send(session, pdu, callback, cb_data)
467  *     struct snmp_session *session;
468  *     struct snmp_pdu	*pdu;
469  *     snmp_callback callback;
470  *     void   *cb_data;
471  *
472  * Sends the input pdu on the session after calling snmp_build to create
473  * a serialized packet.  If necessary, set some of the pdu data from the
474  * session defaults.  Add a request corresponding to this pdu to the list
475  * of outstanding requests on this session and store callback and data,
476  * then send the pdu.
477  * Returns the request id of the generated packet if applicable, otherwise 1.
478  * On any error, 0 is returned.
479  * The pdu is freed by snmp_send() unless a failure occured.
480  */
481 int snmp_async_send (struct snmp_session *, struct snmp_pdu *,
482                          snmp_callback, void *);
483 
484 
485 /*
486  * void snmp_read(fdset)
487  *     fd_set  *fdset;
488  *
489  * Checks to see if any of the fd's set in the fdset belong to
490  * snmp.  Each socket with it's fd set has a packet read from it
491  * and snmp_parse is called on the packet received.  The resulting pdu
492  * is passed to the callback routine for that session.  If the callback
493  * routine returns successfully, the pdu and it's request are deleted.
494  */
495 void snmp_read (fd_set *);
496 
497 
498 
499 /*
500  * void
501  * snmp_free_pdu(pdu)
502  *     struct snmp_pdu *pdu;
503  *
504  * Frees the pdu and any malloc'd data associated with it.
505  */
506 void snmp_free_pdu (struct snmp_pdu *);
507 
508 void snmp_free_var (struct variable_list *); /* frees just this one */
509 
510 void snmp_free_varbind(struct variable_list *var); /* frees all in list */
511 
512 /*
513  * int snmp_select_info(numfds, fdset, timeout, block)
514  * int *numfds;
515  * fd_set   *fdset;
516  * struct timeval *timeout;
517  * int *block;
518  *
519  * Returns info about what snmp requires from a select statement.
520  * numfds is the number of fds in the list that are significant.
521  * All file descriptors opened for SNMP are OR'd into the fdset.
522  * If activity occurs on any of these file descriptors, snmp_read
523  * should be called with that file descriptor set.
524  *
525  * The timeout is the latest time that SNMP can wait for a timeout.  The
526  * select should be done with the minimum time between timeout and any other
527  * timeouts necessary.  This should be checked upon each invocation of select.
528  * If a timeout is received, snmp_timeout should be called to check if the
529  * timeout was for SNMP.  (snmp_timeout is idempotent)
530  *
531  * Block is 1 if the select is requested to block indefinitely, rather than
532  * time out.  If block is input as 1, the timeout value will be treated as
533  * undefined, but it must be available for setting in snmp_select_info.  On
534  * return, if block is true, the value of timeout will be undefined.
535  *
536  * snmp_select_info returns the number of open sockets.  (i.e. The number
537  * of sessions open)
538  */
539 int snmp_select_info (int *, fd_set *, struct timeval *, int *);
540 
541 
542 
543 /*
544  * void snmp_timeout();
545  *
546  * snmp_timeout should be called whenever the timeout from snmp_select_info
547  * expires, but it is idempotent, so snmp_timeout can be polled (probably a
548  * cpu expensive proposition).  snmp_timeout checks to see if any of the
549  * sessions have an outstanding request that has timed out.  If it finds one
550  * (or more), and that pdu has more retries available, a new packet is formed
551  * from the pdu and is resent.  If there are no more retries available, the
552  * callback for the session is used to alert the user of the timeout.
553  */
554 
555 void snmp_timeout (void);
556 
557 
558 /*
559  * This routine must be supplied by the application:
560  *
561  * u_char *authenticator(pdu, length, community, community_len)
562  * u_char *pdu;		The rest of the PDU to be authenticated
563  * int *length;		The length of the PDU (updated by the authenticator)
564  * u_char *community;	The community name to authenticate under.
565  * int	community_len	The length of the community name.
566  *
567  * Returns the authenticated pdu, or NULL if authentication failed.
568  * If null authentication is used, the authenticator in snmp_session can be
569  * set to NULL(0).
570  */
571 
572 
573 
574 /*
575  * This routine must be supplied by the application:
576  *
577  * int callback(operation, session, reqid, pdu, magic)
578  * int operation;
579  * struct snmp_session *session;    The session authenticated under.
580  * int reqid;			    The request id of this pdu (0 for TRAP)
581  * struct snmp_pdu *pdu;	    The pdu information.
582  * void *magic			    A link to the data for this routine.
583  *
584  * Returns 1 if request was successful, 0 if it should be kept pending.
585  * Any data in the pdu must be copied because it will be freed elsewhere.
586  * Operations are defined below:
587  */
588 
589 #define RECEIVED_MESSAGE   1
590 #define TIMED_OUT	   2
591 #define SEND_FAILED	   3
592 
593 long snmp_get_next_msgid(void);
594 long snmp_get_next_reqid(void);
595 long snmp_get_next_sessid(void);
596 long snmp_get_next_transid(void);
597 /* provide for backwards compatibility */
598 void snmp_set_dump_packet(int);
599 int snmp_get_dump_packet(void);
600 void snmp_set_quick_print(int);
601 int snmp_get_quick_print(void);
602 void snmp_set_suffix_only(int);
603 int snmp_get_suffix_only(void);
604 void snmp_set_full_objid(int);
605 int snmp_get_full_objid(void);
606 void snmp_set_random_access(int);
607 int snmp_get_random_access(void);
608 
609 int snmp_oid_compare (const oid *, size_t, const oid *, size_t);
610 void init_snmp (const char *);
611 u_char *snmp_pdu_build (struct snmp_pdu *, u_char *, size_t *);
612 #ifdef USE_REVERSE_ASNENCODING
613 u_char *snmp_pdu_rbuild (struct snmp_pdu *, u_char *, size_t *);
614 #endif
615 int snmpv3_parse(struct snmp_pdu *, u_char *, size_t *, u_char  **, struct snmp_session *);
616 int snmpv3_dparse(struct snmp_pdu *, u_char *, size_t *, u_char  **, int);
617 int snmpv3_packet_build(struct snmp_pdu *pdu, u_char *packet, size_t *out_length, u_char *pdu_data, size_t pdu_data_len);
618 int snmpv3_packet_rbuild(struct snmp_pdu *pdu, u_char *packet, size_t *out_length, u_char *pdu_data, size_t pdu_data_len);
619 int snmpv3_make_report(struct snmp_pdu *pdu, int error);
620 int snmpv3_get_report_type(struct snmp_pdu *pdu);
621 int snmp_pdu_parse(struct snmp_pdu *pdu, u_char *data, size_t *length);
622 int snmp_pdu_dparse(struct snmp_pdu *pdu, u_char *data, size_t *length, int);
623 u_char* snmpv3_scopedPDU_parse(struct snmp_pdu *pdu, u_char *cp, size_t *length);
624 u_char* snmpv3_scopedPDU_dparse(struct snmp_pdu *pdu, u_char *cp, size_t *length, int);
625 void snmp_store(const char *type);
626 void snmp_shutdown(const char *type);
627 struct variable_list *snmp_pdu_add_variable (struct snmp_pdu *, oid *, size_t, u_char, u_char *, size_t);
628 struct variable_list *snmp_varlist_add_variable(struct variable_list **varlist,
629 	oid *name, size_t name_length, u_char type, u_char *value, size_t len);
630 int hex_to_binary (const char *, u_char *);
631 int ascii_to_binary (const char *, u_char *);
632 int snmp_add_var (struct snmp_pdu *, oid*, size_t, char, const char *);
633 oid  *snmp_duplicate_objid(oid *objToCopy, size_t);
634 u_int snmp_increment_statistic(int which);
635 u_int snmp_increment_statistic_by(int which, int count);
636 u_int snmp_get_statistic(int which);
637 void  snmp_init_statistics(void);
638 int create_user_from_session(struct snmp_session *session);
639 
640 /* extended open */
641 struct snmp_session *snmp_open_ex (struct snmp_session *,
642   int (*fpre_parse) (struct snmp_session *, snmp_ipaddr),
643   int (*fparse) (struct snmp_session *, struct snmp_pdu *, u_char *, size_t),
644   int (*fpost_parse) (struct snmp_session *, struct snmp_pdu *, int),
645   int (*fbuild) (struct snmp_session *, struct snmp_pdu *, u_char *, size_t *),
646   int (*fcheck) (u_char *, size_t)
647 );
648 
649 /* provided for backwards compatability.  Don't use these functions.
650    See snmp_debug.h and snmp_debug.c instead.
651 */
652 #if HAVE_STDARG_H
653 void DEBUGP (const char *, ...);
654 #else
655 void DEBUGP (va_alist);
656 #endif
657 void DEBUGPOID(oid *, size_t);
658 void snmp_set_do_debugging (int);
659 int snmp_get_do_debugging (void);
660 
661 #ifdef CMU_COMPATIBLE
662 extern int snmp_dump_packet;
663 extern int quick_print;
664 #endif
665 
666 size_t snmp_socket_length   (int family);
667 
668 /*
669  * snmp_error - return error data
670  * Inputs :  address of errno, address of snmp_errno, address of string
671  * Caller must free the string returned after use.
672  */
673 void snmp_error (struct snmp_session *, int *, int *, char **);
674 /*
675  * single session API.
676  *
677  * These functions perform similar actions as snmp_XX functions,
678  * but operate on a single session only.
679  *
680  * Synopsis:
681 
682 	void * sessp;
683 	struct snmp_session session, *ss;
684 	struct snmp_pdu *pdu, *response;
685 
686 	snmp_sess_init(&session);
687 	session.retries = ...
688 	session.remote_port = ...
689 	sessp = snmp_sess_open(&session);
690 	ss = snmp_sess_session(sessp);
691 	if (ss == NULL)
692 		exit(1);
693 	...
694 	if (ss->community) free(ss->community);
695 	ss->community = strdup(gateway);
696 	ss->community_len = strlen(gateway);
697 	...
698 	snmp_sess_synch_response(sessp, pdu, &response);
699 	...
700 	snmp_sess_close(sessp);
701 
702  * See also:
703  * snmp_sess_synch_response, in snmp_client.h.
704 
705  * Notes:
706  *  1. Invoke snmp_sess_session after snmp_sess_open.
707  *  2. snmp_sess_session return value is an opaque pointer.
708  *  3. Do NOT free memory returned by snmp_sess_session.
709  *  4. Replace snmp_send(ss,pdu) with snmp_sess_send(sessp,pdu)
710  */
711 
712 void   snmp_sess_init       (struct snmp_session *);
713 void * snmp_sess_open       (struct snmp_session *);
714 struct snmp_session * snmp_sess_session    (void *);
715 
716 /* use return value from snmp_sess_open as void * parameter */
717 
718 int    snmp_sess_send       (void *, struct snmp_pdu *);
719 int    snmp_sess_async_send (void *, struct snmp_pdu *,
720                                          snmp_callback, void *);
721 int    snmp_sess_select_info (void *, int *, fd_set *,
722                                          struct timeval *, int *);
723 int    snmp_sess_read       (void *, fd_set *);
724 void   snmp_sess_timeout    (void *);
725 int    snmp_sess_close      (void *);
726 
727 void   snmp_sess_error      (void *, int *, int *, char **);
728 void   snmp_sess_perror     (const char *prog_string, struct snmp_session *ss);
729 
730 /* end single session API */
731 
732 /* generic statistic counters */
733 
734 /* snmpv3 statistics */
735 
736 /* mpd stats */
737 #define   STAT_SNMPUNKNOWNSECURITYMODELS     0
738 #define   STAT_SNMPINVALIDMSGS               1
739 #define   STAT_SNMPUNKNOWNPDUHANDLERS        2
740 #define   STAT_MPD_STATS_START               STAT_SNMPUNKNOWNSECURITYMODELS
741 #define   STAT_MPD_STATS_END                 STAT_SNMPUNKNOWNPDUHANDLERS
742 
743 /* usm stats */
744 #define   STAT_USMSTATSUNSUPPORTEDSECLEVELS  3
745 #define   STAT_USMSTATSNOTINTIMEWINDOWS      4
746 #define   STAT_USMSTATSUNKNOWNUSERNAMES      5
747 #define   STAT_USMSTATSUNKNOWNENGINEIDS      6
748 #define   STAT_USMSTATSWRONGDIGESTS          7
749 #define   STAT_USMSTATSDECRYPTIONERRORS      8
750 #define   STAT_USM_STATS_START               STAT_USMSTATSUNSUPPORTEDSECLEVELS
751 #define   STAT_USM_STATS_END                 STAT_USMSTATSDECRYPTIONERRORS
752 
753 /* snmp counters */
754 #define  STAT_SNMPINPKTS                     9
755 #define  STAT_SNMPOUTPKTS                    10
756 #define  STAT_SNMPINBADVERSIONS              11
757 #define  STAT_SNMPINBADCOMMUNITYNAMES        12
758 #define  STAT_SNMPINBADCOMMUNITYUSES         13
759 #define  STAT_SNMPINASNPARSEERRS             14
760 /* #define  STAT_SNMPINBADTYPES		     15 */
761 #define  STAT_SNMPINTOOBIGS                  16
762 #define  STAT_SNMPINNOSUCHNAMES              17
763 #define  STAT_SNMPINBADVALUES                18
764 #define  STAT_SNMPINREADONLYS                19
765 #define  STAT_SNMPINGENERRS                  20
766 #define  STAT_SNMPINTOTALREQVARS             21
767 #define  STAT_SNMPINTOTALSETVARS             22
768 #define  STAT_SNMPINGETREQUESTS              23
769 #define  STAT_SNMPINGETNEXTS                 24
770 #define  STAT_SNMPINSETREQUESTS              25
771 #define  STAT_SNMPINGETRESPONSES             26
772 #define  STAT_SNMPINTRAPS                    27
773 #define  STAT_SNMPOUTTOOBIGS                 28
774 #define  STAT_SNMPOUTNOSUCHNAMES             29
775 #define  STAT_SNMPOUTBADVALUES               30
776 /* #define  STAT_SNMPOUTREADONLYS	     31 */
777 #define  STAT_SNMPOUTGENERRS                 32
778 #define  STAT_SNMPOUTGETREQUESTS             33
779 #define  STAT_SNMPOUTGETNEXTS                34
780 #define  STAT_SNMPOUTSETREQUESTS             35
781 #define  STAT_SNMPOUTGETRESPONSES            36
782 #define  STAT_SNMPOUTTRAPS                   37
783 /* AUTHTRAPENABLE			     38 */
784 #define  STAT_SNMPSILENTDROPS		     39
785 #define  STAT_SNMPPROXYDROPS		     40
786 #define  STAT_SNMP_STATS_START               STAT_SNMPINPKTS
787 #define  STAT_SNMP_STATS_END                 STAT_SNMPOUTTRAPS
788 
789 #define  MAX_STATS                           41
790 
791 #ifdef __cplusplus
792 }
793 #endif
794 
795 #endif /* SNMP_API_H */
796