📄 interface_sysctl.c
字号:
/* * Interface MIB architecture support * * Based on patch 1362403, submited by Rojer * * $Id: interface_sysctl.c 16552 2007-06-20 21:34:24Z tanders $ */#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>#include "mibII/mibII_common.h"#include "if-mib/ifTable/ifTable_constants.h"#include <net-snmp/agent/net-snmp-agent-includes.h>#if HAVE_SYS_IOCTL_H#include <sys/ioctl.h>#else#error "BSD should have sys/ioctl header"#endif#include <net-snmp/data_access/interface.h>#include <net-snmp/data_access/ipaddress.h>#include "if-mib/data_access/interface.h"#include <sys/types.h>#include <sys/time.h>#include <net/if.h>#include <net/if_dl.h>#include <net/if_types.h>#include <net/if_media.h>/* * account for minor differences between FreeBSD and OpenBSD. * If this gets unruly, split stuff out into the respective * files (interface_*BDS.c). *//* * FreeBSD has 2 promiscuous flags: kernel/user; check either * http://unix.derkeiler.com/Mailing-Lists/FreeBSD/net/2004-09/0289.html * which says: * The first flag (IFF_PROMISC) is the one that the kernel code uses and * sets on an interface's ifp structure. The second one is the one that * comes from user space programs and is sent to the routine ifhwioctl() * to set the first flag. */#ifdef IFF_PPROMISC# define ARCH_PROMISC_FLAG (IFF_PPROMISC|IFF_PROMISC)#else# define ARCH_PROMISC_FLAG IFF_PROMISC#endifextern struct timeval starttime;/* sa_len roundup macro. */#define ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))voidnetsnmp_arch_interface_init(void){ /* * nothing to do */}/* * find the ifIndex for an interface name * * @retval 0 : no index found * @retval >0: ifIndex for interface */oidnetsnmp_arch_interface_index_find(const char *name){ return if_nametoindex(name);}/* * subroutine to translate known media typed to speed. * see /usr/include/net/if_media.h for definitions */voidnetsnmp_sysctl_ifmedia_to_speed(int media, u_int *speed, u_int *speed_high){ *speed = 0; *speed_high = 0; switch (IFM_TYPE(media)) { case IFM_ETHER: switch (IFM_SUBTYPE(media)) { case IFM_10_T: case IFM_10_2: case IFM_10_5: case IFM_10_STP: case IFM_10_FL: *speed = 10000000; *speed_high = 10; break; case IFM_100_TX: case IFM_100_FX: case IFM_100_T4: case IFM_100_VG: case IFM_100_T2: *speed = 100000000; *speed_high = 100; break; case IFM_1000_LX: case IFM_1000_CX:#ifdef IFM_1000_T case IFM_1000_T:#endif *speed = 1000000000; *speed_high = 1000; break;#ifdef IFM_10GBASE_SR case IFM_10GBASE_SR: case IFM_10GBASE_LR: *speed = (u_int) -1; /* 4294967295; */ *speed_high = 10000; break;#endif } break; case IFM_IEEE80211: switch (IFM_SUBTYPE(media)) { case IFM_IEEE80211_FH1: case IFM_IEEE80211_DS1: *speed = 1000000; *speed_high = 1; break; case IFM_IEEE80211_FH2: case IFM_IEEE80211_DS2: *speed = 2000000; *speed_high = 2; break; case IFM_IEEE80211_DS5: *speed = 5500000; *speed_high = 5; break; case IFM_IEEE80211_DS11: *speed = 11000000; *speed_high = 11; break; case IFM_IEEE80211_DS22: *speed = 22000000; *speed_high = 22; break;#ifdef IFM_IEEE80211_OFDM6 case IFM_IEEE80211_OFDM6: *speed = 6000000; *speed_high = 6; break; case IFM_IEEE80211_OFDM9: *speed = 9000000; *speed_high = 9; break; case IFM_IEEE80211_OFDM12: *speed = 12000000; *speed_high = 12; break; case IFM_IEEE80211_OFDM18: *speed = 18000000; *speed_high = 18; break; case IFM_IEEE80211_OFDM24: *speed = 24000000; *speed_high = 24; break; case IFM_IEEE80211_OFDM36: *speed = 36000000; *speed_high = 36; break; case IFM_IEEE80211_OFDM48: *speed = 48000000; *speed_high = 48; break; case IFM_IEEE80211_OFDM54: *speed = 54000000; *speed_high = 54; break; case IFM_IEEE80211_OFDM72: *speed = 72000000; *speed_high = 72; break;#endif } break; case IFM_TOKEN: switch (IFM_SUBTYPE(media)) { case IFM_TOK_STP4: case IFM_TOK_UTP4: *speed = 4000000; *speed_high = 4; break; case IFM_TOK_STP16: case IFM_TOK_UTP16: *speed = 16000000; *speed_high = 16; break;#if defined(IFM_TOK_STP100) /* guessing if you've got one, you've got the other.. */ case IFM_TOK_STP100: case IFM_TOK_UTP100: *speed = 100000000; *speed_high = 100; break;#endif } break;#ifdef IFM_ATM case IFM_ATM: switch (IFM_SUBTYPE(media)) { case IFM_ATM_MM_155: case IFM_ATM_SM_155: *speed = 155000000; *speed_high = 155; break; case IFM_ATM_MM_622: case IFM_ATM_SM_622: *speed = 622000000; *speed_high = 622; break; }#endif } return;}/* * @retval 0 speed could not be determined (error, unknown media) * @retval >0 speed, equal to *speed. * sets *speed=2^31 and returns *speed_high=speed/10^6 as required * by ifSpeed/ifHighSpeed. */intnetsnmp_sysctl_get_if_speed(char *name, u_int *speed, u_int *speed_high){ int s; struct ifmediareq ifmr; int *media_list, i; u_int t_speed, t_speed_high; u_int m_speed, m_speed_high; *speed = 0; *speed_high = 0; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return 0; } (void) memset(&ifmr, 0, sizeof(ifmr)); (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); DEBUGMSGTL(("access:interface:container:sysctl"," speed in\n")); if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0 || ifmr.ifm_count == 0) { close(s); return 0; } /* * try to get speed from current media. * if unsuccessful (e.g., interface is down), get a list of capabilities, * try each and return maximum speed the interface is capable of. */ netsnmp_sysctl_ifmedia_to_speed(ifmr.ifm_current, speed, speed_high); if (*speed == 0 && (media_list = (int *) malloc(ifmr.ifm_count * sizeof(int))) != NULL ) { ifmr.ifm_ulist = media_list; if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == 0) { m_speed = 0; m_speed_high = 0; for (i = 0; i < ifmr.ifm_count; i++) { netsnmp_sysctl_ifmedia_to_speed(media_list[i], &t_speed, &t_speed_high); if (t_speed_high > m_speed_high || (t_speed_high == m_speed_high && t_speed > t_speed)) { m_speed_high = t_speed_high;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -