📄 snmpnotifytable.c
字号:
snmpNotifyTable_add(nptr); return 0;}/* * XXX: this really needs to be done for the target mib entries too. * But we can only trust that we've added stuff here and we don't want * to destroy other valid entries in the target tables, so... Don't * do too many kill -HUPs to your agent as re reading the config file * will be a slow memory leak in the target mib. */intnotifyTable_unregister_notifications(int major, int minor, void *serverarg, void *clientarg){ struct header_complex_index *hptr, *nhptr; struct snmpNotifyTable_data *nptr; for (hptr = snmpNotifyTableStorage; hptr; hptr = nhptr) { nptr = (struct snmpNotifyTable_data *) hptr->data; nhptr = hptr->next; if (nptr->snmpNotifyStorageType == ST_READONLY) { header_complex_extract_entry(&snmpNotifyTableStorage, hptr); SNMP_FREE(nptr->snmpNotifyName); SNMP_FREE(nptr->snmpNotifyTag); SNMP_FREE(nptr); } } return (0);}/* * init_snmpNotifyTable(): * Initialization routine. This is called when the agent starts up. * At a minimum, registration of your variables should take place here. */voidinit_snmpNotifyTable(void){ DEBUGMSGTL(("snmpNotifyTable", "initializing... ")); /* * register ourselves with the agent to handle our mib tree */ REGISTER_MIB("snmpNotifyTable", snmpNotifyTable_variables, variable2, snmpNotifyTable_variables_oid); /* * register our config handler(s) to deal with registrations */ snmpd_register_config_handler("snmpNotifyTable", parse_snmpNotifyTable, NULL, NULL); /* * we need to be called back later to store our data */ snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA, store_snmpNotifyTable, NULL); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_SEND_TRAP1, send_notifications, NULL); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_SEND_TRAP2, send_notifications, NULL); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_REGISTER_NOTIFICATIONS, notifyTable_register_notifications, NULL); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_PRE_UPDATE_CONFIG, notifyTable_unregister_notifications, NULL); /* * place any other initialization junk you need here */ DEBUGMSGTL(("snmpNotifyTable", "done.\n"));}/* * snmpNotifyTable_add(): adds a structure node to our data set */intsnmpNotifyTable_add(struct snmpNotifyTable_data *thedata){ netsnmp_variable_list *vars = NULL; DEBUGMSGTL(("snmpNotifyTable", "adding data... ")); /* * add the index variables to the varbind list, which is * used by header_complex to index the data */ snmp_varlist_add_variable(&vars, NULL, 0, ASN_PRIV_IMPLIED_OCTET_STR, (u_char *) thedata->snmpNotifyName, thedata->snmpNotifyNameLen); /* snmpNotifyName */ header_complex_add_data(&snmpNotifyTableStorage, vars, thedata); DEBUGMSGTL(("snmpNotifyTable", "registered an entry\n")); DEBUGMSGTL(("snmpNotifyTable", "done.\n")); return SNMPERR_SUCCESS;}/* * parse_snmpNotifyTable(): * parses .conf file entries needed to configure the mib. */voidparse_snmpNotifyTable(const char *token, char *line){ size_t tmpint; struct snmpNotifyTable_data *StorageTmp = SNMP_MALLOC_STRUCT(snmpNotifyTable_data); DEBUGMSGTL(("snmpNotifyTable", "parsing config... ")); if (StorageTmp == NULL) { config_perror("malloc failure"); return; } line = read_config_read_data(ASN_OCTET_STR, line, &StorageTmp->snmpNotifyName, &StorageTmp->snmpNotifyNameLen); if (StorageTmp->snmpNotifyName == NULL) { config_perror("invalid specification for snmpNotifyName"); return; } line = read_config_read_data(ASN_OCTET_STR, line, &StorageTmp->snmpNotifyTag, &StorageTmp->snmpNotifyTagLen); if (StorageTmp->snmpNotifyTag == NULL) { config_perror("invalid specification for snmpNotifyTag"); return; } line = read_config_read_data(ASN_INTEGER, line, &StorageTmp->snmpNotifyType, &tmpint); line = read_config_read_data(ASN_INTEGER, line, &StorageTmp->snmpNotifyStorageType, &tmpint); line = read_config_read_data(ASN_INTEGER, line, &StorageTmp->snmpNotifyRowStatus, &tmpint); snmpNotifyTable_add(StorageTmp); DEBUGMSGTL(("snmpNotifyTable", "done.\n"));}/* * store_snmpNotifyTable(): * stores .conf file entries needed to configure the mib. */intstore_snmpNotifyTable(int majorID, int minorID, void *serverarg, void *clientarg){ char line[SNMP_MAXBUF]; char *cptr; size_t tmpint; struct snmpNotifyTable_data *StorageTmp; struct header_complex_index *hcindex; DEBUGMSGTL(("snmpNotifyTable", "storing data... ")); for (hcindex = snmpNotifyTableStorage; hcindex != NULL; hcindex = hcindex->next) { StorageTmp = (struct snmpNotifyTable_data *) hcindex->data; /* * store permanent and nonvolatile rows. * XXX should there be a qualification on RowStatus?? */ if ((StorageTmp->snmpNotifyStorageType == ST_NONVOLATILE) || (StorageTmp->snmpNotifyStorageType == ST_PERMANENT) ){ memset(line, 0, sizeof(line)); strcat(line, "snmpNotifyTable "); cptr = line + strlen(line); cptr = read_config_store_data(ASN_OCTET_STR, cptr, &StorageTmp->snmpNotifyName, &StorageTmp->snmpNotifyNameLen); cptr = read_config_store_data(ASN_OCTET_STR, cptr, &StorageTmp->snmpNotifyTag, &StorageTmp->snmpNotifyTagLen); cptr = read_config_store_data(ASN_INTEGER, cptr, &StorageTmp->snmpNotifyType, &tmpint); cptr = read_config_store_data(ASN_INTEGER, cptr, &StorageTmp->snmpNotifyStorageType, &tmpint); cptr = read_config_store_data(ASN_INTEGER, cptr, &StorageTmp->snmpNotifyRowStatus, &tmpint); snmpd_store_config(line); } } DEBUGMSGTL(("snmpNotifyTable", "done.\n")); return 0;}/* * var_snmpNotifyTable(): * Handle this table separately from the scalar value case. * The workings of this are basically the same as for var_snmpNotifyTable above. */unsigned char *var_snmpNotifyTable(struct variable *vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method){ struct snmpNotifyTable_data *StorageTmp = NULL; int found = 1; DEBUGMSGTL(("snmpNotifyTable", "var_snmpNotifyTable: Entering... \n")); /* * this assumes you have registered all your data properly */ if ((StorageTmp = (struct snmpNotifyTable_data *) header_complex((struct header_complex_index *) snmpNotifyTableStorage, vp, name, length, exact, var_len, write_method)) == NULL) { found = 0; } switch (vp->magic) { case SNMPNOTIFYTAG: *write_method = write_snmpNotifyTag; break; case SNMPNOTIFYTYPE: *write_method = write_snmpNotifyType; break; case SNMPNOTIFYSTORAGETYPE: *write_method = write_snmpNotifyStorageType; break; case SNMPNOTIFYROWSTATUS: *write_method = write_snmpNotifyRowStatus; break; default: *write_method = NULL; } if (!found) { return NULL; } switch (vp->magic) { case SNMPNOTIFYTAG: *var_len = StorageTmp->snmpNotifyTagLen; return (u_char *) StorageTmp->snmpNotifyTag; case SNMPNOTIFYTYPE: *var_len = sizeof(StorageTmp->snmpNotifyType); return (u_char *) & StorageTmp->snmpNotifyType; case SNMPNOTIFYSTORAGETYPE: *var_len = sizeof(StorageTmp->snmpNotifyStorageType); return (u_char *) & StorageTmp->snmpNotifyStorageType; case SNMPNOTIFYROWSTATUS: *var_len = sizeof(StorageTmp->snmpNotifyRowStatus); return (u_char *) & StorageTmp->snmpNotifyRowStatus; default: ERROR_MSG(""); } return NULL;}static intis_delim(const char c){ return (c == 0x020 || c == 0x09 || c == 0x0d || c == 0x0b);}intsnmpTagValid(const char *tag, const size_t tagLen){ size_t i = 0; for (i = 0; i < tagLen; i++) { if (is_delim(tag[i])) { /* * Delimeters aren't allowed. */ return 0; } } return 1;}static struct snmpNotifyTable_data *StorageNew;intwrite_snmpNotifyTag(int action, u_char * var_val, u_char var_val_type, size_t var_val_len, u_char * statP, oid * name, size_t name_len){ static char *tmpvar; struct snmpNotifyTable_data *StorageTmp = NULL; static size_t tmplen; size_t newlen = name_len - (sizeof(snmpNotifyTable_variables_oid) / sizeof(oid) + 3 - 1); DEBUGMSGTL(("snmpNotifyTable", "write_snmpNotifyTag entering action=%d... \n", action)); if (action != RESERVE1 && (StorageTmp = (struct snmpNotifyTable_data *) header_complex((struct header_complex_index *) snmpNotifyTableStorage, NULL, &name[sizeof(snmpNotifyTable_variables_oid) / sizeof(oid) + 3 - 1], &newlen, 1, NULL, NULL)) == NULL) { if ((StorageTmp = StorageNew) == NULL) return SNMP_ERR_NOSUCHNAME; /* remove if you support creation here */ } switch (action) { case RESERVE1: if (var_val_type != ASN_OCTET_STR) { return SNMP_ERR_WRONGTYPE; } if (var_val_len < 0 || var_val_len > 255) { return SNMP_ERR_WRONGLENGTH; } if (!snmpTagValid(var_val, var_val_len)) { return SNMP_ERR_WRONGVALUE; } break; case RESERVE2: /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -