www.pudn.com > snmpsample.rar > interfaces_tabledata.c
/* * Note: this file originally auto-generated by mib2c using * : mib2c.table_data.conf,v 1.3 2004/10/14 12:57:34 dts12 Exp $ */ #include#include #include #include "interfaces.h" /** Initializes the interfaces module */ void init_interfaces(void) { /* here we initialize all the tables we're planning on supporting */ initialize_table_ifTable(); } /* Typical data structure for a row entry */ struct ifTable_entry { /*定义表ifTable的entry*/ /* Index values */ long ifIndex; /* Column values */ long ifIndex1; char ifDescr; long ifType; long ifMtu; u_long ifSpeed; char ifPhysAddress; long ifAdminStatus; long old_ifAdminStatus; long ifOperStatus; u_long ifLastChange; u_long ifInOctets; u_long ifInUcastPkts; u_long ifInNUcastPkts; u_long ifInDiscards; u_long ifInErrors; u_long ifInUnknownProtos; u_long ifOutOctets; u_long ifOutUcastPkts; u_long ifOutNUcastPkts; u_long ifOutDiscards; u_long ifOutErrors; u_long ifOutQLen; oid ifSpecific; int valid; }; /* 在表中创建新行 */ netsnmp_table_row * ifTable_createEntry(netsnmp_table_data *table_data, long ifIndex ) { struct ifTable_entry *entry; netsnmp_table_row *row; entry = SNMP_MALLOC_TYPEDEF(struct ifTable_entry);/*给ifTable_entry分配地址空间*/ if (!entry) return NULL; row = netsnmp_create_table_data_row(); /*创建一个新的行*/ if (!row) { SNMP_FREE(entry); return NULL; } row->data = entry; entry->ifIndex = ifIndex; netsnmp_table_row_add_index( row, ASN_INTEGER, /*创建ifTable_entry的索引项,作为一行*/ &(entry->ifIndex), sizeof(entry->ifIndex)); netsnmp_table_data_add_row( table_data, row ); /*将创建的行数据添加到表中*/ return row; } /*初始化表ifTable*/ void initialize_table_ifTable(void) { static oid ifTable_oid[] = {1,3,6,1,2,1,2,2}; size_t ifTable_oid_len = OID_LENGTH(ifTable_oid); netsnmp_handler_registration *reg; netsnmp_table_data *table_data; netsnmp_table_registration_info *table_info; struct netsnmp_table_row_s *row; struct ifTable_entry *entry1; struct ifTable_entry *entry2; /*表数据初始化*/ static struct ifTable_entry tablevalue[]= {{1,2,'3',4,5,6,'7',8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,{0},25}, {2,3,'4',5,6,7,'8',9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,{0},26}}; entry1=&tablevalue[0]; entry2=&tablevalue[1]; // long ifIndex = 1; /*注册一个处理函数ifTable_handler用来处理对对象ifTable的操作*/ reg = netsnmp_create_handler_registration( "ifTable", ifTable_handler, ifTable_oid, ifTable_oid_len, HANDLER_CAN_RWRITE ); table_data = netsnmp_create_table_data( "ifTable" ); table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );/*为表分配地址空间*/ row = SNMP_MALLOC_TYPEDEF( netsnmp_table_row);/*为表中的行数据分配地址空间*/ //entry1=&sr; netsnmp_table_helper_add_indexes(table_info, /* 添加索引项*/ ASN_INTEGER, /* index: ifIndex */ 0); table_info->min_column = 1; table_info->max_column = 25; netsnmp_register_table_data( reg, table_data, table_info );/*利用刚分配的地址空间对表进行注册*/ row=ifTable_createEntry(table_data, /*创建表中第一个entry*/ 1 ); row->data = entry1; /*将entry1添加入行数据*/ row=ifTable_createEntry(table_data, /*创建表中第二个entry*/ 2 ); row->data = entry2; /*将entry2添加入行数据*/ /* 这里初始化表的内容 */ } /* 从表中删除一行 */ void ifTable_removeEntry(netsnmp_table_data *table_data, netsnmp_table_row *row) { //struct ifTable_entry *entry; if (!row) return; /* Nothing to remove */ // entry = (struct ifTable_entry) netsnmp_table_data_remove_and_delete_row( table_data, row ); // if (entry) // SNMP_FREE( entry ); /* XXX - release any other internal resources */ } /*表的处理函数,其中第三个参数reqinfo结构中包含了PDU,管理程序发出的命令,代理返回的结果则通过第四个参数requests返回 */ int ifTable_handler( netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) { netsnmp_request_info *request; netsnmp_table_request_info *table_info; netsnmp_table_data *table_data; netsnmp_table_row *table_row; struct ifTable_entry *table_entry; /*定义各种指向基本数据结构的指针,用来对表数据进行处理 */ switch (reqinfo->mode) { /* * 针对get 和getnext操作 */ case MODE_GET: for (request=requests; request; request=request->next) { table_entry = (struct ifTable_entry *) netsnmp_extract_table_row_data(request); table_info = netsnmp_extract_table_info( request); switch (table_info->colnum) { /*根据查询的对象,通过api函数snmp_set_var_typed_value取出相应数据放入request结构,由函数参数requests结构可得。因为函数参数requests已经把其指针赋给局部变量request了,将值赋给request和requests效果是一样的 */ case COLUMN_IFINDEX: snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (const u_char*) &(table_entry->ifIndex), sizeof(table_entry->ifIndex)); break; case COLUMN_IFDESCR: snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR, (const u_char*) &(table_entry->ifDescr), sizeof(table_entry->ifDescr)); break; case COLUMN_IFTYPE: snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (const u_char*) &(table_entry->ifType), sizeof(table_entry->ifType)); break; case COLUMN_IFMTU: snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (const u_char*) &(table_entry->ifMtu), sizeof(table_entry->ifMtu)); break; case COLUMN_IFSPEED: snmp_set_var_typed_value( request->requestvb, ASN_GAUGE, (const u_char*) &(table_entry->ifSpeed), sizeof(table_entry->ifSpeed)); break; case COLUMN_IFPHYSADDRESS: snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR, (const u_char*) &(table_entry->ifPhysAddress), sizeof(table_entry->ifPhysAddress)); break; case COLUMN_IFADMINSTATUS: snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (const u_char*) &(table_entry->ifAdminStatus), sizeof(table_entry->ifAdminStatus)); break; case COLUMN_IFOPERSTATUS: snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (const u_char*) &(table_entry->ifOperStatus), sizeof(table_entry->ifOperStatus)); break; case COLUMN_IFLASTCHANGE: snmp_set_var_typed_value( request->requestvb, ASN_TIMETICKS, (const u_char*) &(table_entry->ifLastChange), sizeof(table_entry->ifLastChange)); break; case COLUMN_IFINOCTETS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifInOctets), sizeof(table_entry->ifInOctets)); break; case COLUMN_IFINUCASTPKTS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifInUcastPkts), sizeof(table_entry->ifInUcastPkts)); break; case COLUMN_IFINNUCASTPKTS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifInNUcastPkts), sizeof(table_entry->ifInNUcastPkts)); break; case COLUMN_IFINDISCARDS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifInDiscards), sizeof(table_entry->ifInDiscards)); break; case COLUMN_IFINERRORS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifInErrors), sizeof(table_entry->ifInErrors)); break; case COLUMN_IFINUNKNOWNPROTOS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifInUnknownProtos), sizeof(table_entry->ifInUnknownProtos)); break; case COLUMN_IFOUTOCTETS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifOutOctets), sizeof(table_entry->ifOutOctets)); break; case COLUMN_IFOUTUCASTPKTS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifOutUcastPkts), sizeof(table_entry->ifOutUcastPkts)); break; case COLUMN_IFOUTNUCASTPKTS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifOutNUcastPkts), sizeof(table_entry->ifOutNUcastPkts)); break; case COLUMN_IFOUTDISCARDS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifOutDiscards), sizeof(table_entry->ifOutDiscards)); break; case COLUMN_IFOUTERRORS: snmp_set_var_typed_value( request->requestvb, ASN_COUNTER, (const u_char*) &(table_entry->ifOutErrors), sizeof(table_entry->ifOutErrors)); break; case COLUMN_IFOUTQLEN: snmp_set_var_typed_value( request->requestvb, ASN_GAUGE, (const u_char*) &(table_entry->ifOutQLen), sizeof(table_entry->ifOutQLen)); break; case COLUMN_IFSPECIFIC: snmp_set_var_typed_value( request->requestvb, ASN_OBJECT_ID, (const u_char*) &(table_entry->ifSpecific), sizeof(table_entry->ifSpecific)); break; } } break; /* * 针对set操作 */ case MODE_SET_RESERVE1: /*在这一步,并不真正进行set操作,而是做一些输入的检测,如set操作时用户输入的 数据类型是否与想要改变的对象数据类型一致,输入数据是否超出了对象数据类型的限制等。如果发生set操作输入数据发生错误,则在这里可以令函数返回错误类型*/ for (request=requests; request; request=request->next) { table_entry = (struct ifTable_entry *) netsnmp_extract_table_row_data(request); table_info = netsnmp_extract_table_info( request); switch (table_info->colnum) { case COLUMN_IFADMINSTATUS: if ( request->requestvb->type != ASN_INTEGER ) { netsnmp_set_request_error( reqinfo, request, SNMP_ERR_WRONGTYPE ); return SNMP_ERR_NOERROR; } /* Also may need to check size/value */ break; default: netsnmp_set_request_error( reqinfo, request, SNMP_ERR_NOTWRITABLE ); return SNMP_ERR_NOERROR; } } break; case MODE_SET_RESERVE2: /* 这里操作同MOD_SET_RESERVE1*/ break; case MODE_SET_FREE: /* 当前面两个case语句中发生错误时在这里可以做释放资源的操作 */ break; case MODE_SET_ACTION: /* 这里是set操作真正发生作用的地方,为了防止误操作,在改变对象数据之前,先把对象原有的数据保存起来 */ for (request=requests; request; request=request->next) { table_entry = (struct ifTable_entry *) netsnmp_extract_table_row_data(request); table_info = netsnmp_extract_table_info( request); switch (table_info->colnum) { case COLUMN_IFADMINSTATUS: /* Need to save old 'table_entry->ifAdminStatus' value. May need to use 'memcpy' */ table_entry->old_ifAdminStatus = table_entry->ifAdminStatus; // table_entry->ifAdminStatus = request->requestvb->val.integer; snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (const u_char*) request->requestvb->val.string, sizeof(table_entry->ifAdminStatus)); break; } } break; case MODE_SET_UNDO: /* 这里利用上一个case语句中保存的旧数据恢复对象的数据 */ for (request=requests; request; request=request->next) { table_entry = (struct ifTable_entry *) netsnmp_extract_table_row_data(request); table_row = (netsnmp_table_row *) netsnmp_extract_table_row( request); table_data = netsnmp_extract_table( request); table_info = netsnmp_extract_table_info( request); switch (table_info->colnum) { case COLUMN_IFADMINSTATUS: /* Need to restore old 'table_entry->ifAdminStatus' value. May need to use 'memcpy' */ table_entry->ifAdminStatus = table_entry->old_ifAdminStatus; break; } } break; case MODE_SET_COMMIT: break; } return SNMP_ERR_NOERROR; }