⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statable.c

📁 一个学习SNMP项目:tmoerlan.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*	Copyright (c) 2003, Tijmen Moerland <moerland@yahoo.com>	Code is licensed under the BSD license.*//*!	\file staTable.c	\brief Implements the l80211IfaceStaTable table.			Since the structure of this implementation is similar to if80211Table.c, please refer to that file.	Do note that this table is double-indexed, the first index being the wireless interface number, the second a simple increasing counter starting at one indicating the station on which information is available.	Also refer to the comments within the code.*//* * Note: this file originally auto-generated by mib2c using *        : mib2c.iterate.conf,v 5.4 2002/09/11 22:42:04 hardaker Exp $ */#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "staTable.h"#include "lib80211.h"#include <sys/time.h>#include <sys/timeb.h>#define CACHETIME 5	 // secondsstruct staInfo {	long index;	struct wi_sigextcache *cache;	int assoc;	//1 = true, 2 = false;	int auth;	//1 = true, 2 = false;};struct ifaceInfo {	struct wi_sigextcache *sigextcache;	int wi_sigitems;	struct timeval cacheTimeStamp;	struct staInfo sta[MAXWICACHE];	/* this should be WIHAP_MAX_STATIONS, but we can only have					MAXWICACHE stations in the cache per interface anyway */};struct ifaceInfo ifaceInfoList[IF_MAXNR];long global_index, if_index;long if_nr;/** Initialize the staTable table by defining its contents and how it's structured */voidinitialize_table_staTable(void){    static oid staTable_oid[] = {1,3,6,1,4,1,18003,2,1,1,4};    netsnmp_table_registration_info *table_info;    netsnmp_handler_registration *my_handler;    netsnmp_iterator_info *iinfo;    /* create the table structure itself */    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);    iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);    /* if your table is read only, it's easiest to change the       HANDLER_CAN_RWRITE definition below to HANDLER_CAN_RONLY */    my_handler = netsnmp_create_handler_registration("staTable",                                             staTable_handler,                                             staTable_oid,                                             OID_LENGTH(staTable_oid),                                             HANDLER_CAN_RWRITE);                if (!my_handler || !table_info || !iinfo)        return; /* mallocs failed */    /***************************************************     * Setting up the table's definition     */    netsnmp_table_helper_add_indexes(table_info,                                  ASN_INTEGER, /* index: ifIndex */				  ASN_INTEGER, /* index: l80211StaIndex */                             0);    table_info->min_column = 2;    table_info->max_column = 23;    /* iterator access routines */    iinfo->get_first_data_point = staTable_get_first_data_point;    iinfo->get_next_data_point = staTable_get_next_data_point;//    iinfo->free_loop_contet = staTable_free_loop_context;    iinfo->table_reginfo = table_info;    /***************************************************     * registering the table with the master agent     */    DEBUGMSGTL(("staTable",                "Registering table staTable as a table iterator\n"));		     netsnmp_register_table_iterator(my_handler, iinfo);     bzero((char *) ifaceInfoList, IF_MAXNR * sizeof(struct ifaceInfo));}/** Initializes the staTable module */voidinit_staTable(void){  /* here we initialize all the tables we're planning on supporting */    initialize_table_staTable();}netsnmp_variable_list *staTable_get_first_data_point(void **my_loop_context, void **my_data_context,                          netsnmp_variable_list *put_index_data,                          netsnmp_iterator_info *mydata){	int i, flags = 0;	char buf[IF_NAMELEN];	struct timeval now;	    	netsnmp_variable_list *vptr;//init index		global_index = 1;	//start from index 1	if_index = 0;	if_nr = getNumIf80211();	struct ifaceInfo *currIface;		if (if_nr <= 0)		return NULL;	do	{		if_index++;		currIface = &ifaceInfoList[if_index-1];		gettimeofday(&now, NULL);        	if (now.tv_sec - currIface->cacheTimeStamp.tv_sec >= CACHETIME) {                	DEBUGMSGTL(("staTable", "old copy = %ld, getting fresh copy of extcache: %ld\n", currIface->cacheTimeStamp.tv_sec,                        	now.tv_sec));	    		currIface->wi_sigitems = getCacheItemnr(getNameIf80211(if_index, buf));			if (currIface->wi_sigitems > 0) {				if (currIface->sigextcache != NULL)					free(currIface->sigextcache);				currIface->sigextcache = (struct wi_sigextcache *) malloc(currIface->wi_sigitems * sizeof(struct wi_sigextcache));				getExtendedCache(getNameIf80211(if_index, buf), currIface->sigextcache, currIface->wi_sigitems);                	}			currIface->cacheTimeStamp.tv_sec = now.tv_sec;	                currIface->cacheTimeStamp.tv_usec = now.tv_usec;        	}	        else {        	        DEBUGMSGTL(("staTable", "using old copy of extcache, age=%f seconds\n",                	        now.tv_sec - currIface->cacheTimeStamp.tv_sec +                        	1e-6 * (now.tv_usec - currIface->cacheTimeStamp.tv_usec)));	        }	}	while (if_index < if_nr && currIface->wi_sigitems <= 0);	DEBUGMSGTL(("staTable", "found index %d\n", if_index));	if (if_index > if_nr)	//all interfaces where have no cache items		return NULL;//put the pointer to this cache into the corresponding staInfoList	currIface->sta[global_index].index = global_index;	currIface->sta[global_index].cache = currIface->sigextcache + (global_index-1)*sizeof(struct wi_sigextcache);//now we need to find out about assoc and auth//if the same mac address from the cache is found in the//active stations list we can look up assoc/auth//if not, both are false//the next function returns the flags for this station, and 0 if//station is not in the list	flags = getStaFlags(getNameIf80211(if_index, buf), currIface->sta[global_index].cache->macsrc);	if (flags & HOSTAP_FLAGS_ASSOC)		currIface->sta[global_index].assoc = 1;	else		currIface->sta[global_index].assoc = 2;	if (flags & HOSTAP_FLAGS_AUTHEN)		currIface->sta[global_index].auth = 1;	else		currIface->sta[global_index].auth = 2;    *my_loop_context = (void *) &global_index;    *my_data_context = (void *) &currIface->sta[global_index];//    DEBUGMSGTL(("staTable", "START: index=(%ld, %ld)\n", global_index, if_index));    vptr = put_index_data;        static long ifIndex;    ifIndex = getPtrIf80211(if_index);     snmp_set_var_value(vptr, (u_char *) &ifIndex, sizeof(long));    vptr = vptr->next_variable;    snmp_set_var_value(vptr, (u_char *) &global_index, sizeof(long));    return put_index_data;}netsnmp_variable_list *staTable_get_next_data_point(void **my_loop_context, void **my_data_context,                         netsnmp_variable_list *put_index_data,                         netsnmp_iterator_info *mydata){	int flags = 0;	char buf[IF_NAMELEN];	struct timeval now;		struct ifaceInfo *currIface = &ifaceInfoList[if_index - 1];    netsnmp_variable_list *vptr;//	DEBUGMSGTL(("staTable", "start of get_next_data_point"));	DEBUGMSGTL(("staTable", "*wi_sigitems=%d\n", currIface->wi_sigitems));	if (global_index >= currIface->wi_sigitems) {		//if there's no more data left,		//we leave loop_context and data_context alone		//and return NULL;		DEBUGMSGTL(("staTable", "done all cache items for iface %d\n", if_index));		if (if_index >= if_nr)	    		return NULL;		else {			DEBUGMSGTL(("staTable", "searching for next iface with cache items...\n"));			do {				if_index++;				DEBUGMSGTL(("staTable", "trying %d\t", if_index));				currIface = &ifaceInfoList[if_index-1];				gettimeofday(&now, NULL);        	                if (now.tv_sec - currIface->cacheTimeStamp.tv_sec >= CACHETIME) {                	                DEBUGMSGTL(("staTable", "old copy = %ld, getting fresh copy of extcache: %ld\n", currIface->cacheTimeStamp.tv_sec,			              	                now.tv_sec));					currIface->wi_sigitems = getCacheItemnr(getNameIf80211(if_index, buf));		                        if (currIface->wi_sigitems > 0) {                		                if (currIface->sigextcache != NULL)		                                        free(currIface->sigextcache);						currIface->sigextcache = (struct wi_sigextcache *) malloc(currIface->wi_sigitems * sizeof(struct wi_sigextcache));                                		getExtendedCache(getNameIf80211(if_index, buf), currIface->sigextcache, currIface->wi_sigitems); 					}                        	                                currIface->cacheTimeStamp.tv_sec = now.tv_sec;        	                        currIface->cacheTimeStamp.tv_usec = now.tv_usec;                	        }                        	else {                                	DEBUGMSGTL(("staTable", "using old copy of extcache, age=%f seconds\n",                                        	now.tv_sec - currIface->cacheTimeStamp.tv_sec +                                        	1e-6 * (now.tv_usec - currIface->cacheTimeStamp.tv_usec)));	                        }				DEBUGMSGTL(("staTable", "sigitems=%d\n", currIface->wi_sigitems));			} while (if_index < if_nr && currIface->wi_sigitems <= 0);			if (if_index > if_nr)				return NULL;			global_index = 1;		}	}	else {		global_index++;//		currIface->sigextcache++;	}	DEBUGMSGTL(("staTable", "next: doing iface %d, station %d: %x:%x:%x\n", if_index, global_index, 		currIface->sigextcache->macsrc[1]&0xff, currIface->sigextcache->macsrc[2]&0xff, 		currIface->sigextcache->macsrc[2]&0xff));//put the pointer to this cache into the corresponding staInfoList	currIface->sta[global_index].index = global_index;	currIface->sta[global_index].cache = currIface->sigextcache + (global_index-1);//now we need to find out about assoc and auth//if the same mac address from the cache is found in the//active stations list we can look up assoc/auth//if not, both are false//the next function returns the flags for this station, and 0 if//station is not in the list	flags = getStaFlags(getNameIf80211(if_index, buf), currIface->sta[global_index].cache->macsrc);	if (flags & HOSTAP_FLAGS_ASSOC)                currIface->sta[global_index].assoc = 1;        else                currIface->sta[global_index].assoc = 2;        if (flags & HOSTAP_FLAGS_AUTHEN)                currIface->sta[global_index].auth = 1;        else                currIface->sta[global_index].auth = 2;		    *my_loop_context = (void *) &global_index;    *my_data_context = (void *) &currIface->sta[global_index];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -