📄 kernel_sunos5.c
字号:
if (rc != MOREDATA) ret = 0; /* Found and no more data */ else ret = 1; /* Found and there is another unread data block */ break; } else { /* Restore buffers, continue search */ strbuf.buf = buf; strbuf.maxlen = BUFSIZE; } } Return: (void)ioctl(sd, I_FLUSH, FLUSHRW); DEBUGMSGTL(("kernel_sunos5", "...... getmib returns %d\n", ret)); return(ret);}/* * Get info for interfaces group. Mimics getmib interface as much as possible * to be substituted later if SunSoft decides to extend its mib2 interface. */static intgetif(mib2_ifEntry_t *ifbuf, size_t size, req_e req_type, mib2_ifEntry_t *resp, size_t *length, int (*comp)(void *, void *), void *arg){ int i, ret, idx = 1; int ifsd; static char *buf = NULL; static int bufsize = 0; struct ifconf ifconf; struct ifreq *ifrp; mib2_ifEntry_t *ifp; mib2_ipNetToMediaEntry_t Media; int nentries = size/sizeof(mib2_ifEntry_t); found_e result = NOT_FOUND; if ((ifsd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return -1; if (!buf) { bufsize = 10240; buf = malloc(bufsize); if (!buf) { ret = -1; goto Return; } } ifconf.ifc_buf = buf; ifconf.ifc_len = bufsize; while (ioctl(ifsd, SIOCGIFCONF, &ifconf) == -1) { bufsize += 10240; free(buf); buf = malloc(bufsize); if (!buf) { ret = -1; goto Return; } ifconf.ifc_buf = buf; ifconf.ifc_len = bufsize; } Again: for (i = 0, ifp = (mib2_ifEntry_t *)ifbuf, ifrp = ifconf.ifc_req; ((char *)ifrp < ((char *)ifconf.ifc_buf + ifconf.ifc_len)) && (i < nentries); i++, ifp++, ifrp++, idx++) { DEBUGMSGTL(("kernel_sunos5", "...... getif %s\n", ifrp->ifr_name)); if (ioctl(ifsd, SIOCGIFFLAGS, ifrp) < 0) { ret = -1; DEBUGMSGTL(("kernel_sunos5", "...... SIOCGIFFLAGS failed\n")); goto Return; } (void)memset(ifp, 0, sizeof(mib2_ifEntry_t)); ifp->ifIndex = idx; ifp->ifDescr.o_length = strlen(ifrp->ifr_name); (void)strcpy(ifp->ifDescr.o_bytes, ifrp->ifr_name); ifp->ifAdminStatus = (ifrp->ifr_flags & IFF_RUNNING) ? 1 : 2; ifp->ifOperStatus = (ifrp->ifr_flags & IFF_UP) ? 1 : 2; ifp->ifLastChange = 0; /* Who knows ... */ if (ioctl(ifsd, SIOCGIFMTU, ifrp) < 0) { ret = -1; DEBUGMSGTL(("kernel_sunos5", "...... SIOCGIFMTU failed\n")); goto Return; } ifp->ifMtu = ifrp->ifr_metric; ifp->ifType = 1; ifp->ifSpeed = 0; if (getKstat(ifrp->ifr_name, "ifspeed", &ifp->ifSpeed) == 0 && ifp->ifSpeed != 0) { /* check for SunOS patch with half implemented ifSpeed */ if (ifp->ifSpeed < 10000) ifp->ifSpeed *= 1000000; } else if (getKstat(ifrp->ifr_name, "ifSpeed", &ifp->ifSpeed) == 0) { /* this is good */ } switch (ifrp->ifr_name[0]) { case 'l': /* le / lo / lane (ATM LAN Emulation) */ if (ifrp->ifr_name[1] == 'o') { if (!ifp->ifSpeed) ifp->ifSpeed = 127000000; ifp->ifType = 24; } else if (ifrp->ifr_name[1] == 'e') { if (!ifp->ifSpeed) ifp->ifSpeed = 10000000; ifp->ifType = 6; } else if (ifrp->ifr_name[1] == 'a') { if (!ifp->ifSpeed) ifp->ifSpeed = 155000000; ifp->ifType = 37; } break; case 'h': /* hme (SBus card) */ case 'e': /* eri (PCI card) */ case 'b': /* be */ case 'd': /* dmfe -- found on netra X1 */ if (!ifp->ifSpeed) ifp->ifSpeed = 100000000; ifp->ifType = 6; break; case 'f': /* fa (Fore ATM */ if (!ifp->ifSpeed) ifp->ifSpeed = 155000000; ifp->ifType = 37; break; case 'q': /* qe (QuadEther) / qa (Fore ATM) / qfe (QuadFastEther) */ if (ifrp->ifr_name[1] == 'a') { if (!ifp->ifSpeed) ifp->ifSpeed = 155000000; ifp->ifType = 37; } else if (ifrp->ifr_name[1] == 'e') { if (!ifp->ifSpeed) ifp->ifSpeed = 10000000; ifp->ifType = 6; } else if (ifrp->ifr_name[1] == 'f') { if (!ifp->ifSpeed) ifp->ifSpeed = 100000000; ifp->ifType = 6; } break; } if (!strchr (ifrp->ifr_name, ':')) { Counter l_tmp; if (getKstat(ifrp->ifr_name, "ipackets", &ifp->ifInUcastPkts) < 0) { ret = -1; goto Return; } if (getKstat(ifrp->ifr_name, "rbytes", &ifp->ifInOctets) < 0) ifp->ifInOctets = ifp->ifInUcastPkts * 308; /* XXX */ if (getKstat(ifrp->ifr_name, "opackets", &ifp->ifOutUcastPkts) < 0) { ret = -1; goto Return; } if (getKstat(ifrp->ifr_name, "obytes", &ifp->ifOutOctets) < 0) ifp->ifOutOctets = ifp->ifOutUcastPkts * 308; /* XXX */ if (ifp->ifType == 24) /* Loopback */ continue; if (getKstat(ifrp->ifr_name, "ierrors", &ifp->ifInErrors) < 0) { ret = -1; goto Return; } if (getKstat(ifrp->ifr_name, "oerrors", &ifp->ifOutErrors) < 0) { ret = -1; goto Return; } if (getKstat(ifrp->ifr_name, "brdcstrcv", &ifp->ifInNUcastPkts) == 0 && getKstat(ifrp->ifr_name, "multircv", &l_tmp) == 0) ifp->ifInNUcastPkts += l_tmp; if (getKstat(ifrp->ifr_name, "brdcstxmt", &ifp->ifOutNUcastPkts) == 0 && getKstat(ifrp->ifr_name, "multixmt", &l_tmp) == 0) ifp->ifOutNUcastPkts += l_tmp; } /* * An attempt to determine the physical address of the interface. * There should be a more elegant solution using DLPI, but * "the margin is too small to put it here ..." */ if (ioctl(ifsd, SIOCGIFADDR, ifrp) < 0) { ret = -1; goto Return; } if (getMibstat(MIB_IP_NET, &Media, sizeof(mib2_ipNetToMediaEntry_t), GET_EXACT, &Name_cmp, ifrp) == 0) ifp->ifPhysAddress = Media.ipNetToMediaPhysAddress; } if ((req_type == GET_NEXT) && (result == NEED_NEXT)) /* End of buffer, so "next" is the first item in the next buffer */ req_type = GET_FIRST; result = getentry(req_type, (void *)ifbuf, size, sizeof(mib2_ifEntry_t), (void *)resp, comp, arg); if ((result != FOUND) && (i == nentries) && ((char *)ifrp < (char *)ifconf.ifc_buf + ifconf.ifc_len)) { /* * We reached the end of supplied buffer, but there is * some more stuff to read, so continue. */ ifconf.ifc_len -= i * sizeof(struct ifreq); ifconf.ifc_req = ifrp; goto Again; } if (result != FOUND) ret = 2; else { if ((char *)ifrp < (char *)ifconf.ifc_buf + ifconf.ifc_len) ret = 1; /* Found and more data to fetch */ else ret = 0; /* Found and no more data */ *length = i * sizeof(mib2_ifEntry_t); /* Actual cache length */ } Return: close(ifsd); return (ret); /* DONE */}/* Always TRUE. May be used as a comparison function in getMibstat to obtain the whole table (GET_FIRST should be used) *//*ARGSUSED */intGet_everything(void *x, void *y){ return (0); /* Always TRUE */}/* * Compare name and IP address of the interface to ARP table entry. * Needed to obtain the physical address of the interface in getif. */static intName_cmp(void *ifrp, void *ep){ struct sockaddr_in *s = (struct sockaddr_in *)&(((struct ifreq *)ifrp)->ifr_addr); mib2_ipNetToMediaEntry_t *Ep = (mib2_ipNetToMediaEntry_t *)ep; if ((strncmp(Ep->ipNetToMediaIfIndex.o_bytes, ((struct ifreq *)ifrp)->ifr_name, Ep->ipNetToMediaIfIndex.o_length) == 0) && (s->sin_addr.s_addr == Ep->ipNetToMediaNetAddress)) return (0); else return (1);} #ifdef _STDC_COMPAT#ifdef __cplusplus}#endif#endif#ifdef _GETKSTAT_TESTintmain (int argc, char **argv){ int rc = 0; u_long val = 0; if (argc != 3) { snmp_log(LOG_ERR, "Usage: %s stat_name var_name\n", argv[0]); exit(1); } snmp_set_do_debugging(1); rc = getKstat(argv[1], argv[2], &val); if (rc == 0) snmp_log(LOG_ERR, "%s = %lu\n", argv[2], val); else snmp_log(LOG_ERR, "rc =%d\n", rc); return 0;}#endif /*_GETKSTAT_TEST */#ifdef _GETMIBSTAT_TESTintip20comp(void *ifname, void *ipp){ return(strncmp((char *)ifname, ((mib2_ipAddrEntry_t *)ipp)->ipAdEntIfIndex.o_bytes, ((mib2_ipAddrEntry_t *)ipp)->ipAdEntIfIndex.o_length));}intARP_Cmp_Addr(void *addr, void *ep){ DEBUGMSGTL(("kernel_sunos5", "ARP: %lx <> %lx\n", ((mib2_ipNetToMediaEntry_t *)ep)->ipNetToMediaNetAddress, *(IpAddress *)addr)); if (((mib2_ipNetToMediaEntry_t *)ep)->ipNetToMediaNetAddress == *(IpAddress *)addr) return (0); else return (1);}intIF_cmp(void *addr, void *ep){ if (((mib2_ifEntry_t *)ep)->ifIndex == ((mib2_ifEntry_t *)addr)->ifIndex) return (0); else return (1);}intmain (int argc, char **argv){ int rc = 0, i, idx; mib2_ipAddrEntry_t ipbuf, *ipp = &ipbuf; mib2_ipNetToMediaEntry_t entry, *ep = &entry; mib2_ifEntry_t ifstat; req_e req_type; IpAddress LastAddr = 0; if (argc != 3) { snmp_log(LOG_ERR, "Usage: %s if_name req_type (0 first, 1 exact, 2 next) \n", argv[0]); exit(1); } switch (atoi(argv[2])) { case 0: req_type = GET_FIRST; break; case 1: req_type = GET_EXACT; break; case 2: req_type = GET_NEXT; break; }; snmp_set_do_debugging(0); while ((rc = getMibstat(MIB_INTERFACES, &ifstat, sizeof(mib2_ifEntry_t), req_type, &IF_cmp, &idx)) == 0) { idx = ifstat.ifIndex; DEBUGMSGTL(("kernel_sunos5", "Ifname = %s\n", ifstat.ifDescr.o_bytes)); req_type = GET_NEXT; } rc = getMibstat(MIB_IP_ADDR, &ipbuf, sizeof(mib2_ipAddrEntry_t), req_type, ip20comp, argv[1]); if (rc == 0) DEBUGMSGTL(("kernel_sunos5", "mtu = %ld\n", ipp->ipAdEntInfo.ae_mtu)); else DEBUGMSGTL(("kernel_sunos5", "rc =%d\n", rc)); while ((rc = getMibstat(MIB_IP_NET, &entry, sizeof(mib2_ipNetToMediaEntry_t), req_type, &ARP_Cmp_Addr, &LastAddr)) == 0) { LastAddr = ep->ipNetToMediaNetAddress; DEBUGMSGTL(("kernel_sunos5", "Ipaddr = %lX\n", (u_long)LastAddr)); req_type = GET_NEXT; } return 0;} #endif /*_GETMIBSTAT_TEST */#endif /* SUNOS5 *//*- * These variables describe the formatting of this file. If you don't like the * template defaults, feel free to change them here (not in your .emacs file). * * Local Variables: * comment-column: 32 * c-indent-level: 4 * c-continued-statement-offset: 4 * c-brace-offset: -4 * c-argdecl-indent: 0 * c-label-offset: -4 * fill-column: 79 * fill-prefix: " * " * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -