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 * Kendy Kutzner 8 * 9 * Redistribution of this software and documentation and use in source and 10 * binary forms, with or without modification, are permitted provided that 11 * the following conditions are met: 12 * 13 * 1. Redistributions of source code or documentation must retain the above 14 * copyright notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the Institute nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS 23 * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 25 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 26 * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 29 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $Begemot: bsnmp/lib/snmpclient.h,v 1.17 2003/12/08 17:11:58 hbb Exp $ 35 */ 36 #ifndef _BSNMP_SNMPCLIENT_H 37 #define _BSNMP_SNMPCLIENT_H 38 39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <sys/time.h> 42 #include <netinet/in.h> 43 #include <stddef.h> 44 45 46 #define SNMP_STRERROR_LEN 200 47 48 #define SNMP_LOCAL_PATH "/tmp/snmpXXXXXXXXXXXXXX" 49 50 /* 51 * transport methods 52 */ 53 #define SNMP_TRANS_UDP 0 54 #define SNMP_TRANS_LOC_DGRAM 1 55 #define SNMP_TRANS_LOC_STREAM 2 56 57 /* type of callback function for responses 58 * this callback function is responsible for free() any memory associated with 59 * any of the PDUs. Therefor it may call snmp_pdu_free() */ 60 typedef void (*snmp_send_cb_f)(struct snmp_pdu *, struct snmp_pdu *, void *); 61 62 /* type of callback function for timeouts */ 63 typedef void (*snmp_timeout_cb_f)(void * ); 64 65 /* timeout start function */ 66 typedef void *(*snmp_timeout_start_f)(struct timeval *timeout, 67 snmp_timeout_cb_f callback, void *); 68 69 /* timeout stop function */ 70 typedef void (*snmp_timeout_stop_f)(void *timeout_id); 71 72 /* 73 * Client context. 74 */ 75 struct snmp_client { 76 enum snmp_version version; 77 int trans; /* which transport to use */ 78 79 /* these two are read-only for the application */ 80 char *cport; /* port number as string */ 81 char *chost; /* host name or IP address as string */ 82 83 char read_community[SNMP_COMMUNITY_MAXLEN + 1]; 84 char write_community[SNMP_COMMUNITY_MAXLEN + 1]; 85 86 struct timeval timeout; 87 u_int retries; 88 89 int dump_pdus; 90 91 size_t txbuflen; 92 size_t rxbuflen; 93 94 int fd; 95 96 int32_t next_reqid; 97 int32_t max_reqid; 98 int32_t min_reqid; 99 100 char error[SNMP_STRERROR_LEN]; 101 102 snmp_timeout_start_f timeout_start; 103 snmp_timeout_stop_f timeout_stop; 104 105 char local_path[sizeof(SNMP_LOCAL_PATH)]; 106 }; 107 108 /* the global context */ 109 extern struct snmp_client snmp_client; 110 111 /* initizialies a snmp_client structure */ 112 void snmp_client_init(struct snmp_client *); 113 114 /* initialize fields */ 115 int snmp_client_set_host(struct snmp_client *, const char *); 116 int snmp_client_set_port(struct snmp_client *, const char *); 117 118 /* open connection to snmp server (hostname or portname can be NULL) */ 119 int snmp_open(const char *_hostname, const char *_portname, 120 const char *_read_community, const char *_write_community); 121 122 /* close connection */ 123 void snmp_close(void); 124 125 /* initialize a snmp_pdu structure */ 126 void snmp_pdu_create(struct snmp_pdu *, u_int _op); 127 128 /* add pairs of (struct asn_oid *, enum snmp_syntax) to an existing pdu */ 129 int snmp_add_binding(struct snmp_pdu *, ...); 130 131 /* check wheater the answer is valid or not */ 132 int snmp_pdu_check(const struct snmp_pdu *_req, const struct snmp_pdu *_resp); 133 134 int32_t snmp_pdu_send(struct snmp_pdu *_pdu, snmp_send_cb_f _func, void *_arg); 135 136 /* append an index to an oid */ 137 int snmp_oid_append(struct asn_oid *_oid, const char *_fmt, ...); 138 139 /* receive a packet */ 140 int snmp_receive(int _blocking); 141 142 /* 143 * This structure is used to describe an SNMP table that is to be fetched. 144 * The C-structure that is produced by the fetch function must start with 145 * a TAILQ_ENTRY and an u_int64_t. 146 */ 147 struct snmp_table { 148 /* base OID of the table */ 149 struct asn_oid table; 150 /* type OID of the LastChange variable for the table if any */ 151 struct asn_oid last_change; 152 /* maximum number of iterations if table has changed */ 153 u_int max_iter; 154 /* size of the C-structure */ 155 size_t entry_size; 156 /* number of index fields */ 157 u_int index_size; 158 /* bit mask of required fields */ 159 u_int64_t req_mask; 160 161 /* indexes and columns to fetch. Ended by a NULL syntax entry */ 162 struct snmp_table_entry { 163 /* the column sub-oid, ignored for index fields */ 164 asn_subid_t subid; 165 /* the syntax of the column or index */ 166 enum snmp_syntax syntax; 167 /* offset of the field into the C-structure. For octet strings 168 * this points to an u_char * followed by a size_t */ 169 off_t offset; 170 #if defined(__GNUC__) && __GNUC__ < 3 171 } entries[0]; 172 #else 173 } entries[]; 174 #endif 175 }; 176 177 /* callback type for table fetch */ 178 typedef void (*snmp_table_cb_f)(void *_list, void *_arg, int _res); 179 180 /* fetch a table. The argument points to a TAILQ_HEAD */ 181 int snmp_table_fetch(const struct snmp_table *descr, void *); 182 int snmp_table_fetch_async(const struct snmp_table *, void *, 183 snmp_table_cb_f, void *); 184 185 /* send a request and wait for the response */ 186 int snmp_dialog(struct snmp_pdu *_req, struct snmp_pdu *_resp); 187 188 #endif /* _BSNMP_SNMPCLIENT_H */ 189