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