xref: /freebsd-src/contrib/bsnmp/lib/snmpclient.h (revision f53120073b321c1d2e42641ca22414992ddbe0ff)
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
43f06ca4afSHartmut Brandt 
44f06ca4afSHartmut Brandt #define SNMP_LOCAL_PATH	"/tmp/snmpXXXXXXXXXXXXXX"
45f06ca4afSHartmut Brandt 
4670af00a1SHartmut Brandt /*
4770af00a1SHartmut Brandt  * transport methods
4870af00a1SHartmut Brandt  */
4970af00a1SHartmut Brandt #define	SNMP_TRANS_UDP		0
5070af00a1SHartmut Brandt #define	SNMP_TRANS_LOC_DGRAM	1
5170af00a1SHartmut Brandt #define	SNMP_TRANS_LOC_STREAM	2
5204d17814SAndrey V. Elsukov #define	SNMP_TRANS_UDP6		3
53f06ca4afSHartmut Brandt 
54f06ca4afSHartmut Brandt /* type of callback function for responses
55f06ca4afSHartmut Brandt  * this callback function is responsible for free() any memory associated with
56f06ca4afSHartmut Brandt  * any of the PDUs. Therefor it may call snmp_pdu_free() */
57f06ca4afSHartmut Brandt typedef void (*snmp_send_cb_f)(struct snmp_pdu *, struct snmp_pdu *, void *);
58f06ca4afSHartmut Brandt 
59f06ca4afSHartmut Brandt /* type of callback function for timeouts */
60f06ca4afSHartmut Brandt typedef void (*snmp_timeout_cb_f)(void * );
61f06ca4afSHartmut Brandt 
62f06ca4afSHartmut Brandt /* timeout start function */
63f06ca4afSHartmut Brandt typedef void *(*snmp_timeout_start_f)(struct timeval *timeout,
64f06ca4afSHartmut Brandt     snmp_timeout_cb_f callback, void *);
65f06ca4afSHartmut Brandt 
66f06ca4afSHartmut Brandt /* timeout stop function */
67f06ca4afSHartmut Brandt typedef void (*snmp_timeout_stop_f)(void *timeout_id);
68f06ca4afSHartmut Brandt 
69f06ca4afSHartmut Brandt /*
70f06ca4afSHartmut Brandt  * Client context.
71f06ca4afSHartmut Brandt  */
72f06ca4afSHartmut Brandt struct snmp_client {
73f06ca4afSHartmut Brandt 	enum snmp_version	version;
7470af00a1SHartmut Brandt 	int			trans;	/* which transport to use */
75f06ca4afSHartmut Brandt 
76f06ca4afSHartmut Brandt 	/* these two are read-only for the application */
77f06ca4afSHartmut Brandt 	char			*cport;	/* port number as string */
78f06ca4afSHartmut Brandt 	char			*chost;	/* host name or IP address as string */
79f06ca4afSHartmut Brandt 
80f06ca4afSHartmut Brandt 	char			read_community[SNMP_COMMUNITY_MAXLEN + 1];
81f06ca4afSHartmut Brandt 	char			write_community[SNMP_COMMUNITY_MAXLEN + 1];
82f06ca4afSHartmut Brandt 
83135f7de5SShteryana Shopova 	/* SNMPv3 specific fields */
84135f7de5SShteryana Shopova 	int32_t			identifier;
85135f7de5SShteryana Shopova 	int32_t			security_model;
86135f7de5SShteryana Shopova 	struct snmp_engine	engine;
87135f7de5SShteryana Shopova 	struct snmp_user	user;
88135f7de5SShteryana Shopova 
89135f7de5SShteryana Shopova 	/* SNMPv3 Access control - VACM*/
90135f7de5SShteryana Shopova 	uint32_t		clen;
91135f7de5SShteryana Shopova 	uint8_t			cengine[SNMP_ENGINE_ID_SIZ];
92135f7de5SShteryana Shopova 	char			cname[SNMP_CONTEXT_NAME_SIZ];
93135f7de5SShteryana Shopova 
94f06ca4afSHartmut Brandt 	struct timeval		timeout;
95f06ca4afSHartmut Brandt 	u_int			retries;
96f06ca4afSHartmut Brandt 
97f06ca4afSHartmut Brandt 	int			dump_pdus;
98f06ca4afSHartmut Brandt 
99f06ca4afSHartmut Brandt 	size_t			txbuflen;
100f06ca4afSHartmut Brandt 	size_t			rxbuflen;
101f06ca4afSHartmut Brandt 
102f06ca4afSHartmut Brandt 	int			fd;
103f06ca4afSHartmut Brandt 
104f06ca4afSHartmut Brandt 	int32_t			next_reqid;
105f06ca4afSHartmut Brandt 	int32_t			max_reqid;
106f06ca4afSHartmut Brandt 	int32_t			min_reqid;
107f06ca4afSHartmut Brandt 
108f06ca4afSHartmut Brandt 	char			error[SNMP_STRERROR_LEN];
109f06ca4afSHartmut Brandt 
110f06ca4afSHartmut Brandt 	snmp_timeout_start_f	timeout_start;
111f06ca4afSHartmut Brandt 	snmp_timeout_stop_f	timeout_stop;
112f06ca4afSHartmut Brandt 
113f06ca4afSHartmut Brandt 	char			local_path[sizeof(SNMP_LOCAL_PATH)];
114f06ca4afSHartmut Brandt };
115f06ca4afSHartmut Brandt 
116f06ca4afSHartmut Brandt /* the global context */
117*f5312007SEdward Tomasz Napierala extern struct snmp_client snmp_client;
118f06ca4afSHartmut Brandt 
119f06ca4afSHartmut Brandt /* initizialies a snmp_client structure */
120f06ca4afSHartmut Brandt void snmp_client_init(struct snmp_client *);
121f06ca4afSHartmut Brandt 
122f06ca4afSHartmut Brandt /* initialize fields */
123f06ca4afSHartmut Brandt int snmp_client_set_host(struct snmp_client *, const char *);
124f06ca4afSHartmut Brandt int snmp_client_set_port(struct snmp_client *, const char *);
125f06ca4afSHartmut Brandt 
126f06ca4afSHartmut Brandt /* open connection to snmp server (hostname or portname can be NULL) */
127f06ca4afSHartmut Brandt int snmp_open(const char *_hostname, const char *_portname,
128f06ca4afSHartmut Brandt     const char *_read_community, const char *_write_community);
129f06ca4afSHartmut Brandt 
130f06ca4afSHartmut Brandt /* close connection */
131f06ca4afSHartmut Brandt void snmp_close(void);
132f06ca4afSHartmut Brandt 
133f06ca4afSHartmut Brandt /* initialize a snmp_pdu structure */
134f06ca4afSHartmut Brandt void snmp_pdu_create(struct snmp_pdu *, u_int _op);
135f06ca4afSHartmut Brandt 
136f06ca4afSHartmut Brandt /* add pairs of (struct asn_oid *, enum snmp_syntax) to an existing pdu */
137f06ca4afSHartmut Brandt int snmp_add_binding(struct snmp_pdu *, ...);
138f06ca4afSHartmut Brandt 
139f06ca4afSHartmut Brandt /* check wheater the answer is valid or not */
140f06ca4afSHartmut Brandt int snmp_pdu_check(const struct snmp_pdu *_req, const struct snmp_pdu *_resp);
141f06ca4afSHartmut Brandt 
142f06ca4afSHartmut Brandt int32_t snmp_pdu_send(struct snmp_pdu *_pdu, snmp_send_cb_f _func, void *_arg);
143f06ca4afSHartmut Brandt 
144f06ca4afSHartmut Brandt /*  append an index to an oid */
145f06ca4afSHartmut Brandt int snmp_oid_append(struct asn_oid *_oid, const char *_fmt, ...);
146f06ca4afSHartmut Brandt 
147f06ca4afSHartmut Brandt /* receive a packet */
148f06ca4afSHartmut Brandt int snmp_receive(int _blocking);
149f06ca4afSHartmut Brandt 
150f06ca4afSHartmut Brandt /*
151f06ca4afSHartmut Brandt  * This structure is used to describe an SNMP table that is to be fetched.
152f06ca4afSHartmut Brandt  * The C-structure that is produced by the fetch function must start with
153f06ca4afSHartmut Brandt  * a TAILQ_ENTRY and an u_int64_t.
154f06ca4afSHartmut Brandt  */
155f06ca4afSHartmut Brandt struct snmp_table {
156f06ca4afSHartmut Brandt 	/* base OID of the table */
157f06ca4afSHartmut Brandt 	struct asn_oid		table;
158f06ca4afSHartmut Brandt 	/* type OID of the LastChange variable for the table if any */
159f06ca4afSHartmut Brandt 	struct asn_oid		last_change;
160f06ca4afSHartmut Brandt 	/* maximum number of iterations if table has changed */
161f06ca4afSHartmut Brandt 	u_int			max_iter;
162f06ca4afSHartmut Brandt 	/* size of the C-structure */
163f06ca4afSHartmut Brandt 	size_t			entry_size;
164f06ca4afSHartmut Brandt 	/* number of index fields */
165f06ca4afSHartmut Brandt 	u_int			index_size;
166f06ca4afSHartmut Brandt 	/* bit mask of required fields */
167896052c1SHartmut Brandt 	uint64_t		req_mask;
168f06ca4afSHartmut Brandt 
169f06ca4afSHartmut Brandt 	/* indexes and columns to fetch. Ended by a NULL syntax entry */
170f06ca4afSHartmut Brandt 	struct snmp_table_entry {
171f06ca4afSHartmut Brandt 	    /* the column sub-oid, ignored for index fields */
172f06ca4afSHartmut Brandt 	    asn_subid_t		subid;
173f06ca4afSHartmut Brandt 	    /* the syntax of the column or index */
174f06ca4afSHartmut Brandt 	    enum snmp_syntax	syntax;
175f06ca4afSHartmut Brandt 	    /* offset of the field into the C-structure. For octet strings
176f06ca4afSHartmut Brandt 	     * this points to an u_char * followed by a size_t */
177f06ca4afSHartmut Brandt 	    off_t		offset;
178f06ca4afSHartmut Brandt #if defined(__GNUC__) && __GNUC__ < 3
179f06ca4afSHartmut Brandt 	}			entries[0];
180f06ca4afSHartmut Brandt #else
181f06ca4afSHartmut Brandt 	}			entries[];
182f06ca4afSHartmut Brandt #endif
183f06ca4afSHartmut Brandt };
184f06ca4afSHartmut Brandt 
185f06ca4afSHartmut Brandt /* callback type for table fetch */
186f06ca4afSHartmut Brandt typedef void (*snmp_table_cb_f)(void *_list, void *_arg, int _res);
187f06ca4afSHartmut Brandt 
188f06ca4afSHartmut Brandt /* fetch a table. The argument points to a TAILQ_HEAD */
189f06ca4afSHartmut Brandt int snmp_table_fetch(const struct snmp_table *descr, void *);
190f06ca4afSHartmut Brandt int snmp_table_fetch_async(const struct snmp_table *, void *,
191f06ca4afSHartmut Brandt     snmp_table_cb_f, void *);
192f06ca4afSHartmut Brandt 
193f06ca4afSHartmut Brandt /* send a request and wait for the response */
194f06ca4afSHartmut Brandt int snmp_dialog(struct snmp_pdu *_req, struct snmp_pdu *_resp);
195f06ca4afSHartmut Brandt 
196135f7de5SShteryana Shopova /* discover an authorative snmpEngineId */
197135f7de5SShteryana Shopova int snmp_discover_engine(char *);
198135f7de5SShteryana Shopova 
19969292cedSHartmut Brandt /* parse a server specification */
20069292cedSHartmut Brandt int snmp_parse_server(struct snmp_client *, const char *);
20169292cedSHartmut Brandt 
202f06ca4afSHartmut Brandt #endif /* _BSNMP_SNMPCLIENT_H */
203