📄 interfaces.c
字号:
#endif#else /* sunV3 || linux */#if defined(netbsd1) || defined(openbsd2)#define ia_next ia_list.tqe_next#define if_next if_list.tqe_next#endif#if defined(hpux11)intInterface_Scan_Next(short *Index, char *Name, nmapi_phystat * Retifnet){ static nmapi_phystat *if_ptr = (nmapi_phystat *) 0; int count = Interface_Scan_Get_Count(); unsigned int ulen; int ret; if (!if_ptr) { if (count) if_ptr = (nmapi_phystat *) malloc(sizeof(nmapi_phystat) * count); else return (0); /* EOF */ } if (saveIndex >= count) return (0); /* EOF */ ulen = (unsigned int) count *sizeof(nmapi_phystat); if ((ret = get_physical_stat(if_ptr, &ulen)) < 0) return (0); /* EOF */ if (Retifnet) *Retifnet = if_ptr[saveIndex]; if (Name) strcpy(Name, if_ptr[saveIndex].nm_device); saveIndex++; if (Index) *Index = saveIndex; return (1); /* DONE */}#else /* hpux11 */intInterface_Scan_Next(short *Index, char *Name, struct ifnet *Retifnet, struct in_ifaddr *Retin_ifaddr){ struct ifnet ifnet; struct in_ifaddr *ia, in_ifaddr; short has_ipaddr = 0;#if !STRUCT_IFNET_HAS_IF_XNAME register char *cp;#endif while (ifnetaddr) { /* * Get the "ifnet" structure and extract the device name */ klookup((unsigned long) ifnetaddr, (char *) &ifnet, sizeof ifnet);#if STRUCT_IFNET_HAS_IF_XNAME#if defined(netbsd1) || defined(openbsd2) strncpy(saveName, ifnet.if_xname, sizeof saveName);#else klookup((unsigned long) ifnet.if_xname, (char *) saveName, sizeof saveName);#endif saveName[sizeof(saveName) - 1] = '\0';#else klookup((unsigned long) ifnet.if_name, (char *) saveName, sizeof saveName); saveName[sizeof(saveName) - 1] = '\0'; cp = strchr(saveName, '\0'); string_append_int(cp, ifnet.if_unit);#endif if (1 || strcmp(saveName, "lo0") != 0) { /* XXX */ /* * Try to find an address for this interface */ auto_nlist(IFADDR_SYMBOL, (char *) &ia, sizeof(ia));#ifdef netbsd1 ia = (struct in_ifaddr *) ifnet.if_addrlist.tqh_first;#endif while (ia) { klookup((unsigned long) ia, (char *) &in_ifaddr, sizeof(in_ifaddr)); {#ifdef netbsd1#define CP(x) ((char *)(x)) char *cp; struct sockaddr *sa; cp = (CP(in_ifaddr.ia_ifa.ifa_addr) - CP(ia)) + CP(&in_ifaddr); sa = (struct sockaddr *) cp; if (sa->sa_family == AF_INET)#endif if (in_ifaddr.ia_ifp == ifnetaddr) { has_ipaddr = 1; /* this IF has IP-address */ break; } }#ifdef netbsd1 ia = (struct in_ifaddr *) in_ifaddr.ia_ifa.ifa_list. tqe_next;#else ia = in_ifaddr.ia_next;#endif }#if !defined(netbsd1) && !defined(freebsd2) && !defined(openbsd2) && !defined(STRUCT_IFNET_HAS_IF_ADDRLIST) ifnet.if_addrlist = (struct ifaddr *) ia; /* WRONG DATA TYPE; ONLY A FLAG */#endif /* * ifnet.if_addrlist = (struct ifaddr *)&ia->ia_ifa; * * WRONG DATA TYPE; ONLY A FLAG */ if (Index) *Index = ++saveIndex; if (Retifnet) *Retifnet = ifnet; if (Retin_ifaddr && has_ipaddr) /* assign the in_ifaddr only * if the IF has IP-address */ *Retin_ifaddr = in_ifaddr; if (Name) strcpy(Name, saveName); saveifnet = ifnet; saveifnetaddr = ifnetaddr; savein_ifaddr = in_ifaddr; ifnetaddr = ifnet.if_next; return (1); /* DONE */ } ifnetaddr = ifnet.if_next; } return (0); /* EOF */}#endif /* hpux11 */#endif /* sunV3 || linux */#if defined(hpux11)static intInterface_Scan_By_Index(int Index, char *Name, nmapi_phystat * Retifnet){ short i; Interface_Scan_Init(); while (Interface_Scan_Next(&i, Name, Retifnet)) { if (i == Index) break; } if (i != Index) return (-1); /* Error, doesn't exist */ return (0); /* DONE */}#else /* hpux11 */static intInterface_Scan_By_Index(int Index, char *Name, struct ifnet *Retifnet, struct in_ifaddr *Retin_ifaddr){ short i; Interface_Scan_Init(); while (Interface_Scan_Next(&i, Name, Retifnet, Retin_ifaddr)) { if (i == Index) break; } if (i != Index) return (-1); /* Error, doesn't exist */ return (0); /* DONE */}#endif /* hpux11 */static int Interface_Count = 0;#if defined(hpux11)static intInterface_Scan_Get_Count(void){ if (!Interface_Count) { int fd; struct nmparms p; int val; unsigned int ulen; int ret; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) { p.objid = ID_ifNumber; p.buffer = (void *) &val; ulen = sizeof(int); p.len = &ulen; if ((ret = get_mib_info(fd, &p)) == 0) Interface_Count = val; close_mib(fd); } } return (Interface_Count);}#else /* hpux11 */static time_t scan_time = 0;intInterface_Scan_Get_Count(void){ time_t time_now = time(NULL); if (!Interface_Count || (time_now > scan_time + 60)) { scan_time = time_now; Interface_Scan_Init(); Interface_Count = 0; while (Interface_Scan_Next(NULL, NULL, NULL, NULL) != 0) { Interface_Count++; } } return (Interface_Count);}static intInterface_Get_Ether_By_Index(int Index, u_char * EtherAddr){ short i;#if !(defined(linux) || defined(netbsd1) || defined(bsdi2) || defined(openbsd2)) struct arpcom arpcom;#else /* is linux or netbsd1 */ struct arpcom { char ac_enaddr[6]; } arpcom;#if defined(netbsd1) || defined(bsdi2) || defined(openbsd2) struct sockaddr_dl sadl; struct ifaddr ifaddr; u_long ifaddraddr;#endif#endif#if defined(mips) || defined(hpux) || defined(osf4) || defined(osf3) memset(arpcom.ac_enaddr, 0, sizeof(arpcom.ac_enaddr));#else memset(&arpcom.ac_enaddr, 0, sizeof(arpcom.ac_enaddr));#endif memset(EtherAddr, 0, sizeof(arpcom.ac_enaddr)); if (saveIndex != Index) { /* Optimization! */ Interface_Scan_Init(); while (Interface_Scan_Next((short *) &i, NULL, NULL, NULL) != 0) { if (i == Index) break; } if (i != Index) return (-1); /* Error, doesn't exist */ }#ifdef freebsd2 if (saveifnet.if_type != IFT_ETHER) { return (0); /* Not an ethernet if */ }#endif /* * the arpcom structure is an extended ifnet structure which * contains the ethernet address. */#ifndef linux#if !(defined(netbsd1) || defined(bsdi2) || defined(openbsd2)) klookup((unsigned long) saveifnetaddr, (char *) &arpcom, sizeof arpcom);#else /* netbsd1 or bsdi2 or openbsd2 */#if defined(netbsd1) || defined(openbsd2)#define if_addrlist if_addrlist.tqh_first#define ifa_next ifa_list.tqe_next#endif ifaddraddr = (unsigned long) saveifnet.if_addrlist; while (ifaddraddr) { klookup(ifaddraddr, (char *) &ifaddr, sizeof ifaddr); klookup((unsigned long) ifaddr.ifa_addr, (char *) &sadl, sizeof sadl); if (sadl.sdl_family == AF_LINK && (saveifnet.if_type == IFT_ETHER || saveifnet.if_type == IFT_ISO88025 || saveifnet.if_type == IFT_FDDI)) { memcpy(arpcom.ac_enaddr, sadl.sdl_data + sadl.sdl_nlen, sizeof(arpcom.ac_enaddr)); break; } ifaddraddr = (unsigned long) ifaddr.ifa_next; }#endif /* netbsd1 or bsdi2 or openbsd2 */#else /* linux */ memcpy(arpcom.ac_enaddr, saveifnetaddr->if_hwaddr, 6);#endif if (strncmp("lo", saveName, 2) == 0) { /* * Loopback doesn't have a HW addr, so return 00:00:00:00:00:00 */ memset(EtherAddr, 0, sizeof(arpcom.ac_enaddr)); } else {#if defined(mips) || defined(hpux) || defined(osf4) || defined(osf3) memcpy(EtherAddr, (char *) arpcom.ac_enaddr, sizeof(arpcom.ac_enaddr));#else memcpy(EtherAddr, (char *) &arpcom.ac_enaddr, sizeof(arpcom.ac_enaddr));#endif } return (0); /* DONE */}#endif /* hpux11 */#else /* solaris2 */intInterface_Scan_Get_Count(void){ int i, sd; if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return (0); if (ioctl(sd, SIOCGIFNUM, &i) == -1) { close(sd); return (0); } else { close(sd); return (i); }}intInterface_Index_By_Name(char *Name, int Len){ int i, sd, lastlen = 0, interfaces = 0; struct ifconf ifc; struct ifreq *ifrp = NULL; char *buf = NULL; if (Name == 0) { return 0; } if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return 0; } /* * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF * on some platforms; see W. R. Stevens, ``Unix Network Programming * Volume I'', p.435. */ for (i = 8;; i += 8) { buf = calloc(i, sizeof(struct ifreq)); if (buf == NULL) { close(sd); return 0; } ifc.ifc_len = i * sizeof(struct ifreq); ifc.ifc_buf = (caddr_t) buf; if (ioctl(sd, SIOCGIFCONF, (char *) &ifc) < 0) { if (errno != EINVAL || lastlen != 0) { /* * Something has gone genuinely wrong. */ free(buf); close(sd); return 0; } /* * Otherwise, it could just be that the buffer is too small. */ } else { if (ifc.ifc_len == lastlen) { /* * The length is the same as the last time; we're done. */ break; } lastlen = ifc.ifc_len; } free(buf); } ifrp = ifc.ifc_req; interfaces = (ifc.ifc_len / sizeof(struct ifreq)) + 1; for (i = 1; i < interfaces; i++, ifrp++) { if (strncmp(ifrp->ifr_name, Name, Len) == 0) { free(buf); close(sd); return i; } } free(buf); close(sd); return 0;}#endif /* solaris2 */#else /* HAVE_NET_IF_MIB_H *//* * This code attempts to do the right thing for FreeBSD. Note that * the statistics could be gathered through use of of the * net.route.0.link.iflist.0 sysctl (which we already use to get the * hardware address of the interfaces), rather than using the ifmib * code, but eventually I will implement dot3Stats and we will have to * use the ifmib interface. ifmib is also a much more natural way of * mapping the SNMP MIB onto sysctl(3). */#include <net/if.h>#include <net/if_dl.h>#include <net/if_mib.h>#include <net/route.h>static int header_interfaces(struct variable *, oid *, size_t *, int, size_t *, WriteMethod ** write);static int header_ifEntry(struct variable *, oid *, size_t *, int, size_t *, WriteMethod ** write);u_char *var_ifEntry(struct variable *, oid *, size_t *, int, size_t *, WriteMethod ** write);static char *physaddrbuf;static int nphysaddrs;struct sockaddr_dl **physaddrs;voidinit_interfaces_setup(void){ int naddrs, ilen, bit; static int mib[6] =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -