SNMP is implemented by VC 6.0, SNMP (Simple Network Management Protocol) is a simple network management protocol, mainly used to manage network devices, because "simple", so it is very fast, so far, almost all network products To provide support to facilitate the development of administrators' management and software developers. Here we don't explain too much about the concepts and basic knowledge of SNMP, and assume that you have a certain understanding of its working principles, and how only how to implement him. What we will explain is based on SNMP programming under VC6.0. Detailed to introduce the process of SNMP programming and the usage of the API function. In the part of the difficult to understand, I will explain with a large space. The collapse is correct, enter our theme. First explain several important concepts: 1. Community: If translated can be difficult to understand, in fact, you can understand it as a login account with permissions, this is an important credentials you visit network equipment, such as you have to access the switch, if the switch Community is public, whose permissions are read-only, then you can view the data on the switch record at a time you log in. If its permissions are read or written, you have the right to modify some of these settings, such as blocking ports of a switch. Most switches By default, as public as read-only Community, use private as a read and write Community. 2. OID (Object Narrator): It is a series of dot divisions based on SMI (Structure Of Management Information) management information structure, such as 1.3.6.1.2.1.1.1, these point symbols uniquely identify one in any network device A data parameter. Their collection we call the MIB (Management Information Base) management information library. For the meaning they identifies, the reader can check online, provide a simple search method, you can enter Google, enter the 1.3.6.1.2.1.1 and other strings directly. Let's enter our topish. Like other programming processes, the entire SNMP programming also has to create, execute, destroy process, popular point, is to prepare, initialize the SNMP environment, load SNMP, then execute the operation, SNMP is based on Message mechanism, so message delivery and management is the problem we must pay attention to in programming, and finally destroy and recycle resources, this is believed that programmers will notice. Let's give a detailed introduction by step: 1. SNMP is loaded, the function used is SnmpStartup (smiLPUINT32 nMajorVersion, smiLPUINT32 nMinorVersion, smiLPUINT32 nLevel, smiLPUINT32 nTranslateMode, smiLPUINT32 nRetransmitMode); five parameters as the SNMP receive parameters returned major version number, minor version number, the highest operating standards support, default Entity / context transmission mode, the default resend mechanism.
2. A session is established, the function used is HSNMP_SESSION SnmpOpen (HWND hWnd, // handle to the notification window UINT wMsg // window notification message number); or HSNMP_SESSION SnmpCreateSession (HWND hWnd, // handle to the notification window UINT wMsg, // window notification message number SNMPAPI_CALLBACK pfnCallBack, // notification callback function LPVOID lpClientData // pointer to callback function data); the second function has not been completely settled, he just provides a programmer during programming windows-based programming is not Selection, the first parameter points to the window of the received message, the second parameter points to the message code that needs to receive. This function returns a session sentence Bing, this sentence is an important variable to be used in our following procedures. 3. Set the transfer mode, the function used is SNMPAPI_STATUS SnmpSetTranslateMode (smiUINT32 nTranslateMode // new entity / context translation mode); This function has only one parameter, have the following options: SNMPAPI_TRANSLATED not used SNMPAPI_UNTRANSLATED_V1 release version V2 V1 SNMPAPI_UNTRANSLATED_V2 you can select either a Parameters, I have used the second and third parameters, the difference is that the operation of GET_BULK in the version is not allowed, because the version does not support this operation, the specific details can be found, recommend a book " SNMP management internet network. " As for the first parameter I have not been experimented, I can't make a decompression.
4. Creating an entity, the function used is hsnmp_entity snmpstertoentity (hsnmp_session session, // handle to the winsnmp session lpcstr string //point to a string "iDentifies // the entry); the first parameter of this function is the second step back Session Sentence, the second parameter is related to the transfer mode you set in the third step, if you have two parameters, then the String here is the network device IP address or the management device for receiving messages. IP address. According to your own needs, usually we create these two entities. This function returns an entity. 5. Set the retransmission mode, the function used is SNMPAPI_STATUS SNMPSetRetransmitmode (SMIUINT32 NRETRANSMIDE // NEW RETRANSMISSION MODE); the function has only one parameter, there are two options SNMPAPI_ON boot retransmatherapy mode SNMPAPI_OFF shut down retransmission mode
6. Set the timeout, the function used is SNMPAPI_STATUS SNMPSETTIMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOMEOUS SMITIMETIMEOUT (HSNMP_ENTITY ", // Destination Management Entity SmitiMETIMETYTIMEOUT // NEW TIME-OUT VALUE for Database); the first parameter of the function is the first entity returned by the fourth step. Usually we set the timeout time of the target entity, that is, the entity of the network device that receives the message. The second parameter is timeout.
7. Set the number of retransmission, the function used is SNMPAPI_STATUS SNMPSetRETRY (HSNMP_ENTINTITY, / / DESTINMP_ENTITY HENTITY SMIUINT32 NPOLicyRETRY // New Retry Count Value for Database); the first parameter of this function is the entity story of the fourth step back. Usually we set the number of retransmissions of the target entity, that is, the entity of the network device that receives the message. The second parameter is the number of retransmissions.
8. Create a top and down text message, the function used is hsnmp_context snmpstocontext (hsnmp_session session, // handle to the winsnmp session smilpcock structure); the first parameter of the function is the second step back to the session The second parameter is related to the transfer mode you set in the third step. If you choose two parameters, then the String here is a common name. This function returns a context. From this, we have reached three important sentences, summarize: 1. The session is Bing, 2. Entity Senior, 3. The context is Bing, remember these three important sentences because they are always used during the SNMP programming process, only released after the end.
9. Create a variable binding list function is used HSNMP_VBL SnmpCreateVbl (HSNMP_SESSION session, // handle to the WinSNMP session smiLPCOID name, // pointer to the variable name smiLPCVALUE value // pointer to the value to associate // with the variable); This is a more embarrassing function, you must have an in-depth understanding of it, you have to know about the SNMP's datagram, here I can't do more, you can give you an example, like a train He is only responsible for the destination. All the car is empty, each compartment has a number, the character of the person who is launched, is better than the first carriage, the second car should pick up the leadership, and so on. The head was driven by the driver. Of course he knew his destination, all the way twice, finally arrived at the destination, there is no one in the car, the destination webmaster must first check the driver's documents and instruments, see him Have you had the right to pick out this batch, if not, you will take a notification official in the first car, his responsibility is to return to the Souvenway, the company said that the driver has no power to pick up. If there is, then the webmaster wants to see if the various cars meet the requirements of this site. If it is in line, it will take different people to different cars. If there is a request for change, then you want to see if this site is allowed, if allow, change the firm person who meets the requirements of these stations. Here we can regard each car as a different variable, the space of the carriage is used to receive people, then people with different characteristics are returning data. This kind of compartment is better than a binding, and SNMP's data rendering is a plurality of bindings in this way. The carriage number is better than the object sign. Of course, only the first is special, because he has a head, so use the snmpcreatevbl function, other bindings we need to use SNMPSetVB to connect it to the previous car. Ok, by the above, we can know that the first parameter of this function is the session sentence returned by the second step, while the other two parameters can be set off. This function returns a binding list.
10. Additional binding list, use the function is SNMPAPI_STATUS SnmpSetVb (HSNMP_VBL vbl, // handle to the variable bindings list smiUINT32 index, // position of the variable binding entry // in the list smiLPCOID name, // pointer to the variable name Portion // of the entry smilpcval value // Pointer to the variable value portion // of the entry); We can see that the latter two parameters of this function are the same as SNMPCREATEVBL, which is the binding of the compartments and space. The first parameter is hsnmp_vbl, a bind list sentence Bing, which can also be understood because we have created a binding list, the second parameter is a variable binding index, think about it, so many cars, always have Let's not be busy, you will make an obvious question, if I only create a first binding list, I want to add other binding lists to it, then what is the use of the index value? I can add it in order. Just like a queue. Completely correct! In fact, this function is also true. When we want to add variable binding, we must set the index value to 0. This index value is only used when we implement such a SET command, which will be described below. Once, it is very smooth, but if you don't have SNMP programming, you will be confused, one is smilpcoid, and there is SmilpcValue, don't worry, first look at the first structure, you will find there You will be familiar with one English, because I introduced him in the foregoing, it is OID in smilpcoid, yes, what he means to object symbols, LP If you use VC programming, you will know that most of them is a pointer. Yes, he is a pointer type pointing to SmioID.
Let's take a look at his internal construct: typef struct {smiant32 gen; // number of array elements smilpuint32 ptr; // pointer to an array s subidentifier} smioid, * smilpoid; there are two types, the first one is used to specify him How many numbers, the second point to a one-dimensional array, give an example, if an object flag is 1.3.6.1.2.1.1.1, then the Len should be 8, PTR should point to an array, the array of elements 1, 3, 6, 1, 2, 1, 1, 1, seems to "." Nothing, correct, because in SNMP, you will think, how can you construct such a structure? Very simple, SNMP API function provides us with convenience, you can define a string such as "1.3.6.1.2.1.1.1", with SNMPAPI_STATUS SNMPSTRTOID (LPCSTR STRING, // String Object Identifier To Convert Smilpoid Dstoid // Object Identifier INTERNAL REPRESENTATION; function to transform, see two parameters, is it very energetic. Of course, this conversion is reversible, you can use SNMPOIDTOSTR, specifically check the MSDN. Let's talk about SMILPCVALUE, by the above reason, you can give him a pointer type pointing to SMIValue. Let's take a look at the SMIVALUE structure, this structure is more complicated, I tried to tell some.
typedef struct {// smiVALUE portion of VarBind smiUINT32 syntax; // Insert SNMP_SYNTAX_
Let's take a look at the work we have finished. So far we have four sentences, 1. The session is Bing, 2. Entity Senior, 3. Bing, 4. Bind list Shu Bing. Two structures have also been introduced. These two structures and variable bindings are related. Now the train and space of the train have already, the front of the front is also there, and I will give him a wheel. I find a good driver, I need him to remember my mission. 11. To send the data correctly to the destination, we must send it in a specific format. If you know the programmers of the IP protocol, you don't need to explain too much. We use the function HSNMP_PDU SnmpCreatePdu (HSNMP_SESSION session, // handle to the WinSNMP session smiINT PDU_type, // PDU type smiINT32 request_id, // PDU request identifier smiINT error_status, // valid only for SNMP_PDU_GETBULK requests smiINT error_index, // valid only for SNMP_PDU_GETBULK Requests hsnmp_vbl varbindlist // handle to the variable bindings list); to complete this feature. The first and last parameters are the session 炳,, 参 参 参 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 方式 选 选 选 选 选 选 选 选 如SNMP_PDU_SET SNMP_PDU_V1TRAP SNMP_PDU_GETBULK SNMP_PDU_TRAP
For these operations, I suggest that the reader is best to find some books to see, here I only explain some of the operations, SNMP_PDU_GET is usually used to obtain a value corresponding to a particular object flag, SNMP_PDU_GETNEXT is in the programmer Understand the operation of this list of information to get a set of values. SNMP_PDU_RESPONSE is typically filled in to the SNMP agent, indicating a datagram that answers a request. SNMP_PDU_SET is an operation used to change the value of an object flag. SNMP_PDU_GETBULK can only be used above V2, is used to solve the SNMP_PDU_GETNEXT once a message can only achieve a disadvantage of a data, and a set of data can be obtained by sending a message. SNMP_PDU_V1TRAP and SNMP_PDU_TRAP use the operation of sending a message. The third parameter request_ID, there is almost no effect on the programming of the message mechanism, but for asynchronous operations, this parameter has a very important role, you can use it to sign a request for a message, if there are several messages Both in the message queue, you can determine what you want to handle, this value can be set yourself. ERROR_STATUS and ERROR_INDEX specify a value for the Non_Repeaters field in the PDU in the SNMP_PDU_GETBULK operation, respectively. There are 0 in other operations. This function returns a PDU sentence. Everything is ready, let our train caught it. Use the following function 12. SNMPAPI_STATUS SnmpSendMsg (HSNMP_SESSION session, // handle to the WinSNMP session HSNMP_ENTITY srcEntity, // handle to the source entity HSNMP_ENTITY dstEntity, // handle to the target entity HSNMP_CONTEXT context, // handle to the context HSNMP_PDU PDU // handle to the PDU ); Look at these parameters, is it all created, add them.
The above is the entire sending process, let's take a smooth, 1. Load SNMP, 2. Establish a session, 3. Set the transfer mode, 4. Create an entity, 5. Set the retransmission mode, 6. Set timeout, 7. Set the retransmission number, 8. Create a top and bottom text sentence Bing, 9. Create a variable bundle list, 10. Add a list of bindings, 11. Create a PDU, 12. send messages. Of course, if you only have to get a data, then the 10th step doesn't need it.
Next we have to receive the message and handle them. 1. Receiving a message, a function SNMPAPI_STATUS SnmpRecvMsg (HSNMP_SESSION session, // handle to the WinSNMP session LPHSNMP_ENTITY srcEntity, // handle to the source entity LPHSNMP_ENTITY dstEntity, // handle to the target entity LPHSNMP_CONTEXT context, // handle to the context LPHSNMP_PDU PDU / / Handle to the PDU; declare, the parameter and snmpsendmsg of this function seem to be the same, good, but the parameters of the parameters are different, and the SNMPRecvmsg is except for the first parameters. Other parameters are output parameters, It is used to receive the parameters, it seems to be very cool, because only one parameter is set, the other declarations a variable, just receive it. Think carefully, it's quite right! 2. Extracting data packets, a function SNMPAPI_STATUS SnmpGetPduData (HSNMP_PDU PDU, // handle to the PDU smiLPINT PDU_type, // PDU_type field of the PDU smiLPINT32 request_id, // request_id field of the PDU smiLPINT error_status, // error_status field of the PDU smiLPINT error_index , // Error_index Field of the PDU LPHSNMP_VBL VARBINDLIST / / HANDLE TO The Variable Bindings List); It is also very cool, only the first parameter is required to be entered, and this has been obtained by snmprecvmsg, and other parameters need to receive , What is seen, right, request_id, if you receive asynchronous reception, it can be very important, you can help you identify the message sent. And error_status and error_index, remember, use in snmp_pdu_getbulk operations, their meaning is different, in addition to this, they are used to receive the error message returned by the SNMP side, if the return is 0, that is, returned correctly, If not, then you will check what they mean, and the general SNMP book will be introduced.
3. Calculate the number of lists, use functions snmpapi_status snmpcountvbl (hsnmp_vbl vbl // Handle to the variable bindings list); will go to the Varbindlist to get the last VarbindList, his return is just a whole, so that the variable you get The number of variables returned by the bind list. 4. Get reverse the result, with the function SNMPAPI_STATUS SnmpGetVb (HSNMP_VBL vbl, // handle to the variable bindings list smiUINT32 index, // position of the variable binding entry // in the list smiLPOID name, // pointer to the structure to receive the // Variable name smilpvalue value // Pointer to the structure to receive the // associated value); Since the previous step has been obtained, take out the result with a simple for loop. The function has four parameters, the first one is got in the third step, the second is the variable value in your for loop, remember that the variable starts from 0, and the next two parameters think that it is a front. The parameters of a function are a bit similar. By the way, we all set them empty. Now the SNMP agent adds the return value to it. We can enjoy it, define two variables, and receive it. Remind, the reception of the IP address will be a bit different, because the return value is packaged as a pointer number, you need one to take it out!