interfaces.c
来自「Snmp(简单网管协议)软件包。」· C语言 代码 · 共 2,379 行 · 第 1/5 页
C
2,379 行
switch (ifrq.ifr_hwaddr.sa_family) { case ARPHRD_ETHER: nnew->if_type = 6; break; case ARPHRD_TUNNEL: case ARPHRD_TUNNEL6:#ifdef ARPHRD_IPGRE case ARPHRD_IPGRE:#endif case ARPHRD_SIT: nnew->if_type = 131; break; /* tunnel */ case ARPHRD_SLIP: case ARPHRD_CSLIP: case ARPHRD_SLIP6: case ARPHRD_CSLIP6: nnew->if_type = 28; break; /* slip */ case ARPHRD_PPP: nnew->if_type = 23; break; /* ppp */ case ARPHRD_LOOPBACK: nnew->if_type = 24; break; /* softwareLoopback */ case ARPHRD_FDDI: nnew->if_type = 15; break; case ARPHRD_ARCNET: nnew->if_type = 35; break; case ARPHRD_LOCALTLK: nnew->if_type = 42; break;#ifdef ARPHRD_HIPPI case ARPHRD_HIPPI: nnew->if_type = 47; break;#endif /* XXX: more if_arp.h:ARPHDR_xxx to IANAifType mappings... */ }#endif } strcpy (ifrq.ifr_name, ifname); nnew->if_metric = ioctl (fd, SIOCGIFMETRIC, &ifrq) < 0 ? 0 : ifrq.ifr_metric; #ifdef SIOCGIFMTU strcpy (ifrq.ifr_name, ifname); nnew->if_mtu = (ioctl (fd, SIOCGIFMTU, &ifrq) < 0) ? 0 : ifrq.ifr_mtu;#else nnew->if_mtu = 0;#endif for (if_ptr = conf_list; if_ptr; if_ptr = if_ptr->next) if (! strcmp (if_ptr->name, ifname)) break; if (if_ptr) { nnew->if_type = if_ptr->type; nnew->if_speed = if_ptr->speed; } else { /* do only guess if_type from name, if we could not read * it before from SIOCGIFHWADDR */ if (!nnew->if_type) nnew->if_type = if_type_from_name(nnew->if_name); nnew->if_speed = nnew->if_type == 6 ? 10000000 : nnew->if_type == 24 ? 10000000 : nnew->if_type == 9 ? 4000000 : 0; } } /* while (fgets ... */ ifnetaddr = ifnetaddr_list; if (snmp_get_do_debugging()) { { struct ifnet *x = ifnetaddr; DEBUGMSGTL(("mibII/interfaces", "* see: known interfaces:")); while (x) { DEBUGMSG(("mibII/interfaces", " %s", x->if_name)); x = x->if_next; } DEBUGMSG(("mibII/interfaces", "\n")); } /* XXX */ } fclose (devin); close (fd);#endif /* linux */}#if defined(sunV3) || defined(linux)/*** 4.2 BSD doesn't have ifaddr** */int Interface_Scan_Next(short *Index, char *Name, struct ifnet *Retifnet, struct in_ifaddr *dummy){ struct ifnet ifnet; register char *cp; while (ifnetaddr) { /* * Get the "ifnet" structure and extract the device name */#ifndef linux klookup((unsigned long)ifnetaddr, (char *)&ifnet, sizeof ifnet); klookup((unsigned long)ifnet.if_name, (char *)saveName, sizeof saveName);#else ifnet = *ifnetaddr; strcpy (saveName, ifnet.if_name);#endif if (strcmp(saveName, "ip") == 0) { ifnetaddr = ifnet.if_next; continue; } saveName[sizeof (saveName)-1] = '\0'; cp = (char *) strchr(saveName, '\0');#ifdef linux strcat (cp, ifnet.if_unit);#else string_append_int (cp, ifnet.if_unit);#endif if (1 || strcmp(saveName,"lo0") != 0) { /* XXX */ if (Index) *Index = ++saveIndex; if (Retifnet) *Retifnet = ifnet; if (Name) strcpy(Name, saveName); saveifnet = ifnet; saveifnetaddr = ifnetaddr; ifnetaddr = ifnet.if_next; return(1); /* DONE */ } ifnetaddr = ifnet.if_next; } return(0); /* EOF */}#ifdef linuxintInterface_Index_By_Name(char *Name, int Len){ short ifIndex = 0; char ifName[20]; Interface_Scan_Init(); while(Interface_Scan_Next(&ifIndex, ifName, NULL, NULL) && strcmp(Name, ifName)) ; return ifIndex;}#endif#else#if defined(netbsd1) || defined(openbsd2)#define ia_next ia_list.tqe_next#define if_next if_list.tqe_next#endifint Interface_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)); while (ia) { klookup((unsigned long)ia , (char *)&in_ifaddr, sizeof(in_ifaddr)); if (in_ifaddr.ia_ifp == ifnetaddr) { has_ipaddr = 1; /* this IF has IP-address */ break; } ia = in_ifaddr.ia_next; }#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 /* sunV3 */static int Interface_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 */}static int Interface_Count=0;static time_t scan_time = 0;static int Interface_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 int Interface_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 */}#else /* solaris2 */static int Interface_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, ret; char buf[1024]; struct ifconf ifconf; struct ifreq *ifrp; if (Name == 0) return (0); if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return (0); ifconf.ifc_buf = buf; ifconf.ifc_len = sizeof(buf); if (ioctl(sd, SIOCGIFCONF, &ifconf) == -1) { ret = 0; goto Return; } for (i = 1, ifrp = ifconf.ifc_req, ret = 0; (char *)ifrp < (char *)ifconf.ifc_buf + ifconf.ifc_len; i++, ifrp++) if (strncmp(Name, ifrp->ifr_name, Len) == 0) { ret = i; break; } else ret = 0; Return: close(sd); return (ret); /* DONE */}#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;void init_interfaces_setup(void){ int naddrs, ilen, bit; static int mib[6] = { CTL_NET, PF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 }; char *cp; size_t len; struct rt_msghdr *rtm; struct if_msghdr *ifm; struct ifa_msghdr *ifam; struct sockaddr *sa; naddrs = 0; if (physaddrs) free(physaddrs); if (physaddrbuf) free(physaddrbuf); physaddrbuf = 0; physaddrs = 0; nphysaddrs = 0; len = 0; if (sysctl(mib, 6, 0, &len, 0, 0) < 0) return; cp = physaddrbuf = malloc(len); if (physaddrbuf == 0) return; if (sysctl(mib, 6, physaddrbuf, &len, 0, 0) < 0) { free(physaddrbuf);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?