📄 interfaces.c
字号:
long_return = (u_long) 0; /* XXX */#endif return (u_char *) &long_return; case IFOUTDISCARDS: long_return = ifnet.if_snd.ifq_drops; return (u_char *) &long_return; case IFOUTERRORS: long_return = ifnet.if_oerrors; return (u_char *) &long_return; case IFOUTQLEN: long_return = ifnet.if_snd.ifq_len; return (u_char *) &long_return; case IFSPECIFIC: *var_len = nullOidLen; return (u_char *) nullOid; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ifEntry\n", vp->magic)); } return NULL;}#else /* hpux */u_char *var_ifEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static struct ifnet ifnet; register int interface; static struct in_ifaddr in_ifaddrVar; static char Name[16]; register char *cp;#if STRUCT_IFNET_HAS_IF_LASTCHANGE_TV_SEC struct timeval now;#endif struct nmparms hp_nmparms; static mib_ifEntry hp_ifEntry; int hp_fd; int hp_len=sizeof(hp_ifEntry); interface = header_ifEntry(vp, name, length, exact, var_len, write_method); if ( interface == MATCH_FAILED ) return NULL; Interface_Scan_By_Index(interface, Name, &ifnet, &in_ifaddrVar); /* * Additional information about the interfaces is available under * HP-UX through the network management interface '/dev/netman' */ hp_ifEntry.ifIndex = interface; hp_nmparms.objid = ID_ifEntry; hp_nmparms.buffer = (char *)&hp_ifEntry; hp_nmparms.len = &hp_len; if ((hp_fd=open("/dev/netman", O_RDONLY)) != -1 ) { if (ioctl(hp_fd, NMIOGET, &hp_nmparms) != -1 ) { close(hp_fd); } else { close(hp_fd); hp_fd = -1; /* failed */ } } switch (vp->magic){ case IFINDEX: long_return = interface; return (u_char *) &long_return; case IFDESCR: if ( hp_fd != -1 ) cp = hp_ifEntry.ifDescr; else cp = Name; *var_len = strlen(cp); return (u_char *)cp; case IFTYPE: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifType; else long_return = 1; /* OTHER */ return (u_char *) &long_return; case IFMTU: { long_return = (long) ifnet.if_mtu; return (u_char *) &long_return; } case IFSPEED: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifSpeed; else long_return = (u_long) 1; /* OTHER */ return (u_char *) &long_return; case IFPHYSADDRESS: Interface_Get_Ether_By_Index(interface, return_buf); *var_len = 6; if ((return_buf[0] == 0) && (return_buf[1] == 0) && (return_buf[2] == 0) && (return_buf[3] == 0) && (return_buf[4] == 0) && (return_buf[5] == 0)) *var_len = 0; return(u_char *) return_buf; case IFADMINSTATUS: long_return = ifnet.if_flags & IFF_UP ? 1 : 2; return (u_char *) &long_return; case IFOPERSTATUS: long_return = ifnet.if_flags & IFF_RUNNING ? 1 : 2; return (u_char *) &long_return; case IFLASTCHANGE: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifLastChange; else long_return = 0; /* XXX */ return (u_char *) &long_return; case IFINOCTETS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifInOctets; else long_return = (u_long) ifnet.if_ipackets * 308; /* XXX */ return (u_char *) &long_return; case IFINUCASTPKTS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifInUcastPkts; else long_return = (u_long) ifnet.if_ipackets; return (u_char *) &long_return; case IFINNUCASTPKTS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifInNUcastPkts; else long_return = (u_long) 0; /* XXX */ return (u_char *) &long_return; case IFINDISCARDS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifInDiscards; else long_return = (u_long) 0; /* XXX */ return (u_char *) &long_return; case IFINERRORS: return (u_char *) &ifnet.if_ierrors; case IFINUNKNOWNPROTOS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifInUnknownProtos; else long_return = (u_long) 0; /* XXX */ return (u_char *) &long_return; case IFOUTOCTETS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifOutOctets; else long_return = (u_long) ifnet.if_opackets * 308; /* XXX */ return (u_char *) &long_return; case IFOUTUCASTPKTS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifOutUcastPkts; else long_return = (u_long) ifnet.if_opackets; return (u_char *) &long_return; case IFOUTNUCASTPKTS: if ( hp_fd != -1 ) long_return = hp_ifEntry.ifOutNUcastPkts; else long_return = (u_long) 0; /* XXX */ return (u_char *) &long_return; case IFOUTDISCARDS: return (u_char *) &ifnet.if_snd.ifq_drops; case IFOUTERRORS: return (u_char *) &ifnet.if_oerrors; case IFOUTQLEN: return (u_char *) &ifnet.if_snd.ifq_len; case IFSPECIFIC: *var_len = nullOidLen; return (u_char *) nullOid; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ifEntry\n", vp->magic)); } return NULL;}#endif /* hpux */#else /* solaris2 */static intIF_cmp(void *addr, void *ep){ DEBUGMSGTL(("mibII/interfaces", "... IF_cmp %d %d\n", ((mib2_ifEntry_t *)ep)->ifIndex, ((mib2_ifEntry_t *)addr)->ifIndex)); if (((mib2_ifEntry_t *)ep)->ifIndex == ((mib2_ifEntry_t *)addr)->ifIndex) return (0); else return (1);}u_char *var_ifEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ int interface; mib2_ifEntry_t ifstat; interface = header_ifEntry(vp, name, length, exact, var_len, write_method); if ( interface == MATCH_FAILED ) return NULL; if (getMibstat(MIB_INTERFACES, &ifstat, sizeof(mib2_ifEntry_t), GET_EXACT, &IF_cmp, &interface) != 0) { DEBUGMSGTL(("mibII/interfaces", "... no mib stats\n")); return NULL; } switch (vp->magic){ case IFINDEX: long_return = ifstat.ifIndex; return (u_char *) &long_return; case IFDESCR: *var_len = ifstat.ifDescr.o_length; (void)memcpy(return_buf, ifstat.ifDescr.o_bytes, *var_len); return(u_char *)return_buf; case IFTYPE: long_return = (u_long)ifstat.ifType; return (u_char *) &long_return; case IFMTU: long_return = (u_long)ifstat.ifMtu; return (u_char *) &long_return; case IFSPEED: long_return = (u_long)ifstat.ifSpeed; return (u_char *) &long_return; case IFPHYSADDRESS: *var_len = ifstat.ifPhysAddress.o_length; (void)memcpy(return_buf, ifstat.ifPhysAddress.o_bytes, *var_len); return(u_char *)return_buf; case IFADMINSTATUS: long_return = (u_long)ifstat.ifAdminStatus; return (u_char *) &long_return; case IFOPERSTATUS: long_return = (u_long)ifstat.ifOperStatus; return (u_char *) &long_return; case IFLASTCHANGE: long_return = (u_long)ifstat.ifLastChange; return (u_char *) &long_return; case IFINOCTETS: long_return = (u_long)ifstat.ifInOctets; return (u_char *) &long_return; case IFINUCASTPKTS: long_return = (u_long)ifstat.ifInUcastPkts; return (u_char *) &long_return; case IFINNUCASTPKTS: long_return = (u_long)ifstat.ifInNUcastPkts; return (u_char *) &long_return; case IFINDISCARDS: long_return = (u_long)ifstat.ifInDiscards; return (u_char *) &long_return; case IFINERRORS: long_return = (u_long)ifstat.ifInErrors; return (u_char *) &long_return; case IFINUNKNOWNPROTOS: long_return = (u_long)ifstat.ifInUnknownProtos; return (u_char *) &long_return; case IFOUTOCTETS: long_return = (u_long)ifstat.ifOutOctets; return (u_char *) &long_return; case IFOUTUCASTPKTS: long_return = (u_long)ifstat.ifOutUcastPkts; return (u_char *) &long_return; case IFOUTNUCASTPKTS: long_return = (u_long)ifstat.ifOutNUcastPkts; return (u_char *) &long_return; case IFOUTDISCARDS: long_return = (u_long)ifstat.ifOutDiscards; return (u_char *) &long_return; case IFOUTERRORS: long_return = (u_long)ifstat.ifOutErrors; return (u_char *) &long_return; case IFOUTQLEN: long_return = (u_long)ifstat.ifOutQLen; return (u_char *) &long_return; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ifEntry\n", vp->magic)); } return NULL;}#endif /* solaris2 */ /********************* * * Internal implementation functions * *********************/#ifndef solaris2#if !defined(sunV3) && !defined(linux)static struct in_ifaddr savein_ifaddr;#endifstatic struct ifnet *ifnetaddr, saveifnet, *saveifnetaddr;static int saveIndex=0;static char saveName[16];voidInterface_Scan_Init (void){#ifdef linux char line [256], ifname_buf [64], *ifname, *ptr; struct ifreq ifrq; struct ifnet **ifnetaddr_ptr; FILE *devin; unsigned long rec_pkt, rec_oct, rec_err, snd_pkt, snd_oct, snd_err, coll; int i, fd; conf_if_list *if_ptr; const char *scan_line_2_2="%lu %lu %lu %*lu %*lu %*lu %*lu %*lu %lu %lu %lu %*lu %*lu %lu"; const char *scan_line_2_0="%lu %lu %*lu %*lu %*lu %lu %lu %*lu %*lu %lu"; const char *scan_line_to_use; #endif auto_nlist(IFNET_SYMBOL, (char *)&ifnetaddr, sizeof(ifnetaddr)); saveIndex=0;#ifdef linux /* free old list: */ while (ifnetaddr_list) { struct ifnet *old = ifnetaddr_list; ifnetaddr_list = ifnetaddr_list->if_next; free (old->if_name); free (old->if_unit); free (old); } ifnetaddr = 0; ifnetaddr_ptr = &ifnetaddr_list; if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { DEBUGMSGTL(("snmpd", "socket open failure in Interface_Scan_Init\n")); return; /** exit (1); **/ } /* * build up ifnetaddr list by hand: */ /* at least linux v1.3.53 says EMFILE without reason... */ if (! (devin = fopen ("/proc/net/dev", "r"))) { close (fd); snmp_log(LOG_ERR,"cannot open /proc/net/dev - continuing...\n"); return; /** exit (1); **/ } i = 0; /* read the second line (a header) and determine the fields we should read from. This should be done in a better way by actually looking for the field names we want. But thats too much work for today. -- Wes */ fgets(line, sizeof(line), devin); fgets(line, sizeof(line), devin); if (strstr(line, "compressed")) { scan_line_to_use = scan_line_2_2; DEBUGMSGTL(("mibII/interfaces", "using linux 2.2 kernel /proc/net/dev\n")); } else { scan_line_to_use = scan_line_2_0; DEBUGMSGTL(("mibII/interfaces", "using linux 2.0 kernel /proc/net/dev\n")); } while (fgets (line, sizeof(line), devin)) { struct ifnet *nnew; char *stats, *ifstart = line; if ( line[strlen(line)-1] == '\n' ) line[strlen(line)-1]='\0'; while (*ifstart == ' ') ifstart++; if ( (stats = strrchr(ifstart, ':')) == NULL ) { snmp_log(LOG_ERR,"/proc/net/dev data format error, line ==|%s|",line); continue; } if ( (scan_line_to_use == scan_line_2_2) && ( (stats-line) < 6 ) ) { snmp_log(LOG_ERR,"/proc/net/dev data format error, line ==|%s|",line); } *stats++ = 0; strcpy(ifname_buf, ifstart); while (*stats == ' ') stats++; if ((scan_line_to_use == scan_line_2_2 && sscanf (stats, scan_line_to_use, &rec_oct, &rec_pkt, &rec_err,&snd_oct, &snd_pkt, &snd_err, &coll) != 7) || (scan_line_to_use == scan_line_2_0 && sscanf (stats, scan_line_to_use, &rec_pkt, &rec_err, &snd_pkt,&snd_err, &coll) != 5)) { if ( (scan_line_to_use == scan_line_2_2) && !strstr(line,"No statistics available") ) snmp_log(LOG_ERR,"/proc/net/dev data format error, line ==|%s|",line); continue; } nnew = (struct ifnet *) calloc (1, sizeof (struct ifnet)); if (nnew == NULL) break; /* alloc error */ /* chain in: */ *ifnetaddr_ptr = nnew; ifnetaddr_ptr = &nnew->if_next; i++; /* linux previous to 1.3.~13 may miss transmitted loopback pkts: */ if (! strcmp (ifname_buf, "lo") && rec_pkt > 0 && ! snd_pkt) snd_pkt = rec_pkt; nnew->if_ipackets = rec_pkt; nnew->if_ierrors = rec_err; nnew->if_opackets = snd_pkt; nnew->if_oerrors = snd_err; nnew->if_collisions = coll; if (scan_line_to_use == scan_line_2_2) { nnew->if_ibytes = rec_oct; nnew->if_obytes = snd_oct; } else { nnew->if_ibytes = rec_pkt*308; nnew->if_obytes = snd_pkt*308; } /* ifnames are given as `` eth0'': split in ``eth'' and ``0'': */ for (ifname = ifname_buf; *ifname && *ifname == ' '; ifname++) ; /* set name and interface# : */ nnew->if_name = (char *) strdup (ifname); for (ptr = nnew->if_name; *ptr && (*ptr < '0' || *ptr > '9'); ptr++) ; nnew->if_unit = strdup(*ptr ? ptr : ""); *ptr = 0; strcpy (ifrq.ifr_name, ifname); if (ioctl (fd, SIOCGIFADDR, &ifrq) < 0) memset ((char *) &nnew->if_addr, 0, sizeof (nnew->if_addr)); else nnew->if_addr = ifrq.ifr_addr; strcpy (ifrq.ifr_name, ifname); if (ioctl (fd, SIOCGIFBRDADDR, &ifrq) < 0) memset ((char *)&nnew->ifu_broadaddr, 0, sizeof(nnew->ifu_broadaddr)); else nnew->ifu_broadaddr = ifrq.ifr_broadaddr; strcpy (ifrq.ifr_name, ifname); if (ioctl (fd, SIOCGIFNETMASK, &ifrq) < 0) memset ((char *)&nnew->ia_subnetmask, 0, sizeof(nnew->ia_subnetmask)); else nnew->ia_subnetmask = ifrq.ifr_netmask; strcpy (ifrq.ifr_name, ifname); nnew->if_flags = ioctl (fd, SIOCGIFFLAGS, &ifrq) < 0 ? 0 : ifrq.ifr_flags; nnew->if_type = 0; strcpy (ifrq.ifr_name, ifname); if (ioctl(fd, SIOCGIFHWADDR, &ifrq) < 0) memset (nnew->if_hwaddr,(0), 6); else { memcpy (nnew->if_hwaddr, ifrq.ifr_hwaddr.sa_data, 6);#ifdef ARPHRD_LOOPBACK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -