1f06ca4afSHartmut Brandt.\" 2165c5d31SHartmut Brandt.\" Copyright (c) 2004-2005 3165c5d31SHartmut Brandt.\" Hartmut Brandt. 4165c5d31SHartmut Brandt.\" All rights reserved. 5f06ca4afSHartmut Brandt.\" Copyright (c) 2001-2003 6f06ca4afSHartmut Brandt.\" Fraunhofer Institute for Open Communication Systems (FhG Fokus). 7f06ca4afSHartmut Brandt.\" All rights reserved. 8f06ca4afSHartmut Brandt.\" 9*4f13bbb6SUlrich Spörlein.\" Author: Harti Brandt <harti@FreeBSD.org> 10f06ca4afSHartmut Brandt.\" 11896052c1SHartmut Brandt.\" Redistribution and use in source and binary forms, with or without 12896052c1SHartmut Brandt.\" modification, are permitted provided that the following conditions 13896052c1SHartmut Brandt.\" are met: 14896052c1SHartmut Brandt.\" 1. Redistributions of source code must retain the above copyright 15896052c1SHartmut Brandt.\" notice, this list of conditions and the following disclaimer. 16f06ca4afSHartmut Brandt.\" 2. Redistributions in binary form must reproduce the above copyright 17f06ca4afSHartmut Brandt.\" notice, this list of conditions and the following disclaimer in the 18f06ca4afSHartmut Brandt.\" documentation and/or other materials provided with the distribution. 19f06ca4afSHartmut Brandt.\" 20896052c1SHartmut Brandt.\" THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21896052c1SHartmut Brandt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22896052c1SHartmut Brandt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23896052c1SHartmut Brandt.\" ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 24896052c1SHartmut Brandt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25896052c1SHartmut Brandt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26896052c1SHartmut Brandt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27896052c1SHartmut Brandt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28896052c1SHartmut Brandt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29896052c1SHartmut Brandt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30896052c1SHartmut Brandt.\" SUCH DAMAGE. 31f06ca4afSHartmut Brandt.\" 32165c5d31SHartmut Brandt.\" $Begemot: bsnmp/lib/bsnmpagent.3,v 1.10 2005/10/04 08:46:49 brandt_h Exp $ 33f06ca4afSHartmut Brandt.\" 34165c5d31SHartmut Brandt.Dd October 4, 2005 3594caccb3SHartmut Brandt.Dt BSNMPAGENT 3 36f06ca4afSHartmut Brandt.Os 37f06ca4afSHartmut Brandt.Sh NAME 3894caccb3SHartmut Brandt.Nm bsnmpagent , 39f06ca4afSHartmut Brandt.Nm snmp_depop_t , 40f06ca4afSHartmut Brandt.Nm snmp_op_t , 41f06ca4afSHartmut Brandt.Nm tree , 42f06ca4afSHartmut Brandt.Nm tree_size , 43f06ca4afSHartmut Brandt.Nm snmp_trace , 44f06ca4afSHartmut Brandt.Nm snmp_debug , 45f06ca4afSHartmut Brandt.Nm snmp_get , 46f06ca4afSHartmut Brandt.Nm snmp_getnext , 47f06ca4afSHartmut Brandt.Nm snmp_getbulk , 48f06ca4afSHartmut Brandt.Nm snmp_set , 49f06ca4afSHartmut Brandt.Nm snmp_make_errresp , 50f06ca4afSHartmut Brandt.Nm snmp_dep_lookup , 51f06ca4afSHartmut Brandt.Nm snmp_init_context , 52f06ca4afSHartmut Brandt.Nm snmp_dep_commit , 538eecd77aSHartmut Brandt.Nm snmp_dep_rollback , 548eecd77aSHartmut Brandt.Nm snmp_dep_finish 55f06ca4afSHartmut Brandt.Nd "SNMP agent library" 56f06ca4afSHartmut Brandt.Sh LIBRARY 57f06ca4afSHartmut BrandtBegemot SNMP library 58f06ca4afSHartmut Brandt.Pq libbsnmp, -lbsnmp 59f06ca4afSHartmut Brandt.Sh SYNOPSIS 60f06ca4afSHartmut Brandt.In asn1.h 61f06ca4afSHartmut Brandt.In snmp.h 62f06ca4afSHartmut Brandt.In snmpagent.h 63f06ca4afSHartmut Brandt.Ft typedef int 64f06ca4afSHartmut Brandt.Fn (*snmp_depop_t) "struct snmp_context *ctx" "struct snmp_dependency *dep" "enum snmp_depop op" 65f06ca4afSHartmut Brandt.Ft typedef int 66f06ca4afSHartmut Brandt.Fn (*snmp_op_t) "struct snmp_context *ctx" "struct snmp_value *val" "u_int len" "u_int idx" "enum snmp_op op" 67f06ca4afSHartmut Brandt.Vt extern struct snmp_node *tree ; 68f06ca4afSHartmut Brandt.Vt extern u_int tree_size ; 69f06ca4afSHartmut Brandt.Vt extern u_int snmp_trace ; 70f06ca4afSHartmut Brandt.Vt extern void (*snmp_debug)(const char *fmt, ...) ; 71f06ca4afSHartmut Brandt.Ft enum snmp_ret 72f06ca4afSHartmut Brandt.Fn snmp_get "struct snmp_pdu *pdu" "struct asn_buf *resp_b" "struct snmp_pdu *resp" "void *data" 73f06ca4afSHartmut Brandt.Ft enum snmp_ret 74f06ca4afSHartmut Brandt.Fn snmp_getnext "struct snmp_pdu *pdu" "struct asn_buf *resp_b" "struct snmp_pdu *resp" "void *data" 75f06ca4afSHartmut Brandt.Ft enum snmp_ret 76f06ca4afSHartmut Brandt.Fn snmp_getbulk "struct snmp_pdu *pdu" "struct asn_buf *resp_b" "struct snmp_pdu *resp" "void *data" 77f06ca4afSHartmut Brandt.Ft enum snmp_ret 78f06ca4afSHartmut Brandt.Fn snmp_set "struct snmp_pdu *pdu" "struct asn_buf *resp_b" "struct snmp_pdu *resp" "void *data" 79f06ca4afSHartmut Brandt.Ft enum snmp_ret 80f06ca4afSHartmut Brandt.Fn snmp_make_errresp "const struct snmp_pdu *pdu" "struct asn_buf *req_b" "struct asn_buf *resp_b" 81f06ca4afSHartmut Brandt.Ft struct snmp_dependency * 82f06ca4afSHartmut Brandt.Fn snmp_dep_lookup "struct snmp_context *ctx" "const struct asn_oid *base" "const struct asn_oid *idx" "size_t alloc" "snmp_depop_t func" 83f06ca4afSHartmut Brandt.Ft struct snmp_context * 84f06ca4afSHartmut Brandt.Fn snmp_init_context "void" 85f06ca4afSHartmut Brandt.Ft int 86f06ca4afSHartmut Brandt.Fn snmp_dep_commit "struct snmp_context *ctx" 87f06ca4afSHartmut Brandt.Ft int 88f06ca4afSHartmut Brandt.Fn snmp_dep_rollback "struct snmp_context *ctx" 898eecd77aSHartmut Brandt.Ft void 908eecd77aSHartmut Brandt.Fn snmp_dep_finish "struct snmp_context *ctx" 91f06ca4afSHartmut Brandt.Sh DESCRIPTION 92f06ca4afSHartmut BrandtThe SNMP library contains routines to easily build SNMP agent applications 93165c5d31SHartmut Brandtthat use SNMP versions 1 or 2. 94165c5d31SHartmut BrandtNote, however, that it may be even easier to build an 95165c5d31SHartmut Brandt.Xr bsnmpd 1 96f06ca4afSHartmut Brandtloadable module, that handles the new MIB (see 97f06ca4afSHartmut Brandt.Xr snmpmod 3 ) . 98f06ca4afSHartmut Brandt.Pp 99f06ca4afSHartmut BrandtMost of the agent routines operate on a global array that the describes the 100165c5d31SHartmut Brandtcomplete MIB served by the agent. 101165c5d31SHartmut BrandtThis array is held in the two variables: 102f06ca4afSHartmut Brandt.Bd -literal -offset indent 103f06ca4afSHartmut Brandtextern struct snmp_node *tree; 104f06ca4afSHartmut Brandtextern u_int tree_size; 105f06ca4afSHartmut Brandt.Ed 106f06ca4afSHartmut Brandt.Pp 107f06ca4afSHartmut BrandtThe elements of the array are of type 108f06ca4afSHartmut Brandt.Vt struct snmp_node : 109f06ca4afSHartmut Brandt.Bd -literal -offset indent 110f06ca4afSHartmut Brandttypedef int (*snmp_op_t)(struct snmp_context *, struct snmp_value *, 111f06ca4afSHartmut Brandt u_int, u_int, enum snmp_op); 112f06ca4afSHartmut Brandt 113f06ca4afSHartmut Brandtstruct snmp_node { 114f06ca4afSHartmut Brandt struct asn_oid oid; 115f06ca4afSHartmut Brandt const char *name; /* name of the leaf */ 116f06ca4afSHartmut Brandt enum snmp_node_type type; /* type of this node */ 117f06ca4afSHartmut Brandt enum snmp_syntax syntax; 118f06ca4afSHartmut Brandt snmp_op_t op; 119f06ca4afSHartmut Brandt u_int flags; 120f06ca4afSHartmut Brandt u_int32_t index; /* index data */ 121f06ca4afSHartmut Brandt void *data; /* application data */ 1228eecd77aSHartmut Brandt void *tree_data; /* application data */ 123f06ca4afSHartmut Brandt}; 124f06ca4afSHartmut Brandt.Ed 125f06ca4afSHartmut Brandt.Pp 126f06ca4afSHartmut BrandtThe fields of this structure are described below. 127f06ca4afSHartmut Brandt.Bl -tag -width "syntax" 128f06ca4afSHartmut Brandt.It Va oid 129f06ca4afSHartmut BrandtBase OID of the scalar or table column. 130f06ca4afSHartmut Brandt.It Va name 131f06ca4afSHartmut BrandtName of this variable. 132f06ca4afSHartmut Brandt.It Va type 133165c5d31SHartmut BrandtType of this variable. 134165c5d31SHartmut BrandtOne of: 135f06ca4afSHartmut Brandt.Bd -literal -offset indent 136f06ca4afSHartmut Brandtenum snmp_node_type { 137f06ca4afSHartmut Brandt SNMP_NODE_LEAF = 1, 138f06ca4afSHartmut Brandt SNMP_NODE_COLUMN 139f06ca4afSHartmut Brandt}; 140f06ca4afSHartmut Brandt.Ed 141f06ca4afSHartmut Brandt.It Va syntax 142f06ca4afSHartmut BrandtThe SNMP syntax of this variable. 143f06ca4afSHartmut Brandt.It Va op 144165c5d31SHartmut BrandtThe user supplied handler for this variable. 145165c5d31SHartmut BrandtThe handler is called with the following arguments: 146f06ca4afSHartmut Brandt.Bl -tag -width "ctx" 147f06ca4afSHartmut Brandt.It Fa ctx 148f06ca4afSHartmut BrandtA pointer to the context (see below). 149f06ca4afSHartmut Brandt.Li NULL . 150f06ca4afSHartmut Brandt.It Fa val 151165c5d31SHartmut BrandtThe value to be set or retrieved. 152165c5d31SHartmut BrandtFor GETNEXT and GETBULK operations the oid in 153165c5d31SHartmut Brandtthis value is the current OID. 154165c5d31SHartmut BrandtThe function (called in this case only for 155f06ca4afSHartmut Brandttable rows) must find the lexically next existing OID within the same column and 156165c5d31SHartmut Brandtset the oid and value subfields accordingly. 157165c5d31SHartmut BrandtIf the table column is exhausted the 158f06ca4afSHartmut Brandtfunction must return 159f06ca4afSHartmut Brandt.Li SNMP_ERR_NOSUCHNAME . 160f06ca4afSHartmut BrandtFor all other operations the oid in 161f06ca4afSHartmut Brandt.Fa val 162f06ca4afSHartmut Brandtis the oid to fetch or set. 163f06ca4afSHartmut Brandt.It Fa len 164f06ca4afSHartmut BrandtThe length of the base oid without index. 165f06ca4afSHartmut Brandt.It Fa idx 166f06ca4afSHartmut BrandtFor table columns this is the index expression from the node (see below). 167f06ca4afSHartmut Brandt.It Fa op 168f06ca4afSHartmut BrandtThis is the operation to execute, one of: 169f06ca4afSHartmut Brandt.Bd -literal -offset indent 170f06ca4afSHartmut Brandtenum snmp_op { 171f06ca4afSHartmut Brandt SNMP_OP_GET = 1, 172f06ca4afSHartmut Brandt SNMP_OP_GETNEXT, 173f06ca4afSHartmut Brandt SNMP_OP_SET, 174f06ca4afSHartmut Brandt SNMP_OP_COMMIT, 175f06ca4afSHartmut Brandt SNMP_OP_ROLLBACK, 176f06ca4afSHartmut Brandt}; 177f06ca4afSHartmut Brandt.Ed 178f06ca4afSHartmut Brandt.El 179f06ca4afSHartmut Brandt.Pp 180165c5d31SHartmut BrandtThe user handler must return an appropriate SNMP v2 error code. 181165c5d31SHartmut BrandtIf the original 182f06ca4afSHartmut BrandtPDU was a version 1 PDU, the error code is mapped automatically. 183f06ca4afSHartmut Brandt.It Va flags 184f06ca4afSHartmut BrandtCurrently only the flag 185f06ca4afSHartmut Brandt.Li SNMP_NODE_CANSET is defined and set for nodes, that can be written or 186f06ca4afSHartmut Brandtcreated. 187f06ca4afSHartmut Brandt.It Va index 188165c5d31SHartmut BrandtThis word describes the index for table columns. 189165c5d31SHartmut BrandtEach part of the index takes 4 bits starting at bit 4. 190165c5d31SHartmut BrandtBits 0 to 3 hold the number of index parts. 191165c5d31SHartmut BrandtThis arrangement allows for tables with up to seven indexes. 192165c5d31SHartmut BrandtEach bit group contains the syntax for the index part. 193165c5d31SHartmut BrandtThere are a number of macros to help in parsing this field: 194f06ca4afSHartmut Brandt.Bd -literal -offset indent 195f06ca4afSHartmut Brandt#define SNMP_INDEXES_MAX 7 196f06ca4afSHartmut Brandt#define SNMP_INDEX_SHIFT 4 197f06ca4afSHartmut Brandt#define SNMP_INDEX_MASK 0xf 198f06ca4afSHartmut Brandt#define SNMP_INDEX_COUNT(V) ((V) & SNMP_INDEX_MASK) 199f06ca4afSHartmut Brandt#define SNMP_INDEX(V,I) \e 200f06ca4afSHartmut Brandt (((V) >> (((I) + 1) * SNMP_INDEX_SHIFT)) & \e 201f06ca4afSHartmut Brandt SNMP_INDEX_MASK) 202f06ca4afSHartmut Brandt.Ed 203f06ca4afSHartmut Brandt.It Va data 204f06ca4afSHartmut BrandtThis field may contain arbitrary data and is not used by the library. 205f06ca4afSHartmut Brandt.El 206f06ca4afSHartmut Brandt.Pp 207f06ca4afSHartmut BrandtThe easiest way to construct the node table is 208f06ca4afSHartmut Brandt.Xr gensnmptree 1 . 209f06ca4afSHartmut BrandtNote, that one must be careful when changing the tree while executing a SET 210165c5d31SHartmut Brandtoperation. 211165c5d31SHartmut BrandtConsult the sources for 212165c5d31SHartmut Brandt.Xr bsnmpd 1 . 213f06ca4afSHartmut Brandt.Pp 214f06ca4afSHartmut BrandtThe global variable 215f06ca4afSHartmut Brandt.Va snmp_trace 216f06ca4afSHartmut Brandttogether with the function pointed to by 217f06ca4afSHartmut Brandt.Va snmp_debug 218f06ca4afSHartmut Brandthelp in debugging the library and the agent. 219f06ca4afSHartmut Brandt.Va snmp_trace is a bit mask with the following bits: 220f06ca4afSHartmut Brandt.Bd -literal -offset indent 221f06ca4afSHartmut Brandtenum { 222f06ca4afSHartmut Brandt SNMP_TRACE_GET, 223f06ca4afSHartmut Brandt SNMP_TRACE_GETNEXT, 224f06ca4afSHartmut Brandt SNMP_TRACE_SET, 225f06ca4afSHartmut Brandt SNMP_TRACE_DEPEND, 226f06ca4afSHartmut Brandt SNMP_TRACE_FIND, 227f06ca4afSHartmut Brandt}; 228f06ca4afSHartmut Brandt.Ed 229f06ca4afSHartmut Brandt.Pp 230f06ca4afSHartmut BrandtSetting a bit to true causes the library to call 231f06ca4afSHartmut Brandt.Fn snmp_debug 232165c5d31SHartmut Brandtin strategic places with a debug string. 233165c5d31SHartmut BrandtThe library contains a default 234f06ca4afSHartmut Brandtimplementation for the debug function that prints a message to standard error. 235f06ca4afSHartmut Brandt.Pp 236f06ca4afSHartmut BrandtMany of the functions use a so called context: 237f06ca4afSHartmut Brandt.Bd -literal -offset indent 238f06ca4afSHartmut Brandtstruct snmp_context { 239f06ca4afSHartmut Brandt u_int var_index; 240f06ca4afSHartmut Brandt struct snmp_scratch *scratch; 241f06ca4afSHartmut Brandt struct snmp_dependency *dep; 242f06ca4afSHartmut Brandt void *data; /* user data */ 2438eecd77aSHartmut Brandt enum snmp_ret code; /* return code */ 244f06ca4afSHartmut Brandt}; 245f06ca4afSHartmut Brandt 246f06ca4afSHartmut Brandtstruct snmp_scratch { 247f06ca4afSHartmut Brandt void *ptr1; 248f06ca4afSHartmut Brandt void *ptr2; 249165c5d31SHartmut Brandt uint32_t int1; 250165c5d31SHartmut Brandt uint32_t int2; 251f06ca4afSHartmut Brandt}; 252f06ca4afSHartmut Brandt.Ed 253f06ca4afSHartmut Brandt.Pp 254f06ca4afSHartmut BrandtThe fields are used as follows: 255f06ca4afSHartmut Brandt.Bl -tag -width ".It Va var_index" 256f06ca4afSHartmut Brandt.It Va va_index 257f06ca4afSHartmut BrandtFor the node operation callback this is the 258165c5d31SHartmut Brandtindex of the variable binding that should be returned if an error occurs. 259165c5d31SHartmut BrandtSet by the library. 260165c5d31SHartmut BrandtIn all other functions this is undefined. 261f06ca4afSHartmut Brandt.It Va scratch 262f06ca4afSHartmut BrandtFor the node operation callback this is a pointer to a per variable binding 263165c5d31SHartmut Brandtscratch area that can be used to implement the commit and rollback. 264165c5d31SHartmut BrandtSet by the library. 265f06ca4afSHartmut Brandt.It Va dep 266f06ca4afSHartmut BrandtIn the dependency callback function (see below) this is a pointer to the 267165c5d31SHartmut Brandtcurrent dependency. 268165c5d31SHartmut BrandtSet by the library. 269f06ca4afSHartmut Brandt.It Va data 270f06ca4afSHartmut BrandtThis is the 271f06ca4afSHartmut Brandt.Fa data 272f06ca4afSHartmut Brandtargument from the call to the library and is not used by the library. 273f06ca4afSHartmut Brandt.El 274f06ca4afSHartmut Brandt.Pp 275f06ca4afSHartmut BrandtThe next three functions execute different kinds of GET requests. 276f06ca4afSHartmut BrandtThe function 277f06ca4afSHartmut Brandt.Fn snmp_get 278f06ca4afSHartmut Brandtexecutes an SNMP GET operation, the function 279f06ca4afSHartmut Brandt.Fn snmp_getnext 280f06ca4afSHartmut Brandtexecutes an SNMP GETNEXT operation and the function 281f06ca4afSHartmut Brandt.Fn snmp_getbulk 282f06ca4afSHartmut Brandtexecutes an SNMP GETBULK operation. 283f06ca4afSHartmut BrandtFor all three functions the response PDU is constructed and encoded 284165c5d31SHartmut Brandton the fly. 285165c5d31SHartmut BrandtIf everything is ok, the response PDU is returned in 286f06ca4afSHartmut Brandt.Fa resp 287f06ca4afSHartmut Brandtand 288f06ca4afSHartmut Brandt.Fa resp_b . 289f06ca4afSHartmut BrandtThe caller must call 290f06ca4afSHartmut Brandt.Fn snmp_pdu_free 291165c5d31SHartmut Brandtto free the response PDU in this case. 292165c5d31SHartmut BrandtOne of the following values may be returned: 293f06ca4afSHartmut Brandt.Bl -tag -width ".It Li SNMP_RET_ERR" 294f06ca4afSHartmut Brandt.It Li SNMP_RET_OK 295f06ca4afSHartmut BrandtOperation successful, response PDU may be sent. 296f06ca4afSHartmut Brandt.It Li SNMP_RET_IGN 297165c5d31SHartmut BrandtOperation failed, no response PDU constructed. 298165c5d31SHartmut BrandtRequest is ignored. 299f06ca4afSHartmut Brandt.It Li SNMP_RET_ERR 300165c5d31SHartmut BrandtError in operation. 301165c5d31SHartmut BrandtThe error code and index have been set in 302f06ca4afSHartmut Brandt.Fa pdu . 303f06ca4afSHartmut BrandtNo response PDU has been constructed. 304f06ca4afSHartmut BrandtThe caller may construct an error response PDU via 305f06ca4afSHartmut Brandt.Fn snmp_make_errresp . 306f06ca4afSHartmut Brandt.El 307f06ca4afSHartmut Brandt.Pp 308f06ca4afSHartmut BrandtThe function 309f06ca4afSHartmut Brandt.Fn snmp_set 310165c5d31SHartmut Brandtexecutes an SNMP SET operation. 311165c5d31SHartmut BrandtThe arguments are the same as for the previous 312165c5d31SHartmut Brandtthree functions. 313165c5d31SHartmut BrandtThe operation of this functions is, however, much more complex. 314f06ca4afSHartmut Brandt.Pp 315165c5d31SHartmut BrandtThe SET operation occurs in several stages: 316f06ca4afSHartmut Brandt.Bl -enum -offset indent 317f06ca4afSHartmut Brandt.It 318f06ca4afSHartmut BrandtFor each binding search the corresponding nodes, check that the 319165c5d31SHartmut Brandtvariable is writeable and the syntax is ok. 320165c5d31SHartmut BrandtThe writeable check can be done only for scalars. 321165c5d31SHartmut BrandtFor columns it must be done in the node's operation callback function. 322f06ca4afSHartmut Brandt.It 323f06ca4afSHartmut BrandtFor each binding call the node's operation callback with function SNMP_OP_SET. 324165c5d31SHartmut BrandtThe callback may create dependencies or finalizers (see below). 325165c5d31SHartmut BrandtFor simple 326f06ca4afSHartmut Brandtscalars the scratch area may be enough to handle commit and rollback, for 327165c5d31SHartmut Brandtinterdependent table columns dependencies may be necessary. 328f06ca4afSHartmut Brandt.It 329f06ca4afSHartmut BrandtIf the previous step fails at any point, the node's operation callback 330f06ca4afSHartmut Brandtfunctions are called for all bindings for which SNMP_OP_SET was executed 331165c5d31SHartmut Brandtwith SNMP_OP_ROLLBACK, in the opposite order. 332165c5d31SHartmut BrandtThis allows all variables to undo the effect of the SET operation. 333165c5d31SHartmut BrandtAfter this all the dependencies are freed 334165c5d31SHartmut Brandtand the finalizers are executed with a fail flag of 1. 335165c5d31SHartmut BrandtThen the function 336f06ca4afSHartmut Brandtreturns to the caller with an appropriate error indication. 337f06ca4afSHartmut Brandt.It 338f06ca4afSHartmut BrandtIf the SET step was successful for all bindings, the dependency callbacks 339f06ca4afSHartmut Brandtare executed in the order in which the dependencies were created with an 340165c5d31SHartmut Brandtoperation of SNMP_DEPOP_COMMIT. 341165c5d31SHartmut BrandtIf any of the dependencies fails, all the 342f06ca4afSHartmut Brandtcommitted dependencies are called again in the opposite order 343165c5d31SHartmut Brandtwith SNMP_DEPOP_ROLLBACK. 344165c5d31SHartmut BrandtThan for all bindings from the last to the first 345f06ca4afSHartmut Brandtthe node's operation callback is called with SNMP_OP_ROLLBACK to undo 346165c5d31SHartmut Brandtthe effect of SNMP_OP_SET. 347165c5d31SHartmut BrandtAt the end the dependencies are freed and the finalizers are called with 348165c5d31SHartmut Brandta fail flag of 1 and the function returns to the caller with an appropriate 349165c5d31SHartmut Brandterror indication. 350f06ca4afSHartmut Brandt.It 351f06ca4afSHartmut BrandtIf the dependency commits were successful, for each binding the node's 352165c5d31SHartmut Brandtoperation callback is called with SNMP_OP_COMMIT. 353165c5d31SHartmut BrandtAny error returned from 354f06ca4afSHartmut Brandtthe callbacks is ignored (an error message is generated via 355f06ca4afSHartmut Brandt.Fn snmp_error ). 356f06ca4afSHartmut Brandt.It 357f06ca4afSHartmut BrandtNow the dependencies are freed and the finalizers are called 358165c5d31SHartmut Brandtwith a fail flag of 0. 359165c5d31SHartmut BrandtFor each dependency just before freeing it 3608eecd77aSHartmut Brandtits callback is called with 3618eecd77aSHartmut Brandt.Li SNMP_DEPOP_FINISH. 3628eecd77aSHartmut BrandtThen the function returns 363f06ca4afSHartmut Brandt.Li SNMP_ERR_OK . 364f06ca4afSHartmut Brandt.El 365f06ca4afSHartmut Brandt.Pp 366f06ca4afSHartmut BrandtThere are to mechanisms to help in complex SET operations: dependencies and 367165c5d31SHartmut Brandtfinalizers. 368165c5d31SHartmut BrandtA dependency is used if several bindings depend on each other. 369f06ca4afSHartmut BrandtA typical example is the creation of a conceptual row, which requires 370165c5d31SHartmut Brandtthe setting of several columns to succeed. 371165c5d31SHartmut BrandtA dependency is identified by 372165c5d31SHartmut Brandttwo OIDs. 373165c5d31SHartmut BrandtIn the table case, the first oid is typically the table's base OID 374165c5d31SHartmut Brandtand the second one the index. 375165c5d31SHartmut BrandtBoth of these can easily be generated from the 376f06ca4afSHartmut Brandtvariables OID with 377f06ca4afSHartmut Brandt.Fn asn_slice_oid . 378f06ca4afSHartmut BrandtThe function 379f06ca4afSHartmut Brandt.Fn snmp_dep_lookup 380f06ca4afSHartmut Brandttries to find a dependency based on these two OIDs and, if it cannot find one 381165c5d31SHartmut Brandtcreates a new one. 382165c5d31SHartmut BrandtThis means for the table example, that the function 383f06ca4afSHartmut Brandtreturns the same dependency for each of the columns of the same table row. 384f06ca4afSHartmut BrandtThis allows during the SNMP_OP_SET processing to collect all information 385165c5d31SHartmut Brandtabout the row into the dependency. 386165c5d31SHartmut BrandtThe arguments to 387f06ca4afSHartmut Brandt.Fn snmp_dep_lookup 388f06ca4afSHartmut Brandtare: the two OIDs to identify the dependency (they are copied into newly 389f06ca4afSHartmut Brandtcreated dependencies), the size of the structure to allocate and 390f06ca4afSHartmut Brandtthe dependency callback. 391f06ca4afSHartmut Brandt.Pp 392f06ca4afSHartmut BrandtWhen all SNMP_OP_SET operations have succeeded the dependencies are executed. 393f06ca4afSHartmut BrandtAt this stage the dependency callback has all information about the given 394f06ca4afSHartmut Brandttable row that was available in this SET PDU and can operate accordingly. 395f06ca4afSHartmut Brandt.Pp 3968eecd77aSHartmut BrandtIt is guaranteed that each dependency callback is executed at minimum once 3978eecd77aSHartmut Brandt- with an operation of 3988eecd77aSHartmut Brandt.Li SNMP_OP_ROLLBACK . 3998eecd77aSHartmut BrandtThis ensures that all dynamically allocated resources in a callback can be 4008eecd77aSHartmut Brandtfreed correctly. 401f06ca4afSHartmut Brandt.Pp 402f06ca4afSHartmut BrandtThe function 403f06ca4afSHartmut Brandt.Fn snmp_make_errresp 404165c5d31SHartmut Brandtmakes an error response if an operation has failed. 405165c5d31SHartmut BrandtIt takes the original request PDU (it will look only on the error code and 406165c5d31SHartmut Brandtindex fields), the buffer containing the original PDU and a buffer for the 407165c5d31SHartmut Brandterror PDU. 408165c5d31SHartmut BrandtIt copies the bindings field from the original PDUs buffer directly to 409165c5d31SHartmut Brandtthe response PDU and thus does not depend on the decodability of this field. 410165c5d31SHartmut BrandtIt may return the same values as the operation functions. 411f06ca4afSHartmut Brandt.Pp 4128eecd77aSHartmut BrandtThe next four functions allow some parts of the SET operation to be executed. 413f06ca4afSHartmut BrandtThis is only used in 414165c5d31SHartmut Brandt.Xr bsnmpd 1 415f06ca4afSHartmut Brandtto implement the configuration as a single transaction. 416f06ca4afSHartmut BrandtThe function 417f06ca4afSHartmut Brandt.Fn snmp_init_context 418f06ca4afSHartmut Brandtcreates and initializes a context. 419f06ca4afSHartmut BrandtThe function 420f06ca4afSHartmut Brandt.Fn snmp_dep_commit 421f06ca4afSHartmut Brandtexecutes SNMP_DEPOP_COMMIT for all dependencies in the context stopping at 422f06ca4afSHartmut Brandtthe first error. 423f06ca4afSHartmut BrandtThe function 424f06ca4afSHartmut Brandt.Fn snmp_dep_rollback 425f06ca4afSHartmut Brandtexecutes SNMP_DEPOP_ROLLBACK starting at the previous of the current 426f06ca4afSHartmut Brandtdependency in the context. 4278eecd77aSHartmut BrandtThe function 4288eecd77aSHartmut Brandt.Fn snmp_dep_finish 4298eecd77aSHartmut Brandtexecutes SNMP_DEPOP_FINISH for all dependencies. 430f06ca4afSHartmut Brandt.Sh DIAGNOSTICS 431165c5d31SHartmut BrandtIf an error occurs in any of the function an error indication as described 432165c5d31SHartmut Brandtabove is returned. 433165c5d31SHartmut BrandtAdditionally the functions may call snmp_error on unexpected errors. 434f06ca4afSHartmut Brandt.Sh SEE ALSO 435f06ca4afSHartmut Brandt.Xr gensnmptree 1 , 436165c5d31SHartmut Brandt.Xr bsnmpd 1 , 437f06ca4afSHartmut Brandt.Xr bsnmpclient 3 , 43894caccb3SHartmut Brandt.Xr bsnmplib 3 , 439f06ca4afSHartmut Brandt.Xr snmpmod 3 440f06ca4afSHartmut Brandt.Sh STANDARDS 441f06ca4afSHartmut BrandtThis implementation conforms to the applicable IETF RFCs and ITU-T 442f06ca4afSHartmut Brandtrecommendations. 443f06ca4afSHartmut Brandt.Sh AUTHORS 444*4f13bbb6SUlrich Spörlein.An Hartmut Brandt Aq harti@FreeBSD.org 445