snmp_client snmp_send_cb_f snmp_timeout_cb_f snmp_timeout_start_f snmp_timeout_stop_f snmp_open snmp_close snmp_pdu_create snmp_add_binding snmp_pdu_check snmp_pdu_send snmp_oid_append snmp_parse_server snmp_receive snmp_table_cb_f snmp_table_fetch snmp_table_fetch_async snmp_dialog - SNMP client library
Begemot SNMP library (libbsnmp, -lbsnmp)
struct snmp_client { enum snmp_version version; int trans; /* transport type to use */ /* these two are read-only for the application */ char *cport; /* port number as string */ char *chost; /* host name or IP address as string */ char read_community[SNMP_COMMUNITY_MAXLEN + 1]; char write_community[SNMP_COMMUNITY_MAXLEN + 1]; struct timeval timeout; u_int retries; int dump_pdus; size_t txbuflen; size_t rxbuflen; int fd; int32_t next_reqid; int32_t max_reqid; int32_t min_reqid; char error[SNMP_STRERROR_LEN]; snmp_timeout_start_f timeout_start; snmp_timeout_stop_f timeout_stop; /* private */ char local_path[sizeof(SNMP_LOCAL_PATH)]; };
The fields of this structure are described below.
In the current implementation there is a global variable
that is used by all the library functions.
The first call into the library must be a call to
snmp_client_init ();
to initialize this global variable to the default values.
After this call and before calling
snmp_open ();
the fields of the variable may be modified by the user.
The modification of the
chost
and
cport
fields should be done only via the functions
snmp_client_set_host ();
and
snmp_client_set_port (.);
The function
snmp_open ();
creates a UDP or
UNIX
domain socket and connects it to the agent's IP address and port.
If any of the arguments of the call is not
NULL
the corresponding field in the global
snmp_client
is set from the argument.
Otherwise the values that are already in that variable are used.
The function
snmp_close ();
closes the socket, stops all timeouts and frees all dynamically allocated
resources.
The next three functions are used to create request PDUs.
The function
snmp_pdu_create ();
initializes a PDU of type
op
It does not allocate space for the PDU itself.
This is the responsibility of the caller.
snmp_add_binding ();
adds bindings to the PDU and returns the (zero based) index of the first new
binding.
The arguments are pairs of pointer to the OIDs and syntax constants,
terminated by a NULL.
The call
snmp_add_binding(&pdu, &oid1, SNMP_SYNTAX_INTEGER, &oid2, SNMP_SYNTAX_OCTETSTRING, NULL);
adds two new bindings to the PDU and returns the index of the first one.
It is the responsibility of the caller to set the value part of the binding
if necessary.
The functions returns -1 if the maximum number of bindings is exhausted.
The function
snmp_oid_append ();
can be used to construct variable OIDs for requests.
It takes a pointer to an
Vt struct asn_oid
that is to be constructed, a format string, and a number of arguments
the type of which depends on the format string.
The format string is interpreted
character by character in the following way:
The function
snmp_pdu_check ();
may be used to check a response PDU.
A number of checks are performed
(error code, equal number of bindings, syntaxes and values for SET PDUs).
The function returns +1 if everything is ok, 0 if a NOSUCHNAME or similar
error was detected, -1 if the response PDU had fatal errors
and -2 if
Fa resp
is
NULL
(a timeout occurred).
The function
snmp_pdu_send ();
encodes and sends the given PDU.
It records the PDU together with the callback
and user pointers in an internal list and arranges for retransmission if no
response is received.
When a response is received or the retransmission count
is exceeded the callback
Fa func
is called with the orignal request PDU, the response PDU and the user argument
Fa uarg .
If the retransmit count is exceeded,
Fa func
is called with the original request PDU, the response pointer set to
NULL
and the user argument
Fa uarg .
The caller should not free the request PDU until the callback function is
called.
The callback function must free the request PDU and the response PDU (if not
NULL ).
The function
snmp_receive ();
tries to receive a PDU.
If the argument is zero, the function polls to see
whether a packet is available, if the argument is non-zero, the function blocks
until the next packet is received.
The packet is delivered via the usual callback
mechanism (non-response packets are silently dropped).
The function returns 0, if a packet was received and successfully dispatched,
-1 if an error occurred or no packet was available (in polling mode).
The next two functions are used to retrieve tables from SNMP agents. They use the following input structure, that describes the table:
struct snmp_table { struct asn_oid table; struct asn_oid last_change; u_int max_iter; size_t entry_size; u_int index_size; uint64_t req_mask; struct snmp_table_entry { asn_subid_t subid; enum snmp_syntax syntax; off_t offset; } entries[]; };
The fields of this structure have the following meaning:
Both table fetching functions return TAILQ (see
queue(3))
of structures--one for each table row.
These structures must start with a
TAILQ_ENTRY ();
and a
Vt uint64_t
and are allocated via
malloc(3).
The
Fa list
argument of the table functions must point to a
TAILQ_HEAD (.);
The
Vt uint64_t
fields, usually called
found
is used to indicate which of the columns have been found for the given
row.
It is encoded like the
Fa req_mask
field.
The function
snmp_table_fetch ();
synchronously fetches the given table.
If everything is ok 0 is returned.
Otherwise the function returns -1 and sets an appropriate error string.
The function
snmp_table_fetch_async ();
fetches the tables asynchronously.
If either the entire table is fetch, or
an error occurs the callback function
Fa callback
is called with the callers arguments
Fa list
and
Fa uarg
and a parameter that is either 0 if the table was fetched, or
-1 if there was an error.
The function itself returns -1 if it could not
initialize fetching of the table.
The following table description is used to fetch the ATM interface table:
/* * ATM interface table */ struct atmif { TAILQ_ENTRY(atmif) link; uint64_t found; int32_t index; u_char *ifname; size_t ifnamelen; uint32_t node_id; uint32_t pcr; int32_t media; uint32_t vpi_bits; uint32_t vci_bits; uint32_t max_vpcs; uint32_t max_vccs; u_char *esi; size_t esilen; int32_t carrier; }; TAILQ_HEAD(atmif_list, atmif); /* list of all ATM interfaces */ struct atmif_list atmif_list; static const struct snmp_table atmif_table = { OIDX_begemotAtmIfTable, OIDX_begemotAtmIfTableLastChange, 2, sizeof(struct atmif), 1, 0x7ffULL, { { 0, SNMP_SYNTAX_INTEGER, offsetof(struct atmif, index) }, { 1, SNMP_SYNTAX_OCTETSTRING, offsetof(struct atmif, ifname) }, { 2, SNMP_SYNTAX_GAUGE, offsetof(struct atmif, node_id) }, { 3, SNMP_SYNTAX_GAUGE, offsetof(struct atmif, pcr) }, { 4, SNMP_SYNTAX_INTEGER, offsetof(struct atmif, media) }, { 5, SNMP_SYNTAX_GAUGE, offsetof(struct atmif, vpi_bits) }, { 6, SNMP_SYNTAX_GAUGE, offsetof(struct atmif, vci_bits) }, { 7, SNMP_SYNTAX_GAUGE, offsetof(struct atmif, max_vpcs) }, { 8, SNMP_SYNTAX_GAUGE, offsetof(struct atmif, max_vccs) }, { 9, SNMP_SYNTAX_OCTETSTRING, offsetof(struct atmif, esi) }, { 10, SNMP_SYNTAX_INTEGER, offsetof(struct atmif, carrier) }, { 0, SNMP_SYNTAX_NULL, 0 } } }; ... if (snmp_table_fetch(&atmif_table, &atmif_list) != 0) errx(1, "AtmIf table: %s", snmp_client.error); ...
The function
snmp_dialog ();
is used to execute a synchonuous dialog with the agent.
The request PDU
Fa req
is sent and the function blocks until the response PDU is received.
Note,
that asynchonuous receives are handled (i.e. callback functions of other send
calls or table fetches may be called while in the function).
The response PDU is returned in
Fa resp .
If no response could be received after all timeouts and retries, the function
returns -1.
If a response was received 0 is returned.
The function
snmp_parse_server ();
is used to parse an SNMP server specification string and fill in the
fields of a
Vt struct snmp_client .
The syntax of a server specification is
where trans is the transport name (one of udp, stream or dgram), community is the string to be used for both the read and the write community, server is the server's host name in case of UDP and the path name in case of a local socket, and port is the port in case of UDP transport. The function returns 0 in the case of success and return -1 and sets the error string in case of an error.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |