xref: /freebsd/contrib/bsnmp/lib/snmp.h (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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  * Redistribution of this software and documentation and use in source and
9  * binary forms, with or without modification, are permitted provided that
10  * the following conditions are met:
11  *
12  * 1. Redistributions of source code or documentation must retain the above
13  *    copyright notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
22  * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
25  * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS  BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $Begemot: bsnmp/lib/snmp.h,v 1.29 2003/12/08 17:11:58 hbb Exp $
34  *
35  * Header file for SNMP functions.
36  */
37 #ifndef snmp_h_
38 #define snmp_h_
39 
40 #include <sys/types.h>
41 
42 #define SNMP_COMMUNITY_MAXLEN	128
43 #define SNMP_MAX_BINDINGS	100
44 
45 enum snmp_syntax {
46 	SNMP_SYNTAX_NULL	= 0,
47 	SNMP_SYNTAX_INTEGER,		/* == INTEGER32 */
48 	SNMP_SYNTAX_OCTETSTRING,
49 	SNMP_SYNTAX_OID,
50 	SNMP_SYNTAX_IPADDRESS,
51 	SNMP_SYNTAX_COUNTER,
52 	SNMP_SYNTAX_GAUGE,		/* == UNSIGNED32 */
53 	SNMP_SYNTAX_TIMETICKS,
54 
55 	/* v2 additions */
56 	SNMP_SYNTAX_COUNTER64,
57 	SNMP_SYNTAX_NOSUCHOBJECT,	/* exception */
58 	SNMP_SYNTAX_NOSUCHINSTANCE,	/* exception */
59 	SNMP_SYNTAX_ENDOFMIBVIEW,	/* exception */
60 };
61 
62 struct snmp_value {
63 	struct asn_oid		var;
64 	enum snmp_syntax	syntax;
65 	union snmp_values {
66 	  int32_t		integer;	/* also integer32 */
67 	  struct {
68 	    u_int		len;
69 	    u_char		*octets;
70 	  }			octetstring;
71 	  struct asn_oid	oid;
72 	  u_char		ipaddress[4];
73 	  u_int32_t		uint32;		/* also gauge32, counter32,
74 						   unsigned32, timeticks */
75 	  u_int64_t		counter64;
76 	}			v;
77 };
78 
79 enum snmp_version {
80 	SNMP_Verr = 0,
81 	SNMP_V1 = 1,
82 	SNMP_V2c,
83 };
84 
85 struct snmp_pdu {
86 	char		community[SNMP_COMMUNITY_MAXLEN + 1];
87 	enum snmp_version version;
88 	u_int		type;
89 
90 	/* trap only */
91 	struct asn_oid	enterprise;
92 	u_char		agent_addr[4];
93 	int32_t		generic_trap;
94 	int32_t		specific_trap;
95 	u_int32_t	time_stamp;
96 
97 	/* others */
98 	int32_t		request_id;
99 	int32_t		error_status;
100 	int32_t		error_index;
101 
102 	/* fixes for encoding */
103 	u_char		*outer_ptr;
104 	u_char		*pdu_ptr;
105 	u_char		*vars_ptr;
106 
107 	struct snmp_value bindings[SNMP_MAX_BINDINGS];
108 	u_int		nbindings;
109 };
110 #define snmp_v1_pdu snmp_pdu
111 
112 #define SNMP_PDU_GET		0
113 #define SNMP_PDU_GETNEXT	1
114 #define SNMP_PDU_RESPONSE	2
115 #define SNMP_PDU_SET		3
116 #define SNMP_PDU_TRAP		4	/* v1 */
117 #define SNMP_PDU_GETBULK	5	/* v2 */
118 #define SNMP_PDU_INFORM		6	/* v2 */
119 #define SNMP_PDU_TRAP2		7	/* v2 */
120 #define SNMP_PDU_REPORT		8	/* v2 */
121 
122 #define SNMP_ERR_NOERROR	0
123 #define SNMP_ERR_TOOBIG		1
124 #define SNMP_ERR_NOSUCHNAME	2	/* v1 */
125 #define SNMP_ERR_BADVALUE	3	/* v1 */
126 #define SNMP_ERR_READONLY	4	/* v1 */
127 #define SNMP_ERR_GENERR		5
128 #define SNMP_ERR_NO_ACCESS	6	/* v2 */
129 #define SNMP_ERR_WRONG_TYPE	7	/* v2 */
130 #define SNMP_ERR_WRONG_LENGTH	8	/* v2 */
131 #define SNMP_ERR_WRONG_ENCODING	9	/* v2 */
132 #define SNMP_ERR_WRONG_VALUE	10	/* v2 */
133 #define SNMP_ERR_NO_CREATION	11	/* v2 */
134 #define SNMP_ERR_INCONS_VALUE	12	/* v2 */
135 #define SNMP_ERR_RES_UNAVAIL	13	/* v2 */
136 #define SNMP_ERR_COMMIT_FAILED	14	/* v2 */
137 #define SNMP_ERR_UNDO_FAILED	15	/* v2 */
138 #define SNMP_ERR_AUTH_ERR	16	/* v2 */
139 #define SNMP_ERR_NOT_WRITEABLE	17	/* v2 */
140 #define SNMP_ERR_INCONS_NAME	18	/* v2 */
141 
142 #define SNMP_TRAP_COLDSTART	0
143 #define SNMP_TRAP_WARMSTART	1
144 #define SNMP_TRAP_LINKDOWN	2
145 #define SNMP_TRAP_LINKUP	3
146 #define SNMP_TRAP_AUTHENTICATION_FAILURE	4
147 #define SNMP_TRAP_EGP_NEIGHBOR_LOSS	5
148 #define SNMP_TRAP_ENTERPRISE	6
149 
150 enum snmp_code {
151 	SNMP_CODE_OK = 0,
152 	SNMP_CODE_FAILED,
153 	SNMP_CODE_BADVERS,
154 	SNMP_CODE_BADLEN,
155 	SNMP_CODE_BADENC,
156 	SNMP_CODE_OORANGE,
157 };
158 
159 void snmp_value_free(struct snmp_value *);
160 int snmp_value_parse(const char *, enum snmp_syntax, union snmp_values *);
161 int snmp_value_copy(struct snmp_value *, const struct snmp_value *);
162 
163 void snmp_pdu_free(struct snmp_pdu *);
164 enum snmp_code snmp_pdu_decode(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *);
165 enum snmp_code snmp_pdu_encode(struct snmp_pdu *pdu, struct asn_buf *resp_b);
166 
167 int snmp_pdu_snoop(const struct asn_buf *);
168 
169 void snmp_pdu_dump(const struct snmp_pdu *pdu);
170 
171 extern void (*snmp_error)(const char *, ...);
172 extern void (*snmp_printf)(const char *, ...);
173 
174 #define TRUTH_MK(F) ((F) ? 1 : 2)
175 #define TRUTH_GET(T) (((T) == 1) ? 1 : 0)
176 #define TRUTH_OK(T)  ((T) == 1 || (T) == 2)
177 
178 #endif
179