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

📄 interfaces.c

📁 Snmp(简单网管协议)软件包。
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *  Interfaces MIB group implementation - interfaces.c * */#include <config.h>#if defined(IFNET_NEEDS_KERNEL) && !defined(_KERNEL) && !defined(IFNET_NEEDS_KERNEL_LATE)#define _KERNEL 1#define _I_DEFINED_KERNEL#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <sys/types.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#if defined(IFNET_NEEDS_KERNEL) && !defined(_KERNEL) && defined(IFNET_NEEDS_KERNEL_LATE)#define _KERNEL 1#define _I_DEFINED_KERNEL#endif#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#ifndef STREAM_NEEDS_KERNEL_ISLANDS#if HAVE_SYS_STREAM_H#include <sys/stream.h>#endif#endif#if HAVE_SYS_SOCKETVAR_H#include <sys/socketvar.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32#  include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#if HAVE_SYS_SOCKIO_H#include <sys/sockio.h>#endif#if HAVE_FCNTL_H#include <fcntl.h>#endif#if HAVE_SYS_IOCTL_H#include <sys/ioctl.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_NET_IF_H#include <net/if.h>#endif#if HAVE_NET_IF_VAR_H#include <net/if_var.h>#endif#ifdef _I_DEFINED_KERNEL#undef _KERNEL#endif#ifdef STREAM_NEEDS_KERNEL_ISLANDS#if HAVE_SYS_STREAM_H#include <sys/stream.h>#endif#endif#if HAVE_NET_ROUTE_H#include <net/route.h>#endif#if HAVE_NETINET_IN_SYSTM_H#include <netinet/in_systm.h>#endif#if HAVE_SYS_HASHING_H#include <sys/hashing.h>#endif#if HAVE_NETINET_IN_VAR_H#include <netinet/in_var.h>#endif#if HAVE_NETINET_IP_H#include <netinet/ip.h>#endif#ifdef INET6#if HAVE_NETINET_IP6_H#include <netinet/ip6.h>#endif#endif#if HAVE_SYS_QUEUE_H#include <sys/queue.h>#endif#if HAVE_NETINET_IP_VAR_H#include <netinet/ip_var.h>#endif#ifdef INET6#if HAVE_NETINET6_IP6_VAR_H#include <netinet6/ip6_var.h>#endif#endif#if HAVE_NETINET_IN_PCB_H#include <netinet/in_pcb.h>#endif#if HAVE_NETINET_IF_ETHER_H#include <netinet/if_ether.h>#endif#if HAVE_NET_IF_TYPES_H#include <net/if_types.h>#endif#if HAVE_NET_IF_DL_H#include <net/if_dl.h>#endif#if HAVE_INET_MIB2_H#include <inet/mib2.h>#endif#if HAVE_IOCTLS_H#include <ioctls.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "mibincl.h"#ifdef solaris2#include "kernel_sunos5.h"#else#include "kernel.h"#endif#ifdef hpux#include <sys/mib.h>#include <netinet/mib_kern.h>#endif /* hpux */#ifdef cygwin#define WIN32#include <windows.h>#endif#if HAVE_SYS_SYSCTL_H#include <sys/sysctl.h>#ifdef freebsd3#    define USE_SYSCTL_IFLIST#else# if defined(CTL_NET) && !defined(freebsd2)#  ifdef PF_ROUTE#   ifdef NET_RT_IFLIST#    ifndef netbsd1#     define USE_SYSCTL_IFLIST#    endif#   endif#  endif# endif#endif /* defined(freebsd3) */#endif /* HAVE_SYS_SYSCTL_H *//* #include "../common_header.h" */#include "system.h"#include "snmp_logging.h"#include "read_config.h"#if HAVE_OSRELDATE_H#include <osreldate.h>#endif#ifdef CAN_USE_SYSCTL#include <sys/sysctl.h>#endif#include "interfaces.h"#include "struct.h"#include "util_funcs.h"#include "auto_nlist.h"#include "sysORTable.h"extern struct timeval starttime;static int Interface_Scan_Get_Count (void);static void parse_interface_config(const char *, char *);static void free_interface_config(void);struct variable4 interfaces_variables[] = {    {IFNUMBER, ASN_INTEGER, RONLY, var_interfaces, 1, {1}},    {IFINDEX, ASN_INTEGER, RONLY, var_ifEntry, 3, {2, 1, 1}},    {IFDESCR, ASN_OCTET_STR, RONLY, var_ifEntry, 3, {2, 1, 2}},    {IFTYPE, ASN_INTEGER, RONLY, var_ifEntry, 3, {2, 1, 3}},    {IFMTU, ASN_INTEGER, RONLY, var_ifEntry, 3, {2, 1, 4}},    {IFSPEED, ASN_GAUGE, RONLY, var_ifEntry, 3, {2, 1, 5}},    {IFPHYSADDRESS, ASN_OCTET_STR, RONLY, var_ifEntry, 3, {2, 1, 6}},#ifdef WIN32    {IFADMINSTATUS, ASN_INTEGER, RWRITE, var_ifEntry, 3, {2, 1, 7}},#else    {IFADMINSTATUS, ASN_INTEGER, RONLY, var_ifEntry, 3, {2, 1, 7}},#endif    {IFOPERSTATUS, ASN_INTEGER, RONLY, var_ifEntry, 3, {2, 1, 8}},    {IFLASTCHANGE, ASN_TIMETICKS, RONLY, var_ifEntry, 3, {2, 1, 9}},    {IFINOCTETS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 10}},    {IFINUCASTPKTS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 11}},    {IFINNUCASTPKTS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 12}},    {IFINDISCARDS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 13}},    {IFINERRORS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 14}},    {IFINUNKNOWNPROTOS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 15}},    {IFOUTOCTETS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 16}},    {IFOUTUCASTPKTS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 17}},    {IFOUTNUCASTPKTS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 18}},    {IFOUTDISCARDS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 19}},    {IFOUTERRORS, ASN_COUNTER, RONLY, var_ifEntry, 3, {2, 1, 20}},    {IFOUTQLEN, ASN_GAUGE, RONLY, var_ifEntry, 3, {2, 1, 21}},    {IFSPECIFIC, ASN_OBJECT_ID, RONLY, var_ifEntry, 3, {2, 1, 22}}};/* Define the OID pointer to the top of the mib tree that we're   registering underneath, and the OID of the MIB module */oid interfaces_variables_oid[] = { SNMP_OID_MIB2,2 };oid interfaces_module_oid[]    = { SNMP_OID_MIB2,31 };void init_interfaces(void){  /* register ourselves with the agent to handle our mib tree */  REGISTER_MIB("mibII/interfaces", interfaces_variables, variable4, \               interfaces_variables_oid);  REGISTER_SYSOR_ENTRY(interfaces_module_oid,	"The MIB module to describe generic objects for network interface sub-layers");    snmpd_register_config_handler("interface", parse_interface_config,    				free_interface_config, "name type speed");#ifndef USE_SYSCTL_IFLIST#if HAVE_NET_IF_MIB_H  init_interfaces_setup();#endif#endif}/* * if_type_from_name * Return interface type using the interface name as a clue. * Returns 1 to imply "other" type if name not recognized.  */static intif_type_from_name( const char *pcch){    typedef struct _match_if {    	int mi_type;    	const char *mi_name;    } *pmatch_if, match_if;        static match_if lmatch_if[] = {      { 24, "lo" },      {  6, "eth" },      {  9, "tr" },      { 23, "ppp" },      { 28, "sl" },      {  0, 0 }  /* end of list */    };    int ii, len;    register pmatch_if pm;    for (ii = 0, pm=lmatch_if; pm->mi_name; pm++) {        len = strlen(pm->mi_name);        if (0 == strncmp(pcch, pm->mi_name, len))        {            return (pm->mi_type);        }    }    return (1); /* in case search fails */}typedef struct _conf_if_list {    char *name;    int type;    u_long speed;    struct _conf_if_list *next;} conf_if_list;static conf_if_list *conf_list;static struct ifnet *ifnetaddr_list;static void parse_interface_config(const char *token, char *cptr){    conf_if_list *if_ptr, *if_new;    char *name, *type, *speed, *ecp;    name = strtok(cptr, " \t");    if (!name) {	config_perror("Missing NAME parameter");	return;    }    type = strtok(NULL, " \t");    if (!type) {	config_perror("Missing TYPE parameter");	return;    }    speed = strtok(NULL, " \t");    if (!speed) {	config_perror("Missing SPEED parameter");	return;    }    if_ptr = conf_list;    while (if_ptr)	if (strcmp(if_ptr->name, name)) if_ptr = if_ptr->next;	else break;    if (if_ptr)	config_pwarn("Duplicate interface specification");    if_new = (conf_if_list *)malloc(sizeof(conf_if_list));    if (!if_new) {	config_perror("Out of memory");	return;    }    if_new->speed = strtoul(speed, &ecp, 0);    if (*ecp) {	config_perror("Bad SPEED value");	free(if_new);	return;    }    if_new->type = strtol(type, &ecp, 0);    if (*ecp || if_new->type < 0) {	config_perror("Bad TYPE");	free(if_new);	return;    }    if_new->name = strdup(name);    if (!if_new->name) {	config_perror("Out of memory");	free(if_new);	return;    }    if_new->next = conf_list;    conf_list = if_new;}static void free_interface_config(void){    conf_if_list *if_ptr = conf_list, *if_next;    while (if_ptr) {	if_next = if_ptr->next;	free(if_ptr->name);	free(if_ptr);	if_ptr = if_next;    }    conf_list = NULL;}/*  header_ifEntry(...  Arguments:  vp	  IN      - pointer to variable entry that points here  name    IN/OUT  - IN/name requested, OUT/name found  length  IN/OUT  - length of IN/OUT oid's   exact   IN      - TRUE if an exact match was requested  var_len OUT     - length of variable or 0 if function returned  write_method  */#ifndef WIN32static intheader_ifEntry(struct variable *vp,	       oid *name,	       size_t *length,	       int exact,	       size_t *var_len,	       WriteMethod **write_method){#define IFENTRY_NAME_LENGTH	10    oid newname[MAX_OID_LEN];    register int	interface;    int result, count;    DEBUGMSGTL(("mibII/interfaces", "var_ifEntry: "));    DEBUGMSGOID(("mibII/interfaces", name, *length));    DEBUGMSG(("mibII/interfaces"," %d\n", exact));        memcpy( (char *)newname,(char *)vp->name, (int)vp->namelen * sizeof(oid));    /* find "next" interface */    count = Interface_Scan_Get_Count();    for(interface = 1; interface <= count; interface++){	newname[IFENTRY_NAME_LENGTH] = (oid)interface;	result = snmp_oid_compare(name, *length, newname, (int)vp->namelen + 1);	if ((exact && (result == 0)) || (!exact && (result < 0)))	    break;    }    if (interface > count) {        DEBUGMSGTL(("mibII/interfaces", "... index out of range\n"));        return MATCH_FAILED;    }    memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid));    *length = vp->namelen + 1;    *write_method = 0;    *var_len = sizeof(long);	/* default to 'long' results */    DEBUGMSGTL(("mibII/interfaces", "... get I/F stats "));    DEBUGMSGOID(("mibII/interfaces", name, *length));    DEBUGMSG(("mibII/interfaces","\n"));    return interface;}u_char *var_interfaces(struct variable *vp,	       oid *name,	       size_t *length,	       int exact,	       size_t *var_len,	       WriteMethod **write_method){  if (header_generic(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )    return NULL;  switch (vp->magic)    {    case IFNUMBER:      long_return = Interface_Scan_Get_Count ();      return (u_char *)&long_return;    default:      DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_interfaces\n", vp->magic));    }  return NULL;}#ifdef USE_SYSCTL_IFLISTstatic u_char * if_list = 0;static const u_char * if_list_end;static size_t if_list_size = 0;struct small_ifaddr{  struct in_addr	sifa_addr;  struct in_addr	sifa_netmask;  struct in_addr	sifa_broadcast;};extern const struct sockaddr * get_address (const void *, int, int);extern const struct in_addr * get_in_address (const void *, int, int);static int Interface_Scan_By_Index (int, struct if_msghdr *, char *, struct small_ifaddr *);static int Interface_Get_Ether_By_Index (int, u_char *);static intInterface_Scan_By_Index (int iindex,			 struct if_msghdr *if_msg,			 char *if_name,			 struct small_ifaddr *sifa){  u_char *cp;  struct if_msghdr *ifp;  int have_ifinfo = 0, have_addr = 0;

⌨️ 快捷键说明

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