NETSNMP\_SESSION\_API(3) Net-SNMP NETSNMP\_SESSION\_API(3)

# [NAME](#NAME)

snmp\_sess\_init, snmp\_open, snmp\_send, snmp\_async\_send, snmp\_select\_info, snmp\_read, snmp\_timeout, snmp\_synch\_response, snmp\_close, snmp\_perror, snmp\_sess\_perror, snmp\_error, snmp\_api\_errstring - netsnmp\_session\_api functions

# [SYNOPSIS](#SYNOPSIS)

**#include &lt;net-snmp/session\_api.h&gt;**

**void snmp\_sess\_init ( netsnmp\_session\*sess);**

**netsnmp\_session* snmp\_open ( netsnmp\_session \*sess);**

*/* Input parameter not used in active sessions \*/*

**int snmp\_send ( netsnmp\_session \*session,**

**netsnmp\_pdu \*pdu);**

**int snmp\_async\_send ( netsnmp\_session \*session,**

**netsnmp\_pdu \*pdu,**  
**snmp\_callback callback,**  
**void \*callbackData);**

**int snmp\_select\_info ( int * numfds,fd\_set \*fdset,**

**struct timeval \*timeout,int \*block);**

**void snmp\_read ( fd\_set \*fdset);**

**void snmp\_timeout ( void );**

**int snmp\_synch\_response ( netsnmp\_session \*session,**

**netsnmp\_pdu \*pdu,**  
**netsnmp\_pdu \*\*response);**

**int snmp\_close ( netsnmp\_session \*session);**  
**int snmp\_close\_sessions ( void );**

## [Error Reporting](#Error_Reporting)

*(Move to [output\_api(3)](/man/os/net-snmp/output_api.3))*  
**void snmp\_error ( netsnmp\_session \*session,**

**int \*pcliberr,int \*psnmperr,char \*\*pperrstring);**

**char \*snmp\_api\_errstring ( intsnmperr);**

**void snmp\_perror ( char \*msg);**

*/* for parsing errors only \*/*

**void snmp\_sess\_perror (char \*msg,netsnmp\_session \*sess);**

*/* all other SNMP library errors \*/*

# [DESCRIPTION](#DESCRIPTION)

*Snmp\_sess\_init* prepares a netsnmp\_session that sources transport characteristics and common information that will be used for a set of SNMP transactions. After this structure is passed to *snmp\_open* to create an SNMP session, the structure is not used.

*Snmp\_open* returns a pointer to a newly-formed *netsnmp\_session* object, which the application must use to reference the active SNMP session.

*snmp\_send* and *snmp\_async\_send()* each take as input a pointer to a *netsnmp\_pdu* object. This structure contains information that describes a transaction that will be performed over an open session.

Consult snmp\_api.h for the definitions of these structures.

With the *snmp\_async\_send()* call, *snmp\_read* will invoke the specified callback when the response is received.

*Snmp\_read, snmp\_select\_info,* and *snmp\_timeout* provide an interface for the use of the [*select(2)*](/man/os/net-snmp/select.2) system call so that SNMP transactions can occur asynchronously.

*Snmp\_select\_info* is given the information that would have been passed to *select* in the absence of SNMP. For example, this might include window update information. This information is modified so that SNMP will get the service it requires from the call to *select.* In this case, *numfds, fdset,* and *timeout* correspond to the *nfds, readfds,* and *timeout* arguments to *select,* respectively. The only exception is that timeout must always point to an allocated (but perhaps uninitialized) *struct timeval.* If *timeout* would have been passed as NULL, *block* is set to true, and *timeout* is treated as undefined. This same rule applies upon return from *snmp\_select\_info.*

After calling *snmp\_select\_info, select* is called with the returned data. When select returns, *snmp\_read* should be called with the *fd\_set* returned from *select* to read each SNMP socket that has input. If *select* times out, *snmp\_timeout* should be called to see if the timeout was intended for SNMP.

*snmp\_synch\_response* is a convenience routine that will send the request, wait for the response and process it before returning. See the descriptions of *snmp\_send , snmp\_read* etc for details.

# [DIAGNOSTICS](#DIAGNOSTICS)

Previous versions of the library used *snmp\_get\_errno* to read the global variable *snmp\_errno* which may have held the error status within the SNMP library. The existing method *snmp\_perror* should be used to log ASN.1 coding errors only.

The new method *snmp\_sess\_perror* is provided to capture errors that occur during the processing of a particular SNMP session. *Snmp\_sess\_perror* calls *snmp\_error* function to obtain the "C" library error *errno* , the SNMP library error *snmperr* , and the SNMP library detailed error message that is associated with an error that occurred during a given session.

Note that in all cases except one, *snmp\_sess\_perror* should be handed the *netsnmp\_session \** pointer returned from *snmp\_open.* If *snmp\_open* returns a null pointer, pass the INPUT *netsnmp\_session \** pointer used to call *snmp\_open.*

Error return status from *snmp\_close* and *snmp\_send* is indicated by return of 0. A successful status will return a 1 for *snmp\_close* and the request id of the packet for *snmp\_send.* Upon successful return from *snmp\_send* the pdu will be freed by the library.

Consult snmp\_api.h for the complete set of SNMP library error values. The SNMP library error value *snmperr* can be one of the following values:

[SNMPERR\_GENERR](#SNMPERR_GENERR)

A generic error occurred.

[SNMPERR\_BAD\_LOCPORT](#SNMPERR_BAD_LOCPORT)

The local port was bad because it had already been allocated or permission was denied.

[SNMPERR\_BAD\_ADDRESS](#SNMPERR_BAD_ADDRESS)

The host name or address given was not useable.

[SNMPERR\_BAD\_SESSION](#SNMPERR_BAD_SESSION)

The specified session was not open.

[SNMPERR\_TOO\_LONG](#SNMPERR_TOO_LONG)

[SNMPERR\_NO\_SOCKET](#SNMPERR_NO_SOCKET)

[SNMPERR\_V2\_IN\_V1](#SNMPERR_V2_IN_V1)

[SNMPERR\_V1\_IN\_V2](#SNMPERR_V1_IN_V2)

[SNMPERR\_BAD\_REPEATERS](#SNMPERR_BAD_REPEATERS)

[SNMPERR\_BAD\_REPETITIONS](#SNMPERR_BAD_REPETITIONS)

[SNMPERR\_BAD\_ASN1\_BUILD](#SNMPERR_BAD_ASN1_BUILD)

[SNMPERR\_BAD\_SENDTO](#SNMPERR_BAD_SENDTO)

[SNMPERR\_BAD\_PARSE](#SNMPERR_BAD_PARSE)

[SNMPERR\_BAD\_VERSION](#SNMPERR_BAD_VERSION)

[SNMPERR\_NOAUTH\_DESPRIV](#SNMPERR_NOAUTH_DESPRIV)

[SNMPERR\_ABORT](#SNMPERR_ABORT)

[SNMPERR\_UNKNOWN\_PDU](#SNMPERR_UNKNOWN_PDU)

[SNMPERR\_TIMEOUT](#SNMPERR_TIMEOUT)

A string representation of the error code can be obtained with *snmp\_api\_errstring*, and a standard error message may be printed using *snmp\_perror* that functions like the *perror* standard routine.

# [SEE ALSO](#SEE_ALSO)

[select(2)](/man/os/net-snmp/select.2), [netsnmp\_sess\_api(3)](/man/os/net-snmp/netsnmp_sess_api.3), [netsnmp\_mib\_api(3)](/man/os/net-snmp/netsnmp_mib_api.3), [netsnmp\_pdu\_api(3)](/man/os/net-snmp/netsnmp_pdu_api.3), [netsnmp\_varbind\_api(3)](/man/os/net-snmp/netsnmp_varbind_api.3), snmp\_api.h

19 May 2011 V5.9.5.2

---

Document: netsnmp_session_api(3) from net-snmp 5.9.5.2-r10 (Chainguard OS)
Source: https://github.com/net-snmp/net-snmp.git
