1122394Sharti/*
2122394Sharti * Copyright (c) 2001-2003
3122394Sharti *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4122394Sharti *	All rights reserved.
5122394Sharti *
6122394Sharti * Author: Harti Brandt <harti@freebsd.org>
7122394Sharti *	   Kendy Kutzner
8133211Sharti *
9133211Sharti * Redistribution and use in source and binary forms, with or without
10133211Sharti * modification, are permitted provided that the following conditions
11133211Sharti * are met:
12133211Sharti * 1. Redistributions of source code must retain the above copyright
13133211Sharti *    notice, this list of conditions and the following disclaimer.
14122394Sharti * 2. Redistributions in binary form must reproduce the above copyright
15122394Sharti *    notice, this list of conditions and the following disclaimer in the
16122394Sharti *    documentation and/or other materials provided with the distribution.
17133211Sharti *
18133211Sharti * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19133211Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20133211Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21133211Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22133211Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23133211Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24133211Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25133211Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26133211Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27133211Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28133211Sharti * SUCH DAMAGE.
29122394Sharti *
30146525Sharti * $Begemot: bsnmp/lib/snmpclient.h,v 1.19 2005/05/23 11:10:14 brandt_h Exp $
31122394Sharti */
32122394Sharti#ifndef _BSNMP_SNMPCLIENT_H
33122394Sharti#define _BSNMP_SNMPCLIENT_H
34122394Sharti
35122394Sharti#include <sys/types.h>
36122394Sharti#include <sys/socket.h>
37122394Sharti#include <sys/time.h>
38122394Sharti#include <netinet/in.h>
39122394Sharti#include <stddef.h>
40122394Sharti
41122394Sharti
42122394Sharti#define SNMP_STRERROR_LEN 200
43122394Sharti
44122394Sharti#define SNMP_LOCAL_PATH	"/tmp/snmpXXXXXXXXXXXXXX"
45122394Sharti
46124861Sharti/*
47124861Sharti * transport methods
48124861Sharti */
49124861Sharti#define	SNMP_TRANS_UDP		0
50124861Sharti#define	SNMP_TRANS_LOC_DGRAM	1
51124861Sharti#define	SNMP_TRANS_LOC_STREAM	2
52122394Sharti
53122394Sharti/* type of callback function for responses
54122394Sharti * this callback function is responsible for free() any memory associated with
55122394Sharti * any of the PDUs. Therefor it may call snmp_pdu_free() */
56122394Shartitypedef void (*snmp_send_cb_f)(struct snmp_pdu *, struct snmp_pdu *, void *);
57122394Sharti
58122394Sharti/* type of callback function for timeouts */
59122394Shartitypedef void (*snmp_timeout_cb_f)(void * );
60122394Sharti
61122394Sharti/* timeout start function */
62122394Shartitypedef void *(*snmp_timeout_start_f)(struct timeval *timeout,
63122394Sharti    snmp_timeout_cb_f callback, void *);
64122394Sharti
65122394Sharti/* timeout stop function */
66122394Shartitypedef void (*snmp_timeout_stop_f)(void *timeout_id);
67122394Sharti
68122394Sharti/*
69122394Sharti * Client context.
70122394Sharti */
71122394Shartistruct snmp_client {
72216294Ssyrinx	enum snmp_version	version;
73216294Ssyrinx	int			trans;	/* which transport to use */
74122394Sharti
75122394Sharti	/* these two are read-only for the application */
76216294Ssyrinx	char			*cport;	/* port number as string */
77216294Ssyrinx	char			*chost;	/* host name or IP address as string */
78122394Sharti
79216294Ssyrinx	char			read_community[SNMP_COMMUNITY_MAXLEN + 1];
80216294Ssyrinx	char			write_community[SNMP_COMMUNITY_MAXLEN + 1];
81122394Sharti
82216294Ssyrinx	/* SNMPv3 specific fields */
83216294Ssyrinx	int32_t			identifier;
84216294Ssyrinx	int32_t			security_model;
85216294Ssyrinx	struct snmp_engine	engine;
86216294Ssyrinx	struct snmp_user	user;
87122394Sharti
88216294Ssyrinx	/* SNMPv3 Access control - VACM*/
89216294Ssyrinx	uint32_t		clen;
90216294Ssyrinx	uint8_t			cengine[SNMP_ENGINE_ID_SIZ];
91216294Ssyrinx	char			cname[SNMP_CONTEXT_NAME_SIZ];
92122394Sharti
93216294Ssyrinx	struct timeval		timeout;
94216294Ssyrinx	u_int			retries;
95122394Sharti
96216294Ssyrinx	int			dump_pdus;
97122394Sharti
98216294Ssyrinx	size_t			txbuflen;
99216294Ssyrinx	size_t			rxbuflen;
100122394Sharti
101216294Ssyrinx	int			fd;
102122394Sharti
103216294Ssyrinx	int32_t			next_reqid;
104216294Ssyrinx	int32_t			max_reqid;
105216294Ssyrinx	int32_t			min_reqid;
106122394Sharti
107216294Ssyrinx	char			error[SNMP_STRERROR_LEN];
108216294Ssyrinx
109216294Ssyrinx	snmp_timeout_start_f	timeout_start;
110216294Ssyrinx	snmp_timeout_stop_f	timeout_stop;
111216294Ssyrinx
112216294Ssyrinx	char			local_path[sizeof(SNMP_LOCAL_PATH)];
113122394Sharti};
114122394Sharti
115122394Sharti/* the global context */
116122394Shartiextern struct snmp_client snmp_client;
117122394Sharti
118122394Sharti/* initizialies a snmp_client structure */
119122394Shartivoid snmp_client_init(struct snmp_client *);
120122394Sharti
121122394Sharti/* initialize fields */
122122394Shartiint snmp_client_set_host(struct snmp_client *, const char *);
123122394Shartiint snmp_client_set_port(struct snmp_client *, const char *);
124122394Sharti
125122394Sharti/* open connection to snmp server (hostname or portname can be NULL) */
126122394Shartiint snmp_open(const char *_hostname, const char *_portname,
127122394Sharti    const char *_read_community, const char *_write_community);
128122394Sharti
129122394Sharti/* close connection */
130122394Shartivoid snmp_close(void);
131122394Sharti
132122394Sharti/* initialize a snmp_pdu structure */
133122394Shartivoid snmp_pdu_create(struct snmp_pdu *, u_int _op);
134122394Sharti
135122394Sharti/* add pairs of (struct asn_oid *, enum snmp_syntax) to an existing pdu */
136122394Shartiint snmp_add_binding(struct snmp_pdu *, ...);
137122394Sharti
138122394Sharti/* check wheater the answer is valid or not */
139122394Shartiint snmp_pdu_check(const struct snmp_pdu *_req, const struct snmp_pdu *_resp);
140122394Sharti
141122394Shartiint32_t snmp_pdu_send(struct snmp_pdu *_pdu, snmp_send_cb_f _func, void *_arg);
142122394Sharti
143122394Sharti/*  append an index to an oid */
144122394Shartiint snmp_oid_append(struct asn_oid *_oid, const char *_fmt, ...);
145122394Sharti
146122394Sharti/* receive a packet */
147122394Shartiint snmp_receive(int _blocking);
148122394Sharti
149122394Sharti/*
150122394Sharti * This structure is used to describe an SNMP table that is to be fetched.
151122394Sharti * The C-structure that is produced by the fetch function must start with
152122394Sharti * a TAILQ_ENTRY and an u_int64_t.
153122394Sharti */
154122394Shartistruct snmp_table {
155122394Sharti	/* base OID of the table */
156122394Sharti	struct asn_oid		table;
157122394Sharti	/* type OID of the LastChange variable for the table if any */
158122394Sharti	struct asn_oid		last_change;
159122394Sharti	/* maximum number of iterations if table has changed */
160122394Sharti	u_int			max_iter;
161122394Sharti	/* size of the C-structure */
162122394Sharti	size_t			entry_size;
163122394Sharti	/* number of index fields */
164122394Sharti	u_int			index_size;
165122394Sharti	/* bit mask of required fields */
166133211Sharti	uint64_t		req_mask;
167122394Sharti
168122394Sharti	/* indexes and columns to fetch. Ended by a NULL syntax entry */
169122394Sharti	struct snmp_table_entry {
170122394Sharti	    /* the column sub-oid, ignored for index fields */
171122394Sharti	    asn_subid_t		subid;
172122394Sharti	    /* the syntax of the column or index */
173122394Sharti	    enum snmp_syntax	syntax;
174122394Sharti	    /* offset of the field into the C-structure. For octet strings
175122394Sharti	     * this points to an u_char * followed by a size_t */
176122394Sharti	    off_t		offset;
177122394Sharti#if defined(__GNUC__) && __GNUC__ < 3
178122394Sharti	}			entries[0];
179122394Sharti#else
180122394Sharti	}			entries[];
181122394Sharti#endif
182122394Sharti};
183122394Sharti
184122394Sharti/* callback type for table fetch */
185122394Shartitypedef void (*snmp_table_cb_f)(void *_list, void *_arg, int _res);
186122394Sharti
187122394Sharti/* fetch a table. The argument points to a TAILQ_HEAD */
188122394Shartiint snmp_table_fetch(const struct snmp_table *descr, void *);
189122394Shartiint snmp_table_fetch_async(const struct snmp_table *, void *,
190122394Sharti    snmp_table_cb_f, void *);
191122394Sharti
192122394Sharti/* send a request and wait for the response */
193122394Shartiint snmp_dialog(struct snmp_pdu *_req, struct snmp_pdu *_resp);
194122394Sharti
195216294Ssyrinx/* discover an authorative snmpEngineId */
196216294Ssyrinxint snmp_discover_engine(char *);
197216294Ssyrinx
198146525Sharti/* parse a server specification */
199146525Shartiint snmp_parse_server(struct snmp_client *, const char *);
200146525Sharti
201122394Sharti#endif /* _BSNMP_SNMPCLIENT_H */
202