📄 expvaluetable.c
字号:
temp = malloc(100); result = malloc(100); tempbak = temp; memset(result, 0, 100); *result = '\0'; resultbak = result; DEBUGMSGTL(("expValueTable", "building valuetable... \n")); for (hcindex = expExpressionTableStorage; hcindex != NULL; hcindex = hcindex->next) { expstorage = (struct expExpressionTable_data *) hcindex->data; if (expstorage->expExpressionEntryStatus == RS_ACTIVE) { expression = expstorage->expExpression; expression_len = expstorage->expExpressionLen; while (*expression != '\0') { if (*expression == '$') { i++; for (j = 1; j < 100; j++) { if ((*(expression + j) == '+') || (*(expression + j) == '-') || (*(expression + j) == '*') || (*(expression + j) == '/') || (*(expression + j) == '(') || (*(expression + j) == ')') || *(expression + j) == '\0') { break; } } strncpy(temp, expression + 1, j - 1); *(temp + j - 1) = '\0'; l = atoi(temp); for (object_hcindex = expObjectTableStorage; object_hcindex != NULL; object_hcindex = object_hcindex->next) { objstorage = (struct expObjectTable_data *) object_hcindex-> data; if (!strcmp (objstorage->expExpressionOwner, expstorage->expExpressionOwner) && (objstorage->expExpressionOwnerLen == expstorage->expExpressionOwnerLen) && !strcmp(objstorage->expExpressionName, expstorage->expExpressionName) && (objstorage->expExpressionNameLen == expstorage->expExpressionNameLen) && (l == objstorage->expObjectIndex)) { if (objfound == NULL) { expfound = expstorage; objfound = objstorage; } if (objstorage->expObjectIDWildcard == EXPOBJCETIDWILDCARD_TRUE) objfound = objstorage; } } expression = expression + j; } else { expression++; } }; } if (!objfound) { continue; } if (objfound->expObjectIDWildcard == EXPOBJCETIDWILDCARD_FALSE) { index = calloc(1, MAX_OID_LEN); *index = 0; *(index + 1) = 0; *(index + 2) = 0; expValueTable_add(expstorage, objfound->expExpressionOwner, objfound->expExpressionOwnerLen, objfound->expExpressionName, objfound->expExpressionNameLen, index, 3); } else { oid *targetOID; size_t taggetOID_len; targetOID = objfound->expObjectID; struct snmp_pdu *pdu; struct snmp_pdu *response; oid *next_OID; size_t next_OID_len; taggetOID_len = objfound->expObjectIDLen; struct variable_list *vars; int status; int count = 1; struct snmp_session *ss; /* * Initialize the SNMP library */ /* * set the SNMP version number */ session.version = expstorage->pdu_version; /* * set the SNMPv1 community name used for authentication */ session.community = expstorage->pdu_community; session.community_len = expstorage->pdu_community_len; /* * Open the session */ SOCK_STARTUP; ss = snmp_open(&session); /* establish the session */ if (!ss) { snmp_perror("ack"); snmp_log(LOG_ERR, "something horrible happened!!!\n"); exit(2); } next_OID = targetOID; next_OID_len = taggetOID_len; do { index = calloc(1, MAX_OID_LEN); pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(pdu, next_OID, next_OID_len); /* * Send the Request out. */ status = snmp_synch_response(ss, pdu, &response); /* * Process the response. */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* * SUCCESS: Print the result variables */ if (((response->variables->type >= SNMP_NOSUCHOBJECT && response->variables->type <= SNMP_ENDOFMIBVIEW) || snmp_oid_compare(targetOID, taggetOID_len, response->variables->name, taggetOID_len) != 0)) { break; } /* add to expValueTable */ *index = 0; *(index + 1) = 0; memcpy(index + 2, response->variables->name + taggetOID_len, (response->variables->name_length - taggetOID_len) * sizeof(oid)); expValueTable_add(expstorage, objfound->expExpressionOwner, objfound->expExpressionOwnerLen, objfound->expExpressionName, objfound->expExpressionNameLen, index, response->variables->name_length - taggetOID_len + 2); next_OID = response->variables->name; next_OID_len = response->variables->name_length; } else { /* * FAILURE: print what went wrong! */ if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); else snmp_sess_perror("snmpget", ss); } } while (TRUE); } }}/* * var_expValueTable(): */unsigned char *var_expValueTable(struct variable *vp, oid * name, size_t *length, int exact, size_t *var_len, WriteMethod ** write_method){ static netsnmp_variable_list *vars; size_t newlen = *length - (sizeof(expValueTable_variables_oid) / sizeof(oid) + 3 - 1); struct expValueTable_data *StorageTmp = NULL; unsigned int counter32; DEBUGMSGTL(("expValueTable", "var_expValueTable: Entering... \n")); /* * before we build valuetable we must free any other valutable if exist */ header_complex_free_all(expValueTableStorage, expValueTable_clean); expValueTableStorage = NULL; build_valuetable(); /* * this assumes you have registered all your data properly */ if ((StorageTmp = header_complex(expValueTableStorage, vp, name, length, exact, var_len, write_method)) == NULL) return NULL; /* * this is where we do the value assignments for the mib results. */ switch (vp->magic) { /* * we only support counter32val */ case EXPVALUECOUNTER32VAL: StorageTmp->expValueCounter32Val = Evaluate_Expression(StorageTmp); *var_len = sizeof(StorageTmp->expValueCounter32Val); return (u_char *) & StorageTmp->expValueCounter32Val; case EXPVALUEUNSIGNED32VAL: /* var_len = sizeof(StorageTmp->expValueUnsigned32Val); */ /* return (u_char *) & StorageTmp->expValueUnsigned32Val; */ return NULL; case EXPVALUETIMETICKSVAL: /* var_len = sizeof(StorageTmp->expValueTimeTicksVal); */ /* return (u_char *) & StorageTmp->expValueTimeTicksVal; */ return NULL; case EXPVALUEINTEGER32VAL: /* var_len = sizeof(StorageTmp->expValueInteger32Val); */ /* return (u_char *) & StorageTmp->expValueInteger32Val; */ return NULL; case EXPVALUEIPADDRESSVAL: /* var_len = sizeof(StorageTmp->expValueIpAddressVal); */ /* return (u_char *) & StorageTmp->expValueIpAddressVal; */ return NULL; case EXPVALUEOCTETSTRINGVAL: /* var_len = sizeof(StorageTmp->expValueOctetStringVal); */ /* return (u_char *) & StorageTmp->expValueOctetStringVal; */ return NULL; case EXPVALUEOIDVAL: /* var_len = StorageTmp->expValueOidValLen; */ /* return (u_char *) & StorageTmp->expValueOidVal; */ return NULL; case EXPVALUECOUNTER64VAL: /* var_len = sizeof(StorageTmp->expValueCounter64Val); */ /* return (u_char *) & StorageTmp->expValueCounter64Val; */ return NULL; default: ERROR_MSG(""); }}voidpush(link ** stack, unsigned long value){ link *newnode; newnode = (link *) malloc(sizeof(link)); if (!newnode) { printf("\nMemory allocation failure!"); return; } newnode->data = value; newnode->next = *stack; *stack = newnode;}unsigned longpop(link ** stack){ unsigned long value; link *top; top = *stack; *stack = (*stack)->next; value = top->data; free(top); return value;}intpriority(char operater){ switch (operater) { case '*': case '/': return 4; case '+': case '-': return 3; case ')': return 2; case '(': return 1; default: return 0; }}unsigned longcalculate(int operater, unsigned long a, unsigned long b){ switch (operater) { case '+': return (a + b); case '-': return (a - b); case '*': return (a * b); case '/': if (operater == '/' && b == 0) { printf("\nDivision mustn\'t be 0!"); exit(0); } else return (a / b); }}unsigned longget_operand(char *p, int *length){ char c[13]; int i = 0, k = 1; unsigned long result = 0; while (*p <= 57 && *p >= 48) c[i++] = *(p++); *length += --i; for (; i >= 0; i--) { result += (c[i] - 48) * k; k *= 10; } return result;}intoperator_class(char c){ if (c <= 57 && c >= 48) return 1; if (c == 42 || c == 43 || c == 45 || c == 47) return 2; if (c == 41) return 3; if (c == 40) return 4; return 0;}unsigned longget_result(char *expr){ int position = 0; unsigned long op = 0, a = 0, b = 0, result = 0; char *expression; expression = expr; while (*(expression + position) != '\0' && *(expression + position) != '\n') { switch (operator_class(*(expression + position))) { case 1: push(&operand, get_operand(expression + position, &position)); break; case 2: if (operater != NULL) while (operater != NULL && priority(*(expression + position)) <= priority(operater->data)) { a = pop(&operand); b = pop(&operand); op = pop(&operater); push(&operand, calculate(op, b, a)); } push(&operater, *(expression + position)); break; case 3: while (operater != NULL && operater->data != '(') { a = pop(&operand); b = pop(&operand); op = pop(&operater); push(&operand, calculate(op, b, a)); } if (operater->data == '(') pop(&operater); break; case 4: push(&operater, '('); break; default: printf("\nInvalid character in expression:"); a = 0; while (*(expression + (int) a) != '\n' && *(expression + (int) a) != '\0') { if (a != position) printf("%c", *(expression + (int) a)); else printf("<%c>", *(expression + (int) a)); a++; } exit(0); } /* end switch */ position++; } while (operater != NULL) { op = pop(&operater); a = pop(&operand); b = pop(&operand); push(&operand, calculate(op, b, a)); } result = pop(&operand); return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -