📄 winextdll.c
字号:
bResult = winExtensionAgent[i].xSnmpExtensionTrap( &pEnterprise, &pGenericTrap, &pSpecificTrap, &pTimeStamp, &pVariableBindings); DEBUGMSGTL(("winExtDLL", "result of SnmpExtensionTrap call: %d\n",bResult)); // Assume that if enterprise length is >0, it's a real trap and not a cleanup call // FIXME: Not sure if this is correct. Need to test with agent that sends a non-enterprise trap if (pEnterprise.idLength) { DEBUGMSGTL(("winExtDLL", "GenericTrap: %d\n",pGenericTrap)); DEBUGMSGTL(("winExtDLL", "SpecificTrap: %d\n",pSpecificTrap)); if (pEnterprise.idLength) DEBUGMSGTL(("winExtDLL", "pEnterprise is not 0\n")); else DEBUGMSGTL(("winExtDLL", "pEnterprise is 0\n")); // Send the trap send_trap( &pEnterprise, &pGenericTrap, &pSpecificTrap, &pTimeStamp, &pVariableBindings); } SnmpUtilVarBindListFree(&pVariableBindings); } break; } } // Events should be auto-reset, but just in case.. ResetEvent(dwWaitResult); return 1;}void send_trap( AsnObjectIdentifier *pEnterprise, AsnInteger *pGenericTrap, AsnInteger *pSpecificTrap, AsnTimeticks *pTimeStamp, SnmpVarBindList *pVariableBindings) { int i, j; SnmpVarBind mySnmpVarBind; oid my_oid[MAX_OID_LEN]; // Holder for pVariableBindings OIDs size_t my_oid_length = 0; // Holder for pVariableBindings OIDs oid enterprise_oid[MAX_OID_LEN]; // Holder for enterprise OID size_t enterprise_oid_length = 0; // Holder for enterprise OID oid ret_oid[MAX_OID_LEN]; // Holder for return OIDs size_t ret_oid_length = 0; // Holder for return OIDs char *stringtemp; /* * here is where we store the variables to be sent in the trap */ netsnmp_variable_list *notification_vars = NULL; /* * In the notification, we have to assign our notification OID to * the snmpTrapOID.0 object. Here is it's definition. */ oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap); DEBUGMSGTL(("winExtDLL", "send_trap() called\n")); DEBUGMSGTL(("winExtDLL", "pVariableBindings length: %d\n",pVariableBindings->len)); if (*pGenericTrap != 6) { DEBUGMSGTL(("winExtDLL", "Working on generic trap\n")); DEBUGMSGTL(("winExtDLL", "sending v1 trap\n")); send_easy_trap(*pGenericTrap, *pSpecificTrap); return; } // Convert enterprise OID from Windows to Net-SNMP so Net-SNMP for (j = 0; j < (pEnterprise->idLength > MAX_OID_LEN?MAX_OID_LEN:pEnterprise->idLength); j++) { enterprise_oid[j] = (oid)pEnterprise->ids[j]; } enterprise_oid_length = j; DEBUGMSGTL(("winExtDLL", "Enterprise OID: ")); DEBUGMSGOID(("winExtDLL", enterprise_oid, enterprise_oid_length)); DEBUGMSG(("winExtDLL", "\n")); // We need to copy .0.specific trap to copy of enterprise and use that for objid_snmptrap!! for (i = 0; i < enterprise_oid_length; i++) { my_oid[i] = enterprise_oid[i]; } my_oid_length = i; my_oid[my_oid_length] = 0; my_oid_length++; my_oid[my_oid_length] = *pSpecificTrap; my_oid_length++; DEBUGMSGTL(("winExtDLL", "Trap OID (snmpTrapOID.0): ")); DEBUGMSGOID(("winExtDLL", my_oid, my_oid_length)); DEBUGMSG(("winExtDLL", "\n")); /* * add in the trap definition object */ snmp_varlist_add_variable(¬ification_vars, /* * the snmpTrapOID.0 variable */ objid_snmptrap, objid_snmptrap_len, /* * value type is an OID */ ASN_OBJECT_ID, /* * value contents is our notification OID */ (u_char *) my_oid, /* * size in bytes */ my_oid_length * sizeof(oid)); for (i = 0; i< pVariableBindings->len; i++) { mySnmpVarBind = pVariableBindings->list[i]; // Convert OID from Windows to Net-SNMP so Net-SNMP for (j = 0; j < (mySnmpVarBind.name.idLength > MAX_OID_LEN?MAX_OID_LEN:mySnmpVarBind.name.idLength); j++) { my_oid[j] = (oid)mySnmpVarBind.name.ids[j]; } my_oid_length = j; DEBUGMSGTL(("winExtDLL", "OID in trap variable binding: ")); DEBUGMSGOID(("winExtDLL", my_oid, my_oid_length)); DEBUGMSG(("winExtDLL", "\n")); // Set Net-SNMP ASN type based on closest match to Windows ASN type switch (mySnmpVarBind.value.asnType) { case MS_ASN_OCTETSTRING: // AsnOctetString stringtemp = netsnmp_strdup_and_null(mySnmpVarBind.value.asnValue.string.stream, mySnmpVarBind.value.asnValue.string.length); DEBUGMSGTL(("winExtDLL", "MS_ASN_OCTETSTRING = ASN_OCTET_STR\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_OCTETSTRING = %s\n",stringtemp)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_OCTET_STR, stringtemp, strlen(stringtemp)); break; case MS_ASN_INTEGER: // And MS_ASN_INTEGER32 DEBUGMSGTL(("winExtDLL", "MS_ASN_INTEGER = ASN_INTEGER\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_INTEGER = %d\n",mySnmpVarBind.value.asnValue.number)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_INTEGER, (u_char *)&mySnmpVarBind.value.asnValue.number, sizeof(mySnmpVarBind.value.asnValue.number)); break; case MS_ASN_UNSIGNED32: // SNMP v2 DEBUGMSGTL(("winExtDLL", "MS_ASN_UNSIGNED32 = ASN_UNSIGNED\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_UNSIGNED32 = %d\n",mySnmpVarBind.value.asnValue.unsigned32)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_UNSIGNED, (u_char *)&mySnmpVarBind.value.asnValue.unsigned32, sizeof(mySnmpVarBind.value.asnValue.unsigned32)); break; case MS_ASN_COUNTER64: // SNMP v2 DEBUGMSGTL(("winExtDLL", "MS_ASN_COUNTER64 = ASN_COUNTER64\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_COUNTER64 = %d\n",mySnmpVarBind.value.asnValue.counter64)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_COUNTER64, (u_char *)&mySnmpVarBind.value.asnValue.counter64, sizeof(mySnmpVarBind.value.asnValue.counter64)); break; case MS_ASN_BITS: // AsnOctetString stringtemp = netsnmp_strdup_and_null(mySnmpVarBind.value.asnValue.bits.stream, mySnmpVarBind.value.asnValue.bits.length); DEBUGMSGTL(("winExtDLL", "MS_ASN_BITS = ASN_BIT_STR\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_BITS = %s\n",stringtemp)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_BIT_STR, stringtemp, strlen(stringtemp)); break; case MS_ASN_OBJECTIDENTIFIER: // AsnObjectIdentifier DEBUGMSGTL(("winExtDLL", "MS_ASN_OBJECTIDENTIFIER = ASN_OBJECT_ID\n")); // Convert OID to Net-SNMP DEBUGMSGTL(("winExtDLL", "Returned OID: ")); DEBUGMSGWINOID(("winExtDLL", &mySnmpVarBind.value.asnValue.object)); DEBUGMSG(("winExtDLL", "\n")); // Convert OID from Windows to Net-SNMP for (i = 0; i < (mySnmpVarBind.value.asnValue.object.idLength > MAX_OID_LEN?MAX_OID_LEN: mySnmpVarBind.value.asnValue.object.idLength); i++) { ret_oid[i] = (oid)mySnmpVarBind.value.asnValue.object.ids[i]; } ret_oid_length = i; DEBUGMSGTL(("winExtDLL", "Windows OID converted to Net-SNMP: ")); DEBUGMSGOID(("winExtDLL", ret_oid, ret_oid_length)); DEBUGMSG(("winExtDLL", "\n")); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_OBJECT_ID, (u_char *)&ret_oid, ret_oid_length * sizeof(oid)); break; case MS_ASN_SEQUENCE: // AsnOctetString stringtemp = netsnmp_strdup_and_null(mySnmpVarBind.value.asnValue.sequence.stream, mySnmpVarBind.value.asnValue.sequence.length); DEBUGMSGTL(("winExtDLL", "MS_ASN_SEQUENCE = ASN_SEQUENCE\n")); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_SEQUENCE, stringtemp, strlen(stringtemp)); break; case MS_ASN_IPADDRESS: // AsnOctetString stringtemp = netsnmp_strdup_and_null(mySnmpVarBind.value.asnValue.address.stream, mySnmpVarBind.value.asnValue.address.length); DEBUGMSGTL(("winExtDLL", "MS_ASN_IPADDRESS = ASN_IPADDRESS\n")); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_IPADDRESS, stringtemp, strlen(stringtemp)); break; case MS_ASN_COUNTER32: DEBUGMSGTL(("winExtDLL", "MS_ASN_COUNTER32 = ASN_COUNTER\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_COUNTER32 = %d\n",mySnmpVarBind.value.asnValue.counter)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_COUNTER, (u_char *)&mySnmpVarBind.value.asnValue.counter, sizeof(mySnmpVarBind.value.asnValue.counter)); break; case MS_ASN_GAUGE32: DEBUGMSGTL(("winExtDLL", "MS_ASN_GAUGE32 = ASN_GAUGE\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_GAUGE32 = %d\n",mySnmpVarBind.value.asnValue.gauge)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_GAUGE, (u_char *)&mySnmpVarBind.value.asnValue.gauge, sizeof(mySnmpVarBind.value.asnValue.gauge)); break; case MS_ASN_TIMETICKS: DEBUGMSGTL(("winExtDLL", "MS_ASN_TIMETICKS = ASN_TIMETICKS\n")); DEBUGMSGTL(("winExtDLL", "MS_ASN_TIMETICKS = %d\n",mySnmpVarBind.value.asnValue.ticks)); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_TIMETICKS, (u_char *)&mySnmpVarBind.value.asnValue.ticks, sizeof(mySnmpVarBind.value.asnValue.ticks)); break; case MS_ASN_OPAQUE: // AsnOctetString stringtemp = netsnmp_strdup_and_null(mySnmpVarBind.value.asnValue.arbitrary.stream, mySnmpVarBind.value.asnValue.arbitrary.length); DEBUGMSGTL(("winExtDLL", "MS_ASN_OPAQUE = ASN_OPAQUE\n")); snmp_varlist_add_variable(¬ification_vars, my_oid, my_oid_length, ASN_OPAQUE, stringtemp, strlen(stringtemp)); break; default: DEBUGMSGTL(("winExtDLL", "Defaulting to ASN_INTEGER\n")); break; } } /* * send the trap out. This will send it to all registered * receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS" * section of the snmpd.conf manual page. */ DEBUGMSGTL(("winExtDLL", "sending v2 trap\n")); send_v2trap(notification_vars); /* * free the created notification variable list */ DEBUGMSGTL(("winExtDLL", "cleaning up\n")); snmp_free_varbind(notification_vars);}/* DEBUGMSGWINOID */voiddebugmsg_win_oid(const char *token, const AsnObjectIdentifier * theoid){ u_char buf[1024]; u_char temp[10]; size_t buf_len = 0, out_len = 0; int i; buf[0] = '\0'; for (i = 0; i < theoid->idLength; i++) { sprintf(temp, ".%d", theoid->ids[i]); strcat(buf, temp); } if (buf != NULL) { debugmsg(token, "%s", buf); //DEBUGMSGTL((token, "%s\n", buf)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -