// Main program. INT _CRTAPI1 main( IN int argumentCount, IN char *argumentVector[]) { INT operation; LPSTR agent = NULL; LPSTR community = NULL; RFC1157VarBindList variableBindings; LPSNMP_MGR_SESSION session = NULL; INT timeout = TIMEOUT; INT retries = RETRIES; BYTE requestType; AsnInteger errorStatus; AsnInteger errorIndex; // Parse command line arguments to determine requested operation. // Verify number of arguments... if (argumentCount < 5 && argumentCount != 2) { printf("Error: Incorrect number of arguments specified.\n"); printf( "\nusage: snmputil [get|getnext|walk] agent community oid [oid ...]\n"); printf( " snmputil trap\n"); return 1; } // Get/verify operation... argumentVector++; argumentCount--; if (!strcmp(*argumentVector, "get")) operation = GET; else if (!strcmp(*argumentVector, "getnext")) operation = GETNEXT; else if (!strcmp(*argumentVector, "walk")) operation = WALK; else if (!strcmp(*argumentVector, "trap")) operation = TRAP; else { printf("Error: Invalid operation, '%s', specified.\n", *argumentVector); return 1; } if (operation != TRAP) { if (argumentCount < 4) { printf("Error: Incorrect number of arguments specified.\n"); printf( "\nusage: snmputil [get|getnext|walk] agent community oid [oid ...]\n"); printf( " snmputil trap\n"); return 1; } // Get agent address... argumentVector++; argumentCount--; agent = (LPSTR)SnmpUtilMemAlloc((UINT)(strlen(*argumentVector) + 1)); if (agent != NULL) StringCchCopyA(agent, strlen(*argumentVector) + 1, *argumentVector); else { printf("Error: SnmpUtilMemAlloc failed to allocate memory.\n"); return 1; } // Get agent community... argumentVector++; argumentCount--; community = (LPSTR)SnmpUtilMemAlloc((UINT)(strlen(*argumentVector) + 1)); if (community != NULL) StringCchCopyA(community, strlen(*argumentVector) + 1, *argumentVector); else { printf("Error: SnmpUtilMemAlloc failed to allocate memory.\n"); SnmpUtilMemFree(agent); return 1; } // Get oid's... variableBindings.list = NULL; variableBindings.len = 0; while(--argumentCount) { AsnObjectIdentifier reqObject; RFC1157VarBind * tmpVb; argumentVector++; // Convert the string representation to an internal representation. if (!SnmpMgrStrToOid(*argumentVector, &reqObject)) { printf("Error: Invalid oid, %s, specified.\n", *argumentVector); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilVarBindListFree(&variableBindings); return 1; } else { // Since sucessfull, add to the variable bindings list. variableBindings.len++; if ((tmpVb = (RFC1157VarBind *)SnmpUtilMemReAlloc( variableBindings.list, sizeof(RFC1157VarBind) * variableBindings.len)) == NULL) { printf("Error: Error allocating oid, %s.\n", *argumentVector); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilOidFree(&reqObject); variableBindings.len--; SnmpUtilVarBindListFree(&variableBindings); return 1; } variableBindings.list = tmpVb; variableBindings.list[variableBindings.len - 1].name = reqObject; // NOTE! structure copy variableBindings.list[variableBindings.len - 1].value.asnType = ASN_NULL; } } // end while() // Make sure only one variable binding was specified if operation // is WALK. if (operation == WALK && variableBindings.len != 1) { printf("Error: Multiple oids specified for WALK.\n"); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilVarBindListFree(&variableBindings); return 1; } // Establish a SNMP session to communicate with the remote agent. The // community, communications timeout, and communications retry count // for the session are also required. if ((session = SnmpMgrOpen(agent, community, timeout, retries)) == NULL) { printf("error on SnmpMgrOpen %d\n", GetLastError()); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilVarBindListFree(&variableBindings); return 1; } } // end if(TRAP) // Determine and perform the requested operation. if (operation == GET || operation == GETNEXT) { // Get and GetNext are relatively simple operations to perform. // Simply initiate the request and process the result and/or // possible error conditions. if (operation == GET) requestType = ASN_RFC1157_GETREQUEST; else requestType = ASN_RFC1157_GETNEXTREQUEST; // Request that the API carry out the desired operation. if (!SnmpMgrRequest(session, requestType, &variableBindings, &errorStatus, &errorIndex)) { // The API is indicating an error. printf("error on SnmpMgrRequest %d\n", GetLastError()); } else { // The API succeeded, errors may be indicated from the remote // agent. if (errorStatus > 0) { printf("Error: errorStatus=%d, errorIndex=%d\n", errorStatus, errorIndex); } else { // Display the resulting variable bindings. UINT i; for(i=0; i < variableBindings.len; i++) { char *string = NULL; if (SnmpMgrOidToStr(&variableBindings.list[i].name, &string)) { printf("Variable = %s\n", string); if (string) SnmpUtilMemFree(string); } printf("Value = "); SnmpUtilPrintAsnAny(&variableBindings.list[i].value); printf("\n"); } // end for() } } // Free the variable bindings that have been allocated. SnmpUtilVarBindListFree(&variableBindings); // Free other allocated resources SnmpUtilMemFree(agent); SnmpUtilMemFree(community); } else if (operation == WALK) { // Walk is a common term used to indicate that all MIB variables // under a given OID are to be traversed and displayed. This is // a more complex operation requiring tests and looping in addition // to the steps for get/getnext above. AsnObjectIdentifier root; AsnObjectIdentifier tempOid; if (!SnmpUtilOidCpy(&root, &variableBindings.list[0].name)) { printf("error on SnmpUtilOidCpy\n"); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilVarBindListFree(&variableBindings); return 1; } requestType = ASN_RFC1157_GETNEXTREQUEST; while(1) { if (!SnmpMgrRequest(session, requestType, &variableBindings, &errorStatus, &errorIndex)) { // The API is indicating an error. printf("error on SnmpMgrRequest %d\n", GetLastError()); break; } else { // The API succeeded, errors may be indicated from the remote // agent. // Test for end of subtree or end of MIB. if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME || SnmpUtilOidNCmp(&variableBindings.list[0].name, &root, root.idLength)) { printf("End of MIB subtree.\n\n"); break; } // Test for general error conditions or sucesss. if (errorStatus > 0) { printf("Error: errorStatus=%d, errorIndex=%d \n", errorStatus, errorIndex); break; } else { // Display resulting variable binding for this iteration. char *string = NULL; if (SnmpMgrOidToStr(&variableBindings.list[0].name, &string)) { printf("Variable = %s\n", string); if (string) SnmpUtilMemFree(string); } printf("Value = "); SnmpUtilPrintAsnAny(&variableBindings.list[0].value); printf("\n"); } } // end if() // Prepare for the next iteration. Make sure returned oid is // preserved and the returned value is freed. if (!SnmpUtilOidCpy(&tempOid, &variableBindings.list[0].name)) { printf("error on SnmpUtilOidCpy\n"); SnmpUtilOidFree(&root); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilVarBindListFree(&variableBindings); return 1; } SnmpUtilVarBindFree(&variableBindings.list[0]); if (!SnmpUtilOidCpy(&variableBindings.list[0].name, &tempOid)) { printf("error on SnmpUtilOidCpy\n"); SnmpUtilOidFree(&tempOid); SnmpUtilOidFree(&root); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); SnmpUtilVarBindListFree(&variableBindings); return 1; } variableBindings.list[0].value.asnType = ASN_NULL; SnmpUtilOidFree(&tempOid); } // end while() // Free the variable bindings that have been allocated. SnmpUtilVarBindListFree(&variableBindings); // Free other allocated resources SnmpUtilOidFree(&root); SnmpUtilMemFree(agent); SnmpUtilMemFree(community); } else if (operation == TRAP) { // Trap handling can be done two different ways: event driven or // polled. The following code illustrates the steps to use event // driven trap reception in a management application. HANDLE hNewTraps = NULL; if (!SnmpMgrTrapListen(&hNewTraps)) { printf("error on SnmpMgrTrapListen %d\n", GetLastError()); return 1; } else { printf("snmputil: listening for traps...\n"); } while(1) { DWORD dwResult; if ((dwResult = WaitForSingleObject(hNewTraps, INFINITE)) == WAIT_FAILED) { printf("error on WaitForSingleObject %d\n", GetLastError()); } else if (dwResult != WAIT_OBJECT_0) { printf("hNewTraps is not signaled\n"); } else if (!ResetEvent(hNewTraps)) { printf("error on ResetEvent %d\n", GetLastError()); } else { AsnObjectIdentifier enterprise; AsnNetworkAddress agentAddress; AsnNetworkAddress sourceAddress; AsnInteger genericTrap; AsnInteger specificTrap; AsnOctetString communityTrap; AsnTimeticks timeStamp; RFC1157VarBindList variableBindingsTrap; UINT i; while(SnmpMgrGetTrapEx( &enterprise, &agentAddress, &sourceAddress, &genericTrap, &specificTrap, &communityTrap, &timeStamp, &variableBindingsTrap)) { LPSTR string = NULL; printf("Incoming Trap:\n" " generic = %d\n" " specific = %d\n" " timeStamp = %u\n", genericTrap, specificTrap, timeStamp); if (SnmpMgrOidToStr(&enterprise, &string)) { printf (" enterprise = %s\n", string); if (string) SnmpUtilMemFree(string); } SnmpUtilOidFree(&enterprise); if (agentAddress.length == 4) { printf (" agent = %d.%d.%d.%d\n", (int)agentAddress.stream[0], (int)agentAddress.stream[1], (int)agentAddress.stream[2], (int)agentAddress.stream[3]); } if (agentAddress.dynamic) { SnmpUtilMemFree(agentAddress.stream); } if (sourceAddress.length == 4) { printf (" source IP = %d.%d.%d.%d\n", (int)sourceAddress.stream[0], (int)sourceAddress.stream[1], (int)sourceAddress.stream[2], (int)sourceAddress.stream[3]); } else if (sourceAddress.length == 10) { printf (" source IPX = %.2x%.2x%.2x%.2x." "%.2x%.2x%.2x%.2x%.2x%.2x\n", (int)sourceAddress.stream[0], (int)sourceAddress.stream[1], (int)sourceAddress.stream[2], (int)sourceAddress.stream[3], (int)sourceAddress.stream[4], (int)sourceAddress.stream[5], (int)sourceAddress.stream[6], (int)sourceAddress.stream[7], (int)sourceAddress.stream[8], (int)sourceAddress.stream[9]); } if (sourceAddress.dynamic) { SnmpUtilMemFree(sourceAddress.stream); } if (communityTrap.length) { string = (LPSTR)SnmpUtilMemAlloc (communityTrap.length + 1); if (string) { memcpy (string, communityTrap.stream, communityTrap.length); string[communityTrap.length] = '\0'; printf (" community = %s\n", string); SnmpUtilMemFree(string); } } if (communityTrap.dynamic) { SnmpUtilMemFree(communityTrap.stream); } for(i=0; i < variableBindingsTrap.len; i++) { string = NULL; if (SnmpMgrOidToStr(&variableBindingsTrap.list[i].name, &string)) { printf(" variable = %s\n", string); if (string) SnmpUtilMemFree(string); } printf(" value = "); SnmpUtilPrintAsnAny(&variableBindingsTrap.list[i].value); } // end for() printf("\n"); SnmpUtilVarBindListFree(&variableBindingsTrap); } dwResult = GetLastError(); // check for errors... if ((dwResult != NOERROR) && (dwResult != SNMP_MGMTAPI_NOTRAPS)) { printf("error on SnmpMgrGetTrap %d\n", dwResult); } } } // end while() } // end if(operation) if (operation != TRAP) { // Close SNMP session with the remote agent. if (!SnmpMgrClose(session)) { printf("error on SnmpMgrClose %d\n", GetLastError()); return 1; } } // Let the command interpreter know things went ok. return 0; } // end main()
int _cdecl main( IN int argumentCount, IN char *argumentVector[]) { HANDLE hExtension; FARPROC initAddr; FARPROC queryAddr; FARPROC trapAddr; DWORD timeZeroReference; HANDLE hPollForTrapEvent; View supportedView; INT numQueries = 10; // avoid compiler warning... UNREFERENCED_PARAMETER(argumentCount); UNREFERENCED_PARAMETER(argumentVector); timeZeroReference = GetCurrentTime(); // load the extension agent dll and resolve the entry points... if (GetModuleHandle("testdll.dll") == NULL) { if ((hExtension = LoadLibrary("testdll.dll")) == NULL) { dbgprintf(1, "error on LoadLibrary %d\n", GetLastError()); } else if ((initAddr = GetProcAddress(hExtension, "SnmpExtensionInit")) == NULL) { dbgprintf(1, "error on GetProcAddress %d\n", GetLastError()); } else if ((queryAddr = GetProcAddress(hExtension, "SnmpExtensionQuery")) == NULL) { dbgprintf(1, "error on GetProcAddress %d\n", GetLastError()); } else if ((trapAddr = GetProcAddress(hExtension, "SnmpExtensionTrap")) == NULL) { dbgprintf(1, "error on GetProcAddress %d\n", GetLastError()); } else { // initialize the extension agent via its init entry point... (*initAddr)( timeZeroReference, &hPollForTrapEvent, &supportedView); } } // end if (Already loaded) // create a trap thread to respond to traps from the extension agent... //rather than oomplicate this test routine, will poll for these events //below. normally this would be done by another thread in the extendible //agent. // loop here doing repetitive extension agent get queries... // poll for potential traps each iteration (see note above)... //block... printf( "SET on toasterManufacturer - shouldn't work\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 1, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_OCTETSTRING; varBinds.list[0].value.asnValue.string.length = 0; varBinds.list[0].value.asnValue.string.stream = NULL; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterControl with invalid value\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 3, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_INTEGER; varBinds.list[0].value.asnValue.number = 500; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterControl with invalid type\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 3, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_NULL; varBinds.list[0].value.asnValue.number = 500; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterControl\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 3, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_INTEGER; varBinds.list[0].value.asnValue.number = 2; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterDoneness with invalid value\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 4, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_INTEGER; varBinds.list[0].value.asnValue.number = 1000; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterDoneness with invalid type\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 4, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_NULL; varBinds.list[0].value.asnValue.number = 1000; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterDoneness\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 4, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_INTEGER; varBinds.list[0].value.asnValue.number = 10; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterToastType with invalid value\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 5, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_INTEGER; varBinds.list[0].value.asnValue.number = 10; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterToastType with invalid type\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 5, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_NULL; varBinds.list[0].value.asnValue.number = 10; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } printf( "SET on toasterToastType\n" ); { UINT itemn[] = { 1, 3, 6, 1, 4, 1, 12, 2, 5, 0 }; RFC1157VarBindList varBinds; AsnInteger errorStatus = 0; AsnInteger errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT); varBinds.list[0].name.ids = (UINT *)SnmpUtilMemAlloc( sizeof(UINT)* varBinds.list[0].name.idLength ); memcpy( varBinds.list[0].name.ids, &itemn, sizeof(UINT)*varBinds.list[0].name.idLength ); varBinds.list[0].value.asnType = ASN_INTEGER; varBinds.list[0].value.asnValue.number = 7; printf( "SET: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " to " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); (*queryAddr)( ASN_RFC1157_SETREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\nSET Errorstatus: %lu\n\n", errorStatus ); (*queryAddr)( ASN_RFC1157_GETREQUEST, &varBinds, &errorStatus, &errorIndex ); if ( errorStatus == SNMP_ERRORSTATUS_NOERROR ) { printf( "New Value: " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); putchar( '\n' ); } printf( "\nGET Errorstatus: %lu\n\n", errorStatus ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); printf( "\n\n" ); } { RFC1157VarBindList varBinds; AsnInteger errorStatus; AsnInteger errorIndex; UINT OID_Prefix[] = { 1, 3, 6, 1, 4, 1, 12 }; AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(OID_Prefix), OID_Prefix }; errorStatus = 0; errorIndex = 0; varBinds.list = (RFC1157VarBind *)SnmpUtilMemAlloc( sizeof(RFC1157VarBind) ); varBinds.len = 1; SnmpUtilOidCpy( &varBinds.list[0].name, &MIB_OidPrefix ); varBinds.list[0].value.asnType = ASN_NULL; do { printf( "GET-NEXT of: " ); SnmpUtilPrintOid( &varBinds.list[0].name ); printf( " " ); (*queryAddr)( (AsnInteger)ASN_RFC1157_GETNEXTREQUEST, &varBinds, &errorStatus, &errorIndex ); printf( "\n is " ); SnmpUtilPrintOid( &varBinds.list[0].name ); if ( errorStatus ) { printf( "\nErrorstatus: %lu\n\n", errorStatus ); } else { printf( "\n = " ); SnmpUtilPrintAsnAny( &varBinds.list[0].value ); } putchar( '\n' ); // query potential traps (see notes above) if ( NULL != hPollForTrapEvent ) { DWORD dwResult; if ( 0xffffffff == (dwResult = WaitForSingleObject(hPollForTrapEvent, 1000)) ) { dbgprintf(1, "error on WaitForSingleObject %d\n", GetLastError()); } else { if ( dwResult == 0 /*signaled*/ ) { AsnObjectIdentifier enterprise; AsnInteger genericTrap; AsnInteger specificTrap; AsnTimeticks timeStamp; RFC1157VarBindList variableBindings; while( (*trapAddr)(&enterprise, &genericTrap, &specificTrap, &timeStamp, &variableBindings) ) { printf("trap: gen=%d spec=%d time=%d\n", genericTrap, specificTrap, timeStamp); //also print data } // end while () } // end if (trap ready) } } // end if (handling traps) } while ( varBinds.list[0].name.ids[MIB_PREFIX_LEN-1] == 12 ); // Free the memory SnmpUtilVarBindListFree( &varBinds ); } // block return 0; } // end main()
void _php3_snmp(INTERNAL_FUNCTION_PARAMETERS, int st) { pval *a1, *a2, *a3; INT operation; LPSTR agent; LPSTR community; RFC1157VarBindList variableBindings; LPSNMP_MGR_SESSION session; INT timeout = TIMEOUT; INT retries = RETRIES; BYTE requestType; AsnInteger errorStatus; AsnInteger errorIndex; AsnObjectIdentifier oid; char *chkPtr = NULL; if (getParameters(ht, 3, &a1, &a2, &a3) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(a1); convert_to_string(a2); convert_to_string(a3); agent=a1->value.str.val; community=a2->value.str.val; operation=st; SnmpMgrStrToOid(a3->value.str.val, &oid); /* I've limited this to only one oid, but we can create a list of oid's here, and expand the function to take multiple oid's */ variableBindings.list->name = oid; variableBindings.list->value.asnType = ASN_NULL; variableBindings.len = 1; /* Establish a SNMP session to communicate with the remote agent. The community, communications timeout, and communications retry count for the session are also required. */ if ((session = SnmpMgrOpen(agent, community, timeout, retries)) == NULL){ php_error(E_WARNING,"error on SnmpMgrOpen %d\n", GetLastError()); } /* Determine and perform the requested operation.*/ if (operation == GET || operation == GETNEXT){ /* Get and GetNext are relatively simple operations to perform. Simply initiate the request and process the result and/or possible error conditions. */ if (operation == GET){ requestType = ASN_RFC1157_GETREQUEST; } else { requestType = ASN_RFC1157_GETNEXTREQUEST; } /* Request that the API carry out the desired operation.*/ if (!SnmpMgrRequest(session, requestType, &variableBindings, &errorStatus, &errorIndex)){ /* The API is indicating an error. */ php_error(E_WARNING,"error on SnmpMgrRequest %d\n", GetLastError()); } else { /* The API succeeded, errors may be indicated from the remote agent. */ if (errorStatus > 0){ php_error(E_WARNING,"Error: errorStatus=%d, errorIndex=%d\n", errorStatus, errorIndex); } else { /* Display the resulting variable bindings.*/ UINT i; char *str = NULL; for(i=0; i < variableBindings.len; i++) { SnmpMgrOidToStr(&variableBindings.list[i].name, &str); php_printf("Variable = %s\n", str); if (str) SNMP_free(str); php_printf("Value = "); SnmpUtilPrintAsnAny(&variableBindings.list[i].value); php_printf("\n"); } /* end for() */ } } /* Free the variable bindings that have been allocated.*/ SnmpUtilVarBindListFree(&variableBindings); } else if (operation == WALK) { /* Walk is a common term used to indicate that all MIB variables under a given OID are to be traversed and displayed. This is a more complex operation requiring tests and looping in addition to the steps for get/getnext above. */ AsnObjectIdentifier root; AsnObjectIdentifier tempOid; SnmpUtilOidCpy(&root, &variableBindings.list[0].name); requestType = ASN_RFC1157_GETNEXTREQUEST; while(1) { if (!SnmpMgrRequest(session, requestType, &variableBindings, &errorStatus, &errorIndex)){ /* The API is indicating an error.*/ php_error(E_WARNING,"error on SnmpMgrRequest %d\n", GetLastError()); break; } else { /* The API succeeded, errors may be indicated from the remote agent. Test for end of subtree or end of MIB. */ if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME || SnmpUtilOidNCmp(&variableBindings.list[0].name, &root, root.idLength)) { PUTS("End of MIB subtree.\n\n"); break; } /* Test for general error conditions or sucesss. */ if (errorStatus > 0){ php_error(E_ERROR,"Error: errorStatus=%d, errorIndex=%d \n", errorStatus, errorIndex); break; } else { /* Display resulting variable binding for this iteration. */ char *str = NULL; SnmpMgrOidToStr(&variableBindings.list[0].name, &str); php_printf("Variable = %s\n", str); if (str) SNMP_free(str); php_printf("Value = "); SnmpUtilPrintAsnAny(&variableBindings.list[0].value); php_printf("\n"); } } /* end if () */ /* Prepare for the next iteration. Make sure returned oid is preserved and the returned value is freed. */ SnmpUtilOidCpy(&tempOid, &variableBindings.list[0].name); SnmpUtilVarBindFree(&variableBindings.list[0]); SnmpUtilOidCpy(&variableBindings.list[0].name, &tempOid); variableBindings.list[0].value.asnType = ASN_NULL; SnmpUtilOidFree(&tempOid); } /* end while() */ /* Free the variable bindings that have been allocated.*/ SnmpUtilVarBindListFree(&variableBindings); SnmpUtilOidFree(&root); } /* end if (operation) */ /* Close SNMP session with the remote agent.*/ if (!SnmpMgrClose(session)){ php_error(E_WARNING,"error on SnmpMgrClose %d\n", GetLastError()); } }