NETSNMP_SESS_API(3)
π NETSNMP_SESS_API β session functions π
okay bestie, gather round, because this is net-snmp's session_api page and it's literally the blueprint for running SNMP sessions when you've got threads everywhere. yes, the (3) means library functions. yes, you're gonna slay. π¦
β¨ NAME β¨
snmp_sess_init, snmp_sess_open, snmp_sess_session, snmp_sess_send, snmp_sess_async_send, snmp_sess_select_info, snmp_sess_read, snmp_sess_timeout, snmp_sess_synch_response, snmp_sess_close, snmp_sess_error β session functions π
πΈ SYNOPSIS πΈ
#include <net-snmp/session_api.h>
void snmp_sess_init(struct snmp_session *session);
void *snmp_sess_open(struct snmp_session *session);
struct snmp_session *snmp_sess_session(void *handle);
int snmp_sess_send(void *handle, struct snmp_pdu *pdu);
int snmp_sess_async_send(void *handle,
struct snmp_pdu *pdu,
snmp_callback callback,
void *callbackData);
int snmp_sess_select_info(void *handle,
int *numfds, fd_set *fdset,
struct timeval *timeout,
int *block);
int snmp_sess_read(void *handle, fd_set *fdset);
void snmp_sess_timeout(void *handle);
int snmp_sess_synch_response ( void *handle,
netsnmp_pdu *pdu,
netsnmp_pdu **response);
int snmp_sess_close(void *handle);
void snmp_sess_error(void *handle, int *pcliberr,
int *psnmperr, char **pperrstring);
π DESCRIPTION π
So here's the tea β: these functions are a whole subset of the API, and they're the ones you reach for when you're managing a single SNMP session inside a multi-threaded application. And lowkey the cutest part? Except for snmp_sess_session(), every single one of these is just a single-session version of the classic SNMP library API. same energy, smaller package. π
And bestie, notice the vibe shift: these functions don't hand you a session pointer like the traditional API does. nope. they use an opaque pointer β that's the handle you keep seeing in the prototypes up there β to identify one single session. it's giving mystery, it's giving encapsulation, we love her for it. π
snmp_sess_init() sets up a struct snmp_session that sources transport characteristics and common info, all the stuff that's gonna get used for a set of SNMP transactions. and then β listen β once you pass that structure to snmp_sess_open() to create an SNMP session, the structure is no longer used. periodt. from then on it's the opaque pointer that snmp_sess_open() gave you back that refers to that session. that's the main character now. π
β οΈ real talk, this one matters: SNMP sessions created withsnmp_sess_open()are not affected by, and SHOULD NOT BE USED WITH,snmp_select_info(),snmp_read(),snmp_timeout()norsnmp_close(). instead you use the equivalent single session functions described right here on this very page. don't mix them, bestie, i'm begging. π
snmp_sess_init() and snmp_sess_open() each take as input a pointer to a struct snmp_session object. that structure holds info for a set of transactions that are gonna share similar transport characteristics. and snmp_sess_session()? she takes the opaque session handle and returns a pointer to its associated struct snmp_session. like a little glow-up reveal. β¨
snmp_sess_send() and snmp_sess_async_send() each take a pdu parameter, which points to a struct snmp_pdu object containing info 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_sess_async_send() call, snmp_sess_read will invoke the specified callback when the response is received. (callback girlies, this one's for you π«Ά)
snmp_sess_select_info(), snmp_sess_read() and snmp_sess_timeout() provide an interface for the use of the select(2) system call so that SNMP transactions for a single session can occur asynchronously. asynchronous? we don't wait around. π
snmp_sess_select_info() gets passed the information that would have been passed to select(2) in the absence of SNMP. for example, this might include file descriptors associated with the main loop of a graphical application. this information is modified so that SNMP will get the service it requires from the call to select(2). in this case, numfds, fdset and timeout correspond to the nfds, readfds and timeout arguments to select(2) respectively. π
π The one exception (and it's a big one):timeoutmust ALWAYS point to an allocated (but perhaps uninitialized)struct timeval. it cannot beNULLas forselect(2). iftimeoutwould have been passed asNULL,blockis instead set to true, andtimeoutis treated as undefined. this same rule applies upon return fromsnmp_select_info().
After calling snmp_sess_select_info() , select(2) should be called with the returned data. when it returns, snmp_sess_read() should then be called with the fd_set returned from select(2). this will read any input from this session's SNMP socket. and if select(2) times out (that is, it returns zero), snmp_sess_timeout() should be called to see if a timeout has occurred on the SNMP session. π°οΈ
snmp_sess_synch_response is a convenience routine that will send the request, wait for the response and process it before returning. it's the "do it all for me" bestie of the group. See the descriptions of snmp_sess_send , snmp_sess_read etc for details.
π DIAGNOSTICS π
Error return status from snmp_sess_open() is indicated by return of a NULL pointer. Error return status from snmp_sess_close() and snmp_sess_send() is indicated by a return value of 0. A successful status will return 1. π
Further information can be obtained by using snmp_sess_error() to see what type of error has occurred. This function returns the SNMP snmp_errno variable, the value of the system errno variable, and a string interpretation of both variables.
π The string must be freed after use by the caller. no leaks, bestie, we are not that girl.
For errors returned by snmp_sess_open(), use the corresponding function snmp_error() instead of snmp_sess_error(). (different function, don't get it twisted π
)
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β A generic error occurred.SNMPERR_BAD_LOCPORTβ The local port was bad because it had already been allocated or permission was denied.SNMPERR_BAD_ADDRESSβ The host name or address given was not useable.SNMPERR_BAD_SESSIONβ The specified session was not open.SNMPERR_TOO_LONGSNMPERR_NO_SOCKETSNMPERR_V2_IN_V1SNMPERR_V1_IN_V2SNMPERR_BAD_REPEATERSSNMPERR_BAD_REPETITIONSSNMPERR_BAD_ASN1_BUILDSNMPERR_BAD_SENDTOSNMPERR_BAD_RCVFROMSNMPERR_BAD_PARSESNMPERR_BAD_VERSIONSNMPERR_BAD_COMMUNITYSNMPERR_NOAUTH_DESPRIVSNMPERR_ABORTSNMPERR_UNKNOWN_PDUSNMPERR_TIMEOUT
π¦ SEE ALSO π¦
select(2), netsnmp_session_api(3), netsnmp_pdu_api(3), netsnmp_varbind_api(3), netsnmp_mib_api(3), snmp_api.h
go read them, they're all iconic in their own way. iykyk πβ¨
and that's the whole page, bestie!! this one's from Net-SNMP, version V5.9.5.2, dated 19 May 2011 β an absolute vintage baddie that still runs the group chat. mwah. πππ«Ά
π¦ where this page comes from
- alpine 3.23: net-snmp-doc 5.9.5.2-r0 Β· Net-SNMP
- alpine 3.24: net-snmp-doc 5.9.5.2-r0 Β· Net-SNMP
- arch rolling: net-snmp 5.9.5.2-2 Β· BSD
- fedora 44: net-snmp-devel 1:5.9.5.2-4.fc44 Β· MIT-CMU AND BSD-3-Clause AND MIT
- fedora 44: net-snmp-devel 1:5.9.5.2-4.fc44 Β· MIT-CMU AND BSD-3-Clause AND MIT
- nixos 26.05: net-snmp 5.9.5.2
- nixpkgs unstable: net-snmp 5.9.5.2
- ubuntu stonking: libsnmp-dev 5.9.5.2+dfsg-2.1ubuntu5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms