---
name: "bsnmpagent"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/bsnmpagent"
---

# bsnmpagent(3)

SNMP agent library

## NAME

**bsnmpagent**, **snmp\_depop\_t**, **snmp\_op\_t**, **tree**, **tree\_size**, **snmp\_trace**, **snmp\_debug**, **snmp\_get**, **snmp\_getnext**, **snmp\_getbulk**, **snmp\_set**, **snmp\_make\_errresp**, **snmp\_dep\_lookup**, **snmp\_init\_context**, **snmp\_dep\_commit**, **snmp\_dep\_rollback**, **snmp\_dep\_finish** – SNMP agent library

## LIBRARY

Begemot SNMP library (libbsnmp, -lbsnmp)

## SYNOPSIS

**#include \<asn1.h\>**

**#include \<snmp.h\>**

**#include \<snmpagent.h\>**

*typedef* *int*\

**(\*snmp\_depop\_t)**(*struct snmp\_context \*ctx*, *struct snmp\_dependency \*dep*, *enum snmp\_depop op*);

*typedef* *int*\

**(\*snmp\_op\_t)**(*struct snmp\_context \*ctx*, *struct snmp\_value \*val*, *u\_int len*, *u\_int idx*, *enum snmp\_op op*); *extern* *struct* *snmp\_node* *\*tree*; *extern* *u\_int* *tree\_size*; *extern* *u\_int* *snmp\_trace*; *extern* *void* *(\*snmp\_debug)(const* *char* *\*fmt,* *...)*;

*enum* *snmp\_ret*\

**snmp\_get**(*struct snmp\_pdu \*pdu*, *struct asn\_buf \*resp\_b*, *struct snmp\_pdu \*resp*, *void \*data*);

*enum* *snmp\_ret*\

**snmp\_getnext**(*struct snmp\_pdu \*pdu*, *struct asn\_buf \*resp\_b*, *struct snmp\_pdu \*resp*, *void \*data*);

*enum* *snmp\_ret*\

**snmp\_getbulk**(*struct snmp\_pdu \*pdu*, *struct asn\_buf \*resp\_b*, *struct snmp\_pdu \*resp*, *void \*data*);

*enum* *snmp\_ret*\

**snmp\_set**(*struct snmp\_pdu \*pdu*, *struct asn\_buf \*resp\_b*, *struct snmp\_pdu \*resp*, *void \*data*);

*enum* *snmp\_ret*\

**snmp\_make\_errresp**(*const struct snmp\_pdu \*pdu*, *struct asn\_buf \*req\_b*, *struct asn\_buf \*resp\_b*);

*struct* *snmp\_dependency* *\**\

**snmp\_dep\_lookup**(*struct snmp\_context \*ctx*, *const struct asn\_oid \*base*, *const struct asn\_oid \*idx*, *size\_t alloc*, *snmp\_depop\_t func*);

*struct* *snmp\_context* *\**\

**snmp\_init\_context**(*void*);

*int*\

**snmp\_dep\_commit**(*struct snmp\_context \*ctx*);

*int*\

**snmp\_dep\_rollback**(*struct snmp\_context \*ctx*);

*void*\

**snmp\_dep\_finish**(*struct snmp\_context \*ctx*);

## DESCRIPTION

The SNMP library contains routines to easily build SNMP agent applications that use SNMP versions 1 or 2. Note, however, that it may be even easier to build an [bsnmpd(1)](/bsnmpd/1.md) loadable module, that handles the new MIB (see [snmpmod(3)](/snmpmod/3.md)).

Most of the agent routines operate on a global array that the describes the complete MIB served by the agent. This array is held in the two variables:

```
extern struct snmp_node *tree;
extern u_int  tree_size;
```

The elements of the array are of type *struct* *snmp\_node*:

```
typedef int (*snmp_op_t)(struct snmp_context *, struct snmp_value *,
    u_int, u_int, enum snmp_op);
struct snmp_node {
	struct asn_oid oid;
	const char	*name;		/* name of the leaf */
	enum snmp_node_type type;	/* type of this node */
	enum snmp_syntax syntax;
	snmp_op_t	op;
	u_int		flags;
	u_int32_t	index;		/* index data */
	void		*data;		/* application data */
	void		*tree_data;	/* application data */
};
```

The fields of this structure are described below.

- *oid*

  Base OID of the scalar or table column.

- *name*

  Name of this variable.

- *type*

  Type of this variable. One of:

  ```
  enum snmp_node_type {
  	SNMP_NODE_LEAF = 1,
  	SNMP_NODE_COLUMN
  };
  ```

- *syntax*

  The SNMP syntax of this variable.

- *op*

  The user supplied handler for this variable. The handler is called with the following arguments:

  - *ctx*

    A pointer to the context (see below). NULL.

  - *val*

    The value to be set or retrieved. For GETNEXT and GETBULK operations the oid in this value is the current OID. The function (called in this case only for table rows) must find the lexically next existing OID within the same column and set the oid and value subfields accordingly. If the table column is exhausted the function must return SNMP\_ERR\_NOSUCHNAME. For all other operations the oid in *val* is the oid to fetch or set.

  - *len*

    The length of the base oid without index.

  - *idx*

    For table columns this is the index expression from the node (see below).

  - *op*

    This is the operation to execute, one of:

    ```
    enum snmp_op {
    	SNMP_OP_GET 	= 1,
    	SNMP_OP_GETNEXT,
    	SNMP_OP_SET,
    	SNMP_OP_COMMIT,
    	SNMP_OP_ROLLBACK,
    };
    ```

    The user handler must return an appropriate SNMP v2 error code. If the original PDU was a version 1 PDU, the error code is mapped automatically.

- *flags*

  Currently only the flag SNMP\_NODE\_CANSET is defined and set for nodes, that can be written or created.

- *index*

  This word describes the index for table columns. Each part of the index takes 4 bits starting at bit 4. Bits 0 to 3 hold the number of index parts. This arrangement allows for tables with up to seven indexes. Each bit group contains the syntax for the index part. There are a number of macros to help in parsing this field:

  ```
  #define SNMP_INDEXES_MAX	7
  #define SNMP_INDEX_SHIFT	4
  #define SNMP_INDEX_MASK	0xf
  #define SNMP_INDEX_COUNT(V)	((V) & SNMP_INDEX_MASK)
  #define SNMP_INDEX(V,I) \
  	(((V) >> (((I) + 1) * SNMP_INDEX_SHIFT)) & \
  	SNMP_INDEX_MASK)
  ```

- *data*

  This field may contain arbitrary data and is not used by the library.

The easiest way to construct the node table is [gensnmptree(1)](/gensnmptree/1.md). Note, that one must be careful when changing the tree while executing a SET operation. Consult the sources for [bsnmpd(1)](/bsnmpd/1.md).

The global variable *snmp\_trace* together with the function pointed to by *snmp\_debug* help in debugging the library and the agent. *snmp\_trace* *is* *a* *bit* *mask* *with* *the* *following* *bits:*

```
enum {
	SNMP_TRACE_GET,
	SNMP_TRACE_GETNEXT,
	SNMP_TRACE_SET,
	SNMP_TRACE_DEPEND,
	SNMP_TRACE_FIND,
};
```

Setting a bit to true causes the library to call **snmp\_debug**() in strategic places with a debug string. The library contains a default implementation for the debug function that prints a message to standard error.

Many of the functions use a so called context:

```
struct snmp_context {
	u_int	var_index;
	struct snmp_scratch *scratch;
	struct snmp_dependency *dep;
	void	*data;		/* user data */
	enum snmp_ret code;	/* return code */
};
struct snmp_scratch {
	void		*ptr1;
	void		*ptr2;
	uint32_t	int1;
	uint32_t	int2;
};
```

The fields are used as follows:

- *va\_index*

  For the node operation callback this is the index of the variable binding that should be returned if an error occurs. Set by the library. In all other functions this is undefined.

- *scratch*

  For the node operation callback this is a pointer to a per variable binding scratch area that can be used to implement the commit and rollback. Set by the library.

- *dep*

  In the dependency callback function (see below) this is a pointer to the current dependency. Set by the library.

- *data*

  This is the *data* argument from the call to the library and is not used by the library.

The next three functions execute different kinds of GET requests. The function **snmp\_get**() executes an SNMP GET operation, the function **snmp\_getnext**() executes an SNMP GETNEXT operation and the function **snmp\_getbulk**() executes an SNMP GETBULK operation. For all three functions the response PDU is constructed and encoded on the fly. If everything is ok, the response PDU is returned in *resp* and *resp\_b*. The caller must call **snmp\_pdu\_free**() to free the response PDU in this case. One of the following values may be returned:

- SNMP\_RET\_OK

  Operation successful, response PDU may be sent.

- SNMP\_RET\_IGN

  Operation failed, no response PDU constructed. Request is ignored.

- SNMP\_RET\_ERR

  Error in operation. The error code and index have been set in *pdu*. No response PDU has been constructed. The caller may construct an error response PDU via **snmp\_make\_errresp**().

The function **snmp\_set**() executes an SNMP SET operation. The arguments are the same as for the previous three functions. The operation of this functions is, however, much more complex.

The SET operation occurs in several stages:

1. For each binding search the corresponding nodes, check that the variable is writeable and the syntax is ok. The writeable check can be done only for scalars. For columns it must be done in the node's operation callback function.

2. For each binding call the node's operation callback with function SNMP\_OP\_SET. The callback may create dependencies or finalizers (see below). For simple scalars the scratch area may be enough to handle commit and rollback, for interdependent table columns dependencies may be necessary.

3. If the previous step fails at any point, the node's operation callback functions are called for all bindings for which SNMP\_OP\_SET was executed with SNMP\_OP\_ROLLBACK, in the opposite order. This allows all variables to undo the effect of the SET operation. After this all the dependencies are freed and the finalizers are executed with a fail flag of 1. Then the function returns to the caller with an appropriate error indication.

4. If the SET step was successful for all bindings, the dependency callbacks are executed in the order in which the dependencies were created with an operation of SNMP\_DEPOP\_COMMIT. If any of the dependencies fails, all the committed dependencies are called again in the opposite order with SNMP\_DEPOP\_ROLLBACK. Than for all bindings from the last to the first the node's operation callback is called with SNMP\_OP\_ROLLBACK to undo the effect of SNMP\_OP\_SET. At the end the dependencies are freed and the finalizers are called with a fail flag of 1 and the function returns to the caller with an appropriate error indication.

5. If the dependency commits were successful, for each binding the node's operation callback is called with SNMP\_OP\_COMMIT. Any error returned from the callbacks is ignored (an error message is generated via **snmp\_error**(*).*)

6. Now the dependencies are freed and the finalizers are called with a fail flag of 0. For each dependency just before freeing it its callback is called with SNMP\_DEPOP\_FINISH. Then the function returns SNMP\_ERR\_OK.

There are to mechanisms to help in complex SET operations: dependencies and finalizers. A dependency is used if several bindings depend on each other. A typical example is the creation of a conceptual row, which requires the setting of several columns to succeed. A dependency is identified by two OIDs. In the table case, the first oid is typically the table's base OID and the second one the index. Both of these can easily be generated from the variables OID with **asn\_slice\_oid**(). The function **snmp\_dep\_lookup**() tries to find a dependency based on these two OIDs and, if it cannot find one creates a new one. This means for the table example, that the function returns the same dependency for each of the columns of the same table row. This allows during the SNMP\_OP\_SET processing to collect all information about the row into the dependency. The arguments to **snmp\_dep\_lookup**() are: the two OIDs to identify the dependency (they are copied into newly created dependencies), the size of the structure to allocate and the dependency callback.

When all SNMP\_OP\_SET operations have succeeded the dependencies are executed. At this stage the dependency callback has all information about the given table row that was available in this SET PDU and can operate accordingly.

It is guaranteed that each dependency callback is executed at minimum once - with an operation of SNMP\_OP\_ROLLBACK. This ensures that all dynamically allocated resources in a callback can be freed correctly.

The function **snmp\_make\_errresp**() makes an error response if an operation has failed. It takes the original request PDU (it will look only on the error code and index fields), the buffer containing the original PDU and a buffer for the error PDU. It copies the bindings field from the original PDUs buffer directly to the response PDU and thus does not depend on the decodability of this field. It may return the same values as the operation functions.

The next four functions allow some parts of the SET operation to be executed. This is only used in [bsnmpd(1)](/bsnmpd/1.md) to implement the configuration as a single transaction. The function **snmp\_init\_context**() creates and initializes a context. The function **snmp\_dep\_commit**() executes SNMP\_DEPOP\_COMMIT for all dependencies in the context stopping at the first error. The function **snmp\_dep\_rollback**() executes SNMP\_DEPOP\_ROLLBACK starting at the previous of the current dependency in the context. The function **snmp\_dep\_finish**() executes SNMP\_DEPOP\_FINISH for all dependencies.

## DIAGNOSTICS

If an error occurs in any of the function an error indication as described above is returned. Additionally the functions may call snmp\_error on unexpected errors.

## SEE ALSO

[gensnmptree(1)](/gensnmptree/1.md), [bsnmpd(1)](/bsnmpd/1.md), [bsnmpclient(3)](/bsnmpclient/3.md), [bsnmplib(3)](/bsnmplib/3.md), [snmpmod(3)](/snmpmod/3.md)

## STANDARDS

This implementation conforms to the applicable IETF RFCs and ITU-T recommendations.

## AUTHORS

Hartmut Brandt ⟨harti@FreeBSD.org⟩
