SNMP DPI Application Program Interface
This section documents an API that implements the SNMP DPI. This information has been previously published [6, 8], but the information provided below is more current as of May 14, 1991.
Overview of Request Processing
GET Processing
A GET request is the easiest to process. When the DPI packet is parsed, the parse tree holds the object ID of the variable being requested.
If the specified object is not supported by the sub-agent, it would return an error indication of "no such name". No name/type/value information would be returned.
unsigned char *cp;
cp = mkDPIresponse(SNMP_NO_SUCH_NAME,0);
If the object is recognized, then the sub-agent creates a parse tree representing the name/type/value of the object in question (using the DPI API routine mkDPIset()), and returns no error indication. This is demonstrated below (a string is being returned).
char *obj_id;
unsigned char *cp;
struct dpi_set_packet *ret_value;
char *data;
/* obj_id = object ID of variable, like 1.3.6.1.2.1.1.1 */
/* should be identical to object ID sent in get request */
data = "a string to be returned";
ret_value = mkDPIset(obj_id,SNMP_TYPE_STRING,
strlen(data)+1,data);
cp = mkDPIresponse(0,ret_value);
SET Processing
Processing a SET request is only slightly more difficult than a GET request. In this case, additional information is made available in the parse tree, namely the type, length and value to be set.
The sub-agent may return an error indication of "no such name" if the variable is unrecognized, just as in a GET request. If the variable is recognized, but cannot be set, an error indication of "no such name" should be also be returned, although it is tempting to return a "read only" error.
GET NEXT Processing
GET-NEXT requests are the most complicated requests to process. After parsing a GET-NEXT request, the parse tree will contain two parameters. One is the object ID on which the GET-NEXT operation is being performed. The semantics of the operation are that the sub-agent is to return the name/type/value of the next variable it supports whose name lexicographically follows the passed object ID.
It is important to realize that a given sub-agent may support several discontiguous sections of the MIB tree. In such a situation it would be incorrect to jump from one section to another. This problem is correctly handled by examining the second parameter which is passed. This parameter represents the "reason" why the sub-agent is being called. It holds the prefix of the tree that the sub-agent had indicated it supported.
If the next variable supported by the sub-agent does not begin with that prefix, the sub-agent must return an error indication of "no such name". If required, the SNMP agent will call upon the sub-agent again, but pass it a different group prefix. This is illustrated in the discussion below:
Assume there are two sub-agents. The first sub-agent registers two distinct sections of the tree, A and C. In reality, the sub-agent supports variables A.1 and A.2, but it correctly registers the minimal prefix required to uniquely identify the variable class it supports.
The second sub-agent registers a different section, B, which appears between the two sections registered by the first agent.
If a remote management station begins dumping the MIB, starting from A, the following sequence of queries would be performed:
Sub-agent 1 gets called:
get-next(A,A) == A.1
get-next(A.1,A) = A.2
get-next(A.2,A) = error(no such name)
Sub-agent 2 is then called:
get-next(A.2,B) = B.1
get-next(B.1,B) = error(no such name)
Sub-agent 1 gets called again:
get-next(B.1,C) = C.1
REGISTER Requests
A sub-agent must register the variables it supports with the SNMP agent. The appropriate packets may be created using the DPI API library routine mkDPIregister().
unsigned char *cp;
cp = mkDPIregister("1.3.6.1.2.1.1.2.");
NOTE: object IDs are registered with a trailing dot (".").
TRAP Requests
A sub-agent can request that the SNMP agent generate a trap for it. The sub-agent must provide the desired values for the generic and specific parameters of the trap. It may optionally provide a name/type/value parameter that will be included in the trap packet. The DPI API library routine mkDPItrap() can be used to generate the required packet.
DPI API Library Routines
This section documents Application Program Interfaces to the DPI.
QUERY_DPI_PORT()
int port;
char *hostname, *community_name;
port = query_DPI_port(hostname, community_name);
The query_DPI_port() function is used by a DPI client to determine what TCP port number is associated with the DPI. This port number is needed to connect() to the SNMP agent. If the port cannot be determined, -1 is returned.
The function is passed two arguments: a string representing the host's name or IP address and the community name to be used when making the request.
This function enables a DPI client to "bootstrap" itself. The port number is obtained via an SNMP GET request, but the DPI client does not have to be able to create and parse SNMP packets--this is all done by the query_DPI_port() function.
NOTE: the query_DPI_port() function assumes that the community name does not contain any null characters. If this is not the case, use the _query_DPI_port() function which takes a third parameter, the length of the community name.
MKDPIREGISTER
#include "snmp_dpi.h"
unsigned char *packet;
int len;
/* register sysDescr variable */
packet = mkDPIregister("1.3.6.1.2.1.1.1.");
len = *packet * 256 + *(packet + 1);
len += 2; /* include length bytes */
The mkDPIregister() function creates the necessary register-request packet and returns a pointer to a static buffer holding the packet contents. The null pointer (0) is returned if there is an error detected during the creation of the packet.
The length of the remainder packet is stored in the first two bytes of the packet, as demonstrated in the example above.
NOTE: object identifiers are registered with a trailing dot (".").
MKDPISET
#include "snmp_dpi.h"
struct dpi_set_packet *set_value;
char *obj_id;
int type, length;
char *value;
set_value = mkDPIset(obj_id, type, length, value);
The mkDPIset() function can be used to create the portion of a parse tree that represents a name/value pair (as would be normally be returned in a response packet). It returns a pointer to a dynamically allocated parse tree representing the name/type/value information. If there is an error detected while creating the parse tree, the null pointer (0) is returned.
The value of type can be one of the following (which are defined in the include file "snmp_dpi.h"):
- SNMP_TYPE_NUMBER
- SNMP_TYPE_STRING
- SNMP_TYPE_OBJECT
- SNMP_TYPE_INTERNET
- SNMP_TYPE_COUNTER
- SNMP_TYPE_GAUGE
- SNMP_TYPE_TICKS
The value parameter is always a pointer to the first byte of the object's value.
NOTE: the parse tree is dynamically allocated and copies are made of the passed parameters. After a successful call to mkDPIset(), they can be disposed of in any manner the application chooses without affecting the parse tree contents.
MKDPIRESPONSE
#include "snmp_dpi.h"
unsigned char *packet;
int error_code;
struct dpi_set_packet *ret_value;
packet = mkDPIresponse(error_code, ret_value);
len = *packet * 256 + *(packet + 1);
len += 2; /* include length bytes */
The mkDPIresponse() function creates an appropriate response packet. It takes two parameters. The first is the error code to be returned. It may be 0 (indicating no error) or one of the following (which are defined in the include file "snmp_dpi.h"):
- SNMP_NO_ERROR
- SNMP_TOO_BIG
- SNMP_NO_SUCH_NAME
- SNMP_BAD_VALUE
- SNMP_READ_ONLY
- SNMP_GEN_ERR
If the error code indicates no error, then the second parameter is a pointer to a parse tree (created by mkDPIset()) which represents the name/type/value information being returned. If an error is indicated, the second parameter is passed as a null pointer (0).
If the packet can be created, a pointer to a static buffer containing the packet contents is returned. This is the same buffer used by mkDPIregister(). If an error is encountered while creating the packet, the null pointer (0) is returned.
The length of the remainder packet is stored in the first two bytes of the packet, as demonstrated in the example above.
NOTE: mkDPIresponse() always frees the passed parse tree.
MKDPITRAP
#include "snmp_dpi.h"
unsigned char *packet;
int generic, specific;
struct dpi_set_packet *ret_value;
packet = mkDPItrap(generic, specific, ret_value);
len = *packet * 256 + *(packet + 1);
len += 2; /* include length bytes */
The mkDPItrap() function creates an appropriate trap request packet. The first two parameters correspond to to value of the generic and specific fields in the SNMP trap packet. The third field can be used to pass a name/value pair to be provided in the SNMP trap packet. This information is passed as the set-packet portion of the parse tree. As an example, a linkDown trap for interface 3 might be generated by the following:
struct dpi_set_packet *if_index_value;
unsigned long data;
unsigned char *packet;
int len;
data = 3; /* interface number = 3 */
if_index_value = mkDPIset("1.3.6.1.2.1.2.2.1.1", SNMP_TYPE_NUMBER,
sizeof(unsigned long), &data);
packet = mkDPItrap(2, 0, if_index_value);
len = *packet * 256 + *(packet + 1);
len += 2; /* include length bytes */
write(fd,packet,len);
If the packet can be created, a pointer to a static buffer containing the packet contents is returned. This is the same buffer used by mkDPIregister(). If an error is encountered while creating the packet, the null pointer (0) is returned.
The length of the remainder packet is stored in the first two bytes of the packet, as demonstrated in the example above.
NOTE: mkDPItrap() always frees the passed parse tree.
PDPIPACKET
#include "snmp_dpi.h"
unsigned char *packet;
struct snmp_dpi_hdr *hdr;
hdr = pDPIpacket(packet)
The pDPIpacket() function parses a DPI packet and returns a parse tree representing its contents. The parse tree is dynamically allocated and contains copies of the information within the DPI packet. After a successful call to pDPIpacket(), the packet may be disposed of in any manner the application chooses without affecting the contents of the parse tree. If an error is encountered during the parse, the null pointer (0) is returned.
NOTE: the relevant parse tree structures are defined in the include file "snmp_dpi.h", and that file remains the definitive reference.
The root of the parse tree is represented by a snmp_dpi_hdr structure:
struct snmp_dpi_hdr {
unsigned char proto_major;
unsigned char proto_minor;
unsigned char proto_release;
unsigned char packet_type;
union {
struct dpi_get_packet *dpi_get;
struct dpi_next_packet *dpi_next;
struct dpi_set_packet *dpi_set;
struct dpi_resp_packet *dpi_response;
struct dpi_trap_packet *dpi_trap;
} packet_body;
};
The field of immediate interest is packet_type. This field can have one of the following values (which are defined in the include file "snmp_dpi.h"):
- SNMP_DPI_GET
- SNMP_DPI_GET_NEXT
- SNMP_DPI_SET
The packet_type field indicates what request is being made of the DPI client. For each of these requests, the remainder of the packet_body will be different.
If a get request is indicated, the object ID of the desired variable is passed in a dpi_get_packet structure:
struct dpi_get_packet {
char *object_id;
};
A get-next request is similar, but the dpi_next_packet structure also contains the object ID prefix of the group that is currently being traversed:
struct dpi_next_packet {
char *object_id;
char *group_id;
};
If the next object whose object ID lexicographically follows the object ID indicated by object_id does not begin with the suffix indicated by group_id, the DPI client must return an error indication of SNMP_NO_SUCH_NAME.
A set request has the most amount of data associated with it and this is contained in a dpi_set_packet structure:
struct dpi_set_packet {
char *object_id;
unsigned char type;
unsigned short value_len;
char *value;
};
The object ID of the variable to be modified is indicated by object_id The type of the variable is provided in type and may have one of the following values:
- SNMP_TYPE_NUMBER
- SNMP_TYPE_STRING
- SNMP_TYPE_OBJECT
- SNMP_TYPE_EMPTY
- SNMP_TYPE_INTERNET
- SNMP_TYPE_COUNTER
- SNMP_TYPE_GAUGE
- SNMP_TYPE_TICKS
The length of the value to be set is stored in value_len and value contains a pointer to the value.
NOTE: the storage pointed to by value will be reclaimed when the parse tree is freed. The DPI client must make provision for copying the value contents.
FDPIPARSE
#include "snmp_dpi.h"
struct snmp_dpi_hdr *hdr;
fDPIparse(hdr);
The routine fDPIparse() frees a parse tree previously created by a call to pDPIpacket This routine is declared as void--it has no return value.
NOTE: after calling fDPIparse(), no further references to the parse tree can be made.
Agent Implementation Issues
Although the SNMP DPI protocol is completely documented in this paper, the document itself is somewhat biased towards clearly defining the interface provided to sub-agents (i.e., it provides a specification of a C language API). This detailed coverage is possible because the client side of the interface is completely self-contained.
The agent side of the interface has to be integrated into individual vendor implementations, many of which may have a unique organizational structure in an attempt to address various performance and storage constraints. This makes it infeasible to provide much more than suggestions for SNMP agent implementers. Unfortunately, this leaves room for a large amount of interpretation which can lead to implementations that don't necessarily work they way they should--too much ambiguity can be a bad thing.
The following characteristics of an agent implementation are to be considered mandatory:
Duplicate Registrations
With this release of the protocol, order of registration is significant. The last sub-agent to register a variable is the one that is deemed to be authoritative. Variables implemented by the base SNMP agent are considered to have been registered prior to any sub-agent registrations. Thus sub-agents may re-implement support for variables that were incorrectly implemented by a vendor.
Automatic Deregistration on Close
All SNMP DPI connections are carried over a stream connection. When the connection is closed by the client (no matter what the cause), the agent must automatically unregister all of the variables that were registered by the sub-agent.
Timely Response Constraints
A sub-agent must respond to a request in a timely fashion. In this version of the protocol, we specify that a sub-agent must respond to a request by the SNMP agent within 5 seconds. If the sub-agent does not respond in time, the SNMP agent should terminate the connection and unregister all of the variables that were previously registered by the sub-agent in question.
NOTE: agent implementations that do not have access to a timer may not be able to implement this. In that case, they leave themselves open to being placed in a state where they are blocked forever if the sub-agent malfunctions.
Support for Multiple MIB Views
Some agents allow different MIB views to be selected based on the community name used. It is not the intention of this document to pass judgement on the various approaches that have been proposed or implemented, but instead merely to recognize the existence of implementations that support this feature.
The point of this discussion is to specify clearly that objects supported by an SNMP DPI sub-agent are to be registered under the MIB view that was selected by the community name used in the SNMP GET request that obtained the DPI_port value.
The SNMP DPI does not specify a reserved port, but instead sub-agents bootstrap themselves by making an SNMP GET request for the DPI_port variable. This variable represents the TCP port to which the sub-agent should connect. It should be understood that there is no reason why the SNMP agent cannot have several listens (passive opens) active, each corresponding to a distinct MIB view. The port number returned then would be different based on the community name used in the SNMP GET request for the DPI_port variable.
Considerations for the Next Release
The SNMP DPI protocol makes provision for extension and parallel use of potentially incompatible releases. The discussion above documents the protocol as it is currently in use and has not discussed features of interest that should be considered for a future revision.
UNREGISTER
For closure, an UNREGISTER request could be of use.
Support for Atomic Sets
The SNMP protocol [1] specifies that:
Each variable assignment specified by the SetRequest-PDU should be
effected as if simultaneously set with respect to all other
assignments specified in the same message.
The SNMP DPI has no provision for backing out a successfully processed SET request if one of the subsequent variable assignments fails. This omission is a reflection of several biases:
-
the SNMP DPI was intended to be light-weight.
-
a belief that the SNMP RFC prescribes semantics which are infeasible to implement unless the range of applications is restricted.
It has been suggested that a new request, TEST_SET, be added to the DPI protocol. Processing of a SET request would then be performed as follows:
-
all variables would be processed using TEST_SET unless any error occurred. The subagents would verify that they could process the request.
-
if no error occurred, each of the variables would be reprocessed, this time with a SET request.
A problem with such an approach is that it relies on the TEST_SET operation to make an assertion that the request can be successfully performed. If this is not possible, then it cannot be asserted that the prescribed semantics will be provided. Such situations do exist, for example, a SET request that causes the far-end channel service unit to be looped up--one does not know if the operation will be successful until it is performed.