---
name: "netsnmp_handler"
section: "3snmp"
platform: "linux"
package: "libsnmp-dev"
version: "5.4.3~dfsg-2.8+deb7u1"
release: "Debian 7 (Wheezy)"
platforms: ["linux"]
url: "https://new.carta.tech/netsnmp_handler"
---

# netsnmp\_handler(3snmp)

The basic theory goes something like this: In the past, with the original mib module api (which derived from the original CMU SNMP code) the underlying mib modules were passed very little information (only the truly most basic information about a request).

## NAME

netsnmp\_handler -

The basic theory goes something like this: In the past, with the original mib **module** api (which derived from the original CMU SNMP code) the underlying mib modules were passed very little information (only the truly most basic information about a request).

### Data Structures

struct **netsnmp\_mib\_handler\_s**\
 *the mib handler structure to be registered*  struct **netsnmp\_handler\_registration\_s**\
 *Root registration info.*  struct **netsnmp\_handler\_args\_s**\
 struct **netsnmp\_delegated\_cache\_s**\

### Modules

**utility\_handlers**\

*Simplify request processing A group of handlers intended to simplify certain aspects of processing a request for a MIB object.*  **leaf\_handlers**\

*Process individual leaf objects A group of handlers to implement individual leaf objects and instances (both scalar objects, and individual objects and instances within a table).*  **baby\_steps**\

*Calls your handler in baby\_steps for set processing.*  **old\_api**\

*Calls mib* **module** *code written in the old style of code.*  **stash\_cache**\

*Automatically caches data for certain handlers.*  **table**\

*Helps you implement a table.*

### Defines

\#define **MIB\_HANDLER\_AUTO\_NEXT**   0x00000001\
 \#define **MIB\_HANDLER\_AUTO\_NEXT\_OVERRIDE\_ONCE**   0x00000002\
 \#define **MIB\_HANDLER\_INSTANCE**   0x00000004\
 \#define **MIB\_HANDLER\_CUSTOM4**   0x10000000\
 \#define **MIB\_HANDLER\_CUSTOM3**   0x20000000\
 \#define **MIB\_HANDLER\_CUSTOM2**   0x40000000\
 \#define **MIB\_HANDLER\_CUSTOM1**   0x80000000\
 \#define **HANDLER\_CAN\_GETANDGETNEXT**   0x01\
 \#define **HANDLER\_CAN\_SET**   0x02\
 \#define **HANDLER\_CAN\_GETBULK**   0x04\
 \#define **HANDLER\_CAN\_NOT\_CREATE**   0x08\
 \#define **HANDLER\_CAN\_BABY\_STEP**   0x10\
 \#define **HANDLER\_CAN\_STASH**   0x20\
 \#define **HANDLER\_CAN\_RONLY**   (HANDLER\_CAN\_GETANDGETNEXT)\
 \#define **HANDLER\_CAN\_RWRITE**   (HANDLER\_CAN\_GETANDGETNEXT \| HANDLER\_CAN\_SET)\
 \#define **HANDLER\_CAN\_SET\_ONLY**   (HANDLER\_CAN\_SET \| HANDLER\_CAN\_NOT\_CREATE)\
 \#define **HANDLER\_CAN\_DEFAULT**   (HANDLER\_CAN\_RONLY \| HANDLER\_CAN\_NOT\_CREATE)\
 \#define **REQUEST\_IS\_DELEGATED**   1\
 \#define **REQUEST\_IS\_NOT\_DELEGATED**   0\

### Typedefs

typedef struct **netsnmp\_mib\_handler\_s** **netsnmp\_mib\_handler**\
 *Typedefs the* **netsnmp\_mib\_handler\_s** *struct into netsnmp\_mib\_handler.*  typedef struct **netsnmp\_handler\_registration\_s** **netsnmp\_handler\_registration**\
 *Typedefs the* **netsnmp\_handler\_registration\_s** *struct into netsnmp\_handler\_registration.*  typedef int( **Netsnmp\_Node\_Handler** )(**netsnmp\_mib\_handler** \*handler, **netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests)\
 typedef struct **netsnmp\_handler\_args\_s** **netsnmp\_handler\_args**\
 typedef struct **netsnmp\_delegated\_cache\_s** **netsnmp\_delegated\_cache**\

### Functions

**netsnmp\_mib\_handler** \* **netsnmp\_create\_handler** (const char \*name, Netsnmp\_Node\_Handler \*handler\_access\_method)\
 *creates a netsnmp\_mib\_handler structure given a name and a access method.*  **netsnmp\_handler\_registration** \* **netsnmp\_handler\_registration\_create** (const char \*name, **netsnmp\_mib\_handler** \*handler, oid \*reg\_oid, size\_t reg\_oid\_len, int modes)\
 *creates a handler registration structure given a name, a access\_method function, a registration location oid and the modes the handler supports.*  **netsnmp\_handler\_registration** \* **netsnmp\_create\_handler\_registration** (const char \*name, Netsnmp\_Node\_Handler \*handler\_access\_method, oid \*reg\_oid, size\_t reg\_oid\_len, int modes)\
 int **netsnmp\_register\_handler** (**netsnmp\_handler\_registration** \*reginfo)\
 *register a handler, as defined by the netsnmp\_handler\_registration pointer.*  int **netsnmp\_unregister\_handler** (**netsnmp\_handler\_registration** \*reginfo)\
 *unregister a handler, as defined by the netsnmp\_handler\_registration pointer.*  int **netsnmp\_register\_handler\_nocallback** (**netsnmp\_handler\_registration** \*reginfo)\
 *register a handler, as defined by the netsnmp\_handler\_registration pointer.*  int **netsnmp\_inject\_handler\_before** (**netsnmp\_handler\_registration** \*reginfo, **netsnmp\_mib\_handler** \*handler, const char \*before\_what)\
 *inject a new handler into the calling chain of the handlers definedy by the netsnmp\_handler\_registration pointer.*  int **netsnmp\_inject\_handler** (**netsnmp\_handler\_registration** \*reginfo, **netsnmp\_mib\_handler** \*handler)\
 *inject a new handler into the calling chain of the handlers definedy by the netsnmp\_handler\_registration pointer.*  NETSNMP\_INLINE int **netsnmp\_call\_handler** (**netsnmp\_mib\_handler** \*next\_handler, **netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests)\
 *calls a handler with with appropriate NULL checking of arguments, etc.*  int **netsnmp\_call\_handlers** (**netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests)\
 NETSNMP\_INLINE int **netsnmp\_call\_next\_handler** (**netsnmp\_mib\_handler** \*current, **netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests)\
 *calls the next handler in the chain after the current one with with appropriate NULL checking, etc.*  NETSNMP\_INLINE int **netsnmp\_call\_next\_handler\_one\_request** (**netsnmp\_mib\_handler** \*current, **netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests)\
 *calls the next handler in the chain after the current one with with appropriate NULL checking, etc.*  void **netsnmp\_handler\_free** (**netsnmp\_mib\_handler** \*handler)\
 *free's the resourceses associated with a given handler*  **netsnmp\_mib\_handler** \* **netsnmp\_handler\_dup** (**netsnmp\_mib\_handler** \*handler)\
 *dulpicates a handler and all subsequent handlers see also \_clone\_handler*  void **netsnmp\_handler\_registration\_free** (**netsnmp\_handler\_registration** \*reginfo)\
 *free the resources associated with a handler registration object*  **netsnmp\_handler\_registration** \* **netsnmp\_handler\_registration\_dup** (**netsnmp\_handler\_registration** \*reginfo)\
 *duplicates the handler registration object*  NETSNMP\_INLINE **netsnmp\_delegated\_cache** \* **netsnmp\_create\_delegated\_cache** (**netsnmp\_mib\_handler** \*handler, **netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests, void \*localinfo)\
 *creates a cache of information which can be saved for future reference.*  NETSNMP\_INLINE **netsnmp\_delegated\_cache** \* **netsnmp\_handler\_check\_cache** (**netsnmp\_delegated\_cache** \*dcache)\
 *check's a given cache and returns it if it is still valid (ie, the agent still considers it to be an outstanding request.*  NETSNMP\_INLINE void **netsnmp\_free\_delegated\_cache** (**netsnmp\_delegated\_cache** \*dcache)\
 *frees a cache once you're finished using it*  void **netsnmp\_handler\_mark\_requests\_as\_delegated** (**netsnmp\_request\_info** \*requests, int isdelegated)\
 *marks a list of requests as delegated (or not if isdelegaded = 0)*  NETSNMP\_INLINE void **netsnmp\_request\_add\_list\_data** (**netsnmp\_request\_info** \*request, **netsnmp\_data\_list** \***node**)\
 *add data to a request that can be extracted later by submodules*  NETSNMP\_INLINE int **netsnmp\_request\_remove\_list\_data** (**netsnmp\_request\_info** \*request, const char \*name)\
 *remove data from a request*  void \* **netsnmp\_request\_get\_list\_data** (**netsnmp\_request\_info** \*request, const char \*name)\
 *extract data from a request that was added previously by a parent* **module**  NETSNMP\_INLINE void **netsnmp\_free\_request\_data\_set** (**netsnmp\_request\_info** \*request)\
 *Free the extra data stored in a request.*  NETSNMP\_INLINE void **netsnmp\_free\_request\_data\_sets** (**netsnmp\_request\_info** \*request)\
 *Free the extra data stored in a bunch of requests (all data in the chain).*  **netsnmp\_mib\_handler** \* **netsnmp\_find\_handler\_by\_name** (**netsnmp\_handler\_registration** \*reginfo, const char \*name)\
 *Returns a handler from a chain based on the name.*  void \* **netsnmp\_find\_handler\_data\_by\_name** (**netsnmp\_handler\_registration** \*reginfo, const char \*name)\
 *Returns a handler's void \* pointer from a chain based on the name.*  void **handler\_free\_callback** (void \*free)\
 void **netsnmp\_register\_handler\_by\_name** (const char \*name, **netsnmp\_mib\_handler** \*handler)\
 *registers a given handler by name so that it can be found easily later.*  void **netsnmp\_clear\_handler\_list** (void)\
 *clears the entire handler-registration list*  void **netsnmp\_inject\_handler\_into\_subtree** (**netsnmp\_subtree** \*tp, const char \*name, **netsnmp\_mib\_handler** \*handler, const char \*before\_what)\
 void **parse\_injectHandler\_conf** (const char \*token, char \*cptr)\
 void **netsnmp\_init\_handler\_conf** (void)\
 void \* **netsnmp\_handler\_get\_parent\_data** (**netsnmp\_request\_info** \*, const char \*)\

## Detailed Description

The basic theory goes something like this: In the past, with the original mib **module** api (which derived from the original CMU SNMP code) the underlying mib modules were passed very little information (only the truly most basic information about a request).

This worked well at the time but in todays world of subagents, device instrumentation, low resource consumption, etc, it just isn't flexible enough. 'handlers' are here to fix all that.

With the rewrite of the agent internals for the net-snmp 5.0 release, we introduce a modular calling scheme that allows agent modules to be written in a very flexible manner, and more importantly allows reuse of code in a decent way (and without the memory and speed overheads of OO languages like C++).

Functionally, the notion of what a handler does is the same as the older api: A handler is **created** and then **registered** with the main agent at a given OID in the OID **tree** and gets called any time a request is made that it should respond to. You probably should use one of the convenience helpers instead of doing anything else yourself though:

Most importantly, though, is that the handlers are built on the notion of modularity and reuse. Specifically, rather than do all the really hard work (like parsing table indexes out of an incoming oid request) in each **module**, the API is designed to make it easy to write 'helper' handlers that merely process some aspect of the request before passing it along to the final handler that returns the real answer. Most people will want to make use of the **instance**, **table**, **table\_iterator**, **table\_data**, or **table\_dataset** helpers to make their life easier. These 'helpers' interpert important aspects of the request and pass them on to you.

For instance, the **table** helper is designed to hand you a list of extracted index values from an incoming request. THe **table\_iterator** helper is built on top of the table helper, and is designed to help you iterate through data stored elsewhere (like in a kernel) that is not in OID lexographical order (ie, don't write your own index/oid sorting routine, use this helper instead). The beauty of the **table\_iterator helper**, as well as the **instance** helper is that they take care of the complex GETNEXT processing entirely for you and hand you everything you need to merely return the data as if it was a GET request. Much less code and hair pulling. I've pulled all my hair out to help you so that only one of us has to be bald.

## Typedef Documentation

### struct netsnmp\_handler\_registration\_s netsnmp\_handler\_registration

Typedefs the **netsnmp\_handler\_registration\_s** struct into netsnmp\_handler\_registration.

### struct netsnmp\_mib\_handler\_s netsnmp\_mib\_handler

Typedefs the **netsnmp\_mib\_handler\_s** struct into netsnmp\_mib\_handler.

## Function Documentation

### int netsnmp\_call\_handler (netsnmp\_mib\_handler \* next\_handler, netsnmp\_handler\_registration \* reginfo, netsnmp\_agent\_request\_info \* reqinfo, netsnmp\_request\_info \* requests)

calls a handler with with appropriate NULL checking of arguments, etc.

### int netsnmp\_call\_next\_handler (netsnmp\_mib\_handler \* current, netsnmp\_handler\_registration \* reginfo, netsnmp\_agent\_request\_info \* reqinfo, netsnmp\_request\_info \* requests)

calls the next handler in the chain after the current one with with appropriate NULL checking, etc.

### int netsnmp\_call\_next\_handler\_one\_request (netsnmp\_mib\_handler \* current, netsnmp\_handler\_registration \* reginfo, netsnmp\_agent\_request\_info \* reqinfo, netsnmp\_request\_info \* requests)

calls the next handler in the chain after the current one with with appropriate NULL checking, etc.

### void netsnmp\_clear\_handler\_list (void)

clears the entire handler-registration list

### netsnmp\_delegated\_cache \* netsnmp\_create\_delegated\_cache (netsnmp\_mib\_handler \* handler, netsnmp\_handler\_registration \* reginfo, netsnmp\_agent\_request\_info \* reqinfo, netsnmp\_request\_info \* requests, void \* localinfo)

creates a cache of information which can be saved for future reference. Use **netsnmp\_handler\_check\_cache()** later to make sure it's still valid before referencing it in the future.

**Examples:**  **delayed\_instance.c**.

### netsnmp\_mib\_handler \* netsnmp\_create\_handler (const char \* name, Netsnmp\_Node\_Handler \* handler\_access\_method)

creates a netsnmp\_mib\_handler structure given a name and a access method. The returned handler should then be **registered.**

**Parameters:**

*name* is the handler name and is copied then assigned to netsnmp\_mib\_handler-\>handler\_name\
 *handler\_access\_method* is a function pointer used as the access method for this handler registration instance for whatever required needs.

**Returns:**

a pointer to a populated netsnmp\_mib\_handler struct to be registered

**See also:**

netsnmp\_create\_handler\_registration()

**netsnmp\_register\_handler()**

### netsnmp\_mib\_handler \* netsnmp\_find\_handler\_by\_name (netsnmp\_handler\_registration \* reginfo, const char \* name)

Returns a handler from a chain based on the name.

### void \* netsnmp\_find\_handler\_data\_by\_name (netsnmp\_handler\_registration \* reginfo, const char \* name)

Returns a handler's void \* pointer from a chain based on the name. This probably shouldn't be used by the general public as the void \* data may change as a handler evolves. Handlers should really advertise some function for you to use instead.

### void netsnmp\_free\_delegated\_cache (netsnmp\_delegated\_cache \* dcache)

frees a cache once you're finished using it

**Examples:**  **delayed\_instance.c**.

### void netsnmp\_free\_request\_data\_set (netsnmp\_request\_info \* request)

Free the extra data stored in a request.

### void netsnmp\_free\_request\_data\_sets (netsnmp\_request\_info \* request)

Free the extra data stored in a bunch of requests (all data in the chain).

### netsnmp\_delegated\_cache \* netsnmp\_handler\_check\_cache (netsnmp\_delegated\_cache \* dcache)

check's a given cache and returns it if it is still valid (ie, the agent still considers it to be an outstanding request. Returns NULL if it's no longer valid.

**Examples:**  **delayed\_instance.c**.

### netsnmp\_mib\_handler \* netsnmp\_handler\_dup (netsnmp\_mib\_handler \* handler)

dulpicates a handler and all subsequent handlers see also \_clone\_handler

### void netsnmp\_handler\_free (netsnmp\_mib\_handler \* handler)

free's the resourceses associated with a given handler

make sure we aren't pointing to ourselves.

XXX : segv here at shutdown if SHUTDOWN\_AGENT\_CLEANLY defined. About 30 functions down the stack, starting in clear\_context() -\> clear\_subtree()

### void netsnmp\_handler\_mark\_requests\_as\_delegated (netsnmp\_request\_info \* requests, int isdelegated)

marks a list of requests as delegated (or not if isdelegaded = 0)

### netsnmp\_handler\_registration \* netsnmp\_handler\_registration\_create (const char \* name, netsnmp\_mib\_handler \* handler, oid \* reg\_oid, size\_t reg\_oid\_len, int modes)

creates a handler registration structure given a name, a access\_method function, a registration location oid and the modes the handler supports. If modes == 0, then modes will automatically be set to the default value of only HANDLER\_CAN\_DEFAULT, which is by default read-only GET and GETNEXT requests. A hander which supports sets but not row creation should set us a mode of HANDLER\_CAN\_SET\_ONLY.

**Note:**

This ends up calling netsnmp\_create\_handler(name, handler\_access\_method)

**Parameters:**

*name* is the handler name and is copied then assigned to netsnmp\_handler\_registration-\>handlerName.\
 *handler* is a function pointer used as the access method for this handler registration instance for whatever required needs.\
 *reg\_oid* is the registration location oid.\
 *reg\_oid\_len* is the length of reg\_oid, can use the macro, OID\_LENGTH\
 *modes* is used to configure read/write access. If modes == 0, then modes will automatically be set to the default value of only HANDLER\_CAN\_DEFAULT, which is by default read-only GET and GETNEXT requests. The other two mode options are read only, HANDLER\_CAN\_RONLY, and read/write, HANDLER\_CAN\_RWRITE.

- •

  HANDLER\_CAN\_GETANDGETNEXT

- •

  HANDLER\_CAN\_SET

- •

  HANDLER\_CAN\_GETBULK

- •

  HANDLER\_CAN\_RONLY (HANDLER\_CAN\_GETANDGETNEXT)

- •

  HANDLER\_CAN\_RWRITE (HANDLER\_CAN\_GETANDGETNEXT \| HANDLER\_CAN\_SET)

- •

  HANDLER\_CAN\_DEFAULT HANDLER\_CAN\_RONLY

**Returns:**

Returns a pointer to a netsnmp\_handler\_registration struct. NULL is returned only when memory could not be allocated for the netsnmp\_handler\_registration struct.

**See also:**

**netsnmp\_create\_handler()**

**netsnmp\_register\_handler()**

### netsnmp\_handler\_registration \* netsnmp\_handler\_registration\_dup (netsnmp\_handler\_registration \* reginfo)

duplicates the handler registration object

### void netsnmp\_handler\_registration\_free (netsnmp\_handler\_registration \* reginfo)

free the resources associated with a handler registration object

### int netsnmp\_inject\_handler (netsnmp\_handler\_registration \* reginfo, netsnmp\_mib\_handler \* handler)

inject a new handler into the calling chain of the handlers definedy by the netsnmp\_handler\_registration pointer. The new handler is injected at the top of the list and hence will be the new handler to be called first.

### int netsnmp\_inject\_handler\_before (netsnmp\_handler\_registration \* reginfo, netsnmp\_mib\_handler \* handler, const char \* before\_what)

inject a new handler into the calling chain of the handlers definedy by the netsnmp\_handler\_registration pointer. The new handler is injected after the before\_what handler, or if NULL at the top of the list and hence will be the new handler to be called first.

### int netsnmp\_register\_handler (netsnmp\_handler\_registration \* reginfo)

register a handler, as defined by the netsnmp\_handler\_registration pointer.

### void netsnmp\_register\_handler\_by\_name (const char \* name, netsnmp\_mib\_handler \* handler)

registers a given handler by name so that it can be found easily later.

### int netsnmp\_register\_handler\_nocallback (netsnmp\_handler\_registration \* reginfo)

register a handler, as defined by the netsnmp\_handler\_registration pointer.

### void netsnmp\_request\_add\_list\_data (netsnmp\_request\_info \* request, netsnmp\_data\_list \* node)

add data to a request that can be extracted later by submodules **Parameters:**

*request* the netsnmp request info structure\
 **node this is the data to be added to the linked list request-\>parent\_data**

**Returns:**

**void**

**Examples:**  **delayed\_instance.c.**

### void \* netsnmp\_request\_get\_list\_data (netsnmp\_request\_info \* request, const char \* name)

**extract data from a request that was added previously by a parent module Parameters:**

*request* **the netsnmp request info function**\
 *name* **used to compare against the request-\>parent\_data-\>name value, if a match is found request-\>parent\_data-\>data is returned**

**Returns:**

**a void pointer(request-\>parent\_data-\>data), otherwise NULL is returned if request is NULL or request-\>parent\_data is NULL or request-\>parent\_data object is not found.**

**Examples:**  **delayed\_instance.c.**

### int netsnmp\_request\_remove\_list\_data (netsnmp\_request\_info \* request, const char \* name)

**remove data from a request Parameters:**

*request* **the netsnmp request info structure**\
 *name* **this is the name of the previously added data**

**Returns:**

**0 on successful find-and-delete, 1 otherwise.**

### int netsnmp\_unregister\_handler (netsnmp\_handler\_registration \* reginfo)

**unregister a handler, as defined by the netsnmp\_handler\_registration pointer.**

## Author

**Generated automatically by Doxygen for net-snmp from the source code.**
