SNMPMOD(3)
okay bestie, welcome to SNMPMOD(3) from the Library Functions Manual β this is the bsnmpd loadable module interface and it's genuinely giving main character energy. β¨
πΈ NAME πΈ
okay so this page is the whole group chat for:
INSERT_OBJECT_OID_LINK_INDEX, INSERT_OBJECT_INT_LINK_INDEX,
FIND_OBJECT_OID_LINK_INDEX, NEXT_OBJECT_OID_LINK_INDEX,
FIND_OBJECT_INT_LINK_INDEX, NEXT_OBJECT_INT_LINK_INDEX,
INSERT_OBJECT_OID_LINK, INSERT_OBJECT_INT_LINK, FIND_OBJECT_OID_LINK,
NEXT_OBJECT_OID_LINK, FIND_OBJECT_INT_LINK, NEXT_OBJECT_INT_LINK,
INSERT_OBJECT_OID, INSERT_OBJECT_INT, FIND_OBJECT_OID, FIND_OBJECT_INT,
NEXT_OBJECT_OID, NEXT_OBJECT_INT, this_tick, start_tick, get_ticks,
systemg, comm_define, community, oid_zeroDotZero,
oid_usmUnknownEngineIDs, oid_usmNotInTimeWindows, reqid_allocate,
reqid_next, reqid_base, reqid_istype, reqid_type, timer_start,
timer_start_repeat, timer_stop, fd_select, fd_deselect, fd_suspend,
fd_resume, or_register, or_unregister, buf_alloc, buf_size,
snmp_input_start, snmp_input_finish, snmp_output, snmp_send_port,
snmp_send_trap, snmp_pdu_auth_access string_save, string_commit,
string_rollback, string_get, string_get_max, string_free, ip_save,
ip_rollback, ip_commit, ip_get, oid_save, oid_rollback, oid_commit,
oid_get, index_decode, index_compare, index_compare_off, index_append,
index_append_off, snmpd_usmstats, bsnmpd_get_usm_stats,
bsnmpd_reset_usm_stats, usm_first_user, usm_next_user, usm_find_user,
usm_new_user, usm_delete_user, usm_flush_users, usm_user,
snmpd_target_stat, bsnmpd_get_target_stats, target_first_address,
target_next_address, target_new_address, target_activate_address,
target_delete_address, target_first_param, target_next_param,
target_new_param, target_delete_param, target_first_notify,
target_next_notify, target_new_notify, target_delete_notify,
target_flush_all, target_address, target_param, target_notify
β SNMP daemon loadable module interface. that's the vibe. π
π LIBRARY π
Begemot SNMP library (libbsnmp, -lbsnmp) β that's the one, bestie.
π οΈ SYNOPSIS π οΈ
#include <bsnmp/snmpmod.h>
INSERT_OBJECT_OID_LINK_INDEX(PTR, LIST, LINK, INDEX);
INSERT_OBJECT_INT_LINK_INDEX(PTR, LIST, LINK, INDEX);
FIND_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, LINK, INDEX);
FIND_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, LINK, INDEX);
NEXT_OBJECT_OID_LINK_INDEX(LIST, OID, SUB, LINK, INDEX);
NEXT_OBJECT_INT_LINK_INDEX(LIST, OID, SUB, LINK, INDEX);
INSERT_OBJECT_OID_LINK(PTR, LIST, LINK);
INSERT_OBJECT_INT_LINK(PTR, LIST, LINK);
FIND_OBJECT_OID_LINK(LIST, OID, SUB, LINK);
FIND_OBJECT_INT_LINK(LIST, OID, SUB, LINK);
NEXT_OBJECT_OID_LINK(LIST, OID, SUB, LINK);
NEXT_OBJECT_INT_LINK(LIST, OID, SUB, LINK);
INSERT_OBJECT_OID(PTR, LIST);
INSERT_OBJECT_INT(PTR, LIST);
FIND_OBJECT_OID(LIST, OID, SUB);
FIND_OBJECT_INT(LIST, OID, SUB);
NEXT_OBJECT_OID(LIST, OID, SUB);
NEXT_OBJECT_INT(LIST, OID, SUB);
extern uint64_t this_tick;
extern uint64_t start_tick;
uint64_t
get_ticks(void);
extern struct systemg systemg;
u_int
comm_define(u_int priv, const char *descr, struct lmodule *mod,
const char *str);
const char *
comm_string(u_int comm);
extern u_int community;
extern const struct asn_oid oid_zeroDotZero;
u_int
reqid_allocate(int size, struct lmodule *mod);
int32_t
reqid_next(u_int type);
int32_t
reqid_base(u_int type);
int
reqid_istype(int32_t reqid, u_int type);
u_int
reqid_type(int32_t reqid);
void *
timer_start(u_int ticks, void (*func)(void *), void *uarg,
struct lmodule *mod);
void *
timer_start_repeat(u_int ticks, u_int repeat_ticks,
void (*func)(void *), void *uarg, struct lmodule *mod);
void
timer_stop(void *timer_id);
void *
fd_select(int fd, void (*func)(int, void *), void *uarg,
struct lmodule *mod);
void
fd_deselect(void *fd_id);
void
fd_suspend(void *fd_id);
int
fd_resume(void *fd_id);
u_int
or_register(const struct asn_oid *oid, const char *descr,
struct lmodule *mod);
void
or_unregister(u_int or_id);
void *
buf_alloc(int tx);
size_t
buf_size(int tx);
enum snmpd_input_err
snmp_input_start(const u_char *buf, size_t len, const char *source,
struct snmp_pdu *pdu, int32_t *ip, size_t *pdulen);
enum snmpd_input_err
snmp_input_finish(struct snmp_pdu *pdu, const u_char *rcvbuf,
size_t rcvlen, u_char *sndbuf, size_t *sndlen, const char *source,
enum snmpd_input_err ierr, int32_t ip, void *data);
void
snmp_output(struct snmp_pdu *pdu, u_char *sndbuf, size_t *sndlen,
const char *dest);
void
snmp_send_port(void *trans, const struct asn_oid *port,
struct snmp_pdu *pdu, const struct sockaddr *addr,
socklen_t addrlen);
void
snmp_send_trap(const struct asn_oid *oid, ...);
enum snmp_code
snmp_pdu_auth_access(struct snmp_pdu *pdu, int32_t *ip);
int
string_save(struct snmp_value *val, struct snmp_context *ctx,
ssize_t req_size, u_char **strp);
void
string_commit(struct snmp_context *ctx);
void
string_rollback(struct snmp_context *ctx, u_char **strp);
int
string_get(struct snmp_value *val, const u_char *str, ssize_t len);
int
string_get_max(struct snmp_value *val, const u_char *str, ssize_t len,
size_t maxlen);
void
string_free(struct snmp_context *ctx);
int
ip_save(struct snmp_value *val, struct snmp_context *ctx, u_char *ipa);
void
ip_rollback(struct snmp_context *ctx, u_char *ipa);
void
ip_commit(struct snmp_context *ctx);
int
ip_get(struct snmp_value *val, u_char *ipa);
int
oid_save(struct snmp_value *val, struct snmp_context *ctx,
struct asn_oid *oid);
void
oid_rollback(struct snmp_context *ctx, struct asn_oid *oid);
void
oid_commit(struct snmp_context *ctx);
int
oid_get(struct snmp_value *val, const struct asn_oid *oid);
int
index_decode(const struct asn_oid *oid, u_int sub, u_int code, ...);
int
index_compare(const struct asn_oid *oid1, u_int sub,
const struct asn_oid *oid2);
int
index_compare_off(const struct asn_oid *oid1, u_int sub,
const struct asn_oid *oid2, u_int off);
void
index_append(struct asn_oid *dst, u_int sub, const struct asn_oid *src);
void
index_append_off(struct asn_oid *dst, u_int sub,
const struct asn_oid *src, u_int off);
extern struct snmpd_usmstat snmpd_usmstats;
struct snmpd_usmstat *
bsnmpd_get_usm_stats(void);
void
bsnmpd_reset_usm_stats(void);
struct usm_user *
usm_first_user(void);
struct usm_user *
usm_next_user(struct usm_user *uuser);
struct usm_user *
usm_find_user(uint8_t *engine, uint32_t elen, char *uname);
struct usm_user *
usm_new_user(uint8_t *engine, uint32_t elen, char *uname);
void
usm_delete_user(struct usm_user *);
void
usm_flush_users(void);
extern struct usm_user *usm_user;
struct snmpd_target_stats *
bsnmpd_get_target_stats(void);
struct target_address *
target_first_address(void);
struct target_address *
target_next_address(struct target_address *);
struct target_address *
target_new_address(char *);
int
target_activate_address(struct target_address *);
int
target_delete_address(struct target_address *);
struct target_param *
target_first_param(void);
struct target_param *
target_next_param(struct target_param *);
struct target_param *
target_new_param(char *);
int
target_delete_param(struct target_param *);
struct target_notify *
target_first_notify(void);
struct target_notify *
target_next_notify(struct target_notify *);
struct target_notify *
target_new_notify(char *);
int
target_delete_notify(struct target_notify *);
void
target_flush_all(void);
extern const struct asn_oid oid_usmUnknownEngineIDs;
extern const struct asn_oid oid_usmNotInTimeWindows;
π DESCRIPTION π
okay so here's the vibe: the bsnmpd(1) SNMP daemon ships with a minimal MIB. like, it is not trying to be every MIB at once. that minimal MIB is made of the system group, part of the SNMP MIB, a private configuration MIB, a trap destination table, a UDP port table, a community table, a module table, a statistics group, and a debugging group. every other MIB comes through loadable modules. this setup lets bsnmpd(1) be used for tasks that are not the classical SNMP tasks β main character energy, honestly. π
π§ MODULE LOADING AND UNLOADING π§
modules get loaded by writing to the module table. that table is indexed by a string that identifies the module to the daemon. this identifier is used to select the correct configuration section from the configuration files and to identify resources allocated to this module. a row in the module table is created by writing a string of non-zero length to the begemotSnmpdModulePath column. that string must be the complete path to the file containing the module. to unload a module, write a zero-length string to the path column of an existing row. clean. π
modules may depend on each other, so they must be loaded in the correct order. the dependencies are listed in the corresponding manual pages. no cap.
upon loading a module, the SNMP daemon expects the module file to export a global symbol config. this symbol should be a variable of type struct snmp_module:
typedef enum snmpd_proxy_err (*proxy_err_f)(struct snmp_pdu *, void *,
const struct asn_oid *, const struct sockaddr *, socklen_t,
enum snmpd_input_err, int32_t);
struct snmp_module {
const char *comment;
int (*init)(struct lmodule *, int argc, char *argv[]);
int (*fini)(void);
void (*idle)(void);
void (*dump)(void);
void (*config)(void);
void (*start)(void);
proxy_err_f proxy;
const struct snmp_node *tree;
u_int tree_size;
void (*loading)(const struct lmodule *, int);
};
this structure must be statically initialized, and its fields have the following functions:
commentβ this is a string that will be visible in the module table. it should give some hint about the function of this module. β¨initβ this function is called upon loading the module. the module pointer should be stored by the module because it is needed in other calls, and the argument vector will contain the arguments to this module from the daemon's command line. this function should return0if everything is ok or a UNIX error code (seeerrno(3)). once the function returns0, thefinifunction is called when the module is unloaded. π«Άfiniβ this one fires when the module is getting unloaded π«Ά it's your lil moment to free resources that nothing else is gonna free for you. And bestie, free all the memory, no cap β daemons tend to run very long, so a leak just sits there marinating forever π This function pointer may beNULLif you don't need it.idleβ if this pointer isn'tNULL, the function it points to gets called every time the daemon is about to wait for an event. Lowkeyβ¦ try to avoid using this feature π (it's giving "please don't").dumpβ whenever the daemon catches aSIGUSR1it dumps its internal state viasyslog(3)β¨ If thedumpfield isn'tNULL, the daemon calls it to dump the state of the module.configβ whenever the daemon catches aSIGHUPit re-reads its configuration file. If theconfigfield isn'tNULL, it gets called after the config file is read, so your module can adapt to the new config πstartβ if notNULL, this gets called after the module is successfully loaded and initialized, to kick off its actual operation. main character energy πproxyβ if the daemon receives a PDU and that PDU's community string belongs to a community that this module registered, andproxyisn'tNULL, then this function is called to handle the PDU πtreeβ a pointer to the node array for the MIB tree your module implements π³tree_sizeβ the number of nodes intree.loadingβ if this pointer isn'tNULL, it's called whenever another module was loaded or unloaded. It gets a pointer to that module plus a flag that's0for unloading and1for loading π
When everything is ok, the daemon merges the module's MIB tree into its current global tree, then calls the module's init() function. If that function returns an error, the module's MIB tree gets removed from the global one and the module is unloaded π If initialization succeeds, the module's start() function is called, and after it returns, the loaded() functions of all modules (including the one that just loaded) are called. periodt.
When the module is unloaded, its MIB tree is removed from the global one, the communities, request id ranges, running timers and selected file descriptors are released, fini() is called, the module file is unloaded, and the loaded() functions of all the other modules are called π
β¨ IMPLEMENTING TABLES β¨
There's a whole set of macros designed to help you implement SNMP tables, and honestly they ate π
So the tricky part of implementing a table is supporting the GETNEXT operator β GETNEXT has to figure out, given an arbitrary OID, the least table row whose OID is higher than the given OID. The easiest way to do this is to keep the table as an ordered list of structures, each one containing an OID that's the index of the table row. That gives you easy removal, insertion and search. iconic.
These helper macros assume the table is organized as a TAILQ (see queue(3)) and that each structure contains a struct asn_oid used as index π¦ For simple tables with only an integer or unsigned index, there's an alternate form of the macros that presumes the existence of an integer or unsigned field as the index field.
The macros have names of the form:
{INSERT,FIND,NEXT}_OBJECT_{OID,INT}[_LINK[_INDEX]]
Here's the vibe breakdown:
- The
INSERT_*()macros are used in theSEToperation to insert a new table row into the table π - The
FIND_*()macros are used in theGEToperation to find a specific row in the table π - The
NEXT_*()macros are used in theGETNEXToperation to find the next row in the table βοΈ
The last two macros return a pointer to the row structure if a row is found, and NULL otherwise. The *_OBJECT_OID_*() macros assume there's a struct asn_oid being used as the index; the *_OBJECT_INT_*() macros assume there's an unsigned integer field being used as the index π
The *_INDEX() macros let you explicitly name the index field in the parameter INDEX, whereas the other macros just assume that field is named index. The *_LINK_*() macros let you explicitly name the link field of the tail queues β the others assume the link field is named link. Naming the link field explicitly might be necessary when the same structures are held in two or more different tables π―ββοΈ
The arguments to the macros go like this:
PTRβ a pointer to the new structure being inserted into the table.LISTβ a pointer to the tail queue head.LINKβ the name of the link field in the row structure.INDEXβ the name of the index field in the row structure.OIDβ must point to thevarfield of the value argument to the node operation callback. This is the OID you're searching for πSUBβ the index of the start of the table index in the OID pointed to byOID. This is usually the same as thesubargument to the node operation callback.
πΈ DAEMON TIMESTAMPS πΈ
The variable this_tick holds the tick (and yeah, there are 100 SNMP ticks in a second β±οΈ) for when the current PDU processing started.
The variable start_tick holds the tick for when the daemon was started.
The function get_ticks() returns the current tick β¨ So the number of ticks since the daemon started is:
get_ticks() - start_tick
math slay π
π THE SYSTEM GROUP π
The scalar fields of the system group live in the global variable systemg:
struct systemg {
u_char *descr;
struct asn_oid object_id;
u_char *contact;
u_char *name;
u_char *location;
uint32_t services;
uint32_t or_last_change;
};
π¬ COMMUNITIES π¬
The SNMP daemon implements a community table π«Ά When a request message comes in, the community string in that message gets compared against each of the community strings in that table. If there's a match, the global variable community is set to the community identifier for that community. Community identifiers are unsigned integers.
For the three standard communities there are three constants defined:
#define COMM_INITIALIZE 0
#define COMM_READ 1
#define COMM_WRITE 2
community is set to COMM_INITIALIZE while the assignments in the configuration file are being processed π And it's set to COMM_READ or COMM_WRITE when the community strings for the read-write or read-only community are found in the incoming PDU.
Modules can define additional communities too π This might be necessary to provide transport proxying (where a PDU received on one communication link gets proxied to another link) or to implement non-UDP access points to SNMP.
A new community is defined with the function comm_define(), which takes these parameters:
privβ an integer identifying the community to the module. Each module has its own namespace with regard to this parameter, and the community table is indexed with the module name plus this identifier.descrβ a string with a human readable description of the community. It's visible in the community table πmodβ the module defining the community.strβ the initial community string.
The function returns a globally unique community identifier β¨ If an SNMPv1 or SNMPv2 PDU is received whose community string matches, this identifier is set into the global community.
The function comm_string() returns the current community string for the given community.
All communities defined by a module are automatically released when the module is unloaded π¦
π THE USER-BASED SECURITY GROUP π
The scalar statistics of the USM group live in the global variable snmpd_usmstats:
struct snmpd_usmstat {
uint32_t unsupported_seclevels;
uint32_t not_in_time_windows;
uint32_t unknown_users;
uint32_t unknown_engine_ids;
uint32_t wrong_digests;
uint32_t decrypt_errors;
};
bsnmpd_get_usm_stats() returns a pointer to the global structure containing the statistics π And bsnmpd_reset_usm_stats() clears the statistics of the USM group, fresh start energy β¨
The daemon also maintains a global list of configured USM users:
struct usm_user {
struct snmp_user suser;
uint8_t user_engine_id[SNMP_ENGINE_ID_SIZ];
uint32_t user_engine_len;
char user_public[SNMP_ADM_STR32_SIZ];
uint32_t user_public_len;
int32_t status;
int32_t type;
SLIST_ENTRY(usm_user) up;
};
This structure represents a USM user π The daemon only responds to SNMPv3 PDUs whose user credentials match a USM user entry in its global list. If an SNMPv3 PDU comes in whose security model is USM, the global usm_user is set to point at the user entry matching the credentials in the PDU.
But here's the thing β the daemon does not create or remove USM users π€ it just gives an interface to external loadable module(s) to manage the list:
usm_new_user()adds a user entry to the listusm_delete_user()deletes an existing entry from the listusm_flush_users()removes all configured USM usersusm_first_user()returns the first user in the list, orNULLif the list is emptyusm_next_user()returns the next user after a given entry, if one exists, orNULLusm_find_user()returns the USM user entry matching the given engine and uname, orNULLif no user with that name and engine id is in the list
The list is sorted according to the USM user name and Engine ID π€ iykyk.
π― THE MANAGEMENT TARGET GROUP π―
The Management Target group holds target address information used when sending SNMPv3 notifications π¨
The scalar statistics of the Management Target group live in the global variable snmpd_target_stats:
struct snmpd_target_stats {
uint32_t unavail_contexts;
uint32_t unknown_contexts;
};
bsnmpd_get_target_stats() returns a pointer to the global structure containing the statistics β
Three global lists of configured management target addresses, parameters and notifications respectively are maintained by the daemon π
struct target_address {
char name[SNMP_ADM_STR32_SIZ];
uint8_t address[SNMP_UDP_ADDR_SIZ];
int32_t timeout;
int32_t retry;
char taglist[SNMP_TAG_SIZ];
char paramname[SNMP_ADM_STR32_SIZ];
int32_t type;
int32_t socket;
int32_t status;
SLIST_ENTRY(target_address) ta;
};
Okay so this one is the SNMPv3 Management Target address glow-up π β every single time a SNMP TRAP gets sent, the daemon literally sprays it out to all the active Management Target addresses chilling in its global list. Like a group chat blast, no cap π’
struct target_param {
char name[SNMP_ADM_STR32_SIZ];
int32_t mpmodel;
int32_t sec_model;
char secname[SNMP_ADM_STR32_SIZ];
enum snmp_usm_level sec_level;
int32_t type;
int32_t status;
SLIST_ENTRY(target_param) tp;
};
This struct is the whole vibe check for how we talk to the addresses above β it's the info used to generate SNMP messages for the associated SNMPv3 Management Target addresses π«Ά
struct target_notify {
char name[SNMP_ADM_STR32_SIZ];
char taglist[SNMP_TAG_SIZ];
int32_t notify_type;
int32_t type;
int32_t status;
SLIST_ENTRY(target_notify) tn;
};
And this cutie represents Notification Tag entries β¨ β SNMP notifications get sent to a Target address for every entry in the Management Target Address list whose tag matches the tag in this struct. Tag matching is so it's giving curated guest list π
So here's the tea: the daemon does not create or remove entries in the Management Target group lists itself. It just hands an interface to external loadable module(s) and lets them manage the lists. Delegation, main character energy π
target_new_address()β adds a target address entry.target_delete_address()β deletes an existing entry from the target address list.target_activate_address()β creates a socket associated with the target address entry so SNMP notifications may actually be sent to that target address. (before this, it's giving "all dressed up with nowhere to go" π)target_first_address()β returns a pointer to the first target address entry in the list.target_next_address()β returns a pointer to the next target address of a given entry, if one exists.target_new_param()β adds a target parameters' entry.target_delete_param()β deletes an existing entry from the target parameters list.target_first_param()β returns a pointer to the first target parameters' entry in the list.target_next_param()β returns a pointer to the next target parameters of a given entry, if one exists.target_new_notify()β adds a notification target entry.target_delete_notify()β deletes an existing entry from the notification target list.target_first_notify()β returns a pointer to the first notification target entry in the list.target_next_notify()β returns a pointer to the next notification target of a given entry, if one exists.target_flush_all()β removes all configured data from the three global Management Target Group lists. Nuclear option, lowkey π
π WELL KNOWN OIDS π
The global variable oid_zeroDotZero holds the OID 0.0. Iconic, minimal, does exactly what it says on the tin.
The global variables oid_usmUnknownEngineIDs and oid_usmNotInTimeWindows hold the OIDs 1.3.6.1.6.3.15.1.1.4.0 and 1.3.6.1.6.3.15.1.1.2.0, which get used in SNMPv3 USM Engine Discovery. Yeah, those numbers, you gotta copy them exactly, no paraphrasing bestie π¦
π¨ REQUEST ID RANGES π¨
So if a module implements SNMP client functions alongside SNMP agent functions, it might need to tell SNMP requests apart by their identifier β that way responses get routed to the right sub-system without chaos. Request id ranges are the whole solution: they let you grab globally non-overlapping sub-ranges out of the entire 31-bit id range. Chef's kiss allocation strategy π€
You allocate a request id range with reqid_allocate(). The arguments are the size of the range and the module allocating the range. Like, for example:
id = reqid_allocate(1000, module);
That allocates a range of 1000 request ids. The function returns the request id range identifier, or 0 if there's just not enough identifier space (sad but honest π). And reqid_base() returns the lowest request id in the given range.
Request ids get allocated starting at the lowest one and go linear throughout the range. Pro tip fr fr: if the client app might have a whole lot of outstanding requests, the range has to be big enough that an id doesn't get reused until it's really expired. reqid_next() returns the sequentially next id in the range.
reqid_istype()β checks whether the request idreqidis inside the range identified bytype.reqid_type()β returns the range identifier for the givenreqid, or0if that request id isn't in any of the ranges.
β° TIMERS β°
The SNMP daemon supports an arbitrary number of timers with SNMP tick granularity. Fancy.
timer_start() arranges for the callback func to be called with the argument uarg after ticks SNMP ticks have expired. mod is the module that starts the timer. Heads up: these timers are one-shot β they do not restart, periodt π
Repeatable timers are started with timer_start_repeat(), which takes an additional argument repeat_ticks. Here's the breakdown:
ticksβ number of ticks until the first execution of the callback.repeat_ticksβ number of ticks between invocations of the callback.
Ngl, note that currently the number of initial ticks may silently be set identical to the number of ticks between callback invocations. Not us pretending that's fine π
The function returns a timer identifier you can use to stop the timer via timer_stop(). And if a module gets unloaded, all timers started by that module that haven't expired yet are stopped. Cleanup crew, we love to see it π§
π FILE DESCRIPTOR SUPPORT π
Sometimes a module needs to get input from socket file descriptors without blocking the daemon (like, say, to implement alternative SNMP transports). Very demure, very mindful.
fd_select() causes the callback function func to be called with the file descriptor fd and the user argument uarg whenever fd can be read or has a close condition. If the file descriptor isn't in non-blocking mode, it gets set to non-blocking mode β surprise, you're non-blocking now β¨
If the callback isn't needed anymore, call fd_deselect() with the value returned from fd_select(). And all file descriptors selected by a module are automatically deselected when the module is unloaded. Zero loose ends.
To temporarily suspend the file descriptor registration, call fd_suspend(). This also switches the file descriptor back to blocking mode if it was blocking before the call to fd_select(). Lowkey this is necessary to do synchronous input on a selected socket. The effect of fd_suspend() can be undone with fd_resume(). Pause and unpause, the duality of woman π¦
π¦ OBJECT RESOURCES π¦
The system group contains an object resource table. A module can create an entry in this table by calling or_register() with:
- the
oidto be registered, - a textual description in
str, - and a pointer to the module
mod.
The registration can be removed again with or_unregister(). And all registrations belonging to a module are automatically removed if the module is unloaded. Self-cleaning, we stan πΈ
π’οΈ TRANSMIT AND RECEIVE BUFFERS π’οΈ
A buffer is allocated via buf_alloc(). The argument must be 1 for transmit buffers and 0 for receive buffers β binary, no in-between, no vibes-based guessing π
The function may return NULL if there's no memory available. (Not a mood, just a fact.)
The current buffersize can be obtained with buf_size().
π₯ PROCESSING PDUS π₯
Okay so bestie, if your module needs to do its own PDU processing (think proxying energy), these functions are literally your backstage pass π
snmp_input_start()β decodes the PDU, searches the community, and sets the globalthis_tick. It returns one of these error codes:SNMPD_INPUT_OKβ everything ok, continue with processing β¨SNMPD_INPUT_FAILEDβ the PDU could not be decoded, has a wrong version, or has an unknown community string πSNMPD_INPUT_VALBADLENβ aSETPDU had a value field in a binding with a wrong length field in an ASN.1 headerSNMPD_INPUT_VALRANGEβ aSETPDU had a value field in a binding with a value that is out of range for the given ASN.1 typeSNMPD_INPUT_VALBADENCβ aSETPDU had a value field in a binding with wrong ASN.1 encodingSNMPD_INPUT_TRUNCβ the buffer appears to contain a valid begin of a PDU, but is too short. For streaming transports this means that the caller must save what they already have, try to obtain more input, and reissue this input to the function. For datagram transports this means that part of the datagram was lost and the input should be ignored π«
snmp_input_finish()β does the other half of processing: ifsnmp_input_start()did not return OK, it tries to construct an error response. If the start was OK, it calls the correct function frombsnmpagent(3)to execute the request and, depending on the outcome, constructs a response or error response PDU or ignores the request PDU. It returns eitherSNMPD_INPUT_OKorSNMPD_INPUT_FAILED. In the first case a response PDU was constructed and should be sent πsnmp_output()β takes a PDU and encodes it.snmp_send_port()β takes a PDU, encodes it, and sends it through the given port (identified by the transport and the index in the port table) to the given address.snmp_send_trap()β sends a trap to all trap destinations. The arguments are theoididentifying the trap and aNULL-terminated list ofstruct snmp_valuepointers that are to be inserted into the trap binding list.snmp_pdu_auth_access()β verifies whether access to the object IDs contained in thepdushould be granted or denied, according to the configured View-Based Access rules.ipcontains the index of the first varbinding to which access was denied, or0if access to all varbindings in the PDU is granted π‘οΈ
π§Έ SIMPLE ACTION SUPPORT π§Έ
For simple scalar variables that need no dependencies, a number of support functions is available to handle the set, commit, rollback, and get. No cap, this is the low-effort slay section π
The following functions are used for OCTET STRING scalars, either NUL terminated or not:
string_save()β should be called forSNMP_OP_SET.valueandctxare the resp. arguments to the node callback.valpis a pointer to the pointer that holds the current value, andreq_sizeshould be-1if any size of the string is acceptable or a number larger or equal zero if the string must have a specific size. The function saves the old value in the scratch area (note, that any initial value must have been allocated bymalloc(3)), allocates a new string, copies over the new value,NUL-terminates it, and sets the new current value β¨string_commit()β simply frees the saved old value in the scratch area.string_rollback()β frees the new value, and puts back the old one.string_get()β is used forGETorGETNEXT. The function (and yes, the manpage sentence literally stops there, bestie π)string_get_max()β can be used instead ofstring_get()to ensure that the returned string has a certain maximum length. Iflenis-1, the length is computed viastrlen(3)from the current string value. If the current value isNULL, aOCTET STRINGof zero length is returned.string_free()β must be called if either rollback or commit fails to free the saved old value.
The following functions are used to process scalars of type IP-address:
ip_save()β saves the current value in the scratch area and sets the new value fromvalp.ip_commit()β does nothing.ip_rollback()β restores the old IP address from the scratch area.ip_get()β retrieves the IP current address.
The following functions handle OID-typed variables:
oid_save()β saves the current value in the scratch area by allocating astruct asn_oidwithmalloc(3)and sets the new value fromoid.oid_commit()β frees the old value in the scratch area.oid_rollback()β restores the old OID from the scratch area and frees the old OID.oid_get()β retrieves the OID π
π§ TABLE INDEX HANDLING π§
The following functions help in handling table indexes:
index_decode()β decodes the index part of the OID. The parameteroidmust be a pointer to thevarfield of thevalueargument of the node callback. Thesubargument must be the index of the start of the index in the OID (this is thesubargument to the node callback).codeis the index expression (parameteridxto the node callback). These parameters are followed by parameters depending on the syntax of the index elements as follows:INTEGERβint32_t *expected as argument.COUNTER64βuint64_t *expected as argument. Note, that this syntax is illegal for indexes.OCTET STRINGβ au_char **and asize_t *expected as arguments. A buffer is allocated to hold the decoded string.OIDβ astruct asn_oid *is expected as argument.IP ADDRESSβ au_int8_t *expected as argument that points to a buffer of at least four byte.COUNTER,GAUGE,TIMETICKSβ au_int32_texpected.NULLβ no argument expected.
index_compare()β compares the current variable with an OID.oid1andsubcome from the node callback argumentsvalue->varandsubresp.oid2is the OID to compare to. The function returns-1,0,+1when the variable is lesser, equal, higher to the given OID.oid2must contain only the index part of the table column.index_compare_off()β is equivalent toindex_compare()except that it takes an additional parameteroffthat causes it to ignore the firstoffcomponents of both indexes.index_append()β appends OIDsrcbeginning at positionsubtodst.index_append_off()β appends OIDsrcbeginning at positionofftodstbeginning at positionsub + off.
π SEE ALSO π
π STANDARDS π
This implementation conforms to the applicable IETF RFCs and ITU-T recommendations. Periodt.
π AUTHORS π
Hartmut Brandt <harti@FreeBSD.org>
π That's the whole page, bestie β this is the Debian version, dated December 19, 2010, for SNMPMOD(3). No cap, you ate. π«Ά
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2004-2005 Hartmut Brandt
- Copyright (c) 2001-2003 Fraunhofer Institute for Open Communication Systems (FhG Fokus)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms