xref: /freebsd/contrib/bsnmp/lib/snmpclient.h (revision 665877ced9ea53f838f80176739a8621a61d40c6)
1f06ca4afSHartmut Brandt /*
2f06ca4afSHartmut Brandt  * Copyright (c) 2001-2003
3f06ca4afSHartmut Brandt  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4f06ca4afSHartmut Brandt  *	All rights reserved.
5f06ca4afSHartmut Brandt  *
6f06ca4afSHartmut Brandt  * Author: Harti Brandt <harti@freebsd.org>
7f06ca4afSHartmut Brandt  *	   Kendy Kutzner
8f06ca4afSHartmut Brandt  *
9896052c1SHartmut Brandt  * Redistribution and use in source and binary forms, with or without
10896052c1SHartmut Brandt  * modification, are permitted provided that the following conditions
11896052c1SHartmut Brandt  * are met:
12896052c1SHartmut Brandt  * 1. Redistributions of source code must retain the above copyright
13896052c1SHartmut Brandt  *    notice, this list of conditions and the following disclaimer.
14f06ca4afSHartmut Brandt  * 2. Redistributions in binary form must reproduce the above copyright
15f06ca4afSHartmut Brandt  *    notice, this list of conditions and the following disclaimer in the
16f06ca4afSHartmut Brandt  *    documentation and/or other materials provided with the distribution.
17f06ca4afSHartmut Brandt  *
18896052c1SHartmut Brandt  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19896052c1SHartmut Brandt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20896052c1SHartmut Brandt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21896052c1SHartmut Brandt  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22896052c1SHartmut Brandt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23896052c1SHartmut Brandt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24896052c1SHartmut Brandt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25896052c1SHartmut Brandt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26896052c1SHartmut Brandt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27896052c1SHartmut Brandt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28896052c1SHartmut Brandt  * SUCH DAMAGE.
29f06ca4afSHartmut Brandt  *
3069292cedSHartmut Brandt  * $Begemot: bsnmp/lib/snmpclient.h,v 1.19 2005/05/23 11:10:14 brandt_h Exp $
31f06ca4afSHartmut Brandt  */
32f06ca4afSHartmut Brandt #ifndef _BSNMP_SNMPCLIENT_H
33f06ca4afSHartmut Brandt #define _BSNMP_SNMPCLIENT_H
34f06ca4afSHartmut Brandt 
35f06ca4afSHartmut Brandt #include <sys/types.h>
36f06ca4afSHartmut Brandt #include <sys/socket.h>
37f06ca4afSHartmut Brandt #include <sys/time.h>
38f06ca4afSHartmut Brandt #include <netinet/in.h>
39f06ca4afSHartmut Brandt #include <stddef.h>
40f06ca4afSHartmut Brandt 
41f06ca4afSHartmut Brandt 
42f06ca4afSHartmut Brandt #define SNMP_STRERROR_LEN 200
43*665877ceSGleb Smirnoff #define	SNMP_DEFAULT_LOCAL "/var/run/snmpd.sock"
44f06ca4afSHartmut Brandt 
45f06ca4afSHartmut Brandt #define SNMP_LOCAL_PATH	"/tmp/snmpXXXXXXXXXXXXXX"
46f06ca4afSHartmut Brandt 
4770af00a1SHartmut Brandt /*
4870af00a1SHartmut Brandt  * transport methods
4970af00a1SHartmut Brandt  */
5070af00a1SHartmut Brandt #define	SNMP_TRANS_UDP		0
5170af00a1SHartmut Brandt #define	SNMP_TRANS_LOC_DGRAM	1
5270af00a1SHartmut Brandt #define	SNMP_TRANS_LOC_STREAM	2
5304d17814SAndrey V. Elsukov #define	SNMP_TRANS_UDP6		3
54f06ca4afSHartmut Brandt 
55f06ca4afSHartmut Brandt /* type of callback function for responses
56f06ca4afSHartmut Brandt  * this callback function is responsible for free() any memory associated with
57f06ca4afSHartmut Brandt  * any of the PDUs. Therefor it may call snmp_pdu_free() */
58f06ca4afSHartmut Brandt typedef void (*snmp_send_cb_f)(struct snmp_pdu *, struct snmp_pdu *, void *);
59f06ca4afSHartmut Brandt 
60f06ca4afSHartmut Brandt /* type of callback function for timeouts */
61f06ca4afSHartmut Brandt typedef void (*snmp_timeout_cb_f)(void * );
62f06ca4afSHartmut Brandt 
63f06ca4afSHartmut Brandt /* timeout start function */
64f06ca4afSHartmut Brandt typedef void *(*snmp_timeout_start_f)(struct timeval *timeout,
65f06ca4afSHartmut Brandt     snmp_timeout_cb_f callback, void *);
66f06ca4afSHartmut Brandt 
67f06ca4afSHartmut Brandt /* timeout stop function */
68f06ca4afSHartmut Brandt typedef void (*snmp_timeout_stop_f)(void *timeout_id);
69f06ca4afSHartmut Brandt 
70f06ca4afSHartmut Brandt /*
71f06ca4afSHartmut Brandt  * Client context.
72f06ca4afSHartmut Brandt  */
73f06ca4afSHartmut Brandt struct snmp_client {
74f06ca4afSHartmut Brandt 	enum snmp_version	version;
7570af00a1SHartmut Brandt 	int			trans;	/* which transport to use */
76f06ca4afSHartmut Brandt 
77f06ca4afSHartmut Brandt 	/* these two are read-only for the application */
78f06ca4afSHartmut Brandt 	char			*cport;	/* port number as string */
79f06ca4afSHartmut Brandt 	char			*chost;	/* host name or IP address as string */
80f06ca4afSHartmut Brandt 
81f06ca4afSHartmut Brandt 	char			read_community[SNMP_COMMUNITY_MAXLEN + 1];
82f06ca4afSHartmut Brandt 	char			write_community[SNMP_COMMUNITY_MAXLEN + 1];
83f06ca4afSHartmut Brandt 
84135f7de5SShteryana Shopova 	/* SNMPv3 specific fields */
85135f7de5SShteryana Shopova 	int32_t			identifier;
86135f7de5SShteryana Shopova 	int32_t			security_model;
87135f7de5SShteryana Shopova 	struct snmp_engine	engine;
88135f7de5SShteryana Shopova 	struct snmp_user	user;
89135f7de5SShteryana Shopova 
90135f7de5SShteryana Shopova 	/* SNMPv3 Access control - VACM*/
91135f7de5SShteryana Shopova 	uint32_t		clen;
92135f7de5SShteryana Shopova 	uint8_t			cengine[SNMP_ENGINE_ID_SIZ];
93135f7de5SShteryana Shopova 	char			cname[SNMP_CONTEXT_NAME_SIZ];
94135f7de5SShteryana Shopova 
95f06ca4afSHartmut Brandt 	struct timeval		timeout;
96f06ca4afSHartmut Brandt 	u_int			retries;
97f06ca4afSHartmut Brandt 
98f06ca4afSHartmut Brandt 	int			dump_pdus;
99f06ca4afSHartmut Brandt 
100f06ca4afSHartmut Brandt 	size_t			txbuflen;
101f06ca4afSHartmut Brandt 	size_t			rxbuflen;
102f06ca4afSHartmut Brandt 
103f06ca4afSHartmut Brandt 	int			fd;
104f06ca4afSHartmut Brandt 
105f06ca4afSHartmut Brandt 	int32_t			next_reqid;
106f06ca4afSHartmut Brandt 	int32_t			max_reqid;
107f06ca4afSHartmut Brandt 	int32_t			min_reqid;
108f06ca4afSHartmut Brandt 
109f06ca4afSHartmut Brandt 	char			error[SNMP_STRERROR_LEN];
110f06ca4afSHartmut Brandt 
111f06ca4afSHartmut Brandt 	snmp_timeout_start_f	timeout_start;
112f06ca4afSHartmut Brandt 	snmp_timeout_stop_f	timeout_stop;
113f06ca4afSHartmut Brandt 
114f06ca4afSHartmut Brandt 	char			local_path[sizeof(SNMP_LOCAL_PATH)];
115f06ca4afSHartmut Brandt };
116f06ca4afSHartmut Brandt 
117f06ca4afSHartmut Brandt /* the global context */
118f5312007SEdward Tomasz Napierala extern struct snmp_client snmp_client;
119f06ca4afSHartmut Brandt 
120f06ca4afSHartmut Brandt /* initizialies a snmp_client structure */
121f06ca4afSHartmut Brandt void snmp_client_init(struct snmp_client *);
122f06ca4afSHartmut Brandt 
123f06ca4afSHartmut Brandt /* initialize fields */
124f06ca4afSHartmut Brandt int snmp_client_set_host(struct snmp_client *, const char *);
125f06ca4afSHartmut Brandt int snmp_client_set_port(struct snmp_client *, const char *);
126f06ca4afSHartmut Brandt 
127f06ca4afSHartmut Brandt /* open connection to snmp server (hostname or portname can be NULL) */
128f06ca4afSHartmut Brandt int snmp_open(const char *_hostname, const char *_portname,
129f06ca4afSHartmut Brandt     const char *_read_community, const char *_write_community);
130f06ca4afSHartmut Brandt 
131f06ca4afSHartmut Brandt /* close connection */
132f06ca4afSHartmut Brandt void snmp_close(void);
133f06ca4afSHartmut Brandt 
134f06ca4afSHartmut Brandt /* initialize a snmp_pdu structure */
135f06ca4afSHartmut Brandt void snmp_pdu_create(struct snmp_pdu *, u_int _op);
136f06ca4afSHartmut Brandt 
137f06ca4afSHartmut Brandt /* add pairs of (struct asn_oid *, enum snmp_syntax) to an existing pdu */
138f06ca4afSHartmut Brandt int snmp_add_binding(struct snmp_pdu *, ...);
139f06ca4afSHartmut Brandt 
140f06ca4afSHartmut Brandt /* check wheater the answer is valid or not */
141f06ca4afSHartmut Brandt int snmp_pdu_check(const struct snmp_pdu *_req, const struct snmp_pdu *_resp);
142f06ca4afSHartmut Brandt 
143f06ca4afSHartmut Brandt int32_t snmp_pdu_send(struct snmp_pdu *_pdu, snmp_send_cb_f _func, void *_arg);
144f06ca4afSHartmut Brandt 
145f06ca4afSHartmut Brandt /*  append an index to an oid */
146f06ca4afSHartmut Brandt int snmp_oid_append(struct asn_oid *_oid, const char *_fmt, ...);
147f06ca4afSHartmut Brandt 
148f06ca4afSHartmut Brandt /* receive a packet */
149f06ca4afSHartmut Brandt int snmp_receive(int _blocking);
150f06ca4afSHartmut Brandt 
151f06ca4afSHartmut Brandt /*
152f06ca4afSHartmut Brandt  * This structure is used to describe an SNMP table that is to be fetched.
153f06ca4afSHartmut Brandt  * The C-structure that is produced by the fetch function must start with
154f06ca4afSHartmut Brandt  * a TAILQ_ENTRY and an u_int64_t.
155f06ca4afSHartmut Brandt  */
156f06ca4afSHartmut Brandt struct snmp_table {
157f06ca4afSHartmut Brandt 	/* base OID of the table */
158f06ca4afSHartmut Brandt 	struct asn_oid		table;
159f06ca4afSHartmut Brandt 	/* type OID of the LastChange variable for the table if any */
160f06ca4afSHartmut Brandt 	struct asn_oid		last_change;
161f06ca4afSHartmut Brandt 	/* maximum number of iterations if table has changed */
162f06ca4afSHartmut Brandt 	u_int			max_iter;
163f06ca4afSHartmut Brandt 	/* size of the C-structure */
164f06ca4afSHartmut Brandt 	size_t			entry_size;
165f06ca4afSHartmut Brandt 	/* number of index fields */
166f06ca4afSHartmut Brandt 	u_int			index_size;
167f06ca4afSHartmut Brandt 	/* bit mask of required fields */
168896052c1SHartmut Brandt 	uint64_t		req_mask;
169f06ca4afSHartmut Brandt 
170f06ca4afSHartmut Brandt 	/* indexes and columns to fetch. Ended by a NULL syntax entry */
171f06ca4afSHartmut Brandt 	struct snmp_table_entry {
172f06ca4afSHartmut Brandt 	    /* the column sub-oid, ignored for index fields */
173f06ca4afSHartmut Brandt 	    asn_subid_t		subid;
174f06ca4afSHartmut Brandt 	    /* the syntax of the column or index */
175f06ca4afSHartmut Brandt 	    enum snmp_syntax	syntax;
176f06ca4afSHartmut Brandt 	    /* offset of the field into the C-structure. For octet strings
177f06ca4afSHartmut Brandt 	     * this points to an u_char * followed by a size_t */
178f06ca4afSHartmut Brandt 	    off_t		offset;
179f06ca4afSHartmut Brandt #if defined(__GNUC__) && __GNUC__ < 3
180f06ca4afSHartmut Brandt 	}			entries[0];
181f06ca4afSHartmut Brandt #else
182f06ca4afSHartmut Brandt 	}			entries[];
183f06ca4afSHartmut Brandt #endif
184f06ca4afSHartmut Brandt };
185f06ca4afSHartmut Brandt 
186f06ca4afSHartmut Brandt /* callback type for table fetch */
187f06ca4afSHartmut Brandt typedef void (*snmp_table_cb_f)(void *_list, void *_arg, int _res);
188f06ca4afSHartmut Brandt 
189f06ca4afSHartmut Brandt /* fetch a table. The argument points to a TAILQ_HEAD */
190f06ca4afSHartmut Brandt int snmp_table_fetch(const struct snmp_table *descr, void *);
191f06ca4afSHartmut Brandt int snmp_table_fetch_async(const struct snmp_table *, void *,
192f06ca4afSHartmut Brandt     snmp_table_cb_f, void *);
193f06ca4afSHartmut Brandt 
194f06ca4afSHartmut Brandt /* send a request and wait for the response */
195f06ca4afSHartmut Brandt int snmp_dialog(struct snmp_pdu *_req, struct snmp_pdu *_resp);
196f06ca4afSHartmut Brandt 
197135f7de5SShteryana Shopova /* discover an authorative snmpEngineId */
198135f7de5SShteryana Shopova int snmp_discover_engine(char *);
199135f7de5SShteryana Shopova 
20069292cedSHartmut Brandt /* parse a server specification */
20169292cedSHartmut Brandt int snmp_parse_server(struct snmp_client *, const char *);
20269292cedSHartmut Brandt 
203f06ca4afSHartmut Brandt #endif /* _BSNMP_SNMPCLIENT_H */
204