Building the Sample Visual C++ SNMP ManagerIf 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.
The purpose of these files is now briefly described:
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.exeThe 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 *objectIdentifier, char *objectValue); 20 int prepareDataForOperation(enum Operations reqOperation, unsigned char *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, char *objectIdentifier, char *objectValue); 29 int executeMibWalk(); 30 int waitForTraps(); 31 int startupRoutine (int argc, char *argv []); Snmpdefs.h Lines 1 to 5Section 1 provides five symbolic constants for SNMP operations, comprised of the following:
The SNMP API uses the first three constants. Snmpdefs.h Lines 6 to 16Section 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:
Snmpdefs.h Lines 17 to 31Section 3 lists the available function calls and is comprised of the following:
|