Building the Sample Visual C SNMP Manager


Building the Sample Visual C++ SNMP Manager

If it is required to build the Visual C++ program, then the files in Figure 7-1 should be downloaded from the Prentice Hall Web site [PrenHallCodeWeb]:

Figure 7-1. The Visual C++ SNMP Manager files.

graphics/07fig01.jpg

The purpose of these files is now briefly described:

  1. Get.bat executes a single SNMP GET operation against the agent.

  2. Set.bat executes a single SNMP SET operation against the agent.

  3. Walk.bat walks the contents of a specified agent MIB table.

  4. Gettraps.bat prepares the program for receiving SNMP traps.

  5. Getnext.bat executes a single SNMP getNext operation against the agent.

  6. snmpdefs.h is a header file containing data types, global variables , and function declarations.

  7. snmpmgr.c contains the C code for the program.

  8. snmpmgr.exe contains the executable file for the program.

The program snmpmgr.exe was built using Microsoft Visual C++ Version 6.0. To build the source code, the simplest method is to search Visual C++ for an SNMP example program called snmputil . Our example was originally based on this Microsoft sample program (our source code is a completely rewritten version). Open the Microsoft example and delete the C and header files (one of each), and then select Add Files and choose our two files ( snmpmgr.c and snmpdefs.h ). Change the target executable to snmpmgr.exe . The project should then build successfully.

To run the example (without building it), copy snmpmgr.exe into a directory called debug located immediately underneath the location of the above files. If the program is built using Visual C++, then the debug directory should be created automatically along with the target executable. In either case, the batch files should run successfully.

Our two files snmpdefs.h and snmpmgr.c are now described.

The Source Code Components of snmpmgr.exe

The files snmpdefs.h and snmpmgr.c provide the codebase for our elementary management system. We now describe the principal elements of this program: The source code listing is contained in Appendix D, "Visual C++ Sample Program Source C." The contents of the header file snmpdefs.h are illustrated in Figure 7-2. We added line numbers and section headings to make it easier to follow the description that follows . The header file snmpdefs.h is made up of three sections.

Figure 7-2 snmpdefs.h : The API for our rudimentary management system.
[View full width]
 /****************** SECTION 1 ******************/ 1  #define TIMEOUT 6000 /* milliseconds */ 2  #define RETRIES 3 3  #define MAX_OID_NAME_LENGTH 50 4  #define FUNCTION_SUCCESS 0 5  #define FUNCTION_FAILED -1 /****************** SECTION 2 ******************/ 6  enum Operations { GET, GETNEXT, SET, WALK, TRAP }; 7  char * operationsArray[5] = { "GET", "GETNEXT", "SET", "WALK", "TRAP" }; 8  INT programMode, operation; 9  LPSTR SNMPAgentName, SNMPCommunity, OIDstring; 10 RFC1157VarBindList variableBindings; 11 LPSNMP_MGR_SESSION SNMPsession; 12 AsnAny retrievedInstanceValue; 13 INT timeout = TIMEOUT; 14 INT retries = RETRIES; 15 BYTE requestType; 16 AsnInteger errorStatus, errorIndex; /****************** SECTION 3 ******************/ 17 int allocateResources(LPSTR agentName, LPSTR community, char *objectIdentifier); 18 int deallocateResources(); 19 int prepareForOp(enum Operations reqOperation, LPSTR agentName, LPSTR community, char graphics/ccc.gif *objectIdentifier, char *objectValue); 20 int prepareDataForOperation(enum Operations reqOperation, unsigned char graphics/ccc.gif *newObjectValue); 21 int prepareSetOperation(unsigned char *newObjectValue); 22 int prepareGetOperation(); 23 int prepareGetNextOperation(); 24 int dispatchOperation(int programMode, char * argumentVector[]); 25 int createSNMPSession(); 26 int executeRequest(); 27 int displayMIBInstanceValue(); 28 int doSnmpOperation(enum Operations reqOperation, LPSTR agentName, LPSTR community, graphics/ccc.gif char *objectIdentifier, char *objectValue); 29 int executeMibWalk(); 30 int waitForTraps(); 31 int startupRoutine (int argc, char *argv []); 
Snmpdefs.h Lines 1 to 5

Section 1 provides five symbolic constants for SNMP operations, comprised of the following:

  • Line 1 : Timeouts occur when an operation (such as a GET ) is executed and for some reason no response is received. There are many reasons for this; for example, the agent or network may be down, or the message may have been rejected due to incorrect security information. Timeouts are used to allow the manager to close down an operation rather than wait indefinitely.

  • Line 2 : Retries are needed for situations in which a message is lost or rejected due to some temporary problem. An example is a busy agent. Another is a lost message ”remember that SNMP uses UDP, an unreliable datagram service. Typically, retries work in conjunction with timeouts, as we'll see in Figure 7-11.

  • Line 3 : Maximum length permitted for an object identifier.

  • Lines 4 “5 : Function success and failure codes.

The SNMP API uses the first three constants.

Snmpdefs.h Lines 6 to 16

Section 2 provides global variables. It is not necessary for many of these to have global scope, but they are made global in this example for simplicity. Some of the data objects are specific to the Microsoft SNMP API:

  • Line 6 : Operation indicator to store the SNMP operation type ( GET , SET , etc.).

  • Line 7 : Operation array to store the SNMP operation name , ( GET , SET , etc.).

  • Line 8 : Program mode integer.

  • Line 8 : Program operation integer.

  • Line 9 : String for storing the remote SNMP agent IP address or DNS name.

  • Line 9 : String for storing the SNMP community name.

  • Line 9 : String for storing the specified object identifier.

  • Line 10 : Container for the SNMP PDU variable bindings.

  • Line 11 : SNMP session pointer.

  • Line 12 : Container for retrieved objects.

  • Line 13 : Timeout global variable.

  • Line 14 : Global variable for storing the number of permitted retries.

  • Line 15 : Request type global variable for storing the required SNMP operation type.

  • Line 16 : SNMP error indicator ” tells us if an error or exception occurred.

  • Line 16 : SNMP error offset value ”tells us the variable bindings offset with which the problem is associated.

Snmpdefs.h Lines 17 to 31

Section 3 lists the available function calls and is comprised of the following:

  • Line 17 : allocateResources() dynamically allocates required memory, creates an SNMP session, and sets up the variable bindings structure for the SNMP operations.

  • Line 18 : deallocateResources() deallocates memory, destroys the SNMP session object, and frees up the variable bindings structure.

  • Line 19 : prepareForOp() calls the allocateResources function and prepares for the specific operation ( GET , GETNEXT , etc.).

  • Line 20 : prepareDataForOperation() sets up program mode for the required SNMP operation.

  • Line 21 : prepareSetOperation() indicates to the API to expect a SET operation and populates a variable bindings object ready for transmission to a remote SNMP agent as part of a SET operation.

  • Line 22 : prepareGetOperation() indicates to the API to expect a GET operation.

  • Line 23 : prepareGetNextOperation() indicates to the API to expect a GETNEXT operation.

  • Line 24 : dispatchOperation() issues the final call to the API for the required operation.

  • Line 25 : doSnmpOperation() makes the operation-specific call.

  • Line 26 : createSNMPSession() opens an SNMP session with the remote agent.

  • Line 27 : executeRequest() is the final port of call ”the SNMP API is entered, and SET / GET message traffic is sent across the network.

  • Line 28 : displayMIBInstanceValue() presents the retrieved data to the user , showing the operation type, data type, and value.

  • Line 29 : executeMibWalk() carries out a lexicographic MIB walk using the supplied OID as the root.

  • Line 30 : waitForTraps() prepares the program for receiving incoming traps and displays them as they occur.

  • Line 31 : startupRoutine() parses the command line for program mode and any required parameter strings.



Network Management, MIBs and MPLS
Network Management, MIBs and MPLS: Principles, Design and Implementation
ISBN: 0131011138
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net