📄 interfaces.c
字号:
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 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#ifdef ARPHRD_ATM case ARPHRD_ATM: nnew->if_type = 37; 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 /* 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)int Interface_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 */int 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 /* hpux11 */#endif /* sunV3 || linux */#if defined(hpux11)static int Interface_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 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 */}#endif /* hpux11 */static int Interface_Count=0;#if defined(hpux11)static int Interface_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;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 */}#endif /* hpux11 */#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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -