- Timestamp:
- Dec 14, 2008 1:03:29 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/linux/NetIfList-linux.cpp
r15442 r15462 38 38 #include <iprt/err.h> 39 39 #include <list> 40 #include <sys/ioctl.h> 41 #include <net/if.h> 42 #include <net/if_arp.h> 43 #include <netinet/in.h> 40 44 41 45 #include "HostNetworkInterfaceImpl.h" … … 44 48 int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list) 45 49 { 46 return VERR_NOT_IMPLEMENTED; 50 int sock = socket(AF_INET, SOCK_DGRAM, 0); 51 if (sock >= 0) 52 { 53 char pBuffer[2048]; 54 struct ifconf ifConf; 55 ifConf.ifc_len = sizeof(pBuffer); 56 ifConf.ifc_buf = pBuffer; 57 if (ioctl(sock, SIOCGIFCONF, &ifConf) >= 0) 58 { 59 for (struct ifreq *pReq = ifConf.ifc_req; (char*)pReq < pBuffer + ifConf.ifc_len; pReq++) 60 { 61 if (ioctl(sock, SIOCGIFHWADDR, pReq) >= 0) 62 { 63 if (pReq->ifr_hwaddr.sa_family == ARPHRD_ETHER) 64 { 65 NETIFINFO Info; 66 memset(&Info, 0, sizeof(Info)); 67 Info.enmType = NETIF_T_ETHERNET; 68 /* Pick up some garbage from stack. */ 69 RTUUID uuid; 70 Assert(sizeof(uuid) <= sizeof(*pReq)); 71 memcpy(uuid.Gen.au8Node, &pReq->ifr_hwaddr.sa_data, sizeof(uuid.Gen.au8Node)); 72 Info.Uuid = uuid; 73 memcpy(&Info.MACAddress, pReq->ifr_hwaddr.sa_data, sizeof(Info.MACAddress)); 74 75 if (ioctl(sock, SIOCGIFADDR, pReq) >= 0) 76 memcpy(Info.IPAddress.au8, 77 &((struct sockaddr_in *)&pReq->ifr_addr)->sin_addr.s_addr, 78 sizeof(Info.IPAddress.au8)); 79 80 if (ioctl(sock, SIOCGIFNETMASK, pReq) >= 0) 81 memcpy(Info.IPNetMask.au8, 82 &((struct sockaddr_in *)&pReq->ifr_addr)->sin_addr.s_addr, 83 sizeof(Info.IPNetMask.au8)); 84 85 if (ioctl(sock, SIOCGIFFLAGS, pReq) >= 0) 86 Info.enmStatus = pReq->ifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN; 87 88 FILE *fp = fopen("/proc/net/if_inet6", "r"); 89 if (fp) 90 { 91 RTNETADDRIPV6 IPv6Address; 92 unsigned uIndex, uLength, uScope, uTmp; 93 char szName[30]; 94 while (fscanf(fp, 95 "%08x%08x%08x%08x" 96 " %02x %02x %02x %02x %20s\n", 97 &IPv6Address.au32[0], &IPv6Address.au32[1], 98 &IPv6Address.au32[2], &IPv6Address.au32[3], 99 &uIndex, &uLength, &uScope, &uTmp, szName) != EOF) 100 { 101 if (!strcmp(pReq->ifr_name, szName)) 102 { 103 Info.IPv6Address.au32[0] = htonl(IPv6Address.au32[0]); 104 Info.IPv6Address.au32[1] = htonl(IPv6Address.au32[1]); 105 Info.IPv6Address.au32[2] = htonl(IPv6Address.au32[2]); 106 Info.IPv6Address.au32[3] = htonl(IPv6Address.au32[3]); 107 ASMBitSetRange(&Info.IPv6NetMask, 0, uLength); 108 } 109 } 110 fclose(fp); 111 } 112 ComObjPtr<HostNetworkInterface> IfObj; 113 IfObj.createObject(); 114 if (SUCCEEDED(IfObj->init(Bstr(pReq->ifr_name), &Info))) 115 list.push_back(IfObj); 116 } 117 } 118 } 119 } 120 close(sock); 121 } 122 return VINF_SUCCESS; 47 123 } 48 124
Note:
See TracChangeset
for help on using the changeset viewer.